blob: ae26e7dfcf690d94debc64490e091507c14d3669 [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"
Andreas Gampe0c183382017-07-13 22:26:24 -070045#include "gc/accounting/space_bitmap-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070046#include "gc/space/image_space.h"
47#include "gc/space/large_object_space.h"
48#include "gc/space/space-inl.h"
Mathieu Chartier4a26f172016-01-26 14:26:18 -080049#include "image-inl.h"
Andreas Gampe9fded872016-09-25 16:08:35 -070050#include "imtable-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080051#include "indenter.h"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -070052#include "interpreter/unstarted_runtime.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000053#include "linker/buffered_output_stream.h"
54#include "linker/file_output_stream.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080055#include "mirror/array-inl.h"
56#include "mirror/class-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010057#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080058#include "mirror/object-inl.h"
59#include "mirror/object_array-inl.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080060#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010061#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070062#include "oat_file_manager.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080063#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070064#include "safe_map.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070065#include "scoped_thread_state_change-inl.h"
Nicolas Geoffray9c055782016-07-14 09:24:30 +000066#include "ScopedLocalRef.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070067#include "stack.h"
Mathieu Chartierdc00f182016-07-14 10:10:44 -070068#include "stack_map.h"
69#include "string_reference.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080070#include "thread_list.h"
Andreas Gampe2ba88952016-04-29 17:52:07 -070071#include "type_lookup_table.h"
Nicolas Geoffray4acefd32016-10-24 13:14:58 +010072#include "vdex_file.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080073#include "verifier/method_verifier.h"
Nicolas Geoffraye70dd562016-10-30 21:03:35 +000074#include "verifier/verifier_deps.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070075#include "well_known_classes.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070076
Igor Murashkin37743352014-11-13 14:38:00 -080077#include <sys/stat.h>
78#include "cmdline.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070079
Igor Murashkin37743352014-11-13 14:38:00 -080080namespace art {
Brian Carlstrom78128a62011-09-15 17:21:19 -070081
Andreas Gampe46ee31b2016-12-14 10:11:49 -080082using android::base::StringPrintf;
83
Mathieu Chartiere401d142015-04-22 13:56:20 -070084const char* image_methods_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -080085 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -070086 "kImtConflictMethod",
Mathieu Chartier2d2621a2014-10-23 16:48:06 -070087 "kImtUnimplementedMethod",
Vladimir Markofd36f1f2016-08-03 18:49:58 +010088 "kSaveAllCalleeSavesMethod",
89 "kSaveRefsOnlyMethod",
90 "kSaveRefsAndArgsMethod",
Vladimir Marko952dbb12016-07-28 12:01:51 +010091 "kSaveEverythingMethod",
Mathieu Chartiere401d142015-04-22 13:56:20 -070092};
93
94const char* image_roots_descriptions_[] = {
Brian Carlstrom58ae9412011-10-04 00:56:06 -070095 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070096 "kClassRoots",
Vladimir Markoeca3eda2016-11-09 16:26:44 +000097 "kClassLoader",
Brian Carlstrom78128a62011-09-15 17:21:19 -070098};
99
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700100// Map is so that we don't allocate multiple dex files for the same OatDexFile.
101static std::map<const OatFile::OatDexFile*,
102 std::unique_ptr<const DexFile>> opened_dex_files;
103
104const DexFile* OpenDexFile(const OatFile::OatDexFile* oat_dex_file, std::string* error_msg) {
105 DCHECK(oat_dex_file != nullptr);
106 auto it = opened_dex_files.find(oat_dex_file);
107 if (it != opened_dex_files.end()) {
108 return it->second.get();
109 }
110 const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
111 opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
112 return ret;
113}
114
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800115template <typename ElfTypes>
David Srbeckybc90fd02015-04-22 19:40:27 +0100116class OatSymbolizer FINAL {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700117 public:
David Srbecky2fdd03c2016-03-10 15:32:37 +0000118 OatSymbolizer(const OatFile* oat_file, const std::string& output_name, bool no_bits) :
119 oat_file_(oat_file),
120 builder_(nullptr),
121 output_name_(output_name.empty() ? "symbolized.oat" : output_name),
122 no_bits_(no_bits) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700123 }
124
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700125 bool Symbolize() {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000126 const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800127 std::unique_ptr<const InstructionSetFeatures> features = InstructionSetFeatures::FromBitmap(
David Srbecky5d811202016-03-08 13:21:22 +0000128 isa, oat_file_->GetOatHeader().GetInstructionSetFeaturesBitmap());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000129
Andreas Gampe08c277c2017-04-26 22:22:15 -0700130 std::unique_ptr<File> elf_file(OS::CreateEmptyFile(output_name_.c_str()));
131 if (elf_file == nullptr) {
132 return false;
133 }
Andreas Gampe8bdda5a2017-06-08 15:30:36 -0700134 std::unique_ptr<BufferedOutputStream> output_stream =
135 std::make_unique<BufferedOutputStream>(std::make_unique<FileOutputStream>(elf_file.get()));
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800136 builder_.reset(new ElfBuilder<ElfTypes>(isa, features.get(), output_stream.get()));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000137
138 builder_->Start();
139
140 auto* rodata = builder_->GetRoData();
141 auto* text = builder_->GetText();
142 auto* bss = builder_->GetBss();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000143
David Srbecky6d8c8f02015-10-26 10:57:09 +0000144 const uint8_t* rodata_begin = oat_file_->Begin();
145 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
David Srbecky2fdd03c2016-03-10 15:32:37 +0000146 if (no_bits_) {
147 rodata->WriteNoBitsSection(rodata_size);
148 } else {
149 rodata->Start();
150 rodata->WriteFully(rodata_begin, rodata_size);
151 rodata->End();
152 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000153
David Srbecky6d8c8f02015-10-26 10:57:09 +0000154 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
155 const size_t text_size = oat_file_->End() - text_begin;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000156 if (no_bits_) {
157 text->WriteNoBitsSection(text_size);
158 } else {
159 text->Start();
160 text->WriteFully(text_begin, text_size);
161 text->End();
162 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000163
164 if (oat_file_->BssSize() != 0) {
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000165 bss->WriteNoBitsSection(oat_file_->BssSize());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000166 }
167
Douglas Leung316a2182015-09-17 15:26:25 -0700168 if (isa == kMips || isa == kMips64) {
169 builder_->WriteMIPSabiflagsSection();
170 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000171 builder_->PrepareDynamicSection(elf_file->GetPath(),
172 rodata_size,
173 text_size,
174 oat_file_->BssSize(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100175 oat_file_->BssMethodsOffset(),
Vladimir Markoaad75c62016-10-03 08:46:48 +0000176 oat_file_->BssRootsOffset());
Vladimir Marko944da602016-02-19 12:27:55 +0000177 builder_->WriteDynamicSection();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700178
David Srbecky5d950762016-03-07 20:47:29 +0000179 Walk();
180 for (const auto& trampoline : debug::MakeTrampolineInfos(oat_file_->GetOatHeader())) {
181 method_debug_infos_.push_back(trampoline);
182 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700183
David Srbecky5d950762016-03-07 20:47:29 +0000184 debug::WriteDebugInfo(builder_.get(),
185 ArrayRef<const debug::MethodDebugInfo>(method_debug_infos_),
186 dwarf::DW_DEBUG_FRAME_FORMAT,
187 true /* write_oat_patches */);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700188
David Srbecky6d8c8f02015-10-26 10:57:09 +0000189 builder_->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700190
Andreas Gampe08c277c2017-04-26 22:22:15 -0700191 bool ret_value = builder_->Good();
192
193 builder_.reset();
194 output_stream.reset();
195
196 if (elf_file->FlushCloseOrErase() != 0) {
197 return false;
198 }
199 elf_file.reset();
200
201 return ret_value;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700202 }
203
David Srbecky5d950762016-03-07 20:47:29 +0000204 void Walk() {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700205 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
206 for (size_t i = 0; i < oat_dex_files.size(); i++) {
207 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700208 CHECK(oat_dex_file != nullptr);
David Srbecky5d950762016-03-07 20:47:29 +0000209 WalkOatDexFile(oat_dex_file);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700210 }
211 }
212
David Srbecky5d950762016-03-07 20:47:29 +0000213 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700214 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700215 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
216 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700217 return;
218 }
219 for (size_t class_def_index = 0;
220 class_def_index < dex_file->NumClassDefs();
221 class_def_index++) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700222 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
223 OatClassType type = oat_class.GetType();
224 switch (type) {
225 case kOatClassAllCompiled:
226 case kOatClassSomeCompiled:
David Srbecky5d950762016-03-07 20:47:29 +0000227 WalkOatClass(oat_class, *dex_file, class_def_index);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700228 break;
229
230 case kOatClassNoneCompiled:
231 case kOatClassMax:
232 // Ignore.
233 break;
234 }
235 }
236 }
237
David Srbecky5d950762016-03-07 20:47:29 +0000238 void WalkOatClass(const OatFile::OatClass& oat_class,
239 const DexFile& dex_file,
240 uint32_t class_def_index) {
241 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700242 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700243 if (class_data == nullptr) { // empty class such as a marker interface?
244 return;
245 }
246 // Note: even if this is an interface or a native class, we still have to walk it, as there
247 // might be a static initializer.
248 ClassDataItemIterator it(dex_file, class_data);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700249 uint32_t class_method_idx = 0;
Mathieu Chartiere17cf242017-06-19 11:05:51 -0700250 it.SkipAllFields();
David Srbecky5d950762016-03-07 20:47:29 +0000251 for (; it.HasNextDirectMethod() || it.HasNextVirtualMethod(); it.Next()) {
252 WalkOatMethod(oat_class.GetOatMethod(class_method_idx++),
253 dex_file,
254 class_def_index,
255 it.GetMemberIndex(),
256 it.GetMethodCodeItem(),
257 it.GetMethodAccessFlags());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700258 }
259 DCHECK(!it.HasNext());
260 }
261
David Srbecky5d950762016-03-07 20:47:29 +0000262 void WalkOatMethod(const OatFile::OatMethod& oat_method,
263 const DexFile& dex_file,
264 uint32_t class_def_index,
265 uint32_t dex_method_index,
266 const DexFile::CodeItem* code_item,
267 uint32_t method_access_flags) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700268 if ((method_access_flags & kAccAbstract) != 0) {
269 // Abstract method, no code.
270 return;
271 }
David Srbecky5d950762016-03-07 20:47:29 +0000272 const OatHeader& oat_header = oat_file_->GetOatHeader();
273 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
274 if (method_header == nullptr || method_header->GetCodeSize() == 0) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700275 // No code.
276 return;
277 }
278
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100279 uint32_t entry_point = oat_method.GetCodeOffset() - oat_header.GetExecutableOffset();
280 // Clear Thumb2 bit.
281 const void* code_address = EntryPointToCodePointer(reinterpret_cast<void*>(entry_point));
282
David Srbecky5d950762016-03-07 20:47:29 +0000283 debug::MethodDebugInfo info = debug::MethodDebugInfo();
284 info.trampoline_name = nullptr;
285 info.dex_file = &dex_file;
286 info.class_def_index = class_def_index;
287 info.dex_method_index = dex_method_index;
288 info.access_flags = method_access_flags;
289 info.code_item = code_item;
290 info.isa = oat_header.GetInstructionSet();
291 info.deduped = !seen_offsets_.insert(oat_method.GetCodeOffset()).second;
292 info.is_native_debuggable = oat_header.IsNativeDebuggable();
293 info.is_optimized = method_header->IsOptimized();
294 info.is_code_address_text_relative = true;
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100295 info.code_address = reinterpret_cast<uintptr_t>(code_address);
David Srbecky5d950762016-03-07 20:47:29 +0000296 info.code_size = method_header->GetCodeSize();
297 info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
298 info.code_info = info.is_optimized ? method_header->GetOptimizedCodeInfoPtr() : nullptr;
299 info.cfi = ArrayRef<uint8_t>();
300 method_debug_infos_.push_back(info);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700301 }
302
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700303 private:
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700304 const OatFile* oat_file_;
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800305 std::unique_ptr<ElfBuilder<ElfTypes> > builder_;
David Srbecky5d950762016-03-07 20:47:29 +0000306 std::vector<debug::MethodDebugInfo> method_debug_infos_;
307 std::unordered_set<uint32_t> seen_offsets_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700308 const std::string output_name_;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000309 bool no_bits_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700310};
311
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700312class OatDumperOptions {
313 public:
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100314 OatDumperOptions(bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100315 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700316 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700317 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800318 const char* class_filter,
319 const char* method_filter,
320 bool list_classes,
321 bool list_methods,
David Brazdilc03d7b62016-03-02 12:18:03 +0000322 bool dump_header_only,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800323 const char* export_dex_location,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800324 const char* app_image,
325 const char* app_oat,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800326 uint32_t addr2instr)
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100327 : dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100328 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700329 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700330 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800331 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000332 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800333 list_classes_(list_classes),
334 list_methods_(list_methods),
David Brazdilc03d7b62016-03-02 12:18:03 +0000335 dump_header_only_(dump_header_only),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800336 export_dex_location_(export_dex_location),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800337 app_image_(app_image),
338 app_oat_(app_oat),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800339 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800340 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700341
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700342 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100343 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700344 const bool disassemble_code_;
345 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800346 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000347 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800348 const bool list_classes_;
349 const bool list_methods_;
David Brazdilc03d7b62016-03-02 12:18:03 +0000350 const bool dump_header_only_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800351 const char* const export_dex_location_;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800352 const char* const app_image_;
353 const char* const app_oat_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800354 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700355 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700356};
357
Elliott Hughese3c845c2012-02-28 17:23:01 -0800358class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700359 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100360 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000361 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800362 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700363 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800364 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800365 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
366 disassembler_(Disassembler::Create(instruction_set_,
Andreas Gampe372f3a32016-08-19 10:49:06 -0700367 new DisassemblerOptions(
368 options_.absolute_addresses_,
369 oat_file.Begin(),
370 oat_file.End(),
371 true /* can_read_literals_ */,
372 Is64BitInstructionSet(instruction_set_)
373 ? &Thread::DumpThreadOffset<PointerSize::k64>
374 : &Thread::DumpThreadOffset<PointerSize::k32>))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800375 CHECK(options_.class_loader_ != nullptr);
376 CHECK(options_.class_filter_ != nullptr);
377 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800378 AddAllOffsets();
379 }
380
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700381 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700382 delete disassembler_;
383 }
384
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800385 InstructionSet GetInstructionSet() {
386 return instruction_set_;
387 }
388
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700389 bool Dump(std::ostream& os) {
390 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800391 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700392
393 os << "MAGIC:\n";
394 os << oat_header.GetMagic() << "\n\n";
395
Jeff Hao4b07a6e2016-01-15 12:50:44 -0800396 os << "LOCATION:\n";
397 os << oat_file_.GetLocation() << "\n\n";
398
Brian Carlstromaded5f72011-10-07 17:15:04 -0700399 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800400 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700401
Elliott Hughesa72ec822012-03-05 17:12:22 -0800402 os << "INSTRUCTION SET:\n";
403 os << oat_header.GetInstructionSet() << "\n\n";
404
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700405 {
406 std::unique_ptr<const InstructionSetFeatures> features(
407 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
408 oat_header.GetInstructionSetFeaturesBitmap()));
409 os << "INSTRUCTION SET FEATURES:\n";
410 os << features->GetFeatureString() << "\n\n";
411 }
Dave Allison70202782013-10-22 17:52:19 -0700412
Brian Carlstromaded5f72011-10-07 17:15:04 -0700413 os << "DEX FILE COUNT:\n";
414 os << oat_header.GetDexFileCount() << "\n\n";
415
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800416#define DUMP_OAT_HEADER_OFFSET(label, offset) \
417 os << label " OFFSET:\n"; \
418 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800419 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800420 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
421 } \
422 os << StringPrintf("\n\n");
423
424 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
425 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
426 GetInterpreterToInterpreterBridgeOffset);
427 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
428 GetInterpreterToCompiledCodeBridgeOffset);
429 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
430 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800431 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
432 GetQuickGenericJniTrampolineOffset);
433 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
434 GetQuickImtConflictTrampolineOffset);
435 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
436 GetQuickResolutionTrampolineOffset);
437 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
438 GetQuickToInterpreterBridgeOffset);
439#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700440
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700441 os << "IMAGE PATCH DELTA:\n";
442 os << StringPrintf("%d (0x%08x)\n\n",
443 oat_header.GetImagePatchDelta(),
444 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700445
Brian Carlstrom28db0122012-10-18 16:20:41 -0700446 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
447 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
448
449 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800450 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700451
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700452 // Print the key-value store.
453 {
454 os << "KEY VALUE STORE:\n";
455 size_t index = 0;
456 const char* key;
457 const char* value;
458 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
459 os << key << " = " << value << "\n";
460 index++;
461 }
462 os << "\n";
463 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700464
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800465 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700466 os << "BEGIN:\n";
467 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700468
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700469 os << "END:\n";
470 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
471 }
472
473 os << "SIZE:\n";
474 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700475
476 os << std::flush;
477
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800478 // If set, adjust relative address to be searched
479 if (options_.addr2instr_ != 0) {
480 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
481 os << "SEARCH ADDRESS (executable offset + input):\n";
482 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
483 }
484
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700485 // Dumping the dex file overview is compact enough to do even if header only.
486 DexFileData cumulative;
487 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
488 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
489 CHECK(oat_dex_file != nullptr);
490 std::string error_msg;
491 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
492 if (dex_file == nullptr) {
493 os << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation() << "': "
494 << error_msg;
495 continue;
496 }
497 DexFileData data(*dex_file);
498 os << "Dex file data for " << dex_file->GetLocation() << "\n";
499 data.Dump(os);
500 os << "\n";
501 cumulative.Add(data);
502 }
503 os << "Cumulative dex file data\n";
504 cumulative.Dump(os);
505 os << "\n";
506
David Brazdilc03d7b62016-03-02 12:18:03 +0000507 if (!options_.dump_header_only_) {
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000508 VariableIndentationOutputStream vios(&os);
509 VdexFile::Header vdex_header = oat_file_.GetVdexFile()->GetHeader();
510 if (vdex_header.IsValid()) {
511 std::string error_msg;
512 std::vector<const DexFile*> dex_files;
513 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
514 const DexFile* dex_file = OpenDexFile(oat_dex_files_[i], &error_msg);
515 if (dex_file == nullptr) {
516 os << "Error opening dex file: " << error_msg << std::endl;
517 return false;
518 }
519 dex_files.push_back(dex_file);
520 }
521 verifier::VerifierDeps deps(dex_files, oat_file_.GetVdexFile()->GetVerifierDepsData());
522 deps.Dump(&vios);
523 } else {
524 os << "UNRECOGNIZED vdex file, magic "
525 << vdex_header.GetMagic()
526 << ", version "
527 << vdex_header.GetVersion()
528 << "\n";
529 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000530 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
531 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
532 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800533
David Brazdilc03d7b62016-03-02 12:18:03 +0000534 // If file export selected skip file analysis
535 if (options_.export_dex_location_) {
536 if (!ExportDexFile(os, *oat_dex_file)) {
537 success = false;
538 }
539 } else {
540 if (!DumpOatDexFile(os, *oat_dex_file)) {
541 success = false;
542 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800543 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700544 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700545 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000546
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800547 {
548 os << "OAT FILE STATS:\n";
549 VariableIndentationOutputStream vios(&os);
550 stats_.Dump(vios);
551 }
552
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700553 os << std::flush;
554 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700555 }
556
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800557 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700558 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
559 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800560 return 0; // Address not in oat file
561 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800562 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
563 reinterpret_cast<uintptr_t>(oat_file_.Begin());
564 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800565 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800566 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800567 return end_offset - begin_offset;
568 }
569
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800570 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700571 return oat_file_.GetOatHeader().GetInstructionSet();
572 }
573
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700574 const void* GetQuickOatCode(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800575 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
576 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700577 CHECK(oat_dex_file != nullptr);
578 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700579 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
580 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700581 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
582 << "': " << error_msg;
583 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800584 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700585 const DexFile::ClassDef* class_def =
David Sehr9aa352e2016-09-15 18:13:52 -0700586 OatDexFile::FindClassDef(*dex_file, descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700587 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700588 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100589 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800590 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100591 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800592 }
593 }
594 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700595 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800596 }
597
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800598 struct Stats {
599 enum ByteKind {
600 kByteKindCode,
601 kByteKindQuickMethodHeader,
602 kByteKindCodeInfoLocationCatalog,
603 kByteKindCodeInfoDexRegisterMap,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800604 kByteKindCodeInfoEncoding,
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800605 kByteKindCodeInfoInvokeInfo,
David Srbecky45aa5982016-03-18 02:15:09 +0000606 kByteKindCodeInfoStackMasks,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800607 kByteKindCodeInfoRegisterMasks,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800608 kByteKindStackMapNativePc,
609 kByteKindStackMapDexPc,
610 kByteKindStackMapDexRegisterMap,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800611 kByteKindStackMapInlineInfoIndex,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800612 kByteKindStackMapRegisterMaskIndex,
David Srbecky45aa5982016-03-18 02:15:09 +0000613 kByteKindStackMapStackMaskIndex,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700614 kByteKindInlineInfoMethodIndexIdx,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800615 kByteKindInlineInfoDexPc,
616 kByteKindInlineInfoExtraData,
617 kByteKindInlineInfoDexRegisterMap,
618 kByteKindInlineInfoIsLast,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800619 kByteKindCount,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800620 // Special ranges for std::accumulate convenience.
621 kByteKindStackMapFirst = kByteKindStackMapNativePc,
David Srbecky45aa5982016-03-18 02:15:09 +0000622 kByteKindStackMapLast = kByteKindStackMapStackMaskIndex,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700623 kByteKindInlineInfoFirst = kByteKindInlineInfoMethodIndexIdx,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800624 kByteKindInlineInfoLast = kByteKindInlineInfoIsLast,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800625 };
626 int64_t bits[kByteKindCount] = {};
627 // Since code has deduplication, seen tracks already seen pointers to avoid double counting
628 // deduplicated code and tables.
629 std::unordered_set<const void*> seen;
630
631 // Returns true if it was newly added.
632 bool AddBitsIfUnique(ByteKind kind, int64_t count, const void* address) {
633 if (seen.insert(address).second == true) {
634 // True means the address was not already in the set.
635 AddBits(kind, count);
636 return true;
637 }
638 return false;
639 }
640
641 void AddBits(ByteKind kind, int64_t count) {
642 bits[kind] += count;
643 }
644
645 void Dump(VariableIndentationOutputStream& os) {
646 const int64_t sum = std::accumulate(bits, bits + kByteKindCount, 0u);
647 os.Stream() << "Dumping cumulative use of " << sum / kBitsPerByte << " accounted bytes\n";
648 if (sum > 0) {
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800649 Dump(os, "Code ", bits[kByteKindCode], sum);
650 Dump(os, "QuickMethodHeader ", bits[kByteKindQuickMethodHeader], sum);
651 Dump(os, "CodeInfoEncoding ", bits[kByteKindCodeInfoEncoding], sum);
652 Dump(os, "CodeInfoLocationCatalog ", bits[kByteKindCodeInfoLocationCatalog], sum);
653 Dump(os, "CodeInfoDexRegisterMap ", bits[kByteKindCodeInfoDexRegisterMap], sum);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800654 Dump(os, "CodeInfoStackMasks ", bits[kByteKindCodeInfoStackMasks], sum);
655 Dump(os, "CodeInfoRegisterMasks ", bits[kByteKindCodeInfoRegisterMasks], sum);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800656 Dump(os, "CodeInfoInvokeInfo ", bits[kByteKindCodeInfoInvokeInfo], sum);
Mathieu Chartier1e083792017-02-08 13:30:04 -0800657 // Stack map section.
658 const int64_t stack_map_bits = std::accumulate(bits + kByteKindStackMapFirst,
659 bits + kByteKindStackMapLast + 1,
660 0u);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800661 Dump(os, "CodeInfoStackMap ", stack_map_bits, sum);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800662 {
663 ScopedIndentation indent1(&os);
664 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800665 "StackMapNativePc ",
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800666 bits[kByteKindStackMapNativePc],
667 stack_map_bits,
668 "stack map");
669 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800670 "StackMapDexPcEncoding ",
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800671 bits[kByteKindStackMapDexPc],
672 stack_map_bits,
673 "stack map");
674 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800675 "StackMapDexRegisterMap ",
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800676 bits[kByteKindStackMapDexRegisterMap],
677 stack_map_bits,
678 "stack map");
679 Dump(os,
Mathieu Chartier697dc662017-02-08 16:56:48 -0800680 "StackMapInlineInfoIndex ",
Mathieu Chartier1e083792017-02-08 13:30:04 -0800681 bits[kByteKindStackMapInlineInfoIndex],
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800682 stack_map_bits,
683 "stack map");
684 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800685 "StackMapRegisterMaskIndex ",
686 bits[kByteKindStackMapRegisterMaskIndex],
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800687 stack_map_bits,
688 "stack map");
689 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800690 "StackMapStackMaskIndex ",
David Srbecky45aa5982016-03-18 02:15:09 +0000691 bits[kByteKindStackMapStackMaskIndex],
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800692 stack_map_bits,
693 "stack map");
694 }
Mathieu Chartier1e083792017-02-08 13:30:04 -0800695 // Inline info section.
696 const int64_t inline_info_bits = std::accumulate(bits + kByteKindInlineInfoFirst,
697 bits + kByteKindInlineInfoLast + 1,
698 0u);
699 Dump(os, "CodeInfoInlineInfo ", inline_info_bits, sum);
700 {
701 ScopedIndentation indent1(&os);
702 Dump(os,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700703 "InlineInfoMethodIndexIdx ",
704 bits[kByteKindInlineInfoMethodIndexIdx],
Mathieu Chartier1e083792017-02-08 13:30:04 -0800705 inline_info_bits,
706 "inline info");
707 Dump(os,
708 "InlineInfoDexPc ",
709 bits[kByteKindStackMapDexPc],
710 inline_info_bits,
711 "inline info");
712 Dump(os,
713 "InlineInfoExtraData ",
714 bits[kByteKindInlineInfoExtraData],
715 inline_info_bits,
716 "inline info");
717 Dump(os,
718 "InlineInfoDexRegisterMap ",
719 bits[kByteKindInlineInfoDexRegisterMap],
720 inline_info_bits,
721 "inline info");
722 Dump(os,
723 "InlineInfoIsLast ",
724 bits[kByteKindInlineInfoIsLast],
725 inline_info_bits,
726 "inline info");
727 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800728 }
729 os.Stream() << "\n" << std::flush;
730 }
731
732 private:
733 void Dump(VariableIndentationOutputStream& os,
734 const char* name,
735 int64_t size,
736 int64_t total,
737 const char* sum_of = "total") {
738 const double percent = (static_cast<double>(size) / static_cast<double>(total)) * 100;
739 os.Stream() << StringPrintf("%s = %8" PRId64 " (%2.0f%% of %s)\n",
740 name,
741 size / kBitsPerByte,
742 percent,
743 sum_of);
744 }
745 };
746
Brian Carlstromaded5f72011-10-07 17:15:04 -0700747 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800748 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800749 // We don't know the length of the code for each method, but we need to know where to stop
750 // when disassembling. What we do know is that a region of code will be followed by some other
751 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
752 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800753 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
754 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700755 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700756 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700757 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
758 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700759 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
760 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800761 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800762 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800763 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800764 for (size_t class_def_index = 0;
765 class_def_index < dex_file->NumClassDefs();
766 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800767 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100768 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700769 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700770 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800771 ClassDataItemIterator it(*dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -0700772 it.SkipAllFields();
Elliott Hughese3c845c2012-02-28 17:23:01 -0800773 uint32_t class_method_index = 0;
774 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100775 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800776 it.Next();
777 }
778 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100779 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800780 it.Next();
781 }
782 }
783 }
784 }
785
786 // If the last thing in the file is code for a method, there won't be an offset for the "next"
787 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
788 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800789 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800790 }
791
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700792 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
793 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
794 }
795
Elliott Hughese3c845c2012-02-28 17:23:01 -0800796 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800797 uint32_t code_offset = oat_method.GetCodeOffset();
798 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
799 code_offset &= ~0x1;
800 }
801 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800802 offsets_.insert(oat_method.GetVmapTableOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800803 }
804
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700805 // Dex file data, may be for multiple different dex files.
806 class DexFileData {
807 public:
808 DexFileData() {}
809
810 explicit DexFileData(const DexFile& dex_file)
811 : num_string_ids_(dex_file.NumStringIds()),
812 num_method_ids_(dex_file.NumMethodIds()),
813 num_field_ids_(dex_file.NumFieldIds()),
814 num_type_ids_(dex_file.NumTypeIds()),
815 num_class_defs_(dex_file.NumClassDefs()) {
816 for (size_t class_def_index = 0; class_def_index < num_class_defs_; ++class_def_index) {
817 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
818 WalkClass(dex_file, class_def);
819 }
820 }
821
822 void Add(const DexFileData& other) {
823 AddAll(unique_string_ids_from_code_, other.unique_string_ids_from_code_);
824 num_string_ids_from_code_ += other.num_string_ids_from_code_;
825 AddAll(dex_code_item_ptrs_, other.dex_code_item_ptrs_);
826 dex_code_bytes_ += other.dex_code_bytes_;
827 num_string_ids_ += other.num_string_ids_;
828 num_method_ids_ += other.num_method_ids_;
829 num_field_ids_ += other.num_field_ids_;
830 num_type_ids_ += other.num_type_ids_;
831 num_class_defs_ += other.num_class_defs_;
832 }
833
834 void Dump(std::ostream& os) {
835 os << "Num string ids: " << num_string_ids_ << "\n";
836 os << "Num method ids: " << num_method_ids_ << "\n";
837 os << "Num field ids: " << num_field_ids_ << "\n";
838 os << "Num type ids: " << num_type_ids_ << "\n";
839 os << "Num class defs: " << num_class_defs_ << "\n";
840 os << "Unique strings loaded from dex code: " << unique_string_ids_from_code_.size() << "\n";
841 os << "Total strings loaded from dex code: " << num_string_ids_from_code_ << "\n";
842 os << "Number of unique dex code items: " << dex_code_item_ptrs_.size() << "\n";
843 os << "Total number of dex code bytes: " << dex_code_bytes_ << "\n";
844 }
845
846 private:
Andreas Gampe9186ced2016-12-12 14:28:21 -0800847 // All of the elements from one container to another.
848 template <typename Dest, typename Src>
849 static void AddAll(Dest& dest, const Src& src) {
850 dest.insert(src.begin(), src.end());
851 }
852
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700853 void WalkClass(const DexFile& dex_file, const DexFile::ClassDef& class_def) {
854 const uint8_t* class_data = dex_file.GetClassData(class_def);
855 if (class_data == nullptr) { // empty class such as a marker interface?
856 return;
857 }
858 ClassDataItemIterator it(dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -0700859 it.SkipAllFields();
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700860 while (it.HasNextDirectMethod()) {
861 WalkCodeItem(dex_file, it.GetMethodCodeItem());
862 it.Next();
863 }
864 while (it.HasNextVirtualMethod()) {
865 WalkCodeItem(dex_file, it.GetMethodCodeItem());
866 it.Next();
867 }
868 DCHECK(!it.HasNext());
869 }
870
871 void WalkCodeItem(const DexFile& dex_file, const DexFile::CodeItem* code_item) {
872 if (code_item == nullptr) {
873 return;
874 }
875 const size_t code_item_size = code_item->insns_size_in_code_units_;
876 const uint16_t* code_ptr = code_item->insns_;
877 const uint16_t* code_end = code_item->insns_ + code_item_size;
878
879 // If we inserted a new dex code item pointer, add to total code bytes.
880 if (dex_code_item_ptrs_.insert(code_ptr).second) {
881 dex_code_bytes_ += code_item_size * sizeof(code_ptr[0]);
882 }
883
884 while (code_ptr < code_end) {
885 const Instruction* inst = Instruction::At(code_ptr);
886 switch (inst->Opcode()) {
887 case Instruction::CONST_STRING: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800888 const dex::StringIndex string_index(inst->VRegB_21c());
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700889 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
890 ++num_string_ids_from_code_;
891 break;
892 }
893 case Instruction::CONST_STRING_JUMBO: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800894 const dex::StringIndex string_index(inst->VRegB_31c());
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700895 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
896 ++num_string_ids_from_code_;
897 break;
898 }
899 default:
900 break;
901 }
902
903 code_ptr += inst->SizeInCodeUnits();
904 }
905 }
906
907 // Unique string ids loaded from dex code.
908 std::set<StringReference, StringReferenceComparator> unique_string_ids_from_code_;
909
910 // Total string ids loaded from dex code.
911 size_t num_string_ids_from_code_ = 0;
912
913 // Unique code pointers.
914 std::set<const void*> dex_code_item_ptrs_;
915
916 // Total "unique" dex code bytes.
917 size_t dex_code_bytes_ = 0;
918
919 // Other dex ids.
920 size_t num_string_ids_ = 0;
921 size_t num_method_ids_ = 0;
922 size_t num_field_ids_ = 0;
923 size_t num_type_ids_ = 0;
924 size_t num_class_defs_ = 0;
925 };
926
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700927 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
928 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800929 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700930 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800931 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800932 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700933
Andreas Gampe2ba88952016-04-29 17:52:07 -0700934 const uint8_t* const oat_file_begin = oat_dex_file.GetOatFile()->Begin();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100935 const uint8_t* const vdex_file_begin = oat_dex_file.GetOatFile()->DexBegin();
936
937 // Print data range of the dex file embedded inside the corresponding vdex file.
Andreas Gampe2ba88952016-04-29 17:52:07 -0700938 const uint8_t* const dex_file_pointer = oat_dex_file.GetDexFilePointer();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100939 uint32_t dex_offset = dchecked_integral_cast<uint32_t>(dex_file_pointer - vdex_file_begin);
Andreas Gampe2ba88952016-04-29 17:52:07 -0700940 os << StringPrintf("dex-file: 0x%08x..0x%08x\n",
941 dex_offset,
942 dchecked_integral_cast<uint32_t>(dex_offset + oat_dex_file.FileSize() - 1));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700943
Andreas Gampe2ba88952016-04-29 17:52:07 -0700944 // Create the dex file early. A lot of print-out things depend on it.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700945 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700946 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
947 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700948 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700949 os << std::flush;
950 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700951 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100952
Andreas Gampe2ba88952016-04-29 17:52:07 -0700953 // Print lookup table, if it exists.
954 if (oat_dex_file.GetLookupTableData() != nullptr) {
955 uint32_t table_offset = dchecked_integral_cast<uint32_t>(
956 oat_dex_file.GetLookupTableData() - oat_file_begin);
David Sehr9aa352e2016-09-15 18:13:52 -0700957 uint32_t table_size = TypeLookupTable::RawDataLength(dex_file->NumClassDefs());
Andreas Gampe2ba88952016-04-29 17:52:07 -0700958 os << StringPrintf("type-table: 0x%08x..0x%08x\n",
959 table_offset,
960 table_offset + table_size - 1);
961 }
962
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100963 VariableIndentationOutputStream vios(&os);
964 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800965 for (size_t class_def_index = 0;
966 class_def_index < dex_file->NumClassDefs();
967 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700968 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
969 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800970
971 // TODO: Support regex
972 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
973 continue;
974 }
975
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700976 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100977 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700978 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
Andreas Gampea5b09a62016-11-17 15:21:22 -0800979 class_def_index, descriptor, oat_class_offset, class_def.class_idx_.index_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100980 << " (" << oat_class.GetStatus() << ")"
981 << " (" << oat_class.GetType() << ")\n";
982 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700983 if (options_.list_classes_) {
984 continue;
985 }
986 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700987 success = false;
988 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800989 if (stop_analysis) {
990 os << std::flush;
991 return success;
992 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700993 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700994 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700995 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700996 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700997 }
998
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800999 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
1000 std::string error_msg;
1001 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
1002
Mathieu Chartierac8f4392015-08-27 13:54:20 -07001003 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001004 if (dex_file == nullptr) {
1005 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
1006 return false;
1007 }
1008 size_t fsize = oat_dex_file.FileSize();
1009
1010 // Some quick checks just in case
1011 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
1012 os << "Invalid dex file\n";
1013 return false;
1014 }
1015
1016 // Verify output directory exists
1017 if (!OS::DirectoryExists(options_.export_dex_location_)) {
1018 // TODO: Extend OS::DirectoryExists if symlink support is required
1019 os << options_.export_dex_location_ << " output directory not found or symlink\n";
1020 return false;
1021 }
1022
1023 // Beautify path names
1024 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
1025 return false;
1026 }
1027
1028 std::string dex_orig_name;
1029 size_t dex_orig_pos = dex_file_location.rfind('/');
1030 if (dex_orig_pos == std::string::npos)
1031 dex_orig_name = dex_file_location;
1032 else
1033 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
1034
1035 // A more elegant approach to efficiently name user installed apps is welcome
Andreas Gampef812d8c2017-02-17 10:19:44 -08001036 if (dex_orig_name.size() == 8 &&
1037 dex_orig_name.compare("base.apk") == 0 &&
1038 dex_orig_pos != std::string::npos) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001039 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
1040 size_t apk_orig_pos = dex_file_location.rfind('/');
1041 if (apk_orig_pos != std::string::npos) {
1042 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
1043 }
1044 }
1045
1046 std::string out_dex_path(options_.export_dex_location_);
1047 if (out_dex_path.back() != '/') {
1048 out_dex_path.append("/");
1049 }
1050 out_dex_path.append(dex_orig_name);
1051 out_dex_path.append("_export.dex");
1052 if (out_dex_path.length() > PATH_MAX) {
1053 return false;
1054 }
1055
1056 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
1057 if (file.get() == nullptr) {
1058 os << "Failed to open output dex file " << out_dex_path;
1059 return false;
1060 }
1061
1062 if (!file->WriteFully(dex_file->Begin(), fsize)) {
1063 os << "Failed to write dex file";
1064 file->Erase();
1065 return false;
1066 }
1067
1068 if (file->FlushCloseOrErase() != 0) {
1069 os << "Flush and close failed";
1070 return false;
1071 }
1072
1073 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
1074 os << std::flush;
1075
1076 return true;
1077 }
1078
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001079 bool DumpOatClass(VariableIndentationOutputStream* vios,
1080 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001081 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001082 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001083 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -07001084 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001085 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001086 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001087 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -08001088 }
1089 ClassDataItemIterator it(dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -07001090 it.SkipAllFields();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001091 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001092 while (it.HasNextDirectMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001093 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001094 it.GetMemberIndex(), it.GetMethodCodeItem(),
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001095 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001096 success = false;
1097 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001098 if (addr_found) {
1099 *stop_analysis = true;
1100 return success;
1101 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001102 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -07001103 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001104 }
Ian Rogers0571d352011-11-03 19:51:38 -07001105 while (it.HasNextVirtualMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001106 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001107 it.GetMemberIndex(), it.GetMethodCodeItem(),
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001108 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001109 success = false;
1110 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001111 if (addr_found) {
1112 *stop_analysis = true;
1113 return success;
1114 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001115 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -07001116 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001117 }
Ian Rogers0571d352011-11-03 19:51:38 -07001118 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001119 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001120 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001121 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001122
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001123 static constexpr uint32_t kPrologueBytes = 16;
1124
1125 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
1126 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
1127
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001128 bool DumpOatMethod(VariableIndentationOutputStream* vios,
1129 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -07001130 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001131 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001132 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001133 uint32_t method_access_flags, bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001134 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001135
1136 // TODO: Support regex
1137 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
1138 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00001139 return success;
1140 }
1141
David Sehr709b0702016-10-13 09:12:37 -07001142 std::string pretty_method = dex_file.PrettyMethod(dex_method_idx, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001143 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
1144 class_method_index, pretty_method.c_str(),
1145 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001146 if (options_.list_methods_) return success;
1147
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001148 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
1149 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
1150 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
1151 uint32_t code_offset = oat_method.GetCodeOffset();
1152 uint32_t code_size = oat_method.GetQuickCodeSize();
1153 if (resolved_addr2instr_ != 0) {
1154 if (resolved_addr2instr_ > code_offset + code_size) {
1155 return success;
1156 } else {
1157 *addr_found = true; // stop analyzing file at next iteration
1158 }
1159 }
1160
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001161 // Everything below is indented at least once.
1162 ScopedIndentation indent1(vios);
1163
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001164 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001165 vios->Stream() << "DEX CODE:\n";
1166 ScopedIndentation indent2(vios);
1167 DumpDexCode(vios->Stream(), dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -07001168 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001169
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001170 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001171 std::unique_ptr<verifier::MethodVerifier> verifier;
1172 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001173 // We need to have the handle scope stay live until after the verifier since the verifier has
1174 // a handle to the dex cache from hs.
1175 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001176 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
1177 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001178 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001179 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001180 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -07001181 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001182 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001183 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001184 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001185 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001186 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001187 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001188 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001189 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001190 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
1191 oat_method_offsets_offset, oat_file_.Size());
1192 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001193 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001194 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001195 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001196
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001197 ScopedIndentation indent2(vios);
1198 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001199 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
1200 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001201 vios->Stream() << StringPrintf("WARNING: "
1202 "code offset 0x%08x is past end of file 0x%08zx.\n",
1203 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001204 success = false;
1205 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001206 vios->Stream() << "\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001207 }
1208 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001209 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001210 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
1211 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001212 stats_.AddBitsIfUnique(Stats::kByteKindQuickMethodHeader,
1213 sizeof(*method_header) * kBitsPerByte,
1214 method_header);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001215 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001216 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001217 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001218 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001219 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001220 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001221 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
1222 method_header_offset, oat_file_.Size());
1223 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001224 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001225 return false;
1226 }
1227
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001228 ScopedIndentation indent2(vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001229 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001230 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001231 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001232 }
Mingyao Yang063fc772016-08-02 11:02:54 -07001233 uint32_t vmap_table_offset = method_header ==
1234 nullptr ? 0 : method_header->GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001235 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001236
1237 size_t vmap_table_offset_limit =
1238 (kIsVdexEnabled && IsMethodGeneratedByDexToDexCompiler(oat_method, code_item))
1239 ? oat_file_.GetVdexFile()->Size()
1240 : method_header->GetCode() - oat_file_.Begin();
1241 if (vmap_table_offset >= vmap_table_offset_limit) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001242 vios->Stream() << StringPrintf("WARNING: "
1243 "vmap table offset 0x%08x is past end of file 0x%08zx. "
1244 "vmap table offset was loaded from offset 0x%08x.\n",
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001245 vmap_table_offset,
1246 vmap_table_offset_limit,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001247 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001248 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001249 } else if (options_.dump_vmap_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001250 DumpVmapData(vios, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001251 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001252 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001253 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001254 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001255
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001256 ScopedIndentation indent2(vios);
1257 vios->Stream()
1258 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
1259 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
1260 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
1261 vios->Stream() << "\n";
1262 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
1263 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
1264 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001265 }
1266 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001267 // Based on spill masks from QuickMethodFrameInfo so placed
1268 // after it is dumped, but useful for understanding quick
1269 // code, so dumped here.
1270 ScopedIndentation indent2(vios);
1271 DumpVregLocations(vios->Stream(), oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001272 }
1273 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001274 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001275 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
1276 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001277 ScopedIndentation indent2(vios);
1278 vios->Stream() << StringPrintf("WARNING: "
1279 "code size offset 0x%08x is past end of file 0x%08zx.",
1280 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001281 success = false;
1282 } else {
1283 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001284 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1285 uint64_t aligned_code_end = aligned_code_begin + code_size;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001286 stats_.AddBitsIfUnique(Stats::kByteKindCode, code_size * kBitsPerByte, code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001287
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001288 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001289 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001290 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001291 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
1292 code_offset,
1293 code_size_offset,
1294 code_size,
1295 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001296
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001297 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001298 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001299 vios->Stream() << StringPrintf("WARNING: "
1300 "start of code at 0x%08x is past end of file 0x%08zx.",
1301 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001302 success = false;
1303 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001304 vios->Stream() << StringPrintf(
1305 "WARNING: "
1306 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
1307 "code size is 0x%08x loaded from offset 0x%08x.\n",
1308 aligned_code_end, oat_file_.Size(),
1309 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001310 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001311 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001312 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001313 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001314 }
1315 }
1316 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001317 vios->Stream() << StringPrintf(
1318 "WARNING: "
1319 "code size %d is bigger than max expected threshold of %d. "
1320 "code size is 0x%08x loaded from offset 0x%08x.\n",
1321 code_size, kMaxCodeSize,
1322 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001323 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001324 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001325 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001326 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001327 }
1328 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001329 } else if (options_.disassemble_code_) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001330 DumpCode(vios, oat_method, code_item, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001331 }
1332 }
1333 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001334 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001335 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001336 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001337
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001338 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1339 if (spill_mask == 0) {
1340 return;
1341 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001342 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001343 for (size_t i = 0; i < 32; i++) {
1344 if ((spill_mask & (1 << i)) != 0) {
1345 if (is_float) {
1346 os << "fr" << i;
1347 } else {
1348 os << "r" << i;
1349 }
1350 spill_mask ^= 1 << i; // clear bit
1351 if (spill_mask != 0) {
1352 os << ", ";
1353 } else {
1354 break;
1355 }
1356 }
1357 }
1358 os << ")";
1359 }
1360
Roland Levillain442b46a2015-02-18 16:54:21 +00001361 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001362 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001363 const OatFile::OatMethod& oat_method,
1364 const DexFile::CodeItem* code_item) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001365 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1366 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001367 const void* raw_code_info = oat_method.GetVmapTable();
1368 if (raw_code_info != nullptr) {
1369 CodeInfo code_info(raw_code_info);
1370 DCHECK(code_item != nullptr);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001371 ScopedIndentation indent1(vios);
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001372 MethodInfo method_info = oat_method.GetOatQuickMethodHeader()->GetOptimizedMethodInfo();
1373 DumpCodeInfo(vios, code_info, oat_method, *code_item, method_info);
Roland Levillain442b46a2015-02-18 16:54:21 +00001374 }
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001375 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
1376 // We don't encode the size in the table, so just emit that we have quickened
1377 // information.
1378 ScopedIndentation indent(vios);
1379 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001380 } else {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001381 // Otherwise, there is nothing to display.
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001382 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001383 }
1384
1385 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001386 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001387 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001388 const OatFile::OatMethod& oat_method,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001389 const DexFile::CodeItem& code_item,
1390 const MethodInfo& method_info) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001391 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001392 oat_method.GetCodeOffset(),
1393 code_item.registers_size_,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001394 options_.dump_code_info_stack_maps_,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001395 instruction_set_,
1396 method_info);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001397 }
1398
Andreas Gampe36a296f2017-06-13 14:11:11 -07001399 static int GetOutVROffset(uint16_t out_num, InstructionSet isa) {
1400 // According to stack model, the first out is above the Method referernce.
1401 return static_cast<size_t>(InstructionSetPointerSize(isa)) + out_num * sizeof(uint32_t);
1402 }
1403
1404 static uint32_t GetVRegOffsetFromQuickCode(const DexFile::CodeItem* code_item,
1405 uint32_t core_spills,
1406 uint32_t fp_spills,
1407 size_t frame_size,
1408 int reg,
1409 InstructionSet isa) {
1410 PointerSize pointer_size = InstructionSetPointerSize(isa);
1411 if (kIsDebugBuild) {
1412 auto* runtime = Runtime::Current();
1413 if (runtime != nullptr) {
1414 CHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), pointer_size);
1415 }
1416 }
1417 DCHECK_ALIGNED(frame_size, kStackAlignment);
1418 DCHECK_NE(reg, -1);
1419 int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa)
1420 + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa)
1421 + sizeof(uint32_t); // Filler.
1422 int num_regs = code_item->registers_size_ - code_item->ins_size_;
1423 int temp_threshold = code_item->registers_size_;
1424 const int max_num_special_temps = 1;
1425 if (reg == temp_threshold) {
1426 // The current method pointer corresponds to special location on stack.
1427 return 0;
1428 } else if (reg >= temp_threshold + max_num_special_temps) {
1429 /*
1430 * Special temporaries may have custom locations and the logic above deals with that.
1431 * However, non-special temporaries are placed relative to the outs.
1432 */
1433 int temps_start = code_item->outs_size_ * sizeof(uint32_t)
1434 + static_cast<size_t>(pointer_size) /* art method */;
1435 int relative_offset = (reg - (temp_threshold + max_num_special_temps)) * sizeof(uint32_t);
1436 return temps_start + relative_offset;
1437 } else if (reg < num_regs) {
1438 int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t);
1439 return locals_start + (reg * sizeof(uint32_t));
1440 } else {
1441 // Handle ins.
1442 return frame_size + ((reg - num_regs) * sizeof(uint32_t))
1443 + static_cast<size_t>(pointer_size) /* art method */;
1444 }
1445 }
1446
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001447 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1448 const DexFile::CodeItem* code_item) {
1449 if (code_item != nullptr) {
1450 size_t num_locals_ins = code_item->registers_size_;
1451 size_t num_ins = code_item->ins_size_;
1452 size_t num_locals = num_locals_ins - num_ins;
1453 size_t num_outs = code_item->outs_size_;
1454
1455 os << "vr_stack_locations:";
1456 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1457 // For readability, delimit the different kinds of VRs.
1458 if (reg == num_locals_ins) {
1459 os << "\n\tmethod*:";
1460 } else if (reg == num_locals && num_ins > 0) {
1461 os << "\n\tins:";
1462 } else if (reg == 0 && num_locals > 0) {
1463 os << "\n\tlocals:";
1464 }
1465
Andreas Gampe36a296f2017-06-13 14:11:11 -07001466 uint32_t offset = GetVRegOffsetFromQuickCode(code_item,
1467 oat_method.GetCoreSpillMask(),
1468 oat_method.GetFpSpillMask(),
1469 oat_method.GetFrameSizeInBytes(),
1470 reg,
1471 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001472 os << " v" << reg << "[sp + #" << offset << "]";
1473 }
1474
1475 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1476 if (out_reg == 0) {
1477 os << "\n\touts:";
1478 }
1479
Andreas Gampe36a296f2017-06-13 14:11:11 -07001480 uint32_t offset = GetOutVROffset(out_reg, GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001481 os << " v" << out_reg << "[sp + #" << offset << "]";
1482 }
1483
1484 os << "\n";
1485 }
1486 }
1487
Ian Rogersb23a7722012-10-09 16:54:26 -07001488 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001489 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001490 size_t i = 0;
1491 while (i < code_item->insns_size_in_code_units_) {
1492 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001493 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1494 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001495 i += instruction->SizeInCodeUnits();
1496 }
1497 }
1498 }
1499
Roland Levillainf2650d12015-05-28 14:53:28 +01001500 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1501 // the optimizing compiler?
1502 static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1503 const DexFile::CodeItem* code_item) {
1504 // If the native GC map is null and the Dex `code_item` is not
1505 // null, then this method has been compiled with the optimizing
1506 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001507 return oat_method.GetQuickCode() != nullptr &&
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001508 oat_method.GetVmapTable() != nullptr &&
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001509 code_item != nullptr;
1510 }
1511
1512 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1513 // the dextodex compiler?
1514 static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
1515 const DexFile::CodeItem* code_item) {
1516 // If the quick code is null, the Dex `code_item` is not
1517 // null, and the vmap table is not null, then this method has been compiled
1518 // with the dextodex compiler.
1519 return oat_method.GetQuickCode() == nullptr &&
1520 oat_method.GetVmapTable() != nullptr &&
1521 code_item != nullptr;
Roland Levillainf2650d12015-05-28 14:53:28 +01001522 }
1523
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001524 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001525 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001526 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001527 const DexFile* dex_file,
1528 const DexFile::ClassDef& class_def,
1529 const DexFile::CodeItem* code_item,
1530 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001531 if ((method_access_flags & kAccNative) == 0) {
1532 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001533 Runtime* const runtime = Runtime::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001534 Handle<mirror::DexCache> dex_cache(
Mathieu Chartierf284d442016-06-02 11:48:30 -07001535 hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file, nullptr)));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001536 CHECK(dex_cache != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001537 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001538 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001539 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
David Brazdil15fc7292016-09-02 14:13:18 +01001540 class_def, code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001541 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001542
1543 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001544 }
1545
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001546 // The StackMapsHelper provides the stack maps in the native PC order.
1547 // For identical native PCs, the order from the CodeInfo is preserved.
1548 class StackMapsHelper {
1549 public:
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001550 explicit StackMapsHelper(const uint8_t* raw_code_info, InstructionSet instruction_set)
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001551 : code_info_(raw_code_info),
1552 encoding_(code_info_.ExtractEncoding()),
1553 number_of_stack_maps_(code_info_.GetNumberOfStackMaps(encoding_)),
1554 indexes_(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001555 offset_(static_cast<uint32_t>(-1)),
1556 stack_map_index_(0u),
1557 instruction_set_(instruction_set) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001558 if (number_of_stack_maps_ != 0u) {
1559 // Check if native PCs are ordered.
1560 bool ordered = true;
1561 StackMap last = code_info_.GetStackMapAt(0u, encoding_);
1562 for (size_t i = 1; i != number_of_stack_maps_; ++i) {
1563 StackMap current = code_info_.GetStackMapAt(i, encoding_);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001564 if (last.GetNativePcOffset(encoding_.stack_map.encoding, instruction_set) >
1565 current.GetNativePcOffset(encoding_.stack_map.encoding, instruction_set)) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001566 ordered = false;
1567 break;
1568 }
1569 last = current;
1570 }
1571 if (!ordered) {
1572 // Create indirection indexes for access in native PC order. We do not optimize
1573 // for the fact that there can currently be only two separately ordered ranges,
1574 // namely normal stack maps and catch-point stack maps.
1575 indexes_.resize(number_of_stack_maps_);
1576 std::iota(indexes_.begin(), indexes_.end(), 0u);
1577 std::sort(indexes_.begin(),
1578 indexes_.end(),
1579 [this](size_t lhs, size_t rhs) {
1580 StackMap left = code_info_.GetStackMapAt(lhs, encoding_);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001581 uint32_t left_pc = left.GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001582 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001583 StackMap right = code_info_.GetStackMapAt(rhs, encoding_);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001584 uint32_t right_pc = right.GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001585 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001586 // If the PCs are the same, compare indexes to preserve the original order.
1587 return (left_pc < right_pc) || (left_pc == right_pc && lhs < rhs);
1588 });
1589 }
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001590 offset_ = GetStackMapAt(0).GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001591 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001592 }
1593 }
1594
1595 const CodeInfo& GetCodeInfo() const {
1596 return code_info_;
1597 }
1598
1599 const CodeInfoEncoding& GetEncoding() const {
1600 return encoding_;
1601 }
1602
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001603 uint32_t GetOffset() const {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001604 return offset_;
1605 }
1606
1607 StackMap GetStackMap() const {
1608 return GetStackMapAt(stack_map_index_);
1609 }
1610
1611 void Next() {
1612 ++stack_map_index_;
1613 offset_ = (stack_map_index_ == number_of_stack_maps_)
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001614 ? static_cast<uint32_t>(-1)
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001615 : GetStackMapAt(stack_map_index_).GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001616 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001617 }
1618
1619 private:
1620 StackMap GetStackMapAt(size_t i) const {
1621 if (!indexes_.empty()) {
1622 i = indexes_[i];
1623 }
1624 DCHECK_LT(i, number_of_stack_maps_);
1625 return code_info_.GetStackMapAt(i, encoding_);
1626 }
1627
1628 const CodeInfo code_info_;
1629 const CodeInfoEncoding encoding_;
1630 const size_t number_of_stack_maps_;
1631 dchecked_vector<size_t> indexes_; // Used if stack map native PCs are not ordered.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001632 uint32_t offset_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001633 size_t stack_map_index_;
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001634 const InstructionSet instruction_set_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001635 };
1636
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001637 void DumpCode(VariableIndentationOutputStream* vios,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001638 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1639 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001640 const void* quick_code = oat_method.GetQuickCode();
1641
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001642 if (code_size == 0) {
1643 code_size = oat_method.GetQuickCodeSize();
1644 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001645 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001646 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001647 return;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001648 } else if (!bad_input && IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1649 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001650 StackMapsHelper helper(oat_method.GetVmapTable(), instruction_set_);
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001651 MethodInfo method_info(oat_method.GetOatQuickMethodHeader()->GetOptimizedMethodInfo());
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001652 {
1653 CodeInfoEncoding encoding(helper.GetEncoding());
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001654 StackMapEncoding stack_map_encoding(encoding.stack_map.encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001655 const size_t num_stack_maps = encoding.stack_map.num_entries;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001656 if (stats_.AddBitsIfUnique(Stats::kByteKindCodeInfoEncoding,
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001657 encoding.HeaderSize() * kBitsPerByte,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001658 oat_method.GetVmapTable())) {
Mathieu Chartier1e083792017-02-08 13:30:04 -08001659 // Stack maps
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001660 stats_.AddBits(
1661 Stats::kByteKindStackMapNativePc,
1662 stack_map_encoding.GetNativePcEncoding().BitSize() * num_stack_maps);
1663 stats_.AddBits(
1664 Stats::kByteKindStackMapDexPc,
1665 stack_map_encoding.GetDexPcEncoding().BitSize() * num_stack_maps);
1666 stats_.AddBits(
1667 Stats::kByteKindStackMapDexRegisterMap,
1668 stack_map_encoding.GetDexRegisterMapEncoding().BitSize() * num_stack_maps);
1669 stats_.AddBits(
Mathieu Chartier1e083792017-02-08 13:30:04 -08001670 Stats::kByteKindStackMapInlineInfoIndex,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001671 stack_map_encoding.GetInlineInfoEncoding().BitSize() * num_stack_maps);
1672 stats_.AddBits(
Mathieu Chartier1a20b682017-01-31 14:25:16 -08001673 Stats::kByteKindStackMapRegisterMaskIndex,
1674 stack_map_encoding.GetRegisterMaskIndexEncoding().BitSize() * num_stack_maps);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001675 stats_.AddBits(
David Srbecky45aa5982016-03-18 02:15:09 +00001676 Stats::kByteKindStackMapStackMaskIndex,
1677 stack_map_encoding.GetStackMaskIndexEncoding().BitSize() * num_stack_maps);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001678
1679 // Stack masks
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001680 stats_.AddBits(
David Srbecky45aa5982016-03-18 02:15:09 +00001681 Stats::kByteKindCodeInfoStackMasks,
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001682 encoding.stack_mask.encoding.BitSize() * encoding.stack_mask.num_entries);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001683
1684 // Register masks
Mathieu Chartier1a20b682017-01-31 14:25:16 -08001685 stats_.AddBits(
1686 Stats::kByteKindCodeInfoRegisterMasks,
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001687 encoding.register_mask.encoding.BitSize() * encoding.register_mask.num_entries);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001688
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001689 // Invoke infos
1690 if (encoding.invoke_info.num_entries > 0u) {
1691 stats_.AddBits(
1692 Stats::kByteKindCodeInfoInvokeInfo,
1693 encoding.invoke_info.encoding.BitSize() * encoding.invoke_info.num_entries);
1694 }
1695
Mathieu Chartier1e083792017-02-08 13:30:04 -08001696 // Location catalog
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001697 const size_t location_catalog_bytes =
1698 helper.GetCodeInfo().GetDexRegisterLocationCatalogSize(encoding);
1699 stats_.AddBits(Stats::kByteKindCodeInfoLocationCatalog,
1700 kBitsPerByte * location_catalog_bytes);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001701 // Dex register bytes.
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001702 const size_t dex_register_bytes =
1703 helper.GetCodeInfo().GetDexRegisterMapsSize(encoding, code_item->registers_size_);
1704 stats_.AddBits(
1705 Stats::kByteKindCodeInfoDexRegisterMap,
1706 kBitsPerByte * dex_register_bytes);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001707
1708 // Inline infos.
1709 const size_t num_inline_infos = encoding.inline_info.num_entries;
1710 if (num_inline_infos > 0u) {
1711 stats_.AddBits(
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001712 Stats::kByteKindInlineInfoMethodIndexIdx,
1713 encoding.inline_info.encoding.GetMethodIndexIdxEncoding().BitSize() *
1714 num_inline_infos);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001715 stats_.AddBits(
1716 Stats::kByteKindInlineInfoDexPc,
1717 encoding.inline_info.encoding.GetDexPcEncoding().BitSize() * num_inline_infos);
1718 stats_.AddBits(
1719 Stats::kByteKindInlineInfoExtraData,
1720 encoding.inline_info.encoding.GetExtraDataEncoding().BitSize() * num_inline_infos);
1721 stats_.AddBits(
1722 Stats::kByteKindInlineInfoDexRegisterMap,
1723 encoding.inline_info.encoding.GetDexRegisterMapEncoding().BitSize() *
1724 num_inline_infos);
1725 stats_.AddBits(Stats::kByteKindInlineInfoIsLast, num_inline_infos);
1726 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001727 }
1728 }
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001729 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1730 size_t offset = 0;
1731 while (offset < code_size) {
1732 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
1733 if (offset == helper.GetOffset()) {
1734 ScopedIndentation indent1(vios);
1735 StackMap stack_map = helper.GetStackMap();
1736 DCHECK(stack_map.IsValid());
1737 stack_map.Dump(vios,
1738 helper.GetCodeInfo(),
1739 helper.GetEncoding(),
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001740 method_info,
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001741 oat_method.GetCodeOffset(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001742 code_item->registers_size_,
1743 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001744 do {
1745 helper.Next();
1746 // There may be multiple stack maps at a given PC. We display only the first one.
1747 } while (offset == helper.GetOffset());
1748 }
1749 DCHECK_LT(offset, helper.GetOffset());
1750 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001751 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001752 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1753 size_t offset = 0;
1754 while (offset < code_size) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001755 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001756 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001757 }
1758 }
1759
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001760 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001761 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001762 const OatDumperOptions& options_;
1763 uint32_t resolved_addr2instr_;
Andreas Gampe372f3a32016-08-19 10:49:06 -07001764 const InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001765 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001766 Disassembler* disassembler_;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001767 Stats stats_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001768};
1769
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001770class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001771 public:
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001772 ImageDumper(std::ostream* os,
1773 gc::space::ImageSpace& image_space,
1774 const ImageHeader& image_header,
1775 OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001776 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001777 vios_(os),
1778 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001779 image_space_(image_space),
1780 image_header_(image_header),
1781 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001782
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001783 bool Dump() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001784 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001785 std::ostream& indent_os = vios_.Stream();
1786
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001787 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001788
Jeff Haodcdc85b2015-12-04 14:06:18 -08001789 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1790
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001791 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001792
Mathieu Chartiere401d142015-04-22 13:56:20 -07001793 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1794
1795 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1796 auto section = static_cast<ImageHeader::ImageSections>(i);
1797 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1798 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001799
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001800 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001801
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001802 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001803
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001804 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1805
1806 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1807
1808 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001809
Alex Lighta59dd802014-07-02 16:28:08 -07001810 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1811
Igor Murashkin46774762014-10-22 11:37:02 -07001812 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1813
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001814 {
1815 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001816 static_assert(arraysize(image_roots_descriptions_) ==
1817 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001818 DCHECK_LE(image_header_.GetImageRoots()->GetLength(), ImageHeader::kImageRootsMax);
1819 for (int32_t i = 0, size = image_header_.GetImageRoots()->GetLength(); i != size; ++i) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001820 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1821 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001822 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001823 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001824 if (image_root_object != nullptr && image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001825 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001826 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001827 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001828 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1829 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001830 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001831 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1832 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001833 run++;
1834 } else {
1835 break;
1836 }
1837 }
1838 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001839 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001840 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001841 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001842 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001843 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001844 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001845 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001846 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001847 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001848 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001849 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001850 }
1851 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001852 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001853
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001854 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001855 os << "METHOD ROOTS\n";
1856 static_assert(arraysize(image_methods_descriptions_) ==
1857 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1858 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1859 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1860 const char* description = image_methods_descriptions_[i];
1861 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001862 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001863 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001864 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001865 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001866
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001867 Runtime* const runtime = Runtime::Current();
1868 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001869 std::string image_filename = image_space_.GetImageFilename();
1870 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001871 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001872 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001873 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001874 const OatFile* oat_file = image_space_.GetOatFile();
Alex Lighta59dd802014-07-02 16:28:08 -07001875 if (oat_file == nullptr) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001876 oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location);
1877 }
1878 if (oat_file == nullptr) {
1879 oat_file = OatFile::Open(oat_location,
1880 oat_location,
1881 nullptr,
1882 nullptr,
1883 false,
1884 /*low_4gb*/false,
1885 nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001886 &error_msg);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001887 }
1888 if (oat_file == nullptr) {
1889 os << "OAT FILE NOT FOUND: " << error_msg << "\n";
1890 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001891 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001892 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001893
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001894 stats_.oat_file_bytes = oat_file->Size();
1895
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001896 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001897
Mathieu Chartier02e25112013-08-14 16:14:24 -07001898 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001899 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001900 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1901 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001902 }
1903
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001904 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001905
Jeff Haodcdc85b2015-12-04 14:06:18 -08001906 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001907 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001908 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001909 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001910 {
1911 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1912 heap->FlushAllocStack();
1913 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001914 // Since FlushAllocStack() above resets the (active) allocation
1915 // stack. Need to revoke the thread-local allocation stacks that
1916 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001917 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001918 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001919 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001920 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001921 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001922 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001923 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001924 {
Andreas Gampecc1b5352016-12-01 16:58:38 -08001925 ReaderMutexLock mu(self, *Locks::dex_lock_);
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001926 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001927 ObjPtr<mirror::DexCache> dex_cache =
1928 ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001929 if (dex_cache != nullptr) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001930 dex_caches_.insert(dex_cache.Ptr());
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001931 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001932 }
1933 }
Andreas Gampe0c183382017-07-13 22:26:24 -07001934 auto dump_visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
1935 DumpObject(obj);
1936 };
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001937 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001938 // Dump the normal objects before ArtMethods.
Andreas Gampe0c183382017-07-13 22:26:24 -07001939 image_space_.GetLiveBitmap()->Walk(dump_visitor);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001940 indent_os << "\n";
1941 // TODO: Dump fields.
1942 // Dump methods after.
Jeff Haodcdc85b2015-12-04 14:06:18 -08001943 DumpArtMethodVisitor visitor(this);
Mathieu Chartiere42888f2016-04-14 10:49:19 -07001944 image_header_.VisitPackedArtMethods(&visitor,
1945 image_space_.Begin(),
1946 image_header_.GetPointerSize());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001947 // Dump the large objects separately.
Andreas Gampe0c183382017-07-13 22:26:24 -07001948 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(dump_visitor);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001949 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001950 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001951 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001952 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001953 size_t data_size = image_header_.GetDataSize(); // stored size in file.
1954 if (file == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001955 LOG(WARNING) << "Failed to find image in " << image_filename;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001956 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001957 stats_.file_bytes = file->GetLength();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001958 // If the image is compressed, adjust to decompressed size.
1959 size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader);
1960 if (image_header_.GetStorageMode() == ImageHeader::kStorageModeUncompressed) {
1961 DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image";
1962 }
1963 stats_.file_bytes += uncompressed_size - data_size;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001964 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001965 size_t header_bytes = sizeof(ImageHeader);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001966 const auto& object_section = image_header_.GetImageSection(ImageHeader::kSectionObjects);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001967 const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1968 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001969 const auto& dex_cache_arrays_section = image_header_.GetImageSection(
1970 ImageHeader::kSectionDexCacheArrays);
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001971 const auto& intern_section = image_header_.GetImageSection(
1972 ImageHeader::kSectionInternedStrings);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001973 const auto& class_table_section = image_header_.GetImageSection(
1974 ImageHeader::kSectionClassTable);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001975 const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1976
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001977 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001978
1979 // Objects are kObjectAlignment-aligned.
1980 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1981 if (object_section.Offset() > header_bytes) {
1982 stats_.alignment_bytes += object_section.Offset() - header_bytes;
1983 }
1984
1985 // Field section is 4-byte aligned.
1986 constexpr size_t kFieldSectionAlignment = 4U;
1987 uint32_t end_objects = object_section.Offset() + object_section.Size();
1988 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
1989 stats_.alignment_bytes += field_section.Offset() - end_objects;
1990
1991 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
1992 uint32_t end_fields = field_section.Offset() + field_section.Size();
1993 CHECK_ALIGNED(method_section.Offset(), 4);
1994 stats_.alignment_bytes += method_section.Offset() - end_fields;
1995
1996 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
1997 uint32_t end_methods = method_section.Offset() + method_section.Size();
1998 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
1999 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
2000
2001 // Intern table is 8-byte aligned.
2002 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
2003 CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
2004 stats_.alignment_bytes += intern_section.Offset() - end_caches;
2005
2006 // Add space between intern table and class table.
2007 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
2008 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
2009
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002010 // Add space between end of image data and bitmap. Expect the bitmap to be page-aligned.
2011 const size_t bitmap_offset = sizeof(ImageHeader) + data_size;
Andreas Gampeace0dc12016-01-20 13:33:13 -08002012 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002013 stats_.alignment_bytes += RoundUp(bitmap_offset, kPageSize) - bitmap_offset;
Andreas Gampeace0dc12016-01-20 13:33:13 -08002014
Mathieu Chartiere401d142015-04-22 13:56:20 -07002015 stats_.bitmap_bytes += bitmap_section.Size();
2016 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01002017 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01002018 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002019 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002020 stats_.class_table_bytes += class_table_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002021 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002022 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002023
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002024 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002025
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002026 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002027 }
2028
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002029 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002030 class DumpArtMethodVisitor : public ArtMethodVisitor {
2031 public:
2032 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
2033
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002034 virtual void Visit(ArtMethod* method) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002035 std::ostream& indent_os = image_dumper_->vios_.Stream();
David Sehr709b0702016-10-13 09:12:37 -07002036 indent_os << method << " " << " ArtMethod: " << ArtMethod::PrettyMethod(method) << "\n";
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002037 image_dumper_->DumpMethod(method, indent_os);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002038 indent_os << "\n";
2039 }
2040
2041 private:
2042 ImageDumper* const image_dumper_;
2043 };
2044
Mathieu Chartier3398c782016-09-30 10:27:43 -07002045 static void PrettyObjectValue(std::ostream& os,
2046 ObjPtr<mirror::Class> type,
2047 ObjPtr<mirror::Object> value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002048 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002049 CHECK(type != nullptr);
2050 if (value == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07002051 os << StringPrintf("null %s\n", type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002052 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002053 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002054 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07002055 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07002056 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002057 mirror::Class* klass = value->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07002058 os << StringPrintf("%p Class: %s\n", klass, mirror::Class::PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002059 } else {
David Sehr709b0702016-10-13 09:12:37 -07002060 os << StringPrintf("%p %s\n", value.Ptr(), type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002061 }
2062 }
2063
Mathieu Chartier3398c782016-09-30 10:27:43 -07002064 static void PrintField(std::ostream& os, ArtField* field, ObjPtr<mirror::Object> obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002065 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07002066 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08002067 switch (field->GetTypeAsPrimitiveType()) {
2068 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08002069 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002070 break;
2071 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002072 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002073 break;
2074 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002075 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002076 break;
2077 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002078 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002079 break;
2080 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07002081 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002082 break;
2083 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07002084 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002085 break;
2086 case Primitive::kPrimBoolean:
Roland Levillain5e8d5f02016-10-18 18:03:43 +01002087 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj) ? "true" : "false",
Fred Shih37f05ef2014-07-16 18:38:08 -07002088 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002089 break;
2090 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07002091 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002092 break;
2093 case Primitive::kPrimNot: {
2094 // Get the value, don't compute the type unless it is non-null as we don't want
2095 // to cause class loading.
Mathieu Chartier3398c782016-09-30 10:27:43 -07002096 ObjPtr<mirror::Object> value = field->GetObj(obj);
Ian Rogers08f1f502014-12-02 15:04:37 -08002097 if (value == nullptr) {
2098 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07002099 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08002100 // Grab the field type without causing resolution.
Mathieu Chartier3398c782016-09-30 10:27:43 -07002101 ObjPtr<mirror::Class> field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08002102 if (field_type != nullptr) {
2103 PrettyObjectValue(os, field_type, value);
2104 } else {
Mathieu Chartier3398c782016-09-30 10:27:43 -07002105 os << StringPrintf("%p %s\n",
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07002106 value.Ptr(),
Ian Rogers08f1f502014-12-02 15:04:37 -08002107 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
2108 }
Ian Rogers50239c72013-06-17 14:53:22 -07002109 }
Ian Rogers08f1f502014-12-02 15:04:37 -08002110 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07002111 }
Ian Rogers08f1f502014-12-02 15:04:37 -08002112 default:
2113 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
2114 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08002115 }
2116 }
2117
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002118 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002119 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002120 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002121 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002122 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08002123 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002124 for (ArtField& field : klass->GetIFields()) {
2125 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08002126 }
2127 }
2128
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002129 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002130 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002131 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002132
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002133 const void* GetQuickOatCodeBegin(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08002134 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002135 image_header_.GetPointerSize());
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002136 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002137 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002138 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002139 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002140 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002141 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002142 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002143 }
2144
Mathieu Chartiere401d142015-04-22 13:56:20 -07002145 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002146 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002147 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
2148 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002149 return 0;
2150 }
2151 return oat_code_begin[-1];
2152 }
2153
Mathieu Chartiere401d142015-04-22 13:56:20 -07002154 const void* GetQuickOatCodeEnd(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002155 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002156 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002157 if (oat_code_begin == nullptr) {
2158 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002159 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002160 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002161 }
2162
Andreas Gampe0c183382017-07-13 22:26:24 -07002163 void DumpObject(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002164 DCHECK(obj != nullptr);
Andreas Gampe0c183382017-07-13 22:26:24 -07002165 if (!InDumpSpace(obj)) {
Brian Carlstrom78128a62011-09-15 17:21:19 -07002166 return;
2167 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002168
2169 size_t object_bytes = obj->SizeOf();
2170 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
Andreas Gampe0c183382017-07-13 22:26:24 -07002171 stats_.object_bytes += object_bytes;
2172 stats_.alignment_bytes += alignment_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002173
Andreas Gampe0c183382017-07-13 22:26:24 -07002174 std::ostream& os = vios_.Stream();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002175
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002176 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08002177 if (obj_class->IsArrayClass()) {
David Sehr709b0702016-10-13 09:12:37 -07002178 os << StringPrintf("%p: %s length:%d\n", obj, obj_class->PrettyDescriptor().c_str(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002179 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08002180 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002181 mirror::Class* klass = obj->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07002182 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj,
2183 mirror::Class::PrettyDescriptor(klass).c_str())
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002184 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08002185 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002186 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07002187 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002188 } else {
David Sehr709b0702016-10-13 09:12:37 -07002189 os << StringPrintf("%p: %s\n", obj, obj_class->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002190 }
Andreas Gampe0c183382017-07-13 22:26:24 -07002191 ScopedIndentation indent1(&vios_);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002192 DumpFields(os, obj, obj_class);
Andreas Gampe0c183382017-07-13 22:26:24 -07002193 const PointerSize image_pointer_size = image_header_.GetPointerSize();
Ian Rogersd5b32602012-02-26 16:40:04 -08002194 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002195 auto* obj_array = obj->AsObjectArray<mirror::Object>();
2196 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002197 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002198 size_t run = 0;
2199 for (int32_t j = i + 1; j < length; j++) {
2200 if (value == obj_array->Get(j)) {
2201 run++;
2202 } else {
2203 break;
2204 }
2205 }
2206 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002207 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002208 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002209 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08002210 i = i + run;
2211 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002212 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002213 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002214 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08002215 }
2216 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07002217 mirror::Class* klass = obj->AsClass();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002218 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002219 os << "STATICS:\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002220 ScopedIndentation indent2(&vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002221 for (ArtField& field : klass->GetSFields()) {
2222 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08002223 }
2224 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002225 } else {
Andreas Gampe0c183382017-07-13 22:26:24 -07002226 auto it = dex_caches_.find(obj);
2227 if (it != dex_caches_.end()) {
Vladimir Marko05792b92015-08-03 11:56:49 +01002228 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Andreas Gampe0c183382017-07-13 22:26:24 -07002229 const auto& field_section = image_header_.GetImageSection(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002230 ImageHeader::kSectionArtFields);
Andreas Gampe0c183382017-07-13 22:26:24 -07002231 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01002232 size_t num_methods = dex_cache->NumResolvedMethods();
2233 if (num_methods != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002234 os << "Methods (size=" << num_methods << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002235 ScopedIndentation indent2(&vios_);
Vladimir Marko05792b92015-08-03 11:56:49 +01002236 auto* resolved_methods = dex_cache->GetResolvedMethods();
2237 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002238 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_methods,
2239 i,
2240 image_pointer_size);
Vladimir Marko05792b92015-08-03 11:56:49 +01002241 size_t run = 0;
2242 for (size_t j = i + 1;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002243 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_methods,
2244 j,
2245 image_pointer_size);
2246 ++j) {
2247 ++run;
2248 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002249 if (run == 0) {
2250 os << StringPrintf("%zd: ", i);
2251 } else {
2252 os << StringPrintf("%zd to %zd: ", i, i + run);
2253 i = i + run;
2254 }
2255 std::string msg;
2256 if (elem == nullptr) {
2257 msg = "null";
2258 } else if (method_section.Contains(
Andreas Gampe0c183382017-07-13 22:26:24 -07002259 reinterpret_cast<uint8_t*>(elem) - image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002260 msg = reinterpret_cast<ArtMethod*>(elem)->PrettyMethod();
Vladimir Marko05792b92015-08-03 11:56:49 +01002261 } else {
2262 msg = "<not in method section>";
2263 }
2264 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07002265 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002266 }
2267 size_t num_fields = dex_cache->NumResolvedFields();
2268 if (num_fields != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002269 os << "Fields (size=" << num_fields << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002270 ScopedIndentation indent2(&vios_);
Vladimir Marko05792b92015-08-03 11:56:49 +01002271 auto* resolved_fields = dex_cache->GetResolvedFields();
2272 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
Vladimir Markof44d36c2017-03-14 14:18:46 +00002273 auto* elem = mirror::DexCache::GetNativePairPtrSize(
2274 resolved_fields, i, image_pointer_size).object;
Vladimir Marko05792b92015-08-03 11:56:49 +01002275 size_t run = 0;
2276 for (size_t j = i + 1;
Vladimir Markof44d36c2017-03-14 14:18:46 +00002277 j != length &&
2278 elem == mirror::DexCache::GetNativePairPtrSize(
2279 resolved_fields, j, image_pointer_size).object;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002280 ++j) {
2281 ++run;
2282 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002283 if (run == 0) {
2284 os << StringPrintf("%zd: ", i);
2285 } else {
2286 os << StringPrintf("%zd to %zd: ", i, i + run);
2287 i = i + run;
2288 }
2289 std::string msg;
2290 if (elem == nullptr) {
2291 msg = "null";
2292 } else if (field_section.Contains(
Andreas Gampe0c183382017-07-13 22:26:24 -07002293 reinterpret_cast<uint8_t*>(elem) - image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002294 msg = reinterpret_cast<ArtField*>(elem)->PrettyField();
Vladimir Marko05792b92015-08-03 11:56:49 +01002295 } else {
2296 msg = "<not in field section>";
2297 }
2298 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002299 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002300 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002301 size_t num_types = dex_cache->NumResolvedTypes();
2302 if (num_types != 0u) {
2303 os << "Types (size=" << num_types << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002304 ScopedIndentation indent2(&vios_);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002305 auto* resolved_types = dex_cache->GetResolvedTypes();
2306 for (size_t i = 0; i < num_types; ++i) {
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002307 auto pair = resolved_types[i].load(std::memory_order_relaxed);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002308 size_t run = 0;
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002309 for (size_t j = i + 1; j != num_types; ++j) {
2310 auto other_pair = resolved_types[j].load(std::memory_order_relaxed);
2311 if (pair.index != other_pair.index ||
2312 pair.object.Read() != other_pair.object.Read()) {
2313 break;
2314 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002315 ++run;
2316 }
2317 if (run == 0) {
2318 os << StringPrintf("%zd: ", i);
2319 } else {
2320 os << StringPrintf("%zd to %zd: ", i, i + run);
2321 i = i + run;
2322 }
2323 std::string msg;
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002324 auto* elem = pair.object.Read();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002325 if (elem == nullptr) {
2326 msg = "null";
2327 } else {
David Sehr709b0702016-10-13 09:12:37 -07002328 msg = elem->PrettyClass();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002329 }
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002330 os << StringPrintf("%p %u %s\n", elem, pair.index, msg.c_str());
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002331 }
2332 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002333 }
2334 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002335 std::string temp;
Andreas Gampe0c183382017-07-13 22:26:24 -07002336 stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002337 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002338
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002339 void DumpMethod(ArtMethod* method, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002340 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002341 DCHECK(method != nullptr);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002342 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
2343 const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
Andreas Gampe542451c2016-07-26 09:02:02 -07002344 const PointerSize pointer_size = image_header_.GetPointerSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002345 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2346 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002347 if (method->IsNative()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002348 bool first_occurrence;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002349 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2350 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002351 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002352 stats_.native_to_managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002353 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002354 if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
2355 image_header_.GetPointerSize())) {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002356 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002357 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002358 } else if (method->IsAbstract() || method->IsClassInitializer()) {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002359 // Don't print information for these.
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002360 } else if (method->IsRuntimeMethod()) {
2361 ImtConflictTable* table = method->GetImtConflictTable(image_header_.GetPointerSize());
2362 if (table != nullptr) {
2363 indent_os << "IMT conflict table " << table << " method: ";
2364 for (size_t i = 0, count = table->NumEntries(pointer_size); i < count; ++i) {
David Sehr709b0702016-10-13 09:12:37 -07002365 indent_os << ArtMethod::PrettyMethod(table->GetImplementationMethod(i, pointer_size))
2366 << " ";
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002367 }
2368 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002369 } else {
2370 const DexFile::CodeItem* code_item = method->GetCodeItem();
2371 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002372 stats_.dex_instruction_bytes += dex_instruction_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002373
2374 bool first_occurrence;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002375 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002376 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01002377 // Method compiled with the optimizing compiler have no vmap table.
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002378 vmap_table_bytes = ComputeOatSize(method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002379 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002380 stats_.vmap_table_bytes += vmap_table_bytes;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002381 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002382 }
2383
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002384 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2385 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002386 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002387 stats_.managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002388 if (method->IsConstructor()) {
2389 if (method->IsStatic()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002390 stats_.class_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002391 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002392 stats_.large_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002393 }
2394 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002395 stats_.large_method_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002396 }
2397 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002398 stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002399
Igor Murashkin7617abd2015-07-10 18:27:47 -07002400 uint32_t method_access_flags = method->GetAccessFlags();
2401
Mathieu Chartiere401d142015-04-22 13:56:20 -07002402 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002403 indent_os << StringPrintf("SIZE: Dex Instructions=%zd StackMaps=%zd AccessFlags=0x%x\n",
2404 dex_instruction_bytes,
2405 vmap_table_bytes,
Igor Murashkin7617abd2015-07-10 18:27:47 -07002406 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002407
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002408 size_t total_size = dex_instruction_bytes +
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002409 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002410
2411 double expansion =
2412 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002413 stats_.ComputeOutliers(total_size, expansion, method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002414 }
2415 }
2416
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002417 std::set<const void*> already_seen_;
2418 // Compute the size of the given data within the oat file and whether this is the first time
2419 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002420 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002421 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002422 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002423 already_seen_.insert(oat_data);
2424 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002425 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002426 }
2427 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002428 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002429
2430 public:
2431 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002432 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002433 size_t file_bytes;
2434
2435 size_t header_bytes;
2436 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002437 size_t art_field_bytes;
2438 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01002439 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002440 size_t interned_strings_bytes;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002441 size_t class_table_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07002442 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002443 size_t alignment_bytes;
2444
2445 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002446 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002447 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07002448 size_t class_initializer_code_bytes;
2449 size_t large_initializer_code_bytes;
2450 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002451
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002452 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002453
2454 size_t dex_instruction_bytes;
2455
Mathieu Chartiere401d142015-04-22 13:56:20 -07002456 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002457 std::vector<size_t> method_outlier_size;
2458 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002459 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002460
Roland Levillain3887c462015-08-12 18:15:42 +01002461 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002462 : oat_file_bytes(0),
2463 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002464 header_bytes(0),
2465 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002466 art_field_bytes(0),
2467 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01002468 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002469 interned_strings_bytes(0),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002470 class_table_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07002471 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002472 alignment_bytes(0),
2473 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002474 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002475 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07002476 class_initializer_code_bytes(0),
2477 large_initializer_code_bytes(0),
2478 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002479 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002480 dex_instruction_bytes(0) {}
2481
Elliott Hughesa0e18062012-04-13 15:59:59 -07002482 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002483 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002484 size_t bytes;
2485 size_t count;
2486 };
2487 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2488 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002489
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002490 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002491 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2492 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002493 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002494 it->second.count += 1;
2495 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002496 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002497 }
2498 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002499
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002500 double PercentOfOatBytes(size_t size) {
2501 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2502 }
2503
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002504 double PercentOfFileBytes(size_t size) {
2505 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2506 }
2507
2508 double PercentOfObjectBytes(size_t size) {
2509 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2510 }
2511
Mathieu Chartiere401d142015-04-22 13:56:20 -07002512 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002513 method_outlier_size.push_back(total_size);
2514 method_outlier_expansion.push_back(expansion);
2515 method_outlier.push_back(method);
2516 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002517
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002518 void DumpOutliers(std::ostream& os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002519 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002520 size_t sum_of_sizes = 0;
2521 size_t sum_of_sizes_squared = 0;
2522 size_t sum_of_expansion = 0;
2523 size_t sum_of_expansion_squared = 0;
2524 size_t n = method_outlier_size.size();
Mathieu Chartier1ebf8d32016-06-09 11:51:27 -07002525 if (n <= 1) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08002526 return;
2527 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002528 for (size_t i = 0; i < n; i++) {
2529 size_t cur_size = method_outlier_size[i];
2530 sum_of_sizes += cur_size;
2531 sum_of_sizes_squared += cur_size * cur_size;
2532 double cur_expansion = method_outlier_expansion[i];
2533 sum_of_expansion += cur_expansion;
2534 sum_of_expansion_squared += cur_expansion * cur_expansion;
2535 }
2536 size_t size_mean = sum_of_sizes / n;
2537 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2538 double expansion_mean = sum_of_expansion / n;
2539 double expansion_variance =
2540 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2541
2542 // Dump methods whose size is a certain number of standard deviations from the mean
2543 size_t dumped_values = 0;
2544 size_t skipped_values = 0;
2545 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2546 size_t cur_size_variance = i * i * size_variance;
2547 bool first = true;
2548 for (size_t j = 0; j < n; j++) {
2549 size_t cur_size = method_outlier_size[j];
2550 if (cur_size > size_mean) {
2551 size_t cur_var = cur_size - size_mean;
2552 cur_var = cur_var * cur_var;
2553 if (cur_var > cur_size_variance) {
2554 if (dumped_values > 20) {
2555 if (i == 1) {
2556 skipped_values++;
2557 } else {
2558 i = 2; // jump to counting for 1 standard deviation
2559 break;
2560 }
2561 } else {
2562 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002563 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002564 first = false;
2565 }
David Sehr709b0702016-10-13 09:12:37 -07002566 os << ArtMethod::PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002567 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002568 method_outlier_size[j] = 0; // don't consider this method again
2569 dumped_values++;
2570 }
2571 }
2572 }
2573 }
2574 }
2575 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002576 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002577 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002578 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002579 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002580
2581 // Dump methods whose expansion is a certain number of standard deviations from the mean
2582 dumped_values = 0;
2583 skipped_values = 0;
2584 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2585 double cur_expansion_variance = i * i * expansion_variance;
2586 bool first = true;
2587 for (size_t j = 0; j < n; j++) {
2588 double cur_expansion = method_outlier_expansion[j];
2589 if (cur_expansion > expansion_mean) {
2590 size_t cur_var = cur_expansion - expansion_mean;
2591 cur_var = cur_var * cur_var;
2592 if (cur_var > cur_expansion_variance) {
2593 if (dumped_values > 20) {
2594 if (i == 1) {
2595 skipped_values++;
2596 } else {
2597 i = 2; // jump to counting for 1 standard deviation
2598 break;
2599 }
2600 } else {
2601 if (first) {
2602 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002603 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002604 first = false;
2605 }
David Sehr709b0702016-10-13 09:12:37 -07002606 os << ArtMethod::PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002607 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002608 method_outlier_expansion[j] = 0.0; // don't consider this method again
2609 dumped_values++;
2610 }
2611 }
2612 }
2613 }
2614 }
2615 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002616 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002617 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002618 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002619 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002620 }
2621
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002622 void Dump(std::ostream& os, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002623 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002624 {
2625 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2626 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002627 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2628 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2629 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2630 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2631 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2632 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002633 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002634 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2635 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002636 header_bytes, PercentOfFileBytes(header_bytes),
2637 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002638 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2639 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002640 dex_cache_arrays_bytes,
2641 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002642 interned_strings_bytes,
2643 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002644 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002645 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002646 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2647 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002648 CHECK_EQ(file_bytes,
2649 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2650 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2651 bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002652 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002653
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002654 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002655 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002656 for (const auto& sizes_and_count : sizes_and_counts) {
2657 const std::string& descriptor(sizes_and_count.first);
2658 double average = static_cast<double>(sizes_and_count.second.bytes) /
2659 static_cast<double>(sizes_and_count.second.count);
2660 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002661 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002662 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002663 descriptor.c_str(), sizes_and_count.second.bytes,
2664 sizes_and_count.second.count, average, percent);
2665 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002666 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002667 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002668 CHECK_EQ(object_bytes, object_bytes_total);
2669
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002670 os << StringPrintf("oat_file_bytes = %8zd\n"
2671 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002672 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2673 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2674 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2675 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002676 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002677 managed_code_bytes,
2678 PercentOfOatBytes(managed_code_bytes),
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002679 native_to_managed_code_bytes,
2680 PercentOfOatBytes(native_to_managed_code_bytes),
2681 class_initializer_code_bytes,
2682 PercentOfOatBytes(class_initializer_code_bytes),
2683 large_initializer_code_bytes,
2684 PercentOfOatBytes(large_initializer_code_bytes),
2685 large_method_code_bytes,
2686 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002687 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002688 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002689 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002690 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2691 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002692 }
2693
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002694 os << "\n" << StringPrintf("vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002695 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002696 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002697
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002698 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2699 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002700 static_cast<double>(managed_code_bytes) /
2701 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002702 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002703 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002704 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002705
2706 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002707 }
2708 } stats_;
2709
2710 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002711 enum {
2712 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2713 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2714 kLargeConstructorDexBytes = 4000,
2715 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2716 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2717 kLargeMethodDexBytes = 16000
2718 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002719
2720 // For performance, use the *os_ directly for anything that doesn't need indentation
2721 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002722 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002723 VariableIndentationOutputStream vios_;
2724 ScopedIndentation indent1_;
2725
Ian Rogers1d54e732013-05-02 21:10:01 -07002726 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002727 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002728 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002729 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002730 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002731
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002732 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002733};
2734
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002735static int DumpImage(gc::space::ImageSpace* image_space,
2736 OatDumperOptions* options,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002737 std::ostream* os) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002738 const ImageHeader& image_header = image_space->GetImageHeader();
2739 if (!image_header.IsValid()) {
2740 fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
2741 return EXIT_FAILURE;
2742 }
2743 ImageDumper image_dumper(os, *image_space, image_header, options);
2744 if (!image_dumper.Dump()) {
2745 return EXIT_FAILURE;
2746 }
2747 return EXIT_SUCCESS;
2748}
2749
2750static int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002751 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002752 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002753 options->class_loader_ = &null_class_loader;
2754
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002755 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002756 if (options->app_image_ != nullptr) {
2757 if (options->app_oat_ == nullptr) {
2758 LOG(ERROR) << "Can not dump app image without app oat file";
Jeff Haodcdc85b2015-12-04 14:06:18 -08002759 return EXIT_FAILURE;
2760 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002761 // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
2762 // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
2763 // pointers into 32 bit pointer sized ArtMethods.
2764 std::string error_msg;
2765 std::unique_ptr<OatFile> oat_file(OatFile::Open(options->app_oat_,
2766 options->app_oat_,
2767 nullptr,
2768 nullptr,
2769 false,
2770 /*low_4gb*/true,
2771 nullptr,
2772 &error_msg));
2773 if (oat_file == nullptr) {
2774 LOG(ERROR) << "Failed to open oat file " << options->app_oat_ << " with error " << error_msg;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002775 return EXIT_FAILURE;
2776 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002777 std::unique_ptr<gc::space::ImageSpace> space(
2778 gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg));
2779 if (space == nullptr) {
2780 LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error "
2781 << error_msg;
2782 }
2783 // Open dex files for the image.
2784 std::vector<std::unique_ptr<const DexFile>> dex_files;
2785 if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
2786 LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
2787 << error_msg;
2788 }
2789 // Dump the actual image.
2790 int result = DumpImage(space.get(), options, os);
2791 if (result != EXIT_SUCCESS) {
2792 return result;
2793 }
2794 // Fall through to dump the boot images.
2795 }
2796
2797 gc::Heap* heap = runtime->GetHeap();
2798 CHECK(heap->HasBootImageSpace()) << "No image spaces";
2799 for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
2800 int result = DumpImage(image_space, options, os);
2801 if (result != EXIT_SUCCESS) {
2802 return result;
2803 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002804 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002805 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002806}
2807
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002808static jobject InstallOatFile(Runtime* runtime,
2809 std::unique_ptr<OatFile> oat_file,
2810 std::vector<const DexFile*>* class_path)
2811 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002812 Thread* self = Thread::Current();
2813 CHECK(self != nullptr);
2814 // Need well-known-classes.
2815 WellKnownClasses::Init(self->GetJniEnv());
2816
2817 // Need to register dex files to get a working dex cache.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002818 OatFile* oat_file_ptr = oat_file.get();
Andreas Gampe00b25f32014-09-17 21:49:05 -07002819 ClassLinker* class_linker = runtime->GetClassLinker();
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002820 runtime->GetOatFileManager().RegisterOatFile(std::move(oat_file));
2821 for (const OatFile::OatDexFile* odf : oat_file_ptr->GetOatDexFiles()) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002822 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002823 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002824 CHECK(dex_file != nullptr) << error_msg;
Vladimir Markocd556b02017-02-03 11:47:34 +00002825 ObjPtr<mirror::DexCache> dex_cache =
2826 class_linker->RegisterDexFile(*dex_file, nullptr);
2827 CHECK(dex_cache != nullptr);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002828 class_path->push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002829 }
2830
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002831 // Need a class loader. Fake that we're a compiler.
2832 // Note: this will run initializers through the unstarted runtime, so make sure it's
2833 // initialized.
2834 interpreter::UnstartedRuntime::Initialize();
2835
2836 jobject class_loader = class_linker->CreatePathClassLoader(self, *class_path);
2837
2838 return class_loader;
2839}
2840
2841static int DumpOatWithRuntime(Runtime* runtime,
2842 std::unique_ptr<OatFile> oat_file,
2843 OatDumperOptions* options,
2844 std::ostream* os) {
2845 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2846 ScopedObjectAccess soa(Thread::Current());
2847
2848 OatFile* oat_file_ptr = oat_file.get();
2849 std::vector<const DexFile*> class_path;
2850 jobject class_loader = InstallOatFile(runtime, std::move(oat_file), &class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002851
2852 // Use the class loader while dumping.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002853 StackHandleScope<1> scope(soa.Self());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002854 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07002855 soa.Decode<mirror::ClassLoader>(class_loader));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002856 options->class_loader_ = &loader_handle;
2857
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002858 OatDumper oat_dumper(*oat_file_ptr, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002859 bool success = oat_dumper.Dump(*os);
2860 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2861}
2862
2863static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002864 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002865 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002866 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002867 options->class_loader_ = &null_class_loader;
2868
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002869 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002870 bool success = oat_dumper.Dump(*os);
2871 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2872}
2873
2874static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2875 std::ostream* os) {
2876 std::string error_msg;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002877 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
2878 oat_filename,
2879 nullptr,
2880 nullptr,
2881 false,
2882 /*low_4gb*/false,
2883 nullptr,
2884 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002885 if (oat_file == nullptr) {
2886 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2887 return EXIT_FAILURE;
2888 }
2889
2890 if (runtime != nullptr) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002891 return DumpOatWithRuntime(runtime, std::move(oat_file), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002892 } else {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002893 return DumpOatWithoutRuntime(oat_file.get(), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002894 }
2895}
2896
David Srbecky2fdd03c2016-03-10 15:32:37 +00002897static int SymbolizeOat(const char* oat_filename, std::string& output_name, bool no_bits) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002898 std::string error_msg;
Andreas Gampe08c277c2017-04-26 22:22:15 -07002899 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
2900 oat_filename,
2901 nullptr,
2902 nullptr,
2903 false,
2904 /*low_4gb*/false,
2905 nullptr,
2906 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002907 if (oat_file == nullptr) {
2908 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2909 return EXIT_FAILURE;
2910 }
2911
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002912 bool result;
2913 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
2914 // files for 64-bit code in the past.
2915 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
Andreas Gampe08c277c2017-04-26 22:22:15 -07002916 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file.get(), output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002917 result = oat_symbolizer.Symbolize();
2918 } else {
Andreas Gampe08c277c2017-04-26 22:22:15 -07002919 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file.get(), output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002920 result = oat_symbolizer.Symbolize();
2921 }
2922 if (!result) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002923 fprintf(stderr, "Failed to symbolize\n");
2924 return EXIT_FAILURE;
2925 }
2926
2927 return EXIT_SUCCESS;
2928}
2929
Andreas Gampe9fded872016-09-25 16:08:35 -07002930class IMTDumper {
2931 public:
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002932 static bool Dump(Runtime* runtime,
2933 const std::string& imt_file,
2934 bool dump_imt_stats,
2935 const char* oat_filename) {
2936 Thread* self = Thread::Current();
2937
2938 ScopedObjectAccess soa(self);
2939 StackHandleScope<1> scope(self);
2940 MutableHandle<mirror::ClassLoader> class_loader = scope.NewHandle<mirror::ClassLoader>(nullptr);
2941 std::vector<const DexFile*> class_path;
2942
2943 if (oat_filename != nullptr) {
2944 std::string error_msg;
2945 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
2946 oat_filename,
2947 nullptr,
2948 nullptr,
2949 false,
2950 /*low_4gb*/false,
2951 nullptr,
2952 &error_msg));
2953 if (oat_file == nullptr) {
2954 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2955 return false;
2956 }
2957
2958 class_loader.Assign(soa.Decode<mirror::ClassLoader>(
2959 InstallOatFile(runtime, std::move(oat_file), &class_path)));
2960 } else {
2961 class_loader.Assign(nullptr); // Boot classloader. Just here for explicit documentation.
2962 class_path = runtime->GetClassLinker()->GetBootClassPath();
2963 }
2964
2965 if (!imt_file.empty()) {
2966 return DumpImt(runtime, imt_file, class_loader);
2967 }
2968
2969 if (dump_imt_stats) {
2970 return DumpImtStats(runtime, class_path, class_loader);
2971 }
2972
2973 LOG(FATAL) << "Should not reach here";
2974 UNREACHABLE();
2975 }
2976
2977 private:
2978 static bool DumpImt(Runtime* runtime,
2979 const std::string& imt_file,
2980 Handle<mirror::ClassLoader> h_class_loader)
2981 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002982 std::vector<std::string> lines = ReadCommentedInputFromFile(imt_file);
2983 std::unordered_set<std::string> prepared;
2984
2985 for (const std::string& line : lines) {
2986 // A line should be either a class descriptor, in which case we will dump the complete IMT,
2987 // or a class descriptor and an interface method, in which case we will lookup the method,
2988 // determine its IMT slot, and check the class' IMT.
2989 size_t first_space = line.find(' ');
2990 if (first_space == std::string::npos) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002991 DumpIMTForClass(runtime, line, h_class_loader, &prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07002992 } else {
2993 DumpIMTForMethod(runtime,
2994 line.substr(0, first_space),
2995 line.substr(first_space + 1, std::string::npos),
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002996 h_class_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07002997 &prepared);
2998 }
2999 std::cerr << std::endl;
3000 }
3001
3002 return true;
3003 }
3004
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003005 static bool DumpImtStats(Runtime* runtime,
3006 const std::vector<const DexFile*>& dex_files,
3007 Handle<mirror::ClassLoader> h_class_loader)
3008 REQUIRES_SHARED(Locks::mutator_lock_) {
3009 size_t without_imt = 0;
3010 size_t with_imt = 0;
Andreas Gampe9fded872016-09-25 16:08:35 -07003011 std::map<size_t, size_t> histogram;
3012
3013 ClassLinker* class_linker = runtime->GetClassLinker();
3014 const PointerSize pointer_size = class_linker->GetImagePointerSize();
3015 std::unordered_set<std::string> prepared;
3016
3017 Thread* self = Thread::Current();
Andreas Gampe9fded872016-09-25 16:08:35 -07003018 StackHandleScope<1> scope(self);
3019 MutableHandle<mirror::Class> h_klass(scope.NewHandle<mirror::Class>(nullptr));
3020
3021 for (const DexFile* dex_file : dex_files) {
3022 for (uint32_t class_def_index = 0;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003023 class_def_index != dex_file->NumClassDefs();
3024 ++class_def_index) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003025 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
3026 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003027 h_klass.Assign(class_linker->FindClass(self, descriptor, h_class_loader));
Andreas Gampefa4333d2017-02-14 11:10:34 -08003028 if (h_klass == nullptr) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003029 std::cerr << "Warning: could not load " << descriptor << std::endl;
3030 continue;
3031 }
3032
3033 if (HasNoIMT(runtime, h_klass, pointer_size, &prepared)) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003034 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003035 continue;
3036 }
3037
3038 ImTable* im_table = PrepareAndGetImTable(runtime, h_klass, pointer_size, &prepared);
3039 if (im_table == nullptr) {
3040 // Should not happen, but accept.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003041 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003042 continue;
3043 }
3044
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003045 with_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003046 for (size_t imt_index = 0; imt_index != ImTable::kSize; ++imt_index) {
3047 ArtMethod* ptr = im_table->Get(imt_index, pointer_size);
3048 if (ptr->IsRuntimeMethod()) {
3049 if (ptr->IsImtUnimplementedMethod()) {
3050 histogram[0]++;
3051 } else {
3052 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3053 histogram[current_table->NumEntries(pointer_size)]++;
3054 }
3055 } else {
3056 histogram[1]++;
3057 }
3058 }
3059 }
3060 }
3061
3062 std::cerr << "IMT stats:"
3063 << std::endl << std::endl;
3064
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003065 std::cerr << " " << with_imt << " classes with IMT."
Andreas Gampe9fded872016-09-25 16:08:35 -07003066 << std::endl << std::endl;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003067 std::cerr << " " << without_imt << " classes without IMT (or copy from Object)."
Andreas Gampe9fded872016-09-25 16:08:35 -07003068 << std::endl << std::endl;
3069
3070 double sum_one = 0;
3071 size_t count_one = 0;
3072
3073 std::cerr << " " << "IMT histogram" << std::endl;
3074 for (auto& bucket : histogram) {
3075 std::cerr << " " << bucket.first << " " << bucket.second << std::endl;
3076 if (bucket.first > 0) {
3077 sum_one += bucket.second * bucket.first;
3078 count_one += bucket.second;
3079 }
3080 }
3081
3082 double count_zero = count_one + histogram[0];
3083 std::cerr << " Stats:" << std::endl;
3084 std::cerr << " Average depth (including empty): " << (sum_one / count_zero) << std::endl;
3085 std::cerr << " Average depth (excluding empty): " << (sum_one / count_one) << std::endl;
3086
3087 return true;
3088 }
3089
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003090 // Return whether the given class has no IMT (or the one shared with java.lang.Object).
Andreas Gampe9fded872016-09-25 16:08:35 -07003091 static bool HasNoIMT(Runtime* runtime,
3092 Handle<mirror::Class> klass,
3093 const PointerSize pointer_size,
3094 std::unordered_set<std::string>* prepared)
3095 REQUIRES_SHARED(Locks::mutator_lock_) {
3096 if (klass->IsObjectClass() || !klass->ShouldHaveImt()) {
3097 return true;
3098 }
3099
3100 if (klass->GetImt(pointer_size) == nullptr) {
3101 PrepareClass(runtime, klass, prepared);
3102 }
3103
3104 mirror::Class* object_class = mirror::Class::GetJavaLangClass()->GetSuperClass();
3105 DCHECK(object_class->IsObjectClass());
3106
3107 bool result = klass->GetImt(pointer_size) == object_class->GetImt(pointer_size);
3108
Mathieu Chartier6beced42016-11-15 15:51:31 -08003109 if (klass->GetIfTable()->Count() == 0) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003110 DCHECK(result);
3111 }
3112
3113 return result;
3114 }
3115
3116 static void PrintTable(ImtConflictTable* table, PointerSize pointer_size)
3117 REQUIRES_SHARED(Locks::mutator_lock_) {
3118 if (table == nullptr) {
3119 std::cerr << " <No IMT?>" << std::endl;
3120 return;
3121 }
3122 size_t table_index = 0;
3123 for (;;) {
3124 ArtMethod* ptr = table->GetInterfaceMethod(table_index, pointer_size);
3125 if (ptr == nullptr) {
3126 return;
3127 }
3128 table_index++;
David Sehr709b0702016-10-13 09:12:37 -07003129 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003130 }
3131 }
3132
3133 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3134 Thread* self,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003135 Handle<mirror::ClassLoader> h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003136 const std::string& class_name,
3137 const PointerSize pointer_size,
3138 mirror::Class** klass_out,
3139 std::unordered_set<std::string>* prepared)
3140 REQUIRES_SHARED(Locks::mutator_lock_) {
3141 if (class_name.empty()) {
3142 return nullptr;
3143 }
3144
3145 std::string descriptor;
3146 if (class_name[0] == 'L') {
3147 descriptor = class_name;
3148 } else {
3149 descriptor = DotToDescriptor(class_name.c_str());
3150 }
3151
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003152 mirror::Class* klass = runtime->GetClassLinker()->FindClass(self, descriptor.c_str(), h_loader);
Andreas Gampe9fded872016-09-25 16:08:35 -07003153
3154 if (klass == nullptr) {
3155 self->ClearException();
3156 std::cerr << "Did not find " << class_name << std::endl;
3157 *klass_out = nullptr;
3158 return nullptr;
3159 }
3160
3161 StackHandleScope<1> scope(Thread::Current());
3162 Handle<mirror::Class> h_klass = scope.NewHandle<mirror::Class>(klass);
3163
3164 ImTable* ret = PrepareAndGetImTable(runtime, h_klass, pointer_size, prepared);
3165 *klass_out = h_klass.Get();
3166 return ret;
3167 }
3168
3169 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3170 Handle<mirror::Class> h_klass,
3171 const PointerSize pointer_size,
3172 std::unordered_set<std::string>* prepared)
3173 REQUIRES_SHARED(Locks::mutator_lock_) {
3174 PrepareClass(runtime, h_klass, prepared);
3175 return h_klass->GetImt(pointer_size);
3176 }
3177
3178 static void DumpIMTForClass(Runtime* runtime,
3179 const std::string& class_name,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003180 Handle<mirror::ClassLoader> h_loader,
3181 std::unordered_set<std::string>* prepared)
3182 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003183 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
3184 mirror::Class* klass;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003185 ImTable* imt = PrepareAndGetImTable(runtime,
3186 Thread::Current(),
3187 h_loader,
3188 class_name,
3189 pointer_size,
3190 &klass,
3191 prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07003192 if (imt == nullptr) {
3193 return;
3194 }
3195
3196 std::cerr << class_name << std::endl << " IMT:" << std::endl;
3197 for (size_t index = 0; index < ImTable::kSize; ++index) {
3198 std::cerr << " " << index << ":" << std::endl;
3199 ArtMethod* ptr = imt->Get(index, pointer_size);
3200 if (ptr->IsRuntimeMethod()) {
3201 if (ptr->IsImtUnimplementedMethod()) {
3202 std::cerr << " <empty>" << std::endl;
3203 } else {
3204 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3205 PrintTable(current_table, pointer_size);
3206 }
3207 } else {
David Sehr709b0702016-10-13 09:12:37 -07003208 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003209 }
3210 }
3211
3212 std::cerr << " Interfaces:" << std::endl;
3213 // Run through iftable, find methods that slot here, see if they fit.
3214 mirror::IfTable* if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003215 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
3216 mirror::Class* iface = if_table->GetInterface(i);
3217 std::string iface_name;
3218 std::cerr << " " << iface->GetDescriptor(&iface_name) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003219
Mathieu Chartier6beced42016-11-15 15:51:31 -08003220 for (ArtMethod& iface_method : iface->GetVirtualMethods(pointer_size)) {
3221 uint32_t class_hash, name_hash, signature_hash;
3222 ImTable::GetImtHashComponents(&iface_method, &class_hash, &name_hash, &signature_hash);
3223 uint32_t imt_slot = ImTable::GetImtIndex(&iface_method);
3224 std::cerr << " " << iface_method.PrettyMethod(true)
3225 << " slot=" << imt_slot
3226 << std::hex
3227 << " class_hash=0x" << class_hash
3228 << " name_hash=0x" << name_hash
3229 << " signature_hash=0x" << signature_hash
3230 << std::dec
3231 << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003232 }
3233 }
3234 }
3235
3236 static void DumpIMTForMethod(Runtime* runtime,
3237 const std::string& class_name,
3238 const std::string& method,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003239 Handle<mirror::ClassLoader> h_loader,
3240 std::unordered_set<std::string>* prepared)
3241 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003242 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
3243 mirror::Class* klass;
3244 ImTable* imt = PrepareAndGetImTable(runtime,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003245 Thread::Current(),
3246 h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003247 class_name,
3248 pointer_size,
3249 &klass,
3250 prepared);
3251 if (imt == nullptr) {
3252 return;
3253 }
3254
3255 std::cerr << class_name << " <" << method << ">" << std::endl;
3256 for (size_t index = 0; index < ImTable::kSize; ++index) {
3257 ArtMethod* ptr = imt->Get(index, pointer_size);
3258 if (ptr->IsRuntimeMethod()) {
3259 if (ptr->IsImtUnimplementedMethod()) {
3260 continue;
3261 }
3262
3263 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3264 if (current_table == nullptr) {
3265 continue;
3266 }
3267
3268 size_t table_index = 0;
3269 for (;;) {
3270 ArtMethod* ptr2 = current_table->GetInterfaceMethod(table_index, pointer_size);
3271 if (ptr2 == nullptr) {
3272 break;
3273 }
3274 table_index++;
3275
David Sehr709b0702016-10-13 09:12:37 -07003276 std::string p_name = ptr2->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003277 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003278 std::cerr << " Slot "
3279 << index
3280 << " ("
3281 << current_table->NumEntries(pointer_size)
3282 << ")"
3283 << std::endl;
3284 PrintTable(current_table, pointer_size);
3285 return;
3286 }
3287 }
3288 } else {
David Sehr709b0702016-10-13 09:12:37 -07003289 std::string p_name = ptr->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003290 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003291 std::cerr << " Slot " << index << " (1)" << std::endl;
3292 std::cerr << " " << p_name << std::endl;
3293 } else {
3294 // Run through iftable, find methods that slot here, see if they fit.
3295 mirror::IfTable* if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003296 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
3297 mirror::Class* iface = if_table->GetInterface(i);
3298 size_t num_methods = iface->NumDeclaredVirtualMethods();
3299 if (num_methods > 0) {
3300 for (ArtMethod& iface_method : iface->GetMethods(pointer_size)) {
3301 if (ImTable::GetImtIndex(&iface_method) == index) {
3302 std::string i_name = iface_method.PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003303 if (android::base::StartsWith(i_name, method.c_str())) {
Mathieu Chartier6beced42016-11-15 15:51:31 -08003304 std::cerr << " Slot " << index << " (1)" << std::endl;
3305 std::cerr << " " << p_name << " (" << i_name << ")" << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003306 }
3307 }
3308 }
3309 }
3310 }
3311 }
3312 }
3313 }
3314 }
3315
3316 // Read lines from the given stream, dropping comments and empty lines
3317 static std::vector<std::string> ReadCommentedInputStream(std::istream& in_stream) {
3318 std::vector<std::string> output;
3319 while (in_stream.good()) {
3320 std::string dot;
3321 std::getline(in_stream, dot);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003322 if (android::base::StartsWith(dot, "#") || dot.empty()) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003323 continue;
3324 }
3325 output.push_back(dot);
3326 }
3327 return output;
3328 }
3329
3330 // Read lines from the given file, dropping comments and empty lines.
3331 static std::vector<std::string> ReadCommentedInputFromFile(const std::string& input_filename) {
3332 std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
3333 if (input_file.get() == nullptr) {
3334 LOG(ERROR) << "Failed to open input file " << input_filename;
3335 return std::vector<std::string>();
3336 }
3337 std::vector<std::string> result = ReadCommentedInputStream(*input_file);
3338 input_file->close();
3339 return result;
3340 }
3341
3342 // Prepare a class, i.e., ensure it has a filled IMT. Will do so recursively for superclasses,
3343 // and note in the given set that the work was done.
3344 static void PrepareClass(Runtime* runtime,
3345 Handle<mirror::Class> h_klass,
3346 std::unordered_set<std::string>* done)
3347 REQUIRES_SHARED(Locks::mutator_lock_) {
3348 if (!h_klass->ShouldHaveImt()) {
3349 return;
3350 }
3351
3352 std::string name;
3353 name = h_klass->GetDescriptor(&name);
3354
3355 if (done->find(name) != done->end()) {
3356 return;
3357 }
3358 done->insert(name);
3359
3360 if (h_klass->HasSuperClass()) {
3361 StackHandleScope<1> h(Thread::Current());
3362 PrepareClass(runtime, h.NewHandle<mirror::Class>(h_klass->GetSuperClass()), done);
3363 }
3364
3365 if (!h_klass->IsTemp()) {
3366 runtime->GetClassLinker()->FillIMTAndConflictTables(h_klass.Get());
3367 }
3368 }
3369};
3370
Igor Murashkin37743352014-11-13 14:38:00 -08003371struct OatdumpArgs : public CmdlineArgs {
3372 protected:
3373 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003374
Igor Murashkin37743352014-11-13 14:38:00 -08003375 virtual ParseStatus ParseCustom(const StringPiece& option,
3376 std::string* error_msg) OVERRIDE {
3377 {
3378 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
3379 if (base_parse != kParseUnknownArgument) {
3380 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003381 }
3382 }
3383
Igor Murashkin37743352014-11-13 14:38:00 -08003384 if (option.starts_with("--oat-file=")) {
3385 oat_filename_ = option.substr(strlen("--oat-file=")).data();
3386 } else if (option.starts_with("--image=")) {
3387 image_location_ = option.substr(strlen("--image=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003388 } else if (option == "--no-dump:vmap") {
3389 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01003390 } else if (option =="--dump:code_info_stack_maps") {
3391 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003392 } else if (option == "--no-disassemble") {
3393 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003394 } else if (option =="--header-only") {
3395 dump_header_only_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003396 } else if (option.starts_with("--symbolize=")) {
3397 oat_filename_ = option.substr(strlen("--symbolize=")).data();
3398 symbolize_ = true;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003399 } else if (option.starts_with("--only-keep-debug")) {
3400 only_keep_debug_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003401 } else if (option.starts_with("--class-filter=")) {
3402 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003403 } else if (option.starts_with("--method-filter=")) {
3404 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003405 } else if (option.starts_with("--list-classes")) {
3406 list_classes_ = true;
3407 } else if (option.starts_with("--list-methods")) {
3408 list_methods_ = true;
3409 } else if (option.starts_with("--export-dex-to=")) {
3410 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
3411 } else if (option.starts_with("--addr2instr=")) {
3412 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
3413 *error_msg = "Address conversion failed";
3414 return kParseError;
3415 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003416 } else if (option.starts_with("--app-image=")) {
3417 app_image_ = option.substr(strlen("--app-image=")).data();
3418 } else if (option.starts_with("--app-oat=")) {
3419 app_oat_ = option.substr(strlen("--app-oat=")).data();
Andreas Gampe9fded872016-09-25 16:08:35 -07003420 } else if (option.starts_with("--dump-imt=")) {
3421 imt_dump_ = option.substr(strlen("--dump-imt=")).data();
3422 } else if (option == "--dump-imt-stats") {
3423 imt_stat_dump_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003424 } else {
3425 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003426 }
3427
Igor Murashkin37743352014-11-13 14:38:00 -08003428 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003429 }
3430
Igor Murashkin37743352014-11-13 14:38:00 -08003431 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
3432 // Infer boot image location from the image location if possible.
3433 if (boot_image_location_ == nullptr) {
3434 boot_image_location_ = image_location_;
3435 }
3436
3437 // Perform the parent checks.
3438 ParseStatus parent_checks = Base::ParseChecks(error_msg);
3439 if (parent_checks != kParseOk) {
3440 return parent_checks;
3441 }
3442
3443 // Perform our own checks.
3444 if (image_location_ == nullptr && oat_filename_ == nullptr) {
3445 *error_msg = "Either --image or --oat-file must be specified";
3446 return kParseError;
3447 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
3448 *error_msg = "Either --image or --oat-file must be specified but not both";
3449 return kParseError;
3450 }
3451
3452 return kParseOk;
3453 }
3454
3455 virtual std::string GetUsage() const {
3456 std::string usage;
3457
3458 usage +=
3459 "Usage: oatdump [options] ...\n"
3460 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
3461 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
3462 "\n"
3463 // Either oat-file or image is required.
3464 " --oat-file=<file.oat>: specifies an input oat filename.\n"
3465 " Example: --oat-file=/system/framework/boot.oat\n"
3466 "\n"
3467 " --image=<file.art>: specifies an input image location.\n"
3468 " Example: --image=/system/framework/boot.art\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003469 "\n"
3470 " --app-image=<file.art>: specifies an input app image. Must also have a specified\n"
Roland Levillain4f1c9e62017-06-28 16:44:30 +01003471 " boot image (with --image) and app oat file (with --app-oat).\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003472 " Example: --app-image=app.art\n"
3473 "\n"
3474 " --app-oat=<file.odex>: specifies an input app oat.\n"
3475 " Example: --app-oat=app.odex\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003476 "\n";
3477
3478 usage += Base::GetUsage();
3479
3480 usage += // Optional.
Igor Murashkin37743352014-11-13 14:38:00 -08003481 " --no-dump:vmap may be used to disable vmap dumping.\n"
3482 " Example: --no-dump:vmap\n"
3483 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01003484 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
3485 " Example: --dump:code_info_stack_maps\n"
3486 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003487 " --no-disassemble may be used to disable disassembly.\n"
3488 " Example: --no-disassemble\n"
3489 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00003490 " --header-only may be used to print only the oat header.\n"
3491 " Example: --header-only\n"
3492 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003493 " --list-classes may be used to list target file classes (can be used with filters).\n"
3494 " Example: --list-classes\n"
3495 " Example: --list-classes --class-filter=com.example.foo\n"
3496 "\n"
3497 " --list-methods may be used to list target file methods (can be used with filters).\n"
3498 " Example: --list-methods\n"
3499 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
3500 "\n"
3501 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
3502 " Example: --symbolize=/system/framework/boot.oat\n"
3503 "\n"
David Srbecky2fdd03c2016-03-10 15:32:37 +00003504 " --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
3505 " .rodata and .text sections are omitted in the output file to save space.\n"
3506 " Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
3507 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003508 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
3509 " Example: --class-filter=com.example.foo\n"
3510 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003511 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
3512 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003513 "\n"
3514 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
3515 " Example: --export-dex-to=/data/local/tmp\n"
3516 "\n"
3517 " --addr2instr=<address>: output matching method disassembled code from relative\n"
3518 " address (e.g. PC from crash dump)\n"
3519 " Example: --addr2instr=0x00001a3b\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003520 "\n"
3521 " --dump-imt=<file.txt>: output IMT collisions (if any) for the given receiver\n"
3522 " types and interface methods in the given file. The file\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003523 " is read line-wise, where each line should either be a class\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003524 " name or descriptor, or a class name/descriptor and a prefix\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003525 " of a complete method name (separated by a whitespace).\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003526 " Example: --dump-imt=imt.txt\n"
3527 "\n"
3528 " --dump-imt-stats: output IMT statistics for the given boot image\n"
3529 " Example: --dump-imt-stats"
Igor Murashkin37743352014-11-13 14:38:00 -08003530 "\n";
3531
3532 return usage;
3533 }
3534
3535 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07003536 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003537 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00003538 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07003539 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003540 std::string elf_filename_prefix_;
Andreas Gampe9fded872016-09-25 16:08:35 -07003541 std::string imt_dump_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003542 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01003543 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003544 bool disassemble_code_ = true;
3545 bool symbolize_ = false;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003546 bool only_keep_debug_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003547 bool list_classes_ = false;
3548 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003549 bool dump_header_only_ = false;
Andreas Gampe9fded872016-09-25 16:08:35 -07003550 bool imt_stat_dump_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003551 uint32_t addr2instr_ = 0;
3552 const char* export_dex_location_ = nullptr;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003553 const char* app_image_ = nullptr;
3554 const char* app_oat_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003555};
3556
Igor Murashkin37743352014-11-13 14:38:00 -08003557struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
3558 virtual bool NeedsRuntime() OVERRIDE {
3559 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003560
Igor Murashkin37743352014-11-13 14:38:00 -08003561 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
3562 bool absolute_addresses = (args_->oat_filename_ == nullptr);
3563
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003564 oat_dumper_options_.reset(new OatDumperOptions(
Igor Murashkin37743352014-11-13 14:38:00 -08003565 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01003566 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08003567 args_->disassemble_code_,
3568 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003569 args_->class_filter_,
3570 args_->method_filter_,
3571 args_->list_classes_,
3572 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00003573 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003574 args_->export_dex_location_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003575 args_->app_image_,
3576 args_->app_oat_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003577 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08003578
Andreas Gampe9fded872016-09-25 16:08:35 -07003579 return (args_->boot_image_location_ != nullptr ||
3580 args_->image_location_ != nullptr ||
3581 !args_->imt_dump_.empty()) &&
Igor Murashkin37743352014-11-13 14:38:00 -08003582 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003583 }
3584
Igor Murashkin37743352014-11-13 14:38:00 -08003585 virtual bool ExecuteWithoutRuntime() OVERRIDE {
3586 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08003587 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003588
Mathieu Chartierd424d082014-10-15 10:31:46 -07003589 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08003590
Andreas Gampec24f3992014-12-17 20:40:11 -08003591 if (args_->symbolize_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00003592 // ELF has special kind of section called SHT_NOBITS which allows us to create
3593 // sections which exist but their data is omitted from the ELF file to save space.
3594 // This is what "strip --only-keep-debug" does when it creates separate ELF file
3595 // with only debug data. We use it in similar way to exclude .rodata and .text.
3596 bool no_bits = args_->only_keep_debug_;
3597 return SymbolizeOat(args_->oat_filename_, args_->output_name_, no_bits) == EXIT_SUCCESS;
Andreas Gampec24f3992014-12-17 20:40:11 -08003598 } else {
3599 return DumpOat(nullptr,
3600 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003601 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08003602 args_->os_) == EXIT_SUCCESS;
3603 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07003604 }
3605
Igor Murashkin37743352014-11-13 14:38:00 -08003606 virtual bool ExecuteWithRuntime(Runtime* runtime) {
3607 CHECK(args_ != nullptr);
3608
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003609 if (!args_->imt_dump_.empty() || args_->imt_stat_dump_) {
3610 return IMTDumper::Dump(runtime,
3611 args_->imt_dump_,
3612 args_->imt_stat_dump_,
3613 args_->oat_filename_);
Andreas Gampe9fded872016-09-25 16:08:35 -07003614 }
3615
Igor Murashkin37743352014-11-13 14:38:00 -08003616 if (args_->oat_filename_ != nullptr) {
3617 return DumpOat(runtime,
3618 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003619 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08003620 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003621 }
Igor Murashkin37743352014-11-13 14:38:00 -08003622
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003623 return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003624 }
3625
Igor Murashkin37743352014-11-13 14:38:00 -08003626 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
3627};
Andreas Gampe00b25f32014-09-17 21:49:05 -07003628
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003629} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07003630
3631int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08003632 art::OatdumpMain main;
3633 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07003634}