blob: 692a951c97b88c3c2602fd4b4ac5992db2d2ca09 [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: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800706 const dex::StringIndex string_index(inst->VRegB_21c());
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700707 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: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800712 const dex::StringIndex string_index(inst->VRegB_31c());
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700713 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)",
Andreas Gampea5b09a62016-11-17 15:21:22 -0800797 class_def_index, descriptor, oat_class_offset, class_def.class_idx_.index_)
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 }
Mingyao Yang063fc772016-08-02 11:02:54 -07001056 uint32_t vmap_table_offset = method_header ==
1057 nullptr ? 0 : method_header->GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001058 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001059
1060 size_t vmap_table_offset_limit =
1061 (kIsVdexEnabled && IsMethodGeneratedByDexToDexCompiler(oat_method, code_item))
1062 ? oat_file_.GetVdexFile()->Size()
1063 : method_header->GetCode() - oat_file_.Begin();
1064 if (vmap_table_offset >= vmap_table_offset_limit) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001065 vios->Stream() << StringPrintf("WARNING: "
1066 "vmap table offset 0x%08x is past end of file 0x%08zx. "
1067 "vmap table offset was loaded from offset 0x%08x.\n",
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001068 vmap_table_offset,
1069 vmap_table_offset_limit,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001070 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001071 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001072 } else if (options_.dump_vmap_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001073 DumpVmapData(vios, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001074 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001075 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001076 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001077 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001078
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001079 ScopedIndentation indent2(vios);
1080 vios->Stream()
1081 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
1082 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
1083 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
1084 vios->Stream() << "\n";
1085 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
1086 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
1087 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001088 }
1089 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001090 // Based on spill masks from QuickMethodFrameInfo so placed
1091 // after it is dumped, but useful for understanding quick
1092 // code, so dumped here.
1093 ScopedIndentation indent2(vios);
1094 DumpVregLocations(vios->Stream(), oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001095 }
1096 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001097 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001098 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
1099 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001100 ScopedIndentation indent2(vios);
1101 vios->Stream() << StringPrintf("WARNING: "
1102 "code size offset 0x%08x is past end of file 0x%08zx.",
1103 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001104 success = false;
1105 } else {
1106 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001107 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1108 uint64_t aligned_code_end = aligned_code_begin + code_size;
1109
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001110 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001111 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001112 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001113 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
1114 code_offset,
1115 code_size_offset,
1116 code_size,
1117 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001118
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001119 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001120 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001121 vios->Stream() << StringPrintf("WARNING: "
1122 "start of code at 0x%08x is past end of file 0x%08zx.",
1123 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001124 success = false;
1125 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001126 vios->Stream() << StringPrintf(
1127 "WARNING: "
1128 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
1129 "code size is 0x%08x loaded from offset 0x%08x.\n",
1130 aligned_code_end, oat_file_.Size(),
1131 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001132 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001133 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001134 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001135 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001136 }
1137 }
1138 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001139 vios->Stream() << StringPrintf(
1140 "WARNING: "
1141 "code size %d is bigger than max expected threshold of %d. "
1142 "code size is 0x%08x loaded from offset 0x%08x.\n",
1143 code_size, kMaxCodeSize,
1144 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001145 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001146 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001147 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001148 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001149 }
1150 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001151 } else if (options_.disassemble_code_) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001152 DumpCode(vios, oat_method, code_item, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001153 }
1154 }
1155 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001156 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001157 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001158 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001159
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001160 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1161 if (spill_mask == 0) {
1162 return;
1163 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001164 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001165 for (size_t i = 0; i < 32; i++) {
1166 if ((spill_mask & (1 << i)) != 0) {
1167 if (is_float) {
1168 os << "fr" << i;
1169 } else {
1170 os << "r" << i;
1171 }
1172 spill_mask ^= 1 << i; // clear bit
1173 if (spill_mask != 0) {
1174 os << ", ";
1175 } else {
1176 break;
1177 }
1178 }
1179 }
1180 os << ")";
1181 }
1182
Roland Levillain442b46a2015-02-18 16:54:21 +00001183 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001184 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001185 const OatFile::OatMethod& oat_method,
1186 const DexFile::CodeItem* code_item) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001187 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1188 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001189 const void* raw_code_info = oat_method.GetVmapTable();
1190 if (raw_code_info != nullptr) {
1191 CodeInfo code_info(raw_code_info);
1192 DCHECK(code_item != nullptr);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001193 ScopedIndentation indent1(vios);
1194 DumpCodeInfo(vios, code_info, oat_method, *code_item);
Roland Levillain442b46a2015-02-18 16:54:21 +00001195 }
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001196 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
1197 // We don't encode the size in the table, so just emit that we have quickened
1198 // information.
1199 ScopedIndentation indent(vios);
1200 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001201 } else {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001202 // Otherwise, there is nothing to display.
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001203 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001204 }
1205
1206 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001207 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001208 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001209 const OatFile::OatMethod& oat_method,
Roland Levillain442b46a2015-02-18 16:54:21 +00001210 const DexFile::CodeItem& code_item) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001211 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001212 oat_method.GetCodeOffset(),
1213 code_item.registers_size_,
1214 options_.dump_code_info_stack_maps_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001215 }
1216
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001217 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1218 const DexFile::CodeItem* code_item) {
1219 if (code_item != nullptr) {
1220 size_t num_locals_ins = code_item->registers_size_;
1221 size_t num_ins = code_item->ins_size_;
1222 size_t num_locals = num_locals_ins - num_ins;
1223 size_t num_outs = code_item->outs_size_;
1224
1225 os << "vr_stack_locations:";
1226 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1227 // For readability, delimit the different kinds of VRs.
1228 if (reg == num_locals_ins) {
1229 os << "\n\tmethod*:";
1230 } else if (reg == num_locals && num_ins > 0) {
1231 os << "\n\tins:";
1232 } else if (reg == 0 && num_locals > 0) {
1233 os << "\n\tlocals:";
1234 }
1235
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001236 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1237 code_item,
1238 oat_method.GetCoreSpillMask(),
1239 oat_method.GetFpSpillMask(),
1240 oat_method.GetFrameSizeInBytes(),
1241 reg,
1242 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001243 os << " v" << reg << "[sp + #" << offset << "]";
1244 }
1245
1246 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1247 if (out_reg == 0) {
1248 os << "\n\touts:";
1249 }
1250
1251 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1252 os << " v" << out_reg << "[sp + #" << offset << "]";
1253 }
1254
1255 os << "\n";
1256 }
1257 }
1258
Ian Rogersb23a7722012-10-09 16:54:26 -07001259 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001260 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001261 size_t i = 0;
1262 while (i < code_item->insns_size_in_code_units_) {
1263 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001264 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1265 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001266 i += instruction->SizeInCodeUnits();
1267 }
1268 }
1269 }
1270
Roland Levillainf2650d12015-05-28 14:53:28 +01001271 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1272 // the optimizing compiler?
1273 static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1274 const DexFile::CodeItem* code_item) {
1275 // If the native GC map is null and the Dex `code_item` is not
1276 // null, then this method has been compiled with the optimizing
1277 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001278 return oat_method.GetQuickCode() != nullptr &&
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001279 oat_method.GetVmapTable() != nullptr &&
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001280 code_item != nullptr;
1281 }
1282
1283 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1284 // the dextodex compiler?
1285 static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
1286 const DexFile::CodeItem* code_item) {
1287 // If the quick code is null, the Dex `code_item` is not
1288 // null, and the vmap table is not null, then this method has been compiled
1289 // with the dextodex compiler.
1290 return oat_method.GetQuickCode() == nullptr &&
1291 oat_method.GetVmapTable() != nullptr &&
1292 code_item != nullptr;
Roland Levillainf2650d12015-05-28 14:53:28 +01001293 }
1294
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001295 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001296 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001297 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001298 const DexFile* dex_file,
1299 const DexFile::ClassDef& class_def,
1300 const DexFile::CodeItem* code_item,
1301 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001302 if ((method_access_flags & kAccNative) == 0) {
1303 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001304 Runtime* const runtime = Runtime::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001305 Handle<mirror::DexCache> dex_cache(
Mathieu Chartierf284d442016-06-02 11:48:30 -07001306 hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file, nullptr)));
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001307 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001308 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001309 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
David Brazdil15fc7292016-09-02 14:13:18 +01001310 class_def, code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001311 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001312
1313 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001314 }
1315
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001316 // The StackMapsHelper provides the stack maps in the native PC order.
1317 // For identical native PCs, the order from the CodeInfo is preserved.
1318 class StackMapsHelper {
1319 public:
1320 explicit StackMapsHelper(const uint8_t* raw_code_info)
1321 : code_info_(raw_code_info),
1322 encoding_(code_info_.ExtractEncoding()),
1323 number_of_stack_maps_(code_info_.GetNumberOfStackMaps(encoding_)),
1324 indexes_(),
1325 offset_(static_cast<size_t>(-1)),
1326 stack_map_index_(0u) {
1327 if (number_of_stack_maps_ != 0u) {
1328 // Check if native PCs are ordered.
1329 bool ordered = true;
1330 StackMap last = code_info_.GetStackMapAt(0u, encoding_);
1331 for (size_t i = 1; i != number_of_stack_maps_; ++i) {
1332 StackMap current = code_info_.GetStackMapAt(i, encoding_);
1333 if (last.GetNativePcOffset(encoding_.stack_map_encoding) >
1334 current.GetNativePcOffset(encoding_.stack_map_encoding)) {
1335 ordered = false;
1336 break;
1337 }
1338 last = current;
1339 }
1340 if (!ordered) {
1341 // Create indirection indexes for access in native PC order. We do not optimize
1342 // for the fact that there can currently be only two separately ordered ranges,
1343 // namely normal stack maps and catch-point stack maps.
1344 indexes_.resize(number_of_stack_maps_);
1345 std::iota(indexes_.begin(), indexes_.end(), 0u);
1346 std::sort(indexes_.begin(),
1347 indexes_.end(),
1348 [this](size_t lhs, size_t rhs) {
1349 StackMap left = code_info_.GetStackMapAt(lhs, encoding_);
1350 uint32_t left_pc = left.GetNativePcOffset(encoding_.stack_map_encoding);
1351 StackMap right = code_info_.GetStackMapAt(rhs, encoding_);
1352 uint32_t right_pc = right.GetNativePcOffset(encoding_.stack_map_encoding);
1353 // If the PCs are the same, compare indexes to preserve the original order.
1354 return (left_pc < right_pc) || (left_pc == right_pc && lhs < rhs);
1355 });
1356 }
1357 offset_ = GetStackMapAt(0).GetNativePcOffset(encoding_.stack_map_encoding);
1358 }
1359 }
1360
1361 const CodeInfo& GetCodeInfo() const {
1362 return code_info_;
1363 }
1364
1365 const CodeInfoEncoding& GetEncoding() const {
1366 return encoding_;
1367 }
1368
1369 size_t GetOffset() const {
1370 return offset_;
1371 }
1372
1373 StackMap GetStackMap() const {
1374 return GetStackMapAt(stack_map_index_);
1375 }
1376
1377 void Next() {
1378 ++stack_map_index_;
1379 offset_ = (stack_map_index_ == number_of_stack_maps_)
1380 ? static_cast<size_t>(-1)
1381 : GetStackMapAt(stack_map_index_).GetNativePcOffset(encoding_.stack_map_encoding);
1382 }
1383
1384 private:
1385 StackMap GetStackMapAt(size_t i) const {
1386 if (!indexes_.empty()) {
1387 i = indexes_[i];
1388 }
1389 DCHECK_LT(i, number_of_stack_maps_);
1390 return code_info_.GetStackMapAt(i, encoding_);
1391 }
1392
1393 const CodeInfo code_info_;
1394 const CodeInfoEncoding encoding_;
1395 const size_t number_of_stack_maps_;
1396 dchecked_vector<size_t> indexes_; // Used if stack map native PCs are not ordered.
1397 size_t offset_;
1398 size_t stack_map_index_;
1399 };
1400
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001401 void DumpCode(VariableIndentationOutputStream* vios,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001402 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1403 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001404 const void* quick_code = oat_method.GetQuickCode();
1405
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001406 if (code_size == 0) {
1407 code_size = oat_method.GetQuickCodeSize();
1408 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001409 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001410 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001411 return;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001412 } else if (!bad_input && IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1413 // The optimizing compiler outputs its CodeInfo data in the vmap table.
1414 StackMapsHelper helper(oat_method.GetVmapTable());
1415 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1416 size_t offset = 0;
1417 while (offset < code_size) {
1418 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
1419 if (offset == helper.GetOffset()) {
1420 ScopedIndentation indent1(vios);
1421 StackMap stack_map = helper.GetStackMap();
1422 DCHECK(stack_map.IsValid());
1423 stack_map.Dump(vios,
1424 helper.GetCodeInfo(),
1425 helper.GetEncoding(),
1426 oat_method.GetCodeOffset(),
1427 code_item->registers_size_);
1428 do {
1429 helper.Next();
1430 // There may be multiple stack maps at a given PC. We display only the first one.
1431 } while (offset == helper.GetOffset());
1432 }
1433 DCHECK_LT(offset, helper.GetOffset());
1434 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001435 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001436 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1437 size_t offset = 0;
1438 while (offset < code_size) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001439 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001440 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001441 }
1442 }
1443
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001444 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001445 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001446 const OatDumperOptions& options_;
1447 uint32_t resolved_addr2instr_;
Andreas Gampe372f3a32016-08-19 10:49:06 -07001448 const InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001449 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001450 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001451};
1452
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001453class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001454 public:
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001455 ImageDumper(std::ostream* os,
1456 gc::space::ImageSpace& image_space,
1457 const ImageHeader& image_header,
1458 OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001459 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001460 vios_(os),
1461 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001462 image_space_(image_space),
1463 image_header_(image_header),
1464 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001465
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001466 bool Dump() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001467 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001468 std::ostream& indent_os = vios_.Stream();
1469
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001470 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001471
Jeff Haodcdc85b2015-12-04 14:06:18 -08001472 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1473
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001474 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001475
Mathieu Chartiere401d142015-04-22 13:56:20 -07001476 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1477
1478 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1479 auto section = static_cast<ImageHeader::ImageSections>(i);
1480 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1481 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001482
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001483 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001484
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001485 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001486
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001487 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1488
1489 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1490
1491 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001492
Alex Lighta59dd802014-07-02 16:28:08 -07001493 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1494
Igor Murashkin46774762014-10-22 11:37:02 -07001495 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1496
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001497 {
1498 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001499 static_assert(arraysize(image_roots_descriptions_) ==
1500 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001501 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1502 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1503 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001504 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001505 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001506 if (image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001507 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001508 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001509 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001510 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1511 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001512 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001513 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1514 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001515 run++;
1516 } else {
1517 break;
1518 }
1519 }
1520 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001521 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001522 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001523 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001524 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001525 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001526 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001527 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001528 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001529 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001530 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001531 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001532 }
1533 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001534 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001535
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001536 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001537 os << "METHOD ROOTS\n";
1538 static_assert(arraysize(image_methods_descriptions_) ==
1539 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1540 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1541 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1542 const char* description = image_methods_descriptions_[i];
1543 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001544 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001545 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001546 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001547 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001548
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001549 Runtime* const runtime = Runtime::Current();
1550 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001551 std::string image_filename = image_space_.GetImageFilename();
1552 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001553 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001554 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001555 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001556 const OatFile* oat_file = image_space_.GetOatFile();
Alex Lighta59dd802014-07-02 16:28:08 -07001557 if (oat_file == nullptr) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001558 oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location);
1559 }
1560 if (oat_file == nullptr) {
1561 oat_file = OatFile::Open(oat_location,
1562 oat_location,
1563 nullptr,
1564 nullptr,
1565 false,
1566 /*low_4gb*/false,
1567 nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001568 &error_msg);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001569 }
1570 if (oat_file == nullptr) {
1571 os << "OAT FILE NOT FOUND: " << error_msg << "\n";
1572 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001573 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001574 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001575
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001576 stats_.oat_file_bytes = oat_file->Size();
1577
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001578 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001579
Mathieu Chartier02e25112013-08-14 16:14:24 -07001580 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001581 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001582 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1583 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001584 }
1585
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001586 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001587
Jeff Haodcdc85b2015-12-04 14:06:18 -08001588 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001589 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001590 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001591 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001592 {
1593 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1594 heap->FlushAllocStack();
1595 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001596 // Since FlushAllocStack() above resets the (active) allocation
1597 // stack. Need to revoke the thread-local allocation stacks that
1598 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001599 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001600 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001601 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001602 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001603 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001604 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001605 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001606 {
1607 ReaderMutexLock mu(self, *class_linker->DexLock());
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001608 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001609 ObjPtr<mirror::DexCache> dex_cache =
1610 ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001611 if (dex_cache != nullptr) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001612 dex_caches_.insert(dex_cache.Ptr());
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001613 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001614 }
1615 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001616 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001617 // Dump the normal objects before ArtMethods.
1618 image_space_.GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1619 indent_os << "\n";
1620 // TODO: Dump fields.
1621 // Dump methods after.
Jeff Haodcdc85b2015-12-04 14:06:18 -08001622 DumpArtMethodVisitor visitor(this);
Mathieu Chartiere42888f2016-04-14 10:49:19 -07001623 image_header_.VisitPackedArtMethods(&visitor,
1624 image_space_.Begin(),
1625 image_header_.GetPointerSize());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001626 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001627 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001628 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001629 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001630 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001631 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001632 size_t data_size = image_header_.GetDataSize(); // stored size in file.
1633 if (file == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001634 LOG(WARNING) << "Failed to find image in " << image_filename;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001635 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001636 stats_.file_bytes = file->GetLength();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001637 // If the image is compressed, adjust to decompressed size.
1638 size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader);
1639 if (image_header_.GetStorageMode() == ImageHeader::kStorageModeUncompressed) {
1640 DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image";
1641 }
1642 stats_.file_bytes += uncompressed_size - data_size;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001643 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001644 size_t header_bytes = sizeof(ImageHeader);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001645 const auto& object_section = image_header_.GetImageSection(ImageHeader::kSectionObjects);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001646 const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1647 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001648 const auto& dex_cache_arrays_section = image_header_.GetImageSection(
1649 ImageHeader::kSectionDexCacheArrays);
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001650 const auto& intern_section = image_header_.GetImageSection(
1651 ImageHeader::kSectionInternedStrings);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001652 const auto& class_table_section = image_header_.GetImageSection(
1653 ImageHeader::kSectionClassTable);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001654 const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1655
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001656 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001657
1658 // Objects are kObjectAlignment-aligned.
1659 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1660 if (object_section.Offset() > header_bytes) {
1661 stats_.alignment_bytes += object_section.Offset() - header_bytes;
1662 }
1663
1664 // Field section is 4-byte aligned.
1665 constexpr size_t kFieldSectionAlignment = 4U;
1666 uint32_t end_objects = object_section.Offset() + object_section.Size();
1667 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
1668 stats_.alignment_bytes += field_section.Offset() - end_objects;
1669
1670 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
1671 uint32_t end_fields = field_section.Offset() + field_section.Size();
1672 CHECK_ALIGNED(method_section.Offset(), 4);
1673 stats_.alignment_bytes += method_section.Offset() - end_fields;
1674
1675 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
1676 uint32_t end_methods = method_section.Offset() + method_section.Size();
1677 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
1678 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
1679
1680 // Intern table is 8-byte aligned.
1681 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
1682 CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
1683 stats_.alignment_bytes += intern_section.Offset() - end_caches;
1684
1685 // Add space between intern table and class table.
1686 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
1687 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
1688
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001689 // Add space between end of image data and bitmap. Expect the bitmap to be page-aligned.
1690 const size_t bitmap_offset = sizeof(ImageHeader) + data_size;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001691 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001692 stats_.alignment_bytes += RoundUp(bitmap_offset, kPageSize) - bitmap_offset;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001693
Mathieu Chartiere401d142015-04-22 13:56:20 -07001694 stats_.bitmap_bytes += bitmap_section.Size();
1695 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01001696 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01001697 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001698 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001699 stats_.class_table_bytes += class_table_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001700 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001701 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001702
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001703 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001704
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001705 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001706 }
1707
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001708 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001709 class DumpArtMethodVisitor : public ArtMethodVisitor {
1710 public:
1711 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
1712
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001713 virtual void Visit(ArtMethod* method) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001714 std::ostream& indent_os = image_dumper_->vios_.Stream();
David Sehr709b0702016-10-13 09:12:37 -07001715 indent_os << method << " " << " ArtMethod: " << ArtMethod::PrettyMethod(method) << "\n";
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001716 image_dumper_->DumpMethod(method, indent_os);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001717 indent_os << "\n";
1718 }
1719
1720 private:
1721 ImageDumper* const image_dumper_;
1722 };
1723
Mathieu Chartier3398c782016-09-30 10:27:43 -07001724 static void PrettyObjectValue(std::ostream& os,
1725 ObjPtr<mirror::Class> type,
1726 ObjPtr<mirror::Object> value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001727 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001728 CHECK(type != nullptr);
1729 if (value == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07001730 os << StringPrintf("null %s\n", type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001731 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001732 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001733 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001734 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001735 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001736 mirror::Class* klass = value->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07001737 os << StringPrintf("%p Class: %s\n", klass, mirror::Class::PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001738 } else {
David Sehr709b0702016-10-13 09:12:37 -07001739 os << StringPrintf("%p %s\n", value.Ptr(), type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001740 }
1741 }
1742
Mathieu Chartier3398c782016-09-30 10:27:43 -07001743 static void PrintField(std::ostream& os, ArtField* field, ObjPtr<mirror::Object> obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001744 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001745 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08001746 switch (field->GetTypeAsPrimitiveType()) {
1747 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08001748 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001749 break;
1750 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001751 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001752 break;
1753 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001754 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001755 break;
1756 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001757 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001758 break;
1759 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07001760 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001761 break;
1762 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07001763 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001764 break;
1765 case Primitive::kPrimBoolean:
Fred Shih37f05ef2014-07-16 18:38:08 -07001766 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1767 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001768 break;
1769 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07001770 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001771 break;
1772 case Primitive::kPrimNot: {
1773 // Get the value, don't compute the type unless it is non-null as we don't want
1774 // to cause class loading.
Mathieu Chartier3398c782016-09-30 10:27:43 -07001775 ObjPtr<mirror::Object> value = field->GetObj(obj);
Ian Rogers08f1f502014-12-02 15:04:37 -08001776 if (value == nullptr) {
1777 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07001778 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08001779 // Grab the field type without causing resolution.
Mathieu Chartier3398c782016-09-30 10:27:43 -07001780 ObjPtr<mirror::Class> field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08001781 if (field_type != nullptr) {
1782 PrettyObjectValue(os, field_type, value);
1783 } else {
Mathieu Chartier3398c782016-09-30 10:27:43 -07001784 os << StringPrintf("%p %s\n",
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07001785 value.Ptr(),
Ian Rogers08f1f502014-12-02 15:04:37 -08001786 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
1787 }
Ian Rogers50239c72013-06-17 14:53:22 -07001788 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001789 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07001790 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001791 default:
1792 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
1793 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08001794 }
1795 }
1796
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001797 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001798 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001799 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001800 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001801 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001802 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001803 for (ArtField& field : klass->GetIFields()) {
1804 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001805 }
1806 }
1807
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001808 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001809 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001810 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001811
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001812 const void* GetQuickOatCodeBegin(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001813 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001814 image_header_.GetPointerSize());
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001815 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001816 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001817 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001818 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001819 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001820 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001821 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001822 }
1823
Mathieu Chartiere401d142015-04-22 13:56:20 -07001824 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001825 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001826 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1827 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001828 return 0;
1829 }
1830 return oat_code_begin[-1];
1831 }
1832
Mathieu Chartiere401d142015-04-22 13:56:20 -07001833 const void* GetQuickOatCodeEnd(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001834 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001835 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001836 if (oat_code_begin == nullptr) {
1837 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001838 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001839 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001840 }
1841
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001842 static void Callback(mirror::Object* obj, void* arg) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001843 DCHECK(obj != nullptr);
1844 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001845 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001846 if (!state->InDumpSpace(obj)) {
1847 return;
1848 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001849
1850 size_t object_bytes = obj->SizeOf();
1851 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1852 state->stats_.object_bytes += object_bytes;
1853 state->stats_.alignment_bytes += alignment_bytes;
1854
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001855 std::ostream& os = state->vios_.Stream();
1856
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001857 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001858 if (obj_class->IsArrayClass()) {
David Sehr709b0702016-10-13 09:12:37 -07001859 os << StringPrintf("%p: %s length:%d\n", obj, obj_class->PrettyDescriptor().c_str(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001860 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001861 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001862 mirror::Class* klass = obj->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07001863 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj,
1864 mirror::Class::PrettyDescriptor(klass).c_str())
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001865 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08001866 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001867 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001868 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001869 } else {
David Sehr709b0702016-10-13 09:12:37 -07001870 os << StringPrintf("%p: %s\n", obj, obj_class->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001871 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001872 ScopedIndentation indent1(&state->vios_);
1873 DumpFields(os, obj, obj_class);
Andreas Gampe542451c2016-07-26 09:02:02 -07001874 const PointerSize image_pointer_size = state->image_header_.GetPointerSize();
Ian Rogersd5b32602012-02-26 16:40:04 -08001875 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001876 auto* obj_array = obj->AsObjectArray<mirror::Object>();
1877 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001878 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001879 size_t run = 0;
1880 for (int32_t j = i + 1; j < length; j++) {
1881 if (value == obj_array->Get(j)) {
1882 run++;
1883 } else {
1884 break;
1885 }
1886 }
1887 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001888 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001889 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001890 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001891 i = i + run;
1892 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001893 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001894 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001895 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001896 }
1897 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001898 mirror::Class* klass = obj->AsClass();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001899 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001900 os << "STATICS:\n";
1901 ScopedIndentation indent2(&state->vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001902 for (ArtField& field : klass->GetSFields()) {
1903 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001904 }
1905 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001906 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01001907 auto it = state->dex_caches_.find(obj);
1908 if (it != state->dex_caches_.end()) {
1909 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001910 const auto& field_section = state->image_header_.GetImageSection(
1911 ImageHeader::kSectionArtFields);
1912 const auto& method_section = state->image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001913 size_t num_methods = dex_cache->NumResolvedMethods();
1914 if (num_methods != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001915 os << "Methods (size=" << num_methods << "):\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01001916 ScopedIndentation indent2(&state->vios_);
1917 auto* resolved_methods = dex_cache->GetResolvedMethods();
1918 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001919 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_methods,
1920 i,
1921 image_pointer_size);
Vladimir Marko05792b92015-08-03 11:56:49 +01001922 size_t run = 0;
1923 for (size_t j = i + 1;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001924 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_methods,
1925 j,
1926 image_pointer_size);
1927 ++j) {
1928 ++run;
1929 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001930 if (run == 0) {
1931 os << StringPrintf("%zd: ", i);
1932 } else {
1933 os << StringPrintf("%zd to %zd: ", i, i + run);
1934 i = i + run;
1935 }
1936 std::string msg;
1937 if (elem == nullptr) {
1938 msg = "null";
1939 } else if (method_section.Contains(
1940 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07001941 msg = reinterpret_cast<ArtMethod*>(elem)->PrettyMethod();
Vladimir Marko05792b92015-08-03 11:56:49 +01001942 } else {
1943 msg = "<not in method section>";
1944 }
1945 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07001946 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001947 }
1948 size_t num_fields = dex_cache->NumResolvedFields();
1949 if (num_fields != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001950 os << "Fields (size=" << num_fields << "):\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01001951 ScopedIndentation indent2(&state->vios_);
1952 auto* resolved_fields = dex_cache->GetResolvedFields();
1953 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001954 auto* elem = mirror::DexCache::GetElementPtrSize(
1955 resolved_fields, i, image_pointer_size);
Vladimir Marko05792b92015-08-03 11:56:49 +01001956 size_t run = 0;
1957 for (size_t j = i + 1;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001958 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_fields,
1959 j,
1960 image_pointer_size);
1961 ++j) {
1962 ++run;
1963 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001964 if (run == 0) {
1965 os << StringPrintf("%zd: ", i);
1966 } else {
1967 os << StringPrintf("%zd to %zd: ", i, i + run);
1968 i = i + run;
1969 }
1970 std::string msg;
1971 if (elem == nullptr) {
1972 msg = "null";
1973 } else if (field_section.Contains(
1974 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07001975 msg = reinterpret_cast<ArtField*>(elem)->PrettyField();
Vladimir Marko05792b92015-08-03 11:56:49 +01001976 } else {
1977 msg = "<not in field section>";
1978 }
1979 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07001980 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001981 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001982 size_t num_types = dex_cache->NumResolvedTypes();
1983 if (num_types != 0u) {
1984 os << "Types (size=" << num_types << "):\n";
1985 ScopedIndentation indent2(&state->vios_);
1986 auto* resolved_types = dex_cache->GetResolvedTypes();
1987 for (size_t i = 0; i < num_types; ++i) {
1988 auto* elem = resolved_types[i].Read();
1989 size_t run = 0;
1990 for (size_t j = i + 1; j != num_types && elem == resolved_types[j].Read(); ++j) {
1991 ++run;
1992 }
1993 if (run == 0) {
1994 os << StringPrintf("%zd: ", i);
1995 } else {
1996 os << StringPrintf("%zd to %zd: ", i, i + run);
1997 i = i + run;
1998 }
1999 std::string msg;
2000 if (elem == nullptr) {
2001 msg = "null";
2002 } else {
David Sehr709b0702016-10-13 09:12:37 -07002003 msg = elem->PrettyClass();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002004 }
2005 os << StringPrintf("%p %s\n", elem, msg.c_str());
2006 }
2007 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002008 }
2009 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002010 std::string temp;
2011 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002012 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002013
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002014 void DumpMethod(ArtMethod* method, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002015 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002016 DCHECK(method != nullptr);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002017 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
2018 const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
Andreas Gampe542451c2016-07-26 09:02:02 -07002019 const PointerSize pointer_size = image_header_.GetPointerSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002020 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2021 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002022 if (method->IsNative()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002023 bool first_occurrence;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002024 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2025 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002026 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002027 stats_.native_to_managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002028 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002029 if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
2030 image_header_.GetPointerSize())) {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002031 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002032 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002033 } else if (method->IsAbstract() || method->IsClassInitializer()) {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002034 // Don't print information for these.
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002035 } else if (method->IsRuntimeMethod()) {
2036 ImtConflictTable* table = method->GetImtConflictTable(image_header_.GetPointerSize());
2037 if (table != nullptr) {
2038 indent_os << "IMT conflict table " << table << " method: ";
2039 for (size_t i = 0, count = table->NumEntries(pointer_size); i < count; ++i) {
David Sehr709b0702016-10-13 09:12:37 -07002040 indent_os << ArtMethod::PrettyMethod(table->GetImplementationMethod(i, pointer_size))
2041 << " ";
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002042 }
2043 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002044 } else {
2045 const DexFile::CodeItem* code_item = method->GetCodeItem();
2046 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002047 stats_.dex_instruction_bytes += dex_instruction_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002048
2049 bool first_occurrence;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002050 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002051 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01002052 // Method compiled with the optimizing compiler have no vmap table.
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002053 vmap_table_bytes = ComputeOatSize(method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002054 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002055 stats_.vmap_table_bytes += vmap_table_bytes;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002056 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002057 }
2058
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002059 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2060 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002061 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002062 stats_.managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002063 if (method->IsConstructor()) {
2064 if (method->IsStatic()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002065 stats_.class_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002066 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002067 stats_.large_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002068 }
2069 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002070 stats_.large_method_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002071 }
2072 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002073 stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002074
Igor Murashkin7617abd2015-07-10 18:27:47 -07002075 uint32_t method_access_flags = method->GetAccessFlags();
2076
Mathieu Chartiere401d142015-04-22 13:56:20 -07002077 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002078 indent_os << StringPrintf("SIZE: Dex Instructions=%zd StackMaps=%zd AccessFlags=0x%x\n",
2079 dex_instruction_bytes,
2080 vmap_table_bytes,
Igor Murashkin7617abd2015-07-10 18:27:47 -07002081 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002082
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002083 size_t total_size = dex_instruction_bytes +
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002084 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002085
2086 double expansion =
2087 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002088 stats_.ComputeOutliers(total_size, expansion, method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002089 }
2090 }
2091
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002092 std::set<const void*> already_seen_;
2093 // Compute the size of the given data within the oat file and whether this is the first time
2094 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002095 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002096 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002097 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002098 already_seen_.insert(oat_data);
2099 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002100 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002101 }
2102 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002103 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002104
2105 public:
2106 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002107 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002108 size_t file_bytes;
2109
2110 size_t header_bytes;
2111 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002112 size_t art_field_bytes;
2113 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01002114 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002115 size_t interned_strings_bytes;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002116 size_t class_table_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07002117 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002118 size_t alignment_bytes;
2119
2120 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002121 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002122 size_t managed_to_native_code_bytes;
2123 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07002124 size_t class_initializer_code_bytes;
2125 size_t large_initializer_code_bytes;
2126 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002127
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002128 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002129
2130 size_t dex_instruction_bytes;
2131
Mathieu Chartiere401d142015-04-22 13:56:20 -07002132 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002133 std::vector<size_t> method_outlier_size;
2134 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002135 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002136
Roland Levillain3887c462015-08-12 18:15:42 +01002137 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002138 : oat_file_bytes(0),
2139 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002140 header_bytes(0),
2141 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002142 art_field_bytes(0),
2143 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01002144 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002145 interned_strings_bytes(0),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002146 class_table_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07002147 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002148 alignment_bytes(0),
2149 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002150 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002151 managed_to_native_code_bytes(0),
2152 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07002153 class_initializer_code_bytes(0),
2154 large_initializer_code_bytes(0),
2155 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002156 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002157 dex_instruction_bytes(0) {}
2158
Elliott Hughesa0e18062012-04-13 15:59:59 -07002159 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002160 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002161 size_t bytes;
2162 size_t count;
2163 };
2164 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2165 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002166
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002167 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002168 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2169 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002170 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002171 it->second.count += 1;
2172 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002173 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002174 }
2175 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002176
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002177 double PercentOfOatBytes(size_t size) {
2178 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2179 }
2180
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002181 double PercentOfFileBytes(size_t size) {
2182 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2183 }
2184
2185 double PercentOfObjectBytes(size_t size) {
2186 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2187 }
2188
Mathieu Chartiere401d142015-04-22 13:56:20 -07002189 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002190 method_outlier_size.push_back(total_size);
2191 method_outlier_expansion.push_back(expansion);
2192 method_outlier.push_back(method);
2193 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002194
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002195 void DumpOutliers(std::ostream& os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002196 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002197 size_t sum_of_sizes = 0;
2198 size_t sum_of_sizes_squared = 0;
2199 size_t sum_of_expansion = 0;
2200 size_t sum_of_expansion_squared = 0;
2201 size_t n = method_outlier_size.size();
Mathieu Chartier1ebf8d32016-06-09 11:51:27 -07002202 if (n <= 1) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08002203 return;
2204 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002205 for (size_t i = 0; i < n; i++) {
2206 size_t cur_size = method_outlier_size[i];
2207 sum_of_sizes += cur_size;
2208 sum_of_sizes_squared += cur_size * cur_size;
2209 double cur_expansion = method_outlier_expansion[i];
2210 sum_of_expansion += cur_expansion;
2211 sum_of_expansion_squared += cur_expansion * cur_expansion;
2212 }
2213 size_t size_mean = sum_of_sizes / n;
2214 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2215 double expansion_mean = sum_of_expansion / n;
2216 double expansion_variance =
2217 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2218
2219 // Dump methods whose size is a certain number of standard deviations from the mean
2220 size_t dumped_values = 0;
2221 size_t skipped_values = 0;
2222 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2223 size_t cur_size_variance = i * i * size_variance;
2224 bool first = true;
2225 for (size_t j = 0; j < n; j++) {
2226 size_t cur_size = method_outlier_size[j];
2227 if (cur_size > size_mean) {
2228 size_t cur_var = cur_size - size_mean;
2229 cur_var = cur_var * cur_var;
2230 if (cur_var > cur_size_variance) {
2231 if (dumped_values > 20) {
2232 if (i == 1) {
2233 skipped_values++;
2234 } else {
2235 i = 2; // jump to counting for 1 standard deviation
2236 break;
2237 }
2238 } else {
2239 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002240 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002241 first = false;
2242 }
David Sehr709b0702016-10-13 09:12:37 -07002243 os << ArtMethod::PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002244 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002245 method_outlier_size[j] = 0; // don't consider this method again
2246 dumped_values++;
2247 }
2248 }
2249 }
2250 }
2251 }
2252 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002253 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002254 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002255 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002256 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002257
2258 // Dump methods whose expansion is a certain number of standard deviations from the mean
2259 dumped_values = 0;
2260 skipped_values = 0;
2261 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2262 double cur_expansion_variance = i * i * expansion_variance;
2263 bool first = true;
2264 for (size_t j = 0; j < n; j++) {
2265 double cur_expansion = method_outlier_expansion[j];
2266 if (cur_expansion > expansion_mean) {
2267 size_t cur_var = cur_expansion - expansion_mean;
2268 cur_var = cur_var * cur_var;
2269 if (cur_var > cur_expansion_variance) {
2270 if (dumped_values > 20) {
2271 if (i == 1) {
2272 skipped_values++;
2273 } else {
2274 i = 2; // jump to counting for 1 standard deviation
2275 break;
2276 }
2277 } else {
2278 if (first) {
2279 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002280 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002281 first = false;
2282 }
David Sehr709b0702016-10-13 09:12:37 -07002283 os << ArtMethod::PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002284 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002285 method_outlier_expansion[j] = 0.0; // don't consider this method again
2286 dumped_values++;
2287 }
2288 }
2289 }
2290 }
2291 }
2292 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002293 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002294 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002295 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002296 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002297 }
2298
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002299 void Dump(std::ostream& os, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002300 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002301 {
2302 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2303 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002304 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2305 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2306 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2307 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2308 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2309 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002310 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002311 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2312 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002313 header_bytes, PercentOfFileBytes(header_bytes),
2314 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002315 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2316 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002317 dex_cache_arrays_bytes,
2318 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002319 interned_strings_bytes,
2320 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002321 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002322 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002323 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2324 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002325 CHECK_EQ(file_bytes,
2326 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2327 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2328 bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002329 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002330
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002331 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002332 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002333 for (const auto& sizes_and_count : sizes_and_counts) {
2334 const std::string& descriptor(sizes_and_count.first);
2335 double average = static_cast<double>(sizes_and_count.second.bytes) /
2336 static_cast<double>(sizes_and_count.second.count);
2337 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002338 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002339 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002340 descriptor.c_str(), sizes_and_count.second.bytes,
2341 sizes_and_count.second.count, average, percent);
2342 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002343 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002344 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002345 CHECK_EQ(object_bytes, object_bytes_total);
2346
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002347 os << StringPrintf("oat_file_bytes = %8zd\n"
2348 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2349 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2350 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2351 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2352 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2353 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002354 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002355 managed_code_bytes,
2356 PercentOfOatBytes(managed_code_bytes),
2357 managed_to_native_code_bytes,
2358 PercentOfOatBytes(managed_to_native_code_bytes),
2359 native_to_managed_code_bytes,
2360 PercentOfOatBytes(native_to_managed_code_bytes),
2361 class_initializer_code_bytes,
2362 PercentOfOatBytes(class_initializer_code_bytes),
2363 large_initializer_code_bytes,
2364 PercentOfOatBytes(large_initializer_code_bytes),
2365 large_method_code_bytes,
2366 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002367 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002368 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002369 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002370 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2371 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002372 }
2373
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002374 os << "\n" << StringPrintf("vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002375 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002376 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002377
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002378 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2379 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002380 static_cast<double>(managed_code_bytes) /
2381 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002382 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002383 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002384 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002385
2386 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002387 }
2388 } stats_;
2389
2390 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002391 enum {
2392 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2393 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2394 kLargeConstructorDexBytes = 4000,
2395 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2396 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2397 kLargeMethodDexBytes = 16000
2398 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002399
2400 // For performance, use the *os_ directly for anything that doesn't need indentation
2401 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002402 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002403 VariableIndentationOutputStream vios_;
2404 ScopedIndentation indent1_;
2405
Ian Rogers1d54e732013-05-02 21:10:01 -07002406 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002407 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002408 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002409 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002410 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002411
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002412 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002413};
2414
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002415static int DumpImage(gc::space::ImageSpace* image_space,
2416 OatDumperOptions* options,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002417 std::ostream* os) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002418 const ImageHeader& image_header = image_space->GetImageHeader();
2419 if (!image_header.IsValid()) {
2420 fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
2421 return EXIT_FAILURE;
2422 }
2423 ImageDumper image_dumper(os, *image_space, image_header, options);
2424 if (!image_dumper.Dump()) {
2425 return EXIT_FAILURE;
2426 }
2427 return EXIT_SUCCESS;
2428}
2429
2430static int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002431 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002432 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002433 options->class_loader_ = &null_class_loader;
2434
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002435 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002436 if (options->app_image_ != nullptr) {
2437 if (options->app_oat_ == nullptr) {
2438 LOG(ERROR) << "Can not dump app image without app oat file";
Jeff Haodcdc85b2015-12-04 14:06:18 -08002439 return EXIT_FAILURE;
2440 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002441 // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
2442 // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
2443 // pointers into 32 bit pointer sized ArtMethods.
2444 std::string error_msg;
2445 std::unique_ptr<OatFile> oat_file(OatFile::Open(options->app_oat_,
2446 options->app_oat_,
2447 nullptr,
2448 nullptr,
2449 false,
2450 /*low_4gb*/true,
2451 nullptr,
2452 &error_msg));
2453 if (oat_file == nullptr) {
2454 LOG(ERROR) << "Failed to open oat file " << options->app_oat_ << " with error " << error_msg;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002455 return EXIT_FAILURE;
2456 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002457 std::unique_ptr<gc::space::ImageSpace> space(
2458 gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg));
2459 if (space == nullptr) {
2460 LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error "
2461 << error_msg;
2462 }
2463 // Open dex files for the image.
2464 std::vector<std::unique_ptr<const DexFile>> dex_files;
2465 if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
2466 LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
2467 << error_msg;
2468 }
2469 // Dump the actual image.
2470 int result = DumpImage(space.get(), options, os);
2471 if (result != EXIT_SUCCESS) {
2472 return result;
2473 }
2474 // Fall through to dump the boot images.
2475 }
2476
2477 gc::Heap* heap = runtime->GetHeap();
2478 CHECK(heap->HasBootImageSpace()) << "No image spaces";
2479 for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
2480 int result = DumpImage(image_space, options, os);
2481 if (result != EXIT_SUCCESS) {
2482 return result;
2483 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002484 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002485 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002486}
2487
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002488static jobject InstallOatFile(Runtime* runtime,
2489 std::unique_ptr<OatFile> oat_file,
2490 std::vector<const DexFile*>* class_path)
2491 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002492 Thread* self = Thread::Current();
2493 CHECK(self != nullptr);
2494 // Need well-known-classes.
2495 WellKnownClasses::Init(self->GetJniEnv());
2496
2497 // Need to register dex files to get a working dex cache.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002498 OatFile* oat_file_ptr = oat_file.get();
Andreas Gampe00b25f32014-09-17 21:49:05 -07002499 ClassLinker* class_linker = runtime->GetClassLinker();
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002500 runtime->GetOatFileManager().RegisterOatFile(std::move(oat_file));
2501 for (const OatFile::OatDexFile* odf : oat_file_ptr->GetOatDexFiles()) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002502 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002503 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002504 CHECK(dex_file != nullptr) << error_msg;
Mathieu Chartierf284d442016-06-02 11:48:30 -07002505 class_linker->RegisterDexFile(*dex_file, nullptr);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002506 class_path->push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002507 }
2508
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002509 // Need a class loader. Fake that we're a compiler.
2510 // Note: this will run initializers through the unstarted runtime, so make sure it's
2511 // initialized.
2512 interpreter::UnstartedRuntime::Initialize();
2513
2514 jobject class_loader = class_linker->CreatePathClassLoader(self, *class_path);
2515
2516 return class_loader;
2517}
2518
2519static int DumpOatWithRuntime(Runtime* runtime,
2520 std::unique_ptr<OatFile> oat_file,
2521 OatDumperOptions* options,
2522 std::ostream* os) {
2523 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2524 ScopedObjectAccess soa(Thread::Current());
2525
2526 OatFile* oat_file_ptr = oat_file.get();
2527 std::vector<const DexFile*> class_path;
2528 jobject class_loader = InstallOatFile(runtime, std::move(oat_file), &class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002529
2530 // Use the class loader while dumping.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002531 StackHandleScope<1> scope(soa.Self());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002532 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07002533 soa.Decode<mirror::ClassLoader>(class_loader));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002534 options->class_loader_ = &loader_handle;
2535
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002536 OatDumper oat_dumper(*oat_file_ptr, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002537 bool success = oat_dumper.Dump(*os);
2538 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2539}
2540
2541static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002542 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002543 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002544 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002545 options->class_loader_ = &null_class_loader;
2546
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002547 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002548 bool success = oat_dumper.Dump(*os);
2549 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2550}
2551
2552static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2553 std::ostream* os) {
2554 std::string error_msg;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002555 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
2556 oat_filename,
2557 nullptr,
2558 nullptr,
2559 false,
2560 /*low_4gb*/false,
2561 nullptr,
2562 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002563 if (oat_file == nullptr) {
2564 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2565 return EXIT_FAILURE;
2566 }
2567
2568 if (runtime != nullptr) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002569 return DumpOatWithRuntime(runtime, std::move(oat_file), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002570 } else {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002571 return DumpOatWithoutRuntime(oat_file.get(), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002572 }
2573}
2574
David Srbecky2fdd03c2016-03-10 15:32:37 +00002575static int SymbolizeOat(const char* oat_filename, std::string& output_name, bool no_bits) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002576 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002577 OatFile* oat_file = OatFile::Open(oat_filename,
2578 oat_filename,
2579 nullptr,
2580 nullptr,
2581 false,
2582 /*low_4gb*/false,
2583 nullptr,
2584 &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002585 if (oat_file == nullptr) {
2586 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2587 return EXIT_FAILURE;
2588 }
2589
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002590 bool result;
2591 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
2592 // files for 64-bit code in the past.
2593 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002594 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file, output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002595 result = oat_symbolizer.Symbolize();
2596 } else {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002597 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file, output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002598 result = oat_symbolizer.Symbolize();
2599 }
2600 if (!result) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002601 fprintf(stderr, "Failed to symbolize\n");
2602 return EXIT_FAILURE;
2603 }
2604
2605 return EXIT_SUCCESS;
2606}
2607
Andreas Gampe9fded872016-09-25 16:08:35 -07002608class IMTDumper {
2609 public:
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002610 static bool Dump(Runtime* runtime,
2611 const std::string& imt_file,
2612 bool dump_imt_stats,
2613 const char* oat_filename) {
2614 Thread* self = Thread::Current();
2615
2616 ScopedObjectAccess soa(self);
2617 StackHandleScope<1> scope(self);
2618 MutableHandle<mirror::ClassLoader> class_loader = scope.NewHandle<mirror::ClassLoader>(nullptr);
2619 std::vector<const DexFile*> class_path;
2620
2621 if (oat_filename != nullptr) {
2622 std::string error_msg;
2623 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
2624 oat_filename,
2625 nullptr,
2626 nullptr,
2627 false,
2628 /*low_4gb*/false,
2629 nullptr,
2630 &error_msg));
2631 if (oat_file == nullptr) {
2632 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2633 return false;
2634 }
2635
2636 class_loader.Assign(soa.Decode<mirror::ClassLoader>(
2637 InstallOatFile(runtime, std::move(oat_file), &class_path)));
2638 } else {
2639 class_loader.Assign(nullptr); // Boot classloader. Just here for explicit documentation.
2640 class_path = runtime->GetClassLinker()->GetBootClassPath();
2641 }
2642
2643 if (!imt_file.empty()) {
2644 return DumpImt(runtime, imt_file, class_loader);
2645 }
2646
2647 if (dump_imt_stats) {
2648 return DumpImtStats(runtime, class_path, class_loader);
2649 }
2650
2651 LOG(FATAL) << "Should not reach here";
2652 UNREACHABLE();
2653 }
2654
2655 private:
2656 static bool DumpImt(Runtime* runtime,
2657 const std::string& imt_file,
2658 Handle<mirror::ClassLoader> h_class_loader)
2659 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002660 std::vector<std::string> lines = ReadCommentedInputFromFile(imt_file);
2661 std::unordered_set<std::string> prepared;
2662
2663 for (const std::string& line : lines) {
2664 // A line should be either a class descriptor, in which case we will dump the complete IMT,
2665 // or a class descriptor and an interface method, in which case we will lookup the method,
2666 // determine its IMT slot, and check the class' IMT.
2667 size_t first_space = line.find(' ');
2668 if (first_space == std::string::npos) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002669 DumpIMTForClass(runtime, line, h_class_loader, &prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07002670 } else {
2671 DumpIMTForMethod(runtime,
2672 line.substr(0, first_space),
2673 line.substr(first_space + 1, std::string::npos),
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002674 h_class_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07002675 &prepared);
2676 }
2677 std::cerr << std::endl;
2678 }
2679
2680 return true;
2681 }
2682
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002683 static bool DumpImtStats(Runtime* runtime,
2684 const std::vector<const DexFile*>& dex_files,
2685 Handle<mirror::ClassLoader> h_class_loader)
2686 REQUIRES_SHARED(Locks::mutator_lock_) {
2687 size_t without_imt = 0;
2688 size_t with_imt = 0;
Andreas Gampe9fded872016-09-25 16:08:35 -07002689 std::map<size_t, size_t> histogram;
2690
2691 ClassLinker* class_linker = runtime->GetClassLinker();
2692 const PointerSize pointer_size = class_linker->GetImagePointerSize();
2693 std::unordered_set<std::string> prepared;
2694
2695 Thread* self = Thread::Current();
Andreas Gampe9fded872016-09-25 16:08:35 -07002696 StackHandleScope<1> scope(self);
2697 MutableHandle<mirror::Class> h_klass(scope.NewHandle<mirror::Class>(nullptr));
2698
2699 for (const DexFile* dex_file : dex_files) {
2700 for (uint32_t class_def_index = 0;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002701 class_def_index != dex_file->NumClassDefs();
2702 ++class_def_index) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002703 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
2704 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002705 h_klass.Assign(class_linker->FindClass(self, descriptor, h_class_loader));
Andreas Gampe9fded872016-09-25 16:08:35 -07002706 if (h_klass.Get() == nullptr) {
2707 std::cerr << "Warning: could not load " << descriptor << std::endl;
2708 continue;
2709 }
2710
2711 if (HasNoIMT(runtime, h_klass, pointer_size, &prepared)) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002712 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07002713 continue;
2714 }
2715
2716 ImTable* im_table = PrepareAndGetImTable(runtime, h_klass, pointer_size, &prepared);
2717 if (im_table == nullptr) {
2718 // Should not happen, but accept.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002719 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07002720 continue;
2721 }
2722
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002723 with_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07002724 for (size_t imt_index = 0; imt_index != ImTable::kSize; ++imt_index) {
2725 ArtMethod* ptr = im_table->Get(imt_index, pointer_size);
2726 if (ptr->IsRuntimeMethod()) {
2727 if (ptr->IsImtUnimplementedMethod()) {
2728 histogram[0]++;
2729 } else {
2730 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
2731 histogram[current_table->NumEntries(pointer_size)]++;
2732 }
2733 } else {
2734 histogram[1]++;
2735 }
2736 }
2737 }
2738 }
2739
2740 std::cerr << "IMT stats:"
2741 << std::endl << std::endl;
2742
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002743 std::cerr << " " << with_imt << " classes with IMT."
Andreas Gampe9fded872016-09-25 16:08:35 -07002744 << std::endl << std::endl;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002745 std::cerr << " " << without_imt << " classes without IMT (or copy from Object)."
Andreas Gampe9fded872016-09-25 16:08:35 -07002746 << std::endl << std::endl;
2747
2748 double sum_one = 0;
2749 size_t count_one = 0;
2750
2751 std::cerr << " " << "IMT histogram" << std::endl;
2752 for (auto& bucket : histogram) {
2753 std::cerr << " " << bucket.first << " " << bucket.second << std::endl;
2754 if (bucket.first > 0) {
2755 sum_one += bucket.second * bucket.first;
2756 count_one += bucket.second;
2757 }
2758 }
2759
2760 double count_zero = count_one + histogram[0];
2761 std::cerr << " Stats:" << std::endl;
2762 std::cerr << " Average depth (including empty): " << (sum_one / count_zero) << std::endl;
2763 std::cerr << " Average depth (excluding empty): " << (sum_one / count_one) << std::endl;
2764
2765 return true;
2766 }
2767
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002768 // Return whether the given class has no IMT (or the one shared with java.lang.Object).
Andreas Gampe9fded872016-09-25 16:08:35 -07002769 static bool HasNoIMT(Runtime* runtime,
2770 Handle<mirror::Class> klass,
2771 const PointerSize pointer_size,
2772 std::unordered_set<std::string>* prepared)
2773 REQUIRES_SHARED(Locks::mutator_lock_) {
2774 if (klass->IsObjectClass() || !klass->ShouldHaveImt()) {
2775 return true;
2776 }
2777
2778 if (klass->GetImt(pointer_size) == nullptr) {
2779 PrepareClass(runtime, klass, prepared);
2780 }
2781
2782 mirror::Class* object_class = mirror::Class::GetJavaLangClass()->GetSuperClass();
2783 DCHECK(object_class->IsObjectClass());
2784
2785 bool result = klass->GetImt(pointer_size) == object_class->GetImt(pointer_size);
2786
Mathieu Chartier6beced42016-11-15 15:51:31 -08002787 if (klass->GetIfTable()->Count() == 0) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002788 DCHECK(result);
2789 }
2790
2791 return result;
2792 }
2793
2794 static void PrintTable(ImtConflictTable* table, PointerSize pointer_size)
2795 REQUIRES_SHARED(Locks::mutator_lock_) {
2796 if (table == nullptr) {
2797 std::cerr << " <No IMT?>" << std::endl;
2798 return;
2799 }
2800 size_t table_index = 0;
2801 for (;;) {
2802 ArtMethod* ptr = table->GetInterfaceMethod(table_index, pointer_size);
2803 if (ptr == nullptr) {
2804 return;
2805 }
2806 table_index++;
David Sehr709b0702016-10-13 09:12:37 -07002807 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07002808 }
2809 }
2810
2811 static ImTable* PrepareAndGetImTable(Runtime* runtime,
2812 Thread* self,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002813 Handle<mirror::ClassLoader> h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07002814 const std::string& class_name,
2815 const PointerSize pointer_size,
2816 mirror::Class** klass_out,
2817 std::unordered_set<std::string>* prepared)
2818 REQUIRES_SHARED(Locks::mutator_lock_) {
2819 if (class_name.empty()) {
2820 return nullptr;
2821 }
2822
2823 std::string descriptor;
2824 if (class_name[0] == 'L') {
2825 descriptor = class_name;
2826 } else {
2827 descriptor = DotToDescriptor(class_name.c_str());
2828 }
2829
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002830 mirror::Class* klass = runtime->GetClassLinker()->FindClass(self, descriptor.c_str(), h_loader);
Andreas Gampe9fded872016-09-25 16:08:35 -07002831
2832 if (klass == nullptr) {
2833 self->ClearException();
2834 std::cerr << "Did not find " << class_name << std::endl;
2835 *klass_out = nullptr;
2836 return nullptr;
2837 }
2838
2839 StackHandleScope<1> scope(Thread::Current());
2840 Handle<mirror::Class> h_klass = scope.NewHandle<mirror::Class>(klass);
2841
2842 ImTable* ret = PrepareAndGetImTable(runtime, h_klass, pointer_size, prepared);
2843 *klass_out = h_klass.Get();
2844 return ret;
2845 }
2846
2847 static ImTable* PrepareAndGetImTable(Runtime* runtime,
2848 Handle<mirror::Class> h_klass,
2849 const PointerSize pointer_size,
2850 std::unordered_set<std::string>* prepared)
2851 REQUIRES_SHARED(Locks::mutator_lock_) {
2852 PrepareClass(runtime, h_klass, prepared);
2853 return h_klass->GetImt(pointer_size);
2854 }
2855
2856 static void DumpIMTForClass(Runtime* runtime,
2857 const std::string& class_name,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002858 Handle<mirror::ClassLoader> h_loader,
2859 std::unordered_set<std::string>* prepared)
2860 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002861 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
2862 mirror::Class* klass;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002863 ImTable* imt = PrepareAndGetImTable(runtime,
2864 Thread::Current(),
2865 h_loader,
2866 class_name,
2867 pointer_size,
2868 &klass,
2869 prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07002870 if (imt == nullptr) {
2871 return;
2872 }
2873
2874 std::cerr << class_name << std::endl << " IMT:" << std::endl;
2875 for (size_t index = 0; index < ImTable::kSize; ++index) {
2876 std::cerr << " " << index << ":" << std::endl;
2877 ArtMethod* ptr = imt->Get(index, pointer_size);
2878 if (ptr->IsRuntimeMethod()) {
2879 if (ptr->IsImtUnimplementedMethod()) {
2880 std::cerr << " <empty>" << std::endl;
2881 } else {
2882 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
2883 PrintTable(current_table, pointer_size);
2884 }
2885 } else {
David Sehr709b0702016-10-13 09:12:37 -07002886 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07002887 }
2888 }
2889
2890 std::cerr << " Interfaces:" << std::endl;
2891 // Run through iftable, find methods that slot here, see if they fit.
2892 mirror::IfTable* if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08002893 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;
Andreas Gampe9fded872016-09-25 16:08:35 -07002897
Mathieu Chartier6beced42016-11-15 15:51:31 -08002898 for (ArtMethod& iface_method : iface->GetVirtualMethods(pointer_size)) {
2899 uint32_t class_hash, name_hash, signature_hash;
2900 ImTable::GetImtHashComponents(&iface_method, &class_hash, &name_hash, &signature_hash);
2901 uint32_t imt_slot = ImTable::GetImtIndex(&iface_method);
2902 std::cerr << " " << iface_method.PrettyMethod(true)
2903 << " 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 static void DumpIMTForMethod(Runtime* runtime,
2915 const std::string& class_name,
2916 const std::string& method,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002917 Handle<mirror::ClassLoader> h_loader,
2918 std::unordered_set<std::string>* prepared)
2919 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002920 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
2921 mirror::Class* klass;
2922 ImTable* imt = PrepareAndGetImTable(runtime,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002923 Thread::Current(),
2924 h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07002925 class_name,
2926 pointer_size,
2927 &klass,
2928 prepared);
2929 if (imt == nullptr) {
2930 return;
2931 }
2932
2933 std::cerr << class_name << " <" << method << ">" << std::endl;
2934 for (size_t index = 0; index < ImTable::kSize; ++index) {
2935 ArtMethod* ptr = imt->Get(index, pointer_size);
2936 if (ptr->IsRuntimeMethod()) {
2937 if (ptr->IsImtUnimplementedMethod()) {
2938 continue;
2939 }
2940
2941 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
2942 if (current_table == nullptr) {
2943 continue;
2944 }
2945
2946 size_t table_index = 0;
2947 for (;;) {
2948 ArtMethod* ptr2 = current_table->GetInterfaceMethod(table_index, pointer_size);
2949 if (ptr2 == nullptr) {
2950 break;
2951 }
2952 table_index++;
2953
David Sehr709b0702016-10-13 09:12:37 -07002954 std::string p_name = ptr2->PrettyMethod(true);
Andreas Gampe9fded872016-09-25 16:08:35 -07002955 if (StartsWith(p_name, method.c_str())) {
2956 std::cerr << " Slot "
2957 << index
2958 << " ("
2959 << current_table->NumEntries(pointer_size)
2960 << ")"
2961 << std::endl;
2962 PrintTable(current_table, pointer_size);
2963 return;
2964 }
2965 }
2966 } else {
David Sehr709b0702016-10-13 09:12:37 -07002967 std::string p_name = ptr->PrettyMethod(true);
Andreas Gampe9fded872016-09-25 16:08:35 -07002968 if (StartsWith(p_name, method.c_str())) {
2969 std::cerr << " Slot " << index << " (1)" << std::endl;
2970 std::cerr << " " << p_name << std::endl;
2971 } else {
2972 // Run through iftable, find methods that slot here, see if they fit.
2973 mirror::IfTable* if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08002974 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
2975 mirror::Class* iface = if_table->GetInterface(i);
2976 size_t num_methods = iface->NumDeclaredVirtualMethods();
2977 if (num_methods > 0) {
2978 for (ArtMethod& iface_method : iface->GetMethods(pointer_size)) {
2979 if (ImTable::GetImtIndex(&iface_method) == index) {
2980 std::string i_name = iface_method.PrettyMethod(true);
2981 if (StartsWith(i_name, method.c_str())) {
2982 std::cerr << " Slot " << index << " (1)" << std::endl;
2983 std::cerr << " " << p_name << " (" << i_name << ")" << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07002984 }
2985 }
2986 }
2987 }
2988 }
2989 }
2990 }
2991 }
2992 }
2993
2994 // Read lines from the given stream, dropping comments and empty lines
2995 static std::vector<std::string> ReadCommentedInputStream(std::istream& in_stream) {
2996 std::vector<std::string> output;
2997 while (in_stream.good()) {
2998 std::string dot;
2999 std::getline(in_stream, dot);
3000 if (StartsWith(dot, "#") || dot.empty()) {
3001 continue;
3002 }
3003 output.push_back(dot);
3004 }
3005 return output;
3006 }
3007
3008 // Read lines from the given file, dropping comments and empty lines.
3009 static std::vector<std::string> ReadCommentedInputFromFile(const std::string& input_filename) {
3010 std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
3011 if (input_file.get() == nullptr) {
3012 LOG(ERROR) << "Failed to open input file " << input_filename;
3013 return std::vector<std::string>();
3014 }
3015 std::vector<std::string> result = ReadCommentedInputStream(*input_file);
3016 input_file->close();
3017 return result;
3018 }
3019
3020 // Prepare a class, i.e., ensure it has a filled IMT. Will do so recursively for superclasses,
3021 // and note in the given set that the work was done.
3022 static void PrepareClass(Runtime* runtime,
3023 Handle<mirror::Class> h_klass,
3024 std::unordered_set<std::string>* done)
3025 REQUIRES_SHARED(Locks::mutator_lock_) {
3026 if (!h_klass->ShouldHaveImt()) {
3027 return;
3028 }
3029
3030 std::string name;
3031 name = h_klass->GetDescriptor(&name);
3032
3033 if (done->find(name) != done->end()) {
3034 return;
3035 }
3036 done->insert(name);
3037
3038 if (h_klass->HasSuperClass()) {
3039 StackHandleScope<1> h(Thread::Current());
3040 PrepareClass(runtime, h.NewHandle<mirror::Class>(h_klass->GetSuperClass()), done);
3041 }
3042
3043 if (!h_klass->IsTemp()) {
3044 runtime->GetClassLinker()->FillIMTAndConflictTables(h_klass.Get());
3045 }
3046 }
3047};
3048
Igor Murashkin37743352014-11-13 14:38:00 -08003049struct OatdumpArgs : public CmdlineArgs {
3050 protected:
3051 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003052
Igor Murashkin37743352014-11-13 14:38:00 -08003053 virtual ParseStatus ParseCustom(const StringPiece& option,
3054 std::string* error_msg) OVERRIDE {
3055 {
3056 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
3057 if (base_parse != kParseUnknownArgument) {
3058 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003059 }
3060 }
3061
Igor Murashkin37743352014-11-13 14:38:00 -08003062 if (option.starts_with("--oat-file=")) {
3063 oat_filename_ = option.substr(strlen("--oat-file=")).data();
3064 } else if (option.starts_with("--image=")) {
3065 image_location_ = option.substr(strlen("--image=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003066 } else if (option == "--no-dump:vmap") {
3067 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01003068 } else if (option =="--dump:code_info_stack_maps") {
3069 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003070 } else if (option == "--no-disassemble") {
3071 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003072 } else if (option =="--header-only") {
3073 dump_header_only_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003074 } else if (option.starts_with("--symbolize=")) {
3075 oat_filename_ = option.substr(strlen("--symbolize=")).data();
3076 symbolize_ = true;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003077 } else if (option.starts_with("--only-keep-debug")) {
3078 only_keep_debug_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003079 } else if (option.starts_with("--class-filter=")) {
3080 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003081 } else if (option.starts_with("--method-filter=")) {
3082 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003083 } else if (option.starts_with("--list-classes")) {
3084 list_classes_ = true;
3085 } else if (option.starts_with("--list-methods")) {
3086 list_methods_ = true;
3087 } else if (option.starts_with("--export-dex-to=")) {
3088 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
3089 } else if (option.starts_with("--addr2instr=")) {
3090 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
3091 *error_msg = "Address conversion failed";
3092 return kParseError;
3093 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003094 } else if (option.starts_with("--app-image=")) {
3095 app_image_ = option.substr(strlen("--app-image=")).data();
3096 } else if (option.starts_with("--app-oat=")) {
3097 app_oat_ = option.substr(strlen("--app-oat=")).data();
Andreas Gampe9fded872016-09-25 16:08:35 -07003098 } else if (option.starts_with("--dump-imt=")) {
3099 imt_dump_ = option.substr(strlen("--dump-imt=")).data();
3100 } else if (option == "--dump-imt-stats") {
3101 imt_stat_dump_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003102 } else {
3103 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003104 }
3105
Igor Murashkin37743352014-11-13 14:38:00 -08003106 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003107 }
3108
Igor Murashkin37743352014-11-13 14:38:00 -08003109 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
3110 // Infer boot image location from the image location if possible.
3111 if (boot_image_location_ == nullptr) {
3112 boot_image_location_ = image_location_;
3113 }
3114
3115 // Perform the parent checks.
3116 ParseStatus parent_checks = Base::ParseChecks(error_msg);
3117 if (parent_checks != kParseOk) {
3118 return parent_checks;
3119 }
3120
3121 // Perform our own checks.
3122 if (image_location_ == nullptr && oat_filename_ == nullptr) {
3123 *error_msg = "Either --image or --oat-file must be specified";
3124 return kParseError;
3125 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
3126 *error_msg = "Either --image or --oat-file must be specified but not both";
3127 return kParseError;
3128 }
3129
3130 return kParseOk;
3131 }
3132
3133 virtual std::string GetUsage() const {
3134 std::string usage;
3135
3136 usage +=
3137 "Usage: oatdump [options] ...\n"
3138 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
3139 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
3140 "\n"
3141 // Either oat-file or image is required.
3142 " --oat-file=<file.oat>: specifies an input oat filename.\n"
3143 " Example: --oat-file=/system/framework/boot.oat\n"
3144 "\n"
3145 " --image=<file.art>: specifies an input image location.\n"
3146 " Example: --image=/system/framework/boot.art\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003147 "\n"
3148 " --app-image=<file.art>: specifies an input app image. Must also have a specified\n"
3149 " boot image and app oat file.\n"
3150 " Example: --app-image=app.art\n"
3151 "\n"
3152 " --app-oat=<file.odex>: specifies an input app oat.\n"
3153 " Example: --app-oat=app.odex\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003154 "\n";
3155
3156 usage += Base::GetUsage();
3157
3158 usage += // Optional.
Igor Murashkin37743352014-11-13 14:38:00 -08003159 " --no-dump:vmap may be used to disable vmap dumping.\n"
3160 " Example: --no-dump:vmap\n"
3161 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01003162 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
3163 " Example: --dump:code_info_stack_maps\n"
3164 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003165 " --no-disassemble may be used to disable disassembly.\n"
3166 " Example: --no-disassemble\n"
3167 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00003168 " --header-only may be used to print only the oat header.\n"
3169 " Example: --header-only\n"
3170 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003171 " --list-classes may be used to list target file classes (can be used with filters).\n"
3172 " Example: --list-classes\n"
3173 " Example: --list-classes --class-filter=com.example.foo\n"
3174 "\n"
3175 " --list-methods may be used to list target file methods (can be used with filters).\n"
3176 " Example: --list-methods\n"
3177 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
3178 "\n"
3179 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
3180 " Example: --symbolize=/system/framework/boot.oat\n"
3181 "\n"
David Srbecky2fdd03c2016-03-10 15:32:37 +00003182 " --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
3183 " .rodata and .text sections are omitted in the output file to save space.\n"
3184 " Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
3185 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003186 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
3187 " Example: --class-filter=com.example.foo\n"
3188 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003189 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
3190 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003191 "\n"
3192 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
3193 " Example: --export-dex-to=/data/local/tmp\n"
3194 "\n"
3195 " --addr2instr=<address>: output matching method disassembled code from relative\n"
3196 " address (e.g. PC from crash dump)\n"
3197 " Example: --addr2instr=0x00001a3b\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003198 "\n"
3199 " --dump-imt=<file.txt>: output IMT collisions (if any) for the given receiver\n"
3200 " types and interface methods in the given file. The file\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003201 " is read line-wise, where each line should either be a class\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003202 " name or descriptor, or a class name/descriptor and a prefix\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003203 " of a complete method name (separated by a whitespace).\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003204 " Example: --dump-imt=imt.txt\n"
3205 "\n"
3206 " --dump-imt-stats: output IMT statistics for the given boot image\n"
3207 " Example: --dump-imt-stats"
Igor Murashkin37743352014-11-13 14:38:00 -08003208 "\n";
3209
3210 return usage;
3211 }
3212
3213 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07003214 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003215 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00003216 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07003217 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003218 std::string elf_filename_prefix_;
Andreas Gampe9fded872016-09-25 16:08:35 -07003219 std::string imt_dump_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003220 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01003221 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003222 bool disassemble_code_ = true;
3223 bool symbolize_ = false;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003224 bool only_keep_debug_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003225 bool list_classes_ = false;
3226 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003227 bool dump_header_only_ = false;
Andreas Gampe9fded872016-09-25 16:08:35 -07003228 bool imt_stat_dump_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003229 uint32_t addr2instr_ = 0;
3230 const char* export_dex_location_ = nullptr;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003231 const char* app_image_ = nullptr;
3232 const char* app_oat_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003233};
3234
Igor Murashkin37743352014-11-13 14:38:00 -08003235struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
3236 virtual bool NeedsRuntime() OVERRIDE {
3237 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003238
Igor Murashkin37743352014-11-13 14:38:00 -08003239 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
3240 bool absolute_addresses = (args_->oat_filename_ == nullptr);
3241
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003242 oat_dumper_options_.reset(new OatDumperOptions(
Igor Murashkin37743352014-11-13 14:38:00 -08003243 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01003244 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08003245 args_->disassemble_code_,
3246 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003247 args_->class_filter_,
3248 args_->method_filter_,
3249 args_->list_classes_,
3250 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00003251 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003252 args_->export_dex_location_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003253 args_->app_image_,
3254 args_->app_oat_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003255 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08003256
Andreas Gampe9fded872016-09-25 16:08:35 -07003257 return (args_->boot_image_location_ != nullptr ||
3258 args_->image_location_ != nullptr ||
3259 !args_->imt_dump_.empty()) &&
Igor Murashkin37743352014-11-13 14:38:00 -08003260 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003261 }
3262
Igor Murashkin37743352014-11-13 14:38:00 -08003263 virtual bool ExecuteWithoutRuntime() OVERRIDE {
3264 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08003265 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003266
Mathieu Chartierd424d082014-10-15 10:31:46 -07003267 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08003268
Andreas Gampec24f3992014-12-17 20:40:11 -08003269 if (args_->symbolize_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00003270 // ELF has special kind of section called SHT_NOBITS which allows us to create
3271 // sections which exist but their data is omitted from the ELF file to save space.
3272 // This is what "strip --only-keep-debug" does when it creates separate ELF file
3273 // with only debug data. We use it in similar way to exclude .rodata and .text.
3274 bool no_bits = args_->only_keep_debug_;
3275 return SymbolizeOat(args_->oat_filename_, args_->output_name_, no_bits) == EXIT_SUCCESS;
Andreas Gampec24f3992014-12-17 20:40:11 -08003276 } else {
3277 return DumpOat(nullptr,
3278 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003279 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08003280 args_->os_) == EXIT_SUCCESS;
3281 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07003282 }
3283
Igor Murashkin37743352014-11-13 14:38:00 -08003284 virtual bool ExecuteWithRuntime(Runtime* runtime) {
3285 CHECK(args_ != nullptr);
3286
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003287 if (!args_->imt_dump_.empty() || args_->imt_stat_dump_) {
3288 return IMTDumper::Dump(runtime,
3289 args_->imt_dump_,
3290 args_->imt_stat_dump_,
3291 args_->oat_filename_);
Andreas Gampe9fded872016-09-25 16:08:35 -07003292 }
3293
Igor Murashkin37743352014-11-13 14:38:00 -08003294 if (args_->oat_filename_ != nullptr) {
3295 return DumpOat(runtime,
3296 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003297 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08003298 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003299 }
Igor Murashkin37743352014-11-13 14:38:00 -08003300
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003301 return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003302 }
3303
Igor Murashkin37743352014-11-13 14:38:00 -08003304 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
3305};
Andreas Gampe00b25f32014-09-17 21:49:05 -07003306
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003307} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07003308
3309int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08003310 art::OatdumpMain main;
3311 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07003312}