blob: e4462d83052cf43bf3ee09367da436cc32fda3f9 [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",
Brian Carlstrom78128a62011-09-15 17:21:19 -070092};
93
Mathieu Chartierac8f4392015-08-27 13:54:20 -070094// Map is so that we don't allocate multiple dex files for the same OatDexFile.
95static std::map<const OatFile::OatDexFile*,
96 std::unique_ptr<const DexFile>> opened_dex_files;
97
98const DexFile* OpenDexFile(const OatFile::OatDexFile* oat_dex_file, std::string* error_msg) {
99 DCHECK(oat_dex_file != nullptr);
100 auto it = opened_dex_files.find(oat_dex_file);
101 if (it != opened_dex_files.end()) {
102 return it->second.get();
103 }
104 const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
105 opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
106 return ret;
107}
108
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800109template <typename ElfTypes>
David Srbeckybc90fd02015-04-22 19:40:27 +0100110class OatSymbolizer FINAL {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700111 public:
David Srbecky2fdd03c2016-03-10 15:32:37 +0000112 OatSymbolizer(const OatFile* oat_file, const std::string& output_name, bool no_bits) :
113 oat_file_(oat_file),
114 builder_(nullptr),
115 output_name_(output_name.empty() ? "symbolized.oat" : output_name),
116 no_bits_(no_bits) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700117 }
118
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700119 bool Symbolize() {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000120 const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800121 std::unique_ptr<const InstructionSetFeatures> features = InstructionSetFeatures::FromBitmap(
David Srbecky5d811202016-03-08 13:21:22 +0000122 isa, oat_file_->GetOatHeader().GetInstructionSetFeaturesBitmap());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000123
124 File* elf_file = OS::CreateEmptyFile(output_name_.c_str());
125 std::unique_ptr<BufferedOutputStream> output_stream(
Vladimir Marko10c13562015-11-25 14:33:36 +0000126 MakeUnique<BufferedOutputStream>(MakeUnique<FileOutputStream>(elf_file)));
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800127 builder_.reset(new ElfBuilder<ElfTypes>(isa, features.get(), output_stream.get()));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000128
129 builder_->Start();
130
131 auto* rodata = builder_->GetRoData();
132 auto* text = builder_->GetText();
133 auto* bss = builder_->GetBss();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000134
David Srbecky6d8c8f02015-10-26 10:57:09 +0000135 const uint8_t* rodata_begin = oat_file_->Begin();
136 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
David Srbecky2fdd03c2016-03-10 15:32:37 +0000137 if (no_bits_) {
138 rodata->WriteNoBitsSection(rodata_size);
139 } else {
140 rodata->Start();
141 rodata->WriteFully(rodata_begin, rodata_size);
142 rodata->End();
143 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000144
David Srbecky6d8c8f02015-10-26 10:57:09 +0000145 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
146 const size_t text_size = oat_file_->End() - text_begin;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000147 if (no_bits_) {
148 text->WriteNoBitsSection(text_size);
149 } else {
150 text->Start();
151 text->WriteFully(text_begin, text_size);
152 text->End();
153 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000154
155 if (oat_file_->BssSize() != 0) {
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000156 bss->WriteNoBitsSection(oat_file_->BssSize());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000157 }
158
Douglas Leung316a2182015-09-17 15:26:25 -0700159 if (isa == kMips || isa == kMips64) {
160 builder_->WriteMIPSabiflagsSection();
161 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000162 builder_->PrepareDynamicSection(elf_file->GetPath(),
163 rodata_size,
164 text_size,
165 oat_file_->BssSize(),
166 oat_file_->BssRootsOffset());
Vladimir Marko944da602016-02-19 12:27:55 +0000167 builder_->WriteDynamicSection();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700168
David Srbecky5d950762016-03-07 20:47:29 +0000169 Walk();
170 for (const auto& trampoline : debug::MakeTrampolineInfos(oat_file_->GetOatHeader())) {
171 method_debug_infos_.push_back(trampoline);
172 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700173
David Srbecky5d950762016-03-07 20:47:29 +0000174 debug::WriteDebugInfo(builder_.get(),
175 ArrayRef<const debug::MethodDebugInfo>(method_debug_infos_),
176 dwarf::DW_DEBUG_FRAME_FORMAT,
177 true /* write_oat_patches */);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700178
David Srbecky6d8c8f02015-10-26 10:57:09 +0000179 builder_->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700180
Vladimir Marko10c13562015-11-25 14:33:36 +0000181 return builder_->Good();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700182 }
183
David Srbecky5d950762016-03-07 20:47:29 +0000184 void Walk() {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700185 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
186 for (size_t i = 0; i < oat_dex_files.size(); i++) {
187 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700188 CHECK(oat_dex_file != nullptr);
David Srbecky5d950762016-03-07 20:47:29 +0000189 WalkOatDexFile(oat_dex_file);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700190 }
191 }
192
David Srbecky5d950762016-03-07 20:47:29 +0000193 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700194 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700195 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
196 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700197 return;
198 }
199 for (size_t class_def_index = 0;
200 class_def_index < dex_file->NumClassDefs();
201 class_def_index++) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700202 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
203 OatClassType type = oat_class.GetType();
204 switch (type) {
205 case kOatClassAllCompiled:
206 case kOatClassSomeCompiled:
David Srbecky5d950762016-03-07 20:47:29 +0000207 WalkOatClass(oat_class, *dex_file, class_def_index);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700208 break;
209
210 case kOatClassNoneCompiled:
211 case kOatClassMax:
212 // Ignore.
213 break;
214 }
215 }
216 }
217
David Srbecky5d950762016-03-07 20:47:29 +0000218 void WalkOatClass(const OatFile::OatClass& oat_class,
219 const DexFile& dex_file,
220 uint32_t class_def_index) {
221 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700222 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700223 if (class_data == nullptr) { // empty class such as a marker interface?
224 return;
225 }
226 // Note: even if this is an interface or a native class, we still have to walk it, as there
227 // might be a static initializer.
228 ClassDataItemIterator it(dex_file, class_data);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700229 uint32_t class_method_idx = 0;
David Srbecky5d950762016-03-07 20:47:29 +0000230 for (; it.HasNextStaticField(); it.Next()) { /* skip */ }
231 for (; it.HasNextInstanceField(); it.Next()) { /* skip */ }
232 for (; it.HasNextDirectMethod() || it.HasNextVirtualMethod(); it.Next()) {
233 WalkOatMethod(oat_class.GetOatMethod(class_method_idx++),
234 dex_file,
235 class_def_index,
236 it.GetMemberIndex(),
237 it.GetMethodCodeItem(),
238 it.GetMethodAccessFlags());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700239 }
240 DCHECK(!it.HasNext());
241 }
242
David Srbecky5d950762016-03-07 20:47:29 +0000243 void WalkOatMethod(const OatFile::OatMethod& oat_method,
244 const DexFile& dex_file,
245 uint32_t class_def_index,
246 uint32_t dex_method_index,
247 const DexFile::CodeItem* code_item,
248 uint32_t method_access_flags) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700249 if ((method_access_flags & kAccAbstract) != 0) {
250 // Abstract method, no code.
251 return;
252 }
David Srbecky5d950762016-03-07 20:47:29 +0000253 const OatHeader& oat_header = oat_file_->GetOatHeader();
254 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
255 if (method_header == nullptr || method_header->GetCodeSize() == 0) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700256 // No code.
257 return;
258 }
259
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100260 uint32_t entry_point = oat_method.GetCodeOffset() - oat_header.GetExecutableOffset();
261 // Clear Thumb2 bit.
262 const void* code_address = EntryPointToCodePointer(reinterpret_cast<void*>(entry_point));
263
David Srbecky5d950762016-03-07 20:47:29 +0000264 debug::MethodDebugInfo info = debug::MethodDebugInfo();
265 info.trampoline_name = nullptr;
266 info.dex_file = &dex_file;
267 info.class_def_index = class_def_index;
268 info.dex_method_index = dex_method_index;
269 info.access_flags = method_access_flags;
270 info.code_item = code_item;
271 info.isa = oat_header.GetInstructionSet();
272 info.deduped = !seen_offsets_.insert(oat_method.GetCodeOffset()).second;
273 info.is_native_debuggable = oat_header.IsNativeDebuggable();
274 info.is_optimized = method_header->IsOptimized();
275 info.is_code_address_text_relative = true;
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100276 info.code_address = reinterpret_cast<uintptr_t>(code_address);
David Srbecky5d950762016-03-07 20:47:29 +0000277 info.code_size = method_header->GetCodeSize();
278 info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
279 info.code_info = info.is_optimized ? method_header->GetOptimizedCodeInfoPtr() : nullptr;
280 info.cfi = ArrayRef<uint8_t>();
281 method_debug_infos_.push_back(info);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700282 }
283
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700284 private:
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700285 const OatFile* oat_file_;
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800286 std::unique_ptr<ElfBuilder<ElfTypes> > builder_;
David Srbecky5d950762016-03-07 20:47:29 +0000287 std::vector<debug::MethodDebugInfo> method_debug_infos_;
288 std::unordered_set<uint32_t> seen_offsets_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700289 const std::string output_name_;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000290 bool no_bits_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700291};
292
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700293class OatDumperOptions {
294 public:
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100295 OatDumperOptions(bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100296 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700297 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700298 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800299 const char* class_filter,
300 const char* method_filter,
301 bool list_classes,
302 bool list_methods,
David Brazdilc03d7b62016-03-02 12:18:03 +0000303 bool dump_header_only,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800304 const char* export_dex_location,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800305 const char* app_image,
306 const char* app_oat,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800307 uint32_t addr2instr)
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100308 : dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100309 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700310 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700311 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800312 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000313 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800314 list_classes_(list_classes),
315 list_methods_(list_methods),
David Brazdilc03d7b62016-03-02 12:18:03 +0000316 dump_header_only_(dump_header_only),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800317 export_dex_location_(export_dex_location),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800318 app_image_(app_image),
319 app_oat_(app_oat),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800320 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800321 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700322
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700323 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100324 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700325 const bool disassemble_code_;
326 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800327 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000328 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800329 const bool list_classes_;
330 const bool list_methods_;
David Brazdilc03d7b62016-03-02 12:18:03 +0000331 const bool dump_header_only_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800332 const char* const export_dex_location_;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800333 const char* const app_image_;
334 const char* const app_oat_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800335 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700336 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700337};
338
Elliott Hughese3c845c2012-02-28 17:23:01 -0800339class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700340 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100341 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000342 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800343 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700344 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800345 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800346 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
347 disassembler_(Disassembler::Create(instruction_set_,
Andreas Gampe372f3a32016-08-19 10:49:06 -0700348 new DisassemblerOptions(
349 options_.absolute_addresses_,
350 oat_file.Begin(),
351 oat_file.End(),
352 true /* can_read_literals_ */,
353 Is64BitInstructionSet(instruction_set_)
354 ? &Thread::DumpThreadOffset<PointerSize::k64>
355 : &Thread::DumpThreadOffset<PointerSize::k32>))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800356 CHECK(options_.class_loader_ != nullptr);
357 CHECK(options_.class_filter_ != nullptr);
358 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800359 AddAllOffsets();
360 }
361
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700362 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700363 delete disassembler_;
364 }
365
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800366 InstructionSet GetInstructionSet() {
367 return instruction_set_;
368 }
369
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700370 bool Dump(std::ostream& os) {
371 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800372 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700373
374 os << "MAGIC:\n";
375 os << oat_header.GetMagic() << "\n\n";
376
Jeff Hao4b07a6e2016-01-15 12:50:44 -0800377 os << "LOCATION:\n";
378 os << oat_file_.GetLocation() << "\n\n";
379
Brian Carlstromaded5f72011-10-07 17:15:04 -0700380 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800381 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700382
Elliott Hughesa72ec822012-03-05 17:12:22 -0800383 os << "INSTRUCTION SET:\n";
384 os << oat_header.GetInstructionSet() << "\n\n";
385
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700386 {
387 std::unique_ptr<const InstructionSetFeatures> features(
388 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
389 oat_header.GetInstructionSetFeaturesBitmap()));
390 os << "INSTRUCTION SET FEATURES:\n";
391 os << features->GetFeatureString() << "\n\n";
392 }
Dave Allison70202782013-10-22 17:52:19 -0700393
Brian Carlstromaded5f72011-10-07 17:15:04 -0700394 os << "DEX FILE COUNT:\n";
395 os << oat_header.GetDexFileCount() << "\n\n";
396
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800397#define DUMP_OAT_HEADER_OFFSET(label, offset) \
398 os << label " OFFSET:\n"; \
399 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800400 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800401 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
402 } \
403 os << StringPrintf("\n\n");
404
405 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
406 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
407 GetInterpreterToInterpreterBridgeOffset);
408 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
409 GetInterpreterToCompiledCodeBridgeOffset);
410 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
411 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800412 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
413 GetQuickGenericJniTrampolineOffset);
414 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
415 GetQuickImtConflictTrampolineOffset);
416 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
417 GetQuickResolutionTrampolineOffset);
418 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
419 GetQuickToInterpreterBridgeOffset);
420#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700421
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700422 os << "IMAGE PATCH DELTA:\n";
423 os << StringPrintf("%d (0x%08x)\n\n",
424 oat_header.GetImagePatchDelta(),
425 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700426
Brian Carlstrom28db0122012-10-18 16:20:41 -0700427 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
428 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
429
430 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800431 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700432
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700433 // Print the key-value store.
434 {
435 os << "KEY VALUE STORE:\n";
436 size_t index = 0;
437 const char* key;
438 const char* value;
439 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
440 os << key << " = " << value << "\n";
441 index++;
442 }
443 os << "\n";
444 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700445
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800446 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700447 os << "BEGIN:\n";
448 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700449
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700450 os << "END:\n";
451 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
452 }
453
454 os << "SIZE:\n";
455 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700456
457 os << std::flush;
458
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800459 // If set, adjust relative address to be searched
460 if (options_.addr2instr_ != 0) {
461 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
462 os << "SEARCH ADDRESS (executable offset + input):\n";
463 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
464 }
465
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700466 // Dumping the dex file overview is compact enough to do even if header only.
467 DexFileData cumulative;
468 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
469 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
470 CHECK(oat_dex_file != nullptr);
471 std::string error_msg;
472 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
473 if (dex_file == nullptr) {
474 os << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation() << "': "
475 << error_msg;
476 continue;
477 }
478 DexFileData data(*dex_file);
479 os << "Dex file data for " << dex_file->GetLocation() << "\n";
480 data.Dump(os);
481 os << "\n";
482 cumulative.Add(data);
483 }
484 os << "Cumulative dex file data\n";
485 cumulative.Dump(os);
486 os << "\n";
487
David Brazdilc03d7b62016-03-02 12:18:03 +0000488 if (!options_.dump_header_only_) {
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000489 VariableIndentationOutputStream vios(&os);
490 VdexFile::Header vdex_header = oat_file_.GetVdexFile()->GetHeader();
491 if (vdex_header.IsValid()) {
492 std::string error_msg;
493 std::vector<const DexFile*> dex_files;
494 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
495 const DexFile* dex_file = OpenDexFile(oat_dex_files_[i], &error_msg);
496 if (dex_file == nullptr) {
497 os << "Error opening dex file: " << error_msg << std::endl;
498 return false;
499 }
500 dex_files.push_back(dex_file);
501 }
502 verifier::VerifierDeps deps(dex_files, oat_file_.GetVdexFile()->GetVerifierDepsData());
503 deps.Dump(&vios);
504 } else {
505 os << "UNRECOGNIZED vdex file, magic "
506 << vdex_header.GetMagic()
507 << ", version "
508 << vdex_header.GetVersion()
509 << "\n";
510 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000511 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
512 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
513 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800514
David Brazdilc03d7b62016-03-02 12:18:03 +0000515 // If file export selected skip file analysis
516 if (options_.export_dex_location_) {
517 if (!ExportDexFile(os, *oat_dex_file)) {
518 success = false;
519 }
520 } else {
521 if (!DumpOatDexFile(os, *oat_dex_file)) {
522 success = false;
523 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800524 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700525 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700526 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000527
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700528 os << std::flush;
529 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700530 }
531
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800532 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700533 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
534 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800535 return 0; // Address not in oat file
536 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800537 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
538 reinterpret_cast<uintptr_t>(oat_file_.Begin());
539 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800540 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800541 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800542 return end_offset - begin_offset;
543 }
544
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800545 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700546 return oat_file_.GetOatHeader().GetInstructionSet();
547 }
548
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700549 const void* GetQuickOatCode(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800550 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
551 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700552 CHECK(oat_dex_file != nullptr);
553 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700554 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
555 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700556 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
557 << "': " << error_msg;
558 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800559 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700560 const DexFile::ClassDef* class_def =
David Sehr9aa352e2016-09-15 18:13:52 -0700561 OatDexFile::FindClassDef(*dex_file, descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700562 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700563 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100564 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800565 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100566 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800567 }
568 }
569 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700570 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800571 }
572
Brian Carlstromaded5f72011-10-07 17:15:04 -0700573 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800574 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800575 // We don't know the length of the code for each method, but we need to know where to stop
576 // when disassembling. What we do know is that a region of code will be followed by some other
577 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
578 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800579 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
580 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700581 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700582 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700583 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
584 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700585 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
586 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800587 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800588 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800589 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800590 for (size_t class_def_index = 0;
591 class_def_index < dex_file->NumClassDefs();
592 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800593 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100594 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700595 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700596 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800597 ClassDataItemIterator it(*dex_file, class_data);
598 SkipAllFields(it);
599 uint32_t class_method_index = 0;
600 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100601 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800602 it.Next();
603 }
604 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100605 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800606 it.Next();
607 }
608 }
609 }
610 }
611
612 // If the last thing in the file is code for a method, there won't be an offset for the "next"
613 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
614 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800615 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800616 }
617
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700618 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
619 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
620 }
621
Elliott Hughese3c845c2012-02-28 17:23:01 -0800622 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800623 uint32_t code_offset = oat_method.GetCodeOffset();
624 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
625 code_offset &= ~0x1;
626 }
627 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800628 offsets_.insert(oat_method.GetVmapTableOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800629 }
630
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700631 // Dex file data, may be for multiple different dex files.
632 class DexFileData {
633 public:
634 DexFileData() {}
635
636 explicit DexFileData(const DexFile& dex_file)
637 : num_string_ids_(dex_file.NumStringIds()),
638 num_method_ids_(dex_file.NumMethodIds()),
639 num_field_ids_(dex_file.NumFieldIds()),
640 num_type_ids_(dex_file.NumTypeIds()),
641 num_class_defs_(dex_file.NumClassDefs()) {
642 for (size_t class_def_index = 0; class_def_index < num_class_defs_; ++class_def_index) {
643 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
644 WalkClass(dex_file, class_def);
645 }
646 }
647
648 void Add(const DexFileData& other) {
649 AddAll(unique_string_ids_from_code_, other.unique_string_ids_from_code_);
650 num_string_ids_from_code_ += other.num_string_ids_from_code_;
651 AddAll(dex_code_item_ptrs_, other.dex_code_item_ptrs_);
652 dex_code_bytes_ += other.dex_code_bytes_;
653 num_string_ids_ += other.num_string_ids_;
654 num_method_ids_ += other.num_method_ids_;
655 num_field_ids_ += other.num_field_ids_;
656 num_type_ids_ += other.num_type_ids_;
657 num_class_defs_ += other.num_class_defs_;
658 }
659
660 void Dump(std::ostream& os) {
661 os << "Num string ids: " << num_string_ids_ << "\n";
662 os << "Num method ids: " << num_method_ids_ << "\n";
663 os << "Num field ids: " << num_field_ids_ << "\n";
664 os << "Num type ids: " << num_type_ids_ << "\n";
665 os << "Num class defs: " << num_class_defs_ << "\n";
666 os << "Unique strings loaded from dex code: " << unique_string_ids_from_code_.size() << "\n";
667 os << "Total strings loaded from dex code: " << num_string_ids_from_code_ << "\n";
668 os << "Number of unique dex code items: " << dex_code_item_ptrs_.size() << "\n";
669 os << "Total number of dex code bytes: " << dex_code_bytes_ << "\n";
670 }
671
672 private:
Andreas Gampe9186ced2016-12-12 14:28:21 -0800673 // All of the elements from one container to another.
674 template <typename Dest, typename Src>
675 static void AddAll(Dest& dest, const Src& src) {
676 dest.insert(src.begin(), src.end());
677 }
678
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700679 void WalkClass(const DexFile& dex_file, const DexFile::ClassDef& class_def) {
680 const uint8_t* class_data = dex_file.GetClassData(class_def);
681 if (class_data == nullptr) { // empty class such as a marker interface?
682 return;
683 }
684 ClassDataItemIterator it(dex_file, class_data);
685 SkipAllFields(it);
686 while (it.HasNextDirectMethod()) {
687 WalkCodeItem(dex_file, it.GetMethodCodeItem());
688 it.Next();
689 }
690 while (it.HasNextVirtualMethod()) {
691 WalkCodeItem(dex_file, it.GetMethodCodeItem());
692 it.Next();
693 }
694 DCHECK(!it.HasNext());
695 }
696
697 void WalkCodeItem(const DexFile& dex_file, const DexFile::CodeItem* code_item) {
698 if (code_item == nullptr) {
699 return;
700 }
701 const size_t code_item_size = code_item->insns_size_in_code_units_;
702 const uint16_t* code_ptr = code_item->insns_;
703 const uint16_t* code_end = code_item->insns_ + code_item_size;
704
705 // If we inserted a new dex code item pointer, add to total code bytes.
706 if (dex_code_item_ptrs_.insert(code_ptr).second) {
707 dex_code_bytes_ += code_item_size * sizeof(code_ptr[0]);
708 }
709
710 while (code_ptr < code_end) {
711 const Instruction* inst = Instruction::At(code_ptr);
712 switch (inst->Opcode()) {
713 case Instruction::CONST_STRING: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800714 const dex::StringIndex string_index(inst->VRegB_21c());
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700715 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
716 ++num_string_ids_from_code_;
717 break;
718 }
719 case Instruction::CONST_STRING_JUMBO: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800720 const dex::StringIndex string_index(inst->VRegB_31c());
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700721 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
722 ++num_string_ids_from_code_;
723 break;
724 }
725 default:
726 break;
727 }
728
729 code_ptr += inst->SizeInCodeUnits();
730 }
731 }
732
733 // Unique string ids loaded from dex code.
734 std::set<StringReference, StringReferenceComparator> unique_string_ids_from_code_;
735
736 // Total string ids loaded from dex code.
737 size_t num_string_ids_from_code_ = 0;
738
739 // Unique code pointers.
740 std::set<const void*> dex_code_item_ptrs_;
741
742 // Total "unique" dex code bytes.
743 size_t dex_code_bytes_ = 0;
744
745 // Other dex ids.
746 size_t num_string_ids_ = 0;
747 size_t num_method_ids_ = 0;
748 size_t num_field_ids_ = 0;
749 size_t num_type_ids_ = 0;
750 size_t num_class_defs_ = 0;
751 };
752
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700753 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
754 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800755 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700756 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800757 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800758 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700759
Andreas Gampe2ba88952016-04-29 17:52:07 -0700760 const uint8_t* const oat_file_begin = oat_dex_file.GetOatFile()->Begin();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100761 const uint8_t* const vdex_file_begin = oat_dex_file.GetOatFile()->DexBegin();
762
763 // Print data range of the dex file embedded inside the corresponding vdex file.
Andreas Gampe2ba88952016-04-29 17:52:07 -0700764 const uint8_t* const dex_file_pointer = oat_dex_file.GetDexFilePointer();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100765 uint32_t dex_offset = dchecked_integral_cast<uint32_t>(dex_file_pointer - vdex_file_begin);
Andreas Gampe2ba88952016-04-29 17:52:07 -0700766 os << StringPrintf("dex-file: 0x%08x..0x%08x\n",
767 dex_offset,
768 dchecked_integral_cast<uint32_t>(dex_offset + oat_dex_file.FileSize() - 1));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700769
Andreas Gampe2ba88952016-04-29 17:52:07 -0700770 // Create the dex file early. A lot of print-out things depend on it.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700771 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700772 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
773 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700774 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700775 os << std::flush;
776 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700777 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100778
Andreas Gampe2ba88952016-04-29 17:52:07 -0700779 // Print lookup table, if it exists.
780 if (oat_dex_file.GetLookupTableData() != nullptr) {
781 uint32_t table_offset = dchecked_integral_cast<uint32_t>(
782 oat_dex_file.GetLookupTableData() - oat_file_begin);
David Sehr9aa352e2016-09-15 18:13:52 -0700783 uint32_t table_size = TypeLookupTable::RawDataLength(dex_file->NumClassDefs());
Andreas Gampe2ba88952016-04-29 17:52:07 -0700784 os << StringPrintf("type-table: 0x%08x..0x%08x\n",
785 table_offset,
786 table_offset + table_size - 1);
787 }
788
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100789 VariableIndentationOutputStream vios(&os);
790 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800791 for (size_t class_def_index = 0;
792 class_def_index < dex_file->NumClassDefs();
793 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700794 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
795 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800796
797 // TODO: Support regex
798 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
799 continue;
800 }
801
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700802 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100803 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700804 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
Andreas Gampea5b09a62016-11-17 15:21:22 -0800805 class_def_index, descriptor, oat_class_offset, class_def.class_idx_.index_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100806 << " (" << oat_class.GetStatus() << ")"
807 << " (" << oat_class.GetType() << ")\n";
808 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700809 if (options_.list_classes_) {
810 continue;
811 }
812 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700813 success = false;
814 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800815 if (stop_analysis) {
816 os << std::flush;
817 return success;
818 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700819 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700820 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700821 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700822 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700823 }
824
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800825 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
826 std::string error_msg;
827 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
828
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700829 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800830 if (dex_file == nullptr) {
831 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
832 return false;
833 }
834 size_t fsize = oat_dex_file.FileSize();
835
836 // Some quick checks just in case
837 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
838 os << "Invalid dex file\n";
839 return false;
840 }
841
842 // Verify output directory exists
843 if (!OS::DirectoryExists(options_.export_dex_location_)) {
844 // TODO: Extend OS::DirectoryExists if symlink support is required
845 os << options_.export_dex_location_ << " output directory not found or symlink\n";
846 return false;
847 }
848
849 // Beautify path names
850 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
851 return false;
852 }
853
854 std::string dex_orig_name;
855 size_t dex_orig_pos = dex_file_location.rfind('/');
856 if (dex_orig_pos == std::string::npos)
857 dex_orig_name = dex_file_location;
858 else
859 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
860
861 // A more elegant approach to efficiently name user installed apps is welcome
862 if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
863 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
864 size_t apk_orig_pos = dex_file_location.rfind('/');
865 if (apk_orig_pos != std::string::npos) {
866 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
867 }
868 }
869
870 std::string out_dex_path(options_.export_dex_location_);
871 if (out_dex_path.back() != '/') {
872 out_dex_path.append("/");
873 }
874 out_dex_path.append(dex_orig_name);
875 out_dex_path.append("_export.dex");
876 if (out_dex_path.length() > PATH_MAX) {
877 return false;
878 }
879
880 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
881 if (file.get() == nullptr) {
882 os << "Failed to open output dex file " << out_dex_path;
883 return false;
884 }
885
886 if (!file->WriteFully(dex_file->Begin(), fsize)) {
887 os << "Failed to write dex file";
888 file->Erase();
889 return false;
890 }
891
892 if (file->FlushCloseOrErase() != 0) {
893 os << "Flush and close failed";
894 return false;
895 }
896
897 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
898 os << std::flush;
899
900 return true;
901 }
902
Elliott Hughese3c845c2012-02-28 17:23:01 -0800903 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700904 while (it.HasNextStaticField()) {
905 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700906 }
Ian Rogers0571d352011-11-03 19:51:38 -0700907 while (it.HasNextInstanceField()) {
908 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700909 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800910 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700911
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100912 bool DumpOatClass(VariableIndentationOutputStream* vios,
913 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700914 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700915 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800916 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -0700917 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700918 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100919 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700920 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800921 }
922 ClassDataItemIterator it(dex_file, class_data);
923 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700924 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700925 while (it.HasNextDirectMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100926 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700927 it.GetMemberIndex(), it.GetMethodCodeItem(),
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700928 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700929 success = false;
930 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800931 if (addr_found) {
932 *stop_analysis = true;
933 return success;
934 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700935 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700936 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700937 }
Ian Rogers0571d352011-11-03 19:51:38 -0700938 while (it.HasNextVirtualMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100939 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700940 it.GetMemberIndex(), it.GetMethodCodeItem(),
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700941 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700942 success = false;
943 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800944 if (addr_found) {
945 *stop_analysis = true;
946 return success;
947 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700948 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700949 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700950 }
Ian Rogers0571d352011-11-03 19:51:38 -0700951 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100952 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700953 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700954 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800955
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700956 static constexpr uint32_t kPrologueBytes = 16;
957
958 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
959 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
960
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100961 bool DumpOatMethod(VariableIndentationOutputStream* vios,
962 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700963 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700964 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800965 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700966 uint32_t method_access_flags, bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700967 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800968
969 // TODO: Support regex
970 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
971 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000972 return success;
973 }
974
David Sehr709b0702016-10-13 09:12:37 -0700975 std::string pretty_method = dex_file.PrettyMethod(dex_method_idx, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100976 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
977 class_method_index, pretty_method.c_str(),
978 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800979 if (options_.list_methods_) return success;
980
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800981 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
982 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
983 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
984 uint32_t code_offset = oat_method.GetCodeOffset();
985 uint32_t code_size = oat_method.GetQuickCodeSize();
986 if (resolved_addr2instr_ != 0) {
987 if (resolved_addr2instr_ > code_offset + code_size) {
988 return success;
989 } else {
990 *addr_found = true; // stop analyzing file at next iteration
991 }
992 }
993
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100994 // Everything below is indented at least once.
995 ScopedIndentation indent1(vios);
996
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800997 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100998 vios->Stream() << "DEX CODE:\n";
999 ScopedIndentation indent2(vios);
1000 DumpDexCode(vios->Stream(), dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -07001001 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001002
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001003 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001004 std::unique_ptr<verifier::MethodVerifier> verifier;
1005 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001006 // We need to have the handle scope stay live until after the verifier since the verifier has
1007 // a handle to the dex cache from hs.
1008 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001009 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
1010 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001011 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001012 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001013 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -07001014 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001015 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001016 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001017 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001018 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001019 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001020 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001021 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001022 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001023 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
1024 oat_method_offsets_offset, oat_file_.Size());
1025 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001026 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001027 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001028 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001029
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001030 ScopedIndentation indent2(vios);
1031 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001032 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
1033 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001034 vios->Stream() << StringPrintf("WARNING: "
1035 "code offset 0x%08x is past end of file 0x%08zx.\n",
1036 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001037 success = false;
1038 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001039 vios->Stream() << "\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001040 }
1041 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001042 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001043 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
1044 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -07001045
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001046 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001047 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001048 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001049 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001050 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001051 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001052 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
1053 method_header_offset, oat_file_.Size());
1054 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001055 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001056 return false;
1057 }
1058
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001059 ScopedIndentation indent2(vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001060 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001061 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001062 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001063 }
Mingyao Yang063fc772016-08-02 11:02:54 -07001064 uint32_t vmap_table_offset = method_header ==
1065 nullptr ? 0 : method_header->GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001066 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001067
1068 size_t vmap_table_offset_limit =
1069 (kIsVdexEnabled && IsMethodGeneratedByDexToDexCompiler(oat_method, code_item))
1070 ? oat_file_.GetVdexFile()->Size()
1071 : method_header->GetCode() - oat_file_.Begin();
1072 if (vmap_table_offset >= vmap_table_offset_limit) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001073 vios->Stream() << StringPrintf("WARNING: "
1074 "vmap table offset 0x%08x is past end of file 0x%08zx. "
1075 "vmap table offset was loaded from offset 0x%08x.\n",
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001076 vmap_table_offset,
1077 vmap_table_offset_limit,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001078 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001079 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001080 } else if (options_.dump_vmap_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001081 DumpVmapData(vios, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001082 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001083 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001084 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001085 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001086
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001087 ScopedIndentation indent2(vios);
1088 vios->Stream()
1089 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
1090 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
1091 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
1092 vios->Stream() << "\n";
1093 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
1094 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
1095 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001096 }
1097 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001098 // Based on spill masks from QuickMethodFrameInfo so placed
1099 // after it is dumped, but useful for understanding quick
1100 // code, so dumped here.
1101 ScopedIndentation indent2(vios);
1102 DumpVregLocations(vios->Stream(), oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001103 }
1104 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001105 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001106 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
1107 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001108 ScopedIndentation indent2(vios);
1109 vios->Stream() << StringPrintf("WARNING: "
1110 "code size offset 0x%08x is past end of file 0x%08zx.",
1111 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001112 success = false;
1113 } else {
1114 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001115 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1116 uint64_t aligned_code_end = aligned_code_begin + code_size;
1117
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001118 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001119 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001120 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001121 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
1122 code_offset,
1123 code_size_offset,
1124 code_size,
1125 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001126
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001127 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001128 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001129 vios->Stream() << StringPrintf("WARNING: "
1130 "start of code at 0x%08x is past end of file 0x%08zx.",
1131 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001132 success = false;
1133 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001134 vios->Stream() << StringPrintf(
1135 "WARNING: "
1136 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
1137 "code size is 0x%08x loaded from offset 0x%08x.\n",
1138 aligned_code_end, oat_file_.Size(),
1139 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001140 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001141 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001142 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001143 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001144 }
1145 }
1146 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001147 vios->Stream() << StringPrintf(
1148 "WARNING: "
1149 "code size %d is bigger than max expected threshold of %d. "
1150 "code size is 0x%08x loaded from offset 0x%08x.\n",
1151 code_size, kMaxCodeSize,
1152 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001153 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001154 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001155 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001156 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001157 }
1158 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001159 } else if (options_.disassemble_code_) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001160 DumpCode(vios, oat_method, code_item, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001161 }
1162 }
1163 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001164 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001165 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001166 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001167
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001168 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1169 if (spill_mask == 0) {
1170 return;
1171 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001172 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001173 for (size_t i = 0; i < 32; i++) {
1174 if ((spill_mask & (1 << i)) != 0) {
1175 if (is_float) {
1176 os << "fr" << i;
1177 } else {
1178 os << "r" << i;
1179 }
1180 spill_mask ^= 1 << i; // clear bit
1181 if (spill_mask != 0) {
1182 os << ", ";
1183 } else {
1184 break;
1185 }
1186 }
1187 }
1188 os << ")";
1189 }
1190
Roland Levillain442b46a2015-02-18 16:54:21 +00001191 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001192 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001193 const OatFile::OatMethod& oat_method,
1194 const DexFile::CodeItem* code_item) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001195 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1196 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001197 const void* raw_code_info = oat_method.GetVmapTable();
1198 if (raw_code_info != nullptr) {
1199 CodeInfo code_info(raw_code_info);
1200 DCHECK(code_item != nullptr);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001201 ScopedIndentation indent1(vios);
1202 DumpCodeInfo(vios, code_info, oat_method, *code_item);
Roland Levillain442b46a2015-02-18 16:54:21 +00001203 }
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001204 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
1205 // We don't encode the size in the table, so just emit that we have quickened
1206 // information.
1207 ScopedIndentation indent(vios);
1208 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001209 } else {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001210 // Otherwise, there is nothing to display.
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001211 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001212 }
1213
1214 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001215 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001216 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001217 const OatFile::OatMethod& oat_method,
Roland Levillain442b46a2015-02-18 16:54:21 +00001218 const DexFile::CodeItem& code_item) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001219 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001220 oat_method.GetCodeOffset(),
1221 code_item.registers_size_,
1222 options_.dump_code_info_stack_maps_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001223 }
1224
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001225 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1226 const DexFile::CodeItem* code_item) {
1227 if (code_item != nullptr) {
1228 size_t num_locals_ins = code_item->registers_size_;
1229 size_t num_ins = code_item->ins_size_;
1230 size_t num_locals = num_locals_ins - num_ins;
1231 size_t num_outs = code_item->outs_size_;
1232
1233 os << "vr_stack_locations:";
1234 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1235 // For readability, delimit the different kinds of VRs.
1236 if (reg == num_locals_ins) {
1237 os << "\n\tmethod*:";
1238 } else if (reg == num_locals && num_ins > 0) {
1239 os << "\n\tins:";
1240 } else if (reg == 0 && num_locals > 0) {
1241 os << "\n\tlocals:";
1242 }
1243
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001244 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1245 code_item,
1246 oat_method.GetCoreSpillMask(),
1247 oat_method.GetFpSpillMask(),
1248 oat_method.GetFrameSizeInBytes(),
1249 reg,
1250 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001251 os << " v" << reg << "[sp + #" << offset << "]";
1252 }
1253
1254 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1255 if (out_reg == 0) {
1256 os << "\n\touts:";
1257 }
1258
1259 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1260 os << " v" << out_reg << "[sp + #" << offset << "]";
1261 }
1262
1263 os << "\n";
1264 }
1265 }
1266
Ian Rogersb23a7722012-10-09 16:54:26 -07001267 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001268 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001269 size_t i = 0;
1270 while (i < code_item->insns_size_in_code_units_) {
1271 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001272 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1273 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001274 i += instruction->SizeInCodeUnits();
1275 }
1276 }
1277 }
1278
Roland Levillainf2650d12015-05-28 14:53:28 +01001279 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1280 // the optimizing compiler?
1281 static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1282 const DexFile::CodeItem* code_item) {
1283 // If the native GC map is null and the Dex `code_item` is not
1284 // null, then this method has been compiled with the optimizing
1285 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001286 return oat_method.GetQuickCode() != nullptr &&
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001287 oat_method.GetVmapTable() != nullptr &&
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001288 code_item != nullptr;
1289 }
1290
1291 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1292 // the dextodex compiler?
1293 static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
1294 const DexFile::CodeItem* code_item) {
1295 // If the quick code is null, the Dex `code_item` is not
1296 // null, and the vmap table is not null, then this method has been compiled
1297 // with the dextodex compiler.
1298 return oat_method.GetQuickCode() == nullptr &&
1299 oat_method.GetVmapTable() != nullptr &&
1300 code_item != nullptr;
Roland Levillainf2650d12015-05-28 14:53:28 +01001301 }
1302
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001303 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001304 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001305 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001306 const DexFile* dex_file,
1307 const DexFile::ClassDef& class_def,
1308 const DexFile::CodeItem* code_item,
1309 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001310 if ((method_access_flags & kAccNative) == 0) {
1311 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001312 Runtime* const runtime = Runtime::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001313 Handle<mirror::DexCache> dex_cache(
Mathieu Chartierf284d442016-06-02 11:48:30 -07001314 hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file, nullptr)));
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001315 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001316 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001317 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
David Brazdil15fc7292016-09-02 14:13:18 +01001318 class_def, code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001319 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001320
1321 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001322 }
1323
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001324 // The StackMapsHelper provides the stack maps in the native PC order.
1325 // For identical native PCs, the order from the CodeInfo is preserved.
1326 class StackMapsHelper {
1327 public:
1328 explicit StackMapsHelper(const uint8_t* raw_code_info)
1329 : code_info_(raw_code_info),
1330 encoding_(code_info_.ExtractEncoding()),
1331 number_of_stack_maps_(code_info_.GetNumberOfStackMaps(encoding_)),
1332 indexes_(),
1333 offset_(static_cast<size_t>(-1)),
1334 stack_map_index_(0u) {
1335 if (number_of_stack_maps_ != 0u) {
1336 // Check if native PCs are ordered.
1337 bool ordered = true;
1338 StackMap last = code_info_.GetStackMapAt(0u, encoding_);
1339 for (size_t i = 1; i != number_of_stack_maps_; ++i) {
1340 StackMap current = code_info_.GetStackMapAt(i, encoding_);
1341 if (last.GetNativePcOffset(encoding_.stack_map_encoding) >
1342 current.GetNativePcOffset(encoding_.stack_map_encoding)) {
1343 ordered = false;
1344 break;
1345 }
1346 last = current;
1347 }
1348 if (!ordered) {
1349 // Create indirection indexes for access in native PC order. We do not optimize
1350 // for the fact that there can currently be only two separately ordered ranges,
1351 // namely normal stack maps and catch-point stack maps.
1352 indexes_.resize(number_of_stack_maps_);
1353 std::iota(indexes_.begin(), indexes_.end(), 0u);
1354 std::sort(indexes_.begin(),
1355 indexes_.end(),
1356 [this](size_t lhs, size_t rhs) {
1357 StackMap left = code_info_.GetStackMapAt(lhs, encoding_);
1358 uint32_t left_pc = left.GetNativePcOffset(encoding_.stack_map_encoding);
1359 StackMap right = code_info_.GetStackMapAt(rhs, encoding_);
1360 uint32_t right_pc = right.GetNativePcOffset(encoding_.stack_map_encoding);
1361 // If the PCs are the same, compare indexes to preserve the original order.
1362 return (left_pc < right_pc) || (left_pc == right_pc && lhs < rhs);
1363 });
1364 }
1365 offset_ = GetStackMapAt(0).GetNativePcOffset(encoding_.stack_map_encoding);
1366 }
1367 }
1368
1369 const CodeInfo& GetCodeInfo() const {
1370 return code_info_;
1371 }
1372
1373 const CodeInfoEncoding& GetEncoding() const {
1374 return encoding_;
1375 }
1376
1377 size_t GetOffset() const {
1378 return offset_;
1379 }
1380
1381 StackMap GetStackMap() const {
1382 return GetStackMapAt(stack_map_index_);
1383 }
1384
1385 void Next() {
1386 ++stack_map_index_;
1387 offset_ = (stack_map_index_ == number_of_stack_maps_)
1388 ? static_cast<size_t>(-1)
1389 : GetStackMapAt(stack_map_index_).GetNativePcOffset(encoding_.stack_map_encoding);
1390 }
1391
1392 private:
1393 StackMap GetStackMapAt(size_t i) const {
1394 if (!indexes_.empty()) {
1395 i = indexes_[i];
1396 }
1397 DCHECK_LT(i, number_of_stack_maps_);
1398 return code_info_.GetStackMapAt(i, encoding_);
1399 }
1400
1401 const CodeInfo code_info_;
1402 const CodeInfoEncoding encoding_;
1403 const size_t number_of_stack_maps_;
1404 dchecked_vector<size_t> indexes_; // Used if stack map native PCs are not ordered.
1405 size_t offset_;
1406 size_t stack_map_index_;
1407 };
1408
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001409 void DumpCode(VariableIndentationOutputStream* vios,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001410 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1411 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001412 const void* quick_code = oat_method.GetQuickCode();
1413
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001414 if (code_size == 0) {
1415 code_size = oat_method.GetQuickCodeSize();
1416 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001417 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001418 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001419 return;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001420 } else if (!bad_input && IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1421 // The optimizing compiler outputs its CodeInfo data in the vmap table.
1422 StackMapsHelper helper(oat_method.GetVmapTable());
1423 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1424 size_t offset = 0;
1425 while (offset < code_size) {
1426 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
1427 if (offset == helper.GetOffset()) {
1428 ScopedIndentation indent1(vios);
1429 StackMap stack_map = helper.GetStackMap();
1430 DCHECK(stack_map.IsValid());
1431 stack_map.Dump(vios,
1432 helper.GetCodeInfo(),
1433 helper.GetEncoding(),
1434 oat_method.GetCodeOffset(),
1435 code_item->registers_size_);
1436 do {
1437 helper.Next();
1438 // There may be multiple stack maps at a given PC. We display only the first one.
1439 } while (offset == helper.GetOffset());
1440 }
1441 DCHECK_LT(offset, helper.GetOffset());
1442 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001443 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001444 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1445 size_t offset = 0;
1446 while (offset < code_size) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001447 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001448 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001449 }
1450 }
1451
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001452 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001453 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001454 const OatDumperOptions& options_;
1455 uint32_t resolved_addr2instr_;
Andreas Gampe372f3a32016-08-19 10:49:06 -07001456 const InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001457 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001458 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001459};
1460
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001461class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001462 public:
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001463 ImageDumper(std::ostream* os,
1464 gc::space::ImageSpace& image_space,
1465 const ImageHeader& image_header,
1466 OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001467 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001468 vios_(os),
1469 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001470 image_space_(image_space),
1471 image_header_(image_header),
1472 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001473
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001474 bool Dump() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001475 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001476 std::ostream& indent_os = vios_.Stream();
1477
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001478 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001479
Jeff Haodcdc85b2015-12-04 14:06:18 -08001480 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1481
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001482 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001483
Mathieu Chartiere401d142015-04-22 13:56:20 -07001484 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1485
1486 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1487 auto section = static_cast<ImageHeader::ImageSections>(i);
1488 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1489 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001490
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001491 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001492
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001493 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001494
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001495 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1496
1497 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1498
1499 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001500
Alex Lighta59dd802014-07-02 16:28:08 -07001501 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1502
Igor Murashkin46774762014-10-22 11:37:02 -07001503 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1504
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001505 {
1506 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001507 static_assert(arraysize(image_roots_descriptions_) ==
1508 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001509 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1510 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1511 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001512 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001513 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001514 if (image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001515 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001516 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001517 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001518 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1519 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001520 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001521 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1522 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001523 run++;
1524 } else {
1525 break;
1526 }
1527 }
1528 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001529 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001530 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001531 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001532 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001533 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001534 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001535 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001536 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001537 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001538 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001539 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001540 }
1541 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001542 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001543
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001544 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001545 os << "METHOD ROOTS\n";
1546 static_assert(arraysize(image_methods_descriptions_) ==
1547 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1548 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1549 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1550 const char* description = image_methods_descriptions_[i];
1551 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001552 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001553 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001554 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001555 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001556
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001557 Runtime* const runtime = Runtime::Current();
1558 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001559 std::string image_filename = image_space_.GetImageFilename();
1560 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001561 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001562 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001563 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001564 const OatFile* oat_file = image_space_.GetOatFile();
Alex Lighta59dd802014-07-02 16:28:08 -07001565 if (oat_file == nullptr) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001566 oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location);
1567 }
1568 if (oat_file == nullptr) {
1569 oat_file = OatFile::Open(oat_location,
1570 oat_location,
1571 nullptr,
1572 nullptr,
1573 false,
1574 /*low_4gb*/false,
1575 nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001576 &error_msg);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001577 }
1578 if (oat_file == nullptr) {
1579 os << "OAT FILE NOT FOUND: " << error_msg << "\n";
1580 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001581 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001582 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001583
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001584 stats_.oat_file_bytes = oat_file->Size();
1585
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001586 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001587
Mathieu Chartier02e25112013-08-14 16:14:24 -07001588 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001589 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001590 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1591 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001592 }
1593
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001594 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001595
Jeff Haodcdc85b2015-12-04 14:06:18 -08001596 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001597 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001598 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001599 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001600 {
1601 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1602 heap->FlushAllocStack();
1603 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001604 // Since FlushAllocStack() above resets the (active) allocation
1605 // stack. Need to revoke the thread-local allocation stacks that
1606 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001607 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001608 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001609 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001610 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001611 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001612 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001613 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001614 {
Andreas Gampecc1b5352016-12-01 16:58:38 -08001615 ReaderMutexLock mu(self, *Locks::dex_lock_);
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001616 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001617 ObjPtr<mirror::DexCache> dex_cache =
1618 ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001619 if (dex_cache != nullptr) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001620 dex_caches_.insert(dex_cache.Ptr());
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001621 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001622 }
1623 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001624 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001625 // Dump the normal objects before ArtMethods.
1626 image_space_.GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1627 indent_os << "\n";
1628 // TODO: Dump fields.
1629 // Dump methods after.
Jeff Haodcdc85b2015-12-04 14:06:18 -08001630 DumpArtMethodVisitor visitor(this);
Mathieu Chartiere42888f2016-04-14 10:49:19 -07001631 image_header_.VisitPackedArtMethods(&visitor,
1632 image_space_.Begin(),
1633 image_header_.GetPointerSize());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001634 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001635 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001636 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001637 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001638 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001639 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001640 size_t data_size = image_header_.GetDataSize(); // stored size in file.
1641 if (file == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001642 LOG(WARNING) << "Failed to find image in " << image_filename;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001643 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001644 stats_.file_bytes = file->GetLength();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001645 // If the image is compressed, adjust to decompressed size.
1646 size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader);
1647 if (image_header_.GetStorageMode() == ImageHeader::kStorageModeUncompressed) {
1648 DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image";
1649 }
1650 stats_.file_bytes += uncompressed_size - data_size;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001651 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001652 size_t header_bytes = sizeof(ImageHeader);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001653 const auto& object_section = image_header_.GetImageSection(ImageHeader::kSectionObjects);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001654 const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1655 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001656 const auto& dex_cache_arrays_section = image_header_.GetImageSection(
1657 ImageHeader::kSectionDexCacheArrays);
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001658 const auto& intern_section = image_header_.GetImageSection(
1659 ImageHeader::kSectionInternedStrings);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001660 const auto& class_table_section = image_header_.GetImageSection(
1661 ImageHeader::kSectionClassTable);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001662 const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1663
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001664 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001665
1666 // Objects are kObjectAlignment-aligned.
1667 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1668 if (object_section.Offset() > header_bytes) {
1669 stats_.alignment_bytes += object_section.Offset() - header_bytes;
1670 }
1671
1672 // Field section is 4-byte aligned.
1673 constexpr size_t kFieldSectionAlignment = 4U;
1674 uint32_t end_objects = object_section.Offset() + object_section.Size();
1675 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
1676 stats_.alignment_bytes += field_section.Offset() - end_objects;
1677
1678 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
1679 uint32_t end_fields = field_section.Offset() + field_section.Size();
1680 CHECK_ALIGNED(method_section.Offset(), 4);
1681 stats_.alignment_bytes += method_section.Offset() - end_fields;
1682
1683 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
1684 uint32_t end_methods = method_section.Offset() + method_section.Size();
1685 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
1686 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
1687
1688 // Intern table is 8-byte aligned.
1689 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
1690 CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
1691 stats_.alignment_bytes += intern_section.Offset() - end_caches;
1692
1693 // Add space between intern table and class table.
1694 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
1695 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
1696
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001697 // Add space between end of image data and bitmap. Expect the bitmap to be page-aligned.
1698 const size_t bitmap_offset = sizeof(ImageHeader) + data_size;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001699 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001700 stats_.alignment_bytes += RoundUp(bitmap_offset, kPageSize) - bitmap_offset;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001701
Mathieu Chartiere401d142015-04-22 13:56:20 -07001702 stats_.bitmap_bytes += bitmap_section.Size();
1703 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01001704 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01001705 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001706 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001707 stats_.class_table_bytes += class_table_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001708 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001709 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001710
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001711 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001712
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001713 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001714 }
1715
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001716 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001717 class DumpArtMethodVisitor : public ArtMethodVisitor {
1718 public:
1719 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
1720
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001721 virtual void Visit(ArtMethod* method) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001722 std::ostream& indent_os = image_dumper_->vios_.Stream();
David Sehr709b0702016-10-13 09:12:37 -07001723 indent_os << method << " " << " ArtMethod: " << ArtMethod::PrettyMethod(method) << "\n";
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001724 image_dumper_->DumpMethod(method, indent_os);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001725 indent_os << "\n";
1726 }
1727
1728 private:
1729 ImageDumper* const image_dumper_;
1730 };
1731
Mathieu Chartier3398c782016-09-30 10:27:43 -07001732 static void PrettyObjectValue(std::ostream& os,
1733 ObjPtr<mirror::Class> type,
1734 ObjPtr<mirror::Object> value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001735 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001736 CHECK(type != nullptr);
1737 if (value == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07001738 os << StringPrintf("null %s\n", type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001739 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001740 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001741 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001742 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001743 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001744 mirror::Class* klass = value->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07001745 os << StringPrintf("%p Class: %s\n", klass, mirror::Class::PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001746 } else {
David Sehr709b0702016-10-13 09:12:37 -07001747 os << StringPrintf("%p %s\n", value.Ptr(), type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001748 }
1749 }
1750
Mathieu Chartier3398c782016-09-30 10:27:43 -07001751 static void PrintField(std::ostream& os, ArtField* field, ObjPtr<mirror::Object> obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001752 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001753 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08001754 switch (field->GetTypeAsPrimitiveType()) {
1755 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08001756 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001757 break;
1758 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001759 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001760 break;
1761 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001762 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001763 break;
1764 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001765 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001766 break;
1767 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07001768 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001769 break;
1770 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07001771 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001772 break;
1773 case Primitive::kPrimBoolean:
Fred Shih37f05ef2014-07-16 18:38:08 -07001774 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1775 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001776 break;
1777 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07001778 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001779 break;
1780 case Primitive::kPrimNot: {
1781 // Get the value, don't compute the type unless it is non-null as we don't want
1782 // to cause class loading.
Mathieu Chartier3398c782016-09-30 10:27:43 -07001783 ObjPtr<mirror::Object> value = field->GetObj(obj);
Ian Rogers08f1f502014-12-02 15:04:37 -08001784 if (value == nullptr) {
1785 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07001786 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08001787 // Grab the field type without causing resolution.
Mathieu Chartier3398c782016-09-30 10:27:43 -07001788 ObjPtr<mirror::Class> field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08001789 if (field_type != nullptr) {
1790 PrettyObjectValue(os, field_type, value);
1791 } else {
Mathieu Chartier3398c782016-09-30 10:27:43 -07001792 os << StringPrintf("%p %s\n",
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07001793 value.Ptr(),
Ian Rogers08f1f502014-12-02 15:04:37 -08001794 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
1795 }
Ian Rogers50239c72013-06-17 14:53:22 -07001796 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001797 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07001798 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001799 default:
1800 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
1801 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08001802 }
1803 }
1804
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001805 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001806 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001807 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001808 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001809 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001810 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001811 for (ArtField& field : klass->GetIFields()) {
1812 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001813 }
1814 }
1815
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001816 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001817 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001818 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001819
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001820 const void* GetQuickOatCodeBegin(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001821 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001822 image_header_.GetPointerSize());
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001823 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001824 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001825 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001826 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001827 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001828 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001829 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001830 }
1831
Mathieu Chartiere401d142015-04-22 13:56:20 -07001832 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001833 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001834 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1835 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001836 return 0;
1837 }
1838 return oat_code_begin[-1];
1839 }
1840
Mathieu Chartiere401d142015-04-22 13:56:20 -07001841 const void* GetQuickOatCodeEnd(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001842 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001843 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001844 if (oat_code_begin == nullptr) {
1845 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001846 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001847 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001848 }
1849
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001850 static void Callback(mirror::Object* obj, void* arg) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001851 DCHECK(obj != nullptr);
1852 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001853 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001854 if (!state->InDumpSpace(obj)) {
1855 return;
1856 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001857
1858 size_t object_bytes = obj->SizeOf();
1859 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1860 state->stats_.object_bytes += object_bytes;
1861 state->stats_.alignment_bytes += alignment_bytes;
1862
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001863 std::ostream& os = state->vios_.Stream();
1864
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001865 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001866 if (obj_class->IsArrayClass()) {
David Sehr709b0702016-10-13 09:12:37 -07001867 os << StringPrintf("%p: %s length:%d\n", obj, obj_class->PrettyDescriptor().c_str(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001868 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001869 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001870 mirror::Class* klass = obj->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07001871 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj,
1872 mirror::Class::PrettyDescriptor(klass).c_str())
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001873 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08001874 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001875 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001876 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001877 } else {
David Sehr709b0702016-10-13 09:12:37 -07001878 os << StringPrintf("%p: %s\n", obj, obj_class->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001879 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001880 ScopedIndentation indent1(&state->vios_);
1881 DumpFields(os, obj, obj_class);
Andreas Gampe542451c2016-07-26 09:02:02 -07001882 const PointerSize image_pointer_size = state->image_header_.GetPointerSize();
Ian Rogersd5b32602012-02-26 16:40:04 -08001883 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001884 auto* obj_array = obj->AsObjectArray<mirror::Object>();
1885 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001886 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001887 size_t run = 0;
1888 for (int32_t j = i + 1; j < length; j++) {
1889 if (value == obj_array->Get(j)) {
1890 run++;
1891 } else {
1892 break;
1893 }
1894 }
1895 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001896 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001897 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001898 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001899 i = i + run;
1900 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001901 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001902 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001903 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001904 }
1905 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001906 mirror::Class* klass = obj->AsClass();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001907 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001908 os << "STATICS:\n";
1909 ScopedIndentation indent2(&state->vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001910 for (ArtField& field : klass->GetSFields()) {
1911 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001912 }
1913 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001914 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01001915 auto it = state->dex_caches_.find(obj);
1916 if (it != state->dex_caches_.end()) {
1917 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001918 const auto& field_section = state->image_header_.GetImageSection(
1919 ImageHeader::kSectionArtFields);
1920 const auto& method_section = state->image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001921 size_t num_methods = dex_cache->NumResolvedMethods();
1922 if (num_methods != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001923 os << "Methods (size=" << num_methods << "):\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01001924 ScopedIndentation indent2(&state->vios_);
1925 auto* resolved_methods = dex_cache->GetResolvedMethods();
1926 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001927 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_methods,
1928 i,
1929 image_pointer_size);
Vladimir Marko05792b92015-08-03 11:56:49 +01001930 size_t run = 0;
1931 for (size_t j = i + 1;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001932 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_methods,
1933 j,
1934 image_pointer_size);
1935 ++j) {
1936 ++run;
1937 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001938 if (run == 0) {
1939 os << StringPrintf("%zd: ", i);
1940 } else {
1941 os << StringPrintf("%zd to %zd: ", i, i + run);
1942 i = i + run;
1943 }
1944 std::string msg;
1945 if (elem == nullptr) {
1946 msg = "null";
1947 } else if (method_section.Contains(
1948 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07001949 msg = reinterpret_cast<ArtMethod*>(elem)->PrettyMethod();
Vladimir Marko05792b92015-08-03 11:56:49 +01001950 } else {
1951 msg = "<not in method section>";
1952 }
1953 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07001954 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001955 }
1956 size_t num_fields = dex_cache->NumResolvedFields();
1957 if (num_fields != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001958 os << "Fields (size=" << num_fields << "):\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01001959 ScopedIndentation indent2(&state->vios_);
1960 auto* resolved_fields = dex_cache->GetResolvedFields();
1961 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001962 auto* elem = mirror::DexCache::GetElementPtrSize(
1963 resolved_fields, i, image_pointer_size);
Vladimir Marko05792b92015-08-03 11:56:49 +01001964 size_t run = 0;
1965 for (size_t j = i + 1;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001966 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_fields,
1967 j,
1968 image_pointer_size);
1969 ++j) {
1970 ++run;
1971 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001972 if (run == 0) {
1973 os << StringPrintf("%zd: ", i);
1974 } else {
1975 os << StringPrintf("%zd to %zd: ", i, i + run);
1976 i = i + run;
1977 }
1978 std::string msg;
1979 if (elem == nullptr) {
1980 msg = "null";
1981 } else if (field_section.Contains(
1982 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07001983 msg = reinterpret_cast<ArtField*>(elem)->PrettyField();
Vladimir Marko05792b92015-08-03 11:56:49 +01001984 } else {
1985 msg = "<not in field section>";
1986 }
1987 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07001988 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001989 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001990 size_t num_types = dex_cache->NumResolvedTypes();
1991 if (num_types != 0u) {
1992 os << "Types (size=" << num_types << "):\n";
1993 ScopedIndentation indent2(&state->vios_);
1994 auto* resolved_types = dex_cache->GetResolvedTypes();
1995 for (size_t i = 0; i < num_types; ++i) {
1996 auto* elem = resolved_types[i].Read();
1997 size_t run = 0;
1998 for (size_t j = i + 1; j != num_types && elem == resolved_types[j].Read(); ++j) {
1999 ++run;
2000 }
2001 if (run == 0) {
2002 os << StringPrintf("%zd: ", i);
2003 } else {
2004 os << StringPrintf("%zd to %zd: ", i, i + run);
2005 i = i + run;
2006 }
2007 std::string msg;
2008 if (elem == nullptr) {
2009 msg = "null";
2010 } else {
David Sehr709b0702016-10-13 09:12:37 -07002011 msg = elem->PrettyClass();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002012 }
2013 os << StringPrintf("%p %s\n", elem, msg.c_str());
2014 }
2015 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002016 }
2017 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002018 std::string temp;
2019 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002020 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002021
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002022 void DumpMethod(ArtMethod* method, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002023 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002024 DCHECK(method != nullptr);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002025 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
2026 const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
Andreas Gampe542451c2016-07-26 09:02:02 -07002027 const PointerSize pointer_size = image_header_.GetPointerSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002028 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2029 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002030 if (method->IsNative()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002031 bool first_occurrence;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002032 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2033 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002034 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002035 stats_.native_to_managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002036 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002037 if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
2038 image_header_.GetPointerSize())) {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002039 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002040 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002041 } else if (method->IsAbstract() || method->IsClassInitializer()) {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002042 // Don't print information for these.
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002043 } else if (method->IsRuntimeMethod()) {
2044 ImtConflictTable* table = method->GetImtConflictTable(image_header_.GetPointerSize());
2045 if (table != nullptr) {
2046 indent_os << "IMT conflict table " << table << " method: ";
2047 for (size_t i = 0, count = table->NumEntries(pointer_size); i < count; ++i) {
David Sehr709b0702016-10-13 09:12:37 -07002048 indent_os << ArtMethod::PrettyMethod(table->GetImplementationMethod(i, pointer_size))
2049 << " ";
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002050 }
2051 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002052 } else {
2053 const DexFile::CodeItem* code_item = method->GetCodeItem();
2054 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002055 stats_.dex_instruction_bytes += dex_instruction_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002056
2057 bool first_occurrence;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002058 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002059 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01002060 // Method compiled with the optimizing compiler have no vmap table.
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002061 vmap_table_bytes = ComputeOatSize(method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002062 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002063 stats_.vmap_table_bytes += vmap_table_bytes;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002064 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002065 }
2066
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002067 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2068 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002069 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002070 stats_.managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002071 if (method->IsConstructor()) {
2072 if (method->IsStatic()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002073 stats_.class_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002074 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002075 stats_.large_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002076 }
2077 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002078 stats_.large_method_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002079 }
2080 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002081 stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002082
Igor Murashkin7617abd2015-07-10 18:27:47 -07002083 uint32_t method_access_flags = method->GetAccessFlags();
2084
Mathieu Chartiere401d142015-04-22 13:56:20 -07002085 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002086 indent_os << StringPrintf("SIZE: Dex Instructions=%zd StackMaps=%zd AccessFlags=0x%x\n",
2087 dex_instruction_bytes,
2088 vmap_table_bytes,
Igor Murashkin7617abd2015-07-10 18:27:47 -07002089 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002090
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002091 size_t total_size = dex_instruction_bytes +
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002092 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002093
2094 double expansion =
2095 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002096 stats_.ComputeOutliers(total_size, expansion, method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002097 }
2098 }
2099
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002100 std::set<const void*> already_seen_;
2101 // Compute the size of the given data within the oat file and whether this is the first time
2102 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002103 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002104 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002105 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002106 already_seen_.insert(oat_data);
2107 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002108 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002109 }
2110 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002111 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002112
2113 public:
2114 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002115 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002116 size_t file_bytes;
2117
2118 size_t header_bytes;
2119 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002120 size_t art_field_bytes;
2121 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01002122 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002123 size_t interned_strings_bytes;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002124 size_t class_table_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07002125 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002126 size_t alignment_bytes;
2127
2128 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002129 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002130 size_t managed_to_native_code_bytes;
2131 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07002132 size_t class_initializer_code_bytes;
2133 size_t large_initializer_code_bytes;
2134 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002135
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002136 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002137
2138 size_t dex_instruction_bytes;
2139
Mathieu Chartiere401d142015-04-22 13:56:20 -07002140 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002141 std::vector<size_t> method_outlier_size;
2142 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002143 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002144
Roland Levillain3887c462015-08-12 18:15:42 +01002145 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002146 : oat_file_bytes(0),
2147 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002148 header_bytes(0),
2149 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002150 art_field_bytes(0),
2151 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01002152 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002153 interned_strings_bytes(0),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002154 class_table_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07002155 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002156 alignment_bytes(0),
2157 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002158 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002159 managed_to_native_code_bytes(0),
2160 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07002161 class_initializer_code_bytes(0),
2162 large_initializer_code_bytes(0),
2163 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002164 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002165 dex_instruction_bytes(0) {}
2166
Elliott Hughesa0e18062012-04-13 15:59:59 -07002167 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002168 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002169 size_t bytes;
2170 size_t count;
2171 };
2172 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2173 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002174
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002175 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002176 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2177 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002178 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002179 it->second.count += 1;
2180 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002181 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002182 }
2183 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002184
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002185 double PercentOfOatBytes(size_t size) {
2186 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2187 }
2188
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002189 double PercentOfFileBytes(size_t size) {
2190 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2191 }
2192
2193 double PercentOfObjectBytes(size_t size) {
2194 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2195 }
2196
Mathieu Chartiere401d142015-04-22 13:56:20 -07002197 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002198 method_outlier_size.push_back(total_size);
2199 method_outlier_expansion.push_back(expansion);
2200 method_outlier.push_back(method);
2201 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002202
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002203 void DumpOutliers(std::ostream& os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002204 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002205 size_t sum_of_sizes = 0;
2206 size_t sum_of_sizes_squared = 0;
2207 size_t sum_of_expansion = 0;
2208 size_t sum_of_expansion_squared = 0;
2209 size_t n = method_outlier_size.size();
Mathieu Chartier1ebf8d32016-06-09 11:51:27 -07002210 if (n <= 1) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08002211 return;
2212 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002213 for (size_t i = 0; i < n; i++) {
2214 size_t cur_size = method_outlier_size[i];
2215 sum_of_sizes += cur_size;
2216 sum_of_sizes_squared += cur_size * cur_size;
2217 double cur_expansion = method_outlier_expansion[i];
2218 sum_of_expansion += cur_expansion;
2219 sum_of_expansion_squared += cur_expansion * cur_expansion;
2220 }
2221 size_t size_mean = sum_of_sizes / n;
2222 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2223 double expansion_mean = sum_of_expansion / n;
2224 double expansion_variance =
2225 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2226
2227 // Dump methods whose size is a certain number of standard deviations from the mean
2228 size_t dumped_values = 0;
2229 size_t skipped_values = 0;
2230 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2231 size_t cur_size_variance = i * i * size_variance;
2232 bool first = true;
2233 for (size_t j = 0; j < n; j++) {
2234 size_t cur_size = method_outlier_size[j];
2235 if (cur_size > size_mean) {
2236 size_t cur_var = cur_size - size_mean;
2237 cur_var = cur_var * cur_var;
2238 if (cur_var > cur_size_variance) {
2239 if (dumped_values > 20) {
2240 if (i == 1) {
2241 skipped_values++;
2242 } else {
2243 i = 2; // jump to counting for 1 standard deviation
2244 break;
2245 }
2246 } else {
2247 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002248 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002249 first = false;
2250 }
David Sehr709b0702016-10-13 09:12:37 -07002251 os << ArtMethod::PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002252 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002253 method_outlier_size[j] = 0; // don't consider this method again
2254 dumped_values++;
2255 }
2256 }
2257 }
2258 }
2259 }
2260 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002261 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002262 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002263 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002264 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002265
2266 // Dump methods whose expansion is a certain number of standard deviations from the mean
2267 dumped_values = 0;
2268 skipped_values = 0;
2269 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2270 double cur_expansion_variance = i * i * expansion_variance;
2271 bool first = true;
2272 for (size_t j = 0; j < n; j++) {
2273 double cur_expansion = method_outlier_expansion[j];
2274 if (cur_expansion > expansion_mean) {
2275 size_t cur_var = cur_expansion - expansion_mean;
2276 cur_var = cur_var * cur_var;
2277 if (cur_var > cur_expansion_variance) {
2278 if (dumped_values > 20) {
2279 if (i == 1) {
2280 skipped_values++;
2281 } else {
2282 i = 2; // jump to counting for 1 standard deviation
2283 break;
2284 }
2285 } else {
2286 if (first) {
2287 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002288 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002289 first = false;
2290 }
David Sehr709b0702016-10-13 09:12:37 -07002291 os << ArtMethod::PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002292 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002293 method_outlier_expansion[j] = 0.0; // don't consider this method again
2294 dumped_values++;
2295 }
2296 }
2297 }
2298 }
2299 }
2300 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002301 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002302 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002303 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002304 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002305 }
2306
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002307 void Dump(std::ostream& os, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002308 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002309 {
2310 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2311 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002312 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2313 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2314 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2315 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2316 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2317 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002318 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002319 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2320 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002321 header_bytes, PercentOfFileBytes(header_bytes),
2322 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002323 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2324 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002325 dex_cache_arrays_bytes,
2326 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002327 interned_strings_bytes,
2328 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002329 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002330 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002331 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2332 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002333 CHECK_EQ(file_bytes,
2334 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2335 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2336 bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002337 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002338
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002339 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002340 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002341 for (const auto& sizes_and_count : sizes_and_counts) {
2342 const std::string& descriptor(sizes_and_count.first);
2343 double average = static_cast<double>(sizes_and_count.second.bytes) /
2344 static_cast<double>(sizes_and_count.second.count);
2345 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002346 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002347 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002348 descriptor.c_str(), sizes_and_count.second.bytes,
2349 sizes_and_count.second.count, average, percent);
2350 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002351 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002352 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002353 CHECK_EQ(object_bytes, object_bytes_total);
2354
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002355 os << StringPrintf("oat_file_bytes = %8zd\n"
2356 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2357 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2358 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2359 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2360 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2361 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002362 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002363 managed_code_bytes,
2364 PercentOfOatBytes(managed_code_bytes),
2365 managed_to_native_code_bytes,
2366 PercentOfOatBytes(managed_to_native_code_bytes),
2367 native_to_managed_code_bytes,
2368 PercentOfOatBytes(native_to_managed_code_bytes),
2369 class_initializer_code_bytes,
2370 PercentOfOatBytes(class_initializer_code_bytes),
2371 large_initializer_code_bytes,
2372 PercentOfOatBytes(large_initializer_code_bytes),
2373 large_method_code_bytes,
2374 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002375 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002376 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002377 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002378 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2379 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002380 }
2381
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002382 os << "\n" << StringPrintf("vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002383 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002384 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002385
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002386 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2387 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002388 static_cast<double>(managed_code_bytes) /
2389 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002390 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002391 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002392 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002393
2394 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002395 }
2396 } stats_;
2397
2398 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002399 enum {
2400 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2401 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2402 kLargeConstructorDexBytes = 4000,
2403 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2404 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2405 kLargeMethodDexBytes = 16000
2406 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002407
2408 // For performance, use the *os_ directly for anything that doesn't need indentation
2409 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002410 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002411 VariableIndentationOutputStream vios_;
2412 ScopedIndentation indent1_;
2413
Ian Rogers1d54e732013-05-02 21:10:01 -07002414 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002415 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002416 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002417 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002418 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002419
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002420 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002421};
2422
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002423static int DumpImage(gc::space::ImageSpace* image_space,
2424 OatDumperOptions* options,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002425 std::ostream* os) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002426 const ImageHeader& image_header = image_space->GetImageHeader();
2427 if (!image_header.IsValid()) {
2428 fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
2429 return EXIT_FAILURE;
2430 }
2431 ImageDumper image_dumper(os, *image_space, image_header, options);
2432 if (!image_dumper.Dump()) {
2433 return EXIT_FAILURE;
2434 }
2435 return EXIT_SUCCESS;
2436}
2437
2438static int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002439 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002440 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002441 options->class_loader_ = &null_class_loader;
2442
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002443 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002444 if (options->app_image_ != nullptr) {
2445 if (options->app_oat_ == nullptr) {
2446 LOG(ERROR) << "Can not dump app image without app oat file";
Jeff Haodcdc85b2015-12-04 14:06:18 -08002447 return EXIT_FAILURE;
2448 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002449 // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
2450 // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
2451 // pointers into 32 bit pointer sized ArtMethods.
2452 std::string error_msg;
2453 std::unique_ptr<OatFile> oat_file(OatFile::Open(options->app_oat_,
2454 options->app_oat_,
2455 nullptr,
2456 nullptr,
2457 false,
2458 /*low_4gb*/true,
2459 nullptr,
2460 &error_msg));
2461 if (oat_file == nullptr) {
2462 LOG(ERROR) << "Failed to open oat file " << options->app_oat_ << " with error " << error_msg;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002463 return EXIT_FAILURE;
2464 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002465 std::unique_ptr<gc::space::ImageSpace> space(
2466 gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg));
2467 if (space == nullptr) {
2468 LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error "
2469 << error_msg;
2470 }
2471 // Open dex files for the image.
2472 std::vector<std::unique_ptr<const DexFile>> dex_files;
2473 if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
2474 LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
2475 << error_msg;
2476 }
2477 // Dump the actual image.
2478 int result = DumpImage(space.get(), options, os);
2479 if (result != EXIT_SUCCESS) {
2480 return result;
2481 }
2482 // Fall through to dump the boot images.
2483 }
2484
2485 gc::Heap* heap = runtime->GetHeap();
2486 CHECK(heap->HasBootImageSpace()) << "No image spaces";
2487 for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
2488 int result = DumpImage(image_space, options, os);
2489 if (result != EXIT_SUCCESS) {
2490 return result;
2491 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002492 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002493 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002494}
2495
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002496static jobject InstallOatFile(Runtime* runtime,
2497 std::unique_ptr<OatFile> oat_file,
2498 std::vector<const DexFile*>* class_path)
2499 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002500 Thread* self = Thread::Current();
2501 CHECK(self != nullptr);
2502 // Need well-known-classes.
2503 WellKnownClasses::Init(self->GetJniEnv());
2504
2505 // Need to register dex files to get a working dex cache.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002506 OatFile* oat_file_ptr = oat_file.get();
Andreas Gampe00b25f32014-09-17 21:49:05 -07002507 ClassLinker* class_linker = runtime->GetClassLinker();
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002508 runtime->GetOatFileManager().RegisterOatFile(std::move(oat_file));
2509 for (const OatFile::OatDexFile* odf : oat_file_ptr->GetOatDexFiles()) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002510 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002511 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002512 CHECK(dex_file != nullptr) << error_msg;
Mathieu Chartierf284d442016-06-02 11:48:30 -07002513 class_linker->RegisterDexFile(*dex_file, nullptr);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002514 class_path->push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002515 }
2516
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002517 // Need a class loader. Fake that we're a compiler.
2518 // Note: this will run initializers through the unstarted runtime, so make sure it's
2519 // initialized.
2520 interpreter::UnstartedRuntime::Initialize();
2521
2522 jobject class_loader = class_linker->CreatePathClassLoader(self, *class_path);
2523
2524 return class_loader;
2525}
2526
2527static int DumpOatWithRuntime(Runtime* runtime,
2528 std::unique_ptr<OatFile> oat_file,
2529 OatDumperOptions* options,
2530 std::ostream* os) {
2531 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2532 ScopedObjectAccess soa(Thread::Current());
2533
2534 OatFile* oat_file_ptr = oat_file.get();
2535 std::vector<const DexFile*> class_path;
2536 jobject class_loader = InstallOatFile(runtime, std::move(oat_file), &class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002537
2538 // Use the class loader while dumping.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002539 StackHandleScope<1> scope(soa.Self());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002540 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07002541 soa.Decode<mirror::ClassLoader>(class_loader));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002542 options->class_loader_ = &loader_handle;
2543
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002544 OatDumper oat_dumper(*oat_file_ptr, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002545 bool success = oat_dumper.Dump(*os);
2546 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2547}
2548
2549static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002550 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002551 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002552 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002553 options->class_loader_ = &null_class_loader;
2554
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002555 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002556 bool success = oat_dumper.Dump(*os);
2557 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2558}
2559
2560static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2561 std::ostream* os) {
2562 std::string error_msg;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002563 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
2564 oat_filename,
2565 nullptr,
2566 nullptr,
2567 false,
2568 /*low_4gb*/false,
2569 nullptr,
2570 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002571 if (oat_file == nullptr) {
2572 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2573 return EXIT_FAILURE;
2574 }
2575
2576 if (runtime != nullptr) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002577 return DumpOatWithRuntime(runtime, std::move(oat_file), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002578 } else {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002579 return DumpOatWithoutRuntime(oat_file.get(), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002580 }
2581}
2582
David Srbecky2fdd03c2016-03-10 15:32:37 +00002583static int SymbolizeOat(const char* oat_filename, std::string& output_name, bool no_bits) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002584 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002585 OatFile* oat_file = OatFile::Open(oat_filename,
2586 oat_filename,
2587 nullptr,
2588 nullptr,
2589 false,
2590 /*low_4gb*/false,
2591 nullptr,
2592 &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002593 if (oat_file == nullptr) {
2594 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2595 return EXIT_FAILURE;
2596 }
2597
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002598 bool result;
2599 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
2600 // files for 64-bit code in the past.
2601 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002602 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file, output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002603 result = oat_symbolizer.Symbolize();
2604 } else {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002605 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file, output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002606 result = oat_symbolizer.Symbolize();
2607 }
2608 if (!result) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002609 fprintf(stderr, "Failed to symbolize\n");
2610 return EXIT_FAILURE;
2611 }
2612
2613 return EXIT_SUCCESS;
2614}
2615
Andreas Gampe9fded872016-09-25 16:08:35 -07002616class IMTDumper {
2617 public:
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002618 static bool Dump(Runtime* runtime,
2619 const std::string& imt_file,
2620 bool dump_imt_stats,
2621 const char* oat_filename) {
2622 Thread* self = Thread::Current();
2623
2624 ScopedObjectAccess soa(self);
2625 StackHandleScope<1> scope(self);
2626 MutableHandle<mirror::ClassLoader> class_loader = scope.NewHandle<mirror::ClassLoader>(nullptr);
2627 std::vector<const DexFile*> class_path;
2628
2629 if (oat_filename != nullptr) {
2630 std::string error_msg;
2631 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
2632 oat_filename,
2633 nullptr,
2634 nullptr,
2635 false,
2636 /*low_4gb*/false,
2637 nullptr,
2638 &error_msg));
2639 if (oat_file == nullptr) {
2640 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2641 return false;
2642 }
2643
2644 class_loader.Assign(soa.Decode<mirror::ClassLoader>(
2645 InstallOatFile(runtime, std::move(oat_file), &class_path)));
2646 } else {
2647 class_loader.Assign(nullptr); // Boot classloader. Just here for explicit documentation.
2648 class_path = runtime->GetClassLinker()->GetBootClassPath();
2649 }
2650
2651 if (!imt_file.empty()) {
2652 return DumpImt(runtime, imt_file, class_loader);
2653 }
2654
2655 if (dump_imt_stats) {
2656 return DumpImtStats(runtime, class_path, class_loader);
2657 }
2658
2659 LOG(FATAL) << "Should not reach here";
2660 UNREACHABLE();
2661 }
2662
2663 private:
2664 static bool DumpImt(Runtime* runtime,
2665 const std::string& imt_file,
2666 Handle<mirror::ClassLoader> h_class_loader)
2667 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002668 std::vector<std::string> lines = ReadCommentedInputFromFile(imt_file);
2669 std::unordered_set<std::string> prepared;
2670
2671 for (const std::string& line : lines) {
2672 // A line should be either a class descriptor, in which case we will dump the complete IMT,
2673 // or a class descriptor and an interface method, in which case we will lookup the method,
2674 // determine its IMT slot, and check the class' IMT.
2675 size_t first_space = line.find(' ');
2676 if (first_space == std::string::npos) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002677 DumpIMTForClass(runtime, line, h_class_loader, &prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07002678 } else {
2679 DumpIMTForMethod(runtime,
2680 line.substr(0, first_space),
2681 line.substr(first_space + 1, std::string::npos),
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002682 h_class_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07002683 &prepared);
2684 }
2685 std::cerr << std::endl;
2686 }
2687
2688 return true;
2689 }
2690
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002691 static bool DumpImtStats(Runtime* runtime,
2692 const std::vector<const DexFile*>& dex_files,
2693 Handle<mirror::ClassLoader> h_class_loader)
2694 REQUIRES_SHARED(Locks::mutator_lock_) {
2695 size_t without_imt = 0;
2696 size_t with_imt = 0;
Andreas Gampe9fded872016-09-25 16:08:35 -07002697 std::map<size_t, size_t> histogram;
2698
2699 ClassLinker* class_linker = runtime->GetClassLinker();
2700 const PointerSize pointer_size = class_linker->GetImagePointerSize();
2701 std::unordered_set<std::string> prepared;
2702
2703 Thread* self = Thread::Current();
Andreas Gampe9fded872016-09-25 16:08:35 -07002704 StackHandleScope<1> scope(self);
2705 MutableHandle<mirror::Class> h_klass(scope.NewHandle<mirror::Class>(nullptr));
2706
2707 for (const DexFile* dex_file : dex_files) {
2708 for (uint32_t class_def_index = 0;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002709 class_def_index != dex_file->NumClassDefs();
2710 ++class_def_index) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002711 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
2712 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002713 h_klass.Assign(class_linker->FindClass(self, descriptor, h_class_loader));
Andreas Gampe9fded872016-09-25 16:08:35 -07002714 if (h_klass.Get() == nullptr) {
2715 std::cerr << "Warning: could not load " << descriptor << std::endl;
2716 continue;
2717 }
2718
2719 if (HasNoIMT(runtime, h_klass, pointer_size, &prepared)) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002720 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07002721 continue;
2722 }
2723
2724 ImTable* im_table = PrepareAndGetImTable(runtime, h_klass, pointer_size, &prepared);
2725 if (im_table == nullptr) {
2726 // Should not happen, but accept.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002727 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07002728 continue;
2729 }
2730
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002731 with_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07002732 for (size_t imt_index = 0; imt_index != ImTable::kSize; ++imt_index) {
2733 ArtMethod* ptr = im_table->Get(imt_index, pointer_size);
2734 if (ptr->IsRuntimeMethod()) {
2735 if (ptr->IsImtUnimplementedMethod()) {
2736 histogram[0]++;
2737 } else {
2738 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
2739 histogram[current_table->NumEntries(pointer_size)]++;
2740 }
2741 } else {
2742 histogram[1]++;
2743 }
2744 }
2745 }
2746 }
2747
2748 std::cerr << "IMT stats:"
2749 << std::endl << std::endl;
2750
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002751 std::cerr << " " << with_imt << " classes with IMT."
Andreas Gampe9fded872016-09-25 16:08:35 -07002752 << std::endl << std::endl;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002753 std::cerr << " " << without_imt << " classes without IMT (or copy from Object)."
Andreas Gampe9fded872016-09-25 16:08:35 -07002754 << std::endl << std::endl;
2755
2756 double sum_one = 0;
2757 size_t count_one = 0;
2758
2759 std::cerr << " " << "IMT histogram" << std::endl;
2760 for (auto& bucket : histogram) {
2761 std::cerr << " " << bucket.first << " " << bucket.second << std::endl;
2762 if (bucket.first > 0) {
2763 sum_one += bucket.second * bucket.first;
2764 count_one += bucket.second;
2765 }
2766 }
2767
2768 double count_zero = count_one + histogram[0];
2769 std::cerr << " Stats:" << std::endl;
2770 std::cerr << " Average depth (including empty): " << (sum_one / count_zero) << std::endl;
2771 std::cerr << " Average depth (excluding empty): " << (sum_one / count_one) << std::endl;
2772
2773 return true;
2774 }
2775
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002776 // Return whether the given class has no IMT (or the one shared with java.lang.Object).
Andreas Gampe9fded872016-09-25 16:08:35 -07002777 static bool HasNoIMT(Runtime* runtime,
2778 Handle<mirror::Class> klass,
2779 const PointerSize pointer_size,
2780 std::unordered_set<std::string>* prepared)
2781 REQUIRES_SHARED(Locks::mutator_lock_) {
2782 if (klass->IsObjectClass() || !klass->ShouldHaveImt()) {
2783 return true;
2784 }
2785
2786 if (klass->GetImt(pointer_size) == nullptr) {
2787 PrepareClass(runtime, klass, prepared);
2788 }
2789
2790 mirror::Class* object_class = mirror::Class::GetJavaLangClass()->GetSuperClass();
2791 DCHECK(object_class->IsObjectClass());
2792
2793 bool result = klass->GetImt(pointer_size) == object_class->GetImt(pointer_size);
2794
Mathieu Chartier6beced42016-11-15 15:51:31 -08002795 if (klass->GetIfTable()->Count() == 0) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002796 DCHECK(result);
2797 }
2798
2799 return result;
2800 }
2801
2802 static void PrintTable(ImtConflictTable* table, PointerSize pointer_size)
2803 REQUIRES_SHARED(Locks::mutator_lock_) {
2804 if (table == nullptr) {
2805 std::cerr << " <No IMT?>" << std::endl;
2806 return;
2807 }
2808 size_t table_index = 0;
2809 for (;;) {
2810 ArtMethod* ptr = table->GetInterfaceMethod(table_index, pointer_size);
2811 if (ptr == nullptr) {
2812 return;
2813 }
2814 table_index++;
David Sehr709b0702016-10-13 09:12:37 -07002815 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07002816 }
2817 }
2818
2819 static ImTable* PrepareAndGetImTable(Runtime* runtime,
2820 Thread* self,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002821 Handle<mirror::ClassLoader> h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07002822 const std::string& class_name,
2823 const PointerSize pointer_size,
2824 mirror::Class** klass_out,
2825 std::unordered_set<std::string>* prepared)
2826 REQUIRES_SHARED(Locks::mutator_lock_) {
2827 if (class_name.empty()) {
2828 return nullptr;
2829 }
2830
2831 std::string descriptor;
2832 if (class_name[0] == 'L') {
2833 descriptor = class_name;
2834 } else {
2835 descriptor = DotToDescriptor(class_name.c_str());
2836 }
2837
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002838 mirror::Class* klass = runtime->GetClassLinker()->FindClass(self, descriptor.c_str(), h_loader);
Andreas Gampe9fded872016-09-25 16:08:35 -07002839
2840 if (klass == nullptr) {
2841 self->ClearException();
2842 std::cerr << "Did not find " << class_name << std::endl;
2843 *klass_out = nullptr;
2844 return nullptr;
2845 }
2846
2847 StackHandleScope<1> scope(Thread::Current());
2848 Handle<mirror::Class> h_klass = scope.NewHandle<mirror::Class>(klass);
2849
2850 ImTable* ret = PrepareAndGetImTable(runtime, h_klass, pointer_size, prepared);
2851 *klass_out = h_klass.Get();
2852 return ret;
2853 }
2854
2855 static ImTable* PrepareAndGetImTable(Runtime* runtime,
2856 Handle<mirror::Class> h_klass,
2857 const PointerSize pointer_size,
2858 std::unordered_set<std::string>* prepared)
2859 REQUIRES_SHARED(Locks::mutator_lock_) {
2860 PrepareClass(runtime, h_klass, prepared);
2861 return h_klass->GetImt(pointer_size);
2862 }
2863
2864 static void DumpIMTForClass(Runtime* runtime,
2865 const std::string& class_name,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002866 Handle<mirror::ClassLoader> h_loader,
2867 std::unordered_set<std::string>* prepared)
2868 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002869 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
2870 mirror::Class* klass;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002871 ImTable* imt = PrepareAndGetImTable(runtime,
2872 Thread::Current(),
2873 h_loader,
2874 class_name,
2875 pointer_size,
2876 &klass,
2877 prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07002878 if (imt == nullptr) {
2879 return;
2880 }
2881
2882 std::cerr << class_name << std::endl << " IMT:" << std::endl;
2883 for (size_t index = 0; index < ImTable::kSize; ++index) {
2884 std::cerr << " " << index << ":" << std::endl;
2885 ArtMethod* ptr = imt->Get(index, pointer_size);
2886 if (ptr->IsRuntimeMethod()) {
2887 if (ptr->IsImtUnimplementedMethod()) {
2888 std::cerr << " <empty>" << std::endl;
2889 } else {
2890 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
2891 PrintTable(current_table, pointer_size);
2892 }
2893 } else {
David Sehr709b0702016-10-13 09:12:37 -07002894 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07002895 }
2896 }
2897
2898 std::cerr << " Interfaces:" << std::endl;
2899 // Run through iftable, find methods that slot here, see if they fit.
2900 mirror::IfTable* if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08002901 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
2902 mirror::Class* iface = if_table->GetInterface(i);
2903 std::string iface_name;
2904 std::cerr << " " << iface->GetDescriptor(&iface_name) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07002905
Mathieu Chartier6beced42016-11-15 15:51:31 -08002906 for (ArtMethod& iface_method : iface->GetVirtualMethods(pointer_size)) {
2907 uint32_t class_hash, name_hash, signature_hash;
2908 ImTable::GetImtHashComponents(&iface_method, &class_hash, &name_hash, &signature_hash);
2909 uint32_t imt_slot = ImTable::GetImtIndex(&iface_method);
2910 std::cerr << " " << iface_method.PrettyMethod(true)
2911 << " slot=" << imt_slot
2912 << std::hex
2913 << " class_hash=0x" << class_hash
2914 << " name_hash=0x" << name_hash
2915 << " signature_hash=0x" << signature_hash
2916 << std::dec
2917 << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07002918 }
2919 }
2920 }
2921
2922 static void DumpIMTForMethod(Runtime* runtime,
2923 const std::string& class_name,
2924 const std::string& method,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002925 Handle<mirror::ClassLoader> h_loader,
2926 std::unordered_set<std::string>* prepared)
2927 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002928 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
2929 mirror::Class* klass;
2930 ImTable* imt = PrepareAndGetImTable(runtime,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002931 Thread::Current(),
2932 h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07002933 class_name,
2934 pointer_size,
2935 &klass,
2936 prepared);
2937 if (imt == nullptr) {
2938 return;
2939 }
2940
2941 std::cerr << class_name << " <" << method << ">" << std::endl;
2942 for (size_t index = 0; index < ImTable::kSize; ++index) {
2943 ArtMethod* ptr = imt->Get(index, pointer_size);
2944 if (ptr->IsRuntimeMethod()) {
2945 if (ptr->IsImtUnimplementedMethod()) {
2946 continue;
2947 }
2948
2949 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
2950 if (current_table == nullptr) {
2951 continue;
2952 }
2953
2954 size_t table_index = 0;
2955 for (;;) {
2956 ArtMethod* ptr2 = current_table->GetInterfaceMethod(table_index, pointer_size);
2957 if (ptr2 == nullptr) {
2958 break;
2959 }
2960 table_index++;
2961
David Sehr709b0702016-10-13 09:12:37 -07002962 std::string p_name = ptr2->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08002963 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002964 std::cerr << " Slot "
2965 << index
2966 << " ("
2967 << current_table->NumEntries(pointer_size)
2968 << ")"
2969 << std::endl;
2970 PrintTable(current_table, pointer_size);
2971 return;
2972 }
2973 }
2974 } else {
David Sehr709b0702016-10-13 09:12:37 -07002975 std::string p_name = ptr->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08002976 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002977 std::cerr << " Slot " << index << " (1)" << std::endl;
2978 std::cerr << " " << p_name << std::endl;
2979 } else {
2980 // Run through iftable, find methods that slot here, see if they fit.
2981 mirror::IfTable* if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08002982 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
2983 mirror::Class* iface = if_table->GetInterface(i);
2984 size_t num_methods = iface->NumDeclaredVirtualMethods();
2985 if (num_methods > 0) {
2986 for (ArtMethod& iface_method : iface->GetMethods(pointer_size)) {
2987 if (ImTable::GetImtIndex(&iface_method) == index) {
2988 std::string i_name = iface_method.PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08002989 if (android::base::StartsWith(i_name, method.c_str())) {
Mathieu Chartier6beced42016-11-15 15:51:31 -08002990 std::cerr << " Slot " << index << " (1)" << std::endl;
2991 std::cerr << " " << p_name << " (" << i_name << ")" << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07002992 }
2993 }
2994 }
2995 }
2996 }
2997 }
2998 }
2999 }
3000 }
3001
3002 // Read lines from the given stream, dropping comments and empty lines
3003 static std::vector<std::string> ReadCommentedInputStream(std::istream& in_stream) {
3004 std::vector<std::string> output;
3005 while (in_stream.good()) {
3006 std::string dot;
3007 std::getline(in_stream, dot);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003008 if (android::base::StartsWith(dot, "#") || dot.empty()) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003009 continue;
3010 }
3011 output.push_back(dot);
3012 }
3013 return output;
3014 }
3015
3016 // Read lines from the given file, dropping comments and empty lines.
3017 static std::vector<std::string> ReadCommentedInputFromFile(const std::string& input_filename) {
3018 std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
3019 if (input_file.get() == nullptr) {
3020 LOG(ERROR) << "Failed to open input file " << input_filename;
3021 return std::vector<std::string>();
3022 }
3023 std::vector<std::string> result = ReadCommentedInputStream(*input_file);
3024 input_file->close();
3025 return result;
3026 }
3027
3028 // Prepare a class, i.e., ensure it has a filled IMT. Will do so recursively for superclasses,
3029 // and note in the given set that the work was done.
3030 static void PrepareClass(Runtime* runtime,
3031 Handle<mirror::Class> h_klass,
3032 std::unordered_set<std::string>* done)
3033 REQUIRES_SHARED(Locks::mutator_lock_) {
3034 if (!h_klass->ShouldHaveImt()) {
3035 return;
3036 }
3037
3038 std::string name;
3039 name = h_klass->GetDescriptor(&name);
3040
3041 if (done->find(name) != done->end()) {
3042 return;
3043 }
3044 done->insert(name);
3045
3046 if (h_klass->HasSuperClass()) {
3047 StackHandleScope<1> h(Thread::Current());
3048 PrepareClass(runtime, h.NewHandle<mirror::Class>(h_klass->GetSuperClass()), done);
3049 }
3050
3051 if (!h_klass->IsTemp()) {
3052 runtime->GetClassLinker()->FillIMTAndConflictTables(h_klass.Get());
3053 }
3054 }
3055};
3056
Igor Murashkin37743352014-11-13 14:38:00 -08003057struct OatdumpArgs : public CmdlineArgs {
3058 protected:
3059 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003060
Igor Murashkin37743352014-11-13 14:38:00 -08003061 virtual ParseStatus ParseCustom(const StringPiece& option,
3062 std::string* error_msg) OVERRIDE {
3063 {
3064 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
3065 if (base_parse != kParseUnknownArgument) {
3066 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003067 }
3068 }
3069
Igor Murashkin37743352014-11-13 14:38:00 -08003070 if (option.starts_with("--oat-file=")) {
3071 oat_filename_ = option.substr(strlen("--oat-file=")).data();
3072 } else if (option.starts_with("--image=")) {
3073 image_location_ = option.substr(strlen("--image=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003074 } else if (option == "--no-dump:vmap") {
3075 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01003076 } else if (option =="--dump:code_info_stack_maps") {
3077 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003078 } else if (option == "--no-disassemble") {
3079 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003080 } else if (option =="--header-only") {
3081 dump_header_only_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003082 } else if (option.starts_with("--symbolize=")) {
3083 oat_filename_ = option.substr(strlen("--symbolize=")).data();
3084 symbolize_ = true;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003085 } else if (option.starts_with("--only-keep-debug")) {
3086 only_keep_debug_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003087 } else if (option.starts_with("--class-filter=")) {
3088 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003089 } else if (option.starts_with("--method-filter=")) {
3090 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003091 } else if (option.starts_with("--list-classes")) {
3092 list_classes_ = true;
3093 } else if (option.starts_with("--list-methods")) {
3094 list_methods_ = true;
3095 } else if (option.starts_with("--export-dex-to=")) {
3096 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
3097 } else if (option.starts_with("--addr2instr=")) {
3098 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
3099 *error_msg = "Address conversion failed";
3100 return kParseError;
3101 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003102 } else if (option.starts_with("--app-image=")) {
3103 app_image_ = option.substr(strlen("--app-image=")).data();
3104 } else if (option.starts_with("--app-oat=")) {
3105 app_oat_ = option.substr(strlen("--app-oat=")).data();
Andreas Gampe9fded872016-09-25 16:08:35 -07003106 } else if (option.starts_with("--dump-imt=")) {
3107 imt_dump_ = option.substr(strlen("--dump-imt=")).data();
3108 } else if (option == "--dump-imt-stats") {
3109 imt_stat_dump_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003110 } else {
3111 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003112 }
3113
Igor Murashkin37743352014-11-13 14:38:00 -08003114 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003115 }
3116
Igor Murashkin37743352014-11-13 14:38:00 -08003117 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
3118 // Infer boot image location from the image location if possible.
3119 if (boot_image_location_ == nullptr) {
3120 boot_image_location_ = image_location_;
3121 }
3122
3123 // Perform the parent checks.
3124 ParseStatus parent_checks = Base::ParseChecks(error_msg);
3125 if (parent_checks != kParseOk) {
3126 return parent_checks;
3127 }
3128
3129 // Perform our own checks.
3130 if (image_location_ == nullptr && oat_filename_ == nullptr) {
3131 *error_msg = "Either --image or --oat-file must be specified";
3132 return kParseError;
3133 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
3134 *error_msg = "Either --image or --oat-file must be specified but not both";
3135 return kParseError;
3136 }
3137
3138 return kParseOk;
3139 }
3140
3141 virtual std::string GetUsage() const {
3142 std::string usage;
3143
3144 usage +=
3145 "Usage: oatdump [options] ...\n"
3146 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
3147 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
3148 "\n"
3149 // Either oat-file or image is required.
3150 " --oat-file=<file.oat>: specifies an input oat filename.\n"
3151 " Example: --oat-file=/system/framework/boot.oat\n"
3152 "\n"
3153 " --image=<file.art>: specifies an input image location.\n"
3154 " Example: --image=/system/framework/boot.art\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003155 "\n"
3156 " --app-image=<file.art>: specifies an input app image. Must also have a specified\n"
3157 " boot image and app oat file.\n"
3158 " Example: --app-image=app.art\n"
3159 "\n"
3160 " --app-oat=<file.odex>: specifies an input app oat.\n"
3161 " Example: --app-oat=app.odex\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003162 "\n";
3163
3164 usage += Base::GetUsage();
3165
3166 usage += // Optional.
Igor Murashkin37743352014-11-13 14:38:00 -08003167 " --no-dump:vmap may be used to disable vmap dumping.\n"
3168 " Example: --no-dump:vmap\n"
3169 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01003170 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
3171 " Example: --dump:code_info_stack_maps\n"
3172 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003173 " --no-disassemble may be used to disable disassembly.\n"
3174 " Example: --no-disassemble\n"
3175 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00003176 " --header-only may be used to print only the oat header.\n"
3177 " Example: --header-only\n"
3178 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003179 " --list-classes may be used to list target file classes (can be used with filters).\n"
3180 " Example: --list-classes\n"
3181 " Example: --list-classes --class-filter=com.example.foo\n"
3182 "\n"
3183 " --list-methods may be used to list target file methods (can be used with filters).\n"
3184 " Example: --list-methods\n"
3185 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
3186 "\n"
3187 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
3188 " Example: --symbolize=/system/framework/boot.oat\n"
3189 "\n"
David Srbecky2fdd03c2016-03-10 15:32:37 +00003190 " --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
3191 " .rodata and .text sections are omitted in the output file to save space.\n"
3192 " Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
3193 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003194 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
3195 " Example: --class-filter=com.example.foo\n"
3196 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003197 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
3198 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003199 "\n"
3200 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
3201 " Example: --export-dex-to=/data/local/tmp\n"
3202 "\n"
3203 " --addr2instr=<address>: output matching method disassembled code from relative\n"
3204 " address (e.g. PC from crash dump)\n"
3205 " Example: --addr2instr=0x00001a3b\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003206 "\n"
3207 " --dump-imt=<file.txt>: output IMT collisions (if any) for the given receiver\n"
3208 " types and interface methods in the given file. The file\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003209 " is read line-wise, where each line should either be a class\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003210 " name or descriptor, or a class name/descriptor and a prefix\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003211 " of a complete method name (separated by a whitespace).\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003212 " Example: --dump-imt=imt.txt\n"
3213 "\n"
3214 " --dump-imt-stats: output IMT statistics for the given boot image\n"
3215 " Example: --dump-imt-stats"
Igor Murashkin37743352014-11-13 14:38:00 -08003216 "\n";
3217
3218 return usage;
3219 }
3220
3221 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07003222 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003223 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00003224 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07003225 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003226 std::string elf_filename_prefix_;
Andreas Gampe9fded872016-09-25 16:08:35 -07003227 std::string imt_dump_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003228 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01003229 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003230 bool disassemble_code_ = true;
3231 bool symbolize_ = false;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003232 bool only_keep_debug_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003233 bool list_classes_ = false;
3234 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003235 bool dump_header_only_ = false;
Andreas Gampe9fded872016-09-25 16:08:35 -07003236 bool imt_stat_dump_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003237 uint32_t addr2instr_ = 0;
3238 const char* export_dex_location_ = nullptr;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003239 const char* app_image_ = nullptr;
3240 const char* app_oat_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003241};
3242
Igor Murashkin37743352014-11-13 14:38:00 -08003243struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
3244 virtual bool NeedsRuntime() OVERRIDE {
3245 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003246
Igor Murashkin37743352014-11-13 14:38:00 -08003247 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
3248 bool absolute_addresses = (args_->oat_filename_ == nullptr);
3249
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003250 oat_dumper_options_.reset(new OatDumperOptions(
Igor Murashkin37743352014-11-13 14:38:00 -08003251 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01003252 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08003253 args_->disassemble_code_,
3254 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003255 args_->class_filter_,
3256 args_->method_filter_,
3257 args_->list_classes_,
3258 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00003259 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003260 args_->export_dex_location_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003261 args_->app_image_,
3262 args_->app_oat_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003263 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08003264
Andreas Gampe9fded872016-09-25 16:08:35 -07003265 return (args_->boot_image_location_ != nullptr ||
3266 args_->image_location_ != nullptr ||
3267 !args_->imt_dump_.empty()) &&
Igor Murashkin37743352014-11-13 14:38:00 -08003268 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003269 }
3270
Igor Murashkin37743352014-11-13 14:38:00 -08003271 virtual bool ExecuteWithoutRuntime() OVERRIDE {
3272 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08003273 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003274
Mathieu Chartierd424d082014-10-15 10:31:46 -07003275 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08003276
Andreas Gampec24f3992014-12-17 20:40:11 -08003277 if (args_->symbolize_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00003278 // ELF has special kind of section called SHT_NOBITS which allows us to create
3279 // sections which exist but their data is omitted from the ELF file to save space.
3280 // This is what "strip --only-keep-debug" does when it creates separate ELF file
3281 // with only debug data. We use it in similar way to exclude .rodata and .text.
3282 bool no_bits = args_->only_keep_debug_;
3283 return SymbolizeOat(args_->oat_filename_, args_->output_name_, no_bits) == EXIT_SUCCESS;
Andreas Gampec24f3992014-12-17 20:40:11 -08003284 } else {
3285 return DumpOat(nullptr,
3286 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003287 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08003288 args_->os_) == EXIT_SUCCESS;
3289 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07003290 }
3291
Igor Murashkin37743352014-11-13 14:38:00 -08003292 virtual bool ExecuteWithRuntime(Runtime* runtime) {
3293 CHECK(args_ != nullptr);
3294
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003295 if (!args_->imt_dump_.empty() || args_->imt_stat_dump_) {
3296 return IMTDumper::Dump(runtime,
3297 args_->imt_dump_,
3298 args_->imt_stat_dump_,
3299 args_->oat_filename_);
Andreas Gampe9fded872016-09-25 16:08:35 -07003300 }
3301
Igor Murashkin37743352014-11-13 14:38:00 -08003302 if (args_->oat_filename_ != nullptr) {
3303 return DumpOat(runtime,
3304 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003305 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08003306 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003307 }
Igor Murashkin37743352014-11-13 14:38:00 -08003308
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003309 return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003310 }
3311
Igor Murashkin37743352014-11-13 14:38:00 -08003312 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
3313};
Andreas Gampe00b25f32014-09-17 21:49:05 -07003314
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003315} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07003316
3317int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08003318 art::OatdumpMain main;
3319 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07003320}