blob: f2062c39cbd7070ab49c3a5f7e576fd4d79ba0a8 [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 Marko63dccbbe2016-09-21 13:51:10 +0100157 builder_->PrepareDynamicSection(elf_file->GetPath(),
158 rodata_size,
159 text_size,
160 oat_file_->BssSize(),
161 oat_file_->BssRootsOffset());
Vladimir Marko944da602016-02-19 12:27:55 +0000162 builder_->WriteDynamicSection();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700163
David Srbecky5d950762016-03-07 20:47:29 +0000164 Walk();
165 for (const auto& trampoline : debug::MakeTrampolineInfos(oat_file_->GetOatHeader())) {
166 method_debug_infos_.push_back(trampoline);
167 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700168
David Srbecky5d950762016-03-07 20:47:29 +0000169 debug::WriteDebugInfo(builder_.get(),
170 ArrayRef<const debug::MethodDebugInfo>(method_debug_infos_),
171 dwarf::DW_DEBUG_FRAME_FORMAT,
172 true /* write_oat_patches */);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700173
David Srbecky6d8c8f02015-10-26 10:57:09 +0000174 builder_->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700175
Vladimir Marko10c13562015-11-25 14:33:36 +0000176 return builder_->Good();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700177 }
178
David Srbecky5d950762016-03-07 20:47:29 +0000179 void Walk() {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700180 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
181 for (size_t i = 0; i < oat_dex_files.size(); i++) {
182 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700183 CHECK(oat_dex_file != nullptr);
David Srbecky5d950762016-03-07 20:47:29 +0000184 WalkOatDexFile(oat_dex_file);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700185 }
186 }
187
David Srbecky5d950762016-03-07 20:47:29 +0000188 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700189 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700190 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
191 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700192 return;
193 }
194 for (size_t class_def_index = 0;
195 class_def_index < dex_file->NumClassDefs();
196 class_def_index++) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700197 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
198 OatClassType type = oat_class.GetType();
199 switch (type) {
200 case kOatClassAllCompiled:
201 case kOatClassSomeCompiled:
David Srbecky5d950762016-03-07 20:47:29 +0000202 WalkOatClass(oat_class, *dex_file, class_def_index);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700203 break;
204
205 case kOatClassNoneCompiled:
206 case kOatClassMax:
207 // Ignore.
208 break;
209 }
210 }
211 }
212
David Srbecky5d950762016-03-07 20:47:29 +0000213 void WalkOatClass(const OatFile::OatClass& oat_class,
214 const DexFile& dex_file,
215 uint32_t class_def_index) {
216 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700217 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700218 if (class_data == nullptr) { // empty class such as a marker interface?
219 return;
220 }
221 // Note: even if this is an interface or a native class, we still have to walk it, as there
222 // might be a static initializer.
223 ClassDataItemIterator it(dex_file, class_data);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700224 uint32_t class_method_idx = 0;
David Srbecky5d950762016-03-07 20:47:29 +0000225 for (; it.HasNextStaticField(); it.Next()) { /* skip */ }
226 for (; it.HasNextInstanceField(); it.Next()) { /* skip */ }
227 for (; it.HasNextDirectMethod() || it.HasNextVirtualMethod(); it.Next()) {
228 WalkOatMethod(oat_class.GetOatMethod(class_method_idx++),
229 dex_file,
230 class_def_index,
231 it.GetMemberIndex(),
232 it.GetMethodCodeItem(),
233 it.GetMethodAccessFlags());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700234 }
235 DCHECK(!it.HasNext());
236 }
237
David Srbecky5d950762016-03-07 20:47:29 +0000238 void WalkOatMethod(const OatFile::OatMethod& oat_method,
239 const DexFile& dex_file,
240 uint32_t class_def_index,
241 uint32_t dex_method_index,
242 const DexFile::CodeItem* code_item,
243 uint32_t method_access_flags) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700244 if ((method_access_flags & kAccAbstract) != 0) {
245 // Abstract method, no code.
246 return;
247 }
David Srbecky5d950762016-03-07 20:47:29 +0000248 const OatHeader& oat_header = oat_file_->GetOatHeader();
249 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
250 if (method_header == nullptr || method_header->GetCodeSize() == 0) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700251 // No code.
252 return;
253 }
254
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100255 uint32_t entry_point = oat_method.GetCodeOffset() - oat_header.GetExecutableOffset();
256 // Clear Thumb2 bit.
257 const void* code_address = EntryPointToCodePointer(reinterpret_cast<void*>(entry_point));
258
David Srbecky5d950762016-03-07 20:47:29 +0000259 debug::MethodDebugInfo info = debug::MethodDebugInfo();
260 info.trampoline_name = nullptr;
261 info.dex_file = &dex_file;
262 info.class_def_index = class_def_index;
263 info.dex_method_index = dex_method_index;
264 info.access_flags = method_access_flags;
265 info.code_item = code_item;
266 info.isa = oat_header.GetInstructionSet();
267 info.deduped = !seen_offsets_.insert(oat_method.GetCodeOffset()).second;
268 info.is_native_debuggable = oat_header.IsNativeDebuggable();
269 info.is_optimized = method_header->IsOptimized();
270 info.is_code_address_text_relative = true;
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100271 info.code_address = reinterpret_cast<uintptr_t>(code_address);
David Srbecky5d950762016-03-07 20:47:29 +0000272 info.code_size = method_header->GetCodeSize();
273 info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
274 info.code_info = info.is_optimized ? method_header->GetOptimizedCodeInfoPtr() : nullptr;
275 info.cfi = ArrayRef<uint8_t>();
276 method_debug_infos_.push_back(info);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700277 }
278
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700279 private:
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700280 const OatFile* oat_file_;
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800281 std::unique_ptr<ElfBuilder<ElfTypes> > builder_;
David Srbecky5d950762016-03-07 20:47:29 +0000282 std::vector<debug::MethodDebugInfo> method_debug_infos_;
283 std::unordered_set<uint32_t> seen_offsets_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700284 const std::string output_name_;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000285 bool no_bits_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700286};
287
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700288class OatDumperOptions {
289 public:
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100290 OatDumperOptions(bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100291 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700292 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700293 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800294 const char* class_filter,
295 const char* method_filter,
296 bool list_classes,
297 bool list_methods,
David Brazdilc03d7b62016-03-02 12:18:03 +0000298 bool dump_header_only,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800299 const char* export_dex_location,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800300 const char* app_image,
301 const char* app_oat,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800302 uint32_t addr2instr)
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100303 : dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100304 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700305 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700306 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800307 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000308 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800309 list_classes_(list_classes),
310 list_methods_(list_methods),
David Brazdilc03d7b62016-03-02 12:18:03 +0000311 dump_header_only_(dump_header_only),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800312 export_dex_location_(export_dex_location),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800313 app_image_(app_image),
314 app_oat_(app_oat),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800315 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800316 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700317
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700318 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100319 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700320 const bool disassemble_code_;
321 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800322 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000323 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800324 const bool list_classes_;
325 const bool list_methods_;
David Brazdilc03d7b62016-03-02 12:18:03 +0000326 const bool dump_header_only_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800327 const char* const export_dex_location_;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800328 const char* const app_image_;
329 const char* const app_oat_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800330 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700331 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700332};
333
Elliott Hughese3c845c2012-02-28 17:23:01 -0800334class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700335 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100336 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000337 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800338 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700339 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800340 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800341 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
342 disassembler_(Disassembler::Create(instruction_set_,
Andreas Gampe372f3a32016-08-19 10:49:06 -0700343 new DisassemblerOptions(
344 options_.absolute_addresses_,
345 oat_file.Begin(),
346 oat_file.End(),
347 true /* can_read_literals_ */,
348 Is64BitInstructionSet(instruction_set_)
349 ? &Thread::DumpThreadOffset<PointerSize::k64>
350 : &Thread::DumpThreadOffset<PointerSize::k32>))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800351 CHECK(options_.class_loader_ != nullptr);
352 CHECK(options_.class_filter_ != nullptr);
353 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800354 AddAllOffsets();
355 }
356
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700357 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700358 delete disassembler_;
359 }
360
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800361 InstructionSet GetInstructionSet() {
362 return instruction_set_;
363 }
364
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700365 bool Dump(std::ostream& os) {
366 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800367 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700368
369 os << "MAGIC:\n";
370 os << oat_header.GetMagic() << "\n\n";
371
Jeff Hao4b07a6e2016-01-15 12:50:44 -0800372 os << "LOCATION:\n";
373 os << oat_file_.GetLocation() << "\n\n";
374
Brian Carlstromaded5f72011-10-07 17:15:04 -0700375 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800376 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700377
Elliott Hughesa72ec822012-03-05 17:12:22 -0800378 os << "INSTRUCTION SET:\n";
379 os << oat_header.GetInstructionSet() << "\n\n";
380
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700381 {
382 std::unique_ptr<const InstructionSetFeatures> features(
383 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
384 oat_header.GetInstructionSetFeaturesBitmap()));
385 os << "INSTRUCTION SET FEATURES:\n";
386 os << features->GetFeatureString() << "\n\n";
387 }
Dave Allison70202782013-10-22 17:52:19 -0700388
Brian Carlstromaded5f72011-10-07 17:15:04 -0700389 os << "DEX FILE COUNT:\n";
390 os << oat_header.GetDexFileCount() << "\n\n";
391
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800392#define DUMP_OAT_HEADER_OFFSET(label, offset) \
393 os << label " OFFSET:\n"; \
394 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800395 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800396 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
397 } \
398 os << StringPrintf("\n\n");
399
400 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
401 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
402 GetInterpreterToInterpreterBridgeOffset);
403 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
404 GetInterpreterToCompiledCodeBridgeOffset);
405 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
406 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800407 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
408 GetQuickGenericJniTrampolineOffset);
409 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
410 GetQuickImtConflictTrampolineOffset);
411 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
412 GetQuickResolutionTrampolineOffset);
413 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
414 GetQuickToInterpreterBridgeOffset);
415#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700416
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700417 os << "IMAGE PATCH DELTA:\n";
418 os << StringPrintf("%d (0x%08x)\n\n",
419 oat_header.GetImagePatchDelta(),
420 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700421
Brian Carlstrom28db0122012-10-18 16:20:41 -0700422 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
423 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
424
425 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800426 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700427
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700428 // Print the key-value store.
429 {
430 os << "KEY VALUE STORE:\n";
431 size_t index = 0;
432 const char* key;
433 const char* value;
434 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
435 os << key << " = " << value << "\n";
436 index++;
437 }
438 os << "\n";
439 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700440
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800441 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700442 os << "BEGIN:\n";
443 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700444
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700445 os << "END:\n";
446 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
447 }
448
449 os << "SIZE:\n";
450 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700451
452 os << std::flush;
453
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800454 // If set, adjust relative address to be searched
455 if (options_.addr2instr_ != 0) {
456 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
457 os << "SEARCH ADDRESS (executable offset + input):\n";
458 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
459 }
460
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700461 // Dumping the dex file overview is compact enough to do even if header only.
462 DexFileData cumulative;
463 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
464 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
465 CHECK(oat_dex_file != nullptr);
466 std::string error_msg;
467 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
468 if (dex_file == nullptr) {
469 os << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation() << "': "
470 << error_msg;
471 continue;
472 }
473 DexFileData data(*dex_file);
474 os << "Dex file data for " << dex_file->GetLocation() << "\n";
475 data.Dump(os);
476 os << "\n";
477 cumulative.Add(data);
478 }
479 os << "Cumulative dex file data\n";
480 cumulative.Dump(os);
481 os << "\n";
482
David Brazdilc03d7b62016-03-02 12:18:03 +0000483 if (!options_.dump_header_only_) {
484 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
485 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
486 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800487
David Brazdilc03d7b62016-03-02 12:18:03 +0000488 // If file export selected skip file analysis
489 if (options_.export_dex_location_) {
490 if (!ExportDexFile(os, *oat_dex_file)) {
491 success = false;
492 }
493 } else {
494 if (!DumpOatDexFile(os, *oat_dex_file)) {
495 success = false;
496 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800497 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700498 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700499 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000500
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700501 os << std::flush;
502 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700503 }
504
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800505 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700506 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
507 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800508 return 0; // Address not in oat file
509 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800510 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
511 reinterpret_cast<uintptr_t>(oat_file_.Begin());
512 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800513 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800514 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800515 return end_offset - begin_offset;
516 }
517
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800518 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700519 return oat_file_.GetOatHeader().GetInstructionSet();
520 }
521
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700522 const void* GetQuickOatCode(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800523 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
524 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700525 CHECK(oat_dex_file != nullptr);
526 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700527 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
528 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700529 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
530 << "': " << error_msg;
531 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800532 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700533 const DexFile::ClassDef* class_def =
David Sehr9aa352e2016-09-15 18:13:52 -0700534 OatDexFile::FindClassDef(*dex_file, descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700535 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700536 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100537 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800538 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100539 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800540 }
541 }
542 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700543 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800544 }
545
Brian Carlstromaded5f72011-10-07 17:15:04 -0700546 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800547 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800548 // We don't know the length of the code for each method, but we need to know where to stop
549 // when disassembling. What we do know is that a region of code will be followed by some other
550 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
551 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800552 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
553 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700554 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700555 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700556 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
557 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700558 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
559 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800560 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800561 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800562 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800563 for (size_t class_def_index = 0;
564 class_def_index < dex_file->NumClassDefs();
565 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800566 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100567 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700568 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700569 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800570 ClassDataItemIterator it(*dex_file, class_data);
571 SkipAllFields(it);
572 uint32_t class_method_index = 0;
573 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100574 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800575 it.Next();
576 }
577 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100578 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800579 it.Next();
580 }
581 }
582 }
583 }
584
585 // If the last thing in the file is code for a method, there won't be an offset for the "next"
586 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
587 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800588 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800589 }
590
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700591 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
592 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
593 }
594
Elliott Hughese3c845c2012-02-28 17:23:01 -0800595 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800596 uint32_t code_offset = oat_method.GetCodeOffset();
597 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
598 code_offset &= ~0x1;
599 }
600 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800601 offsets_.insert(oat_method.GetVmapTableOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800602 }
603
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700604 // Dex file data, may be for multiple different dex files.
605 class DexFileData {
606 public:
607 DexFileData() {}
608
609 explicit DexFileData(const DexFile& dex_file)
610 : num_string_ids_(dex_file.NumStringIds()),
611 num_method_ids_(dex_file.NumMethodIds()),
612 num_field_ids_(dex_file.NumFieldIds()),
613 num_type_ids_(dex_file.NumTypeIds()),
614 num_class_defs_(dex_file.NumClassDefs()) {
615 for (size_t class_def_index = 0; class_def_index < num_class_defs_; ++class_def_index) {
616 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
617 WalkClass(dex_file, class_def);
618 }
619 }
620
621 void Add(const DexFileData& other) {
622 AddAll(unique_string_ids_from_code_, other.unique_string_ids_from_code_);
623 num_string_ids_from_code_ += other.num_string_ids_from_code_;
624 AddAll(dex_code_item_ptrs_, other.dex_code_item_ptrs_);
625 dex_code_bytes_ += other.dex_code_bytes_;
626 num_string_ids_ += other.num_string_ids_;
627 num_method_ids_ += other.num_method_ids_;
628 num_field_ids_ += other.num_field_ids_;
629 num_type_ids_ += other.num_type_ids_;
630 num_class_defs_ += other.num_class_defs_;
631 }
632
633 void Dump(std::ostream& os) {
634 os << "Num string ids: " << num_string_ids_ << "\n";
635 os << "Num method ids: " << num_method_ids_ << "\n";
636 os << "Num field ids: " << num_field_ids_ << "\n";
637 os << "Num type ids: " << num_type_ids_ << "\n";
638 os << "Num class defs: " << num_class_defs_ << "\n";
639 os << "Unique strings loaded from dex code: " << unique_string_ids_from_code_.size() << "\n";
640 os << "Total strings loaded from dex code: " << num_string_ids_from_code_ << "\n";
641 os << "Number of unique dex code items: " << dex_code_item_ptrs_.size() << "\n";
642 os << "Total number of dex code bytes: " << dex_code_bytes_ << "\n";
643 }
644
645 private:
646 void WalkClass(const DexFile& dex_file, const DexFile::ClassDef& class_def) {
647 const uint8_t* class_data = dex_file.GetClassData(class_def);
648 if (class_data == nullptr) { // empty class such as a marker interface?
649 return;
650 }
651 ClassDataItemIterator it(dex_file, class_data);
652 SkipAllFields(it);
653 while (it.HasNextDirectMethod()) {
654 WalkCodeItem(dex_file, it.GetMethodCodeItem());
655 it.Next();
656 }
657 while (it.HasNextVirtualMethod()) {
658 WalkCodeItem(dex_file, it.GetMethodCodeItem());
659 it.Next();
660 }
661 DCHECK(!it.HasNext());
662 }
663
664 void WalkCodeItem(const DexFile& dex_file, const DexFile::CodeItem* code_item) {
665 if (code_item == nullptr) {
666 return;
667 }
668 const size_t code_item_size = code_item->insns_size_in_code_units_;
669 const uint16_t* code_ptr = code_item->insns_;
670 const uint16_t* code_end = code_item->insns_ + code_item_size;
671
672 // If we inserted a new dex code item pointer, add to total code bytes.
673 if (dex_code_item_ptrs_.insert(code_ptr).second) {
674 dex_code_bytes_ += code_item_size * sizeof(code_ptr[0]);
675 }
676
677 while (code_ptr < code_end) {
678 const Instruction* inst = Instruction::At(code_ptr);
679 switch (inst->Opcode()) {
680 case Instruction::CONST_STRING: {
681 const uint32_t string_index = inst->VRegB_21c();
682 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
683 ++num_string_ids_from_code_;
684 break;
685 }
686 case Instruction::CONST_STRING_JUMBO: {
687 const uint32_t string_index = inst->VRegB_31c();
688 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
689 ++num_string_ids_from_code_;
690 break;
691 }
692 default:
693 break;
694 }
695
696 code_ptr += inst->SizeInCodeUnits();
697 }
698 }
699
700 // Unique string ids loaded from dex code.
701 std::set<StringReference, StringReferenceComparator> unique_string_ids_from_code_;
702
703 // Total string ids loaded from dex code.
704 size_t num_string_ids_from_code_ = 0;
705
706 // Unique code pointers.
707 std::set<const void*> dex_code_item_ptrs_;
708
709 // Total "unique" dex code bytes.
710 size_t dex_code_bytes_ = 0;
711
712 // Other dex ids.
713 size_t num_string_ids_ = 0;
714 size_t num_method_ids_ = 0;
715 size_t num_field_ids_ = 0;
716 size_t num_type_ids_ = 0;
717 size_t num_class_defs_ = 0;
718 };
719
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700720 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
721 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800722 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700723 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800724 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800725 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700726
Andreas Gampe2ba88952016-04-29 17:52:07 -0700727 const uint8_t* const oat_file_begin = oat_dex_file.GetOatFile()->Begin();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100728 const uint8_t* const vdex_file_begin = oat_dex_file.GetOatFile()->DexBegin();
729
730 // Print data range of the dex file embedded inside the corresponding vdex file.
Andreas Gampe2ba88952016-04-29 17:52:07 -0700731 const uint8_t* const dex_file_pointer = oat_dex_file.GetDexFilePointer();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100732 uint32_t dex_offset = dchecked_integral_cast<uint32_t>(dex_file_pointer - vdex_file_begin);
Andreas Gampe2ba88952016-04-29 17:52:07 -0700733 os << StringPrintf("dex-file: 0x%08x..0x%08x\n",
734 dex_offset,
735 dchecked_integral_cast<uint32_t>(dex_offset + oat_dex_file.FileSize() - 1));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700736
Andreas Gampe2ba88952016-04-29 17:52:07 -0700737 // Create the dex file early. A lot of print-out things depend on it.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700738 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700739 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
740 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700741 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700742 os << std::flush;
743 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700744 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100745
Andreas Gampe2ba88952016-04-29 17:52:07 -0700746 // Print lookup table, if it exists.
747 if (oat_dex_file.GetLookupTableData() != nullptr) {
748 uint32_t table_offset = dchecked_integral_cast<uint32_t>(
749 oat_dex_file.GetLookupTableData() - oat_file_begin);
David Sehr9aa352e2016-09-15 18:13:52 -0700750 uint32_t table_size = TypeLookupTable::RawDataLength(dex_file->NumClassDefs());
Andreas Gampe2ba88952016-04-29 17:52:07 -0700751 os << StringPrintf("type-table: 0x%08x..0x%08x\n",
752 table_offset,
753 table_offset + table_size - 1);
754 }
755
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100756 VariableIndentationOutputStream vios(&os);
757 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800758 for (size_t class_def_index = 0;
759 class_def_index < dex_file->NumClassDefs();
760 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700761 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
762 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800763
764 // TODO: Support regex
765 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
766 continue;
767 }
768
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700769 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100770 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700771 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
772 class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100773 << " (" << oat_class.GetStatus() << ")"
774 << " (" << oat_class.GetType() << ")\n";
775 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700776 if (options_.list_classes_) {
777 continue;
778 }
779 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700780 success = false;
781 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800782 if (stop_analysis) {
783 os << std::flush;
784 return success;
785 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700786 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700787 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700788 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700789 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700790 }
791
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800792 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
793 std::string error_msg;
794 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
795
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700796 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800797 if (dex_file == nullptr) {
798 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
799 return false;
800 }
801 size_t fsize = oat_dex_file.FileSize();
802
803 // Some quick checks just in case
804 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
805 os << "Invalid dex file\n";
806 return false;
807 }
808
809 // Verify output directory exists
810 if (!OS::DirectoryExists(options_.export_dex_location_)) {
811 // TODO: Extend OS::DirectoryExists if symlink support is required
812 os << options_.export_dex_location_ << " output directory not found or symlink\n";
813 return false;
814 }
815
816 // Beautify path names
817 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
818 return false;
819 }
820
821 std::string dex_orig_name;
822 size_t dex_orig_pos = dex_file_location.rfind('/');
823 if (dex_orig_pos == std::string::npos)
824 dex_orig_name = dex_file_location;
825 else
826 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
827
828 // A more elegant approach to efficiently name user installed apps is welcome
829 if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
830 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
831 size_t apk_orig_pos = dex_file_location.rfind('/');
832 if (apk_orig_pos != std::string::npos) {
833 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
834 }
835 }
836
837 std::string out_dex_path(options_.export_dex_location_);
838 if (out_dex_path.back() != '/') {
839 out_dex_path.append("/");
840 }
841 out_dex_path.append(dex_orig_name);
842 out_dex_path.append("_export.dex");
843 if (out_dex_path.length() > PATH_MAX) {
844 return false;
845 }
846
847 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
848 if (file.get() == nullptr) {
849 os << "Failed to open output dex file " << out_dex_path;
850 return false;
851 }
852
853 if (!file->WriteFully(dex_file->Begin(), fsize)) {
854 os << "Failed to write dex file";
855 file->Erase();
856 return false;
857 }
858
859 if (file->FlushCloseOrErase() != 0) {
860 os << "Flush and close failed";
861 return false;
862 }
863
864 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
865 os << std::flush;
866
867 return true;
868 }
869
Elliott Hughese3c845c2012-02-28 17:23:01 -0800870 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700871 while (it.HasNextStaticField()) {
872 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700873 }
Ian Rogers0571d352011-11-03 19:51:38 -0700874 while (it.HasNextInstanceField()) {
875 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700876 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800877 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700878
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100879 bool DumpOatClass(VariableIndentationOutputStream* vios,
880 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700881 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700882 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800883 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -0700884 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700885 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100886 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700887 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800888 }
889 ClassDataItemIterator it(dex_file, class_data);
890 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700891 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700892 while (it.HasNextDirectMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100893 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700894 it.GetMemberIndex(), it.GetMethodCodeItem(),
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700895 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700896 success = false;
897 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800898 if (addr_found) {
899 *stop_analysis = true;
900 return success;
901 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700902 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700903 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700904 }
Ian Rogers0571d352011-11-03 19:51:38 -0700905 while (it.HasNextVirtualMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100906 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700907 it.GetMemberIndex(), it.GetMethodCodeItem(),
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700908 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700909 success = false;
910 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800911 if (addr_found) {
912 *stop_analysis = true;
913 return success;
914 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700915 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700916 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700917 }
Ian Rogers0571d352011-11-03 19:51:38 -0700918 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100919 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700920 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700921 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800922
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700923 static constexpr uint32_t kPrologueBytes = 16;
924
925 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
926 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
927
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100928 bool DumpOatMethod(VariableIndentationOutputStream* vios,
929 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700930 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700931 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800932 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700933 uint32_t method_access_flags, bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700934 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800935
936 // TODO: Support regex
937 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
938 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000939 return success;
940 }
941
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800942 std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100943 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
944 class_method_index, pretty_method.c_str(),
945 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800946 if (options_.list_methods_) return success;
947
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800948 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
949 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
950 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
951 uint32_t code_offset = oat_method.GetCodeOffset();
952 uint32_t code_size = oat_method.GetQuickCodeSize();
953 if (resolved_addr2instr_ != 0) {
954 if (resolved_addr2instr_ > code_offset + code_size) {
955 return success;
956 } else {
957 *addr_found = true; // stop analyzing file at next iteration
958 }
959 }
960
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100961 // Everything below is indented at least once.
962 ScopedIndentation indent1(vios);
963
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800964 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100965 vios->Stream() << "DEX CODE:\n";
966 ScopedIndentation indent2(vios);
967 DumpDexCode(vios->Stream(), dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700968 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700969
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700970 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700971 std::unique_ptr<verifier::MethodVerifier> verifier;
972 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700973 // We need to have the handle scope stay live until after the verifier since the verifier has
974 // a handle to the dex cache from hs.
975 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100976 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
977 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700978 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100979 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700980 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -0700981 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800982 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100983 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800984 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100985 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100986 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100987 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700988 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100989 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700990 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
991 oat_method_offsets_offset, oat_file_.Size());
992 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100993 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700994 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800995 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700996
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100997 ScopedIndentation indent2(vios);
998 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700999 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
1000 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001001 vios->Stream() << StringPrintf("WARNING: "
1002 "code offset 0x%08x is past end of file 0x%08zx.\n",
1003 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001004 success = false;
1005 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001006 vios->Stream() << "\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001007 }
1008 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001009 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001010 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
1011 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -07001012
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001013 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001014 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001015 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001016 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001017 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001018 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001019 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
1020 method_header_offset, oat_file_.Size());
1021 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001022 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001023 return false;
1024 }
1025
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001026 ScopedIndentation indent2(vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001027 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001028 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001029 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001030 }
1031 uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001032 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001033 if (vmap_table_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001034 vios->Stream() << StringPrintf("WARNING: "
1035 "vmap table offset 0x%08x is past end of file 0x%08zx. "
1036 "vmap table offset was loaded from offset 0x%08x.\n",
1037 vmap_table_offset, oat_file_.Size(),
1038 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001039 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001040 } else if (options_.dump_vmap_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001041 DumpVmapData(vios, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001042 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001043 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001044 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001045 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001046
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001047 ScopedIndentation indent2(vios);
1048 vios->Stream()
1049 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
1050 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
1051 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
1052 vios->Stream() << "\n";
1053 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
1054 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
1055 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001056 }
1057 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001058 // Based on spill masks from QuickMethodFrameInfo so placed
1059 // after it is dumped, but useful for understanding quick
1060 // code, so dumped here.
1061 ScopedIndentation indent2(vios);
1062 DumpVregLocations(vios->Stream(), oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001063 }
1064 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001065 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001066 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
1067 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001068 ScopedIndentation indent2(vios);
1069 vios->Stream() << StringPrintf("WARNING: "
1070 "code size offset 0x%08x is past end of file 0x%08zx.",
1071 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001072 success = false;
1073 } else {
1074 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001075 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1076 uint64_t aligned_code_end = aligned_code_begin + code_size;
1077
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001078 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001079 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001080 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001081 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
1082 code_offset,
1083 code_size_offset,
1084 code_size,
1085 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001086
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001087 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001088 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001089 vios->Stream() << StringPrintf("WARNING: "
1090 "start of code at 0x%08x is past end of file 0x%08zx.",
1091 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001092 success = false;
1093 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001094 vios->Stream() << StringPrintf(
1095 "WARNING: "
1096 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
1097 "code size is 0x%08x loaded from offset 0x%08x.\n",
1098 aligned_code_end, oat_file_.Size(),
1099 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001100 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001101 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001102 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001103 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001104 }
1105 }
1106 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001107 vios->Stream() << StringPrintf(
1108 "WARNING: "
1109 "code size %d is bigger than max expected threshold of %d. "
1110 "code size is 0x%08x loaded from offset 0x%08x.\n",
1111 code_size, kMaxCodeSize,
1112 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001113 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001114 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001115 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001116 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001117 }
1118 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001119 } else if (options_.disassemble_code_) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001120 DumpCode(vios, oat_method, code_item, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001121 }
1122 }
1123 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001124 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001125 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001126 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001127
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001128 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1129 if (spill_mask == 0) {
1130 return;
1131 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001132 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001133 for (size_t i = 0; i < 32; i++) {
1134 if ((spill_mask & (1 << i)) != 0) {
1135 if (is_float) {
1136 os << "fr" << i;
1137 } else {
1138 os << "r" << i;
1139 }
1140 spill_mask ^= 1 << i; // clear bit
1141 if (spill_mask != 0) {
1142 os << ", ";
1143 } else {
1144 break;
1145 }
1146 }
1147 }
1148 os << ")";
1149 }
1150
Roland Levillain442b46a2015-02-18 16:54:21 +00001151 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001152 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001153 const OatFile::OatMethod& oat_method,
1154 const DexFile::CodeItem* code_item) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001155 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1156 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001157 const void* raw_code_info = oat_method.GetVmapTable();
1158 if (raw_code_info != nullptr) {
1159 CodeInfo code_info(raw_code_info);
1160 DCHECK(code_item != nullptr);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001161 ScopedIndentation indent1(vios);
1162 DumpCodeInfo(vios, code_info, oat_method, *code_item);
Roland Levillain442b46a2015-02-18 16:54:21 +00001163 }
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001164 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
1165 // We don't encode the size in the table, so just emit that we have quickened
1166 // information.
1167 ScopedIndentation indent(vios);
1168 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001169 } else {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001170 // Otherwise, there is nothing to display.
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001171 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001172 }
1173
1174 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001175 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001176 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001177 const OatFile::OatMethod& oat_method,
Roland Levillain442b46a2015-02-18 16:54:21 +00001178 const DexFile::CodeItem& code_item) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001179 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001180 oat_method.GetCodeOffset(),
1181 code_item.registers_size_,
1182 options_.dump_code_info_stack_maps_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001183 }
1184
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001185 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1186 const DexFile::CodeItem* code_item) {
1187 if (code_item != nullptr) {
1188 size_t num_locals_ins = code_item->registers_size_;
1189 size_t num_ins = code_item->ins_size_;
1190 size_t num_locals = num_locals_ins - num_ins;
1191 size_t num_outs = code_item->outs_size_;
1192
1193 os << "vr_stack_locations:";
1194 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1195 // For readability, delimit the different kinds of VRs.
1196 if (reg == num_locals_ins) {
1197 os << "\n\tmethod*:";
1198 } else if (reg == num_locals && num_ins > 0) {
1199 os << "\n\tins:";
1200 } else if (reg == 0 && num_locals > 0) {
1201 os << "\n\tlocals:";
1202 }
1203
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001204 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1205 code_item,
1206 oat_method.GetCoreSpillMask(),
1207 oat_method.GetFpSpillMask(),
1208 oat_method.GetFrameSizeInBytes(),
1209 reg,
1210 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001211 os << " v" << reg << "[sp + #" << offset << "]";
1212 }
1213
1214 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1215 if (out_reg == 0) {
1216 os << "\n\touts:";
1217 }
1218
1219 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1220 os << " v" << out_reg << "[sp + #" << offset << "]";
1221 }
1222
1223 os << "\n";
1224 }
1225 }
1226
Ian Rogersb23a7722012-10-09 16:54:26 -07001227 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001228 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001229 size_t i = 0;
1230 while (i < code_item->insns_size_in_code_units_) {
1231 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001232 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1233 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001234 i += instruction->SizeInCodeUnits();
1235 }
1236 }
1237 }
1238
Roland Levillainf2650d12015-05-28 14:53:28 +01001239 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1240 // the optimizing compiler?
1241 static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1242 const DexFile::CodeItem* code_item) {
1243 // If the native GC map is null and the Dex `code_item` is not
1244 // null, then this method has been compiled with the optimizing
1245 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001246 return oat_method.GetQuickCode() != nullptr &&
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001247 oat_method.GetVmapTable() != nullptr &&
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001248 code_item != nullptr;
1249 }
1250
1251 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1252 // the dextodex compiler?
1253 static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
1254 const DexFile::CodeItem* code_item) {
1255 // If the quick code is null, the Dex `code_item` is not
1256 // null, and the vmap table is not null, then this method has been compiled
1257 // with the dextodex compiler.
1258 return oat_method.GetQuickCode() == nullptr &&
1259 oat_method.GetVmapTable() != nullptr &&
1260 code_item != nullptr;
Roland Levillainf2650d12015-05-28 14:53:28 +01001261 }
1262
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001263 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001264 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001265 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001266 const DexFile* dex_file,
1267 const DexFile::ClassDef& class_def,
1268 const DexFile::CodeItem* code_item,
1269 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001270 if ((method_access_flags & kAccNative) == 0) {
1271 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001272 Runtime* const runtime = Runtime::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001273 Handle<mirror::DexCache> dex_cache(
Mathieu Chartierf284d442016-06-02 11:48:30 -07001274 hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file, nullptr)));
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001275 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001276 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001277 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
David Brazdil15fc7292016-09-02 14:13:18 +01001278 class_def, code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001279 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001280
1281 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001282 }
1283
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001284 // The StackMapsHelper provides the stack maps in the native PC order.
1285 // For identical native PCs, the order from the CodeInfo is preserved.
1286 class StackMapsHelper {
1287 public:
1288 explicit StackMapsHelper(const uint8_t* raw_code_info)
1289 : code_info_(raw_code_info),
1290 encoding_(code_info_.ExtractEncoding()),
1291 number_of_stack_maps_(code_info_.GetNumberOfStackMaps(encoding_)),
1292 indexes_(),
1293 offset_(static_cast<size_t>(-1)),
1294 stack_map_index_(0u) {
1295 if (number_of_stack_maps_ != 0u) {
1296 // Check if native PCs are ordered.
1297 bool ordered = true;
1298 StackMap last = code_info_.GetStackMapAt(0u, encoding_);
1299 for (size_t i = 1; i != number_of_stack_maps_; ++i) {
1300 StackMap current = code_info_.GetStackMapAt(i, encoding_);
1301 if (last.GetNativePcOffset(encoding_.stack_map_encoding) >
1302 current.GetNativePcOffset(encoding_.stack_map_encoding)) {
1303 ordered = false;
1304 break;
1305 }
1306 last = current;
1307 }
1308 if (!ordered) {
1309 // Create indirection indexes for access in native PC order. We do not optimize
1310 // for the fact that there can currently be only two separately ordered ranges,
1311 // namely normal stack maps and catch-point stack maps.
1312 indexes_.resize(number_of_stack_maps_);
1313 std::iota(indexes_.begin(), indexes_.end(), 0u);
1314 std::sort(indexes_.begin(),
1315 indexes_.end(),
1316 [this](size_t lhs, size_t rhs) {
1317 StackMap left = code_info_.GetStackMapAt(lhs, encoding_);
1318 uint32_t left_pc = left.GetNativePcOffset(encoding_.stack_map_encoding);
1319 StackMap right = code_info_.GetStackMapAt(rhs, encoding_);
1320 uint32_t right_pc = right.GetNativePcOffset(encoding_.stack_map_encoding);
1321 // If the PCs are the same, compare indexes to preserve the original order.
1322 return (left_pc < right_pc) || (left_pc == right_pc && lhs < rhs);
1323 });
1324 }
1325 offset_ = GetStackMapAt(0).GetNativePcOffset(encoding_.stack_map_encoding);
1326 }
1327 }
1328
1329 const CodeInfo& GetCodeInfo() const {
1330 return code_info_;
1331 }
1332
1333 const CodeInfoEncoding& GetEncoding() const {
1334 return encoding_;
1335 }
1336
1337 size_t GetOffset() const {
1338 return offset_;
1339 }
1340
1341 StackMap GetStackMap() const {
1342 return GetStackMapAt(stack_map_index_);
1343 }
1344
1345 void Next() {
1346 ++stack_map_index_;
1347 offset_ = (stack_map_index_ == number_of_stack_maps_)
1348 ? static_cast<size_t>(-1)
1349 : GetStackMapAt(stack_map_index_).GetNativePcOffset(encoding_.stack_map_encoding);
1350 }
1351
1352 private:
1353 StackMap GetStackMapAt(size_t i) const {
1354 if (!indexes_.empty()) {
1355 i = indexes_[i];
1356 }
1357 DCHECK_LT(i, number_of_stack_maps_);
1358 return code_info_.GetStackMapAt(i, encoding_);
1359 }
1360
1361 const CodeInfo code_info_;
1362 const CodeInfoEncoding encoding_;
1363 const size_t number_of_stack_maps_;
1364 dchecked_vector<size_t> indexes_; // Used if stack map native PCs are not ordered.
1365 size_t offset_;
1366 size_t stack_map_index_;
1367 };
1368
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001369 void DumpCode(VariableIndentationOutputStream* vios,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001370 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1371 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001372 const void* quick_code = oat_method.GetQuickCode();
1373
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001374 if (code_size == 0) {
1375 code_size = oat_method.GetQuickCodeSize();
1376 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001377 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001378 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001379 return;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001380 } else if (!bad_input && IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1381 // The optimizing compiler outputs its CodeInfo data in the vmap table.
1382 StackMapsHelper helper(oat_method.GetVmapTable());
1383 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1384 size_t offset = 0;
1385 while (offset < code_size) {
1386 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
1387 if (offset == helper.GetOffset()) {
1388 ScopedIndentation indent1(vios);
1389 StackMap stack_map = helper.GetStackMap();
1390 DCHECK(stack_map.IsValid());
1391 stack_map.Dump(vios,
1392 helper.GetCodeInfo(),
1393 helper.GetEncoding(),
1394 oat_method.GetCodeOffset(),
1395 code_item->registers_size_);
1396 do {
1397 helper.Next();
1398 // There may be multiple stack maps at a given PC. We display only the first one.
1399 } while (offset == helper.GetOffset());
1400 }
1401 DCHECK_LT(offset, helper.GetOffset());
1402 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001403 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001404 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1405 size_t offset = 0;
1406 while (offset < code_size) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001407 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001408 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001409 }
1410 }
1411
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001412 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001413 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001414 const OatDumperOptions& options_;
1415 uint32_t resolved_addr2instr_;
Andreas Gampe372f3a32016-08-19 10:49:06 -07001416 const InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001417 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001418 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001419};
1420
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001421class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001422 public:
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001423 ImageDumper(std::ostream* os,
1424 gc::space::ImageSpace& image_space,
1425 const ImageHeader& image_header,
1426 OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001427 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001428 vios_(os),
1429 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001430 image_space_(image_space),
1431 image_header_(image_header),
1432 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001433
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001434 bool Dump() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001435 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001436 std::ostream& indent_os = vios_.Stream();
1437
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001438 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001439
Jeff Haodcdc85b2015-12-04 14:06:18 -08001440 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1441
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001442 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001443
Mathieu Chartiere401d142015-04-22 13:56:20 -07001444 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1445
1446 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1447 auto section = static_cast<ImageHeader::ImageSections>(i);
1448 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1449 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001450
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001451 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001452
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001453 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001454
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001455 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1456
1457 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1458
1459 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001460
Alex Lighta59dd802014-07-02 16:28:08 -07001461 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1462
Igor Murashkin46774762014-10-22 11:37:02 -07001463 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1464
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001465 {
1466 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001467 static_assert(arraysize(image_roots_descriptions_) ==
1468 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001469 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1470 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1471 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001472 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001473 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001474 if (image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001475 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001476 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001477 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001478 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1479 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001480 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001481 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1482 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001483 run++;
1484 } else {
1485 break;
1486 }
1487 }
1488 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001489 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001490 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001491 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001492 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001493 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001494 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001495 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001496 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001497 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001498 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001499 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001500 }
1501 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001502 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001503
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001504 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001505 os << "METHOD ROOTS\n";
1506 static_assert(arraysize(image_methods_descriptions_) ==
1507 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1508 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1509 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1510 const char* description = image_methods_descriptions_[i];
1511 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001512 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001513 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001514 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001515 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001516
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001517 Runtime* const runtime = Runtime::Current();
1518 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001519 std::string image_filename = image_space_.GetImageFilename();
1520 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001521 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001522 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001523 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001524 const OatFile* oat_file = image_space_.GetOatFile();
Alex Lighta59dd802014-07-02 16:28:08 -07001525 if (oat_file == nullptr) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001526 oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location);
1527 }
1528 if (oat_file == nullptr) {
1529 oat_file = OatFile::Open(oat_location,
1530 oat_location,
1531 nullptr,
1532 nullptr,
1533 false,
1534 /*low_4gb*/false,
1535 nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001536 &error_msg);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001537 }
1538 if (oat_file == nullptr) {
1539 os << "OAT FILE NOT FOUND: " << error_msg << "\n";
1540 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001541 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001542 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001543
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001544 stats_.oat_file_bytes = oat_file->Size();
1545
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001546 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001547
Mathieu Chartier02e25112013-08-14 16:14:24 -07001548 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001549 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001550 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1551 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001552 }
1553
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001554 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001555
Jeff Haodcdc85b2015-12-04 14:06:18 -08001556 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001557 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001558 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001559 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001560 {
1561 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1562 heap->FlushAllocStack();
1563 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001564 // Since FlushAllocStack() above resets the (active) allocation
1565 // stack. Need to revoke the thread-local allocation stacks that
1566 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001567 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001568 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001569 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001570 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001571 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001572 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001573 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001574 {
1575 ReaderMutexLock mu(self, *class_linker->DexLock());
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001576 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001577 mirror::DexCache* dex_cache =
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001578 down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001579 if (dex_cache != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01001580 dex_caches_.insert(dex_cache);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001581 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001582 }
1583 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001584 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001585 // Dump the normal objects before ArtMethods.
1586 image_space_.GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1587 indent_os << "\n";
1588 // TODO: Dump fields.
1589 // Dump methods after.
Jeff Haodcdc85b2015-12-04 14:06:18 -08001590 DumpArtMethodVisitor visitor(this);
Mathieu Chartiere42888f2016-04-14 10:49:19 -07001591 image_header_.VisitPackedArtMethods(&visitor,
1592 image_space_.Begin(),
1593 image_header_.GetPointerSize());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001594 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001595 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001596 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001597 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001598 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001599 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001600 size_t data_size = image_header_.GetDataSize(); // stored size in file.
1601 if (file == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001602 LOG(WARNING) << "Failed to find image in " << image_filename;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001603 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001604 stats_.file_bytes = file->GetLength();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001605 // If the image is compressed, adjust to decompressed size.
1606 size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader);
1607 if (image_header_.GetStorageMode() == ImageHeader::kStorageModeUncompressed) {
1608 DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image";
1609 }
1610 stats_.file_bytes += uncompressed_size - data_size;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001611 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001612 size_t header_bytes = sizeof(ImageHeader);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001613 const auto& object_section = image_header_.GetImageSection(ImageHeader::kSectionObjects);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001614 const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1615 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001616 const auto& dex_cache_arrays_section = image_header_.GetImageSection(
1617 ImageHeader::kSectionDexCacheArrays);
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001618 const auto& intern_section = image_header_.GetImageSection(
1619 ImageHeader::kSectionInternedStrings);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001620 const auto& class_table_section = image_header_.GetImageSection(
1621 ImageHeader::kSectionClassTable);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001622 const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1623
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001624 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001625
1626 // Objects are kObjectAlignment-aligned.
1627 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1628 if (object_section.Offset() > header_bytes) {
1629 stats_.alignment_bytes += object_section.Offset() - header_bytes;
1630 }
1631
1632 // Field section is 4-byte aligned.
1633 constexpr size_t kFieldSectionAlignment = 4U;
1634 uint32_t end_objects = object_section.Offset() + object_section.Size();
1635 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
1636 stats_.alignment_bytes += field_section.Offset() - end_objects;
1637
1638 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
1639 uint32_t end_fields = field_section.Offset() + field_section.Size();
1640 CHECK_ALIGNED(method_section.Offset(), 4);
1641 stats_.alignment_bytes += method_section.Offset() - end_fields;
1642
1643 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
1644 uint32_t end_methods = method_section.Offset() + method_section.Size();
1645 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
1646 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
1647
1648 // Intern table is 8-byte aligned.
1649 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
1650 CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
1651 stats_.alignment_bytes += intern_section.Offset() - end_caches;
1652
1653 // Add space between intern table and class table.
1654 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
1655 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
1656
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001657 // Add space between end of image data and bitmap. Expect the bitmap to be page-aligned.
1658 const size_t bitmap_offset = sizeof(ImageHeader) + data_size;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001659 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001660 stats_.alignment_bytes += RoundUp(bitmap_offset, kPageSize) - bitmap_offset;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001661
Mathieu Chartiere401d142015-04-22 13:56:20 -07001662 stats_.bitmap_bytes += bitmap_section.Size();
1663 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01001664 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01001665 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001666 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001667 stats_.class_table_bytes += class_table_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001668 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001669 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001670
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001671 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001672
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001673 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001674 }
1675
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001676 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001677 class DumpArtMethodVisitor : public ArtMethodVisitor {
1678 public:
1679 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
1680
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001681 virtual void Visit(ArtMethod* method) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001682 std::ostream& indent_os = image_dumper_->vios_.Stream();
1683 indent_os << method << " " << " ArtMethod: " << PrettyMethod(method) << "\n";
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001684 image_dumper_->DumpMethod(method, indent_os);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001685 indent_os << "\n";
1686 }
1687
1688 private:
1689 ImageDumper* const image_dumper_;
1690 };
1691
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001692 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001693 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001694 CHECK(type != nullptr);
1695 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001696 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001697 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001698 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001699 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001700 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001701 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001702 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001703 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001704 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001705 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001706 }
1707 }
1708
Mathieu Chartierc7853442015-03-27 14:35:38 -07001709 static void PrintField(std::ostream& os, ArtField* field, mirror::Object* obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001710 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001711 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08001712 switch (field->GetTypeAsPrimitiveType()) {
1713 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08001714 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001715 break;
1716 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001717 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001718 break;
1719 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001720 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001721 break;
1722 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001723 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001724 break;
1725 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07001726 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001727 break;
1728 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07001729 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001730 break;
1731 case Primitive::kPrimBoolean:
Fred Shih37f05ef2014-07-16 18:38:08 -07001732 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1733 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001734 break;
1735 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07001736 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001737 break;
1738 case Primitive::kPrimNot: {
1739 // Get the value, don't compute the type unless it is non-null as we don't want
1740 // to cause class loading.
1741 mirror::Object* value = field->GetObj(obj);
1742 if (value == nullptr) {
1743 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07001744 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08001745 // Grab the field type without causing resolution.
Mathieu Chartierdaaf3262015-03-24 13:30:28 -07001746 mirror::Class* field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08001747 if (field_type != nullptr) {
1748 PrettyObjectValue(os, field_type, value);
1749 } else {
1750 os << StringPrintf("%p %s\n", value,
1751 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
1752 }
Ian Rogers50239c72013-06-17 14:53:22 -07001753 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001754 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07001755 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001756 default:
1757 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
1758 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08001759 }
1760 }
1761
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001762 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001763 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001764 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001765 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001766 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001767 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001768 for (ArtField& field : klass->GetIFields()) {
1769 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001770 }
1771 }
1772
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001773 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001774 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001775 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001776
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001777 const void* GetQuickOatCodeBegin(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001778 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001779 image_header_.GetPointerSize());
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001780 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001781 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001782 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001783 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001784 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001785 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001786 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001787 }
1788
Mathieu Chartiere401d142015-04-22 13:56:20 -07001789 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001790 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001791 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1792 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001793 return 0;
1794 }
1795 return oat_code_begin[-1];
1796 }
1797
Mathieu Chartiere401d142015-04-22 13:56:20 -07001798 const void* GetQuickOatCodeEnd(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001799 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001800 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001801 if (oat_code_begin == nullptr) {
1802 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001803 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001804 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001805 }
1806
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001807 static void Callback(mirror::Object* obj, void* arg) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001808 DCHECK(obj != nullptr);
1809 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001810 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001811 if (!state->InDumpSpace(obj)) {
1812 return;
1813 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001814
1815 size_t object_bytes = obj->SizeOf();
1816 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1817 state->stats_.object_bytes += object_bytes;
1818 state->stats_.alignment_bytes += alignment_bytes;
1819
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001820 std::ostream& os = state->vios_.Stream();
1821
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001822 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001823 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001824 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
1825 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001826 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001827 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001828 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
1829 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08001830 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001831 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001832 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001833 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001834 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001835 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001836 ScopedIndentation indent1(&state->vios_);
1837 DumpFields(os, obj, obj_class);
Andreas Gampe542451c2016-07-26 09:02:02 -07001838 const PointerSize image_pointer_size = state->image_header_.GetPointerSize();
Ian Rogersd5b32602012-02-26 16:40:04 -08001839 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001840 auto* obj_array = obj->AsObjectArray<mirror::Object>();
1841 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001842 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001843 size_t run = 0;
1844 for (int32_t j = i + 1; j < length; j++) {
1845 if (value == obj_array->Get(j)) {
1846 run++;
1847 } else {
1848 break;
1849 }
1850 }
1851 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001852 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001853 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001854 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001855 i = i + run;
1856 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001857 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001858 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001859 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001860 }
1861 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001862 mirror::Class* klass = obj->AsClass();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001863 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001864 os << "STATICS:\n";
1865 ScopedIndentation indent2(&state->vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001866 for (ArtField& field : klass->GetSFields()) {
1867 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001868 }
1869 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001870 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01001871 auto it = state->dex_caches_.find(obj);
1872 if (it != state->dex_caches_.end()) {
1873 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001874 const auto& field_section = state->image_header_.GetImageSection(
1875 ImageHeader::kSectionArtFields);
1876 const auto& method_section = state->image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001877 size_t num_methods = dex_cache->NumResolvedMethods();
1878 if (num_methods != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001879 os << "Methods (size=" << num_methods << "):\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01001880 ScopedIndentation indent2(&state->vios_);
1881 auto* resolved_methods = dex_cache->GetResolvedMethods();
1882 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001883 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_methods,
1884 i,
1885 image_pointer_size);
Vladimir Marko05792b92015-08-03 11:56:49 +01001886 size_t run = 0;
1887 for (size_t j = i + 1;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001888 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_methods,
1889 j,
1890 image_pointer_size);
1891 ++j) {
1892 ++run;
1893 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001894 if (run == 0) {
1895 os << StringPrintf("%zd: ", i);
1896 } else {
1897 os << StringPrintf("%zd to %zd: ", i, i + run);
1898 i = i + run;
1899 }
1900 std::string msg;
1901 if (elem == nullptr) {
1902 msg = "null";
1903 } else if (method_section.Contains(
1904 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
1905 msg = PrettyMethod(reinterpret_cast<ArtMethod*>(elem));
1906 } else {
1907 msg = "<not in method section>";
1908 }
1909 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07001910 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001911 }
1912 size_t num_fields = dex_cache->NumResolvedFields();
1913 if (num_fields != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001914 os << "Fields (size=" << num_fields << "):\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01001915 ScopedIndentation indent2(&state->vios_);
1916 auto* resolved_fields = dex_cache->GetResolvedFields();
1917 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001918 auto* elem = mirror::DexCache::GetElementPtrSize(
1919 resolved_fields, i, image_pointer_size);
Vladimir Marko05792b92015-08-03 11:56:49 +01001920 size_t run = 0;
1921 for (size_t j = i + 1;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001922 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_fields,
1923 j,
1924 image_pointer_size);
1925 ++j) {
1926 ++run;
1927 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001928 if (run == 0) {
1929 os << StringPrintf("%zd: ", i);
1930 } else {
1931 os << StringPrintf("%zd to %zd: ", i, i + run);
1932 i = i + run;
1933 }
1934 std::string msg;
1935 if (elem == nullptr) {
1936 msg = "null";
1937 } else if (field_section.Contains(
1938 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
1939 msg = PrettyField(reinterpret_cast<ArtField*>(elem));
1940 } else {
1941 msg = "<not in field section>";
1942 }
1943 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07001944 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001945 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001946 size_t num_types = dex_cache->NumResolvedTypes();
1947 if (num_types != 0u) {
1948 os << "Types (size=" << num_types << "):\n";
1949 ScopedIndentation indent2(&state->vios_);
1950 auto* resolved_types = dex_cache->GetResolvedTypes();
1951 for (size_t i = 0; i < num_types; ++i) {
1952 auto* elem = resolved_types[i].Read();
1953 size_t run = 0;
1954 for (size_t j = i + 1; j != num_types && elem == resolved_types[j].Read(); ++j) {
1955 ++run;
1956 }
1957 if (run == 0) {
1958 os << StringPrintf("%zd: ", i);
1959 } else {
1960 os << StringPrintf("%zd to %zd: ", i, i + run);
1961 i = i + run;
1962 }
1963 std::string msg;
1964 if (elem == nullptr) {
1965 msg = "null";
1966 } else {
1967 msg = PrettyClass(elem);
1968 }
1969 os << StringPrintf("%p %s\n", elem, msg.c_str());
1970 }
1971 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001972 }
1973 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07001974 std::string temp;
1975 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001976 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001977
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001978 void DumpMethod(ArtMethod* method, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001979 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001980 DCHECK(method != nullptr);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001981 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
1982 const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
Andreas Gampe542451c2016-07-26 09:02:02 -07001983 const PointerSize pointer_size = image_header_.GetPointerSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01001984 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
1985 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07001986 if (method->IsNative()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001987 bool first_occurrence;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001988 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
1989 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001990 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001991 stats_.native_to_managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001992 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001993 if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
1994 image_header_.GetPointerSize())) {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01001995 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001996 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -07001997 } else if (method->IsAbstract() || method->IsClassInitializer()) {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00001998 // Don't print information for these.
Mathieu Chartiere42888f2016-04-14 10:49:19 -07001999 } else if (method->IsRuntimeMethod()) {
2000 ImtConflictTable* table = method->GetImtConflictTable(image_header_.GetPointerSize());
2001 if (table != nullptr) {
2002 indent_os << "IMT conflict table " << table << " method: ";
2003 for (size_t i = 0, count = table->NumEntries(pointer_size); i < count; ++i) {
2004 indent_os << PrettyMethod(table->GetImplementationMethod(i, pointer_size)) << " ";
2005 }
2006 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002007 } else {
2008 const DexFile::CodeItem* code_item = method->GetCodeItem();
2009 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002010 stats_.dex_instruction_bytes += dex_instruction_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002011
2012 bool first_occurrence;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002013 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002014 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01002015 // Method compiled with the optimizing compiler have no vmap table.
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002016 vmap_table_bytes = ComputeOatSize(method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002017 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002018 stats_.vmap_table_bytes += vmap_table_bytes;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002019 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002020 }
2021
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002022 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2023 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002024 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002025 stats_.managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002026 if (method->IsConstructor()) {
2027 if (method->IsStatic()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002028 stats_.class_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002029 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002030 stats_.large_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002031 }
2032 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002033 stats_.large_method_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002034 }
2035 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002036 stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002037
Igor Murashkin7617abd2015-07-10 18:27:47 -07002038 uint32_t method_access_flags = method->GetAccessFlags();
2039
Mathieu Chartiere401d142015-04-22 13:56:20 -07002040 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002041 indent_os << StringPrintf("SIZE: Dex Instructions=%zd StackMaps=%zd AccessFlags=0x%x\n",
2042 dex_instruction_bytes,
2043 vmap_table_bytes,
Igor Murashkin7617abd2015-07-10 18:27:47 -07002044 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002045
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002046 size_t total_size = dex_instruction_bytes +
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002047 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002048
2049 double expansion =
2050 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002051 stats_.ComputeOutliers(total_size, expansion, method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002052 }
2053 }
2054
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002055 std::set<const void*> already_seen_;
2056 // Compute the size of the given data within the oat file and whether this is the first time
2057 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002058 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002059 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002060 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002061 already_seen_.insert(oat_data);
2062 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002063 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002064 }
2065 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002066 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002067
2068 public:
2069 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002070 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002071 size_t file_bytes;
2072
2073 size_t header_bytes;
2074 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002075 size_t art_field_bytes;
2076 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01002077 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002078 size_t interned_strings_bytes;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002079 size_t class_table_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07002080 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002081 size_t alignment_bytes;
2082
2083 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002084 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002085 size_t managed_to_native_code_bytes;
2086 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07002087 size_t class_initializer_code_bytes;
2088 size_t large_initializer_code_bytes;
2089 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002090
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002091 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002092
2093 size_t dex_instruction_bytes;
2094
Mathieu Chartiere401d142015-04-22 13:56:20 -07002095 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002096 std::vector<size_t> method_outlier_size;
2097 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002098 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002099
Roland Levillain3887c462015-08-12 18:15:42 +01002100 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002101 : oat_file_bytes(0),
2102 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002103 header_bytes(0),
2104 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002105 art_field_bytes(0),
2106 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01002107 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002108 interned_strings_bytes(0),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002109 class_table_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07002110 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002111 alignment_bytes(0),
2112 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002113 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002114 managed_to_native_code_bytes(0),
2115 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07002116 class_initializer_code_bytes(0),
2117 large_initializer_code_bytes(0),
2118 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002119 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002120 dex_instruction_bytes(0) {}
2121
Elliott Hughesa0e18062012-04-13 15:59:59 -07002122 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002123 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002124 size_t bytes;
2125 size_t count;
2126 };
2127 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2128 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002129
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002130 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002131 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2132 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002133 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002134 it->second.count += 1;
2135 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002136 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002137 }
2138 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002139
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002140 double PercentOfOatBytes(size_t size) {
2141 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2142 }
2143
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002144 double PercentOfFileBytes(size_t size) {
2145 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2146 }
2147
2148 double PercentOfObjectBytes(size_t size) {
2149 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2150 }
2151
Mathieu Chartiere401d142015-04-22 13:56:20 -07002152 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002153 method_outlier_size.push_back(total_size);
2154 method_outlier_expansion.push_back(expansion);
2155 method_outlier.push_back(method);
2156 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002157
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002158 void DumpOutliers(std::ostream& os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002159 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002160 size_t sum_of_sizes = 0;
2161 size_t sum_of_sizes_squared = 0;
2162 size_t sum_of_expansion = 0;
2163 size_t sum_of_expansion_squared = 0;
2164 size_t n = method_outlier_size.size();
Mathieu Chartier1ebf8d32016-06-09 11:51:27 -07002165 if (n <= 1) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08002166 return;
2167 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002168 for (size_t i = 0; i < n; i++) {
2169 size_t cur_size = method_outlier_size[i];
2170 sum_of_sizes += cur_size;
2171 sum_of_sizes_squared += cur_size * cur_size;
2172 double cur_expansion = method_outlier_expansion[i];
2173 sum_of_expansion += cur_expansion;
2174 sum_of_expansion_squared += cur_expansion * cur_expansion;
2175 }
2176 size_t size_mean = sum_of_sizes / n;
2177 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2178 double expansion_mean = sum_of_expansion / n;
2179 double expansion_variance =
2180 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2181
2182 // Dump methods whose size is a certain number of standard deviations from the mean
2183 size_t dumped_values = 0;
2184 size_t skipped_values = 0;
2185 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2186 size_t cur_size_variance = i * i * size_variance;
2187 bool first = true;
2188 for (size_t j = 0; j < n; j++) {
2189 size_t cur_size = method_outlier_size[j];
2190 if (cur_size > size_mean) {
2191 size_t cur_var = cur_size - size_mean;
2192 cur_var = cur_var * cur_var;
2193 if (cur_var > cur_size_variance) {
2194 if (dumped_values > 20) {
2195 if (i == 1) {
2196 skipped_values++;
2197 } else {
2198 i = 2; // jump to counting for 1 standard deviation
2199 break;
2200 }
2201 } else {
2202 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002203 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002204 first = false;
2205 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002206 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002207 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002208 method_outlier_size[j] = 0; // don't consider this method again
2209 dumped_values++;
2210 }
2211 }
2212 }
2213 }
2214 }
2215 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002216 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002217 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002218 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002219 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002220
2221 // Dump methods whose expansion is a certain number of standard deviations from the mean
2222 dumped_values = 0;
2223 skipped_values = 0;
2224 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2225 double cur_expansion_variance = i * i * expansion_variance;
2226 bool first = true;
2227 for (size_t j = 0; j < n; j++) {
2228 double cur_expansion = method_outlier_expansion[j];
2229 if (cur_expansion > expansion_mean) {
2230 size_t cur_var = cur_expansion - expansion_mean;
2231 cur_var = cur_var * cur_var;
2232 if (cur_var > cur_expansion_variance) {
2233 if (dumped_values > 20) {
2234 if (i == 1) {
2235 skipped_values++;
2236 } else {
2237 i = 2; // jump to counting for 1 standard deviation
2238 break;
2239 }
2240 } else {
2241 if (first) {
2242 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002243 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002244 first = false;
2245 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002246 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002247 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002248 method_outlier_expansion[j] = 0.0; // don't consider this method again
2249 dumped_values++;
2250 }
2251 }
2252 }
2253 }
2254 }
2255 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002256 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002257 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002258 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002259 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002260 }
2261
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002262 void Dump(std::ostream& os, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002263 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002264 {
2265 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2266 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002267 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2268 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2269 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2270 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2271 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2272 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002273 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002274 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2275 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002276 header_bytes, PercentOfFileBytes(header_bytes),
2277 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002278 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2279 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002280 dex_cache_arrays_bytes,
2281 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002282 interned_strings_bytes,
2283 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002284 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002285 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002286 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2287 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002288 CHECK_EQ(file_bytes,
2289 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2290 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2291 bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002292 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002293
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002294 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002295 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002296 for (const auto& sizes_and_count : sizes_and_counts) {
2297 const std::string& descriptor(sizes_and_count.first);
2298 double average = static_cast<double>(sizes_and_count.second.bytes) /
2299 static_cast<double>(sizes_and_count.second.count);
2300 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002301 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002302 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002303 descriptor.c_str(), sizes_and_count.second.bytes,
2304 sizes_and_count.second.count, average, percent);
2305 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002306 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002307 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002308 CHECK_EQ(object_bytes, object_bytes_total);
2309
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002310 os << StringPrintf("oat_file_bytes = %8zd\n"
2311 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2312 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2313 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2314 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2315 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2316 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002317 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002318 managed_code_bytes,
2319 PercentOfOatBytes(managed_code_bytes),
2320 managed_to_native_code_bytes,
2321 PercentOfOatBytes(managed_to_native_code_bytes),
2322 native_to_managed_code_bytes,
2323 PercentOfOatBytes(native_to_managed_code_bytes),
2324 class_initializer_code_bytes,
2325 PercentOfOatBytes(class_initializer_code_bytes),
2326 large_initializer_code_bytes,
2327 PercentOfOatBytes(large_initializer_code_bytes),
2328 large_method_code_bytes,
2329 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002330 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002331 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002332 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002333 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2334 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002335 }
2336
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002337 os << "\n" << StringPrintf("vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002338 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002339 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002340
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002341 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2342 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002343 static_cast<double>(managed_code_bytes) /
2344 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002345 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002346 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002347 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002348
2349 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002350 }
2351 } stats_;
2352
2353 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002354 enum {
2355 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2356 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2357 kLargeConstructorDexBytes = 4000,
2358 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2359 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2360 kLargeMethodDexBytes = 16000
2361 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002362
2363 // For performance, use the *os_ directly for anything that doesn't need indentation
2364 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002365 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002366 VariableIndentationOutputStream vios_;
2367 ScopedIndentation indent1_;
2368
Ian Rogers1d54e732013-05-02 21:10:01 -07002369 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002370 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002371 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002372 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002373 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002374
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002375 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002376};
2377
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002378static int DumpImage(gc::space::ImageSpace* image_space,
2379 OatDumperOptions* options,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002380 std::ostream* os) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002381 const ImageHeader& image_header = image_space->GetImageHeader();
2382 if (!image_header.IsValid()) {
2383 fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
2384 return EXIT_FAILURE;
2385 }
2386 ImageDumper image_dumper(os, *image_space, image_header, options);
2387 if (!image_dumper.Dump()) {
2388 return EXIT_FAILURE;
2389 }
2390 return EXIT_SUCCESS;
2391}
2392
2393static int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002394 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002395 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002396 options->class_loader_ = &null_class_loader;
2397
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002398 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002399 if (options->app_image_ != nullptr) {
2400 if (options->app_oat_ == nullptr) {
2401 LOG(ERROR) << "Can not dump app image without app oat file";
Jeff Haodcdc85b2015-12-04 14:06:18 -08002402 return EXIT_FAILURE;
2403 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002404 // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
2405 // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
2406 // pointers into 32 bit pointer sized ArtMethods.
2407 std::string error_msg;
2408 std::unique_ptr<OatFile> oat_file(OatFile::Open(options->app_oat_,
2409 options->app_oat_,
2410 nullptr,
2411 nullptr,
2412 false,
2413 /*low_4gb*/true,
2414 nullptr,
2415 &error_msg));
2416 if (oat_file == nullptr) {
2417 LOG(ERROR) << "Failed to open oat file " << options->app_oat_ << " with error " << error_msg;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002418 return EXIT_FAILURE;
2419 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002420 std::unique_ptr<gc::space::ImageSpace> space(
2421 gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg));
2422 if (space == nullptr) {
2423 LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error "
2424 << error_msg;
2425 }
2426 // Open dex files for the image.
2427 std::vector<std::unique_ptr<const DexFile>> dex_files;
2428 if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
2429 LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
2430 << error_msg;
2431 }
2432 // Dump the actual image.
2433 int result = DumpImage(space.get(), options, os);
2434 if (result != EXIT_SUCCESS) {
2435 return result;
2436 }
2437 // Fall through to dump the boot images.
2438 }
2439
2440 gc::Heap* heap = runtime->GetHeap();
2441 CHECK(heap->HasBootImageSpace()) << "No image spaces";
2442 for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
2443 int result = DumpImage(image_space, options, os);
2444 if (result != EXIT_SUCCESS) {
2445 return result;
2446 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002447 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002448 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002449}
2450
Andreas Gampe00b25f32014-09-17 21:49:05 -07002451static int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
2452 std::ostream* os) {
2453 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2454
2455 Thread* self = Thread::Current();
2456 CHECK(self != nullptr);
2457 // Need well-known-classes.
2458 WellKnownClasses::Init(self->GetJniEnv());
2459
2460 // Need to register dex files to get a working dex cache.
2461 ScopedObjectAccess soa(self);
2462 ClassLinker* class_linker = runtime->GetClassLinker();
Mathieu Chartierd57d4542015-10-14 10:55:30 -07002463 runtime->GetOatFileManager().RegisterOatFile(std::unique_ptr<const OatFile>(oat_file));
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002464 std::vector<const DexFile*> class_path;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002465 for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
2466 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002467 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002468 CHECK(dex_file != nullptr) << error_msg;
Mathieu Chartierf284d442016-06-02 11:48:30 -07002469 class_linker->RegisterDexFile(*dex_file, nullptr);
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002470 class_path.push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002471 }
2472
2473 // Need a class loader.
Andreas Gampe00b25f32014-09-17 21:49:05 -07002474 // Fake that we're a compiler.
Mathieu Chartier966878d2016-01-14 14:33:29 -08002475 jobject class_loader = class_linker->CreatePathClassLoader(self, class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002476
2477 // Use the class loader while dumping.
2478 StackHandleScope<1> scope(self);
2479 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07002480 soa.Decode<mirror::ClassLoader>(class_loader));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002481 options->class_loader_ = &loader_handle;
2482
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002483 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002484 bool success = oat_dumper.Dump(*os);
2485 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2486}
2487
2488static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002489 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002490 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002491 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002492 options->class_loader_ = &null_class_loader;
2493
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002494 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002495 bool success = oat_dumper.Dump(*os);
2496 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2497}
2498
2499static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2500 std::ostream* os) {
2501 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002502 OatFile* oat_file = OatFile::Open(oat_filename,
2503 oat_filename,
2504 nullptr,
2505 nullptr,
2506 false,
2507 /*low_4gb*/false,
2508 nullptr,
2509 &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002510 if (oat_file == nullptr) {
2511 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2512 return EXIT_FAILURE;
2513 }
2514
2515 if (runtime != nullptr) {
2516 return DumpOatWithRuntime(runtime, oat_file, options, os);
2517 } else {
2518 return DumpOatWithoutRuntime(oat_file, options, os);
2519 }
2520}
2521
David Srbecky2fdd03c2016-03-10 15:32:37 +00002522static int SymbolizeOat(const char* oat_filename, std::string& output_name, bool no_bits) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002523 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002524 OatFile* oat_file = OatFile::Open(oat_filename,
2525 oat_filename,
2526 nullptr,
2527 nullptr,
2528 false,
2529 /*low_4gb*/false,
2530 nullptr,
2531 &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002532 if (oat_file == nullptr) {
2533 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2534 return EXIT_FAILURE;
2535 }
2536
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002537 bool result;
2538 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
2539 // files for 64-bit code in the past.
2540 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002541 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file, output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002542 result = oat_symbolizer.Symbolize();
2543 } else {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002544 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file, output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002545 result = oat_symbolizer.Symbolize();
2546 }
2547 if (!result) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002548 fprintf(stderr, "Failed to symbolize\n");
2549 return EXIT_FAILURE;
2550 }
2551
2552 return EXIT_SUCCESS;
2553}
2554
Andreas Gampe9fded872016-09-25 16:08:35 -07002555class IMTDumper {
2556 public:
2557 static bool DumpImt(Runtime* runtime, const std::string& imt_file) {
2558 std::vector<std::string> lines = ReadCommentedInputFromFile(imt_file);
2559 std::unordered_set<std::string> prepared;
2560
2561 for (const std::string& line : lines) {
2562 // A line should be either a class descriptor, in which case we will dump the complete IMT,
2563 // or a class descriptor and an interface method, in which case we will lookup the method,
2564 // determine its IMT slot, and check the class' IMT.
2565 size_t first_space = line.find(' ');
2566 if (first_space == std::string::npos) {
2567 DumpIMTForClass(runtime, line, &prepared);
2568 } else {
2569 DumpIMTForMethod(runtime,
2570 line.substr(0, first_space),
2571 line.substr(first_space + 1, std::string::npos),
2572 &prepared);
2573 }
2574 std::cerr << std::endl;
2575 }
2576
2577 return true;
2578 }
2579
2580 static bool DumpImtStats(Runtime* runtime, const std::vector<const DexFile*>& dex_files) {
2581 size_t wo_imt = 0;
2582 size_t w_imt = 0;
2583 std::map<size_t, size_t> histogram;
2584
2585 ClassLinker* class_linker = runtime->GetClassLinker();
2586 const PointerSize pointer_size = class_linker->GetImagePointerSize();
2587 std::unordered_set<std::string> prepared;
2588
2589 Thread* self = Thread::Current();
2590 ScopedObjectAccess soa(self);
2591 StackHandleScope<1> scope(self);
2592 MutableHandle<mirror::Class> h_klass(scope.NewHandle<mirror::Class>(nullptr));
2593
2594 for (const DexFile* dex_file : dex_files) {
2595 for (uint32_t class_def_index = 0;
2596 class_def_index != dex_file->NumClassDefs();
2597 ++class_def_index) {
2598 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
2599 const char* descriptor = dex_file->GetClassDescriptor(class_def);
2600 h_klass.Assign(class_linker->FindClass(self,
2601 descriptor,
2602 ScopedNullHandle<mirror::ClassLoader>()));
2603 if (h_klass.Get() == nullptr) {
2604 std::cerr << "Warning: could not load " << descriptor << std::endl;
2605 continue;
2606 }
2607
2608 if (HasNoIMT(runtime, h_klass, pointer_size, &prepared)) {
2609 wo_imt++;
2610 continue;
2611 }
2612
2613 ImTable* im_table = PrepareAndGetImTable(runtime, h_klass, pointer_size, &prepared);
2614 if (im_table == nullptr) {
2615 // Should not happen, but accept.
2616 wo_imt++;
2617 continue;
2618 }
2619
2620 w_imt++;
2621 for (size_t imt_index = 0; imt_index != ImTable::kSize; ++imt_index) {
2622 ArtMethod* ptr = im_table->Get(imt_index, pointer_size);
2623 if (ptr->IsRuntimeMethod()) {
2624 if (ptr->IsImtUnimplementedMethod()) {
2625 histogram[0]++;
2626 } else {
2627 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
2628 histogram[current_table->NumEntries(pointer_size)]++;
2629 }
2630 } else {
2631 histogram[1]++;
2632 }
2633 }
2634 }
2635 }
2636
2637 std::cerr << "IMT stats:"
2638 << std::endl << std::endl;
2639
2640 std::cerr << " " << w_imt << " classes with IMT."
2641 << std::endl << std::endl;
2642 std::cerr << " " << wo_imt << " classes without IMT (or copy from Object)."
2643 << std::endl << std::endl;
2644
2645 double sum_one = 0;
2646 size_t count_one = 0;
2647
2648 std::cerr << " " << "IMT histogram" << std::endl;
2649 for (auto& bucket : histogram) {
2650 std::cerr << " " << bucket.first << " " << bucket.second << std::endl;
2651 if (bucket.first > 0) {
2652 sum_one += bucket.second * bucket.first;
2653 count_one += bucket.second;
2654 }
2655 }
2656
2657 double count_zero = count_one + histogram[0];
2658 std::cerr << " Stats:" << std::endl;
2659 std::cerr << " Average depth (including empty): " << (sum_one / count_zero) << std::endl;
2660 std::cerr << " Average depth (excluding empty): " << (sum_one / count_one) << std::endl;
2661
2662 return true;
2663 }
2664
2665 private:
2666 // Check whether the given class has no IMT (or the one shared with java.lang.Object).
2667 static bool HasNoIMT(Runtime* runtime,
2668 Handle<mirror::Class> klass,
2669 const PointerSize pointer_size,
2670 std::unordered_set<std::string>* prepared)
2671 REQUIRES_SHARED(Locks::mutator_lock_) {
2672 if (klass->IsObjectClass() || !klass->ShouldHaveImt()) {
2673 return true;
2674 }
2675
2676 if (klass->GetImt(pointer_size) == nullptr) {
2677 PrepareClass(runtime, klass, prepared);
2678 }
2679
2680 mirror::Class* object_class = mirror::Class::GetJavaLangClass()->GetSuperClass();
2681 DCHECK(object_class->IsObjectClass());
2682
2683 bool result = klass->GetImt(pointer_size) == object_class->GetImt(pointer_size);
2684
2685 if (klass->GetIfTable() == nullptr) {
2686 DCHECK(result);
2687 }
2688
2689 return result;
2690 }
2691
2692 static void PrintTable(ImtConflictTable* table, PointerSize pointer_size)
2693 REQUIRES_SHARED(Locks::mutator_lock_) {
2694 if (table == nullptr) {
2695 std::cerr << " <No IMT?>" << std::endl;
2696 return;
2697 }
2698 size_t table_index = 0;
2699 for (;;) {
2700 ArtMethod* ptr = table->GetInterfaceMethod(table_index, pointer_size);
2701 if (ptr == nullptr) {
2702 return;
2703 }
2704 table_index++;
2705 std::cerr << " " << PrettyMethod(ptr, true) << std::endl;
2706 }
2707 }
2708
2709 static ImTable* PrepareAndGetImTable(Runtime* runtime,
2710 Thread* self,
2711 const std::string& class_name,
2712 const PointerSize pointer_size,
2713 mirror::Class** klass_out,
2714 std::unordered_set<std::string>* prepared)
2715 REQUIRES_SHARED(Locks::mutator_lock_) {
2716 if (class_name.empty()) {
2717 return nullptr;
2718 }
2719
2720 std::string descriptor;
2721 if (class_name[0] == 'L') {
2722 descriptor = class_name;
2723 } else {
2724 descriptor = DotToDescriptor(class_name.c_str());
2725 }
2726
2727 ScopedNullHandle<mirror::ClassLoader> null_handle;
2728
2729 mirror::Class* klass =
2730 runtime->GetClassLinker()->FindClass(self, descriptor.c_str(), null_handle);
2731
2732 if (klass == nullptr) {
2733 self->ClearException();
2734 std::cerr << "Did not find " << class_name << std::endl;
2735 *klass_out = nullptr;
2736 return nullptr;
2737 }
2738
2739 StackHandleScope<1> scope(Thread::Current());
2740 Handle<mirror::Class> h_klass = scope.NewHandle<mirror::Class>(klass);
2741
2742 ImTable* ret = PrepareAndGetImTable(runtime, h_klass, pointer_size, prepared);
2743 *klass_out = h_klass.Get();
2744 return ret;
2745 }
2746
2747 static ImTable* PrepareAndGetImTable(Runtime* runtime,
2748 Handle<mirror::Class> h_klass,
2749 const PointerSize pointer_size,
2750 std::unordered_set<std::string>* prepared)
2751 REQUIRES_SHARED(Locks::mutator_lock_) {
2752 PrepareClass(runtime, h_klass, prepared);
2753 return h_klass->GetImt(pointer_size);
2754 }
2755
2756 static void DumpIMTForClass(Runtime* runtime,
2757 const std::string& class_name,
2758 std::unordered_set<std::string>* prepared) {
2759 Thread* self = Thread::Current();
2760 ScopedObjectAccess soa(self);
2761
2762 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
2763 mirror::Class* klass;
2764 ImTable* imt = PrepareAndGetImTable(runtime, self, class_name, pointer_size, &klass, prepared);
2765 if (imt == nullptr) {
2766 return;
2767 }
2768
2769 std::cerr << class_name << std::endl << " IMT:" << std::endl;
2770 for (size_t index = 0; index < ImTable::kSize; ++index) {
2771 std::cerr << " " << index << ":" << std::endl;
2772 ArtMethod* ptr = imt->Get(index, pointer_size);
2773 if (ptr->IsRuntimeMethod()) {
2774 if (ptr->IsImtUnimplementedMethod()) {
2775 std::cerr << " <empty>" << std::endl;
2776 } else {
2777 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
2778 PrintTable(current_table, pointer_size);
2779 }
2780 } else {
2781 std::cerr << " " << PrettyMethod(ptr, true) << std::endl;
2782 }
2783 }
2784
2785 std::cerr << " Interfaces:" << std::endl;
2786 // Run through iftable, find methods that slot here, see if they fit.
2787 mirror::IfTable* if_table = klass->GetIfTable();
2788 if (if_table != nullptr) {
2789 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
2790 mirror::Class* iface = if_table->GetInterface(i);
2791 std::string iface_name;
2792 std::cerr << " " << iface->GetDescriptor(&iface_name) << std::endl;
2793
2794 for (ArtMethod& iface_method : iface->GetVirtualMethods(pointer_size)) {
2795 uint32_t base_hash = ImTable::GetBaseImtHash(&iface_method);
2796 uint32_t imt_slot = ImTable::GetImtIndex(&iface_method);
2797 std::cerr << " " << PrettyMethod(&iface_method, true) << " slot=" << std::dec
2798 << imt_slot << " base_hash=0x" << std::hex << base_hash << std::endl;
2799 }
2800 }
2801 }
2802 }
2803
2804 static void DumpIMTForMethod(Runtime* runtime,
2805 const std::string& class_name,
2806 const std::string& method,
2807 std::unordered_set<std::string>* prepared) {
2808 Thread* self = Thread::Current();
2809 ScopedObjectAccess soa(self);
2810
2811 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
2812 mirror::Class* klass;
2813 ImTable* imt = PrepareAndGetImTable(runtime,
2814 self,
2815 class_name,
2816 pointer_size,
2817 &klass,
2818 prepared);
2819 if (imt == nullptr) {
2820 return;
2821 }
2822
2823 std::cerr << class_name << " <" << method << ">" << std::endl;
2824 for (size_t index = 0; index < ImTable::kSize; ++index) {
2825 ArtMethod* ptr = imt->Get(index, pointer_size);
2826 if (ptr->IsRuntimeMethod()) {
2827 if (ptr->IsImtUnimplementedMethod()) {
2828 continue;
2829 }
2830
2831 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
2832 if (current_table == nullptr) {
2833 continue;
2834 }
2835
2836 size_t table_index = 0;
2837 for (;;) {
2838 ArtMethod* ptr2 = current_table->GetInterfaceMethod(table_index, pointer_size);
2839 if (ptr2 == nullptr) {
2840 break;
2841 }
2842 table_index++;
2843
2844 std::string p_name = PrettyMethod(ptr2, true);
2845 if (StartsWith(p_name, method.c_str())) {
2846 std::cerr << " Slot "
2847 << index
2848 << " ("
2849 << current_table->NumEntries(pointer_size)
2850 << ")"
2851 << std::endl;
2852 PrintTable(current_table, pointer_size);
2853 return;
2854 }
2855 }
2856 } else {
2857 std::string p_name = PrettyMethod(ptr, true);
2858 if (StartsWith(p_name, method.c_str())) {
2859 std::cerr << " Slot " << index << " (1)" << std::endl;
2860 std::cerr << " " << p_name << std::endl;
2861 } else {
2862 // Run through iftable, find methods that slot here, see if they fit.
2863 mirror::IfTable* if_table = klass->GetIfTable();
2864 if (if_table != nullptr) {
2865 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
2866 mirror::Class* iface = if_table->GetInterface(i);
2867 size_t num_methods = iface->NumDeclaredVirtualMethods();
2868 if (num_methods > 0) {
2869 for (ArtMethod& iface_method : iface->GetMethods(pointer_size)) {
2870 if (ImTable::GetImtIndex(&iface_method) == index) {
2871 std::string i_name = PrettyMethod(&iface_method, true);
2872 if (StartsWith(i_name, method.c_str())) {
2873 std::cerr << " Slot " << index << " (1)" << std::endl;
2874 std::cerr << " " << p_name << " (" << i_name << ")" << std::endl;
2875 }
2876 }
2877 }
2878 }
2879 }
2880 }
2881 }
2882 }
2883 }
2884 }
2885
2886 // Read lines from the given stream, dropping comments and empty lines
2887 static std::vector<std::string> ReadCommentedInputStream(std::istream& in_stream) {
2888 std::vector<std::string> output;
2889 while (in_stream.good()) {
2890 std::string dot;
2891 std::getline(in_stream, dot);
2892 if (StartsWith(dot, "#") || dot.empty()) {
2893 continue;
2894 }
2895 output.push_back(dot);
2896 }
2897 return output;
2898 }
2899
2900 // Read lines from the given file, dropping comments and empty lines.
2901 static std::vector<std::string> ReadCommentedInputFromFile(const std::string& input_filename) {
2902 std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
2903 if (input_file.get() == nullptr) {
2904 LOG(ERROR) << "Failed to open input file " << input_filename;
2905 return std::vector<std::string>();
2906 }
2907 std::vector<std::string> result = ReadCommentedInputStream(*input_file);
2908 input_file->close();
2909 return result;
2910 }
2911
2912 // Prepare a class, i.e., ensure it has a filled IMT. Will do so recursively for superclasses,
2913 // and note in the given set that the work was done.
2914 static void PrepareClass(Runtime* runtime,
2915 Handle<mirror::Class> h_klass,
2916 std::unordered_set<std::string>* done)
2917 REQUIRES_SHARED(Locks::mutator_lock_) {
2918 if (!h_klass->ShouldHaveImt()) {
2919 return;
2920 }
2921
2922 std::string name;
2923 name = h_klass->GetDescriptor(&name);
2924
2925 if (done->find(name) != done->end()) {
2926 return;
2927 }
2928 done->insert(name);
2929
2930 if (h_klass->HasSuperClass()) {
2931 StackHandleScope<1> h(Thread::Current());
2932 PrepareClass(runtime, h.NewHandle<mirror::Class>(h_klass->GetSuperClass()), done);
2933 }
2934
2935 if (!h_klass->IsTemp()) {
2936 runtime->GetClassLinker()->FillIMTAndConflictTables(h_klass.Get());
2937 }
2938 }
2939};
2940
Igor Murashkin37743352014-11-13 14:38:00 -08002941struct OatdumpArgs : public CmdlineArgs {
2942 protected:
2943 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002944
Igor Murashkin37743352014-11-13 14:38:00 -08002945 virtual ParseStatus ParseCustom(const StringPiece& option,
2946 std::string* error_msg) OVERRIDE {
2947 {
2948 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
2949 if (base_parse != kParseUnknownArgument) {
2950 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002951 }
2952 }
2953
Igor Murashkin37743352014-11-13 14:38:00 -08002954 if (option.starts_with("--oat-file=")) {
2955 oat_filename_ = option.substr(strlen("--oat-file=")).data();
2956 } else if (option.starts_with("--image=")) {
2957 image_location_ = option.substr(strlen("--image=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002958 } else if (option == "--no-dump:vmap") {
2959 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01002960 } else if (option =="--dump:code_info_stack_maps") {
2961 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002962 } else if (option == "--no-disassemble") {
2963 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00002964 } else if (option =="--header-only") {
2965 dump_header_only_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002966 } else if (option.starts_with("--symbolize=")) {
2967 oat_filename_ = option.substr(strlen("--symbolize=")).data();
2968 symbolize_ = true;
David Srbecky2fdd03c2016-03-10 15:32:37 +00002969 } else if (option.starts_with("--only-keep-debug")) {
2970 only_keep_debug_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002971 } else if (option.starts_with("--class-filter=")) {
2972 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002973 } else if (option.starts_with("--method-filter=")) {
2974 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002975 } else if (option.starts_with("--list-classes")) {
2976 list_classes_ = true;
2977 } else if (option.starts_with("--list-methods")) {
2978 list_methods_ = true;
2979 } else if (option.starts_with("--export-dex-to=")) {
2980 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
2981 } else if (option.starts_with("--addr2instr=")) {
2982 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
2983 *error_msg = "Address conversion failed";
2984 return kParseError;
2985 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002986 } else if (option.starts_with("--app-image=")) {
2987 app_image_ = option.substr(strlen("--app-image=")).data();
2988 } else if (option.starts_with("--app-oat=")) {
2989 app_oat_ = option.substr(strlen("--app-oat=")).data();
Andreas Gampe9fded872016-09-25 16:08:35 -07002990 } else if (option.starts_with("--dump-imt=")) {
2991 imt_dump_ = option.substr(strlen("--dump-imt=")).data();
2992 } else if (option == "--dump-imt-stats") {
2993 imt_stat_dump_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002994 } else {
2995 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002996 }
2997
Igor Murashkin37743352014-11-13 14:38:00 -08002998 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002999 }
3000
Igor Murashkin37743352014-11-13 14:38:00 -08003001 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
3002 // Infer boot image location from the image location if possible.
3003 if (boot_image_location_ == nullptr) {
3004 boot_image_location_ = image_location_;
3005 }
3006
3007 // Perform the parent checks.
3008 ParseStatus parent_checks = Base::ParseChecks(error_msg);
3009 if (parent_checks != kParseOk) {
3010 return parent_checks;
3011 }
3012
3013 // Perform our own checks.
3014 if (image_location_ == nullptr && oat_filename_ == nullptr) {
3015 *error_msg = "Either --image or --oat-file must be specified";
3016 return kParseError;
3017 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
3018 *error_msg = "Either --image or --oat-file must be specified but not both";
3019 return kParseError;
3020 }
3021
3022 return kParseOk;
3023 }
3024
3025 virtual std::string GetUsage() const {
3026 std::string usage;
3027
3028 usage +=
3029 "Usage: oatdump [options] ...\n"
3030 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
3031 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
3032 "\n"
3033 // Either oat-file or image is required.
3034 " --oat-file=<file.oat>: specifies an input oat filename.\n"
3035 " Example: --oat-file=/system/framework/boot.oat\n"
3036 "\n"
3037 " --image=<file.art>: specifies an input image location.\n"
3038 " Example: --image=/system/framework/boot.art\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003039 "\n"
3040 " --app-image=<file.art>: specifies an input app image. Must also have a specified\n"
3041 " boot image and app oat file.\n"
3042 " Example: --app-image=app.art\n"
3043 "\n"
3044 " --app-oat=<file.odex>: specifies an input app oat.\n"
3045 " Example: --app-oat=app.odex\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003046 "\n";
3047
3048 usage += Base::GetUsage();
3049
3050 usage += // Optional.
Igor Murashkin37743352014-11-13 14:38:00 -08003051 " --no-dump:vmap may be used to disable vmap dumping.\n"
3052 " Example: --no-dump:vmap\n"
3053 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01003054 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
3055 " Example: --dump:code_info_stack_maps\n"
3056 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003057 " --no-disassemble may be used to disable disassembly.\n"
3058 " Example: --no-disassemble\n"
3059 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00003060 " --header-only may be used to print only the oat header.\n"
3061 " Example: --header-only\n"
3062 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003063 " --list-classes may be used to list target file classes (can be used with filters).\n"
3064 " Example: --list-classes\n"
3065 " Example: --list-classes --class-filter=com.example.foo\n"
3066 "\n"
3067 " --list-methods may be used to list target file methods (can be used with filters).\n"
3068 " Example: --list-methods\n"
3069 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
3070 "\n"
3071 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
3072 " Example: --symbolize=/system/framework/boot.oat\n"
3073 "\n"
David Srbecky2fdd03c2016-03-10 15:32:37 +00003074 " --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
3075 " .rodata and .text sections are omitted in the output file to save space.\n"
3076 " Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
3077 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003078 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
3079 " Example: --class-filter=com.example.foo\n"
3080 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003081 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
3082 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003083 "\n"
3084 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
3085 " Example: --export-dex-to=/data/local/tmp\n"
3086 "\n"
3087 " --addr2instr=<address>: output matching method disassembled code from relative\n"
3088 " address (e.g. PC from crash dump)\n"
3089 " Example: --addr2instr=0x00001a3b\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003090 "\n"
3091 " --dump-imt=<file.txt>: output IMT collisions (if any) for the given receiver\n"
3092 " types and interface methods in the given file. The file\n"
3093 " is read line-wise, and each line should either be a class\n"
3094 " name or descriptor, or a class name/descriptor and a prefix\n"
3095 " of a complete method name.\n"
3096 " Example: --dump-imt=imt.txt\n"
3097 "\n"
3098 " --dump-imt-stats: output IMT statistics for the given boot image\n"
3099 " Example: --dump-imt-stats"
Igor Murashkin37743352014-11-13 14:38:00 -08003100 "\n";
3101
3102 return usage;
3103 }
3104
3105 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07003106 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003107 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00003108 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07003109 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003110 std::string elf_filename_prefix_;
Andreas Gampe9fded872016-09-25 16:08:35 -07003111 std::string imt_dump_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003112 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01003113 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003114 bool disassemble_code_ = true;
3115 bool symbolize_ = false;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003116 bool only_keep_debug_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003117 bool list_classes_ = false;
3118 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003119 bool dump_header_only_ = false;
Andreas Gampe9fded872016-09-25 16:08:35 -07003120 bool imt_stat_dump_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003121 uint32_t addr2instr_ = 0;
3122 const char* export_dex_location_ = nullptr;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003123 const char* app_image_ = nullptr;
3124 const char* app_oat_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003125};
3126
Igor Murashkin37743352014-11-13 14:38:00 -08003127struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
3128 virtual bool NeedsRuntime() OVERRIDE {
3129 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003130
Igor Murashkin37743352014-11-13 14:38:00 -08003131 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
3132 bool absolute_addresses = (args_->oat_filename_ == nullptr);
3133
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003134 oat_dumper_options_.reset(new OatDumperOptions(
Igor Murashkin37743352014-11-13 14:38:00 -08003135 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01003136 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08003137 args_->disassemble_code_,
3138 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003139 args_->class_filter_,
3140 args_->method_filter_,
3141 args_->list_classes_,
3142 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00003143 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003144 args_->export_dex_location_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003145 args_->app_image_,
3146 args_->app_oat_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003147 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08003148
Andreas Gampe9fded872016-09-25 16:08:35 -07003149 return (args_->boot_image_location_ != nullptr ||
3150 args_->image_location_ != nullptr ||
3151 !args_->imt_dump_.empty()) &&
Igor Murashkin37743352014-11-13 14:38:00 -08003152 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003153 }
3154
Igor Murashkin37743352014-11-13 14:38:00 -08003155 virtual bool ExecuteWithoutRuntime() OVERRIDE {
3156 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08003157 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003158
Mathieu Chartierd424d082014-10-15 10:31:46 -07003159 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08003160
Andreas Gampec24f3992014-12-17 20:40:11 -08003161 if (args_->symbolize_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00003162 // ELF has special kind of section called SHT_NOBITS which allows us to create
3163 // sections which exist but their data is omitted from the ELF file to save space.
3164 // This is what "strip --only-keep-debug" does when it creates separate ELF file
3165 // with only debug data. We use it in similar way to exclude .rodata and .text.
3166 bool no_bits = args_->only_keep_debug_;
3167 return SymbolizeOat(args_->oat_filename_, args_->output_name_, no_bits) == EXIT_SUCCESS;
Andreas Gampec24f3992014-12-17 20:40:11 -08003168 } else {
3169 return DumpOat(nullptr,
3170 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003171 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08003172 args_->os_) == EXIT_SUCCESS;
3173 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07003174 }
3175
Igor Murashkin37743352014-11-13 14:38:00 -08003176 virtual bool ExecuteWithRuntime(Runtime* runtime) {
3177 CHECK(args_ != nullptr);
3178
Andreas Gampe9fded872016-09-25 16:08:35 -07003179 if (!args_->imt_dump_.empty()) {
3180 return IMTDumper::DumpImt(runtime, args_->imt_dump_);
3181 }
3182
3183 if (args_->imt_stat_dump_) {
3184 return IMTDumper::DumpImtStats(runtime, runtime->GetClassLinker()->GetBootClassPath());
3185 }
3186
Igor Murashkin37743352014-11-13 14:38:00 -08003187 if (args_->oat_filename_ != nullptr) {
3188 return DumpOat(runtime,
3189 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003190 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08003191 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003192 }
Igor Murashkin37743352014-11-13 14:38:00 -08003193
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003194 return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003195 }
3196
Igor Murashkin37743352014-11-13 14:38:00 -08003197 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
3198};
Andreas Gampe00b25f32014-09-17 21:49:05 -07003199
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003200} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07003201
3202int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08003203 art::OatdumpMain main;
3204 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07003205}