blob: 3d208b5d28af697d8d18c727170aa3a0db29f886 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrom78128a62011-09-15 17:21:19 -070016
17#include <stdio.h>
18#include <stdlib.h>
19
Brian Carlstrom27ec9612011-09-19 20:20:38 -070020#include <fstream>
21#include <iostream>
Igor Murashkin37743352014-11-13 14:38:00 -080022#include <map>
23#include <set>
Brian Carlstrom78128a62011-09-15 17:21:19 -070024#include <string>
Andreas Gampe54fc26c2014-09-04 21:47:42 -070025#include <unordered_map>
Andreas Gampe9fded872016-09-25 16:08:35 -070026#include <unordered_set>
Brian Carlstrom78128a62011-09-15 17:21:19 -070027#include <vector>
28
Ian Rogersd582fa42014-11-05 23:46:43 -080029#include "arch/instruction_set_features.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070030#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070031#include "art_method-inl.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000032#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080033#include "base/unix_file/fd_file.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070034#include "class_linker.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035#include "class_linker-inl.h"
David Srbecky5d950762016-03-07 20:47:29 +000036#include "debug/elf_debug_writer.h"
37#include "debug/method_debug_info.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070038#include "dex_file-inl.h"
Christina Wadsworthbc233ac2016-06-20 15:01:32 -070039#include "dex_instruction-inl.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080040#include "disassembler.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070041#include "elf_builder.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070042#include "gc/space/image_space.h"
43#include "gc/space/large_object_space.h"
44#include "gc/space/space-inl.h"
Mathieu Chartier4a26f172016-01-26 14:26:18 -080045#include "image-inl.h"
Andreas Gampe9fded872016-09-25 16:08:35 -070046#include "imtable-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080047#include "indenter.h"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -070048#include "interpreter/unstarted_runtime.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000049#include "linker/buffered_output_stream.h"
50#include "linker/file_output_stream.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051#include "mirror/array-inl.h"
52#include "mirror/class-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010053#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080054#include "mirror/object-inl.h"
55#include "mirror/object_array-inl.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080056#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010057#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070058#include "oat_file_manager.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080059#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070060#include "safe_map.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070061#include "scoped_thread_state_change-inl.h"
Nicolas Geoffray9c055782016-07-14 09:24:30 +000062#include "ScopedLocalRef.h"
Mathieu Chartierdc00f182016-07-14 10:10:44 -070063#include "stack_map.h"
64#include "string_reference.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080065#include "thread_list.h"
Andreas Gampe2ba88952016-04-29 17:52:07 -070066#include "type_lookup_table.h"
Nicolas Geoffray4acefd32016-10-24 13:14:58 +010067#include "vdex_file.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080068#include "verifier/method_verifier.h"
Nicolas Geoffraye70dd562016-10-30 21:03:35 +000069#include "verifier/verifier_deps.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070070#include "well_known_classes.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070071
Igor Murashkin37743352014-11-13 14:38:00 -080072#include <sys/stat.h>
73#include "cmdline.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070074
Igor Murashkin37743352014-11-13 14:38:00 -080075namespace art {
Brian Carlstrom78128a62011-09-15 17:21:19 -070076
Mathieu Chartiere401d142015-04-22 13:56:20 -070077const char* image_methods_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -080078 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -070079 "kImtConflictMethod",
Mathieu Chartier2d2621a2014-10-23 16:48:06 -070080 "kImtUnimplementedMethod",
Vladimir Markofd36f1f2016-08-03 18:49:58 +010081 "kSaveAllCalleeSavesMethod",
82 "kSaveRefsOnlyMethod",
83 "kSaveRefsAndArgsMethod",
Vladimir Marko952dbb12016-07-28 12:01:51 +010084 "kSaveEverythingMethod",
Mathieu Chartiere401d142015-04-22 13:56:20 -070085};
86
87const char* image_roots_descriptions_[] = {
Brian Carlstrom58ae9412011-10-04 00:56:06 -070088 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070089 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070090};
91
Mathieu Chartierac8f4392015-08-27 13:54:20 -070092// Map is so that we don't allocate multiple dex files for the same OatDexFile.
93static std::map<const OatFile::OatDexFile*,
94 std::unique_ptr<const DexFile>> opened_dex_files;
95
96const DexFile* OpenDexFile(const OatFile::OatDexFile* oat_dex_file, std::string* error_msg) {
97 DCHECK(oat_dex_file != nullptr);
98 auto it = opened_dex_files.find(oat_dex_file);
99 if (it != opened_dex_files.end()) {
100 return it->second.get();
101 }
102 const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
103 opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
104 return ret;
105}
106
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800107template <typename ElfTypes>
David Srbeckybc90fd02015-04-22 19:40:27 +0100108class OatSymbolizer FINAL {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700109 public:
David Srbecky2fdd03c2016-03-10 15:32:37 +0000110 OatSymbolizer(const OatFile* oat_file, const std::string& output_name, bool no_bits) :
111 oat_file_(oat_file),
112 builder_(nullptr),
113 output_name_(output_name.empty() ? "symbolized.oat" : output_name),
114 no_bits_(no_bits) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700115 }
116
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700117 bool Symbolize() {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000118 const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800119 std::unique_ptr<const InstructionSetFeatures> features = InstructionSetFeatures::FromBitmap(
David Srbecky5d811202016-03-08 13:21:22 +0000120 isa, oat_file_->GetOatHeader().GetInstructionSetFeaturesBitmap());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000121
122 File* elf_file = OS::CreateEmptyFile(output_name_.c_str());
123 std::unique_ptr<BufferedOutputStream> output_stream(
Vladimir Marko10c13562015-11-25 14:33:36 +0000124 MakeUnique<BufferedOutputStream>(MakeUnique<FileOutputStream>(elf_file)));
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800125 builder_.reset(new ElfBuilder<ElfTypes>(isa, features.get(), output_stream.get()));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000126
127 builder_->Start();
128
129 auto* rodata = builder_->GetRoData();
130 auto* text = builder_->GetText();
131 auto* bss = builder_->GetBss();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000132
David Srbecky6d8c8f02015-10-26 10:57:09 +0000133 const uint8_t* rodata_begin = oat_file_->Begin();
134 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
David Srbecky2fdd03c2016-03-10 15:32:37 +0000135 if (no_bits_) {
136 rodata->WriteNoBitsSection(rodata_size);
137 } else {
138 rodata->Start();
139 rodata->WriteFully(rodata_begin, rodata_size);
140 rodata->End();
141 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000142
David Srbecky6d8c8f02015-10-26 10:57:09 +0000143 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
144 const size_t text_size = oat_file_->End() - text_begin;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000145 if (no_bits_) {
146 text->WriteNoBitsSection(text_size);
147 } else {
148 text->Start();
149 text->WriteFully(text_begin, text_size);
150 text->End();
151 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000152
153 if (oat_file_->BssSize() != 0) {
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000154 bss->WriteNoBitsSection(oat_file_->BssSize());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000155 }
156
Douglas Leung316a2182015-09-17 15:26:25 -0700157 if (isa == kMips || isa == kMips64) {
158 builder_->WriteMIPSabiflagsSection();
159 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000160 builder_->PrepareDynamicSection(elf_file->GetPath(),
161 rodata_size,
162 text_size,
163 oat_file_->BssSize(),
164 oat_file_->BssRootsOffset());
Vladimir Marko944da602016-02-19 12:27:55 +0000165 builder_->WriteDynamicSection();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700166
David Srbecky5d950762016-03-07 20:47:29 +0000167 Walk();
168 for (const auto& trampoline : debug::MakeTrampolineInfos(oat_file_->GetOatHeader())) {
169 method_debug_infos_.push_back(trampoline);
170 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700171
David Srbecky5d950762016-03-07 20:47:29 +0000172 debug::WriteDebugInfo(builder_.get(),
173 ArrayRef<const debug::MethodDebugInfo>(method_debug_infos_),
174 dwarf::DW_DEBUG_FRAME_FORMAT,
175 true /* write_oat_patches */);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700176
David Srbecky6d8c8f02015-10-26 10:57:09 +0000177 builder_->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700178
Vladimir Marko10c13562015-11-25 14:33:36 +0000179 return builder_->Good();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700180 }
181
David Srbecky5d950762016-03-07 20:47:29 +0000182 void Walk() {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700183 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
184 for (size_t i = 0; i < oat_dex_files.size(); i++) {
185 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700186 CHECK(oat_dex_file != nullptr);
David Srbecky5d950762016-03-07 20:47:29 +0000187 WalkOatDexFile(oat_dex_file);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700188 }
189 }
190
David Srbecky5d950762016-03-07 20:47:29 +0000191 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700192 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700193 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
194 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700195 return;
196 }
197 for (size_t class_def_index = 0;
198 class_def_index < dex_file->NumClassDefs();
199 class_def_index++) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700200 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
201 OatClassType type = oat_class.GetType();
202 switch (type) {
203 case kOatClassAllCompiled:
204 case kOatClassSomeCompiled:
David Srbecky5d950762016-03-07 20:47:29 +0000205 WalkOatClass(oat_class, *dex_file, class_def_index);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700206 break;
207
208 case kOatClassNoneCompiled:
209 case kOatClassMax:
210 // Ignore.
211 break;
212 }
213 }
214 }
215
David Srbecky5d950762016-03-07 20:47:29 +0000216 void WalkOatClass(const OatFile::OatClass& oat_class,
217 const DexFile& dex_file,
218 uint32_t class_def_index) {
219 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700220 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700221 if (class_data == nullptr) { // empty class such as a marker interface?
222 return;
223 }
224 // Note: even if this is an interface or a native class, we still have to walk it, as there
225 // might be a static initializer.
226 ClassDataItemIterator it(dex_file, class_data);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700227 uint32_t class_method_idx = 0;
David Srbecky5d950762016-03-07 20:47:29 +0000228 for (; it.HasNextStaticField(); it.Next()) { /* skip */ }
229 for (; it.HasNextInstanceField(); it.Next()) { /* skip */ }
230 for (; it.HasNextDirectMethod() || it.HasNextVirtualMethod(); it.Next()) {
231 WalkOatMethod(oat_class.GetOatMethod(class_method_idx++),
232 dex_file,
233 class_def_index,
234 it.GetMemberIndex(),
235 it.GetMethodCodeItem(),
236 it.GetMethodAccessFlags());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700237 }
238 DCHECK(!it.HasNext());
239 }
240
David Srbecky5d950762016-03-07 20:47:29 +0000241 void WalkOatMethod(const OatFile::OatMethod& oat_method,
242 const DexFile& dex_file,
243 uint32_t class_def_index,
244 uint32_t dex_method_index,
245 const DexFile::CodeItem* code_item,
246 uint32_t method_access_flags) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700247 if ((method_access_flags & kAccAbstract) != 0) {
248 // Abstract method, no code.
249 return;
250 }
David Srbecky5d950762016-03-07 20:47:29 +0000251 const OatHeader& oat_header = oat_file_->GetOatHeader();
252 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
253 if (method_header == nullptr || method_header->GetCodeSize() == 0) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700254 // No code.
255 return;
256 }
257
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100258 uint32_t entry_point = oat_method.GetCodeOffset() - oat_header.GetExecutableOffset();
259 // Clear Thumb2 bit.
260 const void* code_address = EntryPointToCodePointer(reinterpret_cast<void*>(entry_point));
261
David Srbecky5d950762016-03-07 20:47:29 +0000262 debug::MethodDebugInfo info = debug::MethodDebugInfo();
263 info.trampoline_name = nullptr;
264 info.dex_file = &dex_file;
265 info.class_def_index = class_def_index;
266 info.dex_method_index = dex_method_index;
267 info.access_flags = method_access_flags;
268 info.code_item = code_item;
269 info.isa = oat_header.GetInstructionSet();
270 info.deduped = !seen_offsets_.insert(oat_method.GetCodeOffset()).second;
271 info.is_native_debuggable = oat_header.IsNativeDebuggable();
272 info.is_optimized = method_header->IsOptimized();
273 info.is_code_address_text_relative = true;
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100274 info.code_address = reinterpret_cast<uintptr_t>(code_address);
David Srbecky5d950762016-03-07 20:47:29 +0000275 info.code_size = method_header->GetCodeSize();
276 info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
277 info.code_info = info.is_optimized ? method_header->GetOptimizedCodeInfoPtr() : nullptr;
278 info.cfi = ArrayRef<uint8_t>();
279 method_debug_infos_.push_back(info);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700280 }
281
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700282 private:
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700283 const OatFile* oat_file_;
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800284 std::unique_ptr<ElfBuilder<ElfTypes> > builder_;
David Srbecky5d950762016-03-07 20:47:29 +0000285 std::vector<debug::MethodDebugInfo> method_debug_infos_;
286 std::unordered_set<uint32_t> seen_offsets_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700287 const std::string output_name_;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000288 bool no_bits_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700289};
290
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700291class OatDumperOptions {
292 public:
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100293 OatDumperOptions(bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100294 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700295 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700296 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800297 const char* class_filter,
298 const char* method_filter,
299 bool list_classes,
300 bool list_methods,
David Brazdilc03d7b62016-03-02 12:18:03 +0000301 bool dump_header_only,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800302 const char* export_dex_location,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800303 const char* app_image,
304 const char* app_oat,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800305 uint32_t addr2instr)
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100306 : dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100307 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700308 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700309 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800310 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000311 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800312 list_classes_(list_classes),
313 list_methods_(list_methods),
David Brazdilc03d7b62016-03-02 12:18:03 +0000314 dump_header_only_(dump_header_only),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800315 export_dex_location_(export_dex_location),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800316 app_image_(app_image),
317 app_oat_(app_oat),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800318 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800319 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700320
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700321 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100322 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700323 const bool disassemble_code_;
324 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800325 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000326 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800327 const bool list_classes_;
328 const bool list_methods_;
David Brazdilc03d7b62016-03-02 12:18:03 +0000329 const bool dump_header_only_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800330 const char* const export_dex_location_;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800331 const char* const app_image_;
332 const char* const app_oat_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800333 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700334 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700335};
336
Elliott Hughese3c845c2012-02-28 17:23:01 -0800337class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700338 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100339 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000340 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800341 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700342 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800343 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800344 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
345 disassembler_(Disassembler::Create(instruction_set_,
Andreas Gampe372f3a32016-08-19 10:49:06 -0700346 new DisassemblerOptions(
347 options_.absolute_addresses_,
348 oat_file.Begin(),
349 oat_file.End(),
350 true /* can_read_literals_ */,
351 Is64BitInstructionSet(instruction_set_)
352 ? &Thread::DumpThreadOffset<PointerSize::k64>
353 : &Thread::DumpThreadOffset<PointerSize::k32>))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800354 CHECK(options_.class_loader_ != nullptr);
355 CHECK(options_.class_filter_ != nullptr);
356 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800357 AddAllOffsets();
358 }
359
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700360 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700361 delete disassembler_;
362 }
363
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800364 InstructionSet GetInstructionSet() {
365 return instruction_set_;
366 }
367
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700368 bool Dump(std::ostream& os) {
369 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800370 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700371
372 os << "MAGIC:\n";
373 os << oat_header.GetMagic() << "\n\n";
374
Jeff Hao4b07a6e2016-01-15 12:50:44 -0800375 os << "LOCATION:\n";
376 os << oat_file_.GetLocation() << "\n\n";
377
Brian Carlstromaded5f72011-10-07 17:15:04 -0700378 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800379 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700380
Elliott Hughesa72ec822012-03-05 17:12:22 -0800381 os << "INSTRUCTION SET:\n";
382 os << oat_header.GetInstructionSet() << "\n\n";
383
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700384 {
385 std::unique_ptr<const InstructionSetFeatures> features(
386 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
387 oat_header.GetInstructionSetFeaturesBitmap()));
388 os << "INSTRUCTION SET FEATURES:\n";
389 os << features->GetFeatureString() << "\n\n";
390 }
Dave Allison70202782013-10-22 17:52:19 -0700391
Brian Carlstromaded5f72011-10-07 17:15:04 -0700392 os << "DEX FILE COUNT:\n";
393 os << oat_header.GetDexFileCount() << "\n\n";
394
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800395#define DUMP_OAT_HEADER_OFFSET(label, offset) \
396 os << label " OFFSET:\n"; \
397 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800398 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800399 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
400 } \
401 os << StringPrintf("\n\n");
402
403 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
404 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
405 GetInterpreterToInterpreterBridgeOffset);
406 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
407 GetInterpreterToCompiledCodeBridgeOffset);
408 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
409 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800410 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
411 GetQuickGenericJniTrampolineOffset);
412 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
413 GetQuickImtConflictTrampolineOffset);
414 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
415 GetQuickResolutionTrampolineOffset);
416 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
417 GetQuickToInterpreterBridgeOffset);
418#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700419
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700420 os << "IMAGE PATCH DELTA:\n";
421 os << StringPrintf("%d (0x%08x)\n\n",
422 oat_header.GetImagePatchDelta(),
423 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700424
Brian Carlstrom28db0122012-10-18 16:20:41 -0700425 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
426 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
427
428 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800429 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700430
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700431 // Print the key-value store.
432 {
433 os << "KEY VALUE STORE:\n";
434 size_t index = 0;
435 const char* key;
436 const char* value;
437 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
438 os << key << " = " << value << "\n";
439 index++;
440 }
441 os << "\n";
442 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700443
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800444 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700445 os << "BEGIN:\n";
446 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700447
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700448 os << "END:\n";
449 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
450 }
451
452 os << "SIZE:\n";
453 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700454
455 os << std::flush;
456
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800457 // If set, adjust relative address to be searched
458 if (options_.addr2instr_ != 0) {
459 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
460 os << "SEARCH ADDRESS (executable offset + input):\n";
461 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
462 }
463
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700464 // Dumping the dex file overview is compact enough to do even if header only.
465 DexFileData cumulative;
466 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
467 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
468 CHECK(oat_dex_file != nullptr);
469 std::string error_msg;
470 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
471 if (dex_file == nullptr) {
472 os << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation() << "': "
473 << error_msg;
474 continue;
475 }
476 DexFileData data(*dex_file);
477 os << "Dex file data for " << dex_file->GetLocation() << "\n";
478 data.Dump(os);
479 os << "\n";
480 cumulative.Add(data);
481 }
482 os << "Cumulative dex file data\n";
483 cumulative.Dump(os);
484 os << "\n";
485
David Brazdilc03d7b62016-03-02 12:18:03 +0000486 if (!options_.dump_header_only_) {
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000487 VariableIndentationOutputStream vios(&os);
488 VdexFile::Header vdex_header = oat_file_.GetVdexFile()->GetHeader();
489 if (vdex_header.IsValid()) {
490 std::string error_msg;
491 std::vector<const DexFile*> dex_files;
492 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
493 const DexFile* dex_file = OpenDexFile(oat_dex_files_[i], &error_msg);
494 if (dex_file == nullptr) {
495 os << "Error opening dex file: " << error_msg << std::endl;
496 return false;
497 }
498 dex_files.push_back(dex_file);
499 }
500 verifier::VerifierDeps deps(dex_files, oat_file_.GetVdexFile()->GetVerifierDepsData());
501 deps.Dump(&vios);
502 } else {
503 os << "UNRECOGNIZED vdex file, magic "
504 << vdex_header.GetMagic()
505 << ", version "
506 << vdex_header.GetVersion()
507 << "\n";
508 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000509 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
510 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
511 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800512
David Brazdilc03d7b62016-03-02 12:18:03 +0000513 // If file export selected skip file analysis
514 if (options_.export_dex_location_) {
515 if (!ExportDexFile(os, *oat_dex_file)) {
516 success = false;
517 }
518 } else {
519 if (!DumpOatDexFile(os, *oat_dex_file)) {
520 success = false;
521 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800522 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700523 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700524 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000525
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700526 os << std::flush;
527 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700528 }
529
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800530 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700531 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
532 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800533 return 0; // Address not in oat file
534 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800535 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
536 reinterpret_cast<uintptr_t>(oat_file_.Begin());
537 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800538 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800539 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800540 return end_offset - begin_offset;
541 }
542
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800543 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700544 return oat_file_.GetOatHeader().GetInstructionSet();
545 }
546
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700547 const void* GetQuickOatCode(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800548 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
549 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700550 CHECK(oat_dex_file != nullptr);
551 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700552 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
553 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700554 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
555 << "': " << error_msg;
556 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800557 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700558 const DexFile::ClassDef* class_def =
David Sehr9aa352e2016-09-15 18:13:52 -0700559 OatDexFile::FindClassDef(*dex_file, descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700560 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700561 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100562 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800563 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100564 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800565 }
566 }
567 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700568 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800569 }
570
Brian Carlstromaded5f72011-10-07 17:15:04 -0700571 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800572 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800573 // We don't know the length of the code for each method, but we need to know where to stop
574 // when disassembling. What we do know is that a region of code will be followed by some other
575 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
576 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800577 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
578 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700579 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700580 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700581 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
582 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700583 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
584 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800585 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800586 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800587 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800588 for (size_t class_def_index = 0;
589 class_def_index < dex_file->NumClassDefs();
590 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800591 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100592 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700593 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700594 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800595 ClassDataItemIterator it(*dex_file, class_data);
596 SkipAllFields(it);
597 uint32_t class_method_index = 0;
598 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100599 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800600 it.Next();
601 }
602 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100603 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800604 it.Next();
605 }
606 }
607 }
608 }
609
610 // If the last thing in the file is code for a method, there won't be an offset for the "next"
611 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
612 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800613 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800614 }
615
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700616 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
617 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
618 }
619
Elliott Hughese3c845c2012-02-28 17:23:01 -0800620 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800621 uint32_t code_offset = oat_method.GetCodeOffset();
622 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
623 code_offset &= ~0x1;
624 }
625 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800626 offsets_.insert(oat_method.GetVmapTableOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800627 }
628
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700629 // Dex file data, may be for multiple different dex files.
630 class DexFileData {
631 public:
632 DexFileData() {}
633
634 explicit DexFileData(const DexFile& dex_file)
635 : num_string_ids_(dex_file.NumStringIds()),
636 num_method_ids_(dex_file.NumMethodIds()),
637 num_field_ids_(dex_file.NumFieldIds()),
638 num_type_ids_(dex_file.NumTypeIds()),
639 num_class_defs_(dex_file.NumClassDefs()) {
640 for (size_t class_def_index = 0; class_def_index < num_class_defs_; ++class_def_index) {
641 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
642 WalkClass(dex_file, class_def);
643 }
644 }
645
646 void Add(const DexFileData& other) {
647 AddAll(unique_string_ids_from_code_, other.unique_string_ids_from_code_);
648 num_string_ids_from_code_ += other.num_string_ids_from_code_;
649 AddAll(dex_code_item_ptrs_, other.dex_code_item_ptrs_);
650 dex_code_bytes_ += other.dex_code_bytes_;
651 num_string_ids_ += other.num_string_ids_;
652 num_method_ids_ += other.num_method_ids_;
653 num_field_ids_ += other.num_field_ids_;
654 num_type_ids_ += other.num_type_ids_;
655 num_class_defs_ += other.num_class_defs_;
656 }
657
658 void Dump(std::ostream& os) {
659 os << "Num string ids: " << num_string_ids_ << "\n";
660 os << "Num method ids: " << num_method_ids_ << "\n";
661 os << "Num field ids: " << num_field_ids_ << "\n";
662 os << "Num type ids: " << num_type_ids_ << "\n";
663 os << "Num class defs: " << num_class_defs_ << "\n";
664 os << "Unique strings loaded from dex code: " << unique_string_ids_from_code_.size() << "\n";
665 os << "Total strings loaded from dex code: " << num_string_ids_from_code_ << "\n";
666 os << "Number of unique dex code items: " << dex_code_item_ptrs_.size() << "\n";
667 os << "Total number of dex code bytes: " << dex_code_bytes_ << "\n";
668 }
669
670 private:
671 void WalkClass(const DexFile& dex_file, const DexFile::ClassDef& class_def) {
672 const uint8_t* class_data = dex_file.GetClassData(class_def);
673 if (class_data == nullptr) { // empty class such as a marker interface?
674 return;
675 }
676 ClassDataItemIterator it(dex_file, class_data);
677 SkipAllFields(it);
678 while (it.HasNextDirectMethod()) {
679 WalkCodeItem(dex_file, it.GetMethodCodeItem());
680 it.Next();
681 }
682 while (it.HasNextVirtualMethod()) {
683 WalkCodeItem(dex_file, it.GetMethodCodeItem());
684 it.Next();
685 }
686 DCHECK(!it.HasNext());
687 }
688
689 void WalkCodeItem(const DexFile& dex_file, const DexFile::CodeItem* code_item) {
690 if (code_item == nullptr) {
691 return;
692 }
693 const size_t code_item_size = code_item->insns_size_in_code_units_;
694 const uint16_t* code_ptr = code_item->insns_;
695 const uint16_t* code_end = code_item->insns_ + code_item_size;
696
697 // If we inserted a new dex code item pointer, add to total code bytes.
698 if (dex_code_item_ptrs_.insert(code_ptr).second) {
699 dex_code_bytes_ += code_item_size * sizeof(code_ptr[0]);
700 }
701
702 while (code_ptr < code_end) {
703 const Instruction* inst = Instruction::At(code_ptr);
704 switch (inst->Opcode()) {
705 case Instruction::CONST_STRING: {
706 const uint32_t string_index = inst->VRegB_21c();
707 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
708 ++num_string_ids_from_code_;
709 break;
710 }
711 case Instruction::CONST_STRING_JUMBO: {
712 const uint32_t string_index = inst->VRegB_31c();
713 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
714 ++num_string_ids_from_code_;
715 break;
716 }
717 default:
718 break;
719 }
720
721 code_ptr += inst->SizeInCodeUnits();
722 }
723 }
724
725 // Unique string ids loaded from dex code.
726 std::set<StringReference, StringReferenceComparator> unique_string_ids_from_code_;
727
728 // Total string ids loaded from dex code.
729 size_t num_string_ids_from_code_ = 0;
730
731 // Unique code pointers.
732 std::set<const void*> dex_code_item_ptrs_;
733
734 // Total "unique" dex code bytes.
735 size_t dex_code_bytes_ = 0;
736
737 // Other dex ids.
738 size_t num_string_ids_ = 0;
739 size_t num_method_ids_ = 0;
740 size_t num_field_ids_ = 0;
741 size_t num_type_ids_ = 0;
742 size_t num_class_defs_ = 0;
743 };
744
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700745 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
746 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800747 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700748 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800749 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800750 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700751
Andreas Gampe2ba88952016-04-29 17:52:07 -0700752 const uint8_t* const oat_file_begin = oat_dex_file.GetOatFile()->Begin();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100753 const uint8_t* const vdex_file_begin = oat_dex_file.GetOatFile()->DexBegin();
754
755 // Print data range of the dex file embedded inside the corresponding vdex file.
Andreas Gampe2ba88952016-04-29 17:52:07 -0700756 const uint8_t* const dex_file_pointer = oat_dex_file.GetDexFilePointer();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100757 uint32_t dex_offset = dchecked_integral_cast<uint32_t>(dex_file_pointer - vdex_file_begin);
Andreas Gampe2ba88952016-04-29 17:52:07 -0700758 os << StringPrintf("dex-file: 0x%08x..0x%08x\n",
759 dex_offset,
760 dchecked_integral_cast<uint32_t>(dex_offset + oat_dex_file.FileSize() - 1));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700761
Andreas Gampe2ba88952016-04-29 17:52:07 -0700762 // Create the dex file early. A lot of print-out things depend on it.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700763 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700764 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
765 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700766 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700767 os << std::flush;
768 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700769 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100770
Andreas Gampe2ba88952016-04-29 17:52:07 -0700771 // Print lookup table, if it exists.
772 if (oat_dex_file.GetLookupTableData() != nullptr) {
773 uint32_t table_offset = dchecked_integral_cast<uint32_t>(
774 oat_dex_file.GetLookupTableData() - oat_file_begin);
David Sehr9aa352e2016-09-15 18:13:52 -0700775 uint32_t table_size = TypeLookupTable::RawDataLength(dex_file->NumClassDefs());
Andreas Gampe2ba88952016-04-29 17:52:07 -0700776 os << StringPrintf("type-table: 0x%08x..0x%08x\n",
777 table_offset,
778 table_offset + table_size - 1);
779 }
780
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100781 VariableIndentationOutputStream vios(&os);
782 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800783 for (size_t class_def_index = 0;
784 class_def_index < dex_file->NumClassDefs();
785 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700786 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
787 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800788
789 // TODO: Support regex
790 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
791 continue;
792 }
793
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700794 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100795 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700796 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
797 class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100798 << " (" << oat_class.GetStatus() << ")"
799 << " (" << oat_class.GetType() << ")\n";
800 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700801 if (options_.list_classes_) {
802 continue;
803 }
804 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700805 success = false;
806 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800807 if (stop_analysis) {
808 os << std::flush;
809 return success;
810 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700811 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700812 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700813 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700814 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700815 }
816
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800817 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
818 std::string error_msg;
819 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
820
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700821 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800822 if (dex_file == nullptr) {
823 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
824 return false;
825 }
826 size_t fsize = oat_dex_file.FileSize();
827
828 // Some quick checks just in case
829 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
830 os << "Invalid dex file\n";
831 return false;
832 }
833
834 // Verify output directory exists
835 if (!OS::DirectoryExists(options_.export_dex_location_)) {
836 // TODO: Extend OS::DirectoryExists if symlink support is required
837 os << options_.export_dex_location_ << " output directory not found or symlink\n";
838 return false;
839 }
840
841 // Beautify path names
842 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
843 return false;
844 }
845
846 std::string dex_orig_name;
847 size_t dex_orig_pos = dex_file_location.rfind('/');
848 if (dex_orig_pos == std::string::npos)
849 dex_orig_name = dex_file_location;
850 else
851 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
852
853 // A more elegant approach to efficiently name user installed apps is welcome
854 if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
855 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
856 size_t apk_orig_pos = dex_file_location.rfind('/');
857 if (apk_orig_pos != std::string::npos) {
858 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
859 }
860 }
861
862 std::string out_dex_path(options_.export_dex_location_);
863 if (out_dex_path.back() != '/') {
864 out_dex_path.append("/");
865 }
866 out_dex_path.append(dex_orig_name);
867 out_dex_path.append("_export.dex");
868 if (out_dex_path.length() > PATH_MAX) {
869 return false;
870 }
871
872 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
873 if (file.get() == nullptr) {
874 os << "Failed to open output dex file " << out_dex_path;
875 return false;
876 }
877
878 if (!file->WriteFully(dex_file->Begin(), fsize)) {
879 os << "Failed to write dex file";
880 file->Erase();
881 return false;
882 }
883
884 if (file->FlushCloseOrErase() != 0) {
885 os << "Flush and close failed";
886 return false;
887 }
888
889 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
890 os << std::flush;
891
892 return true;
893 }
894
Elliott Hughese3c845c2012-02-28 17:23:01 -0800895 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700896 while (it.HasNextStaticField()) {
897 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700898 }
Ian Rogers0571d352011-11-03 19:51:38 -0700899 while (it.HasNextInstanceField()) {
900 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700901 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800902 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700903
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100904 bool DumpOatClass(VariableIndentationOutputStream* vios,
905 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700906 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700907 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800908 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -0700909 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700910 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100911 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700912 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800913 }
914 ClassDataItemIterator it(dex_file, class_data);
915 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700916 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700917 while (it.HasNextDirectMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100918 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700919 it.GetMemberIndex(), it.GetMethodCodeItem(),
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700920 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700921 success = false;
922 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800923 if (addr_found) {
924 *stop_analysis = true;
925 return success;
926 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700927 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700928 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700929 }
Ian Rogers0571d352011-11-03 19:51:38 -0700930 while (it.HasNextVirtualMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100931 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700932 it.GetMemberIndex(), it.GetMethodCodeItem(),
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700933 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700934 success = false;
935 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800936 if (addr_found) {
937 *stop_analysis = true;
938 return success;
939 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700940 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700941 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700942 }
Ian Rogers0571d352011-11-03 19:51:38 -0700943 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100944 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700945 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700946 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800947
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700948 static constexpr uint32_t kPrologueBytes = 16;
949
950 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
951 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
952
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100953 bool DumpOatMethod(VariableIndentationOutputStream* vios,
954 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700955 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700956 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800957 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700958 uint32_t method_access_flags, bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700959 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800960
961 // TODO: Support regex
962 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
963 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000964 return success;
965 }
966
David Sehr709b0702016-10-13 09:12:37 -0700967 std::string pretty_method = dex_file.PrettyMethod(dex_method_idx, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100968 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
969 class_method_index, pretty_method.c_str(),
970 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800971 if (options_.list_methods_) return success;
972
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800973 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
974 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
975 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
976 uint32_t code_offset = oat_method.GetCodeOffset();
977 uint32_t code_size = oat_method.GetQuickCodeSize();
978 if (resolved_addr2instr_ != 0) {
979 if (resolved_addr2instr_ > code_offset + code_size) {
980 return success;
981 } else {
982 *addr_found = true; // stop analyzing file at next iteration
983 }
984 }
985
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100986 // Everything below is indented at least once.
987 ScopedIndentation indent1(vios);
988
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800989 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100990 vios->Stream() << "DEX CODE:\n";
991 ScopedIndentation indent2(vios);
992 DumpDexCode(vios->Stream(), dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700993 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700994
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700995 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700996 std::unique_ptr<verifier::MethodVerifier> verifier;
997 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700998 // We need to have the handle scope stay live until after the verifier since the verifier has
999 // a handle to the dex cache from hs.
1000 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001001 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
1002 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001003 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001004 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001005 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -07001006 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001007 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001008 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001009 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001010 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001011 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001012 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001013 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001014 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001015 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
1016 oat_method_offsets_offset, oat_file_.Size());
1017 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001018 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001019 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001020 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001021
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001022 ScopedIndentation indent2(vios);
1023 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001024 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
1025 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001026 vios->Stream() << StringPrintf("WARNING: "
1027 "code offset 0x%08x is past end of file 0x%08zx.\n",
1028 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001029 success = false;
1030 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001031 vios->Stream() << "\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001032 }
1033 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001034 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001035 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
1036 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -07001037
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001038 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001039 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001040 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001041 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001042 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001043 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001044 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
1045 method_header_offset, oat_file_.Size());
1046 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001047 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001048 return false;
1049 }
1050
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001051 ScopedIndentation indent2(vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001052 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001053 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001054 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001055 }
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001056 uint32_t vmap_table_offset = method_header == nullptr ? 0 : method_header->vmap_table_offset_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001057 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001058
1059 size_t vmap_table_offset_limit =
1060 (kIsVdexEnabled && IsMethodGeneratedByDexToDexCompiler(oat_method, code_item))
1061 ? oat_file_.GetVdexFile()->Size()
1062 : method_header->GetCode() - oat_file_.Begin();
1063 if (vmap_table_offset >= vmap_table_offset_limit) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001064 vios->Stream() << StringPrintf("WARNING: "
1065 "vmap table offset 0x%08x is past end of file 0x%08zx. "
1066 "vmap table offset was loaded from offset 0x%08x.\n",
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001067 vmap_table_offset,
1068 vmap_table_offset_limit,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001069 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001070 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001071 } else if (options_.dump_vmap_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001072 DumpVmapData(vios, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001073 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001074 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001075 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001076 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001077
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001078 ScopedIndentation indent2(vios);
1079 vios->Stream()
1080 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
1081 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
1082 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
1083 vios->Stream() << "\n";
1084 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
1085 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
1086 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001087 }
1088 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001089 // Based on spill masks from QuickMethodFrameInfo so placed
1090 // after it is dumped, but useful for understanding quick
1091 // code, so dumped here.
1092 ScopedIndentation indent2(vios);
1093 DumpVregLocations(vios->Stream(), oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001094 }
1095 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001096 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001097 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
1098 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001099 ScopedIndentation indent2(vios);
1100 vios->Stream() << StringPrintf("WARNING: "
1101 "code size offset 0x%08x is past end of file 0x%08zx.",
1102 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001103 success = false;
1104 } else {
1105 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001106 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1107 uint64_t aligned_code_end = aligned_code_begin + code_size;
1108
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001109 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001110 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001111 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001112 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
1113 code_offset,
1114 code_size_offset,
1115 code_size,
1116 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001117
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001118 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001119 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001120 vios->Stream() << StringPrintf("WARNING: "
1121 "start of code at 0x%08x is past end of file 0x%08zx.",
1122 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001123 success = false;
1124 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001125 vios->Stream() << StringPrintf(
1126 "WARNING: "
1127 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
1128 "code size is 0x%08x loaded from offset 0x%08x.\n",
1129 aligned_code_end, oat_file_.Size(),
1130 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001131 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001132 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001133 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001134 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001135 }
1136 }
1137 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001138 vios->Stream() << StringPrintf(
1139 "WARNING: "
1140 "code size %d is bigger than max expected threshold of %d. "
1141 "code size is 0x%08x loaded from offset 0x%08x.\n",
1142 code_size, kMaxCodeSize,
1143 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001144 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001145 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001146 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001147 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001148 }
1149 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001150 } else if (options_.disassemble_code_) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001151 DumpCode(vios, oat_method, code_item, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001152 }
1153 }
1154 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001155 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001156 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001157 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001158
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001159 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1160 if (spill_mask == 0) {
1161 return;
1162 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001163 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001164 for (size_t i = 0; i < 32; i++) {
1165 if ((spill_mask & (1 << i)) != 0) {
1166 if (is_float) {
1167 os << "fr" << i;
1168 } else {
1169 os << "r" << i;
1170 }
1171 spill_mask ^= 1 << i; // clear bit
1172 if (spill_mask != 0) {
1173 os << ", ";
1174 } else {
1175 break;
1176 }
1177 }
1178 }
1179 os << ")";
1180 }
1181
Roland Levillain442b46a2015-02-18 16:54:21 +00001182 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001183 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001184 const OatFile::OatMethod& oat_method,
1185 const DexFile::CodeItem* code_item) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001186 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1187 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001188 const void* raw_code_info = oat_method.GetVmapTable();
1189 if (raw_code_info != nullptr) {
1190 CodeInfo code_info(raw_code_info);
1191 DCHECK(code_item != nullptr);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001192 ScopedIndentation indent1(vios);
1193 DumpCodeInfo(vios, code_info, oat_method, *code_item);
Roland Levillain442b46a2015-02-18 16:54:21 +00001194 }
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001195 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
1196 // We don't encode the size in the table, so just emit that we have quickened
1197 // information.
1198 ScopedIndentation indent(vios);
1199 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001200 } else {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001201 // Otherwise, there is nothing to display.
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001202 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001203 }
1204
1205 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001206 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001207 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001208 const OatFile::OatMethod& oat_method,
Roland Levillain442b46a2015-02-18 16:54:21 +00001209 const DexFile::CodeItem& code_item) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001210 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001211 oat_method.GetCodeOffset(),
1212 code_item.registers_size_,
1213 options_.dump_code_info_stack_maps_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001214 }
1215
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001216 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1217 const DexFile::CodeItem* code_item) {
1218 if (code_item != nullptr) {
1219 size_t num_locals_ins = code_item->registers_size_;
1220 size_t num_ins = code_item->ins_size_;
1221 size_t num_locals = num_locals_ins - num_ins;
1222 size_t num_outs = code_item->outs_size_;
1223
1224 os << "vr_stack_locations:";
1225 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1226 // For readability, delimit the different kinds of VRs.
1227 if (reg == num_locals_ins) {
1228 os << "\n\tmethod*:";
1229 } else if (reg == num_locals && num_ins > 0) {
1230 os << "\n\tins:";
1231 } else if (reg == 0 && num_locals > 0) {
1232 os << "\n\tlocals:";
1233 }
1234
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001235 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1236 code_item,
1237 oat_method.GetCoreSpillMask(),
1238 oat_method.GetFpSpillMask(),
1239 oat_method.GetFrameSizeInBytes(),
1240 reg,
1241 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001242 os << " v" << reg << "[sp + #" << offset << "]";
1243 }
1244
1245 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1246 if (out_reg == 0) {
1247 os << "\n\touts:";
1248 }
1249
1250 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1251 os << " v" << out_reg << "[sp + #" << offset << "]";
1252 }
1253
1254 os << "\n";
1255 }
1256 }
1257
Ian Rogersb23a7722012-10-09 16:54:26 -07001258 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001259 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001260 size_t i = 0;
1261 while (i < code_item->insns_size_in_code_units_) {
1262 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001263 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1264 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001265 i += instruction->SizeInCodeUnits();
1266 }
1267 }
1268 }
1269
Roland Levillainf2650d12015-05-28 14:53:28 +01001270 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1271 // the optimizing compiler?
1272 static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1273 const DexFile::CodeItem* code_item) {
1274 // If the native GC map is null and the Dex `code_item` is not
1275 // null, then this method has been compiled with the optimizing
1276 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001277 return oat_method.GetQuickCode() != nullptr &&
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001278 oat_method.GetVmapTable() != nullptr &&
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001279 code_item != nullptr;
1280 }
1281
1282 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1283 // the dextodex compiler?
1284 static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
1285 const DexFile::CodeItem* code_item) {
1286 // If the quick code is null, the Dex `code_item` is not
1287 // null, and the vmap table is not null, then this method has been compiled
1288 // with the dextodex compiler.
1289 return oat_method.GetQuickCode() == nullptr &&
1290 oat_method.GetVmapTable() != nullptr &&
1291 code_item != nullptr;
Roland Levillainf2650d12015-05-28 14:53:28 +01001292 }
1293
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001294 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001295 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001296 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001297 const DexFile* dex_file,
1298 const DexFile::ClassDef& class_def,
1299 const DexFile::CodeItem* code_item,
1300 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001301 if ((method_access_flags & kAccNative) == 0) {
1302 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001303 Runtime* const runtime = Runtime::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001304 Handle<mirror::DexCache> dex_cache(
Mathieu Chartierf284d442016-06-02 11:48:30 -07001305 hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file, nullptr)));
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001306 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001307 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001308 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
David Brazdil15fc7292016-09-02 14:13:18 +01001309 class_def, code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001310 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001311
1312 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001313 }
1314
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001315 // The StackMapsHelper provides the stack maps in the native PC order.
1316 // For identical native PCs, the order from the CodeInfo is preserved.
1317 class StackMapsHelper {
1318 public:
1319 explicit StackMapsHelper(const uint8_t* raw_code_info)
1320 : code_info_(raw_code_info),
1321 encoding_(code_info_.ExtractEncoding()),
1322 number_of_stack_maps_(code_info_.GetNumberOfStackMaps(encoding_)),
1323 indexes_(),
1324 offset_(static_cast<size_t>(-1)),
1325 stack_map_index_(0u) {
1326 if (number_of_stack_maps_ != 0u) {
1327 // Check if native PCs are ordered.
1328 bool ordered = true;
1329 StackMap last = code_info_.GetStackMapAt(0u, encoding_);
1330 for (size_t i = 1; i != number_of_stack_maps_; ++i) {
1331 StackMap current = code_info_.GetStackMapAt(i, encoding_);
1332 if (last.GetNativePcOffset(encoding_.stack_map_encoding) >
1333 current.GetNativePcOffset(encoding_.stack_map_encoding)) {
1334 ordered = false;
1335 break;
1336 }
1337 last = current;
1338 }
1339 if (!ordered) {
1340 // Create indirection indexes for access in native PC order. We do not optimize
1341 // for the fact that there can currently be only two separately ordered ranges,
1342 // namely normal stack maps and catch-point stack maps.
1343 indexes_.resize(number_of_stack_maps_);
1344 std::iota(indexes_.begin(), indexes_.end(), 0u);
1345 std::sort(indexes_.begin(),
1346 indexes_.end(),
1347 [this](size_t lhs, size_t rhs) {
1348 StackMap left = code_info_.GetStackMapAt(lhs, encoding_);
1349 uint32_t left_pc = left.GetNativePcOffset(encoding_.stack_map_encoding);
1350 StackMap right = code_info_.GetStackMapAt(rhs, encoding_);
1351 uint32_t right_pc = right.GetNativePcOffset(encoding_.stack_map_encoding);
1352 // If the PCs are the same, compare indexes to preserve the original order.
1353 return (left_pc < right_pc) || (left_pc == right_pc && lhs < rhs);
1354 });
1355 }
1356 offset_ = GetStackMapAt(0).GetNativePcOffset(encoding_.stack_map_encoding);
1357 }
1358 }
1359
1360 const CodeInfo& GetCodeInfo() const {
1361 return code_info_;
1362 }
1363
1364 const CodeInfoEncoding& GetEncoding() const {
1365 return encoding_;
1366 }
1367
1368 size_t GetOffset() const {
1369 return offset_;
1370 }
1371
1372 StackMap GetStackMap() const {
1373 return GetStackMapAt(stack_map_index_);
1374 }
1375
1376 void Next() {
1377 ++stack_map_index_;
1378 offset_ = (stack_map_index_ == number_of_stack_maps_)
1379 ? static_cast<size_t>(-1)
1380 : GetStackMapAt(stack_map_index_).GetNativePcOffset(encoding_.stack_map_encoding);
1381 }
1382
1383 private:
1384 StackMap GetStackMapAt(size_t i) const {
1385 if (!indexes_.empty()) {
1386 i = indexes_[i];
1387 }
1388 DCHECK_LT(i, number_of_stack_maps_);
1389 return code_info_.GetStackMapAt(i, encoding_);
1390 }
1391
1392 const CodeInfo code_info_;
1393 const CodeInfoEncoding encoding_;
1394 const size_t number_of_stack_maps_;
1395 dchecked_vector<size_t> indexes_; // Used if stack map native PCs are not ordered.
1396 size_t offset_;
1397 size_t stack_map_index_;
1398 };
1399
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001400 void DumpCode(VariableIndentationOutputStream* vios,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001401 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1402 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001403 const void* quick_code = oat_method.GetQuickCode();
1404
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001405 if (code_size == 0) {
1406 code_size = oat_method.GetQuickCodeSize();
1407 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001408 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001409 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001410 return;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001411 } else if (!bad_input && IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1412 // The optimizing compiler outputs its CodeInfo data in the vmap table.
1413 StackMapsHelper helper(oat_method.GetVmapTable());
1414 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1415 size_t offset = 0;
1416 while (offset < code_size) {
1417 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
1418 if (offset == helper.GetOffset()) {
1419 ScopedIndentation indent1(vios);
1420 StackMap stack_map = helper.GetStackMap();
1421 DCHECK(stack_map.IsValid());
1422 stack_map.Dump(vios,
1423 helper.GetCodeInfo(),
1424 helper.GetEncoding(),
1425 oat_method.GetCodeOffset(),
1426 code_item->registers_size_);
1427 do {
1428 helper.Next();
1429 // There may be multiple stack maps at a given PC. We display only the first one.
1430 } while (offset == helper.GetOffset());
1431 }
1432 DCHECK_LT(offset, helper.GetOffset());
1433 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001434 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001435 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1436 size_t offset = 0;
1437 while (offset < code_size) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001438 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001439 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001440 }
1441 }
1442
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001443 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001444 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001445 const OatDumperOptions& options_;
1446 uint32_t resolved_addr2instr_;
Andreas Gampe372f3a32016-08-19 10:49:06 -07001447 const InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001448 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001449 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001450};
1451
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001452class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001453 public:
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001454 ImageDumper(std::ostream* os,
1455 gc::space::ImageSpace& image_space,
1456 const ImageHeader& image_header,
1457 OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001458 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001459 vios_(os),
1460 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001461 image_space_(image_space),
1462 image_header_(image_header),
1463 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001464
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001465 bool Dump() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001466 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001467 std::ostream& indent_os = vios_.Stream();
1468
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001469 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001470
Jeff Haodcdc85b2015-12-04 14:06:18 -08001471 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1472
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001473 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001474
Mathieu Chartiere401d142015-04-22 13:56:20 -07001475 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1476
1477 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1478 auto section = static_cast<ImageHeader::ImageSections>(i);
1479 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1480 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001481
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001482 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001483
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001484 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001485
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001486 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1487
1488 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1489
1490 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001491
Alex Lighta59dd802014-07-02 16:28:08 -07001492 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1493
Igor Murashkin46774762014-10-22 11:37:02 -07001494 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1495
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001496 {
1497 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001498 static_assert(arraysize(image_roots_descriptions_) ==
1499 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001500 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1501 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1502 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001503 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001504 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001505 if (image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001506 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001507 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001508 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001509 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1510 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001511 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001512 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1513 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001514 run++;
1515 } else {
1516 break;
1517 }
1518 }
1519 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001520 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001521 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001522 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001523 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001524 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001525 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001526 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001527 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001528 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001529 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001530 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001531 }
1532 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001533 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001534
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001535 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001536 os << "METHOD ROOTS\n";
1537 static_assert(arraysize(image_methods_descriptions_) ==
1538 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1539 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1540 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1541 const char* description = image_methods_descriptions_[i];
1542 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001543 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001544 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001545 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001546 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001547
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001548 Runtime* const runtime = Runtime::Current();
1549 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001550 std::string image_filename = image_space_.GetImageFilename();
1551 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001552 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001553 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001554 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001555 const OatFile* oat_file = image_space_.GetOatFile();
Alex Lighta59dd802014-07-02 16:28:08 -07001556 if (oat_file == nullptr) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001557 oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location);
1558 }
1559 if (oat_file == nullptr) {
1560 oat_file = OatFile::Open(oat_location,
1561 oat_location,
1562 nullptr,
1563 nullptr,
1564 false,
1565 /*low_4gb*/false,
1566 nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001567 &error_msg);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001568 }
1569 if (oat_file == nullptr) {
1570 os << "OAT FILE NOT FOUND: " << error_msg << "\n";
1571 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001572 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001573 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001574
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001575 stats_.oat_file_bytes = oat_file->Size();
1576
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001577 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001578
Mathieu Chartier02e25112013-08-14 16:14:24 -07001579 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001580 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001581 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1582 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001583 }
1584
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001585 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001586
Jeff Haodcdc85b2015-12-04 14:06:18 -08001587 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001588 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001589 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001590 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001591 {
1592 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1593 heap->FlushAllocStack();
1594 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001595 // Since FlushAllocStack() above resets the (active) allocation
1596 // stack. Need to revoke the thread-local allocation stacks that
1597 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001598 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001599 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001600 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001601 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001602 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001603 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001604 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001605 {
1606 ReaderMutexLock mu(self, *class_linker->DexLock());
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001607 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001608 ObjPtr<mirror::DexCache> dex_cache =
1609 ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001610 if (dex_cache != nullptr) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001611 dex_caches_.insert(dex_cache.Ptr());
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001612 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001613 }
1614 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001615 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001616 // Dump the normal objects before ArtMethods.
1617 image_space_.GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1618 indent_os << "\n";
1619 // TODO: Dump fields.
1620 // Dump methods after.
Jeff Haodcdc85b2015-12-04 14:06:18 -08001621 DumpArtMethodVisitor visitor(this);
Mathieu Chartiere42888f2016-04-14 10:49:19 -07001622 image_header_.VisitPackedArtMethods(&visitor,
1623 image_space_.Begin(),
1624 image_header_.GetPointerSize());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001625 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001626 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001627 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001628 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001629 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001630 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001631 size_t data_size = image_header_.GetDataSize(); // stored size in file.
1632 if (file == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001633 LOG(WARNING) << "Failed to find image in " << image_filename;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001634 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001635 stats_.file_bytes = file->GetLength();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001636 // If the image is compressed, adjust to decompressed size.
1637 size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader);
1638 if (image_header_.GetStorageMode() == ImageHeader::kStorageModeUncompressed) {
1639 DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image";
1640 }
1641 stats_.file_bytes += uncompressed_size - data_size;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001642 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001643 size_t header_bytes = sizeof(ImageHeader);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001644 const auto& object_section = image_header_.GetImageSection(ImageHeader::kSectionObjects);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001645 const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1646 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001647 const auto& dex_cache_arrays_section = image_header_.GetImageSection(
1648 ImageHeader::kSectionDexCacheArrays);
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001649 const auto& intern_section = image_header_.GetImageSection(
1650 ImageHeader::kSectionInternedStrings);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001651 const auto& class_table_section = image_header_.GetImageSection(
1652 ImageHeader::kSectionClassTable);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001653 const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1654
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001655 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001656
1657 // Objects are kObjectAlignment-aligned.
1658 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1659 if (object_section.Offset() > header_bytes) {
1660 stats_.alignment_bytes += object_section.Offset() - header_bytes;
1661 }
1662
1663 // Field section is 4-byte aligned.
1664 constexpr size_t kFieldSectionAlignment = 4U;
1665 uint32_t end_objects = object_section.Offset() + object_section.Size();
1666 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
1667 stats_.alignment_bytes += field_section.Offset() - end_objects;
1668
1669 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
1670 uint32_t end_fields = field_section.Offset() + field_section.Size();
1671 CHECK_ALIGNED(method_section.Offset(), 4);
1672 stats_.alignment_bytes += method_section.Offset() - end_fields;
1673
1674 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
1675 uint32_t end_methods = method_section.Offset() + method_section.Size();
1676 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
1677 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
1678
1679 // Intern table is 8-byte aligned.
1680 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
1681 CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
1682 stats_.alignment_bytes += intern_section.Offset() - end_caches;
1683
1684 // Add space between intern table and class table.
1685 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
1686 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
1687
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001688 // Add space between end of image data and bitmap. Expect the bitmap to be page-aligned.
1689 const size_t bitmap_offset = sizeof(ImageHeader) + data_size;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001690 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001691 stats_.alignment_bytes += RoundUp(bitmap_offset, kPageSize) - bitmap_offset;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001692
Mathieu Chartiere401d142015-04-22 13:56:20 -07001693 stats_.bitmap_bytes += bitmap_section.Size();
1694 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01001695 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01001696 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001697 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001698 stats_.class_table_bytes += class_table_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001699 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001700 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001701
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001702 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001703
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001704 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001705 }
1706
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001707 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001708 class DumpArtMethodVisitor : public ArtMethodVisitor {
1709 public:
1710 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
1711
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001712 virtual void Visit(ArtMethod* method) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001713 std::ostream& indent_os = image_dumper_->vios_.Stream();
David Sehr709b0702016-10-13 09:12:37 -07001714 indent_os << method << " " << " ArtMethod: " << ArtMethod::PrettyMethod(method) << "\n";
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001715 image_dumper_->DumpMethod(method, indent_os);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001716 indent_os << "\n";
1717 }
1718
1719 private:
1720 ImageDumper* const image_dumper_;
1721 };
1722
Mathieu Chartier3398c782016-09-30 10:27:43 -07001723 static void PrettyObjectValue(std::ostream& os,
1724 ObjPtr<mirror::Class> type,
1725 ObjPtr<mirror::Object> value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001726 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001727 CHECK(type != nullptr);
1728 if (value == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07001729 os << StringPrintf("null %s\n", type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001730 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001731 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001732 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001733 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001734 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001735 mirror::Class* klass = value->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07001736 os << StringPrintf("%p Class: %s\n", klass, mirror::Class::PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001737 } else {
David Sehr709b0702016-10-13 09:12:37 -07001738 os << StringPrintf("%p %s\n", value.Ptr(), type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001739 }
1740 }
1741
Mathieu Chartier3398c782016-09-30 10:27:43 -07001742 static void PrintField(std::ostream& os, ArtField* field, ObjPtr<mirror::Object> obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001743 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001744 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08001745 switch (field->GetTypeAsPrimitiveType()) {
1746 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08001747 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001748 break;
1749 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001750 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001751 break;
1752 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001753 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001754 break;
1755 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001756 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001757 break;
1758 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07001759 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001760 break;
1761 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07001762 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001763 break;
1764 case Primitive::kPrimBoolean:
Fred Shih37f05ef2014-07-16 18:38:08 -07001765 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1766 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001767 break;
1768 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07001769 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001770 break;
1771 case Primitive::kPrimNot: {
1772 // Get the value, don't compute the type unless it is non-null as we don't want
1773 // to cause class loading.
Mathieu Chartier3398c782016-09-30 10:27:43 -07001774 ObjPtr<mirror::Object> value = field->GetObj(obj);
Ian Rogers08f1f502014-12-02 15:04:37 -08001775 if (value == nullptr) {
1776 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07001777 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08001778 // Grab the field type without causing resolution.
Mathieu Chartier3398c782016-09-30 10:27:43 -07001779 ObjPtr<mirror::Class> field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08001780 if (field_type != nullptr) {
1781 PrettyObjectValue(os, field_type, value);
1782 } else {
Mathieu Chartier3398c782016-09-30 10:27:43 -07001783 os << StringPrintf("%p %s\n",
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07001784 value.Ptr(),
Ian Rogers08f1f502014-12-02 15:04:37 -08001785 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
1786 }
Ian Rogers50239c72013-06-17 14:53:22 -07001787 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001788 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07001789 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001790 default:
1791 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
1792 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08001793 }
1794 }
1795
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001796 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001797 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001798 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001799 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001800 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001801 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001802 for (ArtField& field : klass->GetIFields()) {
1803 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001804 }
1805 }
1806
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001807 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001808 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001809 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001810
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001811 const void* GetQuickOatCodeBegin(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001812 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001813 image_header_.GetPointerSize());
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001814 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001815 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001816 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001817 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001818 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001819 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001820 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001821 }
1822
Mathieu Chartiere401d142015-04-22 13:56:20 -07001823 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001824 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001825 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1826 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001827 return 0;
1828 }
1829 return oat_code_begin[-1];
1830 }
1831
Mathieu Chartiere401d142015-04-22 13:56:20 -07001832 const void* GetQuickOatCodeEnd(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001833 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001834 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001835 if (oat_code_begin == nullptr) {
1836 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001837 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001838 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001839 }
1840
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001841 static void Callback(mirror::Object* obj, void* arg) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001842 DCHECK(obj != nullptr);
1843 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001844 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001845 if (!state->InDumpSpace(obj)) {
1846 return;
1847 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001848
1849 size_t object_bytes = obj->SizeOf();
1850 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1851 state->stats_.object_bytes += object_bytes;
1852 state->stats_.alignment_bytes += alignment_bytes;
1853
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001854 std::ostream& os = state->vios_.Stream();
1855
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001856 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001857 if (obj_class->IsArrayClass()) {
David Sehr709b0702016-10-13 09:12:37 -07001858 os << StringPrintf("%p: %s length:%d\n", obj, obj_class->PrettyDescriptor().c_str(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001859 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001860 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001861 mirror::Class* klass = obj->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07001862 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj,
1863 mirror::Class::PrettyDescriptor(klass).c_str())
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001864 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08001865 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001866 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001867 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001868 } else {
David Sehr709b0702016-10-13 09:12:37 -07001869 os << StringPrintf("%p: %s\n", obj, obj_class->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001870 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001871 ScopedIndentation indent1(&state->vios_);
1872 DumpFields(os, obj, obj_class);
Andreas Gampe542451c2016-07-26 09:02:02 -07001873 const PointerSize image_pointer_size = state->image_header_.GetPointerSize();
Ian Rogersd5b32602012-02-26 16:40:04 -08001874 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001875 auto* obj_array = obj->AsObjectArray<mirror::Object>();
1876 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001877 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001878 size_t run = 0;
1879 for (int32_t j = i + 1; j < length; j++) {
1880 if (value == obj_array->Get(j)) {
1881 run++;
1882 } else {
1883 break;
1884 }
1885 }
1886 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001887 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001888 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001889 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001890 i = i + run;
1891 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001892 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001893 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001894 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001895 }
1896 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001897 mirror::Class* klass = obj->AsClass();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001898 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001899 os << "STATICS:\n";
1900 ScopedIndentation indent2(&state->vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001901 for (ArtField& field : klass->GetSFields()) {
1902 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001903 }
1904 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001905 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01001906 auto it = state->dex_caches_.find(obj);
1907 if (it != state->dex_caches_.end()) {
1908 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001909 const auto& field_section = state->image_header_.GetImageSection(
1910 ImageHeader::kSectionArtFields);
1911 const auto& method_section = state->image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001912 size_t num_methods = dex_cache->NumResolvedMethods();
1913 if (num_methods != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001914 os << "Methods (size=" << num_methods << "):\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01001915 ScopedIndentation indent2(&state->vios_);
1916 auto* resolved_methods = dex_cache->GetResolvedMethods();
1917 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001918 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_methods,
1919 i,
1920 image_pointer_size);
Vladimir Marko05792b92015-08-03 11:56:49 +01001921 size_t run = 0;
1922 for (size_t j = i + 1;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001923 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_methods,
1924 j,
1925 image_pointer_size);
1926 ++j) {
1927 ++run;
1928 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001929 if (run == 0) {
1930 os << StringPrintf("%zd: ", i);
1931 } else {
1932 os << StringPrintf("%zd to %zd: ", i, i + run);
1933 i = i + run;
1934 }
1935 std::string msg;
1936 if (elem == nullptr) {
1937 msg = "null";
1938 } else if (method_section.Contains(
1939 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07001940 msg = reinterpret_cast<ArtMethod*>(elem)->PrettyMethod();
Vladimir Marko05792b92015-08-03 11:56:49 +01001941 } else {
1942 msg = "<not in method section>";
1943 }
1944 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07001945 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001946 }
1947 size_t num_fields = dex_cache->NumResolvedFields();
1948 if (num_fields != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001949 os << "Fields (size=" << num_fields << "):\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01001950 ScopedIndentation indent2(&state->vios_);
1951 auto* resolved_fields = dex_cache->GetResolvedFields();
1952 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001953 auto* elem = mirror::DexCache::GetElementPtrSize(
1954 resolved_fields, i, image_pointer_size);
Vladimir Marko05792b92015-08-03 11:56:49 +01001955 size_t run = 0;
1956 for (size_t j = i + 1;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001957 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_fields,
1958 j,
1959 image_pointer_size);
1960 ++j) {
1961 ++run;
1962 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001963 if (run == 0) {
1964 os << StringPrintf("%zd: ", i);
1965 } else {
1966 os << StringPrintf("%zd to %zd: ", i, i + run);
1967 i = i + run;
1968 }
1969 std::string msg;
1970 if (elem == nullptr) {
1971 msg = "null";
1972 } else if (field_section.Contains(
1973 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07001974 msg = reinterpret_cast<ArtField*>(elem)->PrettyField();
Vladimir Marko05792b92015-08-03 11:56:49 +01001975 } else {
1976 msg = "<not in field section>";
1977 }
1978 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07001979 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001980 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001981 size_t num_types = dex_cache->NumResolvedTypes();
1982 if (num_types != 0u) {
1983 os << "Types (size=" << num_types << "):\n";
1984 ScopedIndentation indent2(&state->vios_);
1985 auto* resolved_types = dex_cache->GetResolvedTypes();
1986 for (size_t i = 0; i < num_types; ++i) {
1987 auto* elem = resolved_types[i].Read();
1988 size_t run = 0;
1989 for (size_t j = i + 1; j != num_types && elem == resolved_types[j].Read(); ++j) {
1990 ++run;
1991 }
1992 if (run == 0) {
1993 os << StringPrintf("%zd: ", i);
1994 } else {
1995 os << StringPrintf("%zd to %zd: ", i, i + run);
1996 i = i + run;
1997 }
1998 std::string msg;
1999 if (elem == nullptr) {
2000 msg = "null";
2001 } else {
David Sehr709b0702016-10-13 09:12:37 -07002002 msg = elem->PrettyClass();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002003 }
2004 os << StringPrintf("%p %s\n", elem, msg.c_str());
2005 }
2006 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002007 }
2008 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002009 std::string temp;
2010 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002011 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002012
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002013 void DumpMethod(ArtMethod* method, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002014 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002015 DCHECK(method != nullptr);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002016 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
2017 const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
Andreas Gampe542451c2016-07-26 09:02:02 -07002018 const PointerSize pointer_size = image_header_.GetPointerSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002019 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2020 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002021 if (method->IsNative()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002022 bool first_occurrence;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002023 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2024 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002025 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002026 stats_.native_to_managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002027 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002028 if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
2029 image_header_.GetPointerSize())) {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002030 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002031 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002032 } else if (method->IsAbstract() || method->IsClassInitializer()) {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002033 // Don't print information for these.
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002034 } else if (method->IsRuntimeMethod()) {
2035 ImtConflictTable* table = method->GetImtConflictTable(image_header_.GetPointerSize());
2036 if (table != nullptr) {
2037 indent_os << "IMT conflict table " << table << " method: ";
2038 for (size_t i = 0, count = table->NumEntries(pointer_size); i < count; ++i) {
David Sehr709b0702016-10-13 09:12:37 -07002039 indent_os << ArtMethod::PrettyMethod(table->GetImplementationMethod(i, pointer_size))
2040 << " ";
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002041 }
2042 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002043 } else {
2044 const DexFile::CodeItem* code_item = method->GetCodeItem();
2045 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002046 stats_.dex_instruction_bytes += dex_instruction_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002047
2048 bool first_occurrence;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002049 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002050 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01002051 // Method compiled with the optimizing compiler have no vmap table.
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002052 vmap_table_bytes = ComputeOatSize(method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002053 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002054 stats_.vmap_table_bytes += vmap_table_bytes;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002055 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002056 }
2057
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002058 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2059 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002060 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002061 stats_.managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002062 if (method->IsConstructor()) {
2063 if (method->IsStatic()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002064 stats_.class_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002065 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002066 stats_.large_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002067 }
2068 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002069 stats_.large_method_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002070 }
2071 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002072 stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002073
Igor Murashkin7617abd2015-07-10 18:27:47 -07002074 uint32_t method_access_flags = method->GetAccessFlags();
2075
Mathieu Chartiere401d142015-04-22 13:56:20 -07002076 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002077 indent_os << StringPrintf("SIZE: Dex Instructions=%zd StackMaps=%zd AccessFlags=0x%x\n",
2078 dex_instruction_bytes,
2079 vmap_table_bytes,
Igor Murashkin7617abd2015-07-10 18:27:47 -07002080 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002081
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002082 size_t total_size = dex_instruction_bytes +
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002083 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002084
2085 double expansion =
2086 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002087 stats_.ComputeOutliers(total_size, expansion, method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002088 }
2089 }
2090
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002091 std::set<const void*> already_seen_;
2092 // Compute the size of the given data within the oat file and whether this is the first time
2093 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002094 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002095 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002096 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002097 already_seen_.insert(oat_data);
2098 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002099 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002100 }
2101 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002102 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002103
2104 public:
2105 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002106 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002107 size_t file_bytes;
2108
2109 size_t header_bytes;
2110 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002111 size_t art_field_bytes;
2112 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01002113 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002114 size_t interned_strings_bytes;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002115 size_t class_table_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07002116 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002117 size_t alignment_bytes;
2118
2119 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002120 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002121 size_t managed_to_native_code_bytes;
2122 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07002123 size_t class_initializer_code_bytes;
2124 size_t large_initializer_code_bytes;
2125 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002126
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002127 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002128
2129 size_t dex_instruction_bytes;
2130
Mathieu Chartiere401d142015-04-22 13:56:20 -07002131 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002132 std::vector<size_t> method_outlier_size;
2133 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002134 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002135
Roland Levillain3887c462015-08-12 18:15:42 +01002136 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002137 : oat_file_bytes(0),
2138 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002139 header_bytes(0),
2140 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002141 art_field_bytes(0),
2142 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01002143 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002144 interned_strings_bytes(0),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002145 class_table_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07002146 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002147 alignment_bytes(0),
2148 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002149 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002150 managed_to_native_code_bytes(0),
2151 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07002152 class_initializer_code_bytes(0),
2153 large_initializer_code_bytes(0),
2154 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002155 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002156 dex_instruction_bytes(0) {}
2157
Elliott Hughesa0e18062012-04-13 15:59:59 -07002158 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002159 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002160 size_t bytes;
2161 size_t count;
2162 };
2163 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2164 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002165
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002166 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002167 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2168 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002169 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002170 it->second.count += 1;
2171 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002172 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002173 }
2174 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002175
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002176 double PercentOfOatBytes(size_t size) {
2177 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2178 }
2179
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002180 double PercentOfFileBytes(size_t size) {
2181 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2182 }
2183
2184 double PercentOfObjectBytes(size_t size) {
2185 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2186 }
2187
Mathieu Chartiere401d142015-04-22 13:56:20 -07002188 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002189 method_outlier_size.push_back(total_size);
2190 method_outlier_expansion.push_back(expansion);
2191 method_outlier.push_back(method);
2192 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002193
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002194 void DumpOutliers(std::ostream& os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002195 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002196 size_t sum_of_sizes = 0;
2197 size_t sum_of_sizes_squared = 0;
2198 size_t sum_of_expansion = 0;
2199 size_t sum_of_expansion_squared = 0;
2200 size_t n = method_outlier_size.size();
Mathieu Chartier1ebf8d32016-06-09 11:51:27 -07002201 if (n <= 1) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08002202 return;
2203 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002204 for (size_t i = 0; i < n; i++) {
2205 size_t cur_size = method_outlier_size[i];
2206 sum_of_sizes += cur_size;
2207 sum_of_sizes_squared += cur_size * cur_size;
2208 double cur_expansion = method_outlier_expansion[i];
2209 sum_of_expansion += cur_expansion;
2210 sum_of_expansion_squared += cur_expansion * cur_expansion;
2211 }
2212 size_t size_mean = sum_of_sizes / n;
2213 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2214 double expansion_mean = sum_of_expansion / n;
2215 double expansion_variance =
2216 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2217
2218 // Dump methods whose size is a certain number of standard deviations from the mean
2219 size_t dumped_values = 0;
2220 size_t skipped_values = 0;
2221 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2222 size_t cur_size_variance = i * i * size_variance;
2223 bool first = true;
2224 for (size_t j = 0; j < n; j++) {
2225 size_t cur_size = method_outlier_size[j];
2226 if (cur_size > size_mean) {
2227 size_t cur_var = cur_size - size_mean;
2228 cur_var = cur_var * cur_var;
2229 if (cur_var > cur_size_variance) {
2230 if (dumped_values > 20) {
2231 if (i == 1) {
2232 skipped_values++;
2233 } else {
2234 i = 2; // jump to counting for 1 standard deviation
2235 break;
2236 }
2237 } else {
2238 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002239 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002240 first = false;
2241 }
David Sehr709b0702016-10-13 09:12:37 -07002242 os << ArtMethod::PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002243 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002244 method_outlier_size[j] = 0; // don't consider this method again
2245 dumped_values++;
2246 }
2247 }
2248 }
2249 }
2250 }
2251 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002252 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002253 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002254 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002255 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002256
2257 // Dump methods whose expansion is a certain number of standard deviations from the mean
2258 dumped_values = 0;
2259 skipped_values = 0;
2260 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2261 double cur_expansion_variance = i * i * expansion_variance;
2262 bool first = true;
2263 for (size_t j = 0; j < n; j++) {
2264 double cur_expansion = method_outlier_expansion[j];
2265 if (cur_expansion > expansion_mean) {
2266 size_t cur_var = cur_expansion - expansion_mean;
2267 cur_var = cur_var * cur_var;
2268 if (cur_var > cur_expansion_variance) {
2269 if (dumped_values > 20) {
2270 if (i == 1) {
2271 skipped_values++;
2272 } else {
2273 i = 2; // jump to counting for 1 standard deviation
2274 break;
2275 }
2276 } else {
2277 if (first) {
2278 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002279 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002280 first = false;
2281 }
David Sehr709b0702016-10-13 09:12:37 -07002282 os << ArtMethod::PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002283 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002284 method_outlier_expansion[j] = 0.0; // don't consider this method again
2285 dumped_values++;
2286 }
2287 }
2288 }
2289 }
2290 }
2291 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002292 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002293 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002294 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002295 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002296 }
2297
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002298 void Dump(std::ostream& os, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002299 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002300 {
2301 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2302 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002303 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2304 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2305 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2306 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2307 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2308 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002309 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002310 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2311 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002312 header_bytes, PercentOfFileBytes(header_bytes),
2313 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002314 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2315 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002316 dex_cache_arrays_bytes,
2317 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002318 interned_strings_bytes,
2319 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002320 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002321 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002322 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2323 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002324 CHECK_EQ(file_bytes,
2325 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2326 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2327 bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002328 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002329
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002330 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002331 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002332 for (const auto& sizes_and_count : sizes_and_counts) {
2333 const std::string& descriptor(sizes_and_count.first);
2334 double average = static_cast<double>(sizes_and_count.second.bytes) /
2335 static_cast<double>(sizes_and_count.second.count);
2336 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002337 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002338 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002339 descriptor.c_str(), sizes_and_count.second.bytes,
2340 sizes_and_count.second.count, average, percent);
2341 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002342 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002343 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002344 CHECK_EQ(object_bytes, object_bytes_total);
2345
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002346 os << StringPrintf("oat_file_bytes = %8zd\n"
2347 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2348 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2349 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2350 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2351 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2352 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002353 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002354 managed_code_bytes,
2355 PercentOfOatBytes(managed_code_bytes),
2356 managed_to_native_code_bytes,
2357 PercentOfOatBytes(managed_to_native_code_bytes),
2358 native_to_managed_code_bytes,
2359 PercentOfOatBytes(native_to_managed_code_bytes),
2360 class_initializer_code_bytes,
2361 PercentOfOatBytes(class_initializer_code_bytes),
2362 large_initializer_code_bytes,
2363 PercentOfOatBytes(large_initializer_code_bytes),
2364 large_method_code_bytes,
2365 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002366 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002367 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002368 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002369 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2370 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002371 }
2372
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002373 os << "\n" << StringPrintf("vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002374 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002375 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002376
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002377 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2378 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002379 static_cast<double>(managed_code_bytes) /
2380 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002381 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002382 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002383 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002384
2385 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002386 }
2387 } stats_;
2388
2389 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002390 enum {
2391 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2392 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2393 kLargeConstructorDexBytes = 4000,
2394 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2395 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2396 kLargeMethodDexBytes = 16000
2397 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002398
2399 // For performance, use the *os_ directly for anything that doesn't need indentation
2400 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002401 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002402 VariableIndentationOutputStream vios_;
2403 ScopedIndentation indent1_;
2404
Ian Rogers1d54e732013-05-02 21:10:01 -07002405 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002406 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002407 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002408 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002409 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002410
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002411 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002412};
2413
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002414static int DumpImage(gc::space::ImageSpace* image_space,
2415 OatDumperOptions* options,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002416 std::ostream* os) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002417 const ImageHeader& image_header = image_space->GetImageHeader();
2418 if (!image_header.IsValid()) {
2419 fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
2420 return EXIT_FAILURE;
2421 }
2422 ImageDumper image_dumper(os, *image_space, image_header, options);
2423 if (!image_dumper.Dump()) {
2424 return EXIT_FAILURE;
2425 }
2426 return EXIT_SUCCESS;
2427}
2428
2429static int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002430 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002431 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002432 options->class_loader_ = &null_class_loader;
2433
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002434 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002435 if (options->app_image_ != nullptr) {
2436 if (options->app_oat_ == nullptr) {
2437 LOG(ERROR) << "Can not dump app image without app oat file";
Jeff Haodcdc85b2015-12-04 14:06:18 -08002438 return EXIT_FAILURE;
2439 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002440 // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
2441 // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
2442 // pointers into 32 bit pointer sized ArtMethods.
2443 std::string error_msg;
2444 std::unique_ptr<OatFile> oat_file(OatFile::Open(options->app_oat_,
2445 options->app_oat_,
2446 nullptr,
2447 nullptr,
2448 false,
2449 /*low_4gb*/true,
2450 nullptr,
2451 &error_msg));
2452 if (oat_file == nullptr) {
2453 LOG(ERROR) << "Failed to open oat file " << options->app_oat_ << " with error " << error_msg;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002454 return EXIT_FAILURE;
2455 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002456 std::unique_ptr<gc::space::ImageSpace> space(
2457 gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg));
2458 if (space == nullptr) {
2459 LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error "
2460 << error_msg;
2461 }
2462 // Open dex files for the image.
2463 std::vector<std::unique_ptr<const DexFile>> dex_files;
2464 if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
2465 LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
2466 << error_msg;
2467 }
2468 // Dump the actual image.
2469 int result = DumpImage(space.get(), options, os);
2470 if (result != EXIT_SUCCESS) {
2471 return result;
2472 }
2473 // Fall through to dump the boot images.
2474 }
2475
2476 gc::Heap* heap = runtime->GetHeap();
2477 CHECK(heap->HasBootImageSpace()) << "No image spaces";
2478 for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
2479 int result = DumpImage(image_space, options, os);
2480 if (result != EXIT_SUCCESS) {
2481 return result;
2482 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002483 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002484 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002485}
2486
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002487static jobject InstallOatFile(Runtime* runtime,
2488 std::unique_ptr<OatFile> oat_file,
2489 std::vector<const DexFile*>* class_path)
2490 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002491 Thread* self = Thread::Current();
2492 CHECK(self != nullptr);
2493 // Need well-known-classes.
2494 WellKnownClasses::Init(self->GetJniEnv());
2495
2496 // Need to register dex files to get a working dex cache.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002497 OatFile* oat_file_ptr = oat_file.get();
Andreas Gampe00b25f32014-09-17 21:49:05 -07002498 ClassLinker* class_linker = runtime->GetClassLinker();
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002499 runtime->GetOatFileManager().RegisterOatFile(std::move(oat_file));
2500 for (const OatFile::OatDexFile* odf : oat_file_ptr->GetOatDexFiles()) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002501 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002502 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002503 CHECK(dex_file != nullptr) << error_msg;
Mathieu Chartierf284d442016-06-02 11:48:30 -07002504 class_linker->RegisterDexFile(*dex_file, nullptr);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002505 class_path->push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002506 }
2507
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002508 // Need a class loader. Fake that we're a compiler.
2509 // Note: this will run initializers through the unstarted runtime, so make sure it's
2510 // initialized.
2511 interpreter::UnstartedRuntime::Initialize();
2512
2513 jobject class_loader = class_linker->CreatePathClassLoader(self, *class_path);
2514
2515 return class_loader;
2516}
2517
2518static int DumpOatWithRuntime(Runtime* runtime,
2519 std::unique_ptr<OatFile> oat_file,
2520 OatDumperOptions* options,
2521 std::ostream* os) {
2522 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2523 ScopedObjectAccess soa(Thread::Current());
2524
2525 OatFile* oat_file_ptr = oat_file.get();
2526 std::vector<const DexFile*> class_path;
2527 jobject class_loader = InstallOatFile(runtime, std::move(oat_file), &class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002528
2529 // Use the class loader while dumping.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002530 StackHandleScope<1> scope(soa.Self());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002531 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07002532 soa.Decode<mirror::ClassLoader>(class_loader));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002533 options->class_loader_ = &loader_handle;
2534
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002535 OatDumper oat_dumper(*oat_file_ptr, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002536 bool success = oat_dumper.Dump(*os);
2537 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2538}
2539
2540static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002541 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002542 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002543 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002544 options->class_loader_ = &null_class_loader;
2545
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002546 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002547 bool success = oat_dumper.Dump(*os);
2548 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2549}
2550
2551static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2552 std::ostream* os) {
2553 std::string error_msg;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002554 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
2555 oat_filename,
2556 nullptr,
2557 nullptr,
2558 false,
2559 /*low_4gb*/false,
2560 nullptr,
2561 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002562 if (oat_file == nullptr) {
2563 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2564 return EXIT_FAILURE;
2565 }
2566
2567 if (runtime != nullptr) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002568 return DumpOatWithRuntime(runtime, std::move(oat_file), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002569 } else {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002570 return DumpOatWithoutRuntime(oat_file.get(), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002571 }
2572}
2573
David Srbecky2fdd03c2016-03-10 15:32:37 +00002574static int SymbolizeOat(const char* oat_filename, std::string& output_name, bool no_bits) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002575 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002576 OatFile* oat_file = OatFile::Open(oat_filename,
2577 oat_filename,
2578 nullptr,
2579 nullptr,
2580 false,
2581 /*low_4gb*/false,
2582 nullptr,
2583 &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002584 if (oat_file == nullptr) {
2585 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2586 return EXIT_FAILURE;
2587 }
2588
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002589 bool result;
2590 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
2591 // files for 64-bit code in the past.
2592 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002593 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file, output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002594 result = oat_symbolizer.Symbolize();
2595 } else {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002596 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file, output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002597 result = oat_symbolizer.Symbolize();
2598 }
2599 if (!result) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002600 fprintf(stderr, "Failed to symbolize\n");
2601 return EXIT_FAILURE;
2602 }
2603
2604 return EXIT_SUCCESS;
2605}
2606
Andreas Gampe9fded872016-09-25 16:08:35 -07002607class IMTDumper {
2608 public:
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002609 static bool Dump(Runtime* runtime,
2610 const std::string& imt_file,
2611 bool dump_imt_stats,
2612 const char* oat_filename) {
2613 Thread* self = Thread::Current();
2614
2615 ScopedObjectAccess soa(self);
2616 StackHandleScope<1> scope(self);
2617 MutableHandle<mirror::ClassLoader> class_loader = scope.NewHandle<mirror::ClassLoader>(nullptr);
2618 std::vector<const DexFile*> class_path;
2619
2620 if (oat_filename != nullptr) {
2621 std::string error_msg;
2622 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
2623 oat_filename,
2624 nullptr,
2625 nullptr,
2626 false,
2627 /*low_4gb*/false,
2628 nullptr,
2629 &error_msg));
2630 if (oat_file == nullptr) {
2631 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2632 return false;
2633 }
2634
2635 class_loader.Assign(soa.Decode<mirror::ClassLoader>(
2636 InstallOatFile(runtime, std::move(oat_file), &class_path)));
2637 } else {
2638 class_loader.Assign(nullptr); // Boot classloader. Just here for explicit documentation.
2639 class_path = runtime->GetClassLinker()->GetBootClassPath();
2640 }
2641
2642 if (!imt_file.empty()) {
2643 return DumpImt(runtime, imt_file, class_loader);
2644 }
2645
2646 if (dump_imt_stats) {
2647 return DumpImtStats(runtime, class_path, class_loader);
2648 }
2649
2650 LOG(FATAL) << "Should not reach here";
2651 UNREACHABLE();
2652 }
2653
2654 private:
2655 static bool DumpImt(Runtime* runtime,
2656 const std::string& imt_file,
2657 Handle<mirror::ClassLoader> h_class_loader)
2658 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002659 std::vector<std::string> lines = ReadCommentedInputFromFile(imt_file);
2660 std::unordered_set<std::string> prepared;
2661
2662 for (const std::string& line : lines) {
2663 // A line should be either a class descriptor, in which case we will dump the complete IMT,
2664 // or a class descriptor and an interface method, in which case we will lookup the method,
2665 // determine its IMT slot, and check the class' IMT.
2666 size_t first_space = line.find(' ');
2667 if (first_space == std::string::npos) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002668 DumpIMTForClass(runtime, line, h_class_loader, &prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07002669 } else {
2670 DumpIMTForMethod(runtime,
2671 line.substr(0, first_space),
2672 line.substr(first_space + 1, std::string::npos),
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002673 h_class_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07002674 &prepared);
2675 }
2676 std::cerr << std::endl;
2677 }
2678
2679 return true;
2680 }
2681
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002682 static bool DumpImtStats(Runtime* runtime,
2683 const std::vector<const DexFile*>& dex_files,
2684 Handle<mirror::ClassLoader> h_class_loader)
2685 REQUIRES_SHARED(Locks::mutator_lock_) {
2686 size_t without_imt = 0;
2687 size_t with_imt = 0;
Andreas Gampe9fded872016-09-25 16:08:35 -07002688 std::map<size_t, size_t> histogram;
2689
2690 ClassLinker* class_linker = runtime->GetClassLinker();
2691 const PointerSize pointer_size = class_linker->GetImagePointerSize();
2692 std::unordered_set<std::string> prepared;
2693
2694 Thread* self = Thread::Current();
Andreas Gampe9fded872016-09-25 16:08:35 -07002695 StackHandleScope<1> scope(self);
2696 MutableHandle<mirror::Class> h_klass(scope.NewHandle<mirror::Class>(nullptr));
2697
2698 for (const DexFile* dex_file : dex_files) {
2699 for (uint32_t class_def_index = 0;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002700 class_def_index != dex_file->NumClassDefs();
2701 ++class_def_index) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002702 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
2703 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002704 h_klass.Assign(class_linker->FindClass(self, descriptor, h_class_loader));
Andreas Gampe9fded872016-09-25 16:08:35 -07002705 if (h_klass.Get() == nullptr) {
2706 std::cerr << "Warning: could not load " << descriptor << std::endl;
2707 continue;
2708 }
2709
2710 if (HasNoIMT(runtime, h_klass, pointer_size, &prepared)) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002711 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07002712 continue;
2713 }
2714
2715 ImTable* im_table = PrepareAndGetImTable(runtime, h_klass, pointer_size, &prepared);
2716 if (im_table == nullptr) {
2717 // Should not happen, but accept.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002718 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07002719 continue;
2720 }
2721
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002722 with_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07002723 for (size_t imt_index = 0; imt_index != ImTable::kSize; ++imt_index) {
2724 ArtMethod* ptr = im_table->Get(imt_index, pointer_size);
2725 if (ptr->IsRuntimeMethod()) {
2726 if (ptr->IsImtUnimplementedMethod()) {
2727 histogram[0]++;
2728 } else {
2729 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
2730 histogram[current_table->NumEntries(pointer_size)]++;
2731 }
2732 } else {
2733 histogram[1]++;
2734 }
2735 }
2736 }
2737 }
2738
2739 std::cerr << "IMT stats:"
2740 << std::endl << std::endl;
2741
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002742 std::cerr << " " << with_imt << " classes with IMT."
Andreas Gampe9fded872016-09-25 16:08:35 -07002743 << std::endl << std::endl;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002744 std::cerr << " " << without_imt << " classes without IMT (or copy from Object)."
Andreas Gampe9fded872016-09-25 16:08:35 -07002745 << std::endl << std::endl;
2746
2747 double sum_one = 0;
2748 size_t count_one = 0;
2749
2750 std::cerr << " " << "IMT histogram" << std::endl;
2751 for (auto& bucket : histogram) {
2752 std::cerr << " " << bucket.first << " " << bucket.second << std::endl;
2753 if (bucket.first > 0) {
2754 sum_one += bucket.second * bucket.first;
2755 count_one += bucket.second;
2756 }
2757 }
2758
2759 double count_zero = count_one + histogram[0];
2760 std::cerr << " Stats:" << std::endl;
2761 std::cerr << " Average depth (including empty): " << (sum_one / count_zero) << std::endl;
2762 std::cerr << " Average depth (excluding empty): " << (sum_one / count_one) << std::endl;
2763
2764 return true;
2765 }
2766
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002767 // Return whether the given class has no IMT (or the one shared with java.lang.Object).
Andreas Gampe9fded872016-09-25 16:08:35 -07002768 static bool HasNoIMT(Runtime* runtime,
2769 Handle<mirror::Class> klass,
2770 const PointerSize pointer_size,
2771 std::unordered_set<std::string>* prepared)
2772 REQUIRES_SHARED(Locks::mutator_lock_) {
2773 if (klass->IsObjectClass() || !klass->ShouldHaveImt()) {
2774 return true;
2775 }
2776
2777 if (klass->GetImt(pointer_size) == nullptr) {
2778 PrepareClass(runtime, klass, prepared);
2779 }
2780
2781 mirror::Class* object_class = mirror::Class::GetJavaLangClass()->GetSuperClass();
2782 DCHECK(object_class->IsObjectClass());
2783
2784 bool result = klass->GetImt(pointer_size) == object_class->GetImt(pointer_size);
2785
2786 if (klass->GetIfTable() == nullptr) {
2787 DCHECK(result);
2788 }
2789
2790 return result;
2791 }
2792
2793 static void PrintTable(ImtConflictTable* table, PointerSize pointer_size)
2794 REQUIRES_SHARED(Locks::mutator_lock_) {
2795 if (table == nullptr) {
2796 std::cerr << " <No IMT?>" << std::endl;
2797 return;
2798 }
2799 size_t table_index = 0;
2800 for (;;) {
2801 ArtMethod* ptr = table->GetInterfaceMethod(table_index, pointer_size);
2802 if (ptr == nullptr) {
2803 return;
2804 }
2805 table_index++;
David Sehr709b0702016-10-13 09:12:37 -07002806 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07002807 }
2808 }
2809
2810 static ImTable* PrepareAndGetImTable(Runtime* runtime,
2811 Thread* self,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002812 Handle<mirror::ClassLoader> h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07002813 const std::string& class_name,
2814 const PointerSize pointer_size,
2815 mirror::Class** klass_out,
2816 std::unordered_set<std::string>* prepared)
2817 REQUIRES_SHARED(Locks::mutator_lock_) {
2818 if (class_name.empty()) {
2819 return nullptr;
2820 }
2821
2822 std::string descriptor;
2823 if (class_name[0] == 'L') {
2824 descriptor = class_name;
2825 } else {
2826 descriptor = DotToDescriptor(class_name.c_str());
2827 }
2828
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002829 mirror::Class* klass = runtime->GetClassLinker()->FindClass(self, descriptor.c_str(), h_loader);
Andreas Gampe9fded872016-09-25 16:08:35 -07002830
2831 if (klass == nullptr) {
2832 self->ClearException();
2833 std::cerr << "Did not find " << class_name << std::endl;
2834 *klass_out = nullptr;
2835 return nullptr;
2836 }
2837
2838 StackHandleScope<1> scope(Thread::Current());
2839 Handle<mirror::Class> h_klass = scope.NewHandle<mirror::Class>(klass);
2840
2841 ImTable* ret = PrepareAndGetImTable(runtime, h_klass, pointer_size, prepared);
2842 *klass_out = h_klass.Get();
2843 return ret;
2844 }
2845
2846 static ImTable* PrepareAndGetImTable(Runtime* runtime,
2847 Handle<mirror::Class> h_klass,
2848 const PointerSize pointer_size,
2849 std::unordered_set<std::string>* prepared)
2850 REQUIRES_SHARED(Locks::mutator_lock_) {
2851 PrepareClass(runtime, h_klass, prepared);
2852 return h_klass->GetImt(pointer_size);
2853 }
2854
2855 static void DumpIMTForClass(Runtime* runtime,
2856 const std::string& class_name,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002857 Handle<mirror::ClassLoader> h_loader,
2858 std::unordered_set<std::string>* prepared)
2859 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002860 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
2861 mirror::Class* klass;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002862 ImTable* imt = PrepareAndGetImTable(runtime,
2863 Thread::Current(),
2864 h_loader,
2865 class_name,
2866 pointer_size,
2867 &klass,
2868 prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07002869 if (imt == nullptr) {
2870 return;
2871 }
2872
2873 std::cerr << class_name << std::endl << " IMT:" << std::endl;
2874 for (size_t index = 0; index < ImTable::kSize; ++index) {
2875 std::cerr << " " << index << ":" << std::endl;
2876 ArtMethod* ptr = imt->Get(index, pointer_size);
2877 if (ptr->IsRuntimeMethod()) {
2878 if (ptr->IsImtUnimplementedMethod()) {
2879 std::cerr << " <empty>" << std::endl;
2880 } else {
2881 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
2882 PrintTable(current_table, pointer_size);
2883 }
2884 } else {
David Sehr709b0702016-10-13 09:12:37 -07002885 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07002886 }
2887 }
2888
2889 std::cerr << " Interfaces:" << std::endl;
2890 // Run through iftable, find methods that slot here, see if they fit.
2891 mirror::IfTable* if_table = klass->GetIfTable();
2892 if (if_table != nullptr) {
2893 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
2894 mirror::Class* iface = if_table->GetInterface(i);
2895 std::string iface_name;
2896 std::cerr << " " << iface->GetDescriptor(&iface_name) << std::endl;
2897
2898 for (ArtMethod& iface_method : iface->GetVirtualMethods(pointer_size)) {
Andreas Gampea1ff30f2016-09-27 12:19:45 -07002899 uint32_t class_hash, name_hash, signature_hash;
2900 ImTable::GetImtHashComponents(&iface_method, &class_hash, &name_hash, &signature_hash);
Andreas Gampe9fded872016-09-25 16:08:35 -07002901 uint32_t imt_slot = ImTable::GetImtIndex(&iface_method);
David Sehr709b0702016-10-13 09:12:37 -07002902 std::cerr << " " << iface_method.PrettyMethod(true)
Andreas Gampea1ff30f2016-09-27 12:19:45 -07002903 << " slot=" << imt_slot
2904 << std::hex
2905 << " class_hash=0x" << class_hash
2906 << " name_hash=0x" << name_hash
2907 << " signature_hash=0x" << signature_hash
2908 << std::dec
2909 << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07002910 }
2911 }
2912 }
2913 }
2914
2915 static void DumpIMTForMethod(Runtime* runtime,
2916 const std::string& class_name,
2917 const std::string& method,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002918 Handle<mirror::ClassLoader> h_loader,
2919 std::unordered_set<std::string>* prepared)
2920 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002921 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
2922 mirror::Class* klass;
2923 ImTable* imt = PrepareAndGetImTable(runtime,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002924 Thread::Current(),
2925 h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07002926 class_name,
2927 pointer_size,
2928 &klass,
2929 prepared);
2930 if (imt == nullptr) {
2931 return;
2932 }
2933
2934 std::cerr << class_name << " <" << method << ">" << std::endl;
2935 for (size_t index = 0; index < ImTable::kSize; ++index) {
2936 ArtMethod* ptr = imt->Get(index, pointer_size);
2937 if (ptr->IsRuntimeMethod()) {
2938 if (ptr->IsImtUnimplementedMethod()) {
2939 continue;
2940 }
2941
2942 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
2943 if (current_table == nullptr) {
2944 continue;
2945 }
2946
2947 size_t table_index = 0;
2948 for (;;) {
2949 ArtMethod* ptr2 = current_table->GetInterfaceMethod(table_index, pointer_size);
2950 if (ptr2 == nullptr) {
2951 break;
2952 }
2953 table_index++;
2954
David Sehr709b0702016-10-13 09:12:37 -07002955 std::string p_name = ptr2->PrettyMethod(true);
Andreas Gampe9fded872016-09-25 16:08:35 -07002956 if (StartsWith(p_name, method.c_str())) {
2957 std::cerr << " Slot "
2958 << index
2959 << " ("
2960 << current_table->NumEntries(pointer_size)
2961 << ")"
2962 << std::endl;
2963 PrintTable(current_table, pointer_size);
2964 return;
2965 }
2966 }
2967 } else {
David Sehr709b0702016-10-13 09:12:37 -07002968 std::string p_name = ptr->PrettyMethod(true);
Andreas Gampe9fded872016-09-25 16:08:35 -07002969 if (StartsWith(p_name, method.c_str())) {
2970 std::cerr << " Slot " << index << " (1)" << std::endl;
2971 std::cerr << " " << p_name << std::endl;
2972 } else {
2973 // Run through iftable, find methods that slot here, see if they fit.
2974 mirror::IfTable* if_table = klass->GetIfTable();
2975 if (if_table != nullptr) {
2976 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
2977 mirror::Class* iface = if_table->GetInterface(i);
2978 size_t num_methods = iface->NumDeclaredVirtualMethods();
2979 if (num_methods > 0) {
2980 for (ArtMethod& iface_method : iface->GetMethods(pointer_size)) {
2981 if (ImTable::GetImtIndex(&iface_method) == index) {
David Sehr709b0702016-10-13 09:12:37 -07002982 std::string i_name = iface_method.PrettyMethod(true);
Andreas Gampe9fded872016-09-25 16:08:35 -07002983 if (StartsWith(i_name, method.c_str())) {
2984 std::cerr << " Slot " << index << " (1)" << std::endl;
2985 std::cerr << " " << p_name << " (" << i_name << ")" << std::endl;
2986 }
2987 }
2988 }
2989 }
2990 }
2991 }
2992 }
2993 }
2994 }
2995 }
2996
2997 // Read lines from the given stream, dropping comments and empty lines
2998 static std::vector<std::string> ReadCommentedInputStream(std::istream& in_stream) {
2999 std::vector<std::string> output;
3000 while (in_stream.good()) {
3001 std::string dot;
3002 std::getline(in_stream, dot);
3003 if (StartsWith(dot, "#") || dot.empty()) {
3004 continue;
3005 }
3006 output.push_back(dot);
3007 }
3008 return output;
3009 }
3010
3011 // Read lines from the given file, dropping comments and empty lines.
3012 static std::vector<std::string> ReadCommentedInputFromFile(const std::string& input_filename) {
3013 std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
3014 if (input_file.get() == nullptr) {
3015 LOG(ERROR) << "Failed to open input file " << input_filename;
3016 return std::vector<std::string>();
3017 }
3018 std::vector<std::string> result = ReadCommentedInputStream(*input_file);
3019 input_file->close();
3020 return result;
3021 }
3022
3023 // Prepare a class, i.e., ensure it has a filled IMT. Will do so recursively for superclasses,
3024 // and note in the given set that the work was done.
3025 static void PrepareClass(Runtime* runtime,
3026 Handle<mirror::Class> h_klass,
3027 std::unordered_set<std::string>* done)
3028 REQUIRES_SHARED(Locks::mutator_lock_) {
3029 if (!h_klass->ShouldHaveImt()) {
3030 return;
3031 }
3032
3033 std::string name;
3034 name = h_klass->GetDescriptor(&name);
3035
3036 if (done->find(name) != done->end()) {
3037 return;
3038 }
3039 done->insert(name);
3040
3041 if (h_klass->HasSuperClass()) {
3042 StackHandleScope<1> h(Thread::Current());
3043 PrepareClass(runtime, h.NewHandle<mirror::Class>(h_klass->GetSuperClass()), done);
3044 }
3045
3046 if (!h_klass->IsTemp()) {
3047 runtime->GetClassLinker()->FillIMTAndConflictTables(h_klass.Get());
3048 }
3049 }
3050};
3051
Igor Murashkin37743352014-11-13 14:38:00 -08003052struct OatdumpArgs : public CmdlineArgs {
3053 protected:
3054 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003055
Igor Murashkin37743352014-11-13 14:38:00 -08003056 virtual ParseStatus ParseCustom(const StringPiece& option,
3057 std::string* error_msg) OVERRIDE {
3058 {
3059 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
3060 if (base_parse != kParseUnknownArgument) {
3061 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003062 }
3063 }
3064
Igor Murashkin37743352014-11-13 14:38:00 -08003065 if (option.starts_with("--oat-file=")) {
3066 oat_filename_ = option.substr(strlen("--oat-file=")).data();
3067 } else if (option.starts_with("--image=")) {
3068 image_location_ = option.substr(strlen("--image=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003069 } else if (option == "--no-dump:vmap") {
3070 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01003071 } else if (option =="--dump:code_info_stack_maps") {
3072 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003073 } else if (option == "--no-disassemble") {
3074 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003075 } else if (option =="--header-only") {
3076 dump_header_only_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003077 } else if (option.starts_with("--symbolize=")) {
3078 oat_filename_ = option.substr(strlen("--symbolize=")).data();
3079 symbolize_ = true;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003080 } else if (option.starts_with("--only-keep-debug")) {
3081 only_keep_debug_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003082 } else if (option.starts_with("--class-filter=")) {
3083 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003084 } else if (option.starts_with("--method-filter=")) {
3085 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003086 } else if (option.starts_with("--list-classes")) {
3087 list_classes_ = true;
3088 } else if (option.starts_with("--list-methods")) {
3089 list_methods_ = true;
3090 } else if (option.starts_with("--export-dex-to=")) {
3091 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
3092 } else if (option.starts_with("--addr2instr=")) {
3093 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
3094 *error_msg = "Address conversion failed";
3095 return kParseError;
3096 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003097 } else if (option.starts_with("--app-image=")) {
3098 app_image_ = option.substr(strlen("--app-image=")).data();
3099 } else if (option.starts_with("--app-oat=")) {
3100 app_oat_ = option.substr(strlen("--app-oat=")).data();
Andreas Gampe9fded872016-09-25 16:08:35 -07003101 } else if (option.starts_with("--dump-imt=")) {
3102 imt_dump_ = option.substr(strlen("--dump-imt=")).data();
3103 } else if (option == "--dump-imt-stats") {
3104 imt_stat_dump_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003105 } else {
3106 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003107 }
3108
Igor Murashkin37743352014-11-13 14:38:00 -08003109 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003110 }
3111
Igor Murashkin37743352014-11-13 14:38:00 -08003112 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
3113 // Infer boot image location from the image location if possible.
3114 if (boot_image_location_ == nullptr) {
3115 boot_image_location_ = image_location_;
3116 }
3117
3118 // Perform the parent checks.
3119 ParseStatus parent_checks = Base::ParseChecks(error_msg);
3120 if (parent_checks != kParseOk) {
3121 return parent_checks;
3122 }
3123
3124 // Perform our own checks.
3125 if (image_location_ == nullptr && oat_filename_ == nullptr) {
3126 *error_msg = "Either --image or --oat-file must be specified";
3127 return kParseError;
3128 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
3129 *error_msg = "Either --image or --oat-file must be specified but not both";
3130 return kParseError;
3131 }
3132
3133 return kParseOk;
3134 }
3135
3136 virtual std::string GetUsage() const {
3137 std::string usage;
3138
3139 usage +=
3140 "Usage: oatdump [options] ...\n"
3141 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
3142 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
3143 "\n"
3144 // Either oat-file or image is required.
3145 " --oat-file=<file.oat>: specifies an input oat filename.\n"
3146 " Example: --oat-file=/system/framework/boot.oat\n"
3147 "\n"
3148 " --image=<file.art>: specifies an input image location.\n"
3149 " Example: --image=/system/framework/boot.art\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003150 "\n"
3151 " --app-image=<file.art>: specifies an input app image. Must also have a specified\n"
3152 " boot image and app oat file.\n"
3153 " Example: --app-image=app.art\n"
3154 "\n"
3155 " --app-oat=<file.odex>: specifies an input app oat.\n"
3156 " Example: --app-oat=app.odex\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003157 "\n";
3158
3159 usage += Base::GetUsage();
3160
3161 usage += // Optional.
Igor Murashkin37743352014-11-13 14:38:00 -08003162 " --no-dump:vmap may be used to disable vmap dumping.\n"
3163 " Example: --no-dump:vmap\n"
3164 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01003165 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
3166 " Example: --dump:code_info_stack_maps\n"
3167 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003168 " --no-disassemble may be used to disable disassembly.\n"
3169 " Example: --no-disassemble\n"
3170 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00003171 " --header-only may be used to print only the oat header.\n"
3172 " Example: --header-only\n"
3173 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003174 " --list-classes may be used to list target file classes (can be used with filters).\n"
3175 " Example: --list-classes\n"
3176 " Example: --list-classes --class-filter=com.example.foo\n"
3177 "\n"
3178 " --list-methods may be used to list target file methods (can be used with filters).\n"
3179 " Example: --list-methods\n"
3180 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
3181 "\n"
3182 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
3183 " Example: --symbolize=/system/framework/boot.oat\n"
3184 "\n"
David Srbecky2fdd03c2016-03-10 15:32:37 +00003185 " --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
3186 " .rodata and .text sections are omitted in the output file to save space.\n"
3187 " Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
3188 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003189 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
3190 " Example: --class-filter=com.example.foo\n"
3191 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003192 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
3193 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003194 "\n"
3195 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
3196 " Example: --export-dex-to=/data/local/tmp\n"
3197 "\n"
3198 " --addr2instr=<address>: output matching method disassembled code from relative\n"
3199 " address (e.g. PC from crash dump)\n"
3200 " Example: --addr2instr=0x00001a3b\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003201 "\n"
3202 " --dump-imt=<file.txt>: output IMT collisions (if any) for the given receiver\n"
3203 " types and interface methods in the given file. The file\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003204 " is read line-wise, where each line should either be a class\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003205 " name or descriptor, or a class name/descriptor and a prefix\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003206 " of a complete method name (separated by a whitespace).\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003207 " Example: --dump-imt=imt.txt\n"
3208 "\n"
3209 " --dump-imt-stats: output IMT statistics for the given boot image\n"
3210 " Example: --dump-imt-stats"
Igor Murashkin37743352014-11-13 14:38:00 -08003211 "\n";
3212
3213 return usage;
3214 }
3215
3216 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07003217 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003218 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00003219 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07003220 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003221 std::string elf_filename_prefix_;
Andreas Gampe9fded872016-09-25 16:08:35 -07003222 std::string imt_dump_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003223 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01003224 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003225 bool disassemble_code_ = true;
3226 bool symbolize_ = false;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003227 bool only_keep_debug_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003228 bool list_classes_ = false;
3229 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003230 bool dump_header_only_ = false;
Andreas Gampe9fded872016-09-25 16:08:35 -07003231 bool imt_stat_dump_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003232 uint32_t addr2instr_ = 0;
3233 const char* export_dex_location_ = nullptr;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003234 const char* app_image_ = nullptr;
3235 const char* app_oat_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003236};
3237
Igor Murashkin37743352014-11-13 14:38:00 -08003238struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
3239 virtual bool NeedsRuntime() OVERRIDE {
3240 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003241
Igor Murashkin37743352014-11-13 14:38:00 -08003242 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
3243 bool absolute_addresses = (args_->oat_filename_ == nullptr);
3244
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003245 oat_dumper_options_.reset(new OatDumperOptions(
Igor Murashkin37743352014-11-13 14:38:00 -08003246 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01003247 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08003248 args_->disassemble_code_,
3249 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003250 args_->class_filter_,
3251 args_->method_filter_,
3252 args_->list_classes_,
3253 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00003254 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003255 args_->export_dex_location_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003256 args_->app_image_,
3257 args_->app_oat_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003258 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08003259
Andreas Gampe9fded872016-09-25 16:08:35 -07003260 return (args_->boot_image_location_ != nullptr ||
3261 args_->image_location_ != nullptr ||
3262 !args_->imt_dump_.empty()) &&
Igor Murashkin37743352014-11-13 14:38:00 -08003263 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003264 }
3265
Igor Murashkin37743352014-11-13 14:38:00 -08003266 virtual bool ExecuteWithoutRuntime() OVERRIDE {
3267 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08003268 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003269
Mathieu Chartierd424d082014-10-15 10:31:46 -07003270 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08003271
Andreas Gampec24f3992014-12-17 20:40:11 -08003272 if (args_->symbolize_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00003273 // ELF has special kind of section called SHT_NOBITS which allows us to create
3274 // sections which exist but their data is omitted from the ELF file to save space.
3275 // This is what "strip --only-keep-debug" does when it creates separate ELF file
3276 // with only debug data. We use it in similar way to exclude .rodata and .text.
3277 bool no_bits = args_->only_keep_debug_;
3278 return SymbolizeOat(args_->oat_filename_, args_->output_name_, no_bits) == EXIT_SUCCESS;
Andreas Gampec24f3992014-12-17 20:40:11 -08003279 } else {
3280 return DumpOat(nullptr,
3281 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003282 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08003283 args_->os_) == EXIT_SUCCESS;
3284 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07003285 }
3286
Igor Murashkin37743352014-11-13 14:38:00 -08003287 virtual bool ExecuteWithRuntime(Runtime* runtime) {
3288 CHECK(args_ != nullptr);
3289
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003290 if (!args_->imt_dump_.empty() || args_->imt_stat_dump_) {
3291 return IMTDumper::Dump(runtime,
3292 args_->imt_dump_,
3293 args_->imt_stat_dump_,
3294 args_->oat_filename_);
Andreas Gampe9fded872016-09-25 16:08:35 -07003295 }
3296
Igor Murashkin37743352014-11-13 14:38:00 -08003297 if (args_->oat_filename_ != nullptr) {
3298 return DumpOat(runtime,
3299 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003300 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08003301 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003302 }
Igor Murashkin37743352014-11-13 14:38:00 -08003303
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003304 return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003305 }
3306
Igor Murashkin37743352014-11-13 14:38:00 -08003307 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
3308};
Andreas Gampe00b25f32014-09-17 21:49:05 -07003309
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003310} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07003311
3312int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08003313 art::OatdumpMain main;
3314 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07003315}