blob: 2a2094eef5c8c9eed280312827ae4b7a705f5e4d [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,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800589 kByteKindCodeInfoEncoding,
David Srbecky45aa5982016-03-18 02:15:09 +0000590 kByteKindCodeInfoStackMasks,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800591 kByteKindCodeInfoRegisterMasks,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800592 kByteKindStackMapNativePc,
593 kByteKindStackMapDexPc,
594 kByteKindStackMapDexRegisterMap,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800595 kByteKindStackMapInlineInfoIndex,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800596 kByteKindStackMapRegisterMaskIndex,
David Srbecky45aa5982016-03-18 02:15:09 +0000597 kByteKindStackMapStackMaskIndex,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800598 kByteKindInlineInfoMethodIndex,
599 kByteKindInlineInfoDexPc,
600 kByteKindInlineInfoExtraData,
601 kByteKindInlineInfoDexRegisterMap,
602 kByteKindInlineInfoIsLast,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800603 kByteKindCount,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800604 // Special ranges for std::accumulate convenience.
605 kByteKindStackMapFirst = kByteKindStackMapNativePc,
David Srbecky45aa5982016-03-18 02:15:09 +0000606 kByteKindStackMapLast = kByteKindStackMapStackMaskIndex,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800607 kByteKindInlineInfoFirst = kByteKindInlineInfoMethodIndex,
608 kByteKindInlineInfoLast = kByteKindInlineInfoIsLast,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800609 };
610 int64_t bits[kByteKindCount] = {};
611 // Since code has deduplication, seen tracks already seen pointers to avoid double counting
612 // deduplicated code and tables.
613 std::unordered_set<const void*> seen;
614
615 // Returns true if it was newly added.
616 bool AddBitsIfUnique(ByteKind kind, int64_t count, const void* address) {
617 if (seen.insert(address).second == true) {
618 // True means the address was not already in the set.
619 AddBits(kind, count);
620 return true;
621 }
622 return false;
623 }
624
625 void AddBits(ByteKind kind, int64_t count) {
626 bits[kind] += count;
627 }
628
629 void Dump(VariableIndentationOutputStream& os) {
630 const int64_t sum = std::accumulate(bits, bits + kByteKindCount, 0u);
631 os.Stream() << "Dumping cumulative use of " << sum / kBitsPerByte << " accounted bytes\n";
632 if (sum > 0) {
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800633 Dump(os, "Code ", bits[kByteKindCode], sum);
634 Dump(os, "QuickMethodHeader ", bits[kByteKindQuickMethodHeader], sum);
635 Dump(os, "CodeInfoEncoding ", bits[kByteKindCodeInfoEncoding], sum);
636 Dump(os, "CodeInfoLocationCatalog ", bits[kByteKindCodeInfoLocationCatalog], sum);
637 Dump(os, "CodeInfoDexRegisterMap ", bits[kByteKindCodeInfoDexRegisterMap], sum);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800638 Dump(os, "CodeInfoStackMasks ", bits[kByteKindCodeInfoStackMasks], sum);
639 Dump(os, "CodeInfoRegisterMasks ", bits[kByteKindCodeInfoRegisterMasks], sum);
Mathieu Chartier1e083792017-02-08 13:30:04 -0800640 // Stack map section.
641 const int64_t stack_map_bits = std::accumulate(bits + kByteKindStackMapFirst,
642 bits + kByteKindStackMapLast + 1,
643 0u);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800644 Dump(os, "CodeInfoStackMap ", stack_map_bits, sum);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800645 {
646 ScopedIndentation indent1(&os);
647 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800648 "StackMapNativePc ",
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800649 bits[kByteKindStackMapNativePc],
650 stack_map_bits,
651 "stack map");
652 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800653 "StackMapDexPcEncoding ",
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800654 bits[kByteKindStackMapDexPc],
655 stack_map_bits,
656 "stack map");
657 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800658 "StackMapDexRegisterMap ",
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800659 bits[kByteKindStackMapDexRegisterMap],
660 stack_map_bits,
661 "stack map");
662 Dump(os,
Mathieu Chartier697dc662017-02-08 16:56:48 -0800663 "StackMapInlineInfoIndex ",
Mathieu Chartier1e083792017-02-08 13:30:04 -0800664 bits[kByteKindStackMapInlineInfoIndex],
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800665 stack_map_bits,
666 "stack map");
667 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800668 "StackMapRegisterMaskIndex ",
669 bits[kByteKindStackMapRegisterMaskIndex],
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800670 stack_map_bits,
671 "stack map");
672 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800673 "StackMapStackMaskIndex ",
David Srbecky45aa5982016-03-18 02:15:09 +0000674 bits[kByteKindStackMapStackMaskIndex],
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800675 stack_map_bits,
676 "stack map");
677 }
Mathieu Chartier1e083792017-02-08 13:30:04 -0800678 // Inline info section.
679 const int64_t inline_info_bits = std::accumulate(bits + kByteKindInlineInfoFirst,
680 bits + kByteKindInlineInfoLast + 1,
681 0u);
682 Dump(os, "CodeInfoInlineInfo ", inline_info_bits, sum);
683 {
684 ScopedIndentation indent1(&os);
685 Dump(os,
686 "InlineInfoMethodIndex ",
687 bits[kByteKindInlineInfoMethodIndex],
688 inline_info_bits,
689 "inline info");
690 Dump(os,
691 "InlineInfoDexPc ",
692 bits[kByteKindStackMapDexPc],
693 inline_info_bits,
694 "inline info");
695 Dump(os,
696 "InlineInfoExtraData ",
697 bits[kByteKindInlineInfoExtraData],
698 inline_info_bits,
699 "inline info");
700 Dump(os,
701 "InlineInfoDexRegisterMap ",
702 bits[kByteKindInlineInfoDexRegisterMap],
703 inline_info_bits,
704 "inline info");
705 Dump(os,
706 "InlineInfoIsLast ",
707 bits[kByteKindInlineInfoIsLast],
708 inline_info_bits,
709 "inline info");
710 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800711 }
712 os.Stream() << "\n" << std::flush;
713 }
714
715 private:
716 void Dump(VariableIndentationOutputStream& os,
717 const char* name,
718 int64_t size,
719 int64_t total,
720 const char* sum_of = "total") {
721 const double percent = (static_cast<double>(size) / static_cast<double>(total)) * 100;
722 os.Stream() << StringPrintf("%s = %8" PRId64 " (%2.0f%% of %s)\n",
723 name,
724 size / kBitsPerByte,
725 percent,
726 sum_of);
727 }
728 };
729
Brian Carlstromaded5f72011-10-07 17:15:04 -0700730 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800731 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800732 // We don't know the length of the code for each method, but we need to know where to stop
733 // when disassembling. What we do know is that a region of code will be followed by some other
734 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
735 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800736 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
737 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700738 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700739 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700740 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
741 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700742 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
743 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800744 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800745 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800746 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800747 for (size_t class_def_index = 0;
748 class_def_index < dex_file->NumClassDefs();
749 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800750 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100751 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700752 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700753 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800754 ClassDataItemIterator it(*dex_file, class_data);
755 SkipAllFields(it);
756 uint32_t class_method_index = 0;
757 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100758 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800759 it.Next();
760 }
761 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100762 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800763 it.Next();
764 }
765 }
766 }
767 }
768
769 // If the last thing in the file is code for a method, there won't be an offset for the "next"
770 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
771 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800772 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800773 }
774
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700775 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
776 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
777 }
778
Elliott Hughese3c845c2012-02-28 17:23:01 -0800779 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800780 uint32_t code_offset = oat_method.GetCodeOffset();
781 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
782 code_offset &= ~0x1;
783 }
784 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800785 offsets_.insert(oat_method.GetVmapTableOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800786 }
787
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700788 // Dex file data, may be for multiple different dex files.
789 class DexFileData {
790 public:
791 DexFileData() {}
792
793 explicit DexFileData(const DexFile& dex_file)
794 : num_string_ids_(dex_file.NumStringIds()),
795 num_method_ids_(dex_file.NumMethodIds()),
796 num_field_ids_(dex_file.NumFieldIds()),
797 num_type_ids_(dex_file.NumTypeIds()),
798 num_class_defs_(dex_file.NumClassDefs()) {
799 for (size_t class_def_index = 0; class_def_index < num_class_defs_; ++class_def_index) {
800 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
801 WalkClass(dex_file, class_def);
802 }
803 }
804
805 void Add(const DexFileData& other) {
806 AddAll(unique_string_ids_from_code_, other.unique_string_ids_from_code_);
807 num_string_ids_from_code_ += other.num_string_ids_from_code_;
808 AddAll(dex_code_item_ptrs_, other.dex_code_item_ptrs_);
809 dex_code_bytes_ += other.dex_code_bytes_;
810 num_string_ids_ += other.num_string_ids_;
811 num_method_ids_ += other.num_method_ids_;
812 num_field_ids_ += other.num_field_ids_;
813 num_type_ids_ += other.num_type_ids_;
814 num_class_defs_ += other.num_class_defs_;
815 }
816
817 void Dump(std::ostream& os) {
818 os << "Num string ids: " << num_string_ids_ << "\n";
819 os << "Num method ids: " << num_method_ids_ << "\n";
820 os << "Num field ids: " << num_field_ids_ << "\n";
821 os << "Num type ids: " << num_type_ids_ << "\n";
822 os << "Num class defs: " << num_class_defs_ << "\n";
823 os << "Unique strings loaded from dex code: " << unique_string_ids_from_code_.size() << "\n";
824 os << "Total strings loaded from dex code: " << num_string_ids_from_code_ << "\n";
825 os << "Number of unique dex code items: " << dex_code_item_ptrs_.size() << "\n";
826 os << "Total number of dex code bytes: " << dex_code_bytes_ << "\n";
827 }
828
829 private:
Andreas Gampe9186ced2016-12-12 14:28:21 -0800830 // All of the elements from one container to another.
831 template <typename Dest, typename Src>
832 static void AddAll(Dest& dest, const Src& src) {
833 dest.insert(src.begin(), src.end());
834 }
835
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700836 void WalkClass(const DexFile& dex_file, const DexFile::ClassDef& class_def) {
837 const uint8_t* class_data = dex_file.GetClassData(class_def);
838 if (class_data == nullptr) { // empty class such as a marker interface?
839 return;
840 }
841 ClassDataItemIterator it(dex_file, class_data);
842 SkipAllFields(it);
843 while (it.HasNextDirectMethod()) {
844 WalkCodeItem(dex_file, it.GetMethodCodeItem());
845 it.Next();
846 }
847 while (it.HasNextVirtualMethod()) {
848 WalkCodeItem(dex_file, it.GetMethodCodeItem());
849 it.Next();
850 }
851 DCHECK(!it.HasNext());
852 }
853
854 void WalkCodeItem(const DexFile& dex_file, const DexFile::CodeItem* code_item) {
855 if (code_item == nullptr) {
856 return;
857 }
858 const size_t code_item_size = code_item->insns_size_in_code_units_;
859 const uint16_t* code_ptr = code_item->insns_;
860 const uint16_t* code_end = code_item->insns_ + code_item_size;
861
862 // If we inserted a new dex code item pointer, add to total code bytes.
863 if (dex_code_item_ptrs_.insert(code_ptr).second) {
864 dex_code_bytes_ += code_item_size * sizeof(code_ptr[0]);
865 }
866
867 while (code_ptr < code_end) {
868 const Instruction* inst = Instruction::At(code_ptr);
869 switch (inst->Opcode()) {
870 case Instruction::CONST_STRING: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800871 const dex::StringIndex string_index(inst->VRegB_21c());
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700872 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
873 ++num_string_ids_from_code_;
874 break;
875 }
876 case Instruction::CONST_STRING_JUMBO: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800877 const dex::StringIndex string_index(inst->VRegB_31c());
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700878 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
879 ++num_string_ids_from_code_;
880 break;
881 }
882 default:
883 break;
884 }
885
886 code_ptr += inst->SizeInCodeUnits();
887 }
888 }
889
890 // Unique string ids loaded from dex code.
891 std::set<StringReference, StringReferenceComparator> unique_string_ids_from_code_;
892
893 // Total string ids loaded from dex code.
894 size_t num_string_ids_from_code_ = 0;
895
896 // Unique code pointers.
897 std::set<const void*> dex_code_item_ptrs_;
898
899 // Total "unique" dex code bytes.
900 size_t dex_code_bytes_ = 0;
901
902 // Other dex ids.
903 size_t num_string_ids_ = 0;
904 size_t num_method_ids_ = 0;
905 size_t num_field_ids_ = 0;
906 size_t num_type_ids_ = 0;
907 size_t num_class_defs_ = 0;
908 };
909
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700910 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
911 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800912 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700913 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800914 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800915 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700916
Andreas Gampe2ba88952016-04-29 17:52:07 -0700917 const uint8_t* const oat_file_begin = oat_dex_file.GetOatFile()->Begin();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100918 const uint8_t* const vdex_file_begin = oat_dex_file.GetOatFile()->DexBegin();
919
920 // Print data range of the dex file embedded inside the corresponding vdex file.
Andreas Gampe2ba88952016-04-29 17:52:07 -0700921 const uint8_t* const dex_file_pointer = oat_dex_file.GetDexFilePointer();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100922 uint32_t dex_offset = dchecked_integral_cast<uint32_t>(dex_file_pointer - vdex_file_begin);
Andreas Gampe2ba88952016-04-29 17:52:07 -0700923 os << StringPrintf("dex-file: 0x%08x..0x%08x\n",
924 dex_offset,
925 dchecked_integral_cast<uint32_t>(dex_offset + oat_dex_file.FileSize() - 1));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700926
Andreas Gampe2ba88952016-04-29 17:52:07 -0700927 // Create the dex file early. A lot of print-out things depend on it.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700928 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700929 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
930 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700931 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700932 os << std::flush;
933 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700934 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100935
Andreas Gampe2ba88952016-04-29 17:52:07 -0700936 // Print lookup table, if it exists.
937 if (oat_dex_file.GetLookupTableData() != nullptr) {
938 uint32_t table_offset = dchecked_integral_cast<uint32_t>(
939 oat_dex_file.GetLookupTableData() - oat_file_begin);
David Sehr9aa352e2016-09-15 18:13:52 -0700940 uint32_t table_size = TypeLookupTable::RawDataLength(dex_file->NumClassDefs());
Andreas Gampe2ba88952016-04-29 17:52:07 -0700941 os << StringPrintf("type-table: 0x%08x..0x%08x\n",
942 table_offset,
943 table_offset + table_size - 1);
944 }
945
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100946 VariableIndentationOutputStream vios(&os);
947 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800948 for (size_t class_def_index = 0;
949 class_def_index < dex_file->NumClassDefs();
950 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700951 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
952 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800953
954 // TODO: Support regex
955 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
956 continue;
957 }
958
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700959 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100960 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700961 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
Andreas Gampea5b09a62016-11-17 15:21:22 -0800962 class_def_index, descriptor, oat_class_offset, class_def.class_idx_.index_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100963 << " (" << oat_class.GetStatus() << ")"
964 << " (" << oat_class.GetType() << ")\n";
965 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700966 if (options_.list_classes_) {
967 continue;
968 }
969 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700970 success = false;
971 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800972 if (stop_analysis) {
973 os << std::flush;
974 return success;
975 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700976 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700977 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700978 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700979 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700980 }
981
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800982 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
983 std::string error_msg;
984 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
985
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700986 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800987 if (dex_file == nullptr) {
988 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
989 return false;
990 }
991 size_t fsize = oat_dex_file.FileSize();
992
993 // Some quick checks just in case
994 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
995 os << "Invalid dex file\n";
996 return false;
997 }
998
999 // Verify output directory exists
1000 if (!OS::DirectoryExists(options_.export_dex_location_)) {
1001 // TODO: Extend OS::DirectoryExists if symlink support is required
1002 os << options_.export_dex_location_ << " output directory not found or symlink\n";
1003 return false;
1004 }
1005
1006 // Beautify path names
1007 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
1008 return false;
1009 }
1010
1011 std::string dex_orig_name;
1012 size_t dex_orig_pos = dex_file_location.rfind('/');
1013 if (dex_orig_pos == std::string::npos)
1014 dex_orig_name = dex_file_location;
1015 else
1016 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
1017
1018 // A more elegant approach to efficiently name user installed apps is welcome
1019 if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
1020 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
1021 size_t apk_orig_pos = dex_file_location.rfind('/');
1022 if (apk_orig_pos != std::string::npos) {
1023 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
1024 }
1025 }
1026
1027 std::string out_dex_path(options_.export_dex_location_);
1028 if (out_dex_path.back() != '/') {
1029 out_dex_path.append("/");
1030 }
1031 out_dex_path.append(dex_orig_name);
1032 out_dex_path.append("_export.dex");
1033 if (out_dex_path.length() > PATH_MAX) {
1034 return false;
1035 }
1036
1037 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
1038 if (file.get() == nullptr) {
1039 os << "Failed to open output dex file " << out_dex_path;
1040 return false;
1041 }
1042
1043 if (!file->WriteFully(dex_file->Begin(), fsize)) {
1044 os << "Failed to write dex file";
1045 file->Erase();
1046 return false;
1047 }
1048
1049 if (file->FlushCloseOrErase() != 0) {
1050 os << "Flush and close failed";
1051 return false;
1052 }
1053
1054 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
1055 os << std::flush;
1056
1057 return true;
1058 }
1059
Elliott Hughese3c845c2012-02-28 17:23:01 -08001060 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -07001061 while (it.HasNextStaticField()) {
1062 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001063 }
Ian Rogers0571d352011-11-03 19:51:38 -07001064 while (it.HasNextInstanceField()) {
1065 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001066 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001067 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001068
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001069 bool DumpOatClass(VariableIndentationOutputStream* vios,
1070 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001071 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001072 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001073 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -07001074 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001075 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001076 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001077 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -08001078 }
1079 ClassDataItemIterator it(dex_file, class_data);
1080 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001081 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001082 while (it.HasNextDirectMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001083 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001084 it.GetMemberIndex(), it.GetMethodCodeItem(),
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001085 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001086 success = false;
1087 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001088 if (addr_found) {
1089 *stop_analysis = true;
1090 return success;
1091 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001092 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -07001093 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001094 }
Ian Rogers0571d352011-11-03 19:51:38 -07001095 while (it.HasNextVirtualMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001096 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001097 it.GetMemberIndex(), it.GetMethodCodeItem(),
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001098 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001099 success = false;
1100 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001101 if (addr_found) {
1102 *stop_analysis = true;
1103 return success;
1104 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001105 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -07001106 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001107 }
Ian Rogers0571d352011-11-03 19:51:38 -07001108 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001109 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001110 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001111 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001112
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001113 static constexpr uint32_t kPrologueBytes = 16;
1114
1115 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
1116 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
1117
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001118 bool DumpOatMethod(VariableIndentationOutputStream* vios,
1119 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -07001120 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001121 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001122 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001123 uint32_t method_access_flags, bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001124 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001125
1126 // TODO: Support regex
1127 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
1128 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00001129 return success;
1130 }
1131
David Sehr709b0702016-10-13 09:12:37 -07001132 std::string pretty_method = dex_file.PrettyMethod(dex_method_idx, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001133 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
1134 class_method_index, pretty_method.c_str(),
1135 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001136 if (options_.list_methods_) return success;
1137
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001138 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
1139 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
1140 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
1141 uint32_t code_offset = oat_method.GetCodeOffset();
1142 uint32_t code_size = oat_method.GetQuickCodeSize();
1143 if (resolved_addr2instr_ != 0) {
1144 if (resolved_addr2instr_ > code_offset + code_size) {
1145 return success;
1146 } else {
1147 *addr_found = true; // stop analyzing file at next iteration
1148 }
1149 }
1150
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001151 // Everything below is indented at least once.
1152 ScopedIndentation indent1(vios);
1153
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001154 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001155 vios->Stream() << "DEX CODE:\n";
1156 ScopedIndentation indent2(vios);
1157 DumpDexCode(vios->Stream(), dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -07001158 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001159
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001160 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001161 std::unique_ptr<verifier::MethodVerifier> verifier;
1162 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001163 // We need to have the handle scope stay live until after the verifier since the verifier has
1164 // a handle to the dex cache from hs.
1165 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001166 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
1167 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001168 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001169 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001170 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -07001171 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001172 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001173 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001174 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001175 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001176 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001177 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001178 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001179 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001180 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
1181 oat_method_offsets_offset, oat_file_.Size());
1182 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001183 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001184 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001185 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001186
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001187 ScopedIndentation indent2(vios);
1188 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001189 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
1190 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001191 vios->Stream() << StringPrintf("WARNING: "
1192 "code offset 0x%08x is past end of file 0x%08zx.\n",
1193 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001194 success = false;
1195 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001196 vios->Stream() << "\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001197 }
1198 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001199 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001200 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
1201 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001202 stats_.AddBitsIfUnique(Stats::kByteKindQuickMethodHeader,
1203 sizeof(*method_header) * kBitsPerByte,
1204 method_header);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001205 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001206 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001207 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001208 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001209 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001210 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001211 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
1212 method_header_offset, oat_file_.Size());
1213 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001214 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001215 return false;
1216 }
1217
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001218 ScopedIndentation indent2(vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001219 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001220 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001221 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001222 }
Mingyao Yang063fc772016-08-02 11:02:54 -07001223 uint32_t vmap_table_offset = method_header ==
1224 nullptr ? 0 : method_header->GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001225 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001226
1227 size_t vmap_table_offset_limit =
1228 (kIsVdexEnabled && IsMethodGeneratedByDexToDexCompiler(oat_method, code_item))
1229 ? oat_file_.GetVdexFile()->Size()
1230 : method_header->GetCode() - oat_file_.Begin();
1231 if (vmap_table_offset >= vmap_table_offset_limit) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001232 vios->Stream() << StringPrintf("WARNING: "
1233 "vmap table offset 0x%08x is past end of file 0x%08zx. "
1234 "vmap table offset was loaded from offset 0x%08x.\n",
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001235 vmap_table_offset,
1236 vmap_table_offset_limit,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001237 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001238 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001239 } else if (options_.dump_vmap_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001240 DumpVmapData(vios, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001241 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001242 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001243 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001244 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001245
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001246 ScopedIndentation indent2(vios);
1247 vios->Stream()
1248 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
1249 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
1250 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
1251 vios->Stream() << "\n";
1252 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
1253 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
1254 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001255 }
1256 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001257 // Based on spill masks from QuickMethodFrameInfo so placed
1258 // after it is dumped, but useful for understanding quick
1259 // code, so dumped here.
1260 ScopedIndentation indent2(vios);
1261 DumpVregLocations(vios->Stream(), oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001262 }
1263 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001264 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001265 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
1266 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001267 ScopedIndentation indent2(vios);
1268 vios->Stream() << StringPrintf("WARNING: "
1269 "code size offset 0x%08x is past end of file 0x%08zx.",
1270 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001271 success = false;
1272 } else {
1273 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001274 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1275 uint64_t aligned_code_end = aligned_code_begin + code_size;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001276 stats_.AddBitsIfUnique(Stats::kByteKindCode, code_size * kBitsPerByte, code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001277
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001278 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001279 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001280 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001281 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
1282 code_offset,
1283 code_size_offset,
1284 code_size,
1285 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001286
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001287 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001288 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001289 vios->Stream() << StringPrintf("WARNING: "
1290 "start of code at 0x%08x is past end of file 0x%08zx.",
1291 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001292 success = false;
1293 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001294 vios->Stream() << StringPrintf(
1295 "WARNING: "
1296 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
1297 "code size is 0x%08x loaded from offset 0x%08x.\n",
1298 aligned_code_end, oat_file_.Size(),
1299 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001300 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001301 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001302 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001303 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001304 }
1305 }
1306 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001307 vios->Stream() << StringPrintf(
1308 "WARNING: "
1309 "code size %d is bigger than max expected threshold of %d. "
1310 "code size is 0x%08x loaded from offset 0x%08x.\n",
1311 code_size, kMaxCodeSize,
1312 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001313 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001314 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001315 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001316 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001317 }
1318 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001319 } else if (options_.disassemble_code_) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001320 DumpCode(vios, oat_method, code_item, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001321 }
1322 }
1323 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001324 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001325 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001326 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001327
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001328 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1329 if (spill_mask == 0) {
1330 return;
1331 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001332 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001333 for (size_t i = 0; i < 32; i++) {
1334 if ((spill_mask & (1 << i)) != 0) {
1335 if (is_float) {
1336 os << "fr" << i;
1337 } else {
1338 os << "r" << i;
1339 }
1340 spill_mask ^= 1 << i; // clear bit
1341 if (spill_mask != 0) {
1342 os << ", ";
1343 } else {
1344 break;
1345 }
1346 }
1347 }
1348 os << ")";
1349 }
1350
Roland Levillain442b46a2015-02-18 16:54:21 +00001351 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001352 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001353 const OatFile::OatMethod& oat_method,
1354 const DexFile::CodeItem* code_item) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001355 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1356 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001357 const void* raw_code_info = oat_method.GetVmapTable();
1358 if (raw_code_info != nullptr) {
1359 CodeInfo code_info(raw_code_info);
1360 DCHECK(code_item != nullptr);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001361 ScopedIndentation indent1(vios);
1362 DumpCodeInfo(vios, code_info, oat_method, *code_item);
Roland Levillain442b46a2015-02-18 16:54:21 +00001363 }
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001364 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
1365 // We don't encode the size in the table, so just emit that we have quickened
1366 // information.
1367 ScopedIndentation indent(vios);
1368 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001369 } else {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001370 // Otherwise, there is nothing to display.
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001371 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001372 }
1373
1374 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001375 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001376 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001377 const OatFile::OatMethod& oat_method,
Roland Levillain442b46a2015-02-18 16:54:21 +00001378 const DexFile::CodeItem& code_item) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001379 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001380 oat_method.GetCodeOffset(),
1381 code_item.registers_size_,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001382 options_.dump_code_info_stack_maps_,
1383 instruction_set_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001384 }
1385
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001386 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1387 const DexFile::CodeItem* code_item) {
1388 if (code_item != nullptr) {
1389 size_t num_locals_ins = code_item->registers_size_;
1390 size_t num_ins = code_item->ins_size_;
1391 size_t num_locals = num_locals_ins - num_ins;
1392 size_t num_outs = code_item->outs_size_;
1393
1394 os << "vr_stack_locations:";
1395 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1396 // For readability, delimit the different kinds of VRs.
1397 if (reg == num_locals_ins) {
1398 os << "\n\tmethod*:";
1399 } else if (reg == num_locals && num_ins > 0) {
1400 os << "\n\tins:";
1401 } else if (reg == 0 && num_locals > 0) {
1402 os << "\n\tlocals:";
1403 }
1404
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001405 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1406 code_item,
1407 oat_method.GetCoreSpillMask(),
1408 oat_method.GetFpSpillMask(),
1409 oat_method.GetFrameSizeInBytes(),
1410 reg,
1411 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001412 os << " v" << reg << "[sp + #" << offset << "]";
1413 }
1414
1415 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1416 if (out_reg == 0) {
1417 os << "\n\touts:";
1418 }
1419
1420 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1421 os << " v" << out_reg << "[sp + #" << offset << "]";
1422 }
1423
1424 os << "\n";
1425 }
1426 }
1427
Ian Rogersb23a7722012-10-09 16:54:26 -07001428 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001429 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001430 size_t i = 0;
1431 while (i < code_item->insns_size_in_code_units_) {
1432 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001433 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1434 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001435 i += instruction->SizeInCodeUnits();
1436 }
1437 }
1438 }
1439
Roland Levillainf2650d12015-05-28 14:53:28 +01001440 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1441 // the optimizing compiler?
1442 static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1443 const DexFile::CodeItem* code_item) {
1444 // If the native GC map is null and the Dex `code_item` is not
1445 // null, then this method has been compiled with the optimizing
1446 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001447 return oat_method.GetQuickCode() != nullptr &&
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001448 oat_method.GetVmapTable() != nullptr &&
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001449 code_item != nullptr;
1450 }
1451
1452 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1453 // the dextodex compiler?
1454 static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
1455 const DexFile::CodeItem* code_item) {
1456 // If the quick code is null, the Dex `code_item` is not
1457 // null, and the vmap table is not null, then this method has been compiled
1458 // with the dextodex compiler.
1459 return oat_method.GetQuickCode() == nullptr &&
1460 oat_method.GetVmapTable() != nullptr &&
1461 code_item != nullptr;
Roland Levillainf2650d12015-05-28 14:53:28 +01001462 }
1463
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001464 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001465 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001466 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001467 const DexFile* dex_file,
1468 const DexFile::ClassDef& class_def,
1469 const DexFile::CodeItem* code_item,
1470 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001471 if ((method_access_flags & kAccNative) == 0) {
1472 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001473 Runtime* const runtime = Runtime::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001474 Handle<mirror::DexCache> dex_cache(
Mathieu Chartierf284d442016-06-02 11:48:30 -07001475 hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file, nullptr)));
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001476 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001477 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001478 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
David Brazdil15fc7292016-09-02 14:13:18 +01001479 class_def, code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001480 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001481
1482 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001483 }
1484
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001485 // The StackMapsHelper provides the stack maps in the native PC order.
1486 // For identical native PCs, the order from the CodeInfo is preserved.
1487 class StackMapsHelper {
1488 public:
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001489 explicit StackMapsHelper(const uint8_t* raw_code_info, InstructionSet instruction_set)
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001490 : code_info_(raw_code_info),
1491 encoding_(code_info_.ExtractEncoding()),
1492 number_of_stack_maps_(code_info_.GetNumberOfStackMaps(encoding_)),
1493 indexes_(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001494 offset_(static_cast<uint32_t>(-1)),
1495 stack_map_index_(0u),
1496 instruction_set_(instruction_set) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001497 if (number_of_stack_maps_ != 0u) {
1498 // Check if native PCs are ordered.
1499 bool ordered = true;
1500 StackMap last = code_info_.GetStackMapAt(0u, encoding_);
1501 for (size_t i = 1; i != number_of_stack_maps_; ++i) {
1502 StackMap current = code_info_.GetStackMapAt(i, encoding_);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001503 if (last.GetNativePcOffset(encoding_.stack_map.encoding, instruction_set) >
1504 current.GetNativePcOffset(encoding_.stack_map.encoding, instruction_set)) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001505 ordered = false;
1506 break;
1507 }
1508 last = current;
1509 }
1510 if (!ordered) {
1511 // Create indirection indexes for access in native PC order. We do not optimize
1512 // for the fact that there can currently be only two separately ordered ranges,
1513 // namely normal stack maps and catch-point stack maps.
1514 indexes_.resize(number_of_stack_maps_);
1515 std::iota(indexes_.begin(), indexes_.end(), 0u);
1516 std::sort(indexes_.begin(),
1517 indexes_.end(),
1518 [this](size_t lhs, size_t rhs) {
1519 StackMap left = code_info_.GetStackMapAt(lhs, encoding_);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001520 uint32_t left_pc = left.GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001521 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001522 StackMap right = code_info_.GetStackMapAt(rhs, encoding_);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001523 uint32_t right_pc = right.GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001524 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001525 // If the PCs are the same, compare indexes to preserve the original order.
1526 return (left_pc < right_pc) || (left_pc == right_pc && lhs < rhs);
1527 });
1528 }
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001529 offset_ = GetStackMapAt(0).GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001530 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001531 }
1532 }
1533
1534 const CodeInfo& GetCodeInfo() const {
1535 return code_info_;
1536 }
1537
1538 const CodeInfoEncoding& GetEncoding() const {
1539 return encoding_;
1540 }
1541
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001542 uint32_t GetOffset() const {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001543 return offset_;
1544 }
1545
1546 StackMap GetStackMap() const {
1547 return GetStackMapAt(stack_map_index_);
1548 }
1549
1550 void Next() {
1551 ++stack_map_index_;
1552 offset_ = (stack_map_index_ == number_of_stack_maps_)
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001553 ? static_cast<uint32_t>(-1)
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001554 : GetStackMapAt(stack_map_index_).GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001555 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001556 }
1557
1558 private:
1559 StackMap GetStackMapAt(size_t i) const {
1560 if (!indexes_.empty()) {
1561 i = indexes_[i];
1562 }
1563 DCHECK_LT(i, number_of_stack_maps_);
1564 return code_info_.GetStackMapAt(i, encoding_);
1565 }
1566
1567 const CodeInfo code_info_;
1568 const CodeInfoEncoding encoding_;
1569 const size_t number_of_stack_maps_;
1570 dchecked_vector<size_t> indexes_; // Used if stack map native PCs are not ordered.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001571 uint32_t offset_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001572 size_t stack_map_index_;
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001573 const InstructionSet instruction_set_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001574 };
1575
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001576 void DumpCode(VariableIndentationOutputStream* vios,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001577 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1578 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001579 const void* quick_code = oat_method.GetQuickCode();
1580
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001581 if (code_size == 0) {
1582 code_size = oat_method.GetQuickCodeSize();
1583 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001584 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001585 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001586 return;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001587 } else if (!bad_input && IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1588 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001589 StackMapsHelper helper(oat_method.GetVmapTable(), instruction_set_);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001590 {
1591 CodeInfoEncoding encoding(helper.GetEncoding());
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001592 StackMapEncoding stack_map_encoding(encoding.stack_map.encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001593 const size_t num_stack_maps = encoding.stack_map.num_entries;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001594 std::vector<uint8_t> size_vector;
1595 encoding.Compress(&size_vector);
1596 if (stats_.AddBitsIfUnique(Stats::kByteKindCodeInfoEncoding,
1597 size_vector.size() * kBitsPerByte,
1598 oat_method.GetVmapTable())) {
Mathieu Chartier1e083792017-02-08 13:30:04 -08001599 // Stack maps
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001600 stats_.AddBits(
1601 Stats::kByteKindStackMapNativePc,
1602 stack_map_encoding.GetNativePcEncoding().BitSize() * num_stack_maps);
1603 stats_.AddBits(
1604 Stats::kByteKindStackMapDexPc,
1605 stack_map_encoding.GetDexPcEncoding().BitSize() * num_stack_maps);
1606 stats_.AddBits(
1607 Stats::kByteKindStackMapDexRegisterMap,
1608 stack_map_encoding.GetDexRegisterMapEncoding().BitSize() * num_stack_maps);
1609 stats_.AddBits(
Mathieu Chartier1e083792017-02-08 13:30:04 -08001610 Stats::kByteKindStackMapInlineInfoIndex,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001611 stack_map_encoding.GetInlineInfoEncoding().BitSize() * num_stack_maps);
1612 stats_.AddBits(
Mathieu Chartier1a20b682017-01-31 14:25:16 -08001613 Stats::kByteKindStackMapRegisterMaskIndex,
1614 stack_map_encoding.GetRegisterMaskIndexEncoding().BitSize() * num_stack_maps);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001615 stats_.AddBits(
David Srbecky45aa5982016-03-18 02:15:09 +00001616 Stats::kByteKindStackMapStackMaskIndex,
1617 stack_map_encoding.GetStackMaskIndexEncoding().BitSize() * num_stack_maps);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001618
1619 // Stack masks
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001620 stats_.AddBits(
David Srbecky45aa5982016-03-18 02:15:09 +00001621 Stats::kByteKindCodeInfoStackMasks,
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001622 encoding.stack_mask.encoding.BitSize() * encoding.stack_mask.num_entries);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001623
1624 // Register masks
Mathieu Chartier1a20b682017-01-31 14:25:16 -08001625 stats_.AddBits(
1626 Stats::kByteKindCodeInfoRegisterMasks,
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001627 encoding.register_mask.encoding.BitSize() * encoding.register_mask.num_entries);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001628
1629 // Location catalog
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001630 const size_t location_catalog_bytes =
1631 helper.GetCodeInfo().GetDexRegisterLocationCatalogSize(encoding);
1632 stats_.AddBits(Stats::kByteKindCodeInfoLocationCatalog,
1633 kBitsPerByte * location_catalog_bytes);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001634 // Dex register bytes.
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001635 const size_t dex_register_bytes =
1636 helper.GetCodeInfo().GetDexRegisterMapsSize(encoding, code_item->registers_size_);
1637 stats_.AddBits(
1638 Stats::kByteKindCodeInfoDexRegisterMap,
1639 kBitsPerByte * dex_register_bytes);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001640
1641 // Inline infos.
1642 const size_t num_inline_infos = encoding.inline_info.num_entries;
1643 if (num_inline_infos > 0u) {
1644 stats_.AddBits(
1645 Stats::kByteKindInlineInfoMethodIndex,
1646 encoding.inline_info.encoding.GetMethodIndexEncoding().BitSize() * num_inline_infos);
1647 stats_.AddBits(
1648 Stats::kByteKindInlineInfoDexPc,
1649 encoding.inline_info.encoding.GetDexPcEncoding().BitSize() * num_inline_infos);
1650 stats_.AddBits(
1651 Stats::kByteKindInlineInfoExtraData,
1652 encoding.inline_info.encoding.GetExtraDataEncoding().BitSize() * num_inline_infos);
1653 stats_.AddBits(
1654 Stats::kByteKindInlineInfoDexRegisterMap,
1655 encoding.inline_info.encoding.GetDexRegisterMapEncoding().BitSize() *
1656 num_inline_infos);
1657 stats_.AddBits(Stats::kByteKindInlineInfoIsLast, num_inline_infos);
1658 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001659 }
1660 }
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001661 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1662 size_t offset = 0;
1663 while (offset < code_size) {
1664 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
1665 if (offset == helper.GetOffset()) {
1666 ScopedIndentation indent1(vios);
1667 StackMap stack_map = helper.GetStackMap();
1668 DCHECK(stack_map.IsValid());
1669 stack_map.Dump(vios,
1670 helper.GetCodeInfo(),
1671 helper.GetEncoding(),
1672 oat_method.GetCodeOffset(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001673 code_item->registers_size_,
1674 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001675 do {
1676 helper.Next();
1677 // There may be multiple stack maps at a given PC. We display only the first one.
1678 } while (offset == helper.GetOffset());
1679 }
1680 DCHECK_LT(offset, helper.GetOffset());
1681 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001682 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001683 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1684 size_t offset = 0;
1685 while (offset < code_size) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001686 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001687 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001688 }
1689 }
1690
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001691 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001692 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001693 const OatDumperOptions& options_;
1694 uint32_t resolved_addr2instr_;
Andreas Gampe372f3a32016-08-19 10:49:06 -07001695 const InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001696 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001697 Disassembler* disassembler_;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001698 Stats stats_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001699};
1700
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001701class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001702 public:
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001703 ImageDumper(std::ostream* os,
1704 gc::space::ImageSpace& image_space,
1705 const ImageHeader& image_header,
1706 OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001707 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001708 vios_(os),
1709 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001710 image_space_(image_space),
1711 image_header_(image_header),
1712 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001713
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001714 bool Dump() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001715 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001716 std::ostream& indent_os = vios_.Stream();
1717
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001718 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001719
Jeff Haodcdc85b2015-12-04 14:06:18 -08001720 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1721
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001722 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001723
Mathieu Chartiere401d142015-04-22 13:56:20 -07001724 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1725
1726 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1727 auto section = static_cast<ImageHeader::ImageSections>(i);
1728 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1729 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001730
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001731 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001732
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001733 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001734
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001735 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1736
1737 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1738
1739 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001740
Alex Lighta59dd802014-07-02 16:28:08 -07001741 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1742
Igor Murashkin46774762014-10-22 11:37:02 -07001743 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1744
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001745 {
1746 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001747 static_assert(arraysize(image_roots_descriptions_) ==
1748 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001749 DCHECK_LE(image_header_.GetImageRoots()->GetLength(), ImageHeader::kImageRootsMax);
1750 for (int32_t i = 0, size = image_header_.GetImageRoots()->GetLength(); i != size; ++i) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001751 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1752 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001753 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001754 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001755 if (image_root_object != nullptr && image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001756 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001757 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001758 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001759 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1760 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001761 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001762 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1763 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001764 run++;
1765 } else {
1766 break;
1767 }
1768 }
1769 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001770 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001771 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001772 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001773 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001774 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001775 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001776 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001777 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001778 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001779 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001780 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001781 }
1782 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001783 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001784
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001785 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001786 os << "METHOD ROOTS\n";
1787 static_assert(arraysize(image_methods_descriptions_) ==
1788 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1789 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1790 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1791 const char* description = image_methods_descriptions_[i];
1792 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001793 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001794 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001795 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001796 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001797
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001798 Runtime* const runtime = Runtime::Current();
1799 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001800 std::string image_filename = image_space_.GetImageFilename();
1801 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001802 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001803 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001804 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001805 const OatFile* oat_file = image_space_.GetOatFile();
Alex Lighta59dd802014-07-02 16:28:08 -07001806 if (oat_file == nullptr) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001807 oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location);
1808 }
1809 if (oat_file == nullptr) {
1810 oat_file = OatFile::Open(oat_location,
1811 oat_location,
1812 nullptr,
1813 nullptr,
1814 false,
1815 /*low_4gb*/false,
1816 nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001817 &error_msg);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001818 }
1819 if (oat_file == nullptr) {
1820 os << "OAT FILE NOT FOUND: " << error_msg << "\n";
1821 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001822 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001823 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001824
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001825 stats_.oat_file_bytes = oat_file->Size();
1826
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001827 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001828
Mathieu Chartier02e25112013-08-14 16:14:24 -07001829 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001830 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001831 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1832 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001833 }
1834
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001835 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001836
Jeff Haodcdc85b2015-12-04 14:06:18 -08001837 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001838 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001839 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001840 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001841 {
1842 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1843 heap->FlushAllocStack();
1844 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001845 // Since FlushAllocStack() above resets the (active) allocation
1846 // stack. Need to revoke the thread-local allocation stacks that
1847 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001848 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001849 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001850 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001851 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001852 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001853 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001854 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001855 {
Andreas Gampecc1b5352016-12-01 16:58:38 -08001856 ReaderMutexLock mu(self, *Locks::dex_lock_);
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001857 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001858 ObjPtr<mirror::DexCache> dex_cache =
1859 ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001860 if (dex_cache != nullptr) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001861 dex_caches_.insert(dex_cache.Ptr());
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001862 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001863 }
1864 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001865 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001866 // Dump the normal objects before ArtMethods.
1867 image_space_.GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1868 indent_os << "\n";
1869 // TODO: Dump fields.
1870 // Dump methods after.
Jeff Haodcdc85b2015-12-04 14:06:18 -08001871 DumpArtMethodVisitor visitor(this);
Mathieu Chartiere42888f2016-04-14 10:49:19 -07001872 image_header_.VisitPackedArtMethods(&visitor,
1873 image_space_.Begin(),
1874 image_header_.GetPointerSize());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001875 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001876 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001877 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001878 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001879 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001880 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001881 size_t data_size = image_header_.GetDataSize(); // stored size in file.
1882 if (file == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001883 LOG(WARNING) << "Failed to find image in " << image_filename;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001884 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001885 stats_.file_bytes = file->GetLength();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001886 // If the image is compressed, adjust to decompressed size.
1887 size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader);
1888 if (image_header_.GetStorageMode() == ImageHeader::kStorageModeUncompressed) {
1889 DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image";
1890 }
1891 stats_.file_bytes += uncompressed_size - data_size;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001892 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001893 size_t header_bytes = sizeof(ImageHeader);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001894 const auto& object_section = image_header_.GetImageSection(ImageHeader::kSectionObjects);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001895 const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1896 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001897 const auto& dex_cache_arrays_section = image_header_.GetImageSection(
1898 ImageHeader::kSectionDexCacheArrays);
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001899 const auto& intern_section = image_header_.GetImageSection(
1900 ImageHeader::kSectionInternedStrings);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001901 const auto& class_table_section = image_header_.GetImageSection(
1902 ImageHeader::kSectionClassTable);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001903 const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1904
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001905 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001906
1907 // Objects are kObjectAlignment-aligned.
1908 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1909 if (object_section.Offset() > header_bytes) {
1910 stats_.alignment_bytes += object_section.Offset() - header_bytes;
1911 }
1912
1913 // Field section is 4-byte aligned.
1914 constexpr size_t kFieldSectionAlignment = 4U;
1915 uint32_t end_objects = object_section.Offset() + object_section.Size();
1916 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
1917 stats_.alignment_bytes += field_section.Offset() - end_objects;
1918
1919 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
1920 uint32_t end_fields = field_section.Offset() + field_section.Size();
1921 CHECK_ALIGNED(method_section.Offset(), 4);
1922 stats_.alignment_bytes += method_section.Offset() - end_fields;
1923
1924 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
1925 uint32_t end_methods = method_section.Offset() + method_section.Size();
1926 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
1927 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
1928
1929 // Intern table is 8-byte aligned.
1930 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
1931 CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
1932 stats_.alignment_bytes += intern_section.Offset() - end_caches;
1933
1934 // Add space between intern table and class table.
1935 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
1936 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
1937
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001938 // Add space between end of image data and bitmap. Expect the bitmap to be page-aligned.
1939 const size_t bitmap_offset = sizeof(ImageHeader) + data_size;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001940 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001941 stats_.alignment_bytes += RoundUp(bitmap_offset, kPageSize) - bitmap_offset;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001942
Mathieu Chartiere401d142015-04-22 13:56:20 -07001943 stats_.bitmap_bytes += bitmap_section.Size();
1944 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01001945 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01001946 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001947 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001948 stats_.class_table_bytes += class_table_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001949 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001950 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001951
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001952 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001953
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001954 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001955 }
1956
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001957 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001958 class DumpArtMethodVisitor : public ArtMethodVisitor {
1959 public:
1960 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
1961
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001962 virtual void Visit(ArtMethod* method) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001963 std::ostream& indent_os = image_dumper_->vios_.Stream();
David Sehr709b0702016-10-13 09:12:37 -07001964 indent_os << method << " " << " ArtMethod: " << ArtMethod::PrettyMethod(method) << "\n";
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001965 image_dumper_->DumpMethod(method, indent_os);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001966 indent_os << "\n";
1967 }
1968
1969 private:
1970 ImageDumper* const image_dumper_;
1971 };
1972
Mathieu Chartier3398c782016-09-30 10:27:43 -07001973 static void PrettyObjectValue(std::ostream& os,
1974 ObjPtr<mirror::Class> type,
1975 ObjPtr<mirror::Object> value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001976 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001977 CHECK(type != nullptr);
1978 if (value == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07001979 os << StringPrintf("null %s\n", type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001980 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001981 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001982 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001983 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001984 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001985 mirror::Class* klass = value->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07001986 os << StringPrintf("%p Class: %s\n", klass, mirror::Class::PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001987 } else {
David Sehr709b0702016-10-13 09:12:37 -07001988 os << StringPrintf("%p %s\n", value.Ptr(), type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001989 }
1990 }
1991
Mathieu Chartier3398c782016-09-30 10:27:43 -07001992 static void PrintField(std::ostream& os, ArtField* field, ObjPtr<mirror::Object> obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001993 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001994 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08001995 switch (field->GetTypeAsPrimitiveType()) {
1996 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08001997 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001998 break;
1999 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002000 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002001 break;
2002 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002003 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002004 break;
2005 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002006 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002007 break;
2008 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07002009 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002010 break;
2011 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07002012 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002013 break;
2014 case Primitive::kPrimBoolean:
Roland Levillain5e8d5f02016-10-18 18:03:43 +01002015 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj) ? "true" : "false",
Fred Shih37f05ef2014-07-16 18:38:08 -07002016 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002017 break;
2018 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07002019 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002020 break;
2021 case Primitive::kPrimNot: {
2022 // Get the value, don't compute the type unless it is non-null as we don't want
2023 // to cause class loading.
Mathieu Chartier3398c782016-09-30 10:27:43 -07002024 ObjPtr<mirror::Object> value = field->GetObj(obj);
Ian Rogers08f1f502014-12-02 15:04:37 -08002025 if (value == nullptr) {
2026 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07002027 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08002028 // Grab the field type without causing resolution.
Mathieu Chartier3398c782016-09-30 10:27:43 -07002029 ObjPtr<mirror::Class> field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08002030 if (field_type != nullptr) {
2031 PrettyObjectValue(os, field_type, value);
2032 } else {
Mathieu Chartier3398c782016-09-30 10:27:43 -07002033 os << StringPrintf("%p %s\n",
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07002034 value.Ptr(),
Ian Rogers08f1f502014-12-02 15:04:37 -08002035 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
2036 }
Ian Rogers50239c72013-06-17 14:53:22 -07002037 }
Ian Rogers08f1f502014-12-02 15:04:37 -08002038 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07002039 }
Ian Rogers08f1f502014-12-02 15:04:37 -08002040 default:
2041 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
2042 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08002043 }
2044 }
2045
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002046 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002047 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002048 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002049 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002050 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08002051 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002052 for (ArtField& field : klass->GetIFields()) {
2053 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08002054 }
2055 }
2056
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002057 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002058 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002059 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002060
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002061 const void* GetQuickOatCodeBegin(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08002062 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002063 image_header_.GetPointerSize());
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002064 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002065 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002066 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002067 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002068 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002069 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002070 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002071 }
2072
Mathieu Chartiere401d142015-04-22 13:56:20 -07002073 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002074 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002075 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
2076 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002077 return 0;
2078 }
2079 return oat_code_begin[-1];
2080 }
2081
Mathieu Chartiere401d142015-04-22 13:56:20 -07002082 const void* GetQuickOatCodeEnd(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002083 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002084 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002085 if (oat_code_begin == nullptr) {
2086 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002087 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002088 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002089 }
2090
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002091 static void Callback(mirror::Object* obj, void* arg) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002092 DCHECK(obj != nullptr);
2093 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002094 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002095 if (!state->InDumpSpace(obj)) {
2096 return;
2097 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002098
2099 size_t object_bytes = obj->SizeOf();
2100 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
2101 state->stats_.object_bytes += object_bytes;
2102 state->stats_.alignment_bytes += alignment_bytes;
2103
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002104 std::ostream& os = state->vios_.Stream();
2105
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002106 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08002107 if (obj_class->IsArrayClass()) {
David Sehr709b0702016-10-13 09:12:37 -07002108 os << StringPrintf("%p: %s length:%d\n", obj, obj_class->PrettyDescriptor().c_str(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002109 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08002110 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002111 mirror::Class* klass = obj->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07002112 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj,
2113 mirror::Class::PrettyDescriptor(klass).c_str())
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002114 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08002115 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002116 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07002117 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002118 } else {
David Sehr709b0702016-10-13 09:12:37 -07002119 os << StringPrintf("%p: %s\n", obj, obj_class->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002120 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002121 ScopedIndentation indent1(&state->vios_);
2122 DumpFields(os, obj, obj_class);
Andreas Gampe542451c2016-07-26 09:02:02 -07002123 const PointerSize image_pointer_size = state->image_header_.GetPointerSize();
Ian Rogersd5b32602012-02-26 16:40:04 -08002124 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002125 auto* obj_array = obj->AsObjectArray<mirror::Object>();
2126 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002127 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002128 size_t run = 0;
2129 for (int32_t j = i + 1; j < length; j++) {
2130 if (value == obj_array->Get(j)) {
2131 run++;
2132 } else {
2133 break;
2134 }
2135 }
2136 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002137 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002138 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002139 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08002140 i = i + run;
2141 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002142 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002143 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002144 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08002145 }
2146 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07002147 mirror::Class* klass = obj->AsClass();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002148 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002149 os << "STATICS:\n";
2150 ScopedIndentation indent2(&state->vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002151 for (ArtField& field : klass->GetSFields()) {
2152 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08002153 }
2154 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002155 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01002156 auto it = state->dex_caches_.find(obj);
2157 if (it != state->dex_caches_.end()) {
2158 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002159 const auto& field_section = state->image_header_.GetImageSection(
2160 ImageHeader::kSectionArtFields);
2161 const auto& method_section = state->image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01002162 size_t num_methods = dex_cache->NumResolvedMethods();
2163 if (num_methods != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002164 os << "Methods (size=" << num_methods << "):\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002165 ScopedIndentation indent2(&state->vios_);
2166 auto* resolved_methods = dex_cache->GetResolvedMethods();
2167 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002168 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_methods,
2169 i,
2170 image_pointer_size);
Vladimir Marko05792b92015-08-03 11:56:49 +01002171 size_t run = 0;
2172 for (size_t j = i + 1;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002173 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_methods,
2174 j,
2175 image_pointer_size);
2176 ++j) {
2177 ++run;
2178 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002179 if (run == 0) {
2180 os << StringPrintf("%zd: ", i);
2181 } else {
2182 os << StringPrintf("%zd to %zd: ", i, i + run);
2183 i = i + run;
2184 }
2185 std::string msg;
2186 if (elem == nullptr) {
2187 msg = "null";
2188 } else if (method_section.Contains(
2189 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002190 msg = reinterpret_cast<ArtMethod*>(elem)->PrettyMethod();
Vladimir Marko05792b92015-08-03 11:56:49 +01002191 } else {
2192 msg = "<not in method section>";
2193 }
2194 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07002195 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002196 }
2197 size_t num_fields = dex_cache->NumResolvedFields();
2198 if (num_fields != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002199 os << "Fields (size=" << num_fields << "):\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002200 ScopedIndentation indent2(&state->vios_);
2201 auto* resolved_fields = dex_cache->GetResolvedFields();
2202 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002203 auto* elem = mirror::DexCache::GetElementPtrSize(
2204 resolved_fields, i, image_pointer_size);
Vladimir Marko05792b92015-08-03 11:56:49 +01002205 size_t run = 0;
2206 for (size_t j = i + 1;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002207 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_fields,
2208 j,
2209 image_pointer_size);
2210 ++j) {
2211 ++run;
2212 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002213 if (run == 0) {
2214 os << StringPrintf("%zd: ", i);
2215 } else {
2216 os << StringPrintf("%zd to %zd: ", i, i + run);
2217 i = i + run;
2218 }
2219 std::string msg;
2220 if (elem == nullptr) {
2221 msg = "null";
2222 } else if (field_section.Contains(
2223 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002224 msg = reinterpret_cast<ArtField*>(elem)->PrettyField();
Vladimir Marko05792b92015-08-03 11:56:49 +01002225 } else {
2226 msg = "<not in field section>";
2227 }
2228 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002229 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002230 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002231 size_t num_types = dex_cache->NumResolvedTypes();
2232 if (num_types != 0u) {
2233 os << "Types (size=" << num_types << "):\n";
2234 ScopedIndentation indent2(&state->vios_);
2235 auto* resolved_types = dex_cache->GetResolvedTypes();
2236 for (size_t i = 0; i < num_types; ++i) {
Vladimir Markod16363a2017-02-01 14:09:13 +00002237 auto* elem = resolved_types[i].Read();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002238 size_t run = 0;
Vladimir Markod16363a2017-02-01 14:09:13 +00002239 for (size_t j = i + 1; j != num_types && elem == resolved_types[j].Read(); ++j) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002240 ++run;
2241 }
2242 if (run == 0) {
2243 os << StringPrintf("%zd: ", i);
2244 } else {
2245 os << StringPrintf("%zd to %zd: ", i, i + run);
2246 i = i + run;
2247 }
2248 std::string msg;
2249 if (elem == nullptr) {
2250 msg = "null";
2251 } else {
David Sehr709b0702016-10-13 09:12:37 -07002252 msg = elem->PrettyClass();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002253 }
Vladimir Markod16363a2017-02-01 14:09:13 +00002254 os << StringPrintf("%p %s\n", elem, msg.c_str());
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002255 }
2256 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002257 }
2258 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002259 std::string temp;
2260 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002261 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002262
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002263 void DumpMethod(ArtMethod* method, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002264 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002265 DCHECK(method != nullptr);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002266 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
2267 const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
Andreas Gampe542451c2016-07-26 09:02:02 -07002268 const PointerSize pointer_size = image_header_.GetPointerSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002269 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2270 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002271 if (method->IsNative()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002272 bool first_occurrence;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002273 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2274 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002275 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002276 stats_.native_to_managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002277 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002278 if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
2279 image_header_.GetPointerSize())) {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002280 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002281 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002282 } else if (method->IsAbstract() || method->IsClassInitializer()) {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002283 // Don't print information for these.
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002284 } else if (method->IsRuntimeMethod()) {
2285 ImtConflictTable* table = method->GetImtConflictTable(image_header_.GetPointerSize());
2286 if (table != nullptr) {
2287 indent_os << "IMT conflict table " << table << " method: ";
2288 for (size_t i = 0, count = table->NumEntries(pointer_size); i < count; ++i) {
David Sehr709b0702016-10-13 09:12:37 -07002289 indent_os << ArtMethod::PrettyMethod(table->GetImplementationMethod(i, pointer_size))
2290 << " ";
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002291 }
2292 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002293 } else {
2294 const DexFile::CodeItem* code_item = method->GetCodeItem();
2295 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002296 stats_.dex_instruction_bytes += dex_instruction_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002297
2298 bool first_occurrence;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002299 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002300 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01002301 // Method compiled with the optimizing compiler have no vmap table.
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002302 vmap_table_bytes = ComputeOatSize(method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002303 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002304 stats_.vmap_table_bytes += vmap_table_bytes;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002305 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002306 }
2307
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002308 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2309 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002310 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002311 stats_.managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002312 if (method->IsConstructor()) {
2313 if (method->IsStatic()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002314 stats_.class_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002315 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002316 stats_.large_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002317 }
2318 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002319 stats_.large_method_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002320 }
2321 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002322 stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002323
Igor Murashkin7617abd2015-07-10 18:27:47 -07002324 uint32_t method_access_flags = method->GetAccessFlags();
2325
Mathieu Chartiere401d142015-04-22 13:56:20 -07002326 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002327 indent_os << StringPrintf("SIZE: Dex Instructions=%zd StackMaps=%zd AccessFlags=0x%x\n",
2328 dex_instruction_bytes,
2329 vmap_table_bytes,
Igor Murashkin7617abd2015-07-10 18:27:47 -07002330 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002331
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002332 size_t total_size = dex_instruction_bytes +
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002333 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002334
2335 double expansion =
2336 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002337 stats_.ComputeOutliers(total_size, expansion, method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002338 }
2339 }
2340
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002341 std::set<const void*> already_seen_;
2342 // Compute the size of the given data within the oat file and whether this is the first time
2343 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002344 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002345 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002346 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002347 already_seen_.insert(oat_data);
2348 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002349 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002350 }
2351 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002352 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002353
2354 public:
2355 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002356 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002357 size_t file_bytes;
2358
2359 size_t header_bytes;
2360 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002361 size_t art_field_bytes;
2362 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01002363 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002364 size_t interned_strings_bytes;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002365 size_t class_table_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07002366 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002367 size_t alignment_bytes;
2368
2369 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002370 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002371 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07002372 size_t class_initializer_code_bytes;
2373 size_t large_initializer_code_bytes;
2374 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002375
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002376 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002377
2378 size_t dex_instruction_bytes;
2379
Mathieu Chartiere401d142015-04-22 13:56:20 -07002380 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002381 std::vector<size_t> method_outlier_size;
2382 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002383 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002384
Roland Levillain3887c462015-08-12 18:15:42 +01002385 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002386 : oat_file_bytes(0),
2387 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002388 header_bytes(0),
2389 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002390 art_field_bytes(0),
2391 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01002392 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002393 interned_strings_bytes(0),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002394 class_table_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07002395 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002396 alignment_bytes(0),
2397 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002398 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002399 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07002400 class_initializer_code_bytes(0),
2401 large_initializer_code_bytes(0),
2402 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002403 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002404 dex_instruction_bytes(0) {}
2405
Elliott Hughesa0e18062012-04-13 15:59:59 -07002406 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002407 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002408 size_t bytes;
2409 size_t count;
2410 };
2411 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2412 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002413
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002414 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002415 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2416 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002417 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002418 it->second.count += 1;
2419 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002420 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002421 }
2422 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002423
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002424 double PercentOfOatBytes(size_t size) {
2425 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2426 }
2427
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002428 double PercentOfFileBytes(size_t size) {
2429 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2430 }
2431
2432 double PercentOfObjectBytes(size_t size) {
2433 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2434 }
2435
Mathieu Chartiere401d142015-04-22 13:56:20 -07002436 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002437 method_outlier_size.push_back(total_size);
2438 method_outlier_expansion.push_back(expansion);
2439 method_outlier.push_back(method);
2440 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002441
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002442 void DumpOutliers(std::ostream& os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002443 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002444 size_t sum_of_sizes = 0;
2445 size_t sum_of_sizes_squared = 0;
2446 size_t sum_of_expansion = 0;
2447 size_t sum_of_expansion_squared = 0;
2448 size_t n = method_outlier_size.size();
Mathieu Chartier1ebf8d32016-06-09 11:51:27 -07002449 if (n <= 1) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08002450 return;
2451 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002452 for (size_t i = 0; i < n; i++) {
2453 size_t cur_size = method_outlier_size[i];
2454 sum_of_sizes += cur_size;
2455 sum_of_sizes_squared += cur_size * cur_size;
2456 double cur_expansion = method_outlier_expansion[i];
2457 sum_of_expansion += cur_expansion;
2458 sum_of_expansion_squared += cur_expansion * cur_expansion;
2459 }
2460 size_t size_mean = sum_of_sizes / n;
2461 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2462 double expansion_mean = sum_of_expansion / n;
2463 double expansion_variance =
2464 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2465
2466 // Dump methods whose size is a certain number of standard deviations from the mean
2467 size_t dumped_values = 0;
2468 size_t skipped_values = 0;
2469 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2470 size_t cur_size_variance = i * i * size_variance;
2471 bool first = true;
2472 for (size_t j = 0; j < n; j++) {
2473 size_t cur_size = method_outlier_size[j];
2474 if (cur_size > size_mean) {
2475 size_t cur_var = cur_size - size_mean;
2476 cur_var = cur_var * cur_var;
2477 if (cur_var > cur_size_variance) {
2478 if (dumped_values > 20) {
2479 if (i == 1) {
2480 skipped_values++;
2481 } else {
2482 i = 2; // jump to counting for 1 standard deviation
2483 break;
2484 }
2485 } else {
2486 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002487 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002488 first = false;
2489 }
David Sehr709b0702016-10-13 09:12:37 -07002490 os << ArtMethod::PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002491 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002492 method_outlier_size[j] = 0; // don't consider this method again
2493 dumped_values++;
2494 }
2495 }
2496 }
2497 }
2498 }
2499 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002500 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002501 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002502 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002503 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002504
2505 // Dump methods whose expansion is a certain number of standard deviations from the mean
2506 dumped_values = 0;
2507 skipped_values = 0;
2508 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2509 double cur_expansion_variance = i * i * expansion_variance;
2510 bool first = true;
2511 for (size_t j = 0; j < n; j++) {
2512 double cur_expansion = method_outlier_expansion[j];
2513 if (cur_expansion > expansion_mean) {
2514 size_t cur_var = cur_expansion - expansion_mean;
2515 cur_var = cur_var * cur_var;
2516 if (cur_var > cur_expansion_variance) {
2517 if (dumped_values > 20) {
2518 if (i == 1) {
2519 skipped_values++;
2520 } else {
2521 i = 2; // jump to counting for 1 standard deviation
2522 break;
2523 }
2524 } else {
2525 if (first) {
2526 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002527 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002528 first = false;
2529 }
David Sehr709b0702016-10-13 09:12:37 -07002530 os << ArtMethod::PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002531 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002532 method_outlier_expansion[j] = 0.0; // don't consider this method again
2533 dumped_values++;
2534 }
2535 }
2536 }
2537 }
2538 }
2539 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002540 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002541 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002542 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002543 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002544 }
2545
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002546 void Dump(std::ostream& os, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002547 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002548 {
2549 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2550 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002551 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2552 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2553 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2554 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2555 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2556 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002557 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002558 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2559 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002560 header_bytes, PercentOfFileBytes(header_bytes),
2561 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002562 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2563 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002564 dex_cache_arrays_bytes,
2565 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002566 interned_strings_bytes,
2567 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002568 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002569 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002570 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2571 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002572 CHECK_EQ(file_bytes,
2573 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2574 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2575 bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002576 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002577
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002578 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002579 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002580 for (const auto& sizes_and_count : sizes_and_counts) {
2581 const std::string& descriptor(sizes_and_count.first);
2582 double average = static_cast<double>(sizes_and_count.second.bytes) /
2583 static_cast<double>(sizes_and_count.second.count);
2584 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002585 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002586 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002587 descriptor.c_str(), sizes_and_count.second.bytes,
2588 sizes_and_count.second.count, average, percent);
2589 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002590 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002591 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002592 CHECK_EQ(object_bytes, object_bytes_total);
2593
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002594 os << StringPrintf("oat_file_bytes = %8zd\n"
2595 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002596 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2597 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2598 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2599 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002600 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002601 managed_code_bytes,
2602 PercentOfOatBytes(managed_code_bytes),
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002603 native_to_managed_code_bytes,
2604 PercentOfOatBytes(native_to_managed_code_bytes),
2605 class_initializer_code_bytes,
2606 PercentOfOatBytes(class_initializer_code_bytes),
2607 large_initializer_code_bytes,
2608 PercentOfOatBytes(large_initializer_code_bytes),
2609 large_method_code_bytes,
2610 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002611 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002612 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002613 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002614 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2615 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002616 }
2617
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002618 os << "\n" << StringPrintf("vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002619 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002620 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002621
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002622 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2623 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002624 static_cast<double>(managed_code_bytes) /
2625 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002626 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002627 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002628 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002629
2630 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002631 }
2632 } stats_;
2633
2634 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002635 enum {
2636 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2637 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2638 kLargeConstructorDexBytes = 4000,
2639 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2640 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2641 kLargeMethodDexBytes = 16000
2642 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002643
2644 // For performance, use the *os_ directly for anything that doesn't need indentation
2645 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002646 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002647 VariableIndentationOutputStream vios_;
2648 ScopedIndentation indent1_;
2649
Ian Rogers1d54e732013-05-02 21:10:01 -07002650 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002651 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002652 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002653 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002654 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002655
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002656 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002657};
2658
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002659static int DumpImage(gc::space::ImageSpace* image_space,
2660 OatDumperOptions* options,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002661 std::ostream* os) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002662 const ImageHeader& image_header = image_space->GetImageHeader();
2663 if (!image_header.IsValid()) {
2664 fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
2665 return EXIT_FAILURE;
2666 }
2667 ImageDumper image_dumper(os, *image_space, image_header, options);
2668 if (!image_dumper.Dump()) {
2669 return EXIT_FAILURE;
2670 }
2671 return EXIT_SUCCESS;
2672}
2673
2674static int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002675 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002676 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002677 options->class_loader_ = &null_class_loader;
2678
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002679 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002680 if (options->app_image_ != nullptr) {
2681 if (options->app_oat_ == nullptr) {
2682 LOG(ERROR) << "Can not dump app image without app oat file";
Jeff Haodcdc85b2015-12-04 14:06:18 -08002683 return EXIT_FAILURE;
2684 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002685 // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
2686 // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
2687 // pointers into 32 bit pointer sized ArtMethods.
2688 std::string error_msg;
2689 std::unique_ptr<OatFile> oat_file(OatFile::Open(options->app_oat_,
2690 options->app_oat_,
2691 nullptr,
2692 nullptr,
2693 false,
2694 /*low_4gb*/true,
2695 nullptr,
2696 &error_msg));
2697 if (oat_file == nullptr) {
2698 LOG(ERROR) << "Failed to open oat file " << options->app_oat_ << " with error " << error_msg;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002699 return EXIT_FAILURE;
2700 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002701 std::unique_ptr<gc::space::ImageSpace> space(
2702 gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg));
2703 if (space == nullptr) {
2704 LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error "
2705 << error_msg;
2706 }
2707 // Open dex files for the image.
2708 std::vector<std::unique_ptr<const DexFile>> dex_files;
2709 if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
2710 LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
2711 << error_msg;
2712 }
2713 // Dump the actual image.
2714 int result = DumpImage(space.get(), options, os);
2715 if (result != EXIT_SUCCESS) {
2716 return result;
2717 }
2718 // Fall through to dump the boot images.
2719 }
2720
2721 gc::Heap* heap = runtime->GetHeap();
2722 CHECK(heap->HasBootImageSpace()) << "No image spaces";
2723 for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
2724 int result = DumpImage(image_space, options, os);
2725 if (result != EXIT_SUCCESS) {
2726 return result;
2727 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002728 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002729 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002730}
2731
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002732static jobject InstallOatFile(Runtime* runtime,
2733 std::unique_ptr<OatFile> oat_file,
2734 std::vector<const DexFile*>* class_path)
2735 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002736 Thread* self = Thread::Current();
2737 CHECK(self != nullptr);
2738 // Need well-known-classes.
2739 WellKnownClasses::Init(self->GetJniEnv());
2740
2741 // Need to register dex files to get a working dex cache.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002742 OatFile* oat_file_ptr = oat_file.get();
Andreas Gampe00b25f32014-09-17 21:49:05 -07002743 ClassLinker* class_linker = runtime->GetClassLinker();
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002744 runtime->GetOatFileManager().RegisterOatFile(std::move(oat_file));
2745 for (const OatFile::OatDexFile* odf : oat_file_ptr->GetOatDexFiles()) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002746 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002747 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002748 CHECK(dex_file != nullptr) << error_msg;
Mathieu Chartierf284d442016-06-02 11:48:30 -07002749 class_linker->RegisterDexFile(*dex_file, nullptr);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002750 class_path->push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002751 }
2752
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002753 // Need a class loader. Fake that we're a compiler.
2754 // Note: this will run initializers through the unstarted runtime, so make sure it's
2755 // initialized.
2756 interpreter::UnstartedRuntime::Initialize();
2757
2758 jobject class_loader = class_linker->CreatePathClassLoader(self, *class_path);
2759
2760 return class_loader;
2761}
2762
2763static int DumpOatWithRuntime(Runtime* runtime,
2764 std::unique_ptr<OatFile> oat_file,
2765 OatDumperOptions* options,
2766 std::ostream* os) {
2767 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2768 ScopedObjectAccess soa(Thread::Current());
2769
2770 OatFile* oat_file_ptr = oat_file.get();
2771 std::vector<const DexFile*> class_path;
2772 jobject class_loader = InstallOatFile(runtime, std::move(oat_file), &class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002773
2774 // Use the class loader while dumping.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002775 StackHandleScope<1> scope(soa.Self());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002776 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07002777 soa.Decode<mirror::ClassLoader>(class_loader));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002778 options->class_loader_ = &loader_handle;
2779
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002780 OatDumper oat_dumper(*oat_file_ptr, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002781 bool success = oat_dumper.Dump(*os);
2782 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2783}
2784
2785static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002786 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002787 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002788 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002789 options->class_loader_ = &null_class_loader;
2790
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002791 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002792 bool success = oat_dumper.Dump(*os);
2793 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2794}
2795
2796static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2797 std::ostream* os) {
2798 std::string error_msg;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002799 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
2800 oat_filename,
2801 nullptr,
2802 nullptr,
2803 false,
2804 /*low_4gb*/false,
2805 nullptr,
2806 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002807 if (oat_file == nullptr) {
2808 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2809 return EXIT_FAILURE;
2810 }
2811
2812 if (runtime != nullptr) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002813 return DumpOatWithRuntime(runtime, std::move(oat_file), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002814 } else {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002815 return DumpOatWithoutRuntime(oat_file.get(), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002816 }
2817}
2818
David Srbecky2fdd03c2016-03-10 15:32:37 +00002819static int SymbolizeOat(const char* oat_filename, std::string& output_name, bool no_bits) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002820 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002821 OatFile* oat_file = OatFile::Open(oat_filename,
2822 oat_filename,
2823 nullptr,
2824 nullptr,
2825 false,
2826 /*low_4gb*/false,
2827 nullptr,
2828 &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002829 if (oat_file == nullptr) {
2830 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2831 return EXIT_FAILURE;
2832 }
2833
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002834 bool result;
2835 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
2836 // files for 64-bit code in the past.
2837 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002838 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file, output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002839 result = oat_symbolizer.Symbolize();
2840 } else {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002841 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file, output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002842 result = oat_symbolizer.Symbolize();
2843 }
2844 if (!result) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002845 fprintf(stderr, "Failed to symbolize\n");
2846 return EXIT_FAILURE;
2847 }
2848
2849 return EXIT_SUCCESS;
2850}
2851
Andreas Gampe9fded872016-09-25 16:08:35 -07002852class IMTDumper {
2853 public:
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002854 static bool Dump(Runtime* runtime,
2855 const std::string& imt_file,
2856 bool dump_imt_stats,
2857 const char* oat_filename) {
2858 Thread* self = Thread::Current();
2859
2860 ScopedObjectAccess soa(self);
2861 StackHandleScope<1> scope(self);
2862 MutableHandle<mirror::ClassLoader> class_loader = scope.NewHandle<mirror::ClassLoader>(nullptr);
2863 std::vector<const DexFile*> class_path;
2864
2865 if (oat_filename != nullptr) {
2866 std::string error_msg;
2867 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
2868 oat_filename,
2869 nullptr,
2870 nullptr,
2871 false,
2872 /*low_4gb*/false,
2873 nullptr,
2874 &error_msg));
2875 if (oat_file == nullptr) {
2876 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2877 return false;
2878 }
2879
2880 class_loader.Assign(soa.Decode<mirror::ClassLoader>(
2881 InstallOatFile(runtime, std::move(oat_file), &class_path)));
2882 } else {
2883 class_loader.Assign(nullptr); // Boot classloader. Just here for explicit documentation.
2884 class_path = runtime->GetClassLinker()->GetBootClassPath();
2885 }
2886
2887 if (!imt_file.empty()) {
2888 return DumpImt(runtime, imt_file, class_loader);
2889 }
2890
2891 if (dump_imt_stats) {
2892 return DumpImtStats(runtime, class_path, class_loader);
2893 }
2894
2895 LOG(FATAL) << "Should not reach here";
2896 UNREACHABLE();
2897 }
2898
2899 private:
2900 static bool DumpImt(Runtime* runtime,
2901 const std::string& imt_file,
2902 Handle<mirror::ClassLoader> h_class_loader)
2903 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002904 std::vector<std::string> lines = ReadCommentedInputFromFile(imt_file);
2905 std::unordered_set<std::string> prepared;
2906
2907 for (const std::string& line : lines) {
2908 // A line should be either a class descriptor, in which case we will dump the complete IMT,
2909 // or a class descriptor and an interface method, in which case we will lookup the method,
2910 // determine its IMT slot, and check the class' IMT.
2911 size_t first_space = line.find(' ');
2912 if (first_space == std::string::npos) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002913 DumpIMTForClass(runtime, line, h_class_loader, &prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07002914 } else {
2915 DumpIMTForMethod(runtime,
2916 line.substr(0, first_space),
2917 line.substr(first_space + 1, std::string::npos),
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002918 h_class_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07002919 &prepared);
2920 }
2921 std::cerr << std::endl;
2922 }
2923
2924 return true;
2925 }
2926
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002927 static bool DumpImtStats(Runtime* runtime,
2928 const std::vector<const DexFile*>& dex_files,
2929 Handle<mirror::ClassLoader> h_class_loader)
2930 REQUIRES_SHARED(Locks::mutator_lock_) {
2931 size_t without_imt = 0;
2932 size_t with_imt = 0;
Andreas Gampe9fded872016-09-25 16:08:35 -07002933 std::map<size_t, size_t> histogram;
2934
2935 ClassLinker* class_linker = runtime->GetClassLinker();
2936 const PointerSize pointer_size = class_linker->GetImagePointerSize();
2937 std::unordered_set<std::string> prepared;
2938
2939 Thread* self = Thread::Current();
Andreas Gampe9fded872016-09-25 16:08:35 -07002940 StackHandleScope<1> scope(self);
2941 MutableHandle<mirror::Class> h_klass(scope.NewHandle<mirror::Class>(nullptr));
2942
2943 for (const DexFile* dex_file : dex_files) {
2944 for (uint32_t class_def_index = 0;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002945 class_def_index != dex_file->NumClassDefs();
2946 ++class_def_index) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002947 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
2948 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002949 h_klass.Assign(class_linker->FindClass(self, descriptor, h_class_loader));
Andreas Gampe9fded872016-09-25 16:08:35 -07002950 if (h_klass.Get() == nullptr) {
2951 std::cerr << "Warning: could not load " << descriptor << std::endl;
2952 continue;
2953 }
2954
2955 if (HasNoIMT(runtime, h_klass, pointer_size, &prepared)) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002956 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07002957 continue;
2958 }
2959
2960 ImTable* im_table = PrepareAndGetImTable(runtime, h_klass, pointer_size, &prepared);
2961 if (im_table == nullptr) {
2962 // Should not happen, but accept.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002963 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07002964 continue;
2965 }
2966
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002967 with_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07002968 for (size_t imt_index = 0; imt_index != ImTable::kSize; ++imt_index) {
2969 ArtMethod* ptr = im_table->Get(imt_index, pointer_size);
2970 if (ptr->IsRuntimeMethod()) {
2971 if (ptr->IsImtUnimplementedMethod()) {
2972 histogram[0]++;
2973 } else {
2974 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
2975 histogram[current_table->NumEntries(pointer_size)]++;
2976 }
2977 } else {
2978 histogram[1]++;
2979 }
2980 }
2981 }
2982 }
2983
2984 std::cerr << "IMT stats:"
2985 << std::endl << std::endl;
2986
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002987 std::cerr << " " << with_imt << " classes with IMT."
Andreas Gampe9fded872016-09-25 16:08:35 -07002988 << std::endl << std::endl;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002989 std::cerr << " " << without_imt << " classes without IMT (or copy from Object)."
Andreas Gampe9fded872016-09-25 16:08:35 -07002990 << std::endl << std::endl;
2991
2992 double sum_one = 0;
2993 size_t count_one = 0;
2994
2995 std::cerr << " " << "IMT histogram" << std::endl;
2996 for (auto& bucket : histogram) {
2997 std::cerr << " " << bucket.first << " " << bucket.second << std::endl;
2998 if (bucket.first > 0) {
2999 sum_one += bucket.second * bucket.first;
3000 count_one += bucket.second;
3001 }
3002 }
3003
3004 double count_zero = count_one + histogram[0];
3005 std::cerr << " Stats:" << std::endl;
3006 std::cerr << " Average depth (including empty): " << (sum_one / count_zero) << std::endl;
3007 std::cerr << " Average depth (excluding empty): " << (sum_one / count_one) << std::endl;
3008
3009 return true;
3010 }
3011
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003012 // Return whether the given class has no IMT (or the one shared with java.lang.Object).
Andreas Gampe9fded872016-09-25 16:08:35 -07003013 static bool HasNoIMT(Runtime* runtime,
3014 Handle<mirror::Class> klass,
3015 const PointerSize pointer_size,
3016 std::unordered_set<std::string>* prepared)
3017 REQUIRES_SHARED(Locks::mutator_lock_) {
3018 if (klass->IsObjectClass() || !klass->ShouldHaveImt()) {
3019 return true;
3020 }
3021
3022 if (klass->GetImt(pointer_size) == nullptr) {
3023 PrepareClass(runtime, klass, prepared);
3024 }
3025
3026 mirror::Class* object_class = mirror::Class::GetJavaLangClass()->GetSuperClass();
3027 DCHECK(object_class->IsObjectClass());
3028
3029 bool result = klass->GetImt(pointer_size) == object_class->GetImt(pointer_size);
3030
Mathieu Chartier6beced42016-11-15 15:51:31 -08003031 if (klass->GetIfTable()->Count() == 0) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003032 DCHECK(result);
3033 }
3034
3035 return result;
3036 }
3037
3038 static void PrintTable(ImtConflictTable* table, PointerSize pointer_size)
3039 REQUIRES_SHARED(Locks::mutator_lock_) {
3040 if (table == nullptr) {
3041 std::cerr << " <No IMT?>" << std::endl;
3042 return;
3043 }
3044 size_t table_index = 0;
3045 for (;;) {
3046 ArtMethod* ptr = table->GetInterfaceMethod(table_index, pointer_size);
3047 if (ptr == nullptr) {
3048 return;
3049 }
3050 table_index++;
David Sehr709b0702016-10-13 09:12:37 -07003051 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003052 }
3053 }
3054
3055 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3056 Thread* self,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003057 Handle<mirror::ClassLoader> h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003058 const std::string& class_name,
3059 const PointerSize pointer_size,
3060 mirror::Class** klass_out,
3061 std::unordered_set<std::string>* prepared)
3062 REQUIRES_SHARED(Locks::mutator_lock_) {
3063 if (class_name.empty()) {
3064 return nullptr;
3065 }
3066
3067 std::string descriptor;
3068 if (class_name[0] == 'L') {
3069 descriptor = class_name;
3070 } else {
3071 descriptor = DotToDescriptor(class_name.c_str());
3072 }
3073
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003074 mirror::Class* klass = runtime->GetClassLinker()->FindClass(self, descriptor.c_str(), h_loader);
Andreas Gampe9fded872016-09-25 16:08:35 -07003075
3076 if (klass == nullptr) {
3077 self->ClearException();
3078 std::cerr << "Did not find " << class_name << std::endl;
3079 *klass_out = nullptr;
3080 return nullptr;
3081 }
3082
3083 StackHandleScope<1> scope(Thread::Current());
3084 Handle<mirror::Class> h_klass = scope.NewHandle<mirror::Class>(klass);
3085
3086 ImTable* ret = PrepareAndGetImTable(runtime, h_klass, pointer_size, prepared);
3087 *klass_out = h_klass.Get();
3088 return ret;
3089 }
3090
3091 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3092 Handle<mirror::Class> h_klass,
3093 const PointerSize pointer_size,
3094 std::unordered_set<std::string>* prepared)
3095 REQUIRES_SHARED(Locks::mutator_lock_) {
3096 PrepareClass(runtime, h_klass, prepared);
3097 return h_klass->GetImt(pointer_size);
3098 }
3099
3100 static void DumpIMTForClass(Runtime* runtime,
3101 const std::string& class_name,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003102 Handle<mirror::ClassLoader> h_loader,
3103 std::unordered_set<std::string>* prepared)
3104 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003105 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
3106 mirror::Class* klass;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003107 ImTable* imt = PrepareAndGetImTable(runtime,
3108 Thread::Current(),
3109 h_loader,
3110 class_name,
3111 pointer_size,
3112 &klass,
3113 prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07003114 if (imt == nullptr) {
3115 return;
3116 }
3117
3118 std::cerr << class_name << std::endl << " IMT:" << std::endl;
3119 for (size_t index = 0; index < ImTable::kSize; ++index) {
3120 std::cerr << " " << index << ":" << std::endl;
3121 ArtMethod* ptr = imt->Get(index, pointer_size);
3122 if (ptr->IsRuntimeMethod()) {
3123 if (ptr->IsImtUnimplementedMethod()) {
3124 std::cerr << " <empty>" << std::endl;
3125 } else {
3126 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3127 PrintTable(current_table, pointer_size);
3128 }
3129 } else {
David Sehr709b0702016-10-13 09:12:37 -07003130 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003131 }
3132 }
3133
3134 std::cerr << " Interfaces:" << std::endl;
3135 // Run through iftable, find methods that slot here, see if they fit.
3136 mirror::IfTable* if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003137 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
3138 mirror::Class* iface = if_table->GetInterface(i);
3139 std::string iface_name;
3140 std::cerr << " " << iface->GetDescriptor(&iface_name) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003141
Mathieu Chartier6beced42016-11-15 15:51:31 -08003142 for (ArtMethod& iface_method : iface->GetVirtualMethods(pointer_size)) {
3143 uint32_t class_hash, name_hash, signature_hash;
3144 ImTable::GetImtHashComponents(&iface_method, &class_hash, &name_hash, &signature_hash);
3145 uint32_t imt_slot = ImTable::GetImtIndex(&iface_method);
3146 std::cerr << " " << iface_method.PrettyMethod(true)
3147 << " slot=" << imt_slot
3148 << std::hex
3149 << " class_hash=0x" << class_hash
3150 << " name_hash=0x" << name_hash
3151 << " signature_hash=0x" << signature_hash
3152 << std::dec
3153 << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003154 }
3155 }
3156 }
3157
3158 static void DumpIMTForMethod(Runtime* runtime,
3159 const std::string& class_name,
3160 const std::string& method,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003161 Handle<mirror::ClassLoader> h_loader,
3162 std::unordered_set<std::string>* prepared)
3163 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003164 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
3165 mirror::Class* klass;
3166 ImTable* imt = PrepareAndGetImTable(runtime,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003167 Thread::Current(),
3168 h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003169 class_name,
3170 pointer_size,
3171 &klass,
3172 prepared);
3173 if (imt == nullptr) {
3174 return;
3175 }
3176
3177 std::cerr << class_name << " <" << method << ">" << std::endl;
3178 for (size_t index = 0; index < ImTable::kSize; ++index) {
3179 ArtMethod* ptr = imt->Get(index, pointer_size);
3180 if (ptr->IsRuntimeMethod()) {
3181 if (ptr->IsImtUnimplementedMethod()) {
3182 continue;
3183 }
3184
3185 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3186 if (current_table == nullptr) {
3187 continue;
3188 }
3189
3190 size_t table_index = 0;
3191 for (;;) {
3192 ArtMethod* ptr2 = current_table->GetInterfaceMethod(table_index, pointer_size);
3193 if (ptr2 == nullptr) {
3194 break;
3195 }
3196 table_index++;
3197
David Sehr709b0702016-10-13 09:12:37 -07003198 std::string p_name = ptr2->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003199 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003200 std::cerr << " Slot "
3201 << index
3202 << " ("
3203 << current_table->NumEntries(pointer_size)
3204 << ")"
3205 << std::endl;
3206 PrintTable(current_table, pointer_size);
3207 return;
3208 }
3209 }
3210 } else {
David Sehr709b0702016-10-13 09:12:37 -07003211 std::string p_name = ptr->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003212 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003213 std::cerr << " Slot " << index << " (1)" << std::endl;
3214 std::cerr << " " << p_name << std::endl;
3215 } else {
3216 // Run through iftable, find methods that slot here, see if they fit.
3217 mirror::IfTable* if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003218 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
3219 mirror::Class* iface = if_table->GetInterface(i);
3220 size_t num_methods = iface->NumDeclaredVirtualMethods();
3221 if (num_methods > 0) {
3222 for (ArtMethod& iface_method : iface->GetMethods(pointer_size)) {
3223 if (ImTable::GetImtIndex(&iface_method) == index) {
3224 std::string i_name = iface_method.PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003225 if (android::base::StartsWith(i_name, method.c_str())) {
Mathieu Chartier6beced42016-11-15 15:51:31 -08003226 std::cerr << " Slot " << index << " (1)" << std::endl;
3227 std::cerr << " " << p_name << " (" << i_name << ")" << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003228 }
3229 }
3230 }
3231 }
3232 }
3233 }
3234 }
3235 }
3236 }
3237
3238 // Read lines from the given stream, dropping comments and empty lines
3239 static std::vector<std::string> ReadCommentedInputStream(std::istream& in_stream) {
3240 std::vector<std::string> output;
3241 while (in_stream.good()) {
3242 std::string dot;
3243 std::getline(in_stream, dot);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003244 if (android::base::StartsWith(dot, "#") || dot.empty()) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003245 continue;
3246 }
3247 output.push_back(dot);
3248 }
3249 return output;
3250 }
3251
3252 // Read lines from the given file, dropping comments and empty lines.
3253 static std::vector<std::string> ReadCommentedInputFromFile(const std::string& input_filename) {
3254 std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
3255 if (input_file.get() == nullptr) {
3256 LOG(ERROR) << "Failed to open input file " << input_filename;
3257 return std::vector<std::string>();
3258 }
3259 std::vector<std::string> result = ReadCommentedInputStream(*input_file);
3260 input_file->close();
3261 return result;
3262 }
3263
3264 // Prepare a class, i.e., ensure it has a filled IMT. Will do so recursively for superclasses,
3265 // and note in the given set that the work was done.
3266 static void PrepareClass(Runtime* runtime,
3267 Handle<mirror::Class> h_klass,
3268 std::unordered_set<std::string>* done)
3269 REQUIRES_SHARED(Locks::mutator_lock_) {
3270 if (!h_klass->ShouldHaveImt()) {
3271 return;
3272 }
3273
3274 std::string name;
3275 name = h_klass->GetDescriptor(&name);
3276
3277 if (done->find(name) != done->end()) {
3278 return;
3279 }
3280 done->insert(name);
3281
3282 if (h_klass->HasSuperClass()) {
3283 StackHandleScope<1> h(Thread::Current());
3284 PrepareClass(runtime, h.NewHandle<mirror::Class>(h_klass->GetSuperClass()), done);
3285 }
3286
3287 if (!h_klass->IsTemp()) {
3288 runtime->GetClassLinker()->FillIMTAndConflictTables(h_klass.Get());
3289 }
3290 }
3291};
3292
Igor Murashkin37743352014-11-13 14:38:00 -08003293struct OatdumpArgs : public CmdlineArgs {
3294 protected:
3295 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003296
Igor Murashkin37743352014-11-13 14:38:00 -08003297 virtual ParseStatus ParseCustom(const StringPiece& option,
3298 std::string* error_msg) OVERRIDE {
3299 {
3300 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
3301 if (base_parse != kParseUnknownArgument) {
3302 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003303 }
3304 }
3305
Igor Murashkin37743352014-11-13 14:38:00 -08003306 if (option.starts_with("--oat-file=")) {
3307 oat_filename_ = option.substr(strlen("--oat-file=")).data();
3308 } else if (option.starts_with("--image=")) {
3309 image_location_ = option.substr(strlen("--image=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003310 } else if (option == "--no-dump:vmap") {
3311 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01003312 } else if (option =="--dump:code_info_stack_maps") {
3313 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003314 } else if (option == "--no-disassemble") {
3315 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003316 } else if (option =="--header-only") {
3317 dump_header_only_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003318 } else if (option.starts_with("--symbolize=")) {
3319 oat_filename_ = option.substr(strlen("--symbolize=")).data();
3320 symbolize_ = true;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003321 } else if (option.starts_with("--only-keep-debug")) {
3322 only_keep_debug_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003323 } else if (option.starts_with("--class-filter=")) {
3324 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003325 } else if (option.starts_with("--method-filter=")) {
3326 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003327 } else if (option.starts_with("--list-classes")) {
3328 list_classes_ = true;
3329 } else if (option.starts_with("--list-methods")) {
3330 list_methods_ = true;
3331 } else if (option.starts_with("--export-dex-to=")) {
3332 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
3333 } else if (option.starts_with("--addr2instr=")) {
3334 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
3335 *error_msg = "Address conversion failed";
3336 return kParseError;
3337 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003338 } else if (option.starts_with("--app-image=")) {
3339 app_image_ = option.substr(strlen("--app-image=")).data();
3340 } else if (option.starts_with("--app-oat=")) {
3341 app_oat_ = option.substr(strlen("--app-oat=")).data();
Andreas Gampe9fded872016-09-25 16:08:35 -07003342 } else if (option.starts_with("--dump-imt=")) {
3343 imt_dump_ = option.substr(strlen("--dump-imt=")).data();
3344 } else if (option == "--dump-imt-stats") {
3345 imt_stat_dump_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003346 } else {
3347 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003348 }
3349
Igor Murashkin37743352014-11-13 14:38:00 -08003350 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003351 }
3352
Igor Murashkin37743352014-11-13 14:38:00 -08003353 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
3354 // Infer boot image location from the image location if possible.
3355 if (boot_image_location_ == nullptr) {
3356 boot_image_location_ = image_location_;
3357 }
3358
3359 // Perform the parent checks.
3360 ParseStatus parent_checks = Base::ParseChecks(error_msg);
3361 if (parent_checks != kParseOk) {
3362 return parent_checks;
3363 }
3364
3365 // Perform our own checks.
3366 if (image_location_ == nullptr && oat_filename_ == nullptr) {
3367 *error_msg = "Either --image or --oat-file must be specified";
3368 return kParseError;
3369 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
3370 *error_msg = "Either --image or --oat-file must be specified but not both";
3371 return kParseError;
3372 }
3373
3374 return kParseOk;
3375 }
3376
3377 virtual std::string GetUsage() const {
3378 std::string usage;
3379
3380 usage +=
3381 "Usage: oatdump [options] ...\n"
3382 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
3383 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
3384 "\n"
3385 // Either oat-file or image is required.
3386 " --oat-file=<file.oat>: specifies an input oat filename.\n"
3387 " Example: --oat-file=/system/framework/boot.oat\n"
3388 "\n"
3389 " --image=<file.art>: specifies an input image location.\n"
3390 " Example: --image=/system/framework/boot.art\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003391 "\n"
3392 " --app-image=<file.art>: specifies an input app image. Must also have a specified\n"
3393 " boot image and app oat file.\n"
3394 " Example: --app-image=app.art\n"
3395 "\n"
3396 " --app-oat=<file.odex>: specifies an input app oat.\n"
3397 " Example: --app-oat=app.odex\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003398 "\n";
3399
3400 usage += Base::GetUsage();
3401
3402 usage += // Optional.
Igor Murashkin37743352014-11-13 14:38:00 -08003403 " --no-dump:vmap may be used to disable vmap dumping.\n"
3404 " Example: --no-dump:vmap\n"
3405 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01003406 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
3407 " Example: --dump:code_info_stack_maps\n"
3408 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003409 " --no-disassemble may be used to disable disassembly.\n"
3410 " Example: --no-disassemble\n"
3411 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00003412 " --header-only may be used to print only the oat header.\n"
3413 " Example: --header-only\n"
3414 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003415 " --list-classes may be used to list target file classes (can be used with filters).\n"
3416 " Example: --list-classes\n"
3417 " Example: --list-classes --class-filter=com.example.foo\n"
3418 "\n"
3419 " --list-methods may be used to list target file methods (can be used with filters).\n"
3420 " Example: --list-methods\n"
3421 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
3422 "\n"
3423 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
3424 " Example: --symbolize=/system/framework/boot.oat\n"
3425 "\n"
David Srbecky2fdd03c2016-03-10 15:32:37 +00003426 " --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
3427 " .rodata and .text sections are omitted in the output file to save space.\n"
3428 " Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
3429 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003430 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
3431 " Example: --class-filter=com.example.foo\n"
3432 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003433 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
3434 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003435 "\n"
3436 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
3437 " Example: --export-dex-to=/data/local/tmp\n"
3438 "\n"
3439 " --addr2instr=<address>: output matching method disassembled code from relative\n"
3440 " address (e.g. PC from crash dump)\n"
3441 " Example: --addr2instr=0x00001a3b\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003442 "\n"
3443 " --dump-imt=<file.txt>: output IMT collisions (if any) for the given receiver\n"
3444 " types and interface methods in the given file. The file\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003445 " is read line-wise, where each line should either be a class\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003446 " name or descriptor, or a class name/descriptor and a prefix\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003447 " of a complete method name (separated by a whitespace).\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003448 " Example: --dump-imt=imt.txt\n"
3449 "\n"
3450 " --dump-imt-stats: output IMT statistics for the given boot image\n"
3451 " Example: --dump-imt-stats"
Igor Murashkin37743352014-11-13 14:38:00 -08003452 "\n";
3453
3454 return usage;
3455 }
3456
3457 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07003458 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003459 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00003460 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07003461 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003462 std::string elf_filename_prefix_;
Andreas Gampe9fded872016-09-25 16:08:35 -07003463 std::string imt_dump_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003464 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01003465 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003466 bool disassemble_code_ = true;
3467 bool symbolize_ = false;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003468 bool only_keep_debug_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003469 bool list_classes_ = false;
3470 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003471 bool dump_header_only_ = false;
Andreas Gampe9fded872016-09-25 16:08:35 -07003472 bool imt_stat_dump_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003473 uint32_t addr2instr_ = 0;
3474 const char* export_dex_location_ = nullptr;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003475 const char* app_image_ = nullptr;
3476 const char* app_oat_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003477};
3478
Igor Murashkin37743352014-11-13 14:38:00 -08003479struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
3480 virtual bool NeedsRuntime() OVERRIDE {
3481 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003482
Igor Murashkin37743352014-11-13 14:38:00 -08003483 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
3484 bool absolute_addresses = (args_->oat_filename_ == nullptr);
3485
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003486 oat_dumper_options_.reset(new OatDumperOptions(
Igor Murashkin37743352014-11-13 14:38:00 -08003487 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01003488 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08003489 args_->disassemble_code_,
3490 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003491 args_->class_filter_,
3492 args_->method_filter_,
3493 args_->list_classes_,
3494 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00003495 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003496 args_->export_dex_location_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003497 args_->app_image_,
3498 args_->app_oat_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003499 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08003500
Andreas Gampe9fded872016-09-25 16:08:35 -07003501 return (args_->boot_image_location_ != nullptr ||
3502 args_->image_location_ != nullptr ||
3503 !args_->imt_dump_.empty()) &&
Igor Murashkin37743352014-11-13 14:38:00 -08003504 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003505 }
3506
Igor Murashkin37743352014-11-13 14:38:00 -08003507 virtual bool ExecuteWithoutRuntime() OVERRIDE {
3508 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08003509 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003510
Mathieu Chartierd424d082014-10-15 10:31:46 -07003511 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08003512
Andreas Gampec24f3992014-12-17 20:40:11 -08003513 if (args_->symbolize_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00003514 // ELF has special kind of section called SHT_NOBITS which allows us to create
3515 // sections which exist but their data is omitted from the ELF file to save space.
3516 // This is what "strip --only-keep-debug" does when it creates separate ELF file
3517 // with only debug data. We use it in similar way to exclude .rodata and .text.
3518 bool no_bits = args_->only_keep_debug_;
3519 return SymbolizeOat(args_->oat_filename_, args_->output_name_, no_bits) == EXIT_SUCCESS;
Andreas Gampec24f3992014-12-17 20:40:11 -08003520 } else {
3521 return DumpOat(nullptr,
3522 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003523 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08003524 args_->os_) == EXIT_SUCCESS;
3525 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07003526 }
3527
Igor Murashkin37743352014-11-13 14:38:00 -08003528 virtual bool ExecuteWithRuntime(Runtime* runtime) {
3529 CHECK(args_ != nullptr);
3530
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003531 if (!args_->imt_dump_.empty() || args_->imt_stat_dump_) {
3532 return IMTDumper::Dump(runtime,
3533 args_->imt_dump_,
3534 args_->imt_stat_dump_,
3535 args_->oat_filename_);
Andreas Gampe9fded872016-09-25 16:08:35 -07003536 }
3537
Igor Murashkin37743352014-11-13 14:38:00 -08003538 if (args_->oat_filename_ != nullptr) {
3539 return DumpOat(runtime,
3540 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003541 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08003542 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003543 }
Igor Murashkin37743352014-11-13 14:38:00 -08003544
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003545 return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003546 }
3547
Igor Murashkin37743352014-11-13 14:38:00 -08003548 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
3549};
Andreas Gampe00b25f32014-09-17 21:49:05 -07003550
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003551} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07003552
3553int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08003554 art::OatdumpMain main;
3555 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07003556}