blob: d2b1c66d7207eeefaf55017b77129ea952573eed [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 Markof3c52b42017-11-17 17:32:12 +000035#include "base/bit_utils_iterator.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000036#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080037#include "base/unix_file/fd_file.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038#include "class_linker-inl.h"
Steven Morelande431e272017-07-18 16:53:49 -070039#include "class_linker.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010040#include "compiled_method.h"
David Srbecky32210b92017-12-04 14:39:21 +000041#include "debug/debug_info.h"
David Srbecky5d950762016-03-07 20:47:29 +000042#include "debug/elf_debug_writer.h"
43#include "debug/method_debug_info.h"
David Sehr9e734c72018-01-04 17:56:19 -080044#include "dex/code_item_accessors-inl.h"
45#include "dex/dex_file-inl.h"
46#include "dex/dex_instruction-inl.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080047#include "disassembler.h"
Andreas Gampe0c183382017-07-13 22:26:24 -070048#include "gc/accounting/space_bitmap-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070049#include "gc/space/image_space.h"
50#include "gc/space/large_object_space.h"
51#include "gc/space/space-inl.h"
Mathieu Chartier4a26f172016-01-26 14:26:18 -080052#include "image-inl.h"
Andreas Gampe9fded872016-09-25 16:08:35 -070053#include "imtable-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080054#include "indenter.h"
Igor Murashkin86083f72017-10-27 10:59:04 -070055#include "subtype_check.h"
Vladimir Markof3c52b42017-11-17 17:32:12 +000056#include "index_bss_mapping.h"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -070057#include "interpreter/unstarted_runtime.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000058#include "linker/buffered_output_stream.h"
Vladimir Marko74527972016-11-29 15:57:32 +000059#include "linker/elf_builder.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000060#include "linker/file_output_stream.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080061#include "mirror/array-inl.h"
62#include "mirror/class-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010063#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080064#include "mirror/object-inl.h"
65#include "mirror/object_array-inl.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080066#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010067#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070068#include "oat_file_manager.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080069#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070070#include "safe_map.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070071#include "scoped_thread_state_change-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070072#include "stack.h"
Mathieu Chartierdc00f182016-07-14 10:10:44 -070073#include "stack_map.h"
74#include "string_reference.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080075#include "thread_list.h"
Andreas Gampe2ba88952016-04-29 17:52:07 -070076#include "type_lookup_table.h"
Nicolas Geoffray4acefd32016-10-24 13:14:58 +010077#include "vdex_file.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080078#include "verifier/method_verifier.h"
Nicolas Geoffraye70dd562016-10-30 21:03:35 +000079#include "verifier/verifier_deps.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070080#include "well_known_classes.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070081
Igor Murashkin37743352014-11-13 14:38:00 -080082#include <sys/stat.h>
83#include "cmdline.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070084
Igor Murashkin37743352014-11-13 14:38:00 -080085namespace art {
Brian Carlstrom78128a62011-09-15 17:21:19 -070086
Andreas Gampe46ee31b2016-12-14 10:11:49 -080087using android::base::StringPrintf;
88
Mathieu Chartiere401d142015-04-22 13:56:20 -070089const char* image_methods_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -080090 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -070091 "kImtConflictMethod",
Mathieu Chartier2d2621a2014-10-23 16:48:06 -070092 "kImtUnimplementedMethod",
Vladimir Markofd36f1f2016-08-03 18:49:58 +010093 "kSaveAllCalleeSavesMethod",
94 "kSaveRefsOnlyMethod",
95 "kSaveRefsAndArgsMethod",
Vladimir Marko952dbb12016-07-28 12:01:51 +010096 "kSaveEverythingMethod",
Mingyao Yang0a87a652017-04-12 13:43:15 -070097 "kSaveEverythingMethodForClinit",
98 "kSaveEverythingMethodForSuspendCheck",
Mathieu Chartiere401d142015-04-22 13:56:20 -070099};
100
101const char* image_roots_descriptions_[] = {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700102 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700103 "kClassRoots",
Vladimir Markoeca3eda2016-11-09 16:26:44 +0000104 "kClassLoader",
Brian Carlstrom78128a62011-09-15 17:21:19 -0700105};
106
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700107// Map is so that we don't allocate multiple dex files for the same OatDexFile.
108static std::map<const OatFile::OatDexFile*,
109 std::unique_ptr<const DexFile>> opened_dex_files;
110
111const DexFile* OpenDexFile(const OatFile::OatDexFile* oat_dex_file, std::string* error_msg) {
112 DCHECK(oat_dex_file != nullptr);
113 auto it = opened_dex_files.find(oat_dex_file);
114 if (it != opened_dex_files.end()) {
115 return it->second.get();
116 }
117 const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
118 opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
119 return ret;
120}
121
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800122template <typename ElfTypes>
David Srbeckybc90fd02015-04-22 19:40:27 +0100123class OatSymbolizer FINAL {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700124 public:
David Srbecky2fdd03c2016-03-10 15:32:37 +0000125 OatSymbolizer(const OatFile* oat_file, const std::string& output_name, bool no_bits) :
126 oat_file_(oat_file),
127 builder_(nullptr),
128 output_name_(output_name.empty() ? "symbolized.oat" : output_name),
129 no_bits_(no_bits) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700130 }
131
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700132 bool Symbolize() {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000133 const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800134 std::unique_ptr<const InstructionSetFeatures> features = InstructionSetFeatures::FromBitmap(
David Srbecky5d811202016-03-08 13:21:22 +0000135 isa, oat_file_->GetOatHeader().GetInstructionSetFeaturesBitmap());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000136
Andreas Gampe08c277c2017-04-26 22:22:15 -0700137 std::unique_ptr<File> elf_file(OS::CreateEmptyFile(output_name_.c_str()));
138 if (elf_file == nullptr) {
139 return false;
140 }
Vladimir Marko74527972016-11-29 15:57:32 +0000141 std::unique_ptr<linker::BufferedOutputStream> output_stream =
142 std::make_unique<linker::BufferedOutputStream>(
143 std::make_unique<linker::FileOutputStream>(elf_file.get()));
144 builder_.reset(new linker::ElfBuilder<ElfTypes>(isa, features.get(), output_stream.get()));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000145
146 builder_->Start();
147
148 auto* rodata = builder_->GetRoData();
149 auto* text = builder_->GetText();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000150
David Srbecky6d8c8f02015-10-26 10:57:09 +0000151 const uint8_t* rodata_begin = oat_file_->Begin();
152 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
David Srbeckye155f4b2017-12-06 15:18:38 +0000153 if (!no_bits_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +0000154 rodata->Start();
155 rodata->WriteFully(rodata_begin, rodata_size);
156 rodata->End();
157 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000158
David Srbecky6d8c8f02015-10-26 10:57:09 +0000159 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
160 const size_t text_size = oat_file_->End() - text_begin;
David Srbeckye155f4b2017-12-06 15:18:38 +0000161 if (!no_bits_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +0000162 text->Start();
163 text->WriteFully(text_begin, text_size);
164 text->End();
165 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000166
Vladimir Marko33bff252017-11-01 14:35:42 +0000167 if (isa == InstructionSet::kMips || isa == InstructionSet::kMips64) {
Douglas Leung316a2182015-09-17 15:26:25 -0700168 builder_->WriteMIPSabiflagsSection();
169 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000170 builder_->PrepareDynamicSection(elf_file->GetPath(),
171 rodata_size,
172 text_size,
173 oat_file_->BssSize(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100174 oat_file_->BssMethodsOffset(),
David Srbeckyec2cdf42017-12-08 16:21:25 +0000175 oat_file_->BssRootsOffset(),
176 oat_file_->VdexSize());
Vladimir Marko944da602016-02-19 12:27:55 +0000177 builder_->WriteDynamicSection();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700178
Vladimir Marko1b404a82017-09-01 13:35:26 +0100179 const OatHeader& oat_header = oat_file_->GetOatHeader();
180 #define DO_TRAMPOLINE(fn_name) \
181 if (oat_header.Get ## fn_name ## Offset() != 0) { \
182 debug::MethodDebugInfo info = {}; \
183 info.trampoline_name = #fn_name; \
184 info.isa = oat_header.GetInstructionSet(); \
185 info.is_code_address_text_relative = true; \
186 size_t code_offset = oat_header.Get ## fn_name ## Offset(); \
187 code_offset -= CompiledCode::CodeDelta(oat_header.GetInstructionSet()); \
188 info.code_address = code_offset - oat_header.GetExecutableOffset(); \
189 info.code_size = 0; /* The symbol lasts until the next symbol. */ \
190 method_debug_infos_.push_back(std::move(info)); \
191 }
192 DO_TRAMPOLINE(InterpreterToInterpreterBridge)
193 DO_TRAMPOLINE(InterpreterToCompiledCodeBridge)
194 DO_TRAMPOLINE(JniDlsymLookup);
195 DO_TRAMPOLINE(QuickGenericJniTrampoline);
196 DO_TRAMPOLINE(QuickImtConflictTrampoline);
197 DO_TRAMPOLINE(QuickResolutionTrampoline);
198 DO_TRAMPOLINE(QuickToInterpreterBridge);
199 #undef DO_TRAMPOLINE
200
David Srbecky5d950762016-03-07 20:47:29 +0000201 Walk();
Vladimir Marko1b404a82017-09-01 13:35:26 +0100202
203 // TODO: Try to symbolize link-time thunks?
204 // This would require disassembling all methods to find branches outside the method code.
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700205
David Srbecky32210b92017-12-04 14:39:21 +0000206 // TODO: Add symbols for dex bytecode in the .dex section.
207
208 debug::DebugInfo debug_info{};
209 debug_info.compiled_methods = ArrayRef<const debug::MethodDebugInfo>(method_debug_infos_);
210
David Srbecky5d950762016-03-07 20:47:29 +0000211 debug::WriteDebugInfo(builder_.get(),
David Srbecky32210b92017-12-04 14:39:21 +0000212 debug_info,
David Srbecky5d950762016-03-07 20:47:29 +0000213 dwarf::DW_DEBUG_FRAME_FORMAT,
214 true /* write_oat_patches */);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700215
David Srbecky6d8c8f02015-10-26 10:57:09 +0000216 builder_->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700217
Andreas Gampe08c277c2017-04-26 22:22:15 -0700218 bool ret_value = builder_->Good();
219
220 builder_.reset();
221 output_stream.reset();
222
223 if (elf_file->FlushCloseOrErase() != 0) {
224 return false;
225 }
226 elf_file.reset();
227
228 return ret_value;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700229 }
230
David Srbecky5d950762016-03-07 20:47:29 +0000231 void Walk() {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700232 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
233 for (size_t i = 0; i < oat_dex_files.size(); i++) {
234 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700235 CHECK(oat_dex_file != nullptr);
David Srbecky5d950762016-03-07 20:47:29 +0000236 WalkOatDexFile(oat_dex_file);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700237 }
238 }
239
David Srbecky5d950762016-03-07 20:47:29 +0000240 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700241 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700242 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
243 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700244 return;
245 }
246 for (size_t class_def_index = 0;
247 class_def_index < dex_file->NumClassDefs();
248 class_def_index++) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700249 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
250 OatClassType type = oat_class.GetType();
251 switch (type) {
252 case kOatClassAllCompiled:
253 case kOatClassSomeCompiled:
David Srbecky5d950762016-03-07 20:47:29 +0000254 WalkOatClass(oat_class, *dex_file, class_def_index);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700255 break;
256
257 case kOatClassNoneCompiled:
258 case kOatClassMax:
259 // Ignore.
260 break;
261 }
262 }
263 }
264
David Srbecky5d950762016-03-07 20:47:29 +0000265 void WalkOatClass(const OatFile::OatClass& oat_class,
266 const DexFile& dex_file,
267 uint32_t class_def_index) {
268 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700269 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700270 if (class_data == nullptr) { // empty class such as a marker interface?
271 return;
272 }
273 // Note: even if this is an interface or a native class, we still have to walk it, as there
274 // might be a static initializer.
275 ClassDataItemIterator it(dex_file, class_data);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700276 uint32_t class_method_idx = 0;
Mathieu Chartiere17cf242017-06-19 11:05:51 -0700277 it.SkipAllFields();
Mathieu Chartierb7c273c2017-11-10 18:07:56 -0800278 for (; it.HasNextMethod(); it.Next()) {
David Srbecky5d950762016-03-07 20:47:29 +0000279 WalkOatMethod(oat_class.GetOatMethod(class_method_idx++),
280 dex_file,
281 class_def_index,
282 it.GetMemberIndex(),
283 it.GetMethodCodeItem(),
284 it.GetMethodAccessFlags());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700285 }
286 DCHECK(!it.HasNext());
287 }
288
David Srbecky5d950762016-03-07 20:47:29 +0000289 void WalkOatMethod(const OatFile::OatMethod& oat_method,
290 const DexFile& dex_file,
291 uint32_t class_def_index,
292 uint32_t dex_method_index,
293 const DexFile::CodeItem* code_item,
294 uint32_t method_access_flags) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700295 if ((method_access_flags & kAccAbstract) != 0) {
296 // Abstract method, no code.
297 return;
298 }
David Srbecky5d950762016-03-07 20:47:29 +0000299 const OatHeader& oat_header = oat_file_->GetOatHeader();
300 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
301 if (method_header == nullptr || method_header->GetCodeSize() == 0) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700302 // No code.
303 return;
304 }
305
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100306 uint32_t entry_point = oat_method.GetCodeOffset() - oat_header.GetExecutableOffset();
307 // Clear Thumb2 bit.
308 const void* code_address = EntryPointToCodePointer(reinterpret_cast<void*>(entry_point));
309
Vladimir Marko1b404a82017-09-01 13:35:26 +0100310 debug::MethodDebugInfo info = {};
311 DCHECK(info.trampoline_name.empty());
David Srbecky5d950762016-03-07 20:47:29 +0000312 info.dex_file = &dex_file;
313 info.class_def_index = class_def_index;
314 info.dex_method_index = dex_method_index;
315 info.access_flags = method_access_flags;
316 info.code_item = code_item;
317 info.isa = oat_header.GetInstructionSet();
318 info.deduped = !seen_offsets_.insert(oat_method.GetCodeOffset()).second;
319 info.is_native_debuggable = oat_header.IsNativeDebuggable();
320 info.is_optimized = method_header->IsOptimized();
321 info.is_code_address_text_relative = true;
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100322 info.code_address = reinterpret_cast<uintptr_t>(code_address);
David Srbecky5d950762016-03-07 20:47:29 +0000323 info.code_size = method_header->GetCodeSize();
324 info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
325 info.code_info = info.is_optimized ? method_header->GetOptimizedCodeInfoPtr() : nullptr;
326 info.cfi = ArrayRef<uint8_t>();
327 method_debug_infos_.push_back(info);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700328 }
329
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700330 private:
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700331 const OatFile* oat_file_;
Vladimir Marko74527972016-11-29 15:57:32 +0000332 std::unique_ptr<linker::ElfBuilder<ElfTypes>> builder_;
David Srbecky5d950762016-03-07 20:47:29 +0000333 std::vector<debug::MethodDebugInfo> method_debug_infos_;
334 std::unordered_set<uint32_t> seen_offsets_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700335 const std::string output_name_;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000336 bool no_bits_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700337};
338
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700339class OatDumperOptions {
340 public:
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100341 OatDumperOptions(bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100342 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700343 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700344 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800345 const char* class_filter,
346 const char* method_filter,
347 bool list_classes,
348 bool list_methods,
David Brazdilc03d7b62016-03-02 12:18:03 +0000349 bool dump_header_only,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800350 const char* export_dex_location,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800351 const char* app_image,
352 const char* app_oat,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800353 uint32_t addr2instr)
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100354 : dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100355 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700356 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700357 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800358 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000359 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800360 list_classes_(list_classes),
361 list_methods_(list_methods),
David Brazdilc03d7b62016-03-02 12:18:03 +0000362 dump_header_only_(dump_header_only),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800363 export_dex_location_(export_dex_location),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800364 app_image_(app_image),
365 app_oat_(app_oat),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800366 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800367 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700368
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700369 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100370 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700371 const bool disassemble_code_;
372 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800373 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000374 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800375 const bool list_classes_;
376 const bool list_methods_;
David Brazdilc03d7b62016-03-02 12:18:03 +0000377 const bool dump_header_only_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800378 const char* const export_dex_location_;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800379 const char* const app_image_;
380 const char* const app_oat_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800381 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700382 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700383};
384
Elliott Hughese3c845c2012-02-28 17:23:01 -0800385class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700386 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100387 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000388 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800389 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700390 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800391 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800392 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
393 disassembler_(Disassembler::Create(instruction_set_,
Andreas Gampe372f3a32016-08-19 10:49:06 -0700394 new DisassemblerOptions(
395 options_.absolute_addresses_,
396 oat_file.Begin(),
397 oat_file.End(),
398 true /* can_read_literals_ */,
399 Is64BitInstructionSet(instruction_set_)
400 ? &Thread::DumpThreadOffset<PointerSize::k64>
401 : &Thread::DumpThreadOffset<PointerSize::k32>))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800402 CHECK(options_.class_loader_ != nullptr);
403 CHECK(options_.class_filter_ != nullptr);
404 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800405 AddAllOffsets();
406 }
407
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700408 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700409 delete disassembler_;
410 }
411
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800412 InstructionSet GetInstructionSet() {
413 return instruction_set_;
414 }
415
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300416 typedef std::vector<std::unique_ptr<const DexFile>> DexFileUniqV;
417
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700418 bool Dump(std::ostream& os) {
419 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800420 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700421
422 os << "MAGIC:\n";
423 os << oat_header.GetMagic() << "\n\n";
424
Jeff Hao4b07a6e2016-01-15 12:50:44 -0800425 os << "LOCATION:\n";
426 os << oat_file_.GetLocation() << "\n\n";
427
Brian Carlstromaded5f72011-10-07 17:15:04 -0700428 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800429 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700430
Elliott Hughesa72ec822012-03-05 17:12:22 -0800431 os << "INSTRUCTION SET:\n";
432 os << oat_header.GetInstructionSet() << "\n\n";
433
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700434 {
435 std::unique_ptr<const InstructionSetFeatures> features(
436 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
437 oat_header.GetInstructionSetFeaturesBitmap()));
438 os << "INSTRUCTION SET FEATURES:\n";
439 os << features->GetFeatureString() << "\n\n";
440 }
Dave Allison70202782013-10-22 17:52:19 -0700441
Brian Carlstromaded5f72011-10-07 17:15:04 -0700442 os << "DEX FILE COUNT:\n";
443 os << oat_header.GetDexFileCount() << "\n\n";
444
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800445#define DUMP_OAT_HEADER_OFFSET(label, offset) \
446 os << label " OFFSET:\n"; \
447 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800448 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800449 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
450 } \
451 os << StringPrintf("\n\n");
452
453 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
454 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
455 GetInterpreterToInterpreterBridgeOffset);
456 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
457 GetInterpreterToCompiledCodeBridgeOffset);
458 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
459 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800460 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
461 GetQuickGenericJniTrampolineOffset);
462 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
463 GetQuickImtConflictTrampolineOffset);
464 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
465 GetQuickResolutionTrampolineOffset);
466 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
467 GetQuickToInterpreterBridgeOffset);
468#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700469
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700470 os << "IMAGE PATCH DELTA:\n";
471 os << StringPrintf("%d (0x%08x)\n\n",
472 oat_header.GetImagePatchDelta(),
473 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700474
Brian Carlstrom28db0122012-10-18 16:20:41 -0700475 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
476 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
477
478 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800479 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700480
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700481 // Print the key-value store.
482 {
483 os << "KEY VALUE STORE:\n";
484 size_t index = 0;
485 const char* key;
486 const char* value;
487 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
488 os << key << " = " << value << "\n";
489 index++;
490 }
491 os << "\n";
492 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700493
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800494 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700495 os << "BEGIN:\n";
496 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700497
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700498 os << "END:\n";
499 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
500 }
501
502 os << "SIZE:\n";
503 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700504
505 os << std::flush;
506
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800507 // If set, adjust relative address to be searched
508 if (options_.addr2instr_ != 0) {
509 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
510 os << "SEARCH ADDRESS (executable offset + input):\n";
511 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
512 }
513
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700514 // Dumping the dex file overview is compact enough to do even if header only.
515 DexFileData cumulative;
516 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
517 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
518 CHECK(oat_dex_file != nullptr);
519 std::string error_msg;
520 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
521 if (dex_file == nullptr) {
522 os << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation() << "': "
523 << error_msg;
524 continue;
525 }
526 DexFileData data(*dex_file);
527 os << "Dex file data for " << dex_file->GetLocation() << "\n";
528 data.Dump(os);
529 os << "\n";
Mathieu Chartier120aa282017-08-05 16:03:03 -0700530 const DexLayoutSections* const layout_sections = oat_dex_file->GetDexLayoutSections();
531 if (layout_sections != nullptr) {
532 os << "Layout data\n";
533 os << *layout_sections;
534 os << "\n";
535 }
536
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700537 cumulative.Add(data);
Vladimir Markof3c52b42017-11-17 17:32:12 +0000538
539 // Dump .bss entries.
540 DumpBssEntries(
541 os,
542 "ArtMethod",
543 oat_dex_file->GetMethodBssMapping(),
544 dex_file->NumMethodIds(),
545 static_cast<size_t>(GetInstructionSetPointerSize(instruction_set_)),
546 [=](uint32_t index) { return dex_file->PrettyMethod(index); });
547 DumpBssEntries(
548 os,
549 "Class",
550 oat_dex_file->GetTypeBssMapping(),
551 dex_file->NumTypeIds(),
552 sizeof(GcRoot<mirror::Class>),
553 [=](uint32_t index) { return dex_file->PrettyType(dex::TypeIndex(index)); });
554 DumpBssEntries(
555 os,
556 "String",
557 oat_dex_file->GetStringBssMapping(),
558 dex_file->NumStringIds(),
559 sizeof(GcRoot<mirror::Class>),
560 [=](uint32_t index) { return dex_file->StringDataByIdx(dex::StringIndex(index)); });
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700561 }
562 os << "Cumulative dex file data\n";
563 cumulative.Dump(os);
564 os << "\n";
565
David Brazdilc03d7b62016-03-02 12:18:03 +0000566 if (!options_.dump_header_only_) {
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000567 VariableIndentationOutputStream vios(&os);
568 VdexFile::Header vdex_header = oat_file_.GetVdexFile()->GetHeader();
569 if (vdex_header.IsValid()) {
570 std::string error_msg;
571 std::vector<const DexFile*> dex_files;
572 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
573 const DexFile* dex_file = OpenDexFile(oat_dex_files_[i], &error_msg);
574 if (dex_file == nullptr) {
575 os << "Error opening dex file: " << error_msg << std::endl;
576 return false;
577 }
578 dex_files.push_back(dex_file);
579 }
580 verifier::VerifierDeps deps(dex_files, oat_file_.GetVdexFile()->GetVerifierDepsData());
581 deps.Dump(&vios);
582 } else {
583 os << "UNRECOGNIZED vdex file, magic "
584 << vdex_header.GetMagic()
585 << ", version "
586 << vdex_header.GetVersion()
587 << "\n";
588 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000589 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
590 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
591 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300592 if (!DumpOatDexFile(os, *oat_dex_file)) {
593 success = false;
594 }
595 }
596 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800597
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300598 if (options_.export_dex_location_) {
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000599 std::string error_msg;
600 std::string vdex_filename = GetVdexFilename(oat_file_.GetLocation());
601 if (!OS::FileExists(vdex_filename.c_str())) {
602 os << "File " << vdex_filename.c_str() << " does not exist\n";
603 return false;
604 }
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300605
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000606 DexFileUniqV vdex_dex_files;
607 std::unique_ptr<const VdexFile> vdex_file = OpenVdexUnquicken(vdex_filename,
608 &vdex_dex_files,
609 &error_msg);
610 if (vdex_file.get() == nullptr) {
611 os << "Failed to open vdex file: " << error_msg << "\n";
612 return false;
613 }
614 if (oat_dex_files_.size() != vdex_dex_files.size()) {
615 os << "Dex files number in Vdex file does not match Dex files number in Oat file: "
616 << vdex_dex_files.size() << " vs " << oat_dex_files_.size() << '\n';
617 return false;
618 }
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300619
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000620 size_t i = 0;
621 for (const auto& vdex_dex_file : vdex_dex_files) {
622 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
623 CHECK(oat_dex_file != nullptr);
624 CHECK(vdex_dex_file != nullptr);
625 if (!ExportDexFile(os, *oat_dex_file, vdex_dex_file.get())) {
626 success = false;
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300627 }
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000628 i++;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700629 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700630 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000631
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800632 {
633 os << "OAT FILE STATS:\n";
634 VariableIndentationOutputStream vios(&os);
635 stats_.Dump(vios);
636 }
637
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700638 os << std::flush;
639 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700640 }
641
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800642 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700643 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
644 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800645 return 0; // Address not in oat file
646 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800647 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
648 reinterpret_cast<uintptr_t>(oat_file_.Begin());
649 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800650 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800651 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800652 return end_offset - begin_offset;
653 }
654
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800655 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700656 return oat_file_.GetOatHeader().GetInstructionSet();
657 }
658
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700659 const void* GetQuickOatCode(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800660 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
661 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700662 CHECK(oat_dex_file != nullptr);
663 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700664 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
665 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700666 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
667 << "': " << error_msg;
668 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800669 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700670 const DexFile::ClassDef* class_def =
David Sehr9aa352e2016-09-15 18:13:52 -0700671 OatDexFile::FindClassDef(*dex_file, descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700672 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700673 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100674 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800675 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100676 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800677 }
678 }
679 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700680 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800681 }
682
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300683 // Returns nullptr and updates error_msg if the Vdex file cannot be opened, otherwise all Dex
684 // files are fully unquickened and stored in dex_files
685 std::unique_ptr<const VdexFile> OpenVdexUnquicken(const std::string& vdex_filename,
686 /* out */ DexFileUniqV* dex_files,
687 /* out */ std::string* error_msg) {
688 std::unique_ptr<const File> file(OS::OpenFileForReading(vdex_filename.c_str()));
689 if (file == nullptr) {
690 *error_msg = "Could not open file " + vdex_filename + " for reading.";
691 return nullptr;
692 }
693
694 int64_t vdex_length = file->GetLength();
695 if (vdex_length == -1) {
696 *error_msg = "Could not read the length of file " + vdex_filename;
697 return nullptr;
698 }
699
700 std::unique_ptr<MemMap> mmap(MemMap::MapFile(
701 file->GetLength(),
702 PROT_READ | PROT_WRITE,
703 MAP_PRIVATE,
704 file->Fd(),
705 /* start offset */ 0,
706 /* low_4gb */ false,
707 vdex_filename.c_str(),
708 error_msg));
709 if (mmap == nullptr) {
710 *error_msg = "Failed to mmap file " + vdex_filename + ": " + *error_msg;
711 return nullptr;
712 }
713
714 std::unique_ptr<VdexFile> vdex_file(new VdexFile(mmap.release()));
715 if (!vdex_file->IsValid()) {
716 *error_msg = "Vdex file is not valid";
717 return nullptr;
718 }
719
720 DexFileUniqV tmp_dex_files;
721 if (!vdex_file->OpenAllDexFiles(&tmp_dex_files, error_msg)) {
722 *error_msg = "Failed to open Dex files from Vdex: " + *error_msg;
723 return nullptr;
724 }
725
726 vdex_file->Unquicken(MakeNonOwningPointerVector(tmp_dex_files),
Nicolas Geoffray67169412018-01-12 09:06:14 +0000727 vdex_file->GetQuickeningInfo(),
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300728 /* decompile_return_instruction */ true);
729
730 *dex_files = std::move(tmp_dex_files);
731 return vdex_file;
732 }
733
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800734 struct Stats {
735 enum ByteKind {
736 kByteKindCode,
737 kByteKindQuickMethodHeader,
738 kByteKindCodeInfoLocationCatalog,
739 kByteKindCodeInfoDexRegisterMap,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800740 kByteKindCodeInfoEncoding,
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800741 kByteKindCodeInfoInvokeInfo,
David Srbecky45aa5982016-03-18 02:15:09 +0000742 kByteKindCodeInfoStackMasks,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800743 kByteKindCodeInfoRegisterMasks,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800744 kByteKindStackMapNativePc,
745 kByteKindStackMapDexPc,
746 kByteKindStackMapDexRegisterMap,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800747 kByteKindStackMapInlineInfoIndex,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800748 kByteKindStackMapRegisterMaskIndex,
David Srbecky45aa5982016-03-18 02:15:09 +0000749 kByteKindStackMapStackMaskIndex,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700750 kByteKindInlineInfoMethodIndexIdx,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800751 kByteKindInlineInfoDexPc,
752 kByteKindInlineInfoExtraData,
753 kByteKindInlineInfoDexRegisterMap,
754 kByteKindInlineInfoIsLast,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800755 kByteKindCount,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800756 // Special ranges for std::accumulate convenience.
757 kByteKindStackMapFirst = kByteKindStackMapNativePc,
David Srbecky45aa5982016-03-18 02:15:09 +0000758 kByteKindStackMapLast = kByteKindStackMapStackMaskIndex,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700759 kByteKindInlineInfoFirst = kByteKindInlineInfoMethodIndexIdx,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800760 kByteKindInlineInfoLast = kByteKindInlineInfoIsLast,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800761 };
762 int64_t bits[kByteKindCount] = {};
763 // Since code has deduplication, seen tracks already seen pointers to avoid double counting
764 // deduplicated code and tables.
765 std::unordered_set<const void*> seen;
766
767 // Returns true if it was newly added.
768 bool AddBitsIfUnique(ByteKind kind, int64_t count, const void* address) {
769 if (seen.insert(address).second == true) {
770 // True means the address was not already in the set.
771 AddBits(kind, count);
772 return true;
773 }
774 return false;
775 }
776
777 void AddBits(ByteKind kind, int64_t count) {
778 bits[kind] += count;
779 }
780
781 void Dump(VariableIndentationOutputStream& os) {
782 const int64_t sum = std::accumulate(bits, bits + kByteKindCount, 0u);
783 os.Stream() << "Dumping cumulative use of " << sum / kBitsPerByte << " accounted bytes\n";
784 if (sum > 0) {
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800785 Dump(os, "Code ", bits[kByteKindCode], sum);
786 Dump(os, "QuickMethodHeader ", bits[kByteKindQuickMethodHeader], sum);
787 Dump(os, "CodeInfoEncoding ", bits[kByteKindCodeInfoEncoding], sum);
788 Dump(os, "CodeInfoLocationCatalog ", bits[kByteKindCodeInfoLocationCatalog], sum);
789 Dump(os, "CodeInfoDexRegisterMap ", bits[kByteKindCodeInfoDexRegisterMap], sum);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800790 Dump(os, "CodeInfoStackMasks ", bits[kByteKindCodeInfoStackMasks], sum);
791 Dump(os, "CodeInfoRegisterMasks ", bits[kByteKindCodeInfoRegisterMasks], sum);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800792 Dump(os, "CodeInfoInvokeInfo ", bits[kByteKindCodeInfoInvokeInfo], sum);
Mathieu Chartier1e083792017-02-08 13:30:04 -0800793 // Stack map section.
794 const int64_t stack_map_bits = std::accumulate(bits + kByteKindStackMapFirst,
795 bits + kByteKindStackMapLast + 1,
796 0u);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800797 Dump(os, "CodeInfoStackMap ", stack_map_bits, sum);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800798 {
799 ScopedIndentation indent1(&os);
800 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800801 "StackMapNativePc ",
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800802 bits[kByteKindStackMapNativePc],
803 stack_map_bits,
804 "stack map");
805 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800806 "StackMapDexPcEncoding ",
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800807 bits[kByteKindStackMapDexPc],
808 stack_map_bits,
809 "stack map");
810 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800811 "StackMapDexRegisterMap ",
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800812 bits[kByteKindStackMapDexRegisterMap],
813 stack_map_bits,
814 "stack map");
815 Dump(os,
Mathieu Chartier697dc662017-02-08 16:56:48 -0800816 "StackMapInlineInfoIndex ",
Mathieu Chartier1e083792017-02-08 13:30:04 -0800817 bits[kByteKindStackMapInlineInfoIndex],
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800818 stack_map_bits,
819 "stack map");
820 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800821 "StackMapRegisterMaskIndex ",
822 bits[kByteKindStackMapRegisterMaskIndex],
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800823 stack_map_bits,
824 "stack map");
825 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800826 "StackMapStackMaskIndex ",
David Srbecky45aa5982016-03-18 02:15:09 +0000827 bits[kByteKindStackMapStackMaskIndex],
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800828 stack_map_bits,
829 "stack map");
830 }
Mathieu Chartier1e083792017-02-08 13:30:04 -0800831 // Inline info section.
832 const int64_t inline_info_bits = std::accumulate(bits + kByteKindInlineInfoFirst,
833 bits + kByteKindInlineInfoLast + 1,
834 0u);
835 Dump(os, "CodeInfoInlineInfo ", inline_info_bits, sum);
836 {
837 ScopedIndentation indent1(&os);
838 Dump(os,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700839 "InlineInfoMethodIndexIdx ",
840 bits[kByteKindInlineInfoMethodIndexIdx],
Mathieu Chartier1e083792017-02-08 13:30:04 -0800841 inline_info_bits,
842 "inline info");
843 Dump(os,
844 "InlineInfoDexPc ",
845 bits[kByteKindStackMapDexPc],
846 inline_info_bits,
847 "inline info");
848 Dump(os,
849 "InlineInfoExtraData ",
850 bits[kByteKindInlineInfoExtraData],
851 inline_info_bits,
852 "inline info");
853 Dump(os,
854 "InlineInfoDexRegisterMap ",
855 bits[kByteKindInlineInfoDexRegisterMap],
856 inline_info_bits,
857 "inline info");
858 Dump(os,
859 "InlineInfoIsLast ",
860 bits[kByteKindInlineInfoIsLast],
861 inline_info_bits,
862 "inline info");
863 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800864 }
865 os.Stream() << "\n" << std::flush;
866 }
867
868 private:
869 void Dump(VariableIndentationOutputStream& os,
870 const char* name,
871 int64_t size,
872 int64_t total,
873 const char* sum_of = "total") {
874 const double percent = (static_cast<double>(size) / static_cast<double>(total)) * 100;
875 os.Stream() << StringPrintf("%s = %8" PRId64 " (%2.0f%% of %s)\n",
876 name,
877 size / kBitsPerByte,
878 percent,
879 sum_of);
880 }
881 };
882
Brian Carlstromaded5f72011-10-07 17:15:04 -0700883 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800884 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800885 // We don't know the length of the code for each method, but we need to know where to stop
886 // when disassembling. What we do know is that a region of code will be followed by some other
887 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
888 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800889 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
890 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700891 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700892 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700893 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
894 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700895 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
896 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800897 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800898 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800899 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800900 for (size_t class_def_index = 0;
901 class_def_index < dex_file->NumClassDefs();
902 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800903 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100904 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700905 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700906 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800907 ClassDataItemIterator it(*dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -0700908 it.SkipAllFields();
Elliott Hughese3c845c2012-02-28 17:23:01 -0800909 uint32_t class_method_index = 0;
Mathieu Chartierb7c273c2017-11-10 18:07:56 -0800910 while (it.HasNextMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100911 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800912 it.Next();
913 }
914 }
915 }
916 }
917
918 // If the last thing in the file is code for a method, there won't be an offset for the "next"
919 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
920 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800921 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800922 }
923
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700924 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
925 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
926 }
927
Elliott Hughese3c845c2012-02-28 17:23:01 -0800928 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800929 uint32_t code_offset = oat_method.GetCodeOffset();
Vladimir Marko33bff252017-11-01 14:35:42 +0000930 if (oat_file_.GetOatHeader().GetInstructionSet() == InstructionSet::kThumb2) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800931 code_offset &= ~0x1;
932 }
933 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800934 offsets_.insert(oat_method.GetVmapTableOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800935 }
936
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700937 // Dex file data, may be for multiple different dex files.
938 class DexFileData {
939 public:
940 DexFileData() {}
941
942 explicit DexFileData(const DexFile& dex_file)
943 : num_string_ids_(dex_file.NumStringIds()),
944 num_method_ids_(dex_file.NumMethodIds()),
945 num_field_ids_(dex_file.NumFieldIds()),
946 num_type_ids_(dex_file.NumTypeIds()),
947 num_class_defs_(dex_file.NumClassDefs()) {
948 for (size_t class_def_index = 0; class_def_index < num_class_defs_; ++class_def_index) {
949 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
950 WalkClass(dex_file, class_def);
951 }
952 }
953
954 void Add(const DexFileData& other) {
955 AddAll(unique_string_ids_from_code_, other.unique_string_ids_from_code_);
956 num_string_ids_from_code_ += other.num_string_ids_from_code_;
957 AddAll(dex_code_item_ptrs_, other.dex_code_item_ptrs_);
958 dex_code_bytes_ += other.dex_code_bytes_;
959 num_string_ids_ += other.num_string_ids_;
960 num_method_ids_ += other.num_method_ids_;
961 num_field_ids_ += other.num_field_ids_;
962 num_type_ids_ += other.num_type_ids_;
963 num_class_defs_ += other.num_class_defs_;
964 }
965
966 void Dump(std::ostream& os) {
967 os << "Num string ids: " << num_string_ids_ << "\n";
968 os << "Num method ids: " << num_method_ids_ << "\n";
969 os << "Num field ids: " << num_field_ids_ << "\n";
970 os << "Num type ids: " << num_type_ids_ << "\n";
971 os << "Num class defs: " << num_class_defs_ << "\n";
972 os << "Unique strings loaded from dex code: " << unique_string_ids_from_code_.size() << "\n";
973 os << "Total strings loaded from dex code: " << num_string_ids_from_code_ << "\n";
974 os << "Number of unique dex code items: " << dex_code_item_ptrs_.size() << "\n";
975 os << "Total number of dex code bytes: " << dex_code_bytes_ << "\n";
976 }
977
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800978 private:
Andreas Gampe9186ced2016-12-12 14:28:21 -0800979 // All of the elements from one container to another.
980 template <typename Dest, typename Src>
981 static void AddAll(Dest& dest, const Src& src) {
982 dest.insert(src.begin(), src.end());
983 }
984
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700985 void WalkClass(const DexFile& dex_file, const DexFile::ClassDef& class_def) {
986 const uint8_t* class_data = dex_file.GetClassData(class_def);
987 if (class_data == nullptr) { // empty class such as a marker interface?
988 return;
989 }
990 ClassDataItemIterator it(dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -0700991 it.SkipAllFields();
Mathieu Chartierb7c273c2017-11-10 18:07:56 -0800992 while (it.HasNextMethod()) {
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700993 WalkCodeItem(dex_file, it.GetMethodCodeItem());
994 it.Next();
995 }
996 DCHECK(!it.HasNext());
997 }
998
999 void WalkCodeItem(const DexFile& dex_file, const DexFile::CodeItem* code_item) {
1000 if (code_item == nullptr) {
1001 return;
1002 }
Mathieu Chartier698ebbc2018-01-05 11:00:42 -08001003 CodeItemInstructionAccessor instructions(dex_file, code_item);
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001004
1005 // If we inserted a new dex code item pointer, add to total code bytes.
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001006 const uint16_t* code_ptr = instructions.Insns();
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001007 if (dex_code_item_ptrs_.insert(code_ptr).second) {
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001008 dex_code_bytes_ += instructions.InsnsSizeInCodeUnits() * sizeof(code_ptr[0]);
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001009 }
1010
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001011 for (const DexInstructionPcPair& inst : instructions) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001012 switch (inst->Opcode()) {
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001013 case Instruction::CONST_STRING: {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001014 const dex::StringIndex string_index(inst->VRegB_21c());
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001015 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
1016 ++num_string_ids_from_code_;
1017 break;
1018 }
1019 case Instruction::CONST_STRING_JUMBO: {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001020 const dex::StringIndex string_index(inst->VRegB_31c());
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001021 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
1022 ++num_string_ids_from_code_;
1023 break;
1024 }
1025 default:
1026 break;
1027 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001028 }
1029 }
1030
1031 // Unique string ids loaded from dex code.
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001032 std::set<StringReference> unique_string_ids_from_code_;
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001033
1034 // Total string ids loaded from dex code.
1035 size_t num_string_ids_from_code_ = 0;
1036
1037 // Unique code pointers.
1038 std::set<const void*> dex_code_item_ptrs_;
1039
1040 // Total "unique" dex code bytes.
1041 size_t dex_code_bytes_ = 0;
1042
1043 // Other dex ids.
1044 size_t num_string_ids_ = 0;
1045 size_t num_method_ids_ = 0;
1046 size_t num_field_ids_ = 0;
1047 size_t num_type_ids_ = 0;
1048 size_t num_class_defs_ = 0;
1049 };
1050
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001051 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
1052 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001053 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001054 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -08001055 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -08001056 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -07001057
Andreas Gampe2ba88952016-04-29 17:52:07 -07001058 const uint8_t* const oat_file_begin = oat_dex_file.GetOatFile()->Begin();
David Brazdil7b49e6c2016-09-01 11:06:18 +01001059 const uint8_t* const vdex_file_begin = oat_dex_file.GetOatFile()->DexBegin();
1060
1061 // Print data range of the dex file embedded inside the corresponding vdex file.
Andreas Gampe2ba88952016-04-29 17:52:07 -07001062 const uint8_t* const dex_file_pointer = oat_dex_file.GetDexFilePointer();
David Brazdil7b49e6c2016-09-01 11:06:18 +01001063 uint32_t dex_offset = dchecked_integral_cast<uint32_t>(dex_file_pointer - vdex_file_begin);
Andreas Gampe2ba88952016-04-29 17:52:07 -07001064 os << StringPrintf("dex-file: 0x%08x..0x%08x\n",
1065 dex_offset,
1066 dchecked_integral_cast<uint32_t>(dex_offset + oat_dex_file.FileSize() - 1));
Mathieu Chartier590fee92013-09-13 13:46:47 -07001067
Andreas Gampe2ba88952016-04-29 17:52:07 -07001068 // Create the dex file early. A lot of print-out things depend on it.
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001069 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07001070 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
1071 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001072 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001073 os << std::flush;
1074 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001075 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001076
Andreas Gampe2ba88952016-04-29 17:52:07 -07001077 // Print lookup table, if it exists.
1078 if (oat_dex_file.GetLookupTableData() != nullptr) {
1079 uint32_t table_offset = dchecked_integral_cast<uint32_t>(
1080 oat_dex_file.GetLookupTableData() - oat_file_begin);
David Sehr9aa352e2016-09-15 18:13:52 -07001081 uint32_t table_size = TypeLookupTable::RawDataLength(dex_file->NumClassDefs());
Andreas Gampe2ba88952016-04-29 17:52:07 -07001082 os << StringPrintf("type-table: 0x%08x..0x%08x\n",
1083 table_offset,
1084 table_offset + table_size - 1);
1085 }
1086
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001087 VariableIndentationOutputStream vios(&os);
1088 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001089 for (size_t class_def_index = 0;
1090 class_def_index < dex_file->NumClassDefs();
1091 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001092 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
1093 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001094
1095 // TODO: Support regex
1096 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
1097 continue;
1098 }
1099
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001100 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001101 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001102 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
Andreas Gampea5b09a62016-11-17 15:21:22 -08001103 class_def_index, descriptor, oat_class_offset, class_def.class_idx_.index_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001104 << " (" << oat_class.GetStatus() << ")"
1105 << " (" << oat_class.GetType() << ")\n";
1106 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001107 if (options_.list_classes_) {
1108 continue;
1109 }
1110 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001111 success = false;
1112 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001113 if (stop_analysis) {
1114 os << std::flush;
1115 return success;
1116 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001117 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001118 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001119 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001120 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001121 }
1122
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +03001123 // Backwards compatible Dex file export. If dex_file is nullptr (valid Vdex file not present) the
1124 // Dex resource is extracted from the oat_dex_file and its checksum is repaired since it's not
1125 // unquickened. Otherwise the dex_file has been fully unquickened and is expected to verify the
1126 // original checksum.
1127 bool ExportDexFile(std::ostream& os,
1128 const OatFile::OatDexFile& oat_dex_file,
1129 const DexFile* dex_file) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001130 std::string error_msg;
1131 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001132 size_t fsize = oat_dex_file.FileSize();
1133
1134 // Some quick checks just in case
1135 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
1136 os << "Invalid dex file\n";
1137 return false;
1138 }
1139
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +03001140 if (dex_file == nullptr) {
1141 // Exported bytecode is quickened (dex-to-dex transformations present)
1142 dex_file = OpenDexFile(&oat_dex_file, &error_msg);
1143 if (dex_file == nullptr) {
1144 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
1145 return false;
1146 }
1147
1148 // Recompute checksum
1149 reinterpret_cast<DexFile::Header*>(const_cast<uint8_t*>(dex_file->Begin()))->checksum_ =
1150 dex_file->CalculateChecksum();
1151 } else {
1152 // Vdex unquicken output should match original input bytecode
1153 uint32_t orig_checksum =
1154 reinterpret_cast<DexFile::Header*>(const_cast<uint8_t*>(dex_file->Begin()))->checksum_;
1155 if (orig_checksum != dex_file->CalculateChecksum()) {
1156 os << "Unexpected checksum from unquicken dex file '" << dex_file_location << "'\n";
1157 return false;
1158 }
1159 }
1160
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001161 // Verify output directory exists
1162 if (!OS::DirectoryExists(options_.export_dex_location_)) {
1163 // TODO: Extend OS::DirectoryExists if symlink support is required
1164 os << options_.export_dex_location_ << " output directory not found or symlink\n";
1165 return false;
1166 }
1167
1168 // Beautify path names
1169 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
1170 return false;
1171 }
1172
1173 std::string dex_orig_name;
1174 size_t dex_orig_pos = dex_file_location.rfind('/');
1175 if (dex_orig_pos == std::string::npos)
1176 dex_orig_name = dex_file_location;
1177 else
1178 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
1179
1180 // A more elegant approach to efficiently name user installed apps is welcome
Andreas Gampef812d8c2017-02-17 10:19:44 -08001181 if (dex_orig_name.size() == 8 &&
1182 dex_orig_name.compare("base.apk") == 0 &&
1183 dex_orig_pos != std::string::npos) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001184 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
1185 size_t apk_orig_pos = dex_file_location.rfind('/');
1186 if (apk_orig_pos != std::string::npos) {
1187 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
1188 }
1189 }
1190
1191 std::string out_dex_path(options_.export_dex_location_);
1192 if (out_dex_path.back() != '/') {
1193 out_dex_path.append("/");
1194 }
1195 out_dex_path.append(dex_orig_name);
1196 out_dex_path.append("_export.dex");
1197 if (out_dex_path.length() > PATH_MAX) {
1198 return false;
1199 }
1200
1201 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
1202 if (file.get() == nullptr) {
1203 os << "Failed to open output dex file " << out_dex_path;
1204 return false;
1205 }
1206
1207 if (!file->WriteFully(dex_file->Begin(), fsize)) {
1208 os << "Failed to write dex file";
1209 file->Erase();
1210 return false;
1211 }
1212
1213 if (file->FlushCloseOrErase() != 0) {
1214 os << "Flush and close failed";
1215 return false;
1216 }
1217
1218 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
1219 os << std::flush;
1220
1221 return true;
1222 }
1223
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001224 bool DumpOatClass(VariableIndentationOutputStream* vios,
1225 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001226 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001227 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001228 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -07001229 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001230 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001231 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001232 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -08001233 }
1234 ClassDataItemIterator it(dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -07001235 it.SkipAllFields();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001236 uint32_t class_method_index = 0;
Mathieu Chartierb7c273c2017-11-10 18:07:56 -08001237 while (it.HasNextMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001238 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001239 it.GetMemberIndex(), it.GetMethodCodeItem(),
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001240 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001241 success = false;
1242 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001243 if (addr_found) {
1244 *stop_analysis = true;
1245 return success;
1246 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001247 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -07001248 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001249 }
Ian Rogers0571d352011-11-03 19:51:38 -07001250 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001251 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001252 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001253 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001254
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001255 static constexpr uint32_t kPrologueBytes = 16;
1256
1257 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
1258 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
1259
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001260 bool DumpOatMethod(VariableIndentationOutputStream* vios,
1261 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -07001262 uint32_t class_method_index,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001263 const OatFile::OatClass& oat_class,
1264 const DexFile& dex_file,
1265 uint32_t dex_method_idx,
1266 const DexFile::CodeItem* code_item,
1267 uint32_t method_access_flags,
1268 bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001269 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001270
Mathieu Chartier698ebbc2018-01-05 11:00:42 -08001271 CodeItemDataAccessor code_item_accessor(dex_file, code_item);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001272
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001273 // TODO: Support regex
1274 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
1275 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00001276 return success;
1277 }
1278
David Sehr709b0702016-10-13 09:12:37 -07001279 std::string pretty_method = dex_file.PrettyMethod(dex_method_idx, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001280 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
1281 class_method_index, pretty_method.c_str(),
1282 dex_method_idx);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001283 if (options_.list_methods_) {
1284 return success;
1285 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001286
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001287 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
1288 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
1289 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
1290 uint32_t code_offset = oat_method.GetCodeOffset();
1291 uint32_t code_size = oat_method.GetQuickCodeSize();
1292 if (resolved_addr2instr_ != 0) {
1293 if (resolved_addr2instr_ > code_offset + code_size) {
1294 return success;
1295 } else {
1296 *addr_found = true; // stop analyzing file at next iteration
1297 }
1298 }
1299
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001300 // Everything below is indented at least once.
1301 ScopedIndentation indent1(vios);
1302
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001303 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001304 vios->Stream() << "DEX CODE:\n";
1305 ScopedIndentation indent2(vios);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001306 if (code_item_accessor.HasCodeItem()) {
1307 for (const DexInstructionPcPair& inst : code_item_accessor) {
1308 vios->Stream() << StringPrintf("0x%04x: ", inst.DexPc()) << inst->DumpHexLE(5)
1309 << StringPrintf("\t| %s\n", inst->DumpString(&dex_file).c_str());
1310 }
1311 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001312 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001313
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001314 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001315 std::unique_ptr<verifier::MethodVerifier> verifier;
1316 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001317 // We need to have the handle scope stay live until after the verifier since the verifier has
1318 // a handle to the dex cache from hs.
1319 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001320 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
1321 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001322 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001323 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001324 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -07001325 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001326 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001327 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001328 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001329 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001330 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001331 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001332 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001333 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001334 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
1335 oat_method_offsets_offset, oat_file_.Size());
1336 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001337 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001338 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001339 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001340
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001341 ScopedIndentation indent2(vios);
1342 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001343 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
1344 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001345 vios->Stream() << StringPrintf("WARNING: "
1346 "code offset 0x%08x is past end of file 0x%08zx.\n",
1347 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001348 success = false;
1349 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001350 vios->Stream() << "\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001351 }
1352 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001353 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001354 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
1355 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001356 stats_.AddBitsIfUnique(Stats::kByteKindQuickMethodHeader,
1357 sizeof(*method_header) * kBitsPerByte,
1358 method_header);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001359 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001360 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001361 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001362 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001363 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001364 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001365 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
1366 method_header_offset, oat_file_.Size());
1367 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001368 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001369 return false;
1370 }
1371
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001372 ScopedIndentation indent2(vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001373 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001374 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001375 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001376 }
Mingyao Yang063fc772016-08-02 11:02:54 -07001377 uint32_t vmap_table_offset = method_header ==
1378 nullptr ? 0 : method_header->GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001379 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001380
1381 size_t vmap_table_offset_limit =
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +00001382 IsMethodGeneratedByDexToDexCompiler(oat_method, code_item_accessor)
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001383 ? oat_file_.GetVdexFile()->Size()
1384 : method_header->GetCode() - oat_file_.Begin();
1385 if (vmap_table_offset >= vmap_table_offset_limit) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001386 vios->Stream() << StringPrintf("WARNING: "
1387 "vmap table offset 0x%08x is past end of file 0x%08zx. "
1388 "vmap table offset was loaded from offset 0x%08x.\n",
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001389 vmap_table_offset,
1390 vmap_table_offset_limit,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001391 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001392 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001393 } else if (options_.dump_vmap_) {
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001394 DumpVmapData(vios, oat_method, code_item_accessor);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001395 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001396 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001397 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001398 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001399
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001400 ScopedIndentation indent2(vios);
1401 vios->Stream()
1402 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
1403 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
1404 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
1405 vios->Stream() << "\n";
1406 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
1407 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
1408 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001409 }
1410 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001411 // Based on spill masks from QuickMethodFrameInfo so placed
1412 // after it is dumped, but useful for understanding quick
1413 // code, so dumped here.
1414 ScopedIndentation indent2(vios);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001415 DumpVregLocations(vios->Stream(), oat_method, code_item_accessor);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001416 }
1417 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001418 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001419 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
1420 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001421 ScopedIndentation indent2(vios);
1422 vios->Stream() << StringPrintf("WARNING: "
1423 "code size offset 0x%08x is past end of file 0x%08zx.",
1424 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001425 success = false;
1426 } else {
1427 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001428 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1429 uint64_t aligned_code_end = aligned_code_begin + code_size;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001430 stats_.AddBitsIfUnique(Stats::kByteKindCode, code_size * kBitsPerByte, code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001431
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001432 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001433 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001434 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001435 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
1436 code_offset,
1437 code_size_offset,
1438 code_size,
1439 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001440
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001441 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001442 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001443 vios->Stream() << StringPrintf("WARNING: "
1444 "start of code at 0x%08x is past end of file 0x%08zx.",
1445 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001446 success = false;
1447 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001448 vios->Stream() << StringPrintf(
1449 "WARNING: "
1450 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
1451 "code size is 0x%08x loaded from offset 0x%08x.\n",
1452 aligned_code_end, oat_file_.Size(),
1453 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001454 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001455 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001456 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001457 DumpCode(vios, oat_method, code_item_accessor, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001458 }
1459 }
1460 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001461 vios->Stream() << StringPrintf(
1462 "WARNING: "
1463 "code size %d is bigger than max expected threshold of %d. "
1464 "code size is 0x%08x loaded from offset 0x%08x.\n",
1465 code_size, kMaxCodeSize,
1466 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001467 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001468 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001469 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001470 DumpCode(vios, oat_method, code_item_accessor, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001471 }
1472 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001473 } else if (options_.disassemble_code_) {
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001474 DumpCode(vios, oat_method, code_item_accessor, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001475 }
1476 }
1477 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001478 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001479 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001480 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001481
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001482 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1483 if (spill_mask == 0) {
1484 return;
1485 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001486 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001487 for (size_t i = 0; i < 32; i++) {
1488 if ((spill_mask & (1 << i)) != 0) {
1489 if (is_float) {
1490 os << "fr" << i;
1491 } else {
1492 os << "r" << i;
1493 }
1494 spill_mask ^= 1 << i; // clear bit
1495 if (spill_mask != 0) {
1496 os << ", ";
1497 } else {
1498 break;
1499 }
1500 }
1501 }
1502 os << ")";
1503 }
1504
Roland Levillain442b46a2015-02-18 16:54:21 +00001505 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001506 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001507 const OatFile::OatMethod& oat_method,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001508 const CodeItemDataAccessor& code_item_accessor) {
1509 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item_accessor)) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001510 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001511 const void* raw_code_info = oat_method.GetVmapTable();
1512 if (raw_code_info != nullptr) {
1513 CodeInfo code_info(raw_code_info);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001514 DCHECK(code_item_accessor.HasCodeItem());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001515 ScopedIndentation indent1(vios);
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001516 MethodInfo method_info = oat_method.GetOatQuickMethodHeader()->GetOptimizedMethodInfo();
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001517 DumpCodeInfo(vios, code_info, oat_method, code_item_accessor, method_info);
Roland Levillain442b46a2015-02-18 16:54:21 +00001518 }
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001519 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item_accessor)) {
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001520 // We don't encode the size in the table, so just emit that we have quickened
1521 // information.
1522 ScopedIndentation indent(vios);
1523 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001524 } else {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001525 // Otherwise, there is nothing to display.
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001526 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001527 }
1528
1529 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001530 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001531 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001532 const OatFile::OatMethod& oat_method,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001533 const CodeItemDataAccessor& code_item_accessor,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001534 const MethodInfo& method_info) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001535 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001536 oat_method.GetCodeOffset(),
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001537 code_item_accessor.RegistersSize(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001538 options_.dump_code_info_stack_maps_,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001539 instruction_set_,
1540 method_info);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001541 }
1542
Andreas Gampe36a296f2017-06-13 14:11:11 -07001543 static int GetOutVROffset(uint16_t out_num, InstructionSet isa) {
1544 // According to stack model, the first out is above the Method referernce.
1545 return static_cast<size_t>(InstructionSetPointerSize(isa)) + out_num * sizeof(uint32_t);
1546 }
1547
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001548 static uint32_t GetVRegOffsetFromQuickCode(const CodeItemDataAccessor& code_item_accessor,
Andreas Gampe36a296f2017-06-13 14:11:11 -07001549 uint32_t core_spills,
1550 uint32_t fp_spills,
1551 size_t frame_size,
1552 int reg,
1553 InstructionSet isa) {
1554 PointerSize pointer_size = InstructionSetPointerSize(isa);
1555 if (kIsDebugBuild) {
1556 auto* runtime = Runtime::Current();
1557 if (runtime != nullptr) {
1558 CHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), pointer_size);
1559 }
1560 }
1561 DCHECK_ALIGNED(frame_size, kStackAlignment);
1562 DCHECK_NE(reg, -1);
1563 int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa)
1564 + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa)
1565 + sizeof(uint32_t); // Filler.
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001566 int num_regs = code_item_accessor.RegistersSize() - code_item_accessor.InsSize();
1567 int temp_threshold = code_item_accessor.RegistersSize();
Andreas Gampe36a296f2017-06-13 14:11:11 -07001568 const int max_num_special_temps = 1;
1569 if (reg == temp_threshold) {
1570 // The current method pointer corresponds to special location on stack.
1571 return 0;
1572 } else if (reg >= temp_threshold + max_num_special_temps) {
1573 /*
1574 * Special temporaries may have custom locations and the logic above deals with that.
1575 * However, non-special temporaries are placed relative to the outs.
1576 */
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001577 int temps_start = code_item_accessor.OutsSize() * sizeof(uint32_t)
Andreas Gampe36a296f2017-06-13 14:11:11 -07001578 + static_cast<size_t>(pointer_size) /* art method */;
1579 int relative_offset = (reg - (temp_threshold + max_num_special_temps)) * sizeof(uint32_t);
1580 return temps_start + relative_offset;
1581 } else if (reg < num_regs) {
1582 int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t);
1583 return locals_start + (reg * sizeof(uint32_t));
1584 } else {
1585 // Handle ins.
1586 return frame_size + ((reg - num_regs) * sizeof(uint32_t))
1587 + static_cast<size_t>(pointer_size) /* art method */;
1588 }
1589 }
1590
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001591 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001592 const CodeItemDataAccessor& code_item_accessor) {
1593 if (code_item_accessor.HasCodeItem()) {
1594 size_t num_locals_ins = code_item_accessor.RegistersSize();
1595 size_t num_ins = code_item_accessor.InsSize();
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001596 size_t num_locals = num_locals_ins - num_ins;
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001597 size_t num_outs = code_item_accessor.OutsSize();
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001598
1599 os << "vr_stack_locations:";
1600 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1601 // For readability, delimit the different kinds of VRs.
1602 if (reg == num_locals_ins) {
1603 os << "\n\tmethod*:";
1604 } else if (reg == num_locals && num_ins > 0) {
1605 os << "\n\tins:";
1606 } else if (reg == 0 && num_locals > 0) {
1607 os << "\n\tlocals:";
1608 }
1609
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001610 uint32_t offset = GetVRegOffsetFromQuickCode(code_item_accessor,
Andreas Gampe36a296f2017-06-13 14:11:11 -07001611 oat_method.GetCoreSpillMask(),
1612 oat_method.GetFpSpillMask(),
1613 oat_method.GetFrameSizeInBytes(),
1614 reg,
1615 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001616 os << " v" << reg << "[sp + #" << offset << "]";
1617 }
1618
1619 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1620 if (out_reg == 0) {
1621 os << "\n\touts:";
1622 }
1623
Andreas Gampe36a296f2017-06-13 14:11:11 -07001624 uint32_t offset = GetOutVROffset(out_reg, GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001625 os << " v" << out_reg << "[sp + #" << offset << "]";
1626 }
1627
1628 os << "\n";
1629 }
1630 }
1631
Roland Levillainf2650d12015-05-28 14:53:28 +01001632 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1633 // the optimizing compiler?
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001634 static bool IsMethodGeneratedByOptimizingCompiler(
1635 const OatFile::OatMethod& oat_method,
1636 const CodeItemDataAccessor& code_item_accessor) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001637 // If the native GC map is null and the Dex `code_item` is not
1638 // null, then this method has been compiled with the optimizing
1639 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001640 return oat_method.GetQuickCode() != nullptr &&
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001641 oat_method.GetVmapTable() != nullptr &&
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001642 code_item_accessor.HasCodeItem();
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001643 }
1644
1645 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1646 // the dextodex compiler?
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001647 static bool IsMethodGeneratedByDexToDexCompiler(
1648 const OatFile::OatMethod& oat_method,
1649 const CodeItemDataAccessor& code_item_accessor) {
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001650 // If the quick code is null, the Dex `code_item` is not
1651 // null, and the vmap table is not null, then this method has been compiled
1652 // with the dextodex compiler.
1653 return oat_method.GetQuickCode() == nullptr &&
1654 oat_method.GetVmapTable() != nullptr &&
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001655 code_item_accessor.HasCodeItem();
Roland Levillainf2650d12015-05-28 14:53:28 +01001656 }
1657
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001658 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001659 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001660 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001661 const DexFile* dex_file,
1662 const DexFile::ClassDef& class_def,
1663 const DexFile::CodeItem* code_item,
1664 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001665 if ((method_access_flags & kAccNative) == 0) {
1666 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001667 Runtime* const runtime = Runtime::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001668 Handle<mirror::DexCache> dex_cache(
Mathieu Chartierf284d442016-06-02 11:48:30 -07001669 hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file, nullptr)));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001670 CHECK(dex_cache != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001671 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001672 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001673 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
David Brazdil15fc7292016-09-02 14:13:18 +01001674 class_def, code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001675 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001676
1677 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001678 }
1679
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001680 // The StackMapsHelper provides the stack maps in the native PC order.
1681 // For identical native PCs, the order from the CodeInfo is preserved.
1682 class StackMapsHelper {
1683 public:
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001684 explicit StackMapsHelper(const uint8_t* raw_code_info, InstructionSet instruction_set)
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001685 : code_info_(raw_code_info),
1686 encoding_(code_info_.ExtractEncoding()),
1687 number_of_stack_maps_(code_info_.GetNumberOfStackMaps(encoding_)),
1688 indexes_(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001689 offset_(static_cast<uint32_t>(-1)),
1690 stack_map_index_(0u),
1691 instruction_set_(instruction_set) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001692 if (number_of_stack_maps_ != 0u) {
1693 // Check if native PCs are ordered.
1694 bool ordered = true;
1695 StackMap last = code_info_.GetStackMapAt(0u, encoding_);
1696 for (size_t i = 1; i != number_of_stack_maps_; ++i) {
1697 StackMap current = code_info_.GetStackMapAt(i, encoding_);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001698 if (last.GetNativePcOffset(encoding_.stack_map.encoding, instruction_set) >
1699 current.GetNativePcOffset(encoding_.stack_map.encoding, instruction_set)) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001700 ordered = false;
1701 break;
1702 }
1703 last = current;
1704 }
1705 if (!ordered) {
1706 // Create indirection indexes for access in native PC order. We do not optimize
1707 // for the fact that there can currently be only two separately ordered ranges,
1708 // namely normal stack maps and catch-point stack maps.
1709 indexes_.resize(number_of_stack_maps_);
1710 std::iota(indexes_.begin(), indexes_.end(), 0u);
1711 std::sort(indexes_.begin(),
1712 indexes_.end(),
1713 [this](size_t lhs, size_t rhs) {
1714 StackMap left = code_info_.GetStackMapAt(lhs, encoding_);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001715 uint32_t left_pc = left.GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001716 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001717 StackMap right = code_info_.GetStackMapAt(rhs, encoding_);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001718 uint32_t right_pc = right.GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001719 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001720 // If the PCs are the same, compare indexes to preserve the original order.
1721 return (left_pc < right_pc) || (left_pc == right_pc && lhs < rhs);
1722 });
1723 }
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001724 offset_ = GetStackMapAt(0).GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001725 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001726 }
1727 }
1728
1729 const CodeInfo& GetCodeInfo() const {
1730 return code_info_;
1731 }
1732
1733 const CodeInfoEncoding& GetEncoding() const {
1734 return encoding_;
1735 }
1736
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001737 uint32_t GetOffset() const {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001738 return offset_;
1739 }
1740
1741 StackMap GetStackMap() const {
1742 return GetStackMapAt(stack_map_index_);
1743 }
1744
1745 void Next() {
1746 ++stack_map_index_;
1747 offset_ = (stack_map_index_ == number_of_stack_maps_)
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001748 ? static_cast<uint32_t>(-1)
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001749 : GetStackMapAt(stack_map_index_).GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001750 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001751 }
1752
1753 private:
1754 StackMap GetStackMapAt(size_t i) const {
1755 if (!indexes_.empty()) {
1756 i = indexes_[i];
1757 }
1758 DCHECK_LT(i, number_of_stack_maps_);
1759 return code_info_.GetStackMapAt(i, encoding_);
1760 }
1761
1762 const CodeInfo code_info_;
1763 const CodeInfoEncoding encoding_;
1764 const size_t number_of_stack_maps_;
1765 dchecked_vector<size_t> indexes_; // Used if stack map native PCs are not ordered.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001766 uint32_t offset_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001767 size_t stack_map_index_;
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001768 const InstructionSet instruction_set_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001769 };
1770
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001771 void DumpCode(VariableIndentationOutputStream* vios,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001772 const OatFile::OatMethod& oat_method,
1773 const CodeItemDataAccessor& code_item_accessor,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001774 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001775 const void* quick_code = oat_method.GetQuickCode();
1776
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001777 if (code_size == 0) {
1778 code_size = oat_method.GetQuickCodeSize();
1779 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001780 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001781 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001782 return;
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001783 } else if (!bad_input && IsMethodGeneratedByOptimizingCompiler(oat_method,
1784 code_item_accessor)) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001785 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001786 StackMapsHelper helper(oat_method.GetVmapTable(), instruction_set_);
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001787 MethodInfo method_info(oat_method.GetOatQuickMethodHeader()->GetOptimizedMethodInfo());
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001788 {
1789 CodeInfoEncoding encoding(helper.GetEncoding());
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001790 StackMapEncoding stack_map_encoding(encoding.stack_map.encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001791 const size_t num_stack_maps = encoding.stack_map.num_entries;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001792 if (stats_.AddBitsIfUnique(Stats::kByteKindCodeInfoEncoding,
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001793 encoding.HeaderSize() * kBitsPerByte,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001794 oat_method.GetVmapTable())) {
Mathieu Chartier1e083792017-02-08 13:30:04 -08001795 // Stack maps
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001796 stats_.AddBits(
1797 Stats::kByteKindStackMapNativePc,
1798 stack_map_encoding.GetNativePcEncoding().BitSize() * num_stack_maps);
1799 stats_.AddBits(
1800 Stats::kByteKindStackMapDexPc,
1801 stack_map_encoding.GetDexPcEncoding().BitSize() * num_stack_maps);
1802 stats_.AddBits(
1803 Stats::kByteKindStackMapDexRegisterMap,
1804 stack_map_encoding.GetDexRegisterMapEncoding().BitSize() * num_stack_maps);
1805 stats_.AddBits(
Mathieu Chartier1e083792017-02-08 13:30:04 -08001806 Stats::kByteKindStackMapInlineInfoIndex,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001807 stack_map_encoding.GetInlineInfoEncoding().BitSize() * num_stack_maps);
1808 stats_.AddBits(
Mathieu Chartier1a20b682017-01-31 14:25:16 -08001809 Stats::kByteKindStackMapRegisterMaskIndex,
1810 stack_map_encoding.GetRegisterMaskIndexEncoding().BitSize() * num_stack_maps);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001811 stats_.AddBits(
David Srbecky45aa5982016-03-18 02:15:09 +00001812 Stats::kByteKindStackMapStackMaskIndex,
1813 stack_map_encoding.GetStackMaskIndexEncoding().BitSize() * num_stack_maps);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001814
1815 // Stack masks
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001816 stats_.AddBits(
David Srbecky45aa5982016-03-18 02:15:09 +00001817 Stats::kByteKindCodeInfoStackMasks,
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001818 encoding.stack_mask.encoding.BitSize() * encoding.stack_mask.num_entries);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001819
1820 // Register masks
Mathieu Chartier1a20b682017-01-31 14:25:16 -08001821 stats_.AddBits(
1822 Stats::kByteKindCodeInfoRegisterMasks,
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001823 encoding.register_mask.encoding.BitSize() * encoding.register_mask.num_entries);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001824
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001825 // Invoke infos
1826 if (encoding.invoke_info.num_entries > 0u) {
1827 stats_.AddBits(
1828 Stats::kByteKindCodeInfoInvokeInfo,
1829 encoding.invoke_info.encoding.BitSize() * encoding.invoke_info.num_entries);
1830 }
1831
Mathieu Chartier1e083792017-02-08 13:30:04 -08001832 // Location catalog
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001833 const size_t location_catalog_bytes =
1834 helper.GetCodeInfo().GetDexRegisterLocationCatalogSize(encoding);
1835 stats_.AddBits(Stats::kByteKindCodeInfoLocationCatalog,
1836 kBitsPerByte * location_catalog_bytes);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001837 // Dex register bytes.
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001838 const size_t dex_register_bytes =
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001839 helper.GetCodeInfo().GetDexRegisterMapsSize(encoding,
1840 code_item_accessor.RegistersSize());
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001841 stats_.AddBits(
1842 Stats::kByteKindCodeInfoDexRegisterMap,
1843 kBitsPerByte * dex_register_bytes);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001844
1845 // Inline infos.
1846 const size_t num_inline_infos = encoding.inline_info.num_entries;
1847 if (num_inline_infos > 0u) {
1848 stats_.AddBits(
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001849 Stats::kByteKindInlineInfoMethodIndexIdx,
1850 encoding.inline_info.encoding.GetMethodIndexIdxEncoding().BitSize() *
1851 num_inline_infos);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001852 stats_.AddBits(
1853 Stats::kByteKindInlineInfoDexPc,
1854 encoding.inline_info.encoding.GetDexPcEncoding().BitSize() * num_inline_infos);
1855 stats_.AddBits(
1856 Stats::kByteKindInlineInfoExtraData,
1857 encoding.inline_info.encoding.GetExtraDataEncoding().BitSize() * num_inline_infos);
1858 stats_.AddBits(
1859 Stats::kByteKindInlineInfoDexRegisterMap,
1860 encoding.inline_info.encoding.GetDexRegisterMapEncoding().BitSize() *
1861 num_inline_infos);
1862 stats_.AddBits(Stats::kByteKindInlineInfoIsLast, num_inline_infos);
1863 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001864 }
1865 }
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001866 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1867 size_t offset = 0;
1868 while (offset < code_size) {
1869 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
1870 if (offset == helper.GetOffset()) {
1871 ScopedIndentation indent1(vios);
1872 StackMap stack_map = helper.GetStackMap();
1873 DCHECK(stack_map.IsValid());
1874 stack_map.Dump(vios,
1875 helper.GetCodeInfo(),
1876 helper.GetEncoding(),
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001877 method_info,
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001878 oat_method.GetCodeOffset(),
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001879 code_item_accessor.RegistersSize(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001880 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001881 do {
1882 helper.Next();
1883 // There may be multiple stack maps at a given PC. We display only the first one.
1884 } while (offset == helper.GetOffset());
1885 }
1886 DCHECK_LT(offset, helper.GetOffset());
1887 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001888 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001889 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1890 size_t offset = 0;
1891 while (offset < code_size) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001892 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001893 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001894 }
1895 }
1896
Vladimir Markof3c52b42017-11-17 17:32:12 +00001897 template <typename NameGetter>
1898 void DumpBssEntries(std::ostream& os,
1899 const char* slot_type,
1900 const IndexBssMapping* mapping,
1901 uint32_t number_of_indexes,
1902 size_t slot_size,
1903 NameGetter name) {
1904 os << ".bss mapping for " << slot_type << ": ";
1905 if (mapping == nullptr) {
1906 os << "empty.\n";
1907 return;
1908 }
1909 size_t index_bits = IndexBssMappingEntry::IndexBits(number_of_indexes);
1910 size_t num_valid_indexes = 0u;
1911 for (const IndexBssMappingEntry& entry : *mapping) {
1912 num_valid_indexes += 1u + POPCOUNT(entry.GetMask(index_bits));
1913 }
1914 os << mapping->size() << " entries for " << num_valid_indexes << " valid indexes.\n";
1915 os << std::hex;
1916 for (const IndexBssMappingEntry& entry : *mapping) {
1917 uint32_t index = entry.GetIndex(index_bits);
1918 uint32_t mask = entry.GetMask(index_bits);
1919 size_t bss_offset = entry.bss_offset - POPCOUNT(mask) * slot_size;
1920 for (uint32_t n : LowToHighBits(mask)) {
1921 size_t current_index = index - (32u - index_bits) + n;
1922 os << " 0x" << bss_offset << ": " << slot_type << ": " << name(current_index) << "\n";
1923 bss_offset += slot_size;
1924 }
1925 DCHECK_EQ(bss_offset, entry.bss_offset);
1926 os << " 0x" << bss_offset << ": " << slot_type << ": " << name(index) << "\n";
1927 }
1928 os << std::dec;
1929 }
1930
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001931 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001932 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001933 const OatDumperOptions& options_;
1934 uint32_t resolved_addr2instr_;
Andreas Gampe372f3a32016-08-19 10:49:06 -07001935 const InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001936 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001937 Disassembler* disassembler_;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001938 Stats stats_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001939};
1940
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001941class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001942 public:
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001943 ImageDumper(std::ostream* os,
1944 gc::space::ImageSpace& image_space,
1945 const ImageHeader& image_header,
1946 OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001947 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001948 vios_(os),
1949 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001950 image_space_(image_space),
1951 image_header_(image_header),
1952 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001953
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001954 bool Dump() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001955 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001956 std::ostream& indent_os = vios_.Stream();
1957
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001958 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001959
Jeff Haodcdc85b2015-12-04 14:06:18 -08001960 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1961
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001962 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001963
Mathieu Chartiere401d142015-04-22 13:56:20 -07001964 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1965
1966 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1967 auto section = static_cast<ImageHeader::ImageSections>(i);
1968 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1969 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001970
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001971 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001972
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001973 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001974
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001975 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1976
1977 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1978
1979 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001980
Alex Lighta59dd802014-07-02 16:28:08 -07001981 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1982
Igor Murashkin46774762014-10-22 11:37:02 -07001983 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1984
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001985 {
1986 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001987 static_assert(arraysize(image_roots_descriptions_) ==
1988 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001989 DCHECK_LE(image_header_.GetImageRoots()->GetLength(), ImageHeader::kImageRootsMax);
1990 for (int32_t i = 0, size = image_header_.GetImageRoots()->GetLength(); i != size; ++i) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001991 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1992 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001993 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001994 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001995 if (image_root_object != nullptr && image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001996 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001997 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001998 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001999 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
2000 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08002001 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002002 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
2003 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08002004 run++;
2005 } else {
2006 break;
2007 }
2008 }
2009 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002010 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08002011 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002012 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002013 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08002014 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002015 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002016 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002017 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002018 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002019 }
Ian Rogersd5b32602012-02-26 16:40:04 -08002020 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07002021 }
2022 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002023 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002024
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002025 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002026 os << "METHOD ROOTS\n";
2027 static_assert(arraysize(image_methods_descriptions_) ==
2028 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
2029 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
2030 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
2031 const char* description = image_methods_descriptions_[i];
2032 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002033 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002034 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002035 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002036 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002037
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07002038 Runtime* const runtime = Runtime::Current();
2039 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002040 std::string image_filename = image_space_.GetImageFilename();
2041 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002042 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002043 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07002044 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002045 const OatFile* oat_file = image_space_.GetOatFile();
Alex Lighta59dd802014-07-02 16:28:08 -07002046 if (oat_file == nullptr) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002047 oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location);
2048 }
2049 if (oat_file == nullptr) {
2050 oat_file = OatFile::Open(oat_location,
2051 oat_location,
2052 nullptr,
2053 nullptr,
2054 false,
2055 /*low_4gb*/false,
2056 nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07002057 &error_msg);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002058 }
2059 if (oat_file == nullptr) {
2060 os << "OAT FILE NOT FOUND: " << error_msg << "\n";
2061 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07002062 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002063 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07002064
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002065 stats_.oat_file_bytes = oat_file->Size();
2066
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002067 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002068
Mathieu Chartier02e25112013-08-14 16:14:24 -07002069 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002070 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07002071 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
2072 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07002073 }
2074
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002075 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07002076
Jeff Haodcdc85b2015-12-04 14:06:18 -08002077 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07002078 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07002079 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07002080 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08002081 {
2082 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
2083 heap->FlushAllocStack();
2084 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08002085 // Since FlushAllocStack() above resets the (active) allocation
2086 // stack. Need to revoke the thread-local allocation stacks that
2087 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07002088 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07002089 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07002090 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07002091 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002092 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002093 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01002094 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002095 {
Andreas Gampecc1b5352016-12-01 16:58:38 -08002096 ReaderMutexLock mu(self, *Locks::dex_lock_);
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08002097 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07002098 ObjPtr<mirror::DexCache> dex_cache =
2099 ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07002100 if (dex_cache != nullptr) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07002101 dex_caches_.insert(dex_cache.Ptr());
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07002102 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002103 }
2104 }
Andreas Gampe0c183382017-07-13 22:26:24 -07002105 auto dump_visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
2106 DumpObject(obj);
2107 };
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002108 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08002109 // Dump the normal objects before ArtMethods.
Andreas Gampe0c183382017-07-13 22:26:24 -07002110 image_space_.GetLiveBitmap()->Walk(dump_visitor);
Jeff Haodcdc85b2015-12-04 14:06:18 -08002111 indent_os << "\n";
2112 // TODO: Dump fields.
2113 // Dump methods after.
Jeff Haodcdc85b2015-12-04 14:06:18 -08002114 DumpArtMethodVisitor visitor(this);
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002115 image_header_.VisitPackedArtMethods(&visitor,
2116 image_space_.Begin(),
2117 image_header_.GetPointerSize());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002118 // Dump the large objects separately.
Andreas Gampe0c183382017-07-13 22:26:24 -07002119 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(dump_visitor);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002120 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07002121 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002122 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07002123 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002124 size_t data_size = image_header_.GetDataSize(); // stored size in file.
2125 if (file == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002126 LOG(WARNING) << "Failed to find image in " << image_filename;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002127 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002128 stats_.file_bytes = file->GetLength();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002129 // If the image is compressed, adjust to decompressed size.
2130 size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader);
2131 if (image_header_.GetStorageMode() == ImageHeader::kStorageModeUncompressed) {
2132 DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image";
2133 }
2134 stats_.file_bytes += uncompressed_size - data_size;
Brian Carlstrom6f277752013-09-30 17:56:45 -07002135 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002136 size_t header_bytes = sizeof(ImageHeader);
Vladimir Markocd87c3e2017-09-05 13:11:57 +01002137 const auto& object_section = image_header_.GetObjectsSection();
2138 const auto& field_section = image_header_.GetFieldsSection();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002139 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Markocd87c3e2017-09-05 13:11:57 +01002140 const auto& dex_cache_arrays_section = image_header_.GetDexCacheArraysSection();
2141 const auto& intern_section = image_header_.GetInternedStringsSection();
2142 const auto& class_table_section = image_header_.GetClassTableSection();
2143 const auto& bitmap_section = image_header_.GetImageBitmapSection();
Andreas Gampeace0dc12016-01-20 13:33:13 -08002144
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002145 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08002146
2147 // Objects are kObjectAlignment-aligned.
2148 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
2149 if (object_section.Offset() > header_bytes) {
2150 stats_.alignment_bytes += object_section.Offset() - header_bytes;
2151 }
2152
2153 // Field section is 4-byte aligned.
2154 constexpr size_t kFieldSectionAlignment = 4U;
2155 uint32_t end_objects = object_section.Offset() + object_section.Size();
2156 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
2157 stats_.alignment_bytes += field_section.Offset() - end_objects;
2158
2159 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
2160 uint32_t end_fields = field_section.Offset() + field_section.Size();
2161 CHECK_ALIGNED(method_section.Offset(), 4);
2162 stats_.alignment_bytes += method_section.Offset() - end_fields;
2163
2164 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
2165 uint32_t end_methods = method_section.Offset() + method_section.Size();
2166 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
2167 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
2168
2169 // Intern table is 8-byte aligned.
2170 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01002171 CHECK_ALIGNED(intern_section.Offset(), sizeof(uint64_t));
Andreas Gampeace0dc12016-01-20 13:33:13 -08002172 stats_.alignment_bytes += intern_section.Offset() - end_caches;
2173
2174 // Add space between intern table and class table.
2175 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
2176 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
2177
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002178 // Add space between end of image data and bitmap. Expect the bitmap to be page-aligned.
2179 const size_t bitmap_offset = sizeof(ImageHeader) + data_size;
Andreas Gampeace0dc12016-01-20 13:33:13 -08002180 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002181 stats_.alignment_bytes += RoundUp(bitmap_offset, kPageSize) - bitmap_offset;
Andreas Gampeace0dc12016-01-20 13:33:13 -08002182
Mathieu Chartiere401d142015-04-22 13:56:20 -07002183 stats_.bitmap_bytes += bitmap_section.Size();
2184 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01002185 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01002186 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002187 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002188 stats_.class_table_bytes += class_table_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002189 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002190 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002191
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002192 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002193
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002194 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002195 }
2196
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002197 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002198 class DumpArtMethodVisitor : public ArtMethodVisitor {
2199 public:
2200 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
2201
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002202 virtual void Visit(ArtMethod* method) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002203 std::ostream& indent_os = image_dumper_->vios_.Stream();
David Sehr709b0702016-10-13 09:12:37 -07002204 indent_os << method << " " << " ArtMethod: " << ArtMethod::PrettyMethod(method) << "\n";
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002205 image_dumper_->DumpMethod(method, indent_os);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002206 indent_os << "\n";
2207 }
2208
2209 private:
2210 ImageDumper* const image_dumper_;
2211 };
2212
Mathieu Chartier3398c782016-09-30 10:27:43 -07002213 static void PrettyObjectValue(std::ostream& os,
2214 ObjPtr<mirror::Class> type,
2215 ObjPtr<mirror::Object> value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002216 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002217 CHECK(type != nullptr);
2218 if (value == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07002219 os << StringPrintf("null %s\n", type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002220 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002221 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002222 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07002223 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07002224 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002225 mirror::Class* klass = value->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07002226 os << StringPrintf("%p Class: %s\n", klass, mirror::Class::PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002227 } else {
David Sehr709b0702016-10-13 09:12:37 -07002228 os << StringPrintf("%p %s\n", value.Ptr(), type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002229 }
2230 }
2231
Mathieu Chartier3398c782016-09-30 10:27:43 -07002232 static void PrintField(std::ostream& os, ArtField* field, ObjPtr<mirror::Object> obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002233 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07002234 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08002235 switch (field->GetTypeAsPrimitiveType()) {
2236 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08002237 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002238 break;
2239 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002240 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002241 break;
2242 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002243 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002244 break;
2245 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002246 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002247 break;
2248 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07002249 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002250 break;
2251 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07002252 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002253 break;
2254 case Primitive::kPrimBoolean:
Roland Levillain5e8d5f02016-10-18 18:03:43 +01002255 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj) ? "true" : "false",
Fred Shih37f05ef2014-07-16 18:38:08 -07002256 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002257 break;
2258 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07002259 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002260 break;
2261 case Primitive::kPrimNot: {
2262 // Get the value, don't compute the type unless it is non-null as we don't want
2263 // to cause class loading.
Mathieu Chartier3398c782016-09-30 10:27:43 -07002264 ObjPtr<mirror::Object> value = field->GetObj(obj);
Ian Rogers08f1f502014-12-02 15:04:37 -08002265 if (value == nullptr) {
2266 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07002267 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08002268 // Grab the field type without causing resolution.
Vladimir Marko208f6702017-12-08 12:00:50 +00002269 ObjPtr<mirror::Class> field_type = field->LookupResolvedType();
Ian Rogers08f1f502014-12-02 15:04:37 -08002270 if (field_type != nullptr) {
2271 PrettyObjectValue(os, field_type, value);
2272 } else {
Mathieu Chartier3398c782016-09-30 10:27:43 -07002273 os << StringPrintf("%p %s\n",
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07002274 value.Ptr(),
Ian Rogers08f1f502014-12-02 15:04:37 -08002275 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
2276 }
Ian Rogers50239c72013-06-17 14:53:22 -07002277 }
Ian Rogers08f1f502014-12-02 15:04:37 -08002278 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07002279 }
Ian Rogers08f1f502014-12-02 15:04:37 -08002280 default:
2281 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
2282 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08002283 }
2284 }
2285
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002286 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002287 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002288 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002289 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002290 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08002291 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002292 for (ArtField& field : klass->GetIFields()) {
2293 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08002294 }
2295 }
2296
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002297 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002298 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002299 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002300
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002301 const void* GetQuickOatCodeBegin(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08002302 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002303 image_header_.GetPointerSize());
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002304 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002305 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002306 }
Vladimir Marko33bff252017-11-01 14:35:42 +00002307 if (oat_dumper_->GetInstructionSet() == InstructionSet::kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002308 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002309 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002310 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002311 }
2312
Mathieu Chartiere401d142015-04-22 13:56:20 -07002313 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002314 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002315 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
2316 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002317 return 0;
2318 }
2319 return oat_code_begin[-1];
2320 }
2321
Mathieu Chartiere401d142015-04-22 13:56:20 -07002322 const void* GetQuickOatCodeEnd(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002323 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002324 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002325 if (oat_code_begin == nullptr) {
2326 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002327 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002328 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002329 }
2330
Andreas Gampe0c183382017-07-13 22:26:24 -07002331 void DumpObject(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002332 DCHECK(obj != nullptr);
Andreas Gampe0c183382017-07-13 22:26:24 -07002333 if (!InDumpSpace(obj)) {
Brian Carlstrom78128a62011-09-15 17:21:19 -07002334 return;
2335 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002336
2337 size_t object_bytes = obj->SizeOf();
2338 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
Andreas Gampe0c183382017-07-13 22:26:24 -07002339 stats_.object_bytes += object_bytes;
2340 stats_.alignment_bytes += alignment_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002341
Andreas Gampe0c183382017-07-13 22:26:24 -07002342 std::ostream& os = vios_.Stream();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002343
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002344 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08002345 if (obj_class->IsArrayClass()) {
David Sehr709b0702016-10-13 09:12:37 -07002346 os << StringPrintf("%p: %s length:%d\n", obj, obj_class->PrettyDescriptor().c_str(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002347 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08002348 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002349 mirror::Class* klass = obj->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07002350 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj,
2351 mirror::Class::PrettyDescriptor(klass).c_str())
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002352 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08002353 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002354 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07002355 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002356 } else {
David Sehr709b0702016-10-13 09:12:37 -07002357 os << StringPrintf("%p: %s\n", obj, obj_class->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002358 }
Andreas Gampe0c183382017-07-13 22:26:24 -07002359 ScopedIndentation indent1(&vios_);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002360 DumpFields(os, obj, obj_class);
Andreas Gampe0c183382017-07-13 22:26:24 -07002361 const PointerSize image_pointer_size = image_header_.GetPointerSize();
Ian Rogersd5b32602012-02-26 16:40:04 -08002362 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002363 auto* obj_array = obj->AsObjectArray<mirror::Object>();
2364 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002365 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002366 size_t run = 0;
2367 for (int32_t j = i + 1; j < length; j++) {
2368 if (value == obj_array->Get(j)) {
2369 run++;
2370 } else {
2371 break;
2372 }
2373 }
2374 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002375 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002376 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002377 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08002378 i = i + run;
2379 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002380 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002381 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002382 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08002383 }
2384 } else if (obj->IsClass()) {
Vladimir Marko38b8b252018-01-02 19:07:06 +00002385 ObjPtr<mirror::Class> klass = obj->AsClass();
Igor Murashkin86083f72017-10-27 10:59:04 -07002386
2387 os << "SUBTYPE_CHECK_BITS: ";
Vladimir Marko38b8b252018-01-02 19:07:06 +00002388 SubtypeCheck<ObjPtr<mirror::Class>>::Dump(klass, os);
Igor Murashkin86083f72017-10-27 10:59:04 -07002389 os << "\n";
2390
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002391 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002392 os << "STATICS:\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002393 ScopedIndentation indent2(&vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002394 for (ArtField& field : klass->GetSFields()) {
2395 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08002396 }
2397 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002398 } else {
Andreas Gampe0c183382017-07-13 22:26:24 -07002399 auto it = dex_caches_.find(obj);
2400 if (it != dex_caches_.end()) {
Vladimir Marko05792b92015-08-03 11:56:49 +01002401 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Vladimir Markocd87c3e2017-09-05 13:11:57 +01002402 const auto& field_section = image_header_.GetFieldsSection();
Andreas Gampe0c183382017-07-13 22:26:24 -07002403 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01002404 size_t num_methods = dex_cache->NumResolvedMethods();
2405 if (num_methods != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002406 os << "Methods (size=" << num_methods << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002407 ScopedIndentation indent2(&vios_);
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002408 mirror::MethodDexCacheType* resolved_methods = dex_cache->GetResolvedMethods();
Vladimir Marko05792b92015-08-03 11:56:49 +01002409 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002410 ArtMethod* elem = mirror::DexCache::GetNativePairPtrSize(
2411 resolved_methods, i, image_pointer_size).object;
Vladimir Marko05792b92015-08-03 11:56:49 +01002412 size_t run = 0;
2413 for (size_t j = i + 1;
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002414 j != length &&
2415 elem == mirror::DexCache::GetNativePairPtrSize(
2416 resolved_methods, j, image_pointer_size).object;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002417 ++j) {
2418 ++run;
2419 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002420 if (run == 0) {
2421 os << StringPrintf("%zd: ", i);
2422 } else {
2423 os << StringPrintf("%zd to %zd: ", i, i + run);
2424 i = i + run;
2425 }
2426 std::string msg;
2427 if (elem == nullptr) {
2428 msg = "null";
2429 } else if (method_section.Contains(
Andreas Gampe0c183382017-07-13 22:26:24 -07002430 reinterpret_cast<uint8_t*>(elem) - image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002431 msg = reinterpret_cast<ArtMethod*>(elem)->PrettyMethod();
Vladimir Marko05792b92015-08-03 11:56:49 +01002432 } else {
2433 msg = "<not in method section>";
2434 }
2435 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07002436 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002437 }
2438 size_t num_fields = dex_cache->NumResolvedFields();
2439 if (num_fields != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002440 os << "Fields (size=" << num_fields << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002441 ScopedIndentation indent2(&vios_);
Vladimir Marko05792b92015-08-03 11:56:49 +01002442 auto* resolved_fields = dex_cache->GetResolvedFields();
2443 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002444 ArtField* elem = mirror::DexCache::GetNativePairPtrSize(
Vladimir Markof44d36c2017-03-14 14:18:46 +00002445 resolved_fields, i, image_pointer_size).object;
Vladimir Marko05792b92015-08-03 11:56:49 +01002446 size_t run = 0;
2447 for (size_t j = i + 1;
Vladimir Markof44d36c2017-03-14 14:18:46 +00002448 j != length &&
2449 elem == mirror::DexCache::GetNativePairPtrSize(
2450 resolved_fields, j, image_pointer_size).object;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002451 ++j) {
2452 ++run;
2453 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002454 if (run == 0) {
2455 os << StringPrintf("%zd: ", i);
2456 } else {
2457 os << StringPrintf("%zd to %zd: ", i, i + run);
2458 i = i + run;
2459 }
2460 std::string msg;
2461 if (elem == nullptr) {
2462 msg = "null";
2463 } else if (field_section.Contains(
Andreas Gampe0c183382017-07-13 22:26:24 -07002464 reinterpret_cast<uint8_t*>(elem) - image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002465 msg = reinterpret_cast<ArtField*>(elem)->PrettyField();
Vladimir Marko05792b92015-08-03 11:56:49 +01002466 } else {
2467 msg = "<not in field section>";
2468 }
2469 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002470 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002471 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002472 size_t num_types = dex_cache->NumResolvedTypes();
2473 if (num_types != 0u) {
2474 os << "Types (size=" << num_types << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002475 ScopedIndentation indent2(&vios_);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002476 auto* resolved_types = dex_cache->GetResolvedTypes();
2477 for (size_t i = 0; i < num_types; ++i) {
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002478 auto pair = resolved_types[i].load(std::memory_order_relaxed);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002479 size_t run = 0;
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002480 for (size_t j = i + 1; j != num_types; ++j) {
2481 auto other_pair = resolved_types[j].load(std::memory_order_relaxed);
2482 if (pair.index != other_pair.index ||
2483 pair.object.Read() != other_pair.object.Read()) {
2484 break;
2485 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002486 ++run;
2487 }
2488 if (run == 0) {
2489 os << StringPrintf("%zd: ", i);
2490 } else {
2491 os << StringPrintf("%zd to %zd: ", i, i + run);
2492 i = i + run;
2493 }
2494 std::string msg;
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002495 auto* elem = pair.object.Read();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002496 if (elem == nullptr) {
2497 msg = "null";
2498 } else {
David Sehr709b0702016-10-13 09:12:37 -07002499 msg = elem->PrettyClass();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002500 }
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002501 os << StringPrintf("%p %u %s\n", elem, pair.index, msg.c_str());
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002502 }
2503 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002504 }
2505 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002506 std::string temp;
Andreas Gampe0c183382017-07-13 22:26:24 -07002507 stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002508 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002509
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002510 void DumpMethod(ArtMethod* method, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002511 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002512 DCHECK(method != nullptr);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002513 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
2514 const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
Andreas Gampe542451c2016-07-26 09:02:02 -07002515 const PointerSize pointer_size = image_header_.GetPointerSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002516 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2517 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002518 if (method->IsNative()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002519 bool first_occurrence;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002520 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2521 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002522 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002523 stats_.native_to_managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002524 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002525 if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
2526 image_header_.GetPointerSize())) {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002527 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002528 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002529 } else if (method->IsAbstract() || method->IsClassInitializer()) {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002530 // Don't print information for these.
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002531 } else if (method->IsRuntimeMethod()) {
2532 ImtConflictTable* table = method->GetImtConflictTable(image_header_.GetPointerSize());
2533 if (table != nullptr) {
2534 indent_os << "IMT conflict table " << table << " method: ";
2535 for (size_t i = 0, count = table->NumEntries(pointer_size); i < count; ++i) {
David Sehr709b0702016-10-13 09:12:37 -07002536 indent_os << ArtMethod::PrettyMethod(table->GetImplementationMethod(i, pointer_size))
2537 << " ";
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002538 }
2539 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002540 } else {
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08002541 CodeItemDataAccessor code_item_accessor(method);
2542 size_t dex_instruction_bytes = code_item_accessor.InsnsSizeInCodeUnits() * 2;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002543 stats_.dex_instruction_bytes += dex_instruction_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002544
2545 bool first_occurrence;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002546 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002547 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01002548 // Method compiled with the optimizing compiler have no vmap table.
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002549 vmap_table_bytes = ComputeOatSize(method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002550 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002551 stats_.vmap_table_bytes += vmap_table_bytes;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002552 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002553 }
2554
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002555 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2556 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002557 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002558 stats_.managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002559 if (method->IsConstructor()) {
2560 if (method->IsStatic()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002561 stats_.class_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002562 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002563 stats_.large_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002564 }
2565 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002566 stats_.large_method_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002567 }
2568 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002569 stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002570
Igor Murashkin7617abd2015-07-10 18:27:47 -07002571 uint32_t method_access_flags = method->GetAccessFlags();
2572
Mathieu Chartiere401d142015-04-22 13:56:20 -07002573 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002574 indent_os << StringPrintf("SIZE: Dex Instructions=%zd StackMaps=%zd AccessFlags=0x%x\n",
2575 dex_instruction_bytes,
2576 vmap_table_bytes,
Igor Murashkin7617abd2015-07-10 18:27:47 -07002577 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002578
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002579 size_t total_size = dex_instruction_bytes +
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002580 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002581
2582 double expansion =
2583 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002584 stats_.ComputeOutliers(total_size, expansion, method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002585 }
2586 }
2587
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002588 std::set<const void*> already_seen_;
2589 // Compute the size of the given data within the oat file and whether this is the first time
2590 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002591 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002592 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002593 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002594 already_seen_.insert(oat_data);
2595 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002596 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002597 }
2598 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002599 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002600
2601 public:
2602 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002603 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002604 size_t file_bytes;
2605
2606 size_t header_bytes;
2607 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002608 size_t art_field_bytes;
2609 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01002610 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002611 size_t interned_strings_bytes;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002612 size_t class_table_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07002613 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002614 size_t alignment_bytes;
2615
2616 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002617 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002618 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07002619 size_t class_initializer_code_bytes;
2620 size_t large_initializer_code_bytes;
2621 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002622
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002623 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002624
2625 size_t dex_instruction_bytes;
2626
Mathieu Chartiere401d142015-04-22 13:56:20 -07002627 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002628 std::vector<size_t> method_outlier_size;
2629 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002630 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002631
Roland Levillain3887c462015-08-12 18:15:42 +01002632 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002633 : oat_file_bytes(0),
2634 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002635 header_bytes(0),
2636 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002637 art_field_bytes(0),
2638 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01002639 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002640 interned_strings_bytes(0),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002641 class_table_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07002642 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002643 alignment_bytes(0),
2644 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002645 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002646 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07002647 class_initializer_code_bytes(0),
2648 large_initializer_code_bytes(0),
2649 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002650 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002651 dex_instruction_bytes(0) {}
2652
Elliott Hughesa0e18062012-04-13 15:59:59 -07002653 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002654 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002655 size_t bytes;
2656 size_t count;
2657 };
2658 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2659 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002660
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002661 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002662 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2663 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002664 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002665 it->second.count += 1;
2666 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002667 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002668 }
2669 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002670
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002671 double PercentOfOatBytes(size_t size) {
2672 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2673 }
2674
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002675 double PercentOfFileBytes(size_t size) {
2676 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2677 }
2678
2679 double PercentOfObjectBytes(size_t size) {
2680 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2681 }
2682
Mathieu Chartiere401d142015-04-22 13:56:20 -07002683 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002684 method_outlier_size.push_back(total_size);
2685 method_outlier_expansion.push_back(expansion);
2686 method_outlier.push_back(method);
2687 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002688
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002689 void DumpOutliers(std::ostream& os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002690 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002691 size_t sum_of_sizes = 0;
2692 size_t sum_of_sizes_squared = 0;
2693 size_t sum_of_expansion = 0;
2694 size_t sum_of_expansion_squared = 0;
2695 size_t n = method_outlier_size.size();
Mathieu Chartier1ebf8d32016-06-09 11:51:27 -07002696 if (n <= 1) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08002697 return;
2698 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002699 for (size_t i = 0; i < n; i++) {
2700 size_t cur_size = method_outlier_size[i];
2701 sum_of_sizes += cur_size;
2702 sum_of_sizes_squared += cur_size * cur_size;
2703 double cur_expansion = method_outlier_expansion[i];
2704 sum_of_expansion += cur_expansion;
2705 sum_of_expansion_squared += cur_expansion * cur_expansion;
2706 }
2707 size_t size_mean = sum_of_sizes / n;
2708 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2709 double expansion_mean = sum_of_expansion / n;
2710 double expansion_variance =
2711 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2712
2713 // Dump methods whose size is a certain number of standard deviations from the mean
2714 size_t dumped_values = 0;
2715 size_t skipped_values = 0;
2716 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2717 size_t cur_size_variance = i * i * size_variance;
2718 bool first = true;
2719 for (size_t j = 0; j < n; j++) {
2720 size_t cur_size = method_outlier_size[j];
2721 if (cur_size > size_mean) {
2722 size_t cur_var = cur_size - size_mean;
2723 cur_var = cur_var * cur_var;
2724 if (cur_var > cur_size_variance) {
2725 if (dumped_values > 20) {
2726 if (i == 1) {
2727 skipped_values++;
2728 } else {
2729 i = 2; // jump to counting for 1 standard deviation
2730 break;
2731 }
2732 } else {
2733 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002734 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002735 first = false;
2736 }
David Sehr709b0702016-10-13 09:12:37 -07002737 os << ArtMethod::PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002738 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002739 method_outlier_size[j] = 0; // don't consider this method again
2740 dumped_values++;
2741 }
2742 }
2743 }
2744 }
2745 }
2746 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002747 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002748 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002749 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002750 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002751
2752 // Dump methods whose expansion is a certain number of standard deviations from the mean
2753 dumped_values = 0;
2754 skipped_values = 0;
2755 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2756 double cur_expansion_variance = i * i * expansion_variance;
2757 bool first = true;
2758 for (size_t j = 0; j < n; j++) {
2759 double cur_expansion = method_outlier_expansion[j];
2760 if (cur_expansion > expansion_mean) {
2761 size_t cur_var = cur_expansion - expansion_mean;
2762 cur_var = cur_var * cur_var;
2763 if (cur_var > cur_expansion_variance) {
2764 if (dumped_values > 20) {
2765 if (i == 1) {
2766 skipped_values++;
2767 } else {
2768 i = 2; // jump to counting for 1 standard deviation
2769 break;
2770 }
2771 } else {
2772 if (first) {
2773 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002774 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002775 first = false;
2776 }
David Sehr709b0702016-10-13 09:12:37 -07002777 os << ArtMethod::PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002778 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002779 method_outlier_expansion[j] = 0.0; // don't consider this method again
2780 dumped_values++;
2781 }
2782 }
2783 }
2784 }
2785 }
2786 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002787 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002788 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002789 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002790 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002791 }
2792
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002793 void Dump(std::ostream& os, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002794 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002795 {
2796 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2797 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002798 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2799 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2800 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2801 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2802 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2803 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002804 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002805 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2806 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002807 header_bytes, PercentOfFileBytes(header_bytes),
2808 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002809 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2810 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002811 dex_cache_arrays_bytes,
2812 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002813 interned_strings_bytes,
2814 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002815 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002816 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002817 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2818 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002819 CHECK_EQ(file_bytes,
2820 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2821 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2822 bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002823 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002824
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002825 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002826 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002827 for (const auto& sizes_and_count : sizes_and_counts) {
2828 const std::string& descriptor(sizes_and_count.first);
2829 double average = static_cast<double>(sizes_and_count.second.bytes) /
2830 static_cast<double>(sizes_and_count.second.count);
2831 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002832 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002833 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002834 descriptor.c_str(), sizes_and_count.second.bytes,
2835 sizes_and_count.second.count, average, percent);
2836 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002837 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002838 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002839 CHECK_EQ(object_bytes, object_bytes_total);
2840
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002841 os << StringPrintf("oat_file_bytes = %8zd\n"
2842 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002843 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2844 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2845 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2846 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002847 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002848 managed_code_bytes,
2849 PercentOfOatBytes(managed_code_bytes),
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002850 native_to_managed_code_bytes,
2851 PercentOfOatBytes(native_to_managed_code_bytes),
2852 class_initializer_code_bytes,
2853 PercentOfOatBytes(class_initializer_code_bytes),
2854 large_initializer_code_bytes,
2855 PercentOfOatBytes(large_initializer_code_bytes),
2856 large_method_code_bytes,
2857 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002858 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002859 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002860 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002861 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2862 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002863 }
2864
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002865 os << "\n" << StringPrintf("vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002866 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002867 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002868
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002869 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2870 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002871 static_cast<double>(managed_code_bytes) /
2872 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002873 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002874 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002875 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002876
2877 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002878 }
2879 } stats_;
2880
2881 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002882 enum {
2883 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2884 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2885 kLargeConstructorDexBytes = 4000,
2886 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2887 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2888 kLargeMethodDexBytes = 16000
2889 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002890
2891 // For performance, use the *os_ directly for anything that doesn't need indentation
2892 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002893 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002894 VariableIndentationOutputStream vios_;
2895 ScopedIndentation indent1_;
2896
Ian Rogers1d54e732013-05-02 21:10:01 -07002897 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002898 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002899 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002900 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002901 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002902
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002903 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002904};
2905
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002906static int DumpImage(gc::space::ImageSpace* image_space,
2907 OatDumperOptions* options,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002908 std::ostream* os) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002909 const ImageHeader& image_header = image_space->GetImageHeader();
2910 if (!image_header.IsValid()) {
2911 fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
2912 return EXIT_FAILURE;
2913 }
2914 ImageDumper image_dumper(os, *image_space, image_header, options);
2915 if (!image_dumper.Dump()) {
2916 return EXIT_FAILURE;
2917 }
2918 return EXIT_SUCCESS;
2919}
2920
2921static int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002922 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002923 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002924 options->class_loader_ = &null_class_loader;
2925
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002926 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002927 if (options->app_image_ != nullptr) {
2928 if (options->app_oat_ == nullptr) {
2929 LOG(ERROR) << "Can not dump app image without app oat file";
Jeff Haodcdc85b2015-12-04 14:06:18 -08002930 return EXIT_FAILURE;
2931 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002932 // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
2933 // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
2934 // pointers into 32 bit pointer sized ArtMethods.
2935 std::string error_msg;
2936 std::unique_ptr<OatFile> oat_file(OatFile::Open(options->app_oat_,
2937 options->app_oat_,
2938 nullptr,
2939 nullptr,
2940 false,
2941 /*low_4gb*/true,
2942 nullptr,
2943 &error_msg));
2944 if (oat_file == nullptr) {
2945 LOG(ERROR) << "Failed to open oat file " << options->app_oat_ << " with error " << error_msg;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002946 return EXIT_FAILURE;
2947 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002948 std::unique_ptr<gc::space::ImageSpace> space(
2949 gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg));
2950 if (space == nullptr) {
2951 LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error "
2952 << error_msg;
2953 }
2954 // Open dex files for the image.
2955 std::vector<std::unique_ptr<const DexFile>> dex_files;
2956 if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
2957 LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
2958 << error_msg;
2959 }
2960 // Dump the actual image.
2961 int result = DumpImage(space.get(), options, os);
2962 if (result != EXIT_SUCCESS) {
2963 return result;
2964 }
2965 // Fall through to dump the boot images.
2966 }
2967
2968 gc::Heap* heap = runtime->GetHeap();
2969 CHECK(heap->HasBootImageSpace()) << "No image spaces";
2970 for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
2971 int result = DumpImage(image_space, options, os);
2972 if (result != EXIT_SUCCESS) {
2973 return result;
2974 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002975 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002976 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002977}
2978
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002979static jobject InstallOatFile(Runtime* runtime,
2980 std::unique_ptr<OatFile> oat_file,
2981 std::vector<const DexFile*>* class_path)
2982 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002983 Thread* self = Thread::Current();
2984 CHECK(self != nullptr);
2985 // Need well-known-classes.
2986 WellKnownClasses::Init(self->GetJniEnv());
2987
2988 // Need to register dex files to get a working dex cache.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002989 OatFile* oat_file_ptr = oat_file.get();
Andreas Gampe00b25f32014-09-17 21:49:05 -07002990 ClassLinker* class_linker = runtime->GetClassLinker();
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002991 runtime->GetOatFileManager().RegisterOatFile(std::move(oat_file));
2992 for (const OatFile::OatDexFile* odf : oat_file_ptr->GetOatDexFiles()) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002993 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002994 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002995 CHECK(dex_file != nullptr) << error_msg;
Vladimir Markocd556b02017-02-03 11:47:34 +00002996 ObjPtr<mirror::DexCache> dex_cache =
2997 class_linker->RegisterDexFile(*dex_file, nullptr);
2998 CHECK(dex_cache != nullptr);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002999 class_path->push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003000 }
3001
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003002 // Need a class loader. Fake that we're a compiler.
3003 // Note: this will run initializers through the unstarted runtime, so make sure it's
3004 // initialized.
3005 interpreter::UnstartedRuntime::Initialize();
3006
3007 jobject class_loader = class_linker->CreatePathClassLoader(self, *class_path);
3008
3009 return class_loader;
3010}
3011
3012static int DumpOatWithRuntime(Runtime* runtime,
3013 std::unique_ptr<OatFile> oat_file,
3014 OatDumperOptions* options,
3015 std::ostream* os) {
3016 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
3017 ScopedObjectAccess soa(Thread::Current());
3018
3019 OatFile* oat_file_ptr = oat_file.get();
3020 std::vector<const DexFile*> class_path;
3021 jobject class_loader = InstallOatFile(runtime, std::move(oat_file), &class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003022
3023 // Use the class loader while dumping.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003024 StackHandleScope<1> scope(soa.Self());
Andreas Gampe00b25f32014-09-17 21:49:05 -07003025 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07003026 soa.Decode<mirror::ClassLoader>(class_loader));
Andreas Gampe00b25f32014-09-17 21:49:05 -07003027 options->class_loader_ = &loader_handle;
3028
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003029 OatDumper oat_dumper(*oat_file_ptr, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003030 bool success = oat_dumper.Dump(*os);
3031 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
3032}
3033
3034static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003035 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003036 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08003037 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003038 options->class_loader_ = &null_class_loader;
3039
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003040 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003041 bool success = oat_dumper.Dump(*os);
3042 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
3043}
3044
3045static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
3046 std::ostream* os) {
3047 std::string error_msg;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003048 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
3049 oat_filename,
3050 nullptr,
3051 nullptr,
3052 false,
3053 /*low_4gb*/false,
3054 nullptr,
3055 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07003056 if (oat_file == nullptr) {
3057 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
3058 return EXIT_FAILURE;
3059 }
3060
3061 if (runtime != nullptr) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003062 return DumpOatWithRuntime(runtime, std::move(oat_file), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003063 } else {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003064 return DumpOatWithoutRuntime(oat_file.get(), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003065 }
3066}
3067
David Srbecky2fdd03c2016-03-10 15:32:37 +00003068static int SymbolizeOat(const char* oat_filename, std::string& output_name, bool no_bits) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07003069 std::string error_msg;
Andreas Gampe08c277c2017-04-26 22:22:15 -07003070 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
3071 oat_filename,
3072 nullptr,
3073 nullptr,
3074 false,
3075 /*low_4gb*/false,
3076 nullptr,
3077 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07003078 if (oat_file == nullptr) {
3079 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
3080 return EXIT_FAILURE;
3081 }
3082
Andreas Gampe2d8614b2016-03-07 16:31:34 -08003083 bool result;
3084 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
3085 // files for 64-bit code in the past.
3086 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
Andreas Gampe08c277c2017-04-26 22:22:15 -07003087 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file.get(), output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08003088 result = oat_symbolizer.Symbolize();
3089 } else {
Andreas Gampe08c277c2017-04-26 22:22:15 -07003090 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file.get(), output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08003091 result = oat_symbolizer.Symbolize();
3092 }
3093 if (!result) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07003094 fprintf(stderr, "Failed to symbolize\n");
3095 return EXIT_FAILURE;
3096 }
3097
3098 return EXIT_SUCCESS;
3099}
3100
Andreas Gampe9fded872016-09-25 16:08:35 -07003101class IMTDumper {
3102 public:
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003103 static bool Dump(Runtime* runtime,
3104 const std::string& imt_file,
3105 bool dump_imt_stats,
3106 const char* oat_filename) {
3107 Thread* self = Thread::Current();
3108
3109 ScopedObjectAccess soa(self);
3110 StackHandleScope<1> scope(self);
3111 MutableHandle<mirror::ClassLoader> class_loader = scope.NewHandle<mirror::ClassLoader>(nullptr);
3112 std::vector<const DexFile*> class_path;
3113
3114 if (oat_filename != nullptr) {
3115 std::string error_msg;
3116 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
3117 oat_filename,
3118 nullptr,
3119 nullptr,
3120 false,
3121 /*low_4gb*/false,
3122 nullptr,
3123 &error_msg));
3124 if (oat_file == nullptr) {
3125 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
3126 return false;
3127 }
3128
3129 class_loader.Assign(soa.Decode<mirror::ClassLoader>(
3130 InstallOatFile(runtime, std::move(oat_file), &class_path)));
3131 } else {
3132 class_loader.Assign(nullptr); // Boot classloader. Just here for explicit documentation.
3133 class_path = runtime->GetClassLinker()->GetBootClassPath();
3134 }
3135
3136 if (!imt_file.empty()) {
3137 return DumpImt(runtime, imt_file, class_loader);
3138 }
3139
3140 if (dump_imt_stats) {
3141 return DumpImtStats(runtime, class_path, class_loader);
3142 }
3143
3144 LOG(FATAL) << "Should not reach here";
3145 UNREACHABLE();
3146 }
3147
3148 private:
3149 static bool DumpImt(Runtime* runtime,
3150 const std::string& imt_file,
3151 Handle<mirror::ClassLoader> h_class_loader)
3152 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003153 std::vector<std::string> lines = ReadCommentedInputFromFile(imt_file);
3154 std::unordered_set<std::string> prepared;
3155
3156 for (const std::string& line : lines) {
3157 // A line should be either a class descriptor, in which case we will dump the complete IMT,
3158 // or a class descriptor and an interface method, in which case we will lookup the method,
3159 // determine its IMT slot, and check the class' IMT.
3160 size_t first_space = line.find(' ');
3161 if (first_space == std::string::npos) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003162 DumpIMTForClass(runtime, line, h_class_loader, &prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07003163 } else {
3164 DumpIMTForMethod(runtime,
3165 line.substr(0, first_space),
3166 line.substr(first_space + 1, std::string::npos),
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003167 h_class_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003168 &prepared);
3169 }
3170 std::cerr << std::endl;
3171 }
3172
3173 return true;
3174 }
3175
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003176 static bool DumpImtStats(Runtime* runtime,
3177 const std::vector<const DexFile*>& dex_files,
3178 Handle<mirror::ClassLoader> h_class_loader)
3179 REQUIRES_SHARED(Locks::mutator_lock_) {
3180 size_t without_imt = 0;
3181 size_t with_imt = 0;
Andreas Gampe9fded872016-09-25 16:08:35 -07003182 std::map<size_t, size_t> histogram;
3183
3184 ClassLinker* class_linker = runtime->GetClassLinker();
3185 const PointerSize pointer_size = class_linker->GetImagePointerSize();
3186 std::unordered_set<std::string> prepared;
3187
3188 Thread* self = Thread::Current();
Andreas Gampe9fded872016-09-25 16:08:35 -07003189 StackHandleScope<1> scope(self);
3190 MutableHandle<mirror::Class> h_klass(scope.NewHandle<mirror::Class>(nullptr));
3191
3192 for (const DexFile* dex_file : dex_files) {
3193 for (uint32_t class_def_index = 0;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003194 class_def_index != dex_file->NumClassDefs();
3195 ++class_def_index) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003196 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
3197 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003198 h_klass.Assign(class_linker->FindClass(self, descriptor, h_class_loader));
Andreas Gampefa4333d2017-02-14 11:10:34 -08003199 if (h_klass == nullptr) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003200 std::cerr << "Warning: could not load " << descriptor << std::endl;
3201 continue;
3202 }
3203
3204 if (HasNoIMT(runtime, h_klass, pointer_size, &prepared)) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003205 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003206 continue;
3207 }
3208
3209 ImTable* im_table = PrepareAndGetImTable(runtime, h_klass, pointer_size, &prepared);
3210 if (im_table == nullptr) {
3211 // Should not happen, but accept.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003212 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003213 continue;
3214 }
3215
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003216 with_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003217 for (size_t imt_index = 0; imt_index != ImTable::kSize; ++imt_index) {
3218 ArtMethod* ptr = im_table->Get(imt_index, pointer_size);
3219 if (ptr->IsRuntimeMethod()) {
3220 if (ptr->IsImtUnimplementedMethod()) {
3221 histogram[0]++;
3222 } else {
3223 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3224 histogram[current_table->NumEntries(pointer_size)]++;
3225 }
3226 } else {
3227 histogram[1]++;
3228 }
3229 }
3230 }
3231 }
3232
3233 std::cerr << "IMT stats:"
3234 << std::endl << std::endl;
3235
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003236 std::cerr << " " << with_imt << " classes with IMT."
Andreas Gampe9fded872016-09-25 16:08:35 -07003237 << std::endl << std::endl;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003238 std::cerr << " " << without_imt << " classes without IMT (or copy from Object)."
Andreas Gampe9fded872016-09-25 16:08:35 -07003239 << std::endl << std::endl;
3240
3241 double sum_one = 0;
3242 size_t count_one = 0;
3243
3244 std::cerr << " " << "IMT histogram" << std::endl;
3245 for (auto& bucket : histogram) {
3246 std::cerr << " " << bucket.first << " " << bucket.second << std::endl;
3247 if (bucket.first > 0) {
3248 sum_one += bucket.second * bucket.first;
3249 count_one += bucket.second;
3250 }
3251 }
3252
3253 double count_zero = count_one + histogram[0];
3254 std::cerr << " Stats:" << std::endl;
3255 std::cerr << " Average depth (including empty): " << (sum_one / count_zero) << std::endl;
3256 std::cerr << " Average depth (excluding empty): " << (sum_one / count_one) << std::endl;
3257
3258 return true;
3259 }
3260
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003261 // Return whether the given class has no IMT (or the one shared with java.lang.Object).
Andreas Gampe9fded872016-09-25 16:08:35 -07003262 static bool HasNoIMT(Runtime* runtime,
3263 Handle<mirror::Class> klass,
3264 const PointerSize pointer_size,
3265 std::unordered_set<std::string>* prepared)
3266 REQUIRES_SHARED(Locks::mutator_lock_) {
3267 if (klass->IsObjectClass() || !klass->ShouldHaveImt()) {
3268 return true;
3269 }
3270
3271 if (klass->GetImt(pointer_size) == nullptr) {
3272 PrepareClass(runtime, klass, prepared);
3273 }
3274
3275 mirror::Class* object_class = mirror::Class::GetJavaLangClass()->GetSuperClass();
3276 DCHECK(object_class->IsObjectClass());
3277
3278 bool result = klass->GetImt(pointer_size) == object_class->GetImt(pointer_size);
3279
Mathieu Chartier6beced42016-11-15 15:51:31 -08003280 if (klass->GetIfTable()->Count() == 0) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003281 DCHECK(result);
3282 }
3283
3284 return result;
3285 }
3286
3287 static void PrintTable(ImtConflictTable* table, PointerSize pointer_size)
3288 REQUIRES_SHARED(Locks::mutator_lock_) {
3289 if (table == nullptr) {
3290 std::cerr << " <No IMT?>" << std::endl;
3291 return;
3292 }
3293 size_t table_index = 0;
3294 for (;;) {
3295 ArtMethod* ptr = table->GetInterfaceMethod(table_index, pointer_size);
3296 if (ptr == nullptr) {
3297 return;
3298 }
3299 table_index++;
David Sehr709b0702016-10-13 09:12:37 -07003300 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003301 }
3302 }
3303
3304 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3305 Thread* self,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003306 Handle<mirror::ClassLoader> h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003307 const std::string& class_name,
3308 const PointerSize pointer_size,
3309 mirror::Class** klass_out,
3310 std::unordered_set<std::string>* prepared)
3311 REQUIRES_SHARED(Locks::mutator_lock_) {
3312 if (class_name.empty()) {
3313 return nullptr;
3314 }
3315
3316 std::string descriptor;
3317 if (class_name[0] == 'L') {
3318 descriptor = class_name;
3319 } else {
3320 descriptor = DotToDescriptor(class_name.c_str());
3321 }
3322
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003323 mirror::Class* klass = runtime->GetClassLinker()->FindClass(self, descriptor.c_str(), h_loader);
Andreas Gampe9fded872016-09-25 16:08:35 -07003324
3325 if (klass == nullptr) {
3326 self->ClearException();
3327 std::cerr << "Did not find " << class_name << std::endl;
3328 *klass_out = nullptr;
3329 return nullptr;
3330 }
3331
3332 StackHandleScope<1> scope(Thread::Current());
3333 Handle<mirror::Class> h_klass = scope.NewHandle<mirror::Class>(klass);
3334
3335 ImTable* ret = PrepareAndGetImTable(runtime, h_klass, pointer_size, prepared);
3336 *klass_out = h_klass.Get();
3337 return ret;
3338 }
3339
3340 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3341 Handle<mirror::Class> h_klass,
3342 const PointerSize pointer_size,
3343 std::unordered_set<std::string>* prepared)
3344 REQUIRES_SHARED(Locks::mutator_lock_) {
3345 PrepareClass(runtime, h_klass, prepared);
3346 return h_klass->GetImt(pointer_size);
3347 }
3348
3349 static void DumpIMTForClass(Runtime* runtime,
3350 const std::string& class_name,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003351 Handle<mirror::ClassLoader> h_loader,
3352 std::unordered_set<std::string>* prepared)
3353 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003354 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
3355 mirror::Class* klass;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003356 ImTable* imt = PrepareAndGetImTable(runtime,
3357 Thread::Current(),
3358 h_loader,
3359 class_name,
3360 pointer_size,
3361 &klass,
3362 prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07003363 if (imt == nullptr) {
3364 return;
3365 }
3366
3367 std::cerr << class_name << std::endl << " IMT:" << std::endl;
3368 for (size_t index = 0; index < ImTable::kSize; ++index) {
3369 std::cerr << " " << index << ":" << std::endl;
3370 ArtMethod* ptr = imt->Get(index, pointer_size);
3371 if (ptr->IsRuntimeMethod()) {
3372 if (ptr->IsImtUnimplementedMethod()) {
3373 std::cerr << " <empty>" << std::endl;
3374 } else {
3375 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3376 PrintTable(current_table, pointer_size);
3377 }
3378 } else {
David Sehr709b0702016-10-13 09:12:37 -07003379 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003380 }
3381 }
3382
3383 std::cerr << " Interfaces:" << std::endl;
3384 // Run through iftable, find methods that slot here, see if they fit.
3385 mirror::IfTable* if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003386 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
3387 mirror::Class* iface = if_table->GetInterface(i);
3388 std::string iface_name;
3389 std::cerr << " " << iface->GetDescriptor(&iface_name) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003390
Mathieu Chartier6beced42016-11-15 15:51:31 -08003391 for (ArtMethod& iface_method : iface->GetVirtualMethods(pointer_size)) {
3392 uint32_t class_hash, name_hash, signature_hash;
3393 ImTable::GetImtHashComponents(&iface_method, &class_hash, &name_hash, &signature_hash);
3394 uint32_t imt_slot = ImTable::GetImtIndex(&iface_method);
3395 std::cerr << " " << iface_method.PrettyMethod(true)
3396 << " slot=" << imt_slot
3397 << std::hex
3398 << " class_hash=0x" << class_hash
3399 << " name_hash=0x" << name_hash
3400 << " signature_hash=0x" << signature_hash
3401 << std::dec
3402 << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003403 }
3404 }
3405 }
3406
3407 static void DumpIMTForMethod(Runtime* runtime,
3408 const std::string& class_name,
3409 const std::string& method,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003410 Handle<mirror::ClassLoader> h_loader,
3411 std::unordered_set<std::string>* prepared)
3412 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003413 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
3414 mirror::Class* klass;
3415 ImTable* imt = PrepareAndGetImTable(runtime,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003416 Thread::Current(),
3417 h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003418 class_name,
3419 pointer_size,
3420 &klass,
3421 prepared);
3422 if (imt == nullptr) {
3423 return;
3424 }
3425
3426 std::cerr << class_name << " <" << method << ">" << std::endl;
3427 for (size_t index = 0; index < ImTable::kSize; ++index) {
3428 ArtMethod* ptr = imt->Get(index, pointer_size);
3429 if (ptr->IsRuntimeMethod()) {
3430 if (ptr->IsImtUnimplementedMethod()) {
3431 continue;
3432 }
3433
3434 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3435 if (current_table == nullptr) {
3436 continue;
3437 }
3438
3439 size_t table_index = 0;
3440 for (;;) {
3441 ArtMethod* ptr2 = current_table->GetInterfaceMethod(table_index, pointer_size);
3442 if (ptr2 == nullptr) {
3443 break;
3444 }
3445 table_index++;
3446
David Sehr709b0702016-10-13 09:12:37 -07003447 std::string p_name = ptr2->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003448 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003449 std::cerr << " Slot "
3450 << index
3451 << " ("
3452 << current_table->NumEntries(pointer_size)
3453 << ")"
3454 << std::endl;
3455 PrintTable(current_table, pointer_size);
3456 return;
3457 }
3458 }
3459 } else {
David Sehr709b0702016-10-13 09:12:37 -07003460 std::string p_name = ptr->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003461 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003462 std::cerr << " Slot " << index << " (1)" << std::endl;
3463 std::cerr << " " << p_name << std::endl;
3464 } else {
3465 // Run through iftable, find methods that slot here, see if they fit.
3466 mirror::IfTable* if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003467 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
3468 mirror::Class* iface = if_table->GetInterface(i);
3469 size_t num_methods = iface->NumDeclaredVirtualMethods();
3470 if (num_methods > 0) {
3471 for (ArtMethod& iface_method : iface->GetMethods(pointer_size)) {
3472 if (ImTable::GetImtIndex(&iface_method) == index) {
3473 std::string i_name = iface_method.PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003474 if (android::base::StartsWith(i_name, method.c_str())) {
Mathieu Chartier6beced42016-11-15 15:51:31 -08003475 std::cerr << " Slot " << index << " (1)" << std::endl;
3476 std::cerr << " " << p_name << " (" << i_name << ")" << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003477 }
3478 }
3479 }
3480 }
3481 }
3482 }
3483 }
3484 }
3485 }
3486
3487 // Read lines from the given stream, dropping comments and empty lines
3488 static std::vector<std::string> ReadCommentedInputStream(std::istream& in_stream) {
3489 std::vector<std::string> output;
3490 while (in_stream.good()) {
3491 std::string dot;
3492 std::getline(in_stream, dot);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003493 if (android::base::StartsWith(dot, "#") || dot.empty()) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003494 continue;
3495 }
3496 output.push_back(dot);
3497 }
3498 return output;
3499 }
3500
3501 // Read lines from the given file, dropping comments and empty lines.
3502 static std::vector<std::string> ReadCommentedInputFromFile(const std::string& input_filename) {
3503 std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
3504 if (input_file.get() == nullptr) {
3505 LOG(ERROR) << "Failed to open input file " << input_filename;
3506 return std::vector<std::string>();
3507 }
3508 std::vector<std::string> result = ReadCommentedInputStream(*input_file);
3509 input_file->close();
3510 return result;
3511 }
3512
3513 // Prepare a class, i.e., ensure it has a filled IMT. Will do so recursively for superclasses,
3514 // and note in the given set that the work was done.
3515 static void PrepareClass(Runtime* runtime,
3516 Handle<mirror::Class> h_klass,
3517 std::unordered_set<std::string>* done)
3518 REQUIRES_SHARED(Locks::mutator_lock_) {
3519 if (!h_klass->ShouldHaveImt()) {
3520 return;
3521 }
3522
3523 std::string name;
3524 name = h_klass->GetDescriptor(&name);
3525
3526 if (done->find(name) != done->end()) {
3527 return;
3528 }
3529 done->insert(name);
3530
3531 if (h_klass->HasSuperClass()) {
3532 StackHandleScope<1> h(Thread::Current());
3533 PrepareClass(runtime, h.NewHandle<mirror::Class>(h_klass->GetSuperClass()), done);
3534 }
3535
3536 if (!h_klass->IsTemp()) {
3537 runtime->GetClassLinker()->FillIMTAndConflictTables(h_klass.Get());
3538 }
3539 }
3540};
3541
Igor Murashkin37743352014-11-13 14:38:00 -08003542struct OatdumpArgs : public CmdlineArgs {
3543 protected:
3544 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003545
Igor Murashkin37743352014-11-13 14:38:00 -08003546 virtual ParseStatus ParseCustom(const StringPiece& option,
3547 std::string* error_msg) OVERRIDE {
3548 {
3549 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
3550 if (base_parse != kParseUnknownArgument) {
3551 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003552 }
3553 }
3554
Igor Murashkin37743352014-11-13 14:38:00 -08003555 if (option.starts_with("--oat-file=")) {
3556 oat_filename_ = option.substr(strlen("--oat-file=")).data();
3557 } else if (option.starts_with("--image=")) {
3558 image_location_ = option.substr(strlen("--image=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003559 } else if (option == "--no-dump:vmap") {
3560 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01003561 } else if (option =="--dump:code_info_stack_maps") {
3562 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003563 } else if (option == "--no-disassemble") {
3564 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003565 } else if (option =="--header-only") {
3566 dump_header_only_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003567 } else if (option.starts_with("--symbolize=")) {
3568 oat_filename_ = option.substr(strlen("--symbolize=")).data();
3569 symbolize_ = true;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003570 } else if (option.starts_with("--only-keep-debug")) {
3571 only_keep_debug_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003572 } else if (option.starts_with("--class-filter=")) {
3573 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003574 } else if (option.starts_with("--method-filter=")) {
3575 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003576 } else if (option.starts_with("--list-classes")) {
3577 list_classes_ = true;
3578 } else if (option.starts_with("--list-methods")) {
3579 list_methods_ = true;
3580 } else if (option.starts_with("--export-dex-to=")) {
3581 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
3582 } else if (option.starts_with("--addr2instr=")) {
3583 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
3584 *error_msg = "Address conversion failed";
3585 return kParseError;
3586 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003587 } else if (option.starts_with("--app-image=")) {
3588 app_image_ = option.substr(strlen("--app-image=")).data();
3589 } else if (option.starts_with("--app-oat=")) {
3590 app_oat_ = option.substr(strlen("--app-oat=")).data();
Andreas Gampe9fded872016-09-25 16:08:35 -07003591 } else if (option.starts_with("--dump-imt=")) {
3592 imt_dump_ = option.substr(strlen("--dump-imt=")).data();
3593 } else if (option == "--dump-imt-stats") {
3594 imt_stat_dump_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003595 } else {
3596 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003597 }
3598
Igor Murashkin37743352014-11-13 14:38:00 -08003599 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003600 }
3601
Igor Murashkin37743352014-11-13 14:38:00 -08003602 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
3603 // Infer boot image location from the image location if possible.
3604 if (boot_image_location_ == nullptr) {
3605 boot_image_location_ = image_location_;
3606 }
3607
3608 // Perform the parent checks.
3609 ParseStatus parent_checks = Base::ParseChecks(error_msg);
3610 if (parent_checks != kParseOk) {
3611 return parent_checks;
3612 }
3613
3614 // Perform our own checks.
3615 if (image_location_ == nullptr && oat_filename_ == nullptr) {
3616 *error_msg = "Either --image or --oat-file must be specified";
3617 return kParseError;
3618 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
3619 *error_msg = "Either --image or --oat-file must be specified but not both";
3620 return kParseError;
3621 }
3622
3623 return kParseOk;
3624 }
3625
3626 virtual std::string GetUsage() const {
3627 std::string usage;
3628
3629 usage +=
3630 "Usage: oatdump [options] ...\n"
3631 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
3632 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
3633 "\n"
3634 // Either oat-file or image is required.
3635 " --oat-file=<file.oat>: specifies an input oat filename.\n"
3636 " Example: --oat-file=/system/framework/boot.oat\n"
3637 "\n"
3638 " --image=<file.art>: specifies an input image location.\n"
3639 " Example: --image=/system/framework/boot.art\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003640 "\n"
3641 " --app-image=<file.art>: specifies an input app image. Must also have a specified\n"
Roland Levillain4f1c9e62017-06-28 16:44:30 +01003642 " boot image (with --image) and app oat file (with --app-oat).\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003643 " Example: --app-image=app.art\n"
3644 "\n"
3645 " --app-oat=<file.odex>: specifies an input app oat.\n"
3646 " Example: --app-oat=app.odex\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003647 "\n";
3648
3649 usage += Base::GetUsage();
3650
3651 usage += // Optional.
Igor Murashkin37743352014-11-13 14:38:00 -08003652 " --no-dump:vmap may be used to disable vmap dumping.\n"
3653 " Example: --no-dump:vmap\n"
3654 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01003655 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
3656 " Example: --dump:code_info_stack_maps\n"
3657 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003658 " --no-disassemble may be used to disable disassembly.\n"
3659 " Example: --no-disassemble\n"
3660 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00003661 " --header-only may be used to print only the oat header.\n"
3662 " Example: --header-only\n"
3663 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003664 " --list-classes may be used to list target file classes (can be used with filters).\n"
3665 " Example: --list-classes\n"
3666 " Example: --list-classes --class-filter=com.example.foo\n"
3667 "\n"
3668 " --list-methods may be used to list target file methods (can be used with filters).\n"
3669 " Example: --list-methods\n"
3670 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
3671 "\n"
3672 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
3673 " Example: --symbolize=/system/framework/boot.oat\n"
3674 "\n"
David Srbecky2fdd03c2016-03-10 15:32:37 +00003675 " --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
3676 " .rodata and .text sections are omitted in the output file to save space.\n"
3677 " Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
3678 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003679 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
3680 " Example: --class-filter=com.example.foo\n"
3681 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003682 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
3683 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003684 "\n"
3685 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
3686 " Example: --export-dex-to=/data/local/tmp\n"
3687 "\n"
3688 " --addr2instr=<address>: output matching method disassembled code from relative\n"
3689 " address (e.g. PC from crash dump)\n"
3690 " Example: --addr2instr=0x00001a3b\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003691 "\n"
3692 " --dump-imt=<file.txt>: output IMT collisions (if any) for the given receiver\n"
3693 " types and interface methods in the given file. The file\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003694 " is read line-wise, where each line should either be a class\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003695 " name or descriptor, or a class name/descriptor and a prefix\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003696 " of a complete method name (separated by a whitespace).\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003697 " Example: --dump-imt=imt.txt\n"
3698 "\n"
3699 " --dump-imt-stats: output IMT statistics for the given boot image\n"
3700 " Example: --dump-imt-stats"
Igor Murashkin37743352014-11-13 14:38:00 -08003701 "\n";
3702
3703 return usage;
3704 }
3705
3706 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07003707 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003708 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00003709 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07003710 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003711 std::string elf_filename_prefix_;
Andreas Gampe9fded872016-09-25 16:08:35 -07003712 std::string imt_dump_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003713 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01003714 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003715 bool disassemble_code_ = true;
3716 bool symbolize_ = false;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003717 bool only_keep_debug_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003718 bool list_classes_ = false;
3719 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003720 bool dump_header_only_ = false;
Andreas Gampe9fded872016-09-25 16:08:35 -07003721 bool imt_stat_dump_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003722 uint32_t addr2instr_ = 0;
3723 const char* export_dex_location_ = nullptr;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003724 const char* app_image_ = nullptr;
3725 const char* app_oat_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003726};
3727
Igor Murashkin37743352014-11-13 14:38:00 -08003728struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
3729 virtual bool NeedsRuntime() OVERRIDE {
3730 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003731
Igor Murashkin37743352014-11-13 14:38:00 -08003732 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
3733 bool absolute_addresses = (args_->oat_filename_ == nullptr);
3734
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003735 oat_dumper_options_.reset(new OatDumperOptions(
Igor Murashkin37743352014-11-13 14:38:00 -08003736 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01003737 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08003738 args_->disassemble_code_,
3739 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003740 args_->class_filter_,
3741 args_->method_filter_,
3742 args_->list_classes_,
3743 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00003744 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003745 args_->export_dex_location_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003746 args_->app_image_,
3747 args_->app_oat_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003748 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08003749
Andreas Gampe9fded872016-09-25 16:08:35 -07003750 return (args_->boot_image_location_ != nullptr ||
3751 args_->image_location_ != nullptr ||
3752 !args_->imt_dump_.empty()) &&
Igor Murashkin37743352014-11-13 14:38:00 -08003753 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003754 }
3755
Igor Murashkin37743352014-11-13 14:38:00 -08003756 virtual bool ExecuteWithoutRuntime() OVERRIDE {
3757 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08003758 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003759
Mathieu Chartierd424d082014-10-15 10:31:46 -07003760 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08003761
Andreas Gampec24f3992014-12-17 20:40:11 -08003762 if (args_->symbolize_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00003763 // ELF has special kind of section called SHT_NOBITS which allows us to create
3764 // sections which exist but their data is omitted from the ELF file to save space.
3765 // This is what "strip --only-keep-debug" does when it creates separate ELF file
3766 // with only debug data. We use it in similar way to exclude .rodata and .text.
3767 bool no_bits = args_->only_keep_debug_;
3768 return SymbolizeOat(args_->oat_filename_, args_->output_name_, no_bits) == EXIT_SUCCESS;
Andreas Gampec24f3992014-12-17 20:40:11 -08003769 } else {
3770 return DumpOat(nullptr,
3771 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003772 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08003773 args_->os_) == EXIT_SUCCESS;
3774 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07003775 }
3776
Igor Murashkin37743352014-11-13 14:38:00 -08003777 virtual bool ExecuteWithRuntime(Runtime* runtime) {
3778 CHECK(args_ != nullptr);
3779
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003780 if (!args_->imt_dump_.empty() || args_->imt_stat_dump_) {
3781 return IMTDumper::Dump(runtime,
3782 args_->imt_dump_,
3783 args_->imt_stat_dump_,
3784 args_->oat_filename_);
Andreas Gampe9fded872016-09-25 16:08:35 -07003785 }
3786
Igor Murashkin37743352014-11-13 14:38:00 -08003787 if (args_->oat_filename_ != nullptr) {
3788 return DumpOat(runtime,
3789 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003790 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08003791 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003792 }
Igor Murashkin37743352014-11-13 14:38:00 -08003793
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003794 return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003795 }
3796
Igor Murashkin37743352014-11-13 14:38:00 -08003797 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
3798};
Andreas Gampe00b25f32014-09-17 21:49:05 -07003799
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003800} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07003801
3802int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08003803 art::OatdumpMain main;
3804 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07003805}