blob: 0f02da77a1526d2ca9a13038ac4a0c8dabdd83eb [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
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800532 {
533 os << "OAT FILE STATS:\n";
534 VariableIndentationOutputStream vios(&os);
535 stats_.Dump(vios);
536 }
537
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700538 os << std::flush;
539 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700540 }
541
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800542 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700543 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
544 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800545 return 0; // Address not in oat file
546 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800547 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
548 reinterpret_cast<uintptr_t>(oat_file_.Begin());
549 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800550 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800551 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800552 return end_offset - begin_offset;
553 }
554
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800555 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700556 return oat_file_.GetOatHeader().GetInstructionSet();
557 }
558
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700559 const void* GetQuickOatCode(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800560 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
561 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700562 CHECK(oat_dex_file != nullptr);
563 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700564 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
565 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700566 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
567 << "': " << error_msg;
568 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800569 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700570 const DexFile::ClassDef* class_def =
David Sehr9aa352e2016-09-15 18:13:52 -0700571 OatDexFile::FindClassDef(*dex_file, descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700572 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700573 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100574 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800575 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100576 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800577 }
578 }
579 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700580 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800581 }
582
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800583 struct Stats {
584 enum ByteKind {
585 kByteKindCode,
586 kByteKindQuickMethodHeader,
587 kByteKindCodeInfoLocationCatalog,
588 kByteKindCodeInfoDexRegisterMap,
589 kByteKindCodeInfoInlineInfo,
590 kByteKindCodeInfoEncoding,
591 kByteKindCodeInfoOther,
David Srbecky45aa5982016-03-18 02:15:09 +0000592 kByteKindCodeInfoStackMasks,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800593 kByteKindCodeInfoRegisterMasks,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800594 kByteKindStackMapNativePc,
595 kByteKindStackMapDexPc,
596 kByteKindStackMapDexRegisterMap,
597 kByteKindStackMapInlineInfo,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800598 kByteKindStackMapRegisterMaskIndex,
David Srbecky45aa5982016-03-18 02:15:09 +0000599 kByteKindStackMapStackMaskIndex,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800600 kByteKindCount,
601 kByteKindStackMapFirst = kByteKindCodeInfoOther,
David Srbecky45aa5982016-03-18 02:15:09 +0000602 kByteKindStackMapLast = kByteKindStackMapStackMaskIndex,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800603 };
604 int64_t bits[kByteKindCount] = {};
605 // Since code has deduplication, seen tracks already seen pointers to avoid double counting
606 // deduplicated code and tables.
607 std::unordered_set<const void*> seen;
608
609 // Returns true if it was newly added.
610 bool AddBitsIfUnique(ByteKind kind, int64_t count, const void* address) {
611 if (seen.insert(address).second == true) {
612 // True means the address was not already in the set.
613 AddBits(kind, count);
614 return true;
615 }
616 return false;
617 }
618
619 void AddBits(ByteKind kind, int64_t count) {
620 bits[kind] += count;
621 }
622
623 void Dump(VariableIndentationOutputStream& os) {
624 const int64_t sum = std::accumulate(bits, bits + kByteKindCount, 0u);
625 os.Stream() << "Dumping cumulative use of " << sum / kBitsPerByte << " accounted bytes\n";
626 if (sum > 0) {
627 const int64_t stack_map_bits = std::accumulate(bits + kByteKindStackMapFirst,
628 bits + kByteKindStackMapLast + 1,
629 0u);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800630 Dump(os, "Code ", bits[kByteKindCode], sum);
631 Dump(os, "QuickMethodHeader ", bits[kByteKindQuickMethodHeader], sum);
632 Dump(os, "CodeInfoEncoding ", bits[kByteKindCodeInfoEncoding], sum);
633 Dump(os, "CodeInfoLocationCatalog ", bits[kByteKindCodeInfoLocationCatalog], sum);
634 Dump(os, "CodeInfoDexRegisterMap ", bits[kByteKindCodeInfoDexRegisterMap], sum);
635 Dump(os, "CodeInfoInlineInfo ", bits[kByteKindCodeInfoInlineInfo], sum);
636 Dump(os, "CodeInfoStackMasks ", bits[kByteKindCodeInfoStackMasks], sum);
637 Dump(os, "CodeInfoRegisterMasks ", bits[kByteKindCodeInfoRegisterMasks], sum);
638 Dump(os, "CodeInfoStackMap ", stack_map_bits, sum);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800639 {
640 ScopedIndentation indent1(&os);
641 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800642 "StackMapNativePc ",
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800643 bits[kByteKindStackMapNativePc],
644 stack_map_bits,
645 "stack map");
646 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800647 "StackMapDexPcEncoding ",
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800648 bits[kByteKindStackMapDexPc],
649 stack_map_bits,
650 "stack map");
651 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800652 "StackMapDexRegisterMap ",
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800653 bits[kByteKindStackMapDexRegisterMap],
654 stack_map_bits,
655 "stack map");
656 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800657 "StackMapInlineInfo ",
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800658 bits[kByteKindStackMapInlineInfo],
659 stack_map_bits,
660 "stack map");
661 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800662 "StackMapRegisterMaskIndex ",
663 bits[kByteKindStackMapRegisterMaskIndex],
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800664 stack_map_bits,
665 "stack map");
666 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800667 "StackMapStackMaskIndex ",
David Srbecky45aa5982016-03-18 02:15:09 +0000668 bits[kByteKindStackMapStackMaskIndex],
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800669 stack_map_bits,
670 "stack map");
671 }
672 }
673 os.Stream() << "\n" << std::flush;
674 }
675
676 private:
677 void Dump(VariableIndentationOutputStream& os,
678 const char* name,
679 int64_t size,
680 int64_t total,
681 const char* sum_of = "total") {
682 const double percent = (static_cast<double>(size) / static_cast<double>(total)) * 100;
683 os.Stream() << StringPrintf("%s = %8" PRId64 " (%2.0f%% of %s)\n",
684 name,
685 size / kBitsPerByte,
686 percent,
687 sum_of);
688 }
689 };
690
Brian Carlstromaded5f72011-10-07 17:15:04 -0700691 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800692 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800693 // We don't know the length of the code for each method, but we need to know where to stop
694 // when disassembling. What we do know is that a region of code will be followed by some other
695 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
696 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800697 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
698 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700699 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700700 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700701 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
702 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700703 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
704 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800705 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800706 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800707 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800708 for (size_t class_def_index = 0;
709 class_def_index < dex_file->NumClassDefs();
710 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800711 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100712 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700713 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700714 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800715 ClassDataItemIterator it(*dex_file, class_data);
716 SkipAllFields(it);
717 uint32_t class_method_index = 0;
718 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100719 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800720 it.Next();
721 }
722 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100723 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800724 it.Next();
725 }
726 }
727 }
728 }
729
730 // If the last thing in the file is code for a method, there won't be an offset for the "next"
731 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
732 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800733 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800734 }
735
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700736 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
737 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
738 }
739
Elliott Hughese3c845c2012-02-28 17:23:01 -0800740 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800741 uint32_t code_offset = oat_method.GetCodeOffset();
742 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
743 code_offset &= ~0x1;
744 }
745 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800746 offsets_.insert(oat_method.GetVmapTableOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800747 }
748
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700749 // Dex file data, may be for multiple different dex files.
750 class DexFileData {
751 public:
752 DexFileData() {}
753
754 explicit DexFileData(const DexFile& dex_file)
755 : num_string_ids_(dex_file.NumStringIds()),
756 num_method_ids_(dex_file.NumMethodIds()),
757 num_field_ids_(dex_file.NumFieldIds()),
758 num_type_ids_(dex_file.NumTypeIds()),
759 num_class_defs_(dex_file.NumClassDefs()) {
760 for (size_t class_def_index = 0; class_def_index < num_class_defs_; ++class_def_index) {
761 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
762 WalkClass(dex_file, class_def);
763 }
764 }
765
766 void Add(const DexFileData& other) {
767 AddAll(unique_string_ids_from_code_, other.unique_string_ids_from_code_);
768 num_string_ids_from_code_ += other.num_string_ids_from_code_;
769 AddAll(dex_code_item_ptrs_, other.dex_code_item_ptrs_);
770 dex_code_bytes_ += other.dex_code_bytes_;
771 num_string_ids_ += other.num_string_ids_;
772 num_method_ids_ += other.num_method_ids_;
773 num_field_ids_ += other.num_field_ids_;
774 num_type_ids_ += other.num_type_ids_;
775 num_class_defs_ += other.num_class_defs_;
776 }
777
778 void Dump(std::ostream& os) {
779 os << "Num string ids: " << num_string_ids_ << "\n";
780 os << "Num method ids: " << num_method_ids_ << "\n";
781 os << "Num field ids: " << num_field_ids_ << "\n";
782 os << "Num type ids: " << num_type_ids_ << "\n";
783 os << "Num class defs: " << num_class_defs_ << "\n";
784 os << "Unique strings loaded from dex code: " << unique_string_ids_from_code_.size() << "\n";
785 os << "Total strings loaded from dex code: " << num_string_ids_from_code_ << "\n";
786 os << "Number of unique dex code items: " << dex_code_item_ptrs_.size() << "\n";
787 os << "Total number of dex code bytes: " << dex_code_bytes_ << "\n";
788 }
789
790 private:
Andreas Gampe9186ced2016-12-12 14:28:21 -0800791 // All of the elements from one container to another.
792 template <typename Dest, typename Src>
793 static void AddAll(Dest& dest, const Src& src) {
794 dest.insert(src.begin(), src.end());
795 }
796
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700797 void WalkClass(const DexFile& dex_file, const DexFile::ClassDef& class_def) {
798 const uint8_t* class_data = dex_file.GetClassData(class_def);
799 if (class_data == nullptr) { // empty class such as a marker interface?
800 return;
801 }
802 ClassDataItemIterator it(dex_file, class_data);
803 SkipAllFields(it);
804 while (it.HasNextDirectMethod()) {
805 WalkCodeItem(dex_file, it.GetMethodCodeItem());
806 it.Next();
807 }
808 while (it.HasNextVirtualMethod()) {
809 WalkCodeItem(dex_file, it.GetMethodCodeItem());
810 it.Next();
811 }
812 DCHECK(!it.HasNext());
813 }
814
815 void WalkCodeItem(const DexFile& dex_file, const DexFile::CodeItem* code_item) {
816 if (code_item == nullptr) {
817 return;
818 }
819 const size_t code_item_size = code_item->insns_size_in_code_units_;
820 const uint16_t* code_ptr = code_item->insns_;
821 const uint16_t* code_end = code_item->insns_ + code_item_size;
822
823 // If we inserted a new dex code item pointer, add to total code bytes.
824 if (dex_code_item_ptrs_.insert(code_ptr).second) {
825 dex_code_bytes_ += code_item_size * sizeof(code_ptr[0]);
826 }
827
828 while (code_ptr < code_end) {
829 const Instruction* inst = Instruction::At(code_ptr);
830 switch (inst->Opcode()) {
831 case Instruction::CONST_STRING: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800832 const dex::StringIndex string_index(inst->VRegB_21c());
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700833 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
834 ++num_string_ids_from_code_;
835 break;
836 }
837 case Instruction::CONST_STRING_JUMBO: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800838 const dex::StringIndex string_index(inst->VRegB_31c());
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700839 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
840 ++num_string_ids_from_code_;
841 break;
842 }
843 default:
844 break;
845 }
846
847 code_ptr += inst->SizeInCodeUnits();
848 }
849 }
850
851 // Unique string ids loaded from dex code.
852 std::set<StringReference, StringReferenceComparator> unique_string_ids_from_code_;
853
854 // Total string ids loaded from dex code.
855 size_t num_string_ids_from_code_ = 0;
856
857 // Unique code pointers.
858 std::set<const void*> dex_code_item_ptrs_;
859
860 // Total "unique" dex code bytes.
861 size_t dex_code_bytes_ = 0;
862
863 // Other dex ids.
864 size_t num_string_ids_ = 0;
865 size_t num_method_ids_ = 0;
866 size_t num_field_ids_ = 0;
867 size_t num_type_ids_ = 0;
868 size_t num_class_defs_ = 0;
869 };
870
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700871 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
872 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800873 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700874 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800875 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800876 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700877
Andreas Gampe2ba88952016-04-29 17:52:07 -0700878 const uint8_t* const oat_file_begin = oat_dex_file.GetOatFile()->Begin();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100879 const uint8_t* const vdex_file_begin = oat_dex_file.GetOatFile()->DexBegin();
880
881 // Print data range of the dex file embedded inside the corresponding vdex file.
Andreas Gampe2ba88952016-04-29 17:52:07 -0700882 const uint8_t* const dex_file_pointer = oat_dex_file.GetDexFilePointer();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100883 uint32_t dex_offset = dchecked_integral_cast<uint32_t>(dex_file_pointer - vdex_file_begin);
Andreas Gampe2ba88952016-04-29 17:52:07 -0700884 os << StringPrintf("dex-file: 0x%08x..0x%08x\n",
885 dex_offset,
886 dchecked_integral_cast<uint32_t>(dex_offset + oat_dex_file.FileSize() - 1));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700887
Andreas Gampe2ba88952016-04-29 17:52:07 -0700888 // Create the dex file early. A lot of print-out things depend on it.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700889 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700890 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
891 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700892 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700893 os << std::flush;
894 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700895 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100896
Andreas Gampe2ba88952016-04-29 17:52:07 -0700897 // Print lookup table, if it exists.
898 if (oat_dex_file.GetLookupTableData() != nullptr) {
899 uint32_t table_offset = dchecked_integral_cast<uint32_t>(
900 oat_dex_file.GetLookupTableData() - oat_file_begin);
David Sehr9aa352e2016-09-15 18:13:52 -0700901 uint32_t table_size = TypeLookupTable::RawDataLength(dex_file->NumClassDefs());
Andreas Gampe2ba88952016-04-29 17:52:07 -0700902 os << StringPrintf("type-table: 0x%08x..0x%08x\n",
903 table_offset,
904 table_offset + table_size - 1);
905 }
906
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100907 VariableIndentationOutputStream vios(&os);
908 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800909 for (size_t class_def_index = 0;
910 class_def_index < dex_file->NumClassDefs();
911 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700912 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
913 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800914
915 // TODO: Support regex
916 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
917 continue;
918 }
919
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700920 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100921 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700922 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
Andreas Gampea5b09a62016-11-17 15:21:22 -0800923 class_def_index, descriptor, oat_class_offset, class_def.class_idx_.index_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100924 << " (" << oat_class.GetStatus() << ")"
925 << " (" << oat_class.GetType() << ")\n";
926 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700927 if (options_.list_classes_) {
928 continue;
929 }
930 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700931 success = false;
932 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800933 if (stop_analysis) {
934 os << std::flush;
935 return success;
936 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700937 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700938 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700939 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700940 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700941 }
942
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800943 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
944 std::string error_msg;
945 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
946
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700947 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800948 if (dex_file == nullptr) {
949 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
950 return false;
951 }
952 size_t fsize = oat_dex_file.FileSize();
953
954 // Some quick checks just in case
955 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
956 os << "Invalid dex file\n";
957 return false;
958 }
959
960 // Verify output directory exists
961 if (!OS::DirectoryExists(options_.export_dex_location_)) {
962 // TODO: Extend OS::DirectoryExists if symlink support is required
963 os << options_.export_dex_location_ << " output directory not found or symlink\n";
964 return false;
965 }
966
967 // Beautify path names
968 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
969 return false;
970 }
971
972 std::string dex_orig_name;
973 size_t dex_orig_pos = dex_file_location.rfind('/');
974 if (dex_orig_pos == std::string::npos)
975 dex_orig_name = dex_file_location;
976 else
977 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
978
979 // A more elegant approach to efficiently name user installed apps is welcome
980 if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
981 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
982 size_t apk_orig_pos = dex_file_location.rfind('/');
983 if (apk_orig_pos != std::string::npos) {
984 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
985 }
986 }
987
988 std::string out_dex_path(options_.export_dex_location_);
989 if (out_dex_path.back() != '/') {
990 out_dex_path.append("/");
991 }
992 out_dex_path.append(dex_orig_name);
993 out_dex_path.append("_export.dex");
994 if (out_dex_path.length() > PATH_MAX) {
995 return false;
996 }
997
998 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
999 if (file.get() == nullptr) {
1000 os << "Failed to open output dex file " << out_dex_path;
1001 return false;
1002 }
1003
1004 if (!file->WriteFully(dex_file->Begin(), fsize)) {
1005 os << "Failed to write dex file";
1006 file->Erase();
1007 return false;
1008 }
1009
1010 if (file->FlushCloseOrErase() != 0) {
1011 os << "Flush and close failed";
1012 return false;
1013 }
1014
1015 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
1016 os << std::flush;
1017
1018 return true;
1019 }
1020
Elliott Hughese3c845c2012-02-28 17:23:01 -08001021 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -07001022 while (it.HasNextStaticField()) {
1023 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001024 }
Ian Rogers0571d352011-11-03 19:51:38 -07001025 while (it.HasNextInstanceField()) {
1026 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001027 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001028 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001029
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001030 bool DumpOatClass(VariableIndentationOutputStream* vios,
1031 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001032 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001033 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001034 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -07001035 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001036 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001037 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001038 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -08001039 }
1040 ClassDataItemIterator it(dex_file, class_data);
1041 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001042 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001043 while (it.HasNextDirectMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001044 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001045 it.GetMemberIndex(), it.GetMethodCodeItem(),
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001046 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001047 success = false;
1048 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001049 if (addr_found) {
1050 *stop_analysis = true;
1051 return success;
1052 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001053 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -07001054 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001055 }
Ian Rogers0571d352011-11-03 19:51:38 -07001056 while (it.HasNextVirtualMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001057 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001058 it.GetMemberIndex(), it.GetMethodCodeItem(),
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001059 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001060 success = false;
1061 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001062 if (addr_found) {
1063 *stop_analysis = true;
1064 return success;
1065 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001066 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -07001067 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001068 }
Ian Rogers0571d352011-11-03 19:51:38 -07001069 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001070 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001071 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001072 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001073
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001074 static constexpr uint32_t kPrologueBytes = 16;
1075
1076 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
1077 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
1078
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001079 bool DumpOatMethod(VariableIndentationOutputStream* vios,
1080 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -07001081 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001082 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001083 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001084 uint32_t method_access_flags, bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001085 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001086
1087 // TODO: Support regex
1088 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
1089 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00001090 return success;
1091 }
1092
David Sehr709b0702016-10-13 09:12:37 -07001093 std::string pretty_method = dex_file.PrettyMethod(dex_method_idx, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001094 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
1095 class_method_index, pretty_method.c_str(),
1096 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001097 if (options_.list_methods_) return success;
1098
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001099 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
1100 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
1101 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
1102 uint32_t code_offset = oat_method.GetCodeOffset();
1103 uint32_t code_size = oat_method.GetQuickCodeSize();
1104 if (resolved_addr2instr_ != 0) {
1105 if (resolved_addr2instr_ > code_offset + code_size) {
1106 return success;
1107 } else {
1108 *addr_found = true; // stop analyzing file at next iteration
1109 }
1110 }
1111
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001112 // Everything below is indented at least once.
1113 ScopedIndentation indent1(vios);
1114
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001115 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001116 vios->Stream() << "DEX CODE:\n";
1117 ScopedIndentation indent2(vios);
1118 DumpDexCode(vios->Stream(), dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -07001119 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001120
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001121 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001122 std::unique_ptr<verifier::MethodVerifier> verifier;
1123 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001124 // We need to have the handle scope stay live until after the verifier since the verifier has
1125 // a handle to the dex cache from hs.
1126 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001127 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
1128 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001129 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001130 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001131 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -07001132 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001133 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001134 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001135 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001136 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001137 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001138 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001139 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001140 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001141 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
1142 oat_method_offsets_offset, oat_file_.Size());
1143 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001144 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001145 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001146 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001147
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001148 ScopedIndentation indent2(vios);
1149 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001150 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
1151 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001152 vios->Stream() << StringPrintf("WARNING: "
1153 "code offset 0x%08x is past end of file 0x%08zx.\n",
1154 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001155 success = false;
1156 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001157 vios->Stream() << "\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001158 }
1159 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001160 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001161 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
1162 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001163 stats_.AddBitsIfUnique(Stats::kByteKindQuickMethodHeader,
1164 sizeof(*method_header) * kBitsPerByte,
1165 method_header);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001166 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001167 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001168 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001169 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001170 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001171 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001172 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
1173 method_header_offset, oat_file_.Size());
1174 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001175 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001176 return false;
1177 }
1178
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001179 ScopedIndentation indent2(vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001180 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001181 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001182 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001183 }
Mingyao Yang063fc772016-08-02 11:02:54 -07001184 uint32_t vmap_table_offset = method_header ==
1185 nullptr ? 0 : method_header->GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001186 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001187
1188 size_t vmap_table_offset_limit =
1189 (kIsVdexEnabled && IsMethodGeneratedByDexToDexCompiler(oat_method, code_item))
1190 ? oat_file_.GetVdexFile()->Size()
1191 : method_header->GetCode() - oat_file_.Begin();
1192 if (vmap_table_offset >= vmap_table_offset_limit) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001193 vios->Stream() << StringPrintf("WARNING: "
1194 "vmap table offset 0x%08x is past end of file 0x%08zx. "
1195 "vmap table offset was loaded from offset 0x%08x.\n",
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001196 vmap_table_offset,
1197 vmap_table_offset_limit,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001198 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001199 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001200 } else if (options_.dump_vmap_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001201 DumpVmapData(vios, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001202 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001203 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001204 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001205 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001206
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001207 ScopedIndentation indent2(vios);
1208 vios->Stream()
1209 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
1210 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
1211 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
1212 vios->Stream() << "\n";
1213 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
1214 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
1215 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001216 }
1217 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001218 // Based on spill masks from QuickMethodFrameInfo so placed
1219 // after it is dumped, but useful for understanding quick
1220 // code, so dumped here.
1221 ScopedIndentation indent2(vios);
1222 DumpVregLocations(vios->Stream(), oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001223 }
1224 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001225 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001226 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
1227 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001228 ScopedIndentation indent2(vios);
1229 vios->Stream() << StringPrintf("WARNING: "
1230 "code size offset 0x%08x is past end of file 0x%08zx.",
1231 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001232 success = false;
1233 } else {
1234 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001235 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1236 uint64_t aligned_code_end = aligned_code_begin + code_size;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001237 stats_.AddBitsIfUnique(Stats::kByteKindCode, code_size * kBitsPerByte, code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001238
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001239 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001240 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001241 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001242 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
1243 code_offset,
1244 code_size_offset,
1245 code_size,
1246 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001247
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001248 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001249 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001250 vios->Stream() << StringPrintf("WARNING: "
1251 "start of code at 0x%08x is past end of file 0x%08zx.",
1252 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001253 success = false;
1254 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001255 vios->Stream() << StringPrintf(
1256 "WARNING: "
1257 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
1258 "code size is 0x%08x loaded from offset 0x%08x.\n",
1259 aligned_code_end, oat_file_.Size(),
1260 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001261 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001262 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001263 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001264 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001265 }
1266 }
1267 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001268 vios->Stream() << StringPrintf(
1269 "WARNING: "
1270 "code size %d is bigger than max expected threshold of %d. "
1271 "code size is 0x%08x loaded from offset 0x%08x.\n",
1272 code_size, kMaxCodeSize,
1273 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001274 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001275 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001276 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001277 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001278 }
1279 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001280 } else if (options_.disassemble_code_) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001281 DumpCode(vios, oat_method, code_item, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001282 }
1283 }
1284 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001285 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001286 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001287 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001288
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001289 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1290 if (spill_mask == 0) {
1291 return;
1292 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001293 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001294 for (size_t i = 0; i < 32; i++) {
1295 if ((spill_mask & (1 << i)) != 0) {
1296 if (is_float) {
1297 os << "fr" << i;
1298 } else {
1299 os << "r" << i;
1300 }
1301 spill_mask ^= 1 << i; // clear bit
1302 if (spill_mask != 0) {
1303 os << ", ";
1304 } else {
1305 break;
1306 }
1307 }
1308 }
1309 os << ")";
1310 }
1311
Roland Levillain442b46a2015-02-18 16:54:21 +00001312 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001313 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001314 const OatFile::OatMethod& oat_method,
1315 const DexFile::CodeItem* code_item) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001316 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1317 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001318 const void* raw_code_info = oat_method.GetVmapTable();
1319 if (raw_code_info != nullptr) {
1320 CodeInfo code_info(raw_code_info);
1321 DCHECK(code_item != nullptr);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001322 ScopedIndentation indent1(vios);
1323 DumpCodeInfo(vios, code_info, oat_method, *code_item);
Roland Levillain442b46a2015-02-18 16:54:21 +00001324 }
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001325 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
1326 // We don't encode the size in the table, so just emit that we have quickened
1327 // information.
1328 ScopedIndentation indent(vios);
1329 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001330 } else {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001331 // Otherwise, there is nothing to display.
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001332 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001333 }
1334
1335 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001336 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001337 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001338 const OatFile::OatMethod& oat_method,
Roland Levillain442b46a2015-02-18 16:54:21 +00001339 const DexFile::CodeItem& code_item) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001340 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001341 oat_method.GetCodeOffset(),
1342 code_item.registers_size_,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001343 options_.dump_code_info_stack_maps_,
1344 instruction_set_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001345 }
1346
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001347 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1348 const DexFile::CodeItem* code_item) {
1349 if (code_item != nullptr) {
1350 size_t num_locals_ins = code_item->registers_size_;
1351 size_t num_ins = code_item->ins_size_;
1352 size_t num_locals = num_locals_ins - num_ins;
1353 size_t num_outs = code_item->outs_size_;
1354
1355 os << "vr_stack_locations:";
1356 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1357 // For readability, delimit the different kinds of VRs.
1358 if (reg == num_locals_ins) {
1359 os << "\n\tmethod*:";
1360 } else if (reg == num_locals && num_ins > 0) {
1361 os << "\n\tins:";
1362 } else if (reg == 0 && num_locals > 0) {
1363 os << "\n\tlocals:";
1364 }
1365
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001366 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1367 code_item,
1368 oat_method.GetCoreSpillMask(),
1369 oat_method.GetFpSpillMask(),
1370 oat_method.GetFrameSizeInBytes(),
1371 reg,
1372 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001373 os << " v" << reg << "[sp + #" << offset << "]";
1374 }
1375
1376 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1377 if (out_reg == 0) {
1378 os << "\n\touts:";
1379 }
1380
1381 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1382 os << " v" << out_reg << "[sp + #" << offset << "]";
1383 }
1384
1385 os << "\n";
1386 }
1387 }
1388
Ian Rogersb23a7722012-10-09 16:54:26 -07001389 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001390 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001391 size_t i = 0;
1392 while (i < code_item->insns_size_in_code_units_) {
1393 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001394 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1395 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001396 i += instruction->SizeInCodeUnits();
1397 }
1398 }
1399 }
1400
Roland Levillainf2650d12015-05-28 14:53:28 +01001401 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1402 // the optimizing compiler?
1403 static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1404 const DexFile::CodeItem* code_item) {
1405 // If the native GC map is null and the Dex `code_item` is not
1406 // null, then this method has been compiled with the optimizing
1407 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001408 return oat_method.GetQuickCode() != nullptr &&
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001409 oat_method.GetVmapTable() != nullptr &&
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001410 code_item != nullptr;
1411 }
1412
1413 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1414 // the dextodex compiler?
1415 static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
1416 const DexFile::CodeItem* code_item) {
1417 // If the quick code is null, the Dex `code_item` is not
1418 // null, and the vmap table is not null, then this method has been compiled
1419 // with the dextodex compiler.
1420 return oat_method.GetQuickCode() == nullptr &&
1421 oat_method.GetVmapTable() != nullptr &&
1422 code_item != nullptr;
Roland Levillainf2650d12015-05-28 14:53:28 +01001423 }
1424
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001425 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001426 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001427 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001428 const DexFile* dex_file,
1429 const DexFile::ClassDef& class_def,
1430 const DexFile::CodeItem* code_item,
1431 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001432 if ((method_access_flags & kAccNative) == 0) {
1433 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001434 Runtime* const runtime = Runtime::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001435 Handle<mirror::DexCache> dex_cache(
Mathieu Chartierf284d442016-06-02 11:48:30 -07001436 hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file, nullptr)));
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001437 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001438 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001439 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
David Brazdil15fc7292016-09-02 14:13:18 +01001440 class_def, code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001441 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001442
1443 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001444 }
1445
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001446 // The StackMapsHelper provides the stack maps in the native PC order.
1447 // For identical native PCs, the order from the CodeInfo is preserved.
1448 class StackMapsHelper {
1449 public:
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001450 explicit StackMapsHelper(const uint8_t* raw_code_info, InstructionSet instruction_set)
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001451 : code_info_(raw_code_info),
1452 encoding_(code_info_.ExtractEncoding()),
1453 number_of_stack_maps_(code_info_.GetNumberOfStackMaps(encoding_)),
1454 indexes_(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001455 offset_(static_cast<uint32_t>(-1)),
1456 stack_map_index_(0u),
1457 instruction_set_(instruction_set) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001458 if (number_of_stack_maps_ != 0u) {
1459 // Check if native PCs are ordered.
1460 bool ordered = true;
1461 StackMap last = code_info_.GetStackMapAt(0u, encoding_);
1462 for (size_t i = 1; i != number_of_stack_maps_; ++i) {
1463 StackMap current = code_info_.GetStackMapAt(i, encoding_);
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001464 if (last.GetNativePcOffset(encoding_.stack_map_encoding, instruction_set) >
1465 current.GetNativePcOffset(encoding_.stack_map_encoding, instruction_set)) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001466 ordered = false;
1467 break;
1468 }
1469 last = current;
1470 }
1471 if (!ordered) {
1472 // Create indirection indexes for access in native PC order. We do not optimize
1473 // for the fact that there can currently be only two separately ordered ranges,
1474 // namely normal stack maps and catch-point stack maps.
1475 indexes_.resize(number_of_stack_maps_);
1476 std::iota(indexes_.begin(), indexes_.end(), 0u);
1477 std::sort(indexes_.begin(),
1478 indexes_.end(),
1479 [this](size_t lhs, size_t rhs) {
1480 StackMap left = code_info_.GetStackMapAt(lhs, encoding_);
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001481 uint32_t left_pc = left.GetNativePcOffset(encoding_.stack_map_encoding,
1482 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001483 StackMap right = code_info_.GetStackMapAt(rhs, encoding_);
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001484 uint32_t right_pc = right.GetNativePcOffset(encoding_.stack_map_encoding,
1485 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001486 // If the PCs are the same, compare indexes to preserve the original order.
1487 return (left_pc < right_pc) || (left_pc == right_pc && lhs < rhs);
1488 });
1489 }
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001490 offset_ = GetStackMapAt(0).GetNativePcOffset(encoding_.stack_map_encoding,
1491 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001492 }
1493 }
1494
1495 const CodeInfo& GetCodeInfo() const {
1496 return code_info_;
1497 }
1498
1499 const CodeInfoEncoding& GetEncoding() const {
1500 return encoding_;
1501 }
1502
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001503 uint32_t GetOffset() const {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001504 return offset_;
1505 }
1506
1507 StackMap GetStackMap() const {
1508 return GetStackMapAt(stack_map_index_);
1509 }
1510
1511 void Next() {
1512 ++stack_map_index_;
1513 offset_ = (stack_map_index_ == number_of_stack_maps_)
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001514 ? static_cast<uint32_t>(-1)
1515 : GetStackMapAt(stack_map_index_).GetNativePcOffset(encoding_.stack_map_encoding,
1516 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001517 }
1518
1519 private:
1520 StackMap GetStackMapAt(size_t i) const {
1521 if (!indexes_.empty()) {
1522 i = indexes_[i];
1523 }
1524 DCHECK_LT(i, number_of_stack_maps_);
1525 return code_info_.GetStackMapAt(i, encoding_);
1526 }
1527
1528 const CodeInfo code_info_;
1529 const CodeInfoEncoding encoding_;
1530 const size_t number_of_stack_maps_;
1531 dchecked_vector<size_t> indexes_; // Used if stack map native PCs are not ordered.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001532 uint32_t offset_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001533 size_t stack_map_index_;
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001534 const InstructionSet instruction_set_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001535 };
1536
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001537 void DumpCode(VariableIndentationOutputStream* vios,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001538 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1539 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001540 const void* quick_code = oat_method.GetQuickCode();
1541
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001542 if (code_size == 0) {
1543 code_size = oat_method.GetQuickCodeSize();
1544 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001545 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001546 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001547 return;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001548 } else if (!bad_input && IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1549 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001550 StackMapsHelper helper(oat_method.GetVmapTable(), instruction_set_);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001551 {
1552 CodeInfoEncoding encoding(helper.GetEncoding());
1553 StackMapEncoding stack_map_encoding(encoding.stack_map_encoding);
1554 // helper.GetCodeInfo().GetStackMapAt(0, encoding).;
1555 const size_t num_stack_maps = encoding.number_of_stack_maps;
1556 std::vector<uint8_t> size_vector;
1557 encoding.Compress(&size_vector);
1558 if (stats_.AddBitsIfUnique(Stats::kByteKindCodeInfoEncoding,
1559 size_vector.size() * kBitsPerByte,
1560 oat_method.GetVmapTable())) {
1561 stats_.AddBits(
1562 Stats::kByteKindStackMapNativePc,
1563 stack_map_encoding.GetNativePcEncoding().BitSize() * num_stack_maps);
1564 stats_.AddBits(
1565 Stats::kByteKindStackMapDexPc,
1566 stack_map_encoding.GetDexPcEncoding().BitSize() * num_stack_maps);
1567 stats_.AddBits(
1568 Stats::kByteKindStackMapDexRegisterMap,
1569 stack_map_encoding.GetDexRegisterMapEncoding().BitSize() * num_stack_maps);
1570 stats_.AddBits(
1571 Stats::kByteKindStackMapInlineInfo,
1572 stack_map_encoding.GetInlineInfoEncoding().BitSize() * num_stack_maps);
1573 stats_.AddBits(
Mathieu Chartier1a20b682017-01-31 14:25:16 -08001574 Stats::kByteKindStackMapRegisterMaskIndex,
1575 stack_map_encoding.GetRegisterMaskIndexEncoding().BitSize() * num_stack_maps);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001576 stats_.AddBits(
David Srbecky45aa5982016-03-18 02:15:09 +00001577 Stats::kByteKindStackMapStackMaskIndex,
1578 stack_map_encoding.GetStackMaskIndexEncoding().BitSize() * num_stack_maps);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001579 stats_.AddBits(
David Srbecky45aa5982016-03-18 02:15:09 +00001580 Stats::kByteKindCodeInfoStackMasks,
1581 helper.GetCodeInfo().GetNumberOfStackMaskBits(encoding) *
1582 encoding.number_of_stack_masks);
Mathieu Chartier1a20b682017-01-31 14:25:16 -08001583 stats_.AddBits(
1584 Stats::kByteKindCodeInfoRegisterMasks,
1585 encoding.register_mask_size_in_bits * encoding.number_of_stack_masks);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001586 const size_t stack_map_bytes = helper.GetCodeInfo().GetStackMapsSize(encoding);
1587 const size_t location_catalog_bytes =
1588 helper.GetCodeInfo().GetDexRegisterLocationCatalogSize(encoding);
1589 stats_.AddBits(Stats::kByteKindCodeInfoLocationCatalog,
1590 kBitsPerByte * location_catalog_bytes);
1591 const size_t dex_register_bytes =
1592 helper.GetCodeInfo().GetDexRegisterMapsSize(encoding, code_item->registers_size_);
1593 stats_.AddBits(
1594 Stats::kByteKindCodeInfoDexRegisterMap,
1595 kBitsPerByte * dex_register_bytes);
1596 const size_t inline_info_bytes =
1597 encoding.non_header_size -
1598 stack_map_bytes -
1599 location_catalog_bytes -
1600 dex_register_bytes;
1601 stats_.AddBits(Stats::kByteKindCodeInfoInlineInfo,
1602 inline_info_bytes * kBitsPerByte);
1603 }
1604 }
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001605 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1606 size_t offset = 0;
1607 while (offset < code_size) {
1608 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
1609 if (offset == helper.GetOffset()) {
1610 ScopedIndentation indent1(vios);
1611 StackMap stack_map = helper.GetStackMap();
1612 DCHECK(stack_map.IsValid());
1613 stack_map.Dump(vios,
1614 helper.GetCodeInfo(),
1615 helper.GetEncoding(),
1616 oat_method.GetCodeOffset(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001617 code_item->registers_size_,
1618 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001619 do {
1620 helper.Next();
1621 // There may be multiple stack maps at a given PC. We display only the first one.
1622 } while (offset == helper.GetOffset());
1623 }
1624 DCHECK_LT(offset, helper.GetOffset());
1625 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001626 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001627 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1628 size_t offset = 0;
1629 while (offset < code_size) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001630 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001631 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001632 }
1633 }
1634
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001635 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001636 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001637 const OatDumperOptions& options_;
1638 uint32_t resolved_addr2instr_;
Andreas Gampe372f3a32016-08-19 10:49:06 -07001639 const InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001640 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001641 Disassembler* disassembler_;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001642 Stats stats_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001643};
1644
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001645class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001646 public:
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001647 ImageDumper(std::ostream* os,
1648 gc::space::ImageSpace& image_space,
1649 const ImageHeader& image_header,
1650 OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001651 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001652 vios_(os),
1653 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001654 image_space_(image_space),
1655 image_header_(image_header),
1656 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001657
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001658 bool Dump() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001659 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001660 std::ostream& indent_os = vios_.Stream();
1661
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001662 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001663
Jeff Haodcdc85b2015-12-04 14:06:18 -08001664 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1665
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001666 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001667
Mathieu Chartiere401d142015-04-22 13:56:20 -07001668 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1669
1670 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1671 auto section = static_cast<ImageHeader::ImageSections>(i);
1672 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1673 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001674
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001675 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001676
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001677 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001678
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001679 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1680
1681 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1682
1683 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001684
Alex Lighta59dd802014-07-02 16:28:08 -07001685 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1686
Igor Murashkin46774762014-10-22 11:37:02 -07001687 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1688
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001689 {
1690 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001691 static_assert(arraysize(image_roots_descriptions_) ==
1692 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001693 DCHECK_LE(image_header_.GetImageRoots()->GetLength(), ImageHeader::kImageRootsMax);
1694 for (int32_t i = 0, size = image_header_.GetImageRoots()->GetLength(); i != size; ++i) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001695 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1696 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001697 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001698 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001699 if (image_root_object != nullptr && image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001700 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001701 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001702 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001703 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1704 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001705 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001706 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1707 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001708 run++;
1709 } else {
1710 break;
1711 }
1712 }
1713 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001714 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001715 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001716 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001717 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001718 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001719 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001720 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001721 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001722 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001723 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001724 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001725 }
1726 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001727 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001728
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001729 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001730 os << "METHOD ROOTS\n";
1731 static_assert(arraysize(image_methods_descriptions_) ==
1732 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1733 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1734 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1735 const char* description = image_methods_descriptions_[i];
1736 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001737 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001738 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001739 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001740 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001741
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001742 Runtime* const runtime = Runtime::Current();
1743 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001744 std::string image_filename = image_space_.GetImageFilename();
1745 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001746 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001747 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001748 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001749 const OatFile* oat_file = image_space_.GetOatFile();
Alex Lighta59dd802014-07-02 16:28:08 -07001750 if (oat_file == nullptr) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001751 oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location);
1752 }
1753 if (oat_file == nullptr) {
1754 oat_file = OatFile::Open(oat_location,
1755 oat_location,
1756 nullptr,
1757 nullptr,
1758 false,
1759 /*low_4gb*/false,
1760 nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001761 &error_msg);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001762 }
1763 if (oat_file == nullptr) {
1764 os << "OAT FILE NOT FOUND: " << error_msg << "\n";
1765 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001766 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001767 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001768
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001769 stats_.oat_file_bytes = oat_file->Size();
1770
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001771 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001772
Mathieu Chartier02e25112013-08-14 16:14:24 -07001773 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001774 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001775 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1776 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001777 }
1778
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001779 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001780
Jeff Haodcdc85b2015-12-04 14:06:18 -08001781 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001782 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001783 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001784 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001785 {
1786 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1787 heap->FlushAllocStack();
1788 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001789 // Since FlushAllocStack() above resets the (active) allocation
1790 // stack. Need to revoke the thread-local allocation stacks that
1791 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001792 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001793 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001794 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001795 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001796 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001797 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001798 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001799 {
Andreas Gampecc1b5352016-12-01 16:58:38 -08001800 ReaderMutexLock mu(self, *Locks::dex_lock_);
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001801 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001802 ObjPtr<mirror::DexCache> dex_cache =
1803 ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001804 if (dex_cache != nullptr) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001805 dex_caches_.insert(dex_cache.Ptr());
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001806 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001807 }
1808 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001809 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001810 // Dump the normal objects before ArtMethods.
1811 image_space_.GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1812 indent_os << "\n";
1813 // TODO: Dump fields.
1814 // Dump methods after.
Jeff Haodcdc85b2015-12-04 14:06:18 -08001815 DumpArtMethodVisitor visitor(this);
Mathieu Chartiere42888f2016-04-14 10:49:19 -07001816 image_header_.VisitPackedArtMethods(&visitor,
1817 image_space_.Begin(),
1818 image_header_.GetPointerSize());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001819 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001820 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001821 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001822 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001823 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001824 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001825 size_t data_size = image_header_.GetDataSize(); // stored size in file.
1826 if (file == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001827 LOG(WARNING) << "Failed to find image in " << image_filename;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001828 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001829 stats_.file_bytes = file->GetLength();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001830 // If the image is compressed, adjust to decompressed size.
1831 size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader);
1832 if (image_header_.GetStorageMode() == ImageHeader::kStorageModeUncompressed) {
1833 DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image";
1834 }
1835 stats_.file_bytes += uncompressed_size - data_size;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001836 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001837 size_t header_bytes = sizeof(ImageHeader);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001838 const auto& object_section = image_header_.GetImageSection(ImageHeader::kSectionObjects);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001839 const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1840 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001841 const auto& dex_cache_arrays_section = image_header_.GetImageSection(
1842 ImageHeader::kSectionDexCacheArrays);
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001843 const auto& intern_section = image_header_.GetImageSection(
1844 ImageHeader::kSectionInternedStrings);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001845 const auto& class_table_section = image_header_.GetImageSection(
1846 ImageHeader::kSectionClassTable);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001847 const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1848
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001849 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001850
1851 // Objects are kObjectAlignment-aligned.
1852 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1853 if (object_section.Offset() > header_bytes) {
1854 stats_.alignment_bytes += object_section.Offset() - header_bytes;
1855 }
1856
1857 // Field section is 4-byte aligned.
1858 constexpr size_t kFieldSectionAlignment = 4U;
1859 uint32_t end_objects = object_section.Offset() + object_section.Size();
1860 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
1861 stats_.alignment_bytes += field_section.Offset() - end_objects;
1862
1863 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
1864 uint32_t end_fields = field_section.Offset() + field_section.Size();
1865 CHECK_ALIGNED(method_section.Offset(), 4);
1866 stats_.alignment_bytes += method_section.Offset() - end_fields;
1867
1868 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
1869 uint32_t end_methods = method_section.Offset() + method_section.Size();
1870 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
1871 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
1872
1873 // Intern table is 8-byte aligned.
1874 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
1875 CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
1876 stats_.alignment_bytes += intern_section.Offset() - end_caches;
1877
1878 // Add space between intern table and class table.
1879 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
1880 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
1881
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001882 // Add space between end of image data and bitmap. Expect the bitmap to be page-aligned.
1883 const size_t bitmap_offset = sizeof(ImageHeader) + data_size;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001884 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001885 stats_.alignment_bytes += RoundUp(bitmap_offset, kPageSize) - bitmap_offset;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001886
Mathieu Chartiere401d142015-04-22 13:56:20 -07001887 stats_.bitmap_bytes += bitmap_section.Size();
1888 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01001889 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01001890 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001891 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001892 stats_.class_table_bytes += class_table_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001893 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001894 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001895
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001896 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001897
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001898 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001899 }
1900
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001901 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001902 class DumpArtMethodVisitor : public ArtMethodVisitor {
1903 public:
1904 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
1905
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001906 virtual void Visit(ArtMethod* method) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001907 std::ostream& indent_os = image_dumper_->vios_.Stream();
David Sehr709b0702016-10-13 09:12:37 -07001908 indent_os << method << " " << " ArtMethod: " << ArtMethod::PrettyMethod(method) << "\n";
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001909 image_dumper_->DumpMethod(method, indent_os);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001910 indent_os << "\n";
1911 }
1912
1913 private:
1914 ImageDumper* const image_dumper_;
1915 };
1916
Mathieu Chartier3398c782016-09-30 10:27:43 -07001917 static void PrettyObjectValue(std::ostream& os,
1918 ObjPtr<mirror::Class> type,
1919 ObjPtr<mirror::Object> value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001920 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001921 CHECK(type != nullptr);
1922 if (value == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07001923 os << StringPrintf("null %s\n", type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001924 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001925 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001926 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001927 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001928 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001929 mirror::Class* klass = value->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07001930 os << StringPrintf("%p Class: %s\n", klass, mirror::Class::PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001931 } else {
David Sehr709b0702016-10-13 09:12:37 -07001932 os << StringPrintf("%p %s\n", value.Ptr(), type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001933 }
1934 }
1935
Mathieu Chartier3398c782016-09-30 10:27:43 -07001936 static void PrintField(std::ostream& os, ArtField* field, ObjPtr<mirror::Object> obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001937 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001938 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08001939 switch (field->GetTypeAsPrimitiveType()) {
1940 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08001941 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001942 break;
1943 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001944 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001945 break;
1946 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001947 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001948 break;
1949 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001950 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001951 break;
1952 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07001953 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001954 break;
1955 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07001956 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001957 break;
1958 case Primitive::kPrimBoolean:
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001959 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj) ? "true" : "false",
Fred Shih37f05ef2014-07-16 18:38:08 -07001960 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001961 break;
1962 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07001963 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001964 break;
1965 case Primitive::kPrimNot: {
1966 // Get the value, don't compute the type unless it is non-null as we don't want
1967 // to cause class loading.
Mathieu Chartier3398c782016-09-30 10:27:43 -07001968 ObjPtr<mirror::Object> value = field->GetObj(obj);
Ian Rogers08f1f502014-12-02 15:04:37 -08001969 if (value == nullptr) {
1970 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07001971 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08001972 // Grab the field type without causing resolution.
Mathieu Chartier3398c782016-09-30 10:27:43 -07001973 ObjPtr<mirror::Class> field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08001974 if (field_type != nullptr) {
1975 PrettyObjectValue(os, field_type, value);
1976 } else {
Mathieu Chartier3398c782016-09-30 10:27:43 -07001977 os << StringPrintf("%p %s\n",
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07001978 value.Ptr(),
Ian Rogers08f1f502014-12-02 15:04:37 -08001979 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
1980 }
Ian Rogers50239c72013-06-17 14:53:22 -07001981 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001982 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07001983 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001984 default:
1985 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
1986 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08001987 }
1988 }
1989
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001990 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001991 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001992 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001993 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001994 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001995 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001996 for (ArtField& field : klass->GetIFields()) {
1997 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001998 }
1999 }
2000
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002001 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002002 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002003 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002004
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002005 const void* GetQuickOatCodeBegin(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08002006 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002007 image_header_.GetPointerSize());
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002008 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002009 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002010 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002011 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002012 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002013 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002014 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002015 }
2016
Mathieu Chartiere401d142015-04-22 13:56:20 -07002017 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002018 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002019 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
2020 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002021 return 0;
2022 }
2023 return oat_code_begin[-1];
2024 }
2025
Mathieu Chartiere401d142015-04-22 13:56:20 -07002026 const void* GetQuickOatCodeEnd(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002027 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002028 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002029 if (oat_code_begin == nullptr) {
2030 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002031 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002032 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002033 }
2034
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002035 static void Callback(mirror::Object* obj, void* arg) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002036 DCHECK(obj != nullptr);
2037 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002038 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002039 if (!state->InDumpSpace(obj)) {
2040 return;
2041 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002042
2043 size_t object_bytes = obj->SizeOf();
2044 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
2045 state->stats_.object_bytes += object_bytes;
2046 state->stats_.alignment_bytes += alignment_bytes;
2047
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002048 std::ostream& os = state->vios_.Stream();
2049
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002050 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08002051 if (obj_class->IsArrayClass()) {
David Sehr709b0702016-10-13 09:12:37 -07002052 os << StringPrintf("%p: %s length:%d\n", obj, obj_class->PrettyDescriptor().c_str(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002053 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08002054 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002055 mirror::Class* klass = obj->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07002056 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj,
2057 mirror::Class::PrettyDescriptor(klass).c_str())
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002058 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08002059 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002060 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07002061 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002062 } else {
David Sehr709b0702016-10-13 09:12:37 -07002063 os << StringPrintf("%p: %s\n", obj, obj_class->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002064 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002065 ScopedIndentation indent1(&state->vios_);
2066 DumpFields(os, obj, obj_class);
Andreas Gampe542451c2016-07-26 09:02:02 -07002067 const PointerSize image_pointer_size = state->image_header_.GetPointerSize();
Ian Rogersd5b32602012-02-26 16:40:04 -08002068 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002069 auto* obj_array = obj->AsObjectArray<mirror::Object>();
2070 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002071 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002072 size_t run = 0;
2073 for (int32_t j = i + 1; j < length; j++) {
2074 if (value == obj_array->Get(j)) {
2075 run++;
2076 } else {
2077 break;
2078 }
2079 }
2080 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002081 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002082 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002083 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08002084 i = i + run;
2085 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002086 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002087 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002088 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08002089 }
2090 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07002091 mirror::Class* klass = obj->AsClass();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002092 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002093 os << "STATICS:\n";
2094 ScopedIndentation indent2(&state->vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002095 for (ArtField& field : klass->GetSFields()) {
2096 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08002097 }
2098 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002099 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01002100 auto it = state->dex_caches_.find(obj);
2101 if (it != state->dex_caches_.end()) {
2102 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002103 const auto& field_section = state->image_header_.GetImageSection(
2104 ImageHeader::kSectionArtFields);
2105 const auto& method_section = state->image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01002106 size_t num_methods = dex_cache->NumResolvedMethods();
2107 if (num_methods != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002108 os << "Methods (size=" << num_methods << "):\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002109 ScopedIndentation indent2(&state->vios_);
2110 auto* resolved_methods = dex_cache->GetResolvedMethods();
2111 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002112 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_methods,
2113 i,
2114 image_pointer_size);
Vladimir Marko05792b92015-08-03 11:56:49 +01002115 size_t run = 0;
2116 for (size_t j = i + 1;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002117 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_methods,
2118 j,
2119 image_pointer_size);
2120 ++j) {
2121 ++run;
2122 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002123 if (run == 0) {
2124 os << StringPrintf("%zd: ", i);
2125 } else {
2126 os << StringPrintf("%zd to %zd: ", i, i + run);
2127 i = i + run;
2128 }
2129 std::string msg;
2130 if (elem == nullptr) {
2131 msg = "null";
2132 } else if (method_section.Contains(
2133 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002134 msg = reinterpret_cast<ArtMethod*>(elem)->PrettyMethod();
Vladimir Marko05792b92015-08-03 11:56:49 +01002135 } else {
2136 msg = "<not in method section>";
2137 }
2138 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07002139 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002140 }
2141 size_t num_fields = dex_cache->NumResolvedFields();
2142 if (num_fields != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002143 os << "Fields (size=" << num_fields << "):\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002144 ScopedIndentation indent2(&state->vios_);
2145 auto* resolved_fields = dex_cache->GetResolvedFields();
2146 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002147 auto* elem = mirror::DexCache::GetElementPtrSize(
2148 resolved_fields, i, image_pointer_size);
Vladimir Marko05792b92015-08-03 11:56:49 +01002149 size_t run = 0;
2150 for (size_t j = i + 1;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002151 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_fields,
2152 j,
2153 image_pointer_size);
2154 ++j) {
2155 ++run;
2156 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002157 if (run == 0) {
2158 os << StringPrintf("%zd: ", i);
2159 } else {
2160 os << StringPrintf("%zd to %zd: ", i, i + run);
2161 i = i + run;
2162 }
2163 std::string msg;
2164 if (elem == nullptr) {
2165 msg = "null";
2166 } else if (field_section.Contains(
2167 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002168 msg = reinterpret_cast<ArtField*>(elem)->PrettyField();
Vladimir Marko05792b92015-08-03 11:56:49 +01002169 } else {
2170 msg = "<not in field section>";
2171 }
2172 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002173 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002174 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002175 size_t num_types = dex_cache->NumResolvedTypes();
2176 if (num_types != 0u) {
2177 os << "Types (size=" << num_types << "):\n";
2178 ScopedIndentation indent2(&state->vios_);
2179 auto* resolved_types = dex_cache->GetResolvedTypes();
2180 for (size_t i = 0; i < num_types; ++i) {
Vladimir Markod16363a2017-02-01 14:09:13 +00002181 auto* elem = resolved_types[i].Read();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002182 size_t run = 0;
Vladimir Markod16363a2017-02-01 14:09:13 +00002183 for (size_t j = i + 1; j != num_types && elem == resolved_types[j].Read(); ++j) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002184 ++run;
2185 }
2186 if (run == 0) {
2187 os << StringPrintf("%zd: ", i);
2188 } else {
2189 os << StringPrintf("%zd to %zd: ", i, i + run);
2190 i = i + run;
2191 }
2192 std::string msg;
2193 if (elem == nullptr) {
2194 msg = "null";
2195 } else {
David Sehr709b0702016-10-13 09:12:37 -07002196 msg = elem->PrettyClass();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002197 }
Vladimir Markod16363a2017-02-01 14:09:13 +00002198 os << StringPrintf("%p %s\n", elem, msg.c_str());
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002199 }
2200 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002201 }
2202 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002203 std::string temp;
2204 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002205 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002206
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002207 void DumpMethod(ArtMethod* method, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002208 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002209 DCHECK(method != nullptr);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002210 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
2211 const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
Andreas Gampe542451c2016-07-26 09:02:02 -07002212 const PointerSize pointer_size = image_header_.GetPointerSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002213 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2214 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002215 if (method->IsNative()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002216 bool first_occurrence;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002217 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2218 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002219 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002220 stats_.native_to_managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002221 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002222 if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
2223 image_header_.GetPointerSize())) {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002224 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002225 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002226 } else if (method->IsAbstract() || method->IsClassInitializer()) {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002227 // Don't print information for these.
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002228 } else if (method->IsRuntimeMethod()) {
2229 ImtConflictTable* table = method->GetImtConflictTable(image_header_.GetPointerSize());
2230 if (table != nullptr) {
2231 indent_os << "IMT conflict table " << table << " method: ";
2232 for (size_t i = 0, count = table->NumEntries(pointer_size); i < count; ++i) {
David Sehr709b0702016-10-13 09:12:37 -07002233 indent_os << ArtMethod::PrettyMethod(table->GetImplementationMethod(i, pointer_size))
2234 << " ";
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002235 }
2236 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002237 } else {
2238 const DexFile::CodeItem* code_item = method->GetCodeItem();
2239 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002240 stats_.dex_instruction_bytes += dex_instruction_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002241
2242 bool first_occurrence;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002243 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002244 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01002245 // Method compiled with the optimizing compiler have no vmap table.
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002246 vmap_table_bytes = ComputeOatSize(method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002247 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002248 stats_.vmap_table_bytes += vmap_table_bytes;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002249 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002250 }
2251
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002252 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2253 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002254 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002255 stats_.managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002256 if (method->IsConstructor()) {
2257 if (method->IsStatic()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002258 stats_.class_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002259 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002260 stats_.large_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002261 }
2262 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002263 stats_.large_method_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002264 }
2265 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002266 stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002267
Igor Murashkin7617abd2015-07-10 18:27:47 -07002268 uint32_t method_access_flags = method->GetAccessFlags();
2269
Mathieu Chartiere401d142015-04-22 13:56:20 -07002270 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002271 indent_os << StringPrintf("SIZE: Dex Instructions=%zd StackMaps=%zd AccessFlags=0x%x\n",
2272 dex_instruction_bytes,
2273 vmap_table_bytes,
Igor Murashkin7617abd2015-07-10 18:27:47 -07002274 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002275
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002276 size_t total_size = dex_instruction_bytes +
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002277 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002278
2279 double expansion =
2280 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002281 stats_.ComputeOutliers(total_size, expansion, method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002282 }
2283 }
2284
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002285 std::set<const void*> already_seen_;
2286 // Compute the size of the given data within the oat file and whether this is the first time
2287 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002288 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002289 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002290 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002291 already_seen_.insert(oat_data);
2292 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002293 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002294 }
2295 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002296 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002297
2298 public:
2299 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002300 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002301 size_t file_bytes;
2302
2303 size_t header_bytes;
2304 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002305 size_t art_field_bytes;
2306 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01002307 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002308 size_t interned_strings_bytes;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002309 size_t class_table_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07002310 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002311 size_t alignment_bytes;
2312
2313 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002314 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002315 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07002316 size_t class_initializer_code_bytes;
2317 size_t large_initializer_code_bytes;
2318 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002319
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002320 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002321
2322 size_t dex_instruction_bytes;
2323
Mathieu Chartiere401d142015-04-22 13:56:20 -07002324 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002325 std::vector<size_t> method_outlier_size;
2326 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002327 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002328
Roland Levillain3887c462015-08-12 18:15:42 +01002329 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002330 : oat_file_bytes(0),
2331 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002332 header_bytes(0),
2333 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002334 art_field_bytes(0),
2335 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01002336 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002337 interned_strings_bytes(0),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002338 class_table_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07002339 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002340 alignment_bytes(0),
2341 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002342 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002343 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07002344 class_initializer_code_bytes(0),
2345 large_initializer_code_bytes(0),
2346 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002347 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002348 dex_instruction_bytes(0) {}
2349
Elliott Hughesa0e18062012-04-13 15:59:59 -07002350 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002351 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002352 size_t bytes;
2353 size_t count;
2354 };
2355 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2356 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002357
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002358 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002359 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2360 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002361 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002362 it->second.count += 1;
2363 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002364 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002365 }
2366 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002367
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002368 double PercentOfOatBytes(size_t size) {
2369 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2370 }
2371
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002372 double PercentOfFileBytes(size_t size) {
2373 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2374 }
2375
2376 double PercentOfObjectBytes(size_t size) {
2377 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2378 }
2379
Mathieu Chartiere401d142015-04-22 13:56:20 -07002380 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002381 method_outlier_size.push_back(total_size);
2382 method_outlier_expansion.push_back(expansion);
2383 method_outlier.push_back(method);
2384 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002385
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002386 void DumpOutliers(std::ostream& os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002387 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002388 size_t sum_of_sizes = 0;
2389 size_t sum_of_sizes_squared = 0;
2390 size_t sum_of_expansion = 0;
2391 size_t sum_of_expansion_squared = 0;
2392 size_t n = method_outlier_size.size();
Mathieu Chartier1ebf8d32016-06-09 11:51:27 -07002393 if (n <= 1) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08002394 return;
2395 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002396 for (size_t i = 0; i < n; i++) {
2397 size_t cur_size = method_outlier_size[i];
2398 sum_of_sizes += cur_size;
2399 sum_of_sizes_squared += cur_size * cur_size;
2400 double cur_expansion = method_outlier_expansion[i];
2401 sum_of_expansion += cur_expansion;
2402 sum_of_expansion_squared += cur_expansion * cur_expansion;
2403 }
2404 size_t size_mean = sum_of_sizes / n;
2405 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2406 double expansion_mean = sum_of_expansion / n;
2407 double expansion_variance =
2408 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2409
2410 // Dump methods whose size is a certain number of standard deviations from the mean
2411 size_t dumped_values = 0;
2412 size_t skipped_values = 0;
2413 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2414 size_t cur_size_variance = i * i * size_variance;
2415 bool first = true;
2416 for (size_t j = 0; j < n; j++) {
2417 size_t cur_size = method_outlier_size[j];
2418 if (cur_size > size_mean) {
2419 size_t cur_var = cur_size - size_mean;
2420 cur_var = cur_var * cur_var;
2421 if (cur_var > cur_size_variance) {
2422 if (dumped_values > 20) {
2423 if (i == 1) {
2424 skipped_values++;
2425 } else {
2426 i = 2; // jump to counting for 1 standard deviation
2427 break;
2428 }
2429 } else {
2430 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002431 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002432 first = false;
2433 }
David Sehr709b0702016-10-13 09:12:37 -07002434 os << ArtMethod::PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002435 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002436 method_outlier_size[j] = 0; // don't consider this method again
2437 dumped_values++;
2438 }
2439 }
2440 }
2441 }
2442 }
2443 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002444 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002445 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002446 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002447 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002448
2449 // Dump methods whose expansion is a certain number of standard deviations from the mean
2450 dumped_values = 0;
2451 skipped_values = 0;
2452 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2453 double cur_expansion_variance = i * i * expansion_variance;
2454 bool first = true;
2455 for (size_t j = 0; j < n; j++) {
2456 double cur_expansion = method_outlier_expansion[j];
2457 if (cur_expansion > expansion_mean) {
2458 size_t cur_var = cur_expansion - expansion_mean;
2459 cur_var = cur_var * cur_var;
2460 if (cur_var > cur_expansion_variance) {
2461 if (dumped_values > 20) {
2462 if (i == 1) {
2463 skipped_values++;
2464 } else {
2465 i = 2; // jump to counting for 1 standard deviation
2466 break;
2467 }
2468 } else {
2469 if (first) {
2470 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002471 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002472 first = false;
2473 }
David Sehr709b0702016-10-13 09:12:37 -07002474 os << ArtMethod::PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002475 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002476 method_outlier_expansion[j] = 0.0; // don't consider this method again
2477 dumped_values++;
2478 }
2479 }
2480 }
2481 }
2482 }
2483 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002484 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002485 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002486 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002487 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002488 }
2489
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002490 void Dump(std::ostream& os, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002491 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002492 {
2493 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2494 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002495 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2496 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2497 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2498 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2499 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2500 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002501 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002502 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2503 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002504 header_bytes, PercentOfFileBytes(header_bytes),
2505 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002506 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2507 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002508 dex_cache_arrays_bytes,
2509 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002510 interned_strings_bytes,
2511 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002512 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002513 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002514 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2515 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002516 CHECK_EQ(file_bytes,
2517 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2518 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2519 bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002520 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002521
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002522 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002523 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002524 for (const auto& sizes_and_count : sizes_and_counts) {
2525 const std::string& descriptor(sizes_and_count.first);
2526 double average = static_cast<double>(sizes_and_count.second.bytes) /
2527 static_cast<double>(sizes_and_count.second.count);
2528 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002529 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002530 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002531 descriptor.c_str(), sizes_and_count.second.bytes,
2532 sizes_and_count.second.count, average, percent);
2533 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002534 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002535 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002536 CHECK_EQ(object_bytes, object_bytes_total);
2537
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002538 os << StringPrintf("oat_file_bytes = %8zd\n"
2539 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002540 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2541 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2542 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2543 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002544 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002545 managed_code_bytes,
2546 PercentOfOatBytes(managed_code_bytes),
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002547 native_to_managed_code_bytes,
2548 PercentOfOatBytes(native_to_managed_code_bytes),
2549 class_initializer_code_bytes,
2550 PercentOfOatBytes(class_initializer_code_bytes),
2551 large_initializer_code_bytes,
2552 PercentOfOatBytes(large_initializer_code_bytes),
2553 large_method_code_bytes,
2554 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002555 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002556 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002557 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002558 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2559 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002560 }
2561
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002562 os << "\n" << StringPrintf("vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002563 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002564 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002565
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002566 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2567 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002568 static_cast<double>(managed_code_bytes) /
2569 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002570 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002571 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002572 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002573
2574 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002575 }
2576 } stats_;
2577
2578 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002579 enum {
2580 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2581 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2582 kLargeConstructorDexBytes = 4000,
2583 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2584 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2585 kLargeMethodDexBytes = 16000
2586 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002587
2588 // For performance, use the *os_ directly for anything that doesn't need indentation
2589 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002590 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002591 VariableIndentationOutputStream vios_;
2592 ScopedIndentation indent1_;
2593
Ian Rogers1d54e732013-05-02 21:10:01 -07002594 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002595 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002596 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002597 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002598 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002599
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002600 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002601};
2602
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002603static int DumpImage(gc::space::ImageSpace* image_space,
2604 OatDumperOptions* options,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002605 std::ostream* os) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002606 const ImageHeader& image_header = image_space->GetImageHeader();
2607 if (!image_header.IsValid()) {
2608 fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
2609 return EXIT_FAILURE;
2610 }
2611 ImageDumper image_dumper(os, *image_space, image_header, options);
2612 if (!image_dumper.Dump()) {
2613 return EXIT_FAILURE;
2614 }
2615 return EXIT_SUCCESS;
2616}
2617
2618static int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002619 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002620 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002621 options->class_loader_ = &null_class_loader;
2622
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002623 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002624 if (options->app_image_ != nullptr) {
2625 if (options->app_oat_ == nullptr) {
2626 LOG(ERROR) << "Can not dump app image without app oat file";
Jeff Haodcdc85b2015-12-04 14:06:18 -08002627 return EXIT_FAILURE;
2628 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002629 // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
2630 // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
2631 // pointers into 32 bit pointer sized ArtMethods.
2632 std::string error_msg;
2633 std::unique_ptr<OatFile> oat_file(OatFile::Open(options->app_oat_,
2634 options->app_oat_,
2635 nullptr,
2636 nullptr,
2637 false,
2638 /*low_4gb*/true,
2639 nullptr,
2640 &error_msg));
2641 if (oat_file == nullptr) {
2642 LOG(ERROR) << "Failed to open oat file " << options->app_oat_ << " with error " << error_msg;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002643 return EXIT_FAILURE;
2644 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002645 std::unique_ptr<gc::space::ImageSpace> space(
2646 gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg));
2647 if (space == nullptr) {
2648 LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error "
2649 << error_msg;
2650 }
2651 // Open dex files for the image.
2652 std::vector<std::unique_ptr<const DexFile>> dex_files;
2653 if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
2654 LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
2655 << error_msg;
2656 }
2657 // Dump the actual image.
2658 int result = DumpImage(space.get(), options, os);
2659 if (result != EXIT_SUCCESS) {
2660 return result;
2661 }
2662 // Fall through to dump the boot images.
2663 }
2664
2665 gc::Heap* heap = runtime->GetHeap();
2666 CHECK(heap->HasBootImageSpace()) << "No image spaces";
2667 for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
2668 int result = DumpImage(image_space, options, os);
2669 if (result != EXIT_SUCCESS) {
2670 return result;
2671 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002672 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002673 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002674}
2675
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002676static jobject InstallOatFile(Runtime* runtime,
2677 std::unique_ptr<OatFile> oat_file,
2678 std::vector<const DexFile*>* class_path)
2679 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002680 Thread* self = Thread::Current();
2681 CHECK(self != nullptr);
2682 // Need well-known-classes.
2683 WellKnownClasses::Init(self->GetJniEnv());
2684
2685 // Need to register dex files to get a working dex cache.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002686 OatFile* oat_file_ptr = oat_file.get();
Andreas Gampe00b25f32014-09-17 21:49:05 -07002687 ClassLinker* class_linker = runtime->GetClassLinker();
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002688 runtime->GetOatFileManager().RegisterOatFile(std::move(oat_file));
2689 for (const OatFile::OatDexFile* odf : oat_file_ptr->GetOatDexFiles()) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002690 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002691 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002692 CHECK(dex_file != nullptr) << error_msg;
Mathieu Chartierf284d442016-06-02 11:48:30 -07002693 class_linker->RegisterDexFile(*dex_file, nullptr);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002694 class_path->push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002695 }
2696
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002697 // Need a class loader. Fake that we're a compiler.
2698 // Note: this will run initializers through the unstarted runtime, so make sure it's
2699 // initialized.
2700 interpreter::UnstartedRuntime::Initialize();
2701
2702 jobject class_loader = class_linker->CreatePathClassLoader(self, *class_path);
2703
2704 return class_loader;
2705}
2706
2707static int DumpOatWithRuntime(Runtime* runtime,
2708 std::unique_ptr<OatFile> oat_file,
2709 OatDumperOptions* options,
2710 std::ostream* os) {
2711 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2712 ScopedObjectAccess soa(Thread::Current());
2713
2714 OatFile* oat_file_ptr = oat_file.get();
2715 std::vector<const DexFile*> class_path;
2716 jobject class_loader = InstallOatFile(runtime, std::move(oat_file), &class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002717
2718 // Use the class loader while dumping.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002719 StackHandleScope<1> scope(soa.Self());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002720 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07002721 soa.Decode<mirror::ClassLoader>(class_loader));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002722 options->class_loader_ = &loader_handle;
2723
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002724 OatDumper oat_dumper(*oat_file_ptr, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002725 bool success = oat_dumper.Dump(*os);
2726 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2727}
2728
2729static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002730 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002731 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002732 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002733 options->class_loader_ = &null_class_loader;
2734
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002735 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002736 bool success = oat_dumper.Dump(*os);
2737 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2738}
2739
2740static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2741 std::ostream* os) {
2742 std::string error_msg;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002743 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
2744 oat_filename,
2745 nullptr,
2746 nullptr,
2747 false,
2748 /*low_4gb*/false,
2749 nullptr,
2750 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002751 if (oat_file == nullptr) {
2752 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2753 return EXIT_FAILURE;
2754 }
2755
2756 if (runtime != nullptr) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002757 return DumpOatWithRuntime(runtime, std::move(oat_file), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002758 } else {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002759 return DumpOatWithoutRuntime(oat_file.get(), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002760 }
2761}
2762
David Srbecky2fdd03c2016-03-10 15:32:37 +00002763static int SymbolizeOat(const char* oat_filename, std::string& output_name, bool no_bits) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002764 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002765 OatFile* oat_file = OatFile::Open(oat_filename,
2766 oat_filename,
2767 nullptr,
2768 nullptr,
2769 false,
2770 /*low_4gb*/false,
2771 nullptr,
2772 &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002773 if (oat_file == nullptr) {
2774 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2775 return EXIT_FAILURE;
2776 }
2777
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002778 bool result;
2779 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
2780 // files for 64-bit code in the past.
2781 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002782 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file, output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002783 result = oat_symbolizer.Symbolize();
2784 } else {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002785 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file, output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002786 result = oat_symbolizer.Symbolize();
2787 }
2788 if (!result) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002789 fprintf(stderr, "Failed to symbolize\n");
2790 return EXIT_FAILURE;
2791 }
2792
2793 return EXIT_SUCCESS;
2794}
2795
Andreas Gampe9fded872016-09-25 16:08:35 -07002796class IMTDumper {
2797 public:
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002798 static bool Dump(Runtime* runtime,
2799 const std::string& imt_file,
2800 bool dump_imt_stats,
2801 const char* oat_filename) {
2802 Thread* self = Thread::Current();
2803
2804 ScopedObjectAccess soa(self);
2805 StackHandleScope<1> scope(self);
2806 MutableHandle<mirror::ClassLoader> class_loader = scope.NewHandle<mirror::ClassLoader>(nullptr);
2807 std::vector<const DexFile*> class_path;
2808
2809 if (oat_filename != nullptr) {
2810 std::string error_msg;
2811 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
2812 oat_filename,
2813 nullptr,
2814 nullptr,
2815 false,
2816 /*low_4gb*/false,
2817 nullptr,
2818 &error_msg));
2819 if (oat_file == nullptr) {
2820 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2821 return false;
2822 }
2823
2824 class_loader.Assign(soa.Decode<mirror::ClassLoader>(
2825 InstallOatFile(runtime, std::move(oat_file), &class_path)));
2826 } else {
2827 class_loader.Assign(nullptr); // Boot classloader. Just here for explicit documentation.
2828 class_path = runtime->GetClassLinker()->GetBootClassPath();
2829 }
2830
2831 if (!imt_file.empty()) {
2832 return DumpImt(runtime, imt_file, class_loader);
2833 }
2834
2835 if (dump_imt_stats) {
2836 return DumpImtStats(runtime, class_path, class_loader);
2837 }
2838
2839 LOG(FATAL) << "Should not reach here";
2840 UNREACHABLE();
2841 }
2842
2843 private:
2844 static bool DumpImt(Runtime* runtime,
2845 const std::string& imt_file,
2846 Handle<mirror::ClassLoader> h_class_loader)
2847 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002848 std::vector<std::string> lines = ReadCommentedInputFromFile(imt_file);
2849 std::unordered_set<std::string> prepared;
2850
2851 for (const std::string& line : lines) {
2852 // A line should be either a class descriptor, in which case we will dump the complete IMT,
2853 // or a class descriptor and an interface method, in which case we will lookup the method,
2854 // determine its IMT slot, and check the class' IMT.
2855 size_t first_space = line.find(' ');
2856 if (first_space == std::string::npos) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002857 DumpIMTForClass(runtime, line, h_class_loader, &prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07002858 } else {
2859 DumpIMTForMethod(runtime,
2860 line.substr(0, first_space),
2861 line.substr(first_space + 1, std::string::npos),
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002862 h_class_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07002863 &prepared);
2864 }
2865 std::cerr << std::endl;
2866 }
2867
2868 return true;
2869 }
2870
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002871 static bool DumpImtStats(Runtime* runtime,
2872 const std::vector<const DexFile*>& dex_files,
2873 Handle<mirror::ClassLoader> h_class_loader)
2874 REQUIRES_SHARED(Locks::mutator_lock_) {
2875 size_t without_imt = 0;
2876 size_t with_imt = 0;
Andreas Gampe9fded872016-09-25 16:08:35 -07002877 std::map<size_t, size_t> histogram;
2878
2879 ClassLinker* class_linker = runtime->GetClassLinker();
2880 const PointerSize pointer_size = class_linker->GetImagePointerSize();
2881 std::unordered_set<std::string> prepared;
2882
2883 Thread* self = Thread::Current();
Andreas Gampe9fded872016-09-25 16:08:35 -07002884 StackHandleScope<1> scope(self);
2885 MutableHandle<mirror::Class> h_klass(scope.NewHandle<mirror::Class>(nullptr));
2886
2887 for (const DexFile* dex_file : dex_files) {
2888 for (uint32_t class_def_index = 0;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002889 class_def_index != dex_file->NumClassDefs();
2890 ++class_def_index) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002891 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
2892 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002893 h_klass.Assign(class_linker->FindClass(self, descriptor, h_class_loader));
Andreas Gampe9fded872016-09-25 16:08:35 -07002894 if (h_klass.Get() == nullptr) {
2895 std::cerr << "Warning: could not load " << descriptor << std::endl;
2896 continue;
2897 }
2898
2899 if (HasNoIMT(runtime, h_klass, pointer_size, &prepared)) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002900 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07002901 continue;
2902 }
2903
2904 ImTable* im_table = PrepareAndGetImTable(runtime, h_klass, pointer_size, &prepared);
2905 if (im_table == nullptr) {
2906 // Should not happen, but accept.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002907 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07002908 continue;
2909 }
2910
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002911 with_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07002912 for (size_t imt_index = 0; imt_index != ImTable::kSize; ++imt_index) {
2913 ArtMethod* ptr = im_table->Get(imt_index, pointer_size);
2914 if (ptr->IsRuntimeMethod()) {
2915 if (ptr->IsImtUnimplementedMethod()) {
2916 histogram[0]++;
2917 } else {
2918 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
2919 histogram[current_table->NumEntries(pointer_size)]++;
2920 }
2921 } else {
2922 histogram[1]++;
2923 }
2924 }
2925 }
2926 }
2927
2928 std::cerr << "IMT stats:"
2929 << std::endl << std::endl;
2930
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002931 std::cerr << " " << with_imt << " classes with IMT."
Andreas Gampe9fded872016-09-25 16:08:35 -07002932 << std::endl << std::endl;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002933 std::cerr << " " << without_imt << " classes without IMT (or copy from Object)."
Andreas Gampe9fded872016-09-25 16:08:35 -07002934 << std::endl << std::endl;
2935
2936 double sum_one = 0;
2937 size_t count_one = 0;
2938
2939 std::cerr << " " << "IMT histogram" << std::endl;
2940 for (auto& bucket : histogram) {
2941 std::cerr << " " << bucket.first << " " << bucket.second << std::endl;
2942 if (bucket.first > 0) {
2943 sum_one += bucket.second * bucket.first;
2944 count_one += bucket.second;
2945 }
2946 }
2947
2948 double count_zero = count_one + histogram[0];
2949 std::cerr << " Stats:" << std::endl;
2950 std::cerr << " Average depth (including empty): " << (sum_one / count_zero) << std::endl;
2951 std::cerr << " Average depth (excluding empty): " << (sum_one / count_one) << std::endl;
2952
2953 return true;
2954 }
2955
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002956 // Return whether the given class has no IMT (or the one shared with java.lang.Object).
Andreas Gampe9fded872016-09-25 16:08:35 -07002957 static bool HasNoIMT(Runtime* runtime,
2958 Handle<mirror::Class> klass,
2959 const PointerSize pointer_size,
2960 std::unordered_set<std::string>* prepared)
2961 REQUIRES_SHARED(Locks::mutator_lock_) {
2962 if (klass->IsObjectClass() || !klass->ShouldHaveImt()) {
2963 return true;
2964 }
2965
2966 if (klass->GetImt(pointer_size) == nullptr) {
2967 PrepareClass(runtime, klass, prepared);
2968 }
2969
2970 mirror::Class* object_class = mirror::Class::GetJavaLangClass()->GetSuperClass();
2971 DCHECK(object_class->IsObjectClass());
2972
2973 bool result = klass->GetImt(pointer_size) == object_class->GetImt(pointer_size);
2974
Mathieu Chartier6beced42016-11-15 15:51:31 -08002975 if (klass->GetIfTable()->Count() == 0) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002976 DCHECK(result);
2977 }
2978
2979 return result;
2980 }
2981
2982 static void PrintTable(ImtConflictTable* table, PointerSize pointer_size)
2983 REQUIRES_SHARED(Locks::mutator_lock_) {
2984 if (table == nullptr) {
2985 std::cerr << " <No IMT?>" << std::endl;
2986 return;
2987 }
2988 size_t table_index = 0;
2989 for (;;) {
2990 ArtMethod* ptr = table->GetInterfaceMethod(table_index, pointer_size);
2991 if (ptr == nullptr) {
2992 return;
2993 }
2994 table_index++;
David Sehr709b0702016-10-13 09:12:37 -07002995 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07002996 }
2997 }
2998
2999 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3000 Thread* self,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003001 Handle<mirror::ClassLoader> h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003002 const std::string& class_name,
3003 const PointerSize pointer_size,
3004 mirror::Class** klass_out,
3005 std::unordered_set<std::string>* prepared)
3006 REQUIRES_SHARED(Locks::mutator_lock_) {
3007 if (class_name.empty()) {
3008 return nullptr;
3009 }
3010
3011 std::string descriptor;
3012 if (class_name[0] == 'L') {
3013 descriptor = class_name;
3014 } else {
3015 descriptor = DotToDescriptor(class_name.c_str());
3016 }
3017
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003018 mirror::Class* klass = runtime->GetClassLinker()->FindClass(self, descriptor.c_str(), h_loader);
Andreas Gampe9fded872016-09-25 16:08:35 -07003019
3020 if (klass == nullptr) {
3021 self->ClearException();
3022 std::cerr << "Did not find " << class_name << std::endl;
3023 *klass_out = nullptr;
3024 return nullptr;
3025 }
3026
3027 StackHandleScope<1> scope(Thread::Current());
3028 Handle<mirror::Class> h_klass = scope.NewHandle<mirror::Class>(klass);
3029
3030 ImTable* ret = PrepareAndGetImTable(runtime, h_klass, pointer_size, prepared);
3031 *klass_out = h_klass.Get();
3032 return ret;
3033 }
3034
3035 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3036 Handle<mirror::Class> h_klass,
3037 const PointerSize pointer_size,
3038 std::unordered_set<std::string>* prepared)
3039 REQUIRES_SHARED(Locks::mutator_lock_) {
3040 PrepareClass(runtime, h_klass, prepared);
3041 return h_klass->GetImt(pointer_size);
3042 }
3043
3044 static void DumpIMTForClass(Runtime* runtime,
3045 const std::string& class_name,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003046 Handle<mirror::ClassLoader> h_loader,
3047 std::unordered_set<std::string>* prepared)
3048 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003049 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
3050 mirror::Class* klass;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003051 ImTable* imt = PrepareAndGetImTable(runtime,
3052 Thread::Current(),
3053 h_loader,
3054 class_name,
3055 pointer_size,
3056 &klass,
3057 prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07003058 if (imt == nullptr) {
3059 return;
3060 }
3061
3062 std::cerr << class_name << std::endl << " IMT:" << std::endl;
3063 for (size_t index = 0; index < ImTable::kSize; ++index) {
3064 std::cerr << " " << index << ":" << std::endl;
3065 ArtMethod* ptr = imt->Get(index, pointer_size);
3066 if (ptr->IsRuntimeMethod()) {
3067 if (ptr->IsImtUnimplementedMethod()) {
3068 std::cerr << " <empty>" << std::endl;
3069 } else {
3070 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3071 PrintTable(current_table, pointer_size);
3072 }
3073 } else {
David Sehr709b0702016-10-13 09:12:37 -07003074 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003075 }
3076 }
3077
3078 std::cerr << " Interfaces:" << std::endl;
3079 // Run through iftable, find methods that slot here, see if they fit.
3080 mirror::IfTable* if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003081 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
3082 mirror::Class* iface = if_table->GetInterface(i);
3083 std::string iface_name;
3084 std::cerr << " " << iface->GetDescriptor(&iface_name) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003085
Mathieu Chartier6beced42016-11-15 15:51:31 -08003086 for (ArtMethod& iface_method : iface->GetVirtualMethods(pointer_size)) {
3087 uint32_t class_hash, name_hash, signature_hash;
3088 ImTable::GetImtHashComponents(&iface_method, &class_hash, &name_hash, &signature_hash);
3089 uint32_t imt_slot = ImTable::GetImtIndex(&iface_method);
3090 std::cerr << " " << iface_method.PrettyMethod(true)
3091 << " slot=" << imt_slot
3092 << std::hex
3093 << " class_hash=0x" << class_hash
3094 << " name_hash=0x" << name_hash
3095 << " signature_hash=0x" << signature_hash
3096 << std::dec
3097 << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003098 }
3099 }
3100 }
3101
3102 static void DumpIMTForMethod(Runtime* runtime,
3103 const std::string& class_name,
3104 const std::string& method,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003105 Handle<mirror::ClassLoader> h_loader,
3106 std::unordered_set<std::string>* prepared)
3107 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003108 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
3109 mirror::Class* klass;
3110 ImTable* imt = PrepareAndGetImTable(runtime,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003111 Thread::Current(),
3112 h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003113 class_name,
3114 pointer_size,
3115 &klass,
3116 prepared);
3117 if (imt == nullptr) {
3118 return;
3119 }
3120
3121 std::cerr << class_name << " <" << method << ">" << std::endl;
3122 for (size_t index = 0; index < ImTable::kSize; ++index) {
3123 ArtMethod* ptr = imt->Get(index, pointer_size);
3124 if (ptr->IsRuntimeMethod()) {
3125 if (ptr->IsImtUnimplementedMethod()) {
3126 continue;
3127 }
3128
3129 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3130 if (current_table == nullptr) {
3131 continue;
3132 }
3133
3134 size_t table_index = 0;
3135 for (;;) {
3136 ArtMethod* ptr2 = current_table->GetInterfaceMethod(table_index, pointer_size);
3137 if (ptr2 == nullptr) {
3138 break;
3139 }
3140 table_index++;
3141
David Sehr709b0702016-10-13 09:12:37 -07003142 std::string p_name = ptr2->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003143 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003144 std::cerr << " Slot "
3145 << index
3146 << " ("
3147 << current_table->NumEntries(pointer_size)
3148 << ")"
3149 << std::endl;
3150 PrintTable(current_table, pointer_size);
3151 return;
3152 }
3153 }
3154 } else {
David Sehr709b0702016-10-13 09:12:37 -07003155 std::string p_name = ptr->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003156 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003157 std::cerr << " Slot " << index << " (1)" << std::endl;
3158 std::cerr << " " << p_name << std::endl;
3159 } else {
3160 // Run through iftable, find methods that slot here, see if they fit.
3161 mirror::IfTable* if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003162 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
3163 mirror::Class* iface = if_table->GetInterface(i);
3164 size_t num_methods = iface->NumDeclaredVirtualMethods();
3165 if (num_methods > 0) {
3166 for (ArtMethod& iface_method : iface->GetMethods(pointer_size)) {
3167 if (ImTable::GetImtIndex(&iface_method) == index) {
3168 std::string i_name = iface_method.PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003169 if (android::base::StartsWith(i_name, method.c_str())) {
Mathieu Chartier6beced42016-11-15 15:51:31 -08003170 std::cerr << " Slot " << index << " (1)" << std::endl;
3171 std::cerr << " " << p_name << " (" << i_name << ")" << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003172 }
3173 }
3174 }
3175 }
3176 }
3177 }
3178 }
3179 }
3180 }
3181
3182 // Read lines from the given stream, dropping comments and empty lines
3183 static std::vector<std::string> ReadCommentedInputStream(std::istream& in_stream) {
3184 std::vector<std::string> output;
3185 while (in_stream.good()) {
3186 std::string dot;
3187 std::getline(in_stream, dot);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003188 if (android::base::StartsWith(dot, "#") || dot.empty()) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003189 continue;
3190 }
3191 output.push_back(dot);
3192 }
3193 return output;
3194 }
3195
3196 // Read lines from the given file, dropping comments and empty lines.
3197 static std::vector<std::string> ReadCommentedInputFromFile(const std::string& input_filename) {
3198 std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
3199 if (input_file.get() == nullptr) {
3200 LOG(ERROR) << "Failed to open input file " << input_filename;
3201 return std::vector<std::string>();
3202 }
3203 std::vector<std::string> result = ReadCommentedInputStream(*input_file);
3204 input_file->close();
3205 return result;
3206 }
3207
3208 // Prepare a class, i.e., ensure it has a filled IMT. Will do so recursively for superclasses,
3209 // and note in the given set that the work was done.
3210 static void PrepareClass(Runtime* runtime,
3211 Handle<mirror::Class> h_klass,
3212 std::unordered_set<std::string>* done)
3213 REQUIRES_SHARED(Locks::mutator_lock_) {
3214 if (!h_klass->ShouldHaveImt()) {
3215 return;
3216 }
3217
3218 std::string name;
3219 name = h_klass->GetDescriptor(&name);
3220
3221 if (done->find(name) != done->end()) {
3222 return;
3223 }
3224 done->insert(name);
3225
3226 if (h_klass->HasSuperClass()) {
3227 StackHandleScope<1> h(Thread::Current());
3228 PrepareClass(runtime, h.NewHandle<mirror::Class>(h_klass->GetSuperClass()), done);
3229 }
3230
3231 if (!h_klass->IsTemp()) {
3232 runtime->GetClassLinker()->FillIMTAndConflictTables(h_klass.Get());
3233 }
3234 }
3235};
3236
Igor Murashkin37743352014-11-13 14:38:00 -08003237struct OatdumpArgs : public CmdlineArgs {
3238 protected:
3239 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003240
Igor Murashkin37743352014-11-13 14:38:00 -08003241 virtual ParseStatus ParseCustom(const StringPiece& option,
3242 std::string* error_msg) OVERRIDE {
3243 {
3244 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
3245 if (base_parse != kParseUnknownArgument) {
3246 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003247 }
3248 }
3249
Igor Murashkin37743352014-11-13 14:38:00 -08003250 if (option.starts_with("--oat-file=")) {
3251 oat_filename_ = option.substr(strlen("--oat-file=")).data();
3252 } else if (option.starts_with("--image=")) {
3253 image_location_ = option.substr(strlen("--image=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003254 } else if (option == "--no-dump:vmap") {
3255 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01003256 } else if (option =="--dump:code_info_stack_maps") {
3257 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003258 } else if (option == "--no-disassemble") {
3259 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003260 } else if (option =="--header-only") {
3261 dump_header_only_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003262 } else if (option.starts_with("--symbolize=")) {
3263 oat_filename_ = option.substr(strlen("--symbolize=")).data();
3264 symbolize_ = true;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003265 } else if (option.starts_with("--only-keep-debug")) {
3266 only_keep_debug_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003267 } else if (option.starts_with("--class-filter=")) {
3268 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003269 } else if (option.starts_with("--method-filter=")) {
3270 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003271 } else if (option.starts_with("--list-classes")) {
3272 list_classes_ = true;
3273 } else if (option.starts_with("--list-methods")) {
3274 list_methods_ = true;
3275 } else if (option.starts_with("--export-dex-to=")) {
3276 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
3277 } else if (option.starts_with("--addr2instr=")) {
3278 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
3279 *error_msg = "Address conversion failed";
3280 return kParseError;
3281 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003282 } else if (option.starts_with("--app-image=")) {
3283 app_image_ = option.substr(strlen("--app-image=")).data();
3284 } else if (option.starts_with("--app-oat=")) {
3285 app_oat_ = option.substr(strlen("--app-oat=")).data();
Andreas Gampe9fded872016-09-25 16:08:35 -07003286 } else if (option.starts_with("--dump-imt=")) {
3287 imt_dump_ = option.substr(strlen("--dump-imt=")).data();
3288 } else if (option == "--dump-imt-stats") {
3289 imt_stat_dump_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003290 } else {
3291 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003292 }
3293
Igor Murashkin37743352014-11-13 14:38:00 -08003294 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003295 }
3296
Igor Murashkin37743352014-11-13 14:38:00 -08003297 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
3298 // Infer boot image location from the image location if possible.
3299 if (boot_image_location_ == nullptr) {
3300 boot_image_location_ = image_location_;
3301 }
3302
3303 // Perform the parent checks.
3304 ParseStatus parent_checks = Base::ParseChecks(error_msg);
3305 if (parent_checks != kParseOk) {
3306 return parent_checks;
3307 }
3308
3309 // Perform our own checks.
3310 if (image_location_ == nullptr && oat_filename_ == nullptr) {
3311 *error_msg = "Either --image or --oat-file must be specified";
3312 return kParseError;
3313 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
3314 *error_msg = "Either --image or --oat-file must be specified but not both";
3315 return kParseError;
3316 }
3317
3318 return kParseOk;
3319 }
3320
3321 virtual std::string GetUsage() const {
3322 std::string usage;
3323
3324 usage +=
3325 "Usage: oatdump [options] ...\n"
3326 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
3327 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
3328 "\n"
3329 // Either oat-file or image is required.
3330 " --oat-file=<file.oat>: specifies an input oat filename.\n"
3331 " Example: --oat-file=/system/framework/boot.oat\n"
3332 "\n"
3333 " --image=<file.art>: specifies an input image location.\n"
3334 " Example: --image=/system/framework/boot.art\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003335 "\n"
3336 " --app-image=<file.art>: specifies an input app image. Must also have a specified\n"
3337 " boot image and app oat file.\n"
3338 " Example: --app-image=app.art\n"
3339 "\n"
3340 " --app-oat=<file.odex>: specifies an input app oat.\n"
3341 " Example: --app-oat=app.odex\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003342 "\n";
3343
3344 usage += Base::GetUsage();
3345
3346 usage += // Optional.
Igor Murashkin37743352014-11-13 14:38:00 -08003347 " --no-dump:vmap may be used to disable vmap dumping.\n"
3348 " Example: --no-dump:vmap\n"
3349 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01003350 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
3351 " Example: --dump:code_info_stack_maps\n"
3352 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003353 " --no-disassemble may be used to disable disassembly.\n"
3354 " Example: --no-disassemble\n"
3355 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00003356 " --header-only may be used to print only the oat header.\n"
3357 " Example: --header-only\n"
3358 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003359 " --list-classes may be used to list target file classes (can be used with filters).\n"
3360 " Example: --list-classes\n"
3361 " Example: --list-classes --class-filter=com.example.foo\n"
3362 "\n"
3363 " --list-methods may be used to list target file methods (can be used with filters).\n"
3364 " Example: --list-methods\n"
3365 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
3366 "\n"
3367 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
3368 " Example: --symbolize=/system/framework/boot.oat\n"
3369 "\n"
David Srbecky2fdd03c2016-03-10 15:32:37 +00003370 " --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
3371 " .rodata and .text sections are omitted in the output file to save space.\n"
3372 " Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
3373 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003374 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
3375 " Example: --class-filter=com.example.foo\n"
3376 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003377 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
3378 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003379 "\n"
3380 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
3381 " Example: --export-dex-to=/data/local/tmp\n"
3382 "\n"
3383 " --addr2instr=<address>: output matching method disassembled code from relative\n"
3384 " address (e.g. PC from crash dump)\n"
3385 " Example: --addr2instr=0x00001a3b\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003386 "\n"
3387 " --dump-imt=<file.txt>: output IMT collisions (if any) for the given receiver\n"
3388 " types and interface methods in the given file. The file\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003389 " is read line-wise, where each line should either be a class\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003390 " name or descriptor, or a class name/descriptor and a prefix\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003391 " of a complete method name (separated by a whitespace).\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003392 " Example: --dump-imt=imt.txt\n"
3393 "\n"
3394 " --dump-imt-stats: output IMT statistics for the given boot image\n"
3395 " Example: --dump-imt-stats"
Igor Murashkin37743352014-11-13 14:38:00 -08003396 "\n";
3397
3398 return usage;
3399 }
3400
3401 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07003402 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003403 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00003404 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07003405 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003406 std::string elf_filename_prefix_;
Andreas Gampe9fded872016-09-25 16:08:35 -07003407 std::string imt_dump_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003408 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01003409 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003410 bool disassemble_code_ = true;
3411 bool symbolize_ = false;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003412 bool only_keep_debug_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003413 bool list_classes_ = false;
3414 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003415 bool dump_header_only_ = false;
Andreas Gampe9fded872016-09-25 16:08:35 -07003416 bool imt_stat_dump_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003417 uint32_t addr2instr_ = 0;
3418 const char* export_dex_location_ = nullptr;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003419 const char* app_image_ = nullptr;
3420 const char* app_oat_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003421};
3422
Igor Murashkin37743352014-11-13 14:38:00 -08003423struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
3424 virtual bool NeedsRuntime() OVERRIDE {
3425 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003426
Igor Murashkin37743352014-11-13 14:38:00 -08003427 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
3428 bool absolute_addresses = (args_->oat_filename_ == nullptr);
3429
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003430 oat_dumper_options_.reset(new OatDumperOptions(
Igor Murashkin37743352014-11-13 14:38:00 -08003431 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01003432 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08003433 args_->disassemble_code_,
3434 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003435 args_->class_filter_,
3436 args_->method_filter_,
3437 args_->list_classes_,
3438 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00003439 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003440 args_->export_dex_location_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003441 args_->app_image_,
3442 args_->app_oat_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003443 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08003444
Andreas Gampe9fded872016-09-25 16:08:35 -07003445 return (args_->boot_image_location_ != nullptr ||
3446 args_->image_location_ != nullptr ||
3447 !args_->imt_dump_.empty()) &&
Igor Murashkin37743352014-11-13 14:38:00 -08003448 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003449 }
3450
Igor Murashkin37743352014-11-13 14:38:00 -08003451 virtual bool ExecuteWithoutRuntime() OVERRIDE {
3452 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08003453 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003454
Mathieu Chartierd424d082014-10-15 10:31:46 -07003455 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08003456
Andreas Gampec24f3992014-12-17 20:40:11 -08003457 if (args_->symbolize_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00003458 // ELF has special kind of section called SHT_NOBITS which allows us to create
3459 // sections which exist but their data is omitted from the ELF file to save space.
3460 // This is what "strip --only-keep-debug" does when it creates separate ELF file
3461 // with only debug data. We use it in similar way to exclude .rodata and .text.
3462 bool no_bits = args_->only_keep_debug_;
3463 return SymbolizeOat(args_->oat_filename_, args_->output_name_, no_bits) == EXIT_SUCCESS;
Andreas Gampec24f3992014-12-17 20:40:11 -08003464 } else {
3465 return DumpOat(nullptr,
3466 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003467 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08003468 args_->os_) == EXIT_SUCCESS;
3469 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07003470 }
3471
Igor Murashkin37743352014-11-13 14:38:00 -08003472 virtual bool ExecuteWithRuntime(Runtime* runtime) {
3473 CHECK(args_ != nullptr);
3474
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003475 if (!args_->imt_dump_.empty() || args_->imt_stat_dump_) {
3476 return IMTDumper::Dump(runtime,
3477 args_->imt_dump_,
3478 args_->imt_stat_dump_,
3479 args_->oat_filename_);
Andreas Gampe9fded872016-09-25 16:08:35 -07003480 }
3481
Igor Murashkin37743352014-11-13 14:38:00 -08003482 if (args_->oat_filename_ != nullptr) {
3483 return DumpOat(runtime,
3484 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003485 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08003486 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003487 }
Igor Murashkin37743352014-11-13 14:38:00 -08003488
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003489 return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003490 }
3491
Igor Murashkin37743352014-11-13 14:38:00 -08003492 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
3493};
Andreas Gampe00b25f32014-09-17 21:49:05 -07003494
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003495} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07003496
3497int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08003498 art::OatdumpMain main;
3499 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07003500}