blob: 3f031a33184eb5f51bc7c2891d59fee6e642fde0 [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"
Christina Wadsworthbc233ac2016-06-20 15:01:32 -070038#include "dex_instruction-inl.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"
Mathieu Chartier8e2c5622016-06-21 15:14:20 -070059#include "stack_map.h"
Nicolas Geoffray9c055782016-07-14 09:24:30 +000060#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();
Nicolas Geoffray9c055782016-07-14 09:24:30 +0000581 std::set<uint32_t> string_ids;
Andreas Gampe2ba88952016-04-29 17:52:07 -0700582 uint32_t dex_offset = dchecked_integral_cast<uint32_t>(dex_file_pointer - oat_file_begin);
583 os << StringPrintf("dex-file: 0x%08x..0x%08x\n",
584 dex_offset,
585 dchecked_integral_cast<uint32_t>(dex_offset + oat_dex_file.FileSize() - 1));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700586
Andreas Gampe2ba88952016-04-29 17:52:07 -0700587 // Create the dex file early. A lot of print-out things depend on it.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700588 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700589 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
590 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700591 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700592 os << std::flush;
593 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700594 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100595
Andreas Gampe2ba88952016-04-29 17:52:07 -0700596 // Print lookup table, if it exists.
597 if (oat_dex_file.GetLookupTableData() != nullptr) {
598 uint32_t table_offset = dchecked_integral_cast<uint32_t>(
599 oat_dex_file.GetLookupTableData() - oat_file_begin);
600 uint32_t table_size = TypeLookupTable::RawDataLength(*dex_file);
601 os << StringPrintf("type-table: 0x%08x..0x%08x\n",
602 table_offset,
603 table_offset + table_size - 1);
604 }
605
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100606 VariableIndentationOutputStream vios(&os);
607 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800608 for (size_t class_def_index = 0;
609 class_def_index < dex_file->NumClassDefs();
610 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700611 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
612 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800613
614 // TODO: Support regex
615 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
616 continue;
617 }
618
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700619 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100620 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700621 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
622 class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100623 << " (" << oat_class.GetStatus() << ")"
624 << " (" << oat_class.GetType() << ")\n";
625 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Nicolas Geoffray9c055782016-07-14 09:24:30 +0000626 if (options_.list_classes_) continue;
627 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis, string_ids)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700628 success = false;
629 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800630 if (stop_analysis) {
631 os << std::flush;
632 return success;
633 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700634 }
Nicolas Geoffray9c055782016-07-14 09:24:30 +0000635 os << "Number of unique strings loaded from dex code: " << string_ids.size() << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700636 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700637 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700638 }
639
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800640 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
641 std::string error_msg;
642 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
643
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700644 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800645 if (dex_file == nullptr) {
646 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
647 return false;
648 }
649 size_t fsize = oat_dex_file.FileSize();
650
651 // Some quick checks just in case
652 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
653 os << "Invalid dex file\n";
654 return false;
655 }
656
657 // Verify output directory exists
658 if (!OS::DirectoryExists(options_.export_dex_location_)) {
659 // TODO: Extend OS::DirectoryExists if symlink support is required
660 os << options_.export_dex_location_ << " output directory not found or symlink\n";
661 return false;
662 }
663
664 // Beautify path names
665 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
666 return false;
667 }
668
669 std::string dex_orig_name;
670 size_t dex_orig_pos = dex_file_location.rfind('/');
671 if (dex_orig_pos == std::string::npos)
672 dex_orig_name = dex_file_location;
673 else
674 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
675
676 // A more elegant approach to efficiently name user installed apps is welcome
677 if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
678 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
679 size_t apk_orig_pos = dex_file_location.rfind('/');
680 if (apk_orig_pos != std::string::npos) {
681 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
682 }
683 }
684
685 std::string out_dex_path(options_.export_dex_location_);
686 if (out_dex_path.back() != '/') {
687 out_dex_path.append("/");
688 }
689 out_dex_path.append(dex_orig_name);
690 out_dex_path.append("_export.dex");
691 if (out_dex_path.length() > PATH_MAX) {
692 return false;
693 }
694
695 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
696 if (file.get() == nullptr) {
697 os << "Failed to open output dex file " << out_dex_path;
698 return false;
699 }
700
701 if (!file->WriteFully(dex_file->Begin(), fsize)) {
702 os << "Failed to write dex file";
703 file->Erase();
704 return false;
705 }
706
707 if (file->FlushCloseOrErase() != 0) {
708 os << "Flush and close failed";
709 return false;
710 }
711
712 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
713 os << std::flush;
714
715 return true;
716 }
717
Elliott Hughese3c845c2012-02-28 17:23:01 -0800718 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700719 while (it.HasNextStaticField()) {
720 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700721 }
Ian Rogers0571d352011-11-03 19:51:38 -0700722 while (it.HasNextInstanceField()) {
723 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700724 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800725 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700726
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100727 bool DumpOatClass(VariableIndentationOutputStream* vios,
728 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Nicolas Geoffray9c055782016-07-14 09:24:30 +0000729 const DexFile::ClassDef& class_def, bool* stop_analysis,
730 std::set<uint32_t>& string_ids) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700731 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800732 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -0700733 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700734 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100735 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700736 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800737 }
738 ClassDataItemIterator it(dex_file, class_data);
739 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700740 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700741 while (it.HasNextDirectMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100742 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700743 it.GetMemberIndex(), it.GetMethodCodeItem(),
Nicolas Geoffray9c055782016-07-14 09:24:30 +0000744 it.GetRawMemberAccessFlags(), &addr_found, string_ids)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700745 success = false;
746 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800747 if (addr_found) {
748 *stop_analysis = true;
749 return success;
750 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700751 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700752 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700753 }
Ian Rogers0571d352011-11-03 19:51:38 -0700754 while (it.HasNextVirtualMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100755 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700756 it.GetMemberIndex(), it.GetMethodCodeItem(),
Nicolas Geoffray9c055782016-07-14 09:24:30 +0000757 it.GetRawMemberAccessFlags(), &addr_found, string_ids)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700758 success = false;
759 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800760 if (addr_found) {
761 *stop_analysis = true;
762 return success;
763 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700764 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700765 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700766 }
Ian Rogers0571d352011-11-03 19:51:38 -0700767 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100768 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700769 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700770 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800771
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700772 static constexpr uint32_t kPrologueBytes = 16;
773
774 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
775 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
776
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100777 bool DumpOatMethod(VariableIndentationOutputStream* vios,
778 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700779 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700780 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800781 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Nicolas Geoffray9c055782016-07-14 09:24:30 +0000782 uint32_t method_access_flags, bool* addr_found,
783 std::set<uint32_t>& string_ids) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700784 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800785
Nicolas Geoffray9c055782016-07-14 09:24:30 +0000786 if (code_item != nullptr) {
787 const uint16_t* code_ptr = code_item->insns_;
788 const uint16_t* code_end = code_item->insns_ + code_item->insns_size_in_code_units_;
789
790 while (code_ptr < code_end) {
791 const Instruction* inst = Instruction::At(code_ptr);
792 switch (inst->Opcode()) {
793 case Instruction::CONST_STRING: {
794 uint32_t string_index = inst->VRegB_21c();
795 string_ids.insert(string_index);
796 break;
797 }
798 case Instruction::CONST_STRING_JUMBO: {
799 uint32_t string_index = inst->VRegB_31c();
800 string_ids.insert(string_index);
801 break;
802 }
803
804 default:
805 break;
806 }
807
808 code_ptr += inst->SizeInCodeUnits();
809 }
810 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800811 // TODO: Support regex
812 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
813 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000814 return success;
815 }
816
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800817 std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100818 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
819 class_method_index, pretty_method.c_str(),
820 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800821 if (options_.list_methods_) return success;
822
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800823 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
824 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
825 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
826 uint32_t code_offset = oat_method.GetCodeOffset();
827 uint32_t code_size = oat_method.GetQuickCodeSize();
828 if (resolved_addr2instr_ != 0) {
829 if (resolved_addr2instr_ > code_offset + code_size) {
830 return success;
831 } else {
832 *addr_found = true; // stop analyzing file at next iteration
833 }
834 }
835
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100836 // Everything below is indented at least once.
837 ScopedIndentation indent1(vios);
838
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800839 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100840 vios->Stream() << "DEX CODE:\n";
841 ScopedIndentation indent2(vios);
842 DumpDexCode(vios->Stream(), dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700843 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700844
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700845 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700846 std::unique_ptr<verifier::MethodVerifier> verifier;
847 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700848 // We need to have the handle scope stay live until after the verifier since the verifier has
849 // a handle to the dex cache from hs.
850 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100851 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
852 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700853 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100854 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700855 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -0700856 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800857 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100858 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800859 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100860 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100861 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100862 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700863 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100864 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700865 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
866 oat_method_offsets_offset, oat_file_.Size());
867 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100868 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700869 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800870 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700871
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100872 ScopedIndentation indent2(vios);
873 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700874 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
875 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100876 vios->Stream() << StringPrintf("WARNING: "
877 "code offset 0x%08x is past end of file 0x%08zx.\n",
878 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700879 success = false;
880 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100881 vios->Stream() << "\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800882 }
883 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100884 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700885 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
886 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700887
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800888 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100889 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700890 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100891 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700892 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100893 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700894 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
895 method_header_offset, oat_file_.Size());
896 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100897 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700898 return false;
899 }
900
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100901 ScopedIndentation indent2(vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100902 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800903 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100904 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700905 }
906 uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100907 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700908 if (vmap_table_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100909 vios->Stream() << StringPrintf("WARNING: "
910 "vmap table offset 0x%08x is past end of file 0x%08zx. "
911 "vmap table offset was loaded from offset 0x%08x.\n",
912 vmap_table_offset, oat_file_.Size(),
913 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700914 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800915 } else if (options_.dump_vmap_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100916 DumpVmapData(vios, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700917 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800918 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700919 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100920 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700921
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100922 ScopedIndentation indent2(vios);
923 vios->Stream()
924 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
925 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
926 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
927 vios->Stream() << "\n";
928 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
929 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
930 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700931 }
932 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100933 // Based on spill masks from QuickMethodFrameInfo so placed
934 // after it is dumped, but useful for understanding quick
935 // code, so dumped here.
936 ScopedIndentation indent2(vios);
937 DumpVregLocations(vios->Stream(), oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700938 }
939 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100940 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700941 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
942 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100943 ScopedIndentation indent2(vios);
944 vios->Stream() << StringPrintf("WARNING: "
945 "code size offset 0x%08x is past end of file 0x%08zx.",
946 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700947 success = false;
948 } else {
949 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700950 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
951 uint64_t aligned_code_end = aligned_code_begin + code_size;
952
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800953 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100954 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700955 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100956 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
957 code_offset,
958 code_size_offset,
959 code_size,
960 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700961
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100962 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700963 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100964 vios->Stream() << StringPrintf("WARNING: "
965 "start of code at 0x%08x is past end of file 0x%08zx.",
966 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700967 success = false;
968 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100969 vios->Stream() << StringPrintf(
970 "WARNING: "
971 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
972 "code size is 0x%08x loaded from offset 0x%08x.\n",
973 aligned_code_end, oat_file_.Size(),
974 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700975 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800976 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700977 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100978 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700979 }
980 }
981 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100982 vios->Stream() << StringPrintf(
983 "WARNING: "
984 "code size %d is bigger than max expected threshold of %d. "
985 "code size is 0x%08x loaded from offset 0x%08x.\n",
986 code_size, kMaxCodeSize,
987 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700988 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800989 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700990 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100991 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700992 }
993 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800994 } else if (options_.disassemble_code_) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100995 DumpCode(vios, oat_method, code_item, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700996 }
997 }
998 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100999 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001000 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001001 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001002
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001003 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1004 if (spill_mask == 0) {
1005 return;
1006 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001007 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001008 for (size_t i = 0; i < 32; i++) {
1009 if ((spill_mask & (1 << i)) != 0) {
1010 if (is_float) {
1011 os << "fr" << i;
1012 } else {
1013 os << "r" << i;
1014 }
1015 spill_mask ^= 1 << i; // clear bit
1016 if (spill_mask != 0) {
1017 os << ", ";
1018 } else {
1019 break;
1020 }
1021 }
1022 }
1023 os << ")";
1024 }
1025
Roland Levillain442b46a2015-02-18 16:54:21 +00001026 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001027 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001028 const OatFile::OatMethod& oat_method,
1029 const DexFile::CodeItem* code_item) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001030 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1031 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001032 const void* raw_code_info = oat_method.GetVmapTable();
1033 if (raw_code_info != nullptr) {
1034 CodeInfo code_info(raw_code_info);
1035 DCHECK(code_item != nullptr);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001036 ScopedIndentation indent1(vios);
1037 DumpCodeInfo(vios, code_info, oat_method, *code_item);
Roland Levillain442b46a2015-02-18 16:54:21 +00001038 }
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001039 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
1040 // We don't encode the size in the table, so just emit that we have quickened
1041 // information.
1042 ScopedIndentation indent(vios);
1043 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001044 } else {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001045 // Otherwise, there is nothing to display.
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001046 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001047 }
1048
1049 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001050 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001051 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001052 const OatFile::OatMethod& oat_method,
Roland Levillain442b46a2015-02-18 16:54:21 +00001053 const DexFile::CodeItem& code_item) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001054 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001055 oat_method.GetCodeOffset(),
1056 code_item.registers_size_,
1057 options_.dump_code_info_stack_maps_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001058 }
1059
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001060 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1061 const DexFile::CodeItem* code_item) {
1062 if (code_item != nullptr) {
1063 size_t num_locals_ins = code_item->registers_size_;
1064 size_t num_ins = code_item->ins_size_;
1065 size_t num_locals = num_locals_ins - num_ins;
1066 size_t num_outs = code_item->outs_size_;
1067
1068 os << "vr_stack_locations:";
1069 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1070 // For readability, delimit the different kinds of VRs.
1071 if (reg == num_locals_ins) {
1072 os << "\n\tmethod*:";
1073 } else if (reg == num_locals && num_ins > 0) {
1074 os << "\n\tins:";
1075 } else if (reg == 0 && num_locals > 0) {
1076 os << "\n\tlocals:";
1077 }
1078
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001079 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1080 code_item,
1081 oat_method.GetCoreSpillMask(),
1082 oat_method.GetFpSpillMask(),
1083 oat_method.GetFrameSizeInBytes(),
1084 reg,
1085 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001086 os << " v" << reg << "[sp + #" << offset << "]";
1087 }
1088
1089 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1090 if (out_reg == 0) {
1091 os << "\n\touts:";
1092 }
1093
1094 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1095 os << " v" << out_reg << "[sp + #" << offset << "]";
1096 }
1097
1098 os << "\n";
1099 }
1100 }
1101
Ian Rogersb23a7722012-10-09 16:54:26 -07001102 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001103 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001104 size_t i = 0;
1105 while (i < code_item->insns_size_in_code_units_) {
1106 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001107 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1108 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001109 i += instruction->SizeInCodeUnits();
1110 }
1111 }
1112 }
1113
Roland Levillainf2650d12015-05-28 14:53:28 +01001114 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1115 // the optimizing compiler?
1116 static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1117 const DexFile::CodeItem* code_item) {
1118 // If the native GC map is null and the Dex `code_item` is not
1119 // null, then this method has been compiled with the optimizing
1120 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001121 return oat_method.GetQuickCode() != nullptr &&
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001122 oat_method.GetVmapTable() != nullptr &&
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001123 code_item != nullptr;
1124 }
1125
1126 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1127 // the dextodex compiler?
1128 static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
1129 const DexFile::CodeItem* code_item) {
1130 // If the quick code is null, the Dex `code_item` is not
1131 // null, and the vmap table is not null, then this method has been compiled
1132 // with the dextodex compiler.
1133 return oat_method.GetQuickCode() == nullptr &&
1134 oat_method.GetVmapTable() != nullptr &&
1135 code_item != nullptr;
Roland Levillainf2650d12015-05-28 14:53:28 +01001136 }
1137
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001138 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001139 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001140 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001141 const DexFile* dex_file,
1142 const DexFile::ClassDef& class_def,
1143 const DexFile::CodeItem* code_item,
1144 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001145 if ((method_access_flags & kAccNative) == 0) {
1146 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001147 Runtime* const runtime = Runtime::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001148 Handle<mirror::DexCache> dex_cache(
Mathieu Chartierf284d442016-06-02 11:48:30 -07001149 hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file, nullptr)));
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001150 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001151 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001152 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
1153 &class_def, code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001154 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001155
1156 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001157 }
1158
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001159 // The StackMapsHelper provides the stack maps in the native PC order.
1160 // For identical native PCs, the order from the CodeInfo is preserved.
1161 class StackMapsHelper {
1162 public:
1163 explicit StackMapsHelper(const uint8_t* raw_code_info)
1164 : code_info_(raw_code_info),
1165 encoding_(code_info_.ExtractEncoding()),
1166 number_of_stack_maps_(code_info_.GetNumberOfStackMaps(encoding_)),
1167 indexes_(),
1168 offset_(static_cast<size_t>(-1)),
1169 stack_map_index_(0u) {
1170 if (number_of_stack_maps_ != 0u) {
1171 // Check if native PCs are ordered.
1172 bool ordered = true;
1173 StackMap last = code_info_.GetStackMapAt(0u, encoding_);
1174 for (size_t i = 1; i != number_of_stack_maps_; ++i) {
1175 StackMap current = code_info_.GetStackMapAt(i, encoding_);
1176 if (last.GetNativePcOffset(encoding_.stack_map_encoding) >
1177 current.GetNativePcOffset(encoding_.stack_map_encoding)) {
1178 ordered = false;
1179 break;
1180 }
1181 last = current;
1182 }
1183 if (!ordered) {
1184 // Create indirection indexes for access in native PC order. We do not optimize
1185 // for the fact that there can currently be only two separately ordered ranges,
1186 // namely normal stack maps and catch-point stack maps.
1187 indexes_.resize(number_of_stack_maps_);
1188 std::iota(indexes_.begin(), indexes_.end(), 0u);
1189 std::sort(indexes_.begin(),
1190 indexes_.end(),
1191 [this](size_t lhs, size_t rhs) {
1192 StackMap left = code_info_.GetStackMapAt(lhs, encoding_);
1193 uint32_t left_pc = left.GetNativePcOffset(encoding_.stack_map_encoding);
1194 StackMap right = code_info_.GetStackMapAt(rhs, encoding_);
1195 uint32_t right_pc = right.GetNativePcOffset(encoding_.stack_map_encoding);
1196 // If the PCs are the same, compare indexes to preserve the original order.
1197 return (left_pc < right_pc) || (left_pc == right_pc && lhs < rhs);
1198 });
1199 }
1200 offset_ = GetStackMapAt(0).GetNativePcOffset(encoding_.stack_map_encoding);
1201 }
1202 }
1203
1204 const CodeInfo& GetCodeInfo() const {
1205 return code_info_;
1206 }
1207
1208 const CodeInfoEncoding& GetEncoding() const {
1209 return encoding_;
1210 }
1211
1212 size_t GetOffset() const {
1213 return offset_;
1214 }
1215
1216 StackMap GetStackMap() const {
1217 return GetStackMapAt(stack_map_index_);
1218 }
1219
1220 void Next() {
1221 ++stack_map_index_;
1222 offset_ = (stack_map_index_ == number_of_stack_maps_)
1223 ? static_cast<size_t>(-1)
1224 : GetStackMapAt(stack_map_index_).GetNativePcOffset(encoding_.stack_map_encoding);
1225 }
1226
1227 private:
1228 StackMap GetStackMapAt(size_t i) const {
1229 if (!indexes_.empty()) {
1230 i = indexes_[i];
1231 }
1232 DCHECK_LT(i, number_of_stack_maps_);
1233 return code_info_.GetStackMapAt(i, encoding_);
1234 }
1235
1236 const CodeInfo code_info_;
1237 const CodeInfoEncoding encoding_;
1238 const size_t number_of_stack_maps_;
1239 dchecked_vector<size_t> indexes_; // Used if stack map native PCs are not ordered.
1240 size_t offset_;
1241 size_t stack_map_index_;
1242 };
1243
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001244 void DumpCode(VariableIndentationOutputStream* vios,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001245 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1246 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001247 const void* quick_code = oat_method.GetQuickCode();
1248
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001249 if (code_size == 0) {
1250 code_size = oat_method.GetQuickCodeSize();
1251 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001252 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001253 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001254 return;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001255 } else if (!bad_input && IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1256 // The optimizing compiler outputs its CodeInfo data in the vmap table.
1257 StackMapsHelper helper(oat_method.GetVmapTable());
1258 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1259 size_t offset = 0;
1260 while (offset < code_size) {
1261 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
1262 if (offset == helper.GetOffset()) {
1263 ScopedIndentation indent1(vios);
1264 StackMap stack_map = helper.GetStackMap();
1265 DCHECK(stack_map.IsValid());
1266 stack_map.Dump(vios,
1267 helper.GetCodeInfo(),
1268 helper.GetEncoding(),
1269 oat_method.GetCodeOffset(),
1270 code_item->registers_size_);
1271 do {
1272 helper.Next();
1273 // There may be multiple stack maps at a given PC. We display only the first one.
1274 } while (offset == helper.GetOffset());
1275 }
1276 DCHECK_LT(offset, helper.GetOffset());
1277 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001278 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001279 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1280 size_t offset = 0;
1281 while (offset < code_size) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001282 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001283 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001284 }
1285 }
1286
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001287 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001288 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001289 const OatDumperOptions& options_;
1290 uint32_t resolved_addr2instr_;
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001291 InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001292 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001293 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001294};
1295
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001296class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001297 public:
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001298 ImageDumper(std::ostream* os,
1299 gc::space::ImageSpace& image_space,
1300 const ImageHeader& image_header,
1301 OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001302 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001303 vios_(os),
1304 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001305 image_space_(image_space),
1306 image_header_(image_header),
1307 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001308
Mathieu Chartier90443472015-07-16 20:32:27 -07001309 bool Dump() SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001310 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001311 std::ostream& indent_os = vios_.Stream();
1312
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001313 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001314
Jeff Haodcdc85b2015-12-04 14:06:18 -08001315 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1316
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001317 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001318
Mathieu Chartiere401d142015-04-22 13:56:20 -07001319 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1320
1321 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1322 auto section = static_cast<ImageHeader::ImageSections>(i);
1323 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1324 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001325
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001326 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001327
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001328 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001329
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001330 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1331
1332 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1333
1334 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001335
Alex Lighta59dd802014-07-02 16:28:08 -07001336 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1337
Igor Murashkin46774762014-10-22 11:37:02 -07001338 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1339
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001340 {
1341 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001342 static_assert(arraysize(image_roots_descriptions_) ==
1343 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001344 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1345 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1346 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001347 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001348 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001349 if (image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001350 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001351 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001352 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001353 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1354 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001355 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001356 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1357 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001358 run++;
1359 } else {
1360 break;
1361 }
1362 }
1363 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001364 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001365 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001366 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001367 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001368 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001369 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001370 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001371 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001372 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001373 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001374 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001375 }
1376 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001377 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001378
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001379 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001380 os << "METHOD ROOTS\n";
1381 static_assert(arraysize(image_methods_descriptions_) ==
1382 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1383 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1384 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1385 const char* description = image_methods_descriptions_[i];
1386 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001387 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001388 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001389 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001390 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001391
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001392 Runtime* const runtime = Runtime::Current();
1393 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001394 std::string image_filename = image_space_.GetImageFilename();
1395 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001396 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001397 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001398 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001399 const OatFile* oat_file = image_space_.GetOatFile();
Alex Lighta59dd802014-07-02 16:28:08 -07001400 if (oat_file == nullptr) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001401 oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location);
1402 }
1403 if (oat_file == nullptr) {
1404 oat_file = OatFile::Open(oat_location,
1405 oat_location,
1406 nullptr,
1407 nullptr,
1408 false,
1409 /*low_4gb*/false,
1410 nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001411 &error_msg);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001412 }
1413 if (oat_file == nullptr) {
1414 os << "OAT FILE NOT FOUND: " << error_msg << "\n";
1415 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001416 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001417 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001418
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001419 stats_.oat_file_bytes = oat_file->Size();
1420
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001421 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001422
Mathieu Chartier02e25112013-08-14 16:14:24 -07001423 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001424 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001425 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1426 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001427 }
1428
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001429 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001430
Jeff Haodcdc85b2015-12-04 14:06:18 -08001431 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001432 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001433 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001434 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001435 {
1436 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1437 heap->FlushAllocStack();
1438 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001439 // Since FlushAllocStack() above resets the (active) allocation
1440 // stack. Need to revoke the thread-local allocation stacks that
1441 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001442 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001443 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001444 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001445 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001446 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001447 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001448 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001449 {
1450 ReaderMutexLock mu(self, *class_linker->DexLock());
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001451 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001452 mirror::DexCache* dex_cache =
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001453 down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001454 if (dex_cache != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01001455 dex_caches_.insert(dex_cache);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001456 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001457 }
1458 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001459 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001460 // Dump the normal objects before ArtMethods.
1461 image_space_.GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1462 indent_os << "\n";
1463 // TODO: Dump fields.
1464 // Dump methods after.
Jeff Haodcdc85b2015-12-04 14:06:18 -08001465 DumpArtMethodVisitor visitor(this);
Mathieu Chartiere42888f2016-04-14 10:49:19 -07001466 image_header_.VisitPackedArtMethods(&visitor,
1467 image_space_.Begin(),
1468 image_header_.GetPointerSize());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001469 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001470 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001471 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001472 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001473 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001474 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001475 size_t data_size = image_header_.GetDataSize(); // stored size in file.
1476 if (file == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001477 LOG(WARNING) << "Failed to find image in " << image_filename;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001478 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001479 stats_.file_bytes = file->GetLength();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001480 // If the image is compressed, adjust to decompressed size.
1481 size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader);
1482 if (image_header_.GetStorageMode() == ImageHeader::kStorageModeUncompressed) {
1483 DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image";
1484 }
1485 stats_.file_bytes += uncompressed_size - data_size;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001486 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001487 size_t header_bytes = sizeof(ImageHeader);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001488 const auto& object_section = image_header_.GetImageSection(ImageHeader::kSectionObjects);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001489 const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1490 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001491 const auto& dex_cache_arrays_section = image_header_.GetImageSection(
1492 ImageHeader::kSectionDexCacheArrays);
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001493 const auto& intern_section = image_header_.GetImageSection(
1494 ImageHeader::kSectionInternedStrings);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001495 const auto& class_table_section = image_header_.GetImageSection(
1496 ImageHeader::kSectionClassTable);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001497 const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1498
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001499 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001500
1501 // Objects are kObjectAlignment-aligned.
1502 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1503 if (object_section.Offset() > header_bytes) {
1504 stats_.alignment_bytes += object_section.Offset() - header_bytes;
1505 }
1506
1507 // Field section is 4-byte aligned.
1508 constexpr size_t kFieldSectionAlignment = 4U;
1509 uint32_t end_objects = object_section.Offset() + object_section.Size();
1510 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
1511 stats_.alignment_bytes += field_section.Offset() - end_objects;
1512
1513 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
1514 uint32_t end_fields = field_section.Offset() + field_section.Size();
1515 CHECK_ALIGNED(method_section.Offset(), 4);
1516 stats_.alignment_bytes += method_section.Offset() - end_fields;
1517
1518 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
1519 uint32_t end_methods = method_section.Offset() + method_section.Size();
1520 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
1521 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
1522
1523 // Intern table is 8-byte aligned.
1524 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
1525 CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
1526 stats_.alignment_bytes += intern_section.Offset() - end_caches;
1527
1528 // Add space between intern table and class table.
1529 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
1530 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
1531
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001532 // Add space between end of image data and bitmap. Expect the bitmap to be page-aligned.
1533 const size_t bitmap_offset = sizeof(ImageHeader) + data_size;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001534 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001535 stats_.alignment_bytes += RoundUp(bitmap_offset, kPageSize) - bitmap_offset;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001536
Mathieu Chartiere401d142015-04-22 13:56:20 -07001537 stats_.bitmap_bytes += bitmap_section.Size();
1538 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01001539 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01001540 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001541 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001542 stats_.class_table_bytes += class_table_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001543 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001544 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001545
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001546 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001547
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001548 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001549 }
1550
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001551 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001552 class DumpArtMethodVisitor : public ArtMethodVisitor {
1553 public:
1554 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
1555
1556 virtual void Visit(ArtMethod* method) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
1557 std::ostream& indent_os = image_dumper_->vios_.Stream();
1558 indent_os << method << " " << " ArtMethod: " << PrettyMethod(method) << "\n";
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001559 image_dumper_->DumpMethod(method, indent_os);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001560 indent_os << "\n";
1561 }
1562
1563 private:
1564 ImageDumper* const image_dumper_;
1565 };
1566
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001567 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Mathieu Chartier90443472015-07-16 20:32:27 -07001568 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001569 CHECK(type != nullptr);
1570 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001571 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001572 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001573 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001574 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001575 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001576 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001577 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001578 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001579 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001580 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001581 }
1582 }
1583
Mathieu Chartierc7853442015-03-27 14:35:38 -07001584 static void PrintField(std::ostream& os, ArtField* field, mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -07001585 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001586 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08001587 switch (field->GetTypeAsPrimitiveType()) {
1588 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08001589 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001590 break;
1591 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001592 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001593 break;
1594 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001595 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001596 break;
1597 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001598 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001599 break;
1600 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07001601 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001602 break;
1603 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07001604 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001605 break;
1606 case Primitive::kPrimBoolean:
Fred Shih37f05ef2014-07-16 18:38:08 -07001607 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1608 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001609 break;
1610 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07001611 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001612 break;
1613 case Primitive::kPrimNot: {
1614 // Get the value, don't compute the type unless it is non-null as we don't want
1615 // to cause class loading.
1616 mirror::Object* value = field->GetObj(obj);
1617 if (value == nullptr) {
1618 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07001619 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08001620 // Grab the field type without causing resolution.
Mathieu Chartierdaaf3262015-03-24 13:30:28 -07001621 mirror::Class* field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08001622 if (field_type != nullptr) {
1623 PrettyObjectValue(os, field_type, value);
1624 } else {
1625 os << StringPrintf("%p %s\n", value,
1626 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
1627 }
Ian Rogers50239c72013-06-17 14:53:22 -07001628 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001629 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07001630 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001631 default:
1632 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
1633 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08001634 }
1635 }
1636
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001637 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Mathieu Chartier90443472015-07-16 20:32:27 -07001638 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001639 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001640 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001641 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001642 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001643 for (ArtField& field : klass->GetIFields()) {
1644 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001645 }
1646 }
1647
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001648 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001649 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001650 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001651
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001652 const void* GetQuickOatCodeBegin(ArtMethod* m) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001653 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001654 image_header_.GetPointerSize());
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001655 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001656 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001657 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001658 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001659 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001660 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001661 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001662 }
1663
Mathieu Chartiere401d142015-04-22 13:56:20 -07001664 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001665 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001666 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1667 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001668 return 0;
1669 }
1670 return oat_code_begin[-1];
1671 }
1672
Mathieu Chartiere401d142015-04-22 13:56:20 -07001673 const void* GetQuickOatCodeEnd(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001674 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001675 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001676 if (oat_code_begin == nullptr) {
1677 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001678 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001679 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001680 }
1681
Mathieu Chartier90443472015-07-16 20:32:27 -07001682 static void Callback(mirror::Object* obj, void* arg) SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001683 DCHECK(obj != nullptr);
1684 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001685 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001686 if (!state->InDumpSpace(obj)) {
1687 return;
1688 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001689
1690 size_t object_bytes = obj->SizeOf();
1691 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1692 state->stats_.object_bytes += object_bytes;
1693 state->stats_.alignment_bytes += alignment_bytes;
1694
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001695 std::ostream& os = state->vios_.Stream();
1696
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001697 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001698 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001699 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
1700 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001701 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001702 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001703 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
1704 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08001705 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001706 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001707 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001708 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001709 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001710 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001711 ScopedIndentation indent1(&state->vios_);
1712 DumpFields(os, obj, obj_class);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001713 const size_t image_pointer_size = state->image_header_.GetPointerSize();
Ian Rogersd5b32602012-02-26 16:40:04 -08001714 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001715 auto* obj_array = obj->AsObjectArray<mirror::Object>();
1716 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001717 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001718 size_t run = 0;
1719 for (int32_t j = i + 1; j < length; j++) {
1720 if (value == obj_array->Get(j)) {
1721 run++;
1722 } else {
1723 break;
1724 }
1725 }
1726 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001727 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001728 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001729 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001730 i = i + run;
1731 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001732 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001733 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001734 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001735 }
1736 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001737 mirror::Class* klass = obj->AsClass();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001738 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001739 os << "STATICS:\n";
1740 ScopedIndentation indent2(&state->vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001741 for (ArtField& field : klass->GetSFields()) {
1742 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001743 }
1744 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001745 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01001746 auto it = state->dex_caches_.find(obj);
1747 if (it != state->dex_caches_.end()) {
1748 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001749 const auto& field_section = state->image_header_.GetImageSection(
1750 ImageHeader::kSectionArtFields);
1751 const auto& method_section = state->image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001752 size_t num_methods = dex_cache->NumResolvedMethods();
1753 if (num_methods != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001754 os << "Methods (size=" << num_methods << "):\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01001755 ScopedIndentation indent2(&state->vios_);
1756 auto* resolved_methods = dex_cache->GetResolvedMethods();
1757 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001758 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_methods,
1759 i,
1760 image_pointer_size);
Vladimir Marko05792b92015-08-03 11:56:49 +01001761 size_t run = 0;
1762 for (size_t j = i + 1;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001763 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_methods,
1764 j,
1765 image_pointer_size);
1766 ++j) {
1767 ++run;
1768 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001769 if (run == 0) {
1770 os << StringPrintf("%zd: ", i);
1771 } else {
1772 os << StringPrintf("%zd to %zd: ", i, i + run);
1773 i = i + run;
1774 }
1775 std::string msg;
1776 if (elem == nullptr) {
1777 msg = "null";
1778 } else if (method_section.Contains(
1779 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
1780 msg = PrettyMethod(reinterpret_cast<ArtMethod*>(elem));
1781 } else {
1782 msg = "<not in method section>";
1783 }
1784 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07001785 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001786 }
1787 size_t num_fields = dex_cache->NumResolvedFields();
1788 if (num_fields != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001789 os << "Fields (size=" << num_fields << "):\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01001790 ScopedIndentation indent2(&state->vios_);
1791 auto* resolved_fields = dex_cache->GetResolvedFields();
1792 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001793 auto* elem = mirror::DexCache::GetElementPtrSize(
1794 resolved_fields, i, image_pointer_size);
Vladimir Marko05792b92015-08-03 11:56:49 +01001795 size_t run = 0;
1796 for (size_t j = i + 1;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001797 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_fields,
1798 j,
1799 image_pointer_size);
1800 ++j) {
1801 ++run;
1802 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001803 if (run == 0) {
1804 os << StringPrintf("%zd: ", i);
1805 } else {
1806 os << StringPrintf("%zd to %zd: ", i, i + run);
1807 i = i + run;
1808 }
1809 std::string msg;
1810 if (elem == nullptr) {
1811 msg = "null";
1812 } else if (field_section.Contains(
1813 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
1814 msg = PrettyField(reinterpret_cast<ArtField*>(elem));
1815 } else {
1816 msg = "<not in field section>";
1817 }
1818 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07001819 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001820 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001821 size_t num_types = dex_cache->NumResolvedTypes();
1822 if (num_types != 0u) {
1823 os << "Types (size=" << num_types << "):\n";
1824 ScopedIndentation indent2(&state->vios_);
1825 auto* resolved_types = dex_cache->GetResolvedTypes();
1826 for (size_t i = 0; i < num_types; ++i) {
1827 auto* elem = resolved_types[i].Read();
1828 size_t run = 0;
1829 for (size_t j = i + 1; j != num_types && elem == resolved_types[j].Read(); ++j) {
1830 ++run;
1831 }
1832 if (run == 0) {
1833 os << StringPrintf("%zd: ", i);
1834 } else {
1835 os << StringPrintf("%zd to %zd: ", i, i + run);
1836 i = i + run;
1837 }
1838 std::string msg;
1839 if (elem == nullptr) {
1840 msg = "null";
1841 } else {
1842 msg = PrettyClass(elem);
1843 }
1844 os << StringPrintf("%p %s\n", elem, msg.c_str());
1845 }
1846 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001847 }
1848 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07001849 std::string temp;
1850 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001851 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001852
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001853 void DumpMethod(ArtMethod* method, std::ostream& indent_os)
Mathieu Chartier90443472015-07-16 20:32:27 -07001854 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001855 DCHECK(method != nullptr);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001856 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
1857 const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
Mathieu Chartiere42888f2016-04-14 10:49:19 -07001858 const size_t pointer_size = image_header_.GetPointerSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01001859 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
1860 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07001861 if (method->IsNative()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001862 bool first_occurrence;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001863 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
1864 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001865 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001866 stats_.native_to_managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001867 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001868 if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
1869 image_header_.GetPointerSize())) {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01001870 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001871 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -07001872 } else if (method->IsAbstract() || method->IsClassInitializer()) {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00001873 // Don't print information for these.
Mathieu Chartiere42888f2016-04-14 10:49:19 -07001874 } else if (method->IsRuntimeMethod()) {
1875 ImtConflictTable* table = method->GetImtConflictTable(image_header_.GetPointerSize());
1876 if (table != nullptr) {
1877 indent_os << "IMT conflict table " << table << " method: ";
1878 for (size_t i = 0, count = table->NumEntries(pointer_size); i < count; ++i) {
1879 indent_os << PrettyMethod(table->GetImplementationMethod(i, pointer_size)) << " ";
1880 }
1881 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001882 } else {
1883 const DexFile::CodeItem* code_item = method->GetCodeItem();
1884 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001885 stats_.dex_instruction_bytes += dex_instruction_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001886
1887 bool first_occurrence;
Roland Levillain6d7f1792015-07-02 10:59:15 +01001888 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01001889 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01001890 // Method compiled with the optimizing compiler have no vmap table.
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001891 vmap_table_bytes = ComputeOatSize(method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01001892 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001893 stats_.vmap_table_bytes += vmap_table_bytes;
Roland Levillain6d7f1792015-07-02 10:59:15 +01001894 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001895 }
1896
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001897 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
1898 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001899 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001900 stats_.managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001901 if (method->IsConstructor()) {
1902 if (method->IsStatic()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001903 stats_.class_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001904 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001905 stats_.large_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001906 }
1907 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001908 stats_.large_method_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001909 }
1910 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001911 stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001912
Igor Murashkin7617abd2015-07-10 18:27:47 -07001913 uint32_t method_access_flags = method->GetAccessFlags();
1914
Mathieu Chartiere401d142015-04-22 13:56:20 -07001915 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001916 indent_os << StringPrintf("SIZE: Dex Instructions=%zd StackMaps=%zd AccessFlags=0x%x\n",
1917 dex_instruction_bytes,
1918 vmap_table_bytes,
Igor Murashkin7617abd2015-07-10 18:27:47 -07001919 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001920
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001921 size_t total_size = dex_instruction_bytes +
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001922 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
Mathieu Chartiere401d142015-04-22 13:56:20 -07001923
1924 double expansion =
1925 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001926 stats_.ComputeOutliers(total_size, expansion, method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001927 }
1928 }
1929
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001930 std::set<const void*> already_seen_;
1931 // Compute the size of the given data within the oat file and whether this is the first time
1932 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07001933 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001934 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001935 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001936 already_seen_.insert(oat_data);
1937 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001938 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001939 }
1940 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001941 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001942
1943 public:
1944 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001945 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001946 size_t file_bytes;
1947
1948 size_t header_bytes;
1949 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001950 size_t art_field_bytes;
1951 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01001952 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001953 size_t interned_strings_bytes;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001954 size_t class_table_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07001955 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001956 size_t alignment_bytes;
1957
1958 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001959 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001960 size_t managed_to_native_code_bytes;
1961 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001962 size_t class_initializer_code_bytes;
1963 size_t large_initializer_code_bytes;
1964 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001965
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001966 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001967
1968 size_t dex_instruction_bytes;
1969
Mathieu Chartiere401d142015-04-22 13:56:20 -07001970 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001971 std::vector<size_t> method_outlier_size;
1972 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07001973 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001974
Roland Levillain3887c462015-08-12 18:15:42 +01001975 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001976 : oat_file_bytes(0),
1977 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001978 header_bytes(0),
1979 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07001980 art_field_bytes(0),
1981 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01001982 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001983 interned_strings_bytes(0),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001984 class_table_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07001985 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001986 alignment_bytes(0),
1987 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001988 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001989 managed_to_native_code_bytes(0),
1990 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07001991 class_initializer_code_bytes(0),
1992 large_initializer_code_bytes(0),
1993 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001994 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001995 dex_instruction_bytes(0) {}
1996
Elliott Hughesa0e18062012-04-13 15:59:59 -07001997 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001998 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07001999 size_t bytes;
2000 size_t count;
2001 };
2002 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2003 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002004
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002005 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002006 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2007 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002008 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002009 it->second.count += 1;
2010 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002011 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002012 }
2013 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002014
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002015 double PercentOfOatBytes(size_t size) {
2016 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2017 }
2018
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002019 double PercentOfFileBytes(size_t size) {
2020 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2021 }
2022
2023 double PercentOfObjectBytes(size_t size) {
2024 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2025 }
2026
Mathieu Chartiere401d142015-04-22 13:56:20 -07002027 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002028 method_outlier_size.push_back(total_size);
2029 method_outlier_expansion.push_back(expansion);
2030 method_outlier.push_back(method);
2031 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002032
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002033 void DumpOutliers(std::ostream& os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002034 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002035 size_t sum_of_sizes = 0;
2036 size_t sum_of_sizes_squared = 0;
2037 size_t sum_of_expansion = 0;
2038 size_t sum_of_expansion_squared = 0;
2039 size_t n = method_outlier_size.size();
Mathieu Chartier1ebf8d32016-06-09 11:51:27 -07002040 if (n <= 1) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08002041 return;
2042 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002043 for (size_t i = 0; i < n; i++) {
2044 size_t cur_size = method_outlier_size[i];
2045 sum_of_sizes += cur_size;
2046 sum_of_sizes_squared += cur_size * cur_size;
2047 double cur_expansion = method_outlier_expansion[i];
2048 sum_of_expansion += cur_expansion;
2049 sum_of_expansion_squared += cur_expansion * cur_expansion;
2050 }
2051 size_t size_mean = sum_of_sizes / n;
2052 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2053 double expansion_mean = sum_of_expansion / n;
2054 double expansion_variance =
2055 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2056
2057 // Dump methods whose size is a certain number of standard deviations from the mean
2058 size_t dumped_values = 0;
2059 size_t skipped_values = 0;
2060 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2061 size_t cur_size_variance = i * i * size_variance;
2062 bool first = true;
2063 for (size_t j = 0; j < n; j++) {
2064 size_t cur_size = method_outlier_size[j];
2065 if (cur_size > size_mean) {
2066 size_t cur_var = cur_size - size_mean;
2067 cur_var = cur_var * cur_var;
2068 if (cur_var > cur_size_variance) {
2069 if (dumped_values > 20) {
2070 if (i == 1) {
2071 skipped_values++;
2072 } else {
2073 i = 2; // jump to counting for 1 standard deviation
2074 break;
2075 }
2076 } else {
2077 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002078 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002079 first = false;
2080 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002081 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002082 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002083 method_outlier_size[j] = 0; // don't consider this method again
2084 dumped_values++;
2085 }
2086 }
2087 }
2088 }
2089 }
2090 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002091 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002092 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002093 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002094 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002095
2096 // Dump methods whose expansion is a certain number of standard deviations from the mean
2097 dumped_values = 0;
2098 skipped_values = 0;
2099 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2100 double cur_expansion_variance = i * i * expansion_variance;
2101 bool first = true;
2102 for (size_t j = 0; j < n; j++) {
2103 double cur_expansion = method_outlier_expansion[j];
2104 if (cur_expansion > expansion_mean) {
2105 size_t cur_var = cur_expansion - expansion_mean;
2106 cur_var = cur_var * cur_var;
2107 if (cur_var > cur_expansion_variance) {
2108 if (dumped_values > 20) {
2109 if (i == 1) {
2110 skipped_values++;
2111 } else {
2112 i = 2; // jump to counting for 1 standard deviation
2113 break;
2114 }
2115 } else {
2116 if (first) {
2117 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002118 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002119 first = false;
2120 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002121 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002122 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002123 method_outlier_expansion[j] = 0.0; // don't consider this method again
2124 dumped_values++;
2125 }
2126 }
2127 }
2128 }
2129 }
2130 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002131 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002132 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002133 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002134 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002135 }
2136
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002137 void Dump(std::ostream& os, std::ostream& indent_os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002138 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002139 {
2140 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2141 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002142 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2143 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2144 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2145 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2146 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2147 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002148 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002149 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2150 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002151 header_bytes, PercentOfFileBytes(header_bytes),
2152 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002153 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2154 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002155 dex_cache_arrays_bytes,
2156 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002157 interned_strings_bytes,
2158 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002159 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002160 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002161 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2162 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002163 CHECK_EQ(file_bytes,
2164 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2165 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2166 bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002167 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002168
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002169 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002170 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002171 for (const auto& sizes_and_count : sizes_and_counts) {
2172 const std::string& descriptor(sizes_and_count.first);
2173 double average = static_cast<double>(sizes_and_count.second.bytes) /
2174 static_cast<double>(sizes_and_count.second.count);
2175 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002176 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002177 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002178 descriptor.c_str(), sizes_and_count.second.bytes,
2179 sizes_and_count.second.count, average, percent);
2180 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002181 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002182 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002183 CHECK_EQ(object_bytes, object_bytes_total);
2184
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002185 os << StringPrintf("oat_file_bytes = %8zd\n"
2186 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2187 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2188 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2189 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2190 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2191 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002192 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002193 managed_code_bytes,
2194 PercentOfOatBytes(managed_code_bytes),
2195 managed_to_native_code_bytes,
2196 PercentOfOatBytes(managed_to_native_code_bytes),
2197 native_to_managed_code_bytes,
2198 PercentOfOatBytes(native_to_managed_code_bytes),
2199 class_initializer_code_bytes,
2200 PercentOfOatBytes(class_initializer_code_bytes),
2201 large_initializer_code_bytes,
2202 PercentOfOatBytes(large_initializer_code_bytes),
2203 large_method_code_bytes,
2204 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002205 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002206 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002207 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002208 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2209 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002210 }
2211
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002212 os << "\n" << StringPrintf("vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002213 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002214 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002215
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002216 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2217 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002218 static_cast<double>(managed_code_bytes) /
2219 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002220 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002221 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002222 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002223
2224 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002225 }
2226 } stats_;
2227
2228 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002229 enum {
2230 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2231 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2232 kLargeConstructorDexBytes = 4000,
2233 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2234 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2235 kLargeMethodDexBytes = 16000
2236 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002237
2238 // For performance, use the *os_ directly for anything that doesn't need indentation
2239 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002240 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002241 VariableIndentationOutputStream vios_;
2242 ScopedIndentation indent1_;
2243
Ian Rogers1d54e732013-05-02 21:10:01 -07002244 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002245 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002246 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002247 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002248 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002249
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002250 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002251};
2252
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002253static int DumpImage(gc::space::ImageSpace* image_space,
2254 OatDumperOptions* options,
2255 std::ostream* os) SHARED_REQUIRES(Locks::mutator_lock_) {
2256 const ImageHeader& image_header = image_space->GetImageHeader();
2257 if (!image_header.IsValid()) {
2258 fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
2259 return EXIT_FAILURE;
2260 }
2261 ImageDumper image_dumper(os, *image_space, image_header, options);
2262 if (!image_dumper.Dump()) {
2263 return EXIT_FAILURE;
2264 }
2265 return EXIT_SUCCESS;
2266}
2267
2268static int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002269 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002270 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002271 options->class_loader_ = &null_class_loader;
2272
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002273 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002274 if (options->app_image_ != nullptr) {
2275 if (options->app_oat_ == nullptr) {
2276 LOG(ERROR) << "Can not dump app image without app oat file";
Jeff Haodcdc85b2015-12-04 14:06:18 -08002277 return EXIT_FAILURE;
2278 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002279 // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
2280 // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
2281 // pointers into 32 bit pointer sized ArtMethods.
2282 std::string error_msg;
2283 std::unique_ptr<OatFile> oat_file(OatFile::Open(options->app_oat_,
2284 options->app_oat_,
2285 nullptr,
2286 nullptr,
2287 false,
2288 /*low_4gb*/true,
2289 nullptr,
2290 &error_msg));
2291 if (oat_file == nullptr) {
2292 LOG(ERROR) << "Failed to open oat file " << options->app_oat_ << " with error " << error_msg;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002293 return EXIT_FAILURE;
2294 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002295 std::unique_ptr<gc::space::ImageSpace> space(
2296 gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg));
2297 if (space == nullptr) {
2298 LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error "
2299 << error_msg;
2300 }
2301 // Open dex files for the image.
2302 std::vector<std::unique_ptr<const DexFile>> dex_files;
2303 if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
2304 LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
2305 << error_msg;
2306 }
2307 // Dump the actual image.
2308 int result = DumpImage(space.get(), options, os);
2309 if (result != EXIT_SUCCESS) {
2310 return result;
2311 }
2312 // Fall through to dump the boot images.
2313 }
2314
2315 gc::Heap* heap = runtime->GetHeap();
2316 CHECK(heap->HasBootImageSpace()) << "No image spaces";
2317 for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
2318 int result = DumpImage(image_space, options, os);
2319 if (result != EXIT_SUCCESS) {
2320 return result;
2321 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002322 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002323 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002324}
2325
Andreas Gampe00b25f32014-09-17 21:49:05 -07002326static int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
2327 std::ostream* os) {
2328 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2329
2330 Thread* self = Thread::Current();
2331 CHECK(self != nullptr);
2332 // Need well-known-classes.
2333 WellKnownClasses::Init(self->GetJniEnv());
2334
2335 // Need to register dex files to get a working dex cache.
2336 ScopedObjectAccess soa(self);
2337 ClassLinker* class_linker = runtime->GetClassLinker();
Mathieu Chartierd57d4542015-10-14 10:55:30 -07002338 runtime->GetOatFileManager().RegisterOatFile(std::unique_ptr<const OatFile>(oat_file));
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002339 std::vector<const DexFile*> class_path;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002340 for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
2341 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002342 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002343 CHECK(dex_file != nullptr) << error_msg;
Mathieu Chartierf284d442016-06-02 11:48:30 -07002344 class_linker->RegisterDexFile(*dex_file, nullptr);
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002345 class_path.push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002346 }
2347
2348 // Need a class loader.
Andreas Gampe00b25f32014-09-17 21:49:05 -07002349 // Fake that we're a compiler.
Mathieu Chartier966878d2016-01-14 14:33:29 -08002350 jobject class_loader = class_linker->CreatePathClassLoader(self, class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002351
2352 // Use the class loader while dumping.
2353 StackHandleScope<1> scope(self);
2354 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
2355 soa.Decode<mirror::ClassLoader*>(class_loader));
2356 options->class_loader_ = &loader_handle;
2357
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002358 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002359 bool success = oat_dumper.Dump(*os);
2360 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2361}
2362
2363static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002364 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002365 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002366 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002367 options->class_loader_ = &null_class_loader;
2368
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002369 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002370 bool success = oat_dumper.Dump(*os);
2371 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2372}
2373
2374static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2375 std::ostream* os) {
2376 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002377 OatFile* oat_file = OatFile::Open(oat_filename,
2378 oat_filename,
2379 nullptr,
2380 nullptr,
2381 false,
2382 /*low_4gb*/false,
2383 nullptr,
2384 &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002385 if (oat_file == nullptr) {
2386 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2387 return EXIT_FAILURE;
2388 }
2389
2390 if (runtime != nullptr) {
2391 return DumpOatWithRuntime(runtime, oat_file, options, os);
2392 } else {
2393 return DumpOatWithoutRuntime(oat_file, options, os);
2394 }
2395}
2396
David Srbecky2fdd03c2016-03-10 15:32:37 +00002397static int SymbolizeOat(const char* oat_filename, std::string& output_name, bool no_bits) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002398 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002399 OatFile* oat_file = OatFile::Open(oat_filename,
2400 oat_filename,
2401 nullptr,
2402 nullptr,
2403 false,
2404 /*low_4gb*/false,
2405 nullptr,
2406 &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002407 if (oat_file == nullptr) {
2408 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2409 return EXIT_FAILURE;
2410 }
2411
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002412 bool result;
2413 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
2414 // files for 64-bit code in the past.
2415 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002416 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file, output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002417 result = oat_symbolizer.Symbolize();
2418 } else {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002419 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file, output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002420 result = oat_symbolizer.Symbolize();
2421 }
2422 if (!result) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002423 fprintf(stderr, "Failed to symbolize\n");
2424 return EXIT_FAILURE;
2425 }
2426
2427 return EXIT_SUCCESS;
2428}
2429
Igor Murashkin37743352014-11-13 14:38:00 -08002430struct OatdumpArgs : public CmdlineArgs {
2431 protected:
2432 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002433
Igor Murashkin37743352014-11-13 14:38:00 -08002434 virtual ParseStatus ParseCustom(const StringPiece& option,
2435 std::string* error_msg) OVERRIDE {
2436 {
2437 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
2438 if (base_parse != kParseUnknownArgument) {
2439 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002440 }
2441 }
2442
Igor Murashkin37743352014-11-13 14:38:00 -08002443 if (option.starts_with("--oat-file=")) {
2444 oat_filename_ = option.substr(strlen("--oat-file=")).data();
2445 } else if (option.starts_with("--image=")) {
2446 image_location_ = option.substr(strlen("--image=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002447 } else if (option == "--no-dump:vmap") {
2448 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01002449 } else if (option =="--dump:code_info_stack_maps") {
2450 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002451 } else if (option == "--no-disassemble") {
2452 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00002453 } else if (option =="--header-only") {
2454 dump_header_only_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002455 } else if (option.starts_with("--symbolize=")) {
2456 oat_filename_ = option.substr(strlen("--symbolize=")).data();
2457 symbolize_ = true;
David Srbecky2fdd03c2016-03-10 15:32:37 +00002458 } else if (option.starts_with("--only-keep-debug")) {
2459 only_keep_debug_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002460 } else if (option.starts_with("--class-filter=")) {
2461 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002462 } else if (option.starts_with("--method-filter=")) {
2463 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002464 } else if (option.starts_with("--list-classes")) {
2465 list_classes_ = true;
2466 } else if (option.starts_with("--list-methods")) {
2467 list_methods_ = true;
2468 } else if (option.starts_with("--export-dex-to=")) {
2469 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
2470 } else if (option.starts_with("--addr2instr=")) {
2471 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
2472 *error_msg = "Address conversion failed";
2473 return kParseError;
2474 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002475 } else if (option.starts_with("--app-image=")) {
2476 app_image_ = option.substr(strlen("--app-image=")).data();
2477 } else if (option.starts_with("--app-oat=")) {
2478 app_oat_ = option.substr(strlen("--app-oat=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002479 } else {
2480 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002481 }
2482
Igor Murashkin37743352014-11-13 14:38:00 -08002483 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002484 }
2485
Igor Murashkin37743352014-11-13 14:38:00 -08002486 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
2487 // Infer boot image location from the image location if possible.
2488 if (boot_image_location_ == nullptr) {
2489 boot_image_location_ = image_location_;
2490 }
2491
2492 // Perform the parent checks.
2493 ParseStatus parent_checks = Base::ParseChecks(error_msg);
2494 if (parent_checks != kParseOk) {
2495 return parent_checks;
2496 }
2497
2498 // Perform our own checks.
2499 if (image_location_ == nullptr && oat_filename_ == nullptr) {
2500 *error_msg = "Either --image or --oat-file must be specified";
2501 return kParseError;
2502 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
2503 *error_msg = "Either --image or --oat-file must be specified but not both";
2504 return kParseError;
2505 }
2506
2507 return kParseOk;
2508 }
2509
2510 virtual std::string GetUsage() const {
2511 std::string usage;
2512
2513 usage +=
2514 "Usage: oatdump [options] ...\n"
2515 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
2516 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
2517 "\n"
2518 // Either oat-file or image is required.
2519 " --oat-file=<file.oat>: specifies an input oat filename.\n"
2520 " Example: --oat-file=/system/framework/boot.oat\n"
2521 "\n"
2522 " --image=<file.art>: specifies an input image location.\n"
2523 " Example: --image=/system/framework/boot.art\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002524 "\n"
2525 " --app-image=<file.art>: specifies an input app image. Must also have a specified\n"
2526 " boot image and app oat file.\n"
2527 " Example: --app-image=app.art\n"
2528 "\n"
2529 " --app-oat=<file.odex>: specifies an input app oat.\n"
2530 " Example: --app-oat=app.odex\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002531 "\n";
2532
2533 usage += Base::GetUsage();
2534
2535 usage += // Optional.
Igor Murashkin37743352014-11-13 14:38:00 -08002536 " --no-dump:vmap may be used to disable vmap dumping.\n"
2537 " Example: --no-dump:vmap\n"
2538 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01002539 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
2540 " Example: --dump:code_info_stack_maps\n"
2541 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002542 " --no-disassemble may be used to disable disassembly.\n"
2543 " Example: --no-disassemble\n"
2544 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00002545 " --header-only may be used to print only the oat header.\n"
2546 " Example: --header-only\n"
2547 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002548 " --list-classes may be used to list target file classes (can be used with filters).\n"
2549 " Example: --list-classes\n"
2550 " Example: --list-classes --class-filter=com.example.foo\n"
2551 "\n"
2552 " --list-methods may be used to list target file methods (can be used with filters).\n"
2553 " Example: --list-methods\n"
2554 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
2555 "\n"
2556 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
2557 " Example: --symbolize=/system/framework/boot.oat\n"
2558 "\n"
David Srbecky2fdd03c2016-03-10 15:32:37 +00002559 " --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
2560 " .rodata and .text sections are omitted in the output file to save space.\n"
2561 " Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
2562 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002563 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
2564 " Example: --class-filter=com.example.foo\n"
2565 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002566 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
2567 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002568 "\n"
2569 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
2570 " Example: --export-dex-to=/data/local/tmp\n"
2571 "\n"
2572 " --addr2instr=<address>: output matching method disassembled code from relative\n"
2573 " address (e.g. PC from crash dump)\n"
2574 " Example: --addr2instr=0x00001a3b\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002575 "\n";
2576
2577 return usage;
2578 }
2579
2580 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07002581 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002582 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00002583 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07002584 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002585 std::string elf_filename_prefix_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002586 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01002587 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002588 bool disassemble_code_ = true;
2589 bool symbolize_ = false;
David Srbecky2fdd03c2016-03-10 15:32:37 +00002590 bool only_keep_debug_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002591 bool list_classes_ = false;
2592 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00002593 bool dump_header_only_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002594 uint32_t addr2instr_ = 0;
2595 const char* export_dex_location_ = nullptr;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002596 const char* app_image_ = nullptr;
2597 const char* app_oat_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002598};
2599
Igor Murashkin37743352014-11-13 14:38:00 -08002600struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
2601 virtual bool NeedsRuntime() OVERRIDE {
2602 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002603
Igor Murashkin37743352014-11-13 14:38:00 -08002604 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
2605 bool absolute_addresses = (args_->oat_filename_ == nullptr);
2606
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002607 oat_dumper_options_.reset(new OatDumperOptions(
Igor Murashkin37743352014-11-13 14:38:00 -08002608 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01002609 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08002610 args_->disassemble_code_,
2611 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002612 args_->class_filter_,
2613 args_->method_filter_,
2614 args_->list_classes_,
2615 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00002616 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002617 args_->export_dex_location_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002618 args_->app_image_,
2619 args_->app_oat_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002620 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08002621
2622 return (args_->boot_image_location_ != nullptr || args_->image_location_ != nullptr) &&
2623 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002624 }
2625
Igor Murashkin37743352014-11-13 14:38:00 -08002626 virtual bool ExecuteWithoutRuntime() OVERRIDE {
2627 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08002628 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002629
Mathieu Chartierd424d082014-10-15 10:31:46 -07002630 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08002631
Andreas Gampec24f3992014-12-17 20:40:11 -08002632 if (args_->symbolize_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002633 // ELF has special kind of section called SHT_NOBITS which allows us to create
2634 // sections which exist but their data is omitted from the ELF file to save space.
2635 // This is what "strip --only-keep-debug" does when it creates separate ELF file
2636 // with only debug data. We use it in similar way to exclude .rodata and .text.
2637 bool no_bits = args_->only_keep_debug_;
2638 return SymbolizeOat(args_->oat_filename_, args_->output_name_, no_bits) == EXIT_SUCCESS;
Andreas Gampec24f3992014-12-17 20:40:11 -08002639 } else {
2640 return DumpOat(nullptr,
2641 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002642 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08002643 args_->os_) == EXIT_SUCCESS;
2644 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002645 }
2646
Igor Murashkin37743352014-11-13 14:38:00 -08002647 virtual bool ExecuteWithRuntime(Runtime* runtime) {
2648 CHECK(args_ != nullptr);
2649
2650 if (args_->oat_filename_ != nullptr) {
2651 return DumpOat(runtime,
2652 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002653 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08002654 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002655 }
Igor Murashkin37743352014-11-13 14:38:00 -08002656
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002657 return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002658 }
2659
Igor Murashkin37743352014-11-13 14:38:00 -08002660 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
2661};
Andreas Gampe00b25f32014-09-17 21:49:05 -07002662
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002663} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07002664
2665int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08002666 art::OatdumpMain main;
2667 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002668}