blob: f5458c067d447d844c0ecfae099b23b1ae1363c4 [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"
Andreas Gampe2ba88952016-04-29 17:52:07 -070062#include "type_lookup_table.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080063#include "verifier/method_verifier.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070064#include "well_known_classes.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070065
Igor Murashkin37743352014-11-13 14:38:00 -080066#include <sys/stat.h>
67#include "cmdline.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070068
Igor Murashkin37743352014-11-13 14:38:00 -080069namespace art {
Brian Carlstrom78128a62011-09-15 17:21:19 -070070
Mathieu Chartiere401d142015-04-22 13:56:20 -070071const char* image_methods_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -080072 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -070073 "kImtConflictMethod",
Mathieu Chartier2d2621a2014-10-23 16:48:06 -070074 "kImtUnimplementedMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070075 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -070076 "kRefsOnlySaveMethod",
77 "kRefsAndArgsSaveMethod",
Mathieu Chartiere401d142015-04-22 13:56:20 -070078};
79
80const char* image_roots_descriptions_[] = {
Brian Carlstrom58ae9412011-10-04 00:56:06 -070081 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070082 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070083};
84
Mathieu Chartierac8f4392015-08-27 13:54:20 -070085// Map is so that we don't allocate multiple dex files for the same OatDexFile.
86static std::map<const OatFile::OatDexFile*,
87 std::unique_ptr<const DexFile>> opened_dex_files;
88
89const DexFile* OpenDexFile(const OatFile::OatDexFile* oat_dex_file, std::string* error_msg) {
90 DCHECK(oat_dex_file != nullptr);
91 auto it = opened_dex_files.find(oat_dex_file);
92 if (it != opened_dex_files.end()) {
93 return it->second.get();
94 }
95 const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
96 opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
97 return ret;
98}
99
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800100template <typename ElfTypes>
David Srbeckybc90fd02015-04-22 19:40:27 +0100101class OatSymbolizer FINAL {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700102 public:
David Srbecky2fdd03c2016-03-10 15:32:37 +0000103 OatSymbolizer(const OatFile* oat_file, const std::string& output_name, bool no_bits) :
104 oat_file_(oat_file),
105 builder_(nullptr),
106 output_name_(output_name.empty() ? "symbolized.oat" : output_name),
107 no_bits_(no_bits) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700108 }
109
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700110 bool Symbolize() {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000111 const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
David Srbecky5d811202016-03-08 13:21:22 +0000112 const InstructionSetFeatures* features = InstructionSetFeatures::FromBitmap(
113 isa, oat_file_->GetOatHeader().GetInstructionSetFeaturesBitmap());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000114
115 File* elf_file = OS::CreateEmptyFile(output_name_.c_str());
116 std::unique_ptr<BufferedOutputStream> output_stream(
Vladimir Marko10c13562015-11-25 14:33:36 +0000117 MakeUnique<BufferedOutputStream>(MakeUnique<FileOutputStream>(elf_file)));
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800118 builder_.reset(new ElfBuilder<ElfTypes>(isa, features, output_stream.get()));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000119
120 builder_->Start();
121
122 auto* rodata = builder_->GetRoData();
123 auto* text = builder_->GetText();
124 auto* bss = builder_->GetBss();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000125
David Srbecky6d8c8f02015-10-26 10:57:09 +0000126 const uint8_t* rodata_begin = oat_file_->Begin();
127 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
David Srbecky2fdd03c2016-03-10 15:32:37 +0000128 if (no_bits_) {
129 rodata->WriteNoBitsSection(rodata_size);
130 } else {
131 rodata->Start();
132 rodata->WriteFully(rodata_begin, rodata_size);
133 rodata->End();
134 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000135
David Srbecky6d8c8f02015-10-26 10:57:09 +0000136 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
137 const size_t text_size = oat_file_->End() - text_begin;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000138 if (no_bits_) {
139 text->WriteNoBitsSection(text_size);
140 } else {
141 text->Start();
142 text->WriteFully(text_begin, text_size);
143 text->End();
144 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000145
146 if (oat_file_->BssSize() != 0) {
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000147 bss->WriteNoBitsSection(oat_file_->BssSize());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000148 }
149
Douglas Leung316a2182015-09-17 15:26:25 -0700150 if (isa == kMips || isa == kMips64) {
151 builder_->WriteMIPSabiflagsSection();
152 }
Vladimir Marko944da602016-02-19 12:27:55 +0000153 builder_->PrepareDynamicSection(
154 elf_file->GetPath(), rodata_size, text_size, oat_file_->BssSize());
155 builder_->WriteDynamicSection();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700156
David Srbecky5d950762016-03-07 20:47:29 +0000157 Walk();
158 for (const auto& trampoline : debug::MakeTrampolineInfos(oat_file_->GetOatHeader())) {
159 method_debug_infos_.push_back(trampoline);
160 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700161
David Srbecky5d950762016-03-07 20:47:29 +0000162 debug::WriteDebugInfo(builder_.get(),
163 ArrayRef<const debug::MethodDebugInfo>(method_debug_infos_),
164 dwarf::DW_DEBUG_FRAME_FORMAT,
165 true /* write_oat_patches */);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700166
David Srbecky6d8c8f02015-10-26 10:57:09 +0000167 builder_->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700168
Vladimir Marko10c13562015-11-25 14:33:36 +0000169 return builder_->Good();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700170 }
171
David Srbecky5d950762016-03-07 20:47:29 +0000172 void Walk() {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700173 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
174 for (size_t i = 0; i < oat_dex_files.size(); i++) {
175 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700176 CHECK(oat_dex_file != nullptr);
David Srbecky5d950762016-03-07 20:47:29 +0000177 WalkOatDexFile(oat_dex_file);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700178 }
179 }
180
David Srbecky5d950762016-03-07 20:47:29 +0000181 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700182 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700183 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
184 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700185 return;
186 }
187 for (size_t class_def_index = 0;
188 class_def_index < dex_file->NumClassDefs();
189 class_def_index++) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700190 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
191 OatClassType type = oat_class.GetType();
192 switch (type) {
193 case kOatClassAllCompiled:
194 case kOatClassSomeCompiled:
David Srbecky5d950762016-03-07 20:47:29 +0000195 WalkOatClass(oat_class, *dex_file, class_def_index);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700196 break;
197
198 case kOatClassNoneCompiled:
199 case kOatClassMax:
200 // Ignore.
201 break;
202 }
203 }
204 }
205
David Srbecky5d950762016-03-07 20:47:29 +0000206 void WalkOatClass(const OatFile::OatClass& oat_class,
207 const DexFile& dex_file,
208 uint32_t class_def_index) {
209 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700210 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700211 if (class_data == nullptr) { // empty class such as a marker interface?
212 return;
213 }
214 // Note: even if this is an interface or a native class, we still have to walk it, as there
215 // might be a static initializer.
216 ClassDataItemIterator it(dex_file, class_data);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700217 uint32_t class_method_idx = 0;
David Srbecky5d950762016-03-07 20:47:29 +0000218 for (; it.HasNextStaticField(); it.Next()) { /* skip */ }
219 for (; it.HasNextInstanceField(); it.Next()) { /* skip */ }
220 for (; it.HasNextDirectMethod() || it.HasNextVirtualMethod(); it.Next()) {
221 WalkOatMethod(oat_class.GetOatMethod(class_method_idx++),
222 dex_file,
223 class_def_index,
224 it.GetMemberIndex(),
225 it.GetMethodCodeItem(),
226 it.GetMethodAccessFlags());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700227 }
228 DCHECK(!it.HasNext());
229 }
230
David Srbecky5d950762016-03-07 20:47:29 +0000231 void WalkOatMethod(const OatFile::OatMethod& oat_method,
232 const DexFile& dex_file,
233 uint32_t class_def_index,
234 uint32_t dex_method_index,
235 const DexFile::CodeItem* code_item,
236 uint32_t method_access_flags) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700237 if ((method_access_flags & kAccAbstract) != 0) {
238 // Abstract method, no code.
239 return;
240 }
David Srbecky5d950762016-03-07 20:47:29 +0000241 const OatHeader& oat_header = oat_file_->GetOatHeader();
242 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
243 if (method_header == nullptr || method_header->GetCodeSize() == 0) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700244 // No code.
245 return;
246 }
247
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100248 uint32_t entry_point = oat_method.GetCodeOffset() - oat_header.GetExecutableOffset();
249 // Clear Thumb2 bit.
250 const void* code_address = EntryPointToCodePointer(reinterpret_cast<void*>(entry_point));
251
David Srbecky5d950762016-03-07 20:47:29 +0000252 debug::MethodDebugInfo info = debug::MethodDebugInfo();
253 info.trampoline_name = nullptr;
254 info.dex_file = &dex_file;
255 info.class_def_index = class_def_index;
256 info.dex_method_index = dex_method_index;
257 info.access_flags = method_access_flags;
258 info.code_item = code_item;
259 info.isa = oat_header.GetInstructionSet();
260 info.deduped = !seen_offsets_.insert(oat_method.GetCodeOffset()).second;
261 info.is_native_debuggable = oat_header.IsNativeDebuggable();
262 info.is_optimized = method_header->IsOptimized();
263 info.is_code_address_text_relative = true;
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100264 info.code_address = reinterpret_cast<uintptr_t>(code_address);
David Srbecky5d950762016-03-07 20:47:29 +0000265 info.code_size = method_header->GetCodeSize();
266 info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
267 info.code_info = info.is_optimized ? method_header->GetOptimizedCodeInfoPtr() : nullptr;
268 info.cfi = ArrayRef<uint8_t>();
269 method_debug_infos_.push_back(info);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700270 }
271
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700272 private:
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700273 const OatFile* oat_file_;
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800274 std::unique_ptr<ElfBuilder<ElfTypes> > builder_;
David Srbecky5d950762016-03-07 20:47:29 +0000275 std::vector<debug::MethodDebugInfo> method_debug_infos_;
276 std::unordered_set<uint32_t> seen_offsets_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700277 const std::string output_name_;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000278 bool no_bits_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700279};
280
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700281class OatDumperOptions {
282 public:
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100283 OatDumperOptions(bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100284 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700285 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700286 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800287 const char* class_filter,
288 const char* method_filter,
289 bool list_classes,
290 bool list_methods,
David Brazdilc03d7b62016-03-02 12:18:03 +0000291 bool dump_header_only,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800292 const char* export_dex_location,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800293 const char* app_image,
294 const char* app_oat,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800295 uint32_t addr2instr)
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100296 : dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100297 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700298 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700299 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800300 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000301 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800302 list_classes_(list_classes),
303 list_methods_(list_methods),
David Brazdilc03d7b62016-03-02 12:18:03 +0000304 dump_header_only_(dump_header_only),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800305 export_dex_location_(export_dex_location),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800306 app_image_(app_image),
307 app_oat_(app_oat),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800308 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800309 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700310
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700311 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100312 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700313 const bool disassemble_code_;
314 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800315 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000316 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800317 const bool list_classes_;
318 const bool list_methods_;
David Brazdilc03d7b62016-03-02 12:18:03 +0000319 const bool dump_header_only_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800320 const char* const export_dex_location_;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800321 const char* const app_image_;
322 const char* const app_oat_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800323 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700324 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700325};
326
Elliott Hughese3c845c2012-02-28 17:23:01 -0800327class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700328 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100329 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000330 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800331 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700332 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800333 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800334 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
335 disassembler_(Disassembler::Create(instruction_set_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800336 new DisassemblerOptions(options_.absolute_addresses_,
Alexandre Ramesa37d9252014-10-27 11:28:14 +0000337 oat_file.Begin(),
Aart Bikd3059e72016-05-11 10:30:47 -0700338 oat_file.End(),
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100339 true /* can_read_literals_ */))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800340 CHECK(options_.class_loader_ != nullptr);
341 CHECK(options_.class_filter_ != nullptr);
342 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800343 AddAllOffsets();
344 }
345
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700346 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700347 delete disassembler_;
348 }
349
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800350 InstructionSet GetInstructionSet() {
351 return instruction_set_;
352 }
353
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700354 bool Dump(std::ostream& os) {
355 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800356 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700357
358 os << "MAGIC:\n";
359 os << oat_header.GetMagic() << "\n\n";
360
Jeff Hao4b07a6e2016-01-15 12:50:44 -0800361 os << "LOCATION:\n";
362 os << oat_file_.GetLocation() << "\n\n";
363
Brian Carlstromaded5f72011-10-07 17:15:04 -0700364 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800365 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700366
Elliott Hughesa72ec822012-03-05 17:12:22 -0800367 os << "INSTRUCTION SET:\n";
368 os << oat_header.GetInstructionSet() << "\n\n";
369
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700370 {
371 std::unique_ptr<const InstructionSetFeatures> features(
372 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
373 oat_header.GetInstructionSetFeaturesBitmap()));
374 os << "INSTRUCTION SET FEATURES:\n";
375 os << features->GetFeatureString() << "\n\n";
376 }
Dave Allison70202782013-10-22 17:52:19 -0700377
Brian Carlstromaded5f72011-10-07 17:15:04 -0700378 os << "DEX FILE COUNT:\n";
379 os << oat_header.GetDexFileCount() << "\n\n";
380
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800381#define DUMP_OAT_HEADER_OFFSET(label, offset) \
382 os << label " OFFSET:\n"; \
383 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800384 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800385 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
386 } \
387 os << StringPrintf("\n\n");
388
389 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
390 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
391 GetInterpreterToInterpreterBridgeOffset);
392 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
393 GetInterpreterToCompiledCodeBridgeOffset);
394 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
395 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800396 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
397 GetQuickGenericJniTrampolineOffset);
398 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
399 GetQuickImtConflictTrampolineOffset);
400 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
401 GetQuickResolutionTrampolineOffset);
402 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
403 GetQuickToInterpreterBridgeOffset);
404#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700405
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700406 os << "IMAGE PATCH DELTA:\n";
407 os << StringPrintf("%d (0x%08x)\n\n",
408 oat_header.GetImagePatchDelta(),
409 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700410
Brian Carlstrom28db0122012-10-18 16:20:41 -0700411 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
412 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
413
414 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800415 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700416
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700417 // Print the key-value store.
418 {
419 os << "KEY VALUE STORE:\n";
420 size_t index = 0;
421 const char* key;
422 const char* value;
423 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
424 os << key << " = " << value << "\n";
425 index++;
426 }
427 os << "\n";
428 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700429
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800430 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700431 os << "BEGIN:\n";
432 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700433
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700434 os << "END:\n";
435 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
436 }
437
438 os << "SIZE:\n";
439 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700440
441 os << std::flush;
442
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800443 // If set, adjust relative address to be searched
444 if (options_.addr2instr_ != 0) {
445 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
446 os << "SEARCH ADDRESS (executable offset + input):\n";
447 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
448 }
449
David Brazdilc03d7b62016-03-02 12:18:03 +0000450 if (!options_.dump_header_only_) {
451 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
452 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
453 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800454
David Brazdilc03d7b62016-03-02 12:18:03 +0000455 // If file export selected skip file analysis
456 if (options_.export_dex_location_) {
457 if (!ExportDexFile(os, *oat_dex_file)) {
458 success = false;
459 }
460 } else {
461 if (!DumpOatDexFile(os, *oat_dex_file)) {
462 success = false;
463 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800464 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700465 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700466 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000467
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700468 os << std::flush;
469 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700470 }
471
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800472 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700473 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
474 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800475 return 0; // Address not in oat file
476 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800477 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
478 reinterpret_cast<uintptr_t>(oat_file_.Begin());
479 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800480 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800481 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800482 return end_offset - begin_offset;
483 }
484
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800485 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700486 return oat_file_.GetOatHeader().GetInstructionSet();
487 }
488
Mathieu Chartier90443472015-07-16 20:32:27 -0700489 const void* GetQuickOatCode(ArtMethod* m) SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800490 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
491 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700492 CHECK(oat_dex_file != nullptr);
493 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700494 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
495 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700496 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
497 << "': " << error_msg;
498 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800499 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700500 const DexFile::ClassDef* class_def =
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800501 dex_file->FindClassDef(descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700502 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700503 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100504 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800505 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100506 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800507 }
508 }
509 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700510 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800511 }
512
Brian Carlstromaded5f72011-10-07 17:15:04 -0700513 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800514 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800515 // We don't know the length of the code for each method, but we need to know where to stop
516 // when disassembling. What we do know is that a region of code will be followed by some other
517 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
518 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800519 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
520 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700521 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700522 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700523 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
524 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700525 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
526 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800527 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800528 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800529 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800530 for (size_t class_def_index = 0;
531 class_def_index < dex_file->NumClassDefs();
532 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800533 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100534 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700535 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700536 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800537 ClassDataItemIterator it(*dex_file, class_data);
538 SkipAllFields(it);
539 uint32_t class_method_index = 0;
540 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100541 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800542 it.Next();
543 }
544 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100545 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800546 it.Next();
547 }
548 }
549 }
550 }
551
552 // If the last thing in the file is code for a method, there won't be an offset for the "next"
553 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
554 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800555 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800556 }
557
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700558 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
559 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
560 }
561
Elliott Hughese3c845c2012-02-28 17:23:01 -0800562 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800563 uint32_t code_offset = oat_method.GetCodeOffset();
564 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
565 code_offset &= ~0x1;
566 }
567 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800568 offsets_.insert(oat_method.GetVmapTableOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800569 }
570
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700571 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
572 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800573 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700574 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800575 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800576 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700577
Andreas Gampe2ba88952016-04-29 17:52:07 -0700578 // Print embedded dex file data range.
579 const uint8_t* const oat_file_begin = oat_dex_file.GetOatFile()->Begin();
580 const uint8_t* const dex_file_pointer = oat_dex_file.GetDexFilePointer();
581 uint32_t dex_offset = dchecked_integral_cast<uint32_t>(dex_file_pointer - oat_file_begin);
582 os << StringPrintf("dex-file: 0x%08x..0x%08x\n",
583 dex_offset,
584 dchecked_integral_cast<uint32_t>(dex_offset + oat_dex_file.FileSize() - 1));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700585
Andreas Gampe2ba88952016-04-29 17:52:07 -0700586 // Create the dex file early. A lot of print-out things depend on it.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700587 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700588 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
589 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700590 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700591 os << std::flush;
592 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700593 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100594
Andreas Gampe2ba88952016-04-29 17:52:07 -0700595 // Print lookup table, if it exists.
596 if (oat_dex_file.GetLookupTableData() != nullptr) {
597 uint32_t table_offset = dchecked_integral_cast<uint32_t>(
598 oat_dex_file.GetLookupTableData() - oat_file_begin);
599 uint32_t table_size = TypeLookupTable::RawDataLength(*dex_file);
600 os << StringPrintf("type-table: 0x%08x..0x%08x\n",
601 table_offset,
602 table_offset + table_size - 1);
603 }
604
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100605 VariableIndentationOutputStream vios(&os);
606 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800607 for (size_t class_def_index = 0;
608 class_def_index < dex_file->NumClassDefs();
609 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700610 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
611 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800612
613 // TODO: Support regex
614 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
615 continue;
616 }
617
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700618 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100619 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700620 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
621 class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100622 << " (" << oat_class.GetStatus() << ")"
623 << " (" << oat_class.GetType() << ")\n";
624 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800625 if (options_.list_classes_) continue;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700626 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700627 success = false;
628 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800629 if (stop_analysis) {
630 os << std::flush;
631 return success;
632 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700633 }
634
635 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700636 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700637 }
638
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800639 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
640 std::string error_msg;
641 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
642
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700643 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800644 if (dex_file == nullptr) {
645 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
646 return false;
647 }
648 size_t fsize = oat_dex_file.FileSize();
649
650 // Some quick checks just in case
651 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
652 os << "Invalid dex file\n";
653 return false;
654 }
655
656 // Verify output directory exists
657 if (!OS::DirectoryExists(options_.export_dex_location_)) {
658 // TODO: Extend OS::DirectoryExists if symlink support is required
659 os << options_.export_dex_location_ << " output directory not found or symlink\n";
660 return false;
661 }
662
663 // Beautify path names
664 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
665 return false;
666 }
667
668 std::string dex_orig_name;
669 size_t dex_orig_pos = dex_file_location.rfind('/');
670 if (dex_orig_pos == std::string::npos)
671 dex_orig_name = dex_file_location;
672 else
673 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
674
675 // A more elegant approach to efficiently name user installed apps is welcome
676 if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
677 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
678 size_t apk_orig_pos = dex_file_location.rfind('/');
679 if (apk_orig_pos != std::string::npos) {
680 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
681 }
682 }
683
684 std::string out_dex_path(options_.export_dex_location_);
685 if (out_dex_path.back() != '/') {
686 out_dex_path.append("/");
687 }
688 out_dex_path.append(dex_orig_name);
689 out_dex_path.append("_export.dex");
690 if (out_dex_path.length() > PATH_MAX) {
691 return false;
692 }
693
694 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
695 if (file.get() == nullptr) {
696 os << "Failed to open output dex file " << out_dex_path;
697 return false;
698 }
699
700 if (!file->WriteFully(dex_file->Begin(), fsize)) {
701 os << "Failed to write dex file";
702 file->Erase();
703 return false;
704 }
705
706 if (file->FlushCloseOrErase() != 0) {
707 os << "Flush and close failed";
708 return false;
709 }
710
711 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
712 os << std::flush;
713
714 return true;
715 }
716
Elliott Hughese3c845c2012-02-28 17:23:01 -0800717 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700718 while (it.HasNextStaticField()) {
719 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700720 }
Ian Rogers0571d352011-11-03 19:51:38 -0700721 while (it.HasNextInstanceField()) {
722 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700723 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800724 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700725
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100726 bool DumpOatClass(VariableIndentationOutputStream* vios,
727 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800728 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700729 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800730 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -0700731 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700732 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100733 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700734 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800735 }
736 ClassDataItemIterator it(dex_file, class_data);
737 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700738 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700739 while (it.HasNextDirectMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100740 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700741 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800742 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700743 success = false;
744 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800745 if (addr_found) {
746 *stop_analysis = true;
747 return success;
748 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700749 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700750 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700751 }
Ian Rogers0571d352011-11-03 19:51:38 -0700752 while (it.HasNextVirtualMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100753 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700754 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800755 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700756 success = false;
757 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800758 if (addr_found) {
759 *stop_analysis = true;
760 return success;
761 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700762 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700763 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700764 }
Ian Rogers0571d352011-11-03 19:51:38 -0700765 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100766 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700767 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700768 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800769
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700770 static constexpr uint32_t kPrologueBytes = 16;
771
772 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
773 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
774
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100775 bool DumpOatMethod(VariableIndentationOutputStream* vios,
776 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700777 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700778 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800779 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800780 uint32_t method_access_flags, bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700781 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800782
783 // TODO: Support regex
784 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
785 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000786 return success;
787 }
788
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800789 std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100790 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
791 class_method_index, pretty_method.c_str(),
792 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800793 if (options_.list_methods_) return success;
794
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800795 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
796 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
797 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
798 uint32_t code_offset = oat_method.GetCodeOffset();
799 uint32_t code_size = oat_method.GetQuickCodeSize();
800 if (resolved_addr2instr_ != 0) {
801 if (resolved_addr2instr_ > code_offset + code_size) {
802 return success;
803 } else {
804 *addr_found = true; // stop analyzing file at next iteration
805 }
806 }
807
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100808 // Everything below is indented at least once.
809 ScopedIndentation indent1(vios);
810
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800811 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100812 vios->Stream() << "DEX CODE:\n";
813 ScopedIndentation indent2(vios);
814 DumpDexCode(vios->Stream(), dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700815 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700816
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700817 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700818 std::unique_ptr<verifier::MethodVerifier> verifier;
819 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700820 // We need to have the handle scope stay live until after the verifier since the verifier has
821 // a handle to the dex cache from hs.
822 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100823 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
824 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700825 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100826 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700827 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -0700828 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800829 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100830 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800831 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100832 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100833 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100834 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700835 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100836 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700837 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
838 oat_method_offsets_offset, oat_file_.Size());
839 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100840 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700841 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800842 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700843
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100844 ScopedIndentation indent2(vios);
845 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700846 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
847 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100848 vios->Stream() << StringPrintf("WARNING: "
849 "code offset 0x%08x is past end of file 0x%08zx.\n",
850 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700851 success = false;
852 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100853 vios->Stream() << "\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800854 }
855 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100856 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700857 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
858 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700859
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800860 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100861 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700862 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100863 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700864 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100865 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700866 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
867 method_header_offset, oat_file_.Size());
868 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100869 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700870 return false;
871 }
872
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100873 ScopedIndentation indent2(vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100874 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800875 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100876 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700877 }
878 uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100879 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700880 if (vmap_table_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100881 vios->Stream() << StringPrintf("WARNING: "
882 "vmap table offset 0x%08x is past end of file 0x%08zx. "
883 "vmap table offset was loaded from offset 0x%08x.\n",
884 vmap_table_offset, oat_file_.Size(),
885 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700886 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800887 } else if (options_.dump_vmap_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100888 DumpVmapData(vios, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700889 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800890 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700891 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100892 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700893
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100894 ScopedIndentation indent2(vios);
895 vios->Stream()
896 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
897 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
898 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
899 vios->Stream() << "\n";
900 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
901 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
902 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700903 }
904 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100905 // Based on spill masks from QuickMethodFrameInfo so placed
906 // after it is dumped, but useful for understanding quick
907 // code, so dumped here.
908 ScopedIndentation indent2(vios);
909 DumpVregLocations(vios->Stream(), oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700910 }
911 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100912 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700913 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
914 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100915 ScopedIndentation indent2(vios);
916 vios->Stream() << StringPrintf("WARNING: "
917 "code size offset 0x%08x is past end of file 0x%08zx.",
918 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700919 success = false;
920 } else {
921 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700922 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
923 uint64_t aligned_code_end = aligned_code_begin + code_size;
924
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800925 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100926 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700927 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100928 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
929 code_offset,
930 code_size_offset,
931 code_size,
932 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700933
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100934 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700935 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100936 vios->Stream() << StringPrintf("WARNING: "
937 "start of code at 0x%08x is past end of file 0x%08zx.",
938 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700939 success = false;
940 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100941 vios->Stream() << StringPrintf(
942 "WARNING: "
943 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
944 "code size is 0x%08x loaded from offset 0x%08x.\n",
945 aligned_code_end, oat_file_.Size(),
946 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700947 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800948 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700949 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100950 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700951 }
952 }
953 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100954 vios->Stream() << StringPrintf(
955 "WARNING: "
956 "code size %d is bigger than max expected threshold of %d. "
957 "code size is 0x%08x loaded from offset 0x%08x.\n",
958 code_size, kMaxCodeSize,
959 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700960 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800961 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700962 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100963 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700964 }
965 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800966 } else if (options_.disassemble_code_) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100967 DumpCode(vios, oat_method, code_item, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700968 }
969 }
970 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100971 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700972 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700973 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800974
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800975 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
976 if (spill_mask == 0) {
977 return;
978 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800979 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800980 for (size_t i = 0; i < 32; i++) {
981 if ((spill_mask & (1 << i)) != 0) {
982 if (is_float) {
983 os << "fr" << i;
984 } else {
985 os << "r" << i;
986 }
987 spill_mask ^= 1 << i; // clear bit
988 if (spill_mask != 0) {
989 os << ", ";
990 } else {
991 break;
992 }
993 }
994 }
995 os << ")";
996 }
997
Roland Levillain442b46a2015-02-18 16:54:21 +0000998 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100999 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001000 const OatFile::OatMethod& oat_method,
1001 const DexFile::CodeItem* code_item) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001002 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1003 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001004 const void* raw_code_info = oat_method.GetVmapTable();
1005 if (raw_code_info != nullptr) {
1006 CodeInfo code_info(raw_code_info);
1007 DCHECK(code_item != nullptr);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001008 ScopedIndentation indent1(vios);
1009 DumpCodeInfo(vios, code_info, oat_method, *code_item);
Roland Levillain442b46a2015-02-18 16:54:21 +00001010 }
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001011 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
1012 // We don't encode the size in the table, so just emit that we have quickened
1013 // information.
1014 ScopedIndentation indent(vios);
1015 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001016 } else {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001017 // Otherwise, there is nothing to display.
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001018 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001019 }
1020
1021 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001022 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001023 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001024 const OatFile::OatMethod& oat_method,
Roland Levillain442b46a2015-02-18 16:54:21 +00001025 const DexFile::CodeItem& code_item) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001026 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001027 oat_method.GetCodeOffset(),
1028 code_item.registers_size_,
1029 options_.dump_code_info_stack_maps_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001030 }
1031
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001032 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1033 const DexFile::CodeItem* code_item) {
1034 if (code_item != nullptr) {
1035 size_t num_locals_ins = code_item->registers_size_;
1036 size_t num_ins = code_item->ins_size_;
1037 size_t num_locals = num_locals_ins - num_ins;
1038 size_t num_outs = code_item->outs_size_;
1039
1040 os << "vr_stack_locations:";
1041 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1042 // For readability, delimit the different kinds of VRs.
1043 if (reg == num_locals_ins) {
1044 os << "\n\tmethod*:";
1045 } else if (reg == num_locals && num_ins > 0) {
1046 os << "\n\tins:";
1047 } else if (reg == 0 && num_locals > 0) {
1048 os << "\n\tlocals:";
1049 }
1050
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001051 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1052 code_item,
1053 oat_method.GetCoreSpillMask(),
1054 oat_method.GetFpSpillMask(),
1055 oat_method.GetFrameSizeInBytes(),
1056 reg,
1057 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001058 os << " v" << reg << "[sp + #" << offset << "]";
1059 }
1060
1061 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1062 if (out_reg == 0) {
1063 os << "\n\touts:";
1064 }
1065
1066 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1067 os << " v" << out_reg << "[sp + #" << offset << "]";
1068 }
1069
1070 os << "\n";
1071 }
1072 }
1073
Ian Rogersb23a7722012-10-09 16:54:26 -07001074 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001075 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001076 size_t i = 0;
1077 while (i < code_item->insns_size_in_code_units_) {
1078 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001079 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1080 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001081 i += instruction->SizeInCodeUnits();
1082 }
1083 }
1084 }
1085
Roland Levillainf2650d12015-05-28 14:53:28 +01001086 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1087 // the optimizing compiler?
1088 static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1089 const DexFile::CodeItem* code_item) {
1090 // If the native GC map is null and the Dex `code_item` is not
1091 // null, then this method has been compiled with the optimizing
1092 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001093 return oat_method.GetQuickCode() != nullptr &&
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001094 oat_method.GetVmapTable() != nullptr &&
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001095 code_item != nullptr;
1096 }
1097
1098 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1099 // the dextodex compiler?
1100 static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
1101 const DexFile::CodeItem* code_item) {
1102 // If the quick code is null, the Dex `code_item` is not
1103 // null, and the vmap table is not null, then this method has been compiled
1104 // with the dextodex compiler.
1105 return oat_method.GetQuickCode() == nullptr &&
1106 oat_method.GetVmapTable() != nullptr &&
1107 code_item != nullptr;
Roland Levillainf2650d12015-05-28 14:53:28 +01001108 }
1109
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001110 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001111 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001112 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001113 const DexFile* dex_file,
1114 const DexFile::ClassDef& class_def,
1115 const DexFile::CodeItem* code_item,
1116 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001117 if ((method_access_flags & kAccNative) == 0) {
1118 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001119 Runtime* const runtime = Runtime::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001120 Handle<mirror::DexCache> dex_cache(
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001121 hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file,
1122 runtime->GetLinearAlloc())));
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001123 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001124 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001125 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
1126 &class_def, code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001127 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001128
1129 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001130 }
1131
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001132 // The StackMapsHelper provides the stack maps in the native PC order.
1133 // For identical native PCs, the order from the CodeInfo is preserved.
1134 class StackMapsHelper {
1135 public:
1136 explicit StackMapsHelper(const uint8_t* raw_code_info)
1137 : code_info_(raw_code_info),
1138 encoding_(code_info_.ExtractEncoding()),
1139 number_of_stack_maps_(code_info_.GetNumberOfStackMaps(encoding_)),
1140 indexes_(),
1141 offset_(static_cast<size_t>(-1)),
1142 stack_map_index_(0u) {
1143 if (number_of_stack_maps_ != 0u) {
1144 // Check if native PCs are ordered.
1145 bool ordered = true;
1146 StackMap last = code_info_.GetStackMapAt(0u, encoding_);
1147 for (size_t i = 1; i != number_of_stack_maps_; ++i) {
1148 StackMap current = code_info_.GetStackMapAt(i, encoding_);
1149 if (last.GetNativePcOffset(encoding_.stack_map_encoding) >
1150 current.GetNativePcOffset(encoding_.stack_map_encoding)) {
1151 ordered = false;
1152 break;
1153 }
1154 last = current;
1155 }
1156 if (!ordered) {
1157 // Create indirection indexes for access in native PC order. We do not optimize
1158 // for the fact that there can currently be only two separately ordered ranges,
1159 // namely normal stack maps and catch-point stack maps.
1160 indexes_.resize(number_of_stack_maps_);
1161 std::iota(indexes_.begin(), indexes_.end(), 0u);
1162 std::sort(indexes_.begin(),
1163 indexes_.end(),
1164 [this](size_t lhs, size_t rhs) {
1165 StackMap left = code_info_.GetStackMapAt(lhs, encoding_);
1166 uint32_t left_pc = left.GetNativePcOffset(encoding_.stack_map_encoding);
1167 StackMap right = code_info_.GetStackMapAt(rhs, encoding_);
1168 uint32_t right_pc = right.GetNativePcOffset(encoding_.stack_map_encoding);
1169 // If the PCs are the same, compare indexes to preserve the original order.
1170 return (left_pc < right_pc) || (left_pc == right_pc && lhs < rhs);
1171 });
1172 }
1173 offset_ = GetStackMapAt(0).GetNativePcOffset(encoding_.stack_map_encoding);
1174 }
1175 }
1176
1177 const CodeInfo& GetCodeInfo() const {
1178 return code_info_;
1179 }
1180
1181 const CodeInfoEncoding& GetEncoding() const {
1182 return encoding_;
1183 }
1184
1185 size_t GetOffset() const {
1186 return offset_;
1187 }
1188
1189 StackMap GetStackMap() const {
1190 return GetStackMapAt(stack_map_index_);
1191 }
1192
1193 void Next() {
1194 ++stack_map_index_;
1195 offset_ = (stack_map_index_ == number_of_stack_maps_)
1196 ? static_cast<size_t>(-1)
1197 : GetStackMapAt(stack_map_index_).GetNativePcOffset(encoding_.stack_map_encoding);
1198 }
1199
1200 private:
1201 StackMap GetStackMapAt(size_t i) const {
1202 if (!indexes_.empty()) {
1203 i = indexes_[i];
1204 }
1205 DCHECK_LT(i, number_of_stack_maps_);
1206 return code_info_.GetStackMapAt(i, encoding_);
1207 }
1208
1209 const CodeInfo code_info_;
1210 const CodeInfoEncoding encoding_;
1211 const size_t number_of_stack_maps_;
1212 dchecked_vector<size_t> indexes_; // Used if stack map native PCs are not ordered.
1213 size_t offset_;
1214 size_t stack_map_index_;
1215 };
1216
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001217 void DumpCode(VariableIndentationOutputStream* vios,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001218 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1219 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001220 const void* quick_code = oat_method.GetQuickCode();
1221
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001222 if (code_size == 0) {
1223 code_size = oat_method.GetQuickCodeSize();
1224 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001225 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001226 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001227 return;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001228 } else if (!bad_input && IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1229 // The optimizing compiler outputs its CodeInfo data in the vmap table.
1230 StackMapsHelper helper(oat_method.GetVmapTable());
1231 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1232 size_t offset = 0;
1233 while (offset < code_size) {
1234 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
1235 if (offset == helper.GetOffset()) {
1236 ScopedIndentation indent1(vios);
1237 StackMap stack_map = helper.GetStackMap();
1238 DCHECK(stack_map.IsValid());
1239 stack_map.Dump(vios,
1240 helper.GetCodeInfo(),
1241 helper.GetEncoding(),
1242 oat_method.GetCodeOffset(),
1243 code_item->registers_size_);
1244 do {
1245 helper.Next();
1246 // There may be multiple stack maps at a given PC. We display only the first one.
1247 } while (offset == helper.GetOffset());
1248 }
1249 DCHECK_LT(offset, helper.GetOffset());
1250 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001251 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001252 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1253 size_t offset = 0;
1254 while (offset < code_size) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001255 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001256 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001257 }
1258 }
1259
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001260 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001261 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001262 const OatDumperOptions& options_;
1263 uint32_t resolved_addr2instr_;
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001264 InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001265 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001266 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001267};
1268
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001269class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001270 public:
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001271 ImageDumper(std::ostream* os,
1272 gc::space::ImageSpace& image_space,
1273 const ImageHeader& image_header,
1274 OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001275 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001276 vios_(os),
1277 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001278 image_space_(image_space),
1279 image_header_(image_header),
1280 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001281
Mathieu Chartier90443472015-07-16 20:32:27 -07001282 bool Dump() SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001283 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001284 std::ostream& indent_os = vios_.Stream();
1285
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001286 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001287
Jeff Haodcdc85b2015-12-04 14:06:18 -08001288 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1289
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001290 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001291
Mathieu Chartiere401d142015-04-22 13:56:20 -07001292 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1293
1294 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1295 auto section = static_cast<ImageHeader::ImageSections>(i);
1296 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1297 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001298
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001299 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001300
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001301 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001302
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001303 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1304
1305 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1306
1307 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001308
Alex Lighta59dd802014-07-02 16:28:08 -07001309 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1310
Igor Murashkin46774762014-10-22 11:37:02 -07001311 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1312
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001313 {
1314 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001315 static_assert(arraysize(image_roots_descriptions_) ==
1316 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001317 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1318 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1319 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001320 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001321 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001322 if (image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001323 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001324 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001325 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001326 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1327 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001328 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001329 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1330 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001331 run++;
1332 } else {
1333 break;
1334 }
1335 }
1336 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001337 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001338 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001339 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001340 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001341 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001342 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001343 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001344 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001345 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001346 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001347 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001348 }
1349 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001350 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001351
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001352 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001353 os << "METHOD ROOTS\n";
1354 static_assert(arraysize(image_methods_descriptions_) ==
1355 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1356 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1357 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1358 const char* description = image_methods_descriptions_[i];
1359 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001360 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001361 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001362 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001363 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001364
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001365 Runtime* const runtime = Runtime::Current();
1366 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001367 std::string image_filename = image_space_.GetImageFilename();
1368 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001369 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001370 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001371 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001372 const OatFile* oat_file = image_space_.GetOatFile();
Alex Lighta59dd802014-07-02 16:28:08 -07001373 if (oat_file == nullptr) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001374 oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location);
1375 }
1376 if (oat_file == nullptr) {
1377 oat_file = OatFile::Open(oat_location,
1378 oat_location,
1379 nullptr,
1380 nullptr,
1381 false,
1382 /*low_4gb*/false,
1383 nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001384 &error_msg);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001385 }
1386 if (oat_file == nullptr) {
1387 os << "OAT FILE NOT FOUND: " << error_msg << "\n";
1388 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001389 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001390 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001391
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001392 stats_.oat_file_bytes = oat_file->Size();
1393
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001394 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001395
Mathieu Chartier02e25112013-08-14 16:14:24 -07001396 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001397 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001398 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1399 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001400 }
1401
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001402 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001403
Jeff Haodcdc85b2015-12-04 14:06:18 -08001404 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001405 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001406 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001407 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001408 {
1409 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1410 heap->FlushAllocStack();
1411 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001412 // Since FlushAllocStack() above resets the (active) allocation
1413 // stack. Need to revoke the thread-local allocation stacks that
1414 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001415 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001416 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001417 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001418 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001419 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001420 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001421 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001422 {
1423 ReaderMutexLock mu(self, *class_linker->DexLock());
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001424 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001425 mirror::DexCache* dex_cache =
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001426 down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001427 if (dex_cache != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01001428 dex_caches_.insert(dex_cache);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001429 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001430 }
1431 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001432 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001433 // Dump the normal objects before ArtMethods.
1434 image_space_.GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1435 indent_os << "\n";
1436 // TODO: Dump fields.
1437 // Dump methods after.
Jeff Haodcdc85b2015-12-04 14:06:18 -08001438 DumpArtMethodVisitor visitor(this);
Mathieu Chartiere42888f2016-04-14 10:49:19 -07001439 image_header_.VisitPackedArtMethods(&visitor,
1440 image_space_.Begin(),
1441 image_header_.GetPointerSize());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001442 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001443 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001444 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001445 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001446 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001447 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001448 size_t data_size = image_header_.GetDataSize(); // stored size in file.
1449 if (file == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001450 LOG(WARNING) << "Failed to find image in " << image_filename;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001451 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001452 stats_.file_bytes = file->GetLength();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001453 // If the image is compressed, adjust to decompressed size.
1454 size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader);
1455 if (image_header_.GetStorageMode() == ImageHeader::kStorageModeUncompressed) {
1456 DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image";
1457 }
1458 stats_.file_bytes += uncompressed_size - data_size;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001459 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001460 size_t header_bytes = sizeof(ImageHeader);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001461 const auto& object_section = image_header_.GetImageSection(ImageHeader::kSectionObjects);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001462 const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1463 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001464 const auto& dex_cache_arrays_section = image_header_.GetImageSection(
1465 ImageHeader::kSectionDexCacheArrays);
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001466 const auto& intern_section = image_header_.GetImageSection(
1467 ImageHeader::kSectionInternedStrings);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001468 const auto& class_table_section = image_header_.GetImageSection(
1469 ImageHeader::kSectionClassTable);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001470 const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1471
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001472 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001473
1474 // Objects are kObjectAlignment-aligned.
1475 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1476 if (object_section.Offset() > header_bytes) {
1477 stats_.alignment_bytes += object_section.Offset() - header_bytes;
1478 }
1479
1480 // Field section is 4-byte aligned.
1481 constexpr size_t kFieldSectionAlignment = 4U;
1482 uint32_t end_objects = object_section.Offset() + object_section.Size();
1483 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
1484 stats_.alignment_bytes += field_section.Offset() - end_objects;
1485
1486 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
1487 uint32_t end_fields = field_section.Offset() + field_section.Size();
1488 CHECK_ALIGNED(method_section.Offset(), 4);
1489 stats_.alignment_bytes += method_section.Offset() - end_fields;
1490
1491 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
1492 uint32_t end_methods = method_section.Offset() + method_section.Size();
1493 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
1494 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
1495
1496 // Intern table is 8-byte aligned.
1497 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
1498 CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
1499 stats_.alignment_bytes += intern_section.Offset() - end_caches;
1500
1501 // Add space between intern table and class table.
1502 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
1503 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
1504
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001505 // Add space between end of image data and bitmap. Expect the bitmap to be page-aligned.
1506 const size_t bitmap_offset = sizeof(ImageHeader) + data_size;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001507 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001508 stats_.alignment_bytes += RoundUp(bitmap_offset, kPageSize) - bitmap_offset;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001509
Mathieu Chartiere401d142015-04-22 13:56:20 -07001510 stats_.bitmap_bytes += bitmap_section.Size();
1511 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01001512 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01001513 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001514 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001515 stats_.class_table_bytes += class_table_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001516 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001517 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001518
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001519 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001520
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001521 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001522 }
1523
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001524 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001525 class DumpArtMethodVisitor : public ArtMethodVisitor {
1526 public:
1527 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
1528
1529 virtual void Visit(ArtMethod* method) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
1530 std::ostream& indent_os = image_dumper_->vios_.Stream();
1531 indent_os << method << " " << " ArtMethod: " << PrettyMethod(method) << "\n";
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001532 image_dumper_->DumpMethod(method, indent_os);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001533 indent_os << "\n";
1534 }
1535
1536 private:
1537 ImageDumper* const image_dumper_;
1538 };
1539
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001540 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Mathieu Chartier90443472015-07-16 20:32:27 -07001541 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001542 CHECK(type != nullptr);
1543 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001544 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001545 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001546 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001547 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001548 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001549 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001550 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001551 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001552 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001553 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001554 }
1555 }
1556
Mathieu Chartierc7853442015-03-27 14:35:38 -07001557 static void PrintField(std::ostream& os, ArtField* field, mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -07001558 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001559 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08001560 switch (field->GetTypeAsPrimitiveType()) {
1561 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08001562 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001563 break;
1564 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001565 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001566 break;
1567 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001568 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001569 break;
1570 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001571 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001572 break;
1573 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07001574 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001575 break;
1576 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07001577 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001578 break;
1579 case Primitive::kPrimBoolean:
Fred Shih37f05ef2014-07-16 18:38:08 -07001580 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1581 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001582 break;
1583 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07001584 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001585 break;
1586 case Primitive::kPrimNot: {
1587 // Get the value, don't compute the type unless it is non-null as we don't want
1588 // to cause class loading.
1589 mirror::Object* value = field->GetObj(obj);
1590 if (value == nullptr) {
1591 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07001592 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08001593 // Grab the field type without causing resolution.
Mathieu Chartierdaaf3262015-03-24 13:30:28 -07001594 mirror::Class* field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08001595 if (field_type != nullptr) {
1596 PrettyObjectValue(os, field_type, value);
1597 } else {
1598 os << StringPrintf("%p %s\n", value,
1599 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
1600 }
Ian Rogers50239c72013-06-17 14:53:22 -07001601 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001602 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07001603 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001604 default:
1605 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
1606 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08001607 }
1608 }
1609
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001610 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Mathieu Chartier90443472015-07-16 20:32:27 -07001611 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001612 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001613 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001614 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001615 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001616 for (ArtField& field : klass->GetIFields()) {
1617 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001618 }
1619 }
1620
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001621 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001622 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001623 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001624
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001625 const void* GetQuickOatCodeBegin(ArtMethod* m) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001626 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001627 image_header_.GetPointerSize());
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001628 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001629 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001630 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001631 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001632 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001633 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001634 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001635 }
1636
Mathieu Chartiere401d142015-04-22 13:56:20 -07001637 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001638 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001639 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1640 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001641 return 0;
1642 }
1643 return oat_code_begin[-1];
1644 }
1645
Mathieu Chartiere401d142015-04-22 13:56:20 -07001646 const void* GetQuickOatCodeEnd(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001647 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001648 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001649 if (oat_code_begin == nullptr) {
1650 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001651 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001652 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001653 }
1654
Mathieu Chartier90443472015-07-16 20:32:27 -07001655 static void Callback(mirror::Object* obj, void* arg) SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001656 DCHECK(obj != nullptr);
1657 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001658 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001659 if (!state->InDumpSpace(obj)) {
1660 return;
1661 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001662
1663 size_t object_bytes = obj->SizeOf();
1664 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1665 state->stats_.object_bytes += object_bytes;
1666 state->stats_.alignment_bytes += alignment_bytes;
1667
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001668 std::ostream& os = state->vios_.Stream();
1669
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001670 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001671 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001672 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
1673 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001674 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001675 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001676 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
1677 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08001678 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001679 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001680 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001681 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001682 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001683 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001684 ScopedIndentation indent1(&state->vios_);
1685 DumpFields(os, obj, obj_class);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001686 const size_t image_pointer_size = state->image_header_.GetPointerSize();
Ian Rogersd5b32602012-02-26 16:40:04 -08001687 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001688 auto* obj_array = obj->AsObjectArray<mirror::Object>();
1689 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001690 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001691 size_t run = 0;
1692 for (int32_t j = i + 1; j < length; j++) {
1693 if (value == obj_array->Get(j)) {
1694 run++;
1695 } else {
1696 break;
1697 }
1698 }
1699 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001700 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001701 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001702 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001703 i = i + run;
1704 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001705 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001706 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001707 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001708 }
1709 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001710 mirror::Class* klass = obj->AsClass();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001711 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001712 os << "STATICS:\n";
1713 ScopedIndentation indent2(&state->vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001714 for (ArtField& field : klass->GetSFields()) {
1715 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001716 }
1717 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001718 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01001719 auto it = state->dex_caches_.find(obj);
1720 if (it != state->dex_caches_.end()) {
1721 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001722 const auto& field_section = state->image_header_.GetImageSection(
1723 ImageHeader::kSectionArtFields);
1724 const auto& method_section = state->image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001725 size_t num_methods = dex_cache->NumResolvedMethods();
1726 if (num_methods != 0u) {
1727 os << "Methods (size=" << num_methods << "):";
1728 ScopedIndentation indent2(&state->vios_);
1729 auto* resolved_methods = dex_cache->GetResolvedMethods();
1730 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001731 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_methods,
1732 i,
1733 image_pointer_size);
Vladimir Marko05792b92015-08-03 11:56:49 +01001734 size_t run = 0;
1735 for (size_t j = i + 1;
1736 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_methods,
1737 j,
1738 image_pointer_size);
1739 ++j, ++run) {}
1740 if (run == 0) {
1741 os << StringPrintf("%zd: ", i);
1742 } else {
1743 os << StringPrintf("%zd to %zd: ", i, i + run);
1744 i = i + run;
1745 }
1746 std::string msg;
1747 if (elem == nullptr) {
1748 msg = "null";
1749 } else if (method_section.Contains(
1750 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
1751 msg = PrettyMethod(reinterpret_cast<ArtMethod*>(elem));
1752 } else {
1753 msg = "<not in method section>";
1754 }
1755 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07001756 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001757 }
1758 size_t num_fields = dex_cache->NumResolvedFields();
1759 if (num_fields != 0u) {
1760 os << "Fields (size=" << num_fields << "):";
1761 ScopedIndentation indent2(&state->vios_);
1762 auto* resolved_fields = dex_cache->GetResolvedFields();
1763 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
1764 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_fields, i, image_pointer_size);
1765 size_t run = 0;
1766 for (size_t j = i + 1;
1767 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_fields,
1768 j,
1769 image_pointer_size);
1770 ++j, ++run) {}
1771 if (run == 0) {
1772 os << StringPrintf("%zd: ", i);
1773 } else {
1774 os << StringPrintf("%zd to %zd: ", i, i + run);
1775 i = i + run;
1776 }
1777 std::string msg;
1778 if (elem == nullptr) {
1779 msg = "null";
1780 } else if (field_section.Contains(
1781 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
1782 msg = PrettyField(reinterpret_cast<ArtField*>(elem));
1783 } else {
1784 msg = "<not in field section>";
1785 }
1786 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07001787 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001788 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001789 }
1790 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07001791 std::string temp;
1792 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001793 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001794
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001795 void DumpMethod(ArtMethod* method, std::ostream& indent_os)
Mathieu Chartier90443472015-07-16 20:32:27 -07001796 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001797 DCHECK(method != nullptr);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001798 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
1799 const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
Mathieu Chartiere42888f2016-04-14 10:49:19 -07001800 const size_t pointer_size = image_header_.GetPointerSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01001801 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
1802 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07001803 if (method->IsNative()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001804 bool first_occurrence;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001805 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
1806 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001807 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001808 stats_.native_to_managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001809 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001810 if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
1811 image_header_.GetPointerSize())) {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01001812 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001813 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -07001814 } else if (method->IsAbstract() || method->IsClassInitializer()) {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00001815 // Don't print information for these.
Mathieu Chartiere42888f2016-04-14 10:49:19 -07001816 } else if (method->IsRuntimeMethod()) {
1817 ImtConflictTable* table = method->GetImtConflictTable(image_header_.GetPointerSize());
1818 if (table != nullptr) {
1819 indent_os << "IMT conflict table " << table << " method: ";
1820 for (size_t i = 0, count = table->NumEntries(pointer_size); i < count; ++i) {
1821 indent_os << PrettyMethod(table->GetImplementationMethod(i, pointer_size)) << " ";
1822 }
1823 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001824 } else {
1825 const DexFile::CodeItem* code_item = method->GetCodeItem();
1826 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001827 stats_.dex_instruction_bytes += dex_instruction_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001828
1829 bool first_occurrence;
Roland Levillain6d7f1792015-07-02 10:59:15 +01001830 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01001831 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01001832 // Method compiled with the optimizing compiler have no vmap table.
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001833 vmap_table_bytes = ComputeOatSize(method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01001834 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001835 stats_.vmap_table_bytes += vmap_table_bytes;
Roland Levillain6d7f1792015-07-02 10:59:15 +01001836 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001837 }
1838
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001839 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
1840 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001841 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001842 stats_.managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001843 if (method->IsConstructor()) {
1844 if (method->IsStatic()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001845 stats_.class_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001846 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001847 stats_.large_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001848 }
1849 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001850 stats_.large_method_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001851 }
1852 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001853 stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001854
Igor Murashkin7617abd2015-07-10 18:27:47 -07001855 uint32_t method_access_flags = method->GetAccessFlags();
1856
Mathieu Chartiere401d142015-04-22 13:56:20 -07001857 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001858 indent_os << StringPrintf("SIZE: Dex Instructions=%zd StackMaps=%zd AccessFlags=0x%x\n",
1859 dex_instruction_bytes,
1860 vmap_table_bytes,
Igor Murashkin7617abd2015-07-10 18:27:47 -07001861 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001862
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001863 size_t total_size = dex_instruction_bytes +
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001864 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
Mathieu Chartiere401d142015-04-22 13:56:20 -07001865
1866 double expansion =
1867 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001868 stats_.ComputeOutliers(total_size, expansion, method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001869 }
1870 }
1871
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001872 std::set<const void*> already_seen_;
1873 // Compute the size of the given data within the oat file and whether this is the first time
1874 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07001875 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001876 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001877 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001878 already_seen_.insert(oat_data);
1879 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001880 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001881 }
1882 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001883 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001884
1885 public:
1886 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001887 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001888 size_t file_bytes;
1889
1890 size_t header_bytes;
1891 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001892 size_t art_field_bytes;
1893 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01001894 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001895 size_t interned_strings_bytes;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001896 size_t class_table_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07001897 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001898 size_t alignment_bytes;
1899
1900 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001901 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001902 size_t managed_to_native_code_bytes;
1903 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001904 size_t class_initializer_code_bytes;
1905 size_t large_initializer_code_bytes;
1906 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001907
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001908 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001909
1910 size_t dex_instruction_bytes;
1911
Mathieu Chartiere401d142015-04-22 13:56:20 -07001912 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001913 std::vector<size_t> method_outlier_size;
1914 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07001915 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001916
Roland Levillain3887c462015-08-12 18:15:42 +01001917 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001918 : oat_file_bytes(0),
1919 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001920 header_bytes(0),
1921 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07001922 art_field_bytes(0),
1923 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01001924 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001925 interned_strings_bytes(0),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001926 class_table_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07001927 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001928 alignment_bytes(0),
1929 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001930 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001931 managed_to_native_code_bytes(0),
1932 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07001933 class_initializer_code_bytes(0),
1934 large_initializer_code_bytes(0),
1935 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001936 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001937 dex_instruction_bytes(0) {}
1938
Elliott Hughesa0e18062012-04-13 15:59:59 -07001939 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001940 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07001941 size_t bytes;
1942 size_t count;
1943 };
1944 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
1945 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001946
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001947 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001948 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
1949 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001950 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07001951 it->second.count += 1;
1952 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001953 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07001954 }
1955 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001956
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001957 double PercentOfOatBytes(size_t size) {
1958 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
1959 }
1960
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001961 double PercentOfFileBytes(size_t size) {
1962 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
1963 }
1964
1965 double PercentOfObjectBytes(size_t size) {
1966 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
1967 }
1968
Mathieu Chartiere401d142015-04-22 13:56:20 -07001969 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001970 method_outlier_size.push_back(total_size);
1971 method_outlier_expansion.push_back(expansion);
1972 method_outlier.push_back(method);
1973 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001974
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001975 void DumpOutliers(std::ostream& os)
Mathieu Chartier90443472015-07-16 20:32:27 -07001976 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001977 size_t sum_of_sizes = 0;
1978 size_t sum_of_sizes_squared = 0;
1979 size_t sum_of_expansion = 0;
1980 size_t sum_of_expansion_squared = 0;
1981 size_t n = method_outlier_size.size();
Jeff Haodcdc85b2015-12-04 14:06:18 -08001982 if (n == 0) {
1983 return;
1984 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001985 for (size_t i = 0; i < n; i++) {
1986 size_t cur_size = method_outlier_size[i];
1987 sum_of_sizes += cur_size;
1988 sum_of_sizes_squared += cur_size * cur_size;
1989 double cur_expansion = method_outlier_expansion[i];
1990 sum_of_expansion += cur_expansion;
1991 sum_of_expansion_squared += cur_expansion * cur_expansion;
1992 }
1993 size_t size_mean = sum_of_sizes / n;
1994 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
1995 double expansion_mean = sum_of_expansion / n;
1996 double expansion_variance =
1997 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
1998
1999 // Dump methods whose size is a certain number of standard deviations from the mean
2000 size_t dumped_values = 0;
2001 size_t skipped_values = 0;
2002 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2003 size_t cur_size_variance = i * i * size_variance;
2004 bool first = true;
2005 for (size_t j = 0; j < n; j++) {
2006 size_t cur_size = method_outlier_size[j];
2007 if (cur_size > size_mean) {
2008 size_t cur_var = cur_size - size_mean;
2009 cur_var = cur_var * cur_var;
2010 if (cur_var > cur_size_variance) {
2011 if (dumped_values > 20) {
2012 if (i == 1) {
2013 skipped_values++;
2014 } else {
2015 i = 2; // jump to counting for 1 standard deviation
2016 break;
2017 }
2018 } else {
2019 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002020 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002021 first = false;
2022 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002023 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002024 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002025 method_outlier_size[j] = 0; // don't consider this method again
2026 dumped_values++;
2027 }
2028 }
2029 }
2030 }
2031 }
2032 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002033 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002034 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002035 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002036 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002037
2038 // Dump methods whose expansion is a certain number of standard deviations from the mean
2039 dumped_values = 0;
2040 skipped_values = 0;
2041 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2042 double cur_expansion_variance = i * i * expansion_variance;
2043 bool first = true;
2044 for (size_t j = 0; j < n; j++) {
2045 double cur_expansion = method_outlier_expansion[j];
2046 if (cur_expansion > expansion_mean) {
2047 size_t cur_var = cur_expansion - expansion_mean;
2048 cur_var = cur_var * cur_var;
2049 if (cur_var > cur_expansion_variance) {
2050 if (dumped_values > 20) {
2051 if (i == 1) {
2052 skipped_values++;
2053 } else {
2054 i = 2; // jump to counting for 1 standard deviation
2055 break;
2056 }
2057 } else {
2058 if (first) {
2059 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002060 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002061 first = false;
2062 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002063 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002064 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002065 method_outlier_expansion[j] = 0.0; // don't consider this method again
2066 dumped_values++;
2067 }
2068 }
2069 }
2070 }
2071 }
2072 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002073 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002074 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002075 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002076 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002077 }
2078
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002079 void Dump(std::ostream& os, std::ostream& indent_os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002080 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002081 {
2082 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2083 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002084 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2085 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2086 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2087 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2088 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2089 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002090 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002091 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2092 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002093 header_bytes, PercentOfFileBytes(header_bytes),
2094 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002095 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2096 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002097 dex_cache_arrays_bytes,
2098 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002099 interned_strings_bytes,
2100 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002101 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002102 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002103 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2104 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002105 CHECK_EQ(file_bytes,
2106 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2107 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2108 bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002109 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002110
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002111 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002112 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002113 for (const auto& sizes_and_count : sizes_and_counts) {
2114 const std::string& descriptor(sizes_and_count.first);
2115 double average = static_cast<double>(sizes_and_count.second.bytes) /
2116 static_cast<double>(sizes_and_count.second.count);
2117 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002118 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002119 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002120 descriptor.c_str(), sizes_and_count.second.bytes,
2121 sizes_and_count.second.count, average, percent);
2122 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002123 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002124 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002125 CHECK_EQ(object_bytes, object_bytes_total);
2126
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002127 os << StringPrintf("oat_file_bytes = %8zd\n"
2128 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2129 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2130 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2131 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2132 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2133 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002134 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002135 managed_code_bytes,
2136 PercentOfOatBytes(managed_code_bytes),
2137 managed_to_native_code_bytes,
2138 PercentOfOatBytes(managed_to_native_code_bytes),
2139 native_to_managed_code_bytes,
2140 PercentOfOatBytes(native_to_managed_code_bytes),
2141 class_initializer_code_bytes,
2142 PercentOfOatBytes(class_initializer_code_bytes),
2143 large_initializer_code_bytes,
2144 PercentOfOatBytes(large_initializer_code_bytes),
2145 large_method_code_bytes,
2146 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002147 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002148 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002149 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002150 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2151 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002152 }
2153
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002154 os << "\n" << StringPrintf("vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002155 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002156 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002157
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002158 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2159 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002160 static_cast<double>(managed_code_bytes) /
2161 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002162 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002163 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002164 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002165
2166 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002167 }
2168 } stats_;
2169
2170 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002171 enum {
2172 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2173 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2174 kLargeConstructorDexBytes = 4000,
2175 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2176 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2177 kLargeMethodDexBytes = 16000
2178 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002179
2180 // For performance, use the *os_ directly for anything that doesn't need indentation
2181 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002182 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002183 VariableIndentationOutputStream vios_;
2184 ScopedIndentation indent1_;
2185
Ian Rogers1d54e732013-05-02 21:10:01 -07002186 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002187 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002188 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002189 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002190 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002191
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002192 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002193};
2194
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002195static int DumpImage(gc::space::ImageSpace* image_space,
2196 OatDumperOptions* options,
2197 std::ostream* os) SHARED_REQUIRES(Locks::mutator_lock_) {
2198 const ImageHeader& image_header = image_space->GetImageHeader();
2199 if (!image_header.IsValid()) {
2200 fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
2201 return EXIT_FAILURE;
2202 }
2203 ImageDumper image_dumper(os, *image_space, image_header, options);
2204 if (!image_dumper.Dump()) {
2205 return EXIT_FAILURE;
2206 }
2207 return EXIT_SUCCESS;
2208}
2209
2210static int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002211 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002212 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002213 options->class_loader_ = &null_class_loader;
2214
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002215 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002216 if (options->app_image_ != nullptr) {
2217 if (options->app_oat_ == nullptr) {
2218 LOG(ERROR) << "Can not dump app image without app oat file";
Jeff Haodcdc85b2015-12-04 14:06:18 -08002219 return EXIT_FAILURE;
2220 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002221 // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
2222 // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
2223 // pointers into 32 bit pointer sized ArtMethods.
2224 std::string error_msg;
2225 std::unique_ptr<OatFile> oat_file(OatFile::Open(options->app_oat_,
2226 options->app_oat_,
2227 nullptr,
2228 nullptr,
2229 false,
2230 /*low_4gb*/true,
2231 nullptr,
2232 &error_msg));
2233 if (oat_file == nullptr) {
2234 LOG(ERROR) << "Failed to open oat file " << options->app_oat_ << " with error " << error_msg;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002235 return EXIT_FAILURE;
2236 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002237 std::unique_ptr<gc::space::ImageSpace> space(
2238 gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg));
2239 if (space == nullptr) {
2240 LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error "
2241 << error_msg;
2242 }
2243 // Open dex files for the image.
2244 std::vector<std::unique_ptr<const DexFile>> dex_files;
2245 if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
2246 LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
2247 << error_msg;
2248 }
2249 // Dump the actual image.
2250 int result = DumpImage(space.get(), options, os);
2251 if (result != EXIT_SUCCESS) {
2252 return result;
2253 }
2254 // Fall through to dump the boot images.
2255 }
2256
2257 gc::Heap* heap = runtime->GetHeap();
2258 CHECK(heap->HasBootImageSpace()) << "No image spaces";
2259 for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
2260 int result = DumpImage(image_space, options, os);
2261 if (result != EXIT_SUCCESS) {
2262 return result;
2263 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002264 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002265 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002266}
2267
Andreas Gampe00b25f32014-09-17 21:49:05 -07002268static int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
2269 std::ostream* os) {
2270 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2271
2272 Thread* self = Thread::Current();
2273 CHECK(self != nullptr);
2274 // Need well-known-classes.
2275 WellKnownClasses::Init(self->GetJniEnv());
2276
2277 // Need to register dex files to get a working dex cache.
2278 ScopedObjectAccess soa(self);
2279 ClassLinker* class_linker = runtime->GetClassLinker();
Mathieu Chartierd57d4542015-10-14 10:55:30 -07002280 runtime->GetOatFileManager().RegisterOatFile(std::unique_ptr<const OatFile>(oat_file));
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002281 std::vector<const DexFile*> class_path;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002282 for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
2283 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002284 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002285 CHECK(dex_file != nullptr) << error_msg;
Mathieu Chartierd57d4542015-10-14 10:55:30 -07002286 class_linker->RegisterDexFile(*dex_file, runtime->GetLinearAlloc());
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002287 class_path.push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002288 }
2289
2290 // Need a class loader.
Andreas Gampe00b25f32014-09-17 21:49:05 -07002291 // Fake that we're a compiler.
Mathieu Chartier966878d2016-01-14 14:33:29 -08002292 jobject class_loader = class_linker->CreatePathClassLoader(self, class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002293
2294 // Use the class loader while dumping.
2295 StackHandleScope<1> scope(self);
2296 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
2297 soa.Decode<mirror::ClassLoader*>(class_loader));
2298 options->class_loader_ = &loader_handle;
2299
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002300 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002301 bool success = oat_dumper.Dump(*os);
2302 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2303}
2304
2305static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002306 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002307 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002308 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002309 options->class_loader_ = &null_class_loader;
2310
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002311 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002312 bool success = oat_dumper.Dump(*os);
2313 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2314}
2315
2316static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2317 std::ostream* os) {
2318 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002319 OatFile* oat_file = OatFile::Open(oat_filename,
2320 oat_filename,
2321 nullptr,
2322 nullptr,
2323 false,
2324 /*low_4gb*/false,
2325 nullptr,
2326 &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002327 if (oat_file == nullptr) {
2328 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2329 return EXIT_FAILURE;
2330 }
2331
2332 if (runtime != nullptr) {
2333 return DumpOatWithRuntime(runtime, oat_file, options, os);
2334 } else {
2335 return DumpOatWithoutRuntime(oat_file, options, os);
2336 }
2337}
2338
David Srbecky2fdd03c2016-03-10 15:32:37 +00002339static int SymbolizeOat(const char* oat_filename, std::string& output_name, bool no_bits) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002340 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002341 OatFile* oat_file = OatFile::Open(oat_filename,
2342 oat_filename,
2343 nullptr,
2344 nullptr,
2345 false,
2346 /*low_4gb*/false,
2347 nullptr,
2348 &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002349 if (oat_file == nullptr) {
2350 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2351 return EXIT_FAILURE;
2352 }
2353
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002354 bool result;
2355 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
2356 // files for 64-bit code in the past.
2357 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002358 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file, output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002359 result = oat_symbolizer.Symbolize();
2360 } else {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002361 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file, output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002362 result = oat_symbolizer.Symbolize();
2363 }
2364 if (!result) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002365 fprintf(stderr, "Failed to symbolize\n");
2366 return EXIT_FAILURE;
2367 }
2368
2369 return EXIT_SUCCESS;
2370}
2371
Igor Murashkin37743352014-11-13 14:38:00 -08002372struct OatdumpArgs : public CmdlineArgs {
2373 protected:
2374 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002375
Igor Murashkin37743352014-11-13 14:38:00 -08002376 virtual ParseStatus ParseCustom(const StringPiece& option,
2377 std::string* error_msg) OVERRIDE {
2378 {
2379 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
2380 if (base_parse != kParseUnknownArgument) {
2381 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002382 }
2383 }
2384
Igor Murashkin37743352014-11-13 14:38:00 -08002385 if (option.starts_with("--oat-file=")) {
2386 oat_filename_ = option.substr(strlen("--oat-file=")).data();
2387 } else if (option.starts_with("--image=")) {
2388 image_location_ = option.substr(strlen("--image=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002389 } else if (option == "--no-dump:vmap") {
2390 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01002391 } else if (option =="--dump:code_info_stack_maps") {
2392 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002393 } else if (option == "--no-disassemble") {
2394 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00002395 } else if (option =="--header-only") {
2396 dump_header_only_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002397 } else if (option.starts_with("--symbolize=")) {
2398 oat_filename_ = option.substr(strlen("--symbolize=")).data();
2399 symbolize_ = true;
David Srbecky2fdd03c2016-03-10 15:32:37 +00002400 } else if (option.starts_with("--only-keep-debug")) {
2401 only_keep_debug_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002402 } else if (option.starts_with("--class-filter=")) {
2403 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002404 } else if (option.starts_with("--method-filter=")) {
2405 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002406 } else if (option.starts_with("--list-classes")) {
2407 list_classes_ = true;
2408 } else if (option.starts_with("--list-methods")) {
2409 list_methods_ = true;
2410 } else if (option.starts_with("--export-dex-to=")) {
2411 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
2412 } else if (option.starts_with("--addr2instr=")) {
2413 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
2414 *error_msg = "Address conversion failed";
2415 return kParseError;
2416 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002417 } else if (option.starts_with("--app-image=")) {
2418 app_image_ = option.substr(strlen("--app-image=")).data();
2419 } else if (option.starts_with("--app-oat=")) {
2420 app_oat_ = option.substr(strlen("--app-oat=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002421 } else {
2422 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002423 }
2424
Igor Murashkin37743352014-11-13 14:38:00 -08002425 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002426 }
2427
Igor Murashkin37743352014-11-13 14:38:00 -08002428 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
2429 // Infer boot image location from the image location if possible.
2430 if (boot_image_location_ == nullptr) {
2431 boot_image_location_ = image_location_;
2432 }
2433
2434 // Perform the parent checks.
2435 ParseStatus parent_checks = Base::ParseChecks(error_msg);
2436 if (parent_checks != kParseOk) {
2437 return parent_checks;
2438 }
2439
2440 // Perform our own checks.
2441 if (image_location_ == nullptr && oat_filename_ == nullptr) {
2442 *error_msg = "Either --image or --oat-file must be specified";
2443 return kParseError;
2444 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
2445 *error_msg = "Either --image or --oat-file must be specified but not both";
2446 return kParseError;
2447 }
2448
2449 return kParseOk;
2450 }
2451
2452 virtual std::string GetUsage() const {
2453 std::string usage;
2454
2455 usage +=
2456 "Usage: oatdump [options] ...\n"
2457 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
2458 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
2459 "\n"
2460 // Either oat-file or image is required.
2461 " --oat-file=<file.oat>: specifies an input oat filename.\n"
2462 " Example: --oat-file=/system/framework/boot.oat\n"
2463 "\n"
2464 " --image=<file.art>: specifies an input image location.\n"
2465 " Example: --image=/system/framework/boot.art\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002466 "\n"
2467 " --app-image=<file.art>: specifies an input app image. Must also have a specified\n"
2468 " boot image and app oat file.\n"
2469 " Example: --app-image=app.art\n"
2470 "\n"
2471 " --app-oat=<file.odex>: specifies an input app oat.\n"
2472 " Example: --app-oat=app.odex\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002473 "\n";
2474
2475 usage += Base::GetUsage();
2476
2477 usage += // Optional.
Igor Murashkin37743352014-11-13 14:38:00 -08002478 " --no-dump:vmap may be used to disable vmap dumping.\n"
2479 " Example: --no-dump:vmap\n"
2480 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01002481 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
2482 " Example: --dump:code_info_stack_maps\n"
2483 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002484 " --no-disassemble may be used to disable disassembly.\n"
2485 " Example: --no-disassemble\n"
2486 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00002487 " --header-only may be used to print only the oat header.\n"
2488 " Example: --header-only\n"
2489 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002490 " --list-classes may be used to list target file classes (can be used with filters).\n"
2491 " Example: --list-classes\n"
2492 " Example: --list-classes --class-filter=com.example.foo\n"
2493 "\n"
2494 " --list-methods may be used to list target file methods (can be used with filters).\n"
2495 " Example: --list-methods\n"
2496 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
2497 "\n"
2498 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
2499 " Example: --symbolize=/system/framework/boot.oat\n"
2500 "\n"
David Srbecky2fdd03c2016-03-10 15:32:37 +00002501 " --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
2502 " .rodata and .text sections are omitted in the output file to save space.\n"
2503 " Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
2504 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002505 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
2506 " Example: --class-filter=com.example.foo\n"
2507 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002508 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
2509 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002510 "\n"
2511 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
2512 " Example: --export-dex-to=/data/local/tmp\n"
2513 "\n"
2514 " --addr2instr=<address>: output matching method disassembled code from relative\n"
2515 " address (e.g. PC from crash dump)\n"
2516 " Example: --addr2instr=0x00001a3b\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002517 "\n";
2518
2519 return usage;
2520 }
2521
2522 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07002523 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002524 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00002525 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07002526 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002527 std::string elf_filename_prefix_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002528 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01002529 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002530 bool disassemble_code_ = true;
2531 bool symbolize_ = false;
David Srbecky2fdd03c2016-03-10 15:32:37 +00002532 bool only_keep_debug_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002533 bool list_classes_ = false;
2534 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00002535 bool dump_header_only_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002536 uint32_t addr2instr_ = 0;
2537 const char* export_dex_location_ = nullptr;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002538 const char* app_image_ = nullptr;
2539 const char* app_oat_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002540};
2541
Igor Murashkin37743352014-11-13 14:38:00 -08002542struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
2543 virtual bool NeedsRuntime() OVERRIDE {
2544 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002545
Igor Murashkin37743352014-11-13 14:38:00 -08002546 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
2547 bool absolute_addresses = (args_->oat_filename_ == nullptr);
2548
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002549 oat_dumper_options_.reset(new OatDumperOptions(
Igor Murashkin37743352014-11-13 14:38:00 -08002550 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01002551 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08002552 args_->disassemble_code_,
2553 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002554 args_->class_filter_,
2555 args_->method_filter_,
2556 args_->list_classes_,
2557 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00002558 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002559 args_->export_dex_location_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002560 args_->app_image_,
2561 args_->app_oat_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002562 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08002563
2564 return (args_->boot_image_location_ != nullptr || args_->image_location_ != nullptr) &&
2565 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002566 }
2567
Igor Murashkin37743352014-11-13 14:38:00 -08002568 virtual bool ExecuteWithoutRuntime() OVERRIDE {
2569 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08002570 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002571
Mathieu Chartierd424d082014-10-15 10:31:46 -07002572 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08002573
Andreas Gampec24f3992014-12-17 20:40:11 -08002574 if (args_->symbolize_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002575 // ELF has special kind of section called SHT_NOBITS which allows us to create
2576 // sections which exist but their data is omitted from the ELF file to save space.
2577 // This is what "strip --only-keep-debug" does when it creates separate ELF file
2578 // with only debug data. We use it in similar way to exclude .rodata and .text.
2579 bool no_bits = args_->only_keep_debug_;
2580 return SymbolizeOat(args_->oat_filename_, args_->output_name_, no_bits) == EXIT_SUCCESS;
Andreas Gampec24f3992014-12-17 20:40:11 -08002581 } else {
2582 return DumpOat(nullptr,
2583 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002584 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08002585 args_->os_) == EXIT_SUCCESS;
2586 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002587 }
2588
Igor Murashkin37743352014-11-13 14:38:00 -08002589 virtual bool ExecuteWithRuntime(Runtime* runtime) {
2590 CHECK(args_ != nullptr);
2591
2592 if (args_->oat_filename_ != nullptr) {
2593 return DumpOat(runtime,
2594 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002595 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08002596 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002597 }
Igor Murashkin37743352014-11-13 14:38:00 -08002598
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002599 return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002600 }
2601
Igor Murashkin37743352014-11-13 14:38:00 -08002602 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
2603};
Andreas Gampe00b25f32014-09-17 21:49:05 -07002604
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002605} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07002606
2607int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08002608 art::OatdumpMain main;
2609 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002610}