blob: 6f833c6e69ddd79053668a683034414a255d935a [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"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "class_linker-inl.h"
Steven Morelande431e272017-07-18 16:53:49 -070038#include "class_linker.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"
Steven Morelande431e272017-07-18 16:53:49 -070060#include "nativehelper/ScopedLocalRef.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080061#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010062#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070063#include "oat_file_manager.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080064#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070065#include "safe_map.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070066#include "scoped_thread_state_change-inl.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",
Mingyao Yang0a87a652017-04-12 13:43:15 -070092 "kSaveEverythingMethodForClinit",
93 "kSaveEverythingMethodForSuspendCheck",
Mathieu Chartiere401d142015-04-22 13:56:20 -070094};
95
96const char* image_roots_descriptions_[] = {
Brian Carlstrom58ae9412011-10-04 00:56:06 -070097 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070098 "kClassRoots",
Vladimir Markoeca3eda2016-11-09 16:26:44 +000099 "kClassLoader",
Brian Carlstrom78128a62011-09-15 17:21:19 -0700100};
101
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700102// Map is so that we don't allocate multiple dex files for the same OatDexFile.
103static std::map<const OatFile::OatDexFile*,
104 std::unique_ptr<const DexFile>> opened_dex_files;
105
106const DexFile* OpenDexFile(const OatFile::OatDexFile* oat_dex_file, std::string* error_msg) {
107 DCHECK(oat_dex_file != nullptr);
108 auto it = opened_dex_files.find(oat_dex_file);
109 if (it != opened_dex_files.end()) {
110 return it->second.get();
111 }
112 const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
113 opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
114 return ret;
115}
116
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800117template <typename ElfTypes>
David Srbeckybc90fd02015-04-22 19:40:27 +0100118class OatSymbolizer FINAL {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700119 public:
David Srbecky2fdd03c2016-03-10 15:32:37 +0000120 OatSymbolizer(const OatFile* oat_file, const std::string& output_name, bool no_bits) :
121 oat_file_(oat_file),
122 builder_(nullptr),
123 output_name_(output_name.empty() ? "symbolized.oat" : output_name),
124 no_bits_(no_bits) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700125 }
126
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700127 bool Symbolize() {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000128 const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800129 std::unique_ptr<const InstructionSetFeatures> features = InstructionSetFeatures::FromBitmap(
David Srbecky5d811202016-03-08 13:21:22 +0000130 isa, oat_file_->GetOatHeader().GetInstructionSetFeaturesBitmap());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000131
Andreas Gampe08c277c2017-04-26 22:22:15 -0700132 std::unique_ptr<File> elf_file(OS::CreateEmptyFile(output_name_.c_str()));
133 if (elf_file == nullptr) {
134 return false;
135 }
Andreas Gampe8bdda5a2017-06-08 15:30:36 -0700136 std::unique_ptr<BufferedOutputStream> output_stream =
137 std::make_unique<BufferedOutputStream>(std::make_unique<FileOutputStream>(elf_file.get()));
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800138 builder_.reset(new ElfBuilder<ElfTypes>(isa, features.get(), output_stream.get()));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000139
140 builder_->Start();
141
142 auto* rodata = builder_->GetRoData();
143 auto* text = builder_->GetText();
144 auto* bss = builder_->GetBss();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000145
David Srbecky6d8c8f02015-10-26 10:57:09 +0000146 const uint8_t* rodata_begin = oat_file_->Begin();
147 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
David Srbecky2fdd03c2016-03-10 15:32:37 +0000148 if (no_bits_) {
149 rodata->WriteNoBitsSection(rodata_size);
150 } else {
151 rodata->Start();
152 rodata->WriteFully(rodata_begin, rodata_size);
153 rodata->End();
154 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000155
David Srbecky6d8c8f02015-10-26 10:57:09 +0000156 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
157 const size_t text_size = oat_file_->End() - text_begin;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000158 if (no_bits_) {
159 text->WriteNoBitsSection(text_size);
160 } else {
161 text->Start();
162 text->WriteFully(text_begin, text_size);
163 text->End();
164 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000165
166 if (oat_file_->BssSize() != 0) {
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000167 bss->WriteNoBitsSection(oat_file_->BssSize());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000168 }
169
Douglas Leung316a2182015-09-17 15:26:25 -0700170 if (isa == kMips || isa == kMips64) {
171 builder_->WriteMIPSabiflagsSection();
172 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000173 builder_->PrepareDynamicSection(elf_file->GetPath(),
174 rodata_size,
175 text_size,
176 oat_file_->BssSize(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100177 oat_file_->BssMethodsOffset(),
Vladimir Markoaad75c62016-10-03 08:46:48 +0000178 oat_file_->BssRootsOffset());
Vladimir Marko944da602016-02-19 12:27:55 +0000179 builder_->WriteDynamicSection();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700180
David Srbecky5d950762016-03-07 20:47:29 +0000181 Walk();
182 for (const auto& trampoline : debug::MakeTrampolineInfos(oat_file_->GetOatHeader())) {
183 method_debug_infos_.push_back(trampoline);
184 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700185
David Srbecky5d950762016-03-07 20:47:29 +0000186 debug::WriteDebugInfo(builder_.get(),
187 ArrayRef<const debug::MethodDebugInfo>(method_debug_infos_),
188 dwarf::DW_DEBUG_FRAME_FORMAT,
189 true /* write_oat_patches */);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700190
David Srbecky6d8c8f02015-10-26 10:57:09 +0000191 builder_->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700192
Andreas Gampe08c277c2017-04-26 22:22:15 -0700193 bool ret_value = builder_->Good();
194
195 builder_.reset();
196 output_stream.reset();
197
198 if (elf_file->FlushCloseOrErase() != 0) {
199 return false;
200 }
201 elf_file.reset();
202
203 return ret_value;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700204 }
205
David Srbecky5d950762016-03-07 20:47:29 +0000206 void Walk() {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700207 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
208 for (size_t i = 0; i < oat_dex_files.size(); i++) {
209 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700210 CHECK(oat_dex_file != nullptr);
David Srbecky5d950762016-03-07 20:47:29 +0000211 WalkOatDexFile(oat_dex_file);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700212 }
213 }
214
David Srbecky5d950762016-03-07 20:47:29 +0000215 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700216 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700217 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
218 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700219 return;
220 }
221 for (size_t class_def_index = 0;
222 class_def_index < dex_file->NumClassDefs();
223 class_def_index++) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700224 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
225 OatClassType type = oat_class.GetType();
226 switch (type) {
227 case kOatClassAllCompiled:
228 case kOatClassSomeCompiled:
David Srbecky5d950762016-03-07 20:47:29 +0000229 WalkOatClass(oat_class, *dex_file, class_def_index);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700230 break;
231
232 case kOatClassNoneCompiled:
233 case kOatClassMax:
234 // Ignore.
235 break;
236 }
237 }
238 }
239
David Srbecky5d950762016-03-07 20:47:29 +0000240 void WalkOatClass(const OatFile::OatClass& oat_class,
241 const DexFile& dex_file,
242 uint32_t class_def_index) {
243 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700244 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700245 if (class_data == nullptr) { // empty class such as a marker interface?
246 return;
247 }
248 // Note: even if this is an interface or a native class, we still have to walk it, as there
249 // might be a static initializer.
250 ClassDataItemIterator it(dex_file, class_data);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700251 uint32_t class_method_idx = 0;
Mathieu Chartiere17cf242017-06-19 11:05:51 -0700252 it.SkipAllFields();
David Srbecky5d950762016-03-07 20:47:29 +0000253 for (; it.HasNextDirectMethod() || it.HasNextVirtualMethod(); it.Next()) {
254 WalkOatMethod(oat_class.GetOatMethod(class_method_idx++),
255 dex_file,
256 class_def_index,
257 it.GetMemberIndex(),
258 it.GetMethodCodeItem(),
259 it.GetMethodAccessFlags());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700260 }
261 DCHECK(!it.HasNext());
262 }
263
David Srbecky5d950762016-03-07 20:47:29 +0000264 void WalkOatMethod(const OatFile::OatMethod& oat_method,
265 const DexFile& dex_file,
266 uint32_t class_def_index,
267 uint32_t dex_method_index,
268 const DexFile::CodeItem* code_item,
269 uint32_t method_access_flags) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700270 if ((method_access_flags & kAccAbstract) != 0) {
271 // Abstract method, no code.
272 return;
273 }
David Srbecky5d950762016-03-07 20:47:29 +0000274 const OatHeader& oat_header = oat_file_->GetOatHeader();
275 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
276 if (method_header == nullptr || method_header->GetCodeSize() == 0) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700277 // No code.
278 return;
279 }
280
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100281 uint32_t entry_point = oat_method.GetCodeOffset() - oat_header.GetExecutableOffset();
282 // Clear Thumb2 bit.
283 const void* code_address = EntryPointToCodePointer(reinterpret_cast<void*>(entry_point));
284
David Srbecky5d950762016-03-07 20:47:29 +0000285 debug::MethodDebugInfo info = debug::MethodDebugInfo();
286 info.trampoline_name = nullptr;
287 info.dex_file = &dex_file;
288 info.class_def_index = class_def_index;
289 info.dex_method_index = dex_method_index;
290 info.access_flags = method_access_flags;
291 info.code_item = code_item;
292 info.isa = oat_header.GetInstructionSet();
293 info.deduped = !seen_offsets_.insert(oat_method.GetCodeOffset()).second;
294 info.is_native_debuggable = oat_header.IsNativeDebuggable();
295 info.is_optimized = method_header->IsOptimized();
296 info.is_code_address_text_relative = true;
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100297 info.code_address = reinterpret_cast<uintptr_t>(code_address);
David Srbecky5d950762016-03-07 20:47:29 +0000298 info.code_size = method_header->GetCodeSize();
299 info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
300 info.code_info = info.is_optimized ? method_header->GetOptimizedCodeInfoPtr() : nullptr;
301 info.cfi = ArrayRef<uint8_t>();
302 method_debug_infos_.push_back(info);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700303 }
304
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700305 private:
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700306 const OatFile* oat_file_;
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800307 std::unique_ptr<ElfBuilder<ElfTypes> > builder_;
David Srbecky5d950762016-03-07 20:47:29 +0000308 std::vector<debug::MethodDebugInfo> method_debug_infos_;
309 std::unordered_set<uint32_t> seen_offsets_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700310 const std::string output_name_;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000311 bool no_bits_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700312};
313
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700314class OatDumperOptions {
315 public:
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100316 OatDumperOptions(bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100317 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700318 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700319 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800320 const char* class_filter,
321 const char* method_filter,
322 bool list_classes,
323 bool list_methods,
David Brazdilc03d7b62016-03-02 12:18:03 +0000324 bool dump_header_only,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800325 const char* export_dex_location,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800326 const char* app_image,
327 const char* app_oat,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800328 uint32_t addr2instr)
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100329 : dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100330 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700331 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700332 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800333 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000334 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800335 list_classes_(list_classes),
336 list_methods_(list_methods),
David Brazdilc03d7b62016-03-02 12:18:03 +0000337 dump_header_only_(dump_header_only),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800338 export_dex_location_(export_dex_location),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800339 app_image_(app_image),
340 app_oat_(app_oat),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800341 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800342 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700343
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700344 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100345 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700346 const bool disassemble_code_;
347 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800348 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000349 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800350 const bool list_classes_;
351 const bool list_methods_;
David Brazdilc03d7b62016-03-02 12:18:03 +0000352 const bool dump_header_only_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800353 const char* const export_dex_location_;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800354 const char* const app_image_;
355 const char* const app_oat_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800356 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700357 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700358};
359
Elliott Hughese3c845c2012-02-28 17:23:01 -0800360class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700361 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100362 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000363 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800364 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700365 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800366 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800367 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
368 disassembler_(Disassembler::Create(instruction_set_,
Andreas Gampe372f3a32016-08-19 10:49:06 -0700369 new DisassemblerOptions(
370 options_.absolute_addresses_,
371 oat_file.Begin(),
372 oat_file.End(),
373 true /* can_read_literals_ */,
374 Is64BitInstructionSet(instruction_set_)
375 ? &Thread::DumpThreadOffset<PointerSize::k64>
376 : &Thread::DumpThreadOffset<PointerSize::k32>))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800377 CHECK(options_.class_loader_ != nullptr);
378 CHECK(options_.class_filter_ != nullptr);
379 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800380 AddAllOffsets();
381 }
382
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700383 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700384 delete disassembler_;
385 }
386
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800387 InstructionSet GetInstructionSet() {
388 return instruction_set_;
389 }
390
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700391 bool Dump(std::ostream& os) {
392 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800393 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700394
395 os << "MAGIC:\n";
396 os << oat_header.GetMagic() << "\n\n";
397
Jeff Hao4b07a6e2016-01-15 12:50:44 -0800398 os << "LOCATION:\n";
399 os << oat_file_.GetLocation() << "\n\n";
400
Brian Carlstromaded5f72011-10-07 17:15:04 -0700401 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800402 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700403
Elliott Hughesa72ec822012-03-05 17:12:22 -0800404 os << "INSTRUCTION SET:\n";
405 os << oat_header.GetInstructionSet() << "\n\n";
406
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700407 {
408 std::unique_ptr<const InstructionSetFeatures> features(
409 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
410 oat_header.GetInstructionSetFeaturesBitmap()));
411 os << "INSTRUCTION SET FEATURES:\n";
412 os << features->GetFeatureString() << "\n\n";
413 }
Dave Allison70202782013-10-22 17:52:19 -0700414
Brian Carlstromaded5f72011-10-07 17:15:04 -0700415 os << "DEX FILE COUNT:\n";
416 os << oat_header.GetDexFileCount() << "\n\n";
417
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800418#define DUMP_OAT_HEADER_OFFSET(label, offset) \
419 os << label " OFFSET:\n"; \
420 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800421 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800422 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
423 } \
424 os << StringPrintf("\n\n");
425
426 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
427 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
428 GetInterpreterToInterpreterBridgeOffset);
429 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
430 GetInterpreterToCompiledCodeBridgeOffset);
431 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
432 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800433 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
434 GetQuickGenericJniTrampolineOffset);
435 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
436 GetQuickImtConflictTrampolineOffset);
437 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
438 GetQuickResolutionTrampolineOffset);
439 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
440 GetQuickToInterpreterBridgeOffset);
441#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700442
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700443 os << "IMAGE PATCH DELTA:\n";
444 os << StringPrintf("%d (0x%08x)\n\n",
445 oat_header.GetImagePatchDelta(),
446 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700447
Brian Carlstrom28db0122012-10-18 16:20:41 -0700448 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
449 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
450
451 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800452 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700453
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700454 // Print the key-value store.
455 {
456 os << "KEY VALUE STORE:\n";
457 size_t index = 0;
458 const char* key;
459 const char* value;
460 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
461 os << key << " = " << value << "\n";
462 index++;
463 }
464 os << "\n";
465 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700466
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800467 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700468 os << "BEGIN:\n";
469 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700470
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700471 os << "END:\n";
472 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
473 }
474
475 os << "SIZE:\n";
476 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700477
478 os << std::flush;
479
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800480 // If set, adjust relative address to be searched
481 if (options_.addr2instr_ != 0) {
482 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
483 os << "SEARCH ADDRESS (executable offset + input):\n";
484 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
485 }
486
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700487 // Dumping the dex file overview is compact enough to do even if header only.
488 DexFileData cumulative;
489 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
490 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
491 CHECK(oat_dex_file != nullptr);
492 std::string error_msg;
493 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
494 if (dex_file == nullptr) {
495 os << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation() << "': "
496 << error_msg;
497 continue;
498 }
499 DexFileData data(*dex_file);
500 os << "Dex file data for " << dex_file->GetLocation() << "\n";
501 data.Dump(os);
502 os << "\n";
Mathieu Chartier120aa282017-08-05 16:03:03 -0700503 const DexLayoutSections* const layout_sections = oat_dex_file->GetDexLayoutSections();
504 if (layout_sections != nullptr) {
505 os << "Layout data\n";
506 os << *layout_sections;
507 os << "\n";
508 }
509
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700510 cumulative.Add(data);
511 }
512 os << "Cumulative dex file data\n";
513 cumulative.Dump(os);
514 os << "\n";
515
David Brazdilc03d7b62016-03-02 12:18:03 +0000516 if (!options_.dump_header_only_) {
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000517 VariableIndentationOutputStream vios(&os);
518 VdexFile::Header vdex_header = oat_file_.GetVdexFile()->GetHeader();
519 if (vdex_header.IsValid()) {
520 std::string error_msg;
521 std::vector<const DexFile*> dex_files;
522 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
523 const DexFile* dex_file = OpenDexFile(oat_dex_files_[i], &error_msg);
524 if (dex_file == nullptr) {
525 os << "Error opening dex file: " << error_msg << std::endl;
526 return false;
527 }
528 dex_files.push_back(dex_file);
529 }
530 verifier::VerifierDeps deps(dex_files, oat_file_.GetVdexFile()->GetVerifierDepsData());
531 deps.Dump(&vios);
532 } else {
533 os << "UNRECOGNIZED vdex file, magic "
534 << vdex_header.GetMagic()
535 << ", version "
536 << vdex_header.GetVersion()
537 << "\n";
538 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000539 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
540 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
541 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800542
David Brazdilc03d7b62016-03-02 12:18:03 +0000543 // If file export selected skip file analysis
544 if (options_.export_dex_location_) {
545 if (!ExportDexFile(os, *oat_dex_file)) {
546 success = false;
547 }
548 } else {
549 if (!DumpOatDexFile(os, *oat_dex_file)) {
550 success = false;
551 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800552 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700553 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700554 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000555
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800556 {
557 os << "OAT FILE STATS:\n";
558 VariableIndentationOutputStream vios(&os);
559 stats_.Dump(vios);
560 }
561
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700562 os << std::flush;
563 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700564 }
565
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800566 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700567 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
568 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800569 return 0; // Address not in oat file
570 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800571 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
572 reinterpret_cast<uintptr_t>(oat_file_.Begin());
573 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800574 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800575 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800576 return end_offset - begin_offset;
577 }
578
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800579 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700580 return oat_file_.GetOatHeader().GetInstructionSet();
581 }
582
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700583 const void* GetQuickOatCode(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800584 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
585 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700586 CHECK(oat_dex_file != nullptr);
587 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700588 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
589 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700590 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
591 << "': " << error_msg;
592 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800593 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700594 const DexFile::ClassDef* class_def =
David Sehr9aa352e2016-09-15 18:13:52 -0700595 OatDexFile::FindClassDef(*dex_file, descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700596 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700597 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100598 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800599 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100600 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800601 }
602 }
603 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700604 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800605 }
606
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800607 struct Stats {
608 enum ByteKind {
609 kByteKindCode,
610 kByteKindQuickMethodHeader,
611 kByteKindCodeInfoLocationCatalog,
612 kByteKindCodeInfoDexRegisterMap,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800613 kByteKindCodeInfoEncoding,
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800614 kByteKindCodeInfoInvokeInfo,
David Srbecky45aa5982016-03-18 02:15:09 +0000615 kByteKindCodeInfoStackMasks,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800616 kByteKindCodeInfoRegisterMasks,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800617 kByteKindStackMapNativePc,
618 kByteKindStackMapDexPc,
619 kByteKindStackMapDexRegisterMap,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800620 kByteKindStackMapInlineInfoIndex,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800621 kByteKindStackMapRegisterMaskIndex,
David Srbecky45aa5982016-03-18 02:15:09 +0000622 kByteKindStackMapStackMaskIndex,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700623 kByteKindInlineInfoMethodIndexIdx,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800624 kByteKindInlineInfoDexPc,
625 kByteKindInlineInfoExtraData,
626 kByteKindInlineInfoDexRegisterMap,
627 kByteKindInlineInfoIsLast,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800628 kByteKindCount,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800629 // Special ranges for std::accumulate convenience.
630 kByteKindStackMapFirst = kByteKindStackMapNativePc,
David Srbecky45aa5982016-03-18 02:15:09 +0000631 kByteKindStackMapLast = kByteKindStackMapStackMaskIndex,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700632 kByteKindInlineInfoFirst = kByteKindInlineInfoMethodIndexIdx,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800633 kByteKindInlineInfoLast = kByteKindInlineInfoIsLast,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800634 };
635 int64_t bits[kByteKindCount] = {};
636 // Since code has deduplication, seen tracks already seen pointers to avoid double counting
637 // deduplicated code and tables.
638 std::unordered_set<const void*> seen;
639
640 // Returns true if it was newly added.
641 bool AddBitsIfUnique(ByteKind kind, int64_t count, const void* address) {
642 if (seen.insert(address).second == true) {
643 // True means the address was not already in the set.
644 AddBits(kind, count);
645 return true;
646 }
647 return false;
648 }
649
650 void AddBits(ByteKind kind, int64_t count) {
651 bits[kind] += count;
652 }
653
654 void Dump(VariableIndentationOutputStream& os) {
655 const int64_t sum = std::accumulate(bits, bits + kByteKindCount, 0u);
656 os.Stream() << "Dumping cumulative use of " << sum / kBitsPerByte << " accounted bytes\n";
657 if (sum > 0) {
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800658 Dump(os, "Code ", bits[kByteKindCode], sum);
659 Dump(os, "QuickMethodHeader ", bits[kByteKindQuickMethodHeader], sum);
660 Dump(os, "CodeInfoEncoding ", bits[kByteKindCodeInfoEncoding], sum);
661 Dump(os, "CodeInfoLocationCatalog ", bits[kByteKindCodeInfoLocationCatalog], sum);
662 Dump(os, "CodeInfoDexRegisterMap ", bits[kByteKindCodeInfoDexRegisterMap], sum);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800663 Dump(os, "CodeInfoStackMasks ", bits[kByteKindCodeInfoStackMasks], sum);
664 Dump(os, "CodeInfoRegisterMasks ", bits[kByteKindCodeInfoRegisterMasks], sum);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800665 Dump(os, "CodeInfoInvokeInfo ", bits[kByteKindCodeInfoInvokeInfo], sum);
Mathieu Chartier1e083792017-02-08 13:30:04 -0800666 // Stack map section.
667 const int64_t stack_map_bits = std::accumulate(bits + kByteKindStackMapFirst,
668 bits + kByteKindStackMapLast + 1,
669 0u);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800670 Dump(os, "CodeInfoStackMap ", stack_map_bits, sum);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800671 {
672 ScopedIndentation indent1(&os);
673 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800674 "StackMapNativePc ",
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800675 bits[kByteKindStackMapNativePc],
676 stack_map_bits,
677 "stack map");
678 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800679 "StackMapDexPcEncoding ",
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800680 bits[kByteKindStackMapDexPc],
681 stack_map_bits,
682 "stack map");
683 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800684 "StackMapDexRegisterMap ",
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800685 bits[kByteKindStackMapDexRegisterMap],
686 stack_map_bits,
687 "stack map");
688 Dump(os,
Mathieu Chartier697dc662017-02-08 16:56:48 -0800689 "StackMapInlineInfoIndex ",
Mathieu Chartier1e083792017-02-08 13:30:04 -0800690 bits[kByteKindStackMapInlineInfoIndex],
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800691 stack_map_bits,
692 "stack map");
693 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800694 "StackMapRegisterMaskIndex ",
695 bits[kByteKindStackMapRegisterMaskIndex],
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800696 stack_map_bits,
697 "stack map");
698 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800699 "StackMapStackMaskIndex ",
David Srbecky45aa5982016-03-18 02:15:09 +0000700 bits[kByteKindStackMapStackMaskIndex],
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800701 stack_map_bits,
702 "stack map");
703 }
Mathieu Chartier1e083792017-02-08 13:30:04 -0800704 // Inline info section.
705 const int64_t inline_info_bits = std::accumulate(bits + kByteKindInlineInfoFirst,
706 bits + kByteKindInlineInfoLast + 1,
707 0u);
708 Dump(os, "CodeInfoInlineInfo ", inline_info_bits, sum);
709 {
710 ScopedIndentation indent1(&os);
711 Dump(os,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700712 "InlineInfoMethodIndexIdx ",
713 bits[kByteKindInlineInfoMethodIndexIdx],
Mathieu Chartier1e083792017-02-08 13:30:04 -0800714 inline_info_bits,
715 "inline info");
716 Dump(os,
717 "InlineInfoDexPc ",
718 bits[kByteKindStackMapDexPc],
719 inline_info_bits,
720 "inline info");
721 Dump(os,
722 "InlineInfoExtraData ",
723 bits[kByteKindInlineInfoExtraData],
724 inline_info_bits,
725 "inline info");
726 Dump(os,
727 "InlineInfoDexRegisterMap ",
728 bits[kByteKindInlineInfoDexRegisterMap],
729 inline_info_bits,
730 "inline info");
731 Dump(os,
732 "InlineInfoIsLast ",
733 bits[kByteKindInlineInfoIsLast],
734 inline_info_bits,
735 "inline info");
736 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800737 }
738 os.Stream() << "\n" << std::flush;
739 }
740
741 private:
742 void Dump(VariableIndentationOutputStream& os,
743 const char* name,
744 int64_t size,
745 int64_t total,
746 const char* sum_of = "total") {
747 const double percent = (static_cast<double>(size) / static_cast<double>(total)) * 100;
748 os.Stream() << StringPrintf("%s = %8" PRId64 " (%2.0f%% of %s)\n",
749 name,
750 size / kBitsPerByte,
751 percent,
752 sum_of);
753 }
754 };
755
Brian Carlstromaded5f72011-10-07 17:15:04 -0700756 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800757 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800758 // We don't know the length of the code for each method, but we need to know where to stop
759 // when disassembling. What we do know is that a region of code will be followed by some other
760 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
761 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800762 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
763 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700764 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700765 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700766 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
767 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700768 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
769 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800770 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800771 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800772 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800773 for (size_t class_def_index = 0;
774 class_def_index < dex_file->NumClassDefs();
775 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800776 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100777 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700778 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700779 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800780 ClassDataItemIterator it(*dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -0700781 it.SkipAllFields();
Elliott Hughese3c845c2012-02-28 17:23:01 -0800782 uint32_t class_method_index = 0;
783 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100784 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800785 it.Next();
786 }
787 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100788 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800789 it.Next();
790 }
791 }
792 }
793 }
794
795 // If the last thing in the file is code for a method, there won't be an offset for the "next"
796 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
797 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800798 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800799 }
800
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700801 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
802 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
803 }
804
Elliott Hughese3c845c2012-02-28 17:23:01 -0800805 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800806 uint32_t code_offset = oat_method.GetCodeOffset();
807 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
808 code_offset &= ~0x1;
809 }
810 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800811 offsets_.insert(oat_method.GetVmapTableOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800812 }
813
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700814 // Dex file data, may be for multiple different dex files.
815 class DexFileData {
816 public:
817 DexFileData() {}
818
819 explicit DexFileData(const DexFile& dex_file)
820 : num_string_ids_(dex_file.NumStringIds()),
821 num_method_ids_(dex_file.NumMethodIds()),
822 num_field_ids_(dex_file.NumFieldIds()),
823 num_type_ids_(dex_file.NumTypeIds()),
824 num_class_defs_(dex_file.NumClassDefs()) {
825 for (size_t class_def_index = 0; class_def_index < num_class_defs_; ++class_def_index) {
826 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
827 WalkClass(dex_file, class_def);
828 }
829 }
830
831 void Add(const DexFileData& other) {
832 AddAll(unique_string_ids_from_code_, other.unique_string_ids_from_code_);
833 num_string_ids_from_code_ += other.num_string_ids_from_code_;
834 AddAll(dex_code_item_ptrs_, other.dex_code_item_ptrs_);
835 dex_code_bytes_ += other.dex_code_bytes_;
836 num_string_ids_ += other.num_string_ids_;
837 num_method_ids_ += other.num_method_ids_;
838 num_field_ids_ += other.num_field_ids_;
839 num_type_ids_ += other.num_type_ids_;
840 num_class_defs_ += other.num_class_defs_;
841 }
842
843 void Dump(std::ostream& os) {
844 os << "Num string ids: " << num_string_ids_ << "\n";
845 os << "Num method ids: " << num_method_ids_ << "\n";
846 os << "Num field ids: " << num_field_ids_ << "\n";
847 os << "Num type ids: " << num_type_ids_ << "\n";
848 os << "Num class defs: " << num_class_defs_ << "\n";
849 os << "Unique strings loaded from dex code: " << unique_string_ids_from_code_.size() << "\n";
850 os << "Total strings loaded from dex code: " << num_string_ids_from_code_ << "\n";
851 os << "Number of unique dex code items: " << dex_code_item_ptrs_.size() << "\n";
852 os << "Total number of dex code bytes: " << dex_code_bytes_ << "\n";
853 }
854
855 private:
Andreas Gampe9186ced2016-12-12 14:28:21 -0800856 // All of the elements from one container to another.
857 template <typename Dest, typename Src>
858 static void AddAll(Dest& dest, const Src& src) {
859 dest.insert(src.begin(), src.end());
860 }
861
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700862 void WalkClass(const DexFile& dex_file, const DexFile::ClassDef& class_def) {
863 const uint8_t* class_data = dex_file.GetClassData(class_def);
864 if (class_data == nullptr) { // empty class such as a marker interface?
865 return;
866 }
867 ClassDataItemIterator it(dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -0700868 it.SkipAllFields();
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700869 while (it.HasNextDirectMethod()) {
870 WalkCodeItem(dex_file, it.GetMethodCodeItem());
871 it.Next();
872 }
873 while (it.HasNextVirtualMethod()) {
874 WalkCodeItem(dex_file, it.GetMethodCodeItem());
875 it.Next();
876 }
877 DCHECK(!it.HasNext());
878 }
879
880 void WalkCodeItem(const DexFile& dex_file, const DexFile::CodeItem* code_item) {
881 if (code_item == nullptr) {
882 return;
883 }
884 const size_t code_item_size = code_item->insns_size_in_code_units_;
885 const uint16_t* code_ptr = code_item->insns_;
886 const uint16_t* code_end = code_item->insns_ + code_item_size;
887
888 // If we inserted a new dex code item pointer, add to total code bytes.
889 if (dex_code_item_ptrs_.insert(code_ptr).second) {
890 dex_code_bytes_ += code_item_size * sizeof(code_ptr[0]);
891 }
892
893 while (code_ptr < code_end) {
894 const Instruction* inst = Instruction::At(code_ptr);
895 switch (inst->Opcode()) {
896 case Instruction::CONST_STRING: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800897 const dex::StringIndex string_index(inst->VRegB_21c());
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700898 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
899 ++num_string_ids_from_code_;
900 break;
901 }
902 case Instruction::CONST_STRING_JUMBO: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800903 const dex::StringIndex string_index(inst->VRegB_31c());
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700904 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
905 ++num_string_ids_from_code_;
906 break;
907 }
908 default:
909 break;
910 }
911
912 code_ptr += inst->SizeInCodeUnits();
913 }
914 }
915
916 // Unique string ids loaded from dex code.
917 std::set<StringReference, StringReferenceComparator> unique_string_ids_from_code_;
918
919 // Total string ids loaded from dex code.
920 size_t num_string_ids_from_code_ = 0;
921
922 // Unique code pointers.
923 std::set<const void*> dex_code_item_ptrs_;
924
925 // Total "unique" dex code bytes.
926 size_t dex_code_bytes_ = 0;
927
928 // Other dex ids.
929 size_t num_string_ids_ = 0;
930 size_t num_method_ids_ = 0;
931 size_t num_field_ids_ = 0;
932 size_t num_type_ids_ = 0;
933 size_t num_class_defs_ = 0;
934 };
935
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700936 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
937 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800938 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700939 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800940 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800941 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700942
Andreas Gampe2ba88952016-04-29 17:52:07 -0700943 const uint8_t* const oat_file_begin = oat_dex_file.GetOatFile()->Begin();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100944 const uint8_t* const vdex_file_begin = oat_dex_file.GetOatFile()->DexBegin();
945
946 // Print data range of the dex file embedded inside the corresponding vdex file.
Andreas Gampe2ba88952016-04-29 17:52:07 -0700947 const uint8_t* const dex_file_pointer = oat_dex_file.GetDexFilePointer();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100948 uint32_t dex_offset = dchecked_integral_cast<uint32_t>(dex_file_pointer - vdex_file_begin);
Andreas Gampe2ba88952016-04-29 17:52:07 -0700949 os << StringPrintf("dex-file: 0x%08x..0x%08x\n",
950 dex_offset,
951 dchecked_integral_cast<uint32_t>(dex_offset + oat_dex_file.FileSize() - 1));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700952
Andreas Gampe2ba88952016-04-29 17:52:07 -0700953 // Create the dex file early. A lot of print-out things depend on it.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700954 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700955 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
956 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700957 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700958 os << std::flush;
959 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700960 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100961
Andreas Gampe2ba88952016-04-29 17:52:07 -0700962 // Print lookup table, if it exists.
963 if (oat_dex_file.GetLookupTableData() != nullptr) {
964 uint32_t table_offset = dchecked_integral_cast<uint32_t>(
965 oat_dex_file.GetLookupTableData() - oat_file_begin);
David Sehr9aa352e2016-09-15 18:13:52 -0700966 uint32_t table_size = TypeLookupTable::RawDataLength(dex_file->NumClassDefs());
Andreas Gampe2ba88952016-04-29 17:52:07 -0700967 os << StringPrintf("type-table: 0x%08x..0x%08x\n",
968 table_offset,
969 table_offset + table_size - 1);
970 }
971
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100972 VariableIndentationOutputStream vios(&os);
973 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800974 for (size_t class_def_index = 0;
975 class_def_index < dex_file->NumClassDefs();
976 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700977 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
978 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800979
980 // TODO: Support regex
981 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
982 continue;
983 }
984
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700985 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100986 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700987 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
Andreas Gampea5b09a62016-11-17 15:21:22 -0800988 class_def_index, descriptor, oat_class_offset, class_def.class_idx_.index_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100989 << " (" << oat_class.GetStatus() << ")"
990 << " (" << oat_class.GetType() << ")\n";
991 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700992 if (options_.list_classes_) {
993 continue;
994 }
995 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700996 success = false;
997 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800998 if (stop_analysis) {
999 os << std::flush;
1000 return success;
1001 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001002 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001003 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001004 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001005 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001006 }
1007
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001008 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
1009 std::string error_msg;
1010 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
1011
Mathieu Chartierac8f4392015-08-27 13:54:20 -07001012 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001013 if (dex_file == nullptr) {
1014 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
1015 return false;
1016 }
1017 size_t fsize = oat_dex_file.FileSize();
1018
1019 // Some quick checks just in case
1020 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
1021 os << "Invalid dex file\n";
1022 return false;
1023 }
1024
1025 // Verify output directory exists
1026 if (!OS::DirectoryExists(options_.export_dex_location_)) {
1027 // TODO: Extend OS::DirectoryExists if symlink support is required
1028 os << options_.export_dex_location_ << " output directory not found or symlink\n";
1029 return false;
1030 }
1031
1032 // Beautify path names
1033 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
1034 return false;
1035 }
1036
1037 std::string dex_orig_name;
1038 size_t dex_orig_pos = dex_file_location.rfind('/');
1039 if (dex_orig_pos == std::string::npos)
1040 dex_orig_name = dex_file_location;
1041 else
1042 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
1043
1044 // A more elegant approach to efficiently name user installed apps is welcome
Andreas Gampef812d8c2017-02-17 10:19:44 -08001045 if (dex_orig_name.size() == 8 &&
1046 dex_orig_name.compare("base.apk") == 0 &&
1047 dex_orig_pos != std::string::npos) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001048 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
1049 size_t apk_orig_pos = dex_file_location.rfind('/');
1050 if (apk_orig_pos != std::string::npos) {
1051 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
1052 }
1053 }
1054
1055 std::string out_dex_path(options_.export_dex_location_);
1056 if (out_dex_path.back() != '/') {
1057 out_dex_path.append("/");
1058 }
1059 out_dex_path.append(dex_orig_name);
1060 out_dex_path.append("_export.dex");
1061 if (out_dex_path.length() > PATH_MAX) {
1062 return false;
1063 }
1064
1065 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
1066 if (file.get() == nullptr) {
1067 os << "Failed to open output dex file " << out_dex_path;
1068 return false;
1069 }
1070
1071 if (!file->WriteFully(dex_file->Begin(), fsize)) {
1072 os << "Failed to write dex file";
1073 file->Erase();
1074 return false;
1075 }
1076
1077 if (file->FlushCloseOrErase() != 0) {
1078 os << "Flush and close failed";
1079 return false;
1080 }
1081
1082 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
1083 os << std::flush;
1084
1085 return true;
1086 }
1087
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001088 bool DumpOatClass(VariableIndentationOutputStream* vios,
1089 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001090 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001091 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001092 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -07001093 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001094 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001095 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001096 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -08001097 }
1098 ClassDataItemIterator it(dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -07001099 it.SkipAllFields();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001100 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001101 while (it.HasNextDirectMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001102 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001103 it.GetMemberIndex(), it.GetMethodCodeItem(),
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001104 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001105 success = false;
1106 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001107 if (addr_found) {
1108 *stop_analysis = true;
1109 return success;
1110 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001111 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -07001112 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001113 }
Ian Rogers0571d352011-11-03 19:51:38 -07001114 while (it.HasNextVirtualMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001115 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001116 it.GetMemberIndex(), it.GetMethodCodeItem(),
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001117 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001118 success = false;
1119 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001120 if (addr_found) {
1121 *stop_analysis = true;
1122 return success;
1123 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001124 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -07001125 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001126 }
Ian Rogers0571d352011-11-03 19:51:38 -07001127 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001128 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001129 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001130 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001131
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001132 static constexpr uint32_t kPrologueBytes = 16;
1133
1134 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
1135 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
1136
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001137 bool DumpOatMethod(VariableIndentationOutputStream* vios,
1138 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -07001139 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001140 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001141 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001142 uint32_t method_access_flags, bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001143 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001144
1145 // TODO: Support regex
1146 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
1147 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00001148 return success;
1149 }
1150
David Sehr709b0702016-10-13 09:12:37 -07001151 std::string pretty_method = dex_file.PrettyMethod(dex_method_idx, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001152 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
1153 class_method_index, pretty_method.c_str(),
1154 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001155 if (options_.list_methods_) return success;
1156
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001157 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
1158 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
1159 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
1160 uint32_t code_offset = oat_method.GetCodeOffset();
1161 uint32_t code_size = oat_method.GetQuickCodeSize();
1162 if (resolved_addr2instr_ != 0) {
1163 if (resolved_addr2instr_ > code_offset + code_size) {
1164 return success;
1165 } else {
1166 *addr_found = true; // stop analyzing file at next iteration
1167 }
1168 }
1169
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001170 // Everything below is indented at least once.
1171 ScopedIndentation indent1(vios);
1172
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001173 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001174 vios->Stream() << "DEX CODE:\n";
1175 ScopedIndentation indent2(vios);
1176 DumpDexCode(vios->Stream(), dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -07001177 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001178
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001179 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001180 std::unique_ptr<verifier::MethodVerifier> verifier;
1181 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001182 // We need to have the handle scope stay live until after the verifier since the verifier has
1183 // a handle to the dex cache from hs.
1184 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001185 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
1186 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001187 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001188 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001189 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -07001190 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001191 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001192 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001193 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001194 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001195 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001196 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001197 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001198 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001199 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
1200 oat_method_offsets_offset, oat_file_.Size());
1201 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001202 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001203 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001204 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001205
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001206 ScopedIndentation indent2(vios);
1207 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001208 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
1209 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001210 vios->Stream() << StringPrintf("WARNING: "
1211 "code offset 0x%08x is past end of file 0x%08zx.\n",
1212 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001213 success = false;
1214 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001215 vios->Stream() << "\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001216 }
1217 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001218 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001219 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
1220 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001221 stats_.AddBitsIfUnique(Stats::kByteKindQuickMethodHeader,
1222 sizeof(*method_header) * kBitsPerByte,
1223 method_header);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001224 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001225 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001226 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001227 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001228 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001229 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001230 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
1231 method_header_offset, oat_file_.Size());
1232 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001233 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001234 return false;
1235 }
1236
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001237 ScopedIndentation indent2(vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001238 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001239 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001240 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001241 }
Mingyao Yang063fc772016-08-02 11:02:54 -07001242 uint32_t vmap_table_offset = method_header ==
1243 nullptr ? 0 : method_header->GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001244 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001245
1246 size_t vmap_table_offset_limit =
1247 (kIsVdexEnabled && IsMethodGeneratedByDexToDexCompiler(oat_method, code_item))
1248 ? oat_file_.GetVdexFile()->Size()
1249 : method_header->GetCode() - oat_file_.Begin();
1250 if (vmap_table_offset >= vmap_table_offset_limit) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001251 vios->Stream() << StringPrintf("WARNING: "
1252 "vmap table offset 0x%08x is past end of file 0x%08zx. "
1253 "vmap table offset was loaded from offset 0x%08x.\n",
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001254 vmap_table_offset,
1255 vmap_table_offset_limit,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001256 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001257 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001258 } else if (options_.dump_vmap_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001259 DumpVmapData(vios, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001260 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001261 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001262 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001263 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001264
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001265 ScopedIndentation indent2(vios);
1266 vios->Stream()
1267 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
1268 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
1269 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
1270 vios->Stream() << "\n";
1271 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
1272 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
1273 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001274 }
1275 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001276 // Based on spill masks from QuickMethodFrameInfo so placed
1277 // after it is dumped, but useful for understanding quick
1278 // code, so dumped here.
1279 ScopedIndentation indent2(vios);
1280 DumpVregLocations(vios->Stream(), oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001281 }
1282 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001283 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001284 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
1285 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001286 ScopedIndentation indent2(vios);
1287 vios->Stream() << StringPrintf("WARNING: "
1288 "code size offset 0x%08x is past end of file 0x%08zx.",
1289 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001290 success = false;
1291 } else {
1292 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001293 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1294 uint64_t aligned_code_end = aligned_code_begin + code_size;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001295 stats_.AddBitsIfUnique(Stats::kByteKindCode, code_size * kBitsPerByte, code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001296
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001297 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001298 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001299 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001300 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
1301 code_offset,
1302 code_size_offset,
1303 code_size,
1304 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001305
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001306 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001307 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001308 vios->Stream() << StringPrintf("WARNING: "
1309 "start of code at 0x%08x is past end of file 0x%08zx.",
1310 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001311 success = false;
1312 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001313 vios->Stream() << StringPrintf(
1314 "WARNING: "
1315 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
1316 "code size is 0x%08x loaded from offset 0x%08x.\n",
1317 aligned_code_end, oat_file_.Size(),
1318 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001319 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001320 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001321 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001322 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001323 }
1324 }
1325 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001326 vios->Stream() << StringPrintf(
1327 "WARNING: "
1328 "code size %d is bigger than max expected threshold of %d. "
1329 "code size is 0x%08x loaded from offset 0x%08x.\n",
1330 code_size, kMaxCodeSize,
1331 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001332 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001333 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001334 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001335 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001336 }
1337 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001338 } else if (options_.disassemble_code_) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001339 DumpCode(vios, oat_method, code_item, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001340 }
1341 }
1342 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001343 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001344 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001345 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001346
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001347 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1348 if (spill_mask == 0) {
1349 return;
1350 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001351 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001352 for (size_t i = 0; i < 32; i++) {
1353 if ((spill_mask & (1 << i)) != 0) {
1354 if (is_float) {
1355 os << "fr" << i;
1356 } else {
1357 os << "r" << i;
1358 }
1359 spill_mask ^= 1 << i; // clear bit
1360 if (spill_mask != 0) {
1361 os << ", ";
1362 } else {
1363 break;
1364 }
1365 }
1366 }
1367 os << ")";
1368 }
1369
Roland Levillain442b46a2015-02-18 16:54:21 +00001370 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001371 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001372 const OatFile::OatMethod& oat_method,
1373 const DexFile::CodeItem* code_item) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001374 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1375 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001376 const void* raw_code_info = oat_method.GetVmapTable();
1377 if (raw_code_info != nullptr) {
1378 CodeInfo code_info(raw_code_info);
1379 DCHECK(code_item != nullptr);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001380 ScopedIndentation indent1(vios);
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001381 MethodInfo method_info = oat_method.GetOatQuickMethodHeader()->GetOptimizedMethodInfo();
1382 DumpCodeInfo(vios, code_info, oat_method, *code_item, method_info);
Roland Levillain442b46a2015-02-18 16:54:21 +00001383 }
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001384 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
1385 // We don't encode the size in the table, so just emit that we have quickened
1386 // information.
1387 ScopedIndentation indent(vios);
1388 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001389 } else {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001390 // Otherwise, there is nothing to display.
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001391 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001392 }
1393
1394 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001395 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001396 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001397 const OatFile::OatMethod& oat_method,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001398 const DexFile::CodeItem& code_item,
1399 const MethodInfo& method_info) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001400 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001401 oat_method.GetCodeOffset(),
1402 code_item.registers_size_,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001403 options_.dump_code_info_stack_maps_,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001404 instruction_set_,
1405 method_info);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001406 }
1407
Andreas Gampe36a296f2017-06-13 14:11:11 -07001408 static int GetOutVROffset(uint16_t out_num, InstructionSet isa) {
1409 // According to stack model, the first out is above the Method referernce.
1410 return static_cast<size_t>(InstructionSetPointerSize(isa)) + out_num * sizeof(uint32_t);
1411 }
1412
1413 static uint32_t GetVRegOffsetFromQuickCode(const DexFile::CodeItem* code_item,
1414 uint32_t core_spills,
1415 uint32_t fp_spills,
1416 size_t frame_size,
1417 int reg,
1418 InstructionSet isa) {
1419 PointerSize pointer_size = InstructionSetPointerSize(isa);
1420 if (kIsDebugBuild) {
1421 auto* runtime = Runtime::Current();
1422 if (runtime != nullptr) {
1423 CHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), pointer_size);
1424 }
1425 }
1426 DCHECK_ALIGNED(frame_size, kStackAlignment);
1427 DCHECK_NE(reg, -1);
1428 int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa)
1429 + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa)
1430 + sizeof(uint32_t); // Filler.
1431 int num_regs = code_item->registers_size_ - code_item->ins_size_;
1432 int temp_threshold = code_item->registers_size_;
1433 const int max_num_special_temps = 1;
1434 if (reg == temp_threshold) {
1435 // The current method pointer corresponds to special location on stack.
1436 return 0;
1437 } else if (reg >= temp_threshold + max_num_special_temps) {
1438 /*
1439 * Special temporaries may have custom locations and the logic above deals with that.
1440 * However, non-special temporaries are placed relative to the outs.
1441 */
1442 int temps_start = code_item->outs_size_ * sizeof(uint32_t)
1443 + static_cast<size_t>(pointer_size) /* art method */;
1444 int relative_offset = (reg - (temp_threshold + max_num_special_temps)) * sizeof(uint32_t);
1445 return temps_start + relative_offset;
1446 } else if (reg < num_regs) {
1447 int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t);
1448 return locals_start + (reg * sizeof(uint32_t));
1449 } else {
1450 // Handle ins.
1451 return frame_size + ((reg - num_regs) * sizeof(uint32_t))
1452 + static_cast<size_t>(pointer_size) /* art method */;
1453 }
1454 }
1455
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001456 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1457 const DexFile::CodeItem* code_item) {
1458 if (code_item != nullptr) {
1459 size_t num_locals_ins = code_item->registers_size_;
1460 size_t num_ins = code_item->ins_size_;
1461 size_t num_locals = num_locals_ins - num_ins;
1462 size_t num_outs = code_item->outs_size_;
1463
1464 os << "vr_stack_locations:";
1465 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1466 // For readability, delimit the different kinds of VRs.
1467 if (reg == num_locals_ins) {
1468 os << "\n\tmethod*:";
1469 } else if (reg == num_locals && num_ins > 0) {
1470 os << "\n\tins:";
1471 } else if (reg == 0 && num_locals > 0) {
1472 os << "\n\tlocals:";
1473 }
1474
Andreas Gampe36a296f2017-06-13 14:11:11 -07001475 uint32_t offset = GetVRegOffsetFromQuickCode(code_item,
1476 oat_method.GetCoreSpillMask(),
1477 oat_method.GetFpSpillMask(),
1478 oat_method.GetFrameSizeInBytes(),
1479 reg,
1480 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001481 os << " v" << reg << "[sp + #" << offset << "]";
1482 }
1483
1484 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1485 if (out_reg == 0) {
1486 os << "\n\touts:";
1487 }
1488
Andreas Gampe36a296f2017-06-13 14:11:11 -07001489 uint32_t offset = GetOutVROffset(out_reg, GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001490 os << " v" << out_reg << "[sp + #" << offset << "]";
1491 }
1492
1493 os << "\n";
1494 }
1495 }
1496
Ian Rogersb23a7722012-10-09 16:54:26 -07001497 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001498 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001499 size_t i = 0;
1500 while (i < code_item->insns_size_in_code_units_) {
1501 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001502 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1503 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001504 i += instruction->SizeInCodeUnits();
1505 }
1506 }
1507 }
1508
Roland Levillainf2650d12015-05-28 14:53:28 +01001509 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1510 // the optimizing compiler?
1511 static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1512 const DexFile::CodeItem* code_item) {
1513 // If the native GC map is null and the Dex `code_item` is not
1514 // null, then this method has been compiled with the optimizing
1515 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001516 return oat_method.GetQuickCode() != nullptr &&
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001517 oat_method.GetVmapTable() != nullptr &&
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001518 code_item != nullptr;
1519 }
1520
1521 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1522 // the dextodex compiler?
1523 static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
1524 const DexFile::CodeItem* code_item) {
1525 // If the quick code is null, the Dex `code_item` is not
1526 // null, and the vmap table is not null, then this method has been compiled
1527 // with the dextodex compiler.
1528 return oat_method.GetQuickCode() == nullptr &&
1529 oat_method.GetVmapTable() != nullptr &&
1530 code_item != nullptr;
Roland Levillainf2650d12015-05-28 14:53:28 +01001531 }
1532
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001533 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001534 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001535 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001536 const DexFile* dex_file,
1537 const DexFile::ClassDef& class_def,
1538 const DexFile::CodeItem* code_item,
1539 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001540 if ((method_access_flags & kAccNative) == 0) {
1541 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001542 Runtime* const runtime = Runtime::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001543 Handle<mirror::DexCache> dex_cache(
Mathieu Chartierf284d442016-06-02 11:48:30 -07001544 hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file, nullptr)));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001545 CHECK(dex_cache != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001546 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001547 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001548 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
David Brazdil15fc7292016-09-02 14:13:18 +01001549 class_def, code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001550 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001551
1552 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001553 }
1554
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001555 // The StackMapsHelper provides the stack maps in the native PC order.
1556 // For identical native PCs, the order from the CodeInfo is preserved.
1557 class StackMapsHelper {
1558 public:
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001559 explicit StackMapsHelper(const uint8_t* raw_code_info, InstructionSet instruction_set)
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001560 : code_info_(raw_code_info),
1561 encoding_(code_info_.ExtractEncoding()),
1562 number_of_stack_maps_(code_info_.GetNumberOfStackMaps(encoding_)),
1563 indexes_(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001564 offset_(static_cast<uint32_t>(-1)),
1565 stack_map_index_(0u),
1566 instruction_set_(instruction_set) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001567 if (number_of_stack_maps_ != 0u) {
1568 // Check if native PCs are ordered.
1569 bool ordered = true;
1570 StackMap last = code_info_.GetStackMapAt(0u, encoding_);
1571 for (size_t i = 1; i != number_of_stack_maps_; ++i) {
1572 StackMap current = code_info_.GetStackMapAt(i, encoding_);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001573 if (last.GetNativePcOffset(encoding_.stack_map.encoding, instruction_set) >
1574 current.GetNativePcOffset(encoding_.stack_map.encoding, instruction_set)) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001575 ordered = false;
1576 break;
1577 }
1578 last = current;
1579 }
1580 if (!ordered) {
1581 // Create indirection indexes for access in native PC order. We do not optimize
1582 // for the fact that there can currently be only two separately ordered ranges,
1583 // namely normal stack maps and catch-point stack maps.
1584 indexes_.resize(number_of_stack_maps_);
1585 std::iota(indexes_.begin(), indexes_.end(), 0u);
1586 std::sort(indexes_.begin(),
1587 indexes_.end(),
1588 [this](size_t lhs, size_t rhs) {
1589 StackMap left = code_info_.GetStackMapAt(lhs, encoding_);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001590 uint32_t left_pc = left.GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001591 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001592 StackMap right = code_info_.GetStackMapAt(rhs, encoding_);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001593 uint32_t right_pc = right.GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001594 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001595 // If the PCs are the same, compare indexes to preserve the original order.
1596 return (left_pc < right_pc) || (left_pc == right_pc && lhs < rhs);
1597 });
1598 }
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001599 offset_ = GetStackMapAt(0).GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001600 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001601 }
1602 }
1603
1604 const CodeInfo& GetCodeInfo() const {
1605 return code_info_;
1606 }
1607
1608 const CodeInfoEncoding& GetEncoding() const {
1609 return encoding_;
1610 }
1611
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001612 uint32_t GetOffset() const {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001613 return offset_;
1614 }
1615
1616 StackMap GetStackMap() const {
1617 return GetStackMapAt(stack_map_index_);
1618 }
1619
1620 void Next() {
1621 ++stack_map_index_;
1622 offset_ = (stack_map_index_ == number_of_stack_maps_)
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001623 ? static_cast<uint32_t>(-1)
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001624 : GetStackMapAt(stack_map_index_).GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001625 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001626 }
1627
1628 private:
1629 StackMap GetStackMapAt(size_t i) const {
1630 if (!indexes_.empty()) {
1631 i = indexes_[i];
1632 }
1633 DCHECK_LT(i, number_of_stack_maps_);
1634 return code_info_.GetStackMapAt(i, encoding_);
1635 }
1636
1637 const CodeInfo code_info_;
1638 const CodeInfoEncoding encoding_;
1639 const size_t number_of_stack_maps_;
1640 dchecked_vector<size_t> indexes_; // Used if stack map native PCs are not ordered.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001641 uint32_t offset_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001642 size_t stack_map_index_;
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001643 const InstructionSet instruction_set_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001644 };
1645
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001646 void DumpCode(VariableIndentationOutputStream* vios,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001647 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1648 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001649 const void* quick_code = oat_method.GetQuickCode();
1650
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001651 if (code_size == 0) {
1652 code_size = oat_method.GetQuickCodeSize();
1653 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001654 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001655 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001656 return;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001657 } else if (!bad_input && IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1658 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001659 StackMapsHelper helper(oat_method.GetVmapTable(), instruction_set_);
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001660 MethodInfo method_info(oat_method.GetOatQuickMethodHeader()->GetOptimizedMethodInfo());
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001661 {
1662 CodeInfoEncoding encoding(helper.GetEncoding());
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001663 StackMapEncoding stack_map_encoding(encoding.stack_map.encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001664 const size_t num_stack_maps = encoding.stack_map.num_entries;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001665 if (stats_.AddBitsIfUnique(Stats::kByteKindCodeInfoEncoding,
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001666 encoding.HeaderSize() * kBitsPerByte,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001667 oat_method.GetVmapTable())) {
Mathieu Chartier1e083792017-02-08 13:30:04 -08001668 // Stack maps
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001669 stats_.AddBits(
1670 Stats::kByteKindStackMapNativePc,
1671 stack_map_encoding.GetNativePcEncoding().BitSize() * num_stack_maps);
1672 stats_.AddBits(
1673 Stats::kByteKindStackMapDexPc,
1674 stack_map_encoding.GetDexPcEncoding().BitSize() * num_stack_maps);
1675 stats_.AddBits(
1676 Stats::kByteKindStackMapDexRegisterMap,
1677 stack_map_encoding.GetDexRegisterMapEncoding().BitSize() * num_stack_maps);
1678 stats_.AddBits(
Mathieu Chartier1e083792017-02-08 13:30:04 -08001679 Stats::kByteKindStackMapInlineInfoIndex,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001680 stack_map_encoding.GetInlineInfoEncoding().BitSize() * num_stack_maps);
1681 stats_.AddBits(
Mathieu Chartier1a20b682017-01-31 14:25:16 -08001682 Stats::kByteKindStackMapRegisterMaskIndex,
1683 stack_map_encoding.GetRegisterMaskIndexEncoding().BitSize() * num_stack_maps);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001684 stats_.AddBits(
David Srbecky45aa5982016-03-18 02:15:09 +00001685 Stats::kByteKindStackMapStackMaskIndex,
1686 stack_map_encoding.GetStackMaskIndexEncoding().BitSize() * num_stack_maps);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001687
1688 // Stack masks
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001689 stats_.AddBits(
David Srbecky45aa5982016-03-18 02:15:09 +00001690 Stats::kByteKindCodeInfoStackMasks,
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001691 encoding.stack_mask.encoding.BitSize() * encoding.stack_mask.num_entries);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001692
1693 // Register masks
Mathieu Chartier1a20b682017-01-31 14:25:16 -08001694 stats_.AddBits(
1695 Stats::kByteKindCodeInfoRegisterMasks,
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001696 encoding.register_mask.encoding.BitSize() * encoding.register_mask.num_entries);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001697
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001698 // Invoke infos
1699 if (encoding.invoke_info.num_entries > 0u) {
1700 stats_.AddBits(
1701 Stats::kByteKindCodeInfoInvokeInfo,
1702 encoding.invoke_info.encoding.BitSize() * encoding.invoke_info.num_entries);
1703 }
1704
Mathieu Chartier1e083792017-02-08 13:30:04 -08001705 // Location catalog
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001706 const size_t location_catalog_bytes =
1707 helper.GetCodeInfo().GetDexRegisterLocationCatalogSize(encoding);
1708 stats_.AddBits(Stats::kByteKindCodeInfoLocationCatalog,
1709 kBitsPerByte * location_catalog_bytes);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001710 // Dex register bytes.
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001711 const size_t dex_register_bytes =
1712 helper.GetCodeInfo().GetDexRegisterMapsSize(encoding, code_item->registers_size_);
1713 stats_.AddBits(
1714 Stats::kByteKindCodeInfoDexRegisterMap,
1715 kBitsPerByte * dex_register_bytes);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001716
1717 // Inline infos.
1718 const size_t num_inline_infos = encoding.inline_info.num_entries;
1719 if (num_inline_infos > 0u) {
1720 stats_.AddBits(
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001721 Stats::kByteKindInlineInfoMethodIndexIdx,
1722 encoding.inline_info.encoding.GetMethodIndexIdxEncoding().BitSize() *
1723 num_inline_infos);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001724 stats_.AddBits(
1725 Stats::kByteKindInlineInfoDexPc,
1726 encoding.inline_info.encoding.GetDexPcEncoding().BitSize() * num_inline_infos);
1727 stats_.AddBits(
1728 Stats::kByteKindInlineInfoExtraData,
1729 encoding.inline_info.encoding.GetExtraDataEncoding().BitSize() * num_inline_infos);
1730 stats_.AddBits(
1731 Stats::kByteKindInlineInfoDexRegisterMap,
1732 encoding.inline_info.encoding.GetDexRegisterMapEncoding().BitSize() *
1733 num_inline_infos);
1734 stats_.AddBits(Stats::kByteKindInlineInfoIsLast, num_inline_infos);
1735 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001736 }
1737 }
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001738 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1739 size_t offset = 0;
1740 while (offset < code_size) {
1741 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
1742 if (offset == helper.GetOffset()) {
1743 ScopedIndentation indent1(vios);
1744 StackMap stack_map = helper.GetStackMap();
1745 DCHECK(stack_map.IsValid());
1746 stack_map.Dump(vios,
1747 helper.GetCodeInfo(),
1748 helper.GetEncoding(),
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001749 method_info,
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001750 oat_method.GetCodeOffset(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001751 code_item->registers_size_,
1752 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001753 do {
1754 helper.Next();
1755 // There may be multiple stack maps at a given PC. We display only the first one.
1756 } while (offset == helper.GetOffset());
1757 }
1758 DCHECK_LT(offset, helper.GetOffset());
1759 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001760 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001761 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1762 size_t offset = 0;
1763 while (offset < code_size) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001764 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001765 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001766 }
1767 }
1768
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001769 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001770 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001771 const OatDumperOptions& options_;
1772 uint32_t resolved_addr2instr_;
Andreas Gampe372f3a32016-08-19 10:49:06 -07001773 const InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001774 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001775 Disassembler* disassembler_;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001776 Stats stats_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001777};
1778
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001779class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001780 public:
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001781 ImageDumper(std::ostream* os,
1782 gc::space::ImageSpace& image_space,
1783 const ImageHeader& image_header,
1784 OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001785 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001786 vios_(os),
1787 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001788 image_space_(image_space),
1789 image_header_(image_header),
1790 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001791
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001792 bool Dump() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001793 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001794 std::ostream& indent_os = vios_.Stream();
1795
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001796 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001797
Jeff Haodcdc85b2015-12-04 14:06:18 -08001798 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1799
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001800 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001801
Mathieu Chartiere401d142015-04-22 13:56:20 -07001802 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1803
1804 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1805 auto section = static_cast<ImageHeader::ImageSections>(i);
1806 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1807 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001808
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001809 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001810
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001811 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001812
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001813 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1814
1815 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1816
1817 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001818
Alex Lighta59dd802014-07-02 16:28:08 -07001819 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1820
Igor Murashkin46774762014-10-22 11:37:02 -07001821 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1822
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001823 {
1824 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001825 static_assert(arraysize(image_roots_descriptions_) ==
1826 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001827 DCHECK_LE(image_header_.GetImageRoots()->GetLength(), ImageHeader::kImageRootsMax);
1828 for (int32_t i = 0, size = image_header_.GetImageRoots()->GetLength(); i != size; ++i) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001829 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1830 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001831 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001832 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001833 if (image_root_object != nullptr && image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001834 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001835 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001836 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001837 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1838 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001839 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001840 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1841 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001842 run++;
1843 } else {
1844 break;
1845 }
1846 }
1847 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001848 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001849 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001850 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001851 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001852 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001853 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001854 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001855 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001856 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001857 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001858 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001859 }
1860 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001861 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001862
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001863 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001864 os << "METHOD ROOTS\n";
1865 static_assert(arraysize(image_methods_descriptions_) ==
1866 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1867 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1868 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1869 const char* description = image_methods_descriptions_[i];
1870 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001871 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001872 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001873 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001874 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001875
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001876 Runtime* const runtime = Runtime::Current();
1877 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001878 std::string image_filename = image_space_.GetImageFilename();
1879 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001880 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001881 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001882 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001883 const OatFile* oat_file = image_space_.GetOatFile();
Alex Lighta59dd802014-07-02 16:28:08 -07001884 if (oat_file == nullptr) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001885 oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location);
1886 }
1887 if (oat_file == nullptr) {
1888 oat_file = OatFile::Open(oat_location,
1889 oat_location,
1890 nullptr,
1891 nullptr,
1892 false,
1893 /*low_4gb*/false,
1894 nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001895 &error_msg);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001896 }
1897 if (oat_file == nullptr) {
1898 os << "OAT FILE NOT FOUND: " << error_msg << "\n";
1899 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001900 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001901 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001902
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001903 stats_.oat_file_bytes = oat_file->Size();
1904
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001905 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001906
Mathieu Chartier02e25112013-08-14 16:14:24 -07001907 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001908 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001909 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1910 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001911 }
1912
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001913 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001914
Jeff Haodcdc85b2015-12-04 14:06:18 -08001915 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001916 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001917 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001918 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001919 {
1920 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1921 heap->FlushAllocStack();
1922 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001923 // Since FlushAllocStack() above resets the (active) allocation
1924 // stack. Need to revoke the thread-local allocation stacks that
1925 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001926 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001927 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001928 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001929 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001930 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001931 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001932 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001933 {
Andreas Gampecc1b5352016-12-01 16:58:38 -08001934 ReaderMutexLock mu(self, *Locks::dex_lock_);
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001935 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001936 ObjPtr<mirror::DexCache> dex_cache =
1937 ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001938 if (dex_cache != nullptr) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001939 dex_caches_.insert(dex_cache.Ptr());
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001940 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001941 }
1942 }
Andreas Gampe0c183382017-07-13 22:26:24 -07001943 auto dump_visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
1944 DumpObject(obj);
1945 };
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001946 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001947 // Dump the normal objects before ArtMethods.
Andreas Gampe0c183382017-07-13 22:26:24 -07001948 image_space_.GetLiveBitmap()->Walk(dump_visitor);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001949 indent_os << "\n";
1950 // TODO: Dump fields.
1951 // Dump methods after.
Jeff Haodcdc85b2015-12-04 14:06:18 -08001952 DumpArtMethodVisitor visitor(this);
Mathieu Chartiere42888f2016-04-14 10:49:19 -07001953 image_header_.VisitPackedArtMethods(&visitor,
1954 image_space_.Begin(),
1955 image_header_.GetPointerSize());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001956 // Dump the large objects separately.
Andreas Gampe0c183382017-07-13 22:26:24 -07001957 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(dump_visitor);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001958 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001959 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001960 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001961 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001962 size_t data_size = image_header_.GetDataSize(); // stored size in file.
1963 if (file == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001964 LOG(WARNING) << "Failed to find image in " << image_filename;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001965 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001966 stats_.file_bytes = file->GetLength();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001967 // If the image is compressed, adjust to decompressed size.
1968 size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader);
1969 if (image_header_.GetStorageMode() == ImageHeader::kStorageModeUncompressed) {
1970 DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image";
1971 }
1972 stats_.file_bytes += uncompressed_size - data_size;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001973 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001974 size_t header_bytes = sizeof(ImageHeader);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001975 const auto& object_section = image_header_.GetImageSection(ImageHeader::kSectionObjects);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001976 const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1977 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001978 const auto& dex_cache_arrays_section = image_header_.GetImageSection(
1979 ImageHeader::kSectionDexCacheArrays);
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001980 const auto& intern_section = image_header_.GetImageSection(
1981 ImageHeader::kSectionInternedStrings);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001982 const auto& class_table_section = image_header_.GetImageSection(
1983 ImageHeader::kSectionClassTable);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001984 const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1985
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001986 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001987
1988 // Objects are kObjectAlignment-aligned.
1989 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1990 if (object_section.Offset() > header_bytes) {
1991 stats_.alignment_bytes += object_section.Offset() - header_bytes;
1992 }
1993
1994 // Field section is 4-byte aligned.
1995 constexpr size_t kFieldSectionAlignment = 4U;
1996 uint32_t end_objects = object_section.Offset() + object_section.Size();
1997 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
1998 stats_.alignment_bytes += field_section.Offset() - end_objects;
1999
2000 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
2001 uint32_t end_fields = field_section.Offset() + field_section.Size();
2002 CHECK_ALIGNED(method_section.Offset(), 4);
2003 stats_.alignment_bytes += method_section.Offset() - end_fields;
2004
2005 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
2006 uint32_t end_methods = method_section.Offset() + method_section.Size();
2007 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
2008 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
2009
2010 // Intern table is 8-byte aligned.
2011 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
2012 CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
2013 stats_.alignment_bytes += intern_section.Offset() - end_caches;
2014
2015 // Add space between intern table and class table.
2016 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
2017 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
2018
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002019 // Add space between end of image data and bitmap. Expect the bitmap to be page-aligned.
2020 const size_t bitmap_offset = sizeof(ImageHeader) + data_size;
Andreas Gampeace0dc12016-01-20 13:33:13 -08002021 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002022 stats_.alignment_bytes += RoundUp(bitmap_offset, kPageSize) - bitmap_offset;
Andreas Gampeace0dc12016-01-20 13:33:13 -08002023
Mathieu Chartiere401d142015-04-22 13:56:20 -07002024 stats_.bitmap_bytes += bitmap_section.Size();
2025 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01002026 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01002027 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002028 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002029 stats_.class_table_bytes += class_table_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002030 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002031 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002032
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002033 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002034
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002035 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002036 }
2037
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002038 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002039 class DumpArtMethodVisitor : public ArtMethodVisitor {
2040 public:
2041 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
2042
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002043 virtual void Visit(ArtMethod* method) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002044 std::ostream& indent_os = image_dumper_->vios_.Stream();
David Sehr709b0702016-10-13 09:12:37 -07002045 indent_os << method << " " << " ArtMethod: " << ArtMethod::PrettyMethod(method) << "\n";
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002046 image_dumper_->DumpMethod(method, indent_os);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002047 indent_os << "\n";
2048 }
2049
2050 private:
2051 ImageDumper* const image_dumper_;
2052 };
2053
Mathieu Chartier3398c782016-09-30 10:27:43 -07002054 static void PrettyObjectValue(std::ostream& os,
2055 ObjPtr<mirror::Class> type,
2056 ObjPtr<mirror::Object> value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002057 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002058 CHECK(type != nullptr);
2059 if (value == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07002060 os << StringPrintf("null %s\n", type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002061 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002062 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002063 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07002064 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07002065 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002066 mirror::Class* klass = value->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07002067 os << StringPrintf("%p Class: %s\n", klass, mirror::Class::PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002068 } else {
David Sehr709b0702016-10-13 09:12:37 -07002069 os << StringPrintf("%p %s\n", value.Ptr(), type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002070 }
2071 }
2072
Mathieu Chartier3398c782016-09-30 10:27:43 -07002073 static void PrintField(std::ostream& os, ArtField* field, ObjPtr<mirror::Object> obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002074 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07002075 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08002076 switch (field->GetTypeAsPrimitiveType()) {
2077 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08002078 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002079 break;
2080 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002081 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002082 break;
2083 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002084 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002085 break;
2086 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002087 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002088 break;
2089 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07002090 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002091 break;
2092 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07002093 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002094 break;
2095 case Primitive::kPrimBoolean:
Roland Levillain5e8d5f02016-10-18 18:03:43 +01002096 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj) ? "true" : "false",
Fred Shih37f05ef2014-07-16 18:38:08 -07002097 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002098 break;
2099 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07002100 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002101 break;
2102 case Primitive::kPrimNot: {
2103 // Get the value, don't compute the type unless it is non-null as we don't want
2104 // to cause class loading.
Mathieu Chartier3398c782016-09-30 10:27:43 -07002105 ObjPtr<mirror::Object> value = field->GetObj(obj);
Ian Rogers08f1f502014-12-02 15:04:37 -08002106 if (value == nullptr) {
2107 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07002108 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08002109 // Grab the field type without causing resolution.
Mathieu Chartier3398c782016-09-30 10:27:43 -07002110 ObjPtr<mirror::Class> field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08002111 if (field_type != nullptr) {
2112 PrettyObjectValue(os, field_type, value);
2113 } else {
Mathieu Chartier3398c782016-09-30 10:27:43 -07002114 os << StringPrintf("%p %s\n",
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07002115 value.Ptr(),
Ian Rogers08f1f502014-12-02 15:04:37 -08002116 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
2117 }
Ian Rogers50239c72013-06-17 14:53:22 -07002118 }
Ian Rogers08f1f502014-12-02 15:04:37 -08002119 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07002120 }
Ian Rogers08f1f502014-12-02 15:04:37 -08002121 default:
2122 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
2123 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08002124 }
2125 }
2126
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002127 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002128 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002129 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002130 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002131 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08002132 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002133 for (ArtField& field : klass->GetIFields()) {
2134 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08002135 }
2136 }
2137
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002138 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002139 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002140 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002141
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002142 const void* GetQuickOatCodeBegin(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08002143 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002144 image_header_.GetPointerSize());
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002145 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002146 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002147 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002148 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002149 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002150 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002151 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002152 }
2153
Mathieu Chartiere401d142015-04-22 13:56:20 -07002154 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002155 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002156 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
2157 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002158 return 0;
2159 }
2160 return oat_code_begin[-1];
2161 }
2162
Mathieu Chartiere401d142015-04-22 13:56:20 -07002163 const void* GetQuickOatCodeEnd(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002164 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002165 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002166 if (oat_code_begin == nullptr) {
2167 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002168 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002169 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002170 }
2171
Andreas Gampe0c183382017-07-13 22:26:24 -07002172 void DumpObject(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002173 DCHECK(obj != nullptr);
Andreas Gampe0c183382017-07-13 22:26:24 -07002174 if (!InDumpSpace(obj)) {
Brian Carlstrom78128a62011-09-15 17:21:19 -07002175 return;
2176 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002177
2178 size_t object_bytes = obj->SizeOf();
2179 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
Andreas Gampe0c183382017-07-13 22:26:24 -07002180 stats_.object_bytes += object_bytes;
2181 stats_.alignment_bytes += alignment_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002182
Andreas Gampe0c183382017-07-13 22:26:24 -07002183 std::ostream& os = vios_.Stream();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002184
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002185 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08002186 if (obj_class->IsArrayClass()) {
David Sehr709b0702016-10-13 09:12:37 -07002187 os << StringPrintf("%p: %s length:%d\n", obj, obj_class->PrettyDescriptor().c_str(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002188 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08002189 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002190 mirror::Class* klass = obj->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07002191 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj,
2192 mirror::Class::PrettyDescriptor(klass).c_str())
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002193 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08002194 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002195 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07002196 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002197 } else {
David Sehr709b0702016-10-13 09:12:37 -07002198 os << StringPrintf("%p: %s\n", obj, obj_class->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002199 }
Andreas Gampe0c183382017-07-13 22:26:24 -07002200 ScopedIndentation indent1(&vios_);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002201 DumpFields(os, obj, obj_class);
Andreas Gampe0c183382017-07-13 22:26:24 -07002202 const PointerSize image_pointer_size = image_header_.GetPointerSize();
Ian Rogersd5b32602012-02-26 16:40:04 -08002203 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002204 auto* obj_array = obj->AsObjectArray<mirror::Object>();
2205 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002206 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002207 size_t run = 0;
2208 for (int32_t j = i + 1; j < length; j++) {
2209 if (value == obj_array->Get(j)) {
2210 run++;
2211 } else {
2212 break;
2213 }
2214 }
2215 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002216 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002217 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002218 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08002219 i = i + run;
2220 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002221 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002222 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002223 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08002224 }
2225 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07002226 mirror::Class* klass = obj->AsClass();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002227 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002228 os << "STATICS:\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002229 ScopedIndentation indent2(&vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002230 for (ArtField& field : klass->GetSFields()) {
2231 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08002232 }
2233 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002234 } else {
Andreas Gampe0c183382017-07-13 22:26:24 -07002235 auto it = dex_caches_.find(obj);
2236 if (it != dex_caches_.end()) {
Vladimir Marko05792b92015-08-03 11:56:49 +01002237 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Andreas Gampe0c183382017-07-13 22:26:24 -07002238 const auto& field_section = image_header_.GetImageSection(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002239 ImageHeader::kSectionArtFields);
Andreas Gampe0c183382017-07-13 22:26:24 -07002240 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01002241 size_t num_methods = dex_cache->NumResolvedMethods();
2242 if (num_methods != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002243 os << "Methods (size=" << num_methods << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002244 ScopedIndentation indent2(&vios_);
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002245 mirror::MethodDexCacheType* resolved_methods = dex_cache->GetResolvedMethods();
Vladimir Marko05792b92015-08-03 11:56:49 +01002246 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002247 ArtMethod* elem = mirror::DexCache::GetNativePairPtrSize(
2248 resolved_methods, i, image_pointer_size).object;
Vladimir Marko05792b92015-08-03 11:56:49 +01002249 size_t run = 0;
2250 for (size_t j = i + 1;
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002251 j != length &&
2252 elem == mirror::DexCache::GetNativePairPtrSize(
2253 resolved_methods, j, image_pointer_size).object;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002254 ++j) {
2255 ++run;
2256 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002257 if (run == 0) {
2258 os << StringPrintf("%zd: ", i);
2259 } else {
2260 os << StringPrintf("%zd to %zd: ", i, i + run);
2261 i = i + run;
2262 }
2263 std::string msg;
2264 if (elem == nullptr) {
2265 msg = "null";
2266 } else if (method_section.Contains(
Andreas Gampe0c183382017-07-13 22:26:24 -07002267 reinterpret_cast<uint8_t*>(elem) - image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002268 msg = reinterpret_cast<ArtMethod*>(elem)->PrettyMethod();
Vladimir Marko05792b92015-08-03 11:56:49 +01002269 } else {
2270 msg = "<not in method section>";
2271 }
2272 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07002273 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002274 }
2275 size_t num_fields = dex_cache->NumResolvedFields();
2276 if (num_fields != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002277 os << "Fields (size=" << num_fields << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002278 ScopedIndentation indent2(&vios_);
Vladimir Marko05792b92015-08-03 11:56:49 +01002279 auto* resolved_fields = dex_cache->GetResolvedFields();
2280 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002281 ArtField* elem = mirror::DexCache::GetNativePairPtrSize(
Vladimir Markof44d36c2017-03-14 14:18:46 +00002282 resolved_fields, i, image_pointer_size).object;
Vladimir Marko05792b92015-08-03 11:56:49 +01002283 size_t run = 0;
2284 for (size_t j = i + 1;
Vladimir Markof44d36c2017-03-14 14:18:46 +00002285 j != length &&
2286 elem == mirror::DexCache::GetNativePairPtrSize(
2287 resolved_fields, j, image_pointer_size).object;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002288 ++j) {
2289 ++run;
2290 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002291 if (run == 0) {
2292 os << StringPrintf("%zd: ", i);
2293 } else {
2294 os << StringPrintf("%zd to %zd: ", i, i + run);
2295 i = i + run;
2296 }
2297 std::string msg;
2298 if (elem == nullptr) {
2299 msg = "null";
2300 } else if (field_section.Contains(
Andreas Gampe0c183382017-07-13 22:26:24 -07002301 reinterpret_cast<uint8_t*>(elem) - image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002302 msg = reinterpret_cast<ArtField*>(elem)->PrettyField();
Vladimir Marko05792b92015-08-03 11:56:49 +01002303 } else {
2304 msg = "<not in field section>";
2305 }
2306 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002307 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002308 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002309 size_t num_types = dex_cache->NumResolvedTypes();
2310 if (num_types != 0u) {
2311 os << "Types (size=" << num_types << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002312 ScopedIndentation indent2(&vios_);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002313 auto* resolved_types = dex_cache->GetResolvedTypes();
2314 for (size_t i = 0; i < num_types; ++i) {
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002315 auto pair = resolved_types[i].load(std::memory_order_relaxed);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002316 size_t run = 0;
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002317 for (size_t j = i + 1; j != num_types; ++j) {
2318 auto other_pair = resolved_types[j].load(std::memory_order_relaxed);
2319 if (pair.index != other_pair.index ||
2320 pair.object.Read() != other_pair.object.Read()) {
2321 break;
2322 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002323 ++run;
2324 }
2325 if (run == 0) {
2326 os << StringPrintf("%zd: ", i);
2327 } else {
2328 os << StringPrintf("%zd to %zd: ", i, i + run);
2329 i = i + run;
2330 }
2331 std::string msg;
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002332 auto* elem = pair.object.Read();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002333 if (elem == nullptr) {
2334 msg = "null";
2335 } else {
David Sehr709b0702016-10-13 09:12:37 -07002336 msg = elem->PrettyClass();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002337 }
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002338 os << StringPrintf("%p %u %s\n", elem, pair.index, msg.c_str());
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002339 }
2340 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002341 }
2342 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002343 std::string temp;
Andreas Gampe0c183382017-07-13 22:26:24 -07002344 stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002345 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002346
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002347 void DumpMethod(ArtMethod* method, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002348 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002349 DCHECK(method != nullptr);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002350 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
2351 const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
Andreas Gampe542451c2016-07-26 09:02:02 -07002352 const PointerSize pointer_size = image_header_.GetPointerSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002353 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2354 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002355 if (method->IsNative()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002356 bool first_occurrence;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002357 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2358 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002359 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002360 stats_.native_to_managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002361 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002362 if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
2363 image_header_.GetPointerSize())) {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002364 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002365 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002366 } else if (method->IsAbstract() || method->IsClassInitializer()) {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002367 // Don't print information for these.
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002368 } else if (method->IsRuntimeMethod()) {
2369 ImtConflictTable* table = method->GetImtConflictTable(image_header_.GetPointerSize());
2370 if (table != nullptr) {
2371 indent_os << "IMT conflict table " << table << " method: ";
2372 for (size_t i = 0, count = table->NumEntries(pointer_size); i < count; ++i) {
David Sehr709b0702016-10-13 09:12:37 -07002373 indent_os << ArtMethod::PrettyMethod(table->GetImplementationMethod(i, pointer_size))
2374 << " ";
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002375 }
2376 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002377 } else {
2378 const DexFile::CodeItem* code_item = method->GetCodeItem();
2379 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002380 stats_.dex_instruction_bytes += dex_instruction_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002381
2382 bool first_occurrence;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002383 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002384 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01002385 // Method compiled with the optimizing compiler have no vmap table.
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002386 vmap_table_bytes = ComputeOatSize(method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002387 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002388 stats_.vmap_table_bytes += vmap_table_bytes;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002389 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002390 }
2391
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002392 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2393 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002394 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002395 stats_.managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002396 if (method->IsConstructor()) {
2397 if (method->IsStatic()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002398 stats_.class_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002399 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002400 stats_.large_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002401 }
2402 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002403 stats_.large_method_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002404 }
2405 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002406 stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002407
Igor Murashkin7617abd2015-07-10 18:27:47 -07002408 uint32_t method_access_flags = method->GetAccessFlags();
2409
Mathieu Chartiere401d142015-04-22 13:56:20 -07002410 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002411 indent_os << StringPrintf("SIZE: Dex Instructions=%zd StackMaps=%zd AccessFlags=0x%x\n",
2412 dex_instruction_bytes,
2413 vmap_table_bytes,
Igor Murashkin7617abd2015-07-10 18:27:47 -07002414 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002415
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002416 size_t total_size = dex_instruction_bytes +
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002417 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002418
2419 double expansion =
2420 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002421 stats_.ComputeOutliers(total_size, expansion, method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002422 }
2423 }
2424
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002425 std::set<const void*> already_seen_;
2426 // Compute the size of the given data within the oat file and whether this is the first time
2427 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002428 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002429 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002430 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002431 already_seen_.insert(oat_data);
2432 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002433 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002434 }
2435 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002436 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002437
2438 public:
2439 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002440 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002441 size_t file_bytes;
2442
2443 size_t header_bytes;
2444 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002445 size_t art_field_bytes;
2446 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01002447 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002448 size_t interned_strings_bytes;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002449 size_t class_table_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07002450 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002451 size_t alignment_bytes;
2452
2453 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002454 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002455 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07002456 size_t class_initializer_code_bytes;
2457 size_t large_initializer_code_bytes;
2458 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002459
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002460 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002461
2462 size_t dex_instruction_bytes;
2463
Mathieu Chartiere401d142015-04-22 13:56:20 -07002464 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002465 std::vector<size_t> method_outlier_size;
2466 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002467 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002468
Roland Levillain3887c462015-08-12 18:15:42 +01002469 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002470 : oat_file_bytes(0),
2471 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002472 header_bytes(0),
2473 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002474 art_field_bytes(0),
2475 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01002476 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002477 interned_strings_bytes(0),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002478 class_table_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07002479 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002480 alignment_bytes(0),
2481 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002482 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002483 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07002484 class_initializer_code_bytes(0),
2485 large_initializer_code_bytes(0),
2486 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002487 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002488 dex_instruction_bytes(0) {}
2489
Elliott Hughesa0e18062012-04-13 15:59:59 -07002490 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002491 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002492 size_t bytes;
2493 size_t count;
2494 };
2495 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2496 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002497
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002498 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002499 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2500 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002501 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002502 it->second.count += 1;
2503 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002504 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002505 }
2506 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002507
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002508 double PercentOfOatBytes(size_t size) {
2509 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2510 }
2511
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002512 double PercentOfFileBytes(size_t size) {
2513 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2514 }
2515
2516 double PercentOfObjectBytes(size_t size) {
2517 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2518 }
2519
Mathieu Chartiere401d142015-04-22 13:56:20 -07002520 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002521 method_outlier_size.push_back(total_size);
2522 method_outlier_expansion.push_back(expansion);
2523 method_outlier.push_back(method);
2524 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002525
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002526 void DumpOutliers(std::ostream& os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002527 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002528 size_t sum_of_sizes = 0;
2529 size_t sum_of_sizes_squared = 0;
2530 size_t sum_of_expansion = 0;
2531 size_t sum_of_expansion_squared = 0;
2532 size_t n = method_outlier_size.size();
Mathieu Chartier1ebf8d32016-06-09 11:51:27 -07002533 if (n <= 1) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08002534 return;
2535 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002536 for (size_t i = 0; i < n; i++) {
2537 size_t cur_size = method_outlier_size[i];
2538 sum_of_sizes += cur_size;
2539 sum_of_sizes_squared += cur_size * cur_size;
2540 double cur_expansion = method_outlier_expansion[i];
2541 sum_of_expansion += cur_expansion;
2542 sum_of_expansion_squared += cur_expansion * cur_expansion;
2543 }
2544 size_t size_mean = sum_of_sizes / n;
2545 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2546 double expansion_mean = sum_of_expansion / n;
2547 double expansion_variance =
2548 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2549
2550 // Dump methods whose size is a certain number of standard deviations from the mean
2551 size_t dumped_values = 0;
2552 size_t skipped_values = 0;
2553 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2554 size_t cur_size_variance = i * i * size_variance;
2555 bool first = true;
2556 for (size_t j = 0; j < n; j++) {
2557 size_t cur_size = method_outlier_size[j];
2558 if (cur_size > size_mean) {
2559 size_t cur_var = cur_size - size_mean;
2560 cur_var = cur_var * cur_var;
2561 if (cur_var > cur_size_variance) {
2562 if (dumped_values > 20) {
2563 if (i == 1) {
2564 skipped_values++;
2565 } else {
2566 i = 2; // jump to counting for 1 standard deviation
2567 break;
2568 }
2569 } else {
2570 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002571 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002572 first = false;
2573 }
David Sehr709b0702016-10-13 09:12:37 -07002574 os << ArtMethod::PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002575 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002576 method_outlier_size[j] = 0; // don't consider this method again
2577 dumped_values++;
2578 }
2579 }
2580 }
2581 }
2582 }
2583 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002584 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002585 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002586 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002587 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002588
2589 // Dump methods whose expansion is a certain number of standard deviations from the mean
2590 dumped_values = 0;
2591 skipped_values = 0;
2592 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2593 double cur_expansion_variance = i * i * expansion_variance;
2594 bool first = true;
2595 for (size_t j = 0; j < n; j++) {
2596 double cur_expansion = method_outlier_expansion[j];
2597 if (cur_expansion > expansion_mean) {
2598 size_t cur_var = cur_expansion - expansion_mean;
2599 cur_var = cur_var * cur_var;
2600 if (cur_var > cur_expansion_variance) {
2601 if (dumped_values > 20) {
2602 if (i == 1) {
2603 skipped_values++;
2604 } else {
2605 i = 2; // jump to counting for 1 standard deviation
2606 break;
2607 }
2608 } else {
2609 if (first) {
2610 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002611 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002612 first = false;
2613 }
David Sehr709b0702016-10-13 09:12:37 -07002614 os << ArtMethod::PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002615 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002616 method_outlier_expansion[j] = 0.0; // don't consider this method again
2617 dumped_values++;
2618 }
2619 }
2620 }
2621 }
2622 }
2623 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002624 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002625 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002626 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002627 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002628 }
2629
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002630 void Dump(std::ostream& os, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002631 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002632 {
2633 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2634 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002635 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2636 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2637 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2638 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2639 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2640 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002641 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002642 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2643 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002644 header_bytes, PercentOfFileBytes(header_bytes),
2645 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002646 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2647 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002648 dex_cache_arrays_bytes,
2649 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002650 interned_strings_bytes,
2651 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002652 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002653 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002654 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2655 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002656 CHECK_EQ(file_bytes,
2657 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2658 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2659 bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002660 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002661
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002662 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002663 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002664 for (const auto& sizes_and_count : sizes_and_counts) {
2665 const std::string& descriptor(sizes_and_count.first);
2666 double average = static_cast<double>(sizes_and_count.second.bytes) /
2667 static_cast<double>(sizes_and_count.second.count);
2668 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002669 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002670 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002671 descriptor.c_str(), sizes_and_count.second.bytes,
2672 sizes_and_count.second.count, average, percent);
2673 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002674 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002675 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002676 CHECK_EQ(object_bytes, object_bytes_total);
2677
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002678 os << StringPrintf("oat_file_bytes = %8zd\n"
2679 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002680 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2681 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2682 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2683 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002684 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002685 managed_code_bytes,
2686 PercentOfOatBytes(managed_code_bytes),
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002687 native_to_managed_code_bytes,
2688 PercentOfOatBytes(native_to_managed_code_bytes),
2689 class_initializer_code_bytes,
2690 PercentOfOatBytes(class_initializer_code_bytes),
2691 large_initializer_code_bytes,
2692 PercentOfOatBytes(large_initializer_code_bytes),
2693 large_method_code_bytes,
2694 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002695 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002696 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002697 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002698 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2699 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002700 }
2701
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002702 os << "\n" << StringPrintf("vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002703 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002704 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002705
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002706 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2707 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002708 static_cast<double>(managed_code_bytes) /
2709 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002710 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002711 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002712 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002713
2714 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002715 }
2716 } stats_;
2717
2718 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002719 enum {
2720 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2721 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2722 kLargeConstructorDexBytes = 4000,
2723 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2724 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2725 kLargeMethodDexBytes = 16000
2726 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002727
2728 // For performance, use the *os_ directly for anything that doesn't need indentation
2729 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002730 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002731 VariableIndentationOutputStream vios_;
2732 ScopedIndentation indent1_;
2733
Ian Rogers1d54e732013-05-02 21:10:01 -07002734 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002735 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002736 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002737 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002738 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002739
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002740 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002741};
2742
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002743static int DumpImage(gc::space::ImageSpace* image_space,
2744 OatDumperOptions* options,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002745 std::ostream* os) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002746 const ImageHeader& image_header = image_space->GetImageHeader();
2747 if (!image_header.IsValid()) {
2748 fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
2749 return EXIT_FAILURE;
2750 }
2751 ImageDumper image_dumper(os, *image_space, image_header, options);
2752 if (!image_dumper.Dump()) {
2753 return EXIT_FAILURE;
2754 }
2755 return EXIT_SUCCESS;
2756}
2757
2758static int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002759 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002760 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002761 options->class_loader_ = &null_class_loader;
2762
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002763 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002764 if (options->app_image_ != nullptr) {
2765 if (options->app_oat_ == nullptr) {
2766 LOG(ERROR) << "Can not dump app image without app oat file";
Jeff Haodcdc85b2015-12-04 14:06:18 -08002767 return EXIT_FAILURE;
2768 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002769 // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
2770 // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
2771 // pointers into 32 bit pointer sized ArtMethods.
2772 std::string error_msg;
2773 std::unique_ptr<OatFile> oat_file(OatFile::Open(options->app_oat_,
2774 options->app_oat_,
2775 nullptr,
2776 nullptr,
2777 false,
2778 /*low_4gb*/true,
2779 nullptr,
2780 &error_msg));
2781 if (oat_file == nullptr) {
2782 LOG(ERROR) << "Failed to open oat file " << options->app_oat_ << " with error " << error_msg;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002783 return EXIT_FAILURE;
2784 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002785 std::unique_ptr<gc::space::ImageSpace> space(
2786 gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg));
2787 if (space == nullptr) {
2788 LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error "
2789 << error_msg;
2790 }
2791 // Open dex files for the image.
2792 std::vector<std::unique_ptr<const DexFile>> dex_files;
2793 if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
2794 LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
2795 << error_msg;
2796 }
2797 // Dump the actual image.
2798 int result = DumpImage(space.get(), options, os);
2799 if (result != EXIT_SUCCESS) {
2800 return result;
2801 }
2802 // Fall through to dump the boot images.
2803 }
2804
2805 gc::Heap* heap = runtime->GetHeap();
2806 CHECK(heap->HasBootImageSpace()) << "No image spaces";
2807 for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
2808 int result = DumpImage(image_space, options, os);
2809 if (result != EXIT_SUCCESS) {
2810 return result;
2811 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002812 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002813 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002814}
2815
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002816static jobject InstallOatFile(Runtime* runtime,
2817 std::unique_ptr<OatFile> oat_file,
2818 std::vector<const DexFile*>* class_path)
2819 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002820 Thread* self = Thread::Current();
2821 CHECK(self != nullptr);
2822 // Need well-known-classes.
2823 WellKnownClasses::Init(self->GetJniEnv());
2824
2825 // Need to register dex files to get a working dex cache.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002826 OatFile* oat_file_ptr = oat_file.get();
Andreas Gampe00b25f32014-09-17 21:49:05 -07002827 ClassLinker* class_linker = runtime->GetClassLinker();
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002828 runtime->GetOatFileManager().RegisterOatFile(std::move(oat_file));
2829 for (const OatFile::OatDexFile* odf : oat_file_ptr->GetOatDexFiles()) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002830 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002831 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002832 CHECK(dex_file != nullptr) << error_msg;
Vladimir Markocd556b02017-02-03 11:47:34 +00002833 ObjPtr<mirror::DexCache> dex_cache =
2834 class_linker->RegisterDexFile(*dex_file, nullptr);
2835 CHECK(dex_cache != nullptr);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002836 class_path->push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002837 }
2838
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002839 // Need a class loader. Fake that we're a compiler.
2840 // Note: this will run initializers through the unstarted runtime, so make sure it's
2841 // initialized.
2842 interpreter::UnstartedRuntime::Initialize();
2843
2844 jobject class_loader = class_linker->CreatePathClassLoader(self, *class_path);
2845
2846 return class_loader;
2847}
2848
2849static int DumpOatWithRuntime(Runtime* runtime,
2850 std::unique_ptr<OatFile> oat_file,
2851 OatDumperOptions* options,
2852 std::ostream* os) {
2853 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2854 ScopedObjectAccess soa(Thread::Current());
2855
2856 OatFile* oat_file_ptr = oat_file.get();
2857 std::vector<const DexFile*> class_path;
2858 jobject class_loader = InstallOatFile(runtime, std::move(oat_file), &class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002859
2860 // Use the class loader while dumping.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002861 StackHandleScope<1> scope(soa.Self());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002862 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07002863 soa.Decode<mirror::ClassLoader>(class_loader));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002864 options->class_loader_ = &loader_handle;
2865
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002866 OatDumper oat_dumper(*oat_file_ptr, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002867 bool success = oat_dumper.Dump(*os);
2868 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2869}
2870
2871static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002872 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002873 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002874 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002875 options->class_loader_ = &null_class_loader;
2876
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002877 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002878 bool success = oat_dumper.Dump(*os);
2879 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2880}
2881
2882static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2883 std::ostream* os) {
2884 std::string error_msg;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002885 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
2886 oat_filename,
2887 nullptr,
2888 nullptr,
2889 false,
2890 /*low_4gb*/false,
2891 nullptr,
2892 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002893 if (oat_file == nullptr) {
2894 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2895 return EXIT_FAILURE;
2896 }
2897
2898 if (runtime != nullptr) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002899 return DumpOatWithRuntime(runtime, std::move(oat_file), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002900 } else {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002901 return DumpOatWithoutRuntime(oat_file.get(), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002902 }
2903}
2904
David Srbecky2fdd03c2016-03-10 15:32:37 +00002905static int SymbolizeOat(const char* oat_filename, std::string& output_name, bool no_bits) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002906 std::string error_msg;
Andreas Gampe08c277c2017-04-26 22:22:15 -07002907 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
2908 oat_filename,
2909 nullptr,
2910 nullptr,
2911 false,
2912 /*low_4gb*/false,
2913 nullptr,
2914 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002915 if (oat_file == nullptr) {
2916 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2917 return EXIT_FAILURE;
2918 }
2919
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002920 bool result;
2921 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
2922 // files for 64-bit code in the past.
2923 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
Andreas Gampe08c277c2017-04-26 22:22:15 -07002924 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file.get(), output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002925 result = oat_symbolizer.Symbolize();
2926 } else {
Andreas Gampe08c277c2017-04-26 22:22:15 -07002927 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file.get(), output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002928 result = oat_symbolizer.Symbolize();
2929 }
2930 if (!result) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002931 fprintf(stderr, "Failed to symbolize\n");
2932 return EXIT_FAILURE;
2933 }
2934
2935 return EXIT_SUCCESS;
2936}
2937
Andreas Gampe9fded872016-09-25 16:08:35 -07002938class IMTDumper {
2939 public:
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002940 static bool Dump(Runtime* runtime,
2941 const std::string& imt_file,
2942 bool dump_imt_stats,
2943 const char* oat_filename) {
2944 Thread* self = Thread::Current();
2945
2946 ScopedObjectAccess soa(self);
2947 StackHandleScope<1> scope(self);
2948 MutableHandle<mirror::ClassLoader> class_loader = scope.NewHandle<mirror::ClassLoader>(nullptr);
2949 std::vector<const DexFile*> class_path;
2950
2951 if (oat_filename != nullptr) {
2952 std::string error_msg;
2953 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
2954 oat_filename,
2955 nullptr,
2956 nullptr,
2957 false,
2958 /*low_4gb*/false,
2959 nullptr,
2960 &error_msg));
2961 if (oat_file == nullptr) {
2962 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2963 return false;
2964 }
2965
2966 class_loader.Assign(soa.Decode<mirror::ClassLoader>(
2967 InstallOatFile(runtime, std::move(oat_file), &class_path)));
2968 } else {
2969 class_loader.Assign(nullptr); // Boot classloader. Just here for explicit documentation.
2970 class_path = runtime->GetClassLinker()->GetBootClassPath();
2971 }
2972
2973 if (!imt_file.empty()) {
2974 return DumpImt(runtime, imt_file, class_loader);
2975 }
2976
2977 if (dump_imt_stats) {
2978 return DumpImtStats(runtime, class_path, class_loader);
2979 }
2980
2981 LOG(FATAL) << "Should not reach here";
2982 UNREACHABLE();
2983 }
2984
2985 private:
2986 static bool DumpImt(Runtime* runtime,
2987 const std::string& imt_file,
2988 Handle<mirror::ClassLoader> h_class_loader)
2989 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002990 std::vector<std::string> lines = ReadCommentedInputFromFile(imt_file);
2991 std::unordered_set<std::string> prepared;
2992
2993 for (const std::string& line : lines) {
2994 // A line should be either a class descriptor, in which case we will dump the complete IMT,
2995 // or a class descriptor and an interface method, in which case we will lookup the method,
2996 // determine its IMT slot, and check the class' IMT.
2997 size_t first_space = line.find(' ');
2998 if (first_space == std::string::npos) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002999 DumpIMTForClass(runtime, line, h_class_loader, &prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07003000 } else {
3001 DumpIMTForMethod(runtime,
3002 line.substr(0, first_space),
3003 line.substr(first_space + 1, std::string::npos),
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003004 h_class_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003005 &prepared);
3006 }
3007 std::cerr << std::endl;
3008 }
3009
3010 return true;
3011 }
3012
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003013 static bool DumpImtStats(Runtime* runtime,
3014 const std::vector<const DexFile*>& dex_files,
3015 Handle<mirror::ClassLoader> h_class_loader)
3016 REQUIRES_SHARED(Locks::mutator_lock_) {
3017 size_t without_imt = 0;
3018 size_t with_imt = 0;
Andreas Gampe9fded872016-09-25 16:08:35 -07003019 std::map<size_t, size_t> histogram;
3020
3021 ClassLinker* class_linker = runtime->GetClassLinker();
3022 const PointerSize pointer_size = class_linker->GetImagePointerSize();
3023 std::unordered_set<std::string> prepared;
3024
3025 Thread* self = Thread::Current();
Andreas Gampe9fded872016-09-25 16:08:35 -07003026 StackHandleScope<1> scope(self);
3027 MutableHandle<mirror::Class> h_klass(scope.NewHandle<mirror::Class>(nullptr));
3028
3029 for (const DexFile* dex_file : dex_files) {
3030 for (uint32_t class_def_index = 0;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003031 class_def_index != dex_file->NumClassDefs();
3032 ++class_def_index) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003033 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
3034 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003035 h_klass.Assign(class_linker->FindClass(self, descriptor, h_class_loader));
Andreas Gampefa4333d2017-02-14 11:10:34 -08003036 if (h_klass == nullptr) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003037 std::cerr << "Warning: could not load " << descriptor << std::endl;
3038 continue;
3039 }
3040
3041 if (HasNoIMT(runtime, h_klass, pointer_size, &prepared)) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003042 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003043 continue;
3044 }
3045
3046 ImTable* im_table = PrepareAndGetImTable(runtime, h_klass, pointer_size, &prepared);
3047 if (im_table == nullptr) {
3048 // Should not happen, but accept.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003049 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003050 continue;
3051 }
3052
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003053 with_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003054 for (size_t imt_index = 0; imt_index != ImTable::kSize; ++imt_index) {
3055 ArtMethod* ptr = im_table->Get(imt_index, pointer_size);
3056 if (ptr->IsRuntimeMethod()) {
3057 if (ptr->IsImtUnimplementedMethod()) {
3058 histogram[0]++;
3059 } else {
3060 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3061 histogram[current_table->NumEntries(pointer_size)]++;
3062 }
3063 } else {
3064 histogram[1]++;
3065 }
3066 }
3067 }
3068 }
3069
3070 std::cerr << "IMT stats:"
3071 << std::endl << std::endl;
3072
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003073 std::cerr << " " << with_imt << " classes with IMT."
Andreas Gampe9fded872016-09-25 16:08:35 -07003074 << std::endl << std::endl;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003075 std::cerr << " " << without_imt << " classes without IMT (or copy from Object)."
Andreas Gampe9fded872016-09-25 16:08:35 -07003076 << std::endl << std::endl;
3077
3078 double sum_one = 0;
3079 size_t count_one = 0;
3080
3081 std::cerr << " " << "IMT histogram" << std::endl;
3082 for (auto& bucket : histogram) {
3083 std::cerr << " " << bucket.first << " " << bucket.second << std::endl;
3084 if (bucket.first > 0) {
3085 sum_one += bucket.second * bucket.first;
3086 count_one += bucket.second;
3087 }
3088 }
3089
3090 double count_zero = count_one + histogram[0];
3091 std::cerr << " Stats:" << std::endl;
3092 std::cerr << " Average depth (including empty): " << (sum_one / count_zero) << std::endl;
3093 std::cerr << " Average depth (excluding empty): " << (sum_one / count_one) << std::endl;
3094
3095 return true;
3096 }
3097
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003098 // Return whether the given class has no IMT (or the one shared with java.lang.Object).
Andreas Gampe9fded872016-09-25 16:08:35 -07003099 static bool HasNoIMT(Runtime* runtime,
3100 Handle<mirror::Class> klass,
3101 const PointerSize pointer_size,
3102 std::unordered_set<std::string>* prepared)
3103 REQUIRES_SHARED(Locks::mutator_lock_) {
3104 if (klass->IsObjectClass() || !klass->ShouldHaveImt()) {
3105 return true;
3106 }
3107
3108 if (klass->GetImt(pointer_size) == nullptr) {
3109 PrepareClass(runtime, klass, prepared);
3110 }
3111
3112 mirror::Class* object_class = mirror::Class::GetJavaLangClass()->GetSuperClass();
3113 DCHECK(object_class->IsObjectClass());
3114
3115 bool result = klass->GetImt(pointer_size) == object_class->GetImt(pointer_size);
3116
Mathieu Chartier6beced42016-11-15 15:51:31 -08003117 if (klass->GetIfTable()->Count() == 0) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003118 DCHECK(result);
3119 }
3120
3121 return result;
3122 }
3123
3124 static void PrintTable(ImtConflictTable* table, PointerSize pointer_size)
3125 REQUIRES_SHARED(Locks::mutator_lock_) {
3126 if (table == nullptr) {
3127 std::cerr << " <No IMT?>" << std::endl;
3128 return;
3129 }
3130 size_t table_index = 0;
3131 for (;;) {
3132 ArtMethod* ptr = table->GetInterfaceMethod(table_index, pointer_size);
3133 if (ptr == nullptr) {
3134 return;
3135 }
3136 table_index++;
David Sehr709b0702016-10-13 09:12:37 -07003137 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003138 }
3139 }
3140
3141 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3142 Thread* self,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003143 Handle<mirror::ClassLoader> h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003144 const std::string& class_name,
3145 const PointerSize pointer_size,
3146 mirror::Class** klass_out,
3147 std::unordered_set<std::string>* prepared)
3148 REQUIRES_SHARED(Locks::mutator_lock_) {
3149 if (class_name.empty()) {
3150 return nullptr;
3151 }
3152
3153 std::string descriptor;
3154 if (class_name[0] == 'L') {
3155 descriptor = class_name;
3156 } else {
3157 descriptor = DotToDescriptor(class_name.c_str());
3158 }
3159
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003160 mirror::Class* klass = runtime->GetClassLinker()->FindClass(self, descriptor.c_str(), h_loader);
Andreas Gampe9fded872016-09-25 16:08:35 -07003161
3162 if (klass == nullptr) {
3163 self->ClearException();
3164 std::cerr << "Did not find " << class_name << std::endl;
3165 *klass_out = nullptr;
3166 return nullptr;
3167 }
3168
3169 StackHandleScope<1> scope(Thread::Current());
3170 Handle<mirror::Class> h_klass = scope.NewHandle<mirror::Class>(klass);
3171
3172 ImTable* ret = PrepareAndGetImTable(runtime, h_klass, pointer_size, prepared);
3173 *klass_out = h_klass.Get();
3174 return ret;
3175 }
3176
3177 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3178 Handle<mirror::Class> h_klass,
3179 const PointerSize pointer_size,
3180 std::unordered_set<std::string>* prepared)
3181 REQUIRES_SHARED(Locks::mutator_lock_) {
3182 PrepareClass(runtime, h_klass, prepared);
3183 return h_klass->GetImt(pointer_size);
3184 }
3185
3186 static void DumpIMTForClass(Runtime* runtime,
3187 const std::string& class_name,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003188 Handle<mirror::ClassLoader> h_loader,
3189 std::unordered_set<std::string>* prepared)
3190 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003191 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
3192 mirror::Class* klass;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003193 ImTable* imt = PrepareAndGetImTable(runtime,
3194 Thread::Current(),
3195 h_loader,
3196 class_name,
3197 pointer_size,
3198 &klass,
3199 prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07003200 if (imt == nullptr) {
3201 return;
3202 }
3203
3204 std::cerr << class_name << std::endl << " IMT:" << std::endl;
3205 for (size_t index = 0; index < ImTable::kSize; ++index) {
3206 std::cerr << " " << index << ":" << std::endl;
3207 ArtMethod* ptr = imt->Get(index, pointer_size);
3208 if (ptr->IsRuntimeMethod()) {
3209 if (ptr->IsImtUnimplementedMethod()) {
3210 std::cerr << " <empty>" << std::endl;
3211 } else {
3212 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3213 PrintTable(current_table, pointer_size);
3214 }
3215 } else {
David Sehr709b0702016-10-13 09:12:37 -07003216 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003217 }
3218 }
3219
3220 std::cerr << " Interfaces:" << std::endl;
3221 // Run through iftable, find methods that slot here, see if they fit.
3222 mirror::IfTable* if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003223 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
3224 mirror::Class* iface = if_table->GetInterface(i);
3225 std::string iface_name;
3226 std::cerr << " " << iface->GetDescriptor(&iface_name) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003227
Mathieu Chartier6beced42016-11-15 15:51:31 -08003228 for (ArtMethod& iface_method : iface->GetVirtualMethods(pointer_size)) {
3229 uint32_t class_hash, name_hash, signature_hash;
3230 ImTable::GetImtHashComponents(&iface_method, &class_hash, &name_hash, &signature_hash);
3231 uint32_t imt_slot = ImTable::GetImtIndex(&iface_method);
3232 std::cerr << " " << iface_method.PrettyMethod(true)
3233 << " slot=" << imt_slot
3234 << std::hex
3235 << " class_hash=0x" << class_hash
3236 << " name_hash=0x" << name_hash
3237 << " signature_hash=0x" << signature_hash
3238 << std::dec
3239 << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003240 }
3241 }
3242 }
3243
3244 static void DumpIMTForMethod(Runtime* runtime,
3245 const std::string& class_name,
3246 const std::string& method,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003247 Handle<mirror::ClassLoader> h_loader,
3248 std::unordered_set<std::string>* prepared)
3249 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003250 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
3251 mirror::Class* klass;
3252 ImTable* imt = PrepareAndGetImTable(runtime,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003253 Thread::Current(),
3254 h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003255 class_name,
3256 pointer_size,
3257 &klass,
3258 prepared);
3259 if (imt == nullptr) {
3260 return;
3261 }
3262
3263 std::cerr << class_name << " <" << method << ">" << std::endl;
3264 for (size_t index = 0; index < ImTable::kSize; ++index) {
3265 ArtMethod* ptr = imt->Get(index, pointer_size);
3266 if (ptr->IsRuntimeMethod()) {
3267 if (ptr->IsImtUnimplementedMethod()) {
3268 continue;
3269 }
3270
3271 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3272 if (current_table == nullptr) {
3273 continue;
3274 }
3275
3276 size_t table_index = 0;
3277 for (;;) {
3278 ArtMethod* ptr2 = current_table->GetInterfaceMethod(table_index, pointer_size);
3279 if (ptr2 == nullptr) {
3280 break;
3281 }
3282 table_index++;
3283
David Sehr709b0702016-10-13 09:12:37 -07003284 std::string p_name = ptr2->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003285 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003286 std::cerr << " Slot "
3287 << index
3288 << " ("
3289 << current_table->NumEntries(pointer_size)
3290 << ")"
3291 << std::endl;
3292 PrintTable(current_table, pointer_size);
3293 return;
3294 }
3295 }
3296 } else {
David Sehr709b0702016-10-13 09:12:37 -07003297 std::string p_name = ptr->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003298 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003299 std::cerr << " Slot " << index << " (1)" << std::endl;
3300 std::cerr << " " << p_name << std::endl;
3301 } else {
3302 // Run through iftable, find methods that slot here, see if they fit.
3303 mirror::IfTable* if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003304 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
3305 mirror::Class* iface = if_table->GetInterface(i);
3306 size_t num_methods = iface->NumDeclaredVirtualMethods();
3307 if (num_methods > 0) {
3308 for (ArtMethod& iface_method : iface->GetMethods(pointer_size)) {
3309 if (ImTable::GetImtIndex(&iface_method) == index) {
3310 std::string i_name = iface_method.PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003311 if (android::base::StartsWith(i_name, method.c_str())) {
Mathieu Chartier6beced42016-11-15 15:51:31 -08003312 std::cerr << " Slot " << index << " (1)" << std::endl;
3313 std::cerr << " " << p_name << " (" << i_name << ")" << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003314 }
3315 }
3316 }
3317 }
3318 }
3319 }
3320 }
3321 }
3322 }
3323
3324 // Read lines from the given stream, dropping comments and empty lines
3325 static std::vector<std::string> ReadCommentedInputStream(std::istream& in_stream) {
3326 std::vector<std::string> output;
3327 while (in_stream.good()) {
3328 std::string dot;
3329 std::getline(in_stream, dot);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003330 if (android::base::StartsWith(dot, "#") || dot.empty()) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003331 continue;
3332 }
3333 output.push_back(dot);
3334 }
3335 return output;
3336 }
3337
3338 // Read lines from the given file, dropping comments and empty lines.
3339 static std::vector<std::string> ReadCommentedInputFromFile(const std::string& input_filename) {
3340 std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
3341 if (input_file.get() == nullptr) {
3342 LOG(ERROR) << "Failed to open input file " << input_filename;
3343 return std::vector<std::string>();
3344 }
3345 std::vector<std::string> result = ReadCommentedInputStream(*input_file);
3346 input_file->close();
3347 return result;
3348 }
3349
3350 // Prepare a class, i.e., ensure it has a filled IMT. Will do so recursively for superclasses,
3351 // and note in the given set that the work was done.
3352 static void PrepareClass(Runtime* runtime,
3353 Handle<mirror::Class> h_klass,
3354 std::unordered_set<std::string>* done)
3355 REQUIRES_SHARED(Locks::mutator_lock_) {
3356 if (!h_klass->ShouldHaveImt()) {
3357 return;
3358 }
3359
3360 std::string name;
3361 name = h_klass->GetDescriptor(&name);
3362
3363 if (done->find(name) != done->end()) {
3364 return;
3365 }
3366 done->insert(name);
3367
3368 if (h_klass->HasSuperClass()) {
3369 StackHandleScope<1> h(Thread::Current());
3370 PrepareClass(runtime, h.NewHandle<mirror::Class>(h_klass->GetSuperClass()), done);
3371 }
3372
3373 if (!h_klass->IsTemp()) {
3374 runtime->GetClassLinker()->FillIMTAndConflictTables(h_klass.Get());
3375 }
3376 }
3377};
3378
Igor Murashkin37743352014-11-13 14:38:00 -08003379struct OatdumpArgs : public CmdlineArgs {
3380 protected:
3381 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003382
Igor Murashkin37743352014-11-13 14:38:00 -08003383 virtual ParseStatus ParseCustom(const StringPiece& option,
3384 std::string* error_msg) OVERRIDE {
3385 {
3386 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
3387 if (base_parse != kParseUnknownArgument) {
3388 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003389 }
3390 }
3391
Igor Murashkin37743352014-11-13 14:38:00 -08003392 if (option.starts_with("--oat-file=")) {
3393 oat_filename_ = option.substr(strlen("--oat-file=")).data();
3394 } else if (option.starts_with("--image=")) {
3395 image_location_ = option.substr(strlen("--image=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003396 } else if (option == "--no-dump:vmap") {
3397 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01003398 } else if (option =="--dump:code_info_stack_maps") {
3399 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003400 } else if (option == "--no-disassemble") {
3401 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003402 } else if (option =="--header-only") {
3403 dump_header_only_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003404 } else if (option.starts_with("--symbolize=")) {
3405 oat_filename_ = option.substr(strlen("--symbolize=")).data();
3406 symbolize_ = true;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003407 } else if (option.starts_with("--only-keep-debug")) {
3408 only_keep_debug_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003409 } else if (option.starts_with("--class-filter=")) {
3410 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003411 } else if (option.starts_with("--method-filter=")) {
3412 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003413 } else if (option.starts_with("--list-classes")) {
3414 list_classes_ = true;
3415 } else if (option.starts_with("--list-methods")) {
3416 list_methods_ = true;
3417 } else if (option.starts_with("--export-dex-to=")) {
3418 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
3419 } else if (option.starts_with("--addr2instr=")) {
3420 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
3421 *error_msg = "Address conversion failed";
3422 return kParseError;
3423 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003424 } else if (option.starts_with("--app-image=")) {
3425 app_image_ = option.substr(strlen("--app-image=")).data();
3426 } else if (option.starts_with("--app-oat=")) {
3427 app_oat_ = option.substr(strlen("--app-oat=")).data();
Andreas Gampe9fded872016-09-25 16:08:35 -07003428 } else if (option.starts_with("--dump-imt=")) {
3429 imt_dump_ = option.substr(strlen("--dump-imt=")).data();
3430 } else if (option == "--dump-imt-stats") {
3431 imt_stat_dump_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003432 } else {
3433 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003434 }
3435
Igor Murashkin37743352014-11-13 14:38:00 -08003436 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003437 }
3438
Igor Murashkin37743352014-11-13 14:38:00 -08003439 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
3440 // Infer boot image location from the image location if possible.
3441 if (boot_image_location_ == nullptr) {
3442 boot_image_location_ = image_location_;
3443 }
3444
3445 // Perform the parent checks.
3446 ParseStatus parent_checks = Base::ParseChecks(error_msg);
3447 if (parent_checks != kParseOk) {
3448 return parent_checks;
3449 }
3450
3451 // Perform our own checks.
3452 if (image_location_ == nullptr && oat_filename_ == nullptr) {
3453 *error_msg = "Either --image or --oat-file must be specified";
3454 return kParseError;
3455 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
3456 *error_msg = "Either --image or --oat-file must be specified but not both";
3457 return kParseError;
3458 }
3459
3460 return kParseOk;
3461 }
3462
3463 virtual std::string GetUsage() const {
3464 std::string usage;
3465
3466 usage +=
3467 "Usage: oatdump [options] ...\n"
3468 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
3469 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
3470 "\n"
3471 // Either oat-file or image is required.
3472 " --oat-file=<file.oat>: specifies an input oat filename.\n"
3473 " Example: --oat-file=/system/framework/boot.oat\n"
3474 "\n"
3475 " --image=<file.art>: specifies an input image location.\n"
3476 " Example: --image=/system/framework/boot.art\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003477 "\n"
3478 " --app-image=<file.art>: specifies an input app image. Must also have a specified\n"
Roland Levillain4f1c9e62017-06-28 16:44:30 +01003479 " boot image (with --image) and app oat file (with --app-oat).\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003480 " Example: --app-image=app.art\n"
3481 "\n"
3482 " --app-oat=<file.odex>: specifies an input app oat.\n"
3483 " Example: --app-oat=app.odex\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003484 "\n";
3485
3486 usage += Base::GetUsage();
3487
3488 usage += // Optional.
Igor Murashkin37743352014-11-13 14:38:00 -08003489 " --no-dump:vmap may be used to disable vmap dumping.\n"
3490 " Example: --no-dump:vmap\n"
3491 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01003492 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
3493 " Example: --dump:code_info_stack_maps\n"
3494 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003495 " --no-disassemble may be used to disable disassembly.\n"
3496 " Example: --no-disassemble\n"
3497 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00003498 " --header-only may be used to print only the oat header.\n"
3499 " Example: --header-only\n"
3500 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003501 " --list-classes may be used to list target file classes (can be used with filters).\n"
3502 " Example: --list-classes\n"
3503 " Example: --list-classes --class-filter=com.example.foo\n"
3504 "\n"
3505 " --list-methods may be used to list target file methods (can be used with filters).\n"
3506 " Example: --list-methods\n"
3507 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
3508 "\n"
3509 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
3510 " Example: --symbolize=/system/framework/boot.oat\n"
3511 "\n"
David Srbecky2fdd03c2016-03-10 15:32:37 +00003512 " --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
3513 " .rodata and .text sections are omitted in the output file to save space.\n"
3514 " Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
3515 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003516 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
3517 " Example: --class-filter=com.example.foo\n"
3518 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003519 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
3520 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003521 "\n"
3522 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
3523 " Example: --export-dex-to=/data/local/tmp\n"
3524 "\n"
3525 " --addr2instr=<address>: output matching method disassembled code from relative\n"
3526 " address (e.g. PC from crash dump)\n"
3527 " Example: --addr2instr=0x00001a3b\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003528 "\n"
3529 " --dump-imt=<file.txt>: output IMT collisions (if any) for the given receiver\n"
3530 " types and interface methods in the given file. The file\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003531 " is read line-wise, where each line should either be a class\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003532 " name or descriptor, or a class name/descriptor and a prefix\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003533 " of a complete method name (separated by a whitespace).\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003534 " Example: --dump-imt=imt.txt\n"
3535 "\n"
3536 " --dump-imt-stats: output IMT statistics for the given boot image\n"
3537 " Example: --dump-imt-stats"
Igor Murashkin37743352014-11-13 14:38:00 -08003538 "\n";
3539
3540 return usage;
3541 }
3542
3543 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07003544 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003545 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00003546 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07003547 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003548 std::string elf_filename_prefix_;
Andreas Gampe9fded872016-09-25 16:08:35 -07003549 std::string imt_dump_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003550 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01003551 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003552 bool disassemble_code_ = true;
3553 bool symbolize_ = false;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003554 bool only_keep_debug_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003555 bool list_classes_ = false;
3556 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003557 bool dump_header_only_ = false;
Andreas Gampe9fded872016-09-25 16:08:35 -07003558 bool imt_stat_dump_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003559 uint32_t addr2instr_ = 0;
3560 const char* export_dex_location_ = nullptr;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003561 const char* app_image_ = nullptr;
3562 const char* app_oat_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003563};
3564
Igor Murashkin37743352014-11-13 14:38:00 -08003565struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
3566 virtual bool NeedsRuntime() OVERRIDE {
3567 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003568
Igor Murashkin37743352014-11-13 14:38:00 -08003569 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
3570 bool absolute_addresses = (args_->oat_filename_ == nullptr);
3571
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003572 oat_dumper_options_.reset(new OatDumperOptions(
Igor Murashkin37743352014-11-13 14:38:00 -08003573 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01003574 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08003575 args_->disassemble_code_,
3576 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003577 args_->class_filter_,
3578 args_->method_filter_,
3579 args_->list_classes_,
3580 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00003581 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003582 args_->export_dex_location_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003583 args_->app_image_,
3584 args_->app_oat_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003585 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08003586
Andreas Gampe9fded872016-09-25 16:08:35 -07003587 return (args_->boot_image_location_ != nullptr ||
3588 args_->image_location_ != nullptr ||
3589 !args_->imt_dump_.empty()) &&
Igor Murashkin37743352014-11-13 14:38:00 -08003590 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003591 }
3592
Igor Murashkin37743352014-11-13 14:38:00 -08003593 virtual bool ExecuteWithoutRuntime() OVERRIDE {
3594 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08003595 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003596
Mathieu Chartierd424d082014-10-15 10:31:46 -07003597 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08003598
Andreas Gampec24f3992014-12-17 20:40:11 -08003599 if (args_->symbolize_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00003600 // ELF has special kind of section called SHT_NOBITS which allows us to create
3601 // sections which exist but their data is omitted from the ELF file to save space.
3602 // This is what "strip --only-keep-debug" does when it creates separate ELF file
3603 // with only debug data. We use it in similar way to exclude .rodata and .text.
3604 bool no_bits = args_->only_keep_debug_;
3605 return SymbolizeOat(args_->oat_filename_, args_->output_name_, no_bits) == EXIT_SUCCESS;
Andreas Gampec24f3992014-12-17 20:40:11 -08003606 } else {
3607 return DumpOat(nullptr,
3608 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003609 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08003610 args_->os_) == EXIT_SUCCESS;
3611 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07003612 }
3613
Igor Murashkin37743352014-11-13 14:38:00 -08003614 virtual bool ExecuteWithRuntime(Runtime* runtime) {
3615 CHECK(args_ != nullptr);
3616
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003617 if (!args_->imt_dump_.empty() || args_->imt_stat_dump_) {
3618 return IMTDumper::Dump(runtime,
3619 args_->imt_dump_,
3620 args_->imt_stat_dump_,
3621 args_->oat_filename_);
Andreas Gampe9fded872016-09-25 16:08:35 -07003622 }
3623
Igor Murashkin37743352014-11-13 14:38:00 -08003624 if (args_->oat_filename_ != nullptr) {
3625 return DumpOat(runtime,
3626 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003627 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08003628 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003629 }
Igor Murashkin37743352014-11-13 14:38:00 -08003630
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003631 return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003632 }
3633
Igor Murashkin37743352014-11-13 14:38:00 -08003634 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
3635};
Andreas Gampe00b25f32014-09-17 21:49:05 -07003636
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003637} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07003638
3639int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08003640 art::OatdumpMain main;
3641 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07003642}