blob: f3f418d32f24b3bcb5b2cef02ca595a21b9ed17e [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
Ian Rogersd582fa42014-11-05 23:46:43 -080029#include "arch/instruction_set_features.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070030#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070031#include "art_method-inl.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000032#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080033#include "base/unix_file/fd_file.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070034#include "class_linker.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035#include "class_linker-inl.h"
David Srbecky5d950762016-03-07 20:47:29 +000036#include "debug/elf_debug_writer.h"
37#include "debug/method_debug_info.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070038#include "dex_file-inl.h"
Christina Wadsworthbc233ac2016-06-20 15:01:32 -070039#include "dex_instruction-inl.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080040#include "disassembler.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070041#include "elf_builder.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070042#include "gc/space/image_space.h"
43#include "gc/space/large_object_space.h"
44#include "gc/space/space-inl.h"
Mathieu Chartier4a26f172016-01-26 14:26:18 -080045#include "image-inl.h"
Andreas Gampe9fded872016-09-25 16:08:35 -070046#include "imtable-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080047#include "indenter.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000048#include "linker/buffered_output_stream.h"
49#include "linker/file_output_stream.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080050#include "mirror/array-inl.h"
51#include "mirror/class-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010052#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080053#include "mirror/object-inl.h"
54#include "mirror/object_array-inl.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080055#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010056#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070057#include "oat_file_manager.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080058#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070059#include "safe_map.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070060#include "scoped_thread_state_change-inl.h"
Nicolas Geoffray9c055782016-07-14 09:24:30 +000061#include "ScopedLocalRef.h"
Mathieu Chartierdc00f182016-07-14 10:10:44 -070062#include "stack_map.h"
63#include "string_reference.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080064#include "thread_list.h"
Andreas Gampe2ba88952016-04-29 17:52:07 -070065#include "type_lookup_table.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080066#include "verifier/method_verifier.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070067#include "well_known_classes.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070068
Igor Murashkin37743352014-11-13 14:38:00 -080069#include <sys/stat.h>
70#include "cmdline.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070071
Igor Murashkin37743352014-11-13 14:38:00 -080072namespace art {
Brian Carlstrom78128a62011-09-15 17:21:19 -070073
Mathieu Chartiere401d142015-04-22 13:56:20 -070074const char* image_methods_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -080075 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -070076 "kImtConflictMethod",
Mathieu Chartier2d2621a2014-10-23 16:48:06 -070077 "kImtUnimplementedMethod",
Vladimir Markofd36f1f2016-08-03 18:49:58 +010078 "kSaveAllCalleeSavesMethod",
79 "kSaveRefsOnlyMethod",
80 "kSaveRefsAndArgsMethod",
Vladimir Marko952dbb12016-07-28 12:01:51 +010081 "kSaveEverythingMethod",
Mathieu Chartiere401d142015-04-22 13:56:20 -070082};
83
84const char* image_roots_descriptions_[] = {
Brian Carlstrom58ae9412011-10-04 00:56:06 -070085 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070086 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070087};
88
Mathieu Chartierac8f4392015-08-27 13:54:20 -070089// Map is so that we don't allocate multiple dex files for the same OatDexFile.
90static std::map<const OatFile::OatDexFile*,
91 std::unique_ptr<const DexFile>> opened_dex_files;
92
93const DexFile* OpenDexFile(const OatFile::OatDexFile* oat_dex_file, std::string* error_msg) {
94 DCHECK(oat_dex_file != nullptr);
95 auto it = opened_dex_files.find(oat_dex_file);
96 if (it != opened_dex_files.end()) {
97 return it->second.get();
98 }
99 const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
100 opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
101 return ret;
102}
103
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800104template <typename ElfTypes>
David Srbeckybc90fd02015-04-22 19:40:27 +0100105class OatSymbolizer FINAL {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700106 public:
David Srbecky2fdd03c2016-03-10 15:32:37 +0000107 OatSymbolizer(const OatFile* oat_file, const std::string& output_name, bool no_bits) :
108 oat_file_(oat_file),
109 builder_(nullptr),
110 output_name_(output_name.empty() ? "symbolized.oat" : output_name),
111 no_bits_(no_bits) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700112 }
113
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700114 bool Symbolize() {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000115 const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
David Srbecky5d811202016-03-08 13:21:22 +0000116 const InstructionSetFeatures* features = InstructionSetFeatures::FromBitmap(
117 isa, oat_file_->GetOatHeader().GetInstructionSetFeaturesBitmap());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000118
119 File* elf_file = OS::CreateEmptyFile(output_name_.c_str());
120 std::unique_ptr<BufferedOutputStream> output_stream(
Vladimir Marko10c13562015-11-25 14:33:36 +0000121 MakeUnique<BufferedOutputStream>(MakeUnique<FileOutputStream>(elf_file)));
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800122 builder_.reset(new ElfBuilder<ElfTypes>(isa, features, output_stream.get()));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000123
124 builder_->Start();
125
126 auto* rodata = builder_->GetRoData();
127 auto* text = builder_->GetText();
128 auto* bss = builder_->GetBss();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000129
David Srbecky6d8c8f02015-10-26 10:57:09 +0000130 const uint8_t* rodata_begin = oat_file_->Begin();
131 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
David Srbecky2fdd03c2016-03-10 15:32:37 +0000132 if (no_bits_) {
133 rodata->WriteNoBitsSection(rodata_size);
134 } else {
135 rodata->Start();
136 rodata->WriteFully(rodata_begin, rodata_size);
137 rodata->End();
138 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000139
David Srbecky6d8c8f02015-10-26 10:57:09 +0000140 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
141 const size_t text_size = oat_file_->End() - text_begin;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000142 if (no_bits_) {
143 text->WriteNoBitsSection(text_size);
144 } else {
145 text->Start();
146 text->WriteFully(text_begin, text_size);
147 text->End();
148 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000149
150 if (oat_file_->BssSize() != 0) {
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000151 bss->WriteNoBitsSection(oat_file_->BssSize());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000152 }
153
Douglas Leung316a2182015-09-17 15:26:25 -0700154 if (isa == kMips || isa == kMips64) {
155 builder_->WriteMIPSabiflagsSection();
156 }
Vladimir Marko944da602016-02-19 12:27:55 +0000157 builder_->PrepareDynamicSection(
158 elf_file->GetPath(), rodata_size, text_size, oat_file_->BssSize());
159 builder_->WriteDynamicSection();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700160
David Srbecky5d950762016-03-07 20:47:29 +0000161 Walk();
162 for (const auto& trampoline : debug::MakeTrampolineInfos(oat_file_->GetOatHeader())) {
163 method_debug_infos_.push_back(trampoline);
164 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700165
David Srbecky5d950762016-03-07 20:47:29 +0000166 debug::WriteDebugInfo(builder_.get(),
167 ArrayRef<const debug::MethodDebugInfo>(method_debug_infos_),
168 dwarf::DW_DEBUG_FRAME_FORMAT,
169 true /* write_oat_patches */);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700170
David Srbecky6d8c8f02015-10-26 10:57:09 +0000171 builder_->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700172
Vladimir Marko10c13562015-11-25 14:33:36 +0000173 return builder_->Good();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700174 }
175
David Srbecky5d950762016-03-07 20:47:29 +0000176 void Walk() {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700177 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
178 for (size_t i = 0; i < oat_dex_files.size(); i++) {
179 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700180 CHECK(oat_dex_file != nullptr);
David Srbecky5d950762016-03-07 20:47:29 +0000181 WalkOatDexFile(oat_dex_file);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700182 }
183 }
184
David Srbecky5d950762016-03-07 20:47:29 +0000185 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700186 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700187 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
188 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700189 return;
190 }
191 for (size_t class_def_index = 0;
192 class_def_index < dex_file->NumClassDefs();
193 class_def_index++) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700194 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
195 OatClassType type = oat_class.GetType();
196 switch (type) {
197 case kOatClassAllCompiled:
198 case kOatClassSomeCompiled:
David Srbecky5d950762016-03-07 20:47:29 +0000199 WalkOatClass(oat_class, *dex_file, class_def_index);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700200 break;
201
202 case kOatClassNoneCompiled:
203 case kOatClassMax:
204 // Ignore.
205 break;
206 }
207 }
208 }
209
David Srbecky5d950762016-03-07 20:47:29 +0000210 void WalkOatClass(const OatFile::OatClass& oat_class,
211 const DexFile& dex_file,
212 uint32_t class_def_index) {
213 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700214 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700215 if (class_data == nullptr) { // empty class such as a marker interface?
216 return;
217 }
218 // Note: even if this is an interface or a native class, we still have to walk it, as there
219 // might be a static initializer.
220 ClassDataItemIterator it(dex_file, class_data);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700221 uint32_t class_method_idx = 0;
David Srbecky5d950762016-03-07 20:47:29 +0000222 for (; it.HasNextStaticField(); it.Next()) { /* skip */ }
223 for (; it.HasNextInstanceField(); it.Next()) { /* skip */ }
224 for (; it.HasNextDirectMethod() || it.HasNextVirtualMethod(); it.Next()) {
225 WalkOatMethod(oat_class.GetOatMethod(class_method_idx++),
226 dex_file,
227 class_def_index,
228 it.GetMemberIndex(),
229 it.GetMethodCodeItem(),
230 it.GetMethodAccessFlags());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700231 }
232 DCHECK(!it.HasNext());
233 }
234
David Srbecky5d950762016-03-07 20:47:29 +0000235 void WalkOatMethod(const OatFile::OatMethod& oat_method,
236 const DexFile& dex_file,
237 uint32_t class_def_index,
238 uint32_t dex_method_index,
239 const DexFile::CodeItem* code_item,
240 uint32_t method_access_flags) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700241 if ((method_access_flags & kAccAbstract) != 0) {
242 // Abstract method, no code.
243 return;
244 }
David Srbecky5d950762016-03-07 20:47:29 +0000245 const OatHeader& oat_header = oat_file_->GetOatHeader();
246 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
247 if (method_header == nullptr || method_header->GetCodeSize() == 0) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700248 // No code.
249 return;
250 }
251
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100252 uint32_t entry_point = oat_method.GetCodeOffset() - oat_header.GetExecutableOffset();
253 // Clear Thumb2 bit.
254 const void* code_address = EntryPointToCodePointer(reinterpret_cast<void*>(entry_point));
255
David Srbecky5d950762016-03-07 20:47:29 +0000256 debug::MethodDebugInfo info = debug::MethodDebugInfo();
257 info.trampoline_name = nullptr;
258 info.dex_file = &dex_file;
259 info.class_def_index = class_def_index;
260 info.dex_method_index = dex_method_index;
261 info.access_flags = method_access_flags;
262 info.code_item = code_item;
263 info.isa = oat_header.GetInstructionSet();
264 info.deduped = !seen_offsets_.insert(oat_method.GetCodeOffset()).second;
265 info.is_native_debuggable = oat_header.IsNativeDebuggable();
266 info.is_optimized = method_header->IsOptimized();
267 info.is_code_address_text_relative = true;
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100268 info.code_address = reinterpret_cast<uintptr_t>(code_address);
David Srbecky5d950762016-03-07 20:47:29 +0000269 info.code_size = method_header->GetCodeSize();
270 info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
271 info.code_info = info.is_optimized ? method_header->GetOptimizedCodeInfoPtr() : nullptr;
272 info.cfi = ArrayRef<uint8_t>();
273 method_debug_infos_.push_back(info);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700274 }
275
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700276 private:
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700277 const OatFile* oat_file_;
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800278 std::unique_ptr<ElfBuilder<ElfTypes> > builder_;
David Srbecky5d950762016-03-07 20:47:29 +0000279 std::vector<debug::MethodDebugInfo> method_debug_infos_;
280 std::unordered_set<uint32_t> seen_offsets_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700281 const std::string output_name_;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000282 bool no_bits_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700283};
284
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700285class OatDumperOptions {
286 public:
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100287 OatDumperOptions(bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100288 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700289 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700290 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800291 const char* class_filter,
292 const char* method_filter,
293 bool list_classes,
294 bool list_methods,
David Brazdilc03d7b62016-03-02 12:18:03 +0000295 bool dump_header_only,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800296 const char* export_dex_location,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800297 const char* app_image,
298 const char* app_oat,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800299 uint32_t addr2instr)
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100300 : dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100301 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700302 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700303 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800304 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000305 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800306 list_classes_(list_classes),
307 list_methods_(list_methods),
David Brazdilc03d7b62016-03-02 12:18:03 +0000308 dump_header_only_(dump_header_only),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800309 export_dex_location_(export_dex_location),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800310 app_image_(app_image),
311 app_oat_(app_oat),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800312 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800313 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700314
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700315 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100316 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700317 const bool disassemble_code_;
318 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800319 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000320 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800321 const bool list_classes_;
322 const bool list_methods_;
David Brazdilc03d7b62016-03-02 12:18:03 +0000323 const bool dump_header_only_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800324 const char* const export_dex_location_;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800325 const char* const app_image_;
326 const char* const app_oat_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800327 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700328 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700329};
330
Elliott Hughese3c845c2012-02-28 17:23:01 -0800331class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700332 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100333 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000334 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800335 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700336 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800337 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800338 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
339 disassembler_(Disassembler::Create(instruction_set_,
Andreas Gampe372f3a32016-08-19 10:49:06 -0700340 new DisassemblerOptions(
341 options_.absolute_addresses_,
342 oat_file.Begin(),
343 oat_file.End(),
344 true /* can_read_literals_ */,
345 Is64BitInstructionSet(instruction_set_)
346 ? &Thread::DumpThreadOffset<PointerSize::k64>
347 : &Thread::DumpThreadOffset<PointerSize::k32>))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800348 CHECK(options_.class_loader_ != nullptr);
349 CHECK(options_.class_filter_ != nullptr);
350 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800351 AddAllOffsets();
352 }
353
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700354 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700355 delete disassembler_;
356 }
357
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800358 InstructionSet GetInstructionSet() {
359 return instruction_set_;
360 }
361
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700362 bool Dump(std::ostream& os) {
363 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800364 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700365
366 os << "MAGIC:\n";
367 os << oat_header.GetMagic() << "\n\n";
368
Jeff Hao4b07a6e2016-01-15 12:50:44 -0800369 os << "LOCATION:\n";
370 os << oat_file_.GetLocation() << "\n\n";
371
Brian Carlstromaded5f72011-10-07 17:15:04 -0700372 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800373 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700374
Elliott Hughesa72ec822012-03-05 17:12:22 -0800375 os << "INSTRUCTION SET:\n";
376 os << oat_header.GetInstructionSet() << "\n\n";
377
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700378 {
379 std::unique_ptr<const InstructionSetFeatures> features(
380 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
381 oat_header.GetInstructionSetFeaturesBitmap()));
382 os << "INSTRUCTION SET FEATURES:\n";
383 os << features->GetFeatureString() << "\n\n";
384 }
Dave Allison70202782013-10-22 17:52:19 -0700385
Brian Carlstromaded5f72011-10-07 17:15:04 -0700386 os << "DEX FILE COUNT:\n";
387 os << oat_header.GetDexFileCount() << "\n\n";
388
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800389#define DUMP_OAT_HEADER_OFFSET(label, offset) \
390 os << label " OFFSET:\n"; \
391 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800392 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800393 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
394 } \
395 os << StringPrintf("\n\n");
396
397 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
398 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
399 GetInterpreterToInterpreterBridgeOffset);
400 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
401 GetInterpreterToCompiledCodeBridgeOffset);
402 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
403 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800404 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
405 GetQuickGenericJniTrampolineOffset);
406 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
407 GetQuickImtConflictTrampolineOffset);
408 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
409 GetQuickResolutionTrampolineOffset);
410 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
411 GetQuickToInterpreterBridgeOffset);
412#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700413
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700414 os << "IMAGE PATCH DELTA:\n";
415 os << StringPrintf("%d (0x%08x)\n\n",
416 oat_header.GetImagePatchDelta(),
417 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700418
Brian Carlstrom28db0122012-10-18 16:20:41 -0700419 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
420 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
421
422 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800423 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700424
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700425 // Print the key-value store.
426 {
427 os << "KEY VALUE STORE:\n";
428 size_t index = 0;
429 const char* key;
430 const char* value;
431 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
432 os << key << " = " << value << "\n";
433 index++;
434 }
435 os << "\n";
436 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700437
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800438 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700439 os << "BEGIN:\n";
440 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700441
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700442 os << "END:\n";
443 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
444 }
445
446 os << "SIZE:\n";
447 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700448
449 os << std::flush;
450
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800451 // If set, adjust relative address to be searched
452 if (options_.addr2instr_ != 0) {
453 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
454 os << "SEARCH ADDRESS (executable offset + input):\n";
455 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
456 }
457
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700458 // Dumping the dex file overview is compact enough to do even if header only.
459 DexFileData cumulative;
460 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
461 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
462 CHECK(oat_dex_file != nullptr);
463 std::string error_msg;
464 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
465 if (dex_file == nullptr) {
466 os << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation() << "': "
467 << error_msg;
468 continue;
469 }
470 DexFileData data(*dex_file);
471 os << "Dex file data for " << dex_file->GetLocation() << "\n";
472 data.Dump(os);
473 os << "\n";
474 cumulative.Add(data);
475 }
476 os << "Cumulative dex file data\n";
477 cumulative.Dump(os);
478 os << "\n";
479
David Brazdilc03d7b62016-03-02 12:18:03 +0000480 if (!options_.dump_header_only_) {
481 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
482 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
483 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800484
David Brazdilc03d7b62016-03-02 12:18:03 +0000485 // If file export selected skip file analysis
486 if (options_.export_dex_location_) {
487 if (!ExportDexFile(os, *oat_dex_file)) {
488 success = false;
489 }
490 } else {
491 if (!DumpOatDexFile(os, *oat_dex_file)) {
492 success = false;
493 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800494 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700495 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700496 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000497
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700498 os << std::flush;
499 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700500 }
501
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800502 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700503 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
504 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800505 return 0; // Address not in oat file
506 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800507 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
508 reinterpret_cast<uintptr_t>(oat_file_.Begin());
509 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800510 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800511 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800512 return end_offset - begin_offset;
513 }
514
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800515 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700516 return oat_file_.GetOatHeader().GetInstructionSet();
517 }
518
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700519 const void* GetQuickOatCode(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800520 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
521 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700522 CHECK(oat_dex_file != nullptr);
523 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700524 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
525 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700526 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
527 << "': " << error_msg;
528 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800529 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700530 const DexFile::ClassDef* class_def =
David Sehr9aa352e2016-09-15 18:13:52 -0700531 OatDexFile::FindClassDef(*dex_file, descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700532 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700533 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100534 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800535 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100536 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800537 }
538 }
539 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700540 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800541 }
542
Brian Carlstromaded5f72011-10-07 17:15:04 -0700543 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800544 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800545 // We don't know the length of the code for each method, but we need to know where to stop
546 // when disassembling. What we do know is that a region of code will be followed by some other
547 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
548 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800549 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
550 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700551 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700552 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700553 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
554 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700555 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
556 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800557 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800558 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800559 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800560 for (size_t class_def_index = 0;
561 class_def_index < dex_file->NumClassDefs();
562 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800563 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100564 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700565 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700566 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800567 ClassDataItemIterator it(*dex_file, class_data);
568 SkipAllFields(it);
569 uint32_t class_method_index = 0;
570 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100571 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800572 it.Next();
573 }
574 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100575 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800576 it.Next();
577 }
578 }
579 }
580 }
581
582 // If the last thing in the file is code for a method, there won't be an offset for the "next"
583 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
584 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800585 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800586 }
587
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700588 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
589 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
590 }
591
Elliott Hughese3c845c2012-02-28 17:23:01 -0800592 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800593 uint32_t code_offset = oat_method.GetCodeOffset();
594 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
595 code_offset &= ~0x1;
596 }
597 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800598 offsets_.insert(oat_method.GetVmapTableOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800599 }
600
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700601 // Dex file data, may be for multiple different dex files.
602 class DexFileData {
603 public:
604 DexFileData() {}
605
606 explicit DexFileData(const DexFile& dex_file)
607 : num_string_ids_(dex_file.NumStringIds()),
608 num_method_ids_(dex_file.NumMethodIds()),
609 num_field_ids_(dex_file.NumFieldIds()),
610 num_type_ids_(dex_file.NumTypeIds()),
611 num_class_defs_(dex_file.NumClassDefs()) {
612 for (size_t class_def_index = 0; class_def_index < num_class_defs_; ++class_def_index) {
613 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
614 WalkClass(dex_file, class_def);
615 }
616 }
617
618 void Add(const DexFileData& other) {
619 AddAll(unique_string_ids_from_code_, other.unique_string_ids_from_code_);
620 num_string_ids_from_code_ += other.num_string_ids_from_code_;
621 AddAll(dex_code_item_ptrs_, other.dex_code_item_ptrs_);
622 dex_code_bytes_ += other.dex_code_bytes_;
623 num_string_ids_ += other.num_string_ids_;
624 num_method_ids_ += other.num_method_ids_;
625 num_field_ids_ += other.num_field_ids_;
626 num_type_ids_ += other.num_type_ids_;
627 num_class_defs_ += other.num_class_defs_;
628 }
629
630 void Dump(std::ostream& os) {
631 os << "Num string ids: " << num_string_ids_ << "\n";
632 os << "Num method ids: " << num_method_ids_ << "\n";
633 os << "Num field ids: " << num_field_ids_ << "\n";
634 os << "Num type ids: " << num_type_ids_ << "\n";
635 os << "Num class defs: " << num_class_defs_ << "\n";
636 os << "Unique strings loaded from dex code: " << unique_string_ids_from_code_.size() << "\n";
637 os << "Total strings loaded from dex code: " << num_string_ids_from_code_ << "\n";
638 os << "Number of unique dex code items: " << dex_code_item_ptrs_.size() << "\n";
639 os << "Total number of dex code bytes: " << dex_code_bytes_ << "\n";
640 }
641
642 private:
643 void WalkClass(const DexFile& dex_file, const DexFile::ClassDef& class_def) {
644 const uint8_t* class_data = dex_file.GetClassData(class_def);
645 if (class_data == nullptr) { // empty class such as a marker interface?
646 return;
647 }
648 ClassDataItemIterator it(dex_file, class_data);
649 SkipAllFields(it);
650 while (it.HasNextDirectMethod()) {
651 WalkCodeItem(dex_file, it.GetMethodCodeItem());
652 it.Next();
653 }
654 while (it.HasNextVirtualMethod()) {
655 WalkCodeItem(dex_file, it.GetMethodCodeItem());
656 it.Next();
657 }
658 DCHECK(!it.HasNext());
659 }
660
661 void WalkCodeItem(const DexFile& dex_file, const DexFile::CodeItem* code_item) {
662 if (code_item == nullptr) {
663 return;
664 }
665 const size_t code_item_size = code_item->insns_size_in_code_units_;
666 const uint16_t* code_ptr = code_item->insns_;
667 const uint16_t* code_end = code_item->insns_ + code_item_size;
668
669 // If we inserted a new dex code item pointer, add to total code bytes.
670 if (dex_code_item_ptrs_.insert(code_ptr).second) {
671 dex_code_bytes_ += code_item_size * sizeof(code_ptr[0]);
672 }
673
674 while (code_ptr < code_end) {
675 const Instruction* inst = Instruction::At(code_ptr);
676 switch (inst->Opcode()) {
677 case Instruction::CONST_STRING: {
678 const uint32_t string_index = inst->VRegB_21c();
679 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
680 ++num_string_ids_from_code_;
681 break;
682 }
683 case Instruction::CONST_STRING_JUMBO: {
684 const uint32_t string_index = inst->VRegB_31c();
685 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
686 ++num_string_ids_from_code_;
687 break;
688 }
689 default:
690 break;
691 }
692
693 code_ptr += inst->SizeInCodeUnits();
694 }
695 }
696
697 // Unique string ids loaded from dex code.
698 std::set<StringReference, StringReferenceComparator> unique_string_ids_from_code_;
699
700 // Total string ids loaded from dex code.
701 size_t num_string_ids_from_code_ = 0;
702
703 // Unique code pointers.
704 std::set<const void*> dex_code_item_ptrs_;
705
706 // Total "unique" dex code bytes.
707 size_t dex_code_bytes_ = 0;
708
709 // Other dex ids.
710 size_t num_string_ids_ = 0;
711 size_t num_method_ids_ = 0;
712 size_t num_field_ids_ = 0;
713 size_t num_type_ids_ = 0;
714 size_t num_class_defs_ = 0;
715 };
716
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700717 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
718 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800719 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700720 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800721 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800722 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700723
Andreas Gampe2ba88952016-04-29 17:52:07 -0700724 const uint8_t* const oat_file_begin = oat_dex_file.GetOatFile()->Begin();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100725 const uint8_t* const vdex_file_begin = oat_dex_file.GetOatFile()->DexBegin();
726
727 // Print data range of the dex file embedded inside the corresponding vdex file.
Andreas Gampe2ba88952016-04-29 17:52:07 -0700728 const uint8_t* const dex_file_pointer = oat_dex_file.GetDexFilePointer();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100729 uint32_t dex_offset = dchecked_integral_cast<uint32_t>(dex_file_pointer - vdex_file_begin);
Andreas Gampe2ba88952016-04-29 17:52:07 -0700730 os << StringPrintf("dex-file: 0x%08x..0x%08x\n",
731 dex_offset,
732 dchecked_integral_cast<uint32_t>(dex_offset + oat_dex_file.FileSize() - 1));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700733
Andreas Gampe2ba88952016-04-29 17:52:07 -0700734 // Create the dex file early. A lot of print-out things depend on it.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700735 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700736 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
737 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700738 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700739 os << std::flush;
740 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700741 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100742
Andreas Gampe2ba88952016-04-29 17:52:07 -0700743 // Print lookup table, if it exists.
744 if (oat_dex_file.GetLookupTableData() != nullptr) {
745 uint32_t table_offset = dchecked_integral_cast<uint32_t>(
746 oat_dex_file.GetLookupTableData() - oat_file_begin);
David Sehr9aa352e2016-09-15 18:13:52 -0700747 uint32_t table_size = TypeLookupTable::RawDataLength(dex_file->NumClassDefs());
Andreas Gampe2ba88952016-04-29 17:52:07 -0700748 os << StringPrintf("type-table: 0x%08x..0x%08x\n",
749 table_offset,
750 table_offset + table_size - 1);
751 }
752
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100753 VariableIndentationOutputStream vios(&os);
754 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800755 for (size_t class_def_index = 0;
756 class_def_index < dex_file->NumClassDefs();
757 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700758 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
759 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800760
761 // TODO: Support regex
762 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
763 continue;
764 }
765
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700766 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100767 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700768 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
769 class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100770 << " (" << oat_class.GetStatus() << ")"
771 << " (" << oat_class.GetType() << ")\n";
772 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700773 if (options_.list_classes_) {
774 continue;
775 }
776 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700777 success = false;
778 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800779 if (stop_analysis) {
780 os << std::flush;
781 return success;
782 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700783 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700784 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700785 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700786 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700787 }
788
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800789 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
790 std::string error_msg;
791 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
792
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700793 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800794 if (dex_file == nullptr) {
795 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
796 return false;
797 }
798 size_t fsize = oat_dex_file.FileSize();
799
800 // Some quick checks just in case
801 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
802 os << "Invalid dex file\n";
803 return false;
804 }
805
806 // Verify output directory exists
807 if (!OS::DirectoryExists(options_.export_dex_location_)) {
808 // TODO: Extend OS::DirectoryExists if symlink support is required
809 os << options_.export_dex_location_ << " output directory not found or symlink\n";
810 return false;
811 }
812
813 // Beautify path names
814 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
815 return false;
816 }
817
818 std::string dex_orig_name;
819 size_t dex_orig_pos = dex_file_location.rfind('/');
820 if (dex_orig_pos == std::string::npos)
821 dex_orig_name = dex_file_location;
822 else
823 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
824
825 // A more elegant approach to efficiently name user installed apps is welcome
826 if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
827 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
828 size_t apk_orig_pos = dex_file_location.rfind('/');
829 if (apk_orig_pos != std::string::npos) {
830 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
831 }
832 }
833
834 std::string out_dex_path(options_.export_dex_location_);
835 if (out_dex_path.back() != '/') {
836 out_dex_path.append("/");
837 }
838 out_dex_path.append(dex_orig_name);
839 out_dex_path.append("_export.dex");
840 if (out_dex_path.length() > PATH_MAX) {
841 return false;
842 }
843
844 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
845 if (file.get() == nullptr) {
846 os << "Failed to open output dex file " << out_dex_path;
847 return false;
848 }
849
850 if (!file->WriteFully(dex_file->Begin(), fsize)) {
851 os << "Failed to write dex file";
852 file->Erase();
853 return false;
854 }
855
856 if (file->FlushCloseOrErase() != 0) {
857 os << "Flush and close failed";
858 return false;
859 }
860
861 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
862 os << std::flush;
863
864 return true;
865 }
866
Elliott Hughese3c845c2012-02-28 17:23:01 -0800867 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700868 while (it.HasNextStaticField()) {
869 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700870 }
Ian Rogers0571d352011-11-03 19:51:38 -0700871 while (it.HasNextInstanceField()) {
872 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700873 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800874 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700875
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100876 bool DumpOatClass(VariableIndentationOutputStream* vios,
877 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700878 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700879 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800880 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -0700881 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700882 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100883 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700884 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800885 }
886 ClassDataItemIterator it(dex_file, class_data);
887 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700888 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700889 while (it.HasNextDirectMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100890 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700891 it.GetMemberIndex(), it.GetMethodCodeItem(),
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700892 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700893 success = false;
894 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800895 if (addr_found) {
896 *stop_analysis = true;
897 return success;
898 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700899 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700900 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700901 }
Ian Rogers0571d352011-11-03 19:51:38 -0700902 while (it.HasNextVirtualMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100903 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700904 it.GetMemberIndex(), it.GetMethodCodeItem(),
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700905 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700906 success = false;
907 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800908 if (addr_found) {
909 *stop_analysis = true;
910 return success;
911 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700912 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700913 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700914 }
Ian Rogers0571d352011-11-03 19:51:38 -0700915 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100916 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700917 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700918 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800919
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700920 static constexpr uint32_t kPrologueBytes = 16;
921
922 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
923 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
924
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100925 bool DumpOatMethod(VariableIndentationOutputStream* vios,
926 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700927 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700928 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800929 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700930 uint32_t method_access_flags, bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700931 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800932
933 // TODO: Support regex
934 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
935 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000936 return success;
937 }
938
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800939 std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100940 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
941 class_method_index, pretty_method.c_str(),
942 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800943 if (options_.list_methods_) return success;
944
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800945 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
946 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
947 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
948 uint32_t code_offset = oat_method.GetCodeOffset();
949 uint32_t code_size = oat_method.GetQuickCodeSize();
950 if (resolved_addr2instr_ != 0) {
951 if (resolved_addr2instr_ > code_offset + code_size) {
952 return success;
953 } else {
954 *addr_found = true; // stop analyzing file at next iteration
955 }
956 }
957
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100958 // Everything below is indented at least once.
959 ScopedIndentation indent1(vios);
960
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800961 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100962 vios->Stream() << "DEX CODE:\n";
963 ScopedIndentation indent2(vios);
964 DumpDexCode(vios->Stream(), dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700965 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700966
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700967 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700968 std::unique_ptr<verifier::MethodVerifier> verifier;
969 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700970 // We need to have the handle scope stay live until after the verifier since the verifier has
971 // a handle to the dex cache from hs.
972 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100973 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
974 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700975 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100976 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700977 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -0700978 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800979 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100980 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800981 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100982 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100983 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100984 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700985 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100986 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700987 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
988 oat_method_offsets_offset, oat_file_.Size());
989 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100990 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700991 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800992 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700993
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100994 ScopedIndentation indent2(vios);
995 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700996 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
997 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100998 vios->Stream() << StringPrintf("WARNING: "
999 "code offset 0x%08x is past end of file 0x%08zx.\n",
1000 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001001 success = false;
1002 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001003 vios->Stream() << "\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001004 }
1005 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001006 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001007 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
1008 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -07001009
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001010 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001011 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001012 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001013 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001014 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001015 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001016 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
1017 method_header_offset, oat_file_.Size());
1018 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001019 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001020 return false;
1021 }
1022
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001023 ScopedIndentation indent2(vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001024 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001025 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001026 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001027 }
1028 uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001029 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001030 if (vmap_table_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001031 vios->Stream() << StringPrintf("WARNING: "
1032 "vmap table offset 0x%08x is past end of file 0x%08zx. "
1033 "vmap table offset was loaded from offset 0x%08x.\n",
1034 vmap_table_offset, oat_file_.Size(),
1035 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001036 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001037 } else if (options_.dump_vmap_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001038 DumpVmapData(vios, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001039 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001040 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001041 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001042 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001043
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001044 ScopedIndentation indent2(vios);
1045 vios->Stream()
1046 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
1047 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
1048 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
1049 vios->Stream() << "\n";
1050 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
1051 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
1052 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001053 }
1054 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001055 // Based on spill masks from QuickMethodFrameInfo so placed
1056 // after it is dumped, but useful for understanding quick
1057 // code, so dumped here.
1058 ScopedIndentation indent2(vios);
1059 DumpVregLocations(vios->Stream(), oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001060 }
1061 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001062 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001063 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
1064 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001065 ScopedIndentation indent2(vios);
1066 vios->Stream() << StringPrintf("WARNING: "
1067 "code size offset 0x%08x is past end of file 0x%08zx.",
1068 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001069 success = false;
1070 } else {
1071 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001072 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1073 uint64_t aligned_code_end = aligned_code_begin + code_size;
1074
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001075 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001076 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001077 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001078 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
1079 code_offset,
1080 code_size_offset,
1081 code_size,
1082 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001083
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001084 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001085 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001086 vios->Stream() << StringPrintf("WARNING: "
1087 "start of code at 0x%08x is past end of file 0x%08zx.",
1088 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001089 success = false;
1090 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001091 vios->Stream() << StringPrintf(
1092 "WARNING: "
1093 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
1094 "code size is 0x%08x loaded from offset 0x%08x.\n",
1095 aligned_code_end, oat_file_.Size(),
1096 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001097 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001098 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001099 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001100 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001101 }
1102 }
1103 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001104 vios->Stream() << StringPrintf(
1105 "WARNING: "
1106 "code size %d is bigger than max expected threshold of %d. "
1107 "code size is 0x%08x loaded from offset 0x%08x.\n",
1108 code_size, kMaxCodeSize,
1109 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001110 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001111 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001112 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001113 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001114 }
1115 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001116 } else if (options_.disassemble_code_) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001117 DumpCode(vios, oat_method, code_item, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001118 }
1119 }
1120 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001121 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001122 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001123 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001124
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001125 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1126 if (spill_mask == 0) {
1127 return;
1128 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001129 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001130 for (size_t i = 0; i < 32; i++) {
1131 if ((spill_mask & (1 << i)) != 0) {
1132 if (is_float) {
1133 os << "fr" << i;
1134 } else {
1135 os << "r" << i;
1136 }
1137 spill_mask ^= 1 << i; // clear bit
1138 if (spill_mask != 0) {
1139 os << ", ";
1140 } else {
1141 break;
1142 }
1143 }
1144 }
1145 os << ")";
1146 }
1147
Roland Levillain442b46a2015-02-18 16:54:21 +00001148 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001149 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001150 const OatFile::OatMethod& oat_method,
1151 const DexFile::CodeItem* code_item) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001152 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1153 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001154 const void* raw_code_info = oat_method.GetVmapTable();
1155 if (raw_code_info != nullptr) {
1156 CodeInfo code_info(raw_code_info);
1157 DCHECK(code_item != nullptr);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001158 ScopedIndentation indent1(vios);
1159 DumpCodeInfo(vios, code_info, oat_method, *code_item);
Roland Levillain442b46a2015-02-18 16:54:21 +00001160 }
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001161 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
1162 // We don't encode the size in the table, so just emit that we have quickened
1163 // information.
1164 ScopedIndentation indent(vios);
1165 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001166 } else {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001167 // Otherwise, there is nothing to display.
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001168 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001169 }
1170
1171 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001172 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001173 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001174 const OatFile::OatMethod& oat_method,
Roland Levillain442b46a2015-02-18 16:54:21 +00001175 const DexFile::CodeItem& code_item) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001176 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001177 oat_method.GetCodeOffset(),
1178 code_item.registers_size_,
1179 options_.dump_code_info_stack_maps_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001180 }
1181
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001182 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1183 const DexFile::CodeItem* code_item) {
1184 if (code_item != nullptr) {
1185 size_t num_locals_ins = code_item->registers_size_;
1186 size_t num_ins = code_item->ins_size_;
1187 size_t num_locals = num_locals_ins - num_ins;
1188 size_t num_outs = code_item->outs_size_;
1189
1190 os << "vr_stack_locations:";
1191 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1192 // For readability, delimit the different kinds of VRs.
1193 if (reg == num_locals_ins) {
1194 os << "\n\tmethod*:";
1195 } else if (reg == num_locals && num_ins > 0) {
1196 os << "\n\tins:";
1197 } else if (reg == 0 && num_locals > 0) {
1198 os << "\n\tlocals:";
1199 }
1200
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001201 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1202 code_item,
1203 oat_method.GetCoreSpillMask(),
1204 oat_method.GetFpSpillMask(),
1205 oat_method.GetFrameSizeInBytes(),
1206 reg,
1207 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001208 os << " v" << reg << "[sp + #" << offset << "]";
1209 }
1210
1211 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1212 if (out_reg == 0) {
1213 os << "\n\touts:";
1214 }
1215
1216 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1217 os << " v" << out_reg << "[sp + #" << offset << "]";
1218 }
1219
1220 os << "\n";
1221 }
1222 }
1223
Ian Rogersb23a7722012-10-09 16:54:26 -07001224 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001225 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001226 size_t i = 0;
1227 while (i < code_item->insns_size_in_code_units_) {
1228 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001229 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1230 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001231 i += instruction->SizeInCodeUnits();
1232 }
1233 }
1234 }
1235
Roland Levillainf2650d12015-05-28 14:53:28 +01001236 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1237 // the optimizing compiler?
1238 static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1239 const DexFile::CodeItem* code_item) {
1240 // If the native GC map is null and the Dex `code_item` is not
1241 // null, then this method has been compiled with the optimizing
1242 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001243 return oat_method.GetQuickCode() != nullptr &&
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001244 oat_method.GetVmapTable() != nullptr &&
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001245 code_item != nullptr;
1246 }
1247
1248 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1249 // the dextodex compiler?
1250 static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
1251 const DexFile::CodeItem* code_item) {
1252 // If the quick code is null, the Dex `code_item` is not
1253 // null, and the vmap table is not null, then this method has been compiled
1254 // with the dextodex compiler.
1255 return oat_method.GetQuickCode() == nullptr &&
1256 oat_method.GetVmapTable() != nullptr &&
1257 code_item != nullptr;
Roland Levillainf2650d12015-05-28 14:53:28 +01001258 }
1259
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001260 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001261 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001262 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001263 const DexFile* dex_file,
1264 const DexFile::ClassDef& class_def,
1265 const DexFile::CodeItem* code_item,
1266 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001267 if ((method_access_flags & kAccNative) == 0) {
1268 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001269 Runtime* const runtime = Runtime::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001270 Handle<mirror::DexCache> dex_cache(
Mathieu Chartierf284d442016-06-02 11:48:30 -07001271 hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file, nullptr)));
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001272 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001273 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001274 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
David Brazdil15fc7292016-09-02 14:13:18 +01001275 class_def, code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001276 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001277
1278 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001279 }
1280
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001281 // The StackMapsHelper provides the stack maps in the native PC order.
1282 // For identical native PCs, the order from the CodeInfo is preserved.
1283 class StackMapsHelper {
1284 public:
1285 explicit StackMapsHelper(const uint8_t* raw_code_info)
1286 : code_info_(raw_code_info),
1287 encoding_(code_info_.ExtractEncoding()),
1288 number_of_stack_maps_(code_info_.GetNumberOfStackMaps(encoding_)),
1289 indexes_(),
1290 offset_(static_cast<size_t>(-1)),
1291 stack_map_index_(0u) {
1292 if (number_of_stack_maps_ != 0u) {
1293 // Check if native PCs are ordered.
1294 bool ordered = true;
1295 StackMap last = code_info_.GetStackMapAt(0u, encoding_);
1296 for (size_t i = 1; i != number_of_stack_maps_; ++i) {
1297 StackMap current = code_info_.GetStackMapAt(i, encoding_);
1298 if (last.GetNativePcOffset(encoding_.stack_map_encoding) >
1299 current.GetNativePcOffset(encoding_.stack_map_encoding)) {
1300 ordered = false;
1301 break;
1302 }
1303 last = current;
1304 }
1305 if (!ordered) {
1306 // Create indirection indexes for access in native PC order. We do not optimize
1307 // for the fact that there can currently be only two separately ordered ranges,
1308 // namely normal stack maps and catch-point stack maps.
1309 indexes_.resize(number_of_stack_maps_);
1310 std::iota(indexes_.begin(), indexes_.end(), 0u);
1311 std::sort(indexes_.begin(),
1312 indexes_.end(),
1313 [this](size_t lhs, size_t rhs) {
1314 StackMap left = code_info_.GetStackMapAt(lhs, encoding_);
1315 uint32_t left_pc = left.GetNativePcOffset(encoding_.stack_map_encoding);
1316 StackMap right = code_info_.GetStackMapAt(rhs, encoding_);
1317 uint32_t right_pc = right.GetNativePcOffset(encoding_.stack_map_encoding);
1318 // If the PCs are the same, compare indexes to preserve the original order.
1319 return (left_pc < right_pc) || (left_pc == right_pc && lhs < rhs);
1320 });
1321 }
1322 offset_ = GetStackMapAt(0).GetNativePcOffset(encoding_.stack_map_encoding);
1323 }
1324 }
1325
1326 const CodeInfo& GetCodeInfo() const {
1327 return code_info_;
1328 }
1329
1330 const CodeInfoEncoding& GetEncoding() const {
1331 return encoding_;
1332 }
1333
1334 size_t GetOffset() const {
1335 return offset_;
1336 }
1337
1338 StackMap GetStackMap() const {
1339 return GetStackMapAt(stack_map_index_);
1340 }
1341
1342 void Next() {
1343 ++stack_map_index_;
1344 offset_ = (stack_map_index_ == number_of_stack_maps_)
1345 ? static_cast<size_t>(-1)
1346 : GetStackMapAt(stack_map_index_).GetNativePcOffset(encoding_.stack_map_encoding);
1347 }
1348
1349 private:
1350 StackMap GetStackMapAt(size_t i) const {
1351 if (!indexes_.empty()) {
1352 i = indexes_[i];
1353 }
1354 DCHECK_LT(i, number_of_stack_maps_);
1355 return code_info_.GetStackMapAt(i, encoding_);
1356 }
1357
1358 const CodeInfo code_info_;
1359 const CodeInfoEncoding encoding_;
1360 const size_t number_of_stack_maps_;
1361 dchecked_vector<size_t> indexes_; // Used if stack map native PCs are not ordered.
1362 size_t offset_;
1363 size_t stack_map_index_;
1364 };
1365
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001366 void DumpCode(VariableIndentationOutputStream* vios,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001367 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1368 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001369 const void* quick_code = oat_method.GetQuickCode();
1370
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001371 if (code_size == 0) {
1372 code_size = oat_method.GetQuickCodeSize();
1373 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001374 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001375 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001376 return;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001377 } else if (!bad_input && IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1378 // The optimizing compiler outputs its CodeInfo data in the vmap table.
1379 StackMapsHelper helper(oat_method.GetVmapTable());
1380 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1381 size_t offset = 0;
1382 while (offset < code_size) {
1383 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
1384 if (offset == helper.GetOffset()) {
1385 ScopedIndentation indent1(vios);
1386 StackMap stack_map = helper.GetStackMap();
1387 DCHECK(stack_map.IsValid());
1388 stack_map.Dump(vios,
1389 helper.GetCodeInfo(),
1390 helper.GetEncoding(),
1391 oat_method.GetCodeOffset(),
1392 code_item->registers_size_);
1393 do {
1394 helper.Next();
1395 // There may be multiple stack maps at a given PC. We display only the first one.
1396 } while (offset == helper.GetOffset());
1397 }
1398 DCHECK_LT(offset, helper.GetOffset());
1399 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001400 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001401 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1402 size_t offset = 0;
1403 while (offset < code_size) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001404 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001405 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001406 }
1407 }
1408
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001409 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001410 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001411 const OatDumperOptions& options_;
1412 uint32_t resolved_addr2instr_;
Andreas Gampe372f3a32016-08-19 10:49:06 -07001413 const InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001414 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001415 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001416};
1417
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001418class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001419 public:
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001420 ImageDumper(std::ostream* os,
1421 gc::space::ImageSpace& image_space,
1422 const ImageHeader& image_header,
1423 OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001424 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001425 vios_(os),
1426 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001427 image_space_(image_space),
1428 image_header_(image_header),
1429 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001430
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001431 bool Dump() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001432 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001433 std::ostream& indent_os = vios_.Stream();
1434
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001435 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001436
Jeff Haodcdc85b2015-12-04 14:06:18 -08001437 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1438
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001439 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001440
Mathieu Chartiere401d142015-04-22 13:56:20 -07001441 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1442
1443 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1444 auto section = static_cast<ImageHeader::ImageSections>(i);
1445 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1446 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001447
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001448 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001449
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001450 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001451
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001452 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1453
1454 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1455
1456 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001457
Alex Lighta59dd802014-07-02 16:28:08 -07001458 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1459
Igor Murashkin46774762014-10-22 11:37:02 -07001460 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1461
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001462 {
1463 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001464 static_assert(arraysize(image_roots_descriptions_) ==
1465 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001466 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1467 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1468 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001469 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001470 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001471 if (image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001472 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001473 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001474 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001475 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1476 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001477 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001478 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1479 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001480 run++;
1481 } else {
1482 break;
1483 }
1484 }
1485 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001486 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001487 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001488 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001489 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001490 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001491 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001492 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001493 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001494 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001495 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001496 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001497 }
1498 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001499 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001500
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001501 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001502 os << "METHOD ROOTS\n";
1503 static_assert(arraysize(image_methods_descriptions_) ==
1504 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1505 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1506 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1507 const char* description = image_methods_descriptions_[i];
1508 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001509 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001510 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001511 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001512 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001513
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001514 Runtime* const runtime = Runtime::Current();
1515 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001516 std::string image_filename = image_space_.GetImageFilename();
1517 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001518 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001519 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001520 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001521 const OatFile* oat_file = image_space_.GetOatFile();
Alex Lighta59dd802014-07-02 16:28:08 -07001522 if (oat_file == nullptr) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001523 oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location);
1524 }
1525 if (oat_file == nullptr) {
1526 oat_file = OatFile::Open(oat_location,
1527 oat_location,
1528 nullptr,
1529 nullptr,
1530 false,
1531 /*low_4gb*/false,
1532 nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001533 &error_msg);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001534 }
1535 if (oat_file == nullptr) {
1536 os << "OAT FILE NOT FOUND: " << error_msg << "\n";
1537 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001538 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001539 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001540
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001541 stats_.oat_file_bytes = oat_file->Size();
1542
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001543 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001544
Mathieu Chartier02e25112013-08-14 16:14:24 -07001545 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001546 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001547 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1548 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001549 }
1550
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001551 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001552
Jeff Haodcdc85b2015-12-04 14:06:18 -08001553 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001554 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001555 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001556 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001557 {
1558 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1559 heap->FlushAllocStack();
1560 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001561 // Since FlushAllocStack() above resets the (active) allocation
1562 // stack. Need to revoke the thread-local allocation stacks that
1563 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001564 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001565 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001566 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001567 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001568 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001569 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001570 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001571 {
1572 ReaderMutexLock mu(self, *class_linker->DexLock());
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001573 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001574 mirror::DexCache* dex_cache =
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001575 down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001576 if (dex_cache != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01001577 dex_caches_.insert(dex_cache);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001578 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001579 }
1580 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001581 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001582 // Dump the normal objects before ArtMethods.
1583 image_space_.GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1584 indent_os << "\n";
1585 // TODO: Dump fields.
1586 // Dump methods after.
Jeff Haodcdc85b2015-12-04 14:06:18 -08001587 DumpArtMethodVisitor visitor(this);
Mathieu Chartiere42888f2016-04-14 10:49:19 -07001588 image_header_.VisitPackedArtMethods(&visitor,
1589 image_space_.Begin(),
1590 image_header_.GetPointerSize());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001591 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001592 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001593 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001594 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001595 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001596 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001597 size_t data_size = image_header_.GetDataSize(); // stored size in file.
1598 if (file == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001599 LOG(WARNING) << "Failed to find image in " << image_filename;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001600 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001601 stats_.file_bytes = file->GetLength();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001602 // If the image is compressed, adjust to decompressed size.
1603 size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader);
1604 if (image_header_.GetStorageMode() == ImageHeader::kStorageModeUncompressed) {
1605 DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image";
1606 }
1607 stats_.file_bytes += uncompressed_size - data_size;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001608 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001609 size_t header_bytes = sizeof(ImageHeader);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001610 const auto& object_section = image_header_.GetImageSection(ImageHeader::kSectionObjects);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001611 const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1612 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001613 const auto& dex_cache_arrays_section = image_header_.GetImageSection(
1614 ImageHeader::kSectionDexCacheArrays);
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001615 const auto& intern_section = image_header_.GetImageSection(
1616 ImageHeader::kSectionInternedStrings);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001617 const auto& class_table_section = image_header_.GetImageSection(
1618 ImageHeader::kSectionClassTable);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001619 const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1620
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001621 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001622
1623 // Objects are kObjectAlignment-aligned.
1624 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1625 if (object_section.Offset() > header_bytes) {
1626 stats_.alignment_bytes += object_section.Offset() - header_bytes;
1627 }
1628
1629 // Field section is 4-byte aligned.
1630 constexpr size_t kFieldSectionAlignment = 4U;
1631 uint32_t end_objects = object_section.Offset() + object_section.Size();
1632 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
1633 stats_.alignment_bytes += field_section.Offset() - end_objects;
1634
1635 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
1636 uint32_t end_fields = field_section.Offset() + field_section.Size();
1637 CHECK_ALIGNED(method_section.Offset(), 4);
1638 stats_.alignment_bytes += method_section.Offset() - end_fields;
1639
1640 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
1641 uint32_t end_methods = method_section.Offset() + method_section.Size();
1642 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
1643 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
1644
1645 // Intern table is 8-byte aligned.
1646 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
1647 CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
1648 stats_.alignment_bytes += intern_section.Offset() - end_caches;
1649
1650 // Add space between intern table and class table.
1651 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
1652 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
1653
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001654 // Add space between end of image data and bitmap. Expect the bitmap to be page-aligned.
1655 const size_t bitmap_offset = sizeof(ImageHeader) + data_size;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001656 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001657 stats_.alignment_bytes += RoundUp(bitmap_offset, kPageSize) - bitmap_offset;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001658
Mathieu Chartiere401d142015-04-22 13:56:20 -07001659 stats_.bitmap_bytes += bitmap_section.Size();
1660 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01001661 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01001662 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001663 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001664 stats_.class_table_bytes += class_table_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001665 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001666 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001667
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001668 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001669
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001670 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001671 }
1672
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001673 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001674 class DumpArtMethodVisitor : public ArtMethodVisitor {
1675 public:
1676 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
1677
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001678 virtual void Visit(ArtMethod* method) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001679 std::ostream& indent_os = image_dumper_->vios_.Stream();
1680 indent_os << method << " " << " ArtMethod: " << PrettyMethod(method) << "\n";
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001681 image_dumper_->DumpMethod(method, indent_os);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001682 indent_os << "\n";
1683 }
1684
1685 private:
1686 ImageDumper* const image_dumper_;
1687 };
1688
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001689 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001690 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001691 CHECK(type != nullptr);
1692 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001693 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001694 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001695 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001696 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001697 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001698 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001699 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001700 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001701 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001702 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001703 }
1704 }
1705
Mathieu Chartierc7853442015-03-27 14:35:38 -07001706 static void PrintField(std::ostream& os, ArtField* field, mirror::Object* obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001707 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001708 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08001709 switch (field->GetTypeAsPrimitiveType()) {
1710 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08001711 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001712 break;
1713 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001714 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001715 break;
1716 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001717 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001718 break;
1719 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001720 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001721 break;
1722 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07001723 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001724 break;
1725 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07001726 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001727 break;
1728 case Primitive::kPrimBoolean:
Fred Shih37f05ef2014-07-16 18:38:08 -07001729 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1730 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001731 break;
1732 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07001733 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001734 break;
1735 case Primitive::kPrimNot: {
1736 // Get the value, don't compute the type unless it is non-null as we don't want
1737 // to cause class loading.
1738 mirror::Object* value = field->GetObj(obj);
1739 if (value == nullptr) {
1740 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07001741 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08001742 // Grab the field type without causing resolution.
Mathieu Chartierdaaf3262015-03-24 13:30:28 -07001743 mirror::Class* field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08001744 if (field_type != nullptr) {
1745 PrettyObjectValue(os, field_type, value);
1746 } else {
1747 os << StringPrintf("%p %s\n", value,
1748 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
1749 }
Ian Rogers50239c72013-06-17 14:53:22 -07001750 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001751 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07001752 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001753 default:
1754 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
1755 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08001756 }
1757 }
1758
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001759 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001760 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001761 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001762 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001763 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001764 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001765 for (ArtField& field : klass->GetIFields()) {
1766 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001767 }
1768 }
1769
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001770 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001771 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001772 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001773
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001774 const void* GetQuickOatCodeBegin(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001775 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001776 image_header_.GetPointerSize());
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001777 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001778 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001779 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001780 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001781 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001782 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001783 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001784 }
1785
Mathieu Chartiere401d142015-04-22 13:56:20 -07001786 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001787 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001788 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1789 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001790 return 0;
1791 }
1792 return oat_code_begin[-1];
1793 }
1794
Mathieu Chartiere401d142015-04-22 13:56:20 -07001795 const void* GetQuickOatCodeEnd(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001796 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001797 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001798 if (oat_code_begin == nullptr) {
1799 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001800 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001801 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001802 }
1803
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001804 static void Callback(mirror::Object* obj, void* arg) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001805 DCHECK(obj != nullptr);
1806 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001807 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001808 if (!state->InDumpSpace(obj)) {
1809 return;
1810 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001811
1812 size_t object_bytes = obj->SizeOf();
1813 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1814 state->stats_.object_bytes += object_bytes;
1815 state->stats_.alignment_bytes += alignment_bytes;
1816
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001817 std::ostream& os = state->vios_.Stream();
1818
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001819 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001820 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001821 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
1822 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001823 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001824 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001825 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
1826 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08001827 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001828 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001829 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001830 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001831 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001832 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001833 ScopedIndentation indent1(&state->vios_);
1834 DumpFields(os, obj, obj_class);
Andreas Gampe542451c2016-07-26 09:02:02 -07001835 const PointerSize image_pointer_size = state->image_header_.GetPointerSize();
Ian Rogersd5b32602012-02-26 16:40:04 -08001836 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001837 auto* obj_array = obj->AsObjectArray<mirror::Object>();
1838 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001839 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001840 size_t run = 0;
1841 for (int32_t j = i + 1; j < length; j++) {
1842 if (value == obj_array->Get(j)) {
1843 run++;
1844 } else {
1845 break;
1846 }
1847 }
1848 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001849 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001850 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001851 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001852 i = i + run;
1853 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001854 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001855 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001856 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001857 }
1858 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001859 mirror::Class* klass = obj->AsClass();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001860 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001861 os << "STATICS:\n";
1862 ScopedIndentation indent2(&state->vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001863 for (ArtField& field : klass->GetSFields()) {
1864 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001865 }
1866 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001867 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01001868 auto it = state->dex_caches_.find(obj);
1869 if (it != state->dex_caches_.end()) {
1870 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001871 const auto& field_section = state->image_header_.GetImageSection(
1872 ImageHeader::kSectionArtFields);
1873 const auto& method_section = state->image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001874 size_t num_methods = dex_cache->NumResolvedMethods();
1875 if (num_methods != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001876 os << "Methods (size=" << num_methods << "):\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01001877 ScopedIndentation indent2(&state->vios_);
1878 auto* resolved_methods = dex_cache->GetResolvedMethods();
1879 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001880 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_methods,
1881 i,
1882 image_pointer_size);
Vladimir Marko05792b92015-08-03 11:56:49 +01001883 size_t run = 0;
1884 for (size_t j = i + 1;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001885 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_methods,
1886 j,
1887 image_pointer_size);
1888 ++j) {
1889 ++run;
1890 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001891 if (run == 0) {
1892 os << StringPrintf("%zd: ", i);
1893 } else {
1894 os << StringPrintf("%zd to %zd: ", i, i + run);
1895 i = i + run;
1896 }
1897 std::string msg;
1898 if (elem == nullptr) {
1899 msg = "null";
1900 } else if (method_section.Contains(
1901 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
1902 msg = PrettyMethod(reinterpret_cast<ArtMethod*>(elem));
1903 } else {
1904 msg = "<not in method section>";
1905 }
1906 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07001907 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001908 }
1909 size_t num_fields = dex_cache->NumResolvedFields();
1910 if (num_fields != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001911 os << "Fields (size=" << num_fields << "):\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01001912 ScopedIndentation indent2(&state->vios_);
1913 auto* resolved_fields = dex_cache->GetResolvedFields();
1914 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001915 auto* elem = mirror::DexCache::GetElementPtrSize(
1916 resolved_fields, i, image_pointer_size);
Vladimir Marko05792b92015-08-03 11:56:49 +01001917 size_t run = 0;
1918 for (size_t j = i + 1;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001919 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_fields,
1920 j,
1921 image_pointer_size);
1922 ++j) {
1923 ++run;
1924 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001925 if (run == 0) {
1926 os << StringPrintf("%zd: ", i);
1927 } else {
1928 os << StringPrintf("%zd to %zd: ", i, i + run);
1929 i = i + run;
1930 }
1931 std::string msg;
1932 if (elem == nullptr) {
1933 msg = "null";
1934 } else if (field_section.Contains(
1935 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
1936 msg = PrettyField(reinterpret_cast<ArtField*>(elem));
1937 } else {
1938 msg = "<not in field section>";
1939 }
1940 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07001941 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001942 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001943 size_t num_types = dex_cache->NumResolvedTypes();
1944 if (num_types != 0u) {
1945 os << "Types (size=" << num_types << "):\n";
1946 ScopedIndentation indent2(&state->vios_);
1947 auto* resolved_types = dex_cache->GetResolvedTypes();
1948 for (size_t i = 0; i < num_types; ++i) {
1949 auto* elem = resolved_types[i].Read();
1950 size_t run = 0;
1951 for (size_t j = i + 1; j != num_types && elem == resolved_types[j].Read(); ++j) {
1952 ++run;
1953 }
1954 if (run == 0) {
1955 os << StringPrintf("%zd: ", i);
1956 } else {
1957 os << StringPrintf("%zd to %zd: ", i, i + run);
1958 i = i + run;
1959 }
1960 std::string msg;
1961 if (elem == nullptr) {
1962 msg = "null";
1963 } else {
1964 msg = PrettyClass(elem);
1965 }
1966 os << StringPrintf("%p %s\n", elem, msg.c_str());
1967 }
1968 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001969 }
1970 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07001971 std::string temp;
1972 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001973 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001974
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001975 void DumpMethod(ArtMethod* method, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001976 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001977 DCHECK(method != nullptr);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001978 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
1979 const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
Andreas Gampe542451c2016-07-26 09:02:02 -07001980 const PointerSize pointer_size = image_header_.GetPointerSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01001981 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
1982 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07001983 if (method->IsNative()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001984 bool first_occurrence;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001985 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
1986 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001987 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001988 stats_.native_to_managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001989 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001990 if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
1991 image_header_.GetPointerSize())) {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01001992 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001993 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -07001994 } else if (method->IsAbstract() || method->IsClassInitializer()) {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00001995 // Don't print information for these.
Mathieu Chartiere42888f2016-04-14 10:49:19 -07001996 } else if (method->IsRuntimeMethod()) {
1997 ImtConflictTable* table = method->GetImtConflictTable(image_header_.GetPointerSize());
1998 if (table != nullptr) {
1999 indent_os << "IMT conflict table " << table << " method: ";
2000 for (size_t i = 0, count = table->NumEntries(pointer_size); i < count; ++i) {
2001 indent_os << PrettyMethod(table->GetImplementationMethod(i, pointer_size)) << " ";
2002 }
2003 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002004 } else {
2005 const DexFile::CodeItem* code_item = method->GetCodeItem();
2006 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002007 stats_.dex_instruction_bytes += dex_instruction_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002008
2009 bool first_occurrence;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002010 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002011 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01002012 // Method compiled with the optimizing compiler have no vmap table.
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002013 vmap_table_bytes = ComputeOatSize(method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002014 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002015 stats_.vmap_table_bytes += vmap_table_bytes;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002016 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002017 }
2018
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002019 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2020 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002021 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002022 stats_.managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002023 if (method->IsConstructor()) {
2024 if (method->IsStatic()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002025 stats_.class_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002026 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002027 stats_.large_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002028 }
2029 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002030 stats_.large_method_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002031 }
2032 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002033 stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002034
Igor Murashkin7617abd2015-07-10 18:27:47 -07002035 uint32_t method_access_flags = method->GetAccessFlags();
2036
Mathieu Chartiere401d142015-04-22 13:56:20 -07002037 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002038 indent_os << StringPrintf("SIZE: Dex Instructions=%zd StackMaps=%zd AccessFlags=0x%x\n",
2039 dex_instruction_bytes,
2040 vmap_table_bytes,
Igor Murashkin7617abd2015-07-10 18:27:47 -07002041 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002042
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002043 size_t total_size = dex_instruction_bytes +
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002044 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002045
2046 double expansion =
2047 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002048 stats_.ComputeOutliers(total_size, expansion, method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002049 }
2050 }
2051
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002052 std::set<const void*> already_seen_;
2053 // Compute the size of the given data within the oat file and whether this is the first time
2054 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002055 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002056 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002057 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002058 already_seen_.insert(oat_data);
2059 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002060 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002061 }
2062 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002063 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002064
2065 public:
2066 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002067 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002068 size_t file_bytes;
2069
2070 size_t header_bytes;
2071 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002072 size_t art_field_bytes;
2073 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01002074 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002075 size_t interned_strings_bytes;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002076 size_t class_table_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07002077 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002078 size_t alignment_bytes;
2079
2080 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002081 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002082 size_t managed_to_native_code_bytes;
2083 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07002084 size_t class_initializer_code_bytes;
2085 size_t large_initializer_code_bytes;
2086 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002087
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002088 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002089
2090 size_t dex_instruction_bytes;
2091
Mathieu Chartiere401d142015-04-22 13:56:20 -07002092 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002093 std::vector<size_t> method_outlier_size;
2094 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002095 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002096
Roland Levillain3887c462015-08-12 18:15:42 +01002097 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002098 : oat_file_bytes(0),
2099 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002100 header_bytes(0),
2101 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002102 art_field_bytes(0),
2103 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01002104 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002105 interned_strings_bytes(0),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002106 class_table_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07002107 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002108 alignment_bytes(0),
2109 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002110 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002111 managed_to_native_code_bytes(0),
2112 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07002113 class_initializer_code_bytes(0),
2114 large_initializer_code_bytes(0),
2115 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002116 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002117 dex_instruction_bytes(0) {}
2118
Elliott Hughesa0e18062012-04-13 15:59:59 -07002119 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002120 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002121 size_t bytes;
2122 size_t count;
2123 };
2124 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2125 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002126
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002127 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002128 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2129 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002130 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002131 it->second.count += 1;
2132 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002133 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002134 }
2135 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002136
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002137 double PercentOfOatBytes(size_t size) {
2138 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2139 }
2140
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002141 double PercentOfFileBytes(size_t size) {
2142 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2143 }
2144
2145 double PercentOfObjectBytes(size_t size) {
2146 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2147 }
2148
Mathieu Chartiere401d142015-04-22 13:56:20 -07002149 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002150 method_outlier_size.push_back(total_size);
2151 method_outlier_expansion.push_back(expansion);
2152 method_outlier.push_back(method);
2153 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002154
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002155 void DumpOutliers(std::ostream& os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002156 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002157 size_t sum_of_sizes = 0;
2158 size_t sum_of_sizes_squared = 0;
2159 size_t sum_of_expansion = 0;
2160 size_t sum_of_expansion_squared = 0;
2161 size_t n = method_outlier_size.size();
Mathieu Chartier1ebf8d32016-06-09 11:51:27 -07002162 if (n <= 1) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08002163 return;
2164 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002165 for (size_t i = 0; i < n; i++) {
2166 size_t cur_size = method_outlier_size[i];
2167 sum_of_sizes += cur_size;
2168 sum_of_sizes_squared += cur_size * cur_size;
2169 double cur_expansion = method_outlier_expansion[i];
2170 sum_of_expansion += cur_expansion;
2171 sum_of_expansion_squared += cur_expansion * cur_expansion;
2172 }
2173 size_t size_mean = sum_of_sizes / n;
2174 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2175 double expansion_mean = sum_of_expansion / n;
2176 double expansion_variance =
2177 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2178
2179 // Dump methods whose size is a certain number of standard deviations from the mean
2180 size_t dumped_values = 0;
2181 size_t skipped_values = 0;
2182 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2183 size_t cur_size_variance = i * i * size_variance;
2184 bool first = true;
2185 for (size_t j = 0; j < n; j++) {
2186 size_t cur_size = method_outlier_size[j];
2187 if (cur_size > size_mean) {
2188 size_t cur_var = cur_size - size_mean;
2189 cur_var = cur_var * cur_var;
2190 if (cur_var > cur_size_variance) {
2191 if (dumped_values > 20) {
2192 if (i == 1) {
2193 skipped_values++;
2194 } else {
2195 i = 2; // jump to counting for 1 standard deviation
2196 break;
2197 }
2198 } else {
2199 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002200 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002201 first = false;
2202 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002203 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002204 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002205 method_outlier_size[j] = 0; // don't consider this method again
2206 dumped_values++;
2207 }
2208 }
2209 }
2210 }
2211 }
2212 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002213 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002214 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002215 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002216 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002217
2218 // Dump methods whose expansion is a certain number of standard deviations from the mean
2219 dumped_values = 0;
2220 skipped_values = 0;
2221 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2222 double cur_expansion_variance = i * i * expansion_variance;
2223 bool first = true;
2224 for (size_t j = 0; j < n; j++) {
2225 double cur_expansion = method_outlier_expansion[j];
2226 if (cur_expansion > expansion_mean) {
2227 size_t cur_var = cur_expansion - expansion_mean;
2228 cur_var = cur_var * cur_var;
2229 if (cur_var > cur_expansion_variance) {
2230 if (dumped_values > 20) {
2231 if (i == 1) {
2232 skipped_values++;
2233 } else {
2234 i = 2; // jump to counting for 1 standard deviation
2235 break;
2236 }
2237 } else {
2238 if (first) {
2239 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002240 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002241 first = false;
2242 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002243 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002244 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002245 method_outlier_expansion[j] = 0.0; // don't consider this method again
2246 dumped_values++;
2247 }
2248 }
2249 }
2250 }
2251 }
2252 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002253 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002254 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002255 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002256 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002257 }
2258
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002259 void Dump(std::ostream& os, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002260 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002261 {
2262 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2263 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002264 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2265 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2266 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2267 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2268 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2269 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002270 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002271 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2272 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002273 header_bytes, PercentOfFileBytes(header_bytes),
2274 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002275 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2276 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002277 dex_cache_arrays_bytes,
2278 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002279 interned_strings_bytes,
2280 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002281 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002282 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002283 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2284 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002285 CHECK_EQ(file_bytes,
2286 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2287 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2288 bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002289 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002290
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002291 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002292 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002293 for (const auto& sizes_and_count : sizes_and_counts) {
2294 const std::string& descriptor(sizes_and_count.first);
2295 double average = static_cast<double>(sizes_and_count.second.bytes) /
2296 static_cast<double>(sizes_and_count.second.count);
2297 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002298 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002299 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002300 descriptor.c_str(), sizes_and_count.second.bytes,
2301 sizes_and_count.second.count, average, percent);
2302 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002303 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002304 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002305 CHECK_EQ(object_bytes, object_bytes_total);
2306
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002307 os << StringPrintf("oat_file_bytes = %8zd\n"
2308 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2309 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2310 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2311 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2312 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2313 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002314 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002315 managed_code_bytes,
2316 PercentOfOatBytes(managed_code_bytes),
2317 managed_to_native_code_bytes,
2318 PercentOfOatBytes(managed_to_native_code_bytes),
2319 native_to_managed_code_bytes,
2320 PercentOfOatBytes(native_to_managed_code_bytes),
2321 class_initializer_code_bytes,
2322 PercentOfOatBytes(class_initializer_code_bytes),
2323 large_initializer_code_bytes,
2324 PercentOfOatBytes(large_initializer_code_bytes),
2325 large_method_code_bytes,
2326 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002327 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002328 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002329 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002330 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2331 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002332 }
2333
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002334 os << "\n" << StringPrintf("vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002335 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002336 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002337
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002338 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2339 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002340 static_cast<double>(managed_code_bytes) /
2341 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002342 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002343 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002344 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002345
2346 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002347 }
2348 } stats_;
2349
2350 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002351 enum {
2352 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2353 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2354 kLargeConstructorDexBytes = 4000,
2355 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2356 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2357 kLargeMethodDexBytes = 16000
2358 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002359
2360 // For performance, use the *os_ directly for anything that doesn't need indentation
2361 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002362 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002363 VariableIndentationOutputStream vios_;
2364 ScopedIndentation indent1_;
2365
Ian Rogers1d54e732013-05-02 21:10:01 -07002366 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002367 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002368 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002369 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002370 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002371
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002372 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002373};
2374
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002375static int DumpImage(gc::space::ImageSpace* image_space,
2376 OatDumperOptions* options,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002377 std::ostream* os) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002378 const ImageHeader& image_header = image_space->GetImageHeader();
2379 if (!image_header.IsValid()) {
2380 fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
2381 return EXIT_FAILURE;
2382 }
2383 ImageDumper image_dumper(os, *image_space, image_header, options);
2384 if (!image_dumper.Dump()) {
2385 return EXIT_FAILURE;
2386 }
2387 return EXIT_SUCCESS;
2388}
2389
2390static int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002391 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002392 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002393 options->class_loader_ = &null_class_loader;
2394
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002395 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002396 if (options->app_image_ != nullptr) {
2397 if (options->app_oat_ == nullptr) {
2398 LOG(ERROR) << "Can not dump app image without app oat file";
Jeff Haodcdc85b2015-12-04 14:06:18 -08002399 return EXIT_FAILURE;
2400 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002401 // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
2402 // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
2403 // pointers into 32 bit pointer sized ArtMethods.
2404 std::string error_msg;
2405 std::unique_ptr<OatFile> oat_file(OatFile::Open(options->app_oat_,
2406 options->app_oat_,
2407 nullptr,
2408 nullptr,
2409 false,
2410 /*low_4gb*/true,
2411 nullptr,
2412 &error_msg));
2413 if (oat_file == nullptr) {
2414 LOG(ERROR) << "Failed to open oat file " << options->app_oat_ << " with error " << error_msg;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002415 return EXIT_FAILURE;
2416 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002417 std::unique_ptr<gc::space::ImageSpace> space(
2418 gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg));
2419 if (space == nullptr) {
2420 LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error "
2421 << error_msg;
2422 }
2423 // Open dex files for the image.
2424 std::vector<std::unique_ptr<const DexFile>> dex_files;
2425 if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
2426 LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
2427 << error_msg;
2428 }
2429 // Dump the actual image.
2430 int result = DumpImage(space.get(), options, os);
2431 if (result != EXIT_SUCCESS) {
2432 return result;
2433 }
2434 // Fall through to dump the boot images.
2435 }
2436
2437 gc::Heap* heap = runtime->GetHeap();
2438 CHECK(heap->HasBootImageSpace()) << "No image spaces";
2439 for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
2440 int result = DumpImage(image_space, options, os);
2441 if (result != EXIT_SUCCESS) {
2442 return result;
2443 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002444 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002445 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002446}
2447
Andreas Gampe00b25f32014-09-17 21:49:05 -07002448static int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
2449 std::ostream* os) {
2450 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2451
2452 Thread* self = Thread::Current();
2453 CHECK(self != nullptr);
2454 // Need well-known-classes.
2455 WellKnownClasses::Init(self->GetJniEnv());
2456
2457 // Need to register dex files to get a working dex cache.
2458 ScopedObjectAccess soa(self);
2459 ClassLinker* class_linker = runtime->GetClassLinker();
Mathieu Chartierd57d4542015-10-14 10:55:30 -07002460 runtime->GetOatFileManager().RegisterOatFile(std::unique_ptr<const OatFile>(oat_file));
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002461 std::vector<const DexFile*> class_path;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002462 for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
2463 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002464 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002465 CHECK(dex_file != nullptr) << error_msg;
Mathieu Chartierf284d442016-06-02 11:48:30 -07002466 class_linker->RegisterDexFile(*dex_file, nullptr);
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002467 class_path.push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002468 }
2469
2470 // Need a class loader.
Andreas Gampe00b25f32014-09-17 21:49:05 -07002471 // Fake that we're a compiler.
Mathieu Chartier966878d2016-01-14 14:33:29 -08002472 jobject class_loader = class_linker->CreatePathClassLoader(self, class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002473
2474 // Use the class loader while dumping.
2475 StackHandleScope<1> scope(self);
2476 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07002477 soa.Decode<mirror::ClassLoader>(class_loader));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002478 options->class_loader_ = &loader_handle;
2479
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002480 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002481 bool success = oat_dumper.Dump(*os);
2482 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2483}
2484
2485static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002486 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002487 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002488 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002489 options->class_loader_ = &null_class_loader;
2490
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002491 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002492 bool success = oat_dumper.Dump(*os);
2493 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2494}
2495
2496static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2497 std::ostream* os) {
2498 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002499 OatFile* oat_file = OatFile::Open(oat_filename,
2500 oat_filename,
2501 nullptr,
2502 nullptr,
2503 false,
2504 /*low_4gb*/false,
2505 nullptr,
2506 &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002507 if (oat_file == nullptr) {
2508 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2509 return EXIT_FAILURE;
2510 }
2511
2512 if (runtime != nullptr) {
2513 return DumpOatWithRuntime(runtime, oat_file, options, os);
2514 } else {
2515 return DumpOatWithoutRuntime(oat_file, options, os);
2516 }
2517}
2518
David Srbecky2fdd03c2016-03-10 15:32:37 +00002519static int SymbolizeOat(const char* oat_filename, std::string& output_name, bool no_bits) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002520 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002521 OatFile* oat_file = OatFile::Open(oat_filename,
2522 oat_filename,
2523 nullptr,
2524 nullptr,
2525 false,
2526 /*low_4gb*/false,
2527 nullptr,
2528 &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002529 if (oat_file == nullptr) {
2530 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2531 return EXIT_FAILURE;
2532 }
2533
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002534 bool result;
2535 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
2536 // files for 64-bit code in the past.
2537 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002538 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file, output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002539 result = oat_symbolizer.Symbolize();
2540 } else {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002541 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file, output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002542 result = oat_symbolizer.Symbolize();
2543 }
2544 if (!result) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002545 fprintf(stderr, "Failed to symbolize\n");
2546 return EXIT_FAILURE;
2547 }
2548
2549 return EXIT_SUCCESS;
2550}
2551
Andreas Gampe9fded872016-09-25 16:08:35 -07002552class IMTDumper {
2553 public:
2554 static bool DumpImt(Runtime* runtime, const std::string& imt_file) {
2555 std::vector<std::string> lines = ReadCommentedInputFromFile(imt_file);
2556 std::unordered_set<std::string> prepared;
2557
2558 for (const std::string& line : lines) {
2559 // A line should be either a class descriptor, in which case we will dump the complete IMT,
2560 // or a class descriptor and an interface method, in which case we will lookup the method,
2561 // determine its IMT slot, and check the class' IMT.
2562 size_t first_space = line.find(' ');
2563 if (first_space == std::string::npos) {
2564 DumpIMTForClass(runtime, line, &prepared);
2565 } else {
2566 DumpIMTForMethod(runtime,
2567 line.substr(0, first_space),
2568 line.substr(first_space + 1, std::string::npos),
2569 &prepared);
2570 }
2571 std::cerr << std::endl;
2572 }
2573
2574 return true;
2575 }
2576
2577 static bool DumpImtStats(Runtime* runtime, const std::vector<const DexFile*>& dex_files) {
2578 size_t wo_imt = 0;
2579 size_t w_imt = 0;
2580 std::map<size_t, size_t> histogram;
2581
2582 ClassLinker* class_linker = runtime->GetClassLinker();
2583 const PointerSize pointer_size = class_linker->GetImagePointerSize();
2584 std::unordered_set<std::string> prepared;
2585
2586 Thread* self = Thread::Current();
2587 ScopedObjectAccess soa(self);
2588 StackHandleScope<1> scope(self);
2589 MutableHandle<mirror::Class> h_klass(scope.NewHandle<mirror::Class>(nullptr));
2590
2591 for (const DexFile* dex_file : dex_files) {
2592 for (uint32_t class_def_index = 0;
2593 class_def_index != dex_file->NumClassDefs();
2594 ++class_def_index) {
2595 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
2596 const char* descriptor = dex_file->GetClassDescriptor(class_def);
2597 h_klass.Assign(class_linker->FindClass(self,
2598 descriptor,
2599 ScopedNullHandle<mirror::ClassLoader>()));
2600 if (h_klass.Get() == nullptr) {
2601 std::cerr << "Warning: could not load " << descriptor << std::endl;
2602 continue;
2603 }
2604
2605 if (HasNoIMT(runtime, h_klass, pointer_size, &prepared)) {
2606 wo_imt++;
2607 continue;
2608 }
2609
2610 ImTable* im_table = PrepareAndGetImTable(runtime, h_klass, pointer_size, &prepared);
2611 if (im_table == nullptr) {
2612 // Should not happen, but accept.
2613 wo_imt++;
2614 continue;
2615 }
2616
2617 w_imt++;
2618 for (size_t imt_index = 0; imt_index != ImTable::kSize; ++imt_index) {
2619 ArtMethod* ptr = im_table->Get(imt_index, pointer_size);
2620 if (ptr->IsRuntimeMethod()) {
2621 if (ptr->IsImtUnimplementedMethod()) {
2622 histogram[0]++;
2623 } else {
2624 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
2625 histogram[current_table->NumEntries(pointer_size)]++;
2626 }
2627 } else {
2628 histogram[1]++;
2629 }
2630 }
2631 }
2632 }
2633
2634 std::cerr << "IMT stats:"
2635 << std::endl << std::endl;
2636
2637 std::cerr << " " << w_imt << " classes with IMT."
2638 << std::endl << std::endl;
2639 std::cerr << " " << wo_imt << " classes without IMT (or copy from Object)."
2640 << std::endl << std::endl;
2641
2642 double sum_one = 0;
2643 size_t count_one = 0;
2644
2645 std::cerr << " " << "IMT histogram" << std::endl;
2646 for (auto& bucket : histogram) {
2647 std::cerr << " " << bucket.first << " " << bucket.second << std::endl;
2648 if (bucket.first > 0) {
2649 sum_one += bucket.second * bucket.first;
2650 count_one += bucket.second;
2651 }
2652 }
2653
2654 double count_zero = count_one + histogram[0];
2655 std::cerr << " Stats:" << std::endl;
2656 std::cerr << " Average depth (including empty): " << (sum_one / count_zero) << std::endl;
2657 std::cerr << " Average depth (excluding empty): " << (sum_one / count_one) << std::endl;
2658
2659 return true;
2660 }
2661
2662 private:
2663 // Check whether the given class has no IMT (or the one shared with java.lang.Object).
2664 static bool HasNoIMT(Runtime* runtime,
2665 Handle<mirror::Class> klass,
2666 const PointerSize pointer_size,
2667 std::unordered_set<std::string>* prepared)
2668 REQUIRES_SHARED(Locks::mutator_lock_) {
2669 if (klass->IsObjectClass() || !klass->ShouldHaveImt()) {
2670 return true;
2671 }
2672
2673 if (klass->GetImt(pointer_size) == nullptr) {
2674 PrepareClass(runtime, klass, prepared);
2675 }
2676
2677 mirror::Class* object_class = mirror::Class::GetJavaLangClass()->GetSuperClass();
2678 DCHECK(object_class->IsObjectClass());
2679
2680 bool result = klass->GetImt(pointer_size) == object_class->GetImt(pointer_size);
2681
2682 if (klass->GetIfTable() == nullptr) {
2683 DCHECK(result);
2684 }
2685
2686 return result;
2687 }
2688
2689 static void PrintTable(ImtConflictTable* table, PointerSize pointer_size)
2690 REQUIRES_SHARED(Locks::mutator_lock_) {
2691 if (table == nullptr) {
2692 std::cerr << " <No IMT?>" << std::endl;
2693 return;
2694 }
2695 size_t table_index = 0;
2696 for (;;) {
2697 ArtMethod* ptr = table->GetInterfaceMethod(table_index, pointer_size);
2698 if (ptr == nullptr) {
2699 return;
2700 }
2701 table_index++;
2702 std::cerr << " " << PrettyMethod(ptr, true) << std::endl;
2703 }
2704 }
2705
2706 static ImTable* PrepareAndGetImTable(Runtime* runtime,
2707 Thread* self,
2708 const std::string& class_name,
2709 const PointerSize pointer_size,
2710 mirror::Class** klass_out,
2711 std::unordered_set<std::string>* prepared)
2712 REQUIRES_SHARED(Locks::mutator_lock_) {
2713 if (class_name.empty()) {
2714 return nullptr;
2715 }
2716
2717 std::string descriptor;
2718 if (class_name[0] == 'L') {
2719 descriptor = class_name;
2720 } else {
2721 descriptor = DotToDescriptor(class_name.c_str());
2722 }
2723
2724 ScopedNullHandle<mirror::ClassLoader> null_handle;
2725
2726 mirror::Class* klass =
2727 runtime->GetClassLinker()->FindClass(self, descriptor.c_str(), null_handle);
2728
2729 if (klass == nullptr) {
2730 self->ClearException();
2731 std::cerr << "Did not find " << class_name << std::endl;
2732 *klass_out = nullptr;
2733 return nullptr;
2734 }
2735
2736 StackHandleScope<1> scope(Thread::Current());
2737 Handle<mirror::Class> h_klass = scope.NewHandle<mirror::Class>(klass);
2738
2739 ImTable* ret = PrepareAndGetImTable(runtime, h_klass, pointer_size, prepared);
2740 *klass_out = h_klass.Get();
2741 return ret;
2742 }
2743
2744 static ImTable* PrepareAndGetImTable(Runtime* runtime,
2745 Handle<mirror::Class> h_klass,
2746 const PointerSize pointer_size,
2747 std::unordered_set<std::string>* prepared)
2748 REQUIRES_SHARED(Locks::mutator_lock_) {
2749 PrepareClass(runtime, h_klass, prepared);
2750 return h_klass->GetImt(pointer_size);
2751 }
2752
2753 static void DumpIMTForClass(Runtime* runtime,
2754 const std::string& class_name,
2755 std::unordered_set<std::string>* prepared) {
2756 Thread* self = Thread::Current();
2757 ScopedObjectAccess soa(self);
2758
2759 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
2760 mirror::Class* klass;
2761 ImTable* imt = PrepareAndGetImTable(runtime, self, class_name, pointer_size, &klass, prepared);
2762 if (imt == nullptr) {
2763 return;
2764 }
2765
2766 std::cerr << class_name << std::endl << " IMT:" << std::endl;
2767 for (size_t index = 0; index < ImTable::kSize; ++index) {
2768 std::cerr << " " << index << ":" << std::endl;
2769 ArtMethod* ptr = imt->Get(index, pointer_size);
2770 if (ptr->IsRuntimeMethod()) {
2771 if (ptr->IsImtUnimplementedMethod()) {
2772 std::cerr << " <empty>" << std::endl;
2773 } else {
2774 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
2775 PrintTable(current_table, pointer_size);
2776 }
2777 } else {
2778 std::cerr << " " << PrettyMethod(ptr, true) << std::endl;
2779 }
2780 }
2781
2782 std::cerr << " Interfaces:" << std::endl;
2783 // Run through iftable, find methods that slot here, see if they fit.
2784 mirror::IfTable* if_table = klass->GetIfTable();
2785 if (if_table != nullptr) {
2786 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
2787 mirror::Class* iface = if_table->GetInterface(i);
2788 std::string iface_name;
2789 std::cerr << " " << iface->GetDescriptor(&iface_name) << std::endl;
2790
2791 for (ArtMethod& iface_method : iface->GetVirtualMethods(pointer_size)) {
2792 uint32_t base_hash = ImTable::GetBaseImtHash(&iface_method);
2793 uint32_t imt_slot = ImTable::GetImtIndex(&iface_method);
2794 std::cerr << " " << PrettyMethod(&iface_method, true) << " slot=" << std::dec
2795 << imt_slot << " base_hash=0x" << std::hex << base_hash << std::endl;
2796 }
2797 }
2798 }
2799 }
2800
2801 static void DumpIMTForMethod(Runtime* runtime,
2802 const std::string& class_name,
2803 const std::string& method,
2804 std::unordered_set<std::string>* prepared) {
2805 Thread* self = Thread::Current();
2806 ScopedObjectAccess soa(self);
2807
2808 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
2809 mirror::Class* klass;
2810 ImTable* imt = PrepareAndGetImTable(runtime,
2811 self,
2812 class_name,
2813 pointer_size,
2814 &klass,
2815 prepared);
2816 if (imt == nullptr) {
2817 return;
2818 }
2819
2820 std::cerr << class_name << " <" << method << ">" << std::endl;
2821 for (size_t index = 0; index < ImTable::kSize; ++index) {
2822 ArtMethod* ptr = imt->Get(index, pointer_size);
2823 if (ptr->IsRuntimeMethod()) {
2824 if (ptr->IsImtUnimplementedMethod()) {
2825 continue;
2826 }
2827
2828 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
2829 if (current_table == nullptr) {
2830 continue;
2831 }
2832
2833 size_t table_index = 0;
2834 for (;;) {
2835 ArtMethod* ptr2 = current_table->GetInterfaceMethod(table_index, pointer_size);
2836 if (ptr2 == nullptr) {
2837 break;
2838 }
2839 table_index++;
2840
2841 std::string p_name = PrettyMethod(ptr2, true);
2842 if (StartsWith(p_name, method.c_str())) {
2843 std::cerr << " Slot "
2844 << index
2845 << " ("
2846 << current_table->NumEntries(pointer_size)
2847 << ")"
2848 << std::endl;
2849 PrintTable(current_table, pointer_size);
2850 return;
2851 }
2852 }
2853 } else {
2854 std::string p_name = PrettyMethod(ptr, true);
2855 if (StartsWith(p_name, method.c_str())) {
2856 std::cerr << " Slot " << index << " (1)" << std::endl;
2857 std::cerr << " " << p_name << std::endl;
2858 } else {
2859 // Run through iftable, find methods that slot here, see if they fit.
2860 mirror::IfTable* if_table = klass->GetIfTable();
2861 if (if_table != nullptr) {
2862 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
2863 mirror::Class* iface = if_table->GetInterface(i);
2864 size_t num_methods = iface->NumDeclaredVirtualMethods();
2865 if (num_methods > 0) {
2866 for (ArtMethod& iface_method : iface->GetMethods(pointer_size)) {
2867 if (ImTable::GetImtIndex(&iface_method) == index) {
2868 std::string i_name = PrettyMethod(&iface_method, true);
2869 if (StartsWith(i_name, method.c_str())) {
2870 std::cerr << " Slot " << index << " (1)" << std::endl;
2871 std::cerr << " " << p_name << " (" << i_name << ")" << std::endl;
2872 }
2873 }
2874 }
2875 }
2876 }
2877 }
2878 }
2879 }
2880 }
2881 }
2882
2883 // Read lines from the given stream, dropping comments and empty lines
2884 static std::vector<std::string> ReadCommentedInputStream(std::istream& in_stream) {
2885 std::vector<std::string> output;
2886 while (in_stream.good()) {
2887 std::string dot;
2888 std::getline(in_stream, dot);
2889 if (StartsWith(dot, "#") || dot.empty()) {
2890 continue;
2891 }
2892 output.push_back(dot);
2893 }
2894 return output;
2895 }
2896
2897 // Read lines from the given file, dropping comments and empty lines.
2898 static std::vector<std::string> ReadCommentedInputFromFile(const std::string& input_filename) {
2899 std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
2900 if (input_file.get() == nullptr) {
2901 LOG(ERROR) << "Failed to open input file " << input_filename;
2902 return std::vector<std::string>();
2903 }
2904 std::vector<std::string> result = ReadCommentedInputStream(*input_file);
2905 input_file->close();
2906 return result;
2907 }
2908
2909 // Prepare a class, i.e., ensure it has a filled IMT. Will do so recursively for superclasses,
2910 // and note in the given set that the work was done.
2911 static void PrepareClass(Runtime* runtime,
2912 Handle<mirror::Class> h_klass,
2913 std::unordered_set<std::string>* done)
2914 REQUIRES_SHARED(Locks::mutator_lock_) {
2915 if (!h_klass->ShouldHaveImt()) {
2916 return;
2917 }
2918
2919 std::string name;
2920 name = h_klass->GetDescriptor(&name);
2921
2922 if (done->find(name) != done->end()) {
2923 return;
2924 }
2925 done->insert(name);
2926
2927 if (h_klass->HasSuperClass()) {
2928 StackHandleScope<1> h(Thread::Current());
2929 PrepareClass(runtime, h.NewHandle<mirror::Class>(h_klass->GetSuperClass()), done);
2930 }
2931
2932 if (!h_klass->IsTemp()) {
2933 runtime->GetClassLinker()->FillIMTAndConflictTables(h_klass.Get());
2934 }
2935 }
2936};
2937
Igor Murashkin37743352014-11-13 14:38:00 -08002938struct OatdumpArgs : public CmdlineArgs {
2939 protected:
2940 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002941
Igor Murashkin37743352014-11-13 14:38:00 -08002942 virtual ParseStatus ParseCustom(const StringPiece& option,
2943 std::string* error_msg) OVERRIDE {
2944 {
2945 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
2946 if (base_parse != kParseUnknownArgument) {
2947 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002948 }
2949 }
2950
Igor Murashkin37743352014-11-13 14:38:00 -08002951 if (option.starts_with("--oat-file=")) {
2952 oat_filename_ = option.substr(strlen("--oat-file=")).data();
2953 } else if (option.starts_with("--image=")) {
2954 image_location_ = option.substr(strlen("--image=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002955 } else if (option == "--no-dump:vmap") {
2956 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01002957 } else if (option =="--dump:code_info_stack_maps") {
2958 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002959 } else if (option == "--no-disassemble") {
2960 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00002961 } else if (option =="--header-only") {
2962 dump_header_only_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002963 } else if (option.starts_with("--symbolize=")) {
2964 oat_filename_ = option.substr(strlen("--symbolize=")).data();
2965 symbolize_ = true;
David Srbecky2fdd03c2016-03-10 15:32:37 +00002966 } else if (option.starts_with("--only-keep-debug")) {
2967 only_keep_debug_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002968 } else if (option.starts_with("--class-filter=")) {
2969 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002970 } else if (option.starts_with("--method-filter=")) {
2971 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002972 } else if (option.starts_with("--list-classes")) {
2973 list_classes_ = true;
2974 } else if (option.starts_with("--list-methods")) {
2975 list_methods_ = true;
2976 } else if (option.starts_with("--export-dex-to=")) {
2977 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
2978 } else if (option.starts_with("--addr2instr=")) {
2979 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
2980 *error_msg = "Address conversion failed";
2981 return kParseError;
2982 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002983 } else if (option.starts_with("--app-image=")) {
2984 app_image_ = option.substr(strlen("--app-image=")).data();
2985 } else if (option.starts_with("--app-oat=")) {
2986 app_oat_ = option.substr(strlen("--app-oat=")).data();
Andreas Gampe9fded872016-09-25 16:08:35 -07002987 } else if (option.starts_with("--dump-imt=")) {
2988 imt_dump_ = option.substr(strlen("--dump-imt=")).data();
2989 } else if (option == "--dump-imt-stats") {
2990 imt_stat_dump_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002991 } else {
2992 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002993 }
2994
Igor Murashkin37743352014-11-13 14:38:00 -08002995 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002996 }
2997
Igor Murashkin37743352014-11-13 14:38:00 -08002998 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
2999 // Infer boot image location from the image location if possible.
3000 if (boot_image_location_ == nullptr) {
3001 boot_image_location_ = image_location_;
3002 }
3003
3004 // Perform the parent checks.
3005 ParseStatus parent_checks = Base::ParseChecks(error_msg);
3006 if (parent_checks != kParseOk) {
3007 return parent_checks;
3008 }
3009
3010 // Perform our own checks.
3011 if (image_location_ == nullptr && oat_filename_ == nullptr) {
3012 *error_msg = "Either --image or --oat-file must be specified";
3013 return kParseError;
3014 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
3015 *error_msg = "Either --image or --oat-file must be specified but not both";
3016 return kParseError;
3017 }
3018
3019 return kParseOk;
3020 }
3021
3022 virtual std::string GetUsage() const {
3023 std::string usage;
3024
3025 usage +=
3026 "Usage: oatdump [options] ...\n"
3027 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
3028 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
3029 "\n"
3030 // Either oat-file or image is required.
3031 " --oat-file=<file.oat>: specifies an input oat filename.\n"
3032 " Example: --oat-file=/system/framework/boot.oat\n"
3033 "\n"
3034 " --image=<file.art>: specifies an input image location.\n"
3035 " Example: --image=/system/framework/boot.art\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003036 "\n"
3037 " --app-image=<file.art>: specifies an input app image. Must also have a specified\n"
3038 " boot image and app oat file.\n"
3039 " Example: --app-image=app.art\n"
3040 "\n"
3041 " --app-oat=<file.odex>: specifies an input app oat.\n"
3042 " Example: --app-oat=app.odex\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003043 "\n";
3044
3045 usage += Base::GetUsage();
3046
3047 usage += // Optional.
Igor Murashkin37743352014-11-13 14:38:00 -08003048 " --no-dump:vmap may be used to disable vmap dumping.\n"
3049 " Example: --no-dump:vmap\n"
3050 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01003051 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
3052 " Example: --dump:code_info_stack_maps\n"
3053 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003054 " --no-disassemble may be used to disable disassembly.\n"
3055 " Example: --no-disassemble\n"
3056 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00003057 " --header-only may be used to print only the oat header.\n"
3058 " Example: --header-only\n"
3059 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003060 " --list-classes may be used to list target file classes (can be used with filters).\n"
3061 " Example: --list-classes\n"
3062 " Example: --list-classes --class-filter=com.example.foo\n"
3063 "\n"
3064 " --list-methods may be used to list target file methods (can be used with filters).\n"
3065 " Example: --list-methods\n"
3066 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
3067 "\n"
3068 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
3069 " Example: --symbolize=/system/framework/boot.oat\n"
3070 "\n"
David Srbecky2fdd03c2016-03-10 15:32:37 +00003071 " --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
3072 " .rodata and .text sections are omitted in the output file to save space.\n"
3073 " Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
3074 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003075 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
3076 " Example: --class-filter=com.example.foo\n"
3077 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003078 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
3079 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003080 "\n"
3081 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
3082 " Example: --export-dex-to=/data/local/tmp\n"
3083 "\n"
3084 " --addr2instr=<address>: output matching method disassembled code from relative\n"
3085 " address (e.g. PC from crash dump)\n"
3086 " Example: --addr2instr=0x00001a3b\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003087 "\n"
3088 " --dump-imt=<file.txt>: output IMT collisions (if any) for the given receiver\n"
3089 " types and interface methods in the given file. The file\n"
3090 " is read line-wise, and each line should either be a class\n"
3091 " name or descriptor, or a class name/descriptor and a prefix\n"
3092 " of a complete method name.\n"
3093 " Example: --dump-imt=imt.txt\n"
3094 "\n"
3095 " --dump-imt-stats: output IMT statistics for the given boot image\n"
3096 " Example: --dump-imt-stats"
Igor Murashkin37743352014-11-13 14:38:00 -08003097 "\n";
3098
3099 return usage;
3100 }
3101
3102 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07003103 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003104 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00003105 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07003106 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003107 std::string elf_filename_prefix_;
Andreas Gampe9fded872016-09-25 16:08:35 -07003108 std::string imt_dump_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003109 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01003110 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003111 bool disassemble_code_ = true;
3112 bool symbolize_ = false;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003113 bool only_keep_debug_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003114 bool list_classes_ = false;
3115 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003116 bool dump_header_only_ = false;
Andreas Gampe9fded872016-09-25 16:08:35 -07003117 bool imt_stat_dump_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003118 uint32_t addr2instr_ = 0;
3119 const char* export_dex_location_ = nullptr;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003120 const char* app_image_ = nullptr;
3121 const char* app_oat_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003122};
3123
Igor Murashkin37743352014-11-13 14:38:00 -08003124struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
3125 virtual bool NeedsRuntime() OVERRIDE {
3126 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003127
Igor Murashkin37743352014-11-13 14:38:00 -08003128 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
3129 bool absolute_addresses = (args_->oat_filename_ == nullptr);
3130
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003131 oat_dumper_options_.reset(new OatDumperOptions(
Igor Murashkin37743352014-11-13 14:38:00 -08003132 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01003133 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08003134 args_->disassemble_code_,
3135 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003136 args_->class_filter_,
3137 args_->method_filter_,
3138 args_->list_classes_,
3139 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00003140 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003141 args_->export_dex_location_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003142 args_->app_image_,
3143 args_->app_oat_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003144 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08003145
Andreas Gampe9fded872016-09-25 16:08:35 -07003146 return (args_->boot_image_location_ != nullptr ||
3147 args_->image_location_ != nullptr ||
3148 !args_->imt_dump_.empty()) &&
Igor Murashkin37743352014-11-13 14:38:00 -08003149 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003150 }
3151
Igor Murashkin37743352014-11-13 14:38:00 -08003152 virtual bool ExecuteWithoutRuntime() OVERRIDE {
3153 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08003154 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003155
Mathieu Chartierd424d082014-10-15 10:31:46 -07003156 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08003157
Andreas Gampec24f3992014-12-17 20:40:11 -08003158 if (args_->symbolize_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00003159 // ELF has special kind of section called SHT_NOBITS which allows us to create
3160 // sections which exist but their data is omitted from the ELF file to save space.
3161 // This is what "strip --only-keep-debug" does when it creates separate ELF file
3162 // with only debug data. We use it in similar way to exclude .rodata and .text.
3163 bool no_bits = args_->only_keep_debug_;
3164 return SymbolizeOat(args_->oat_filename_, args_->output_name_, no_bits) == EXIT_SUCCESS;
Andreas Gampec24f3992014-12-17 20:40:11 -08003165 } else {
3166 return DumpOat(nullptr,
3167 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003168 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08003169 args_->os_) == EXIT_SUCCESS;
3170 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07003171 }
3172
Igor Murashkin37743352014-11-13 14:38:00 -08003173 virtual bool ExecuteWithRuntime(Runtime* runtime) {
3174 CHECK(args_ != nullptr);
3175
Andreas Gampe9fded872016-09-25 16:08:35 -07003176 if (!args_->imt_dump_.empty()) {
3177 return IMTDumper::DumpImt(runtime, args_->imt_dump_);
3178 }
3179
3180 if (args_->imt_stat_dump_) {
3181 return IMTDumper::DumpImtStats(runtime, runtime->GetClassLinker()->GetBootClassPath());
3182 }
3183
Igor Murashkin37743352014-11-13 14:38:00 -08003184 if (args_->oat_filename_ != nullptr) {
3185 return DumpOat(runtime,
3186 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003187 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08003188 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003189 }
Igor Murashkin37743352014-11-13 14:38:00 -08003190
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003191 return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003192 }
3193
Igor Murashkin37743352014-11-13 14:38:00 -08003194 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
3195};
Andreas Gampe00b25f32014-09-17 21:49:05 -07003196
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003197} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07003198
3199int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08003200 art::OatdumpMain main;
3201 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07003202}