blob: 35f8ee2ce2c8d07eefeb73f6045ccc8ff4a5d443 [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>
David Srbecky86decb62018-06-05 06:41:10 +010021#include <iomanip>
Brian Carlstrom27ec9612011-09-19 20:20:38 -070022#include <iostream>
Igor Murashkin37743352014-11-13 14:38:00 -080023#include <map>
24#include <set>
Brian Carlstrom78128a62011-09-15 17:21:19 -070025#include <string>
Andreas Gampe54fc26c2014-09-04 21:47:42 -070026#include <unordered_map>
Andreas Gampe9fded872016-09-25 16:08:35 -070027#include <unordered_set>
Brian Carlstrom78128a62011-09-15 17:21:19 -070028#include <vector>
29
Andreas Gampe221d9812018-01-22 17:48:56 -080030#include "android-base/logging.h"
Andreas Gampef9411702018-09-06 17:16:57 -070031#include "android-base/parseint.h"
Andreas Gampe46ee31b2016-12-14 10:11:49 -080032#include "android-base/stringprintf.h"
Andreas Gampe9186ced2016-12-12 14:28:21 -080033#include "android-base/strings.h"
34
Ian Rogersd582fa42014-11-05 23:46:43 -080035#include "arch/instruction_set_features.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070036#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070037#include "art_method-inl.h"
Vladimir Markof3c52b42017-11-17 17:32:12 +000038#include "base/bit_utils_iterator.h"
David Sehr9c4a0152018-04-05 12:23:54 -070039#include "base/indenter.h"
David Sehrc431b9d2018-03-02 12:01:51 -080040#include "base/os.h"
David Sehr67bf42e2018-02-26 16:43:04 -080041#include "base/safe_map.h"
David Srbecky86decb62018-06-05 06:41:10 +010042#include "base/stats.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000043#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080044#include "base/unix_file/fd_file.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045#include "class_linker-inl.h"
Steven Morelande431e272017-07-18 16:53:49 -070046#include "class_linker.h"
Vladimir Markoa8bba7d2018-05-30 15:18:48 +010047#include "class_root.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010048#include "compiled_method.h"
David Srbecky32210b92017-12-04 14:39:21 +000049#include "debug/debug_info.h"
David Srbecky5d950762016-03-07 20:47:29 +000050#include "debug/elf_debug_writer.h"
51#include "debug/method_debug_info.h"
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +030052#include "dex/art_dex_file_loader.h"
Mathieu Chartier3d092992018-05-24 13:36:23 -070053#include "dex/class_accessor-inl.h"
David Sehr9e734c72018-01-04 17:56:19 -080054#include "dex/code_item_accessors-inl.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080055#include "dex/descriptors_names.h"
David Sehr9e734c72018-01-04 17:56:19 -080056#include "dex/dex_file-inl.h"
57#include "dex/dex_instruction-inl.h"
David Sehr312f3b22018-03-19 08:39:26 -070058#include "dex/string_reference.h"
David Sehr9c4a0152018-04-05 12:23:54 -070059#include "dex/type_lookup_table.h"
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +030060#include "dexlayout.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080061#include "disassembler.h"
Andreas Gampe0c183382017-07-13 22:26:24 -070062#include "gc/accounting/space_bitmap-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070063#include "gc/space/image_space.h"
64#include "gc/space/large_object_space.h"
65#include "gc/space/space-inl.h"
Mathieu Chartier4a26f172016-01-26 14:26:18 -080066#include "image-inl.h"
Andreas Gampe9fded872016-09-25 16:08:35 -070067#include "imtable-inl.h"
Igor Murashkin86083f72017-10-27 10:59:04 -070068#include "subtype_check.h"
Vladimir Markof3c52b42017-11-17 17:32:12 +000069#include "index_bss_mapping.h"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -070070#include "interpreter/unstarted_runtime.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000071#include "linker/buffered_output_stream.h"
Vladimir Marko74527972016-11-29 15:57:32 +000072#include "linker/elf_builder.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000073#include "linker/file_output_stream.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080074#include "mirror/array-inl.h"
75#include "mirror/class-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010076#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080077#include "mirror/object-inl.h"
78#include "mirror/object_array-inl.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080079#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010080#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070081#include "oat_file_manager.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070082#include "scoped_thread_state_change-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070083#include "stack.h"
Mathieu Chartierdc00f182016-07-14 10:10:44 -070084#include "stack_map.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080085#include "thread_list.h"
Nicolas Geoffray4acefd32016-10-24 13:14:58 +010086#include "vdex_file.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080087#include "verifier/method_verifier.h"
Nicolas Geoffraye70dd562016-10-30 21:03:35 +000088#include "verifier/verifier_deps.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070089#include "well_known_classes.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070090
Igor Murashkin37743352014-11-13 14:38:00 -080091#include <sys/stat.h>
92#include "cmdline.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070093
Igor Murashkin37743352014-11-13 14:38:00 -080094namespace art {
Brian Carlstrom78128a62011-09-15 17:21:19 -070095
Andreas Gampe46ee31b2016-12-14 10:11:49 -080096using android::base::StringPrintf;
97
Mathieu Chartiere401d142015-04-22 13:56:20 -070098const char* image_methods_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -080099 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -0700100 "kImtConflictMethod",
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700101 "kImtUnimplementedMethod",
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100102 "kSaveAllCalleeSavesMethod",
103 "kSaveRefsOnlyMethod",
104 "kSaveRefsAndArgsMethod",
Vladimir Marko952dbb12016-07-28 12:01:51 +0100105 "kSaveEverythingMethod",
Mingyao Yang0a87a652017-04-12 13:43:15 -0700106 "kSaveEverythingMethodForClinit",
107 "kSaveEverythingMethodForSuspendCheck",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700108};
109
110const char* image_roots_descriptions_[] = {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700111 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700112 "kClassRoots",
Vladimir Markoc13fbd82018-06-04 16:16:28 +0100113 "kOomeWhenThrowingException",
114 "kOomeWhenThrowingOome",
115 "kOomeWhenHandlingStackOverflow",
116 "kNoClassDefFoundError",
Alex Light885f0c12019-01-09 13:48:55 -0800117 "kSpecialRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -0700118};
119
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700120// Map is so that we don't allocate multiple dex files for the same OatDexFile.
Andreas Gampeb40d3612018-06-26 15:49:42 -0700121static std::map<const OatDexFile*, std::unique_ptr<const DexFile>> opened_dex_files;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700122
Andreas Gampeb40d3612018-06-26 15:49:42 -0700123const DexFile* OpenDexFile(const OatDexFile* oat_dex_file, std::string* error_msg) {
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700124 DCHECK(oat_dex_file != nullptr);
125 auto it = opened_dex_files.find(oat_dex_file);
126 if (it != opened_dex_files.end()) {
127 return it->second.get();
128 }
129 const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
130 opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
131 return ret;
132}
133
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800134template <typename ElfTypes>
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100135class OatSymbolizer final {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700136 public:
David Srbecky2fdd03c2016-03-10 15:32:37 +0000137 OatSymbolizer(const OatFile* oat_file, const std::string& output_name, bool no_bits) :
138 oat_file_(oat_file),
139 builder_(nullptr),
140 output_name_(output_name.empty() ? "symbolized.oat" : output_name),
141 no_bits_(no_bits) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700142 }
143
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700144 bool Symbolize() {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000145 const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800146 std::unique_ptr<const InstructionSetFeatures> features = InstructionSetFeatures::FromBitmap(
David Srbecky5d811202016-03-08 13:21:22 +0000147 isa, oat_file_->GetOatHeader().GetInstructionSetFeaturesBitmap());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000148
Andreas Gampe08c277c2017-04-26 22:22:15 -0700149 std::unique_ptr<File> elf_file(OS::CreateEmptyFile(output_name_.c_str()));
150 if (elf_file == nullptr) {
151 return false;
152 }
Vladimir Marko74527972016-11-29 15:57:32 +0000153 std::unique_ptr<linker::BufferedOutputStream> output_stream =
154 std::make_unique<linker::BufferedOutputStream>(
155 std::make_unique<linker::FileOutputStream>(elf_file.get()));
156 builder_.reset(new linker::ElfBuilder<ElfTypes>(isa, features.get(), output_stream.get()));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000157
158 builder_->Start();
159
160 auto* rodata = builder_->GetRoData();
161 auto* text = builder_->GetText();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000162
David Srbecky6d8c8f02015-10-26 10:57:09 +0000163 const uint8_t* rodata_begin = oat_file_->Begin();
164 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
David Srbeckye155f4b2017-12-06 15:18:38 +0000165 if (!no_bits_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +0000166 rodata->Start();
167 rodata->WriteFully(rodata_begin, rodata_size);
168 rodata->End();
169 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000170
David Srbecky6d8c8f02015-10-26 10:57:09 +0000171 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
172 const size_t text_size = oat_file_->End() - text_begin;
David Srbeckye155f4b2017-12-06 15:18:38 +0000173 if (!no_bits_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +0000174 text->Start();
175 text->WriteFully(text_begin, text_size);
176 text->End();
177 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000178
Vladimir Marko33bff252017-11-01 14:35:42 +0000179 if (isa == InstructionSet::kMips || isa == InstructionSet::kMips64) {
Douglas Leung316a2182015-09-17 15:26:25 -0700180 builder_->WriteMIPSabiflagsSection();
181 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000182 builder_->PrepareDynamicSection(elf_file->GetPath(),
183 rodata_size,
184 text_size,
Vladimir Markob066d432018-01-03 13:14:37 +0000185 oat_file_->DataBimgRelRoSize(),
Vladimir Markoaad75c62016-10-03 08:46:48 +0000186 oat_file_->BssSize(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100187 oat_file_->BssMethodsOffset(),
David Srbeckyec2cdf42017-12-08 16:21:25 +0000188 oat_file_->BssRootsOffset(),
189 oat_file_->VdexSize());
Vladimir Marko944da602016-02-19 12:27:55 +0000190 builder_->WriteDynamicSection();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700191
Vladimir Marko1b404a82017-09-01 13:35:26 +0100192 const OatHeader& oat_header = oat_file_->GetOatHeader();
193 #define DO_TRAMPOLINE(fn_name) \
194 if (oat_header.Get ## fn_name ## Offset() != 0) { \
195 debug::MethodDebugInfo info = {}; \
David Srbeckyc684f332018-01-19 17:38:06 +0000196 info.custom_name = #fn_name; \
Vladimir Marko1b404a82017-09-01 13:35:26 +0100197 info.isa = oat_header.GetInstructionSet(); \
198 info.is_code_address_text_relative = true; \
199 size_t code_offset = oat_header.Get ## fn_name ## Offset(); \
200 code_offset -= CompiledCode::CodeDelta(oat_header.GetInstructionSet()); \
201 info.code_address = code_offset - oat_header.GetExecutableOffset(); \
202 info.code_size = 0; /* The symbol lasts until the next symbol. */ \
203 method_debug_infos_.push_back(std::move(info)); \
204 }
205 DO_TRAMPOLINE(InterpreterToInterpreterBridge)
206 DO_TRAMPOLINE(InterpreterToCompiledCodeBridge)
207 DO_TRAMPOLINE(JniDlsymLookup);
208 DO_TRAMPOLINE(QuickGenericJniTrampoline);
209 DO_TRAMPOLINE(QuickImtConflictTrampoline);
210 DO_TRAMPOLINE(QuickResolutionTrampoline);
211 DO_TRAMPOLINE(QuickToInterpreterBridge);
212 #undef DO_TRAMPOLINE
213
David Srbecky5d950762016-03-07 20:47:29 +0000214 Walk();
Vladimir Marko1b404a82017-09-01 13:35:26 +0100215
216 // TODO: Try to symbolize link-time thunks?
217 // This would require disassembling all methods to find branches outside the method code.
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700218
David Srbecky32210b92017-12-04 14:39:21 +0000219 // TODO: Add symbols for dex bytecode in the .dex section.
220
221 debug::DebugInfo debug_info{};
222 debug_info.compiled_methods = ArrayRef<const debug::MethodDebugInfo>(method_debug_infos_);
223
David Srbecky7370d922019-02-12 14:00:30 +0000224 debug::WriteDebugInfo(builder_.get(), debug_info);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700225
David Srbecky6d8c8f02015-10-26 10:57:09 +0000226 builder_->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700227
Andreas Gampe08c277c2017-04-26 22:22:15 -0700228 bool ret_value = builder_->Good();
229
230 builder_.reset();
231 output_stream.reset();
232
233 if (elf_file->FlushCloseOrErase() != 0) {
234 return false;
235 }
236 elf_file.reset();
237
238 return ret_value;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700239 }
240
David Srbecky5d950762016-03-07 20:47:29 +0000241 void Walk() {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700242 std::vector<const OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700243 for (size_t i = 0; i < oat_dex_files.size(); i++) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700244 const OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700245 CHECK(oat_dex_file != nullptr);
David Srbecky5d950762016-03-07 20:47:29 +0000246 WalkOatDexFile(oat_dex_file);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700247 }
248 }
249
Andreas Gampeb40d3612018-06-26 15:49:42 -0700250 void WalkOatDexFile(const OatDexFile* oat_dex_file) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700251 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700252 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
253 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700254 return;
255 }
256 for (size_t class_def_index = 0;
257 class_def_index < dex_file->NumClassDefs();
258 class_def_index++) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700259 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
260 OatClassType type = oat_class.GetType();
261 switch (type) {
262 case kOatClassAllCompiled:
263 case kOatClassSomeCompiled:
David Srbecky5d950762016-03-07 20:47:29 +0000264 WalkOatClass(oat_class, *dex_file, class_def_index);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700265 break;
266
267 case kOatClassNoneCompiled:
268 case kOatClassMax:
269 // Ignore.
270 break;
271 }
272 }
273 }
274
David Srbecky5d950762016-03-07 20:47:29 +0000275 void WalkOatClass(const OatFile::OatClass& oat_class,
276 const DexFile& dex_file,
277 uint32_t class_def_index) {
Mathieu Chartier18e26872018-06-04 17:19:02 -0700278 ClassAccessor accessor(dex_file, class_def_index);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700279 // Note: even if this is an interface or a native class, we still have to walk it, as there
280 // might be a static initializer.
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700281 uint32_t class_method_idx = 0;
Mathieu Chartier3d092992018-05-24 13:36:23 -0700282 for (const ClassAccessor::Method& method : accessor.GetMethods()) {
David Srbecky5d950762016-03-07 20:47:29 +0000283 WalkOatMethod(oat_class.GetOatMethod(class_method_idx++),
284 dex_file,
285 class_def_index,
Mathieu Chartier3d092992018-05-24 13:36:23 -0700286 method.GetIndex(),
287 method.GetCodeItem(),
288 method.GetAccessFlags());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700289 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700290 }
291
David Srbecky5d950762016-03-07 20:47:29 +0000292 void WalkOatMethod(const OatFile::OatMethod& oat_method,
293 const DexFile& dex_file,
294 uint32_t class_def_index,
295 uint32_t dex_method_index,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800296 const dex::CodeItem* code_item,
David Srbecky5d950762016-03-07 20:47:29 +0000297 uint32_t method_access_flags) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700298 if ((method_access_flags & kAccAbstract) != 0) {
299 // Abstract method, no code.
300 return;
301 }
David Srbecky5d950762016-03-07 20:47:29 +0000302 const OatHeader& oat_header = oat_file_->GetOatHeader();
303 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
304 if (method_header == nullptr || method_header->GetCodeSize() == 0) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700305 // No code.
306 return;
307 }
308
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100309 uint32_t entry_point = oat_method.GetCodeOffset() - oat_header.GetExecutableOffset();
310 // Clear Thumb2 bit.
311 const void* code_address = EntryPointToCodePointer(reinterpret_cast<void*>(entry_point));
312
Vladimir Marko1b404a82017-09-01 13:35:26 +0100313 debug::MethodDebugInfo info = {};
David Srbeckyc684f332018-01-19 17:38:06 +0000314 DCHECK(info.custom_name.empty());
David Srbecky5d950762016-03-07 20:47:29 +0000315 info.dex_file = &dex_file;
316 info.class_def_index = class_def_index;
317 info.dex_method_index = dex_method_index;
318 info.access_flags = method_access_flags;
319 info.code_item = code_item;
320 info.isa = oat_header.GetInstructionSet();
321 info.deduped = !seen_offsets_.insert(oat_method.GetCodeOffset()).second;
322 info.is_native_debuggable = oat_header.IsNativeDebuggable();
323 info.is_optimized = method_header->IsOptimized();
324 info.is_code_address_text_relative = true;
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100325 info.code_address = reinterpret_cast<uintptr_t>(code_address);
David Srbecky5d950762016-03-07 20:47:29 +0000326 info.code_size = method_header->GetCodeSize();
327 info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
328 info.code_info = info.is_optimized ? method_header->GetOptimizedCodeInfoPtr() : nullptr;
329 info.cfi = ArrayRef<uint8_t>();
330 method_debug_infos_.push_back(info);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700331 }
332
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700333 private:
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700334 const OatFile* oat_file_;
Vladimir Marko74527972016-11-29 15:57:32 +0000335 std::unique_ptr<linker::ElfBuilder<ElfTypes>> builder_;
David Srbecky5d950762016-03-07 20:47:29 +0000336 std::vector<debug::MethodDebugInfo> method_debug_infos_;
337 std::unordered_set<uint32_t> seen_offsets_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700338 const std::string output_name_;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000339 bool no_bits_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700340};
341
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700342class OatDumperOptions {
343 public:
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100344 OatDumperOptions(bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100345 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700346 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700347 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800348 const char* class_filter,
349 const char* method_filter,
350 bool list_classes,
351 bool list_methods,
David Brazdilc03d7b62016-03-02 12:18:03 +0000352 bool dump_header_only,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800353 const char* export_dex_location,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800354 const char* app_image,
355 const char* app_oat,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800356 uint32_t addr2instr)
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100357 : dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100358 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700359 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700360 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800361 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000362 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800363 list_classes_(list_classes),
364 list_methods_(list_methods),
David Brazdilc03d7b62016-03-02 12:18:03 +0000365 dump_header_only_(dump_header_only),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800366 export_dex_location_(export_dex_location),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800367 app_image_(app_image),
368 app_oat_(app_oat),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800369 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800370 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700371
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700372 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100373 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700374 const bool disassemble_code_;
375 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800376 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000377 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800378 const bool list_classes_;
379 const bool list_methods_;
David Brazdilc03d7b62016-03-02 12:18:03 +0000380 const bool dump_header_only_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800381 const char* const export_dex_location_;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800382 const char* const app_image_;
383 const char* const app_oat_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800384 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700385 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700386};
387
Elliott Hughese3c845c2012-02-28 17:23:01 -0800388class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700389 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100390 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000391 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800392 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700393 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800394 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800395 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
396 disassembler_(Disassembler::Create(instruction_set_,
Andreas Gampe372f3a32016-08-19 10:49:06 -0700397 new DisassemblerOptions(
398 options_.absolute_addresses_,
399 oat_file.Begin(),
400 oat_file.End(),
Andreas Gampe9b031f72018-10-04 11:03:34 -0700401 /* can_read_literals_= */ true,
Andreas Gampe372f3a32016-08-19 10:49:06 -0700402 Is64BitInstructionSet(instruction_set_)
403 ? &Thread::DumpThreadOffset<PointerSize::k64>
404 : &Thread::DumpThreadOffset<PointerSize::k32>))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800405 CHECK(options_.class_loader_ != nullptr);
406 CHECK(options_.class_filter_ != nullptr);
407 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800408 AddAllOffsets();
409 }
410
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700411 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700412 delete disassembler_;
413 }
414
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800415 InstructionSet GetInstructionSet() {
416 return instruction_set_;
417 }
418
Andreas Gampec55bb392018-09-21 00:02:02 +0000419 using DexFileUniqV = std::vector<std::unique_ptr<const DexFile>>;
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300420
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700421 bool Dump(std::ostream& os) {
422 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800423 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700424
425 os << "MAGIC:\n";
426 os << oat_header.GetMagic() << "\n\n";
427
Jeff Hao4b07a6e2016-01-15 12:50:44 -0800428 os << "LOCATION:\n";
429 os << oat_file_.GetLocation() << "\n\n";
430
Brian Carlstromaded5f72011-10-07 17:15:04 -0700431 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800432 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700433
Elliott Hughesa72ec822012-03-05 17:12:22 -0800434 os << "INSTRUCTION SET:\n";
435 os << oat_header.GetInstructionSet() << "\n\n";
436
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700437 {
438 std::unique_ptr<const InstructionSetFeatures> features(
439 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
440 oat_header.GetInstructionSetFeaturesBitmap()));
441 os << "INSTRUCTION SET FEATURES:\n";
442 os << features->GetFeatureString() << "\n\n";
443 }
Dave Allison70202782013-10-22 17:52:19 -0700444
Brian Carlstromaded5f72011-10-07 17:15:04 -0700445 os << "DEX FILE COUNT:\n";
446 os << oat_header.GetDexFileCount() << "\n\n";
447
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800448#define DUMP_OAT_HEADER_OFFSET(label, offset) \
449 os << label " OFFSET:\n"; \
450 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800451 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800452 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
453 } \
454 os << StringPrintf("\n\n");
455
456 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
457 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
458 GetInterpreterToInterpreterBridgeOffset);
459 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
460 GetInterpreterToCompiledCodeBridgeOffset);
461 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
462 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800463 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
464 GetQuickGenericJniTrampolineOffset);
465 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
466 GetQuickImtConflictTrampolineOffset);
467 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
468 GetQuickResolutionTrampolineOffset);
469 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
470 GetQuickToInterpreterBridgeOffset);
471#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700472
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700473 // Print the key-value store.
474 {
475 os << "KEY VALUE STORE:\n";
476 size_t index = 0;
477 const char* key;
478 const char* value;
479 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
480 os << key << " = " << value << "\n";
481 index++;
482 }
483 os << "\n";
484 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700485
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800486 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700487 os << "BEGIN:\n";
488 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700489
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700490 os << "END:\n";
491 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
492 }
493
494 os << "SIZE:\n";
495 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700496
497 os << std::flush;
498
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800499 // If set, adjust relative address to be searched
500 if (options_.addr2instr_ != 0) {
501 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
502 os << "SEARCH ADDRESS (executable offset + input):\n";
503 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
504 }
505
Vladimir Marko812fb4d2018-03-14 13:07:21 +0000506 // Dump .data.bimg.rel.ro entries.
507 DumpDataBimgRelRoEntries(os);
508
509 // Dump .bss summary, individual entries are dumped per dex file.
510 os << ".bss: ";
511 if (oat_file_.GetBssMethods().empty() && oat_file_.GetBssGcRoots().empty()) {
512 os << "empty.\n\n";
513 } else {
514 os << oat_file_.GetBssMethods().size() << " methods, ";
515 os << oat_file_.GetBssGcRoots().size() << " GC roots.\n\n";
516 }
517
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700518 // Dumping the dex file overview is compact enough to do even if header only.
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700519 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700520 const OatDexFile* oat_dex_file = oat_dex_files_[i];
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700521 CHECK(oat_dex_file != nullptr);
522 std::string error_msg;
523 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
524 if (dex_file == nullptr) {
525 os << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation() << "': "
526 << error_msg;
527 continue;
528 }
Mathieu Chartierba4c1182018-05-24 14:05:55 -0700529
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
Vladimir Markobacb8e42018-06-13 14:45:30 +0100537 if (!options_.dump_header_only_) {
538 // Dump .bss entries.
539 DumpBssEntries(
540 os,
541 "ArtMethod",
542 oat_dex_file->GetMethodBssMapping(),
543 dex_file->NumMethodIds(),
544 static_cast<size_t>(GetInstructionSetPointerSize(instruction_set_)),
545 [=](uint32_t index) { return dex_file->PrettyMethod(index); });
546 DumpBssEntries(
547 os,
548 "Class",
549 oat_dex_file->GetTypeBssMapping(),
550 dex_file->NumTypeIds(),
551 sizeof(GcRoot<mirror::Class>),
552 [=](uint32_t index) { return dex_file->PrettyType(dex::TypeIndex(index)); });
553 DumpBssEntries(
554 os,
555 "String",
556 oat_dex_file->GetStringBssMapping(),
557 dex_file->NumStringIds(),
558 sizeof(GcRoot<mirror::Class>),
559 [=](uint32_t index) { return dex_file->StringDataByIdx(dex::StringIndex(index)); });
560 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700561 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700562
David Brazdilc03d7b62016-03-02 12:18:03 +0000563 if (!options_.dump_header_only_) {
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000564 VariableIndentationOutputStream vios(&os);
Nicolas Geoffray3a293552018-03-02 10:52:16 +0000565 VdexFile::VerifierDepsHeader vdex_header = oat_file_.GetVdexFile()->GetVerifierDepsHeader();
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000566 if (vdex_header.IsValid()) {
567 std::string error_msg;
568 std::vector<const DexFile*> dex_files;
569 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
570 const DexFile* dex_file = OpenDexFile(oat_dex_files_[i], &error_msg);
571 if (dex_file == nullptr) {
572 os << "Error opening dex file: " << error_msg << std::endl;
573 return false;
574 }
575 dex_files.push_back(dex_file);
576 }
577 verifier::VerifierDeps deps(dex_files, oat_file_.GetVdexFile()->GetVerifierDepsData());
578 deps.Dump(&vios);
579 } else {
580 os << "UNRECOGNIZED vdex file, magic "
581 << vdex_header.GetMagic()
Nicolas Geoffray3a293552018-03-02 10:52:16 +0000582 << ", verifier deps version "
583 << vdex_header.GetVerifierDepsVersion()
584 << ", dex section version "
585 << vdex_header.GetDexSectionVersion()
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000586 << "\n";
587 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000588 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700589 const OatDexFile* oat_dex_file = oat_dex_files_[i];
David Brazdilc03d7b62016-03-02 12:18:03 +0000590 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300591 if (!DumpOatDexFile(os, *oat_dex_file)) {
592 success = false;
593 }
594 }
595 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800596
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300597 if (options_.export_dex_location_) {
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000598 std::string error_msg;
599 std::string vdex_filename = GetVdexFilename(oat_file_.GetLocation());
600 if (!OS::FileExists(vdex_filename.c_str())) {
601 os << "File " << vdex_filename.c_str() << " does not exist\n";
602 return false;
603 }
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300604
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000605 DexFileUniqV vdex_dex_files;
606 std::unique_ptr<const VdexFile> vdex_file = OpenVdexUnquicken(vdex_filename,
607 &vdex_dex_files,
608 &error_msg);
609 if (vdex_file.get() == nullptr) {
610 os << "Failed to open vdex file: " << error_msg << "\n";
611 return false;
612 }
613 if (oat_dex_files_.size() != vdex_dex_files.size()) {
614 os << "Dex files number in Vdex file does not match Dex files number in Oat file: "
615 << vdex_dex_files.size() << " vs " << oat_dex_files_.size() << '\n';
616 return false;
617 }
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300618
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000619 size_t i = 0;
620 for (const auto& vdex_dex_file : vdex_dex_files) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700621 const OatDexFile* oat_dex_file = oat_dex_files_[i];
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000622 CHECK(oat_dex_file != nullptr);
623 CHECK(vdex_dex_file != nullptr);
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300624
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300625 // If a CompactDex file is detected within a Vdex container, DexLayout is used to convert
626 // back to a StandardDex file. Since the converted DexFile will most likely not reproduce
627 // the original input Dex file, the `update_checksum_` option is used to recompute the
628 // checksum. If the vdex container does not contain cdex resources (`used_dexlayout` is
629 // false), ExportDexFile() enforces a reproducible checksum verification.
630 if (vdex_dex_file->IsCompactDexFile()) {
631 Options options;
632 options.compact_dex_level_ = CompactDexLevel::kCompactDexLevelNone;
633 options.update_checksum_ = true;
Andreas Gampe9b031f72018-10-04 11:03:34 -0700634 DexLayout dex_layout(options, /*info=*/ nullptr, /*out_file=*/ nullptr, /*header=*/ nullptr);
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300635 std::unique_ptr<art::DexContainer> dex_container;
636 bool result = dex_layout.ProcessDexFile(vdex_dex_file->GetLocation().c_str(),
637 vdex_dex_file.get(),
638 i,
639 &dex_container,
640 &error_msg);
641 if (!result) {
642 os << "DexLayout failed to process Dex file: " + error_msg;
643 success = false;
644 break;
645 }
646 DexContainer::Section* main_section = dex_container->GetMainSection();
647 CHECK_EQ(dex_container->GetDataSection()->Size(), 0u);
648
649 const ArtDexFileLoader dex_file_loader;
650 std::unique_ptr<const DexFile> dex(dex_file_loader.Open(
651 main_section->Begin(),
652 main_section->Size(),
653 vdex_dex_file->GetLocation(),
654 vdex_file->GetLocationChecksum(i),
Andreas Gampe9b031f72018-10-04 11:03:34 -0700655 /*oat_dex_file=*/ nullptr,
656 /*verify=*/ false,
657 /*verify_checksum=*/ true,
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300658 &error_msg));
659 if (dex == nullptr) {
660 os << "Failed to load DexFile from layout container: " + error_msg;
661 success = false;
662 break;
663 }
664 if (dex->IsCompactDexFile()) {
665 os <<"CompactDex conversion to StandardDex failed";
666 success = false;
667 break;
668 }
669
Andreas Gampe9b031f72018-10-04 11:03:34 -0700670 if (!ExportDexFile(os, *oat_dex_file, dex.get(), /*used_dexlayout=*/ true)) {
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300671 success = false;
672 break;
673 }
674 } else {
Andreas Gampe9b031f72018-10-04 11:03:34 -0700675 if (!ExportDexFile(os, *oat_dex_file, vdex_dex_file.get(), /*used_dexlayout=*/ false)) {
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300676 success = false;
677 break;
678 }
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300679 }
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000680 i++;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700681 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700682 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000683
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800684 {
685 os << "OAT FILE STATS:\n";
686 VariableIndentationOutputStream vios(&os);
David Srbecky86decb62018-06-05 06:41:10 +0100687 stats_.AddBytes(oat_file_.Size());
688 DumpStats(vios, "OatFile", stats_, stats_.Value());
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800689 }
690
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700691 os << std::flush;
692 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700693 }
694
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800695 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700696 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
697 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800698 return 0; // Address not in oat file
699 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800700 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
701 reinterpret_cast<uintptr_t>(oat_file_.Begin());
702 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800703 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800704 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800705 return end_offset - begin_offset;
706 }
707
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800708 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700709 return oat_file_.GetOatHeader().GetInstructionSet();
710 }
711
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700712 const void* GetQuickOatCode(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800713 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700714 const OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700715 CHECK(oat_dex_file != nullptr);
716 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700717 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
718 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700719 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
720 << "': " << error_msg;
721 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800722 const char* descriptor = m->GetDeclaringClassDescriptor();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800723 const dex::ClassDef* class_def =
David Sehr9aa352e2016-09-15 18:13:52 -0700724 OatDexFile::FindClassDef(*dex_file, descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700725 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700726 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100727 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800728 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100729 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800730 }
731 }
732 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700733 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800734 }
735
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300736 // Returns nullptr and updates error_msg if the Vdex file cannot be opened, otherwise all Dex
737 // files are fully unquickened and stored in dex_files
738 std::unique_ptr<const VdexFile> OpenVdexUnquicken(const std::string& vdex_filename,
739 /* out */ DexFileUniqV* dex_files,
740 /* out */ std::string* error_msg) {
741 std::unique_ptr<const File> file(OS::OpenFileForReading(vdex_filename.c_str()));
742 if (file == nullptr) {
743 *error_msg = "Could not open file " + vdex_filename + " for reading.";
744 return nullptr;
745 }
746
747 int64_t vdex_length = file->GetLength();
748 if (vdex_length == -1) {
749 *error_msg = "Could not read the length of file " + vdex_filename;
750 return nullptr;
751 }
752
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100753 MemMap mmap = MemMap::MapFile(
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300754 file->GetLength(),
755 PROT_READ | PROT_WRITE,
756 MAP_PRIVATE,
757 file->Fd(),
Andreas Gampe9b031f72018-10-04 11:03:34 -0700758 /* start offset= */ 0,
759 /* low_4gb= */ false,
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300760 vdex_filename.c_str(),
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100761 error_msg);
762 if (!mmap.IsValid()) {
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300763 *error_msg = "Failed to mmap file " + vdex_filename + ": " + *error_msg;
764 return nullptr;
765 }
766
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100767 std::unique_ptr<VdexFile> vdex_file(new VdexFile(std::move(mmap)));
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300768 if (!vdex_file->IsValid()) {
769 *error_msg = "Vdex file is not valid";
770 return nullptr;
771 }
772
773 DexFileUniqV tmp_dex_files;
774 if (!vdex_file->OpenAllDexFiles(&tmp_dex_files, error_msg)) {
775 *error_msg = "Failed to open Dex files from Vdex: " + *error_msg;
776 return nullptr;
777 }
778
779 vdex_file->Unquicken(MakeNonOwningPointerVector(tmp_dex_files),
Andreas Gampe9b031f72018-10-04 11:03:34 -0700780 /* decompile_return_instruction= */ true);
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300781
782 *dex_files = std::move(tmp_dex_files);
783 return vdex_file;
784 }
785
David Srbecky86decb62018-06-05 06:41:10 +0100786 bool AddStatsObject(const void* address) {
787 return seen_stats_objects_.insert(address).second; // Inserted new entry.
788 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800789
David Srbecky86decb62018-06-05 06:41:10 +0100790 void DumpStats(VariableIndentationOutputStream& os,
791 const std::string& name,
792 const Stats& stats,
793 double total) {
794 if (std::fabs(stats.Value()) > 0 || !stats.Children().empty()) {
795 double percent = 100.0 * stats.Value() / total;
796 os.Stream()
797 << std::setw(40 - os.GetIndentation()) << std::left << name << std::right << " "
798 << std::setw(8) << stats.Count() << " "
799 << std::setw(12) << std::fixed << std::setprecision(3) << stats.Value() / KB << "KB "
800 << std::setw(8) << std::fixed << std::setprecision(1) << percent << "%\n";
801
802 // Sort all children by largest value first, than by name.
803 std::map<std::pair<double, std::string>, const Stats&> sorted_children;
804 for (const auto& it : stats.Children()) {
805 sorted_children.emplace(std::make_pair(-it.second.Value(), it.first), it.second);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800806 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800807
David Srbecky86decb62018-06-05 06:41:10 +0100808 // Add "other" row to represent any amount not account for by the children.
809 Stats other;
810 other.AddBytes(stats.Value() - stats.SumChildrenValues(), stats.Count());
811 if (std::fabs(other.Value()) > 0 && !stats.Children().empty()) {
812 sorted_children.emplace(std::make_pair(-other.Value(), "(other)"), other);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800813 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800814
David Srbecky86decb62018-06-05 06:41:10 +0100815 // Print the data.
816 ScopedIndentation indent1(&os);
817 for (const auto& it : sorted_children) {
818 DumpStats(os, it.first.second, it.second, total);
819 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800820 }
David Srbecky86decb62018-06-05 06:41:10 +0100821 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800822
Brian Carlstromaded5f72011-10-07 17:15:04 -0700823 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800824 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800825 // We don't know the length of the code for each method, but we need to know where to stop
826 // when disassembling. What we do know is that a region of code will be followed by some other
827 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
828 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800829 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700830 const OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700831 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700832 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700833 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
834 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700835 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
836 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800837 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800838 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800839 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Mathieu Chartier3d092992018-05-24 13:36:23 -0700840 for (ClassAccessor accessor : dex_file->GetClasses()) {
Mathieu Chartier18e26872018-06-04 17:19:02 -0700841 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(accessor.GetClassDefIndex());
Mathieu Chartier3d092992018-05-24 13:36:23 -0700842 for (uint32_t class_method_index = 0;
843 class_method_index < accessor.NumMethods();
844 ++class_method_index) {
845 AddOffsets(oat_class.GetOatMethod(class_method_index));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800846 }
847 }
848 }
849
850 // If the last thing in the file is code for a method, there won't be an offset for the "next"
851 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
852 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800853 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800854 }
855
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700856 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
857 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
858 }
859
Elliott Hughese3c845c2012-02-28 17:23:01 -0800860 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800861 uint32_t code_offset = oat_method.GetCodeOffset();
Vladimir Marko33bff252017-11-01 14:35:42 +0000862 if (oat_file_.GetOatHeader().GetInstructionSet() == InstructionSet::kThumb2) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800863 code_offset &= ~0x1;
864 }
865 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800866 offsets_.insert(oat_method.GetVmapTableOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800867 }
868
Andreas Gampeb40d3612018-06-26 15:49:42 -0700869 bool DumpOatDexFile(std::ostream& os, const OatDexFile& oat_dex_file) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700870 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800871 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700872 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800873 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800874 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700875
Andreas Gampe2ba88952016-04-29 17:52:07 -0700876 const uint8_t* const oat_file_begin = oat_dex_file.GetOatFile()->Begin();
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000877 if (oat_dex_file.GetOatFile()->ContainsDexCode()) {
878 const uint8_t* const vdex_file_begin = oat_dex_file.GetOatFile()->DexBegin();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100879
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000880 // Print data range of the dex file embedded inside the corresponding vdex file.
881 const uint8_t* const dex_file_pointer = oat_dex_file.GetDexFilePointer();
882 uint32_t dex_offset = dchecked_integral_cast<uint32_t>(dex_file_pointer - vdex_file_begin);
883 os << StringPrintf(
884 "dex-file: 0x%08x..0x%08x\n",
885 dex_offset,
886 dchecked_integral_cast<uint32_t>(dex_offset + oat_dex_file.FileSize() - 1));
887 } else {
888 os << StringPrintf("dex-file not in VDEX file\n");
889 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700890
Andreas Gampe2ba88952016-04-29 17:52:07 -0700891 // Create the dex file early. A lot of print-out things depend on it.
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 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700896 os << std::flush;
897 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700898 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100899
Andreas Gampe2ba88952016-04-29 17:52:07 -0700900 // Print lookup table, if it exists.
901 if (oat_dex_file.GetLookupTableData() != nullptr) {
902 uint32_t table_offset = dchecked_integral_cast<uint32_t>(
903 oat_dex_file.GetLookupTableData() - oat_file_begin);
David Sehr9aa352e2016-09-15 18:13:52 -0700904 uint32_t table_size = TypeLookupTable::RawDataLength(dex_file->NumClassDefs());
Andreas Gampe2ba88952016-04-29 17:52:07 -0700905 os << StringPrintf("type-table: 0x%08x..0x%08x\n",
906 table_offset,
907 table_offset + table_size - 1);
908 }
909
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100910 VariableIndentationOutputStream vios(&os);
911 ScopedIndentation indent1(&vios);
Mathieu Chartier98dad402018-06-04 18:00:12 -0700912 for (ClassAccessor accessor : dex_file->GetClasses()) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800913 // TODO: Support regex
Mathieu Chartier98dad402018-06-04 18:00:12 -0700914 const char* descriptor = accessor.GetDescriptor();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800915 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
916 continue;
917 }
918
Mathieu Chartier98dad402018-06-04 18:00:12 -0700919 const uint16_t class_def_index = accessor.GetClassDefIndex();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700920 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100921 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700922 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
Mathieu Chartier98dad402018-06-04 18:00:12 -0700923 static_cast<ssize_t>(class_def_index),
924 descriptor,
925 oat_class_offset,
926 accessor.GetClassIdx().index_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100927 << " (" << oat_class.GetStatus() << ")"
928 << " (" << oat_class.GetType() << ")\n";
929 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700930 if (options_.list_classes_) {
931 continue;
932 }
Mathieu Chartier98dad402018-06-04 18:00:12 -0700933 if (!DumpOatClass(&vios, oat_class, *dex_file, accessor, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700934 success = false;
935 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800936 if (stop_analysis) {
937 os << std::flush;
938 return success;
939 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700940 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700941 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700942 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700943 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700944 }
945
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300946 // Backwards compatible Dex file export. If dex_file is nullptr (valid Vdex file not present) the
947 // Dex resource is extracted from the oat_dex_file and its checksum is repaired since it's not
948 // unquickened. Otherwise the dex_file has been fully unquickened and is expected to verify the
949 // original checksum.
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300950 bool ExportDexFile(std::ostream& os,
951 const OatDexFile& oat_dex_file,
952 const DexFile* dex_file,
953 bool used_dexlayout) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800954 std::string error_msg;
955 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300956
957 // If dex_file (from unquicken or dexlayout) is not available, the output DexFile size is the
958 // same as the one extracted from the Oat container (pre-oreo)
959 size_t fsize = dex_file == nullptr ? oat_dex_file.FileSize() : dex_file->Size();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800960
961 // Some quick checks just in case
962 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
963 os << "Invalid dex file\n";
964 return false;
965 }
966
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300967 if (dex_file == nullptr) {
968 // Exported bytecode is quickened (dex-to-dex transformations present)
969 dex_file = OpenDexFile(&oat_dex_file, &error_msg);
970 if (dex_file == nullptr) {
971 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
972 return false;
973 }
974
975 // Recompute checksum
976 reinterpret_cast<DexFile::Header*>(const_cast<uint8_t*>(dex_file->Begin()))->checksum_ =
977 dex_file->CalculateChecksum();
978 } else {
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300979 // If dexlayout was used to convert CompactDex back to StandardDex, checksum will be updated
980 // due to `update_checksum_` option, otherwise we expect a reproducible checksum.
981 if (!used_dexlayout) {
982 // Vdex unquicken output should match original input bytecode
983 uint32_t orig_checksum =
984 reinterpret_cast<DexFile::Header*>(const_cast<uint8_t*>(dex_file->Begin()))->checksum_;
985 if (orig_checksum != dex_file->CalculateChecksum()) {
986 os << "Unexpected checksum from unquicken dex file '" << dex_file_location << "'\n";
987 return false;
988 }
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300989 }
990 }
991
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800992 // Verify output directory exists
993 if (!OS::DirectoryExists(options_.export_dex_location_)) {
994 // TODO: Extend OS::DirectoryExists if symlink support is required
995 os << options_.export_dex_location_ << " output directory not found or symlink\n";
996 return false;
997 }
998
999 // Beautify path names
1000 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
1001 return false;
1002 }
1003
1004 std::string dex_orig_name;
1005 size_t dex_orig_pos = dex_file_location.rfind('/');
1006 if (dex_orig_pos == std::string::npos)
1007 dex_orig_name = dex_file_location;
1008 else
1009 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
1010
1011 // A more elegant approach to efficiently name user installed apps is welcome
Andreas Gampef812d8c2017-02-17 10:19:44 -08001012 if (dex_orig_name.size() == 8 &&
1013 dex_orig_name.compare("base.apk") == 0 &&
1014 dex_orig_pos != std::string::npos) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001015 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
1016 size_t apk_orig_pos = dex_file_location.rfind('/');
1017 if (apk_orig_pos != std::string::npos) {
1018 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
1019 }
1020 }
1021
1022 std::string out_dex_path(options_.export_dex_location_);
1023 if (out_dex_path.back() != '/') {
1024 out_dex_path.append("/");
1025 }
1026 out_dex_path.append(dex_orig_name);
1027 out_dex_path.append("_export.dex");
1028 if (out_dex_path.length() > PATH_MAX) {
1029 return false;
1030 }
1031
1032 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
1033 if (file.get() == nullptr) {
1034 os << "Failed to open output dex file " << out_dex_path;
1035 return false;
1036 }
1037
Mathieu Chartier567dc6f2018-04-05 16:37:14 -07001038 bool success = file->WriteFully(dex_file->Begin(), fsize);
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001039 if (!success) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001040 os << "Failed to write dex file";
1041 file->Erase();
1042 return false;
1043 }
1044
1045 if (file->FlushCloseOrErase() != 0) {
1046 os << "Flush and close failed";
1047 return false;
1048 }
1049
1050 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
1051 os << std::flush;
1052
1053 return true;
1054 }
1055
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001056 bool DumpOatClass(VariableIndentationOutputStream* vios,
Mathieu Chartier98dad402018-06-04 18:00:12 -07001057 const OatFile::OatClass& oat_class,
1058 const DexFile& dex_file,
1059 const ClassAccessor& class_accessor,
1060 bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001061 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001062 bool addr_found = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001063 uint32_t class_method_index = 0;
Mathieu Chartier98dad402018-06-04 18:00:12 -07001064 for (const ClassAccessor::Method& method : class_accessor.GetMethods()) {
1065 if (!DumpOatMethod(vios,
1066 dex_file.GetClassDef(class_accessor.GetClassDefIndex()),
1067 class_method_index,
1068 oat_class,
1069 dex_file,
1070 method.GetIndex(),
1071 method.GetCodeItem(),
David Brazdil20c765f2018-10-27 21:45:15 +00001072 method.GetAccessFlags(),
Mathieu Chartier98dad402018-06-04 18:00:12 -07001073 &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001074 success = false;
1075 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001076 if (addr_found) {
1077 *stop_analysis = true;
1078 return success;
1079 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001080 class_method_index++;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001081 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001082 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001083 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001084 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001085
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001086 static constexpr uint32_t kPrologueBytes = 16;
1087
1088 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
1089 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
1090
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001091 bool DumpOatMethod(VariableIndentationOutputStream* vios,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001092 const dex::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -07001093 uint32_t class_method_index,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001094 const OatFile::OatClass& oat_class,
1095 const DexFile& dex_file,
1096 uint32_t dex_method_idx,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001097 const dex::CodeItem* code_item,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001098 uint32_t method_access_flags,
1099 bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001100 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001101
Mathieu Chartier698ebbc2018-01-05 11:00:42 -08001102 CodeItemDataAccessor code_item_accessor(dex_file, code_item);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001103
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001104 // TODO: Support regex
1105 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
1106 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00001107 return success;
1108 }
1109
David Sehr709b0702016-10-13 09:12:37 -07001110 std::string pretty_method = dex_file.PrettyMethod(dex_method_idx, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001111 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
1112 class_method_index, pretty_method.c_str(),
1113 dex_method_idx);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001114 if (options_.list_methods_) {
1115 return success;
1116 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001117
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001118 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
1119 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
1120 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
1121 uint32_t code_offset = oat_method.GetCodeOffset();
1122 uint32_t code_size = oat_method.GetQuickCodeSize();
1123 if (resolved_addr2instr_ != 0) {
1124 if (resolved_addr2instr_ > code_offset + code_size) {
1125 return success;
1126 } else {
1127 *addr_found = true; // stop analyzing file at next iteration
1128 }
1129 }
1130
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001131 // Everything below is indented at least once.
1132 ScopedIndentation indent1(vios);
1133
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001134 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001135 vios->Stream() << "DEX CODE:\n";
1136 ScopedIndentation indent2(vios);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001137 if (code_item_accessor.HasCodeItem()) {
1138 for (const DexInstructionPcPair& inst : code_item_accessor) {
1139 vios->Stream() << StringPrintf("0x%04x: ", inst.DexPc()) << inst->DumpHexLE(5)
1140 << StringPrintf("\t| %s\n", inst->DumpString(&dex_file).c_str());
1141 }
1142 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001143 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001144
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001145 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001146 std::unique_ptr<verifier::MethodVerifier> verifier;
1147 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001148 // We need to have the handle scope stay live until after the verifier since the verifier has
1149 // a handle to the dex cache from hs.
1150 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001151 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
1152 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001153 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001154 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001155 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -07001156 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001157 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001158 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001159 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001160 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001161 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001162 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001163 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001164 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001165 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
1166 oat_method_offsets_offset, oat_file_.Size());
1167 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001168 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001169 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001170 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001171
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001172 ScopedIndentation indent2(vios);
1173 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001174 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
1175 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001176 vios->Stream() << StringPrintf("WARNING: "
1177 "code offset 0x%08x is past end of file 0x%08zx.\n",
1178 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001179 success = false;
1180 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001181 vios->Stream() << "\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001182 }
1183 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001184 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001185 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
1186 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
David Srbecky86decb62018-06-05 06:41:10 +01001187 if (AddStatsObject(method_header)) {
1188 stats_.Child("QuickMethodHeader")->AddBytes(sizeof(*method_header));
1189 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001190 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001191 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001192 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001193 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001194 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001195 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001196 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
1197 method_header_offset, oat_file_.Size());
1198 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001199 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001200 return false;
1201 }
1202
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001203 ScopedIndentation indent2(vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001204 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001205 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001206 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001207 }
Mingyao Yang063fc772016-08-02 11:02:54 -07001208 uint32_t vmap_table_offset = method_header ==
1209 nullptr ? 0 : method_header->GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001210 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001211
1212 size_t vmap_table_offset_limit =
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +00001213 IsMethodGeneratedByDexToDexCompiler(oat_method, code_item_accessor)
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001214 ? oat_file_.GetVdexFile()->Size()
1215 : method_header->GetCode() - oat_file_.Begin();
1216 if (vmap_table_offset >= vmap_table_offset_limit) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001217 vios->Stream() << StringPrintf("WARNING: "
1218 "vmap table offset 0x%08x is past end of file 0x%08zx. "
1219 "vmap table offset was loaded from offset 0x%08x.\n",
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001220 vmap_table_offset,
1221 vmap_table_offset_limit,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001222 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001223 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001224 } else if (options_.dump_vmap_) {
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001225 DumpVmapData(vios, oat_method, code_item_accessor);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001226 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001227 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001228 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001229 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001230
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001231 ScopedIndentation indent2(vios);
1232 vios->Stream()
1233 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
1234 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
1235 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
1236 vios->Stream() << "\n";
1237 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
1238 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
1239 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001240 }
1241 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001242 // Based on spill masks from QuickMethodFrameInfo so placed
1243 // after it is dumped, but useful for understanding quick
1244 // code, so dumped here.
1245 ScopedIndentation indent2(vios);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001246 DumpVregLocations(vios->Stream(), oat_method, code_item_accessor);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001247 }
1248 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001249 vios->Stream() << "CODE: ";
David Srbecky131f23a2019-01-29 18:52:12 +00001250 const void* code = oat_method.GetQuickCode();
1251 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1252 uint64_t aligned_code_end = aligned_code_begin + code_size;
1253 if (AddStatsObject(code)) {
1254 stats_.Child("Code")->AddBytes(code_size);
1255 }
1256
1257 if (options_.absolute_addresses_) {
1258 vios->Stream() << StringPrintf("%p ", code);
1259 }
1260 vios->Stream() << StringPrintf("(code_offset=0x%08x size=%u)%s\n",
1261 code_offset,
1262 code_size,
1263 code != nullptr ? "..." : "");
1264
1265 ScopedIndentation indent2(vios);
1266 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001267 vios->Stream() << StringPrintf("WARNING: "
David Srbecky131f23a2019-01-29 18:52:12 +00001268 "start of code at 0x%08x is past end of file 0x%08zx.",
1269 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001270 success = false;
David Srbecky131f23a2019-01-29 18:52:12 +00001271 } else if (aligned_code_end > oat_file_.Size()) {
1272 vios->Stream() << StringPrintf(
1273 "WARNING: "
1274 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
1275 "code size is 0x%08x.\n",
1276 aligned_code_end, oat_file_.Size(),
1277 code_size);
1278 success = false;
1279 } else if (code_size > kMaxCodeSize) {
1280 vios->Stream() << StringPrintf(
1281 "WARNING: "
1282 "code size %d is bigger than max expected threshold of %d. "
1283 "code size is 0x%08x.\n",
1284 code_size, kMaxCodeSize,
1285 code_size);
1286 success = false;
1287 } else if (options_.disassemble_code_) {
1288 DumpCode(vios, oat_method, code_item_accessor, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001289 }
1290 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001291 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001292 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001293 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001294
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001295 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1296 if (spill_mask == 0) {
1297 return;
1298 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001299 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001300 for (size_t i = 0; i < 32; i++) {
1301 if ((spill_mask & (1 << i)) != 0) {
1302 if (is_float) {
1303 os << "fr" << i;
1304 } else {
1305 os << "r" << i;
1306 }
1307 spill_mask ^= 1 << i; // clear bit
1308 if (spill_mask != 0) {
1309 os << ", ";
1310 } else {
1311 break;
1312 }
1313 }
1314 }
1315 os << ")";
1316 }
1317
Roland Levillain442b46a2015-02-18 16:54:21 +00001318 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001319 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001320 const OatFile::OatMethod& oat_method,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001321 const CodeItemDataAccessor& code_item_accessor) {
1322 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item_accessor)) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001323 // The optimizing compiler outputs its CodeInfo data in the vmap table.
David Srbecky6ee06e92018-07-25 21:45:54 +01001324 const uint8_t* raw_code_info = oat_method.GetVmapTable();
Roland Levillain442b46a2015-02-18 16:54:21 +00001325 if (raw_code_info != nullptr) {
1326 CodeInfo code_info(raw_code_info);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001327 DCHECK(code_item_accessor.HasCodeItem());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001328 ScopedIndentation indent1(vios);
David Srbecky8cd54542018-07-15 23:58:44 +01001329 DumpCodeInfo(vios, code_info, oat_method);
Roland Levillain442b46a2015-02-18 16:54:21 +00001330 }
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001331 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item_accessor)) {
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001332 // We don't encode the size in the table, so just emit that we have quickened
1333 // information.
1334 ScopedIndentation indent(vios);
1335 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001336 } else {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001337 // Otherwise, there is nothing to display.
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001338 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001339 }
1340
1341 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001342 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001343 const CodeInfo& code_info,
David Srbecky8cd54542018-07-15 23:58:44 +01001344 const OatFile::OatMethod& oat_method) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001345 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001346 oat_method.GetCodeOffset(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001347 options_.dump_code_info_stack_maps_,
David Srbecky8cd54542018-07-15 23:58:44 +01001348 instruction_set_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001349 }
1350
Andreas Gampe36a296f2017-06-13 14:11:11 -07001351 static int GetOutVROffset(uint16_t out_num, InstructionSet isa) {
1352 // According to stack model, the first out is above the Method referernce.
1353 return static_cast<size_t>(InstructionSetPointerSize(isa)) + out_num * sizeof(uint32_t);
1354 }
1355
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001356 static uint32_t GetVRegOffsetFromQuickCode(const CodeItemDataAccessor& code_item_accessor,
Andreas Gampe36a296f2017-06-13 14:11:11 -07001357 uint32_t core_spills,
1358 uint32_t fp_spills,
1359 size_t frame_size,
1360 int reg,
1361 InstructionSet isa) {
1362 PointerSize pointer_size = InstructionSetPointerSize(isa);
1363 if (kIsDebugBuild) {
1364 auto* runtime = Runtime::Current();
1365 if (runtime != nullptr) {
1366 CHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), pointer_size);
1367 }
1368 }
1369 DCHECK_ALIGNED(frame_size, kStackAlignment);
1370 DCHECK_NE(reg, -1);
1371 int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa)
1372 + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa)
1373 + sizeof(uint32_t); // Filler.
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001374 int num_regs = code_item_accessor.RegistersSize() - code_item_accessor.InsSize();
1375 int temp_threshold = code_item_accessor.RegistersSize();
Andreas Gampe36a296f2017-06-13 14:11:11 -07001376 const int max_num_special_temps = 1;
1377 if (reg == temp_threshold) {
1378 // The current method pointer corresponds to special location on stack.
1379 return 0;
1380 } else if (reg >= temp_threshold + max_num_special_temps) {
1381 /*
1382 * Special temporaries may have custom locations and the logic above deals with that.
1383 * However, non-special temporaries are placed relative to the outs.
1384 */
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001385 int temps_start = code_item_accessor.OutsSize() * sizeof(uint32_t)
Andreas Gampe36a296f2017-06-13 14:11:11 -07001386 + static_cast<size_t>(pointer_size) /* art method */;
1387 int relative_offset = (reg - (temp_threshold + max_num_special_temps)) * sizeof(uint32_t);
1388 return temps_start + relative_offset;
1389 } else if (reg < num_regs) {
1390 int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t);
1391 return locals_start + (reg * sizeof(uint32_t));
1392 } else {
1393 // Handle ins.
1394 return frame_size + ((reg - num_regs) * sizeof(uint32_t))
1395 + static_cast<size_t>(pointer_size) /* art method */;
1396 }
1397 }
1398
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001399 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001400 const CodeItemDataAccessor& code_item_accessor) {
1401 if (code_item_accessor.HasCodeItem()) {
1402 size_t num_locals_ins = code_item_accessor.RegistersSize();
1403 size_t num_ins = code_item_accessor.InsSize();
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001404 size_t num_locals = num_locals_ins - num_ins;
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001405 size_t num_outs = code_item_accessor.OutsSize();
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001406
1407 os << "vr_stack_locations:";
1408 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1409 // For readability, delimit the different kinds of VRs.
1410 if (reg == num_locals_ins) {
1411 os << "\n\tmethod*:";
1412 } else if (reg == num_locals && num_ins > 0) {
1413 os << "\n\tins:";
1414 } else if (reg == 0 && num_locals > 0) {
1415 os << "\n\tlocals:";
1416 }
1417
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001418 uint32_t offset = GetVRegOffsetFromQuickCode(code_item_accessor,
Andreas Gampe36a296f2017-06-13 14:11:11 -07001419 oat_method.GetCoreSpillMask(),
1420 oat_method.GetFpSpillMask(),
1421 oat_method.GetFrameSizeInBytes(),
1422 reg,
1423 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001424 os << " v" << reg << "[sp + #" << offset << "]";
1425 }
1426
1427 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1428 if (out_reg == 0) {
1429 os << "\n\touts:";
1430 }
1431
Andreas Gampe36a296f2017-06-13 14:11:11 -07001432 uint32_t offset = GetOutVROffset(out_reg, GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001433 os << " v" << out_reg << "[sp + #" << offset << "]";
1434 }
1435
1436 os << "\n";
1437 }
1438 }
1439
Roland Levillainf2650d12015-05-28 14:53:28 +01001440 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1441 // the optimizing compiler?
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001442 static bool IsMethodGeneratedByOptimizingCompiler(
1443 const OatFile::OatMethod& oat_method,
1444 const CodeItemDataAccessor& code_item_accessor) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001445 // If the native GC map is null and the Dex `code_item` is not
1446 // null, then this method has been compiled with the optimizing
1447 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001448 return oat_method.GetQuickCode() != nullptr &&
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001449 oat_method.GetVmapTable() != nullptr &&
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001450 code_item_accessor.HasCodeItem();
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001451 }
1452
1453 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1454 // the dextodex compiler?
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001455 static bool IsMethodGeneratedByDexToDexCompiler(
1456 const OatFile::OatMethod& oat_method,
1457 const CodeItemDataAccessor& code_item_accessor) {
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001458 // If the quick code is null, the Dex `code_item` is not
1459 // null, and the vmap table is not null, then this method has been compiled
1460 // with the dextodex compiler.
1461 return oat_method.GetQuickCode() == nullptr &&
1462 oat_method.GetVmapTable() != nullptr &&
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001463 code_item_accessor.HasCodeItem();
Roland Levillainf2650d12015-05-28 14:53:28 +01001464 }
1465
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001466 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001467 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001468 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001469 const DexFile* dex_file,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001470 const dex::ClassDef& class_def,
1471 const dex::CodeItem* code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001472 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001473 if ((method_access_flags & kAccNative) == 0) {
1474 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001475 Runtime* const runtime = Runtime::Current();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001476 DCHECK(options_.class_loader_ != nullptr);
Vladimir Marko421087b2018-02-27 11:00:17 +00001477 Handle<mirror::DexCache> dex_cache = hs->NewHandle(
1478 runtime->GetClassLinker()->RegisterDexFile(*dex_file, options_.class_loader_->Get()));
1479 CHECK(dex_cache != nullptr);
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00001480 ArtMethod* method = runtime->GetClassLinker()->ResolveMethodWithoutInvokeType(
1481 dex_method_idx, dex_cache, *options_.class_loader_);
Andreas Gampe03da7842018-04-12 11:12:52 -07001482 if (method == nullptr) {
1483 soa.Self()->ClearException();
1484 return nullptr;
1485 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001486 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001487 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
Andreas Gampe9b031f72018-10-04 11:03:34 -07001488 class_def, code_item, method, method_access_flags, /* api_level= */ 0);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001489 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001490
1491 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001492 }
1493
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001494 // The StackMapsHelper provides the stack maps in the native PC order.
1495 // For identical native PCs, the order from the CodeInfo is preserved.
1496 class StackMapsHelper {
1497 public:
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001498 explicit StackMapsHelper(const uint8_t* raw_code_info, InstructionSet instruction_set)
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001499 : code_info_(raw_code_info),
David Srbecky052f8ca2018-04-26 15:42:54 +01001500 number_of_stack_maps_(code_info_.GetNumberOfStackMaps()),
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001501 indexes_(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001502 offset_(static_cast<uint32_t>(-1)),
1503 stack_map_index_(0u),
1504 instruction_set_(instruction_set) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001505 if (number_of_stack_maps_ != 0u) {
1506 // Check if native PCs are ordered.
1507 bool ordered = true;
David Srbecky052f8ca2018-04-26 15:42:54 +01001508 StackMap last = code_info_.GetStackMapAt(0u);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001509 for (size_t i = 1; i != number_of_stack_maps_; ++i) {
David Srbecky052f8ca2018-04-26 15:42:54 +01001510 StackMap current = code_info_.GetStackMapAt(i);
1511 if (last.GetNativePcOffset(instruction_set) >
1512 current.GetNativePcOffset(instruction_set)) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001513 ordered = false;
1514 break;
1515 }
1516 last = current;
1517 }
1518 if (!ordered) {
1519 // Create indirection indexes for access in native PC order. We do not optimize
1520 // for the fact that there can currently be only two separately ordered ranges,
1521 // namely normal stack maps and catch-point stack maps.
1522 indexes_.resize(number_of_stack_maps_);
1523 std::iota(indexes_.begin(), indexes_.end(), 0u);
1524 std::sort(indexes_.begin(),
1525 indexes_.end(),
1526 [this](size_t lhs, size_t rhs) {
David Srbecky052f8ca2018-04-26 15:42:54 +01001527 StackMap left = code_info_.GetStackMapAt(lhs);
1528 uint32_t left_pc = left.GetNativePcOffset(instruction_set_);
1529 StackMap right = code_info_.GetStackMapAt(rhs);
1530 uint32_t right_pc = right.GetNativePcOffset(instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001531 // If the PCs are the same, compare indexes to preserve the original order.
1532 return (left_pc < right_pc) || (left_pc == right_pc && lhs < rhs);
1533 });
1534 }
David Srbecky052f8ca2018-04-26 15:42:54 +01001535 offset_ = GetStackMapAt(0).GetNativePcOffset(instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001536 }
1537 }
1538
1539 const CodeInfo& GetCodeInfo() const {
1540 return code_info_;
1541 }
1542
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001543 uint32_t GetOffset() const {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001544 return offset_;
1545 }
1546
1547 StackMap GetStackMap() const {
1548 return GetStackMapAt(stack_map_index_);
1549 }
1550
1551 void Next() {
1552 ++stack_map_index_;
1553 offset_ = (stack_map_index_ == number_of_stack_maps_)
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001554 ? static_cast<uint32_t>(-1)
David Srbecky052f8ca2018-04-26 15:42:54 +01001555 : GetStackMapAt(stack_map_index_).GetNativePcOffset(instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001556 }
1557
1558 private:
1559 StackMap GetStackMapAt(size_t i) const {
1560 if (!indexes_.empty()) {
1561 i = indexes_[i];
1562 }
1563 DCHECK_LT(i, number_of_stack_maps_);
David Srbecky052f8ca2018-04-26 15:42:54 +01001564 return code_info_.GetStackMapAt(i);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001565 }
1566
1567 const CodeInfo code_info_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001568 const size_t number_of_stack_maps_;
1569 dchecked_vector<size_t> indexes_; // Used if stack map native PCs are not ordered.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001570 uint32_t offset_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001571 size_t stack_map_index_;
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001572 const InstructionSet instruction_set_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001573 };
1574
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001575 void DumpCode(VariableIndentationOutputStream* vios,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001576 const OatFile::OatMethod& oat_method,
1577 const CodeItemDataAccessor& code_item_accessor,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001578 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001579 const void* quick_code = oat_method.GetQuickCode();
1580
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001581 if (code_size == 0) {
1582 code_size = oat_method.GetQuickCodeSize();
1583 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001584 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001585 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001586 return;
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001587 } else if (!bad_input && IsMethodGeneratedByOptimizingCompiler(oat_method,
1588 code_item_accessor)) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001589 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001590 StackMapsHelper helper(oat_method.GetVmapTable(), instruction_set_);
David Srbecky86decb62018-06-05 06:41:10 +01001591 if (AddStatsObject(oat_method.GetVmapTable())) {
David Srbecky42deda82018-08-10 11:23:27 +01001592 helper.GetCodeInfo().CollectSizeStats(oat_method.GetVmapTable(), &stats_);
David Srbecky86decb62018-06-05 06:41:10 +01001593 }
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001594 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1595 size_t offset = 0;
1596 while (offset < code_size) {
1597 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
1598 if (offset == helper.GetOffset()) {
1599 ScopedIndentation indent1(vios);
1600 StackMap stack_map = helper.GetStackMap();
1601 DCHECK(stack_map.IsValid());
1602 stack_map.Dump(vios,
1603 helper.GetCodeInfo(),
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001604 oat_method.GetCodeOffset(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001605 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001606 do {
1607 helper.Next();
1608 // There may be multiple stack maps at a given PC. We display only the first one.
1609 } while (offset == helper.GetOffset());
1610 }
1611 DCHECK_LT(offset, helper.GetOffset());
1612 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001613 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001614 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1615 size_t offset = 0;
1616 while (offset < code_size) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001617 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001618 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001619 }
1620 }
1621
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001622 std::pair<const uint8_t*, const uint8_t*> GetBootImageLiveObjectsDataRange(gc::Heap* heap) const
1623 REQUIRES_SHARED(Locks::mutator_lock_) {
1624 const std::vector<gc::space::ImageSpace*>& boot_image_spaces = heap->GetBootImageSpaces();
1625 const ImageHeader& main_header = boot_image_spaces[0]->GetImageHeader();
1626 ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_live_objects =
1627 ObjPtr<mirror::ObjectArray<mirror::Object>>::DownCast(
1628 main_header.GetImageRoot<kWithoutReadBarrier>(ImageHeader::kBootImageLiveObjects));
1629 DCHECK(boot_image_live_objects != nullptr);
1630 DCHECK(heap->ObjectIsInBootImageSpace(boot_image_live_objects));
1631 const uint8_t* boot_image_live_objects_address =
1632 reinterpret_cast<const uint8_t*>(boot_image_live_objects.Ptr());
1633 uint32_t begin_offset = mirror::ObjectArray<mirror::Object>::OffsetOfElement(0).Uint32Value();
1634 uint32_t end_offset = mirror::ObjectArray<mirror::Object>::OffsetOfElement(
1635 boot_image_live_objects->GetLength()).Uint32Value();
1636 return std::make_pair(boot_image_live_objects_address + begin_offset,
1637 boot_image_live_objects_address + end_offset);
1638 }
1639
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001640 void DumpDataBimgRelRoEntries(std::ostream& os) {
1641 os << ".data.bimg.rel.ro: ";
1642 if (oat_file_.GetBootImageRelocations().empty()) {
1643 os << "empty.\n\n";
1644 return;
1645 }
1646
1647 os << oat_file_.GetBootImageRelocations().size() << " entries.\n";
1648 Runtime* runtime = Runtime::Current();
1649 if (runtime != nullptr && !runtime->GetHeap()->GetBootImageSpaces().empty()) {
1650 const std::vector<gc::space::ImageSpace*>& boot_image_spaces =
1651 runtime->GetHeap()->GetBootImageSpaces();
1652 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001653 auto live_objects = GetBootImageLiveObjectsDataRange(runtime->GetHeap());
1654 const uint8_t* live_objects_begin = live_objects.first;
1655 const uint8_t* live_objects_end = live_objects.second;
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001656 for (const uint32_t& object_offset : oat_file_.GetBootImageRelocations()) {
1657 uint32_t entry_index = &object_offset - oat_file_.GetBootImageRelocations().data();
1658 uint32_t entry_offset = entry_index * sizeof(oat_file_.GetBootImageRelocations()[0]);
1659 os << StringPrintf(" 0x%x: 0x%08x", entry_offset, object_offset);
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001660 uint8_t* address = boot_image_spaces[0]->Begin() + object_offset;
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001661 bool found = false;
1662 for (gc::space::ImageSpace* space : boot_image_spaces) {
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001663 uint64_t local_offset = address - space->Begin();
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001664 if (local_offset < space->GetImageHeader().GetImageSize()) {
1665 if (space->GetImageHeader().GetObjectsSection().Contains(local_offset)) {
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001666 if (address >= live_objects_begin && address < live_objects_end) {
1667 size_t index =
1668 (address - live_objects_begin) / sizeof(mirror::HeapReference<mirror::Object>);
1669 os << StringPrintf(" 0x%08x BootImageLiveObject[%zu]",
Lokesh Gidra44044b12019-02-26 00:10:04 +00001670 object_offset,
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001671 index);
1672 } else {
1673 ObjPtr<mirror::Object> o = reinterpret_cast<mirror::Object*>(address);
1674 if (o->IsString()) {
1675 os << " String: " << o->AsString()->ToModifiedUtf8();
1676 } else if (o->IsClass()) {
1677 os << " Class: " << o->AsClass()->PrettyDescriptor();
1678 } else {
1679 os << StringPrintf(" 0x%08x %s",
1680 object_offset,
1681 o->GetClass()->PrettyDescriptor().c_str());
1682 }
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001683 }
1684 } else if (space->GetImageHeader().GetMethodsSection().Contains(local_offset)) {
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001685 ArtMethod* m = reinterpret_cast<ArtMethod*>(address);
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001686 os << " ArtMethod: " << m->PrettyMethod();
1687 } else {
1688 os << StringPrintf(" 0x%08x <unexpected section in %s>",
1689 object_offset,
1690 space->GetImageFilename().c_str());
1691 }
1692 found = true;
1693 break;
1694 }
1695 }
1696 if (!found) {
1697 os << StringPrintf(" 0x%08x <outside boot image spaces>", object_offset);
1698 }
1699 os << "\n";
1700 }
1701 } else {
1702 for (const uint32_t& object_offset : oat_file_.GetBootImageRelocations()) {
1703 uint32_t entry_index = &object_offset - oat_file_.GetBootImageRelocations().data();
1704 uint32_t entry_offset = entry_index * sizeof(oat_file_.GetBootImageRelocations()[0]);
1705 os << StringPrintf(" 0x%x: 0x%08x\n", entry_offset, object_offset);
1706 }
1707 }
1708 os << "\n";
1709 }
1710
Vladimir Markof3c52b42017-11-17 17:32:12 +00001711 template <typename NameGetter>
1712 void DumpBssEntries(std::ostream& os,
1713 const char* slot_type,
1714 const IndexBssMapping* mapping,
1715 uint32_t number_of_indexes,
1716 size_t slot_size,
1717 NameGetter name) {
1718 os << ".bss mapping for " << slot_type << ": ";
1719 if (mapping == nullptr) {
1720 os << "empty.\n";
1721 return;
1722 }
1723 size_t index_bits = IndexBssMappingEntry::IndexBits(number_of_indexes);
1724 size_t num_valid_indexes = 0u;
1725 for (const IndexBssMappingEntry& entry : *mapping) {
1726 num_valid_indexes += 1u + POPCOUNT(entry.GetMask(index_bits));
1727 }
1728 os << mapping->size() << " entries for " << num_valid_indexes << " valid indexes.\n";
1729 os << std::hex;
1730 for (const IndexBssMappingEntry& entry : *mapping) {
1731 uint32_t index = entry.GetIndex(index_bits);
1732 uint32_t mask = entry.GetMask(index_bits);
1733 size_t bss_offset = entry.bss_offset - POPCOUNT(mask) * slot_size;
1734 for (uint32_t n : LowToHighBits(mask)) {
1735 size_t current_index = index - (32u - index_bits) + n;
1736 os << " 0x" << bss_offset << ": " << slot_type << ": " << name(current_index) << "\n";
1737 bss_offset += slot_size;
1738 }
1739 DCHECK_EQ(bss_offset, entry.bss_offset);
1740 os << " 0x" << bss_offset << ": " << slot_type << ": " << name(index) << "\n";
1741 }
1742 os << std::dec;
1743 }
1744
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001745 const OatFile& oat_file_;
Andreas Gampeb40d3612018-06-26 15:49:42 -07001746 const std::vector<const OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001747 const OatDumperOptions& options_;
1748 uint32_t resolved_addr2instr_;
Andreas Gampe372f3a32016-08-19 10:49:06 -07001749 const InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001750 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001751 Disassembler* disassembler_;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001752 Stats stats_;
David Srbecky86decb62018-06-05 06:41:10 +01001753 std::unordered_set<const void*> seen_stats_objects_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001754};
1755
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001756class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001757 public:
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001758 ImageDumper(std::ostream* os,
1759 gc::space::ImageSpace& image_space,
1760 const ImageHeader& image_header,
1761 OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001762 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001763 vios_(os),
1764 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001765 image_space_(image_space),
1766 image_header_(image_header),
1767 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001768
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001769 bool Dump() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001770 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001771 std::ostream& indent_os = vios_.Stream();
1772
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001773 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001774
Jeff Haodcdc85b2015-12-04 14:06:18 -08001775 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1776
Vladimir Markoc10a0c62018-11-16 11:39:22 +00001777 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n";
1778 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n";
1779 os << "IMAGE CHECKSUM: " << std::hex << image_header_.GetImageChecksum() << std::dec << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001780
Vladimir Markoc10a0c62018-11-16 11:39:22 +00001781 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum()) << "\n";
1782 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n";
1783 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n";
1784 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n";
1785 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
1786
1787 os << "BOOT IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetBootImageBegin())
1788 << "\n";
Vladimir Marko0c78ef72018-11-21 14:09:35 +00001789 os << "BOOT IMAGE SIZE: " << image_header_.GetBootImageSize() << "\n\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001790
1791 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1792 auto section = static_cast<ImageHeader::ImageSections>(i);
1793 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1794 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001795
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001796 {
Vladimir Markoc13fbd82018-06-04 16:16:28 +01001797 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots().Ptr()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001798 static_assert(arraysize(image_roots_descriptions_) ==
1799 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001800 DCHECK_LE(image_header_.GetImageRoots()->GetLength(), ImageHeader::kImageRootsMax);
1801 for (int32_t i = 0, size = image_header_.GetImageRoots()->GetLength(); i != size; ++i) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001802 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1803 const char* image_root_description = image_roots_descriptions_[i];
Vladimir Markoc13fbd82018-06-04 16:16:28 +01001804 ObjPtr<mirror::Object> image_root_object = image_header_.GetImageRoot(image_root);
1805 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object.Ptr());
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001806 if (image_root_object != nullptr && image_root_object->IsObjectArray()) {
Vladimir Markoc13fbd82018-06-04 16:16:28 +01001807 ObjPtr<mirror::ObjectArray<mirror::Object>> image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001808 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001809 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001810 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1811 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001812 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001813 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1814 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001815 run++;
1816 } else {
1817 break;
1818 }
1819 }
1820 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001821 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001822 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001823 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001824 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001825 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001826 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001827 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001828 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001829 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001830 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001831 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001832 }
1833 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001834 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001835
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001836 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001837 os << "METHOD ROOTS\n";
1838 static_assert(arraysize(image_methods_descriptions_) ==
1839 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1840 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1841 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1842 const char* description = image_methods_descriptions_[i];
1843 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001844 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001845 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001846 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001847 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001848
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001849 Runtime* const runtime = Runtime::Current();
1850 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001851 std::string image_filename = image_space_.GetImageFilename();
1852 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001853 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001854 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001855 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001856 const OatFile* oat_file = image_space_.GetOatFile();
Alex Lighta59dd802014-07-02 16:28:08 -07001857 if (oat_file == nullptr) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001858 oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location);
1859 }
1860 if (oat_file == nullptr) {
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001861 oat_file = OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01001862 oat_location,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001863 oat_location,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001864 /*executable=*/ false,
1865 /*low_4gb=*/ false,
1866 /*abs_dex_location=*/ nullptr,
1867 /*reservation=*/ nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001868 &error_msg);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001869 }
1870 if (oat_file == nullptr) {
1871 os << "OAT FILE NOT FOUND: " << error_msg << "\n";
1872 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001873 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001874 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001875
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001876 stats_.oat_file_bytes = oat_file->Size();
1877
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001878 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001879
Andreas Gampeb40d3612018-06-26 15:49:42 -07001880 for (const OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001881 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001882 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1883 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001884 }
1885
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001886 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001887
Jeff Haodcdc85b2015-12-04 14:06:18 -08001888 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001889 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001890 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001891 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001892 {
1893 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1894 heap->FlushAllocStack();
1895 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001896 // Since FlushAllocStack() above resets the (active) allocation
1897 // stack. Need to revoke the thread-local allocation stacks that
1898 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001899 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001900 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001901 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001902 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001903 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001904 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001905 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001906 {
Andreas Gampecc1b5352016-12-01 16:58:38 -08001907 ReaderMutexLock mu(self, *Locks::dex_lock_);
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001908 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001909 ObjPtr<mirror::DexCache> dex_cache =
1910 ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001911 if (dex_cache != nullptr) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001912 dex_caches_.insert(dex_cache.Ptr());
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001913 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001914 }
1915 }
Andreas Gampe0c183382017-07-13 22:26:24 -07001916 auto dump_visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
1917 DumpObject(obj);
1918 };
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001919 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001920 // Dump the normal objects before ArtMethods.
Andreas Gampe0c183382017-07-13 22:26:24 -07001921 image_space_.GetLiveBitmap()->Walk(dump_visitor);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001922 indent_os << "\n";
1923 // TODO: Dump fields.
1924 // Dump methods after.
Jeff Haodcdc85b2015-12-04 14:06:18 -08001925 DumpArtMethodVisitor visitor(this);
Mathieu Chartiere42888f2016-04-14 10:49:19 -07001926 image_header_.VisitPackedArtMethods(&visitor,
1927 image_space_.Begin(),
1928 image_header_.GetPointerSize());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001929 // Dump the large objects separately.
Andreas Gampe0c183382017-07-13 22:26:24 -07001930 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(dump_visitor);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001931 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001932 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001933 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001934 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001935 size_t data_size = image_header_.GetDataSize(); // stored size in file.
1936 if (file == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001937 LOG(WARNING) << "Failed to find image in " << image_filename;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001938 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001939 stats_.file_bytes = file->GetLength();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001940 // If the image is compressed, adjust to decompressed size.
1941 size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader);
Mathieu Chartier1a842962018-11-13 15:09:51 -08001942 if (image_header_.HasCompressedBlock()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001943 DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image";
1944 }
1945 stats_.file_bytes += uncompressed_size - data_size;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001946 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001947 size_t header_bytes = sizeof(ImageHeader);
Vladimir Markocd87c3e2017-09-05 13:11:57 +01001948 const auto& object_section = image_header_.GetObjectsSection();
1949 const auto& field_section = image_header_.GetFieldsSection();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001950 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Markocd87c3e2017-09-05 13:11:57 +01001951 const auto& dex_cache_arrays_section = image_header_.GetDexCacheArraysSection();
1952 const auto& intern_section = image_header_.GetInternedStringsSection();
1953 const auto& class_table_section = image_header_.GetClassTableSection();
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07001954 const auto& sro_section = image_header_.GetImageStringReferenceOffsetsSection();
1955 const auto& metadata_section = image_header_.GetMetadataSection();
Vladimir Markocd87c3e2017-09-05 13:11:57 +01001956 const auto& bitmap_section = image_header_.GetImageBitmapSection();
Andreas Gampeace0dc12016-01-20 13:33:13 -08001957
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001958 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001959
1960 // Objects are kObjectAlignment-aligned.
1961 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1962 if (object_section.Offset() > header_bytes) {
1963 stats_.alignment_bytes += object_section.Offset() - header_bytes;
1964 }
1965
1966 // Field section is 4-byte aligned.
1967 constexpr size_t kFieldSectionAlignment = 4U;
1968 uint32_t end_objects = object_section.Offset() + object_section.Size();
1969 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
1970 stats_.alignment_bytes += field_section.Offset() - end_objects;
1971
1972 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
1973 uint32_t end_fields = field_section.Offset() + field_section.Size();
1974 CHECK_ALIGNED(method_section.Offset(), 4);
1975 stats_.alignment_bytes += method_section.Offset() - end_fields;
1976
1977 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
1978 uint32_t end_methods = method_section.Offset() + method_section.Size();
1979 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
1980 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
1981
1982 // Intern table is 8-byte aligned.
1983 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
Vladimir Markoe47f60c2018-02-21 13:43:28 +00001984 CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
Andreas Gampeace0dc12016-01-20 13:33:13 -08001985 stats_.alignment_bytes += intern_section.Offset() - end_caches;
1986
1987 // Add space between intern table and class table.
1988 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
1989 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
1990
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001991 // Add space between end of image data and bitmap. Expect the bitmap to be page-aligned.
1992 const size_t bitmap_offset = sizeof(ImageHeader) + data_size;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001993 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001994 stats_.alignment_bytes += RoundUp(bitmap_offset, kPageSize) - bitmap_offset;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001995
Mathieu Chartiere401d142015-04-22 13:56:20 -07001996 stats_.bitmap_bytes += bitmap_section.Size();
1997 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01001998 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01001999 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002000 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002001 stats_.class_table_bytes += class_table_section.Size();
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002002 stats_.sro_offset_bytes += sro_section.Size();
2003 stats_.metadata_bytes += metadata_section.Size();
2004
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002005 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002006 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002007
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002008 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002009
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002010 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002011 }
2012
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002013 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002014 class DumpArtMethodVisitor : public ArtMethodVisitor {
2015 public:
2016 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
2017
Roland Levillainf73caca2018-08-24 17:19:07 +01002018 void Visit(ArtMethod* method) override REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002019 std::ostream& indent_os = image_dumper_->vios_.Stream();
David Sehr709b0702016-10-13 09:12:37 -07002020 indent_os << method << " " << " ArtMethod: " << ArtMethod::PrettyMethod(method) << "\n";
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002021 image_dumper_->DumpMethod(method, indent_os);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002022 indent_os << "\n";
2023 }
2024
2025 private:
2026 ImageDumper* const image_dumper_;
2027 };
2028
Mathieu Chartier3398c782016-09-30 10:27:43 -07002029 static void PrettyObjectValue(std::ostream& os,
2030 ObjPtr<mirror::Class> type,
2031 ObjPtr<mirror::Object> value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002032 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002033 CHECK(type != nullptr);
2034 if (value == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07002035 os << StringPrintf("null %s\n", type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002036 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002037 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002038 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07002039 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07002040 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002041 mirror::Class* klass = value->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07002042 os << StringPrintf("%p Class: %s\n", klass, mirror::Class::PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002043 } else {
David Sehr709b0702016-10-13 09:12:37 -07002044 os << StringPrintf("%p %s\n", value.Ptr(), type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002045 }
2046 }
2047
Mathieu Chartier3398c782016-09-30 10:27:43 -07002048 static void PrintField(std::ostream& os, ArtField* field, ObjPtr<mirror::Object> obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002049 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07002050 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08002051 switch (field->GetTypeAsPrimitiveType()) {
2052 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08002053 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002054 break;
2055 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002056 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002057 break;
2058 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002059 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002060 break;
2061 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002062 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002063 break;
2064 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07002065 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002066 break;
2067 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07002068 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002069 break;
2070 case Primitive::kPrimBoolean:
Roland Levillain5e8d5f02016-10-18 18:03:43 +01002071 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj) ? "true" : "false",
Fred Shih37f05ef2014-07-16 18:38:08 -07002072 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002073 break;
2074 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07002075 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002076 break;
2077 case Primitive::kPrimNot: {
2078 // Get the value, don't compute the type unless it is non-null as we don't want
2079 // to cause class loading.
Mathieu Chartier3398c782016-09-30 10:27:43 -07002080 ObjPtr<mirror::Object> value = field->GetObj(obj);
Ian Rogers08f1f502014-12-02 15:04:37 -08002081 if (value == nullptr) {
2082 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07002083 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08002084 // Grab the field type without causing resolution.
Vladimir Marko208f6702017-12-08 12:00:50 +00002085 ObjPtr<mirror::Class> field_type = field->LookupResolvedType();
Ian Rogers08f1f502014-12-02 15:04:37 -08002086 if (field_type != nullptr) {
2087 PrettyObjectValue(os, field_type, value);
2088 } else {
Mathieu Chartier3398c782016-09-30 10:27:43 -07002089 os << StringPrintf("%p %s\n",
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07002090 value.Ptr(),
Ian Rogers08f1f502014-12-02 15:04:37 -08002091 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
2092 }
Ian Rogers50239c72013-06-17 14:53:22 -07002093 }
Ian Rogers08f1f502014-12-02 15:04:37 -08002094 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07002095 }
Ian Rogers08f1f502014-12-02 15:04:37 -08002096 default:
2097 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
2098 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08002099 }
2100 }
2101
Andreas Gampe98104992018-10-16 12:49:47 -07002102 static void DumpFields(std::ostream& os, mirror::Object* obj, ObjPtr<mirror::Class> klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002103 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe98104992018-10-16 12:49:47 -07002104 ObjPtr<mirror::Class> super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002105 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002106 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08002107 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002108 for (ArtField& field : klass->GetIFields()) {
2109 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08002110 }
2111 }
2112
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002113 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002114 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002115 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002116
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002117 const void* GetQuickOatCodeBegin(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08002118 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002119 image_header_.GetPointerSize());
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002120 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002121 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002122 }
Vladimir Marko33bff252017-11-01 14:35:42 +00002123 if (oat_dumper_->GetInstructionSet() == InstructionSet::kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002124 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002125 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002126 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002127 }
2128
Mathieu Chartiere401d142015-04-22 13:56:20 -07002129 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002130 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002131 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
2132 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002133 return 0;
2134 }
2135 return oat_code_begin[-1];
2136 }
2137
Mathieu Chartiere401d142015-04-22 13:56:20 -07002138 const void* GetQuickOatCodeEnd(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002139 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002140 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002141 if (oat_code_begin == nullptr) {
2142 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002143 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002144 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002145 }
2146
Andreas Gampe0c183382017-07-13 22:26:24 -07002147 void DumpObject(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002148 DCHECK(obj != nullptr);
Andreas Gampe0c183382017-07-13 22:26:24 -07002149 if (!InDumpSpace(obj)) {
Brian Carlstrom78128a62011-09-15 17:21:19 -07002150 return;
2151 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002152
2153 size_t object_bytes = obj->SizeOf();
2154 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
Andreas Gampe0c183382017-07-13 22:26:24 -07002155 stats_.object_bytes += object_bytes;
2156 stats_.alignment_bytes += alignment_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002157
Andreas Gampe0c183382017-07-13 22:26:24 -07002158 std::ostream& os = vios_.Stream();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002159
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002160 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08002161 if (obj_class->IsArrayClass()) {
David Sehr709b0702016-10-13 09:12:37 -07002162 os << StringPrintf("%p: %s length:%d\n", obj, obj_class->PrettyDescriptor().c_str(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002163 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08002164 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002165 mirror::Class* klass = obj->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07002166 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj,
2167 mirror::Class::PrettyDescriptor(klass).c_str())
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002168 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08002169 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002170 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07002171 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002172 } else {
David Sehr709b0702016-10-13 09:12:37 -07002173 os << StringPrintf("%p: %s\n", obj, obj_class->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002174 }
Andreas Gampe0c183382017-07-13 22:26:24 -07002175 ScopedIndentation indent1(&vios_);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002176 DumpFields(os, obj, obj_class);
Andreas Gampe0c183382017-07-13 22:26:24 -07002177 const PointerSize image_pointer_size = image_header_.GetPointerSize();
Ian Rogersd5b32602012-02-26 16:40:04 -08002178 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002179 auto* obj_array = obj->AsObjectArray<mirror::Object>();
2180 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002181 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002182 size_t run = 0;
2183 for (int32_t j = i + 1; j < length; j++) {
2184 if (value == obj_array->Get(j)) {
2185 run++;
2186 } else {
2187 break;
2188 }
2189 }
2190 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002191 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002192 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002193 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08002194 i = i + run;
2195 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002196 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002197 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002198 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08002199 }
2200 } else if (obj->IsClass()) {
Vladimir Marko38b8b252018-01-02 19:07:06 +00002201 ObjPtr<mirror::Class> klass = obj->AsClass();
Igor Murashkin86083f72017-10-27 10:59:04 -07002202
Vladimir Marko305c38b2018-02-14 11:50:07 +00002203 if (kBitstringSubtypeCheckEnabled) {
2204 os << "SUBTYPE_CHECK_BITS: ";
2205 SubtypeCheck<ObjPtr<mirror::Class>>::Dump(klass, os);
2206 os << "\n";
2207 }
Igor Murashkin86083f72017-10-27 10:59:04 -07002208
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002209 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002210 os << "STATICS:\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002211 ScopedIndentation indent2(&vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002212 for (ArtField& field : klass->GetSFields()) {
2213 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08002214 }
2215 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002216 } else {
Andreas Gampe0c183382017-07-13 22:26:24 -07002217 auto it = dex_caches_.find(obj);
2218 if (it != dex_caches_.end()) {
Vladimir Marko05792b92015-08-03 11:56:49 +01002219 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Vladimir Markocd87c3e2017-09-05 13:11:57 +01002220 const auto& field_section = image_header_.GetFieldsSection();
Andreas Gampe0c183382017-07-13 22:26:24 -07002221 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01002222 size_t num_methods = dex_cache->NumResolvedMethods();
2223 if (num_methods != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002224 os << "Methods (size=" << num_methods << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002225 ScopedIndentation indent2(&vios_);
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002226 mirror::MethodDexCacheType* resolved_methods = dex_cache->GetResolvedMethods();
Vladimir Marko05792b92015-08-03 11:56:49 +01002227 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002228 ArtMethod* elem = mirror::DexCache::GetNativePairPtrSize(
2229 resolved_methods, i, image_pointer_size).object;
Vladimir Marko05792b92015-08-03 11:56:49 +01002230 size_t run = 0;
2231 for (size_t j = i + 1;
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002232 j != length &&
2233 elem == mirror::DexCache::GetNativePairPtrSize(
2234 resolved_methods, j, image_pointer_size).object;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002235 ++j) {
2236 ++run;
2237 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002238 if (run == 0) {
2239 os << StringPrintf("%zd: ", i);
2240 } else {
2241 os << StringPrintf("%zd to %zd: ", i, i + run);
2242 i = i + run;
2243 }
2244 std::string msg;
2245 if (elem == nullptr) {
2246 msg = "null";
2247 } else if (method_section.Contains(
Andreas Gampe0c183382017-07-13 22:26:24 -07002248 reinterpret_cast<uint8_t*>(elem) - image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002249 msg = reinterpret_cast<ArtMethod*>(elem)->PrettyMethod();
Vladimir Marko05792b92015-08-03 11:56:49 +01002250 } else {
2251 msg = "<not in method section>";
2252 }
2253 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07002254 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002255 }
2256 size_t num_fields = dex_cache->NumResolvedFields();
2257 if (num_fields != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002258 os << "Fields (size=" << num_fields << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002259 ScopedIndentation indent2(&vios_);
Vladimir Marko05792b92015-08-03 11:56:49 +01002260 auto* resolved_fields = dex_cache->GetResolvedFields();
2261 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002262 ArtField* elem = mirror::DexCache::GetNativePairPtrSize(
Vladimir Markof44d36c2017-03-14 14:18:46 +00002263 resolved_fields, i, image_pointer_size).object;
Vladimir Marko05792b92015-08-03 11:56:49 +01002264 size_t run = 0;
2265 for (size_t j = i + 1;
Vladimir Markof44d36c2017-03-14 14:18:46 +00002266 j != length &&
2267 elem == mirror::DexCache::GetNativePairPtrSize(
2268 resolved_fields, j, image_pointer_size).object;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002269 ++j) {
2270 ++run;
2271 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002272 if (run == 0) {
2273 os << StringPrintf("%zd: ", i);
2274 } else {
2275 os << StringPrintf("%zd to %zd: ", i, i + run);
2276 i = i + run;
2277 }
2278 std::string msg;
2279 if (elem == nullptr) {
2280 msg = "null";
2281 } else if (field_section.Contains(
Andreas Gampe0c183382017-07-13 22:26:24 -07002282 reinterpret_cast<uint8_t*>(elem) - image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002283 msg = reinterpret_cast<ArtField*>(elem)->PrettyField();
Vladimir Marko05792b92015-08-03 11:56:49 +01002284 } else {
2285 msg = "<not in field section>";
2286 }
2287 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002288 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002289 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002290 size_t num_types = dex_cache->NumResolvedTypes();
2291 if (num_types != 0u) {
2292 os << "Types (size=" << num_types << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002293 ScopedIndentation indent2(&vios_);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002294 auto* resolved_types = dex_cache->GetResolvedTypes();
2295 for (size_t i = 0; i < num_types; ++i) {
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002296 auto pair = resolved_types[i].load(std::memory_order_relaxed);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002297 size_t run = 0;
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002298 for (size_t j = i + 1; j != num_types; ++j) {
2299 auto other_pair = resolved_types[j].load(std::memory_order_relaxed);
2300 if (pair.index != other_pair.index ||
2301 pair.object.Read() != other_pair.object.Read()) {
2302 break;
2303 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002304 ++run;
2305 }
2306 if (run == 0) {
2307 os << StringPrintf("%zd: ", i);
2308 } else {
2309 os << StringPrintf("%zd to %zd: ", i, i + run);
2310 i = i + run;
2311 }
2312 std::string msg;
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002313 auto* elem = pair.object.Read();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002314 if (elem == nullptr) {
2315 msg = "null";
2316 } else {
David Sehr709b0702016-10-13 09:12:37 -07002317 msg = elem->PrettyClass();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002318 }
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002319 os << StringPrintf("%p %u %s\n", elem, pair.index, msg.c_str());
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002320 }
2321 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002322 }
2323 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002324 std::string temp;
Andreas Gampe0c183382017-07-13 22:26:24 -07002325 stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002326 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002327
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002328 void DumpMethod(ArtMethod* method, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002329 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002330 DCHECK(method != nullptr);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002331 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
2332 const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
Andreas Gampe542451c2016-07-26 09:02:02 -07002333 const PointerSize pointer_size = image_header_.GetPointerSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002334 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2335 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002336 if (method->IsNative()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002337 bool first_occurrence;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002338 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2339 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002340 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002341 stats_.native_to_managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002342 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002343 if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
2344 image_header_.GetPointerSize())) {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002345 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002346 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002347 } else if (method->IsAbstract() || method->IsClassInitializer()) {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002348 // Don't print information for these.
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002349 } else if (method->IsRuntimeMethod()) {
2350 ImtConflictTable* table = method->GetImtConflictTable(image_header_.GetPointerSize());
2351 if (table != nullptr) {
2352 indent_os << "IMT conflict table " << table << " method: ";
2353 for (size_t i = 0, count = table->NumEntries(pointer_size); i < count; ++i) {
David Sehr709b0702016-10-13 09:12:37 -07002354 indent_os << ArtMethod::PrettyMethod(table->GetImplementationMethod(i, pointer_size))
2355 << " ";
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002356 }
2357 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002358 } else {
David Sehr0225f8e2018-01-31 08:52:24 +00002359 CodeItemDataAccessor code_item_accessor(method->DexInstructionData());
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08002360 size_t dex_instruction_bytes = code_item_accessor.InsnsSizeInCodeUnits() * 2;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002361 stats_.dex_instruction_bytes += dex_instruction_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002362
2363 bool first_occurrence;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002364 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002365 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01002366 // Method compiled with the optimizing compiler have no vmap table.
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002367 vmap_table_bytes = ComputeOatSize(method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002368 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002369 stats_.vmap_table_bytes += vmap_table_bytes;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002370 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002371 }
2372
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002373 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2374 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002375 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002376 stats_.managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002377 if (method->IsConstructor()) {
2378 if (method->IsStatic()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002379 stats_.class_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002380 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002381 stats_.large_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002382 }
2383 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002384 stats_.large_method_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002385 }
2386 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002387 stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002388
Igor Murashkin7617abd2015-07-10 18:27:47 -07002389 uint32_t method_access_flags = method->GetAccessFlags();
2390
Mathieu Chartiere401d142015-04-22 13:56:20 -07002391 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002392 indent_os << StringPrintf("SIZE: Dex Instructions=%zd StackMaps=%zd AccessFlags=0x%x\n",
2393 dex_instruction_bytes,
2394 vmap_table_bytes,
Igor Murashkin7617abd2015-07-10 18:27:47 -07002395 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002396
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002397 size_t total_size = dex_instruction_bytes +
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002398 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002399
2400 double expansion =
2401 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002402 stats_.ComputeOutliers(total_size, expansion, method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002403 }
2404 }
2405
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002406 std::set<const void*> already_seen_;
2407 // Compute the size of the given data within the oat file and whether this is the first time
2408 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002409 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002410 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002411 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002412 already_seen_.insert(oat_data);
2413 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002414 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002415 }
2416 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002417 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002418
2419 public:
2420 struct Stats {
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002421 size_t oat_file_bytes = 0u;
2422 size_t file_bytes = 0u;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002423
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002424 size_t header_bytes = 0u;
2425 size_t object_bytes = 0u;
2426 size_t art_field_bytes = 0u;
2427 size_t art_method_bytes = 0u;
2428 size_t dex_cache_arrays_bytes = 0u;
2429 size_t interned_strings_bytes = 0u;
2430 size_t class_table_bytes = 0u;
2431 size_t sro_offset_bytes = 0u;
2432 size_t metadata_bytes = 0u;
2433 size_t bitmap_bytes = 0u;
2434 size_t alignment_bytes = 0u;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002435
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002436 size_t managed_code_bytes = 0u;
2437 size_t managed_code_bytes_ignoring_deduplication = 0u;
2438 size_t native_to_managed_code_bytes = 0u;
2439 size_t class_initializer_code_bytes = 0u;
2440 size_t large_initializer_code_bytes = 0u;
2441 size_t large_method_code_bytes = 0u;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002442
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002443 size_t vmap_table_bytes = 0u;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002444
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002445 size_t dex_instruction_bytes = 0u;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002446
Mathieu Chartiere401d142015-04-22 13:56:20 -07002447 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002448 std::vector<size_t> method_outlier_size;
2449 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002450 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002451
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002452 Stats() {}
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002453
Elliott Hughesa0e18062012-04-13 15:59:59 -07002454 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002455 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002456 size_t bytes;
2457 size_t count;
2458 };
Andreas Gampec55bb392018-09-21 00:02:02 +00002459 using SizeAndCountTable = SafeMap<std::string, SizeAndCount>;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002460 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002461
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002462 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002463 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2464 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002465 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002466 it->second.count += 1;
2467 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002468 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002469 }
2470 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002471
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002472 double PercentOfOatBytes(size_t size) {
2473 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2474 }
2475
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002476 double PercentOfFileBytes(size_t size) {
2477 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2478 }
2479
2480 double PercentOfObjectBytes(size_t size) {
2481 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2482 }
2483
Mathieu Chartiere401d142015-04-22 13:56:20 -07002484 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002485 method_outlier_size.push_back(total_size);
2486 method_outlier_expansion.push_back(expansion);
2487 method_outlier.push_back(method);
2488 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002489
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002490 void DumpOutliers(std::ostream& os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002491 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002492 size_t sum_of_sizes = 0;
2493 size_t sum_of_sizes_squared = 0;
2494 size_t sum_of_expansion = 0;
2495 size_t sum_of_expansion_squared = 0;
2496 size_t n = method_outlier_size.size();
Mathieu Chartier1ebf8d32016-06-09 11:51:27 -07002497 if (n <= 1) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08002498 return;
2499 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002500 for (size_t i = 0; i < n; i++) {
2501 size_t cur_size = method_outlier_size[i];
2502 sum_of_sizes += cur_size;
2503 sum_of_sizes_squared += cur_size * cur_size;
2504 double cur_expansion = method_outlier_expansion[i];
2505 sum_of_expansion += cur_expansion;
2506 sum_of_expansion_squared += cur_expansion * cur_expansion;
2507 }
2508 size_t size_mean = sum_of_sizes / n;
2509 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2510 double expansion_mean = sum_of_expansion / n;
2511 double expansion_variance =
2512 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2513
2514 // Dump methods whose size is a certain number of standard deviations from the mean
2515 size_t dumped_values = 0;
2516 size_t skipped_values = 0;
2517 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2518 size_t cur_size_variance = i * i * size_variance;
2519 bool first = true;
2520 for (size_t j = 0; j < n; j++) {
2521 size_t cur_size = method_outlier_size[j];
2522 if (cur_size > size_mean) {
2523 size_t cur_var = cur_size - size_mean;
2524 cur_var = cur_var * cur_var;
2525 if (cur_var > cur_size_variance) {
2526 if (dumped_values > 20) {
2527 if (i == 1) {
2528 skipped_values++;
2529 } else {
2530 i = 2; // jump to counting for 1 standard deviation
2531 break;
2532 }
2533 } else {
2534 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002535 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002536 first = false;
2537 }
David Sehr709b0702016-10-13 09:12:37 -07002538 os << ArtMethod::PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002539 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002540 method_outlier_size[j] = 0; // don't consider this method again
2541 dumped_values++;
2542 }
2543 }
2544 }
2545 }
2546 }
2547 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002548 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002549 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002550 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002551 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002552
2553 // Dump methods whose expansion is a certain number of standard deviations from the mean
2554 dumped_values = 0;
2555 skipped_values = 0;
2556 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2557 double cur_expansion_variance = i * i * expansion_variance;
2558 bool first = true;
2559 for (size_t j = 0; j < n; j++) {
2560 double cur_expansion = method_outlier_expansion[j];
2561 if (cur_expansion > expansion_mean) {
2562 size_t cur_var = cur_expansion - expansion_mean;
2563 cur_var = cur_var * cur_var;
2564 if (cur_var > cur_expansion_variance) {
2565 if (dumped_values > 20) {
2566 if (i == 1) {
2567 skipped_values++;
2568 } else {
2569 i = 2; // jump to counting for 1 standard deviation
2570 break;
2571 }
2572 } else {
2573 if (first) {
2574 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002575 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002576 first = false;
2577 }
David Sehr709b0702016-10-13 09:12:37 -07002578 os << ArtMethod::PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002579 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002580 method_outlier_expansion[j] = 0.0; // don't consider this method again
2581 dumped_values++;
2582 }
2583 }
2584 }
2585 }
2586 }
2587 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002588 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002589 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002590 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002591 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002592 }
2593
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002594 void Dump(std::ostream& os, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002595 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002596 {
2597 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2598 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002599 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2600 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2601 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2602 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2603 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2604 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002605 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002606 "sro_bytes = %8zd (%2.0f%% of art file bytes)\n"
2607 "metadata_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002608 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2609 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002610 header_bytes, PercentOfFileBytes(header_bytes),
2611 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002612 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2613 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002614 dex_cache_arrays_bytes,
2615 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002616 interned_strings_bytes,
2617 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002618 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002619 sro_offset_bytes, PercentOfFileBytes(sro_offset_bytes),
2620 metadata_bytes, PercentOfFileBytes(metadata_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002621 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002622 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2623 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002624 CHECK_EQ(file_bytes,
2625 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2626 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002627 sro_offset_bytes + metadata_bytes + bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002628 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002629
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002630 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002631 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002632 for (const auto& sizes_and_count : sizes_and_counts) {
2633 const std::string& descriptor(sizes_and_count.first);
2634 double average = static_cast<double>(sizes_and_count.second.bytes) /
2635 static_cast<double>(sizes_and_count.second.count);
2636 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002637 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002638 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002639 descriptor.c_str(), sizes_and_count.second.bytes,
2640 sizes_and_count.second.count, average, percent);
2641 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002642 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002643 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002644 CHECK_EQ(object_bytes, object_bytes_total);
2645
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002646 os << StringPrintf("oat_file_bytes = %8zd\n"
2647 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002648 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2649 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2650 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2651 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002652 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002653 managed_code_bytes,
2654 PercentOfOatBytes(managed_code_bytes),
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002655 native_to_managed_code_bytes,
2656 PercentOfOatBytes(native_to_managed_code_bytes),
2657 class_initializer_code_bytes,
2658 PercentOfOatBytes(class_initializer_code_bytes),
2659 large_initializer_code_bytes,
2660 PercentOfOatBytes(large_initializer_code_bytes),
2661 large_method_code_bytes,
2662 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002663 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002664 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002665 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002666 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2667 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002668 }
2669
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002670 os << "\n" << StringPrintf("vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002671 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002672 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002673
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002674 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2675 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002676 static_cast<double>(managed_code_bytes) /
2677 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002678 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002679 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002680 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002681
2682 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002683 }
2684 } stats_;
2685
2686 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002687 enum {
2688 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2689 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2690 kLargeConstructorDexBytes = 4000,
2691 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2692 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2693 kLargeMethodDexBytes = 16000
2694 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002695
2696 // For performance, use the *os_ directly for anything that doesn't need indentation
2697 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002698 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002699 VariableIndentationOutputStream vios_;
2700 ScopedIndentation indent1_;
2701
Ian Rogers1d54e732013-05-02 21:10:01 -07002702 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002703 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002704 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002705 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002706 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002707
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002708 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002709};
2710
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002711static int DumpImage(gc::space::ImageSpace* image_space,
2712 OatDumperOptions* options,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002713 std::ostream* os) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002714 const ImageHeader& image_header = image_space->GetImageHeader();
2715 if (!image_header.IsValid()) {
Andreas Gampe221d9812018-01-22 17:48:56 -08002716 LOG(ERROR) << "Invalid image header " << image_space->GetImageLocation();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002717 return EXIT_FAILURE;
2718 }
2719 ImageDumper image_dumper(os, *image_space, image_header, options);
2720 if (!image_dumper.Dump()) {
2721 return EXIT_FAILURE;
2722 }
2723 return EXIT_SUCCESS;
2724}
2725
2726static int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002727 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002728 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002729 options->class_loader_ = &null_class_loader;
2730
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002731 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002732 if (options->app_image_ != nullptr) {
2733 if (options->app_oat_ == nullptr) {
2734 LOG(ERROR) << "Can not dump app image without app oat file";
Jeff Haodcdc85b2015-12-04 14:06:18 -08002735 return EXIT_FAILURE;
2736 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002737 // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
2738 // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
2739 // pointers into 32 bit pointer sized ArtMethods.
2740 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002741 std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01002742 options->app_oat_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002743 options->app_oat_,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002744 /*executable=*/ false,
2745 /*low_4gb=*/ true,
2746 /*abs_dex_location=*/ nullptr,
2747 /*reservation=*/ nullptr,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002748 &error_msg));
2749 if (oat_file == nullptr) {
2750 LOG(ERROR) << "Failed to open oat file " << options->app_oat_ << " with error " << error_msg;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002751 return EXIT_FAILURE;
2752 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002753 std::unique_ptr<gc::space::ImageSpace> space(
2754 gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg));
2755 if (space == nullptr) {
2756 LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error "
2757 << error_msg;
2758 }
2759 // Open dex files for the image.
2760 std::vector<std::unique_ptr<const DexFile>> dex_files;
2761 if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
2762 LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
2763 << error_msg;
2764 }
2765 // Dump the actual image.
2766 int result = DumpImage(space.get(), options, os);
2767 if (result != EXIT_SUCCESS) {
2768 return result;
2769 }
2770 // Fall through to dump the boot images.
2771 }
2772
2773 gc::Heap* heap = runtime->GetHeap();
2774 CHECK(heap->HasBootImageSpace()) << "No image spaces";
2775 for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
2776 int result = DumpImage(image_space, options, os);
2777 if (result != EXIT_SUCCESS) {
2778 return result;
2779 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002780 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002781 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002782}
2783
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002784static jobject InstallOatFile(Runtime* runtime,
2785 std::unique_ptr<OatFile> oat_file,
2786 std::vector<const DexFile*>* class_path)
2787 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002788 Thread* self = Thread::Current();
2789 CHECK(self != nullptr);
2790 // Need well-known-classes.
2791 WellKnownClasses::Init(self->GetJniEnv());
2792
Vladimir Marko421087b2018-02-27 11:00:17 +00002793 // Open dex files.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002794 OatFile* oat_file_ptr = oat_file.get();
Andreas Gampe00b25f32014-09-17 21:49:05 -07002795 ClassLinker* class_linker = runtime->GetClassLinker();
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002796 runtime->GetOatFileManager().RegisterOatFile(std::move(oat_file));
Andreas Gampeb40d3612018-06-26 15:49:42 -07002797 for (const OatDexFile* odf : oat_file_ptr->GetOatDexFiles()) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002798 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002799 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002800 CHECK(dex_file != nullptr) << error_msg;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002801 class_path->push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002802 }
2803
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002804 // Need a class loader. Fake that we're a compiler.
2805 // Note: this will run initializers through the unstarted runtime, so make sure it's
2806 // initialized.
2807 interpreter::UnstartedRuntime::Initialize();
2808
2809 jobject class_loader = class_linker->CreatePathClassLoader(self, *class_path);
2810
Vladimir Marko421087b2018-02-27 11:00:17 +00002811 // Need to register dex files to get a working dex cache.
2812 for (const DexFile* dex_file : *class_path) {
2813 ObjPtr<mirror::DexCache> dex_cache = class_linker->RegisterDexFile(
2814 *dex_file, self->DecodeJObject(class_loader)->AsClassLoader());
2815 CHECK(dex_cache != nullptr);
2816 }
2817
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002818 return class_loader;
2819}
2820
2821static int DumpOatWithRuntime(Runtime* runtime,
2822 std::unique_ptr<OatFile> oat_file,
2823 OatDumperOptions* options,
2824 std::ostream* os) {
2825 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2826 ScopedObjectAccess soa(Thread::Current());
2827
2828 OatFile* oat_file_ptr = oat_file.get();
2829 std::vector<const DexFile*> class_path;
2830 jobject class_loader = InstallOatFile(runtime, std::move(oat_file), &class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002831
2832 // Use the class loader while dumping.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002833 StackHandleScope<1> scope(soa.Self());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002834 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07002835 soa.Decode<mirror::ClassLoader>(class_loader));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002836 options->class_loader_ = &loader_handle;
2837
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002838 OatDumper oat_dumper(*oat_file_ptr, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002839 bool success = oat_dumper.Dump(*os);
2840 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2841}
2842
2843static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002844 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002845 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002846 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002847 options->class_loader_ = &null_class_loader;
2848
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002849 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002850 bool success = oat_dumper.Dump(*os);
2851 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2852}
2853
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002854static int DumpOat(Runtime* runtime,
2855 const char* oat_filename,
2856 const char* dex_filename,
2857 OatDumperOptions* options,
Andreas Gampe00b25f32014-09-17 21:49:05 -07002858 std::ostream* os) {
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002859 if (dex_filename == nullptr) {
2860 LOG(WARNING) << "No dex filename provided, "
2861 << "oatdump might fail if the oat file does not contain the dex code.";
2862 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002863 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002864 std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01002865 oat_filename,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002866 oat_filename,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002867 /*executable=*/ false,
2868 /*low_4gb=*/ false,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002869 dex_filename,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002870 /*reservation=*/ nullptr,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002871 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002872 if (oat_file == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08002873 LOG(ERROR) << "Failed to open oat file from '" << oat_filename << "': " << error_msg;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002874 return EXIT_FAILURE;
2875 }
2876
2877 if (runtime != nullptr) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002878 return DumpOatWithRuntime(runtime, std::move(oat_file), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002879 } else {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002880 return DumpOatWithoutRuntime(oat_file.get(), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002881 }
2882}
2883
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002884static int SymbolizeOat(const char* oat_filename,
2885 const char* dex_filename,
2886 std::string& output_name,
2887 bool no_bits) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002888 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002889 std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01002890 oat_filename,
Andreas Gampe08c277c2017-04-26 22:22:15 -07002891 oat_filename,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002892 /*executable=*/ false,
2893 /*low_4gb=*/ false,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002894 dex_filename,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002895 /*reservation=*/ nullptr,
Andreas Gampe08c277c2017-04-26 22:22:15 -07002896 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002897 if (oat_file == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08002898 LOG(ERROR) << "Failed to open oat file from '" << oat_filename << "': " << error_msg;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002899 return EXIT_FAILURE;
2900 }
2901
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002902 bool result;
2903 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
2904 // files for 64-bit code in the past.
2905 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
Andreas Gampe08c277c2017-04-26 22:22:15 -07002906 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file.get(), output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002907 result = oat_symbolizer.Symbolize();
2908 } else {
Andreas Gampe08c277c2017-04-26 22:22:15 -07002909 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file.get(), output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002910 result = oat_symbolizer.Symbolize();
2911 }
2912 if (!result) {
Andreas Gampe221d9812018-01-22 17:48:56 -08002913 LOG(ERROR) << "Failed to symbolize";
Andreas Gampe00b25f32014-09-17 21:49:05 -07002914 return EXIT_FAILURE;
2915 }
2916
2917 return EXIT_SUCCESS;
2918}
2919
Andreas Gampe9fded872016-09-25 16:08:35 -07002920class IMTDumper {
2921 public:
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002922 static bool Dump(Runtime* runtime,
2923 const std::string& imt_file,
2924 bool dump_imt_stats,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002925 const char* oat_filename,
2926 const char* dex_filename) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002927 Thread* self = Thread::Current();
2928
2929 ScopedObjectAccess soa(self);
2930 StackHandleScope<1> scope(self);
2931 MutableHandle<mirror::ClassLoader> class_loader = scope.NewHandle<mirror::ClassLoader>(nullptr);
2932 std::vector<const DexFile*> class_path;
2933
2934 if (oat_filename != nullptr) {
2935 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002936 std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01002937 oat_filename,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002938 oat_filename,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002939 /*executable=*/ false,
Andreas Gampe9b031f72018-10-04 11:03:34 -07002940 /*low_4gb=*/false,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002941 dex_filename,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002942 /*reservation=*/ nullptr,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002943 &error_msg));
2944 if (oat_file == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08002945 LOG(ERROR) << "Failed to open oat file from '" << oat_filename << "': " << error_msg;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002946 return false;
2947 }
2948
2949 class_loader.Assign(soa.Decode<mirror::ClassLoader>(
2950 InstallOatFile(runtime, std::move(oat_file), &class_path)));
2951 } else {
2952 class_loader.Assign(nullptr); // Boot classloader. Just here for explicit documentation.
2953 class_path = runtime->GetClassLinker()->GetBootClassPath();
2954 }
2955
2956 if (!imt_file.empty()) {
2957 return DumpImt(runtime, imt_file, class_loader);
2958 }
2959
2960 if (dump_imt_stats) {
2961 return DumpImtStats(runtime, class_path, class_loader);
2962 }
2963
2964 LOG(FATAL) << "Should not reach here";
2965 UNREACHABLE();
2966 }
2967
2968 private:
2969 static bool DumpImt(Runtime* runtime,
2970 const std::string& imt_file,
2971 Handle<mirror::ClassLoader> h_class_loader)
2972 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002973 std::vector<std::string> lines = ReadCommentedInputFromFile(imt_file);
2974 std::unordered_set<std::string> prepared;
2975
2976 for (const std::string& line : lines) {
2977 // A line should be either a class descriptor, in which case we will dump the complete IMT,
2978 // or a class descriptor and an interface method, in which case we will lookup the method,
2979 // determine its IMT slot, and check the class' IMT.
2980 size_t first_space = line.find(' ');
2981 if (first_space == std::string::npos) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002982 DumpIMTForClass(runtime, line, h_class_loader, &prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07002983 } else {
2984 DumpIMTForMethod(runtime,
2985 line.substr(0, first_space),
2986 line.substr(first_space + 1, std::string::npos),
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002987 h_class_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07002988 &prepared);
2989 }
2990 std::cerr << std::endl;
2991 }
2992
2993 return true;
2994 }
2995
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002996 static bool DumpImtStats(Runtime* runtime,
2997 const std::vector<const DexFile*>& dex_files,
2998 Handle<mirror::ClassLoader> h_class_loader)
2999 REQUIRES_SHARED(Locks::mutator_lock_) {
3000 size_t without_imt = 0;
3001 size_t with_imt = 0;
Andreas Gampe9fded872016-09-25 16:08:35 -07003002 std::map<size_t, size_t> histogram;
3003
3004 ClassLinker* class_linker = runtime->GetClassLinker();
3005 const PointerSize pointer_size = class_linker->GetImagePointerSize();
3006 std::unordered_set<std::string> prepared;
3007
3008 Thread* self = Thread::Current();
Andreas Gampe9fded872016-09-25 16:08:35 -07003009 StackHandleScope<1> scope(self);
3010 MutableHandle<mirror::Class> h_klass(scope.NewHandle<mirror::Class>(nullptr));
3011
3012 for (const DexFile* dex_file : dex_files) {
3013 for (uint32_t class_def_index = 0;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003014 class_def_index != dex_file->NumClassDefs();
3015 ++class_def_index) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003016 const dex::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Andreas Gampe9fded872016-09-25 16:08:35 -07003017 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003018 h_klass.Assign(class_linker->FindClass(self, descriptor, h_class_loader));
Andreas Gampefa4333d2017-02-14 11:10:34 -08003019 if (h_klass == nullptr) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003020 std::cerr << "Warning: could not load " << descriptor << std::endl;
3021 continue;
3022 }
3023
3024 if (HasNoIMT(runtime, h_klass, pointer_size, &prepared)) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003025 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003026 continue;
3027 }
3028
3029 ImTable* im_table = PrepareAndGetImTable(runtime, h_klass, pointer_size, &prepared);
3030 if (im_table == nullptr) {
3031 // Should not happen, but accept.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003032 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003033 continue;
3034 }
3035
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003036 with_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003037 for (size_t imt_index = 0; imt_index != ImTable::kSize; ++imt_index) {
3038 ArtMethod* ptr = im_table->Get(imt_index, pointer_size);
3039 if (ptr->IsRuntimeMethod()) {
3040 if (ptr->IsImtUnimplementedMethod()) {
3041 histogram[0]++;
3042 } else {
3043 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3044 histogram[current_table->NumEntries(pointer_size)]++;
3045 }
3046 } else {
3047 histogram[1]++;
3048 }
3049 }
3050 }
3051 }
3052
3053 std::cerr << "IMT stats:"
3054 << std::endl << std::endl;
3055
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003056 std::cerr << " " << with_imt << " classes with IMT."
Andreas Gampe9fded872016-09-25 16:08:35 -07003057 << std::endl << std::endl;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003058 std::cerr << " " << without_imt << " classes without IMT (or copy from Object)."
Andreas Gampe9fded872016-09-25 16:08:35 -07003059 << std::endl << std::endl;
3060
3061 double sum_one = 0;
3062 size_t count_one = 0;
3063
3064 std::cerr << " " << "IMT histogram" << std::endl;
3065 for (auto& bucket : histogram) {
3066 std::cerr << " " << bucket.first << " " << bucket.second << std::endl;
3067 if (bucket.first > 0) {
3068 sum_one += bucket.second * bucket.first;
3069 count_one += bucket.second;
3070 }
3071 }
3072
3073 double count_zero = count_one + histogram[0];
3074 std::cerr << " Stats:" << std::endl;
3075 std::cerr << " Average depth (including empty): " << (sum_one / count_zero) << std::endl;
3076 std::cerr << " Average depth (excluding empty): " << (sum_one / count_one) << std::endl;
3077
3078 return true;
3079 }
3080
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003081 // Return whether the given class has no IMT (or the one shared with java.lang.Object).
Andreas Gampe9fded872016-09-25 16:08:35 -07003082 static bool HasNoIMT(Runtime* runtime,
3083 Handle<mirror::Class> klass,
3084 const PointerSize pointer_size,
3085 std::unordered_set<std::string>* prepared)
3086 REQUIRES_SHARED(Locks::mutator_lock_) {
3087 if (klass->IsObjectClass() || !klass->ShouldHaveImt()) {
3088 return true;
3089 }
3090
3091 if (klass->GetImt(pointer_size) == nullptr) {
3092 PrepareClass(runtime, klass, prepared);
3093 }
3094
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003095 ObjPtr<mirror::Class> object_class = GetClassRoot<mirror::Object>();
Andreas Gampe9fded872016-09-25 16:08:35 -07003096 DCHECK(object_class->IsObjectClass());
3097
3098 bool result = klass->GetImt(pointer_size) == object_class->GetImt(pointer_size);
3099
Mathieu Chartier6beced42016-11-15 15:51:31 -08003100 if (klass->GetIfTable()->Count() == 0) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003101 DCHECK(result);
3102 }
3103
3104 return result;
3105 }
3106
3107 static void PrintTable(ImtConflictTable* table, PointerSize pointer_size)
3108 REQUIRES_SHARED(Locks::mutator_lock_) {
3109 if (table == nullptr) {
3110 std::cerr << " <No IMT?>" << std::endl;
3111 return;
3112 }
3113 size_t table_index = 0;
3114 for (;;) {
3115 ArtMethod* ptr = table->GetInterfaceMethod(table_index, pointer_size);
3116 if (ptr == nullptr) {
3117 return;
3118 }
3119 table_index++;
David Sehr709b0702016-10-13 09:12:37 -07003120 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003121 }
3122 }
3123
3124 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3125 Thread* self,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003126 Handle<mirror::ClassLoader> h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003127 const std::string& class_name,
3128 const PointerSize pointer_size,
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003129 /*out*/ ObjPtr<mirror::Class>* klass_out,
3130 /*inout*/ std::unordered_set<std::string>* prepared)
Andreas Gampe9fded872016-09-25 16:08:35 -07003131 REQUIRES_SHARED(Locks::mutator_lock_) {
3132 if (class_name.empty()) {
3133 return nullptr;
3134 }
3135
3136 std::string descriptor;
3137 if (class_name[0] == 'L') {
3138 descriptor = class_name;
3139 } else {
3140 descriptor = DotToDescriptor(class_name.c_str());
3141 }
3142
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003143 ObjPtr<mirror::Class> klass =
3144 runtime->GetClassLinker()->FindClass(self, descriptor.c_str(), h_loader);
Andreas Gampe9fded872016-09-25 16:08:35 -07003145
3146 if (klass == nullptr) {
3147 self->ClearException();
3148 std::cerr << "Did not find " << class_name << std::endl;
3149 *klass_out = nullptr;
3150 return nullptr;
3151 }
3152
3153 StackHandleScope<1> scope(Thread::Current());
3154 Handle<mirror::Class> h_klass = scope.NewHandle<mirror::Class>(klass);
3155
3156 ImTable* ret = PrepareAndGetImTable(runtime, h_klass, pointer_size, prepared);
3157 *klass_out = h_klass.Get();
3158 return ret;
3159 }
3160
3161 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3162 Handle<mirror::Class> h_klass,
3163 const PointerSize pointer_size,
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003164 /*inout*/ std::unordered_set<std::string>* prepared)
Andreas Gampe9fded872016-09-25 16:08:35 -07003165 REQUIRES_SHARED(Locks::mutator_lock_) {
3166 PrepareClass(runtime, h_klass, prepared);
3167 return h_klass->GetImt(pointer_size);
3168 }
3169
3170 static void DumpIMTForClass(Runtime* runtime,
3171 const std::string& class_name,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003172 Handle<mirror::ClassLoader> h_loader,
3173 std::unordered_set<std::string>* prepared)
3174 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003175 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003176 ObjPtr<mirror::Class> klass;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003177 ImTable* imt = PrepareAndGetImTable(runtime,
3178 Thread::Current(),
3179 h_loader,
3180 class_name,
3181 pointer_size,
3182 &klass,
3183 prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07003184 if (imt == nullptr) {
3185 return;
3186 }
3187
3188 std::cerr << class_name << std::endl << " IMT:" << std::endl;
3189 for (size_t index = 0; index < ImTable::kSize; ++index) {
3190 std::cerr << " " << index << ":" << std::endl;
3191 ArtMethod* ptr = imt->Get(index, pointer_size);
3192 if (ptr->IsRuntimeMethod()) {
3193 if (ptr->IsImtUnimplementedMethod()) {
3194 std::cerr << " <empty>" << std::endl;
3195 } else {
3196 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3197 PrintTable(current_table, pointer_size);
3198 }
3199 } else {
David Sehr709b0702016-10-13 09:12:37 -07003200 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003201 }
3202 }
3203
3204 std::cerr << " Interfaces:" << std::endl;
3205 // Run through iftable, find methods that slot here, see if they fit.
3206 mirror::IfTable* if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003207 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
3208 mirror::Class* iface = if_table->GetInterface(i);
3209 std::string iface_name;
3210 std::cerr << " " << iface->GetDescriptor(&iface_name) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003211
Mathieu Chartier6beced42016-11-15 15:51:31 -08003212 for (ArtMethod& iface_method : iface->GetVirtualMethods(pointer_size)) {
3213 uint32_t class_hash, name_hash, signature_hash;
3214 ImTable::GetImtHashComponents(&iface_method, &class_hash, &name_hash, &signature_hash);
3215 uint32_t imt_slot = ImTable::GetImtIndex(&iface_method);
3216 std::cerr << " " << iface_method.PrettyMethod(true)
3217 << " slot=" << imt_slot
3218 << std::hex
3219 << " class_hash=0x" << class_hash
3220 << " name_hash=0x" << name_hash
3221 << " signature_hash=0x" << signature_hash
3222 << std::dec
3223 << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003224 }
3225 }
3226 }
3227
3228 static void DumpIMTForMethod(Runtime* runtime,
3229 const std::string& class_name,
3230 const std::string& method,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003231 Handle<mirror::ClassLoader> h_loader,
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003232 /*inout*/ std::unordered_set<std::string>* prepared)
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003233 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003234 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003235 ObjPtr<mirror::Class> klass;
Andreas Gampe9fded872016-09-25 16:08:35 -07003236 ImTable* imt = PrepareAndGetImTable(runtime,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003237 Thread::Current(),
3238 h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003239 class_name,
3240 pointer_size,
3241 &klass,
3242 prepared);
3243 if (imt == nullptr) {
3244 return;
3245 }
3246
3247 std::cerr << class_name << " <" << method << ">" << std::endl;
3248 for (size_t index = 0; index < ImTable::kSize; ++index) {
3249 ArtMethod* ptr = imt->Get(index, pointer_size);
3250 if (ptr->IsRuntimeMethod()) {
3251 if (ptr->IsImtUnimplementedMethod()) {
3252 continue;
3253 }
3254
3255 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3256 if (current_table == nullptr) {
3257 continue;
3258 }
3259
3260 size_t table_index = 0;
3261 for (;;) {
3262 ArtMethod* ptr2 = current_table->GetInterfaceMethod(table_index, pointer_size);
3263 if (ptr2 == nullptr) {
3264 break;
3265 }
3266 table_index++;
3267
David Sehr709b0702016-10-13 09:12:37 -07003268 std::string p_name = ptr2->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003269 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003270 std::cerr << " Slot "
3271 << index
3272 << " ("
3273 << current_table->NumEntries(pointer_size)
3274 << ")"
3275 << std::endl;
3276 PrintTable(current_table, pointer_size);
3277 return;
3278 }
3279 }
3280 } else {
David Sehr709b0702016-10-13 09:12:37 -07003281 std::string p_name = ptr->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003282 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003283 std::cerr << " Slot " << index << " (1)" << std::endl;
3284 std::cerr << " " << p_name << std::endl;
3285 } else {
3286 // Run through iftable, find methods that slot here, see if they fit.
3287 mirror::IfTable* if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003288 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
3289 mirror::Class* iface = if_table->GetInterface(i);
3290 size_t num_methods = iface->NumDeclaredVirtualMethods();
3291 if (num_methods > 0) {
3292 for (ArtMethod& iface_method : iface->GetMethods(pointer_size)) {
3293 if (ImTable::GetImtIndex(&iface_method) == index) {
3294 std::string i_name = iface_method.PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003295 if (android::base::StartsWith(i_name, method.c_str())) {
Mathieu Chartier6beced42016-11-15 15:51:31 -08003296 std::cerr << " Slot " << index << " (1)" << std::endl;
3297 std::cerr << " " << p_name << " (" << i_name << ")" << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003298 }
3299 }
3300 }
3301 }
3302 }
3303 }
3304 }
3305 }
3306 }
3307
3308 // Read lines from the given stream, dropping comments and empty lines
3309 static std::vector<std::string> ReadCommentedInputStream(std::istream& in_stream) {
3310 std::vector<std::string> output;
3311 while (in_stream.good()) {
3312 std::string dot;
3313 std::getline(in_stream, dot);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003314 if (android::base::StartsWith(dot, "#") || dot.empty()) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003315 continue;
3316 }
3317 output.push_back(dot);
3318 }
3319 return output;
3320 }
3321
3322 // Read lines from the given file, dropping comments and empty lines.
3323 static std::vector<std::string> ReadCommentedInputFromFile(const std::string& input_filename) {
3324 std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
3325 if (input_file.get() == nullptr) {
3326 LOG(ERROR) << "Failed to open input file " << input_filename;
3327 return std::vector<std::string>();
3328 }
3329 std::vector<std::string> result = ReadCommentedInputStream(*input_file);
3330 input_file->close();
3331 return result;
3332 }
3333
3334 // Prepare a class, i.e., ensure it has a filled IMT. Will do so recursively for superclasses,
3335 // and note in the given set that the work was done.
3336 static void PrepareClass(Runtime* runtime,
3337 Handle<mirror::Class> h_klass,
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003338 /*inout*/ std::unordered_set<std::string>* done)
Andreas Gampe9fded872016-09-25 16:08:35 -07003339 REQUIRES_SHARED(Locks::mutator_lock_) {
3340 if (!h_klass->ShouldHaveImt()) {
3341 return;
3342 }
3343
3344 std::string name;
3345 name = h_klass->GetDescriptor(&name);
3346
3347 if (done->find(name) != done->end()) {
3348 return;
3349 }
3350 done->insert(name);
3351
3352 if (h_klass->HasSuperClass()) {
3353 StackHandleScope<1> h(Thread::Current());
3354 PrepareClass(runtime, h.NewHandle<mirror::Class>(h_klass->GetSuperClass()), done);
3355 }
3356
3357 if (!h_klass->IsTemp()) {
3358 runtime->GetClassLinker()->FillIMTAndConflictTables(h_klass.Get());
3359 }
3360 }
3361};
3362
Igor Murashkin37743352014-11-13 14:38:00 -08003363struct OatdumpArgs : public CmdlineArgs {
3364 protected:
3365 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003366
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003367 ParseStatus ParseCustom(const char* raw_option,
3368 size_t raw_option_length,
3369 std::string* error_msg) override {
3370 DCHECK_EQ(strlen(raw_option), raw_option_length);
Igor Murashkin37743352014-11-13 14:38:00 -08003371 {
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003372 ParseStatus base_parse = Base::ParseCustom(raw_option, raw_option_length, error_msg);
Igor Murashkin37743352014-11-13 14:38:00 -08003373 if (base_parse != kParseUnknownArgument) {
3374 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003375 }
3376 }
3377
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003378 std::string_view option(raw_option, raw_option_length);
3379 if (StartsWith(option, "--oat-file=")) {
3380 oat_filename_ = raw_option + strlen("--oat-file=");
3381 } else if (StartsWith(option, "--dex-file=")) {
3382 dex_filename_ = raw_option + strlen("--dex-file=");
3383 } else if (StartsWith(option, "--image=")) {
3384 image_location_ = raw_option + strlen("--image=");
Igor Murashkin37743352014-11-13 14:38:00 -08003385 } else if (option == "--no-dump:vmap") {
3386 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01003387 } else if (option =="--dump:code_info_stack_maps") {
3388 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003389 } else if (option == "--no-disassemble") {
3390 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003391 } else if (option =="--header-only") {
3392 dump_header_only_ = true;
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003393 } else if (StartsWith(option, "--symbolize=")) {
3394 oat_filename_ = raw_option + strlen("--symbolize=");
Igor Murashkin37743352014-11-13 14:38:00 -08003395 symbolize_ = true;
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003396 } else if (StartsWith(option, "--only-keep-debug")) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00003397 only_keep_debug_ = true;
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003398 } else if (StartsWith(option, "--class-filter=")) {
3399 class_filter_ = raw_option + strlen("--class-filter=");
3400 } else if (StartsWith(option, "--method-filter=")) {
3401 method_filter_ = raw_option + strlen("--method-filter=");
3402 } else if (StartsWith(option, "--list-classes")) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003403 list_classes_ = true;
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003404 } else if (StartsWith(option, "--list-methods")) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003405 list_methods_ = true;
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003406 } else if (StartsWith(option, "--export-dex-to=")) {
3407 export_dex_location_ = raw_option + strlen("--export-dex-to=");
3408 } else if (StartsWith(option, "--addr2instr=")) {
3409 if (!android::base::ParseUint(raw_option + strlen("--addr2instr="), &addr2instr_)) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003410 *error_msg = "Address conversion failed";
3411 return kParseError;
3412 }
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003413 } else if (StartsWith(option, "--app-image=")) {
3414 app_image_ = raw_option + strlen("--app-image=");
3415 } else if (StartsWith(option, "--app-oat=")) {
3416 app_oat_ = raw_option + strlen("--app-oat=");
3417 } else if (StartsWith(option, "--dump-imt=")) {
3418 imt_dump_ = std::string(option.substr(strlen("--dump-imt=")));
Andreas Gampe9fded872016-09-25 16:08:35 -07003419 } else if (option == "--dump-imt-stats") {
3420 imt_stat_dump_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003421 } else {
3422 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003423 }
3424
Igor Murashkin37743352014-11-13 14:38:00 -08003425 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003426 }
3427
Roland Levillainf73caca2018-08-24 17:19:07 +01003428 ParseStatus ParseChecks(std::string* error_msg) override {
Igor Murashkin37743352014-11-13 14:38:00 -08003429 // Infer boot image location from the image location if possible.
3430 if (boot_image_location_ == nullptr) {
3431 boot_image_location_ = image_location_;
3432 }
3433
3434 // Perform the parent checks.
3435 ParseStatus parent_checks = Base::ParseChecks(error_msg);
3436 if (parent_checks != kParseOk) {
3437 return parent_checks;
3438 }
3439
3440 // Perform our own checks.
3441 if (image_location_ == nullptr && oat_filename_ == nullptr) {
3442 *error_msg = "Either --image or --oat-file must be specified";
3443 return kParseError;
3444 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
3445 *error_msg = "Either --image or --oat-file must be specified but not both";
3446 return kParseError;
3447 }
3448
3449 return kParseOk;
3450 }
3451
Andreas Gampefa6a1b02018-09-07 08:11:55 -07003452 std::string GetUsage() const override {
Igor Murashkin37743352014-11-13 14:38:00 -08003453 std::string usage;
3454
3455 usage +=
3456 "Usage: oatdump [options] ...\n"
3457 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
3458 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
3459 "\n"
3460 // Either oat-file or image is required.
3461 " --oat-file=<file.oat>: specifies an input oat filename.\n"
3462 " Example: --oat-file=/system/framework/boot.oat\n"
3463 "\n"
3464 " --image=<file.art>: specifies an input image location.\n"
3465 " Example: --image=/system/framework/boot.art\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003466 "\n"
3467 " --app-image=<file.art>: specifies an input app image. Must also have a specified\n"
Roland Levillain4f1c9e62017-06-28 16:44:30 +01003468 " boot image (with --image) and app oat file (with --app-oat).\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003469 " Example: --app-image=app.art\n"
3470 "\n"
3471 " --app-oat=<file.odex>: specifies an input app oat.\n"
3472 " Example: --app-oat=app.odex\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003473 "\n";
3474
3475 usage += Base::GetUsage();
3476
3477 usage += // Optional.
Igor Murashkin37743352014-11-13 14:38:00 -08003478 " --no-dump:vmap may be used to disable vmap dumping.\n"
3479 " Example: --no-dump:vmap\n"
3480 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01003481 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
3482 " Example: --dump:code_info_stack_maps\n"
3483 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003484 " --no-disassemble may be used to disable disassembly.\n"
3485 " Example: --no-disassemble\n"
3486 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00003487 " --header-only may be used to print only the oat header.\n"
3488 " Example: --header-only\n"
3489 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003490 " --list-classes may be used to list target file classes (can be used with filters).\n"
3491 " Example: --list-classes\n"
3492 " Example: --list-classes --class-filter=com.example.foo\n"
3493 "\n"
3494 " --list-methods may be used to list target file methods (can be used with filters).\n"
3495 " Example: --list-methods\n"
3496 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
3497 "\n"
3498 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
3499 " Example: --symbolize=/system/framework/boot.oat\n"
3500 "\n"
David Srbecky2fdd03c2016-03-10 15:32:37 +00003501 " --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
3502 " .rodata and .text sections are omitted in the output file to save space.\n"
3503 " Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
3504 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003505 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
3506 " Example: --class-filter=com.example.foo\n"
3507 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003508 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
3509 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003510 "\n"
3511 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
3512 " Example: --export-dex-to=/data/local/tmp\n"
3513 "\n"
3514 " --addr2instr=<address>: output matching method disassembled code from relative\n"
3515 " address (e.g. PC from crash dump)\n"
3516 " Example: --addr2instr=0x00001a3b\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003517 "\n"
3518 " --dump-imt=<file.txt>: output IMT collisions (if any) for the given receiver\n"
3519 " types and interface methods in the given file. The file\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003520 " is read line-wise, where each line should either be a class\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003521 " name or descriptor, or a class name/descriptor and a prefix\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003522 " of a complete method name (separated by a whitespace).\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003523 " Example: --dump-imt=imt.txt\n"
3524 "\n"
3525 " --dump-imt-stats: output IMT statistics for the given boot image\n"
3526 " Example: --dump-imt-stats"
Igor Murashkin37743352014-11-13 14:38:00 -08003527 "\n";
3528
3529 return usage;
3530 }
3531
3532 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07003533 const char* oat_filename_ = nullptr;
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003534 const char* dex_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003535 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00003536 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07003537 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003538 std::string elf_filename_prefix_;
Andreas Gampe9fded872016-09-25 16:08:35 -07003539 std::string imt_dump_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003540 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01003541 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003542 bool disassemble_code_ = true;
3543 bool symbolize_ = false;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003544 bool only_keep_debug_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003545 bool list_classes_ = false;
3546 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003547 bool dump_header_only_ = false;
Andreas Gampe9fded872016-09-25 16:08:35 -07003548 bool imt_stat_dump_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003549 uint32_t addr2instr_ = 0;
3550 const char* export_dex_location_ = nullptr;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003551 const char* app_image_ = nullptr;
3552 const char* app_oat_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003553};
3554
Igor Murashkin37743352014-11-13 14:38:00 -08003555struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
Roland Levillainf73caca2018-08-24 17:19:07 +01003556 bool NeedsRuntime() override {
Igor Murashkin37743352014-11-13 14:38:00 -08003557 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003558
Igor Murashkin37743352014-11-13 14:38:00 -08003559 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
3560 bool absolute_addresses = (args_->oat_filename_ == nullptr);
3561
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003562 oat_dumper_options_.reset(new OatDumperOptions(
Igor Murashkin37743352014-11-13 14:38:00 -08003563 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01003564 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08003565 args_->disassemble_code_,
3566 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003567 args_->class_filter_,
3568 args_->method_filter_,
3569 args_->list_classes_,
3570 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00003571 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003572 args_->export_dex_location_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003573 args_->app_image_,
3574 args_->app_oat_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003575 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08003576
Andreas Gampe9fded872016-09-25 16:08:35 -07003577 return (args_->boot_image_location_ != nullptr ||
3578 args_->image_location_ != nullptr ||
3579 !args_->imt_dump_.empty()) &&
Igor Murashkin37743352014-11-13 14:38:00 -08003580 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003581 }
3582
Roland Levillainf73caca2018-08-24 17:19:07 +01003583 bool ExecuteWithoutRuntime() override {
Igor Murashkin37743352014-11-13 14:38:00 -08003584 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08003585 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003586
Mathieu Chartierd424d082014-10-15 10:31:46 -07003587 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08003588
Andreas Gampec24f3992014-12-17 20:40:11 -08003589 if (args_->symbolize_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00003590 // ELF has special kind of section called SHT_NOBITS which allows us to create
3591 // sections which exist but their data is omitted from the ELF file to save space.
3592 // This is what "strip --only-keep-debug" does when it creates separate ELF file
3593 // with only debug data. We use it in similar way to exclude .rodata and .text.
3594 bool no_bits = args_->only_keep_debug_;
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003595 return SymbolizeOat(args_->oat_filename_, args_->dex_filename_, args_->output_name_, no_bits)
3596 == EXIT_SUCCESS;
Andreas Gampec24f3992014-12-17 20:40:11 -08003597 } else {
3598 return DumpOat(nullptr,
3599 args_->oat_filename_,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003600 args_->dex_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003601 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08003602 args_->os_) == EXIT_SUCCESS;
3603 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07003604 }
3605
Andreas Gampefa6a1b02018-09-07 08:11:55 -07003606 bool ExecuteWithRuntime(Runtime* runtime) override {
Igor Murashkin37743352014-11-13 14:38:00 -08003607 CHECK(args_ != nullptr);
3608
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003609 if (!args_->imt_dump_.empty() || args_->imt_stat_dump_) {
3610 return IMTDumper::Dump(runtime,
3611 args_->imt_dump_,
3612 args_->imt_stat_dump_,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003613 args_->oat_filename_,
3614 args_->dex_filename_);
Andreas Gampe9fded872016-09-25 16:08:35 -07003615 }
3616
Igor Murashkin37743352014-11-13 14:38:00 -08003617 if (args_->oat_filename_ != nullptr) {
3618 return DumpOat(runtime,
3619 args_->oat_filename_,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003620 args_->dex_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003621 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08003622 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003623 }
Igor Murashkin37743352014-11-13 14:38:00 -08003624
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003625 return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003626 }
3627
Igor Murashkin37743352014-11-13 14:38:00 -08003628 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
3629};
Andreas Gampe00b25f32014-09-17 21:49:05 -07003630
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003631} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07003632
3633int main(int argc, char** argv) {
Andreas Gampe221d9812018-01-22 17:48:56 -08003634 // Output all logging to stderr.
3635 android::base::SetLogger(android::base::StderrLogger);
3636
Igor Murashkin37743352014-11-13 14:38:00 -08003637 art::OatdumpMain main;
3638 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07003639}