blob: b673eff9ad6e76e8934f41cc389fe3e757c10997 [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>
Brian Carlstrom78128a62011-09-15 17:21:19 -070026#include <vector>
27
Ian Rogersd582fa42014-11-05 23:46:43 -080028#include "arch/instruction_set_features.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070029#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070030#include "art_method-inl.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000031#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080032#include "base/unix_file/fd_file.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070033#include "class_linker.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "class_linker-inl.h"
David Srbecky5d950762016-03-07 20:47:29 +000035#include "debug/elf_debug_writer.h"
36#include "debug/method_debug_info.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070037#include "dex_file-inl.h"
Elliott Hughese3c845c2012-02-28 17:23:01 -080038#include "dex_instruction.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080039#include "disassembler.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070040#include "elf_builder.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070041#include "gc/space/image_space.h"
42#include "gc/space/large_object_space.h"
43#include "gc/space/space-inl.h"
Mathieu Chartier4a26f172016-01-26 14:26:18 -080044#include "image-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080045#include "indenter.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000046#include "linker/buffered_output_stream.h"
47#include "linker/file_output_stream.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048#include "mirror/array-inl.h"
49#include "mirror/class-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010050#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051#include "mirror/object-inl.h"
52#include "mirror/object_array-inl.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080053#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010054#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070055#include "oat_file_manager.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080056#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070057#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070058#include "scoped_thread_state_change.h"
Nicolas Geoffray6bc43742015-10-12 18:11:10 +010059#include "stack_map.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070060#include "ScopedLocalRef.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080061#include "thread_list.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080062#include "verifier/method_verifier.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070063#include "well_known_classes.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070064
Igor Murashkin37743352014-11-13 14:38:00 -080065#include <sys/stat.h>
66#include "cmdline.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070067
Igor Murashkin37743352014-11-13 14:38:00 -080068namespace art {
Brian Carlstrom78128a62011-09-15 17:21:19 -070069
Mathieu Chartiere401d142015-04-22 13:56:20 -070070const char* image_methods_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -080071 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -070072 "kImtConflictMethod",
Mathieu Chartier2d2621a2014-10-23 16:48:06 -070073 "kImtUnimplementedMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070074 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -070075 "kRefsOnlySaveMethod",
76 "kRefsAndArgsSaveMethod",
Mathieu Chartiere401d142015-04-22 13:56:20 -070077};
78
79const char* image_roots_descriptions_[] = {
Brian Carlstrom58ae9412011-10-04 00:56:06 -070080 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070081 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070082};
83
Mathieu Chartierac8f4392015-08-27 13:54:20 -070084// Map is so that we don't allocate multiple dex files for the same OatDexFile.
85static std::map<const OatFile::OatDexFile*,
86 std::unique_ptr<const DexFile>> opened_dex_files;
87
88const DexFile* OpenDexFile(const OatFile::OatDexFile* oat_dex_file, std::string* error_msg) {
89 DCHECK(oat_dex_file != nullptr);
90 auto it = opened_dex_files.find(oat_dex_file);
91 if (it != opened_dex_files.end()) {
92 return it->second.get();
93 }
94 const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
95 opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
96 return ret;
97}
98
Andreas Gampe2d8614b2016-03-07 16:31:34 -080099template <typename ElfTypes>
David Srbeckybc90fd02015-04-22 19:40:27 +0100100class OatSymbolizer FINAL {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700101 public:
David Srbecky2fdd03c2016-03-10 15:32:37 +0000102 OatSymbolizer(const OatFile* oat_file, const std::string& output_name, bool no_bits) :
103 oat_file_(oat_file),
104 builder_(nullptr),
105 output_name_(output_name.empty() ? "symbolized.oat" : output_name),
106 no_bits_(no_bits) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700107 }
108
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700109 bool Symbolize() {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000110 const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
David Srbecky5d811202016-03-08 13:21:22 +0000111 const InstructionSetFeatures* features = InstructionSetFeatures::FromBitmap(
112 isa, oat_file_->GetOatHeader().GetInstructionSetFeaturesBitmap());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000113
114 File* elf_file = OS::CreateEmptyFile(output_name_.c_str());
115 std::unique_ptr<BufferedOutputStream> output_stream(
Vladimir Marko10c13562015-11-25 14:33:36 +0000116 MakeUnique<BufferedOutputStream>(MakeUnique<FileOutputStream>(elf_file)));
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800117 builder_.reset(new ElfBuilder<ElfTypes>(isa, features, output_stream.get()));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000118
119 builder_->Start();
120
121 auto* rodata = builder_->GetRoData();
122 auto* text = builder_->GetText();
123 auto* bss = builder_->GetBss();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000124
David Srbecky6d8c8f02015-10-26 10:57:09 +0000125 const uint8_t* rodata_begin = oat_file_->Begin();
126 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
David Srbecky2fdd03c2016-03-10 15:32:37 +0000127 if (no_bits_) {
128 rodata->WriteNoBitsSection(rodata_size);
129 } else {
130 rodata->Start();
131 rodata->WriteFully(rodata_begin, rodata_size);
132 rodata->End();
133 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000134
David Srbecky6d8c8f02015-10-26 10:57:09 +0000135 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
136 const size_t text_size = oat_file_->End() - text_begin;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000137 if (no_bits_) {
138 text->WriteNoBitsSection(text_size);
139 } else {
140 text->Start();
141 text->WriteFully(text_begin, text_size);
142 text->End();
143 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000144
145 if (oat_file_->BssSize() != 0) {
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000146 bss->WriteNoBitsSection(oat_file_->BssSize());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000147 }
148
Douglas Leung316a2182015-09-17 15:26:25 -0700149 if (isa == kMips || isa == kMips64) {
150 builder_->WriteMIPSabiflagsSection();
151 }
Vladimir Marko944da602016-02-19 12:27:55 +0000152 builder_->PrepareDynamicSection(
153 elf_file->GetPath(), rodata_size, text_size, oat_file_->BssSize());
154 builder_->WriteDynamicSection();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700155
David Srbecky5d950762016-03-07 20:47:29 +0000156 Walk();
157 for (const auto& trampoline : debug::MakeTrampolineInfos(oat_file_->GetOatHeader())) {
158 method_debug_infos_.push_back(trampoline);
159 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700160
David Srbecky5d950762016-03-07 20:47:29 +0000161 debug::WriteDebugInfo(builder_.get(),
162 ArrayRef<const debug::MethodDebugInfo>(method_debug_infos_),
163 dwarf::DW_DEBUG_FRAME_FORMAT,
164 true /* write_oat_patches */);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700165
David Srbecky6d8c8f02015-10-26 10:57:09 +0000166 builder_->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700167
Vladimir Marko10c13562015-11-25 14:33:36 +0000168 return builder_->Good();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700169 }
170
David Srbecky5d950762016-03-07 20:47:29 +0000171 void Walk() {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700172 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
173 for (size_t i = 0; i < oat_dex_files.size(); i++) {
174 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700175 CHECK(oat_dex_file != nullptr);
David Srbecky5d950762016-03-07 20:47:29 +0000176 WalkOatDexFile(oat_dex_file);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700177 }
178 }
179
David Srbecky5d950762016-03-07 20:47:29 +0000180 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700181 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700182 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
183 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700184 return;
185 }
186 for (size_t class_def_index = 0;
187 class_def_index < dex_file->NumClassDefs();
188 class_def_index++) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700189 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
190 OatClassType type = oat_class.GetType();
191 switch (type) {
192 case kOatClassAllCompiled:
193 case kOatClassSomeCompiled:
David Srbecky5d950762016-03-07 20:47:29 +0000194 WalkOatClass(oat_class, *dex_file, class_def_index);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700195 break;
196
197 case kOatClassNoneCompiled:
198 case kOatClassMax:
199 // Ignore.
200 break;
201 }
202 }
203 }
204
David Srbecky5d950762016-03-07 20:47:29 +0000205 void WalkOatClass(const OatFile::OatClass& oat_class,
206 const DexFile& dex_file,
207 uint32_t class_def_index) {
208 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700209 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700210 if (class_data == nullptr) { // empty class such as a marker interface?
211 return;
212 }
213 // Note: even if this is an interface or a native class, we still have to walk it, as there
214 // might be a static initializer.
215 ClassDataItemIterator it(dex_file, class_data);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700216 uint32_t class_method_idx = 0;
David Srbecky5d950762016-03-07 20:47:29 +0000217 for (; it.HasNextStaticField(); it.Next()) { /* skip */ }
218 for (; it.HasNextInstanceField(); it.Next()) { /* skip */ }
219 for (; it.HasNextDirectMethod() || it.HasNextVirtualMethod(); it.Next()) {
220 WalkOatMethod(oat_class.GetOatMethod(class_method_idx++),
221 dex_file,
222 class_def_index,
223 it.GetMemberIndex(),
224 it.GetMethodCodeItem(),
225 it.GetMethodAccessFlags());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700226 }
227 DCHECK(!it.HasNext());
228 }
229
David Srbecky5d950762016-03-07 20:47:29 +0000230 void WalkOatMethod(const OatFile::OatMethod& oat_method,
231 const DexFile& dex_file,
232 uint32_t class_def_index,
233 uint32_t dex_method_index,
234 const DexFile::CodeItem* code_item,
235 uint32_t method_access_flags) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700236 if ((method_access_flags & kAccAbstract) != 0) {
237 // Abstract method, no code.
238 return;
239 }
David Srbecky5d950762016-03-07 20:47:29 +0000240 const OatHeader& oat_header = oat_file_->GetOatHeader();
241 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
242 if (method_header == nullptr || method_header->GetCodeSize() == 0) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700243 // No code.
244 return;
245 }
246
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100247 uint32_t entry_point = oat_method.GetCodeOffset() - oat_header.GetExecutableOffset();
248 // Clear Thumb2 bit.
249 const void* code_address = EntryPointToCodePointer(reinterpret_cast<void*>(entry_point));
250
David Srbecky5d950762016-03-07 20:47:29 +0000251 debug::MethodDebugInfo info = debug::MethodDebugInfo();
252 info.trampoline_name = nullptr;
253 info.dex_file = &dex_file;
254 info.class_def_index = class_def_index;
255 info.dex_method_index = dex_method_index;
256 info.access_flags = method_access_flags;
257 info.code_item = code_item;
258 info.isa = oat_header.GetInstructionSet();
259 info.deduped = !seen_offsets_.insert(oat_method.GetCodeOffset()).second;
260 info.is_native_debuggable = oat_header.IsNativeDebuggable();
261 info.is_optimized = method_header->IsOptimized();
262 info.is_code_address_text_relative = true;
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100263 info.code_address = reinterpret_cast<uintptr_t>(code_address);
David Srbecky5d950762016-03-07 20:47:29 +0000264 info.code_size = method_header->GetCodeSize();
265 info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
266 info.code_info = info.is_optimized ? method_header->GetOptimizedCodeInfoPtr() : nullptr;
267 info.cfi = ArrayRef<uint8_t>();
268 method_debug_infos_.push_back(info);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700269 }
270
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700271 private:
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700272 const OatFile* oat_file_;
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800273 std::unique_ptr<ElfBuilder<ElfTypes> > builder_;
David Srbecky5d950762016-03-07 20:47:29 +0000274 std::vector<debug::MethodDebugInfo> method_debug_infos_;
275 std::unordered_set<uint32_t> seen_offsets_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700276 const std::string output_name_;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000277 bool no_bits_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700278};
279
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700280class OatDumperOptions {
281 public:
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100282 OatDumperOptions(bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100283 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700284 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700285 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800286 const char* class_filter,
287 const char* method_filter,
288 bool list_classes,
289 bool list_methods,
David Brazdilc03d7b62016-03-02 12:18:03 +0000290 bool dump_header_only,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800291 const char* export_dex_location,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800292 const char* app_image,
293 const char* app_oat,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800294 uint32_t addr2instr)
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100295 : dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100296 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700297 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700298 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800299 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000300 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800301 list_classes_(list_classes),
302 list_methods_(list_methods),
David Brazdilc03d7b62016-03-02 12:18:03 +0000303 dump_header_only_(dump_header_only),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800304 export_dex_location_(export_dex_location),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800305 app_image_(app_image),
306 app_oat_(app_oat),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800307 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800308 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700309
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700310 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100311 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700312 const bool disassemble_code_;
313 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800314 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000315 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800316 const bool list_classes_;
317 const bool list_methods_;
David Brazdilc03d7b62016-03-02 12:18:03 +0000318 const bool dump_header_only_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800319 const char* const export_dex_location_;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800320 const char* const app_image_;
321 const char* const app_oat_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800322 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700323 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700324};
325
Elliott Hughese3c845c2012-02-28 17:23:01 -0800326class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700327 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100328 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000329 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800330 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700331 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800332 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800333 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
334 disassembler_(Disassembler::Create(instruction_set_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800335 new DisassemblerOptions(options_.absolute_addresses_,
Alexandre Ramesa37d9252014-10-27 11:28:14 +0000336 oat_file.Begin(),
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100337 true /* can_read_literals_ */))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800338 CHECK(options_.class_loader_ != nullptr);
339 CHECK(options_.class_filter_ != nullptr);
340 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800341 AddAllOffsets();
342 }
343
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700344 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700345 delete disassembler_;
346 }
347
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800348 InstructionSet GetInstructionSet() {
349 return instruction_set_;
350 }
351
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700352 bool Dump(std::ostream& os) {
353 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800354 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700355
356 os << "MAGIC:\n";
357 os << oat_header.GetMagic() << "\n\n";
358
Jeff Hao4b07a6e2016-01-15 12:50:44 -0800359 os << "LOCATION:\n";
360 os << oat_file_.GetLocation() << "\n\n";
361
Brian Carlstromaded5f72011-10-07 17:15:04 -0700362 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800363 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700364
Elliott Hughesa72ec822012-03-05 17:12:22 -0800365 os << "INSTRUCTION SET:\n";
366 os << oat_header.GetInstructionSet() << "\n\n";
367
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700368 {
369 std::unique_ptr<const InstructionSetFeatures> features(
370 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
371 oat_header.GetInstructionSetFeaturesBitmap()));
372 os << "INSTRUCTION SET FEATURES:\n";
373 os << features->GetFeatureString() << "\n\n";
374 }
Dave Allison70202782013-10-22 17:52:19 -0700375
Brian Carlstromaded5f72011-10-07 17:15:04 -0700376 os << "DEX FILE COUNT:\n";
377 os << oat_header.GetDexFileCount() << "\n\n";
378
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800379#define DUMP_OAT_HEADER_OFFSET(label, offset) \
380 os << label " OFFSET:\n"; \
381 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800382 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800383 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
384 } \
385 os << StringPrintf("\n\n");
386
387 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
388 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
389 GetInterpreterToInterpreterBridgeOffset);
390 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
391 GetInterpreterToCompiledCodeBridgeOffset);
392 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
393 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800394 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
395 GetQuickGenericJniTrampolineOffset);
396 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
397 GetQuickImtConflictTrampolineOffset);
398 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
399 GetQuickResolutionTrampolineOffset);
400 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
401 GetQuickToInterpreterBridgeOffset);
402#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700403
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700404 os << "IMAGE PATCH DELTA:\n";
405 os << StringPrintf("%d (0x%08x)\n\n",
406 oat_header.GetImagePatchDelta(),
407 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700408
Brian Carlstrom28db0122012-10-18 16:20:41 -0700409 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
410 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
411
412 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800413 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700414
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700415 // Print the key-value store.
416 {
417 os << "KEY VALUE STORE:\n";
418 size_t index = 0;
419 const char* key;
420 const char* value;
421 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
422 os << key << " = " << value << "\n";
423 index++;
424 }
425 os << "\n";
426 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700427
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800428 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700429 os << "BEGIN:\n";
430 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700431
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700432 os << "END:\n";
433 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
434 }
435
436 os << "SIZE:\n";
437 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700438
439 os << std::flush;
440
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800441 // If set, adjust relative address to be searched
442 if (options_.addr2instr_ != 0) {
443 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
444 os << "SEARCH ADDRESS (executable offset + input):\n";
445 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
446 }
447
David Brazdilc03d7b62016-03-02 12:18:03 +0000448 if (!options_.dump_header_only_) {
449 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
450 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
451 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800452
David Brazdilc03d7b62016-03-02 12:18:03 +0000453 // If file export selected skip file analysis
454 if (options_.export_dex_location_) {
455 if (!ExportDexFile(os, *oat_dex_file)) {
456 success = false;
457 }
458 } else {
459 if (!DumpOatDexFile(os, *oat_dex_file)) {
460 success = false;
461 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800462 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700463 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700464 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000465
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700466 os << std::flush;
467 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700468 }
469
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800470 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700471 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
472 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800473 return 0; // Address not in oat file
474 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800475 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
476 reinterpret_cast<uintptr_t>(oat_file_.Begin());
477 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800478 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800479 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800480 return end_offset - begin_offset;
481 }
482
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800483 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700484 return oat_file_.GetOatHeader().GetInstructionSet();
485 }
486
Mathieu Chartier90443472015-07-16 20:32:27 -0700487 const void* GetQuickOatCode(ArtMethod* m) SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800488 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
489 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700490 CHECK(oat_dex_file != nullptr);
491 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700492 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
493 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700494 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
495 << "': " << error_msg;
496 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800497 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700498 const DexFile::ClassDef* class_def =
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800499 dex_file->FindClassDef(descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700500 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700501 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100502 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800503 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100504 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800505 }
506 }
507 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700508 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800509 }
510
Brian Carlstromaded5f72011-10-07 17:15:04 -0700511 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800512 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800513 // We don't know the length of the code for each method, but we need to know where to stop
514 // when disassembling. What we do know is that a region of code will be followed by some other
515 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
516 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800517 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
518 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700519 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700520 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700521 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
522 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700523 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
524 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800525 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800526 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800527 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800528 for (size_t class_def_index = 0;
529 class_def_index < dex_file->NumClassDefs();
530 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800531 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100532 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700533 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700534 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800535 ClassDataItemIterator it(*dex_file, class_data);
536 SkipAllFields(it);
537 uint32_t class_method_index = 0;
538 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100539 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800540 it.Next();
541 }
542 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100543 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800544 it.Next();
545 }
546 }
547 }
548 }
549
550 // If the last thing in the file is code for a method, there won't be an offset for the "next"
551 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
552 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800553 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800554 }
555
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700556 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
557 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
558 }
559
Elliott Hughese3c845c2012-02-28 17:23:01 -0800560 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800561 uint32_t code_offset = oat_method.GetCodeOffset();
562 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
563 code_offset &= ~0x1;
564 }
565 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800566 offsets_.insert(oat_method.GetVmapTableOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800567 }
568
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700569 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
570 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800571 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700572 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800573 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800574 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700575
576 // Create the verifier early.
577
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700578 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700579 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
580 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700581 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700582 os << std::flush;
583 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700584 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100585
586 VariableIndentationOutputStream vios(&os);
587 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800588 for (size_t class_def_index = 0;
589 class_def_index < dex_file->NumClassDefs();
590 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700591 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
592 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800593
594 // TODO: Support regex
595 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
596 continue;
597 }
598
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700599 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100600 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700601 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
602 class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100603 << " (" << oat_class.GetStatus() << ")"
604 << " (" << oat_class.GetType() << ")\n";
605 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800606 if (options_.list_classes_) continue;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700607 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700608 success = false;
609 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800610 if (stop_analysis) {
611 os << std::flush;
612 return success;
613 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700614 }
615
616 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700617 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700618 }
619
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800620 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
621 std::string error_msg;
622 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
623
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700624 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800625 if (dex_file == nullptr) {
626 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
627 return false;
628 }
629 size_t fsize = oat_dex_file.FileSize();
630
631 // Some quick checks just in case
632 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
633 os << "Invalid dex file\n";
634 return false;
635 }
636
637 // Verify output directory exists
638 if (!OS::DirectoryExists(options_.export_dex_location_)) {
639 // TODO: Extend OS::DirectoryExists if symlink support is required
640 os << options_.export_dex_location_ << " output directory not found or symlink\n";
641 return false;
642 }
643
644 // Beautify path names
645 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
646 return false;
647 }
648
649 std::string dex_orig_name;
650 size_t dex_orig_pos = dex_file_location.rfind('/');
651 if (dex_orig_pos == std::string::npos)
652 dex_orig_name = dex_file_location;
653 else
654 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
655
656 // A more elegant approach to efficiently name user installed apps is welcome
657 if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
658 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
659 size_t apk_orig_pos = dex_file_location.rfind('/');
660 if (apk_orig_pos != std::string::npos) {
661 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
662 }
663 }
664
665 std::string out_dex_path(options_.export_dex_location_);
666 if (out_dex_path.back() != '/') {
667 out_dex_path.append("/");
668 }
669 out_dex_path.append(dex_orig_name);
670 out_dex_path.append("_export.dex");
671 if (out_dex_path.length() > PATH_MAX) {
672 return false;
673 }
674
675 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
676 if (file.get() == nullptr) {
677 os << "Failed to open output dex file " << out_dex_path;
678 return false;
679 }
680
681 if (!file->WriteFully(dex_file->Begin(), fsize)) {
682 os << "Failed to write dex file";
683 file->Erase();
684 return false;
685 }
686
687 if (file->FlushCloseOrErase() != 0) {
688 os << "Flush and close failed";
689 return false;
690 }
691
692 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
693 os << std::flush;
694
695 return true;
696 }
697
Elliott Hughese3c845c2012-02-28 17:23:01 -0800698 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700699 while (it.HasNextStaticField()) {
700 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700701 }
Ian Rogers0571d352011-11-03 19:51:38 -0700702 while (it.HasNextInstanceField()) {
703 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700704 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800705 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700706
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100707 bool DumpOatClass(VariableIndentationOutputStream* vios,
708 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800709 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700710 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800711 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -0700712 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700713 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100714 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700715 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800716 }
717 ClassDataItemIterator it(dex_file, class_data);
718 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700719 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700720 while (it.HasNextDirectMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100721 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700722 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800723 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700724 success = false;
725 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800726 if (addr_found) {
727 *stop_analysis = true;
728 return success;
729 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700730 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700731 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700732 }
Ian Rogers0571d352011-11-03 19:51:38 -0700733 while (it.HasNextVirtualMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100734 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700735 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800736 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700737 success = false;
738 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800739 if (addr_found) {
740 *stop_analysis = true;
741 return success;
742 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700743 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700744 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700745 }
Ian Rogers0571d352011-11-03 19:51:38 -0700746 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100747 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700748 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700749 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800750
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700751 static constexpr uint32_t kPrologueBytes = 16;
752
753 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
754 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
755
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100756 bool DumpOatMethod(VariableIndentationOutputStream* vios,
757 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700758 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700759 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800760 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800761 uint32_t method_access_flags, bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700762 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800763
764 // TODO: Support regex
765 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
766 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000767 return success;
768 }
769
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800770 std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100771 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
772 class_method_index, pretty_method.c_str(),
773 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800774 if (options_.list_methods_) return success;
775
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800776 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
777 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
778 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
779 uint32_t code_offset = oat_method.GetCodeOffset();
780 uint32_t code_size = oat_method.GetQuickCodeSize();
781 if (resolved_addr2instr_ != 0) {
782 if (resolved_addr2instr_ > code_offset + code_size) {
783 return success;
784 } else {
785 *addr_found = true; // stop analyzing file at next iteration
786 }
787 }
788
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100789 // Everything below is indented at least once.
790 ScopedIndentation indent1(vios);
791
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800792 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100793 vios->Stream() << "DEX CODE:\n";
794 ScopedIndentation indent2(vios);
795 DumpDexCode(vios->Stream(), dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700796 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700797
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700798 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700799 std::unique_ptr<verifier::MethodVerifier> verifier;
800 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700801 // We need to have the handle scope stay live until after the verifier since the verifier has
802 // a handle to the dex cache from hs.
803 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100804 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
805 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700806 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100807 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700808 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -0700809 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800810 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100811 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800812 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100813 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100814 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100815 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700816 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100817 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700818 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
819 oat_method_offsets_offset, oat_file_.Size());
820 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100821 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700822 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800823 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700824
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100825 ScopedIndentation indent2(vios);
826 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700827 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
828 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100829 vios->Stream() << StringPrintf("WARNING: "
830 "code offset 0x%08x is past end of file 0x%08zx.\n",
831 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700832 success = false;
833 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100834 vios->Stream() << "\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800835 }
836 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100837 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700838 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
839 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700840
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800841 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100842 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700843 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100844 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700845 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100846 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700847 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
848 method_header_offset, oat_file_.Size());
849 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100850 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700851 return false;
852 }
853
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100854 ScopedIndentation indent2(vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100855 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800856 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100857 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700858 }
859 uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100860 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700861 if (vmap_table_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100862 vios->Stream() << StringPrintf("WARNING: "
863 "vmap table offset 0x%08x is past end of file 0x%08zx. "
864 "vmap table offset was loaded from offset 0x%08x.\n",
865 vmap_table_offset, oat_file_.Size(),
866 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700867 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800868 } else if (options_.dump_vmap_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100869 DumpVmapData(vios, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700870 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800871 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700872 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100873 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700874
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100875 ScopedIndentation indent2(vios);
876 vios->Stream()
877 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
878 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
879 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
880 vios->Stream() << "\n";
881 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
882 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
883 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700884 }
885 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100886 // Based on spill masks from QuickMethodFrameInfo so placed
887 // after it is dumped, but useful for understanding quick
888 // code, so dumped here.
889 ScopedIndentation indent2(vios);
890 DumpVregLocations(vios->Stream(), oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700891 }
892 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100893 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700894 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
895 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100896 ScopedIndentation indent2(vios);
897 vios->Stream() << StringPrintf("WARNING: "
898 "code size offset 0x%08x is past end of file 0x%08zx.",
899 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700900 success = false;
901 } else {
902 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700903 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
904 uint64_t aligned_code_end = aligned_code_begin + code_size;
905
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800906 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100907 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700908 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100909 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
910 code_offset,
911 code_size_offset,
912 code_size,
913 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700914
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100915 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700916 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100917 vios->Stream() << StringPrintf("WARNING: "
918 "start of code at 0x%08x is past end of file 0x%08zx.",
919 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700920 success = false;
921 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100922 vios->Stream() << StringPrintf(
923 "WARNING: "
924 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
925 "code size is 0x%08x loaded from offset 0x%08x.\n",
926 aligned_code_end, oat_file_.Size(),
927 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700928 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800929 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700930 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100931 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700932 }
933 }
934 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100935 vios->Stream() << StringPrintf(
936 "WARNING: "
937 "code size %d is bigger than max expected threshold of %d. "
938 "code size is 0x%08x loaded from offset 0x%08x.\n",
939 code_size, kMaxCodeSize,
940 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700941 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800942 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700943 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100944 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700945 }
946 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800947 } else if (options_.disassemble_code_) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100948 DumpCode(vios, oat_method, code_item, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700949 }
950 }
951 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100952 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700953 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700954 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800955
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800956 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
957 if (spill_mask == 0) {
958 return;
959 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800960 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800961 for (size_t i = 0; i < 32; i++) {
962 if ((spill_mask & (1 << i)) != 0) {
963 if (is_float) {
964 os << "fr" << i;
965 } else {
966 os << "r" << i;
967 }
968 spill_mask ^= 1 << i; // clear bit
969 if (spill_mask != 0) {
970 os << ", ";
971 } else {
972 break;
973 }
974 }
975 }
976 os << ")";
977 }
978
Roland Levillain442b46a2015-02-18 16:54:21 +0000979 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100980 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +0000981 const OatFile::OatMethod& oat_method,
982 const DexFile::CodeItem* code_item) {
Roland Levillainf2650d12015-05-28 14:53:28 +0100983 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
984 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +0000985 const void* raw_code_info = oat_method.GetVmapTable();
986 if (raw_code_info != nullptr) {
987 CodeInfo code_info(raw_code_info);
988 DCHECK(code_item != nullptr);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100989 ScopedIndentation indent1(vios);
990 DumpCodeInfo(vios, code_info, oat_method, *code_item);
Roland Levillain442b46a2015-02-18 16:54:21 +0000991 }
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +0100992 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
993 // We don't encode the size in the table, so just emit that we have quickened
994 // information.
995 ScopedIndentation indent(vios);
996 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +0000997 } else {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100998 // Otherwise, there is nothing to display.
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +0100999 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001000 }
1001
1002 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001003 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001004 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001005 const OatFile::OatMethod& oat_method,
Roland Levillain442b46a2015-02-18 16:54:21 +00001006 const DexFile::CodeItem& code_item) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001007 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001008 oat_method.GetCodeOffset(),
1009 code_item.registers_size_,
1010 options_.dump_code_info_stack_maps_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001011 }
1012
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001013 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1014 const DexFile::CodeItem* code_item) {
1015 if (code_item != nullptr) {
1016 size_t num_locals_ins = code_item->registers_size_;
1017 size_t num_ins = code_item->ins_size_;
1018 size_t num_locals = num_locals_ins - num_ins;
1019 size_t num_outs = code_item->outs_size_;
1020
1021 os << "vr_stack_locations:";
1022 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1023 // For readability, delimit the different kinds of VRs.
1024 if (reg == num_locals_ins) {
1025 os << "\n\tmethod*:";
1026 } else if (reg == num_locals && num_ins > 0) {
1027 os << "\n\tins:";
1028 } else if (reg == 0 && num_locals > 0) {
1029 os << "\n\tlocals:";
1030 }
1031
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001032 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1033 code_item,
1034 oat_method.GetCoreSpillMask(),
1035 oat_method.GetFpSpillMask(),
1036 oat_method.GetFrameSizeInBytes(),
1037 reg,
1038 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001039 os << " v" << reg << "[sp + #" << offset << "]";
1040 }
1041
1042 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1043 if (out_reg == 0) {
1044 os << "\n\touts:";
1045 }
1046
1047 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1048 os << " v" << out_reg << "[sp + #" << offset << "]";
1049 }
1050
1051 os << "\n";
1052 }
1053 }
1054
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001055 void DumpInformationAtOffset(VariableIndentationOutputStream* vios,
1056 const OatFile::OatMethod& oat_method,
1057 const DexFile::CodeItem* code_item,
1058 size_t offset,
1059 bool suspend_point_mapping) {
1060 if (!IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1061 // Native method.
Elliott Hughese3c845c2012-02-28 17:23:01 -08001062 return;
1063 }
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001064 if (suspend_point_mapping) {
1065 ScopedIndentation indent1(vios);
1066 DumpDexRegisterMapAtOffset(vios, oat_method, code_item, offset);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001067 }
1068 }
1069
1070
Ian Rogersb23a7722012-10-09 16:54:26 -07001071 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001072 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001073 size_t i = 0;
1074 while (i < code_item->insns_size_in_code_units_) {
1075 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001076 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1077 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001078 i += instruction->SizeInCodeUnits();
1079 }
1080 }
1081 }
1082
Roland Levillainf2650d12015-05-28 14:53:28 +01001083 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1084 // the optimizing compiler?
1085 static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1086 const DexFile::CodeItem* code_item) {
1087 // If the native GC map is null and the Dex `code_item` is not
1088 // null, then this method has been compiled with the optimizing
1089 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001090 return oat_method.GetQuickCode() != nullptr &&
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001091 oat_method.GetVmapTable() != nullptr &&
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001092 code_item != nullptr;
1093 }
1094
1095 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1096 // the dextodex compiler?
1097 static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
1098 const DexFile::CodeItem* code_item) {
1099 // If the quick code is null, the Dex `code_item` is not
1100 // null, and the vmap table is not null, then this method has been compiled
1101 // with the dextodex compiler.
1102 return oat_method.GetQuickCode() == nullptr &&
1103 oat_method.GetVmapTable() != nullptr &&
1104 code_item != nullptr;
Roland Levillainf2650d12015-05-28 14:53:28 +01001105 }
1106
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001107 void DumpDexRegisterMapAtOffset(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001108 const OatFile::OatMethod& oat_method,
1109 const DexFile::CodeItem* code_item,
1110 size_t offset) {
1111 // This method is only relevant for oat methods compiled with the
1112 // optimizing compiler.
1113 DCHECK(IsMethodGeneratedByOptimizingCompiler(oat_method, code_item));
1114
1115 // The optimizing compiler outputs its CodeInfo data in the vmap table.
1116 const void* raw_code_info = oat_method.GetVmapTable();
1117 if (raw_code_info != nullptr) {
1118 CodeInfo code_info(raw_code_info);
David Srbecky09ed0982016-02-12 21:58:43 +00001119 CodeInfoEncoding encoding = code_info.ExtractEncoding();
David Brazdilf677ebf2015-05-29 16:29:43 +01001120 StackMap stack_map = code_info.GetStackMapForNativePcOffset(offset, encoding);
Roland Levillainf2650d12015-05-28 14:53:28 +01001121 if (stack_map.IsValid()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001122 stack_map.Dump(vios, code_info, encoding, oat_method.GetCodeOffset(),
1123 code_item->registers_size_);
Roland Levillainf2650d12015-05-28 14:53:28 +01001124 }
1125 }
1126 }
1127
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001128 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001129 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001130 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001131 const DexFile* dex_file,
1132 const DexFile::ClassDef& class_def,
1133 const DexFile::CodeItem* code_item,
1134 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001135 if ((method_access_flags & kAccNative) == 0) {
1136 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001137 Runtime* const runtime = Runtime::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001138 Handle<mirror::DexCache> dex_cache(
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001139 hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file,
1140 runtime->GetLinearAlloc())));
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001141 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001142 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001143 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
1144 &class_def, code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001145 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001146
1147 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001148 }
1149
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001150 void DumpCode(VariableIndentationOutputStream* vios,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001151 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1152 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001153 const void* quick_code = oat_method.GetQuickCode();
1154
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001155 if (code_size == 0) {
1156 code_size = oat_method.GetQuickCodeSize();
1157 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001158 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001159 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001160 return;
Elliott Hughes956af0f2014-12-11 14:34:28 -08001161 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001162 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1163 size_t offset = 0;
1164 while (offset < code_size) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001165 if (!bad_input) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001166 DumpInformationAtOffset(vios, oat_method, code_item, offset, false);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001167 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001168 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001169 if (!bad_input) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001170 DumpInformationAtOffset(vios, oat_method, code_item, offset, true);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001171 }
1172 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001173 }
1174 }
1175
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001176 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001177 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001178 const OatDumperOptions& options_;
1179 uint32_t resolved_addr2instr_;
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001180 InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001181 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001182 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001183};
1184
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001185class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001186 public:
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001187 ImageDumper(std::ostream* os,
1188 gc::space::ImageSpace& image_space,
1189 const ImageHeader& image_header,
1190 OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001191 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001192 vios_(os),
1193 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001194 image_space_(image_space),
1195 image_header_(image_header),
1196 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001197
Mathieu Chartier90443472015-07-16 20:32:27 -07001198 bool Dump() SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001199 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001200 std::ostream& indent_os = vios_.Stream();
1201
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001202 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001203
Jeff Haodcdc85b2015-12-04 14:06:18 -08001204 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1205
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001206 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001207
Mathieu Chartiere401d142015-04-22 13:56:20 -07001208 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1209
1210 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1211 auto section = static_cast<ImageHeader::ImageSections>(i);
1212 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1213 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001214
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001215 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001216
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001217 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001218
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001219 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1220
1221 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1222
1223 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001224
Alex Lighta59dd802014-07-02 16:28:08 -07001225 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1226
Igor Murashkin46774762014-10-22 11:37:02 -07001227 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1228
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001229 {
1230 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001231 static_assert(arraysize(image_roots_descriptions_) ==
1232 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001233 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1234 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1235 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001236 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001237 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001238 if (image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001239 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001240 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001241 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001242 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1243 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001244 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001245 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1246 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001247 run++;
1248 } else {
1249 break;
1250 }
1251 }
1252 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001253 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001254 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001255 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001256 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001257 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001258 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001259 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001260 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001261 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001262 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001263 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001264 }
1265 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001266 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001267
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001268 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001269 os << "METHOD ROOTS\n";
1270 static_assert(arraysize(image_methods_descriptions_) ==
1271 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1272 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1273 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1274 const char* description = image_methods_descriptions_[i];
1275 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001276 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001277 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001278 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001279 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001280
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001281 Runtime* const runtime = Runtime::Current();
1282 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001283 std::string image_filename = image_space_.GetImageFilename();
1284 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001285 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001286 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001287 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001288 const OatFile* oat_file = image_space_.GetOatFile();
Alex Lighta59dd802014-07-02 16:28:08 -07001289 if (oat_file == nullptr) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001290 oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location);
1291 }
1292 if (oat_file == nullptr) {
1293 oat_file = OatFile::Open(oat_location,
1294 oat_location,
1295 nullptr,
1296 nullptr,
1297 false,
1298 /*low_4gb*/false,
1299 nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001300 &error_msg);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001301 }
1302 if (oat_file == nullptr) {
1303 os << "OAT FILE NOT FOUND: " << error_msg << "\n";
1304 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001305 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001306 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001307
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001308 stats_.oat_file_bytes = oat_file->Size();
1309
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001310 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001311
Mathieu Chartier02e25112013-08-14 16:14:24 -07001312 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001313 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001314 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1315 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001316 }
1317
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001318 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001319
Jeff Haodcdc85b2015-12-04 14:06:18 -08001320 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001321 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001322 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001323 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001324 {
1325 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1326 heap->FlushAllocStack();
1327 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001328 // Since FlushAllocStack() above resets the (active) allocation
1329 // stack. Need to revoke the thread-local allocation stacks that
1330 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001331 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001332 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001333 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001334 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001335 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001336 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001337 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001338 {
1339 ReaderMutexLock mu(self, *class_linker->DexLock());
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001340 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001341 mirror::DexCache* dex_cache =
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001342 down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001343 if (dex_cache != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01001344 dex_caches_.insert(dex_cache);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001345 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001346 }
1347 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001348 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001349 // Dump the normal objects before ArtMethods.
1350 image_space_.GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1351 indent_os << "\n";
1352 // TODO: Dump fields.
1353 // Dump methods after.
1354 const auto& methods_section = image_header_.GetMethodsSection();
Jeff Haodcdc85b2015-12-04 14:06:18 -08001355 DumpArtMethodVisitor visitor(this);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001356 methods_section.VisitPackedArtMethods(&visitor,
1357 image_space_.Begin(),
1358 image_header_.GetPointerSize());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001359 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001360 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001361 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001362 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001363 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001364 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001365 size_t data_size = image_header_.GetDataSize(); // stored size in file.
1366 if (file == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001367 LOG(WARNING) << "Failed to find image in " << image_filename;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001368 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001369 stats_.file_bytes = file->GetLength();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001370 // If the image is compressed, adjust to decompressed size.
1371 size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader);
1372 if (image_header_.GetStorageMode() == ImageHeader::kStorageModeUncompressed) {
1373 DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image";
1374 }
1375 stats_.file_bytes += uncompressed_size - data_size;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001376 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001377 size_t header_bytes = sizeof(ImageHeader);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001378 const auto& object_section = image_header_.GetImageSection(ImageHeader::kSectionObjects);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001379 const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1380 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001381 const auto& dex_cache_arrays_section = image_header_.GetImageSection(
1382 ImageHeader::kSectionDexCacheArrays);
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001383 const auto& intern_section = image_header_.GetImageSection(
1384 ImageHeader::kSectionInternedStrings);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001385 const auto& class_table_section = image_header_.GetImageSection(
1386 ImageHeader::kSectionClassTable);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001387 const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1388
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001389 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001390
1391 // Objects are kObjectAlignment-aligned.
1392 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1393 if (object_section.Offset() > header_bytes) {
1394 stats_.alignment_bytes += object_section.Offset() - header_bytes;
1395 }
1396
1397 // Field section is 4-byte aligned.
1398 constexpr size_t kFieldSectionAlignment = 4U;
1399 uint32_t end_objects = object_section.Offset() + object_section.Size();
1400 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
1401 stats_.alignment_bytes += field_section.Offset() - end_objects;
1402
1403 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
1404 uint32_t end_fields = field_section.Offset() + field_section.Size();
1405 CHECK_ALIGNED(method_section.Offset(), 4);
1406 stats_.alignment_bytes += method_section.Offset() - end_fields;
1407
1408 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
1409 uint32_t end_methods = method_section.Offset() + method_section.Size();
1410 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
1411 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
1412
1413 // Intern table is 8-byte aligned.
1414 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
1415 CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
1416 stats_.alignment_bytes += intern_section.Offset() - end_caches;
1417
1418 // Add space between intern table and class table.
1419 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
1420 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
1421
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001422 // Add space between end of image data and bitmap. Expect the bitmap to be page-aligned.
1423 const size_t bitmap_offset = sizeof(ImageHeader) + data_size;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001424 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001425 stats_.alignment_bytes += RoundUp(bitmap_offset, kPageSize) - bitmap_offset;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001426
Mathieu Chartiere401d142015-04-22 13:56:20 -07001427 stats_.bitmap_bytes += bitmap_section.Size();
1428 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01001429 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01001430 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001431 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001432 stats_.class_table_bytes += class_table_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001433 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001434 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001435
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001436 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001437
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001438 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001439 }
1440
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001441 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001442 class DumpArtMethodVisitor : public ArtMethodVisitor {
1443 public:
1444 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
1445
1446 virtual void Visit(ArtMethod* method) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
1447 std::ostream& indent_os = image_dumper_->vios_.Stream();
1448 indent_os << method << " " << " ArtMethod: " << PrettyMethod(method) << "\n";
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001449 image_dumper_->DumpMethod(method, indent_os);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001450 indent_os << "\n";
1451 }
1452
1453 private:
1454 ImageDumper* const image_dumper_;
1455 };
1456
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001457 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Mathieu Chartier90443472015-07-16 20:32:27 -07001458 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001459 CHECK(type != nullptr);
1460 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001461 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001462 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001463 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001464 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001465 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001466 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001467 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001468 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001469 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001470 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001471 }
1472 }
1473
Mathieu Chartierc7853442015-03-27 14:35:38 -07001474 static void PrintField(std::ostream& os, ArtField* field, mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -07001475 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001476 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08001477 switch (field->GetTypeAsPrimitiveType()) {
1478 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08001479 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001480 break;
1481 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001482 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001483 break;
1484 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001485 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001486 break;
1487 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001488 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001489 break;
1490 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07001491 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001492 break;
1493 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07001494 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001495 break;
1496 case Primitive::kPrimBoolean:
Fred Shih37f05ef2014-07-16 18:38:08 -07001497 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1498 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001499 break;
1500 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07001501 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001502 break;
1503 case Primitive::kPrimNot: {
1504 // Get the value, don't compute the type unless it is non-null as we don't want
1505 // to cause class loading.
1506 mirror::Object* value = field->GetObj(obj);
1507 if (value == nullptr) {
1508 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07001509 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08001510 // Grab the field type without causing resolution.
Mathieu Chartierdaaf3262015-03-24 13:30:28 -07001511 mirror::Class* field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08001512 if (field_type != nullptr) {
1513 PrettyObjectValue(os, field_type, value);
1514 } else {
1515 os << StringPrintf("%p %s\n", value,
1516 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
1517 }
Ian Rogers50239c72013-06-17 14:53:22 -07001518 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001519 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07001520 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001521 default:
1522 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
1523 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08001524 }
1525 }
1526
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001527 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Mathieu Chartier90443472015-07-16 20:32:27 -07001528 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001529 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001530 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001531 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001532 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001533 for (ArtField& field : klass->GetIFields()) {
1534 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001535 }
1536 }
1537
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001538 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001539 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001540 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001541
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001542 const void* GetQuickOatCodeBegin(ArtMethod* m) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001543 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001544 image_header_.GetPointerSize());
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001545 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001546 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001547 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001548 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001549 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001550 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001551 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001552 }
1553
Mathieu Chartiere401d142015-04-22 13:56:20 -07001554 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001555 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001556 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1557 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001558 return 0;
1559 }
1560 return oat_code_begin[-1];
1561 }
1562
Mathieu Chartiere401d142015-04-22 13:56:20 -07001563 const void* GetQuickOatCodeEnd(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001564 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001565 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001566 if (oat_code_begin == nullptr) {
1567 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001568 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001569 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001570 }
1571
Mathieu Chartier90443472015-07-16 20:32:27 -07001572 static void Callback(mirror::Object* obj, void* arg) SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001573 DCHECK(obj != nullptr);
1574 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001575 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001576 if (!state->InDumpSpace(obj)) {
1577 return;
1578 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001579
1580 size_t object_bytes = obj->SizeOf();
1581 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1582 state->stats_.object_bytes += object_bytes;
1583 state->stats_.alignment_bytes += alignment_bytes;
1584
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001585 std::ostream& os = state->vios_.Stream();
1586
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001587 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001588 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001589 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
1590 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001591 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001592 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001593 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
1594 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08001595 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001596 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001597 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001598 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001599 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001600 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001601 ScopedIndentation indent1(&state->vios_);
1602 DumpFields(os, obj, obj_class);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001603 const size_t image_pointer_size = state->image_header_.GetPointerSize();
Ian Rogersd5b32602012-02-26 16:40:04 -08001604 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001605 auto* obj_array = obj->AsObjectArray<mirror::Object>();
1606 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001607 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001608 size_t run = 0;
1609 for (int32_t j = i + 1; j < length; j++) {
1610 if (value == obj_array->Get(j)) {
1611 run++;
1612 } else {
1613 break;
1614 }
1615 }
1616 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001617 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001618 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001619 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001620 i = i + run;
1621 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001622 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001623 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001624 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001625 }
1626 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001627 mirror::Class* klass = obj->AsClass();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001628 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001629 os << "STATICS:\n";
1630 ScopedIndentation indent2(&state->vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001631 for (ArtField& field : klass->GetSFields()) {
1632 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001633 }
1634 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001635 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01001636 auto it = state->dex_caches_.find(obj);
1637 if (it != state->dex_caches_.end()) {
1638 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001639 const auto& field_section = state->image_header_.GetImageSection(
1640 ImageHeader::kSectionArtFields);
1641 const auto& method_section = state->image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001642 size_t num_methods = dex_cache->NumResolvedMethods();
1643 if (num_methods != 0u) {
1644 os << "Methods (size=" << num_methods << "):";
1645 ScopedIndentation indent2(&state->vios_);
1646 auto* resolved_methods = dex_cache->GetResolvedMethods();
1647 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001648 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_methods,
1649 i,
1650 image_pointer_size);
Vladimir Marko05792b92015-08-03 11:56:49 +01001651 size_t run = 0;
1652 for (size_t j = i + 1;
1653 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_methods,
1654 j,
1655 image_pointer_size);
1656 ++j, ++run) {}
1657 if (run == 0) {
1658 os << StringPrintf("%zd: ", i);
1659 } else {
1660 os << StringPrintf("%zd to %zd: ", i, i + run);
1661 i = i + run;
1662 }
1663 std::string msg;
1664 if (elem == nullptr) {
1665 msg = "null";
1666 } else if (method_section.Contains(
1667 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
1668 msg = PrettyMethod(reinterpret_cast<ArtMethod*>(elem));
1669 } else {
1670 msg = "<not in method section>";
1671 }
1672 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07001673 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001674 }
1675 size_t num_fields = dex_cache->NumResolvedFields();
1676 if (num_fields != 0u) {
1677 os << "Fields (size=" << num_fields << "):";
1678 ScopedIndentation indent2(&state->vios_);
1679 auto* resolved_fields = dex_cache->GetResolvedFields();
1680 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
1681 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_fields, i, image_pointer_size);
1682 size_t run = 0;
1683 for (size_t j = i + 1;
1684 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_fields,
1685 j,
1686 image_pointer_size);
1687 ++j, ++run) {}
1688 if (run == 0) {
1689 os << StringPrintf("%zd: ", i);
1690 } else {
1691 os << StringPrintf("%zd to %zd: ", i, i + run);
1692 i = i + run;
1693 }
1694 std::string msg;
1695 if (elem == nullptr) {
1696 msg = "null";
1697 } else if (field_section.Contains(
1698 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
1699 msg = PrettyField(reinterpret_cast<ArtField*>(elem));
1700 } else {
1701 msg = "<not in field section>";
1702 }
1703 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07001704 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001705 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001706 }
1707 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07001708 std::string temp;
1709 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001710 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001711
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001712 void DumpMethod(ArtMethod* method, std::ostream& indent_os)
Mathieu Chartier90443472015-07-16 20:32:27 -07001713 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001714 DCHECK(method != nullptr);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001715 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
1716 const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01001717 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
1718 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07001719 if (method->IsNative()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001720 bool first_occurrence;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001721 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
1722 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001723 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001724 stats_.native_to_managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001725 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001726 if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
1727 image_header_.GetPointerSize())) {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01001728 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001729 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00001730 } else if (method->IsAbstract() ||
1731 method->IsCalleeSaveMethod() ||
1732 method->IsResolutionMethod() ||
1733 (method == Runtime::Current()->GetImtConflictMethod()) ||
1734 method->IsImtUnimplementedMethod() ||
1735 method->IsClassInitializer()) {
1736 // Don't print information for these.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001737 } else {
1738 const DexFile::CodeItem* code_item = method->GetCodeItem();
1739 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001740 stats_.dex_instruction_bytes += dex_instruction_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001741
1742 bool first_occurrence;
Roland Levillain6d7f1792015-07-02 10:59:15 +01001743 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01001744 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01001745 // Method compiled with the optimizing compiler have no vmap table.
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001746 vmap_table_bytes = ComputeOatSize(method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01001747 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001748 stats_.vmap_table_bytes += vmap_table_bytes;
Roland Levillain6d7f1792015-07-02 10:59:15 +01001749 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001750 }
1751
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001752 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
1753 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001754 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001755 stats_.managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001756 if (method->IsConstructor()) {
1757 if (method->IsStatic()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001758 stats_.class_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001759 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001760 stats_.large_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001761 }
1762 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001763 stats_.large_method_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001764 }
1765 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001766 stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001767
Igor Murashkin7617abd2015-07-10 18:27:47 -07001768 uint32_t method_access_flags = method->GetAccessFlags();
1769
Mathieu Chartiere401d142015-04-22 13:56:20 -07001770 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001771 indent_os << StringPrintf("SIZE: Dex Instructions=%zd StackMaps=%zd AccessFlags=0x%x\n",
1772 dex_instruction_bytes,
1773 vmap_table_bytes,
Igor Murashkin7617abd2015-07-10 18:27:47 -07001774 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001775
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001776 size_t total_size = dex_instruction_bytes +
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001777 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
Mathieu Chartiere401d142015-04-22 13:56:20 -07001778
1779 double expansion =
1780 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001781 stats_.ComputeOutliers(total_size, expansion, method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001782 }
1783 }
1784
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001785 std::set<const void*> already_seen_;
1786 // Compute the size of the given data within the oat file and whether this is the first time
1787 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07001788 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001789 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001790 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001791 already_seen_.insert(oat_data);
1792 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001793 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001794 }
1795 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001796 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001797
1798 public:
1799 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001800 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001801 size_t file_bytes;
1802
1803 size_t header_bytes;
1804 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001805 size_t art_field_bytes;
1806 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01001807 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001808 size_t interned_strings_bytes;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001809 size_t class_table_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07001810 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001811 size_t alignment_bytes;
1812
1813 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001814 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001815 size_t managed_to_native_code_bytes;
1816 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001817 size_t class_initializer_code_bytes;
1818 size_t large_initializer_code_bytes;
1819 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001820
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001821 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001822
1823 size_t dex_instruction_bytes;
1824
Mathieu Chartiere401d142015-04-22 13:56:20 -07001825 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001826 std::vector<size_t> method_outlier_size;
1827 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07001828 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001829
Roland Levillain3887c462015-08-12 18:15:42 +01001830 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001831 : oat_file_bytes(0),
1832 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001833 header_bytes(0),
1834 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07001835 art_field_bytes(0),
1836 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01001837 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001838 interned_strings_bytes(0),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001839 class_table_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07001840 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001841 alignment_bytes(0),
1842 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001843 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001844 managed_to_native_code_bytes(0),
1845 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07001846 class_initializer_code_bytes(0),
1847 large_initializer_code_bytes(0),
1848 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001849 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001850 dex_instruction_bytes(0) {}
1851
Elliott Hughesa0e18062012-04-13 15:59:59 -07001852 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001853 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07001854 size_t bytes;
1855 size_t count;
1856 };
1857 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
1858 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001859
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001860 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001861 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
1862 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001863 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07001864 it->second.count += 1;
1865 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001866 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07001867 }
1868 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001869
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001870 double PercentOfOatBytes(size_t size) {
1871 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
1872 }
1873
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001874 double PercentOfFileBytes(size_t size) {
1875 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
1876 }
1877
1878 double PercentOfObjectBytes(size_t size) {
1879 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
1880 }
1881
Mathieu Chartiere401d142015-04-22 13:56:20 -07001882 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001883 method_outlier_size.push_back(total_size);
1884 method_outlier_expansion.push_back(expansion);
1885 method_outlier.push_back(method);
1886 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001887
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001888 void DumpOutliers(std::ostream& os)
Mathieu Chartier90443472015-07-16 20:32:27 -07001889 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001890 size_t sum_of_sizes = 0;
1891 size_t sum_of_sizes_squared = 0;
1892 size_t sum_of_expansion = 0;
1893 size_t sum_of_expansion_squared = 0;
1894 size_t n = method_outlier_size.size();
Jeff Haodcdc85b2015-12-04 14:06:18 -08001895 if (n == 0) {
1896 return;
1897 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001898 for (size_t i = 0; i < n; i++) {
1899 size_t cur_size = method_outlier_size[i];
1900 sum_of_sizes += cur_size;
1901 sum_of_sizes_squared += cur_size * cur_size;
1902 double cur_expansion = method_outlier_expansion[i];
1903 sum_of_expansion += cur_expansion;
1904 sum_of_expansion_squared += cur_expansion * cur_expansion;
1905 }
1906 size_t size_mean = sum_of_sizes / n;
1907 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
1908 double expansion_mean = sum_of_expansion / n;
1909 double expansion_variance =
1910 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
1911
1912 // Dump methods whose size is a certain number of standard deviations from the mean
1913 size_t dumped_values = 0;
1914 size_t skipped_values = 0;
1915 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
1916 size_t cur_size_variance = i * i * size_variance;
1917 bool first = true;
1918 for (size_t j = 0; j < n; j++) {
1919 size_t cur_size = method_outlier_size[j];
1920 if (cur_size > size_mean) {
1921 size_t cur_var = cur_size - size_mean;
1922 cur_var = cur_var * cur_var;
1923 if (cur_var > cur_size_variance) {
1924 if (dumped_values > 20) {
1925 if (i == 1) {
1926 skipped_values++;
1927 } else {
1928 i = 2; // jump to counting for 1 standard deviation
1929 break;
1930 }
1931 } else {
1932 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001933 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001934 first = false;
1935 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001936 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07001937 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001938 method_outlier_size[j] = 0; // don't consider this method again
1939 dumped_values++;
1940 }
1941 }
1942 }
1943 }
1944 }
1945 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001946 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001947 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001948 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001949 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001950
1951 // Dump methods whose expansion is a certain number of standard deviations from the mean
1952 dumped_values = 0;
1953 skipped_values = 0;
1954 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
1955 double cur_expansion_variance = i * i * expansion_variance;
1956 bool first = true;
1957 for (size_t j = 0; j < n; j++) {
1958 double cur_expansion = method_outlier_expansion[j];
1959 if (cur_expansion > expansion_mean) {
1960 size_t cur_var = cur_expansion - expansion_mean;
1961 cur_var = cur_var * cur_var;
1962 if (cur_var > cur_expansion_variance) {
1963 if (dumped_values > 20) {
1964 if (i == 1) {
1965 skipped_values++;
1966 } else {
1967 i = 2; // jump to counting for 1 standard deviation
1968 break;
1969 }
1970 } else {
1971 if (first) {
1972 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07001973 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001974 first = false;
1975 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001976 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07001977 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001978 method_outlier_expansion[j] = 0.0; // don't consider this method again
1979 dumped_values++;
1980 }
1981 }
1982 }
1983 }
1984 }
1985 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001986 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001987 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001988 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001989 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001990 }
1991
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001992 void Dump(std::ostream& os, std::ostream& indent_os)
Mathieu Chartier90443472015-07-16 20:32:27 -07001993 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001994 {
1995 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
1996 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01001997 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
1998 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
1999 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2000 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2001 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2002 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002003 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002004 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2005 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002006 header_bytes, PercentOfFileBytes(header_bytes),
2007 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002008 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2009 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002010 dex_cache_arrays_bytes,
2011 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002012 interned_strings_bytes,
2013 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002014 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002015 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002016 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2017 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002018 CHECK_EQ(file_bytes,
2019 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2020 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2021 bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002022 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002023
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002024 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002025 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002026 for (const auto& sizes_and_count : sizes_and_counts) {
2027 const std::string& descriptor(sizes_and_count.first);
2028 double average = static_cast<double>(sizes_and_count.second.bytes) /
2029 static_cast<double>(sizes_and_count.second.count);
2030 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002031 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002032 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002033 descriptor.c_str(), sizes_and_count.second.bytes,
2034 sizes_and_count.second.count, average, percent);
2035 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002036 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002037 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002038 CHECK_EQ(object_bytes, object_bytes_total);
2039
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002040 os << StringPrintf("oat_file_bytes = %8zd\n"
2041 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2042 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2043 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2044 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2045 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2046 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002047 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002048 managed_code_bytes,
2049 PercentOfOatBytes(managed_code_bytes),
2050 managed_to_native_code_bytes,
2051 PercentOfOatBytes(managed_to_native_code_bytes),
2052 native_to_managed_code_bytes,
2053 PercentOfOatBytes(native_to_managed_code_bytes),
2054 class_initializer_code_bytes,
2055 PercentOfOatBytes(class_initializer_code_bytes),
2056 large_initializer_code_bytes,
2057 PercentOfOatBytes(large_initializer_code_bytes),
2058 large_method_code_bytes,
2059 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002060 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002061 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002062 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002063 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2064 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002065 }
2066
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002067 os << "\n" << StringPrintf("vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002068 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002069 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002070
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002071 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2072 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002073 static_cast<double>(managed_code_bytes) /
2074 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002075 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002076 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002077 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002078
2079 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002080 }
2081 } stats_;
2082
2083 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002084 enum {
2085 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2086 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2087 kLargeConstructorDexBytes = 4000,
2088 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2089 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2090 kLargeMethodDexBytes = 16000
2091 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002092
2093 // For performance, use the *os_ directly for anything that doesn't need indentation
2094 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002095 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002096 VariableIndentationOutputStream vios_;
2097 ScopedIndentation indent1_;
2098
Ian Rogers1d54e732013-05-02 21:10:01 -07002099 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002100 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002101 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002102 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002103 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002104
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002105 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002106};
2107
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002108static int DumpImage(gc::space::ImageSpace* image_space,
2109 OatDumperOptions* options,
2110 std::ostream* os) SHARED_REQUIRES(Locks::mutator_lock_) {
2111 const ImageHeader& image_header = image_space->GetImageHeader();
2112 if (!image_header.IsValid()) {
2113 fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
2114 return EXIT_FAILURE;
2115 }
2116 ImageDumper image_dumper(os, *image_space, image_header, options);
2117 if (!image_dumper.Dump()) {
2118 return EXIT_FAILURE;
2119 }
2120 return EXIT_SUCCESS;
2121}
2122
2123static int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002124 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002125 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002126 options->class_loader_ = &null_class_loader;
2127
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002128 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002129 if (options->app_image_ != nullptr) {
2130 if (options->app_oat_ == nullptr) {
2131 LOG(ERROR) << "Can not dump app image without app oat file";
Jeff Haodcdc85b2015-12-04 14:06:18 -08002132 return EXIT_FAILURE;
2133 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002134 // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
2135 // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
2136 // pointers into 32 bit pointer sized ArtMethods.
2137 std::string error_msg;
2138 std::unique_ptr<OatFile> oat_file(OatFile::Open(options->app_oat_,
2139 options->app_oat_,
2140 nullptr,
2141 nullptr,
2142 false,
2143 /*low_4gb*/true,
2144 nullptr,
2145 &error_msg));
2146 if (oat_file == nullptr) {
2147 LOG(ERROR) << "Failed to open oat file " << options->app_oat_ << " with error " << error_msg;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002148 return EXIT_FAILURE;
2149 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002150 std::unique_ptr<gc::space::ImageSpace> space(
2151 gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg));
2152 if (space == nullptr) {
2153 LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error "
2154 << error_msg;
2155 }
2156 // Open dex files for the image.
2157 std::vector<std::unique_ptr<const DexFile>> dex_files;
2158 if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
2159 LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
2160 << error_msg;
2161 }
2162 // Dump the actual image.
2163 int result = DumpImage(space.get(), options, os);
2164 if (result != EXIT_SUCCESS) {
2165 return result;
2166 }
2167 // Fall through to dump the boot images.
2168 }
2169
2170 gc::Heap* heap = runtime->GetHeap();
2171 CHECK(heap->HasBootImageSpace()) << "No image spaces";
2172 for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
2173 int result = DumpImage(image_space, options, os);
2174 if (result != EXIT_SUCCESS) {
2175 return result;
2176 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002177 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002178 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002179}
2180
Andreas Gampe00b25f32014-09-17 21:49:05 -07002181static int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
2182 std::ostream* os) {
2183 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2184
2185 Thread* self = Thread::Current();
2186 CHECK(self != nullptr);
2187 // Need well-known-classes.
2188 WellKnownClasses::Init(self->GetJniEnv());
2189
2190 // Need to register dex files to get a working dex cache.
2191 ScopedObjectAccess soa(self);
2192 ClassLinker* class_linker = runtime->GetClassLinker();
Mathieu Chartierd57d4542015-10-14 10:55:30 -07002193 runtime->GetOatFileManager().RegisterOatFile(std::unique_ptr<const OatFile>(oat_file));
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002194 std::vector<const DexFile*> class_path;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002195 for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
2196 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002197 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002198 CHECK(dex_file != nullptr) << error_msg;
Mathieu Chartierd57d4542015-10-14 10:55:30 -07002199 class_linker->RegisterDexFile(*dex_file, runtime->GetLinearAlloc());
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002200 class_path.push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002201 }
2202
2203 // Need a class loader.
Andreas Gampe00b25f32014-09-17 21:49:05 -07002204 // Fake that we're a compiler.
Mathieu Chartier966878d2016-01-14 14:33:29 -08002205 jobject class_loader = class_linker->CreatePathClassLoader(self, class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002206
2207 // Use the class loader while dumping.
2208 StackHandleScope<1> scope(self);
2209 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
2210 soa.Decode<mirror::ClassLoader*>(class_loader));
2211 options->class_loader_ = &loader_handle;
2212
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002213 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002214 bool success = oat_dumper.Dump(*os);
2215 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2216}
2217
2218static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002219 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002220 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002221 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002222 options->class_loader_ = &null_class_loader;
2223
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002224 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002225 bool success = oat_dumper.Dump(*os);
2226 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2227}
2228
2229static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2230 std::ostream* os) {
2231 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002232 OatFile* oat_file = OatFile::Open(oat_filename,
2233 oat_filename,
2234 nullptr,
2235 nullptr,
2236 false,
2237 /*low_4gb*/false,
2238 nullptr,
2239 &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002240 if (oat_file == nullptr) {
2241 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2242 return EXIT_FAILURE;
2243 }
2244
2245 if (runtime != nullptr) {
2246 return DumpOatWithRuntime(runtime, oat_file, options, os);
2247 } else {
2248 return DumpOatWithoutRuntime(oat_file, options, os);
2249 }
2250}
2251
David Srbecky2fdd03c2016-03-10 15:32:37 +00002252static int SymbolizeOat(const char* oat_filename, std::string& output_name, bool no_bits) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002253 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002254 OatFile* oat_file = OatFile::Open(oat_filename,
2255 oat_filename,
2256 nullptr,
2257 nullptr,
2258 false,
2259 /*low_4gb*/false,
2260 nullptr,
2261 &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002262 if (oat_file == nullptr) {
2263 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2264 return EXIT_FAILURE;
2265 }
2266
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002267 bool result;
2268 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
2269 // files for 64-bit code in the past.
2270 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002271 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file, output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002272 result = oat_symbolizer.Symbolize();
2273 } else {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002274 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file, output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002275 result = oat_symbolizer.Symbolize();
2276 }
2277 if (!result) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002278 fprintf(stderr, "Failed to symbolize\n");
2279 return EXIT_FAILURE;
2280 }
2281
2282 return EXIT_SUCCESS;
2283}
2284
Igor Murashkin37743352014-11-13 14:38:00 -08002285struct OatdumpArgs : public CmdlineArgs {
2286 protected:
2287 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002288
Igor Murashkin37743352014-11-13 14:38:00 -08002289 virtual ParseStatus ParseCustom(const StringPiece& option,
2290 std::string* error_msg) OVERRIDE {
2291 {
2292 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
2293 if (base_parse != kParseUnknownArgument) {
2294 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002295 }
2296 }
2297
Igor Murashkin37743352014-11-13 14:38:00 -08002298 if (option.starts_with("--oat-file=")) {
2299 oat_filename_ = option.substr(strlen("--oat-file=")).data();
2300 } else if (option.starts_with("--image=")) {
2301 image_location_ = option.substr(strlen("--image=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002302 } else if (option == "--no-dump:vmap") {
2303 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01002304 } else if (option =="--dump:code_info_stack_maps") {
2305 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002306 } else if (option == "--no-disassemble") {
2307 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00002308 } else if (option =="--header-only") {
2309 dump_header_only_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002310 } else if (option.starts_with("--symbolize=")) {
2311 oat_filename_ = option.substr(strlen("--symbolize=")).data();
2312 symbolize_ = true;
David Srbecky2fdd03c2016-03-10 15:32:37 +00002313 } else if (option.starts_with("--only-keep-debug")) {
2314 only_keep_debug_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002315 } else if (option.starts_with("--class-filter=")) {
2316 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002317 } else if (option.starts_with("--method-filter=")) {
2318 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002319 } else if (option.starts_with("--list-classes")) {
2320 list_classes_ = true;
2321 } else if (option.starts_with("--list-methods")) {
2322 list_methods_ = true;
2323 } else if (option.starts_with("--export-dex-to=")) {
2324 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
2325 } else if (option.starts_with("--addr2instr=")) {
2326 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
2327 *error_msg = "Address conversion failed";
2328 return kParseError;
2329 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002330 } else if (option.starts_with("--app-image=")) {
2331 app_image_ = option.substr(strlen("--app-image=")).data();
2332 } else if (option.starts_with("--app-oat=")) {
2333 app_oat_ = option.substr(strlen("--app-oat=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002334 } else {
2335 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002336 }
2337
Igor Murashkin37743352014-11-13 14:38:00 -08002338 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002339 }
2340
Igor Murashkin37743352014-11-13 14:38:00 -08002341 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
2342 // Infer boot image location from the image location if possible.
2343 if (boot_image_location_ == nullptr) {
2344 boot_image_location_ = image_location_;
2345 }
2346
2347 // Perform the parent checks.
2348 ParseStatus parent_checks = Base::ParseChecks(error_msg);
2349 if (parent_checks != kParseOk) {
2350 return parent_checks;
2351 }
2352
2353 // Perform our own checks.
2354 if (image_location_ == nullptr && oat_filename_ == nullptr) {
2355 *error_msg = "Either --image or --oat-file must be specified";
2356 return kParseError;
2357 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
2358 *error_msg = "Either --image or --oat-file must be specified but not both";
2359 return kParseError;
2360 }
2361
2362 return kParseOk;
2363 }
2364
2365 virtual std::string GetUsage() const {
2366 std::string usage;
2367
2368 usage +=
2369 "Usage: oatdump [options] ...\n"
2370 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
2371 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
2372 "\n"
2373 // Either oat-file or image is required.
2374 " --oat-file=<file.oat>: specifies an input oat filename.\n"
2375 " Example: --oat-file=/system/framework/boot.oat\n"
2376 "\n"
2377 " --image=<file.art>: specifies an input image location.\n"
2378 " Example: --image=/system/framework/boot.art\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002379 "\n"
2380 " --app-image=<file.art>: specifies an input app image. Must also have a specified\n"
2381 " boot image and app oat file.\n"
2382 " Example: --app-image=app.art\n"
2383 "\n"
2384 " --app-oat=<file.odex>: specifies an input app oat.\n"
2385 " Example: --app-oat=app.odex\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002386 "\n";
2387
2388 usage += Base::GetUsage();
2389
2390 usage += // Optional.
Igor Murashkin37743352014-11-13 14:38:00 -08002391 " --no-dump:vmap may be used to disable vmap dumping.\n"
2392 " Example: --no-dump:vmap\n"
2393 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01002394 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
2395 " Example: --dump:code_info_stack_maps\n"
2396 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002397 " --no-disassemble may be used to disable disassembly.\n"
2398 " Example: --no-disassemble\n"
2399 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00002400 " --header-only may be used to print only the oat header.\n"
2401 " Example: --header-only\n"
2402 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002403 " --list-classes may be used to list target file classes (can be used with filters).\n"
2404 " Example: --list-classes\n"
2405 " Example: --list-classes --class-filter=com.example.foo\n"
2406 "\n"
2407 " --list-methods may be used to list target file methods (can be used with filters).\n"
2408 " Example: --list-methods\n"
2409 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
2410 "\n"
2411 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
2412 " Example: --symbolize=/system/framework/boot.oat\n"
2413 "\n"
David Srbecky2fdd03c2016-03-10 15:32:37 +00002414 " --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
2415 " .rodata and .text sections are omitted in the output file to save space.\n"
2416 " Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
2417 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002418 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
2419 " Example: --class-filter=com.example.foo\n"
2420 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002421 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
2422 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002423 "\n"
2424 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
2425 " Example: --export-dex-to=/data/local/tmp\n"
2426 "\n"
2427 " --addr2instr=<address>: output matching method disassembled code from relative\n"
2428 " address (e.g. PC from crash dump)\n"
2429 " Example: --addr2instr=0x00001a3b\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002430 "\n";
2431
2432 return usage;
2433 }
2434
2435 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07002436 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002437 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00002438 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07002439 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002440 std::string elf_filename_prefix_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002441 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01002442 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002443 bool disassemble_code_ = true;
2444 bool symbolize_ = false;
David Srbecky2fdd03c2016-03-10 15:32:37 +00002445 bool only_keep_debug_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002446 bool list_classes_ = false;
2447 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00002448 bool dump_header_only_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002449 uint32_t addr2instr_ = 0;
2450 const char* export_dex_location_ = nullptr;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002451 const char* app_image_ = nullptr;
2452 const char* app_oat_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002453};
2454
Igor Murashkin37743352014-11-13 14:38:00 -08002455struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
2456 virtual bool NeedsRuntime() OVERRIDE {
2457 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002458
Igor Murashkin37743352014-11-13 14:38:00 -08002459 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
2460 bool absolute_addresses = (args_->oat_filename_ == nullptr);
2461
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002462 oat_dumper_options_.reset(new OatDumperOptions(
Igor Murashkin37743352014-11-13 14:38:00 -08002463 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01002464 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08002465 args_->disassemble_code_,
2466 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002467 args_->class_filter_,
2468 args_->method_filter_,
2469 args_->list_classes_,
2470 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00002471 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002472 args_->export_dex_location_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002473 args_->app_image_,
2474 args_->app_oat_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002475 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08002476
2477 return (args_->boot_image_location_ != nullptr || args_->image_location_ != nullptr) &&
2478 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002479 }
2480
Igor Murashkin37743352014-11-13 14:38:00 -08002481 virtual bool ExecuteWithoutRuntime() OVERRIDE {
2482 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08002483 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002484
Mathieu Chartierd424d082014-10-15 10:31:46 -07002485 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08002486
Andreas Gampec24f3992014-12-17 20:40:11 -08002487 if (args_->symbolize_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002488 // ELF has special kind of section called SHT_NOBITS which allows us to create
2489 // sections which exist but their data is omitted from the ELF file to save space.
2490 // This is what "strip --only-keep-debug" does when it creates separate ELF file
2491 // with only debug data. We use it in similar way to exclude .rodata and .text.
2492 bool no_bits = args_->only_keep_debug_;
2493 return SymbolizeOat(args_->oat_filename_, args_->output_name_, no_bits) == EXIT_SUCCESS;
Andreas Gampec24f3992014-12-17 20:40:11 -08002494 } else {
2495 return DumpOat(nullptr,
2496 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002497 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08002498 args_->os_) == EXIT_SUCCESS;
2499 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002500 }
2501
Igor Murashkin37743352014-11-13 14:38:00 -08002502 virtual bool ExecuteWithRuntime(Runtime* runtime) {
2503 CHECK(args_ != nullptr);
2504
2505 if (args_->oat_filename_ != nullptr) {
2506 return DumpOat(runtime,
2507 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002508 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08002509 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002510 }
Igor Murashkin37743352014-11-13 14:38:00 -08002511
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002512 return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002513 }
2514
Igor Murashkin37743352014-11-13 14:38:00 -08002515 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
2516};
Andreas Gampe00b25f32014-09-17 21:49:05 -07002517
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002518} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07002519
2520int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08002521 art::OatdumpMain main;
2522 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002523}