blob: da0db013865ef523411dfdba9caede60a4d9912d [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"
Andreas Gampe00b25f32014-09-17 21:49:05 -070069#include "well_known_classes.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070070
Igor Murashkin37743352014-11-13 14:38:00 -080071#include <sys/stat.h>
72#include "cmdline.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070073
Igor Murashkin37743352014-11-13 14:38:00 -080074namespace art {
Brian Carlstrom78128a62011-09-15 17:21:19 -070075
Mathieu Chartiere401d142015-04-22 13:56:20 -070076const char* image_methods_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -080077 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -070078 "kImtConflictMethod",
Mathieu Chartier2d2621a2014-10-23 16:48:06 -070079 "kImtUnimplementedMethod",
Vladimir Markofd36f1f2016-08-03 18:49:58 +010080 "kSaveAllCalleeSavesMethod",
81 "kSaveRefsOnlyMethod",
82 "kSaveRefsAndArgsMethod",
Vladimir Marko952dbb12016-07-28 12:01:51 +010083 "kSaveEverythingMethod",
Mathieu Chartiere401d142015-04-22 13:56:20 -070084};
85
86const char* image_roots_descriptions_[] = {
Brian Carlstrom58ae9412011-10-04 00:56:06 -070087 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070088 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070089};
90
Mathieu Chartierac8f4392015-08-27 13:54:20 -070091// Map is so that we don't allocate multiple dex files for the same OatDexFile.
92static std::map<const OatFile::OatDexFile*,
93 std::unique_ptr<const DexFile>> opened_dex_files;
94
95const DexFile* OpenDexFile(const OatFile::OatDexFile* oat_dex_file, std::string* error_msg) {
96 DCHECK(oat_dex_file != nullptr);
97 auto it = opened_dex_files.find(oat_dex_file);
98 if (it != opened_dex_files.end()) {
99 return it->second.get();
100 }
101 const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
102 opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
103 return ret;
104}
105
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800106template <typename ElfTypes>
David Srbeckybc90fd02015-04-22 19:40:27 +0100107class OatSymbolizer FINAL {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700108 public:
David Srbecky2fdd03c2016-03-10 15:32:37 +0000109 OatSymbolizer(const OatFile* oat_file, const std::string& output_name, bool no_bits) :
110 oat_file_(oat_file),
111 builder_(nullptr),
112 output_name_(output_name.empty() ? "symbolized.oat" : output_name),
113 no_bits_(no_bits) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700114 }
115
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700116 bool Symbolize() {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000117 const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800118 std::unique_ptr<const InstructionSetFeatures> features = InstructionSetFeatures::FromBitmap(
David Srbecky5d811202016-03-08 13:21:22 +0000119 isa, oat_file_->GetOatHeader().GetInstructionSetFeaturesBitmap());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000120
121 File* elf_file = OS::CreateEmptyFile(output_name_.c_str());
122 std::unique_ptr<BufferedOutputStream> output_stream(
Vladimir Marko10c13562015-11-25 14:33:36 +0000123 MakeUnique<BufferedOutputStream>(MakeUnique<FileOutputStream>(elf_file)));
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800124 builder_.reset(new ElfBuilder<ElfTypes>(isa, features.get(), output_stream.get()));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000125
126 builder_->Start();
127
128 auto* rodata = builder_->GetRoData();
129 auto* text = builder_->GetText();
130 auto* bss = builder_->GetBss();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000131
David Srbecky6d8c8f02015-10-26 10:57:09 +0000132 const uint8_t* rodata_begin = oat_file_->Begin();
133 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
David Srbecky2fdd03c2016-03-10 15:32:37 +0000134 if (no_bits_) {
135 rodata->WriteNoBitsSection(rodata_size);
136 } else {
137 rodata->Start();
138 rodata->WriteFully(rodata_begin, rodata_size);
139 rodata->End();
140 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000141
David Srbecky6d8c8f02015-10-26 10:57:09 +0000142 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
143 const size_t text_size = oat_file_->End() - text_begin;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000144 if (no_bits_) {
145 text->WriteNoBitsSection(text_size);
146 } else {
147 text->Start();
148 text->WriteFully(text_begin, text_size);
149 text->End();
150 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000151
152 if (oat_file_->BssSize() != 0) {
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000153 bss->WriteNoBitsSection(oat_file_->BssSize());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000154 }
155
Douglas Leung316a2182015-09-17 15:26:25 -0700156 if (isa == kMips || isa == kMips64) {
157 builder_->WriteMIPSabiflagsSection();
158 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000159 builder_->PrepareDynamicSection(elf_file->GetPath(),
160 rodata_size,
161 text_size,
162 oat_file_->BssSize(),
163 oat_file_->BssRootsOffset());
Vladimir Marko944da602016-02-19 12:27:55 +0000164 builder_->WriteDynamicSection();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700165
David Srbecky5d950762016-03-07 20:47:29 +0000166 Walk();
167 for (const auto& trampoline : debug::MakeTrampolineInfos(oat_file_->GetOatHeader())) {
168 method_debug_infos_.push_back(trampoline);
169 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700170
David Srbecky5d950762016-03-07 20:47:29 +0000171 debug::WriteDebugInfo(builder_.get(),
172 ArrayRef<const debug::MethodDebugInfo>(method_debug_infos_),
173 dwarf::DW_DEBUG_FRAME_FORMAT,
174 true /* write_oat_patches */);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700175
David Srbecky6d8c8f02015-10-26 10:57:09 +0000176 builder_->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700177
Vladimir Marko10c13562015-11-25 14:33:36 +0000178 return builder_->Good();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700179 }
180
David Srbecky5d950762016-03-07 20:47:29 +0000181 void Walk() {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700182 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
183 for (size_t i = 0; i < oat_dex_files.size(); i++) {
184 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700185 CHECK(oat_dex_file != nullptr);
David Srbecky5d950762016-03-07 20:47:29 +0000186 WalkOatDexFile(oat_dex_file);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700187 }
188 }
189
David Srbecky5d950762016-03-07 20:47:29 +0000190 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700191 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700192 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
193 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700194 return;
195 }
196 for (size_t class_def_index = 0;
197 class_def_index < dex_file->NumClassDefs();
198 class_def_index++) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700199 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
200 OatClassType type = oat_class.GetType();
201 switch (type) {
202 case kOatClassAllCompiled:
203 case kOatClassSomeCompiled:
David Srbecky5d950762016-03-07 20:47:29 +0000204 WalkOatClass(oat_class, *dex_file, class_def_index);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700205 break;
206
207 case kOatClassNoneCompiled:
208 case kOatClassMax:
209 // Ignore.
210 break;
211 }
212 }
213 }
214
David Srbecky5d950762016-03-07 20:47:29 +0000215 void WalkOatClass(const OatFile::OatClass& oat_class,
216 const DexFile& dex_file,
217 uint32_t class_def_index) {
218 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700219 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700220 if (class_data == nullptr) { // empty class such as a marker interface?
221 return;
222 }
223 // Note: even if this is an interface or a native class, we still have to walk it, as there
224 // might be a static initializer.
225 ClassDataItemIterator it(dex_file, class_data);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700226 uint32_t class_method_idx = 0;
David Srbecky5d950762016-03-07 20:47:29 +0000227 for (; it.HasNextStaticField(); it.Next()) { /* skip */ }
228 for (; it.HasNextInstanceField(); it.Next()) { /* skip */ }
229 for (; it.HasNextDirectMethod() || it.HasNextVirtualMethod(); it.Next()) {
230 WalkOatMethod(oat_class.GetOatMethod(class_method_idx++),
231 dex_file,
232 class_def_index,
233 it.GetMemberIndex(),
234 it.GetMethodCodeItem(),
235 it.GetMethodAccessFlags());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700236 }
237 DCHECK(!it.HasNext());
238 }
239
David Srbecky5d950762016-03-07 20:47:29 +0000240 void WalkOatMethod(const OatFile::OatMethod& oat_method,
241 const DexFile& dex_file,
242 uint32_t class_def_index,
243 uint32_t dex_method_index,
244 const DexFile::CodeItem* code_item,
245 uint32_t method_access_flags) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700246 if ((method_access_flags & kAccAbstract) != 0) {
247 // Abstract method, no code.
248 return;
249 }
David Srbecky5d950762016-03-07 20:47:29 +0000250 const OatHeader& oat_header = oat_file_->GetOatHeader();
251 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
252 if (method_header == nullptr || method_header->GetCodeSize() == 0) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700253 // No code.
254 return;
255 }
256
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100257 uint32_t entry_point = oat_method.GetCodeOffset() - oat_header.GetExecutableOffset();
258 // Clear Thumb2 bit.
259 const void* code_address = EntryPointToCodePointer(reinterpret_cast<void*>(entry_point));
260
David Srbecky5d950762016-03-07 20:47:29 +0000261 debug::MethodDebugInfo info = debug::MethodDebugInfo();
262 info.trampoline_name = nullptr;
263 info.dex_file = &dex_file;
264 info.class_def_index = class_def_index;
265 info.dex_method_index = dex_method_index;
266 info.access_flags = method_access_flags;
267 info.code_item = code_item;
268 info.isa = oat_header.GetInstructionSet();
269 info.deduped = !seen_offsets_.insert(oat_method.GetCodeOffset()).second;
270 info.is_native_debuggable = oat_header.IsNativeDebuggable();
271 info.is_optimized = method_header->IsOptimized();
272 info.is_code_address_text_relative = true;
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100273 info.code_address = reinterpret_cast<uintptr_t>(code_address);
David Srbecky5d950762016-03-07 20:47:29 +0000274 info.code_size = method_header->GetCodeSize();
275 info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
276 info.code_info = info.is_optimized ? method_header->GetOptimizedCodeInfoPtr() : nullptr;
277 info.cfi = ArrayRef<uint8_t>();
278 method_debug_infos_.push_back(info);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700279 }
280
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700281 private:
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700282 const OatFile* oat_file_;
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800283 std::unique_ptr<ElfBuilder<ElfTypes> > builder_;
David Srbecky5d950762016-03-07 20:47:29 +0000284 std::vector<debug::MethodDebugInfo> method_debug_infos_;
285 std::unordered_set<uint32_t> seen_offsets_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700286 const std::string output_name_;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000287 bool no_bits_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700288};
289
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700290class OatDumperOptions {
291 public:
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100292 OatDumperOptions(bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100293 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700294 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700295 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800296 const char* class_filter,
297 const char* method_filter,
298 bool list_classes,
299 bool list_methods,
David Brazdilc03d7b62016-03-02 12:18:03 +0000300 bool dump_header_only,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800301 const char* export_dex_location,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800302 const char* app_image,
303 const char* app_oat,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800304 uint32_t addr2instr)
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100305 : dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100306 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700307 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700308 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800309 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000310 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800311 list_classes_(list_classes),
312 list_methods_(list_methods),
David Brazdilc03d7b62016-03-02 12:18:03 +0000313 dump_header_only_(dump_header_only),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800314 export_dex_location_(export_dex_location),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800315 app_image_(app_image),
316 app_oat_(app_oat),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800317 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800318 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700319
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700320 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100321 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700322 const bool disassemble_code_;
323 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800324 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000325 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800326 const bool list_classes_;
327 const bool list_methods_;
David Brazdilc03d7b62016-03-02 12:18:03 +0000328 const bool dump_header_only_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800329 const char* const export_dex_location_;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800330 const char* const app_image_;
331 const char* const app_oat_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800332 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700333 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700334};
335
Elliott Hughese3c845c2012-02-28 17:23:01 -0800336class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700337 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100338 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000339 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800340 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700341 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800342 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800343 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
344 disassembler_(Disassembler::Create(instruction_set_,
Andreas Gampe372f3a32016-08-19 10:49:06 -0700345 new DisassemblerOptions(
346 options_.absolute_addresses_,
347 oat_file.Begin(),
348 oat_file.End(),
349 true /* can_read_literals_ */,
350 Is64BitInstructionSet(instruction_set_)
351 ? &Thread::DumpThreadOffset<PointerSize::k64>
352 : &Thread::DumpThreadOffset<PointerSize::k32>))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800353 CHECK(options_.class_loader_ != nullptr);
354 CHECK(options_.class_filter_ != nullptr);
355 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800356 AddAllOffsets();
357 }
358
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700359 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700360 delete disassembler_;
361 }
362
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800363 InstructionSet GetInstructionSet() {
364 return instruction_set_;
365 }
366
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700367 bool Dump(std::ostream& os) {
368 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800369 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700370
371 os << "MAGIC:\n";
372 os << oat_header.GetMagic() << "\n\n";
373
Jeff Hao4b07a6e2016-01-15 12:50:44 -0800374 os << "LOCATION:\n";
375 os << oat_file_.GetLocation() << "\n\n";
376
Brian Carlstromaded5f72011-10-07 17:15:04 -0700377 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800378 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700379
Elliott Hughesa72ec822012-03-05 17:12:22 -0800380 os << "INSTRUCTION SET:\n";
381 os << oat_header.GetInstructionSet() << "\n\n";
382
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700383 {
384 std::unique_ptr<const InstructionSetFeatures> features(
385 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
386 oat_header.GetInstructionSetFeaturesBitmap()));
387 os << "INSTRUCTION SET FEATURES:\n";
388 os << features->GetFeatureString() << "\n\n";
389 }
Dave Allison70202782013-10-22 17:52:19 -0700390
Brian Carlstromaded5f72011-10-07 17:15:04 -0700391 os << "DEX FILE COUNT:\n";
392 os << oat_header.GetDexFileCount() << "\n\n";
393
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800394#define DUMP_OAT_HEADER_OFFSET(label, offset) \
395 os << label " OFFSET:\n"; \
396 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800397 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800398 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
399 } \
400 os << StringPrintf("\n\n");
401
402 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
403 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
404 GetInterpreterToInterpreterBridgeOffset);
405 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
406 GetInterpreterToCompiledCodeBridgeOffset);
407 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
408 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800409 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
410 GetQuickGenericJniTrampolineOffset);
411 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
412 GetQuickImtConflictTrampolineOffset);
413 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
414 GetQuickResolutionTrampolineOffset);
415 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
416 GetQuickToInterpreterBridgeOffset);
417#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700418
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700419 os << "IMAGE PATCH DELTA:\n";
420 os << StringPrintf("%d (0x%08x)\n\n",
421 oat_header.GetImagePatchDelta(),
422 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700423
Brian Carlstrom28db0122012-10-18 16:20:41 -0700424 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
425 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
426
427 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800428 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700429
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700430 // Print the key-value store.
431 {
432 os << "KEY VALUE STORE:\n";
433 size_t index = 0;
434 const char* key;
435 const char* value;
436 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
437 os << key << " = " << value << "\n";
438 index++;
439 }
440 os << "\n";
441 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700442
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800443 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700444 os << "BEGIN:\n";
445 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700446
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700447 os << "END:\n";
448 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
449 }
450
451 os << "SIZE:\n";
452 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700453
454 os << std::flush;
455
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800456 // If set, adjust relative address to be searched
457 if (options_.addr2instr_ != 0) {
458 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
459 os << "SEARCH ADDRESS (executable offset + input):\n";
460 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
461 }
462
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700463 // Dumping the dex file overview is compact enough to do even if header only.
464 DexFileData cumulative;
465 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
466 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
467 CHECK(oat_dex_file != nullptr);
468 std::string error_msg;
469 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
470 if (dex_file == nullptr) {
471 os << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation() << "': "
472 << error_msg;
473 continue;
474 }
475 DexFileData data(*dex_file);
476 os << "Dex file data for " << dex_file->GetLocation() << "\n";
477 data.Dump(os);
478 os << "\n";
479 cumulative.Add(data);
480 }
481 os << "Cumulative dex file data\n";
482 cumulative.Dump(os);
483 os << "\n";
484
David Brazdilc03d7b62016-03-02 12:18:03 +0000485 if (!options_.dump_header_only_) {
486 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
487 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
488 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800489
David Brazdilc03d7b62016-03-02 12:18:03 +0000490 // If file export selected skip file analysis
491 if (options_.export_dex_location_) {
492 if (!ExportDexFile(os, *oat_dex_file)) {
493 success = false;
494 }
495 } else {
496 if (!DumpOatDexFile(os, *oat_dex_file)) {
497 success = false;
498 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800499 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700500 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700501 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000502
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700503 os << std::flush;
504 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700505 }
506
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800507 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700508 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
509 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800510 return 0; // Address not in oat file
511 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800512 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
513 reinterpret_cast<uintptr_t>(oat_file_.Begin());
514 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800515 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800516 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800517 return end_offset - begin_offset;
518 }
519
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800520 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700521 return oat_file_.GetOatHeader().GetInstructionSet();
522 }
523
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700524 const void* GetQuickOatCode(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800525 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
526 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700527 CHECK(oat_dex_file != nullptr);
528 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700529 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
530 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700531 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
532 << "': " << error_msg;
533 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800534 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700535 const DexFile::ClassDef* class_def =
David Sehr9aa352e2016-09-15 18:13:52 -0700536 OatDexFile::FindClassDef(*dex_file, descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700537 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700538 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100539 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800540 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100541 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800542 }
543 }
544 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700545 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800546 }
547
Brian Carlstromaded5f72011-10-07 17:15:04 -0700548 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800549 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800550 // We don't know the length of the code for each method, but we need to know where to stop
551 // when disassembling. What we do know is that a region of code will be followed by some other
552 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
553 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800554 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
555 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700556 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700557 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700558 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
559 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700560 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
561 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800562 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800563 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800564 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800565 for (size_t class_def_index = 0;
566 class_def_index < dex_file->NumClassDefs();
567 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800568 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100569 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700570 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700571 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800572 ClassDataItemIterator it(*dex_file, class_data);
573 SkipAllFields(it);
574 uint32_t class_method_index = 0;
575 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100576 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800577 it.Next();
578 }
579 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100580 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800581 it.Next();
582 }
583 }
584 }
585 }
586
587 // If the last thing in the file is code for a method, there won't be an offset for the "next"
588 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
589 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800590 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800591 }
592
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700593 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
594 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
595 }
596
Elliott Hughese3c845c2012-02-28 17:23:01 -0800597 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800598 uint32_t code_offset = oat_method.GetCodeOffset();
599 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
600 code_offset &= ~0x1;
601 }
602 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800603 offsets_.insert(oat_method.GetVmapTableOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800604 }
605
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700606 // Dex file data, may be for multiple different dex files.
607 class DexFileData {
608 public:
609 DexFileData() {}
610
611 explicit DexFileData(const DexFile& dex_file)
612 : num_string_ids_(dex_file.NumStringIds()),
613 num_method_ids_(dex_file.NumMethodIds()),
614 num_field_ids_(dex_file.NumFieldIds()),
615 num_type_ids_(dex_file.NumTypeIds()),
616 num_class_defs_(dex_file.NumClassDefs()) {
617 for (size_t class_def_index = 0; class_def_index < num_class_defs_; ++class_def_index) {
618 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
619 WalkClass(dex_file, class_def);
620 }
621 }
622
623 void Add(const DexFileData& other) {
624 AddAll(unique_string_ids_from_code_, other.unique_string_ids_from_code_);
625 num_string_ids_from_code_ += other.num_string_ids_from_code_;
626 AddAll(dex_code_item_ptrs_, other.dex_code_item_ptrs_);
627 dex_code_bytes_ += other.dex_code_bytes_;
628 num_string_ids_ += other.num_string_ids_;
629 num_method_ids_ += other.num_method_ids_;
630 num_field_ids_ += other.num_field_ids_;
631 num_type_ids_ += other.num_type_ids_;
632 num_class_defs_ += other.num_class_defs_;
633 }
634
635 void Dump(std::ostream& os) {
636 os << "Num string ids: " << num_string_ids_ << "\n";
637 os << "Num method ids: " << num_method_ids_ << "\n";
638 os << "Num field ids: " << num_field_ids_ << "\n";
639 os << "Num type ids: " << num_type_ids_ << "\n";
640 os << "Num class defs: " << num_class_defs_ << "\n";
641 os << "Unique strings loaded from dex code: " << unique_string_ids_from_code_.size() << "\n";
642 os << "Total strings loaded from dex code: " << num_string_ids_from_code_ << "\n";
643 os << "Number of unique dex code items: " << dex_code_item_ptrs_.size() << "\n";
644 os << "Total number of dex code bytes: " << dex_code_bytes_ << "\n";
645 }
646
647 private:
648 void WalkClass(const DexFile& dex_file, const DexFile::ClassDef& class_def) {
649 const uint8_t* class_data = dex_file.GetClassData(class_def);
650 if (class_data == nullptr) { // empty class such as a marker interface?
651 return;
652 }
653 ClassDataItemIterator it(dex_file, class_data);
654 SkipAllFields(it);
655 while (it.HasNextDirectMethod()) {
656 WalkCodeItem(dex_file, it.GetMethodCodeItem());
657 it.Next();
658 }
659 while (it.HasNextVirtualMethod()) {
660 WalkCodeItem(dex_file, it.GetMethodCodeItem());
661 it.Next();
662 }
663 DCHECK(!it.HasNext());
664 }
665
666 void WalkCodeItem(const DexFile& dex_file, const DexFile::CodeItem* code_item) {
667 if (code_item == nullptr) {
668 return;
669 }
670 const size_t code_item_size = code_item->insns_size_in_code_units_;
671 const uint16_t* code_ptr = code_item->insns_;
672 const uint16_t* code_end = code_item->insns_ + code_item_size;
673
674 // If we inserted a new dex code item pointer, add to total code bytes.
675 if (dex_code_item_ptrs_.insert(code_ptr).second) {
676 dex_code_bytes_ += code_item_size * sizeof(code_ptr[0]);
677 }
678
679 while (code_ptr < code_end) {
680 const Instruction* inst = Instruction::At(code_ptr);
681 switch (inst->Opcode()) {
682 case Instruction::CONST_STRING: {
683 const uint32_t string_index = inst->VRegB_21c();
684 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
685 ++num_string_ids_from_code_;
686 break;
687 }
688 case Instruction::CONST_STRING_JUMBO: {
689 const uint32_t string_index = inst->VRegB_31c();
690 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
691 ++num_string_ids_from_code_;
692 break;
693 }
694 default:
695 break;
696 }
697
698 code_ptr += inst->SizeInCodeUnits();
699 }
700 }
701
702 // Unique string ids loaded from dex code.
703 std::set<StringReference, StringReferenceComparator> unique_string_ids_from_code_;
704
705 // Total string ids loaded from dex code.
706 size_t num_string_ids_from_code_ = 0;
707
708 // Unique code pointers.
709 std::set<const void*> dex_code_item_ptrs_;
710
711 // Total "unique" dex code bytes.
712 size_t dex_code_bytes_ = 0;
713
714 // Other dex ids.
715 size_t num_string_ids_ = 0;
716 size_t num_method_ids_ = 0;
717 size_t num_field_ids_ = 0;
718 size_t num_type_ids_ = 0;
719 size_t num_class_defs_ = 0;
720 };
721
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700722 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
723 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800724 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700725 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800726 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800727 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700728
Andreas Gampe2ba88952016-04-29 17:52:07 -0700729 const uint8_t* const oat_file_begin = oat_dex_file.GetOatFile()->Begin();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100730 const uint8_t* const vdex_file_begin = oat_dex_file.GetOatFile()->DexBegin();
731
732 // Print data range of the dex file embedded inside the corresponding vdex file.
Andreas Gampe2ba88952016-04-29 17:52:07 -0700733 const uint8_t* const dex_file_pointer = oat_dex_file.GetDexFilePointer();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100734 uint32_t dex_offset = dchecked_integral_cast<uint32_t>(dex_file_pointer - vdex_file_begin);
Andreas Gampe2ba88952016-04-29 17:52:07 -0700735 os << StringPrintf("dex-file: 0x%08x..0x%08x\n",
736 dex_offset,
737 dchecked_integral_cast<uint32_t>(dex_offset + oat_dex_file.FileSize() - 1));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700738
Andreas Gampe2ba88952016-04-29 17:52:07 -0700739 // Create the dex file early. A lot of print-out things depend on it.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700740 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700741 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
742 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700743 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700744 os << std::flush;
745 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700746 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100747
Andreas Gampe2ba88952016-04-29 17:52:07 -0700748 // Print lookup table, if it exists.
749 if (oat_dex_file.GetLookupTableData() != nullptr) {
750 uint32_t table_offset = dchecked_integral_cast<uint32_t>(
751 oat_dex_file.GetLookupTableData() - oat_file_begin);
David Sehr9aa352e2016-09-15 18:13:52 -0700752 uint32_t table_size = TypeLookupTable::RawDataLength(dex_file->NumClassDefs());
Andreas Gampe2ba88952016-04-29 17:52:07 -0700753 os << StringPrintf("type-table: 0x%08x..0x%08x\n",
754 table_offset,
755 table_offset + table_size - 1);
756 }
757
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100758 VariableIndentationOutputStream vios(&os);
759 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800760 for (size_t class_def_index = 0;
761 class_def_index < dex_file->NumClassDefs();
762 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700763 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
764 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800765
766 // TODO: Support regex
767 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
768 continue;
769 }
770
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700771 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100772 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700773 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
774 class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100775 << " (" << oat_class.GetStatus() << ")"
776 << " (" << oat_class.GetType() << ")\n";
777 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700778 if (options_.list_classes_) {
779 continue;
780 }
781 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700782 success = false;
783 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800784 if (stop_analysis) {
785 os << std::flush;
786 return success;
787 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700788 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700789 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700790 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700791 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700792 }
793
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800794 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
795 std::string error_msg;
796 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
797
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700798 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800799 if (dex_file == nullptr) {
800 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
801 return false;
802 }
803 size_t fsize = oat_dex_file.FileSize();
804
805 // Some quick checks just in case
806 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
807 os << "Invalid dex file\n";
808 return false;
809 }
810
811 // Verify output directory exists
812 if (!OS::DirectoryExists(options_.export_dex_location_)) {
813 // TODO: Extend OS::DirectoryExists if symlink support is required
814 os << options_.export_dex_location_ << " output directory not found or symlink\n";
815 return false;
816 }
817
818 // Beautify path names
819 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
820 return false;
821 }
822
823 std::string dex_orig_name;
824 size_t dex_orig_pos = dex_file_location.rfind('/');
825 if (dex_orig_pos == std::string::npos)
826 dex_orig_name = dex_file_location;
827 else
828 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
829
830 // A more elegant approach to efficiently name user installed apps is welcome
831 if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
832 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
833 size_t apk_orig_pos = dex_file_location.rfind('/');
834 if (apk_orig_pos != std::string::npos) {
835 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
836 }
837 }
838
839 std::string out_dex_path(options_.export_dex_location_);
840 if (out_dex_path.back() != '/') {
841 out_dex_path.append("/");
842 }
843 out_dex_path.append(dex_orig_name);
844 out_dex_path.append("_export.dex");
845 if (out_dex_path.length() > PATH_MAX) {
846 return false;
847 }
848
849 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
850 if (file.get() == nullptr) {
851 os << "Failed to open output dex file " << out_dex_path;
852 return false;
853 }
854
855 if (!file->WriteFully(dex_file->Begin(), fsize)) {
856 os << "Failed to write dex file";
857 file->Erase();
858 return false;
859 }
860
861 if (file->FlushCloseOrErase() != 0) {
862 os << "Flush and close failed";
863 return false;
864 }
865
866 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
867 os << std::flush;
868
869 return true;
870 }
871
Elliott Hughese3c845c2012-02-28 17:23:01 -0800872 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700873 while (it.HasNextStaticField()) {
874 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700875 }
Ian Rogers0571d352011-11-03 19:51:38 -0700876 while (it.HasNextInstanceField()) {
877 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700878 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800879 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700880
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100881 bool DumpOatClass(VariableIndentationOutputStream* vios,
882 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700883 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700884 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800885 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -0700886 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700887 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100888 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700889 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800890 }
891 ClassDataItemIterator it(dex_file, class_data);
892 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700893 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700894 while (it.HasNextDirectMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100895 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700896 it.GetMemberIndex(), it.GetMethodCodeItem(),
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700897 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700898 success = false;
899 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800900 if (addr_found) {
901 *stop_analysis = true;
902 return success;
903 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700904 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700905 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700906 }
Ian Rogers0571d352011-11-03 19:51:38 -0700907 while (it.HasNextVirtualMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100908 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700909 it.GetMemberIndex(), it.GetMethodCodeItem(),
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700910 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700911 success = false;
912 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800913 if (addr_found) {
914 *stop_analysis = true;
915 return success;
916 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700917 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700918 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700919 }
Ian Rogers0571d352011-11-03 19:51:38 -0700920 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100921 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700922 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700923 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800924
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700925 static constexpr uint32_t kPrologueBytes = 16;
926
927 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
928 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
929
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100930 bool DumpOatMethod(VariableIndentationOutputStream* vios,
931 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700932 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700933 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800934 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700935 uint32_t method_access_flags, bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700936 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800937
938 // TODO: Support regex
939 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
940 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000941 return success;
942 }
943
David Sehr709b0702016-10-13 09:12:37 -0700944 std::string pretty_method = dex_file.PrettyMethod(dex_method_idx, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100945 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
946 class_method_index, pretty_method.c_str(),
947 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800948 if (options_.list_methods_) return success;
949
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800950 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
951 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
952 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
953 uint32_t code_offset = oat_method.GetCodeOffset();
954 uint32_t code_size = oat_method.GetQuickCodeSize();
955 if (resolved_addr2instr_ != 0) {
956 if (resolved_addr2instr_ > code_offset + code_size) {
957 return success;
958 } else {
959 *addr_found = true; // stop analyzing file at next iteration
960 }
961 }
962
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100963 // Everything below is indented at least once.
964 ScopedIndentation indent1(vios);
965
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800966 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100967 vios->Stream() << "DEX CODE:\n";
968 ScopedIndentation indent2(vios);
969 DumpDexCode(vios->Stream(), dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700970 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700971
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700972 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700973 std::unique_ptr<verifier::MethodVerifier> verifier;
974 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700975 // We need to have the handle scope stay live until after the verifier since the verifier has
976 // a handle to the dex cache from hs.
977 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100978 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
979 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700980 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100981 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700982 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -0700983 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800984 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100985 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800986 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100987 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100988 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100989 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700990 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100991 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700992 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
993 oat_method_offsets_offset, oat_file_.Size());
994 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100995 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700996 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800997 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700998
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100999 ScopedIndentation indent2(vios);
1000 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001001 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
1002 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001003 vios->Stream() << StringPrintf("WARNING: "
1004 "code offset 0x%08x is past end of file 0x%08zx.\n",
1005 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001006 success = false;
1007 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001008 vios->Stream() << "\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001009 }
1010 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001011 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001012 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
1013 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -07001014
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001015 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001016 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001017 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001018 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001019 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001020 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001021 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
1022 method_header_offset, oat_file_.Size());
1023 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001024 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001025 return false;
1026 }
1027
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001028 ScopedIndentation indent2(vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001029 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001030 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001031 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001032 }
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001033 uint32_t vmap_table_offset = method_header == nullptr ? 0 : method_header->vmap_table_offset_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001034 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001035
1036 size_t vmap_table_offset_limit =
1037 (kIsVdexEnabled && IsMethodGeneratedByDexToDexCompiler(oat_method, code_item))
1038 ? oat_file_.GetVdexFile()->Size()
1039 : method_header->GetCode() - oat_file_.Begin();
1040 if (vmap_table_offset >= vmap_table_offset_limit) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001041 vios->Stream() << StringPrintf("WARNING: "
1042 "vmap table offset 0x%08x is past end of file 0x%08zx. "
1043 "vmap table offset was loaded from offset 0x%08x.\n",
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001044 vmap_table_offset,
1045 vmap_table_offset_limit,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001046 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001047 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001048 } else if (options_.dump_vmap_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001049 DumpVmapData(vios, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001050 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001051 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001052 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001053 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001054
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001055 ScopedIndentation indent2(vios);
1056 vios->Stream()
1057 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
1058 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
1059 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
1060 vios->Stream() << "\n";
1061 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
1062 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
1063 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001064 }
1065 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001066 // Based on spill masks from QuickMethodFrameInfo so placed
1067 // after it is dumped, but useful for understanding quick
1068 // code, so dumped here.
1069 ScopedIndentation indent2(vios);
1070 DumpVregLocations(vios->Stream(), oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001071 }
1072 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001073 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001074 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
1075 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001076 ScopedIndentation indent2(vios);
1077 vios->Stream() << StringPrintf("WARNING: "
1078 "code size offset 0x%08x is past end of file 0x%08zx.",
1079 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001080 success = false;
1081 } else {
1082 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001083 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1084 uint64_t aligned_code_end = aligned_code_begin + code_size;
1085
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001086 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001087 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001088 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001089 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
1090 code_offset,
1091 code_size_offset,
1092 code_size,
1093 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001094
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001095 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001096 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001097 vios->Stream() << StringPrintf("WARNING: "
1098 "start of code at 0x%08x is past end of file 0x%08zx.",
1099 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001100 success = false;
1101 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001102 vios->Stream() << StringPrintf(
1103 "WARNING: "
1104 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
1105 "code size is 0x%08x loaded from offset 0x%08x.\n",
1106 aligned_code_end, oat_file_.Size(),
1107 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001108 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001109 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001110 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001111 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001112 }
1113 }
1114 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001115 vios->Stream() << StringPrintf(
1116 "WARNING: "
1117 "code size %d is bigger than max expected threshold of %d. "
1118 "code size is 0x%08x loaded from offset 0x%08x.\n",
1119 code_size, kMaxCodeSize,
1120 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001121 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001122 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001123 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001124 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001125 }
1126 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001127 } else if (options_.disassemble_code_) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001128 DumpCode(vios, oat_method, code_item, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001129 }
1130 }
1131 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001132 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001133 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001134 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001135
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001136 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1137 if (spill_mask == 0) {
1138 return;
1139 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001140 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001141 for (size_t i = 0; i < 32; i++) {
1142 if ((spill_mask & (1 << i)) != 0) {
1143 if (is_float) {
1144 os << "fr" << i;
1145 } else {
1146 os << "r" << i;
1147 }
1148 spill_mask ^= 1 << i; // clear bit
1149 if (spill_mask != 0) {
1150 os << ", ";
1151 } else {
1152 break;
1153 }
1154 }
1155 }
1156 os << ")";
1157 }
1158
Roland Levillain442b46a2015-02-18 16:54:21 +00001159 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001160 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001161 const OatFile::OatMethod& oat_method,
1162 const DexFile::CodeItem* code_item) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001163 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1164 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001165 const void* raw_code_info = oat_method.GetVmapTable();
1166 if (raw_code_info != nullptr) {
1167 CodeInfo code_info(raw_code_info);
1168 DCHECK(code_item != nullptr);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001169 ScopedIndentation indent1(vios);
1170 DumpCodeInfo(vios, code_info, oat_method, *code_item);
Roland Levillain442b46a2015-02-18 16:54:21 +00001171 }
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001172 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
1173 // We don't encode the size in the table, so just emit that we have quickened
1174 // information.
1175 ScopedIndentation indent(vios);
1176 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001177 } else {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001178 // Otherwise, there is nothing to display.
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001179 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001180 }
1181
1182 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001183 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001184 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001185 const OatFile::OatMethod& oat_method,
Roland Levillain442b46a2015-02-18 16:54:21 +00001186 const DexFile::CodeItem& code_item) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001187 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001188 oat_method.GetCodeOffset(),
1189 code_item.registers_size_,
1190 options_.dump_code_info_stack_maps_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001191 }
1192
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001193 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1194 const DexFile::CodeItem* code_item) {
1195 if (code_item != nullptr) {
1196 size_t num_locals_ins = code_item->registers_size_;
1197 size_t num_ins = code_item->ins_size_;
1198 size_t num_locals = num_locals_ins - num_ins;
1199 size_t num_outs = code_item->outs_size_;
1200
1201 os << "vr_stack_locations:";
1202 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1203 // For readability, delimit the different kinds of VRs.
1204 if (reg == num_locals_ins) {
1205 os << "\n\tmethod*:";
1206 } else if (reg == num_locals && num_ins > 0) {
1207 os << "\n\tins:";
1208 } else if (reg == 0 && num_locals > 0) {
1209 os << "\n\tlocals:";
1210 }
1211
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001212 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1213 code_item,
1214 oat_method.GetCoreSpillMask(),
1215 oat_method.GetFpSpillMask(),
1216 oat_method.GetFrameSizeInBytes(),
1217 reg,
1218 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001219 os << " v" << reg << "[sp + #" << offset << "]";
1220 }
1221
1222 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1223 if (out_reg == 0) {
1224 os << "\n\touts:";
1225 }
1226
1227 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1228 os << " v" << out_reg << "[sp + #" << offset << "]";
1229 }
1230
1231 os << "\n";
1232 }
1233 }
1234
Ian Rogersb23a7722012-10-09 16:54:26 -07001235 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001236 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001237 size_t i = 0;
1238 while (i < code_item->insns_size_in_code_units_) {
1239 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001240 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1241 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001242 i += instruction->SizeInCodeUnits();
1243 }
1244 }
1245 }
1246
Roland Levillainf2650d12015-05-28 14:53:28 +01001247 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1248 // the optimizing compiler?
1249 static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1250 const DexFile::CodeItem* code_item) {
1251 // If the native GC map is null and the Dex `code_item` is not
1252 // null, then this method has been compiled with the optimizing
1253 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001254 return oat_method.GetQuickCode() != nullptr &&
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001255 oat_method.GetVmapTable() != nullptr &&
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001256 code_item != nullptr;
1257 }
1258
1259 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1260 // the dextodex compiler?
1261 static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
1262 const DexFile::CodeItem* code_item) {
1263 // If the quick code is null, the Dex `code_item` is not
1264 // null, and the vmap table is not null, then this method has been compiled
1265 // with the dextodex compiler.
1266 return oat_method.GetQuickCode() == nullptr &&
1267 oat_method.GetVmapTable() != nullptr &&
1268 code_item != nullptr;
Roland Levillainf2650d12015-05-28 14:53:28 +01001269 }
1270
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001271 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001272 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001273 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001274 const DexFile* dex_file,
1275 const DexFile::ClassDef& class_def,
1276 const DexFile::CodeItem* code_item,
1277 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001278 if ((method_access_flags & kAccNative) == 0) {
1279 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001280 Runtime* const runtime = Runtime::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001281 Handle<mirror::DexCache> dex_cache(
Mathieu Chartierf284d442016-06-02 11:48:30 -07001282 hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file, nullptr)));
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001283 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001284 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001285 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
David Brazdil15fc7292016-09-02 14:13:18 +01001286 class_def, code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001287 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001288
1289 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001290 }
1291
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001292 // The StackMapsHelper provides the stack maps in the native PC order.
1293 // For identical native PCs, the order from the CodeInfo is preserved.
1294 class StackMapsHelper {
1295 public:
1296 explicit StackMapsHelper(const uint8_t* raw_code_info)
1297 : code_info_(raw_code_info),
1298 encoding_(code_info_.ExtractEncoding()),
1299 number_of_stack_maps_(code_info_.GetNumberOfStackMaps(encoding_)),
1300 indexes_(),
1301 offset_(static_cast<size_t>(-1)),
1302 stack_map_index_(0u) {
1303 if (number_of_stack_maps_ != 0u) {
1304 // Check if native PCs are ordered.
1305 bool ordered = true;
1306 StackMap last = code_info_.GetStackMapAt(0u, encoding_);
1307 for (size_t i = 1; i != number_of_stack_maps_; ++i) {
1308 StackMap current = code_info_.GetStackMapAt(i, encoding_);
1309 if (last.GetNativePcOffset(encoding_.stack_map_encoding) >
1310 current.GetNativePcOffset(encoding_.stack_map_encoding)) {
1311 ordered = false;
1312 break;
1313 }
1314 last = current;
1315 }
1316 if (!ordered) {
1317 // Create indirection indexes for access in native PC order. We do not optimize
1318 // for the fact that there can currently be only two separately ordered ranges,
1319 // namely normal stack maps and catch-point stack maps.
1320 indexes_.resize(number_of_stack_maps_);
1321 std::iota(indexes_.begin(), indexes_.end(), 0u);
1322 std::sort(indexes_.begin(),
1323 indexes_.end(),
1324 [this](size_t lhs, size_t rhs) {
1325 StackMap left = code_info_.GetStackMapAt(lhs, encoding_);
1326 uint32_t left_pc = left.GetNativePcOffset(encoding_.stack_map_encoding);
1327 StackMap right = code_info_.GetStackMapAt(rhs, encoding_);
1328 uint32_t right_pc = right.GetNativePcOffset(encoding_.stack_map_encoding);
1329 // If the PCs are the same, compare indexes to preserve the original order.
1330 return (left_pc < right_pc) || (left_pc == right_pc && lhs < rhs);
1331 });
1332 }
1333 offset_ = GetStackMapAt(0).GetNativePcOffset(encoding_.stack_map_encoding);
1334 }
1335 }
1336
1337 const CodeInfo& GetCodeInfo() const {
1338 return code_info_;
1339 }
1340
1341 const CodeInfoEncoding& GetEncoding() const {
1342 return encoding_;
1343 }
1344
1345 size_t GetOffset() const {
1346 return offset_;
1347 }
1348
1349 StackMap GetStackMap() const {
1350 return GetStackMapAt(stack_map_index_);
1351 }
1352
1353 void Next() {
1354 ++stack_map_index_;
1355 offset_ = (stack_map_index_ == number_of_stack_maps_)
1356 ? static_cast<size_t>(-1)
1357 : GetStackMapAt(stack_map_index_).GetNativePcOffset(encoding_.stack_map_encoding);
1358 }
1359
1360 private:
1361 StackMap GetStackMapAt(size_t i) const {
1362 if (!indexes_.empty()) {
1363 i = indexes_[i];
1364 }
1365 DCHECK_LT(i, number_of_stack_maps_);
1366 return code_info_.GetStackMapAt(i, encoding_);
1367 }
1368
1369 const CodeInfo code_info_;
1370 const CodeInfoEncoding encoding_;
1371 const size_t number_of_stack_maps_;
1372 dchecked_vector<size_t> indexes_; // Used if stack map native PCs are not ordered.
1373 size_t offset_;
1374 size_t stack_map_index_;
1375 };
1376
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001377 void DumpCode(VariableIndentationOutputStream* vios,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001378 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1379 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001380 const void* quick_code = oat_method.GetQuickCode();
1381
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001382 if (code_size == 0) {
1383 code_size = oat_method.GetQuickCodeSize();
1384 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001385 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001386 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001387 return;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001388 } else if (!bad_input && IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1389 // The optimizing compiler outputs its CodeInfo data in the vmap table.
1390 StackMapsHelper helper(oat_method.GetVmapTable());
1391 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1392 size_t offset = 0;
1393 while (offset < code_size) {
1394 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
1395 if (offset == helper.GetOffset()) {
1396 ScopedIndentation indent1(vios);
1397 StackMap stack_map = helper.GetStackMap();
1398 DCHECK(stack_map.IsValid());
1399 stack_map.Dump(vios,
1400 helper.GetCodeInfo(),
1401 helper.GetEncoding(),
1402 oat_method.GetCodeOffset(),
1403 code_item->registers_size_);
1404 do {
1405 helper.Next();
1406 // There may be multiple stack maps at a given PC. We display only the first one.
1407 } while (offset == helper.GetOffset());
1408 }
1409 DCHECK_LT(offset, helper.GetOffset());
1410 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001411 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001412 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1413 size_t offset = 0;
1414 while (offset < code_size) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001415 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001416 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001417 }
1418 }
1419
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001420 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001421 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001422 const OatDumperOptions& options_;
1423 uint32_t resolved_addr2instr_;
Andreas Gampe372f3a32016-08-19 10:49:06 -07001424 const InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001425 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001426 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001427};
1428
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001429class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001430 public:
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001431 ImageDumper(std::ostream* os,
1432 gc::space::ImageSpace& image_space,
1433 const ImageHeader& image_header,
1434 OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001435 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001436 vios_(os),
1437 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001438 image_space_(image_space),
1439 image_header_(image_header),
1440 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001441
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001442 bool Dump() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001443 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001444 std::ostream& indent_os = vios_.Stream();
1445
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001446 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001447
Jeff Haodcdc85b2015-12-04 14:06:18 -08001448 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1449
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001450 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001451
Mathieu Chartiere401d142015-04-22 13:56:20 -07001452 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1453
1454 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1455 auto section = static_cast<ImageHeader::ImageSections>(i);
1456 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1457 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001458
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001459 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001460
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001461 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001462
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001463 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1464
1465 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1466
1467 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001468
Alex Lighta59dd802014-07-02 16:28:08 -07001469 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1470
Igor Murashkin46774762014-10-22 11:37:02 -07001471 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1472
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001473 {
1474 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001475 static_assert(arraysize(image_roots_descriptions_) ==
1476 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001477 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1478 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1479 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001480 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001481 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001482 if (image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001483 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001484 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001485 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001486 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1487 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001488 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001489 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1490 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001491 run++;
1492 } else {
1493 break;
1494 }
1495 }
1496 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001497 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001498 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001499 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001500 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001501 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001502 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001503 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001504 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001505 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001506 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001507 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001508 }
1509 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001510 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001511
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001512 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001513 os << "METHOD ROOTS\n";
1514 static_assert(arraysize(image_methods_descriptions_) ==
1515 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1516 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1517 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1518 const char* description = image_methods_descriptions_[i];
1519 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001520 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001521 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001522 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001523 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001524
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001525 Runtime* const runtime = Runtime::Current();
1526 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001527 std::string image_filename = image_space_.GetImageFilename();
1528 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001529 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001530 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001531 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001532 const OatFile* oat_file = image_space_.GetOatFile();
Alex Lighta59dd802014-07-02 16:28:08 -07001533 if (oat_file == nullptr) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001534 oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location);
1535 }
1536 if (oat_file == nullptr) {
1537 oat_file = OatFile::Open(oat_location,
1538 oat_location,
1539 nullptr,
1540 nullptr,
1541 false,
1542 /*low_4gb*/false,
1543 nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001544 &error_msg);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001545 }
1546 if (oat_file == nullptr) {
1547 os << "OAT FILE NOT FOUND: " << error_msg << "\n";
1548 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001549 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001550 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001551
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001552 stats_.oat_file_bytes = oat_file->Size();
1553
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001554 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001555
Mathieu Chartier02e25112013-08-14 16:14:24 -07001556 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001557 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001558 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1559 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001560 }
1561
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001562 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001563
Jeff Haodcdc85b2015-12-04 14:06:18 -08001564 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001565 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001566 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001567 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001568 {
1569 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1570 heap->FlushAllocStack();
1571 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001572 // Since FlushAllocStack() above resets the (active) allocation
1573 // stack. Need to revoke the thread-local allocation stacks that
1574 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001575 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001576 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001577 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001578 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001579 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001580 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001581 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001582 {
1583 ReaderMutexLock mu(self, *class_linker->DexLock());
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001584 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001585 ObjPtr<mirror::DexCache> dex_cache =
1586 ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001587 if (dex_cache != nullptr) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001588 dex_caches_.insert(dex_cache.Ptr());
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001589 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001590 }
1591 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001592 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001593 // Dump the normal objects before ArtMethods.
1594 image_space_.GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1595 indent_os << "\n";
1596 // TODO: Dump fields.
1597 // Dump methods after.
Jeff Haodcdc85b2015-12-04 14:06:18 -08001598 DumpArtMethodVisitor visitor(this);
Mathieu Chartiere42888f2016-04-14 10:49:19 -07001599 image_header_.VisitPackedArtMethods(&visitor,
1600 image_space_.Begin(),
1601 image_header_.GetPointerSize());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001602 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001603 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001604 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001605 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001606 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001607 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001608 size_t data_size = image_header_.GetDataSize(); // stored size in file.
1609 if (file == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001610 LOG(WARNING) << "Failed to find image in " << image_filename;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001611 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001612 stats_.file_bytes = file->GetLength();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001613 // If the image is compressed, adjust to decompressed size.
1614 size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader);
1615 if (image_header_.GetStorageMode() == ImageHeader::kStorageModeUncompressed) {
1616 DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image";
1617 }
1618 stats_.file_bytes += uncompressed_size - data_size;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001619 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001620 size_t header_bytes = sizeof(ImageHeader);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001621 const auto& object_section = image_header_.GetImageSection(ImageHeader::kSectionObjects);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001622 const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1623 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001624 const auto& dex_cache_arrays_section = image_header_.GetImageSection(
1625 ImageHeader::kSectionDexCacheArrays);
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001626 const auto& intern_section = image_header_.GetImageSection(
1627 ImageHeader::kSectionInternedStrings);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001628 const auto& class_table_section = image_header_.GetImageSection(
1629 ImageHeader::kSectionClassTable);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001630 const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1631
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001632 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001633
1634 // Objects are kObjectAlignment-aligned.
1635 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1636 if (object_section.Offset() > header_bytes) {
1637 stats_.alignment_bytes += object_section.Offset() - header_bytes;
1638 }
1639
1640 // Field section is 4-byte aligned.
1641 constexpr size_t kFieldSectionAlignment = 4U;
1642 uint32_t end_objects = object_section.Offset() + object_section.Size();
1643 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
1644 stats_.alignment_bytes += field_section.Offset() - end_objects;
1645
1646 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
1647 uint32_t end_fields = field_section.Offset() + field_section.Size();
1648 CHECK_ALIGNED(method_section.Offset(), 4);
1649 stats_.alignment_bytes += method_section.Offset() - end_fields;
1650
1651 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
1652 uint32_t end_methods = method_section.Offset() + method_section.Size();
1653 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
1654 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
1655
1656 // Intern table is 8-byte aligned.
1657 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
1658 CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
1659 stats_.alignment_bytes += intern_section.Offset() - end_caches;
1660
1661 // Add space between intern table and class table.
1662 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
1663 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
1664
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001665 // Add space between end of image data and bitmap. Expect the bitmap to be page-aligned.
1666 const size_t bitmap_offset = sizeof(ImageHeader) + data_size;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001667 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001668 stats_.alignment_bytes += RoundUp(bitmap_offset, kPageSize) - bitmap_offset;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001669
Mathieu Chartiere401d142015-04-22 13:56:20 -07001670 stats_.bitmap_bytes += bitmap_section.Size();
1671 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01001672 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01001673 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001674 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001675 stats_.class_table_bytes += class_table_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001676 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001677 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001678
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001679 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001680
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001681 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001682 }
1683
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001684 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001685 class DumpArtMethodVisitor : public ArtMethodVisitor {
1686 public:
1687 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
1688
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001689 virtual void Visit(ArtMethod* method) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001690 std::ostream& indent_os = image_dumper_->vios_.Stream();
David Sehr709b0702016-10-13 09:12:37 -07001691 indent_os << method << " " << " ArtMethod: " << ArtMethod::PrettyMethod(method) << "\n";
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001692 image_dumper_->DumpMethod(method, indent_os);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001693 indent_os << "\n";
1694 }
1695
1696 private:
1697 ImageDumper* const image_dumper_;
1698 };
1699
Mathieu Chartier3398c782016-09-30 10:27:43 -07001700 static void PrettyObjectValue(std::ostream& os,
1701 ObjPtr<mirror::Class> type,
1702 ObjPtr<mirror::Object> value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001703 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001704 CHECK(type != nullptr);
1705 if (value == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07001706 os << StringPrintf("null %s\n", type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001707 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001708 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001709 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001710 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001711 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001712 mirror::Class* klass = value->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07001713 os << StringPrintf("%p Class: %s\n", klass, mirror::Class::PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001714 } else {
David Sehr709b0702016-10-13 09:12:37 -07001715 os << StringPrintf("%p %s\n", value.Ptr(), type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001716 }
1717 }
1718
Mathieu Chartier3398c782016-09-30 10:27:43 -07001719 static void PrintField(std::ostream& os, ArtField* field, ObjPtr<mirror::Object> obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001720 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001721 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08001722 switch (field->GetTypeAsPrimitiveType()) {
1723 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08001724 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001725 break;
1726 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001727 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001728 break;
1729 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001730 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001731 break;
1732 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001733 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001734 break;
1735 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07001736 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001737 break;
1738 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07001739 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001740 break;
1741 case Primitive::kPrimBoolean:
Fred Shih37f05ef2014-07-16 18:38:08 -07001742 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1743 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001744 break;
1745 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07001746 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001747 break;
1748 case Primitive::kPrimNot: {
1749 // Get the value, don't compute the type unless it is non-null as we don't want
1750 // to cause class loading.
Mathieu Chartier3398c782016-09-30 10:27:43 -07001751 ObjPtr<mirror::Object> value = field->GetObj(obj);
Ian Rogers08f1f502014-12-02 15:04:37 -08001752 if (value == nullptr) {
1753 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07001754 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08001755 // Grab the field type without causing resolution.
Mathieu Chartier3398c782016-09-30 10:27:43 -07001756 ObjPtr<mirror::Class> field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08001757 if (field_type != nullptr) {
1758 PrettyObjectValue(os, field_type, value);
1759 } else {
Mathieu Chartier3398c782016-09-30 10:27:43 -07001760 os << StringPrintf("%p %s\n",
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07001761 value.Ptr(),
Ian Rogers08f1f502014-12-02 15:04:37 -08001762 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
1763 }
Ian Rogers50239c72013-06-17 14:53:22 -07001764 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001765 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07001766 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001767 default:
1768 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
1769 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08001770 }
1771 }
1772
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001773 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001774 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001775 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001776 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001777 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001778 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001779 for (ArtField& field : klass->GetIFields()) {
1780 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001781 }
1782 }
1783
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001784 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001785 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001786 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001787
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001788 const void* GetQuickOatCodeBegin(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001789 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001790 image_header_.GetPointerSize());
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001791 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001792 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001793 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001794 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001795 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001796 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001797 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001798 }
1799
Mathieu Chartiere401d142015-04-22 13:56:20 -07001800 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001801 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001802 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1803 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001804 return 0;
1805 }
1806 return oat_code_begin[-1];
1807 }
1808
Mathieu Chartiere401d142015-04-22 13:56:20 -07001809 const void* GetQuickOatCodeEnd(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001810 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001811 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001812 if (oat_code_begin == nullptr) {
1813 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001814 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001815 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001816 }
1817
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001818 static void Callback(mirror::Object* obj, void* arg) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001819 DCHECK(obj != nullptr);
1820 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001821 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001822 if (!state->InDumpSpace(obj)) {
1823 return;
1824 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001825
1826 size_t object_bytes = obj->SizeOf();
1827 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1828 state->stats_.object_bytes += object_bytes;
1829 state->stats_.alignment_bytes += alignment_bytes;
1830
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001831 std::ostream& os = state->vios_.Stream();
1832
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001833 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001834 if (obj_class->IsArrayClass()) {
David Sehr709b0702016-10-13 09:12:37 -07001835 os << StringPrintf("%p: %s length:%d\n", obj, obj_class->PrettyDescriptor().c_str(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001836 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001837 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001838 mirror::Class* klass = obj->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07001839 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj,
1840 mirror::Class::PrettyDescriptor(klass).c_str())
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001841 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08001842 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001843 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001844 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001845 } else {
David Sehr709b0702016-10-13 09:12:37 -07001846 os << StringPrintf("%p: %s\n", obj, obj_class->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001847 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001848 ScopedIndentation indent1(&state->vios_);
1849 DumpFields(os, obj, obj_class);
Andreas Gampe542451c2016-07-26 09:02:02 -07001850 const PointerSize image_pointer_size = state->image_header_.GetPointerSize();
Ian Rogersd5b32602012-02-26 16:40:04 -08001851 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001852 auto* obj_array = obj->AsObjectArray<mirror::Object>();
1853 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001854 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001855 size_t run = 0;
1856 for (int32_t j = i + 1; j < length; j++) {
1857 if (value == obj_array->Get(j)) {
1858 run++;
1859 } else {
1860 break;
1861 }
1862 }
1863 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001864 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001865 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001866 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001867 i = i + run;
1868 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001869 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001870 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001871 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001872 }
1873 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001874 mirror::Class* klass = obj->AsClass();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001875 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001876 os << "STATICS:\n";
1877 ScopedIndentation indent2(&state->vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001878 for (ArtField& field : klass->GetSFields()) {
1879 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001880 }
1881 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001882 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01001883 auto it = state->dex_caches_.find(obj);
1884 if (it != state->dex_caches_.end()) {
1885 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001886 const auto& field_section = state->image_header_.GetImageSection(
1887 ImageHeader::kSectionArtFields);
1888 const auto& method_section = state->image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001889 size_t num_methods = dex_cache->NumResolvedMethods();
1890 if (num_methods != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001891 os << "Methods (size=" << num_methods << "):\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01001892 ScopedIndentation indent2(&state->vios_);
1893 auto* resolved_methods = dex_cache->GetResolvedMethods();
1894 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001895 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_methods,
1896 i,
1897 image_pointer_size);
Vladimir Marko05792b92015-08-03 11:56:49 +01001898 size_t run = 0;
1899 for (size_t j = i + 1;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001900 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_methods,
1901 j,
1902 image_pointer_size);
1903 ++j) {
1904 ++run;
1905 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001906 if (run == 0) {
1907 os << StringPrintf("%zd: ", i);
1908 } else {
1909 os << StringPrintf("%zd to %zd: ", i, i + run);
1910 i = i + run;
1911 }
1912 std::string msg;
1913 if (elem == nullptr) {
1914 msg = "null";
1915 } else if (method_section.Contains(
1916 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07001917 msg = reinterpret_cast<ArtMethod*>(elem)->PrettyMethod();
Vladimir Marko05792b92015-08-03 11:56:49 +01001918 } else {
1919 msg = "<not in method section>";
1920 }
1921 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07001922 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001923 }
1924 size_t num_fields = dex_cache->NumResolvedFields();
1925 if (num_fields != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001926 os << "Fields (size=" << num_fields << "):\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01001927 ScopedIndentation indent2(&state->vios_);
1928 auto* resolved_fields = dex_cache->GetResolvedFields();
1929 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001930 auto* elem = mirror::DexCache::GetElementPtrSize(
1931 resolved_fields, i, image_pointer_size);
Vladimir Marko05792b92015-08-03 11:56:49 +01001932 size_t run = 0;
1933 for (size_t j = i + 1;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001934 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_fields,
1935 j,
1936 image_pointer_size);
1937 ++j) {
1938 ++run;
1939 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001940 if (run == 0) {
1941 os << StringPrintf("%zd: ", i);
1942 } else {
1943 os << StringPrintf("%zd to %zd: ", i, i + run);
1944 i = i + run;
1945 }
1946 std::string msg;
1947 if (elem == nullptr) {
1948 msg = "null";
1949 } else if (field_section.Contains(
1950 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07001951 msg = reinterpret_cast<ArtField*>(elem)->PrettyField();
Vladimir Marko05792b92015-08-03 11:56:49 +01001952 } else {
1953 msg = "<not in field section>";
1954 }
1955 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07001956 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001957 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001958 size_t num_types = dex_cache->NumResolvedTypes();
1959 if (num_types != 0u) {
1960 os << "Types (size=" << num_types << "):\n";
1961 ScopedIndentation indent2(&state->vios_);
1962 auto* resolved_types = dex_cache->GetResolvedTypes();
1963 for (size_t i = 0; i < num_types; ++i) {
1964 auto* elem = resolved_types[i].Read();
1965 size_t run = 0;
1966 for (size_t j = i + 1; j != num_types && elem == resolved_types[j].Read(); ++j) {
1967 ++run;
1968 }
1969 if (run == 0) {
1970 os << StringPrintf("%zd: ", i);
1971 } else {
1972 os << StringPrintf("%zd to %zd: ", i, i + run);
1973 i = i + run;
1974 }
1975 std::string msg;
1976 if (elem == nullptr) {
1977 msg = "null";
1978 } else {
David Sehr709b0702016-10-13 09:12:37 -07001979 msg = elem->PrettyClass();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001980 }
1981 os << StringPrintf("%p %s\n", elem, msg.c_str());
1982 }
1983 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001984 }
1985 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07001986 std::string temp;
1987 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001988 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001989
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001990 void DumpMethod(ArtMethod* method, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001991 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001992 DCHECK(method != nullptr);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001993 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
1994 const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
Andreas Gampe542451c2016-07-26 09:02:02 -07001995 const PointerSize pointer_size = image_header_.GetPointerSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01001996 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
1997 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07001998 if (method->IsNative()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001999 bool first_occurrence;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002000 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2001 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002002 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002003 stats_.native_to_managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002004 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002005 if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
2006 image_header_.GetPointerSize())) {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002007 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002008 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002009 } else if (method->IsAbstract() || method->IsClassInitializer()) {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002010 // Don't print information for these.
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002011 } else if (method->IsRuntimeMethod()) {
2012 ImtConflictTable* table = method->GetImtConflictTable(image_header_.GetPointerSize());
2013 if (table != nullptr) {
2014 indent_os << "IMT conflict table " << table << " method: ";
2015 for (size_t i = 0, count = table->NumEntries(pointer_size); i < count; ++i) {
David Sehr709b0702016-10-13 09:12:37 -07002016 indent_os << ArtMethod::PrettyMethod(table->GetImplementationMethod(i, pointer_size))
2017 << " ";
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002018 }
2019 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002020 } else {
2021 const DexFile::CodeItem* code_item = method->GetCodeItem();
2022 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002023 stats_.dex_instruction_bytes += dex_instruction_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002024
2025 bool first_occurrence;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002026 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002027 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01002028 // Method compiled with the optimizing compiler have no vmap table.
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002029 vmap_table_bytes = ComputeOatSize(method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002030 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002031 stats_.vmap_table_bytes += vmap_table_bytes;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002032 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002033 }
2034
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002035 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2036 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002037 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002038 stats_.managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002039 if (method->IsConstructor()) {
2040 if (method->IsStatic()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002041 stats_.class_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002042 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002043 stats_.large_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002044 }
2045 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002046 stats_.large_method_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002047 }
2048 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002049 stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002050
Igor Murashkin7617abd2015-07-10 18:27:47 -07002051 uint32_t method_access_flags = method->GetAccessFlags();
2052
Mathieu Chartiere401d142015-04-22 13:56:20 -07002053 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002054 indent_os << StringPrintf("SIZE: Dex Instructions=%zd StackMaps=%zd AccessFlags=0x%x\n",
2055 dex_instruction_bytes,
2056 vmap_table_bytes,
Igor Murashkin7617abd2015-07-10 18:27:47 -07002057 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002058
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002059 size_t total_size = dex_instruction_bytes +
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002060 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002061
2062 double expansion =
2063 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002064 stats_.ComputeOutliers(total_size, expansion, method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002065 }
2066 }
2067
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002068 std::set<const void*> already_seen_;
2069 // Compute the size of the given data within the oat file and whether this is the first time
2070 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002071 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002072 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002073 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002074 already_seen_.insert(oat_data);
2075 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002076 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002077 }
2078 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002079 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002080
2081 public:
2082 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002083 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002084 size_t file_bytes;
2085
2086 size_t header_bytes;
2087 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002088 size_t art_field_bytes;
2089 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01002090 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002091 size_t interned_strings_bytes;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002092 size_t class_table_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07002093 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002094 size_t alignment_bytes;
2095
2096 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002097 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002098 size_t managed_to_native_code_bytes;
2099 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07002100 size_t class_initializer_code_bytes;
2101 size_t large_initializer_code_bytes;
2102 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002103
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002104 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002105
2106 size_t dex_instruction_bytes;
2107
Mathieu Chartiere401d142015-04-22 13:56:20 -07002108 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002109 std::vector<size_t> method_outlier_size;
2110 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002111 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002112
Roland Levillain3887c462015-08-12 18:15:42 +01002113 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002114 : oat_file_bytes(0),
2115 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002116 header_bytes(0),
2117 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002118 art_field_bytes(0),
2119 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01002120 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002121 interned_strings_bytes(0),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002122 class_table_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07002123 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002124 alignment_bytes(0),
2125 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002126 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002127 managed_to_native_code_bytes(0),
2128 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07002129 class_initializer_code_bytes(0),
2130 large_initializer_code_bytes(0),
2131 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002132 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002133 dex_instruction_bytes(0) {}
2134
Elliott Hughesa0e18062012-04-13 15:59:59 -07002135 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002136 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002137 size_t bytes;
2138 size_t count;
2139 };
2140 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2141 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002142
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002143 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002144 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2145 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002146 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002147 it->second.count += 1;
2148 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002149 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002150 }
2151 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002152
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002153 double PercentOfOatBytes(size_t size) {
2154 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2155 }
2156
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002157 double PercentOfFileBytes(size_t size) {
2158 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2159 }
2160
2161 double PercentOfObjectBytes(size_t size) {
2162 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2163 }
2164
Mathieu Chartiere401d142015-04-22 13:56:20 -07002165 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002166 method_outlier_size.push_back(total_size);
2167 method_outlier_expansion.push_back(expansion);
2168 method_outlier.push_back(method);
2169 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002170
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002171 void DumpOutliers(std::ostream& os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002172 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002173 size_t sum_of_sizes = 0;
2174 size_t sum_of_sizes_squared = 0;
2175 size_t sum_of_expansion = 0;
2176 size_t sum_of_expansion_squared = 0;
2177 size_t n = method_outlier_size.size();
Mathieu Chartier1ebf8d32016-06-09 11:51:27 -07002178 if (n <= 1) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08002179 return;
2180 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002181 for (size_t i = 0; i < n; i++) {
2182 size_t cur_size = method_outlier_size[i];
2183 sum_of_sizes += cur_size;
2184 sum_of_sizes_squared += cur_size * cur_size;
2185 double cur_expansion = method_outlier_expansion[i];
2186 sum_of_expansion += cur_expansion;
2187 sum_of_expansion_squared += cur_expansion * cur_expansion;
2188 }
2189 size_t size_mean = sum_of_sizes / n;
2190 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2191 double expansion_mean = sum_of_expansion / n;
2192 double expansion_variance =
2193 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2194
2195 // Dump methods whose size is a certain number of standard deviations from the mean
2196 size_t dumped_values = 0;
2197 size_t skipped_values = 0;
2198 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2199 size_t cur_size_variance = i * i * size_variance;
2200 bool first = true;
2201 for (size_t j = 0; j < n; j++) {
2202 size_t cur_size = method_outlier_size[j];
2203 if (cur_size > size_mean) {
2204 size_t cur_var = cur_size - size_mean;
2205 cur_var = cur_var * cur_var;
2206 if (cur_var > cur_size_variance) {
2207 if (dumped_values > 20) {
2208 if (i == 1) {
2209 skipped_values++;
2210 } else {
2211 i = 2; // jump to counting for 1 standard deviation
2212 break;
2213 }
2214 } else {
2215 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002216 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002217 first = false;
2218 }
David Sehr709b0702016-10-13 09:12:37 -07002219 os << ArtMethod::PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002220 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002221 method_outlier_size[j] = 0; // don't consider this method again
2222 dumped_values++;
2223 }
2224 }
2225 }
2226 }
2227 }
2228 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002229 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002230 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002231 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002232 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002233
2234 // Dump methods whose expansion is a certain number of standard deviations from the mean
2235 dumped_values = 0;
2236 skipped_values = 0;
2237 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2238 double cur_expansion_variance = i * i * expansion_variance;
2239 bool first = true;
2240 for (size_t j = 0; j < n; j++) {
2241 double cur_expansion = method_outlier_expansion[j];
2242 if (cur_expansion > expansion_mean) {
2243 size_t cur_var = cur_expansion - expansion_mean;
2244 cur_var = cur_var * cur_var;
2245 if (cur_var > cur_expansion_variance) {
2246 if (dumped_values > 20) {
2247 if (i == 1) {
2248 skipped_values++;
2249 } else {
2250 i = 2; // jump to counting for 1 standard deviation
2251 break;
2252 }
2253 } else {
2254 if (first) {
2255 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002256 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002257 first = false;
2258 }
David Sehr709b0702016-10-13 09:12:37 -07002259 os << ArtMethod::PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002260 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002261 method_outlier_expansion[j] = 0.0; // don't consider this method again
2262 dumped_values++;
2263 }
2264 }
2265 }
2266 }
2267 }
2268 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002269 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002270 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002271 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002272 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002273 }
2274
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002275 void Dump(std::ostream& os, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002276 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002277 {
2278 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2279 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002280 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2281 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2282 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2283 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2284 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2285 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002286 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002287 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2288 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002289 header_bytes, PercentOfFileBytes(header_bytes),
2290 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002291 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2292 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002293 dex_cache_arrays_bytes,
2294 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002295 interned_strings_bytes,
2296 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002297 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002298 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002299 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2300 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002301 CHECK_EQ(file_bytes,
2302 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2303 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2304 bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002305 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002306
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002307 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002308 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002309 for (const auto& sizes_and_count : sizes_and_counts) {
2310 const std::string& descriptor(sizes_and_count.first);
2311 double average = static_cast<double>(sizes_and_count.second.bytes) /
2312 static_cast<double>(sizes_and_count.second.count);
2313 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002314 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002315 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002316 descriptor.c_str(), sizes_and_count.second.bytes,
2317 sizes_and_count.second.count, average, percent);
2318 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002319 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002320 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002321 CHECK_EQ(object_bytes, object_bytes_total);
2322
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002323 os << StringPrintf("oat_file_bytes = %8zd\n"
2324 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2325 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2326 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2327 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2328 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2329 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002330 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002331 managed_code_bytes,
2332 PercentOfOatBytes(managed_code_bytes),
2333 managed_to_native_code_bytes,
2334 PercentOfOatBytes(managed_to_native_code_bytes),
2335 native_to_managed_code_bytes,
2336 PercentOfOatBytes(native_to_managed_code_bytes),
2337 class_initializer_code_bytes,
2338 PercentOfOatBytes(class_initializer_code_bytes),
2339 large_initializer_code_bytes,
2340 PercentOfOatBytes(large_initializer_code_bytes),
2341 large_method_code_bytes,
2342 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002343 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002344 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002345 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002346 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2347 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002348 }
2349
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002350 os << "\n" << StringPrintf("vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002351 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002352 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002353
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002354 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2355 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002356 static_cast<double>(managed_code_bytes) /
2357 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002358 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002359 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002360 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002361
2362 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002363 }
2364 } stats_;
2365
2366 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002367 enum {
2368 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2369 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2370 kLargeConstructorDexBytes = 4000,
2371 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2372 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2373 kLargeMethodDexBytes = 16000
2374 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002375
2376 // For performance, use the *os_ directly for anything that doesn't need indentation
2377 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002378 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002379 VariableIndentationOutputStream vios_;
2380 ScopedIndentation indent1_;
2381
Ian Rogers1d54e732013-05-02 21:10:01 -07002382 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002383 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002384 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002385 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002386 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002387
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002388 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002389};
2390
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002391static int DumpImage(gc::space::ImageSpace* image_space,
2392 OatDumperOptions* options,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002393 std::ostream* os) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002394 const ImageHeader& image_header = image_space->GetImageHeader();
2395 if (!image_header.IsValid()) {
2396 fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
2397 return EXIT_FAILURE;
2398 }
2399 ImageDumper image_dumper(os, *image_space, image_header, options);
2400 if (!image_dumper.Dump()) {
2401 return EXIT_FAILURE;
2402 }
2403 return EXIT_SUCCESS;
2404}
2405
2406static int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002407 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002408 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002409 options->class_loader_ = &null_class_loader;
2410
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002411 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002412 if (options->app_image_ != nullptr) {
2413 if (options->app_oat_ == nullptr) {
2414 LOG(ERROR) << "Can not dump app image without app oat file";
Jeff Haodcdc85b2015-12-04 14:06:18 -08002415 return EXIT_FAILURE;
2416 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002417 // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
2418 // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
2419 // pointers into 32 bit pointer sized ArtMethods.
2420 std::string error_msg;
2421 std::unique_ptr<OatFile> oat_file(OatFile::Open(options->app_oat_,
2422 options->app_oat_,
2423 nullptr,
2424 nullptr,
2425 false,
2426 /*low_4gb*/true,
2427 nullptr,
2428 &error_msg));
2429 if (oat_file == nullptr) {
2430 LOG(ERROR) << "Failed to open oat file " << options->app_oat_ << " with error " << error_msg;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002431 return EXIT_FAILURE;
2432 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002433 std::unique_ptr<gc::space::ImageSpace> space(
2434 gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg));
2435 if (space == nullptr) {
2436 LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error "
2437 << error_msg;
2438 }
2439 // Open dex files for the image.
2440 std::vector<std::unique_ptr<const DexFile>> dex_files;
2441 if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
2442 LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
2443 << error_msg;
2444 }
2445 // Dump the actual image.
2446 int result = DumpImage(space.get(), options, os);
2447 if (result != EXIT_SUCCESS) {
2448 return result;
2449 }
2450 // Fall through to dump the boot images.
2451 }
2452
2453 gc::Heap* heap = runtime->GetHeap();
2454 CHECK(heap->HasBootImageSpace()) << "No image spaces";
2455 for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
2456 int result = DumpImage(image_space, options, os);
2457 if (result != EXIT_SUCCESS) {
2458 return result;
2459 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002460 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002461 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002462}
2463
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002464static jobject InstallOatFile(Runtime* runtime,
2465 std::unique_ptr<OatFile> oat_file,
2466 std::vector<const DexFile*>* class_path)
2467 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002468 Thread* self = Thread::Current();
2469 CHECK(self != nullptr);
2470 // Need well-known-classes.
2471 WellKnownClasses::Init(self->GetJniEnv());
2472
2473 // Need to register dex files to get a working dex cache.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002474 OatFile* oat_file_ptr = oat_file.get();
Andreas Gampe00b25f32014-09-17 21:49:05 -07002475 ClassLinker* class_linker = runtime->GetClassLinker();
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002476 runtime->GetOatFileManager().RegisterOatFile(std::move(oat_file));
2477 for (const OatFile::OatDexFile* odf : oat_file_ptr->GetOatDexFiles()) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002478 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002479 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002480 CHECK(dex_file != nullptr) << error_msg;
Mathieu Chartierf284d442016-06-02 11:48:30 -07002481 class_linker->RegisterDexFile(*dex_file, nullptr);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002482 class_path->push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002483 }
2484
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002485 // Need a class loader. Fake that we're a compiler.
2486 // Note: this will run initializers through the unstarted runtime, so make sure it's
2487 // initialized.
2488 interpreter::UnstartedRuntime::Initialize();
2489
2490 jobject class_loader = class_linker->CreatePathClassLoader(self, *class_path);
2491
2492 return class_loader;
2493}
2494
2495static int DumpOatWithRuntime(Runtime* runtime,
2496 std::unique_ptr<OatFile> oat_file,
2497 OatDumperOptions* options,
2498 std::ostream* os) {
2499 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2500 ScopedObjectAccess soa(Thread::Current());
2501
2502 OatFile* oat_file_ptr = oat_file.get();
2503 std::vector<const DexFile*> class_path;
2504 jobject class_loader = InstallOatFile(runtime, std::move(oat_file), &class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002505
2506 // Use the class loader while dumping.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002507 StackHandleScope<1> scope(soa.Self());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002508 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07002509 soa.Decode<mirror::ClassLoader>(class_loader));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002510 options->class_loader_ = &loader_handle;
2511
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002512 OatDumper oat_dumper(*oat_file_ptr, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002513 bool success = oat_dumper.Dump(*os);
2514 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2515}
2516
2517static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002518 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002519 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002520 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002521 options->class_loader_ = &null_class_loader;
2522
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002523 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002524 bool success = oat_dumper.Dump(*os);
2525 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2526}
2527
2528static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2529 std::ostream* os) {
2530 std::string error_msg;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002531 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
2532 oat_filename,
2533 nullptr,
2534 nullptr,
2535 false,
2536 /*low_4gb*/false,
2537 nullptr,
2538 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002539 if (oat_file == nullptr) {
2540 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2541 return EXIT_FAILURE;
2542 }
2543
2544 if (runtime != nullptr) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002545 return DumpOatWithRuntime(runtime, std::move(oat_file), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002546 } else {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002547 return DumpOatWithoutRuntime(oat_file.get(), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002548 }
2549}
2550
David Srbecky2fdd03c2016-03-10 15:32:37 +00002551static int SymbolizeOat(const char* oat_filename, std::string& output_name, bool no_bits) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002552 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002553 OatFile* oat_file = OatFile::Open(oat_filename,
2554 oat_filename,
2555 nullptr,
2556 nullptr,
2557 false,
2558 /*low_4gb*/false,
2559 nullptr,
2560 &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002561 if (oat_file == nullptr) {
2562 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2563 return EXIT_FAILURE;
2564 }
2565
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002566 bool result;
2567 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
2568 // files for 64-bit code in the past.
2569 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002570 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file, output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002571 result = oat_symbolizer.Symbolize();
2572 } else {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002573 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file, output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002574 result = oat_symbolizer.Symbolize();
2575 }
2576 if (!result) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002577 fprintf(stderr, "Failed to symbolize\n");
2578 return EXIT_FAILURE;
2579 }
2580
2581 return EXIT_SUCCESS;
2582}
2583
Andreas Gampe9fded872016-09-25 16:08:35 -07002584class IMTDumper {
2585 public:
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002586 static bool Dump(Runtime* runtime,
2587 const std::string& imt_file,
2588 bool dump_imt_stats,
2589 const char* oat_filename) {
2590 Thread* self = Thread::Current();
2591
2592 ScopedObjectAccess soa(self);
2593 StackHandleScope<1> scope(self);
2594 MutableHandle<mirror::ClassLoader> class_loader = scope.NewHandle<mirror::ClassLoader>(nullptr);
2595 std::vector<const DexFile*> class_path;
2596
2597 if (oat_filename != nullptr) {
2598 std::string error_msg;
2599 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
2600 oat_filename,
2601 nullptr,
2602 nullptr,
2603 false,
2604 /*low_4gb*/false,
2605 nullptr,
2606 &error_msg));
2607 if (oat_file == nullptr) {
2608 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2609 return false;
2610 }
2611
2612 class_loader.Assign(soa.Decode<mirror::ClassLoader>(
2613 InstallOatFile(runtime, std::move(oat_file), &class_path)));
2614 } else {
2615 class_loader.Assign(nullptr); // Boot classloader. Just here for explicit documentation.
2616 class_path = runtime->GetClassLinker()->GetBootClassPath();
2617 }
2618
2619 if (!imt_file.empty()) {
2620 return DumpImt(runtime, imt_file, class_loader);
2621 }
2622
2623 if (dump_imt_stats) {
2624 return DumpImtStats(runtime, class_path, class_loader);
2625 }
2626
2627 LOG(FATAL) << "Should not reach here";
2628 UNREACHABLE();
2629 }
2630
2631 private:
2632 static bool DumpImt(Runtime* runtime,
2633 const std::string& imt_file,
2634 Handle<mirror::ClassLoader> h_class_loader)
2635 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002636 std::vector<std::string> lines = ReadCommentedInputFromFile(imt_file);
2637 std::unordered_set<std::string> prepared;
2638
2639 for (const std::string& line : lines) {
2640 // A line should be either a class descriptor, in which case we will dump the complete IMT,
2641 // or a class descriptor and an interface method, in which case we will lookup the method,
2642 // determine its IMT slot, and check the class' IMT.
2643 size_t first_space = line.find(' ');
2644 if (first_space == std::string::npos) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002645 DumpIMTForClass(runtime, line, h_class_loader, &prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07002646 } else {
2647 DumpIMTForMethod(runtime,
2648 line.substr(0, first_space),
2649 line.substr(first_space + 1, std::string::npos),
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002650 h_class_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07002651 &prepared);
2652 }
2653 std::cerr << std::endl;
2654 }
2655
2656 return true;
2657 }
2658
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002659 static bool DumpImtStats(Runtime* runtime,
2660 const std::vector<const DexFile*>& dex_files,
2661 Handle<mirror::ClassLoader> h_class_loader)
2662 REQUIRES_SHARED(Locks::mutator_lock_) {
2663 size_t without_imt = 0;
2664 size_t with_imt = 0;
Andreas Gampe9fded872016-09-25 16:08:35 -07002665 std::map<size_t, size_t> histogram;
2666
2667 ClassLinker* class_linker = runtime->GetClassLinker();
2668 const PointerSize pointer_size = class_linker->GetImagePointerSize();
2669 std::unordered_set<std::string> prepared;
2670
2671 Thread* self = Thread::Current();
Andreas Gampe9fded872016-09-25 16:08:35 -07002672 StackHandleScope<1> scope(self);
2673 MutableHandle<mirror::Class> h_klass(scope.NewHandle<mirror::Class>(nullptr));
2674
2675 for (const DexFile* dex_file : dex_files) {
2676 for (uint32_t class_def_index = 0;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002677 class_def_index != dex_file->NumClassDefs();
2678 ++class_def_index) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002679 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
2680 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002681 h_klass.Assign(class_linker->FindClass(self, descriptor, h_class_loader));
Andreas Gampe9fded872016-09-25 16:08:35 -07002682 if (h_klass.Get() == nullptr) {
2683 std::cerr << "Warning: could not load " << descriptor << std::endl;
2684 continue;
2685 }
2686
2687 if (HasNoIMT(runtime, h_klass, pointer_size, &prepared)) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002688 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07002689 continue;
2690 }
2691
2692 ImTable* im_table = PrepareAndGetImTable(runtime, h_klass, pointer_size, &prepared);
2693 if (im_table == nullptr) {
2694 // Should not happen, but accept.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002695 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07002696 continue;
2697 }
2698
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002699 with_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07002700 for (size_t imt_index = 0; imt_index != ImTable::kSize; ++imt_index) {
2701 ArtMethod* ptr = im_table->Get(imt_index, pointer_size);
2702 if (ptr->IsRuntimeMethod()) {
2703 if (ptr->IsImtUnimplementedMethod()) {
2704 histogram[0]++;
2705 } else {
2706 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
2707 histogram[current_table->NumEntries(pointer_size)]++;
2708 }
2709 } else {
2710 histogram[1]++;
2711 }
2712 }
2713 }
2714 }
2715
2716 std::cerr << "IMT stats:"
2717 << std::endl << std::endl;
2718
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002719 std::cerr << " " << with_imt << " classes with IMT."
Andreas Gampe9fded872016-09-25 16:08:35 -07002720 << std::endl << std::endl;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002721 std::cerr << " " << without_imt << " classes without IMT (or copy from Object)."
Andreas Gampe9fded872016-09-25 16:08:35 -07002722 << std::endl << std::endl;
2723
2724 double sum_one = 0;
2725 size_t count_one = 0;
2726
2727 std::cerr << " " << "IMT histogram" << std::endl;
2728 for (auto& bucket : histogram) {
2729 std::cerr << " " << bucket.first << " " << bucket.second << std::endl;
2730 if (bucket.first > 0) {
2731 sum_one += bucket.second * bucket.first;
2732 count_one += bucket.second;
2733 }
2734 }
2735
2736 double count_zero = count_one + histogram[0];
2737 std::cerr << " Stats:" << std::endl;
2738 std::cerr << " Average depth (including empty): " << (sum_one / count_zero) << std::endl;
2739 std::cerr << " Average depth (excluding empty): " << (sum_one / count_one) << std::endl;
2740
2741 return true;
2742 }
2743
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002744 // Return whether the given class has no IMT (or the one shared with java.lang.Object).
Andreas Gampe9fded872016-09-25 16:08:35 -07002745 static bool HasNoIMT(Runtime* runtime,
2746 Handle<mirror::Class> klass,
2747 const PointerSize pointer_size,
2748 std::unordered_set<std::string>* prepared)
2749 REQUIRES_SHARED(Locks::mutator_lock_) {
2750 if (klass->IsObjectClass() || !klass->ShouldHaveImt()) {
2751 return true;
2752 }
2753
2754 if (klass->GetImt(pointer_size) == nullptr) {
2755 PrepareClass(runtime, klass, prepared);
2756 }
2757
2758 mirror::Class* object_class = mirror::Class::GetJavaLangClass()->GetSuperClass();
2759 DCHECK(object_class->IsObjectClass());
2760
2761 bool result = klass->GetImt(pointer_size) == object_class->GetImt(pointer_size);
2762
2763 if (klass->GetIfTable() == nullptr) {
2764 DCHECK(result);
2765 }
2766
2767 return result;
2768 }
2769
2770 static void PrintTable(ImtConflictTable* table, PointerSize pointer_size)
2771 REQUIRES_SHARED(Locks::mutator_lock_) {
2772 if (table == nullptr) {
2773 std::cerr << " <No IMT?>" << std::endl;
2774 return;
2775 }
2776 size_t table_index = 0;
2777 for (;;) {
2778 ArtMethod* ptr = table->GetInterfaceMethod(table_index, pointer_size);
2779 if (ptr == nullptr) {
2780 return;
2781 }
2782 table_index++;
David Sehr709b0702016-10-13 09:12:37 -07002783 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07002784 }
2785 }
2786
2787 static ImTable* PrepareAndGetImTable(Runtime* runtime,
2788 Thread* self,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002789 Handle<mirror::ClassLoader> h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07002790 const std::string& class_name,
2791 const PointerSize pointer_size,
2792 mirror::Class** klass_out,
2793 std::unordered_set<std::string>* prepared)
2794 REQUIRES_SHARED(Locks::mutator_lock_) {
2795 if (class_name.empty()) {
2796 return nullptr;
2797 }
2798
2799 std::string descriptor;
2800 if (class_name[0] == 'L') {
2801 descriptor = class_name;
2802 } else {
2803 descriptor = DotToDescriptor(class_name.c_str());
2804 }
2805
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002806 mirror::Class* klass = runtime->GetClassLinker()->FindClass(self, descriptor.c_str(), h_loader);
Andreas Gampe9fded872016-09-25 16:08:35 -07002807
2808 if (klass == nullptr) {
2809 self->ClearException();
2810 std::cerr << "Did not find " << class_name << std::endl;
2811 *klass_out = nullptr;
2812 return nullptr;
2813 }
2814
2815 StackHandleScope<1> scope(Thread::Current());
2816 Handle<mirror::Class> h_klass = scope.NewHandle<mirror::Class>(klass);
2817
2818 ImTable* ret = PrepareAndGetImTable(runtime, h_klass, pointer_size, prepared);
2819 *klass_out = h_klass.Get();
2820 return ret;
2821 }
2822
2823 static ImTable* PrepareAndGetImTable(Runtime* runtime,
2824 Handle<mirror::Class> h_klass,
2825 const PointerSize pointer_size,
2826 std::unordered_set<std::string>* prepared)
2827 REQUIRES_SHARED(Locks::mutator_lock_) {
2828 PrepareClass(runtime, h_klass, prepared);
2829 return h_klass->GetImt(pointer_size);
2830 }
2831
2832 static void DumpIMTForClass(Runtime* runtime,
2833 const std::string& class_name,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002834 Handle<mirror::ClassLoader> h_loader,
2835 std::unordered_set<std::string>* prepared)
2836 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002837 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
2838 mirror::Class* klass;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002839 ImTable* imt = PrepareAndGetImTable(runtime,
2840 Thread::Current(),
2841 h_loader,
2842 class_name,
2843 pointer_size,
2844 &klass,
2845 prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07002846 if (imt == nullptr) {
2847 return;
2848 }
2849
2850 std::cerr << class_name << std::endl << " IMT:" << std::endl;
2851 for (size_t index = 0; index < ImTable::kSize; ++index) {
2852 std::cerr << " " << index << ":" << std::endl;
2853 ArtMethod* ptr = imt->Get(index, pointer_size);
2854 if (ptr->IsRuntimeMethod()) {
2855 if (ptr->IsImtUnimplementedMethod()) {
2856 std::cerr << " <empty>" << std::endl;
2857 } else {
2858 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
2859 PrintTable(current_table, pointer_size);
2860 }
2861 } else {
David Sehr709b0702016-10-13 09:12:37 -07002862 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07002863 }
2864 }
2865
2866 std::cerr << " Interfaces:" << std::endl;
2867 // Run through iftable, find methods that slot here, see if they fit.
2868 mirror::IfTable* if_table = klass->GetIfTable();
2869 if (if_table != nullptr) {
2870 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
2871 mirror::Class* iface = if_table->GetInterface(i);
2872 std::string iface_name;
2873 std::cerr << " " << iface->GetDescriptor(&iface_name) << std::endl;
2874
2875 for (ArtMethod& iface_method : iface->GetVirtualMethods(pointer_size)) {
Andreas Gampea1ff30f2016-09-27 12:19:45 -07002876 uint32_t class_hash, name_hash, signature_hash;
2877 ImTable::GetImtHashComponents(&iface_method, &class_hash, &name_hash, &signature_hash);
Andreas Gampe9fded872016-09-25 16:08:35 -07002878 uint32_t imt_slot = ImTable::GetImtIndex(&iface_method);
David Sehr709b0702016-10-13 09:12:37 -07002879 std::cerr << " " << iface_method.PrettyMethod(true)
Andreas Gampea1ff30f2016-09-27 12:19:45 -07002880 << " slot=" << imt_slot
2881 << std::hex
2882 << " class_hash=0x" << class_hash
2883 << " name_hash=0x" << name_hash
2884 << " signature_hash=0x" << signature_hash
2885 << std::dec
2886 << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07002887 }
2888 }
2889 }
2890 }
2891
2892 static void DumpIMTForMethod(Runtime* runtime,
2893 const std::string& class_name,
2894 const std::string& method,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002895 Handle<mirror::ClassLoader> h_loader,
2896 std::unordered_set<std::string>* prepared)
2897 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002898 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
2899 mirror::Class* klass;
2900 ImTable* imt = PrepareAndGetImTable(runtime,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002901 Thread::Current(),
2902 h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07002903 class_name,
2904 pointer_size,
2905 &klass,
2906 prepared);
2907 if (imt == nullptr) {
2908 return;
2909 }
2910
2911 std::cerr << class_name << " <" << method << ">" << std::endl;
2912 for (size_t index = 0; index < ImTable::kSize; ++index) {
2913 ArtMethod* ptr = imt->Get(index, pointer_size);
2914 if (ptr->IsRuntimeMethod()) {
2915 if (ptr->IsImtUnimplementedMethod()) {
2916 continue;
2917 }
2918
2919 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
2920 if (current_table == nullptr) {
2921 continue;
2922 }
2923
2924 size_t table_index = 0;
2925 for (;;) {
2926 ArtMethod* ptr2 = current_table->GetInterfaceMethod(table_index, pointer_size);
2927 if (ptr2 == nullptr) {
2928 break;
2929 }
2930 table_index++;
2931
David Sehr709b0702016-10-13 09:12:37 -07002932 std::string p_name = ptr2->PrettyMethod(true);
Andreas Gampe9fded872016-09-25 16:08:35 -07002933 if (StartsWith(p_name, method.c_str())) {
2934 std::cerr << " Slot "
2935 << index
2936 << " ("
2937 << current_table->NumEntries(pointer_size)
2938 << ")"
2939 << std::endl;
2940 PrintTable(current_table, pointer_size);
2941 return;
2942 }
2943 }
2944 } else {
David Sehr709b0702016-10-13 09:12:37 -07002945 std::string p_name = ptr->PrettyMethod(true);
Andreas Gampe9fded872016-09-25 16:08:35 -07002946 if (StartsWith(p_name, method.c_str())) {
2947 std::cerr << " Slot " << index << " (1)" << std::endl;
2948 std::cerr << " " << p_name << std::endl;
2949 } else {
2950 // Run through iftable, find methods that slot here, see if they fit.
2951 mirror::IfTable* if_table = klass->GetIfTable();
2952 if (if_table != nullptr) {
2953 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
2954 mirror::Class* iface = if_table->GetInterface(i);
2955 size_t num_methods = iface->NumDeclaredVirtualMethods();
2956 if (num_methods > 0) {
2957 for (ArtMethod& iface_method : iface->GetMethods(pointer_size)) {
2958 if (ImTable::GetImtIndex(&iface_method) == index) {
David Sehr709b0702016-10-13 09:12:37 -07002959 std::string i_name = iface_method.PrettyMethod(true);
Andreas Gampe9fded872016-09-25 16:08:35 -07002960 if (StartsWith(i_name, method.c_str())) {
2961 std::cerr << " Slot " << index << " (1)" << std::endl;
2962 std::cerr << " " << p_name << " (" << i_name << ")" << std::endl;
2963 }
2964 }
2965 }
2966 }
2967 }
2968 }
2969 }
2970 }
2971 }
2972 }
2973
2974 // Read lines from the given stream, dropping comments and empty lines
2975 static std::vector<std::string> ReadCommentedInputStream(std::istream& in_stream) {
2976 std::vector<std::string> output;
2977 while (in_stream.good()) {
2978 std::string dot;
2979 std::getline(in_stream, dot);
2980 if (StartsWith(dot, "#") || dot.empty()) {
2981 continue;
2982 }
2983 output.push_back(dot);
2984 }
2985 return output;
2986 }
2987
2988 // Read lines from the given file, dropping comments and empty lines.
2989 static std::vector<std::string> ReadCommentedInputFromFile(const std::string& input_filename) {
2990 std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
2991 if (input_file.get() == nullptr) {
2992 LOG(ERROR) << "Failed to open input file " << input_filename;
2993 return std::vector<std::string>();
2994 }
2995 std::vector<std::string> result = ReadCommentedInputStream(*input_file);
2996 input_file->close();
2997 return result;
2998 }
2999
3000 // Prepare a class, i.e., ensure it has a filled IMT. Will do so recursively for superclasses,
3001 // and note in the given set that the work was done.
3002 static void PrepareClass(Runtime* runtime,
3003 Handle<mirror::Class> h_klass,
3004 std::unordered_set<std::string>* done)
3005 REQUIRES_SHARED(Locks::mutator_lock_) {
3006 if (!h_klass->ShouldHaveImt()) {
3007 return;
3008 }
3009
3010 std::string name;
3011 name = h_klass->GetDescriptor(&name);
3012
3013 if (done->find(name) != done->end()) {
3014 return;
3015 }
3016 done->insert(name);
3017
3018 if (h_klass->HasSuperClass()) {
3019 StackHandleScope<1> h(Thread::Current());
3020 PrepareClass(runtime, h.NewHandle<mirror::Class>(h_klass->GetSuperClass()), done);
3021 }
3022
3023 if (!h_klass->IsTemp()) {
3024 runtime->GetClassLinker()->FillIMTAndConflictTables(h_klass.Get());
3025 }
3026 }
3027};
3028
Igor Murashkin37743352014-11-13 14:38:00 -08003029struct OatdumpArgs : public CmdlineArgs {
3030 protected:
3031 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003032
Igor Murashkin37743352014-11-13 14:38:00 -08003033 virtual ParseStatus ParseCustom(const StringPiece& option,
3034 std::string* error_msg) OVERRIDE {
3035 {
3036 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
3037 if (base_parse != kParseUnknownArgument) {
3038 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003039 }
3040 }
3041
Igor Murashkin37743352014-11-13 14:38:00 -08003042 if (option.starts_with("--oat-file=")) {
3043 oat_filename_ = option.substr(strlen("--oat-file=")).data();
3044 } else if (option.starts_with("--image=")) {
3045 image_location_ = option.substr(strlen("--image=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003046 } else if (option == "--no-dump:vmap") {
3047 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01003048 } else if (option =="--dump:code_info_stack_maps") {
3049 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003050 } else if (option == "--no-disassemble") {
3051 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003052 } else if (option =="--header-only") {
3053 dump_header_only_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003054 } else if (option.starts_with("--symbolize=")) {
3055 oat_filename_ = option.substr(strlen("--symbolize=")).data();
3056 symbolize_ = true;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003057 } else if (option.starts_with("--only-keep-debug")) {
3058 only_keep_debug_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003059 } else if (option.starts_with("--class-filter=")) {
3060 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003061 } else if (option.starts_with("--method-filter=")) {
3062 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003063 } else if (option.starts_with("--list-classes")) {
3064 list_classes_ = true;
3065 } else if (option.starts_with("--list-methods")) {
3066 list_methods_ = true;
3067 } else if (option.starts_with("--export-dex-to=")) {
3068 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
3069 } else if (option.starts_with("--addr2instr=")) {
3070 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
3071 *error_msg = "Address conversion failed";
3072 return kParseError;
3073 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003074 } else if (option.starts_with("--app-image=")) {
3075 app_image_ = option.substr(strlen("--app-image=")).data();
3076 } else if (option.starts_with("--app-oat=")) {
3077 app_oat_ = option.substr(strlen("--app-oat=")).data();
Andreas Gampe9fded872016-09-25 16:08:35 -07003078 } else if (option.starts_with("--dump-imt=")) {
3079 imt_dump_ = option.substr(strlen("--dump-imt=")).data();
3080 } else if (option == "--dump-imt-stats") {
3081 imt_stat_dump_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003082 } else {
3083 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003084 }
3085
Igor Murashkin37743352014-11-13 14:38:00 -08003086 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003087 }
3088
Igor Murashkin37743352014-11-13 14:38:00 -08003089 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
3090 // Infer boot image location from the image location if possible.
3091 if (boot_image_location_ == nullptr) {
3092 boot_image_location_ = image_location_;
3093 }
3094
3095 // Perform the parent checks.
3096 ParseStatus parent_checks = Base::ParseChecks(error_msg);
3097 if (parent_checks != kParseOk) {
3098 return parent_checks;
3099 }
3100
3101 // Perform our own checks.
3102 if (image_location_ == nullptr && oat_filename_ == nullptr) {
3103 *error_msg = "Either --image or --oat-file must be specified";
3104 return kParseError;
3105 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
3106 *error_msg = "Either --image or --oat-file must be specified but not both";
3107 return kParseError;
3108 }
3109
3110 return kParseOk;
3111 }
3112
3113 virtual std::string GetUsage() const {
3114 std::string usage;
3115
3116 usage +=
3117 "Usage: oatdump [options] ...\n"
3118 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
3119 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
3120 "\n"
3121 // Either oat-file or image is required.
3122 " --oat-file=<file.oat>: specifies an input oat filename.\n"
3123 " Example: --oat-file=/system/framework/boot.oat\n"
3124 "\n"
3125 " --image=<file.art>: specifies an input image location.\n"
3126 " Example: --image=/system/framework/boot.art\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003127 "\n"
3128 " --app-image=<file.art>: specifies an input app image. Must also have a specified\n"
3129 " boot image and app oat file.\n"
3130 " Example: --app-image=app.art\n"
3131 "\n"
3132 " --app-oat=<file.odex>: specifies an input app oat.\n"
3133 " Example: --app-oat=app.odex\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003134 "\n";
3135
3136 usage += Base::GetUsage();
3137
3138 usage += // Optional.
Igor Murashkin37743352014-11-13 14:38:00 -08003139 " --no-dump:vmap may be used to disable vmap dumping.\n"
3140 " Example: --no-dump:vmap\n"
3141 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01003142 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
3143 " Example: --dump:code_info_stack_maps\n"
3144 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003145 " --no-disassemble may be used to disable disassembly.\n"
3146 " Example: --no-disassemble\n"
3147 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00003148 " --header-only may be used to print only the oat header.\n"
3149 " Example: --header-only\n"
3150 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003151 " --list-classes may be used to list target file classes (can be used with filters).\n"
3152 " Example: --list-classes\n"
3153 " Example: --list-classes --class-filter=com.example.foo\n"
3154 "\n"
3155 " --list-methods may be used to list target file methods (can be used with filters).\n"
3156 " Example: --list-methods\n"
3157 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
3158 "\n"
3159 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
3160 " Example: --symbolize=/system/framework/boot.oat\n"
3161 "\n"
David Srbecky2fdd03c2016-03-10 15:32:37 +00003162 " --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
3163 " .rodata and .text sections are omitted in the output file to save space.\n"
3164 " Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
3165 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003166 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
3167 " Example: --class-filter=com.example.foo\n"
3168 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003169 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
3170 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003171 "\n"
3172 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
3173 " Example: --export-dex-to=/data/local/tmp\n"
3174 "\n"
3175 " --addr2instr=<address>: output matching method disassembled code from relative\n"
3176 " address (e.g. PC from crash dump)\n"
3177 " Example: --addr2instr=0x00001a3b\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003178 "\n"
3179 " --dump-imt=<file.txt>: output IMT collisions (if any) for the given receiver\n"
3180 " types and interface methods in the given file. The file\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003181 " is read line-wise, where each line should either be a class\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003182 " name or descriptor, or a class name/descriptor and a prefix\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003183 " of a complete method name (separated by a whitespace).\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003184 " Example: --dump-imt=imt.txt\n"
3185 "\n"
3186 " --dump-imt-stats: output IMT statistics for the given boot image\n"
3187 " Example: --dump-imt-stats"
Igor Murashkin37743352014-11-13 14:38:00 -08003188 "\n";
3189
3190 return usage;
3191 }
3192
3193 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07003194 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003195 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00003196 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07003197 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003198 std::string elf_filename_prefix_;
Andreas Gampe9fded872016-09-25 16:08:35 -07003199 std::string imt_dump_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003200 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01003201 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003202 bool disassemble_code_ = true;
3203 bool symbolize_ = false;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003204 bool only_keep_debug_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003205 bool list_classes_ = false;
3206 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003207 bool dump_header_only_ = false;
Andreas Gampe9fded872016-09-25 16:08:35 -07003208 bool imt_stat_dump_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003209 uint32_t addr2instr_ = 0;
3210 const char* export_dex_location_ = nullptr;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003211 const char* app_image_ = nullptr;
3212 const char* app_oat_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003213};
3214
Igor Murashkin37743352014-11-13 14:38:00 -08003215struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
3216 virtual bool NeedsRuntime() OVERRIDE {
3217 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003218
Igor Murashkin37743352014-11-13 14:38:00 -08003219 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
3220 bool absolute_addresses = (args_->oat_filename_ == nullptr);
3221
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003222 oat_dumper_options_.reset(new OatDumperOptions(
Igor Murashkin37743352014-11-13 14:38:00 -08003223 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01003224 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08003225 args_->disassemble_code_,
3226 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003227 args_->class_filter_,
3228 args_->method_filter_,
3229 args_->list_classes_,
3230 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00003231 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003232 args_->export_dex_location_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003233 args_->app_image_,
3234 args_->app_oat_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003235 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08003236
Andreas Gampe9fded872016-09-25 16:08:35 -07003237 return (args_->boot_image_location_ != nullptr ||
3238 args_->image_location_ != nullptr ||
3239 !args_->imt_dump_.empty()) &&
Igor Murashkin37743352014-11-13 14:38:00 -08003240 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003241 }
3242
Igor Murashkin37743352014-11-13 14:38:00 -08003243 virtual bool ExecuteWithoutRuntime() OVERRIDE {
3244 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08003245 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003246
Mathieu Chartierd424d082014-10-15 10:31:46 -07003247 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08003248
Andreas Gampec24f3992014-12-17 20:40:11 -08003249 if (args_->symbolize_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00003250 // ELF has special kind of section called SHT_NOBITS which allows us to create
3251 // sections which exist but their data is omitted from the ELF file to save space.
3252 // This is what "strip --only-keep-debug" does when it creates separate ELF file
3253 // with only debug data. We use it in similar way to exclude .rodata and .text.
3254 bool no_bits = args_->only_keep_debug_;
3255 return SymbolizeOat(args_->oat_filename_, args_->output_name_, no_bits) == EXIT_SUCCESS;
Andreas Gampec24f3992014-12-17 20:40:11 -08003256 } else {
3257 return DumpOat(nullptr,
3258 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003259 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08003260 args_->os_) == EXIT_SUCCESS;
3261 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07003262 }
3263
Igor Murashkin37743352014-11-13 14:38:00 -08003264 virtual bool ExecuteWithRuntime(Runtime* runtime) {
3265 CHECK(args_ != nullptr);
3266
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003267 if (!args_->imt_dump_.empty() || args_->imt_stat_dump_) {
3268 return IMTDumper::Dump(runtime,
3269 args_->imt_dump_,
3270 args_->imt_stat_dump_,
3271 args_->oat_filename_);
Andreas Gampe9fded872016-09-25 16:08:35 -07003272 }
3273
Igor Murashkin37743352014-11-13 14:38:00 -08003274 if (args_->oat_filename_ != nullptr) {
3275 return DumpOat(runtime,
3276 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003277 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08003278 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003279 }
Igor Murashkin37743352014-11-13 14:38:00 -08003280
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003281 return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003282 }
3283
Igor Murashkin37743352014-11-13 14:38:00 -08003284 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
3285};
Andreas Gampe00b25f32014-09-17 21:49:05 -07003286
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003287} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07003288
3289int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08003290 art::OatdumpMain main;
3291 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07003292}