blob: 148ee886696c665c068b2d20dba739f785647689 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrom78128a62011-09-15 17:21:19 -070016
17#include <stdio.h>
18#include <stdlib.h>
19
Brian Carlstrom27ec9612011-09-19 20:20:38 -070020#include <fstream>
21#include <iostream>
Igor Murashkin37743352014-11-13 14:38:00 -080022#include <map>
23#include <set>
Brian Carlstrom78128a62011-09-15 17:21:19 -070024#include <string>
Andreas Gampe54fc26c2014-09-04 21:47:42 -070025#include <unordered_map>
Andreas Gampe9fded872016-09-25 16:08:35 -070026#include <unordered_set>
Brian Carlstrom78128a62011-09-15 17:21:19 -070027#include <vector>
28
Andreas Gampe46ee31b2016-12-14 10:11:49 -080029#include "android-base/stringprintf.h"
Andreas Gampe9186ced2016-12-12 14:28:21 -080030#include "android-base/strings.h"
31
Ian Rogersd582fa42014-11-05 23:46:43 -080032#include "arch/instruction_set_features.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070033#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070034#include "art_method-inl.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000035#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080036#include "base/unix_file/fd_file.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070037#include "class_linker.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038#include "class_linker-inl.h"
David Srbecky5d950762016-03-07 20:47:29 +000039#include "debug/elf_debug_writer.h"
40#include "debug/method_debug_info.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070041#include "dex_file-inl.h"
Christina Wadsworthbc233ac2016-06-20 15:01:32 -070042#include "dex_instruction-inl.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080043#include "disassembler.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070044#include "elf_builder.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070045#include "gc/space/image_space.h"
46#include "gc/space/large_object_space.h"
47#include "gc/space/space-inl.h"
Mathieu Chartier4a26f172016-01-26 14:26:18 -080048#include "image-inl.h"
Andreas Gampe9fded872016-09-25 16:08:35 -070049#include "imtable-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080050#include "indenter.h"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -070051#include "interpreter/unstarted_runtime.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000052#include "linker/buffered_output_stream.h"
53#include "linker/file_output_stream.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080054#include "mirror/array-inl.h"
55#include "mirror/class-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010056#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080057#include "mirror/object-inl.h"
58#include "mirror/object_array-inl.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080059#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010060#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070061#include "oat_file_manager.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080062#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070063#include "safe_map.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070064#include "scoped_thread_state_change-inl.h"
Nicolas Geoffray9c055782016-07-14 09:24:30 +000065#include "ScopedLocalRef.h"
Mathieu Chartierdc00f182016-07-14 10:10:44 -070066#include "stack_map.h"
67#include "string_reference.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080068#include "thread_list.h"
Andreas Gampe2ba88952016-04-29 17:52:07 -070069#include "type_lookup_table.h"
Nicolas Geoffray4acefd32016-10-24 13:14:58 +010070#include "vdex_file.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080071#include "verifier/method_verifier.h"
Nicolas Geoffraye70dd562016-10-30 21:03:35 +000072#include "verifier/verifier_deps.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070073#include "well_known_classes.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070074
Igor Murashkin37743352014-11-13 14:38:00 -080075#include <sys/stat.h>
76#include "cmdline.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070077
Igor Murashkin37743352014-11-13 14:38:00 -080078namespace art {
Brian Carlstrom78128a62011-09-15 17:21:19 -070079
Andreas Gampe46ee31b2016-12-14 10:11:49 -080080using android::base::StringPrintf;
81
Mathieu Chartiere401d142015-04-22 13:56:20 -070082const char* image_methods_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -080083 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -070084 "kImtConflictMethod",
Mathieu Chartier2d2621a2014-10-23 16:48:06 -070085 "kImtUnimplementedMethod",
Vladimir Markofd36f1f2016-08-03 18:49:58 +010086 "kSaveAllCalleeSavesMethod",
87 "kSaveRefsOnlyMethod",
88 "kSaveRefsAndArgsMethod",
Vladimir Marko952dbb12016-07-28 12:01:51 +010089 "kSaveEverythingMethod",
Mathieu Chartiere401d142015-04-22 13:56:20 -070090};
91
92const char* image_roots_descriptions_[] = {
Brian Carlstrom58ae9412011-10-04 00:56:06 -070093 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070094 "kClassRoots",
Vladimir Markoeca3eda2016-11-09 16:26:44 +000095 "kClassLoader",
Brian Carlstrom78128a62011-09-15 17:21:19 -070096};
97
Mathieu Chartierac8f4392015-08-27 13:54:20 -070098// Map is so that we don't allocate multiple dex files for the same OatDexFile.
99static std::map<const OatFile::OatDexFile*,
100 std::unique_ptr<const DexFile>> opened_dex_files;
101
102const DexFile* OpenDexFile(const OatFile::OatDexFile* oat_dex_file, std::string* error_msg) {
103 DCHECK(oat_dex_file != nullptr);
104 auto it = opened_dex_files.find(oat_dex_file);
105 if (it != opened_dex_files.end()) {
106 return it->second.get();
107 }
108 const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
109 opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
110 return ret;
111}
112
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800113template <typename ElfTypes>
David Srbeckybc90fd02015-04-22 19:40:27 +0100114class OatSymbolizer FINAL {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700115 public:
David Srbecky2fdd03c2016-03-10 15:32:37 +0000116 OatSymbolizer(const OatFile* oat_file, const std::string& output_name, bool no_bits) :
117 oat_file_(oat_file),
118 builder_(nullptr),
119 output_name_(output_name.empty() ? "symbolized.oat" : output_name),
120 no_bits_(no_bits) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700121 }
122
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700123 bool Symbolize() {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000124 const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800125 std::unique_ptr<const InstructionSetFeatures> features = InstructionSetFeatures::FromBitmap(
David Srbecky5d811202016-03-08 13:21:22 +0000126 isa, oat_file_->GetOatHeader().GetInstructionSetFeaturesBitmap());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000127
128 File* elf_file = OS::CreateEmptyFile(output_name_.c_str());
129 std::unique_ptr<BufferedOutputStream> output_stream(
Vladimir Marko10c13562015-11-25 14:33:36 +0000130 MakeUnique<BufferedOutputStream>(MakeUnique<FileOutputStream>(elf_file)));
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800131 builder_.reset(new ElfBuilder<ElfTypes>(isa, features.get(), output_stream.get()));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000132
133 builder_->Start();
134
135 auto* rodata = builder_->GetRoData();
136 auto* text = builder_->GetText();
137 auto* bss = builder_->GetBss();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000138
David Srbecky6d8c8f02015-10-26 10:57:09 +0000139 const uint8_t* rodata_begin = oat_file_->Begin();
140 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
David Srbecky2fdd03c2016-03-10 15:32:37 +0000141 if (no_bits_) {
142 rodata->WriteNoBitsSection(rodata_size);
143 } else {
144 rodata->Start();
145 rodata->WriteFully(rodata_begin, rodata_size);
146 rodata->End();
147 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000148
David Srbecky6d8c8f02015-10-26 10:57:09 +0000149 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
150 const size_t text_size = oat_file_->End() - text_begin;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000151 if (no_bits_) {
152 text->WriteNoBitsSection(text_size);
153 } else {
154 text->Start();
155 text->WriteFully(text_begin, text_size);
156 text->End();
157 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000158
159 if (oat_file_->BssSize() != 0) {
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000160 bss->WriteNoBitsSection(oat_file_->BssSize());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000161 }
162
Douglas Leung316a2182015-09-17 15:26:25 -0700163 if (isa == kMips || isa == kMips64) {
164 builder_->WriteMIPSabiflagsSection();
165 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000166 builder_->PrepareDynamicSection(elf_file->GetPath(),
167 rodata_size,
168 text_size,
169 oat_file_->BssSize(),
170 oat_file_->BssRootsOffset());
Vladimir Marko944da602016-02-19 12:27:55 +0000171 builder_->WriteDynamicSection();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700172
David Srbecky5d950762016-03-07 20:47:29 +0000173 Walk();
174 for (const auto& trampoline : debug::MakeTrampolineInfos(oat_file_->GetOatHeader())) {
175 method_debug_infos_.push_back(trampoline);
176 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700177
David Srbecky5d950762016-03-07 20:47:29 +0000178 debug::WriteDebugInfo(builder_.get(),
179 ArrayRef<const debug::MethodDebugInfo>(method_debug_infos_),
180 dwarf::DW_DEBUG_FRAME_FORMAT,
181 true /* write_oat_patches */);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700182
David Srbecky6d8c8f02015-10-26 10:57:09 +0000183 builder_->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700184
Vladimir Marko10c13562015-11-25 14:33:36 +0000185 return builder_->Good();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700186 }
187
David Srbecky5d950762016-03-07 20:47:29 +0000188 void Walk() {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700189 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
190 for (size_t i = 0; i < oat_dex_files.size(); i++) {
191 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700192 CHECK(oat_dex_file != nullptr);
David Srbecky5d950762016-03-07 20:47:29 +0000193 WalkOatDexFile(oat_dex_file);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700194 }
195 }
196
David Srbecky5d950762016-03-07 20:47:29 +0000197 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700198 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700199 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
200 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700201 return;
202 }
203 for (size_t class_def_index = 0;
204 class_def_index < dex_file->NumClassDefs();
205 class_def_index++) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700206 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
207 OatClassType type = oat_class.GetType();
208 switch (type) {
209 case kOatClassAllCompiled:
210 case kOatClassSomeCompiled:
David Srbecky5d950762016-03-07 20:47:29 +0000211 WalkOatClass(oat_class, *dex_file, class_def_index);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700212 break;
213
214 case kOatClassNoneCompiled:
215 case kOatClassMax:
216 // Ignore.
217 break;
218 }
219 }
220 }
221
David Srbecky5d950762016-03-07 20:47:29 +0000222 void WalkOatClass(const OatFile::OatClass& oat_class,
223 const DexFile& dex_file,
224 uint32_t class_def_index) {
225 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700226 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700227 if (class_data == nullptr) { // empty class such as a marker interface?
228 return;
229 }
230 // Note: even if this is an interface or a native class, we still have to walk it, as there
231 // might be a static initializer.
232 ClassDataItemIterator it(dex_file, class_data);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700233 uint32_t class_method_idx = 0;
David Srbecky5d950762016-03-07 20:47:29 +0000234 for (; it.HasNextStaticField(); it.Next()) { /* skip */ }
235 for (; it.HasNextInstanceField(); it.Next()) { /* skip */ }
236 for (; it.HasNextDirectMethod() || it.HasNextVirtualMethod(); it.Next()) {
237 WalkOatMethod(oat_class.GetOatMethod(class_method_idx++),
238 dex_file,
239 class_def_index,
240 it.GetMemberIndex(),
241 it.GetMethodCodeItem(),
242 it.GetMethodAccessFlags());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700243 }
244 DCHECK(!it.HasNext());
245 }
246
David Srbecky5d950762016-03-07 20:47:29 +0000247 void WalkOatMethod(const OatFile::OatMethod& oat_method,
248 const DexFile& dex_file,
249 uint32_t class_def_index,
250 uint32_t dex_method_index,
251 const DexFile::CodeItem* code_item,
252 uint32_t method_access_flags) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700253 if ((method_access_flags & kAccAbstract) != 0) {
254 // Abstract method, no code.
255 return;
256 }
David Srbecky5d950762016-03-07 20:47:29 +0000257 const OatHeader& oat_header = oat_file_->GetOatHeader();
258 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
259 if (method_header == nullptr || method_header->GetCodeSize() == 0) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700260 // No code.
261 return;
262 }
263
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100264 uint32_t entry_point = oat_method.GetCodeOffset() - oat_header.GetExecutableOffset();
265 // Clear Thumb2 bit.
266 const void* code_address = EntryPointToCodePointer(reinterpret_cast<void*>(entry_point));
267
David Srbecky5d950762016-03-07 20:47:29 +0000268 debug::MethodDebugInfo info = debug::MethodDebugInfo();
269 info.trampoline_name = nullptr;
270 info.dex_file = &dex_file;
271 info.class_def_index = class_def_index;
272 info.dex_method_index = dex_method_index;
273 info.access_flags = method_access_flags;
274 info.code_item = code_item;
275 info.isa = oat_header.GetInstructionSet();
276 info.deduped = !seen_offsets_.insert(oat_method.GetCodeOffset()).second;
277 info.is_native_debuggable = oat_header.IsNativeDebuggable();
278 info.is_optimized = method_header->IsOptimized();
279 info.is_code_address_text_relative = true;
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100280 info.code_address = reinterpret_cast<uintptr_t>(code_address);
David Srbecky5d950762016-03-07 20:47:29 +0000281 info.code_size = method_header->GetCodeSize();
282 info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
283 info.code_info = info.is_optimized ? method_header->GetOptimizedCodeInfoPtr() : nullptr;
284 info.cfi = ArrayRef<uint8_t>();
285 method_debug_infos_.push_back(info);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700286 }
287
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700288 private:
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700289 const OatFile* oat_file_;
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800290 std::unique_ptr<ElfBuilder<ElfTypes> > builder_;
David Srbecky5d950762016-03-07 20:47:29 +0000291 std::vector<debug::MethodDebugInfo> method_debug_infos_;
292 std::unordered_set<uint32_t> seen_offsets_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700293 const std::string output_name_;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000294 bool no_bits_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700295};
296
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700297class OatDumperOptions {
298 public:
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100299 OatDumperOptions(bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100300 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700301 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700302 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800303 const char* class_filter,
304 const char* method_filter,
305 bool list_classes,
306 bool list_methods,
David Brazdilc03d7b62016-03-02 12:18:03 +0000307 bool dump_header_only,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800308 const char* export_dex_location,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800309 const char* app_image,
310 const char* app_oat,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800311 uint32_t addr2instr)
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100312 : dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100313 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700314 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700315 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800316 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000317 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800318 list_classes_(list_classes),
319 list_methods_(list_methods),
David Brazdilc03d7b62016-03-02 12:18:03 +0000320 dump_header_only_(dump_header_only),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800321 export_dex_location_(export_dex_location),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800322 app_image_(app_image),
323 app_oat_(app_oat),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800324 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800325 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700326
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700327 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100328 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700329 const bool disassemble_code_;
330 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800331 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000332 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800333 const bool list_classes_;
334 const bool list_methods_;
David Brazdilc03d7b62016-03-02 12:18:03 +0000335 const bool dump_header_only_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800336 const char* const export_dex_location_;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800337 const char* const app_image_;
338 const char* const app_oat_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800339 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700340 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700341};
342
Elliott Hughese3c845c2012-02-28 17:23:01 -0800343class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700344 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100345 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000346 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800347 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700348 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800349 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800350 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
351 disassembler_(Disassembler::Create(instruction_set_,
Andreas Gampe372f3a32016-08-19 10:49:06 -0700352 new DisassemblerOptions(
353 options_.absolute_addresses_,
354 oat_file.Begin(),
355 oat_file.End(),
356 true /* can_read_literals_ */,
357 Is64BitInstructionSet(instruction_set_)
358 ? &Thread::DumpThreadOffset<PointerSize::k64>
359 : &Thread::DumpThreadOffset<PointerSize::k32>))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800360 CHECK(options_.class_loader_ != nullptr);
361 CHECK(options_.class_filter_ != nullptr);
362 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800363 AddAllOffsets();
364 }
365
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700366 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700367 delete disassembler_;
368 }
369
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800370 InstructionSet GetInstructionSet() {
371 return instruction_set_;
372 }
373
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700374 bool Dump(std::ostream& os) {
375 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800376 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700377
378 os << "MAGIC:\n";
379 os << oat_header.GetMagic() << "\n\n";
380
Jeff Hao4b07a6e2016-01-15 12:50:44 -0800381 os << "LOCATION:\n";
382 os << oat_file_.GetLocation() << "\n\n";
383
Brian Carlstromaded5f72011-10-07 17:15:04 -0700384 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800385 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700386
Elliott Hughesa72ec822012-03-05 17:12:22 -0800387 os << "INSTRUCTION SET:\n";
388 os << oat_header.GetInstructionSet() << "\n\n";
389
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700390 {
391 std::unique_ptr<const InstructionSetFeatures> features(
392 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
393 oat_header.GetInstructionSetFeaturesBitmap()));
394 os << "INSTRUCTION SET FEATURES:\n";
395 os << features->GetFeatureString() << "\n\n";
396 }
Dave Allison70202782013-10-22 17:52:19 -0700397
Brian Carlstromaded5f72011-10-07 17:15:04 -0700398 os << "DEX FILE COUNT:\n";
399 os << oat_header.GetDexFileCount() << "\n\n";
400
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800401#define DUMP_OAT_HEADER_OFFSET(label, offset) \
402 os << label " OFFSET:\n"; \
403 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800404 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800405 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
406 } \
407 os << StringPrintf("\n\n");
408
409 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
410 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
411 GetInterpreterToInterpreterBridgeOffset);
412 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
413 GetInterpreterToCompiledCodeBridgeOffset);
414 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
415 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800416 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
417 GetQuickGenericJniTrampolineOffset);
418 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
419 GetQuickImtConflictTrampolineOffset);
420 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
421 GetQuickResolutionTrampolineOffset);
422 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
423 GetQuickToInterpreterBridgeOffset);
424#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700425
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700426 os << "IMAGE PATCH DELTA:\n";
427 os << StringPrintf("%d (0x%08x)\n\n",
428 oat_header.GetImagePatchDelta(),
429 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700430
Brian Carlstrom28db0122012-10-18 16:20:41 -0700431 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
432 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
433
434 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800435 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700436
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700437 // Print the key-value store.
438 {
439 os << "KEY VALUE STORE:\n";
440 size_t index = 0;
441 const char* key;
442 const char* value;
443 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
444 os << key << " = " << value << "\n";
445 index++;
446 }
447 os << "\n";
448 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700449
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800450 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700451 os << "BEGIN:\n";
452 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700453
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700454 os << "END:\n";
455 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
456 }
457
458 os << "SIZE:\n";
459 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700460
461 os << std::flush;
462
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800463 // If set, adjust relative address to be searched
464 if (options_.addr2instr_ != 0) {
465 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
466 os << "SEARCH ADDRESS (executable offset + input):\n";
467 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
468 }
469
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700470 // Dumping the dex file overview is compact enough to do even if header only.
471 DexFileData cumulative;
472 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
473 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
474 CHECK(oat_dex_file != nullptr);
475 std::string error_msg;
476 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
477 if (dex_file == nullptr) {
478 os << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation() << "': "
479 << error_msg;
480 continue;
481 }
482 DexFileData data(*dex_file);
483 os << "Dex file data for " << dex_file->GetLocation() << "\n";
484 data.Dump(os);
485 os << "\n";
486 cumulative.Add(data);
487 }
488 os << "Cumulative dex file data\n";
489 cumulative.Dump(os);
490 os << "\n";
491
David Brazdilc03d7b62016-03-02 12:18:03 +0000492 if (!options_.dump_header_only_) {
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000493 VariableIndentationOutputStream vios(&os);
494 VdexFile::Header vdex_header = oat_file_.GetVdexFile()->GetHeader();
495 if (vdex_header.IsValid()) {
496 std::string error_msg;
497 std::vector<const DexFile*> dex_files;
498 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
499 const DexFile* dex_file = OpenDexFile(oat_dex_files_[i], &error_msg);
500 if (dex_file == nullptr) {
501 os << "Error opening dex file: " << error_msg << std::endl;
502 return false;
503 }
504 dex_files.push_back(dex_file);
505 }
506 verifier::VerifierDeps deps(dex_files, oat_file_.GetVdexFile()->GetVerifierDepsData());
507 deps.Dump(&vios);
508 } else {
509 os << "UNRECOGNIZED vdex file, magic "
510 << vdex_header.GetMagic()
511 << ", version "
512 << vdex_header.GetVersion()
513 << "\n";
514 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000515 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
516 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
517 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800518
David Brazdilc03d7b62016-03-02 12:18:03 +0000519 // If file export selected skip file analysis
520 if (options_.export_dex_location_) {
521 if (!ExportDexFile(os, *oat_dex_file)) {
522 success = false;
523 }
524 } else {
525 if (!DumpOatDexFile(os, *oat_dex_file)) {
526 success = false;
527 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800528 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700529 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700530 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000531
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700532 os << std::flush;
533 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700534 }
535
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800536 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700537 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
538 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800539 return 0; // Address not in oat file
540 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800541 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
542 reinterpret_cast<uintptr_t>(oat_file_.Begin());
543 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800544 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800545 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800546 return end_offset - begin_offset;
547 }
548
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800549 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700550 return oat_file_.GetOatHeader().GetInstructionSet();
551 }
552
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700553 const void* GetQuickOatCode(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800554 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
555 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700556 CHECK(oat_dex_file != nullptr);
557 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700558 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
559 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700560 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
561 << "': " << error_msg;
562 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800563 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700564 const DexFile::ClassDef* class_def =
David Sehr9aa352e2016-09-15 18:13:52 -0700565 OatDexFile::FindClassDef(*dex_file, descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700566 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700567 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100568 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800569 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100570 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800571 }
572 }
573 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700574 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800575 }
576
Brian Carlstromaded5f72011-10-07 17:15:04 -0700577 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800578 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800579 // We don't know the length of the code for each method, but we need to know where to stop
580 // when disassembling. What we do know is that a region of code will be followed by some other
581 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
582 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800583 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
584 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700585 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700586 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700587 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
588 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700589 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
590 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800591 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800592 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800593 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800594 for (size_t class_def_index = 0;
595 class_def_index < dex_file->NumClassDefs();
596 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800597 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100598 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700599 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700600 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800601 ClassDataItemIterator it(*dex_file, class_data);
602 SkipAllFields(it);
603 uint32_t class_method_index = 0;
604 while (it.HasNextDirectMethod()) {
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 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100609 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800610 it.Next();
611 }
612 }
613 }
614 }
615
616 // If the last thing in the file is code for a method, there won't be an offset for the "next"
617 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
618 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800619 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800620 }
621
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700622 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
623 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
624 }
625
Elliott Hughese3c845c2012-02-28 17:23:01 -0800626 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800627 uint32_t code_offset = oat_method.GetCodeOffset();
628 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
629 code_offset &= ~0x1;
630 }
631 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800632 offsets_.insert(oat_method.GetVmapTableOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800633 }
634
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700635 // Dex file data, may be for multiple different dex files.
636 class DexFileData {
637 public:
638 DexFileData() {}
639
640 explicit DexFileData(const DexFile& dex_file)
641 : num_string_ids_(dex_file.NumStringIds()),
642 num_method_ids_(dex_file.NumMethodIds()),
643 num_field_ids_(dex_file.NumFieldIds()),
644 num_type_ids_(dex_file.NumTypeIds()),
645 num_class_defs_(dex_file.NumClassDefs()) {
646 for (size_t class_def_index = 0; class_def_index < num_class_defs_; ++class_def_index) {
647 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
648 WalkClass(dex_file, class_def);
649 }
650 }
651
652 void Add(const DexFileData& other) {
653 AddAll(unique_string_ids_from_code_, other.unique_string_ids_from_code_);
654 num_string_ids_from_code_ += other.num_string_ids_from_code_;
655 AddAll(dex_code_item_ptrs_, other.dex_code_item_ptrs_);
656 dex_code_bytes_ += other.dex_code_bytes_;
657 num_string_ids_ += other.num_string_ids_;
658 num_method_ids_ += other.num_method_ids_;
659 num_field_ids_ += other.num_field_ids_;
660 num_type_ids_ += other.num_type_ids_;
661 num_class_defs_ += other.num_class_defs_;
662 }
663
664 void Dump(std::ostream& os) {
665 os << "Num string ids: " << num_string_ids_ << "\n";
666 os << "Num method ids: " << num_method_ids_ << "\n";
667 os << "Num field ids: " << num_field_ids_ << "\n";
668 os << "Num type ids: " << num_type_ids_ << "\n";
669 os << "Num class defs: " << num_class_defs_ << "\n";
670 os << "Unique strings loaded from dex code: " << unique_string_ids_from_code_.size() << "\n";
671 os << "Total strings loaded from dex code: " << num_string_ids_from_code_ << "\n";
672 os << "Number of unique dex code items: " << dex_code_item_ptrs_.size() << "\n";
673 os << "Total number of dex code bytes: " << dex_code_bytes_ << "\n";
674 }
675
676 private:
Andreas Gampe9186ced2016-12-12 14:28:21 -0800677 // All of the elements from one container to another.
678 template <typename Dest, typename Src>
679 static void AddAll(Dest& dest, const Src& src) {
680 dest.insert(src.begin(), src.end());
681 }
682
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700683 void WalkClass(const DexFile& dex_file, const DexFile::ClassDef& class_def) {
684 const uint8_t* class_data = dex_file.GetClassData(class_def);
685 if (class_data == nullptr) { // empty class such as a marker interface?
686 return;
687 }
688 ClassDataItemIterator it(dex_file, class_data);
689 SkipAllFields(it);
690 while (it.HasNextDirectMethod()) {
691 WalkCodeItem(dex_file, it.GetMethodCodeItem());
692 it.Next();
693 }
694 while (it.HasNextVirtualMethod()) {
695 WalkCodeItem(dex_file, it.GetMethodCodeItem());
696 it.Next();
697 }
698 DCHECK(!it.HasNext());
699 }
700
701 void WalkCodeItem(const DexFile& dex_file, const DexFile::CodeItem* code_item) {
702 if (code_item == nullptr) {
703 return;
704 }
705 const size_t code_item_size = code_item->insns_size_in_code_units_;
706 const uint16_t* code_ptr = code_item->insns_;
707 const uint16_t* code_end = code_item->insns_ + code_item_size;
708
709 // If we inserted a new dex code item pointer, add to total code bytes.
710 if (dex_code_item_ptrs_.insert(code_ptr).second) {
711 dex_code_bytes_ += code_item_size * sizeof(code_ptr[0]);
712 }
713
714 while (code_ptr < code_end) {
715 const Instruction* inst = Instruction::At(code_ptr);
716 switch (inst->Opcode()) {
717 case Instruction::CONST_STRING: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800718 const dex::StringIndex string_index(inst->VRegB_21c());
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700719 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
720 ++num_string_ids_from_code_;
721 break;
722 }
723 case Instruction::CONST_STRING_JUMBO: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800724 const dex::StringIndex string_index(inst->VRegB_31c());
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700725 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
726 ++num_string_ids_from_code_;
727 break;
728 }
729 default:
730 break;
731 }
732
733 code_ptr += inst->SizeInCodeUnits();
734 }
735 }
736
737 // Unique string ids loaded from dex code.
738 std::set<StringReference, StringReferenceComparator> unique_string_ids_from_code_;
739
740 // Total string ids loaded from dex code.
741 size_t num_string_ids_from_code_ = 0;
742
743 // Unique code pointers.
744 std::set<const void*> dex_code_item_ptrs_;
745
746 // Total "unique" dex code bytes.
747 size_t dex_code_bytes_ = 0;
748
749 // Other dex ids.
750 size_t num_string_ids_ = 0;
751 size_t num_method_ids_ = 0;
752 size_t num_field_ids_ = 0;
753 size_t num_type_ids_ = 0;
754 size_t num_class_defs_ = 0;
755 };
756
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700757 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
758 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800759 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700760 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800761 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800762 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700763
Andreas Gampe2ba88952016-04-29 17:52:07 -0700764 const uint8_t* const oat_file_begin = oat_dex_file.GetOatFile()->Begin();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100765 const uint8_t* const vdex_file_begin = oat_dex_file.GetOatFile()->DexBegin();
766
767 // Print data range of the dex file embedded inside the corresponding vdex file.
Andreas Gampe2ba88952016-04-29 17:52:07 -0700768 const uint8_t* const dex_file_pointer = oat_dex_file.GetDexFilePointer();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100769 uint32_t dex_offset = dchecked_integral_cast<uint32_t>(dex_file_pointer - vdex_file_begin);
Andreas Gampe2ba88952016-04-29 17:52:07 -0700770 os << StringPrintf("dex-file: 0x%08x..0x%08x\n",
771 dex_offset,
772 dchecked_integral_cast<uint32_t>(dex_offset + oat_dex_file.FileSize() - 1));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700773
Andreas Gampe2ba88952016-04-29 17:52:07 -0700774 // Create the dex file early. A lot of print-out things depend on it.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700775 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700776 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
777 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700778 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700779 os << std::flush;
780 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700781 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100782
Andreas Gampe2ba88952016-04-29 17:52:07 -0700783 // Print lookup table, if it exists.
784 if (oat_dex_file.GetLookupTableData() != nullptr) {
785 uint32_t table_offset = dchecked_integral_cast<uint32_t>(
786 oat_dex_file.GetLookupTableData() - oat_file_begin);
David Sehr9aa352e2016-09-15 18:13:52 -0700787 uint32_t table_size = TypeLookupTable::RawDataLength(dex_file->NumClassDefs());
Andreas Gampe2ba88952016-04-29 17:52:07 -0700788 os << StringPrintf("type-table: 0x%08x..0x%08x\n",
789 table_offset,
790 table_offset + table_size - 1);
791 }
792
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100793 VariableIndentationOutputStream vios(&os);
794 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800795 for (size_t class_def_index = 0;
796 class_def_index < dex_file->NumClassDefs();
797 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700798 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
799 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800800
801 // TODO: Support regex
802 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
803 continue;
804 }
805
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700806 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100807 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700808 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
Andreas Gampea5b09a62016-11-17 15:21:22 -0800809 class_def_index, descriptor, oat_class_offset, class_def.class_idx_.index_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100810 << " (" << oat_class.GetStatus() << ")"
811 << " (" << oat_class.GetType() << ")\n";
812 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700813 if (options_.list_classes_) {
814 continue;
815 }
816 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700817 success = false;
818 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800819 if (stop_analysis) {
820 os << std::flush;
821 return success;
822 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700823 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700824 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700825 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700826 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700827 }
828
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800829 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
830 std::string error_msg;
831 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
832
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700833 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800834 if (dex_file == nullptr) {
835 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
836 return false;
837 }
838 size_t fsize = oat_dex_file.FileSize();
839
840 // Some quick checks just in case
841 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
842 os << "Invalid dex file\n";
843 return false;
844 }
845
846 // Verify output directory exists
847 if (!OS::DirectoryExists(options_.export_dex_location_)) {
848 // TODO: Extend OS::DirectoryExists if symlink support is required
849 os << options_.export_dex_location_ << " output directory not found or symlink\n";
850 return false;
851 }
852
853 // Beautify path names
854 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
855 return false;
856 }
857
858 std::string dex_orig_name;
859 size_t dex_orig_pos = dex_file_location.rfind('/');
860 if (dex_orig_pos == std::string::npos)
861 dex_orig_name = dex_file_location;
862 else
863 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
864
865 // A more elegant approach to efficiently name user installed apps is welcome
866 if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
867 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
868 size_t apk_orig_pos = dex_file_location.rfind('/');
869 if (apk_orig_pos != std::string::npos) {
870 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
871 }
872 }
873
874 std::string out_dex_path(options_.export_dex_location_);
875 if (out_dex_path.back() != '/') {
876 out_dex_path.append("/");
877 }
878 out_dex_path.append(dex_orig_name);
879 out_dex_path.append("_export.dex");
880 if (out_dex_path.length() > PATH_MAX) {
881 return false;
882 }
883
884 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
885 if (file.get() == nullptr) {
886 os << "Failed to open output dex file " << out_dex_path;
887 return false;
888 }
889
890 if (!file->WriteFully(dex_file->Begin(), fsize)) {
891 os << "Failed to write dex file";
892 file->Erase();
893 return false;
894 }
895
896 if (file->FlushCloseOrErase() != 0) {
897 os << "Flush and close failed";
898 return false;
899 }
900
901 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
902 os << std::flush;
903
904 return true;
905 }
906
Elliott Hughese3c845c2012-02-28 17:23:01 -0800907 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700908 while (it.HasNextStaticField()) {
909 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700910 }
Ian Rogers0571d352011-11-03 19:51:38 -0700911 while (it.HasNextInstanceField()) {
912 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700913 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800914 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700915
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100916 bool DumpOatClass(VariableIndentationOutputStream* vios,
917 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700918 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700919 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800920 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -0700921 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700922 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100923 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700924 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800925 }
926 ClassDataItemIterator it(dex_file, class_data);
927 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700928 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700929 while (it.HasNextDirectMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100930 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700931 it.GetMemberIndex(), it.GetMethodCodeItem(),
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700932 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700933 success = false;
934 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800935 if (addr_found) {
936 *stop_analysis = true;
937 return success;
938 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700939 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700940 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700941 }
Ian Rogers0571d352011-11-03 19:51:38 -0700942 while (it.HasNextVirtualMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100943 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700944 it.GetMemberIndex(), it.GetMethodCodeItem(),
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700945 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700946 success = false;
947 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800948 if (addr_found) {
949 *stop_analysis = true;
950 return success;
951 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700952 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700953 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700954 }
Ian Rogers0571d352011-11-03 19:51:38 -0700955 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100956 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700957 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700958 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800959
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700960 static constexpr uint32_t kPrologueBytes = 16;
961
962 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
963 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
964
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100965 bool DumpOatMethod(VariableIndentationOutputStream* vios,
966 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700967 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700968 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800969 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700970 uint32_t method_access_flags, bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700971 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800972
973 // TODO: Support regex
974 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
975 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000976 return success;
977 }
978
David Sehr709b0702016-10-13 09:12:37 -0700979 std::string pretty_method = dex_file.PrettyMethod(dex_method_idx, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100980 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
981 class_method_index, pretty_method.c_str(),
982 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800983 if (options_.list_methods_) return success;
984
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800985 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
986 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
987 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
988 uint32_t code_offset = oat_method.GetCodeOffset();
989 uint32_t code_size = oat_method.GetQuickCodeSize();
990 if (resolved_addr2instr_ != 0) {
991 if (resolved_addr2instr_ > code_offset + code_size) {
992 return success;
993 } else {
994 *addr_found = true; // stop analyzing file at next iteration
995 }
996 }
997
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100998 // Everything below is indented at least once.
999 ScopedIndentation indent1(vios);
1000
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001001 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001002 vios->Stream() << "DEX CODE:\n";
1003 ScopedIndentation indent2(vios);
1004 DumpDexCode(vios->Stream(), dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -07001005 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001006
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001007 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001008 std::unique_ptr<verifier::MethodVerifier> verifier;
1009 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001010 // We need to have the handle scope stay live until after the verifier since the verifier has
1011 // a handle to the dex cache from hs.
1012 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001013 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
1014 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001015 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001016 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001017 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -07001018 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001019 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001020 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001021 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001022 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001023 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001024 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001025 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001026 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001027 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
1028 oat_method_offsets_offset, oat_file_.Size());
1029 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001030 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001031 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001032 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001033
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001034 ScopedIndentation indent2(vios);
1035 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001036 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
1037 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001038 vios->Stream() << StringPrintf("WARNING: "
1039 "code offset 0x%08x is past end of file 0x%08zx.\n",
1040 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001041 success = false;
1042 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001043 vios->Stream() << "\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001044 }
1045 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001046 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001047 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
1048 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -07001049
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001050 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001051 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001052 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001053 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001054 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001055 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001056 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
1057 method_header_offset, oat_file_.Size());
1058 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001059 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001060 return false;
1061 }
1062
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001063 ScopedIndentation indent2(vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001064 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001065 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001066 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001067 }
Mingyao Yang063fc772016-08-02 11:02:54 -07001068 uint32_t vmap_table_offset = method_header ==
1069 nullptr ? 0 : method_header->GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001070 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001071
1072 size_t vmap_table_offset_limit =
1073 (kIsVdexEnabled && IsMethodGeneratedByDexToDexCompiler(oat_method, code_item))
1074 ? oat_file_.GetVdexFile()->Size()
1075 : method_header->GetCode() - oat_file_.Begin();
1076 if (vmap_table_offset >= vmap_table_offset_limit) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001077 vios->Stream() << StringPrintf("WARNING: "
1078 "vmap table offset 0x%08x is past end of file 0x%08zx. "
1079 "vmap table offset was loaded from offset 0x%08x.\n",
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001080 vmap_table_offset,
1081 vmap_table_offset_limit,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001082 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001083 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001084 } else if (options_.dump_vmap_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001085 DumpVmapData(vios, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001086 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001087 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001088 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001089 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001090
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001091 ScopedIndentation indent2(vios);
1092 vios->Stream()
1093 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
1094 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
1095 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
1096 vios->Stream() << "\n";
1097 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
1098 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
1099 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001100 }
1101 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001102 // Based on spill masks from QuickMethodFrameInfo so placed
1103 // after it is dumped, but useful for understanding quick
1104 // code, so dumped here.
1105 ScopedIndentation indent2(vios);
1106 DumpVregLocations(vios->Stream(), oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001107 }
1108 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001109 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001110 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
1111 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001112 ScopedIndentation indent2(vios);
1113 vios->Stream() << StringPrintf("WARNING: "
1114 "code size offset 0x%08x is past end of file 0x%08zx.",
1115 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001116 success = false;
1117 } else {
1118 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001119 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1120 uint64_t aligned_code_end = aligned_code_begin + code_size;
1121
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001122 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001123 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001124 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001125 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
1126 code_offset,
1127 code_size_offset,
1128 code_size,
1129 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001130
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001131 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001132 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001133 vios->Stream() << StringPrintf("WARNING: "
1134 "start of code at 0x%08x is past end of file 0x%08zx.",
1135 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001136 success = false;
1137 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001138 vios->Stream() << StringPrintf(
1139 "WARNING: "
1140 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
1141 "code size is 0x%08x loaded from offset 0x%08x.\n",
1142 aligned_code_end, oat_file_.Size(),
1143 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001144 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001145 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001146 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001147 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001148 }
1149 }
1150 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001151 vios->Stream() << StringPrintf(
1152 "WARNING: "
1153 "code size %d is bigger than max expected threshold of %d. "
1154 "code size is 0x%08x loaded from offset 0x%08x.\n",
1155 code_size, kMaxCodeSize,
1156 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001157 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001158 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001159 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001160 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001161 }
1162 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001163 } else if (options_.disassemble_code_) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001164 DumpCode(vios, oat_method, code_item, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001165 }
1166 }
1167 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001168 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001169 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001170 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001171
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001172 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1173 if (spill_mask == 0) {
1174 return;
1175 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001176 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001177 for (size_t i = 0; i < 32; i++) {
1178 if ((spill_mask & (1 << i)) != 0) {
1179 if (is_float) {
1180 os << "fr" << i;
1181 } else {
1182 os << "r" << i;
1183 }
1184 spill_mask ^= 1 << i; // clear bit
1185 if (spill_mask != 0) {
1186 os << ", ";
1187 } else {
1188 break;
1189 }
1190 }
1191 }
1192 os << ")";
1193 }
1194
Roland Levillain442b46a2015-02-18 16:54:21 +00001195 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001196 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001197 const OatFile::OatMethod& oat_method,
1198 const DexFile::CodeItem* code_item) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001199 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1200 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001201 const void* raw_code_info = oat_method.GetVmapTable();
1202 if (raw_code_info != nullptr) {
1203 CodeInfo code_info(raw_code_info);
1204 DCHECK(code_item != nullptr);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001205 ScopedIndentation indent1(vios);
1206 DumpCodeInfo(vios, code_info, oat_method, *code_item);
Roland Levillain442b46a2015-02-18 16:54:21 +00001207 }
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001208 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
1209 // We don't encode the size in the table, so just emit that we have quickened
1210 // information.
1211 ScopedIndentation indent(vios);
1212 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001213 } else {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001214 // Otherwise, there is nothing to display.
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001215 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001216 }
1217
1218 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001219 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001220 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001221 const OatFile::OatMethod& oat_method,
Roland Levillain442b46a2015-02-18 16:54:21 +00001222 const DexFile::CodeItem& code_item) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001223 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001224 oat_method.GetCodeOffset(),
1225 code_item.registers_size_,
1226 options_.dump_code_info_stack_maps_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001227 }
1228
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001229 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1230 const DexFile::CodeItem* code_item) {
1231 if (code_item != nullptr) {
1232 size_t num_locals_ins = code_item->registers_size_;
1233 size_t num_ins = code_item->ins_size_;
1234 size_t num_locals = num_locals_ins - num_ins;
1235 size_t num_outs = code_item->outs_size_;
1236
1237 os << "vr_stack_locations:";
1238 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1239 // For readability, delimit the different kinds of VRs.
1240 if (reg == num_locals_ins) {
1241 os << "\n\tmethod*:";
1242 } else if (reg == num_locals && num_ins > 0) {
1243 os << "\n\tins:";
1244 } else if (reg == 0 && num_locals > 0) {
1245 os << "\n\tlocals:";
1246 }
1247
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001248 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1249 code_item,
1250 oat_method.GetCoreSpillMask(),
1251 oat_method.GetFpSpillMask(),
1252 oat_method.GetFrameSizeInBytes(),
1253 reg,
1254 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001255 os << " v" << reg << "[sp + #" << offset << "]";
1256 }
1257
1258 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1259 if (out_reg == 0) {
1260 os << "\n\touts:";
1261 }
1262
1263 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1264 os << " v" << out_reg << "[sp + #" << offset << "]";
1265 }
1266
1267 os << "\n";
1268 }
1269 }
1270
Ian Rogersb23a7722012-10-09 16:54:26 -07001271 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001272 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001273 size_t i = 0;
1274 while (i < code_item->insns_size_in_code_units_) {
1275 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001276 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1277 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001278 i += instruction->SizeInCodeUnits();
1279 }
1280 }
1281 }
1282
Roland Levillainf2650d12015-05-28 14:53:28 +01001283 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1284 // the optimizing compiler?
1285 static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1286 const DexFile::CodeItem* code_item) {
1287 // If the native GC map is null and the Dex `code_item` is not
1288 // null, then this method has been compiled with the optimizing
1289 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001290 return oat_method.GetQuickCode() != nullptr &&
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001291 oat_method.GetVmapTable() != nullptr &&
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001292 code_item != nullptr;
1293 }
1294
1295 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1296 // the dextodex compiler?
1297 static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
1298 const DexFile::CodeItem* code_item) {
1299 // If the quick code is null, the Dex `code_item` is not
1300 // null, and the vmap table is not null, then this method has been compiled
1301 // with the dextodex compiler.
1302 return oat_method.GetQuickCode() == nullptr &&
1303 oat_method.GetVmapTable() != nullptr &&
1304 code_item != nullptr;
Roland Levillainf2650d12015-05-28 14:53:28 +01001305 }
1306
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001307 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001308 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001309 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001310 const DexFile* dex_file,
1311 const DexFile::ClassDef& class_def,
1312 const DexFile::CodeItem* code_item,
1313 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001314 if ((method_access_flags & kAccNative) == 0) {
1315 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001316 Runtime* const runtime = Runtime::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001317 Handle<mirror::DexCache> dex_cache(
Mathieu Chartierf284d442016-06-02 11:48:30 -07001318 hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file, nullptr)));
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001319 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001320 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001321 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
David Brazdil15fc7292016-09-02 14:13:18 +01001322 class_def, code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001323 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001324
1325 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001326 }
1327
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001328 // The StackMapsHelper provides the stack maps in the native PC order.
1329 // For identical native PCs, the order from the CodeInfo is preserved.
1330 class StackMapsHelper {
1331 public:
1332 explicit StackMapsHelper(const uint8_t* raw_code_info)
1333 : code_info_(raw_code_info),
1334 encoding_(code_info_.ExtractEncoding()),
1335 number_of_stack_maps_(code_info_.GetNumberOfStackMaps(encoding_)),
1336 indexes_(),
1337 offset_(static_cast<size_t>(-1)),
1338 stack_map_index_(0u) {
1339 if (number_of_stack_maps_ != 0u) {
1340 // Check if native PCs are ordered.
1341 bool ordered = true;
1342 StackMap last = code_info_.GetStackMapAt(0u, encoding_);
1343 for (size_t i = 1; i != number_of_stack_maps_; ++i) {
1344 StackMap current = code_info_.GetStackMapAt(i, encoding_);
1345 if (last.GetNativePcOffset(encoding_.stack_map_encoding) >
1346 current.GetNativePcOffset(encoding_.stack_map_encoding)) {
1347 ordered = false;
1348 break;
1349 }
1350 last = current;
1351 }
1352 if (!ordered) {
1353 // Create indirection indexes for access in native PC order. We do not optimize
1354 // for the fact that there can currently be only two separately ordered ranges,
1355 // namely normal stack maps and catch-point stack maps.
1356 indexes_.resize(number_of_stack_maps_);
1357 std::iota(indexes_.begin(), indexes_.end(), 0u);
1358 std::sort(indexes_.begin(),
1359 indexes_.end(),
1360 [this](size_t lhs, size_t rhs) {
1361 StackMap left = code_info_.GetStackMapAt(lhs, encoding_);
1362 uint32_t left_pc = left.GetNativePcOffset(encoding_.stack_map_encoding);
1363 StackMap right = code_info_.GetStackMapAt(rhs, encoding_);
1364 uint32_t right_pc = right.GetNativePcOffset(encoding_.stack_map_encoding);
1365 // If the PCs are the same, compare indexes to preserve the original order.
1366 return (left_pc < right_pc) || (left_pc == right_pc && lhs < rhs);
1367 });
1368 }
1369 offset_ = GetStackMapAt(0).GetNativePcOffset(encoding_.stack_map_encoding);
1370 }
1371 }
1372
1373 const CodeInfo& GetCodeInfo() const {
1374 return code_info_;
1375 }
1376
1377 const CodeInfoEncoding& GetEncoding() const {
1378 return encoding_;
1379 }
1380
1381 size_t GetOffset() const {
1382 return offset_;
1383 }
1384
1385 StackMap GetStackMap() const {
1386 return GetStackMapAt(stack_map_index_);
1387 }
1388
1389 void Next() {
1390 ++stack_map_index_;
1391 offset_ = (stack_map_index_ == number_of_stack_maps_)
1392 ? static_cast<size_t>(-1)
1393 : GetStackMapAt(stack_map_index_).GetNativePcOffset(encoding_.stack_map_encoding);
1394 }
1395
1396 private:
1397 StackMap GetStackMapAt(size_t i) const {
1398 if (!indexes_.empty()) {
1399 i = indexes_[i];
1400 }
1401 DCHECK_LT(i, number_of_stack_maps_);
1402 return code_info_.GetStackMapAt(i, encoding_);
1403 }
1404
1405 const CodeInfo code_info_;
1406 const CodeInfoEncoding encoding_;
1407 const size_t number_of_stack_maps_;
1408 dchecked_vector<size_t> indexes_; // Used if stack map native PCs are not ordered.
1409 size_t offset_;
1410 size_t stack_map_index_;
1411 };
1412
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001413 void DumpCode(VariableIndentationOutputStream* vios,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001414 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1415 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001416 const void* quick_code = oat_method.GetQuickCode();
1417
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001418 if (code_size == 0) {
1419 code_size = oat_method.GetQuickCodeSize();
1420 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001421 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001422 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001423 return;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001424 } else if (!bad_input && IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1425 // The optimizing compiler outputs its CodeInfo data in the vmap table.
1426 StackMapsHelper helper(oat_method.GetVmapTable());
1427 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1428 size_t offset = 0;
1429 while (offset < code_size) {
1430 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
1431 if (offset == helper.GetOffset()) {
1432 ScopedIndentation indent1(vios);
1433 StackMap stack_map = helper.GetStackMap();
1434 DCHECK(stack_map.IsValid());
1435 stack_map.Dump(vios,
1436 helper.GetCodeInfo(),
1437 helper.GetEncoding(),
1438 oat_method.GetCodeOffset(),
1439 code_item->registers_size_);
1440 do {
1441 helper.Next();
1442 // There may be multiple stack maps at a given PC. We display only the first one.
1443 } while (offset == helper.GetOffset());
1444 }
1445 DCHECK_LT(offset, helper.GetOffset());
1446 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001447 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001448 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1449 size_t offset = 0;
1450 while (offset < code_size) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001451 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001452 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001453 }
1454 }
1455
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001456 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001457 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001458 const OatDumperOptions& options_;
1459 uint32_t resolved_addr2instr_;
Andreas Gampe372f3a32016-08-19 10:49:06 -07001460 const InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001461 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001462 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001463};
1464
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001465class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001466 public:
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001467 ImageDumper(std::ostream* os,
1468 gc::space::ImageSpace& image_space,
1469 const ImageHeader& image_header,
1470 OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001471 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001472 vios_(os),
1473 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001474 image_space_(image_space),
1475 image_header_(image_header),
1476 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001477
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001478 bool Dump() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001479 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001480 std::ostream& indent_os = vios_.Stream();
1481
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001482 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001483
Jeff Haodcdc85b2015-12-04 14:06:18 -08001484 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1485
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001486 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001487
Mathieu Chartiere401d142015-04-22 13:56:20 -07001488 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1489
1490 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1491 auto section = static_cast<ImageHeader::ImageSections>(i);
1492 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1493 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001494
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001495 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001496
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001497 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001498
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001499 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1500
1501 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1502
1503 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001504
Alex Lighta59dd802014-07-02 16:28:08 -07001505 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1506
Igor Murashkin46774762014-10-22 11:37:02 -07001507 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1508
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001509 {
1510 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001511 static_assert(arraysize(image_roots_descriptions_) ==
1512 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001513 DCHECK_LE(image_header_.GetImageRoots()->GetLength(), ImageHeader::kImageRootsMax);
1514 for (int32_t i = 0, size = image_header_.GetImageRoots()->GetLength(); i != size; ++i) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001515 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1516 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001517 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001518 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001519 if (image_root_object != nullptr && image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001520 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001521 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001522 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001523 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1524 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001525 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001526 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1527 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001528 run++;
1529 } else {
1530 break;
1531 }
1532 }
1533 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001534 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001535 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001536 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001537 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001538 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001539 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001540 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001541 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001542 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001543 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001544 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001545 }
1546 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001547 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001548
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001549 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001550 os << "METHOD ROOTS\n";
1551 static_assert(arraysize(image_methods_descriptions_) ==
1552 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1553 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1554 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1555 const char* description = image_methods_descriptions_[i];
1556 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001557 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001558 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001559 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001560 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001561
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001562 Runtime* const runtime = Runtime::Current();
1563 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001564 std::string image_filename = image_space_.GetImageFilename();
1565 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001566 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001567 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001568 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001569 const OatFile* oat_file = image_space_.GetOatFile();
Alex Lighta59dd802014-07-02 16:28:08 -07001570 if (oat_file == nullptr) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001571 oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location);
1572 }
1573 if (oat_file == nullptr) {
1574 oat_file = OatFile::Open(oat_location,
1575 oat_location,
1576 nullptr,
1577 nullptr,
1578 false,
1579 /*low_4gb*/false,
1580 nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001581 &error_msg);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001582 }
1583 if (oat_file == nullptr) {
1584 os << "OAT FILE NOT FOUND: " << error_msg << "\n";
1585 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001586 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001587 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001588
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001589 stats_.oat_file_bytes = oat_file->Size();
1590
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001591 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001592
Mathieu Chartier02e25112013-08-14 16:14:24 -07001593 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001594 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001595 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1596 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001597 }
1598
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001599 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001600
Jeff Haodcdc85b2015-12-04 14:06:18 -08001601 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001602 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001603 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001604 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001605 {
1606 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1607 heap->FlushAllocStack();
1608 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001609 // Since FlushAllocStack() above resets the (active) allocation
1610 // stack. Need to revoke the thread-local allocation stacks that
1611 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001612 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001613 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001614 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001615 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001616 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001617 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001618 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001619 {
Andreas Gampecc1b5352016-12-01 16:58:38 -08001620 ReaderMutexLock mu(self, *Locks::dex_lock_);
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001621 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001622 ObjPtr<mirror::DexCache> dex_cache =
1623 ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001624 if (dex_cache != nullptr) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001625 dex_caches_.insert(dex_cache.Ptr());
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001626 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001627 }
1628 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001629 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001630 // Dump the normal objects before ArtMethods.
1631 image_space_.GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1632 indent_os << "\n";
1633 // TODO: Dump fields.
1634 // Dump methods after.
Jeff Haodcdc85b2015-12-04 14:06:18 -08001635 DumpArtMethodVisitor visitor(this);
Mathieu Chartiere42888f2016-04-14 10:49:19 -07001636 image_header_.VisitPackedArtMethods(&visitor,
1637 image_space_.Begin(),
1638 image_header_.GetPointerSize());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001639 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001640 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001641 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001642 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001643 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001644 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001645 size_t data_size = image_header_.GetDataSize(); // stored size in file.
1646 if (file == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001647 LOG(WARNING) << "Failed to find image in " << image_filename;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001648 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001649 stats_.file_bytes = file->GetLength();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001650 // If the image is compressed, adjust to decompressed size.
1651 size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader);
1652 if (image_header_.GetStorageMode() == ImageHeader::kStorageModeUncompressed) {
1653 DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image";
1654 }
1655 stats_.file_bytes += uncompressed_size - data_size;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001656 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001657 size_t header_bytes = sizeof(ImageHeader);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001658 const auto& object_section = image_header_.GetImageSection(ImageHeader::kSectionObjects);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001659 const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1660 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001661 const auto& dex_cache_arrays_section = image_header_.GetImageSection(
1662 ImageHeader::kSectionDexCacheArrays);
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001663 const auto& intern_section = image_header_.GetImageSection(
1664 ImageHeader::kSectionInternedStrings);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001665 const auto& class_table_section = image_header_.GetImageSection(
1666 ImageHeader::kSectionClassTable);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001667 const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1668
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001669 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001670
1671 // Objects are kObjectAlignment-aligned.
1672 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1673 if (object_section.Offset() > header_bytes) {
1674 stats_.alignment_bytes += object_section.Offset() - header_bytes;
1675 }
1676
1677 // Field section is 4-byte aligned.
1678 constexpr size_t kFieldSectionAlignment = 4U;
1679 uint32_t end_objects = object_section.Offset() + object_section.Size();
1680 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
1681 stats_.alignment_bytes += field_section.Offset() - end_objects;
1682
1683 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
1684 uint32_t end_fields = field_section.Offset() + field_section.Size();
1685 CHECK_ALIGNED(method_section.Offset(), 4);
1686 stats_.alignment_bytes += method_section.Offset() - end_fields;
1687
1688 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
1689 uint32_t end_methods = method_section.Offset() + method_section.Size();
1690 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
1691 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
1692
1693 // Intern table is 8-byte aligned.
1694 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
1695 CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
1696 stats_.alignment_bytes += intern_section.Offset() - end_caches;
1697
1698 // Add space between intern table and class table.
1699 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
1700 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
1701
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001702 // Add space between end of image data and bitmap. Expect the bitmap to be page-aligned.
1703 const size_t bitmap_offset = sizeof(ImageHeader) + data_size;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001704 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001705 stats_.alignment_bytes += RoundUp(bitmap_offset, kPageSize) - bitmap_offset;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001706
Mathieu Chartiere401d142015-04-22 13:56:20 -07001707 stats_.bitmap_bytes += bitmap_section.Size();
1708 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01001709 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01001710 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001711 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001712 stats_.class_table_bytes += class_table_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001713 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001714 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001715
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001716 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001717
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001718 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001719 }
1720
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001721 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001722 class DumpArtMethodVisitor : public ArtMethodVisitor {
1723 public:
1724 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
1725
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001726 virtual void Visit(ArtMethod* method) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001727 std::ostream& indent_os = image_dumper_->vios_.Stream();
David Sehr709b0702016-10-13 09:12:37 -07001728 indent_os << method << " " << " ArtMethod: " << ArtMethod::PrettyMethod(method) << "\n";
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001729 image_dumper_->DumpMethod(method, indent_os);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001730 indent_os << "\n";
1731 }
1732
1733 private:
1734 ImageDumper* const image_dumper_;
1735 };
1736
Mathieu Chartier3398c782016-09-30 10:27:43 -07001737 static void PrettyObjectValue(std::ostream& os,
1738 ObjPtr<mirror::Class> type,
1739 ObjPtr<mirror::Object> value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001740 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001741 CHECK(type != nullptr);
1742 if (value == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07001743 os << StringPrintf("null %s\n", type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001744 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001745 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001746 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001747 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001748 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001749 mirror::Class* klass = value->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07001750 os << StringPrintf("%p Class: %s\n", klass, mirror::Class::PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001751 } else {
David Sehr709b0702016-10-13 09:12:37 -07001752 os << StringPrintf("%p %s\n", value.Ptr(), type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001753 }
1754 }
1755
Mathieu Chartier3398c782016-09-30 10:27:43 -07001756 static void PrintField(std::ostream& os, ArtField* field, ObjPtr<mirror::Object> obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001757 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001758 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08001759 switch (field->GetTypeAsPrimitiveType()) {
1760 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08001761 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001762 break;
1763 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001764 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001765 break;
1766 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001767 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001768 break;
1769 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001770 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001771 break;
1772 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07001773 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001774 break;
1775 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07001776 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001777 break;
1778 case Primitive::kPrimBoolean:
Fred Shih37f05ef2014-07-16 18:38:08 -07001779 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1780 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001781 break;
1782 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07001783 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001784 break;
1785 case Primitive::kPrimNot: {
1786 // Get the value, don't compute the type unless it is non-null as we don't want
1787 // to cause class loading.
Mathieu Chartier3398c782016-09-30 10:27:43 -07001788 ObjPtr<mirror::Object> value = field->GetObj(obj);
Ian Rogers08f1f502014-12-02 15:04:37 -08001789 if (value == nullptr) {
1790 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07001791 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08001792 // Grab the field type without causing resolution.
Mathieu Chartier3398c782016-09-30 10:27:43 -07001793 ObjPtr<mirror::Class> field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08001794 if (field_type != nullptr) {
1795 PrettyObjectValue(os, field_type, value);
1796 } else {
Mathieu Chartier3398c782016-09-30 10:27:43 -07001797 os << StringPrintf("%p %s\n",
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07001798 value.Ptr(),
Ian Rogers08f1f502014-12-02 15:04:37 -08001799 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
1800 }
Ian Rogers50239c72013-06-17 14:53:22 -07001801 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001802 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07001803 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001804 default:
1805 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
1806 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08001807 }
1808 }
1809
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001810 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001811 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001812 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001813 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001814 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001815 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001816 for (ArtField& field : klass->GetIFields()) {
1817 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001818 }
1819 }
1820
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001821 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001822 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001823 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001824
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001825 const void* GetQuickOatCodeBegin(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001826 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001827 image_header_.GetPointerSize());
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001828 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001829 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001830 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001831 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001832 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001833 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001834 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001835 }
1836
Mathieu Chartiere401d142015-04-22 13:56:20 -07001837 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001838 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001839 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1840 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001841 return 0;
1842 }
1843 return oat_code_begin[-1];
1844 }
1845
Mathieu Chartiere401d142015-04-22 13:56:20 -07001846 const void* GetQuickOatCodeEnd(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001847 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001848 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001849 if (oat_code_begin == nullptr) {
1850 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001851 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001852 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001853 }
1854
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001855 static void Callback(mirror::Object* obj, void* arg) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001856 DCHECK(obj != nullptr);
1857 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001858 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001859 if (!state->InDumpSpace(obj)) {
1860 return;
1861 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001862
1863 size_t object_bytes = obj->SizeOf();
1864 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1865 state->stats_.object_bytes += object_bytes;
1866 state->stats_.alignment_bytes += alignment_bytes;
1867
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001868 std::ostream& os = state->vios_.Stream();
1869
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001870 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001871 if (obj_class->IsArrayClass()) {
David Sehr709b0702016-10-13 09:12:37 -07001872 os << StringPrintf("%p: %s length:%d\n", obj, obj_class->PrettyDescriptor().c_str(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001873 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001874 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001875 mirror::Class* klass = obj->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07001876 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj,
1877 mirror::Class::PrettyDescriptor(klass).c_str())
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001878 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08001879 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001880 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001881 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001882 } else {
David Sehr709b0702016-10-13 09:12:37 -07001883 os << StringPrintf("%p: %s\n", obj, obj_class->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001884 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001885 ScopedIndentation indent1(&state->vios_);
1886 DumpFields(os, obj, obj_class);
Andreas Gampe542451c2016-07-26 09:02:02 -07001887 const PointerSize image_pointer_size = state->image_header_.GetPointerSize();
Ian Rogersd5b32602012-02-26 16:40:04 -08001888 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001889 auto* obj_array = obj->AsObjectArray<mirror::Object>();
1890 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001891 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001892 size_t run = 0;
1893 for (int32_t j = i + 1; j < length; j++) {
1894 if (value == obj_array->Get(j)) {
1895 run++;
1896 } else {
1897 break;
1898 }
1899 }
1900 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001901 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001902 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001903 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001904 i = i + run;
1905 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001906 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001907 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001908 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001909 }
1910 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001911 mirror::Class* klass = obj->AsClass();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001912 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001913 os << "STATICS:\n";
1914 ScopedIndentation indent2(&state->vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001915 for (ArtField& field : klass->GetSFields()) {
1916 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001917 }
1918 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001919 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01001920 auto it = state->dex_caches_.find(obj);
1921 if (it != state->dex_caches_.end()) {
1922 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001923 const auto& field_section = state->image_header_.GetImageSection(
1924 ImageHeader::kSectionArtFields);
1925 const auto& method_section = state->image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001926 size_t num_methods = dex_cache->NumResolvedMethods();
1927 if (num_methods != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001928 os << "Methods (size=" << num_methods << "):\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01001929 ScopedIndentation indent2(&state->vios_);
1930 auto* resolved_methods = dex_cache->GetResolvedMethods();
1931 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001932 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_methods,
1933 i,
1934 image_pointer_size);
Vladimir Marko05792b92015-08-03 11:56:49 +01001935 size_t run = 0;
1936 for (size_t j = i + 1;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001937 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_methods,
1938 j,
1939 image_pointer_size);
1940 ++j) {
1941 ++run;
1942 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001943 if (run == 0) {
1944 os << StringPrintf("%zd: ", i);
1945 } else {
1946 os << StringPrintf("%zd to %zd: ", i, i + run);
1947 i = i + run;
1948 }
1949 std::string msg;
1950 if (elem == nullptr) {
1951 msg = "null";
1952 } else if (method_section.Contains(
1953 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07001954 msg = reinterpret_cast<ArtMethod*>(elem)->PrettyMethod();
Vladimir Marko05792b92015-08-03 11:56:49 +01001955 } else {
1956 msg = "<not in method section>";
1957 }
1958 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07001959 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001960 }
1961 size_t num_fields = dex_cache->NumResolvedFields();
1962 if (num_fields != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001963 os << "Fields (size=" << num_fields << "):\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01001964 ScopedIndentation indent2(&state->vios_);
1965 auto* resolved_fields = dex_cache->GetResolvedFields();
1966 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001967 auto* elem = mirror::DexCache::GetElementPtrSize(
1968 resolved_fields, i, image_pointer_size);
Vladimir Marko05792b92015-08-03 11:56:49 +01001969 size_t run = 0;
1970 for (size_t j = i + 1;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001971 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_fields,
1972 j,
1973 image_pointer_size);
1974 ++j) {
1975 ++run;
1976 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001977 if (run == 0) {
1978 os << StringPrintf("%zd: ", i);
1979 } else {
1980 os << StringPrintf("%zd to %zd: ", i, i + run);
1981 i = i + run;
1982 }
1983 std::string msg;
1984 if (elem == nullptr) {
1985 msg = "null";
1986 } else if (field_section.Contains(
1987 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07001988 msg = reinterpret_cast<ArtField*>(elem)->PrettyField();
Vladimir Marko05792b92015-08-03 11:56:49 +01001989 } else {
1990 msg = "<not in field section>";
1991 }
1992 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07001993 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001994 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001995 size_t num_types = dex_cache->NumResolvedTypes();
1996 if (num_types != 0u) {
1997 os << "Types (size=" << num_types << "):\n";
1998 ScopedIndentation indent2(&state->vios_);
1999 auto* resolved_types = dex_cache->GetResolvedTypes();
2000 for (size_t i = 0; i < num_types; ++i) {
2001 auto* elem = resolved_types[i].Read();
2002 size_t run = 0;
2003 for (size_t j = i + 1; j != num_types && elem == resolved_types[j].Read(); ++j) {
2004 ++run;
2005 }
2006 if (run == 0) {
2007 os << StringPrintf("%zd: ", i);
2008 } else {
2009 os << StringPrintf("%zd to %zd: ", i, i + run);
2010 i = i + run;
2011 }
2012 std::string msg;
2013 if (elem == nullptr) {
2014 msg = "null";
2015 } else {
David Sehr709b0702016-10-13 09:12:37 -07002016 msg = elem->PrettyClass();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002017 }
2018 os << StringPrintf("%p %s\n", elem, msg.c_str());
2019 }
2020 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002021 }
2022 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002023 std::string temp;
2024 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002025 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002026
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002027 void DumpMethod(ArtMethod* method, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002028 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002029 DCHECK(method != nullptr);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002030 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
2031 const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
Andreas Gampe542451c2016-07-26 09:02:02 -07002032 const PointerSize pointer_size = image_header_.GetPointerSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002033 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2034 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002035 if (method->IsNative()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002036 bool first_occurrence;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002037 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2038 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002039 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002040 stats_.native_to_managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002041 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002042 if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
2043 image_header_.GetPointerSize())) {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002044 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002045 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002046 } else if (method->IsAbstract() || method->IsClassInitializer()) {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002047 // Don't print information for these.
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002048 } else if (method->IsRuntimeMethod()) {
2049 ImtConflictTable* table = method->GetImtConflictTable(image_header_.GetPointerSize());
2050 if (table != nullptr) {
2051 indent_os << "IMT conflict table " << table << " method: ";
2052 for (size_t i = 0, count = table->NumEntries(pointer_size); i < count; ++i) {
David Sehr709b0702016-10-13 09:12:37 -07002053 indent_os << ArtMethod::PrettyMethod(table->GetImplementationMethod(i, pointer_size))
2054 << " ";
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002055 }
2056 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002057 } else {
2058 const DexFile::CodeItem* code_item = method->GetCodeItem();
2059 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002060 stats_.dex_instruction_bytes += dex_instruction_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002061
2062 bool first_occurrence;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002063 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002064 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01002065 // Method compiled with the optimizing compiler have no vmap table.
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002066 vmap_table_bytes = ComputeOatSize(method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002067 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002068 stats_.vmap_table_bytes += vmap_table_bytes;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002069 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002070 }
2071
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002072 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2073 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002074 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002075 stats_.managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002076 if (method->IsConstructor()) {
2077 if (method->IsStatic()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002078 stats_.class_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002079 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002080 stats_.large_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002081 }
2082 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002083 stats_.large_method_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002084 }
2085 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002086 stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002087
Igor Murashkin7617abd2015-07-10 18:27:47 -07002088 uint32_t method_access_flags = method->GetAccessFlags();
2089
Mathieu Chartiere401d142015-04-22 13:56:20 -07002090 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002091 indent_os << StringPrintf("SIZE: Dex Instructions=%zd StackMaps=%zd AccessFlags=0x%x\n",
2092 dex_instruction_bytes,
2093 vmap_table_bytes,
Igor Murashkin7617abd2015-07-10 18:27:47 -07002094 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002095
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002096 size_t total_size = dex_instruction_bytes +
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002097 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002098
2099 double expansion =
2100 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002101 stats_.ComputeOutliers(total_size, expansion, method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002102 }
2103 }
2104
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002105 std::set<const void*> already_seen_;
2106 // Compute the size of the given data within the oat file and whether this is the first time
2107 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002108 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002109 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002110 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002111 already_seen_.insert(oat_data);
2112 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002113 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002114 }
2115 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002116 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002117
2118 public:
2119 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002120 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002121 size_t file_bytes;
2122
2123 size_t header_bytes;
2124 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002125 size_t art_field_bytes;
2126 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01002127 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002128 size_t interned_strings_bytes;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002129 size_t class_table_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07002130 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002131 size_t alignment_bytes;
2132
2133 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002134 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002135 size_t managed_to_native_code_bytes;
2136 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07002137 size_t class_initializer_code_bytes;
2138 size_t large_initializer_code_bytes;
2139 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002140
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002141 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002142
2143 size_t dex_instruction_bytes;
2144
Mathieu Chartiere401d142015-04-22 13:56:20 -07002145 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002146 std::vector<size_t> method_outlier_size;
2147 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002148 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002149
Roland Levillain3887c462015-08-12 18:15:42 +01002150 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002151 : oat_file_bytes(0),
2152 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002153 header_bytes(0),
2154 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002155 art_field_bytes(0),
2156 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01002157 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002158 interned_strings_bytes(0),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002159 class_table_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07002160 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002161 alignment_bytes(0),
2162 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002163 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002164 managed_to_native_code_bytes(0),
2165 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07002166 class_initializer_code_bytes(0),
2167 large_initializer_code_bytes(0),
2168 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002169 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002170 dex_instruction_bytes(0) {}
2171
Elliott Hughesa0e18062012-04-13 15:59:59 -07002172 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002173 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002174 size_t bytes;
2175 size_t count;
2176 };
2177 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2178 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002179
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002180 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002181 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2182 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002183 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002184 it->second.count += 1;
2185 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002186 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002187 }
2188 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002189
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002190 double PercentOfOatBytes(size_t size) {
2191 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2192 }
2193
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002194 double PercentOfFileBytes(size_t size) {
2195 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2196 }
2197
2198 double PercentOfObjectBytes(size_t size) {
2199 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2200 }
2201
Mathieu Chartiere401d142015-04-22 13:56:20 -07002202 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002203 method_outlier_size.push_back(total_size);
2204 method_outlier_expansion.push_back(expansion);
2205 method_outlier.push_back(method);
2206 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002207
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002208 void DumpOutliers(std::ostream& os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002209 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002210 size_t sum_of_sizes = 0;
2211 size_t sum_of_sizes_squared = 0;
2212 size_t sum_of_expansion = 0;
2213 size_t sum_of_expansion_squared = 0;
2214 size_t n = method_outlier_size.size();
Mathieu Chartier1ebf8d32016-06-09 11:51:27 -07002215 if (n <= 1) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08002216 return;
2217 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002218 for (size_t i = 0; i < n; i++) {
2219 size_t cur_size = method_outlier_size[i];
2220 sum_of_sizes += cur_size;
2221 sum_of_sizes_squared += cur_size * cur_size;
2222 double cur_expansion = method_outlier_expansion[i];
2223 sum_of_expansion += cur_expansion;
2224 sum_of_expansion_squared += cur_expansion * cur_expansion;
2225 }
2226 size_t size_mean = sum_of_sizes / n;
2227 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2228 double expansion_mean = sum_of_expansion / n;
2229 double expansion_variance =
2230 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2231
2232 // Dump methods whose size is a certain number of standard deviations from the mean
2233 size_t dumped_values = 0;
2234 size_t skipped_values = 0;
2235 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2236 size_t cur_size_variance = i * i * size_variance;
2237 bool first = true;
2238 for (size_t j = 0; j < n; j++) {
2239 size_t cur_size = method_outlier_size[j];
2240 if (cur_size > size_mean) {
2241 size_t cur_var = cur_size - size_mean;
2242 cur_var = cur_var * cur_var;
2243 if (cur_var > cur_size_variance) {
2244 if (dumped_values > 20) {
2245 if (i == 1) {
2246 skipped_values++;
2247 } else {
2248 i = 2; // jump to counting for 1 standard deviation
2249 break;
2250 }
2251 } else {
2252 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002253 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002254 first = false;
2255 }
David Sehr709b0702016-10-13 09:12:37 -07002256 os << ArtMethod::PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002257 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002258 method_outlier_size[j] = 0; // don't consider this method again
2259 dumped_values++;
2260 }
2261 }
2262 }
2263 }
2264 }
2265 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002266 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002267 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002268 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002269 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002270
2271 // Dump methods whose expansion is a certain number of standard deviations from the mean
2272 dumped_values = 0;
2273 skipped_values = 0;
2274 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2275 double cur_expansion_variance = i * i * expansion_variance;
2276 bool first = true;
2277 for (size_t j = 0; j < n; j++) {
2278 double cur_expansion = method_outlier_expansion[j];
2279 if (cur_expansion > expansion_mean) {
2280 size_t cur_var = cur_expansion - expansion_mean;
2281 cur_var = cur_var * cur_var;
2282 if (cur_var > cur_expansion_variance) {
2283 if (dumped_values > 20) {
2284 if (i == 1) {
2285 skipped_values++;
2286 } else {
2287 i = 2; // jump to counting for 1 standard deviation
2288 break;
2289 }
2290 } else {
2291 if (first) {
2292 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002293 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002294 first = false;
2295 }
David Sehr709b0702016-10-13 09:12:37 -07002296 os << ArtMethod::PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002297 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002298 method_outlier_expansion[j] = 0.0; // don't consider this method again
2299 dumped_values++;
2300 }
2301 }
2302 }
2303 }
2304 }
2305 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002306 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002307 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002308 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002309 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002310 }
2311
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002312 void Dump(std::ostream& os, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002313 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002314 {
2315 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2316 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002317 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2318 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2319 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2320 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2321 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2322 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002323 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002324 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2325 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002326 header_bytes, PercentOfFileBytes(header_bytes),
2327 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002328 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2329 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002330 dex_cache_arrays_bytes,
2331 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002332 interned_strings_bytes,
2333 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002334 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002335 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002336 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2337 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002338 CHECK_EQ(file_bytes,
2339 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2340 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2341 bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002342 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002343
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002344 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002345 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002346 for (const auto& sizes_and_count : sizes_and_counts) {
2347 const std::string& descriptor(sizes_and_count.first);
2348 double average = static_cast<double>(sizes_and_count.second.bytes) /
2349 static_cast<double>(sizes_and_count.second.count);
2350 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002351 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002352 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002353 descriptor.c_str(), sizes_and_count.second.bytes,
2354 sizes_and_count.second.count, average, percent);
2355 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002356 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002357 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002358 CHECK_EQ(object_bytes, object_bytes_total);
2359
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002360 os << StringPrintf("oat_file_bytes = %8zd\n"
2361 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2362 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2363 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2364 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2365 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2366 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002367 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002368 managed_code_bytes,
2369 PercentOfOatBytes(managed_code_bytes),
2370 managed_to_native_code_bytes,
2371 PercentOfOatBytes(managed_to_native_code_bytes),
2372 native_to_managed_code_bytes,
2373 PercentOfOatBytes(native_to_managed_code_bytes),
2374 class_initializer_code_bytes,
2375 PercentOfOatBytes(class_initializer_code_bytes),
2376 large_initializer_code_bytes,
2377 PercentOfOatBytes(large_initializer_code_bytes),
2378 large_method_code_bytes,
2379 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002380 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002381 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002382 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002383 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2384 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002385 }
2386
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002387 os << "\n" << StringPrintf("vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002388 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002389 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002390
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002391 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2392 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002393 static_cast<double>(managed_code_bytes) /
2394 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002395 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002396 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002397 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002398
2399 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002400 }
2401 } stats_;
2402
2403 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002404 enum {
2405 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2406 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2407 kLargeConstructorDexBytes = 4000,
2408 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2409 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2410 kLargeMethodDexBytes = 16000
2411 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002412
2413 // For performance, use the *os_ directly for anything that doesn't need indentation
2414 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002415 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002416 VariableIndentationOutputStream vios_;
2417 ScopedIndentation indent1_;
2418
Ian Rogers1d54e732013-05-02 21:10:01 -07002419 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002420 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002421 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002422 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002423 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002424
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002425 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002426};
2427
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002428static int DumpImage(gc::space::ImageSpace* image_space,
2429 OatDumperOptions* options,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002430 std::ostream* os) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002431 const ImageHeader& image_header = image_space->GetImageHeader();
2432 if (!image_header.IsValid()) {
2433 fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
2434 return EXIT_FAILURE;
2435 }
2436 ImageDumper image_dumper(os, *image_space, image_header, options);
2437 if (!image_dumper.Dump()) {
2438 return EXIT_FAILURE;
2439 }
2440 return EXIT_SUCCESS;
2441}
2442
2443static int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002444 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002445 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002446 options->class_loader_ = &null_class_loader;
2447
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002448 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002449 if (options->app_image_ != nullptr) {
2450 if (options->app_oat_ == nullptr) {
2451 LOG(ERROR) << "Can not dump app image without app oat file";
Jeff Haodcdc85b2015-12-04 14:06:18 -08002452 return EXIT_FAILURE;
2453 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002454 // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
2455 // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
2456 // pointers into 32 bit pointer sized ArtMethods.
2457 std::string error_msg;
2458 std::unique_ptr<OatFile> oat_file(OatFile::Open(options->app_oat_,
2459 options->app_oat_,
2460 nullptr,
2461 nullptr,
2462 false,
2463 /*low_4gb*/true,
2464 nullptr,
2465 &error_msg));
2466 if (oat_file == nullptr) {
2467 LOG(ERROR) << "Failed to open oat file " << options->app_oat_ << " with error " << error_msg;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002468 return EXIT_FAILURE;
2469 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002470 std::unique_ptr<gc::space::ImageSpace> space(
2471 gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg));
2472 if (space == nullptr) {
2473 LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error "
2474 << error_msg;
2475 }
2476 // Open dex files for the image.
2477 std::vector<std::unique_ptr<const DexFile>> dex_files;
2478 if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
2479 LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
2480 << error_msg;
2481 }
2482 // Dump the actual image.
2483 int result = DumpImage(space.get(), options, os);
2484 if (result != EXIT_SUCCESS) {
2485 return result;
2486 }
2487 // Fall through to dump the boot images.
2488 }
2489
2490 gc::Heap* heap = runtime->GetHeap();
2491 CHECK(heap->HasBootImageSpace()) << "No image spaces";
2492 for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
2493 int result = DumpImage(image_space, options, os);
2494 if (result != EXIT_SUCCESS) {
2495 return result;
2496 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002497 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002498 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002499}
2500
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002501static jobject InstallOatFile(Runtime* runtime,
2502 std::unique_ptr<OatFile> oat_file,
2503 std::vector<const DexFile*>* class_path)
2504 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002505 Thread* self = Thread::Current();
2506 CHECK(self != nullptr);
2507 // Need well-known-classes.
2508 WellKnownClasses::Init(self->GetJniEnv());
2509
2510 // Need to register dex files to get a working dex cache.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002511 OatFile* oat_file_ptr = oat_file.get();
Andreas Gampe00b25f32014-09-17 21:49:05 -07002512 ClassLinker* class_linker = runtime->GetClassLinker();
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002513 runtime->GetOatFileManager().RegisterOatFile(std::move(oat_file));
2514 for (const OatFile::OatDexFile* odf : oat_file_ptr->GetOatDexFiles()) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002515 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002516 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002517 CHECK(dex_file != nullptr) << error_msg;
Mathieu Chartierf284d442016-06-02 11:48:30 -07002518 class_linker->RegisterDexFile(*dex_file, nullptr);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002519 class_path->push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002520 }
2521
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002522 // Need a class loader. Fake that we're a compiler.
2523 // Note: this will run initializers through the unstarted runtime, so make sure it's
2524 // initialized.
2525 interpreter::UnstartedRuntime::Initialize();
2526
2527 jobject class_loader = class_linker->CreatePathClassLoader(self, *class_path);
2528
2529 return class_loader;
2530}
2531
2532static int DumpOatWithRuntime(Runtime* runtime,
2533 std::unique_ptr<OatFile> oat_file,
2534 OatDumperOptions* options,
2535 std::ostream* os) {
2536 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2537 ScopedObjectAccess soa(Thread::Current());
2538
2539 OatFile* oat_file_ptr = oat_file.get();
2540 std::vector<const DexFile*> class_path;
2541 jobject class_loader = InstallOatFile(runtime, std::move(oat_file), &class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002542
2543 // Use the class loader while dumping.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002544 StackHandleScope<1> scope(soa.Self());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002545 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07002546 soa.Decode<mirror::ClassLoader>(class_loader));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002547 options->class_loader_ = &loader_handle;
2548
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002549 OatDumper oat_dumper(*oat_file_ptr, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002550 bool success = oat_dumper.Dump(*os);
2551 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2552}
2553
2554static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002555 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002556 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002557 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002558 options->class_loader_ = &null_class_loader;
2559
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002560 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002561 bool success = oat_dumper.Dump(*os);
2562 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2563}
2564
2565static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2566 std::ostream* os) {
2567 std::string error_msg;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002568 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
2569 oat_filename,
2570 nullptr,
2571 nullptr,
2572 false,
2573 /*low_4gb*/false,
2574 nullptr,
2575 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002576 if (oat_file == nullptr) {
2577 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2578 return EXIT_FAILURE;
2579 }
2580
2581 if (runtime != nullptr) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002582 return DumpOatWithRuntime(runtime, std::move(oat_file), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002583 } else {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002584 return DumpOatWithoutRuntime(oat_file.get(), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002585 }
2586}
2587
David Srbecky2fdd03c2016-03-10 15:32:37 +00002588static int SymbolizeOat(const char* oat_filename, std::string& output_name, bool no_bits) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002589 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002590 OatFile* oat_file = OatFile::Open(oat_filename,
2591 oat_filename,
2592 nullptr,
2593 nullptr,
2594 false,
2595 /*low_4gb*/false,
2596 nullptr,
2597 &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002598 if (oat_file == nullptr) {
2599 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2600 return EXIT_FAILURE;
2601 }
2602
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002603 bool result;
2604 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
2605 // files for 64-bit code in the past.
2606 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002607 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file, output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002608 result = oat_symbolizer.Symbolize();
2609 } else {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002610 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file, output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002611 result = oat_symbolizer.Symbolize();
2612 }
2613 if (!result) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002614 fprintf(stderr, "Failed to symbolize\n");
2615 return EXIT_FAILURE;
2616 }
2617
2618 return EXIT_SUCCESS;
2619}
2620
Andreas Gampe9fded872016-09-25 16:08:35 -07002621class IMTDumper {
2622 public:
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002623 static bool Dump(Runtime* runtime,
2624 const std::string& imt_file,
2625 bool dump_imt_stats,
2626 const char* oat_filename) {
2627 Thread* self = Thread::Current();
2628
2629 ScopedObjectAccess soa(self);
2630 StackHandleScope<1> scope(self);
2631 MutableHandle<mirror::ClassLoader> class_loader = scope.NewHandle<mirror::ClassLoader>(nullptr);
2632 std::vector<const DexFile*> class_path;
2633
2634 if (oat_filename != nullptr) {
2635 std::string error_msg;
2636 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
2637 oat_filename,
2638 nullptr,
2639 nullptr,
2640 false,
2641 /*low_4gb*/false,
2642 nullptr,
2643 &error_msg));
2644 if (oat_file == nullptr) {
2645 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2646 return false;
2647 }
2648
2649 class_loader.Assign(soa.Decode<mirror::ClassLoader>(
2650 InstallOatFile(runtime, std::move(oat_file), &class_path)));
2651 } else {
2652 class_loader.Assign(nullptr); // Boot classloader. Just here for explicit documentation.
2653 class_path = runtime->GetClassLinker()->GetBootClassPath();
2654 }
2655
2656 if (!imt_file.empty()) {
2657 return DumpImt(runtime, imt_file, class_loader);
2658 }
2659
2660 if (dump_imt_stats) {
2661 return DumpImtStats(runtime, class_path, class_loader);
2662 }
2663
2664 LOG(FATAL) << "Should not reach here";
2665 UNREACHABLE();
2666 }
2667
2668 private:
2669 static bool DumpImt(Runtime* runtime,
2670 const std::string& imt_file,
2671 Handle<mirror::ClassLoader> h_class_loader)
2672 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002673 std::vector<std::string> lines = ReadCommentedInputFromFile(imt_file);
2674 std::unordered_set<std::string> prepared;
2675
2676 for (const std::string& line : lines) {
2677 // A line should be either a class descriptor, in which case we will dump the complete IMT,
2678 // or a class descriptor and an interface method, in which case we will lookup the method,
2679 // determine its IMT slot, and check the class' IMT.
2680 size_t first_space = line.find(' ');
2681 if (first_space == std::string::npos) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002682 DumpIMTForClass(runtime, line, h_class_loader, &prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07002683 } else {
2684 DumpIMTForMethod(runtime,
2685 line.substr(0, first_space),
2686 line.substr(first_space + 1, std::string::npos),
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002687 h_class_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07002688 &prepared);
2689 }
2690 std::cerr << std::endl;
2691 }
2692
2693 return true;
2694 }
2695
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002696 static bool DumpImtStats(Runtime* runtime,
2697 const std::vector<const DexFile*>& dex_files,
2698 Handle<mirror::ClassLoader> h_class_loader)
2699 REQUIRES_SHARED(Locks::mutator_lock_) {
2700 size_t without_imt = 0;
2701 size_t with_imt = 0;
Andreas Gampe9fded872016-09-25 16:08:35 -07002702 std::map<size_t, size_t> histogram;
2703
2704 ClassLinker* class_linker = runtime->GetClassLinker();
2705 const PointerSize pointer_size = class_linker->GetImagePointerSize();
2706 std::unordered_set<std::string> prepared;
2707
2708 Thread* self = Thread::Current();
Andreas Gampe9fded872016-09-25 16:08:35 -07002709 StackHandleScope<1> scope(self);
2710 MutableHandle<mirror::Class> h_klass(scope.NewHandle<mirror::Class>(nullptr));
2711
2712 for (const DexFile* dex_file : dex_files) {
2713 for (uint32_t class_def_index = 0;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002714 class_def_index != dex_file->NumClassDefs();
2715 ++class_def_index) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002716 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
2717 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002718 h_klass.Assign(class_linker->FindClass(self, descriptor, h_class_loader));
Andreas Gampe9fded872016-09-25 16:08:35 -07002719 if (h_klass.Get() == nullptr) {
2720 std::cerr << "Warning: could not load " << descriptor << std::endl;
2721 continue;
2722 }
2723
2724 if (HasNoIMT(runtime, h_klass, pointer_size, &prepared)) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002725 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07002726 continue;
2727 }
2728
2729 ImTable* im_table = PrepareAndGetImTable(runtime, h_klass, pointer_size, &prepared);
2730 if (im_table == nullptr) {
2731 // Should not happen, but accept.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002732 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07002733 continue;
2734 }
2735
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002736 with_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07002737 for (size_t imt_index = 0; imt_index != ImTable::kSize; ++imt_index) {
2738 ArtMethod* ptr = im_table->Get(imt_index, pointer_size);
2739 if (ptr->IsRuntimeMethod()) {
2740 if (ptr->IsImtUnimplementedMethod()) {
2741 histogram[0]++;
2742 } else {
2743 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
2744 histogram[current_table->NumEntries(pointer_size)]++;
2745 }
2746 } else {
2747 histogram[1]++;
2748 }
2749 }
2750 }
2751 }
2752
2753 std::cerr << "IMT stats:"
2754 << std::endl << std::endl;
2755
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002756 std::cerr << " " << with_imt << " classes with IMT."
Andreas Gampe9fded872016-09-25 16:08:35 -07002757 << std::endl << std::endl;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002758 std::cerr << " " << without_imt << " classes without IMT (or copy from Object)."
Andreas Gampe9fded872016-09-25 16:08:35 -07002759 << std::endl << std::endl;
2760
2761 double sum_one = 0;
2762 size_t count_one = 0;
2763
2764 std::cerr << " " << "IMT histogram" << std::endl;
2765 for (auto& bucket : histogram) {
2766 std::cerr << " " << bucket.first << " " << bucket.second << std::endl;
2767 if (bucket.first > 0) {
2768 sum_one += bucket.second * bucket.first;
2769 count_one += bucket.second;
2770 }
2771 }
2772
2773 double count_zero = count_one + histogram[0];
2774 std::cerr << " Stats:" << std::endl;
2775 std::cerr << " Average depth (including empty): " << (sum_one / count_zero) << std::endl;
2776 std::cerr << " Average depth (excluding empty): " << (sum_one / count_one) << std::endl;
2777
2778 return true;
2779 }
2780
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002781 // Return whether the given class has no IMT (or the one shared with java.lang.Object).
Andreas Gampe9fded872016-09-25 16:08:35 -07002782 static bool HasNoIMT(Runtime* runtime,
2783 Handle<mirror::Class> klass,
2784 const PointerSize pointer_size,
2785 std::unordered_set<std::string>* prepared)
2786 REQUIRES_SHARED(Locks::mutator_lock_) {
2787 if (klass->IsObjectClass() || !klass->ShouldHaveImt()) {
2788 return true;
2789 }
2790
2791 if (klass->GetImt(pointer_size) == nullptr) {
2792 PrepareClass(runtime, klass, prepared);
2793 }
2794
2795 mirror::Class* object_class = mirror::Class::GetJavaLangClass()->GetSuperClass();
2796 DCHECK(object_class->IsObjectClass());
2797
2798 bool result = klass->GetImt(pointer_size) == object_class->GetImt(pointer_size);
2799
Mathieu Chartier6beced42016-11-15 15:51:31 -08002800 if (klass->GetIfTable()->Count() == 0) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002801 DCHECK(result);
2802 }
2803
2804 return result;
2805 }
2806
2807 static void PrintTable(ImtConflictTable* table, PointerSize pointer_size)
2808 REQUIRES_SHARED(Locks::mutator_lock_) {
2809 if (table == nullptr) {
2810 std::cerr << " <No IMT?>" << std::endl;
2811 return;
2812 }
2813 size_t table_index = 0;
2814 for (;;) {
2815 ArtMethod* ptr = table->GetInterfaceMethod(table_index, pointer_size);
2816 if (ptr == nullptr) {
2817 return;
2818 }
2819 table_index++;
David Sehr709b0702016-10-13 09:12:37 -07002820 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07002821 }
2822 }
2823
2824 static ImTable* PrepareAndGetImTable(Runtime* runtime,
2825 Thread* self,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002826 Handle<mirror::ClassLoader> h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07002827 const std::string& class_name,
2828 const PointerSize pointer_size,
2829 mirror::Class** klass_out,
2830 std::unordered_set<std::string>* prepared)
2831 REQUIRES_SHARED(Locks::mutator_lock_) {
2832 if (class_name.empty()) {
2833 return nullptr;
2834 }
2835
2836 std::string descriptor;
2837 if (class_name[0] == 'L') {
2838 descriptor = class_name;
2839 } else {
2840 descriptor = DotToDescriptor(class_name.c_str());
2841 }
2842
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002843 mirror::Class* klass = runtime->GetClassLinker()->FindClass(self, descriptor.c_str(), h_loader);
Andreas Gampe9fded872016-09-25 16:08:35 -07002844
2845 if (klass == nullptr) {
2846 self->ClearException();
2847 std::cerr << "Did not find " << class_name << std::endl;
2848 *klass_out = nullptr;
2849 return nullptr;
2850 }
2851
2852 StackHandleScope<1> scope(Thread::Current());
2853 Handle<mirror::Class> h_klass = scope.NewHandle<mirror::Class>(klass);
2854
2855 ImTable* ret = PrepareAndGetImTable(runtime, h_klass, pointer_size, prepared);
2856 *klass_out = h_klass.Get();
2857 return ret;
2858 }
2859
2860 static ImTable* PrepareAndGetImTable(Runtime* runtime,
2861 Handle<mirror::Class> h_klass,
2862 const PointerSize pointer_size,
2863 std::unordered_set<std::string>* prepared)
2864 REQUIRES_SHARED(Locks::mutator_lock_) {
2865 PrepareClass(runtime, h_klass, prepared);
2866 return h_klass->GetImt(pointer_size);
2867 }
2868
2869 static void DumpIMTForClass(Runtime* runtime,
2870 const std::string& class_name,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002871 Handle<mirror::ClassLoader> h_loader,
2872 std::unordered_set<std::string>* prepared)
2873 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002874 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
2875 mirror::Class* klass;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002876 ImTable* imt = PrepareAndGetImTable(runtime,
2877 Thread::Current(),
2878 h_loader,
2879 class_name,
2880 pointer_size,
2881 &klass,
2882 prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07002883 if (imt == nullptr) {
2884 return;
2885 }
2886
2887 std::cerr << class_name << std::endl << " IMT:" << std::endl;
2888 for (size_t index = 0; index < ImTable::kSize; ++index) {
2889 std::cerr << " " << index << ":" << std::endl;
2890 ArtMethod* ptr = imt->Get(index, pointer_size);
2891 if (ptr->IsRuntimeMethod()) {
2892 if (ptr->IsImtUnimplementedMethod()) {
2893 std::cerr << " <empty>" << std::endl;
2894 } else {
2895 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
2896 PrintTable(current_table, pointer_size);
2897 }
2898 } else {
David Sehr709b0702016-10-13 09:12:37 -07002899 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07002900 }
2901 }
2902
2903 std::cerr << " Interfaces:" << std::endl;
2904 // Run through iftable, find methods that slot here, see if they fit.
2905 mirror::IfTable* if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08002906 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
2907 mirror::Class* iface = if_table->GetInterface(i);
2908 std::string iface_name;
2909 std::cerr << " " << iface->GetDescriptor(&iface_name) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07002910
Mathieu Chartier6beced42016-11-15 15:51:31 -08002911 for (ArtMethod& iface_method : iface->GetVirtualMethods(pointer_size)) {
2912 uint32_t class_hash, name_hash, signature_hash;
2913 ImTable::GetImtHashComponents(&iface_method, &class_hash, &name_hash, &signature_hash);
2914 uint32_t imt_slot = ImTable::GetImtIndex(&iface_method);
2915 std::cerr << " " << iface_method.PrettyMethod(true)
2916 << " slot=" << imt_slot
2917 << std::hex
2918 << " class_hash=0x" << class_hash
2919 << " name_hash=0x" << name_hash
2920 << " signature_hash=0x" << signature_hash
2921 << std::dec
2922 << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07002923 }
2924 }
2925 }
2926
2927 static void DumpIMTForMethod(Runtime* runtime,
2928 const std::string& class_name,
2929 const std::string& method,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002930 Handle<mirror::ClassLoader> h_loader,
2931 std::unordered_set<std::string>* prepared)
2932 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002933 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
2934 mirror::Class* klass;
2935 ImTable* imt = PrepareAndGetImTable(runtime,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002936 Thread::Current(),
2937 h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07002938 class_name,
2939 pointer_size,
2940 &klass,
2941 prepared);
2942 if (imt == nullptr) {
2943 return;
2944 }
2945
2946 std::cerr << class_name << " <" << method << ">" << std::endl;
2947 for (size_t index = 0; index < ImTable::kSize; ++index) {
2948 ArtMethod* ptr = imt->Get(index, pointer_size);
2949 if (ptr->IsRuntimeMethod()) {
2950 if (ptr->IsImtUnimplementedMethod()) {
2951 continue;
2952 }
2953
2954 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
2955 if (current_table == nullptr) {
2956 continue;
2957 }
2958
2959 size_t table_index = 0;
2960 for (;;) {
2961 ArtMethod* ptr2 = current_table->GetInterfaceMethod(table_index, pointer_size);
2962 if (ptr2 == nullptr) {
2963 break;
2964 }
2965 table_index++;
2966
David Sehr709b0702016-10-13 09:12:37 -07002967 std::string p_name = ptr2->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08002968 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002969 std::cerr << " Slot "
2970 << index
2971 << " ("
2972 << current_table->NumEntries(pointer_size)
2973 << ")"
2974 << std::endl;
2975 PrintTable(current_table, pointer_size);
2976 return;
2977 }
2978 }
2979 } else {
David Sehr709b0702016-10-13 09:12:37 -07002980 std::string p_name = ptr->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08002981 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002982 std::cerr << " Slot " << index << " (1)" << std::endl;
2983 std::cerr << " " << p_name << std::endl;
2984 } else {
2985 // Run through iftable, find methods that slot here, see if they fit.
2986 mirror::IfTable* if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08002987 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
2988 mirror::Class* iface = if_table->GetInterface(i);
2989 size_t num_methods = iface->NumDeclaredVirtualMethods();
2990 if (num_methods > 0) {
2991 for (ArtMethod& iface_method : iface->GetMethods(pointer_size)) {
2992 if (ImTable::GetImtIndex(&iface_method) == index) {
2993 std::string i_name = iface_method.PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08002994 if (android::base::StartsWith(i_name, method.c_str())) {
Mathieu Chartier6beced42016-11-15 15:51:31 -08002995 std::cerr << " Slot " << index << " (1)" << std::endl;
2996 std::cerr << " " << p_name << " (" << i_name << ")" << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07002997 }
2998 }
2999 }
3000 }
3001 }
3002 }
3003 }
3004 }
3005 }
3006
3007 // Read lines from the given stream, dropping comments and empty lines
3008 static std::vector<std::string> ReadCommentedInputStream(std::istream& in_stream) {
3009 std::vector<std::string> output;
3010 while (in_stream.good()) {
3011 std::string dot;
3012 std::getline(in_stream, dot);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003013 if (android::base::StartsWith(dot, "#") || dot.empty()) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003014 continue;
3015 }
3016 output.push_back(dot);
3017 }
3018 return output;
3019 }
3020
3021 // Read lines from the given file, dropping comments and empty lines.
3022 static std::vector<std::string> ReadCommentedInputFromFile(const std::string& input_filename) {
3023 std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
3024 if (input_file.get() == nullptr) {
3025 LOG(ERROR) << "Failed to open input file " << input_filename;
3026 return std::vector<std::string>();
3027 }
3028 std::vector<std::string> result = ReadCommentedInputStream(*input_file);
3029 input_file->close();
3030 return result;
3031 }
3032
3033 // Prepare a class, i.e., ensure it has a filled IMT. Will do so recursively for superclasses,
3034 // and note in the given set that the work was done.
3035 static void PrepareClass(Runtime* runtime,
3036 Handle<mirror::Class> h_klass,
3037 std::unordered_set<std::string>* done)
3038 REQUIRES_SHARED(Locks::mutator_lock_) {
3039 if (!h_klass->ShouldHaveImt()) {
3040 return;
3041 }
3042
3043 std::string name;
3044 name = h_klass->GetDescriptor(&name);
3045
3046 if (done->find(name) != done->end()) {
3047 return;
3048 }
3049 done->insert(name);
3050
3051 if (h_klass->HasSuperClass()) {
3052 StackHandleScope<1> h(Thread::Current());
3053 PrepareClass(runtime, h.NewHandle<mirror::Class>(h_klass->GetSuperClass()), done);
3054 }
3055
3056 if (!h_klass->IsTemp()) {
3057 runtime->GetClassLinker()->FillIMTAndConflictTables(h_klass.Get());
3058 }
3059 }
3060};
3061
Igor Murashkin37743352014-11-13 14:38:00 -08003062struct OatdumpArgs : public CmdlineArgs {
3063 protected:
3064 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003065
Igor Murashkin37743352014-11-13 14:38:00 -08003066 virtual ParseStatus ParseCustom(const StringPiece& option,
3067 std::string* error_msg) OVERRIDE {
3068 {
3069 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
3070 if (base_parse != kParseUnknownArgument) {
3071 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003072 }
3073 }
3074
Igor Murashkin37743352014-11-13 14:38:00 -08003075 if (option.starts_with("--oat-file=")) {
3076 oat_filename_ = option.substr(strlen("--oat-file=")).data();
3077 } else if (option.starts_with("--image=")) {
3078 image_location_ = option.substr(strlen("--image=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003079 } else if (option == "--no-dump:vmap") {
3080 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01003081 } else if (option =="--dump:code_info_stack_maps") {
3082 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003083 } else if (option == "--no-disassemble") {
3084 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003085 } else if (option =="--header-only") {
3086 dump_header_only_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003087 } else if (option.starts_with("--symbolize=")) {
3088 oat_filename_ = option.substr(strlen("--symbolize=")).data();
3089 symbolize_ = true;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003090 } else if (option.starts_with("--only-keep-debug")) {
3091 only_keep_debug_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003092 } else if (option.starts_with("--class-filter=")) {
3093 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003094 } else if (option.starts_with("--method-filter=")) {
3095 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003096 } else if (option.starts_with("--list-classes")) {
3097 list_classes_ = true;
3098 } else if (option.starts_with("--list-methods")) {
3099 list_methods_ = true;
3100 } else if (option.starts_with("--export-dex-to=")) {
3101 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
3102 } else if (option.starts_with("--addr2instr=")) {
3103 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
3104 *error_msg = "Address conversion failed";
3105 return kParseError;
3106 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003107 } else if (option.starts_with("--app-image=")) {
3108 app_image_ = option.substr(strlen("--app-image=")).data();
3109 } else if (option.starts_with("--app-oat=")) {
3110 app_oat_ = option.substr(strlen("--app-oat=")).data();
Andreas Gampe9fded872016-09-25 16:08:35 -07003111 } else if (option.starts_with("--dump-imt=")) {
3112 imt_dump_ = option.substr(strlen("--dump-imt=")).data();
3113 } else if (option == "--dump-imt-stats") {
3114 imt_stat_dump_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003115 } else {
3116 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003117 }
3118
Igor Murashkin37743352014-11-13 14:38:00 -08003119 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003120 }
3121
Igor Murashkin37743352014-11-13 14:38:00 -08003122 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
3123 // Infer boot image location from the image location if possible.
3124 if (boot_image_location_ == nullptr) {
3125 boot_image_location_ = image_location_;
3126 }
3127
3128 // Perform the parent checks.
3129 ParseStatus parent_checks = Base::ParseChecks(error_msg);
3130 if (parent_checks != kParseOk) {
3131 return parent_checks;
3132 }
3133
3134 // Perform our own checks.
3135 if (image_location_ == nullptr && oat_filename_ == nullptr) {
3136 *error_msg = "Either --image or --oat-file must be specified";
3137 return kParseError;
3138 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
3139 *error_msg = "Either --image or --oat-file must be specified but not both";
3140 return kParseError;
3141 }
3142
3143 return kParseOk;
3144 }
3145
3146 virtual std::string GetUsage() const {
3147 std::string usage;
3148
3149 usage +=
3150 "Usage: oatdump [options] ...\n"
3151 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
3152 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
3153 "\n"
3154 // Either oat-file or image is required.
3155 " --oat-file=<file.oat>: specifies an input oat filename.\n"
3156 " Example: --oat-file=/system/framework/boot.oat\n"
3157 "\n"
3158 " --image=<file.art>: specifies an input image location.\n"
3159 " Example: --image=/system/framework/boot.art\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003160 "\n"
3161 " --app-image=<file.art>: specifies an input app image. Must also have a specified\n"
3162 " boot image and app oat file.\n"
3163 " Example: --app-image=app.art\n"
3164 "\n"
3165 " --app-oat=<file.odex>: specifies an input app oat.\n"
3166 " Example: --app-oat=app.odex\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003167 "\n";
3168
3169 usage += Base::GetUsage();
3170
3171 usage += // Optional.
Igor Murashkin37743352014-11-13 14:38:00 -08003172 " --no-dump:vmap may be used to disable vmap dumping.\n"
3173 " Example: --no-dump:vmap\n"
3174 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01003175 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
3176 " Example: --dump:code_info_stack_maps\n"
3177 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003178 " --no-disassemble may be used to disable disassembly.\n"
3179 " Example: --no-disassemble\n"
3180 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00003181 " --header-only may be used to print only the oat header.\n"
3182 " Example: --header-only\n"
3183 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003184 " --list-classes may be used to list target file classes (can be used with filters).\n"
3185 " Example: --list-classes\n"
3186 " Example: --list-classes --class-filter=com.example.foo\n"
3187 "\n"
3188 " --list-methods may be used to list target file methods (can be used with filters).\n"
3189 " Example: --list-methods\n"
3190 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
3191 "\n"
3192 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
3193 " Example: --symbolize=/system/framework/boot.oat\n"
3194 "\n"
David Srbecky2fdd03c2016-03-10 15:32:37 +00003195 " --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
3196 " .rodata and .text sections are omitted in the output file to save space.\n"
3197 " Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
3198 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003199 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
3200 " Example: --class-filter=com.example.foo\n"
3201 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003202 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
3203 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003204 "\n"
3205 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
3206 " Example: --export-dex-to=/data/local/tmp\n"
3207 "\n"
3208 " --addr2instr=<address>: output matching method disassembled code from relative\n"
3209 " address (e.g. PC from crash dump)\n"
3210 " Example: --addr2instr=0x00001a3b\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003211 "\n"
3212 " --dump-imt=<file.txt>: output IMT collisions (if any) for the given receiver\n"
3213 " types and interface methods in the given file. The file\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003214 " is read line-wise, where each line should either be a class\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003215 " name or descriptor, or a class name/descriptor and a prefix\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003216 " of a complete method name (separated by a whitespace).\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003217 " Example: --dump-imt=imt.txt\n"
3218 "\n"
3219 " --dump-imt-stats: output IMT statistics for the given boot image\n"
3220 " Example: --dump-imt-stats"
Igor Murashkin37743352014-11-13 14:38:00 -08003221 "\n";
3222
3223 return usage;
3224 }
3225
3226 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07003227 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003228 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00003229 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07003230 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003231 std::string elf_filename_prefix_;
Andreas Gampe9fded872016-09-25 16:08:35 -07003232 std::string imt_dump_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003233 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01003234 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003235 bool disassemble_code_ = true;
3236 bool symbolize_ = false;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003237 bool only_keep_debug_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003238 bool list_classes_ = false;
3239 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003240 bool dump_header_only_ = false;
Andreas Gampe9fded872016-09-25 16:08:35 -07003241 bool imt_stat_dump_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003242 uint32_t addr2instr_ = 0;
3243 const char* export_dex_location_ = nullptr;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003244 const char* app_image_ = nullptr;
3245 const char* app_oat_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003246};
3247
Igor Murashkin37743352014-11-13 14:38:00 -08003248struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
3249 virtual bool NeedsRuntime() OVERRIDE {
3250 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003251
Igor Murashkin37743352014-11-13 14:38:00 -08003252 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
3253 bool absolute_addresses = (args_->oat_filename_ == nullptr);
3254
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003255 oat_dumper_options_.reset(new OatDumperOptions(
Igor Murashkin37743352014-11-13 14:38:00 -08003256 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01003257 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08003258 args_->disassemble_code_,
3259 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003260 args_->class_filter_,
3261 args_->method_filter_,
3262 args_->list_classes_,
3263 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00003264 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003265 args_->export_dex_location_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003266 args_->app_image_,
3267 args_->app_oat_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003268 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08003269
Andreas Gampe9fded872016-09-25 16:08:35 -07003270 return (args_->boot_image_location_ != nullptr ||
3271 args_->image_location_ != nullptr ||
3272 !args_->imt_dump_.empty()) &&
Igor Murashkin37743352014-11-13 14:38:00 -08003273 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003274 }
3275
Igor Murashkin37743352014-11-13 14:38:00 -08003276 virtual bool ExecuteWithoutRuntime() OVERRIDE {
3277 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08003278 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003279
Mathieu Chartierd424d082014-10-15 10:31:46 -07003280 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08003281
Andreas Gampec24f3992014-12-17 20:40:11 -08003282 if (args_->symbolize_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00003283 // ELF has special kind of section called SHT_NOBITS which allows us to create
3284 // sections which exist but their data is omitted from the ELF file to save space.
3285 // This is what "strip --only-keep-debug" does when it creates separate ELF file
3286 // with only debug data. We use it in similar way to exclude .rodata and .text.
3287 bool no_bits = args_->only_keep_debug_;
3288 return SymbolizeOat(args_->oat_filename_, args_->output_name_, no_bits) == EXIT_SUCCESS;
Andreas Gampec24f3992014-12-17 20:40:11 -08003289 } else {
3290 return DumpOat(nullptr,
3291 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003292 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08003293 args_->os_) == EXIT_SUCCESS;
3294 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07003295 }
3296
Igor Murashkin37743352014-11-13 14:38:00 -08003297 virtual bool ExecuteWithRuntime(Runtime* runtime) {
3298 CHECK(args_ != nullptr);
3299
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003300 if (!args_->imt_dump_.empty() || args_->imt_stat_dump_) {
3301 return IMTDumper::Dump(runtime,
3302 args_->imt_dump_,
3303 args_->imt_stat_dump_,
3304 args_->oat_filename_);
Andreas Gampe9fded872016-09-25 16:08:35 -07003305 }
3306
Igor Murashkin37743352014-11-13 14:38:00 -08003307 if (args_->oat_filename_ != nullptr) {
3308 return DumpOat(runtime,
3309 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003310 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08003311 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003312 }
Igor Murashkin37743352014-11-13 14:38:00 -08003313
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003314 return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003315 }
3316
Igor Murashkin37743352014-11-13 14:38:00 -08003317 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
3318};
Andreas Gampe00b25f32014-09-17 21:49:05 -07003319
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003320} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07003321
3322int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08003323 art::OatdumpMain main;
3324 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07003325}