blob: 878d0f2cfeea033ad603951ec08f2067c2d87209 [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
Andreas Gampe46ee31b2016-12-14 10:11:49 -080029#include "android-base/stringprintf.h"
Andreas Gampe9186ced2016-12-12 14:28:21 -080030#include "android-base/strings.h"
31
Ian Rogersd582fa42014-11-05 23:46:43 -080032#include "arch/instruction_set_features.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070033#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070034#include "art_method-inl.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000035#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080036#include "base/unix_file/fd_file.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070037#include "class_linker.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038#include "class_linker-inl.h"
David Srbecky5d950762016-03-07 20:47:29 +000039#include "debug/elf_debug_writer.h"
40#include "debug/method_debug_info.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070041#include "dex_file-inl.h"
Christina Wadsworthbc233ac2016-06-20 15:01:32 -070042#include "dex_instruction-inl.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080043#include "disassembler.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070044#include "elf_builder.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070045#include "gc/space/image_space.h"
46#include "gc/space/large_object_space.h"
47#include "gc/space/space-inl.h"
Mathieu Chartier4a26f172016-01-26 14:26:18 -080048#include "image-inl.h"
Andreas Gampe9fded872016-09-25 16:08:35 -070049#include "imtable-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080050#include "indenter.h"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -070051#include "interpreter/unstarted_runtime.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000052#include "linker/buffered_output_stream.h"
53#include "linker/file_output_stream.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080054#include "mirror/array-inl.h"
55#include "mirror/class-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010056#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080057#include "mirror/object-inl.h"
58#include "mirror/object_array-inl.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080059#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010060#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070061#include "oat_file_manager.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080062#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070063#include "safe_map.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070064#include "scoped_thread_state_change-inl.h"
Nicolas Geoffray9c055782016-07-14 09:24:30 +000065#include "ScopedLocalRef.h"
Mathieu Chartierdc00f182016-07-14 10:10:44 -070066#include "stack_map.h"
67#include "string_reference.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080068#include "thread_list.h"
Andreas Gampe2ba88952016-04-29 17:52:07 -070069#include "type_lookup_table.h"
Nicolas Geoffray4acefd32016-10-24 13:14:58 +010070#include "vdex_file.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080071#include "verifier/method_verifier.h"
Nicolas Geoffraye70dd562016-10-30 21:03:35 +000072#include "verifier/verifier_deps.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070073#include "well_known_classes.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070074
Igor Murashkin37743352014-11-13 14:38:00 -080075#include <sys/stat.h>
76#include "cmdline.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070077
Igor Murashkin37743352014-11-13 14:38:00 -080078namespace art {
Brian Carlstrom78128a62011-09-15 17:21:19 -070079
Andreas Gampe46ee31b2016-12-14 10:11:49 -080080using android::base::StringPrintf;
81
Mathieu Chartiere401d142015-04-22 13:56:20 -070082const char* image_methods_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -080083 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -070084 "kImtConflictMethod",
Mathieu Chartier2d2621a2014-10-23 16:48:06 -070085 "kImtUnimplementedMethod",
Vladimir Markofd36f1f2016-08-03 18:49:58 +010086 "kSaveAllCalleeSavesMethod",
87 "kSaveRefsOnlyMethod",
88 "kSaveRefsAndArgsMethod",
Vladimir Marko952dbb12016-07-28 12:01:51 +010089 "kSaveEverythingMethod",
Mathieu Chartiere401d142015-04-22 13:56:20 -070090};
91
92const char* image_roots_descriptions_[] = {
Brian Carlstrom58ae9412011-10-04 00:56:06 -070093 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070094 "kClassRoots",
Vladimir Markoeca3eda2016-11-09 16:26:44 +000095 "kClassLoader",
Brian Carlstrom78128a62011-09-15 17:21:19 -070096};
97
Mathieu Chartierac8f4392015-08-27 13:54:20 -070098// Map is so that we don't allocate multiple dex files for the same OatDexFile.
99static std::map<const OatFile::OatDexFile*,
100 std::unique_ptr<const DexFile>> opened_dex_files;
101
102const DexFile* OpenDexFile(const OatFile::OatDexFile* oat_dex_file, std::string* error_msg) {
103 DCHECK(oat_dex_file != nullptr);
104 auto it = opened_dex_files.find(oat_dex_file);
105 if (it != opened_dex_files.end()) {
106 return it->second.get();
107 }
108 const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
109 opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
110 return ret;
111}
112
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800113template <typename ElfTypes>
David Srbeckybc90fd02015-04-22 19:40:27 +0100114class OatSymbolizer FINAL {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700115 public:
David Srbecky2fdd03c2016-03-10 15:32:37 +0000116 OatSymbolizer(const OatFile* oat_file, const std::string& output_name, bool no_bits) :
117 oat_file_(oat_file),
118 builder_(nullptr),
119 output_name_(output_name.empty() ? "symbolized.oat" : output_name),
120 no_bits_(no_bits) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700121 }
122
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700123 bool Symbolize() {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000124 const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800125 std::unique_ptr<const InstructionSetFeatures> features = InstructionSetFeatures::FromBitmap(
David Srbecky5d811202016-03-08 13:21:22 +0000126 isa, oat_file_->GetOatHeader().GetInstructionSetFeaturesBitmap());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000127
128 File* elf_file = OS::CreateEmptyFile(output_name_.c_str());
129 std::unique_ptr<BufferedOutputStream> output_stream(
Vladimir Marko10c13562015-11-25 14:33:36 +0000130 MakeUnique<BufferedOutputStream>(MakeUnique<FileOutputStream>(elf_file)));
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800131 builder_.reset(new ElfBuilder<ElfTypes>(isa, features.get(), output_stream.get()));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000132
133 builder_->Start();
134
135 auto* rodata = builder_->GetRoData();
136 auto* text = builder_->GetText();
137 auto* bss = builder_->GetBss();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000138
David Srbecky6d8c8f02015-10-26 10:57:09 +0000139 const uint8_t* rodata_begin = oat_file_->Begin();
140 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
David Srbecky2fdd03c2016-03-10 15:32:37 +0000141 if (no_bits_) {
142 rodata->WriteNoBitsSection(rodata_size);
143 } else {
144 rodata->Start();
145 rodata->WriteFully(rodata_begin, rodata_size);
146 rodata->End();
147 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000148
David Srbecky6d8c8f02015-10-26 10:57:09 +0000149 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
150 const size_t text_size = oat_file_->End() - text_begin;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000151 if (no_bits_) {
152 text->WriteNoBitsSection(text_size);
153 } else {
154 text->Start();
155 text->WriteFully(text_begin, text_size);
156 text->End();
157 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000158
159 if (oat_file_->BssSize() != 0) {
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000160 bss->WriteNoBitsSection(oat_file_->BssSize());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000161 }
162
Douglas Leung316a2182015-09-17 15:26:25 -0700163 if (isa == kMips || isa == kMips64) {
164 builder_->WriteMIPSabiflagsSection();
165 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000166 builder_->PrepareDynamicSection(elf_file->GetPath(),
167 rodata_size,
168 text_size,
169 oat_file_->BssSize(),
170 oat_file_->BssRootsOffset());
Vladimir Marko944da602016-02-19 12:27:55 +0000171 builder_->WriteDynamicSection();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700172
David Srbecky5d950762016-03-07 20:47:29 +0000173 Walk();
174 for (const auto& trampoline : debug::MakeTrampolineInfos(oat_file_->GetOatHeader())) {
175 method_debug_infos_.push_back(trampoline);
176 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700177
David Srbecky5d950762016-03-07 20:47:29 +0000178 debug::WriteDebugInfo(builder_.get(),
179 ArrayRef<const debug::MethodDebugInfo>(method_debug_infos_),
180 dwarf::DW_DEBUG_FRAME_FORMAT,
181 true /* write_oat_patches */);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700182
David Srbecky6d8c8f02015-10-26 10:57:09 +0000183 builder_->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700184
Vladimir Marko10c13562015-11-25 14:33:36 +0000185 return builder_->Good();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700186 }
187
David Srbecky5d950762016-03-07 20:47:29 +0000188 void Walk() {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700189 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
190 for (size_t i = 0; i < oat_dex_files.size(); i++) {
191 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700192 CHECK(oat_dex_file != nullptr);
David Srbecky5d950762016-03-07 20:47:29 +0000193 WalkOatDexFile(oat_dex_file);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700194 }
195 }
196
David Srbecky5d950762016-03-07 20:47:29 +0000197 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700198 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700199 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
200 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700201 return;
202 }
203 for (size_t class_def_index = 0;
204 class_def_index < dex_file->NumClassDefs();
205 class_def_index++) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700206 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
207 OatClassType type = oat_class.GetType();
208 switch (type) {
209 case kOatClassAllCompiled:
210 case kOatClassSomeCompiled:
David Srbecky5d950762016-03-07 20:47:29 +0000211 WalkOatClass(oat_class, *dex_file, class_def_index);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700212 break;
213
214 case kOatClassNoneCompiled:
215 case kOatClassMax:
216 // Ignore.
217 break;
218 }
219 }
220 }
221
David Srbecky5d950762016-03-07 20:47:29 +0000222 void WalkOatClass(const OatFile::OatClass& oat_class,
223 const DexFile& dex_file,
224 uint32_t class_def_index) {
225 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700226 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700227 if (class_data == nullptr) { // empty class such as a marker interface?
228 return;
229 }
230 // Note: even if this is an interface or a native class, we still have to walk it, as there
231 // might be a static initializer.
232 ClassDataItemIterator it(dex_file, class_data);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700233 uint32_t class_method_idx = 0;
David Srbecky5d950762016-03-07 20:47:29 +0000234 for (; it.HasNextStaticField(); it.Next()) { /* skip */ }
235 for (; it.HasNextInstanceField(); it.Next()) { /* skip */ }
236 for (; it.HasNextDirectMethod() || it.HasNextVirtualMethod(); it.Next()) {
237 WalkOatMethod(oat_class.GetOatMethod(class_method_idx++),
238 dex_file,
239 class_def_index,
240 it.GetMemberIndex(),
241 it.GetMethodCodeItem(),
242 it.GetMethodAccessFlags());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700243 }
244 DCHECK(!it.HasNext());
245 }
246
David Srbecky5d950762016-03-07 20:47:29 +0000247 void WalkOatMethod(const OatFile::OatMethod& oat_method,
248 const DexFile& dex_file,
249 uint32_t class_def_index,
250 uint32_t dex_method_index,
251 const DexFile::CodeItem* code_item,
252 uint32_t method_access_flags) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700253 if ((method_access_flags & kAccAbstract) != 0) {
254 // Abstract method, no code.
255 return;
256 }
David Srbecky5d950762016-03-07 20:47:29 +0000257 const OatHeader& oat_header = oat_file_->GetOatHeader();
258 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
259 if (method_header == nullptr || method_header->GetCodeSize() == 0) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700260 // No code.
261 return;
262 }
263
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100264 uint32_t entry_point = oat_method.GetCodeOffset() - oat_header.GetExecutableOffset();
265 // Clear Thumb2 bit.
266 const void* code_address = EntryPointToCodePointer(reinterpret_cast<void*>(entry_point));
267
David Srbecky5d950762016-03-07 20:47:29 +0000268 debug::MethodDebugInfo info = debug::MethodDebugInfo();
269 info.trampoline_name = nullptr;
270 info.dex_file = &dex_file;
271 info.class_def_index = class_def_index;
272 info.dex_method_index = dex_method_index;
273 info.access_flags = method_access_flags;
274 info.code_item = code_item;
275 info.isa = oat_header.GetInstructionSet();
276 info.deduped = !seen_offsets_.insert(oat_method.GetCodeOffset()).second;
277 info.is_native_debuggable = oat_header.IsNativeDebuggable();
278 info.is_optimized = method_header->IsOptimized();
279 info.is_code_address_text_relative = true;
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100280 info.code_address = reinterpret_cast<uintptr_t>(code_address);
David Srbecky5d950762016-03-07 20:47:29 +0000281 info.code_size = method_header->GetCodeSize();
282 info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
283 info.code_info = info.is_optimized ? method_header->GetOptimizedCodeInfoPtr() : nullptr;
284 info.cfi = ArrayRef<uint8_t>();
285 method_debug_infos_.push_back(info);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700286 }
287
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700288 private:
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700289 const OatFile* oat_file_;
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800290 std::unique_ptr<ElfBuilder<ElfTypes> > builder_;
David Srbecky5d950762016-03-07 20:47:29 +0000291 std::vector<debug::MethodDebugInfo> method_debug_infos_;
292 std::unordered_set<uint32_t> seen_offsets_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700293 const std::string output_name_;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000294 bool no_bits_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700295};
296
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700297class OatDumperOptions {
298 public:
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100299 OatDumperOptions(bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100300 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700301 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700302 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800303 const char* class_filter,
304 const char* method_filter,
305 bool list_classes,
306 bool list_methods,
David Brazdilc03d7b62016-03-02 12:18:03 +0000307 bool dump_header_only,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800308 const char* export_dex_location,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800309 const char* app_image,
310 const char* app_oat,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800311 uint32_t addr2instr)
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100312 : dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100313 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700314 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700315 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800316 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000317 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800318 list_classes_(list_classes),
319 list_methods_(list_methods),
David Brazdilc03d7b62016-03-02 12:18:03 +0000320 dump_header_only_(dump_header_only),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800321 export_dex_location_(export_dex_location),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800322 app_image_(app_image),
323 app_oat_(app_oat),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800324 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800325 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700326
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700327 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100328 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700329 const bool disassemble_code_;
330 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800331 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000332 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800333 const bool list_classes_;
334 const bool list_methods_;
David Brazdilc03d7b62016-03-02 12:18:03 +0000335 const bool dump_header_only_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800336 const char* const export_dex_location_;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800337 const char* const app_image_;
338 const char* const app_oat_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800339 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700340 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700341};
342
Elliott Hughese3c845c2012-02-28 17:23:01 -0800343class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700344 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100345 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000346 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800347 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700348 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800349 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800350 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
351 disassembler_(Disassembler::Create(instruction_set_,
Andreas Gampe372f3a32016-08-19 10:49:06 -0700352 new DisassemblerOptions(
353 options_.absolute_addresses_,
354 oat_file.Begin(),
355 oat_file.End(),
356 true /* can_read_literals_ */,
357 Is64BitInstructionSet(instruction_set_)
358 ? &Thread::DumpThreadOffset<PointerSize::k64>
359 : &Thread::DumpThreadOffset<PointerSize::k32>))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800360 CHECK(options_.class_loader_ != nullptr);
361 CHECK(options_.class_filter_ != nullptr);
362 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800363 AddAllOffsets();
364 }
365
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700366 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700367 delete disassembler_;
368 }
369
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800370 InstructionSet GetInstructionSet() {
371 return instruction_set_;
372 }
373
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700374 bool Dump(std::ostream& os) {
375 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800376 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700377
378 os << "MAGIC:\n";
379 os << oat_header.GetMagic() << "\n\n";
380
Jeff Hao4b07a6e2016-01-15 12:50:44 -0800381 os << "LOCATION:\n";
382 os << oat_file_.GetLocation() << "\n\n";
383
Brian Carlstromaded5f72011-10-07 17:15:04 -0700384 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800385 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700386
Elliott Hughesa72ec822012-03-05 17:12:22 -0800387 os << "INSTRUCTION SET:\n";
388 os << oat_header.GetInstructionSet() << "\n\n";
389
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700390 {
391 std::unique_ptr<const InstructionSetFeatures> features(
392 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
393 oat_header.GetInstructionSetFeaturesBitmap()));
394 os << "INSTRUCTION SET FEATURES:\n";
395 os << features->GetFeatureString() << "\n\n";
396 }
Dave Allison70202782013-10-22 17:52:19 -0700397
Brian Carlstromaded5f72011-10-07 17:15:04 -0700398 os << "DEX FILE COUNT:\n";
399 os << oat_header.GetDexFileCount() << "\n\n";
400
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800401#define DUMP_OAT_HEADER_OFFSET(label, offset) \
402 os << label " OFFSET:\n"; \
403 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800404 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800405 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
406 } \
407 os << StringPrintf("\n\n");
408
409 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
410 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
411 GetInterpreterToInterpreterBridgeOffset);
412 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
413 GetInterpreterToCompiledCodeBridgeOffset);
414 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
415 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800416 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
417 GetQuickGenericJniTrampolineOffset);
418 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
419 GetQuickImtConflictTrampolineOffset);
420 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
421 GetQuickResolutionTrampolineOffset);
422 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
423 GetQuickToInterpreterBridgeOffset);
424#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700425
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700426 os << "IMAGE PATCH DELTA:\n";
427 os << StringPrintf("%d (0x%08x)\n\n",
428 oat_header.GetImagePatchDelta(),
429 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700430
Brian Carlstrom28db0122012-10-18 16:20:41 -0700431 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
432 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
433
434 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800435 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700436
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700437 // Print the key-value store.
438 {
439 os << "KEY VALUE STORE:\n";
440 size_t index = 0;
441 const char* key;
442 const char* value;
443 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
444 os << key << " = " << value << "\n";
445 index++;
446 }
447 os << "\n";
448 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700449
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800450 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700451 os << "BEGIN:\n";
452 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700453
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700454 os << "END:\n";
455 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
456 }
457
458 os << "SIZE:\n";
459 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700460
461 os << std::flush;
462
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800463 // If set, adjust relative address to be searched
464 if (options_.addr2instr_ != 0) {
465 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
466 os << "SEARCH ADDRESS (executable offset + input):\n";
467 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
468 }
469
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700470 // Dumping the dex file overview is compact enough to do even if header only.
471 DexFileData cumulative;
472 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
473 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
474 CHECK(oat_dex_file != nullptr);
475 std::string error_msg;
476 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
477 if (dex_file == nullptr) {
478 os << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation() << "': "
479 << error_msg;
480 continue;
481 }
482 DexFileData data(*dex_file);
483 os << "Dex file data for " << dex_file->GetLocation() << "\n";
484 data.Dump(os);
485 os << "\n";
486 cumulative.Add(data);
487 }
488 os << "Cumulative dex file data\n";
489 cumulative.Dump(os);
490 os << "\n";
491
David Brazdilc03d7b62016-03-02 12:18:03 +0000492 if (!options_.dump_header_only_) {
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000493 VariableIndentationOutputStream vios(&os);
494 VdexFile::Header vdex_header = oat_file_.GetVdexFile()->GetHeader();
495 if (vdex_header.IsValid()) {
496 std::string error_msg;
497 std::vector<const DexFile*> dex_files;
498 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
499 const DexFile* dex_file = OpenDexFile(oat_dex_files_[i], &error_msg);
500 if (dex_file == nullptr) {
501 os << "Error opening dex file: " << error_msg << std::endl;
502 return false;
503 }
504 dex_files.push_back(dex_file);
505 }
506 verifier::VerifierDeps deps(dex_files, oat_file_.GetVdexFile()->GetVerifierDepsData());
507 deps.Dump(&vios);
508 } else {
509 os << "UNRECOGNIZED vdex file, magic "
510 << vdex_header.GetMagic()
511 << ", version "
512 << vdex_header.GetVersion()
513 << "\n";
514 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000515 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
516 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
517 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800518
David Brazdilc03d7b62016-03-02 12:18:03 +0000519 // If file export selected skip file analysis
520 if (options_.export_dex_location_) {
521 if (!ExportDexFile(os, *oat_dex_file)) {
522 success = false;
523 }
524 } else {
525 if (!DumpOatDexFile(os, *oat_dex_file)) {
526 success = false;
527 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800528 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700529 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700530 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000531
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800532 {
533 os << "OAT FILE STATS:\n";
534 VariableIndentationOutputStream vios(&os);
535 stats_.Dump(vios);
536 }
537
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700538 os << std::flush;
539 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700540 }
541
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800542 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700543 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
544 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800545 return 0; // Address not in oat file
546 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800547 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
548 reinterpret_cast<uintptr_t>(oat_file_.Begin());
549 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800550 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800551 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800552 return end_offset - begin_offset;
553 }
554
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800555 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700556 return oat_file_.GetOatHeader().GetInstructionSet();
557 }
558
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700559 const void* GetQuickOatCode(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800560 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
561 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700562 CHECK(oat_dex_file != nullptr);
563 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700564 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
565 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700566 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
567 << "': " << error_msg;
568 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800569 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700570 const DexFile::ClassDef* class_def =
David Sehr9aa352e2016-09-15 18:13:52 -0700571 OatDexFile::FindClassDef(*dex_file, descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700572 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700573 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100574 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800575 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100576 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800577 }
578 }
579 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700580 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800581 }
582
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800583 struct Stats {
584 enum ByteKind {
585 kByteKindCode,
586 kByteKindQuickMethodHeader,
587 kByteKindCodeInfoLocationCatalog,
588 kByteKindCodeInfoDexRegisterMap,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800589 kByteKindCodeInfoEncoding,
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800590 kByteKindCodeInfoInvokeInfo,
David Srbecky45aa5982016-03-18 02:15:09 +0000591 kByteKindCodeInfoStackMasks,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800592 kByteKindCodeInfoRegisterMasks,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800593 kByteKindStackMapNativePc,
594 kByteKindStackMapDexPc,
595 kByteKindStackMapDexRegisterMap,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800596 kByteKindStackMapInlineInfoIndex,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800597 kByteKindStackMapRegisterMaskIndex,
David Srbecky45aa5982016-03-18 02:15:09 +0000598 kByteKindStackMapStackMaskIndex,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700599 kByteKindInlineInfoMethodIndexIdx,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800600 kByteKindInlineInfoDexPc,
601 kByteKindInlineInfoExtraData,
602 kByteKindInlineInfoDexRegisterMap,
603 kByteKindInlineInfoIsLast,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800604 kByteKindCount,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800605 // Special ranges for std::accumulate convenience.
606 kByteKindStackMapFirst = kByteKindStackMapNativePc,
David Srbecky45aa5982016-03-18 02:15:09 +0000607 kByteKindStackMapLast = kByteKindStackMapStackMaskIndex,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700608 kByteKindInlineInfoFirst = kByteKindInlineInfoMethodIndexIdx,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800609 kByteKindInlineInfoLast = kByteKindInlineInfoIsLast,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800610 };
611 int64_t bits[kByteKindCount] = {};
612 // Since code has deduplication, seen tracks already seen pointers to avoid double counting
613 // deduplicated code and tables.
614 std::unordered_set<const void*> seen;
615
616 // Returns true if it was newly added.
617 bool AddBitsIfUnique(ByteKind kind, int64_t count, const void* address) {
618 if (seen.insert(address).second == true) {
619 // True means the address was not already in the set.
620 AddBits(kind, count);
621 return true;
622 }
623 return false;
624 }
625
626 void AddBits(ByteKind kind, int64_t count) {
627 bits[kind] += count;
628 }
629
630 void Dump(VariableIndentationOutputStream& os) {
631 const int64_t sum = std::accumulate(bits, bits + kByteKindCount, 0u);
632 os.Stream() << "Dumping cumulative use of " << sum / kBitsPerByte << " accounted bytes\n";
633 if (sum > 0) {
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800634 Dump(os, "Code ", bits[kByteKindCode], sum);
635 Dump(os, "QuickMethodHeader ", bits[kByteKindQuickMethodHeader], sum);
636 Dump(os, "CodeInfoEncoding ", bits[kByteKindCodeInfoEncoding], sum);
637 Dump(os, "CodeInfoLocationCatalog ", bits[kByteKindCodeInfoLocationCatalog], sum);
638 Dump(os, "CodeInfoDexRegisterMap ", bits[kByteKindCodeInfoDexRegisterMap], sum);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800639 Dump(os, "CodeInfoStackMasks ", bits[kByteKindCodeInfoStackMasks], sum);
640 Dump(os, "CodeInfoRegisterMasks ", bits[kByteKindCodeInfoRegisterMasks], sum);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800641 Dump(os, "CodeInfoInvokeInfo ", bits[kByteKindCodeInfoInvokeInfo], sum);
Mathieu Chartier1e083792017-02-08 13:30:04 -0800642 // Stack map section.
643 const int64_t stack_map_bits = std::accumulate(bits + kByteKindStackMapFirst,
644 bits + kByteKindStackMapLast + 1,
645 0u);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800646 Dump(os, "CodeInfoStackMap ", stack_map_bits, sum);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800647 {
648 ScopedIndentation indent1(&os);
649 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800650 "StackMapNativePc ",
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800651 bits[kByteKindStackMapNativePc],
652 stack_map_bits,
653 "stack map");
654 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800655 "StackMapDexPcEncoding ",
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800656 bits[kByteKindStackMapDexPc],
657 stack_map_bits,
658 "stack map");
659 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800660 "StackMapDexRegisterMap ",
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800661 bits[kByteKindStackMapDexRegisterMap],
662 stack_map_bits,
663 "stack map");
664 Dump(os,
Mathieu Chartier697dc662017-02-08 16:56:48 -0800665 "StackMapInlineInfoIndex ",
Mathieu Chartier1e083792017-02-08 13:30:04 -0800666 bits[kByteKindStackMapInlineInfoIndex],
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800667 stack_map_bits,
668 "stack map");
669 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800670 "StackMapRegisterMaskIndex ",
671 bits[kByteKindStackMapRegisterMaskIndex],
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800672 stack_map_bits,
673 "stack map");
674 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800675 "StackMapStackMaskIndex ",
David Srbecky45aa5982016-03-18 02:15:09 +0000676 bits[kByteKindStackMapStackMaskIndex],
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800677 stack_map_bits,
678 "stack map");
679 }
Mathieu Chartier1e083792017-02-08 13:30:04 -0800680 // Inline info section.
681 const int64_t inline_info_bits = std::accumulate(bits + kByteKindInlineInfoFirst,
682 bits + kByteKindInlineInfoLast + 1,
683 0u);
684 Dump(os, "CodeInfoInlineInfo ", inline_info_bits, sum);
685 {
686 ScopedIndentation indent1(&os);
687 Dump(os,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700688 "InlineInfoMethodIndexIdx ",
689 bits[kByteKindInlineInfoMethodIndexIdx],
Mathieu Chartier1e083792017-02-08 13:30:04 -0800690 inline_info_bits,
691 "inline info");
692 Dump(os,
693 "InlineInfoDexPc ",
694 bits[kByteKindStackMapDexPc],
695 inline_info_bits,
696 "inline info");
697 Dump(os,
698 "InlineInfoExtraData ",
699 bits[kByteKindInlineInfoExtraData],
700 inline_info_bits,
701 "inline info");
702 Dump(os,
703 "InlineInfoDexRegisterMap ",
704 bits[kByteKindInlineInfoDexRegisterMap],
705 inline_info_bits,
706 "inline info");
707 Dump(os,
708 "InlineInfoIsLast ",
709 bits[kByteKindInlineInfoIsLast],
710 inline_info_bits,
711 "inline info");
712 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800713 }
714 os.Stream() << "\n" << std::flush;
715 }
716
717 private:
718 void Dump(VariableIndentationOutputStream& os,
719 const char* name,
720 int64_t size,
721 int64_t total,
722 const char* sum_of = "total") {
723 const double percent = (static_cast<double>(size) / static_cast<double>(total)) * 100;
724 os.Stream() << StringPrintf("%s = %8" PRId64 " (%2.0f%% of %s)\n",
725 name,
726 size / kBitsPerByte,
727 percent,
728 sum_of);
729 }
730 };
731
Brian Carlstromaded5f72011-10-07 17:15:04 -0700732 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800733 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800734 // We don't know the length of the code for each method, but we need to know where to stop
735 // when disassembling. What we do know is that a region of code will be followed by some other
736 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
737 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800738 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
739 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700740 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700741 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700742 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
743 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700744 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
745 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800746 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800747 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800748 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800749 for (size_t class_def_index = 0;
750 class_def_index < dex_file->NumClassDefs();
751 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800752 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100753 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700754 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700755 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800756 ClassDataItemIterator it(*dex_file, class_data);
757 SkipAllFields(it);
758 uint32_t class_method_index = 0;
759 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100760 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800761 it.Next();
762 }
763 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100764 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800765 it.Next();
766 }
767 }
768 }
769 }
770
771 // If the last thing in the file is code for a method, there won't be an offset for the "next"
772 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
773 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800774 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800775 }
776
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700777 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
778 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
779 }
780
Elliott Hughese3c845c2012-02-28 17:23:01 -0800781 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800782 uint32_t code_offset = oat_method.GetCodeOffset();
783 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
784 code_offset &= ~0x1;
785 }
786 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800787 offsets_.insert(oat_method.GetVmapTableOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800788 }
789
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700790 // Dex file data, may be for multiple different dex files.
791 class DexFileData {
792 public:
793 DexFileData() {}
794
795 explicit DexFileData(const DexFile& dex_file)
796 : num_string_ids_(dex_file.NumStringIds()),
797 num_method_ids_(dex_file.NumMethodIds()),
798 num_field_ids_(dex_file.NumFieldIds()),
799 num_type_ids_(dex_file.NumTypeIds()),
800 num_class_defs_(dex_file.NumClassDefs()) {
801 for (size_t class_def_index = 0; class_def_index < num_class_defs_; ++class_def_index) {
802 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
803 WalkClass(dex_file, class_def);
804 }
805 }
806
807 void Add(const DexFileData& other) {
808 AddAll(unique_string_ids_from_code_, other.unique_string_ids_from_code_);
809 num_string_ids_from_code_ += other.num_string_ids_from_code_;
810 AddAll(dex_code_item_ptrs_, other.dex_code_item_ptrs_);
811 dex_code_bytes_ += other.dex_code_bytes_;
812 num_string_ids_ += other.num_string_ids_;
813 num_method_ids_ += other.num_method_ids_;
814 num_field_ids_ += other.num_field_ids_;
815 num_type_ids_ += other.num_type_ids_;
816 num_class_defs_ += other.num_class_defs_;
817 }
818
819 void Dump(std::ostream& os) {
820 os << "Num string ids: " << num_string_ids_ << "\n";
821 os << "Num method ids: " << num_method_ids_ << "\n";
822 os << "Num field ids: " << num_field_ids_ << "\n";
823 os << "Num type ids: " << num_type_ids_ << "\n";
824 os << "Num class defs: " << num_class_defs_ << "\n";
825 os << "Unique strings loaded from dex code: " << unique_string_ids_from_code_.size() << "\n";
826 os << "Total strings loaded from dex code: " << num_string_ids_from_code_ << "\n";
827 os << "Number of unique dex code items: " << dex_code_item_ptrs_.size() << "\n";
828 os << "Total number of dex code bytes: " << dex_code_bytes_ << "\n";
829 }
830
831 private:
Andreas Gampe9186ced2016-12-12 14:28:21 -0800832 // All of the elements from one container to another.
833 template <typename Dest, typename Src>
834 static void AddAll(Dest& dest, const Src& src) {
835 dest.insert(src.begin(), src.end());
836 }
837
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700838 void WalkClass(const DexFile& dex_file, const DexFile::ClassDef& class_def) {
839 const uint8_t* class_data = dex_file.GetClassData(class_def);
840 if (class_data == nullptr) { // empty class such as a marker interface?
841 return;
842 }
843 ClassDataItemIterator it(dex_file, class_data);
844 SkipAllFields(it);
845 while (it.HasNextDirectMethod()) {
846 WalkCodeItem(dex_file, it.GetMethodCodeItem());
847 it.Next();
848 }
849 while (it.HasNextVirtualMethod()) {
850 WalkCodeItem(dex_file, it.GetMethodCodeItem());
851 it.Next();
852 }
853 DCHECK(!it.HasNext());
854 }
855
856 void WalkCodeItem(const DexFile& dex_file, const DexFile::CodeItem* code_item) {
857 if (code_item == nullptr) {
858 return;
859 }
860 const size_t code_item_size = code_item->insns_size_in_code_units_;
861 const uint16_t* code_ptr = code_item->insns_;
862 const uint16_t* code_end = code_item->insns_ + code_item_size;
863
864 // If we inserted a new dex code item pointer, add to total code bytes.
865 if (dex_code_item_ptrs_.insert(code_ptr).second) {
866 dex_code_bytes_ += code_item_size * sizeof(code_ptr[0]);
867 }
868
869 while (code_ptr < code_end) {
870 const Instruction* inst = Instruction::At(code_ptr);
871 switch (inst->Opcode()) {
872 case Instruction::CONST_STRING: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800873 const dex::StringIndex string_index(inst->VRegB_21c());
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700874 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
875 ++num_string_ids_from_code_;
876 break;
877 }
878 case Instruction::CONST_STRING_JUMBO: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800879 const dex::StringIndex string_index(inst->VRegB_31c());
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700880 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
881 ++num_string_ids_from_code_;
882 break;
883 }
884 default:
885 break;
886 }
887
888 code_ptr += inst->SizeInCodeUnits();
889 }
890 }
891
892 // Unique string ids loaded from dex code.
893 std::set<StringReference, StringReferenceComparator> unique_string_ids_from_code_;
894
895 // Total string ids loaded from dex code.
896 size_t num_string_ids_from_code_ = 0;
897
898 // Unique code pointers.
899 std::set<const void*> dex_code_item_ptrs_;
900
901 // Total "unique" dex code bytes.
902 size_t dex_code_bytes_ = 0;
903
904 // Other dex ids.
905 size_t num_string_ids_ = 0;
906 size_t num_method_ids_ = 0;
907 size_t num_field_ids_ = 0;
908 size_t num_type_ids_ = 0;
909 size_t num_class_defs_ = 0;
910 };
911
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700912 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
913 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800914 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700915 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800916 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800917 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700918
Andreas Gampe2ba88952016-04-29 17:52:07 -0700919 const uint8_t* const oat_file_begin = oat_dex_file.GetOatFile()->Begin();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100920 const uint8_t* const vdex_file_begin = oat_dex_file.GetOatFile()->DexBegin();
921
922 // Print data range of the dex file embedded inside the corresponding vdex file.
Andreas Gampe2ba88952016-04-29 17:52:07 -0700923 const uint8_t* const dex_file_pointer = oat_dex_file.GetDexFilePointer();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100924 uint32_t dex_offset = dchecked_integral_cast<uint32_t>(dex_file_pointer - vdex_file_begin);
Andreas Gampe2ba88952016-04-29 17:52:07 -0700925 os << StringPrintf("dex-file: 0x%08x..0x%08x\n",
926 dex_offset,
927 dchecked_integral_cast<uint32_t>(dex_offset + oat_dex_file.FileSize() - 1));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700928
Andreas Gampe2ba88952016-04-29 17:52:07 -0700929 // Create the dex file early. A lot of print-out things depend on it.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700930 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700931 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
932 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700933 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700934 os << std::flush;
935 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700936 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100937
Andreas Gampe2ba88952016-04-29 17:52:07 -0700938 // Print lookup table, if it exists.
939 if (oat_dex_file.GetLookupTableData() != nullptr) {
940 uint32_t table_offset = dchecked_integral_cast<uint32_t>(
941 oat_dex_file.GetLookupTableData() - oat_file_begin);
David Sehr9aa352e2016-09-15 18:13:52 -0700942 uint32_t table_size = TypeLookupTable::RawDataLength(dex_file->NumClassDefs());
Andreas Gampe2ba88952016-04-29 17:52:07 -0700943 os << StringPrintf("type-table: 0x%08x..0x%08x\n",
944 table_offset,
945 table_offset + table_size - 1);
946 }
947
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100948 VariableIndentationOutputStream vios(&os);
949 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800950 for (size_t class_def_index = 0;
951 class_def_index < dex_file->NumClassDefs();
952 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700953 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
954 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800955
956 // TODO: Support regex
957 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
958 continue;
959 }
960
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700961 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100962 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700963 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
Andreas Gampea5b09a62016-11-17 15:21:22 -0800964 class_def_index, descriptor, oat_class_offset, class_def.class_idx_.index_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100965 << " (" << oat_class.GetStatus() << ")"
966 << " (" << oat_class.GetType() << ")\n";
967 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700968 if (options_.list_classes_) {
969 continue;
970 }
971 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700972 success = false;
973 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800974 if (stop_analysis) {
975 os << std::flush;
976 return success;
977 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700978 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700979 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700980 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700981 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700982 }
983
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800984 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
985 std::string error_msg;
986 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
987
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700988 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800989 if (dex_file == nullptr) {
990 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
991 return false;
992 }
993 size_t fsize = oat_dex_file.FileSize();
994
995 // Some quick checks just in case
996 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
997 os << "Invalid dex file\n";
998 return false;
999 }
1000
1001 // Verify output directory exists
1002 if (!OS::DirectoryExists(options_.export_dex_location_)) {
1003 // TODO: Extend OS::DirectoryExists if symlink support is required
1004 os << options_.export_dex_location_ << " output directory not found or symlink\n";
1005 return false;
1006 }
1007
1008 // Beautify path names
1009 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
1010 return false;
1011 }
1012
1013 std::string dex_orig_name;
1014 size_t dex_orig_pos = dex_file_location.rfind('/');
1015 if (dex_orig_pos == std::string::npos)
1016 dex_orig_name = dex_file_location;
1017 else
1018 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
1019
1020 // A more elegant approach to efficiently name user installed apps is welcome
Andreas Gampef812d8c2017-02-17 10:19:44 -08001021 if (dex_orig_name.size() == 8 &&
1022 dex_orig_name.compare("base.apk") == 0 &&
1023 dex_orig_pos != std::string::npos) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001024 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
1025 size_t apk_orig_pos = dex_file_location.rfind('/');
1026 if (apk_orig_pos != std::string::npos) {
1027 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
1028 }
1029 }
1030
1031 std::string out_dex_path(options_.export_dex_location_);
1032 if (out_dex_path.back() != '/') {
1033 out_dex_path.append("/");
1034 }
1035 out_dex_path.append(dex_orig_name);
1036 out_dex_path.append("_export.dex");
1037 if (out_dex_path.length() > PATH_MAX) {
1038 return false;
1039 }
1040
1041 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
1042 if (file.get() == nullptr) {
1043 os << "Failed to open output dex file " << out_dex_path;
1044 return false;
1045 }
1046
1047 if (!file->WriteFully(dex_file->Begin(), fsize)) {
1048 os << "Failed to write dex file";
1049 file->Erase();
1050 return false;
1051 }
1052
1053 if (file->FlushCloseOrErase() != 0) {
1054 os << "Flush and close failed";
1055 return false;
1056 }
1057
1058 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
1059 os << std::flush;
1060
1061 return true;
1062 }
1063
Elliott Hughese3c845c2012-02-28 17:23:01 -08001064 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -07001065 while (it.HasNextStaticField()) {
1066 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001067 }
Ian Rogers0571d352011-11-03 19:51:38 -07001068 while (it.HasNextInstanceField()) {
1069 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001070 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001071 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001072
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001073 bool DumpOatClass(VariableIndentationOutputStream* vios,
1074 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001075 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001076 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001077 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -07001078 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001079 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001080 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001081 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -08001082 }
1083 ClassDataItemIterator it(dex_file, class_data);
1084 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001085 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001086 while (it.HasNextDirectMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001087 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001088 it.GetMemberIndex(), it.GetMethodCodeItem(),
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001089 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001090 success = false;
1091 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001092 if (addr_found) {
1093 *stop_analysis = true;
1094 return success;
1095 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001096 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -07001097 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001098 }
Ian Rogers0571d352011-11-03 19:51:38 -07001099 while (it.HasNextVirtualMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001100 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001101 it.GetMemberIndex(), it.GetMethodCodeItem(),
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001102 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001103 success = false;
1104 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001105 if (addr_found) {
1106 *stop_analysis = true;
1107 return success;
1108 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001109 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -07001110 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001111 }
Ian Rogers0571d352011-11-03 19:51:38 -07001112 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001113 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001114 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001115 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001116
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001117 static constexpr uint32_t kPrologueBytes = 16;
1118
1119 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
1120 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
1121
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001122 bool DumpOatMethod(VariableIndentationOutputStream* vios,
1123 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -07001124 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001125 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001126 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001127 uint32_t method_access_flags, bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001128 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001129
1130 // TODO: Support regex
1131 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
1132 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00001133 return success;
1134 }
1135
David Sehr709b0702016-10-13 09:12:37 -07001136 std::string pretty_method = dex_file.PrettyMethod(dex_method_idx, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001137 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
1138 class_method_index, pretty_method.c_str(),
1139 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001140 if (options_.list_methods_) return success;
1141
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001142 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
1143 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
1144 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
1145 uint32_t code_offset = oat_method.GetCodeOffset();
1146 uint32_t code_size = oat_method.GetQuickCodeSize();
1147 if (resolved_addr2instr_ != 0) {
1148 if (resolved_addr2instr_ > code_offset + code_size) {
1149 return success;
1150 } else {
1151 *addr_found = true; // stop analyzing file at next iteration
1152 }
1153 }
1154
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001155 // Everything below is indented at least once.
1156 ScopedIndentation indent1(vios);
1157
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001158 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001159 vios->Stream() << "DEX CODE:\n";
1160 ScopedIndentation indent2(vios);
1161 DumpDexCode(vios->Stream(), dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -07001162 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001163
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001164 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001165 std::unique_ptr<verifier::MethodVerifier> verifier;
1166 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001167 // We need to have the handle scope stay live until after the verifier since the verifier has
1168 // a handle to the dex cache from hs.
1169 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001170 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
1171 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001172 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001173 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001174 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -07001175 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001176 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001177 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001178 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001179 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001180 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001181 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001182 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001183 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001184 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
1185 oat_method_offsets_offset, oat_file_.Size());
1186 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001187 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001188 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001189 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001190
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001191 ScopedIndentation indent2(vios);
1192 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001193 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
1194 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001195 vios->Stream() << StringPrintf("WARNING: "
1196 "code offset 0x%08x is past end of file 0x%08zx.\n",
1197 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001198 success = false;
1199 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001200 vios->Stream() << "\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001201 }
1202 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001203 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001204 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
1205 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001206 stats_.AddBitsIfUnique(Stats::kByteKindQuickMethodHeader,
1207 sizeof(*method_header) * kBitsPerByte,
1208 method_header);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001209 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001210 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001211 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001212 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001213 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001214 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001215 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
1216 method_header_offset, oat_file_.Size());
1217 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001218 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001219 return false;
1220 }
1221
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001222 ScopedIndentation indent2(vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001223 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001224 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001225 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001226 }
Mingyao Yang063fc772016-08-02 11:02:54 -07001227 uint32_t vmap_table_offset = method_header ==
1228 nullptr ? 0 : method_header->GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001229 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001230
1231 size_t vmap_table_offset_limit =
1232 (kIsVdexEnabled && IsMethodGeneratedByDexToDexCompiler(oat_method, code_item))
1233 ? oat_file_.GetVdexFile()->Size()
1234 : method_header->GetCode() - oat_file_.Begin();
1235 if (vmap_table_offset >= vmap_table_offset_limit) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001236 vios->Stream() << StringPrintf("WARNING: "
1237 "vmap table offset 0x%08x is past end of file 0x%08zx. "
1238 "vmap table offset was loaded from offset 0x%08x.\n",
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001239 vmap_table_offset,
1240 vmap_table_offset_limit,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001241 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001242 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001243 } else if (options_.dump_vmap_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001244 DumpVmapData(vios, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001245 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001246 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001247 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001248 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001249
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001250 ScopedIndentation indent2(vios);
1251 vios->Stream()
1252 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
1253 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
1254 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
1255 vios->Stream() << "\n";
1256 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
1257 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
1258 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001259 }
1260 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001261 // Based on spill masks from QuickMethodFrameInfo so placed
1262 // after it is dumped, but useful for understanding quick
1263 // code, so dumped here.
1264 ScopedIndentation indent2(vios);
1265 DumpVregLocations(vios->Stream(), oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001266 }
1267 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001268 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001269 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
1270 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001271 ScopedIndentation indent2(vios);
1272 vios->Stream() << StringPrintf("WARNING: "
1273 "code size offset 0x%08x is past end of file 0x%08zx.",
1274 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001275 success = false;
1276 } else {
1277 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001278 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1279 uint64_t aligned_code_end = aligned_code_begin + code_size;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001280 stats_.AddBitsIfUnique(Stats::kByteKindCode, code_size * kBitsPerByte, code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001281
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001282 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001283 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001284 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001285 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
1286 code_offset,
1287 code_size_offset,
1288 code_size,
1289 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001290
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001291 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001292 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001293 vios->Stream() << StringPrintf("WARNING: "
1294 "start of code at 0x%08x is past end of file 0x%08zx.",
1295 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001296 success = false;
1297 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001298 vios->Stream() << StringPrintf(
1299 "WARNING: "
1300 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
1301 "code size is 0x%08x loaded from offset 0x%08x.\n",
1302 aligned_code_end, oat_file_.Size(),
1303 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001304 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001305 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001306 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001307 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001308 }
1309 }
1310 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001311 vios->Stream() << StringPrintf(
1312 "WARNING: "
1313 "code size %d is bigger than max expected threshold of %d. "
1314 "code size is 0x%08x loaded from offset 0x%08x.\n",
1315 code_size, kMaxCodeSize,
1316 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001317 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001318 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001319 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001320 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001321 }
1322 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001323 } else if (options_.disassemble_code_) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001324 DumpCode(vios, oat_method, code_item, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001325 }
1326 }
1327 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001328 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001329 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001330 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001331
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001332 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1333 if (spill_mask == 0) {
1334 return;
1335 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001336 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001337 for (size_t i = 0; i < 32; i++) {
1338 if ((spill_mask & (1 << i)) != 0) {
1339 if (is_float) {
1340 os << "fr" << i;
1341 } else {
1342 os << "r" << i;
1343 }
1344 spill_mask ^= 1 << i; // clear bit
1345 if (spill_mask != 0) {
1346 os << ", ";
1347 } else {
1348 break;
1349 }
1350 }
1351 }
1352 os << ")";
1353 }
1354
Roland Levillain442b46a2015-02-18 16:54:21 +00001355 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001356 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001357 const OatFile::OatMethod& oat_method,
1358 const DexFile::CodeItem* code_item) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001359 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1360 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001361 const void* raw_code_info = oat_method.GetVmapTable();
1362 if (raw_code_info != nullptr) {
1363 CodeInfo code_info(raw_code_info);
1364 DCHECK(code_item != nullptr);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001365 ScopedIndentation indent1(vios);
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001366 MethodInfo method_info = oat_method.GetOatQuickMethodHeader()->GetOptimizedMethodInfo();
1367 DumpCodeInfo(vios, code_info, oat_method, *code_item, method_info);
Roland Levillain442b46a2015-02-18 16:54:21 +00001368 }
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001369 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
1370 // We don't encode the size in the table, so just emit that we have quickened
1371 // information.
1372 ScopedIndentation indent(vios);
1373 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001374 } else {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001375 // Otherwise, there is nothing to display.
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001376 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001377 }
1378
1379 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001380 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001381 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001382 const OatFile::OatMethod& oat_method,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001383 const DexFile::CodeItem& code_item,
1384 const MethodInfo& method_info) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001385 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001386 oat_method.GetCodeOffset(),
1387 code_item.registers_size_,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001388 options_.dump_code_info_stack_maps_,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001389 instruction_set_,
1390 method_info);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001391 }
1392
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001393 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1394 const DexFile::CodeItem* code_item) {
1395 if (code_item != nullptr) {
1396 size_t num_locals_ins = code_item->registers_size_;
1397 size_t num_ins = code_item->ins_size_;
1398 size_t num_locals = num_locals_ins - num_ins;
1399 size_t num_outs = code_item->outs_size_;
1400
1401 os << "vr_stack_locations:";
1402 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1403 // For readability, delimit the different kinds of VRs.
1404 if (reg == num_locals_ins) {
1405 os << "\n\tmethod*:";
1406 } else if (reg == num_locals && num_ins > 0) {
1407 os << "\n\tins:";
1408 } else if (reg == 0 && num_locals > 0) {
1409 os << "\n\tlocals:";
1410 }
1411
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001412 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1413 code_item,
1414 oat_method.GetCoreSpillMask(),
1415 oat_method.GetFpSpillMask(),
1416 oat_method.GetFrameSizeInBytes(),
1417 reg,
1418 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001419 os << " v" << reg << "[sp + #" << offset << "]";
1420 }
1421
1422 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1423 if (out_reg == 0) {
1424 os << "\n\touts:";
1425 }
1426
1427 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1428 os << " v" << out_reg << "[sp + #" << offset << "]";
1429 }
1430
1431 os << "\n";
1432 }
1433 }
1434
Ian Rogersb23a7722012-10-09 16:54:26 -07001435 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001436 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001437 size_t i = 0;
1438 while (i < code_item->insns_size_in_code_units_) {
1439 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001440 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1441 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001442 i += instruction->SizeInCodeUnits();
1443 }
1444 }
1445 }
1446
Roland Levillainf2650d12015-05-28 14:53:28 +01001447 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1448 // the optimizing compiler?
1449 static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1450 const DexFile::CodeItem* code_item) {
1451 // If the native GC map is null and the Dex `code_item` is not
1452 // null, then this method has been compiled with the optimizing
1453 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001454 return oat_method.GetQuickCode() != nullptr &&
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001455 oat_method.GetVmapTable() != nullptr &&
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001456 code_item != nullptr;
1457 }
1458
1459 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1460 // the dextodex compiler?
1461 static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
1462 const DexFile::CodeItem* code_item) {
1463 // If the quick code is null, the Dex `code_item` is not
1464 // null, and the vmap table is not null, then this method has been compiled
1465 // with the dextodex compiler.
1466 return oat_method.GetQuickCode() == nullptr &&
1467 oat_method.GetVmapTable() != nullptr &&
1468 code_item != nullptr;
Roland Levillainf2650d12015-05-28 14:53:28 +01001469 }
1470
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001471 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001472 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001473 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001474 const DexFile* dex_file,
1475 const DexFile::ClassDef& class_def,
1476 const DexFile::CodeItem* code_item,
1477 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001478 if ((method_access_flags & kAccNative) == 0) {
1479 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001480 Runtime* const runtime = Runtime::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001481 Handle<mirror::DexCache> dex_cache(
Mathieu Chartierf284d442016-06-02 11:48:30 -07001482 hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file, nullptr)));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001483 CHECK(dex_cache != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001484 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001485 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001486 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
David Brazdil15fc7292016-09-02 14:13:18 +01001487 class_def, code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001488 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001489
1490 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001491 }
1492
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001493 // The StackMapsHelper provides the stack maps in the native PC order.
1494 // For identical native PCs, the order from the CodeInfo is preserved.
1495 class StackMapsHelper {
1496 public:
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001497 explicit StackMapsHelper(const uint8_t* raw_code_info, InstructionSet instruction_set)
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001498 : code_info_(raw_code_info),
1499 encoding_(code_info_.ExtractEncoding()),
1500 number_of_stack_maps_(code_info_.GetNumberOfStackMaps(encoding_)),
1501 indexes_(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001502 offset_(static_cast<uint32_t>(-1)),
1503 stack_map_index_(0u),
1504 instruction_set_(instruction_set) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001505 if (number_of_stack_maps_ != 0u) {
1506 // Check if native PCs are ordered.
1507 bool ordered = true;
1508 StackMap last = code_info_.GetStackMapAt(0u, encoding_);
1509 for (size_t i = 1; i != number_of_stack_maps_; ++i) {
1510 StackMap current = code_info_.GetStackMapAt(i, encoding_);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001511 if (last.GetNativePcOffset(encoding_.stack_map.encoding, instruction_set) >
1512 current.GetNativePcOffset(encoding_.stack_map.encoding, instruction_set)) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001513 ordered = false;
1514 break;
1515 }
1516 last = current;
1517 }
1518 if (!ordered) {
1519 // Create indirection indexes for access in native PC order. We do not optimize
1520 // for the fact that there can currently be only two separately ordered ranges,
1521 // namely normal stack maps and catch-point stack maps.
1522 indexes_.resize(number_of_stack_maps_);
1523 std::iota(indexes_.begin(), indexes_.end(), 0u);
1524 std::sort(indexes_.begin(),
1525 indexes_.end(),
1526 [this](size_t lhs, size_t rhs) {
1527 StackMap left = code_info_.GetStackMapAt(lhs, encoding_);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001528 uint32_t left_pc = left.GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001529 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001530 StackMap right = code_info_.GetStackMapAt(rhs, encoding_);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001531 uint32_t right_pc = right.GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001532 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001533 // If the PCs are the same, compare indexes to preserve the original order.
1534 return (left_pc < right_pc) || (left_pc == right_pc && lhs < rhs);
1535 });
1536 }
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001537 offset_ = GetStackMapAt(0).GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001538 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001539 }
1540 }
1541
1542 const CodeInfo& GetCodeInfo() const {
1543 return code_info_;
1544 }
1545
1546 const CodeInfoEncoding& GetEncoding() const {
1547 return encoding_;
1548 }
1549
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001550 uint32_t GetOffset() const {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001551 return offset_;
1552 }
1553
1554 StackMap GetStackMap() const {
1555 return GetStackMapAt(stack_map_index_);
1556 }
1557
1558 void Next() {
1559 ++stack_map_index_;
1560 offset_ = (stack_map_index_ == number_of_stack_maps_)
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001561 ? static_cast<uint32_t>(-1)
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001562 : GetStackMapAt(stack_map_index_).GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001563 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001564 }
1565
1566 private:
1567 StackMap GetStackMapAt(size_t i) const {
1568 if (!indexes_.empty()) {
1569 i = indexes_[i];
1570 }
1571 DCHECK_LT(i, number_of_stack_maps_);
1572 return code_info_.GetStackMapAt(i, encoding_);
1573 }
1574
1575 const CodeInfo code_info_;
1576 const CodeInfoEncoding encoding_;
1577 const size_t number_of_stack_maps_;
1578 dchecked_vector<size_t> indexes_; // Used if stack map native PCs are not ordered.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001579 uint32_t offset_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001580 size_t stack_map_index_;
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001581 const InstructionSet instruction_set_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001582 };
1583
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001584 void DumpCode(VariableIndentationOutputStream* vios,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001585 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1586 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001587 const void* quick_code = oat_method.GetQuickCode();
1588
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001589 if (code_size == 0) {
1590 code_size = oat_method.GetQuickCodeSize();
1591 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001592 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001593 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001594 return;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001595 } else if (!bad_input && IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1596 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001597 StackMapsHelper helper(oat_method.GetVmapTable(), instruction_set_);
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001598 MethodInfo method_info(oat_method.GetOatQuickMethodHeader()->GetOptimizedMethodInfo());
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001599 {
1600 CodeInfoEncoding encoding(helper.GetEncoding());
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001601 StackMapEncoding stack_map_encoding(encoding.stack_map.encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001602 const size_t num_stack_maps = encoding.stack_map.num_entries;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001603 if (stats_.AddBitsIfUnique(Stats::kByteKindCodeInfoEncoding,
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001604 encoding.HeaderSize() * kBitsPerByte,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001605 oat_method.GetVmapTable())) {
Mathieu Chartier1e083792017-02-08 13:30:04 -08001606 // Stack maps
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001607 stats_.AddBits(
1608 Stats::kByteKindStackMapNativePc,
1609 stack_map_encoding.GetNativePcEncoding().BitSize() * num_stack_maps);
1610 stats_.AddBits(
1611 Stats::kByteKindStackMapDexPc,
1612 stack_map_encoding.GetDexPcEncoding().BitSize() * num_stack_maps);
1613 stats_.AddBits(
1614 Stats::kByteKindStackMapDexRegisterMap,
1615 stack_map_encoding.GetDexRegisterMapEncoding().BitSize() * num_stack_maps);
1616 stats_.AddBits(
Mathieu Chartier1e083792017-02-08 13:30:04 -08001617 Stats::kByteKindStackMapInlineInfoIndex,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001618 stack_map_encoding.GetInlineInfoEncoding().BitSize() * num_stack_maps);
1619 stats_.AddBits(
Mathieu Chartier1a20b682017-01-31 14:25:16 -08001620 Stats::kByteKindStackMapRegisterMaskIndex,
1621 stack_map_encoding.GetRegisterMaskIndexEncoding().BitSize() * num_stack_maps);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001622 stats_.AddBits(
David Srbecky45aa5982016-03-18 02:15:09 +00001623 Stats::kByteKindStackMapStackMaskIndex,
1624 stack_map_encoding.GetStackMaskIndexEncoding().BitSize() * num_stack_maps);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001625
1626 // Stack masks
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001627 stats_.AddBits(
David Srbecky45aa5982016-03-18 02:15:09 +00001628 Stats::kByteKindCodeInfoStackMasks,
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001629 encoding.stack_mask.encoding.BitSize() * encoding.stack_mask.num_entries);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001630
1631 // Register masks
Mathieu Chartier1a20b682017-01-31 14:25:16 -08001632 stats_.AddBits(
1633 Stats::kByteKindCodeInfoRegisterMasks,
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001634 encoding.register_mask.encoding.BitSize() * encoding.register_mask.num_entries);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001635
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001636 // Invoke infos
1637 if (encoding.invoke_info.num_entries > 0u) {
1638 stats_.AddBits(
1639 Stats::kByteKindCodeInfoInvokeInfo,
1640 encoding.invoke_info.encoding.BitSize() * encoding.invoke_info.num_entries);
1641 }
1642
Mathieu Chartier1e083792017-02-08 13:30:04 -08001643 // Location catalog
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001644 const size_t location_catalog_bytes =
1645 helper.GetCodeInfo().GetDexRegisterLocationCatalogSize(encoding);
1646 stats_.AddBits(Stats::kByteKindCodeInfoLocationCatalog,
1647 kBitsPerByte * location_catalog_bytes);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001648 // Dex register bytes.
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001649 const size_t dex_register_bytes =
1650 helper.GetCodeInfo().GetDexRegisterMapsSize(encoding, code_item->registers_size_);
1651 stats_.AddBits(
1652 Stats::kByteKindCodeInfoDexRegisterMap,
1653 kBitsPerByte * dex_register_bytes);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001654
1655 // Inline infos.
1656 const size_t num_inline_infos = encoding.inline_info.num_entries;
1657 if (num_inline_infos > 0u) {
1658 stats_.AddBits(
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001659 Stats::kByteKindInlineInfoMethodIndexIdx,
1660 encoding.inline_info.encoding.GetMethodIndexIdxEncoding().BitSize() *
1661 num_inline_infos);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001662 stats_.AddBits(
1663 Stats::kByteKindInlineInfoDexPc,
1664 encoding.inline_info.encoding.GetDexPcEncoding().BitSize() * num_inline_infos);
1665 stats_.AddBits(
1666 Stats::kByteKindInlineInfoExtraData,
1667 encoding.inline_info.encoding.GetExtraDataEncoding().BitSize() * num_inline_infos);
1668 stats_.AddBits(
1669 Stats::kByteKindInlineInfoDexRegisterMap,
1670 encoding.inline_info.encoding.GetDexRegisterMapEncoding().BitSize() *
1671 num_inline_infos);
1672 stats_.AddBits(Stats::kByteKindInlineInfoIsLast, num_inline_infos);
1673 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001674 }
1675 }
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001676 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1677 size_t offset = 0;
1678 while (offset < code_size) {
1679 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
1680 if (offset == helper.GetOffset()) {
1681 ScopedIndentation indent1(vios);
1682 StackMap stack_map = helper.GetStackMap();
1683 DCHECK(stack_map.IsValid());
1684 stack_map.Dump(vios,
1685 helper.GetCodeInfo(),
1686 helper.GetEncoding(),
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001687 method_info,
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001688 oat_method.GetCodeOffset(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001689 code_item->registers_size_,
1690 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001691 do {
1692 helper.Next();
1693 // There may be multiple stack maps at a given PC. We display only the first one.
1694 } while (offset == helper.GetOffset());
1695 }
1696 DCHECK_LT(offset, helper.GetOffset());
1697 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001698 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001699 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1700 size_t offset = 0;
1701 while (offset < code_size) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001702 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001703 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001704 }
1705 }
1706
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001707 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001708 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001709 const OatDumperOptions& options_;
1710 uint32_t resolved_addr2instr_;
Andreas Gampe372f3a32016-08-19 10:49:06 -07001711 const InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001712 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001713 Disassembler* disassembler_;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001714 Stats stats_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001715};
1716
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001717class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001718 public:
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001719 ImageDumper(std::ostream* os,
1720 gc::space::ImageSpace& image_space,
1721 const ImageHeader& image_header,
1722 OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001723 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001724 vios_(os),
1725 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001726 image_space_(image_space),
1727 image_header_(image_header),
1728 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001729
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001730 bool Dump() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001731 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001732 std::ostream& indent_os = vios_.Stream();
1733
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001734 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001735
Jeff Haodcdc85b2015-12-04 14:06:18 -08001736 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1737
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001738 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001739
Mathieu Chartiere401d142015-04-22 13:56:20 -07001740 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1741
1742 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1743 auto section = static_cast<ImageHeader::ImageSections>(i);
1744 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1745 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001746
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001747 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001748
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001749 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001750
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001751 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1752
1753 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1754
1755 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001756
Alex Lighta59dd802014-07-02 16:28:08 -07001757 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1758
Igor Murashkin46774762014-10-22 11:37:02 -07001759 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1760
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001761 {
1762 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001763 static_assert(arraysize(image_roots_descriptions_) ==
1764 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001765 DCHECK_LE(image_header_.GetImageRoots()->GetLength(), ImageHeader::kImageRootsMax);
1766 for (int32_t i = 0, size = image_header_.GetImageRoots()->GetLength(); i != size; ++i) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001767 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1768 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001769 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001770 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001771 if (image_root_object != nullptr && image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001772 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001773 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001774 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001775 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1776 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001777 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001778 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1779 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001780 run++;
1781 } else {
1782 break;
1783 }
1784 }
1785 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001786 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001787 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001788 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001789 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001790 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001791 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001792 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001793 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001794 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001795 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001796 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001797 }
1798 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001799 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001800
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001801 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001802 os << "METHOD ROOTS\n";
1803 static_assert(arraysize(image_methods_descriptions_) ==
1804 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1805 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1806 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1807 const char* description = image_methods_descriptions_[i];
1808 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001809 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001810 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001811 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001812 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001813
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001814 Runtime* const runtime = Runtime::Current();
1815 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001816 std::string image_filename = image_space_.GetImageFilename();
1817 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001818 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001819 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001820 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001821 const OatFile* oat_file = image_space_.GetOatFile();
Alex Lighta59dd802014-07-02 16:28:08 -07001822 if (oat_file == nullptr) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001823 oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location);
1824 }
1825 if (oat_file == nullptr) {
1826 oat_file = OatFile::Open(oat_location,
1827 oat_location,
1828 nullptr,
1829 nullptr,
1830 false,
1831 /*low_4gb*/false,
1832 nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001833 &error_msg);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001834 }
1835 if (oat_file == nullptr) {
1836 os << "OAT FILE NOT FOUND: " << error_msg << "\n";
1837 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001838 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001839 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001840
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001841 stats_.oat_file_bytes = oat_file->Size();
1842
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001843 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001844
Mathieu Chartier02e25112013-08-14 16:14:24 -07001845 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001846 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001847 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1848 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001849 }
1850
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001851 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001852
Jeff Haodcdc85b2015-12-04 14:06:18 -08001853 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001854 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001855 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001856 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001857 {
1858 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1859 heap->FlushAllocStack();
1860 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001861 // Since FlushAllocStack() above resets the (active) allocation
1862 // stack. Need to revoke the thread-local allocation stacks that
1863 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001864 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001865 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001866 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001867 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001868 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001869 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001870 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001871 {
Andreas Gampecc1b5352016-12-01 16:58:38 -08001872 ReaderMutexLock mu(self, *Locks::dex_lock_);
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001873 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001874 ObjPtr<mirror::DexCache> dex_cache =
1875 ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001876 if (dex_cache != nullptr) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001877 dex_caches_.insert(dex_cache.Ptr());
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001878 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001879 }
1880 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001881 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001882 // Dump the normal objects before ArtMethods.
1883 image_space_.GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1884 indent_os << "\n";
1885 // TODO: Dump fields.
1886 // Dump methods after.
Jeff Haodcdc85b2015-12-04 14:06:18 -08001887 DumpArtMethodVisitor visitor(this);
Mathieu Chartiere42888f2016-04-14 10:49:19 -07001888 image_header_.VisitPackedArtMethods(&visitor,
1889 image_space_.Begin(),
1890 image_header_.GetPointerSize());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001891 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001892 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001893 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001894 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001895 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001896 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001897 size_t data_size = image_header_.GetDataSize(); // stored size in file.
1898 if (file == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001899 LOG(WARNING) << "Failed to find image in " << image_filename;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001900 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001901 stats_.file_bytes = file->GetLength();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001902 // If the image is compressed, adjust to decompressed size.
1903 size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader);
1904 if (image_header_.GetStorageMode() == ImageHeader::kStorageModeUncompressed) {
1905 DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image";
1906 }
1907 stats_.file_bytes += uncompressed_size - data_size;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001908 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001909 size_t header_bytes = sizeof(ImageHeader);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001910 const auto& object_section = image_header_.GetImageSection(ImageHeader::kSectionObjects);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001911 const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1912 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001913 const auto& dex_cache_arrays_section = image_header_.GetImageSection(
1914 ImageHeader::kSectionDexCacheArrays);
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001915 const auto& intern_section = image_header_.GetImageSection(
1916 ImageHeader::kSectionInternedStrings);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001917 const auto& class_table_section = image_header_.GetImageSection(
1918 ImageHeader::kSectionClassTable);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001919 const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1920
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001921 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001922
1923 // Objects are kObjectAlignment-aligned.
1924 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1925 if (object_section.Offset() > header_bytes) {
1926 stats_.alignment_bytes += object_section.Offset() - header_bytes;
1927 }
1928
1929 // Field section is 4-byte aligned.
1930 constexpr size_t kFieldSectionAlignment = 4U;
1931 uint32_t end_objects = object_section.Offset() + object_section.Size();
1932 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
1933 stats_.alignment_bytes += field_section.Offset() - end_objects;
1934
1935 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
1936 uint32_t end_fields = field_section.Offset() + field_section.Size();
1937 CHECK_ALIGNED(method_section.Offset(), 4);
1938 stats_.alignment_bytes += method_section.Offset() - end_fields;
1939
1940 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
1941 uint32_t end_methods = method_section.Offset() + method_section.Size();
1942 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
1943 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
1944
1945 // Intern table is 8-byte aligned.
1946 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
1947 CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
1948 stats_.alignment_bytes += intern_section.Offset() - end_caches;
1949
1950 // Add space between intern table and class table.
1951 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
1952 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
1953
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001954 // Add space between end of image data and bitmap. Expect the bitmap to be page-aligned.
1955 const size_t bitmap_offset = sizeof(ImageHeader) + data_size;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001956 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001957 stats_.alignment_bytes += RoundUp(bitmap_offset, kPageSize) - bitmap_offset;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001958
Mathieu Chartiere401d142015-04-22 13:56:20 -07001959 stats_.bitmap_bytes += bitmap_section.Size();
1960 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01001961 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01001962 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001963 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001964 stats_.class_table_bytes += class_table_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001965 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001966 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001967
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001968 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001969
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001970 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001971 }
1972
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001973 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001974 class DumpArtMethodVisitor : public ArtMethodVisitor {
1975 public:
1976 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
1977
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001978 virtual void Visit(ArtMethod* method) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001979 std::ostream& indent_os = image_dumper_->vios_.Stream();
David Sehr709b0702016-10-13 09:12:37 -07001980 indent_os << method << " " << " ArtMethod: " << ArtMethod::PrettyMethod(method) << "\n";
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001981 image_dumper_->DumpMethod(method, indent_os);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001982 indent_os << "\n";
1983 }
1984
1985 private:
1986 ImageDumper* const image_dumper_;
1987 };
1988
Mathieu Chartier3398c782016-09-30 10:27:43 -07001989 static void PrettyObjectValue(std::ostream& os,
1990 ObjPtr<mirror::Class> type,
1991 ObjPtr<mirror::Object> value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001992 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001993 CHECK(type != nullptr);
1994 if (value == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07001995 os << StringPrintf("null %s\n", type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001996 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001997 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001998 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001999 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07002000 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002001 mirror::Class* klass = value->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07002002 os << StringPrintf("%p Class: %s\n", klass, mirror::Class::PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002003 } else {
David Sehr709b0702016-10-13 09:12:37 -07002004 os << StringPrintf("%p %s\n", value.Ptr(), type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002005 }
2006 }
2007
Mathieu Chartier3398c782016-09-30 10:27:43 -07002008 static void PrintField(std::ostream& os, ArtField* field, ObjPtr<mirror::Object> obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002009 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07002010 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08002011 switch (field->GetTypeAsPrimitiveType()) {
2012 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08002013 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002014 break;
2015 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002016 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002017 break;
2018 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002019 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002020 break;
2021 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002022 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002023 break;
2024 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07002025 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002026 break;
2027 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07002028 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002029 break;
2030 case Primitive::kPrimBoolean:
Roland Levillain5e8d5f02016-10-18 18:03:43 +01002031 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj) ? "true" : "false",
Fred Shih37f05ef2014-07-16 18:38:08 -07002032 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002033 break;
2034 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07002035 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002036 break;
2037 case Primitive::kPrimNot: {
2038 // Get the value, don't compute the type unless it is non-null as we don't want
2039 // to cause class loading.
Mathieu Chartier3398c782016-09-30 10:27:43 -07002040 ObjPtr<mirror::Object> value = field->GetObj(obj);
Ian Rogers08f1f502014-12-02 15:04:37 -08002041 if (value == nullptr) {
2042 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07002043 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08002044 // Grab the field type without causing resolution.
Mathieu Chartier3398c782016-09-30 10:27:43 -07002045 ObjPtr<mirror::Class> field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08002046 if (field_type != nullptr) {
2047 PrettyObjectValue(os, field_type, value);
2048 } else {
Mathieu Chartier3398c782016-09-30 10:27:43 -07002049 os << StringPrintf("%p %s\n",
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07002050 value.Ptr(),
Ian Rogers08f1f502014-12-02 15:04:37 -08002051 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
2052 }
Ian Rogers50239c72013-06-17 14:53:22 -07002053 }
Ian Rogers08f1f502014-12-02 15:04:37 -08002054 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07002055 }
Ian Rogers08f1f502014-12-02 15:04:37 -08002056 default:
2057 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
2058 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08002059 }
2060 }
2061
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002062 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002063 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002064 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002065 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002066 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08002067 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002068 for (ArtField& field : klass->GetIFields()) {
2069 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08002070 }
2071 }
2072
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002073 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002074 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002075 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002076
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002077 const void* GetQuickOatCodeBegin(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08002078 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002079 image_header_.GetPointerSize());
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002080 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002081 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002082 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002083 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002084 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002085 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002086 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002087 }
2088
Mathieu Chartiere401d142015-04-22 13:56:20 -07002089 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002090 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002091 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
2092 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002093 return 0;
2094 }
2095 return oat_code_begin[-1];
2096 }
2097
Mathieu Chartiere401d142015-04-22 13:56:20 -07002098 const void* GetQuickOatCodeEnd(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002099 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002100 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002101 if (oat_code_begin == nullptr) {
2102 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002103 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002104 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002105 }
2106
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002107 static void Callback(mirror::Object* obj, void* arg) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002108 DCHECK(obj != nullptr);
2109 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002110 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002111 if (!state->InDumpSpace(obj)) {
2112 return;
2113 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002114
2115 size_t object_bytes = obj->SizeOf();
2116 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
2117 state->stats_.object_bytes += object_bytes;
2118 state->stats_.alignment_bytes += alignment_bytes;
2119
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002120 std::ostream& os = state->vios_.Stream();
2121
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002122 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08002123 if (obj_class->IsArrayClass()) {
David Sehr709b0702016-10-13 09:12:37 -07002124 os << StringPrintf("%p: %s length:%d\n", obj, obj_class->PrettyDescriptor().c_str(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002125 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08002126 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002127 mirror::Class* klass = obj->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07002128 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj,
2129 mirror::Class::PrettyDescriptor(klass).c_str())
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002130 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08002131 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002132 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07002133 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002134 } else {
David Sehr709b0702016-10-13 09:12:37 -07002135 os << StringPrintf("%p: %s\n", obj, obj_class->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002136 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002137 ScopedIndentation indent1(&state->vios_);
2138 DumpFields(os, obj, obj_class);
Andreas Gampe542451c2016-07-26 09:02:02 -07002139 const PointerSize image_pointer_size = state->image_header_.GetPointerSize();
Ian Rogersd5b32602012-02-26 16:40:04 -08002140 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002141 auto* obj_array = obj->AsObjectArray<mirror::Object>();
2142 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002143 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002144 size_t run = 0;
2145 for (int32_t j = i + 1; j < length; j++) {
2146 if (value == obj_array->Get(j)) {
2147 run++;
2148 } else {
2149 break;
2150 }
2151 }
2152 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002153 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002154 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002155 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08002156 i = i + run;
2157 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002158 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002159 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002160 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08002161 }
2162 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07002163 mirror::Class* klass = obj->AsClass();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002164 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002165 os << "STATICS:\n";
2166 ScopedIndentation indent2(&state->vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002167 for (ArtField& field : klass->GetSFields()) {
2168 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08002169 }
2170 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002171 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01002172 auto it = state->dex_caches_.find(obj);
2173 if (it != state->dex_caches_.end()) {
2174 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002175 const auto& field_section = state->image_header_.GetImageSection(
2176 ImageHeader::kSectionArtFields);
2177 const auto& method_section = state->image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01002178 size_t num_methods = dex_cache->NumResolvedMethods();
2179 if (num_methods != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002180 os << "Methods (size=" << num_methods << "):\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002181 ScopedIndentation indent2(&state->vios_);
2182 auto* resolved_methods = dex_cache->GetResolvedMethods();
2183 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002184 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_methods,
2185 i,
2186 image_pointer_size);
Vladimir Marko05792b92015-08-03 11:56:49 +01002187 size_t run = 0;
2188 for (size_t j = i + 1;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002189 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_methods,
2190 j,
2191 image_pointer_size);
2192 ++j) {
2193 ++run;
2194 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002195 if (run == 0) {
2196 os << StringPrintf("%zd: ", i);
2197 } else {
2198 os << StringPrintf("%zd to %zd: ", i, i + run);
2199 i = i + run;
2200 }
2201 std::string msg;
2202 if (elem == nullptr) {
2203 msg = "null";
2204 } else if (method_section.Contains(
2205 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002206 msg = reinterpret_cast<ArtMethod*>(elem)->PrettyMethod();
Vladimir Marko05792b92015-08-03 11:56:49 +01002207 } else {
2208 msg = "<not in method section>";
2209 }
2210 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07002211 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002212 }
2213 size_t num_fields = dex_cache->NumResolvedFields();
2214 if (num_fields != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002215 os << "Fields (size=" << num_fields << "):\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002216 ScopedIndentation indent2(&state->vios_);
2217 auto* resolved_fields = dex_cache->GetResolvedFields();
2218 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
Vladimir Markof44d36c2017-03-14 14:18:46 +00002219 auto* elem = mirror::DexCache::GetNativePairPtrSize(
2220 resolved_fields, i, image_pointer_size).object;
Vladimir Marko05792b92015-08-03 11:56:49 +01002221 size_t run = 0;
2222 for (size_t j = i + 1;
Vladimir Markof44d36c2017-03-14 14:18:46 +00002223 j != length &&
2224 elem == mirror::DexCache::GetNativePairPtrSize(
2225 resolved_fields, j, image_pointer_size).object;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002226 ++j) {
2227 ++run;
2228 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002229 if (run == 0) {
2230 os << StringPrintf("%zd: ", i);
2231 } else {
2232 os << StringPrintf("%zd to %zd: ", i, i + run);
2233 i = i + run;
2234 }
2235 std::string msg;
2236 if (elem == nullptr) {
2237 msg = "null";
2238 } else if (field_section.Contains(
2239 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002240 msg = reinterpret_cast<ArtField*>(elem)->PrettyField();
Vladimir Marko05792b92015-08-03 11:56:49 +01002241 } else {
2242 msg = "<not in field section>";
2243 }
2244 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002245 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002246 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002247 size_t num_types = dex_cache->NumResolvedTypes();
2248 if (num_types != 0u) {
2249 os << "Types (size=" << num_types << "):\n";
2250 ScopedIndentation indent2(&state->vios_);
2251 auto* resolved_types = dex_cache->GetResolvedTypes();
2252 for (size_t i = 0; i < num_types; ++i) {
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002253 auto pair = resolved_types[i].load(std::memory_order_relaxed);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002254 size_t run = 0;
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002255 for (size_t j = i + 1; j != num_types; ++j) {
2256 auto other_pair = resolved_types[j].load(std::memory_order_relaxed);
2257 if (pair.index != other_pair.index ||
2258 pair.object.Read() != other_pair.object.Read()) {
2259 break;
2260 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002261 ++run;
2262 }
2263 if (run == 0) {
2264 os << StringPrintf("%zd: ", i);
2265 } else {
2266 os << StringPrintf("%zd to %zd: ", i, i + run);
2267 i = i + run;
2268 }
2269 std::string msg;
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002270 auto* elem = pair.object.Read();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002271 if (elem == nullptr) {
2272 msg = "null";
2273 } else {
David Sehr709b0702016-10-13 09:12:37 -07002274 msg = elem->PrettyClass();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002275 }
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002276 os << StringPrintf("%p %u %s\n", elem, pair.index, msg.c_str());
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002277 }
2278 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002279 }
2280 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002281 std::string temp;
2282 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002283 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002284
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002285 void DumpMethod(ArtMethod* method, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002286 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002287 DCHECK(method != nullptr);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002288 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
2289 const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
Andreas Gampe542451c2016-07-26 09:02:02 -07002290 const PointerSize pointer_size = image_header_.GetPointerSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002291 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2292 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002293 if (method->IsNative()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002294 bool first_occurrence;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002295 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2296 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002297 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002298 stats_.native_to_managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002299 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002300 if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
2301 image_header_.GetPointerSize())) {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002302 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002303 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002304 } else if (method->IsAbstract() || method->IsClassInitializer()) {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002305 // Don't print information for these.
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002306 } else if (method->IsRuntimeMethod()) {
2307 ImtConflictTable* table = method->GetImtConflictTable(image_header_.GetPointerSize());
2308 if (table != nullptr) {
2309 indent_os << "IMT conflict table " << table << " method: ";
2310 for (size_t i = 0, count = table->NumEntries(pointer_size); i < count; ++i) {
David Sehr709b0702016-10-13 09:12:37 -07002311 indent_os << ArtMethod::PrettyMethod(table->GetImplementationMethod(i, pointer_size))
2312 << " ";
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002313 }
2314 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002315 } else {
2316 const DexFile::CodeItem* code_item = method->GetCodeItem();
2317 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002318 stats_.dex_instruction_bytes += dex_instruction_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002319
2320 bool first_occurrence;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002321 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002322 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01002323 // Method compiled with the optimizing compiler have no vmap table.
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002324 vmap_table_bytes = ComputeOatSize(method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002325 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002326 stats_.vmap_table_bytes += vmap_table_bytes;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002327 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002328 }
2329
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002330 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2331 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002332 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002333 stats_.managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002334 if (method->IsConstructor()) {
2335 if (method->IsStatic()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002336 stats_.class_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002337 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002338 stats_.large_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002339 }
2340 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002341 stats_.large_method_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002342 }
2343 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002344 stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002345
Igor Murashkin7617abd2015-07-10 18:27:47 -07002346 uint32_t method_access_flags = method->GetAccessFlags();
2347
Mathieu Chartiere401d142015-04-22 13:56:20 -07002348 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002349 indent_os << StringPrintf("SIZE: Dex Instructions=%zd StackMaps=%zd AccessFlags=0x%x\n",
2350 dex_instruction_bytes,
2351 vmap_table_bytes,
Igor Murashkin7617abd2015-07-10 18:27:47 -07002352 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002353
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002354 size_t total_size = dex_instruction_bytes +
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002355 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002356
2357 double expansion =
2358 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002359 stats_.ComputeOutliers(total_size, expansion, method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002360 }
2361 }
2362
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002363 std::set<const void*> already_seen_;
2364 // Compute the size of the given data within the oat file and whether this is the first time
2365 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002366 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002367 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002368 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002369 already_seen_.insert(oat_data);
2370 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002371 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002372 }
2373 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002374 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002375
2376 public:
2377 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002378 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002379 size_t file_bytes;
2380
2381 size_t header_bytes;
2382 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002383 size_t art_field_bytes;
2384 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01002385 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002386 size_t interned_strings_bytes;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002387 size_t class_table_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07002388 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002389 size_t alignment_bytes;
2390
2391 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002392 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002393 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07002394 size_t class_initializer_code_bytes;
2395 size_t large_initializer_code_bytes;
2396 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002397
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002398 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002399
2400 size_t dex_instruction_bytes;
2401
Mathieu Chartiere401d142015-04-22 13:56:20 -07002402 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002403 std::vector<size_t> method_outlier_size;
2404 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002405 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002406
Roland Levillain3887c462015-08-12 18:15:42 +01002407 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002408 : oat_file_bytes(0),
2409 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002410 header_bytes(0),
2411 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002412 art_field_bytes(0),
2413 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01002414 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002415 interned_strings_bytes(0),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002416 class_table_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07002417 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002418 alignment_bytes(0),
2419 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002420 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002421 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07002422 class_initializer_code_bytes(0),
2423 large_initializer_code_bytes(0),
2424 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002425 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002426 dex_instruction_bytes(0) {}
2427
Elliott Hughesa0e18062012-04-13 15:59:59 -07002428 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002429 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002430 size_t bytes;
2431 size_t count;
2432 };
2433 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2434 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002435
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002436 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002437 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2438 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002439 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002440 it->second.count += 1;
2441 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002442 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002443 }
2444 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002445
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002446 double PercentOfOatBytes(size_t size) {
2447 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2448 }
2449
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002450 double PercentOfFileBytes(size_t size) {
2451 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2452 }
2453
2454 double PercentOfObjectBytes(size_t size) {
2455 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2456 }
2457
Mathieu Chartiere401d142015-04-22 13:56:20 -07002458 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002459 method_outlier_size.push_back(total_size);
2460 method_outlier_expansion.push_back(expansion);
2461 method_outlier.push_back(method);
2462 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002463
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002464 void DumpOutliers(std::ostream& os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002465 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002466 size_t sum_of_sizes = 0;
2467 size_t sum_of_sizes_squared = 0;
2468 size_t sum_of_expansion = 0;
2469 size_t sum_of_expansion_squared = 0;
2470 size_t n = method_outlier_size.size();
Mathieu Chartier1ebf8d32016-06-09 11:51:27 -07002471 if (n <= 1) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08002472 return;
2473 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002474 for (size_t i = 0; i < n; i++) {
2475 size_t cur_size = method_outlier_size[i];
2476 sum_of_sizes += cur_size;
2477 sum_of_sizes_squared += cur_size * cur_size;
2478 double cur_expansion = method_outlier_expansion[i];
2479 sum_of_expansion += cur_expansion;
2480 sum_of_expansion_squared += cur_expansion * cur_expansion;
2481 }
2482 size_t size_mean = sum_of_sizes / n;
2483 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2484 double expansion_mean = sum_of_expansion / n;
2485 double expansion_variance =
2486 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2487
2488 // Dump methods whose size is a certain number of standard deviations from the mean
2489 size_t dumped_values = 0;
2490 size_t skipped_values = 0;
2491 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2492 size_t cur_size_variance = i * i * size_variance;
2493 bool first = true;
2494 for (size_t j = 0; j < n; j++) {
2495 size_t cur_size = method_outlier_size[j];
2496 if (cur_size > size_mean) {
2497 size_t cur_var = cur_size - size_mean;
2498 cur_var = cur_var * cur_var;
2499 if (cur_var > cur_size_variance) {
2500 if (dumped_values > 20) {
2501 if (i == 1) {
2502 skipped_values++;
2503 } else {
2504 i = 2; // jump to counting for 1 standard deviation
2505 break;
2506 }
2507 } else {
2508 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002509 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002510 first = false;
2511 }
David Sehr709b0702016-10-13 09:12:37 -07002512 os << ArtMethod::PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002513 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002514 method_outlier_size[j] = 0; // don't consider this method again
2515 dumped_values++;
2516 }
2517 }
2518 }
2519 }
2520 }
2521 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002522 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002523 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002524 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002525 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002526
2527 // Dump methods whose expansion is a certain number of standard deviations from the mean
2528 dumped_values = 0;
2529 skipped_values = 0;
2530 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2531 double cur_expansion_variance = i * i * expansion_variance;
2532 bool first = true;
2533 for (size_t j = 0; j < n; j++) {
2534 double cur_expansion = method_outlier_expansion[j];
2535 if (cur_expansion > expansion_mean) {
2536 size_t cur_var = cur_expansion - expansion_mean;
2537 cur_var = cur_var * cur_var;
2538 if (cur_var > cur_expansion_variance) {
2539 if (dumped_values > 20) {
2540 if (i == 1) {
2541 skipped_values++;
2542 } else {
2543 i = 2; // jump to counting for 1 standard deviation
2544 break;
2545 }
2546 } else {
2547 if (first) {
2548 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002549 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002550 first = false;
2551 }
David Sehr709b0702016-10-13 09:12:37 -07002552 os << ArtMethod::PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002553 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002554 method_outlier_expansion[j] = 0.0; // don't consider this method again
2555 dumped_values++;
2556 }
2557 }
2558 }
2559 }
2560 }
2561 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002562 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002563 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002564 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002565 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002566 }
2567
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002568 void Dump(std::ostream& os, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002569 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002570 {
2571 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2572 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002573 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2574 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2575 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2576 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2577 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2578 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002579 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002580 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2581 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002582 header_bytes, PercentOfFileBytes(header_bytes),
2583 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002584 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2585 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002586 dex_cache_arrays_bytes,
2587 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002588 interned_strings_bytes,
2589 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002590 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002591 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002592 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2593 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002594 CHECK_EQ(file_bytes,
2595 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2596 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2597 bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002598 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002599
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002600 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002601 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002602 for (const auto& sizes_and_count : sizes_and_counts) {
2603 const std::string& descriptor(sizes_and_count.first);
2604 double average = static_cast<double>(sizes_and_count.second.bytes) /
2605 static_cast<double>(sizes_and_count.second.count);
2606 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002607 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002608 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002609 descriptor.c_str(), sizes_and_count.second.bytes,
2610 sizes_and_count.second.count, average, percent);
2611 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002612 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002613 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002614 CHECK_EQ(object_bytes, object_bytes_total);
2615
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002616 os << StringPrintf("oat_file_bytes = %8zd\n"
2617 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002618 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2619 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2620 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2621 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002622 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002623 managed_code_bytes,
2624 PercentOfOatBytes(managed_code_bytes),
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002625 native_to_managed_code_bytes,
2626 PercentOfOatBytes(native_to_managed_code_bytes),
2627 class_initializer_code_bytes,
2628 PercentOfOatBytes(class_initializer_code_bytes),
2629 large_initializer_code_bytes,
2630 PercentOfOatBytes(large_initializer_code_bytes),
2631 large_method_code_bytes,
2632 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002633 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002634 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002635 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002636 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2637 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002638 }
2639
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002640 os << "\n" << StringPrintf("vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002641 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002642 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002643
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002644 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2645 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002646 static_cast<double>(managed_code_bytes) /
2647 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002648 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002649 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002650 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002651
2652 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002653 }
2654 } stats_;
2655
2656 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002657 enum {
2658 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2659 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2660 kLargeConstructorDexBytes = 4000,
2661 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2662 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2663 kLargeMethodDexBytes = 16000
2664 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002665
2666 // For performance, use the *os_ directly for anything that doesn't need indentation
2667 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002668 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002669 VariableIndentationOutputStream vios_;
2670 ScopedIndentation indent1_;
2671
Ian Rogers1d54e732013-05-02 21:10:01 -07002672 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002673 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002674 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002675 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002676 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002677
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002678 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002679};
2680
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002681static int DumpImage(gc::space::ImageSpace* image_space,
2682 OatDumperOptions* options,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002683 std::ostream* os) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002684 const ImageHeader& image_header = image_space->GetImageHeader();
2685 if (!image_header.IsValid()) {
2686 fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
2687 return EXIT_FAILURE;
2688 }
2689 ImageDumper image_dumper(os, *image_space, image_header, options);
2690 if (!image_dumper.Dump()) {
2691 return EXIT_FAILURE;
2692 }
2693 return EXIT_SUCCESS;
2694}
2695
2696static int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002697 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002698 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002699 options->class_loader_ = &null_class_loader;
2700
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002701 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002702 if (options->app_image_ != nullptr) {
2703 if (options->app_oat_ == nullptr) {
2704 LOG(ERROR) << "Can not dump app image without app oat file";
Jeff Haodcdc85b2015-12-04 14:06:18 -08002705 return EXIT_FAILURE;
2706 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002707 // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
2708 // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
2709 // pointers into 32 bit pointer sized ArtMethods.
2710 std::string error_msg;
2711 std::unique_ptr<OatFile> oat_file(OatFile::Open(options->app_oat_,
2712 options->app_oat_,
2713 nullptr,
2714 nullptr,
2715 false,
2716 /*low_4gb*/true,
2717 nullptr,
2718 &error_msg));
2719 if (oat_file == nullptr) {
2720 LOG(ERROR) << "Failed to open oat file " << options->app_oat_ << " with error " << error_msg;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002721 return EXIT_FAILURE;
2722 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002723 std::unique_ptr<gc::space::ImageSpace> space(
2724 gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg));
2725 if (space == nullptr) {
2726 LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error "
2727 << error_msg;
2728 }
2729 // Open dex files for the image.
2730 std::vector<std::unique_ptr<const DexFile>> dex_files;
2731 if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
2732 LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
2733 << error_msg;
2734 }
2735 // Dump the actual image.
2736 int result = DumpImage(space.get(), options, os);
2737 if (result != EXIT_SUCCESS) {
2738 return result;
2739 }
2740 // Fall through to dump the boot images.
2741 }
2742
2743 gc::Heap* heap = runtime->GetHeap();
2744 CHECK(heap->HasBootImageSpace()) << "No image spaces";
2745 for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
2746 int result = DumpImage(image_space, options, os);
2747 if (result != EXIT_SUCCESS) {
2748 return result;
2749 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002750 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002751 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002752}
2753
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002754static jobject InstallOatFile(Runtime* runtime,
2755 std::unique_ptr<OatFile> oat_file,
2756 std::vector<const DexFile*>* class_path)
2757 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002758 Thread* self = Thread::Current();
2759 CHECK(self != nullptr);
2760 // Need well-known-classes.
2761 WellKnownClasses::Init(self->GetJniEnv());
2762
2763 // Need to register dex files to get a working dex cache.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002764 OatFile* oat_file_ptr = oat_file.get();
Andreas Gampe00b25f32014-09-17 21:49:05 -07002765 ClassLinker* class_linker = runtime->GetClassLinker();
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002766 runtime->GetOatFileManager().RegisterOatFile(std::move(oat_file));
2767 for (const OatFile::OatDexFile* odf : oat_file_ptr->GetOatDexFiles()) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002768 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002769 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002770 CHECK(dex_file != nullptr) << error_msg;
Vladimir Markocd556b02017-02-03 11:47:34 +00002771 ObjPtr<mirror::DexCache> dex_cache =
2772 class_linker->RegisterDexFile(*dex_file, nullptr);
2773 CHECK(dex_cache != nullptr);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002774 class_path->push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002775 }
2776
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002777 // Need a class loader. Fake that we're a compiler.
2778 // Note: this will run initializers through the unstarted runtime, so make sure it's
2779 // initialized.
2780 interpreter::UnstartedRuntime::Initialize();
2781
2782 jobject class_loader = class_linker->CreatePathClassLoader(self, *class_path);
2783
2784 return class_loader;
2785}
2786
2787static int DumpOatWithRuntime(Runtime* runtime,
2788 std::unique_ptr<OatFile> oat_file,
2789 OatDumperOptions* options,
2790 std::ostream* os) {
2791 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2792 ScopedObjectAccess soa(Thread::Current());
2793
2794 OatFile* oat_file_ptr = oat_file.get();
2795 std::vector<const DexFile*> class_path;
2796 jobject class_loader = InstallOatFile(runtime, std::move(oat_file), &class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002797
2798 // Use the class loader while dumping.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002799 StackHandleScope<1> scope(soa.Self());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002800 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07002801 soa.Decode<mirror::ClassLoader>(class_loader));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002802 options->class_loader_ = &loader_handle;
2803
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002804 OatDumper oat_dumper(*oat_file_ptr, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002805 bool success = oat_dumper.Dump(*os);
2806 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2807}
2808
2809static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002810 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002811 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002812 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002813 options->class_loader_ = &null_class_loader;
2814
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002815 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002816 bool success = oat_dumper.Dump(*os);
2817 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2818}
2819
2820static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2821 std::ostream* os) {
2822 std::string error_msg;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002823 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
2824 oat_filename,
2825 nullptr,
2826 nullptr,
2827 false,
2828 /*low_4gb*/false,
2829 nullptr,
2830 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002831 if (oat_file == nullptr) {
2832 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2833 return EXIT_FAILURE;
2834 }
2835
2836 if (runtime != nullptr) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002837 return DumpOatWithRuntime(runtime, std::move(oat_file), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002838 } else {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002839 return DumpOatWithoutRuntime(oat_file.get(), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002840 }
2841}
2842
David Srbecky2fdd03c2016-03-10 15:32:37 +00002843static int SymbolizeOat(const char* oat_filename, std::string& output_name, bool no_bits) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002844 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002845 OatFile* oat_file = OatFile::Open(oat_filename,
2846 oat_filename,
2847 nullptr,
2848 nullptr,
2849 false,
2850 /*low_4gb*/false,
2851 nullptr,
2852 &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002853 if (oat_file == nullptr) {
2854 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2855 return EXIT_FAILURE;
2856 }
2857
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002858 bool result;
2859 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
2860 // files for 64-bit code in the past.
2861 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002862 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file, output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002863 result = oat_symbolizer.Symbolize();
2864 } else {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002865 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file, output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002866 result = oat_symbolizer.Symbolize();
2867 }
2868 if (!result) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002869 fprintf(stderr, "Failed to symbolize\n");
2870 return EXIT_FAILURE;
2871 }
2872
2873 return EXIT_SUCCESS;
2874}
2875
Andreas Gampe9fded872016-09-25 16:08:35 -07002876class IMTDumper {
2877 public:
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002878 static bool Dump(Runtime* runtime,
2879 const std::string& imt_file,
2880 bool dump_imt_stats,
2881 const char* oat_filename) {
2882 Thread* self = Thread::Current();
2883
2884 ScopedObjectAccess soa(self);
2885 StackHandleScope<1> scope(self);
2886 MutableHandle<mirror::ClassLoader> class_loader = scope.NewHandle<mirror::ClassLoader>(nullptr);
2887 std::vector<const DexFile*> class_path;
2888
2889 if (oat_filename != nullptr) {
2890 std::string error_msg;
2891 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
2892 oat_filename,
2893 nullptr,
2894 nullptr,
2895 false,
2896 /*low_4gb*/false,
2897 nullptr,
2898 &error_msg));
2899 if (oat_file == nullptr) {
2900 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2901 return false;
2902 }
2903
2904 class_loader.Assign(soa.Decode<mirror::ClassLoader>(
2905 InstallOatFile(runtime, std::move(oat_file), &class_path)));
2906 } else {
2907 class_loader.Assign(nullptr); // Boot classloader. Just here for explicit documentation.
2908 class_path = runtime->GetClassLinker()->GetBootClassPath();
2909 }
2910
2911 if (!imt_file.empty()) {
2912 return DumpImt(runtime, imt_file, class_loader);
2913 }
2914
2915 if (dump_imt_stats) {
2916 return DumpImtStats(runtime, class_path, class_loader);
2917 }
2918
2919 LOG(FATAL) << "Should not reach here";
2920 UNREACHABLE();
2921 }
2922
2923 private:
2924 static bool DumpImt(Runtime* runtime,
2925 const std::string& imt_file,
2926 Handle<mirror::ClassLoader> h_class_loader)
2927 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002928 std::vector<std::string> lines = ReadCommentedInputFromFile(imt_file);
2929 std::unordered_set<std::string> prepared;
2930
2931 for (const std::string& line : lines) {
2932 // A line should be either a class descriptor, in which case we will dump the complete IMT,
2933 // or a class descriptor and an interface method, in which case we will lookup the method,
2934 // determine its IMT slot, and check the class' IMT.
2935 size_t first_space = line.find(' ');
2936 if (first_space == std::string::npos) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002937 DumpIMTForClass(runtime, line, h_class_loader, &prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07002938 } else {
2939 DumpIMTForMethod(runtime,
2940 line.substr(0, first_space),
2941 line.substr(first_space + 1, std::string::npos),
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002942 h_class_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07002943 &prepared);
2944 }
2945 std::cerr << std::endl;
2946 }
2947
2948 return true;
2949 }
2950
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002951 static bool DumpImtStats(Runtime* runtime,
2952 const std::vector<const DexFile*>& dex_files,
2953 Handle<mirror::ClassLoader> h_class_loader)
2954 REQUIRES_SHARED(Locks::mutator_lock_) {
2955 size_t without_imt = 0;
2956 size_t with_imt = 0;
Andreas Gampe9fded872016-09-25 16:08:35 -07002957 std::map<size_t, size_t> histogram;
2958
2959 ClassLinker* class_linker = runtime->GetClassLinker();
2960 const PointerSize pointer_size = class_linker->GetImagePointerSize();
2961 std::unordered_set<std::string> prepared;
2962
2963 Thread* self = Thread::Current();
Andreas Gampe9fded872016-09-25 16:08:35 -07002964 StackHandleScope<1> scope(self);
2965 MutableHandle<mirror::Class> h_klass(scope.NewHandle<mirror::Class>(nullptr));
2966
2967 for (const DexFile* dex_file : dex_files) {
2968 for (uint32_t class_def_index = 0;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002969 class_def_index != dex_file->NumClassDefs();
2970 ++class_def_index) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002971 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
2972 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002973 h_klass.Assign(class_linker->FindClass(self, descriptor, h_class_loader));
Andreas Gampefa4333d2017-02-14 11:10:34 -08002974 if (h_klass == nullptr) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002975 std::cerr << "Warning: could not load " << descriptor << std::endl;
2976 continue;
2977 }
2978
2979 if (HasNoIMT(runtime, h_klass, pointer_size, &prepared)) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002980 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07002981 continue;
2982 }
2983
2984 ImTable* im_table = PrepareAndGetImTable(runtime, h_klass, pointer_size, &prepared);
2985 if (im_table == nullptr) {
2986 // Should not happen, but accept.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002987 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07002988 continue;
2989 }
2990
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002991 with_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07002992 for (size_t imt_index = 0; imt_index != ImTable::kSize; ++imt_index) {
2993 ArtMethod* ptr = im_table->Get(imt_index, pointer_size);
2994 if (ptr->IsRuntimeMethod()) {
2995 if (ptr->IsImtUnimplementedMethod()) {
2996 histogram[0]++;
2997 } else {
2998 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
2999 histogram[current_table->NumEntries(pointer_size)]++;
3000 }
3001 } else {
3002 histogram[1]++;
3003 }
3004 }
3005 }
3006 }
3007
3008 std::cerr << "IMT stats:"
3009 << std::endl << std::endl;
3010
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003011 std::cerr << " " << with_imt << " classes with IMT."
Andreas Gampe9fded872016-09-25 16:08:35 -07003012 << std::endl << std::endl;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003013 std::cerr << " " << without_imt << " classes without IMT (or copy from Object)."
Andreas Gampe9fded872016-09-25 16:08:35 -07003014 << std::endl << std::endl;
3015
3016 double sum_one = 0;
3017 size_t count_one = 0;
3018
3019 std::cerr << " " << "IMT histogram" << std::endl;
3020 for (auto& bucket : histogram) {
3021 std::cerr << " " << bucket.first << " " << bucket.second << std::endl;
3022 if (bucket.first > 0) {
3023 sum_one += bucket.second * bucket.first;
3024 count_one += bucket.second;
3025 }
3026 }
3027
3028 double count_zero = count_one + histogram[0];
3029 std::cerr << " Stats:" << std::endl;
3030 std::cerr << " Average depth (including empty): " << (sum_one / count_zero) << std::endl;
3031 std::cerr << " Average depth (excluding empty): " << (sum_one / count_one) << std::endl;
3032
3033 return true;
3034 }
3035
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003036 // Return whether the given class has no IMT (or the one shared with java.lang.Object).
Andreas Gampe9fded872016-09-25 16:08:35 -07003037 static bool HasNoIMT(Runtime* runtime,
3038 Handle<mirror::Class> klass,
3039 const PointerSize pointer_size,
3040 std::unordered_set<std::string>* prepared)
3041 REQUIRES_SHARED(Locks::mutator_lock_) {
3042 if (klass->IsObjectClass() || !klass->ShouldHaveImt()) {
3043 return true;
3044 }
3045
3046 if (klass->GetImt(pointer_size) == nullptr) {
3047 PrepareClass(runtime, klass, prepared);
3048 }
3049
3050 mirror::Class* object_class = mirror::Class::GetJavaLangClass()->GetSuperClass();
3051 DCHECK(object_class->IsObjectClass());
3052
3053 bool result = klass->GetImt(pointer_size) == object_class->GetImt(pointer_size);
3054
Mathieu Chartier6beced42016-11-15 15:51:31 -08003055 if (klass->GetIfTable()->Count() == 0) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003056 DCHECK(result);
3057 }
3058
3059 return result;
3060 }
3061
3062 static void PrintTable(ImtConflictTable* table, PointerSize pointer_size)
3063 REQUIRES_SHARED(Locks::mutator_lock_) {
3064 if (table == nullptr) {
3065 std::cerr << " <No IMT?>" << std::endl;
3066 return;
3067 }
3068 size_t table_index = 0;
3069 for (;;) {
3070 ArtMethod* ptr = table->GetInterfaceMethod(table_index, pointer_size);
3071 if (ptr == nullptr) {
3072 return;
3073 }
3074 table_index++;
David Sehr709b0702016-10-13 09:12:37 -07003075 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003076 }
3077 }
3078
3079 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3080 Thread* self,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003081 Handle<mirror::ClassLoader> h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003082 const std::string& class_name,
3083 const PointerSize pointer_size,
3084 mirror::Class** klass_out,
3085 std::unordered_set<std::string>* prepared)
3086 REQUIRES_SHARED(Locks::mutator_lock_) {
3087 if (class_name.empty()) {
3088 return nullptr;
3089 }
3090
3091 std::string descriptor;
3092 if (class_name[0] == 'L') {
3093 descriptor = class_name;
3094 } else {
3095 descriptor = DotToDescriptor(class_name.c_str());
3096 }
3097
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003098 mirror::Class* klass = runtime->GetClassLinker()->FindClass(self, descriptor.c_str(), h_loader);
Andreas Gampe9fded872016-09-25 16:08:35 -07003099
3100 if (klass == nullptr) {
3101 self->ClearException();
3102 std::cerr << "Did not find " << class_name << std::endl;
3103 *klass_out = nullptr;
3104 return nullptr;
3105 }
3106
3107 StackHandleScope<1> scope(Thread::Current());
3108 Handle<mirror::Class> h_klass = scope.NewHandle<mirror::Class>(klass);
3109
3110 ImTable* ret = PrepareAndGetImTable(runtime, h_klass, pointer_size, prepared);
3111 *klass_out = h_klass.Get();
3112 return ret;
3113 }
3114
3115 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3116 Handle<mirror::Class> h_klass,
3117 const PointerSize pointer_size,
3118 std::unordered_set<std::string>* prepared)
3119 REQUIRES_SHARED(Locks::mutator_lock_) {
3120 PrepareClass(runtime, h_klass, prepared);
3121 return h_klass->GetImt(pointer_size);
3122 }
3123
3124 static void DumpIMTForClass(Runtime* runtime,
3125 const std::string& class_name,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003126 Handle<mirror::ClassLoader> h_loader,
3127 std::unordered_set<std::string>* prepared)
3128 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003129 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
3130 mirror::Class* klass;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003131 ImTable* imt = PrepareAndGetImTable(runtime,
3132 Thread::Current(),
3133 h_loader,
3134 class_name,
3135 pointer_size,
3136 &klass,
3137 prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07003138 if (imt == nullptr) {
3139 return;
3140 }
3141
3142 std::cerr << class_name << std::endl << " IMT:" << std::endl;
3143 for (size_t index = 0; index < ImTable::kSize; ++index) {
3144 std::cerr << " " << index << ":" << std::endl;
3145 ArtMethod* ptr = imt->Get(index, pointer_size);
3146 if (ptr->IsRuntimeMethod()) {
3147 if (ptr->IsImtUnimplementedMethod()) {
3148 std::cerr << " <empty>" << std::endl;
3149 } else {
3150 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3151 PrintTable(current_table, pointer_size);
3152 }
3153 } else {
David Sehr709b0702016-10-13 09:12:37 -07003154 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003155 }
3156 }
3157
3158 std::cerr << " Interfaces:" << std::endl;
3159 // Run through iftable, find methods that slot here, see if they fit.
3160 mirror::IfTable* if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003161 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
3162 mirror::Class* iface = if_table->GetInterface(i);
3163 std::string iface_name;
3164 std::cerr << " " << iface->GetDescriptor(&iface_name) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003165
Mathieu Chartier6beced42016-11-15 15:51:31 -08003166 for (ArtMethod& iface_method : iface->GetVirtualMethods(pointer_size)) {
3167 uint32_t class_hash, name_hash, signature_hash;
3168 ImTable::GetImtHashComponents(&iface_method, &class_hash, &name_hash, &signature_hash);
3169 uint32_t imt_slot = ImTable::GetImtIndex(&iface_method);
3170 std::cerr << " " << iface_method.PrettyMethod(true)
3171 << " slot=" << imt_slot
3172 << std::hex
3173 << " class_hash=0x" << class_hash
3174 << " name_hash=0x" << name_hash
3175 << " signature_hash=0x" << signature_hash
3176 << std::dec
3177 << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003178 }
3179 }
3180 }
3181
3182 static void DumpIMTForMethod(Runtime* runtime,
3183 const std::string& class_name,
3184 const std::string& method,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003185 Handle<mirror::ClassLoader> h_loader,
3186 std::unordered_set<std::string>* prepared)
3187 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003188 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
3189 mirror::Class* klass;
3190 ImTable* imt = PrepareAndGetImTable(runtime,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003191 Thread::Current(),
3192 h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003193 class_name,
3194 pointer_size,
3195 &klass,
3196 prepared);
3197 if (imt == nullptr) {
3198 return;
3199 }
3200
3201 std::cerr << class_name << " <" << method << ">" << std::endl;
3202 for (size_t index = 0; index < ImTable::kSize; ++index) {
3203 ArtMethod* ptr = imt->Get(index, pointer_size);
3204 if (ptr->IsRuntimeMethod()) {
3205 if (ptr->IsImtUnimplementedMethod()) {
3206 continue;
3207 }
3208
3209 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3210 if (current_table == nullptr) {
3211 continue;
3212 }
3213
3214 size_t table_index = 0;
3215 for (;;) {
3216 ArtMethod* ptr2 = current_table->GetInterfaceMethod(table_index, pointer_size);
3217 if (ptr2 == nullptr) {
3218 break;
3219 }
3220 table_index++;
3221
David Sehr709b0702016-10-13 09:12:37 -07003222 std::string p_name = ptr2->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003223 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003224 std::cerr << " Slot "
3225 << index
3226 << " ("
3227 << current_table->NumEntries(pointer_size)
3228 << ")"
3229 << std::endl;
3230 PrintTable(current_table, pointer_size);
3231 return;
3232 }
3233 }
3234 } else {
David Sehr709b0702016-10-13 09:12:37 -07003235 std::string p_name = ptr->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003236 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003237 std::cerr << " Slot " << index << " (1)" << std::endl;
3238 std::cerr << " " << p_name << std::endl;
3239 } else {
3240 // Run through iftable, find methods that slot here, see if they fit.
3241 mirror::IfTable* if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003242 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
3243 mirror::Class* iface = if_table->GetInterface(i);
3244 size_t num_methods = iface->NumDeclaredVirtualMethods();
3245 if (num_methods > 0) {
3246 for (ArtMethod& iface_method : iface->GetMethods(pointer_size)) {
3247 if (ImTable::GetImtIndex(&iface_method) == index) {
3248 std::string i_name = iface_method.PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003249 if (android::base::StartsWith(i_name, method.c_str())) {
Mathieu Chartier6beced42016-11-15 15:51:31 -08003250 std::cerr << " Slot " << index << " (1)" << std::endl;
3251 std::cerr << " " << p_name << " (" << i_name << ")" << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003252 }
3253 }
3254 }
3255 }
3256 }
3257 }
3258 }
3259 }
3260 }
3261
3262 // Read lines from the given stream, dropping comments and empty lines
3263 static std::vector<std::string> ReadCommentedInputStream(std::istream& in_stream) {
3264 std::vector<std::string> output;
3265 while (in_stream.good()) {
3266 std::string dot;
3267 std::getline(in_stream, dot);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003268 if (android::base::StartsWith(dot, "#") || dot.empty()) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003269 continue;
3270 }
3271 output.push_back(dot);
3272 }
3273 return output;
3274 }
3275
3276 // Read lines from the given file, dropping comments and empty lines.
3277 static std::vector<std::string> ReadCommentedInputFromFile(const std::string& input_filename) {
3278 std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
3279 if (input_file.get() == nullptr) {
3280 LOG(ERROR) << "Failed to open input file " << input_filename;
3281 return std::vector<std::string>();
3282 }
3283 std::vector<std::string> result = ReadCommentedInputStream(*input_file);
3284 input_file->close();
3285 return result;
3286 }
3287
3288 // Prepare a class, i.e., ensure it has a filled IMT. Will do so recursively for superclasses,
3289 // and note in the given set that the work was done.
3290 static void PrepareClass(Runtime* runtime,
3291 Handle<mirror::Class> h_klass,
3292 std::unordered_set<std::string>* done)
3293 REQUIRES_SHARED(Locks::mutator_lock_) {
3294 if (!h_klass->ShouldHaveImt()) {
3295 return;
3296 }
3297
3298 std::string name;
3299 name = h_klass->GetDescriptor(&name);
3300
3301 if (done->find(name) != done->end()) {
3302 return;
3303 }
3304 done->insert(name);
3305
3306 if (h_klass->HasSuperClass()) {
3307 StackHandleScope<1> h(Thread::Current());
3308 PrepareClass(runtime, h.NewHandle<mirror::Class>(h_klass->GetSuperClass()), done);
3309 }
3310
3311 if (!h_klass->IsTemp()) {
3312 runtime->GetClassLinker()->FillIMTAndConflictTables(h_klass.Get());
3313 }
3314 }
3315};
3316
Igor Murashkin37743352014-11-13 14:38:00 -08003317struct OatdumpArgs : public CmdlineArgs {
3318 protected:
3319 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003320
Igor Murashkin37743352014-11-13 14:38:00 -08003321 virtual ParseStatus ParseCustom(const StringPiece& option,
3322 std::string* error_msg) OVERRIDE {
3323 {
3324 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
3325 if (base_parse != kParseUnknownArgument) {
3326 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003327 }
3328 }
3329
Igor Murashkin37743352014-11-13 14:38:00 -08003330 if (option.starts_with("--oat-file=")) {
3331 oat_filename_ = option.substr(strlen("--oat-file=")).data();
3332 } else if (option.starts_with("--image=")) {
3333 image_location_ = option.substr(strlen("--image=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003334 } else if (option == "--no-dump:vmap") {
3335 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01003336 } else if (option =="--dump:code_info_stack_maps") {
3337 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003338 } else if (option == "--no-disassemble") {
3339 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003340 } else if (option =="--header-only") {
3341 dump_header_only_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003342 } else if (option.starts_with("--symbolize=")) {
3343 oat_filename_ = option.substr(strlen("--symbolize=")).data();
3344 symbolize_ = true;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003345 } else if (option.starts_with("--only-keep-debug")) {
3346 only_keep_debug_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003347 } else if (option.starts_with("--class-filter=")) {
3348 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003349 } else if (option.starts_with("--method-filter=")) {
3350 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003351 } else if (option.starts_with("--list-classes")) {
3352 list_classes_ = true;
3353 } else if (option.starts_with("--list-methods")) {
3354 list_methods_ = true;
3355 } else if (option.starts_with("--export-dex-to=")) {
3356 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
3357 } else if (option.starts_with("--addr2instr=")) {
3358 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
3359 *error_msg = "Address conversion failed";
3360 return kParseError;
3361 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003362 } else if (option.starts_with("--app-image=")) {
3363 app_image_ = option.substr(strlen("--app-image=")).data();
3364 } else if (option.starts_with("--app-oat=")) {
3365 app_oat_ = option.substr(strlen("--app-oat=")).data();
Andreas Gampe9fded872016-09-25 16:08:35 -07003366 } else if (option.starts_with("--dump-imt=")) {
3367 imt_dump_ = option.substr(strlen("--dump-imt=")).data();
3368 } else if (option == "--dump-imt-stats") {
3369 imt_stat_dump_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003370 } else {
3371 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003372 }
3373
Igor Murashkin37743352014-11-13 14:38:00 -08003374 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003375 }
3376
Igor Murashkin37743352014-11-13 14:38:00 -08003377 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
3378 // Infer boot image location from the image location if possible.
3379 if (boot_image_location_ == nullptr) {
3380 boot_image_location_ = image_location_;
3381 }
3382
3383 // Perform the parent checks.
3384 ParseStatus parent_checks = Base::ParseChecks(error_msg);
3385 if (parent_checks != kParseOk) {
3386 return parent_checks;
3387 }
3388
3389 // Perform our own checks.
3390 if (image_location_ == nullptr && oat_filename_ == nullptr) {
3391 *error_msg = "Either --image or --oat-file must be specified";
3392 return kParseError;
3393 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
3394 *error_msg = "Either --image or --oat-file must be specified but not both";
3395 return kParseError;
3396 }
3397
3398 return kParseOk;
3399 }
3400
3401 virtual std::string GetUsage() const {
3402 std::string usage;
3403
3404 usage +=
3405 "Usage: oatdump [options] ...\n"
3406 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
3407 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
3408 "\n"
3409 // Either oat-file or image is required.
3410 " --oat-file=<file.oat>: specifies an input oat filename.\n"
3411 " Example: --oat-file=/system/framework/boot.oat\n"
3412 "\n"
3413 " --image=<file.art>: specifies an input image location.\n"
3414 " Example: --image=/system/framework/boot.art\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003415 "\n"
3416 " --app-image=<file.art>: specifies an input app image. Must also have a specified\n"
3417 " boot image and app oat file.\n"
3418 " Example: --app-image=app.art\n"
3419 "\n"
3420 " --app-oat=<file.odex>: specifies an input app oat.\n"
3421 " Example: --app-oat=app.odex\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003422 "\n";
3423
3424 usage += Base::GetUsage();
3425
3426 usage += // Optional.
Igor Murashkin37743352014-11-13 14:38:00 -08003427 " --no-dump:vmap may be used to disable vmap dumping.\n"
3428 " Example: --no-dump:vmap\n"
3429 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01003430 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
3431 " Example: --dump:code_info_stack_maps\n"
3432 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003433 " --no-disassemble may be used to disable disassembly.\n"
3434 " Example: --no-disassemble\n"
3435 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00003436 " --header-only may be used to print only the oat header.\n"
3437 " Example: --header-only\n"
3438 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003439 " --list-classes may be used to list target file classes (can be used with filters).\n"
3440 " Example: --list-classes\n"
3441 " Example: --list-classes --class-filter=com.example.foo\n"
3442 "\n"
3443 " --list-methods may be used to list target file methods (can be used with filters).\n"
3444 " Example: --list-methods\n"
3445 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
3446 "\n"
3447 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
3448 " Example: --symbolize=/system/framework/boot.oat\n"
3449 "\n"
David Srbecky2fdd03c2016-03-10 15:32:37 +00003450 " --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
3451 " .rodata and .text sections are omitted in the output file to save space.\n"
3452 " Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
3453 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003454 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
3455 " Example: --class-filter=com.example.foo\n"
3456 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003457 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
3458 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003459 "\n"
3460 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
3461 " Example: --export-dex-to=/data/local/tmp\n"
3462 "\n"
3463 " --addr2instr=<address>: output matching method disassembled code from relative\n"
3464 " address (e.g. PC from crash dump)\n"
3465 " Example: --addr2instr=0x00001a3b\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003466 "\n"
3467 " --dump-imt=<file.txt>: output IMT collisions (if any) for the given receiver\n"
3468 " types and interface methods in the given file. The file\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003469 " is read line-wise, where each line should either be a class\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003470 " name or descriptor, or a class name/descriptor and a prefix\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003471 " of a complete method name (separated by a whitespace).\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003472 " Example: --dump-imt=imt.txt\n"
3473 "\n"
3474 " --dump-imt-stats: output IMT statistics for the given boot image\n"
3475 " Example: --dump-imt-stats"
Igor Murashkin37743352014-11-13 14:38:00 -08003476 "\n";
3477
3478 return usage;
3479 }
3480
3481 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07003482 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003483 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00003484 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07003485 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003486 std::string elf_filename_prefix_;
Andreas Gampe9fded872016-09-25 16:08:35 -07003487 std::string imt_dump_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003488 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01003489 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003490 bool disassemble_code_ = true;
3491 bool symbolize_ = false;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003492 bool only_keep_debug_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003493 bool list_classes_ = false;
3494 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003495 bool dump_header_only_ = false;
Andreas Gampe9fded872016-09-25 16:08:35 -07003496 bool imt_stat_dump_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003497 uint32_t addr2instr_ = 0;
3498 const char* export_dex_location_ = nullptr;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003499 const char* app_image_ = nullptr;
3500 const char* app_oat_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003501};
3502
Igor Murashkin37743352014-11-13 14:38:00 -08003503struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
3504 virtual bool NeedsRuntime() OVERRIDE {
3505 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003506
Igor Murashkin37743352014-11-13 14:38:00 -08003507 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
3508 bool absolute_addresses = (args_->oat_filename_ == nullptr);
3509
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003510 oat_dumper_options_.reset(new OatDumperOptions(
Igor Murashkin37743352014-11-13 14:38:00 -08003511 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01003512 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08003513 args_->disassemble_code_,
3514 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003515 args_->class_filter_,
3516 args_->method_filter_,
3517 args_->list_classes_,
3518 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00003519 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003520 args_->export_dex_location_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003521 args_->app_image_,
3522 args_->app_oat_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003523 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08003524
Andreas Gampe9fded872016-09-25 16:08:35 -07003525 return (args_->boot_image_location_ != nullptr ||
3526 args_->image_location_ != nullptr ||
3527 !args_->imt_dump_.empty()) &&
Igor Murashkin37743352014-11-13 14:38:00 -08003528 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003529 }
3530
Igor Murashkin37743352014-11-13 14:38:00 -08003531 virtual bool ExecuteWithoutRuntime() OVERRIDE {
3532 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08003533 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003534
Mathieu Chartierd424d082014-10-15 10:31:46 -07003535 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08003536
Andreas Gampec24f3992014-12-17 20:40:11 -08003537 if (args_->symbolize_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00003538 // ELF has special kind of section called SHT_NOBITS which allows us to create
3539 // sections which exist but their data is omitted from the ELF file to save space.
3540 // This is what "strip --only-keep-debug" does when it creates separate ELF file
3541 // with only debug data. We use it in similar way to exclude .rodata and .text.
3542 bool no_bits = args_->only_keep_debug_;
3543 return SymbolizeOat(args_->oat_filename_, args_->output_name_, no_bits) == EXIT_SUCCESS;
Andreas Gampec24f3992014-12-17 20:40:11 -08003544 } else {
3545 return DumpOat(nullptr,
3546 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003547 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08003548 args_->os_) == EXIT_SUCCESS;
3549 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07003550 }
3551
Igor Murashkin37743352014-11-13 14:38:00 -08003552 virtual bool ExecuteWithRuntime(Runtime* runtime) {
3553 CHECK(args_ != nullptr);
3554
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003555 if (!args_->imt_dump_.empty() || args_->imt_stat_dump_) {
3556 return IMTDumper::Dump(runtime,
3557 args_->imt_dump_,
3558 args_->imt_stat_dump_,
3559 args_->oat_filename_);
Andreas Gampe9fded872016-09-25 16:08:35 -07003560 }
3561
Igor Murashkin37743352014-11-13 14:38:00 -08003562 if (args_->oat_filename_ != nullptr) {
3563 return DumpOat(runtime,
3564 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003565 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08003566 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003567 }
Igor Murashkin37743352014-11-13 14:38:00 -08003568
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003569 return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003570 }
3571
Igor Murashkin37743352014-11-13 14:38:00 -08003572 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
3573};
Andreas Gampe00b25f32014-09-17 21:49:05 -07003574
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003575} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07003576
3577int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08003578 art::OatdumpMain main;
3579 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07003580}