blob: 4161067250529de6efc87444ae93332c8017bd17 [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);
Vladimir Markocd87c3e2017-09-05 13:11:57 +01001975 const auto& object_section = image_header_.GetObjectsSection();
1976 const auto& field_section = image_header_.GetFieldsSection();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001977 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Markocd87c3e2017-09-05 13:11:57 +01001978 const auto& dex_cache_arrays_section = image_header_.GetDexCacheArraysSection();
1979 const auto& intern_section = image_header_.GetInternedStringsSection();
1980 const auto& class_table_section = image_header_.GetClassTableSection();
1981 const auto& bitmap_section = image_header_.GetImageBitmapSection();
Andreas Gampeace0dc12016-01-20 13:33:13 -08001982
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001983 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001984
1985 // Objects are kObjectAlignment-aligned.
1986 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1987 if (object_section.Offset() > header_bytes) {
1988 stats_.alignment_bytes += object_section.Offset() - header_bytes;
1989 }
1990
1991 // Field section is 4-byte aligned.
1992 constexpr size_t kFieldSectionAlignment = 4U;
1993 uint32_t end_objects = object_section.Offset() + object_section.Size();
1994 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
1995 stats_.alignment_bytes += field_section.Offset() - end_objects;
1996
1997 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
1998 uint32_t end_fields = field_section.Offset() + field_section.Size();
1999 CHECK_ALIGNED(method_section.Offset(), 4);
2000 stats_.alignment_bytes += method_section.Offset() - end_fields;
2001
2002 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
2003 uint32_t end_methods = method_section.Offset() + method_section.Size();
2004 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
2005 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
2006
2007 // Intern table is 8-byte aligned.
2008 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01002009 CHECK_ALIGNED(intern_section.Offset(), sizeof(uint64_t));
Andreas Gampeace0dc12016-01-20 13:33:13 -08002010 stats_.alignment_bytes += intern_section.Offset() - end_caches;
2011
2012 // Add space between intern table and class table.
2013 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
2014 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
2015
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002016 // Add space between end of image data and bitmap. Expect the bitmap to be page-aligned.
2017 const size_t bitmap_offset = sizeof(ImageHeader) + data_size;
Andreas Gampeace0dc12016-01-20 13:33:13 -08002018 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002019 stats_.alignment_bytes += RoundUp(bitmap_offset, kPageSize) - bitmap_offset;
Andreas Gampeace0dc12016-01-20 13:33:13 -08002020
Mathieu Chartiere401d142015-04-22 13:56:20 -07002021 stats_.bitmap_bytes += bitmap_section.Size();
2022 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01002023 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01002024 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002025 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002026 stats_.class_table_bytes += class_table_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002027 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002028 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002029
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002030 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002031
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002032 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002033 }
2034
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002035 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002036 class DumpArtMethodVisitor : public ArtMethodVisitor {
2037 public:
2038 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
2039
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002040 virtual void Visit(ArtMethod* method) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002041 std::ostream& indent_os = image_dumper_->vios_.Stream();
David Sehr709b0702016-10-13 09:12:37 -07002042 indent_os << method << " " << " ArtMethod: " << ArtMethod::PrettyMethod(method) << "\n";
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002043 image_dumper_->DumpMethod(method, indent_os);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002044 indent_os << "\n";
2045 }
2046
2047 private:
2048 ImageDumper* const image_dumper_;
2049 };
2050
Mathieu Chartier3398c782016-09-30 10:27:43 -07002051 static void PrettyObjectValue(std::ostream& os,
2052 ObjPtr<mirror::Class> type,
2053 ObjPtr<mirror::Object> value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002054 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002055 CHECK(type != nullptr);
2056 if (value == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07002057 os << StringPrintf("null %s\n", type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002058 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002059 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002060 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07002061 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07002062 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002063 mirror::Class* klass = value->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07002064 os << StringPrintf("%p Class: %s\n", klass, mirror::Class::PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002065 } else {
David Sehr709b0702016-10-13 09:12:37 -07002066 os << StringPrintf("%p %s\n", value.Ptr(), type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002067 }
2068 }
2069
Mathieu Chartier3398c782016-09-30 10:27:43 -07002070 static void PrintField(std::ostream& os, ArtField* field, ObjPtr<mirror::Object> obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002071 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07002072 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08002073 switch (field->GetTypeAsPrimitiveType()) {
2074 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08002075 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002076 break;
2077 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002078 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002079 break;
2080 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002081 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002082 break;
2083 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002084 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002085 break;
2086 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07002087 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002088 break;
2089 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07002090 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002091 break;
2092 case Primitive::kPrimBoolean:
Roland Levillain5e8d5f02016-10-18 18:03:43 +01002093 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj) ? "true" : "false",
Fred Shih37f05ef2014-07-16 18:38:08 -07002094 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002095 break;
2096 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07002097 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002098 break;
2099 case Primitive::kPrimNot: {
2100 // Get the value, don't compute the type unless it is non-null as we don't want
2101 // to cause class loading.
Mathieu Chartier3398c782016-09-30 10:27:43 -07002102 ObjPtr<mirror::Object> value = field->GetObj(obj);
Ian Rogers08f1f502014-12-02 15:04:37 -08002103 if (value == nullptr) {
2104 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07002105 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08002106 // Grab the field type without causing resolution.
Mathieu Chartier3398c782016-09-30 10:27:43 -07002107 ObjPtr<mirror::Class> field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08002108 if (field_type != nullptr) {
2109 PrettyObjectValue(os, field_type, value);
2110 } else {
Mathieu Chartier3398c782016-09-30 10:27:43 -07002111 os << StringPrintf("%p %s\n",
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07002112 value.Ptr(),
Ian Rogers08f1f502014-12-02 15:04:37 -08002113 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
2114 }
Ian Rogers50239c72013-06-17 14:53:22 -07002115 }
Ian Rogers08f1f502014-12-02 15:04:37 -08002116 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07002117 }
Ian Rogers08f1f502014-12-02 15:04:37 -08002118 default:
2119 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
2120 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08002121 }
2122 }
2123
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002124 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002125 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002126 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002127 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002128 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08002129 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002130 for (ArtField& field : klass->GetIFields()) {
2131 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08002132 }
2133 }
2134
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002135 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002136 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002137 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002138
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002139 const void* GetQuickOatCodeBegin(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08002140 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002141 image_header_.GetPointerSize());
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002142 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002143 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002144 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002145 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002146 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002147 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002148 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002149 }
2150
Mathieu Chartiere401d142015-04-22 13:56:20 -07002151 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002152 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002153 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
2154 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002155 return 0;
2156 }
2157 return oat_code_begin[-1];
2158 }
2159
Mathieu Chartiere401d142015-04-22 13:56:20 -07002160 const void* GetQuickOatCodeEnd(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002161 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002162 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002163 if (oat_code_begin == nullptr) {
2164 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002165 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002166 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002167 }
2168
Andreas Gampe0c183382017-07-13 22:26:24 -07002169 void DumpObject(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002170 DCHECK(obj != nullptr);
Andreas Gampe0c183382017-07-13 22:26:24 -07002171 if (!InDumpSpace(obj)) {
Brian Carlstrom78128a62011-09-15 17:21:19 -07002172 return;
2173 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002174
2175 size_t object_bytes = obj->SizeOf();
2176 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
Andreas Gampe0c183382017-07-13 22:26:24 -07002177 stats_.object_bytes += object_bytes;
2178 stats_.alignment_bytes += alignment_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002179
Andreas Gampe0c183382017-07-13 22:26:24 -07002180 std::ostream& os = vios_.Stream();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002181
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002182 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08002183 if (obj_class->IsArrayClass()) {
David Sehr709b0702016-10-13 09:12:37 -07002184 os << StringPrintf("%p: %s length:%d\n", obj, obj_class->PrettyDescriptor().c_str(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002185 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08002186 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002187 mirror::Class* klass = obj->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07002188 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj,
2189 mirror::Class::PrettyDescriptor(klass).c_str())
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002190 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08002191 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002192 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07002193 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002194 } else {
David Sehr709b0702016-10-13 09:12:37 -07002195 os << StringPrintf("%p: %s\n", obj, obj_class->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002196 }
Andreas Gampe0c183382017-07-13 22:26:24 -07002197 ScopedIndentation indent1(&vios_);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002198 DumpFields(os, obj, obj_class);
Andreas Gampe0c183382017-07-13 22:26:24 -07002199 const PointerSize image_pointer_size = image_header_.GetPointerSize();
Ian Rogersd5b32602012-02-26 16:40:04 -08002200 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002201 auto* obj_array = obj->AsObjectArray<mirror::Object>();
2202 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002203 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002204 size_t run = 0;
2205 for (int32_t j = i + 1; j < length; j++) {
2206 if (value == obj_array->Get(j)) {
2207 run++;
2208 } else {
2209 break;
2210 }
2211 }
2212 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002213 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002214 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002215 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08002216 i = i + run;
2217 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002218 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002219 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002220 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08002221 }
2222 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07002223 mirror::Class* klass = obj->AsClass();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002224 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002225 os << "STATICS:\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002226 ScopedIndentation indent2(&vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002227 for (ArtField& field : klass->GetSFields()) {
2228 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08002229 }
2230 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002231 } else {
Andreas Gampe0c183382017-07-13 22:26:24 -07002232 auto it = dex_caches_.find(obj);
2233 if (it != dex_caches_.end()) {
Vladimir Marko05792b92015-08-03 11:56:49 +01002234 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Vladimir Markocd87c3e2017-09-05 13:11:57 +01002235 const auto& field_section = image_header_.GetFieldsSection();
Andreas Gampe0c183382017-07-13 22:26:24 -07002236 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01002237 size_t num_methods = dex_cache->NumResolvedMethods();
2238 if (num_methods != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002239 os << "Methods (size=" << num_methods << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002240 ScopedIndentation indent2(&vios_);
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002241 mirror::MethodDexCacheType* resolved_methods = dex_cache->GetResolvedMethods();
Vladimir Marko05792b92015-08-03 11:56:49 +01002242 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002243 ArtMethod* elem = mirror::DexCache::GetNativePairPtrSize(
2244 resolved_methods, i, image_pointer_size).object;
Vladimir Marko05792b92015-08-03 11:56:49 +01002245 size_t run = 0;
2246 for (size_t j = i + 1;
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002247 j != length &&
2248 elem == mirror::DexCache::GetNativePairPtrSize(
2249 resolved_methods, j, image_pointer_size).object;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002250 ++j) {
2251 ++run;
2252 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002253 if (run == 0) {
2254 os << StringPrintf("%zd: ", i);
2255 } else {
2256 os << StringPrintf("%zd to %zd: ", i, i + run);
2257 i = i + run;
2258 }
2259 std::string msg;
2260 if (elem == nullptr) {
2261 msg = "null";
2262 } else if (method_section.Contains(
Andreas Gampe0c183382017-07-13 22:26:24 -07002263 reinterpret_cast<uint8_t*>(elem) - image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002264 msg = reinterpret_cast<ArtMethod*>(elem)->PrettyMethod();
Vladimir Marko05792b92015-08-03 11:56:49 +01002265 } else {
2266 msg = "<not in method section>";
2267 }
2268 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07002269 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002270 }
2271 size_t num_fields = dex_cache->NumResolvedFields();
2272 if (num_fields != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002273 os << "Fields (size=" << num_fields << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002274 ScopedIndentation indent2(&vios_);
Vladimir Marko05792b92015-08-03 11:56:49 +01002275 auto* resolved_fields = dex_cache->GetResolvedFields();
2276 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002277 ArtField* elem = mirror::DexCache::GetNativePairPtrSize(
Vladimir Markof44d36c2017-03-14 14:18:46 +00002278 resolved_fields, i, image_pointer_size).object;
Vladimir Marko05792b92015-08-03 11:56:49 +01002279 size_t run = 0;
2280 for (size_t j = i + 1;
Vladimir Markof44d36c2017-03-14 14:18:46 +00002281 j != length &&
2282 elem == mirror::DexCache::GetNativePairPtrSize(
2283 resolved_fields, j, image_pointer_size).object;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002284 ++j) {
2285 ++run;
2286 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002287 if (run == 0) {
2288 os << StringPrintf("%zd: ", i);
2289 } else {
2290 os << StringPrintf("%zd to %zd: ", i, i + run);
2291 i = i + run;
2292 }
2293 std::string msg;
2294 if (elem == nullptr) {
2295 msg = "null";
2296 } else if (field_section.Contains(
Andreas Gampe0c183382017-07-13 22:26:24 -07002297 reinterpret_cast<uint8_t*>(elem) - image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002298 msg = reinterpret_cast<ArtField*>(elem)->PrettyField();
Vladimir Marko05792b92015-08-03 11:56:49 +01002299 } else {
2300 msg = "<not in field section>";
2301 }
2302 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002303 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002304 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002305 size_t num_types = dex_cache->NumResolvedTypes();
2306 if (num_types != 0u) {
2307 os << "Types (size=" << num_types << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002308 ScopedIndentation indent2(&vios_);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002309 auto* resolved_types = dex_cache->GetResolvedTypes();
2310 for (size_t i = 0; i < num_types; ++i) {
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002311 auto pair = resolved_types[i].load(std::memory_order_relaxed);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002312 size_t run = 0;
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002313 for (size_t j = i + 1; j != num_types; ++j) {
2314 auto other_pair = resolved_types[j].load(std::memory_order_relaxed);
2315 if (pair.index != other_pair.index ||
2316 pair.object.Read() != other_pair.object.Read()) {
2317 break;
2318 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002319 ++run;
2320 }
2321 if (run == 0) {
2322 os << StringPrintf("%zd: ", i);
2323 } else {
2324 os << StringPrintf("%zd to %zd: ", i, i + run);
2325 i = i + run;
2326 }
2327 std::string msg;
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002328 auto* elem = pair.object.Read();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002329 if (elem == nullptr) {
2330 msg = "null";
2331 } else {
David Sehr709b0702016-10-13 09:12:37 -07002332 msg = elem->PrettyClass();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002333 }
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002334 os << StringPrintf("%p %u %s\n", elem, pair.index, msg.c_str());
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002335 }
2336 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002337 }
2338 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002339 std::string temp;
Andreas Gampe0c183382017-07-13 22:26:24 -07002340 stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002341 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002342
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002343 void DumpMethod(ArtMethod* method, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002344 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002345 DCHECK(method != nullptr);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002346 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
2347 const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
Andreas Gampe542451c2016-07-26 09:02:02 -07002348 const PointerSize pointer_size = image_header_.GetPointerSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002349 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2350 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002351 if (method->IsNative()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002352 bool first_occurrence;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002353 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2354 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002355 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002356 stats_.native_to_managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002357 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002358 if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
2359 image_header_.GetPointerSize())) {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002360 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002361 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002362 } else if (method->IsAbstract() || method->IsClassInitializer()) {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002363 // Don't print information for these.
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002364 } else if (method->IsRuntimeMethod()) {
2365 ImtConflictTable* table = method->GetImtConflictTable(image_header_.GetPointerSize());
2366 if (table != nullptr) {
2367 indent_os << "IMT conflict table " << table << " method: ";
2368 for (size_t i = 0, count = table->NumEntries(pointer_size); i < count; ++i) {
David Sehr709b0702016-10-13 09:12:37 -07002369 indent_os << ArtMethod::PrettyMethod(table->GetImplementationMethod(i, pointer_size))
2370 << " ";
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002371 }
2372 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002373 } else {
2374 const DexFile::CodeItem* code_item = method->GetCodeItem();
2375 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002376 stats_.dex_instruction_bytes += dex_instruction_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002377
2378 bool first_occurrence;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002379 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002380 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01002381 // Method compiled with the optimizing compiler have no vmap table.
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002382 vmap_table_bytes = ComputeOatSize(method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002383 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002384 stats_.vmap_table_bytes += vmap_table_bytes;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002385 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002386 }
2387
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002388 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2389 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002390 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002391 stats_.managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002392 if (method->IsConstructor()) {
2393 if (method->IsStatic()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002394 stats_.class_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002395 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002396 stats_.large_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002397 }
2398 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002399 stats_.large_method_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002400 }
2401 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002402 stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002403
Igor Murashkin7617abd2015-07-10 18:27:47 -07002404 uint32_t method_access_flags = method->GetAccessFlags();
2405
Mathieu Chartiere401d142015-04-22 13:56:20 -07002406 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002407 indent_os << StringPrintf("SIZE: Dex Instructions=%zd StackMaps=%zd AccessFlags=0x%x\n",
2408 dex_instruction_bytes,
2409 vmap_table_bytes,
Igor Murashkin7617abd2015-07-10 18:27:47 -07002410 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002411
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002412 size_t total_size = dex_instruction_bytes +
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002413 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002414
2415 double expansion =
2416 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002417 stats_.ComputeOutliers(total_size, expansion, method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002418 }
2419 }
2420
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002421 std::set<const void*> already_seen_;
2422 // Compute the size of the given data within the oat file and whether this is the first time
2423 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002424 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002425 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002426 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002427 already_seen_.insert(oat_data);
2428 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002429 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002430 }
2431 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002432 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002433
2434 public:
2435 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002436 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002437 size_t file_bytes;
2438
2439 size_t header_bytes;
2440 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002441 size_t art_field_bytes;
2442 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01002443 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002444 size_t interned_strings_bytes;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002445 size_t class_table_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07002446 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002447 size_t alignment_bytes;
2448
2449 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002450 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002451 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07002452 size_t class_initializer_code_bytes;
2453 size_t large_initializer_code_bytes;
2454 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002455
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002456 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002457
2458 size_t dex_instruction_bytes;
2459
Mathieu Chartiere401d142015-04-22 13:56:20 -07002460 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002461 std::vector<size_t> method_outlier_size;
2462 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002463 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002464
Roland Levillain3887c462015-08-12 18:15:42 +01002465 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002466 : oat_file_bytes(0),
2467 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002468 header_bytes(0),
2469 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002470 art_field_bytes(0),
2471 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01002472 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002473 interned_strings_bytes(0),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002474 class_table_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07002475 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002476 alignment_bytes(0),
2477 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002478 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002479 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07002480 class_initializer_code_bytes(0),
2481 large_initializer_code_bytes(0),
2482 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002483 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002484 dex_instruction_bytes(0) {}
2485
Elliott Hughesa0e18062012-04-13 15:59:59 -07002486 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002487 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002488 size_t bytes;
2489 size_t count;
2490 };
2491 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2492 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002493
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002494 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002495 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2496 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002497 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002498 it->second.count += 1;
2499 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002500 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002501 }
2502 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002503
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002504 double PercentOfOatBytes(size_t size) {
2505 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2506 }
2507
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002508 double PercentOfFileBytes(size_t size) {
2509 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2510 }
2511
2512 double PercentOfObjectBytes(size_t size) {
2513 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2514 }
2515
Mathieu Chartiere401d142015-04-22 13:56:20 -07002516 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002517 method_outlier_size.push_back(total_size);
2518 method_outlier_expansion.push_back(expansion);
2519 method_outlier.push_back(method);
2520 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002521
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002522 void DumpOutliers(std::ostream& os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002523 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002524 size_t sum_of_sizes = 0;
2525 size_t sum_of_sizes_squared = 0;
2526 size_t sum_of_expansion = 0;
2527 size_t sum_of_expansion_squared = 0;
2528 size_t n = method_outlier_size.size();
Mathieu Chartier1ebf8d32016-06-09 11:51:27 -07002529 if (n <= 1) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08002530 return;
2531 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002532 for (size_t i = 0; i < n; i++) {
2533 size_t cur_size = method_outlier_size[i];
2534 sum_of_sizes += cur_size;
2535 sum_of_sizes_squared += cur_size * cur_size;
2536 double cur_expansion = method_outlier_expansion[i];
2537 sum_of_expansion += cur_expansion;
2538 sum_of_expansion_squared += cur_expansion * cur_expansion;
2539 }
2540 size_t size_mean = sum_of_sizes / n;
2541 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2542 double expansion_mean = sum_of_expansion / n;
2543 double expansion_variance =
2544 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2545
2546 // Dump methods whose size is a certain number of standard deviations from the mean
2547 size_t dumped_values = 0;
2548 size_t skipped_values = 0;
2549 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2550 size_t cur_size_variance = i * i * size_variance;
2551 bool first = true;
2552 for (size_t j = 0; j < n; j++) {
2553 size_t cur_size = method_outlier_size[j];
2554 if (cur_size > size_mean) {
2555 size_t cur_var = cur_size - size_mean;
2556 cur_var = cur_var * cur_var;
2557 if (cur_var > cur_size_variance) {
2558 if (dumped_values > 20) {
2559 if (i == 1) {
2560 skipped_values++;
2561 } else {
2562 i = 2; // jump to counting for 1 standard deviation
2563 break;
2564 }
2565 } else {
2566 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002567 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002568 first = false;
2569 }
David Sehr709b0702016-10-13 09:12:37 -07002570 os << ArtMethod::PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002571 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002572 method_outlier_size[j] = 0; // don't consider this method again
2573 dumped_values++;
2574 }
2575 }
2576 }
2577 }
2578 }
2579 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002580 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002581 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002582 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002583 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002584
2585 // Dump methods whose expansion is a certain number of standard deviations from the mean
2586 dumped_values = 0;
2587 skipped_values = 0;
2588 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2589 double cur_expansion_variance = i * i * expansion_variance;
2590 bool first = true;
2591 for (size_t j = 0; j < n; j++) {
2592 double cur_expansion = method_outlier_expansion[j];
2593 if (cur_expansion > expansion_mean) {
2594 size_t cur_var = cur_expansion - expansion_mean;
2595 cur_var = cur_var * cur_var;
2596 if (cur_var > cur_expansion_variance) {
2597 if (dumped_values > 20) {
2598 if (i == 1) {
2599 skipped_values++;
2600 } else {
2601 i = 2; // jump to counting for 1 standard deviation
2602 break;
2603 }
2604 } else {
2605 if (first) {
2606 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002607 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002608 first = false;
2609 }
David Sehr709b0702016-10-13 09:12:37 -07002610 os << ArtMethod::PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002611 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002612 method_outlier_expansion[j] = 0.0; // don't consider this method again
2613 dumped_values++;
2614 }
2615 }
2616 }
2617 }
2618 }
2619 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002620 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002621 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002622 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002623 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002624 }
2625
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002626 void Dump(std::ostream& os, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002627 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002628 {
2629 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2630 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002631 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2632 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2633 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2634 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2635 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2636 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002637 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002638 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2639 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002640 header_bytes, PercentOfFileBytes(header_bytes),
2641 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002642 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2643 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002644 dex_cache_arrays_bytes,
2645 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002646 interned_strings_bytes,
2647 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002648 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002649 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002650 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2651 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002652 CHECK_EQ(file_bytes,
2653 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2654 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2655 bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002656 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002657
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002658 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002659 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002660 for (const auto& sizes_and_count : sizes_and_counts) {
2661 const std::string& descriptor(sizes_and_count.first);
2662 double average = static_cast<double>(sizes_and_count.second.bytes) /
2663 static_cast<double>(sizes_and_count.second.count);
2664 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002665 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002666 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002667 descriptor.c_str(), sizes_and_count.second.bytes,
2668 sizes_and_count.second.count, average, percent);
2669 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002670 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002671 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002672 CHECK_EQ(object_bytes, object_bytes_total);
2673
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002674 os << StringPrintf("oat_file_bytes = %8zd\n"
2675 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002676 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2677 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2678 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2679 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002680 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002681 managed_code_bytes,
2682 PercentOfOatBytes(managed_code_bytes),
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002683 native_to_managed_code_bytes,
2684 PercentOfOatBytes(native_to_managed_code_bytes),
2685 class_initializer_code_bytes,
2686 PercentOfOatBytes(class_initializer_code_bytes),
2687 large_initializer_code_bytes,
2688 PercentOfOatBytes(large_initializer_code_bytes),
2689 large_method_code_bytes,
2690 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002691 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002692 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002693 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002694 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2695 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002696 }
2697
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002698 os << "\n" << StringPrintf("vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002699 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002700 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002701
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002702 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2703 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002704 static_cast<double>(managed_code_bytes) /
2705 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002706 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002707 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002708 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002709
2710 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002711 }
2712 } stats_;
2713
2714 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002715 enum {
2716 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2717 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2718 kLargeConstructorDexBytes = 4000,
2719 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2720 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2721 kLargeMethodDexBytes = 16000
2722 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002723
2724 // For performance, use the *os_ directly for anything that doesn't need indentation
2725 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002726 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002727 VariableIndentationOutputStream vios_;
2728 ScopedIndentation indent1_;
2729
Ian Rogers1d54e732013-05-02 21:10:01 -07002730 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002731 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002732 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002733 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002734 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002735
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002736 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002737};
2738
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002739static int DumpImage(gc::space::ImageSpace* image_space,
2740 OatDumperOptions* options,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002741 std::ostream* os) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002742 const ImageHeader& image_header = image_space->GetImageHeader();
2743 if (!image_header.IsValid()) {
2744 fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
2745 return EXIT_FAILURE;
2746 }
2747 ImageDumper image_dumper(os, *image_space, image_header, options);
2748 if (!image_dumper.Dump()) {
2749 return EXIT_FAILURE;
2750 }
2751 return EXIT_SUCCESS;
2752}
2753
2754static int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002755 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002756 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002757 options->class_loader_ = &null_class_loader;
2758
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002759 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002760 if (options->app_image_ != nullptr) {
2761 if (options->app_oat_ == nullptr) {
2762 LOG(ERROR) << "Can not dump app image without app oat file";
Jeff Haodcdc85b2015-12-04 14:06:18 -08002763 return EXIT_FAILURE;
2764 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002765 // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
2766 // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
2767 // pointers into 32 bit pointer sized ArtMethods.
2768 std::string error_msg;
2769 std::unique_ptr<OatFile> oat_file(OatFile::Open(options->app_oat_,
2770 options->app_oat_,
2771 nullptr,
2772 nullptr,
2773 false,
2774 /*low_4gb*/true,
2775 nullptr,
2776 &error_msg));
2777 if (oat_file == nullptr) {
2778 LOG(ERROR) << "Failed to open oat file " << options->app_oat_ << " with error " << error_msg;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002779 return EXIT_FAILURE;
2780 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002781 std::unique_ptr<gc::space::ImageSpace> space(
2782 gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg));
2783 if (space == nullptr) {
2784 LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error "
2785 << error_msg;
2786 }
2787 // Open dex files for the image.
2788 std::vector<std::unique_ptr<const DexFile>> dex_files;
2789 if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
2790 LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
2791 << error_msg;
2792 }
2793 // Dump the actual image.
2794 int result = DumpImage(space.get(), options, os);
2795 if (result != EXIT_SUCCESS) {
2796 return result;
2797 }
2798 // Fall through to dump the boot images.
2799 }
2800
2801 gc::Heap* heap = runtime->GetHeap();
2802 CHECK(heap->HasBootImageSpace()) << "No image spaces";
2803 for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
2804 int result = DumpImage(image_space, options, os);
2805 if (result != EXIT_SUCCESS) {
2806 return result;
2807 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002808 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002809 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002810}
2811
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002812static jobject InstallOatFile(Runtime* runtime,
2813 std::unique_ptr<OatFile> oat_file,
2814 std::vector<const DexFile*>* class_path)
2815 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002816 Thread* self = Thread::Current();
2817 CHECK(self != nullptr);
2818 // Need well-known-classes.
2819 WellKnownClasses::Init(self->GetJniEnv());
2820
2821 // Need to register dex files to get a working dex cache.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002822 OatFile* oat_file_ptr = oat_file.get();
Andreas Gampe00b25f32014-09-17 21:49:05 -07002823 ClassLinker* class_linker = runtime->GetClassLinker();
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002824 runtime->GetOatFileManager().RegisterOatFile(std::move(oat_file));
2825 for (const OatFile::OatDexFile* odf : oat_file_ptr->GetOatDexFiles()) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002826 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002827 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002828 CHECK(dex_file != nullptr) << error_msg;
Vladimir Markocd556b02017-02-03 11:47:34 +00002829 ObjPtr<mirror::DexCache> dex_cache =
2830 class_linker->RegisterDexFile(*dex_file, nullptr);
2831 CHECK(dex_cache != nullptr);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002832 class_path->push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002833 }
2834
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002835 // Need a class loader. Fake that we're a compiler.
2836 // Note: this will run initializers through the unstarted runtime, so make sure it's
2837 // initialized.
2838 interpreter::UnstartedRuntime::Initialize();
2839
2840 jobject class_loader = class_linker->CreatePathClassLoader(self, *class_path);
2841
2842 return class_loader;
2843}
2844
2845static int DumpOatWithRuntime(Runtime* runtime,
2846 std::unique_ptr<OatFile> oat_file,
2847 OatDumperOptions* options,
2848 std::ostream* os) {
2849 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2850 ScopedObjectAccess soa(Thread::Current());
2851
2852 OatFile* oat_file_ptr = oat_file.get();
2853 std::vector<const DexFile*> class_path;
2854 jobject class_loader = InstallOatFile(runtime, std::move(oat_file), &class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002855
2856 // Use the class loader while dumping.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002857 StackHandleScope<1> scope(soa.Self());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002858 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07002859 soa.Decode<mirror::ClassLoader>(class_loader));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002860 options->class_loader_ = &loader_handle;
2861
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002862 OatDumper oat_dumper(*oat_file_ptr, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002863 bool success = oat_dumper.Dump(*os);
2864 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2865}
2866
2867static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002868 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002869 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002870 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002871 options->class_loader_ = &null_class_loader;
2872
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002873 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002874 bool success = oat_dumper.Dump(*os);
2875 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2876}
2877
2878static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2879 std::ostream* os) {
2880 std::string error_msg;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002881 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
2882 oat_filename,
2883 nullptr,
2884 nullptr,
2885 false,
2886 /*low_4gb*/false,
2887 nullptr,
2888 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002889 if (oat_file == nullptr) {
2890 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2891 return EXIT_FAILURE;
2892 }
2893
2894 if (runtime != nullptr) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002895 return DumpOatWithRuntime(runtime, std::move(oat_file), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002896 } else {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002897 return DumpOatWithoutRuntime(oat_file.get(), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002898 }
2899}
2900
David Srbecky2fdd03c2016-03-10 15:32:37 +00002901static int SymbolizeOat(const char* oat_filename, std::string& output_name, bool no_bits) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002902 std::string error_msg;
Andreas Gampe08c277c2017-04-26 22:22:15 -07002903 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
2904 oat_filename,
2905 nullptr,
2906 nullptr,
2907 false,
2908 /*low_4gb*/false,
2909 nullptr,
2910 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002911 if (oat_file == nullptr) {
2912 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2913 return EXIT_FAILURE;
2914 }
2915
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002916 bool result;
2917 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
2918 // files for 64-bit code in the past.
2919 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
Andreas Gampe08c277c2017-04-26 22:22:15 -07002920 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file.get(), output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002921 result = oat_symbolizer.Symbolize();
2922 } else {
Andreas Gampe08c277c2017-04-26 22:22:15 -07002923 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file.get(), output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002924 result = oat_symbolizer.Symbolize();
2925 }
2926 if (!result) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002927 fprintf(stderr, "Failed to symbolize\n");
2928 return EXIT_FAILURE;
2929 }
2930
2931 return EXIT_SUCCESS;
2932}
2933
Andreas Gampe9fded872016-09-25 16:08:35 -07002934class IMTDumper {
2935 public:
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002936 static bool Dump(Runtime* runtime,
2937 const std::string& imt_file,
2938 bool dump_imt_stats,
2939 const char* oat_filename) {
2940 Thread* self = Thread::Current();
2941
2942 ScopedObjectAccess soa(self);
2943 StackHandleScope<1> scope(self);
2944 MutableHandle<mirror::ClassLoader> class_loader = scope.NewHandle<mirror::ClassLoader>(nullptr);
2945 std::vector<const DexFile*> class_path;
2946
2947 if (oat_filename != nullptr) {
2948 std::string error_msg;
2949 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
2950 oat_filename,
2951 nullptr,
2952 nullptr,
2953 false,
2954 /*low_4gb*/false,
2955 nullptr,
2956 &error_msg));
2957 if (oat_file == nullptr) {
2958 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2959 return false;
2960 }
2961
2962 class_loader.Assign(soa.Decode<mirror::ClassLoader>(
2963 InstallOatFile(runtime, std::move(oat_file), &class_path)));
2964 } else {
2965 class_loader.Assign(nullptr); // Boot classloader. Just here for explicit documentation.
2966 class_path = runtime->GetClassLinker()->GetBootClassPath();
2967 }
2968
2969 if (!imt_file.empty()) {
2970 return DumpImt(runtime, imt_file, class_loader);
2971 }
2972
2973 if (dump_imt_stats) {
2974 return DumpImtStats(runtime, class_path, class_loader);
2975 }
2976
2977 LOG(FATAL) << "Should not reach here";
2978 UNREACHABLE();
2979 }
2980
2981 private:
2982 static bool DumpImt(Runtime* runtime,
2983 const std::string& imt_file,
2984 Handle<mirror::ClassLoader> h_class_loader)
2985 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002986 std::vector<std::string> lines = ReadCommentedInputFromFile(imt_file);
2987 std::unordered_set<std::string> prepared;
2988
2989 for (const std::string& line : lines) {
2990 // A line should be either a class descriptor, in which case we will dump the complete IMT,
2991 // or a class descriptor and an interface method, in which case we will lookup the method,
2992 // determine its IMT slot, and check the class' IMT.
2993 size_t first_space = line.find(' ');
2994 if (first_space == std::string::npos) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002995 DumpIMTForClass(runtime, line, h_class_loader, &prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07002996 } else {
2997 DumpIMTForMethod(runtime,
2998 line.substr(0, first_space),
2999 line.substr(first_space + 1, std::string::npos),
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003000 h_class_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003001 &prepared);
3002 }
3003 std::cerr << std::endl;
3004 }
3005
3006 return true;
3007 }
3008
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003009 static bool DumpImtStats(Runtime* runtime,
3010 const std::vector<const DexFile*>& dex_files,
3011 Handle<mirror::ClassLoader> h_class_loader)
3012 REQUIRES_SHARED(Locks::mutator_lock_) {
3013 size_t without_imt = 0;
3014 size_t with_imt = 0;
Andreas Gampe9fded872016-09-25 16:08:35 -07003015 std::map<size_t, size_t> histogram;
3016
3017 ClassLinker* class_linker = runtime->GetClassLinker();
3018 const PointerSize pointer_size = class_linker->GetImagePointerSize();
3019 std::unordered_set<std::string> prepared;
3020
3021 Thread* self = Thread::Current();
Andreas Gampe9fded872016-09-25 16:08:35 -07003022 StackHandleScope<1> scope(self);
3023 MutableHandle<mirror::Class> h_klass(scope.NewHandle<mirror::Class>(nullptr));
3024
3025 for (const DexFile* dex_file : dex_files) {
3026 for (uint32_t class_def_index = 0;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003027 class_def_index != dex_file->NumClassDefs();
3028 ++class_def_index) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003029 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
3030 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003031 h_klass.Assign(class_linker->FindClass(self, descriptor, h_class_loader));
Andreas Gampefa4333d2017-02-14 11:10:34 -08003032 if (h_klass == nullptr) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003033 std::cerr << "Warning: could not load " << descriptor << std::endl;
3034 continue;
3035 }
3036
3037 if (HasNoIMT(runtime, h_klass, pointer_size, &prepared)) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003038 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003039 continue;
3040 }
3041
3042 ImTable* im_table = PrepareAndGetImTable(runtime, h_klass, pointer_size, &prepared);
3043 if (im_table == nullptr) {
3044 // Should not happen, but accept.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003045 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003046 continue;
3047 }
3048
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003049 with_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003050 for (size_t imt_index = 0; imt_index != ImTable::kSize; ++imt_index) {
3051 ArtMethod* ptr = im_table->Get(imt_index, pointer_size);
3052 if (ptr->IsRuntimeMethod()) {
3053 if (ptr->IsImtUnimplementedMethod()) {
3054 histogram[0]++;
3055 } else {
3056 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3057 histogram[current_table->NumEntries(pointer_size)]++;
3058 }
3059 } else {
3060 histogram[1]++;
3061 }
3062 }
3063 }
3064 }
3065
3066 std::cerr << "IMT stats:"
3067 << std::endl << std::endl;
3068
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003069 std::cerr << " " << with_imt << " classes with IMT."
Andreas Gampe9fded872016-09-25 16:08:35 -07003070 << std::endl << std::endl;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003071 std::cerr << " " << without_imt << " classes without IMT (or copy from Object)."
Andreas Gampe9fded872016-09-25 16:08:35 -07003072 << std::endl << std::endl;
3073
3074 double sum_one = 0;
3075 size_t count_one = 0;
3076
3077 std::cerr << " " << "IMT histogram" << std::endl;
3078 for (auto& bucket : histogram) {
3079 std::cerr << " " << bucket.first << " " << bucket.second << std::endl;
3080 if (bucket.first > 0) {
3081 sum_one += bucket.second * bucket.first;
3082 count_one += bucket.second;
3083 }
3084 }
3085
3086 double count_zero = count_one + histogram[0];
3087 std::cerr << " Stats:" << std::endl;
3088 std::cerr << " Average depth (including empty): " << (sum_one / count_zero) << std::endl;
3089 std::cerr << " Average depth (excluding empty): " << (sum_one / count_one) << std::endl;
3090
3091 return true;
3092 }
3093
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003094 // Return whether the given class has no IMT (or the one shared with java.lang.Object).
Andreas Gampe9fded872016-09-25 16:08:35 -07003095 static bool HasNoIMT(Runtime* runtime,
3096 Handle<mirror::Class> klass,
3097 const PointerSize pointer_size,
3098 std::unordered_set<std::string>* prepared)
3099 REQUIRES_SHARED(Locks::mutator_lock_) {
3100 if (klass->IsObjectClass() || !klass->ShouldHaveImt()) {
3101 return true;
3102 }
3103
3104 if (klass->GetImt(pointer_size) == nullptr) {
3105 PrepareClass(runtime, klass, prepared);
3106 }
3107
3108 mirror::Class* object_class = mirror::Class::GetJavaLangClass()->GetSuperClass();
3109 DCHECK(object_class->IsObjectClass());
3110
3111 bool result = klass->GetImt(pointer_size) == object_class->GetImt(pointer_size);
3112
Mathieu Chartier6beced42016-11-15 15:51:31 -08003113 if (klass->GetIfTable()->Count() == 0) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003114 DCHECK(result);
3115 }
3116
3117 return result;
3118 }
3119
3120 static void PrintTable(ImtConflictTable* table, PointerSize pointer_size)
3121 REQUIRES_SHARED(Locks::mutator_lock_) {
3122 if (table == nullptr) {
3123 std::cerr << " <No IMT?>" << std::endl;
3124 return;
3125 }
3126 size_t table_index = 0;
3127 for (;;) {
3128 ArtMethod* ptr = table->GetInterfaceMethod(table_index, pointer_size);
3129 if (ptr == nullptr) {
3130 return;
3131 }
3132 table_index++;
David Sehr709b0702016-10-13 09:12:37 -07003133 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003134 }
3135 }
3136
3137 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3138 Thread* self,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003139 Handle<mirror::ClassLoader> h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003140 const std::string& class_name,
3141 const PointerSize pointer_size,
3142 mirror::Class** klass_out,
3143 std::unordered_set<std::string>* prepared)
3144 REQUIRES_SHARED(Locks::mutator_lock_) {
3145 if (class_name.empty()) {
3146 return nullptr;
3147 }
3148
3149 std::string descriptor;
3150 if (class_name[0] == 'L') {
3151 descriptor = class_name;
3152 } else {
3153 descriptor = DotToDescriptor(class_name.c_str());
3154 }
3155
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003156 mirror::Class* klass = runtime->GetClassLinker()->FindClass(self, descriptor.c_str(), h_loader);
Andreas Gampe9fded872016-09-25 16:08:35 -07003157
3158 if (klass == nullptr) {
3159 self->ClearException();
3160 std::cerr << "Did not find " << class_name << std::endl;
3161 *klass_out = nullptr;
3162 return nullptr;
3163 }
3164
3165 StackHandleScope<1> scope(Thread::Current());
3166 Handle<mirror::Class> h_klass = scope.NewHandle<mirror::Class>(klass);
3167
3168 ImTable* ret = PrepareAndGetImTable(runtime, h_klass, pointer_size, prepared);
3169 *klass_out = h_klass.Get();
3170 return ret;
3171 }
3172
3173 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3174 Handle<mirror::Class> h_klass,
3175 const PointerSize pointer_size,
3176 std::unordered_set<std::string>* prepared)
3177 REQUIRES_SHARED(Locks::mutator_lock_) {
3178 PrepareClass(runtime, h_klass, prepared);
3179 return h_klass->GetImt(pointer_size);
3180 }
3181
3182 static void DumpIMTForClass(Runtime* runtime,
3183 const std::string& class_name,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003184 Handle<mirror::ClassLoader> h_loader,
3185 std::unordered_set<std::string>* prepared)
3186 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003187 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
3188 mirror::Class* klass;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003189 ImTable* imt = PrepareAndGetImTable(runtime,
3190 Thread::Current(),
3191 h_loader,
3192 class_name,
3193 pointer_size,
3194 &klass,
3195 prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07003196 if (imt == nullptr) {
3197 return;
3198 }
3199
3200 std::cerr << class_name << std::endl << " IMT:" << std::endl;
3201 for (size_t index = 0; index < ImTable::kSize; ++index) {
3202 std::cerr << " " << index << ":" << std::endl;
3203 ArtMethod* ptr = imt->Get(index, pointer_size);
3204 if (ptr->IsRuntimeMethod()) {
3205 if (ptr->IsImtUnimplementedMethod()) {
3206 std::cerr << " <empty>" << std::endl;
3207 } else {
3208 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3209 PrintTable(current_table, pointer_size);
3210 }
3211 } else {
David Sehr709b0702016-10-13 09:12:37 -07003212 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003213 }
3214 }
3215
3216 std::cerr << " Interfaces:" << std::endl;
3217 // Run through iftable, find methods that slot here, see if they fit.
3218 mirror::IfTable* if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003219 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
3220 mirror::Class* iface = if_table->GetInterface(i);
3221 std::string iface_name;
3222 std::cerr << " " << iface->GetDescriptor(&iface_name) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003223
Mathieu Chartier6beced42016-11-15 15:51:31 -08003224 for (ArtMethod& iface_method : iface->GetVirtualMethods(pointer_size)) {
3225 uint32_t class_hash, name_hash, signature_hash;
3226 ImTable::GetImtHashComponents(&iface_method, &class_hash, &name_hash, &signature_hash);
3227 uint32_t imt_slot = ImTable::GetImtIndex(&iface_method);
3228 std::cerr << " " << iface_method.PrettyMethod(true)
3229 << " slot=" << imt_slot
3230 << std::hex
3231 << " class_hash=0x" << class_hash
3232 << " name_hash=0x" << name_hash
3233 << " signature_hash=0x" << signature_hash
3234 << std::dec
3235 << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003236 }
3237 }
3238 }
3239
3240 static void DumpIMTForMethod(Runtime* runtime,
3241 const std::string& class_name,
3242 const std::string& method,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003243 Handle<mirror::ClassLoader> h_loader,
3244 std::unordered_set<std::string>* prepared)
3245 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003246 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
3247 mirror::Class* klass;
3248 ImTable* imt = PrepareAndGetImTable(runtime,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003249 Thread::Current(),
3250 h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003251 class_name,
3252 pointer_size,
3253 &klass,
3254 prepared);
3255 if (imt == nullptr) {
3256 return;
3257 }
3258
3259 std::cerr << class_name << " <" << method << ">" << std::endl;
3260 for (size_t index = 0; index < ImTable::kSize; ++index) {
3261 ArtMethod* ptr = imt->Get(index, pointer_size);
3262 if (ptr->IsRuntimeMethod()) {
3263 if (ptr->IsImtUnimplementedMethod()) {
3264 continue;
3265 }
3266
3267 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3268 if (current_table == nullptr) {
3269 continue;
3270 }
3271
3272 size_t table_index = 0;
3273 for (;;) {
3274 ArtMethod* ptr2 = current_table->GetInterfaceMethod(table_index, pointer_size);
3275 if (ptr2 == nullptr) {
3276 break;
3277 }
3278 table_index++;
3279
David Sehr709b0702016-10-13 09:12:37 -07003280 std::string p_name = ptr2->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003281 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003282 std::cerr << " Slot "
3283 << index
3284 << " ("
3285 << current_table->NumEntries(pointer_size)
3286 << ")"
3287 << std::endl;
3288 PrintTable(current_table, pointer_size);
3289 return;
3290 }
3291 }
3292 } else {
David Sehr709b0702016-10-13 09:12:37 -07003293 std::string p_name = ptr->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003294 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003295 std::cerr << " Slot " << index << " (1)" << std::endl;
3296 std::cerr << " " << p_name << std::endl;
3297 } else {
3298 // Run through iftable, find methods that slot here, see if they fit.
3299 mirror::IfTable* if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003300 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
3301 mirror::Class* iface = if_table->GetInterface(i);
3302 size_t num_methods = iface->NumDeclaredVirtualMethods();
3303 if (num_methods > 0) {
3304 for (ArtMethod& iface_method : iface->GetMethods(pointer_size)) {
3305 if (ImTable::GetImtIndex(&iface_method) == index) {
3306 std::string i_name = iface_method.PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003307 if (android::base::StartsWith(i_name, method.c_str())) {
Mathieu Chartier6beced42016-11-15 15:51:31 -08003308 std::cerr << " Slot " << index << " (1)" << std::endl;
3309 std::cerr << " " << p_name << " (" << i_name << ")" << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003310 }
3311 }
3312 }
3313 }
3314 }
3315 }
3316 }
3317 }
3318 }
3319
3320 // Read lines from the given stream, dropping comments and empty lines
3321 static std::vector<std::string> ReadCommentedInputStream(std::istream& in_stream) {
3322 std::vector<std::string> output;
3323 while (in_stream.good()) {
3324 std::string dot;
3325 std::getline(in_stream, dot);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003326 if (android::base::StartsWith(dot, "#") || dot.empty()) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003327 continue;
3328 }
3329 output.push_back(dot);
3330 }
3331 return output;
3332 }
3333
3334 // Read lines from the given file, dropping comments and empty lines.
3335 static std::vector<std::string> ReadCommentedInputFromFile(const std::string& input_filename) {
3336 std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
3337 if (input_file.get() == nullptr) {
3338 LOG(ERROR) << "Failed to open input file " << input_filename;
3339 return std::vector<std::string>();
3340 }
3341 std::vector<std::string> result = ReadCommentedInputStream(*input_file);
3342 input_file->close();
3343 return result;
3344 }
3345
3346 // Prepare a class, i.e., ensure it has a filled IMT. Will do so recursively for superclasses,
3347 // and note in the given set that the work was done.
3348 static void PrepareClass(Runtime* runtime,
3349 Handle<mirror::Class> h_klass,
3350 std::unordered_set<std::string>* done)
3351 REQUIRES_SHARED(Locks::mutator_lock_) {
3352 if (!h_klass->ShouldHaveImt()) {
3353 return;
3354 }
3355
3356 std::string name;
3357 name = h_klass->GetDescriptor(&name);
3358
3359 if (done->find(name) != done->end()) {
3360 return;
3361 }
3362 done->insert(name);
3363
3364 if (h_klass->HasSuperClass()) {
3365 StackHandleScope<1> h(Thread::Current());
3366 PrepareClass(runtime, h.NewHandle<mirror::Class>(h_klass->GetSuperClass()), done);
3367 }
3368
3369 if (!h_klass->IsTemp()) {
3370 runtime->GetClassLinker()->FillIMTAndConflictTables(h_klass.Get());
3371 }
3372 }
3373};
3374
Igor Murashkin37743352014-11-13 14:38:00 -08003375struct OatdumpArgs : public CmdlineArgs {
3376 protected:
3377 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003378
Igor Murashkin37743352014-11-13 14:38:00 -08003379 virtual ParseStatus ParseCustom(const StringPiece& option,
3380 std::string* error_msg) OVERRIDE {
3381 {
3382 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
3383 if (base_parse != kParseUnknownArgument) {
3384 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003385 }
3386 }
3387
Igor Murashkin37743352014-11-13 14:38:00 -08003388 if (option.starts_with("--oat-file=")) {
3389 oat_filename_ = option.substr(strlen("--oat-file=")).data();
3390 } else if (option.starts_with("--image=")) {
3391 image_location_ = option.substr(strlen("--image=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003392 } else if (option == "--no-dump:vmap") {
3393 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01003394 } else if (option =="--dump:code_info_stack_maps") {
3395 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003396 } else if (option == "--no-disassemble") {
3397 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003398 } else if (option =="--header-only") {
3399 dump_header_only_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003400 } else if (option.starts_with("--symbolize=")) {
3401 oat_filename_ = option.substr(strlen("--symbolize=")).data();
3402 symbolize_ = true;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003403 } else if (option.starts_with("--only-keep-debug")) {
3404 only_keep_debug_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003405 } else if (option.starts_with("--class-filter=")) {
3406 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003407 } else if (option.starts_with("--method-filter=")) {
3408 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003409 } else if (option.starts_with("--list-classes")) {
3410 list_classes_ = true;
3411 } else if (option.starts_with("--list-methods")) {
3412 list_methods_ = true;
3413 } else if (option.starts_with("--export-dex-to=")) {
3414 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
3415 } else if (option.starts_with("--addr2instr=")) {
3416 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
3417 *error_msg = "Address conversion failed";
3418 return kParseError;
3419 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003420 } else if (option.starts_with("--app-image=")) {
3421 app_image_ = option.substr(strlen("--app-image=")).data();
3422 } else if (option.starts_with("--app-oat=")) {
3423 app_oat_ = option.substr(strlen("--app-oat=")).data();
Andreas Gampe9fded872016-09-25 16:08:35 -07003424 } else if (option.starts_with("--dump-imt=")) {
3425 imt_dump_ = option.substr(strlen("--dump-imt=")).data();
3426 } else if (option == "--dump-imt-stats") {
3427 imt_stat_dump_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003428 } else {
3429 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003430 }
3431
Igor Murashkin37743352014-11-13 14:38:00 -08003432 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003433 }
3434
Igor Murashkin37743352014-11-13 14:38:00 -08003435 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
3436 // Infer boot image location from the image location if possible.
3437 if (boot_image_location_ == nullptr) {
3438 boot_image_location_ = image_location_;
3439 }
3440
3441 // Perform the parent checks.
3442 ParseStatus parent_checks = Base::ParseChecks(error_msg);
3443 if (parent_checks != kParseOk) {
3444 return parent_checks;
3445 }
3446
3447 // Perform our own checks.
3448 if (image_location_ == nullptr && oat_filename_ == nullptr) {
3449 *error_msg = "Either --image or --oat-file must be specified";
3450 return kParseError;
3451 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
3452 *error_msg = "Either --image or --oat-file must be specified but not both";
3453 return kParseError;
3454 }
3455
3456 return kParseOk;
3457 }
3458
3459 virtual std::string GetUsage() const {
3460 std::string usage;
3461
3462 usage +=
3463 "Usage: oatdump [options] ...\n"
3464 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
3465 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
3466 "\n"
3467 // Either oat-file or image is required.
3468 " --oat-file=<file.oat>: specifies an input oat filename.\n"
3469 " Example: --oat-file=/system/framework/boot.oat\n"
3470 "\n"
3471 " --image=<file.art>: specifies an input image location.\n"
3472 " Example: --image=/system/framework/boot.art\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003473 "\n"
3474 " --app-image=<file.art>: specifies an input app image. Must also have a specified\n"
Roland Levillain4f1c9e62017-06-28 16:44:30 +01003475 " boot image (with --image) and app oat file (with --app-oat).\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003476 " Example: --app-image=app.art\n"
3477 "\n"
3478 " --app-oat=<file.odex>: specifies an input app oat.\n"
3479 " Example: --app-oat=app.odex\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003480 "\n";
3481
3482 usage += Base::GetUsage();
3483
3484 usage += // Optional.
Igor Murashkin37743352014-11-13 14:38:00 -08003485 " --no-dump:vmap may be used to disable vmap dumping.\n"
3486 " Example: --no-dump:vmap\n"
3487 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01003488 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
3489 " Example: --dump:code_info_stack_maps\n"
3490 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003491 " --no-disassemble may be used to disable disassembly.\n"
3492 " Example: --no-disassemble\n"
3493 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00003494 " --header-only may be used to print only the oat header.\n"
3495 " Example: --header-only\n"
3496 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003497 " --list-classes may be used to list target file classes (can be used with filters).\n"
3498 " Example: --list-classes\n"
3499 " Example: --list-classes --class-filter=com.example.foo\n"
3500 "\n"
3501 " --list-methods may be used to list target file methods (can be used with filters).\n"
3502 " Example: --list-methods\n"
3503 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
3504 "\n"
3505 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
3506 " Example: --symbolize=/system/framework/boot.oat\n"
3507 "\n"
David Srbecky2fdd03c2016-03-10 15:32:37 +00003508 " --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
3509 " .rodata and .text sections are omitted in the output file to save space.\n"
3510 " Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
3511 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003512 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
3513 " Example: --class-filter=com.example.foo\n"
3514 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003515 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
3516 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003517 "\n"
3518 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
3519 " Example: --export-dex-to=/data/local/tmp\n"
3520 "\n"
3521 " --addr2instr=<address>: output matching method disassembled code from relative\n"
3522 " address (e.g. PC from crash dump)\n"
3523 " Example: --addr2instr=0x00001a3b\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003524 "\n"
3525 " --dump-imt=<file.txt>: output IMT collisions (if any) for the given receiver\n"
3526 " types and interface methods in the given file. The file\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003527 " is read line-wise, where each line should either be a class\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003528 " name or descriptor, or a class name/descriptor and a prefix\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003529 " of a complete method name (separated by a whitespace).\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003530 " Example: --dump-imt=imt.txt\n"
3531 "\n"
3532 " --dump-imt-stats: output IMT statistics for the given boot image\n"
3533 " Example: --dump-imt-stats"
Igor Murashkin37743352014-11-13 14:38:00 -08003534 "\n";
3535
3536 return usage;
3537 }
3538
3539 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07003540 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003541 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00003542 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07003543 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003544 std::string elf_filename_prefix_;
Andreas Gampe9fded872016-09-25 16:08:35 -07003545 std::string imt_dump_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003546 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01003547 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003548 bool disassemble_code_ = true;
3549 bool symbolize_ = false;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003550 bool only_keep_debug_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003551 bool list_classes_ = false;
3552 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003553 bool dump_header_only_ = false;
Andreas Gampe9fded872016-09-25 16:08:35 -07003554 bool imt_stat_dump_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003555 uint32_t addr2instr_ = 0;
3556 const char* export_dex_location_ = nullptr;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003557 const char* app_image_ = nullptr;
3558 const char* app_oat_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003559};
3560
Igor Murashkin37743352014-11-13 14:38:00 -08003561struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
3562 virtual bool NeedsRuntime() OVERRIDE {
3563 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003564
Igor Murashkin37743352014-11-13 14:38:00 -08003565 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
3566 bool absolute_addresses = (args_->oat_filename_ == nullptr);
3567
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003568 oat_dumper_options_.reset(new OatDumperOptions(
Igor Murashkin37743352014-11-13 14:38:00 -08003569 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01003570 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08003571 args_->disassemble_code_,
3572 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003573 args_->class_filter_,
3574 args_->method_filter_,
3575 args_->list_classes_,
3576 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00003577 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003578 args_->export_dex_location_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003579 args_->app_image_,
3580 args_->app_oat_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003581 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08003582
Andreas Gampe9fded872016-09-25 16:08:35 -07003583 return (args_->boot_image_location_ != nullptr ||
3584 args_->image_location_ != nullptr ||
3585 !args_->imt_dump_.empty()) &&
Igor Murashkin37743352014-11-13 14:38:00 -08003586 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003587 }
3588
Igor Murashkin37743352014-11-13 14:38:00 -08003589 virtual bool ExecuteWithoutRuntime() OVERRIDE {
3590 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08003591 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003592
Mathieu Chartierd424d082014-10-15 10:31:46 -07003593 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08003594
Andreas Gampec24f3992014-12-17 20:40:11 -08003595 if (args_->symbolize_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00003596 // ELF has special kind of section called SHT_NOBITS which allows us to create
3597 // sections which exist but their data is omitted from the ELF file to save space.
3598 // This is what "strip --only-keep-debug" does when it creates separate ELF file
3599 // with only debug data. We use it in similar way to exclude .rodata and .text.
3600 bool no_bits = args_->only_keep_debug_;
3601 return SymbolizeOat(args_->oat_filename_, args_->output_name_, no_bits) == EXIT_SUCCESS;
Andreas Gampec24f3992014-12-17 20:40:11 -08003602 } else {
3603 return DumpOat(nullptr,
3604 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003605 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08003606 args_->os_) == EXIT_SUCCESS;
3607 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07003608 }
3609
Igor Murashkin37743352014-11-13 14:38:00 -08003610 virtual bool ExecuteWithRuntime(Runtime* runtime) {
3611 CHECK(args_ != nullptr);
3612
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003613 if (!args_->imt_dump_.empty() || args_->imt_stat_dump_) {
3614 return IMTDumper::Dump(runtime,
3615 args_->imt_dump_,
3616 args_->imt_stat_dump_,
3617 args_->oat_filename_);
Andreas Gampe9fded872016-09-25 16:08:35 -07003618 }
3619
Igor Murashkin37743352014-11-13 14:38:00 -08003620 if (args_->oat_filename_ != nullptr) {
3621 return DumpOat(runtime,
3622 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003623 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08003624 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003625 }
Igor Murashkin37743352014-11-13 14:38:00 -08003626
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003627 return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003628 }
3629
Igor Murashkin37743352014-11-13 14:38:00 -08003630 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
3631};
Andreas Gampe00b25f32014-09-17 21:49:05 -07003632
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003633} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07003634
3635int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08003636 art::OatdumpMain main;
3637 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07003638}