blob: cfecb61e3e71ad0bdadfe9c257b649e4f0e25feb [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"
David Srbecky2faab002019-02-12 16:35:48 +000062#include "elf/elf_builder.h"
Andreas Gampe0c183382017-07-13 22:26:24 -070063#include "gc/accounting/space_bitmap-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070064#include "gc/space/image_space.h"
65#include "gc/space/large_object_space.h"
66#include "gc/space/space-inl.h"
Mathieu Chartier4a26f172016-01-26 14:26:18 -080067#include "image-inl.h"
Andreas Gampe9fded872016-09-25 16:08:35 -070068#include "imtable-inl.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"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080071#include "mirror/array-inl.h"
72#include "mirror/class-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010073#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080074#include "mirror/object-inl.h"
75#include "mirror/object_array-inl.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080076#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010077#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070078#include "oat_file_manager.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070079#include "scoped_thread_state_change-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070080#include "stack.h"
Mathieu Chartierdc00f182016-07-14 10:10:44 -070081#include "stack_map.h"
David Srbecky2faab002019-02-12 16:35:48 +000082#include "stream/buffered_output_stream.h"
83#include "stream/file_output_stream.h"
84#include "subtype_check.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",
Alex Light885f0c12019-01-09 13:48:55 -0800113 "kSpecialRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -0700114};
115
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700116// Map is so that we don't allocate multiple dex files for the same OatDexFile.
Andreas Gampeb40d3612018-06-26 15:49:42 -0700117static std::map<const OatDexFile*, std::unique_ptr<const DexFile>> opened_dex_files;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700118
Andreas Gampeb40d3612018-06-26 15:49:42 -0700119const DexFile* OpenDexFile(const OatDexFile* oat_dex_file, std::string* error_msg) {
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700120 DCHECK(oat_dex_file != nullptr);
121 auto it = opened_dex_files.find(oat_dex_file);
122 if (it != opened_dex_files.end()) {
123 return it->second.get();
124 }
125 const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
126 opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
127 return ret;
128}
129
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800130template <typename ElfTypes>
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100131class OatSymbolizer final {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700132 public:
David Srbecky2fdd03c2016-03-10 15:32:37 +0000133 OatSymbolizer(const OatFile* oat_file, const std::string& output_name, bool no_bits) :
134 oat_file_(oat_file),
135 builder_(nullptr),
136 output_name_(output_name.empty() ? "symbolized.oat" : output_name),
137 no_bits_(no_bits) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700138 }
139
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700140 bool Symbolize() {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000141 const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800142 std::unique_ptr<const InstructionSetFeatures> features = InstructionSetFeatures::FromBitmap(
David Srbecky5d811202016-03-08 13:21:22 +0000143 isa, oat_file_->GetOatHeader().GetInstructionSetFeaturesBitmap());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000144
Andreas Gampe08c277c2017-04-26 22:22:15 -0700145 std::unique_ptr<File> elf_file(OS::CreateEmptyFile(output_name_.c_str()));
146 if (elf_file == nullptr) {
147 return false;
148 }
David Srbecky2faab002019-02-12 16:35:48 +0000149 std::unique_ptr<BufferedOutputStream> output_stream =
150 std::make_unique<BufferedOutputStream>(
151 std::make_unique<FileOutputStream>(elf_file.get()));
152 builder_.reset(new ElfBuilder<ElfTypes>(isa, output_stream.get()));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000153
154 builder_->Start();
155
156 auto* rodata = builder_->GetRoData();
157 auto* text = builder_->GetText();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000158
David Srbecky6d8c8f02015-10-26 10:57:09 +0000159 const uint8_t* rodata_begin = oat_file_->Begin();
160 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
David Srbeckye155f4b2017-12-06 15:18:38 +0000161 if (!no_bits_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +0000162 rodata->Start();
163 rodata->WriteFully(rodata_begin, rodata_size);
164 rodata->End();
165 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000166
David Srbecky6d8c8f02015-10-26 10:57:09 +0000167 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
168 const size_t text_size = oat_file_->End() - text_begin;
David Srbeckye155f4b2017-12-06 15:18:38 +0000169 if (!no_bits_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +0000170 text->Start();
171 text->WriteFully(text_begin, text_size);
172 text->End();
173 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000174
Vladimir Markoaad75c62016-10-03 08:46:48 +0000175 builder_->PrepareDynamicSection(elf_file->GetPath(),
176 rodata_size,
177 text_size,
Vladimir Markob066d432018-01-03 13:14:37 +0000178 oat_file_->DataBimgRelRoSize(),
Vladimir Markoaad75c62016-10-03 08:46:48 +0000179 oat_file_->BssSize(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100180 oat_file_->BssMethodsOffset(),
David Srbeckyec2cdf42017-12-08 16:21:25 +0000181 oat_file_->BssRootsOffset(),
182 oat_file_->VdexSize());
Vladimir Marko944da602016-02-19 12:27:55 +0000183 builder_->WriteDynamicSection();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700184
Vladimir Marko1b404a82017-09-01 13:35:26 +0100185 const OatHeader& oat_header = oat_file_->GetOatHeader();
186 #define DO_TRAMPOLINE(fn_name) \
187 if (oat_header.Get ## fn_name ## Offset() != 0) { \
188 debug::MethodDebugInfo info = {}; \
David Srbeckyc684f332018-01-19 17:38:06 +0000189 info.custom_name = #fn_name; \
Vladimir Marko1b404a82017-09-01 13:35:26 +0100190 info.isa = oat_header.GetInstructionSet(); \
191 info.is_code_address_text_relative = true; \
192 size_t code_offset = oat_header.Get ## fn_name ## Offset(); \
193 code_offset -= CompiledCode::CodeDelta(oat_header.GetInstructionSet()); \
194 info.code_address = code_offset - oat_header.GetExecutableOffset(); \
195 info.code_size = 0; /* The symbol lasts until the next symbol. */ \
196 method_debug_infos_.push_back(std::move(info)); \
197 }
Vladimir Marko7dac8642019-11-06 17:09:30 +0000198 DO_TRAMPOLINE(JniDlsymLookupTrampoline);
Vladimir Marko1b404a82017-09-01 13:35:26 +0100199 DO_TRAMPOLINE(QuickGenericJniTrampoline);
200 DO_TRAMPOLINE(QuickImtConflictTrampoline);
201 DO_TRAMPOLINE(QuickResolutionTrampoline);
202 DO_TRAMPOLINE(QuickToInterpreterBridge);
203 #undef DO_TRAMPOLINE
204
David Srbecky5d950762016-03-07 20:47:29 +0000205 Walk();
Vladimir Marko1b404a82017-09-01 13:35:26 +0100206
207 // TODO: Try to symbolize link-time thunks?
208 // This would require disassembling all methods to find branches outside the method code.
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700209
David Srbecky32210b92017-12-04 14:39:21 +0000210 // TODO: Add symbols for dex bytecode in the .dex section.
211
212 debug::DebugInfo debug_info{};
213 debug_info.compiled_methods = ArrayRef<const debug::MethodDebugInfo>(method_debug_infos_);
214
David Srbecky7370d922019-02-12 14:00:30 +0000215 debug::WriteDebugInfo(builder_.get(), debug_info);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700216
David Srbecky6d8c8f02015-10-26 10:57:09 +0000217 builder_->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700218
Andreas Gampe08c277c2017-04-26 22:22:15 -0700219 bool ret_value = builder_->Good();
220
221 builder_.reset();
222 output_stream.reset();
223
224 if (elf_file->FlushCloseOrErase() != 0) {
225 return false;
226 }
227 elf_file.reset();
228
229 return ret_value;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700230 }
231
David Srbecky5d950762016-03-07 20:47:29 +0000232 void Walk() {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700233 std::vector<const OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700234 for (size_t i = 0; i < oat_dex_files.size(); i++) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700235 const OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700236 CHECK(oat_dex_file != nullptr);
David Srbecky5d950762016-03-07 20:47:29 +0000237 WalkOatDexFile(oat_dex_file);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700238 }
239 }
240
Andreas Gampeb40d3612018-06-26 15:49:42 -0700241 void WalkOatDexFile(const OatDexFile* oat_dex_file) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700242 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700243 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
244 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700245 return;
246 }
247 for (size_t class_def_index = 0;
248 class_def_index < dex_file->NumClassDefs();
249 class_def_index++) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700250 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
251 OatClassType type = oat_class.GetType();
252 switch (type) {
253 case kOatClassAllCompiled:
254 case kOatClassSomeCompiled:
David Srbecky5d950762016-03-07 20:47:29 +0000255 WalkOatClass(oat_class, *dex_file, class_def_index);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700256 break;
257
258 case kOatClassNoneCompiled:
259 case kOatClassMax:
260 // Ignore.
261 break;
262 }
263 }
264 }
265
David Srbecky5d950762016-03-07 20:47:29 +0000266 void WalkOatClass(const OatFile::OatClass& oat_class,
267 const DexFile& dex_file,
268 uint32_t class_def_index) {
Mathieu Chartier18e26872018-06-04 17:19:02 -0700269 ClassAccessor accessor(dex_file, class_def_index);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700270 // Note: even if this is an interface or a native class, we still have to walk it, as there
271 // might be a static initializer.
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700272 uint32_t class_method_idx = 0;
Mathieu Chartier3d092992018-05-24 13:36:23 -0700273 for (const ClassAccessor::Method& method : accessor.GetMethods()) {
David Srbecky5d950762016-03-07 20:47:29 +0000274 WalkOatMethod(oat_class.GetOatMethod(class_method_idx++),
275 dex_file,
276 class_def_index,
Mathieu Chartier3d092992018-05-24 13:36:23 -0700277 method.GetIndex(),
278 method.GetCodeItem(),
279 method.GetAccessFlags());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700280 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700281 }
282
David Srbecky5d950762016-03-07 20:47:29 +0000283 void WalkOatMethod(const OatFile::OatMethod& oat_method,
284 const DexFile& dex_file,
285 uint32_t class_def_index,
286 uint32_t dex_method_index,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800287 const dex::CodeItem* code_item,
David Srbecky5d950762016-03-07 20:47:29 +0000288 uint32_t method_access_flags) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700289 if ((method_access_flags & kAccAbstract) != 0) {
290 // Abstract method, no code.
291 return;
292 }
David Srbecky5d950762016-03-07 20:47:29 +0000293 const OatHeader& oat_header = oat_file_->GetOatHeader();
294 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
295 if (method_header == nullptr || method_header->GetCodeSize() == 0) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700296 // No code.
297 return;
298 }
299
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100300 uint32_t entry_point = oat_method.GetCodeOffset() - oat_header.GetExecutableOffset();
301 // Clear Thumb2 bit.
302 const void* code_address = EntryPointToCodePointer(reinterpret_cast<void*>(entry_point));
303
Vladimir Marko1b404a82017-09-01 13:35:26 +0100304 debug::MethodDebugInfo info = {};
David Srbeckyc684f332018-01-19 17:38:06 +0000305 DCHECK(info.custom_name.empty());
David Srbecky5d950762016-03-07 20:47:29 +0000306 info.dex_file = &dex_file;
307 info.class_def_index = class_def_index;
308 info.dex_method_index = dex_method_index;
309 info.access_flags = method_access_flags;
310 info.code_item = code_item;
311 info.isa = oat_header.GetInstructionSet();
312 info.deduped = !seen_offsets_.insert(oat_method.GetCodeOffset()).second;
313 info.is_native_debuggable = oat_header.IsNativeDebuggable();
314 info.is_optimized = method_header->IsOptimized();
315 info.is_code_address_text_relative = true;
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100316 info.code_address = reinterpret_cast<uintptr_t>(code_address);
David Srbecky5d950762016-03-07 20:47:29 +0000317 info.code_size = method_header->GetCodeSize();
318 info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
319 info.code_info = info.is_optimized ? method_header->GetOptimizedCodeInfoPtr() : nullptr;
320 info.cfi = ArrayRef<uint8_t>();
321 method_debug_infos_.push_back(info);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700322 }
323
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700324 private:
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700325 const OatFile* oat_file_;
David Srbecky2faab002019-02-12 16:35:48 +0000326 std::unique_ptr<ElfBuilder<ElfTypes>> builder_;
David Srbecky5d950762016-03-07 20:47:29 +0000327 std::vector<debug::MethodDebugInfo> method_debug_infos_;
328 std::unordered_set<uint32_t> seen_offsets_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700329 const std::string output_name_;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000330 bool no_bits_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700331};
332
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700333class OatDumperOptions {
334 public:
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100335 OatDumperOptions(bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100336 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700337 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700338 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800339 const char* class_filter,
340 const char* method_filter,
341 bool list_classes,
342 bool list_methods,
David Brazdilc03d7b62016-03-02 12:18:03 +0000343 bool dump_header_only,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800344 const char* export_dex_location,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800345 const char* app_image,
346 const char* app_oat,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800347 uint32_t addr2instr)
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100348 : dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100349 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700350 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700351 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800352 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000353 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800354 list_classes_(list_classes),
355 list_methods_(list_methods),
David Brazdilc03d7b62016-03-02 12:18:03 +0000356 dump_header_only_(dump_header_only),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800357 export_dex_location_(export_dex_location),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800358 app_image_(app_image),
359 app_oat_(app_oat),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800360 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800361 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700362
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700363 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100364 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700365 const bool disassemble_code_;
366 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800367 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000368 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800369 const bool list_classes_;
370 const bool list_methods_;
David Brazdilc03d7b62016-03-02 12:18:03 +0000371 const bool dump_header_only_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800372 const char* const export_dex_location_;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800373 const char* const app_image_;
374 const char* const app_oat_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800375 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700376 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700377};
378
Elliott Hughese3c845c2012-02-28 17:23:01 -0800379class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700380 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100381 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000382 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800383 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700384 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800385 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800386 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
387 disassembler_(Disassembler::Create(instruction_set_,
Andreas Gampe372f3a32016-08-19 10:49:06 -0700388 new DisassemblerOptions(
389 options_.absolute_addresses_,
390 oat_file.Begin(),
391 oat_file.End(),
Andreas Gampe9b031f72018-10-04 11:03:34 -0700392 /* can_read_literals_= */ true,
Andreas Gampe372f3a32016-08-19 10:49:06 -0700393 Is64BitInstructionSet(instruction_set_)
394 ? &Thread::DumpThreadOffset<PointerSize::k64>
395 : &Thread::DumpThreadOffset<PointerSize::k32>))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800396 CHECK(options_.class_loader_ != nullptr);
397 CHECK(options_.class_filter_ != nullptr);
398 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800399 AddAllOffsets();
400 }
401
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700402 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700403 delete disassembler_;
404 }
405
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800406 InstructionSet GetInstructionSet() {
407 return instruction_set_;
408 }
409
Andreas Gampec55bb392018-09-21 00:02:02 +0000410 using DexFileUniqV = std::vector<std::unique_ptr<const DexFile>>;
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300411
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700412 bool Dump(std::ostream& os) {
413 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800414 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700415
416 os << "MAGIC:\n";
417 os << oat_header.GetMagic() << "\n\n";
418
Jeff Hao4b07a6e2016-01-15 12:50:44 -0800419 os << "LOCATION:\n";
420 os << oat_file_.GetLocation() << "\n\n";
421
Brian Carlstromaded5f72011-10-07 17:15:04 -0700422 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800423 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700424
Elliott Hughesa72ec822012-03-05 17:12:22 -0800425 os << "INSTRUCTION SET:\n";
426 os << oat_header.GetInstructionSet() << "\n\n";
427
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700428 {
429 std::unique_ptr<const InstructionSetFeatures> features(
430 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
431 oat_header.GetInstructionSetFeaturesBitmap()));
432 os << "INSTRUCTION SET FEATURES:\n";
433 os << features->GetFeatureString() << "\n\n";
434 }
Dave Allison70202782013-10-22 17:52:19 -0700435
Brian Carlstromaded5f72011-10-07 17:15:04 -0700436 os << "DEX FILE COUNT:\n";
437 os << oat_header.GetDexFileCount() << "\n\n";
438
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800439#define DUMP_OAT_HEADER_OFFSET(label, offset) \
440 os << label " OFFSET:\n"; \
441 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800442 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800443 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
444 } \
445 os << StringPrintf("\n\n");
446
447 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
Vladimir Marko7dac8642019-11-06 17:09:30 +0000448 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP TRAMPOLINE",
449 GetJniDlsymLookupTrampolineOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800450 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
451 GetQuickGenericJniTrampolineOffset);
452 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
453 GetQuickImtConflictTrampolineOffset);
454 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
455 GetQuickResolutionTrampolineOffset);
456 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
457 GetQuickToInterpreterBridgeOffset);
458#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700459
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700460 // Print the key-value store.
461 {
462 os << "KEY VALUE STORE:\n";
463 size_t index = 0;
464 const char* key;
465 const char* value;
466 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
467 os << key << " = " << value << "\n";
468 index++;
469 }
470 os << "\n";
471 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700472
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800473 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700474 os << "BEGIN:\n";
475 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700476
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700477 os << "END:\n";
478 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
479 }
480
481 os << "SIZE:\n";
482 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700483
484 os << std::flush;
485
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800486 // If set, adjust relative address to be searched
487 if (options_.addr2instr_ != 0) {
488 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
489 os << "SEARCH ADDRESS (executable offset + input):\n";
490 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
491 }
492
Vladimir Marko812fb4d2018-03-14 13:07:21 +0000493 // Dump .data.bimg.rel.ro entries.
494 DumpDataBimgRelRoEntries(os);
495
496 // Dump .bss summary, individual entries are dumped per dex file.
497 os << ".bss: ";
498 if (oat_file_.GetBssMethods().empty() && oat_file_.GetBssGcRoots().empty()) {
499 os << "empty.\n\n";
500 } else {
501 os << oat_file_.GetBssMethods().size() << " methods, ";
502 os << oat_file_.GetBssGcRoots().size() << " GC roots.\n\n";
503 }
504
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700505 // Dumping the dex file overview is compact enough to do even if header only.
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700506 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700507 const OatDexFile* oat_dex_file = oat_dex_files_[i];
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700508 CHECK(oat_dex_file != nullptr);
509 std::string error_msg;
510 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
511 if (dex_file == nullptr) {
512 os << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation() << "': "
513 << error_msg;
514 continue;
515 }
Mathieu Chartierba4c1182018-05-24 14:05:55 -0700516
Mathieu Chartier120aa282017-08-05 16:03:03 -0700517 const DexLayoutSections* const layout_sections = oat_dex_file->GetDexLayoutSections();
518 if (layout_sections != nullptr) {
519 os << "Layout data\n";
520 os << *layout_sections;
521 os << "\n";
522 }
523
Vladimir Markobacb8e42018-06-13 14:45:30 +0100524 if (!options_.dump_header_only_) {
525 // Dump .bss entries.
526 DumpBssEntries(
527 os,
528 "ArtMethod",
529 oat_dex_file->GetMethodBssMapping(),
530 dex_file->NumMethodIds(),
531 static_cast<size_t>(GetInstructionSetPointerSize(instruction_set_)),
532 [=](uint32_t index) { return dex_file->PrettyMethod(index); });
533 DumpBssEntries(
534 os,
535 "Class",
536 oat_dex_file->GetTypeBssMapping(),
537 dex_file->NumTypeIds(),
538 sizeof(GcRoot<mirror::Class>),
539 [=](uint32_t index) { return dex_file->PrettyType(dex::TypeIndex(index)); });
540 DumpBssEntries(
541 os,
542 "String",
543 oat_dex_file->GetStringBssMapping(),
544 dex_file->NumStringIds(),
545 sizeof(GcRoot<mirror::Class>),
546 [=](uint32_t index) { return dex_file->StringDataByIdx(dex::StringIndex(index)); });
547 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700548 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700549
David Brazdilc03d7b62016-03-02 12:18:03 +0000550 if (!options_.dump_header_only_) {
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000551 VariableIndentationOutputStream vios(&os);
Nicolas Geoffray3a293552018-03-02 10:52:16 +0000552 VdexFile::VerifierDepsHeader vdex_header = oat_file_.GetVdexFile()->GetVerifierDepsHeader();
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000553 if (vdex_header.IsValid()) {
554 std::string error_msg;
555 std::vector<const DexFile*> dex_files;
556 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
557 const DexFile* dex_file = OpenDexFile(oat_dex_files_[i], &error_msg);
558 if (dex_file == nullptr) {
559 os << "Error opening dex file: " << error_msg << std::endl;
560 return false;
561 }
562 dex_files.push_back(dex_file);
563 }
564 verifier::VerifierDeps deps(dex_files, oat_file_.GetVdexFile()->GetVerifierDepsData());
565 deps.Dump(&vios);
566 } else {
567 os << "UNRECOGNIZED vdex file, magic "
568 << vdex_header.GetMagic()
Nicolas Geoffray3a293552018-03-02 10:52:16 +0000569 << ", verifier deps version "
570 << vdex_header.GetVerifierDepsVersion()
571 << ", dex section version "
572 << vdex_header.GetDexSectionVersion()
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000573 << "\n";
574 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000575 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700576 const OatDexFile* oat_dex_file = oat_dex_files_[i];
David Brazdilc03d7b62016-03-02 12:18:03 +0000577 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300578 if (!DumpOatDexFile(os, *oat_dex_file)) {
579 success = false;
580 }
581 }
582 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800583
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300584 if (options_.export_dex_location_) {
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000585 std::string error_msg;
586 std::string vdex_filename = GetVdexFilename(oat_file_.GetLocation());
587 if (!OS::FileExists(vdex_filename.c_str())) {
588 os << "File " << vdex_filename.c_str() << " does not exist\n";
589 return false;
590 }
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300591
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000592 DexFileUniqV vdex_dex_files;
593 std::unique_ptr<const VdexFile> vdex_file = OpenVdexUnquicken(vdex_filename,
594 &vdex_dex_files,
595 &error_msg);
596 if (vdex_file.get() == nullptr) {
597 os << "Failed to open vdex file: " << error_msg << "\n";
598 return false;
599 }
600 if (oat_dex_files_.size() != vdex_dex_files.size()) {
601 os << "Dex files number in Vdex file does not match Dex files number in Oat file: "
602 << vdex_dex_files.size() << " vs " << oat_dex_files_.size() << '\n';
603 return false;
604 }
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300605
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000606 size_t i = 0;
607 for (const auto& vdex_dex_file : vdex_dex_files) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700608 const OatDexFile* oat_dex_file = oat_dex_files_[i];
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000609 CHECK(oat_dex_file != nullptr);
610 CHECK(vdex_dex_file != nullptr);
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300611
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300612 // If a CompactDex file is detected within a Vdex container, DexLayout is used to convert
613 // back to a StandardDex file. Since the converted DexFile will most likely not reproduce
614 // the original input Dex file, the `update_checksum_` option is used to recompute the
615 // checksum. If the vdex container does not contain cdex resources (`used_dexlayout` is
616 // false), ExportDexFile() enforces a reproducible checksum verification.
617 if (vdex_dex_file->IsCompactDexFile()) {
618 Options options;
619 options.compact_dex_level_ = CompactDexLevel::kCompactDexLevelNone;
620 options.update_checksum_ = true;
Andreas Gampe9b031f72018-10-04 11:03:34 -0700621 DexLayout dex_layout(options, /*info=*/ nullptr, /*out_file=*/ nullptr, /*header=*/ nullptr);
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300622 std::unique_ptr<art::DexContainer> dex_container;
623 bool result = dex_layout.ProcessDexFile(vdex_dex_file->GetLocation().c_str(),
624 vdex_dex_file.get(),
625 i,
626 &dex_container,
627 &error_msg);
628 if (!result) {
629 os << "DexLayout failed to process Dex file: " + error_msg;
630 success = false;
631 break;
632 }
633 DexContainer::Section* main_section = dex_container->GetMainSection();
634 CHECK_EQ(dex_container->GetDataSection()->Size(), 0u);
635
636 const ArtDexFileLoader dex_file_loader;
637 std::unique_ptr<const DexFile> dex(dex_file_loader.Open(
638 main_section->Begin(),
639 main_section->Size(),
640 vdex_dex_file->GetLocation(),
641 vdex_file->GetLocationChecksum(i),
Andreas Gampe9b031f72018-10-04 11:03:34 -0700642 /*oat_dex_file=*/ nullptr,
643 /*verify=*/ false,
644 /*verify_checksum=*/ true,
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300645 &error_msg));
646 if (dex == nullptr) {
647 os << "Failed to load DexFile from layout container: " + error_msg;
648 success = false;
649 break;
650 }
651 if (dex->IsCompactDexFile()) {
652 os <<"CompactDex conversion to StandardDex failed";
653 success = false;
654 break;
655 }
656
Andreas Gampe9b031f72018-10-04 11:03:34 -0700657 if (!ExportDexFile(os, *oat_dex_file, dex.get(), /*used_dexlayout=*/ true)) {
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300658 success = false;
659 break;
660 }
661 } else {
Andreas Gampe9b031f72018-10-04 11:03:34 -0700662 if (!ExportDexFile(os, *oat_dex_file, vdex_dex_file.get(), /*used_dexlayout=*/ false)) {
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300663 success = false;
664 break;
665 }
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300666 }
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000667 i++;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700668 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700669 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000670
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800671 {
672 os << "OAT FILE STATS:\n";
673 VariableIndentationOutputStream vios(&os);
David Srbecky86decb62018-06-05 06:41:10 +0100674 stats_.AddBytes(oat_file_.Size());
675 DumpStats(vios, "OatFile", stats_, stats_.Value());
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800676 }
677
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700678 os << std::flush;
679 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700680 }
681
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800682 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700683 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
684 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800685 return 0; // Address not in oat file
686 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800687 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
688 reinterpret_cast<uintptr_t>(oat_file_.Begin());
689 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800690 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800691 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800692 return end_offset - begin_offset;
693 }
694
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800695 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700696 return oat_file_.GetOatHeader().GetInstructionSet();
697 }
698
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700699 const void* GetQuickOatCode(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800700 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700701 const OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700702 CHECK(oat_dex_file != nullptr);
703 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700704 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
705 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700706 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
707 << "': " << error_msg;
708 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800709 const char* descriptor = m->GetDeclaringClassDescriptor();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800710 const dex::ClassDef* class_def =
David Sehr9aa352e2016-09-15 18:13:52 -0700711 OatDexFile::FindClassDef(*dex_file, descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700712 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700713 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100714 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Vladimir Markoe00e2012019-05-14 14:42:02 +0100715 uint32_t oat_method_index;
716 if (m->IsStatic() || m->IsDirect()) {
717 // Simple case where the oat method index was stashed at load time.
718 oat_method_index = m->GetMethodIndex();
719 } else {
720 // Compute the oat_method_index by search for its position in the class def.
721 ClassAccessor accessor(*dex_file, *class_def);
722 oat_method_index = accessor.NumDirectMethods();
723 bool found_virtual = false;
724 for (ClassAccessor::Method dex_method : accessor.GetVirtualMethods()) {
725 // Check method index instead of identity in case of duplicate method definitions.
726 if (dex_method.GetIndex() == m->GetDexMethodIndex()) {
727 found_virtual = true;
728 break;
729 }
730 ++oat_method_index;
731 }
732 CHECK(found_virtual) << "Didn't find oat method index for virtual method: "
733 << dex_file->PrettyMethod(m->GetDexMethodIndex());
734 }
735 return oat_class.GetOatMethod(oat_method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800736 }
737 }
738 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700739 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800740 }
741
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300742 // Returns nullptr and updates error_msg if the Vdex file cannot be opened, otherwise all Dex
743 // files are fully unquickened and stored in dex_files
744 std::unique_ptr<const VdexFile> OpenVdexUnquicken(const std::string& vdex_filename,
745 /* out */ DexFileUniqV* dex_files,
746 /* out */ std::string* error_msg) {
747 std::unique_ptr<const File> file(OS::OpenFileForReading(vdex_filename.c_str()));
748 if (file == nullptr) {
749 *error_msg = "Could not open file " + vdex_filename + " for reading.";
750 return nullptr;
751 }
752
753 int64_t vdex_length = file->GetLength();
754 if (vdex_length == -1) {
755 *error_msg = "Could not read the length of file " + vdex_filename;
756 return nullptr;
757 }
758
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100759 MemMap mmap = MemMap::MapFile(
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300760 file->GetLength(),
761 PROT_READ | PROT_WRITE,
762 MAP_PRIVATE,
763 file->Fd(),
Andreas Gampe9b031f72018-10-04 11:03:34 -0700764 /* start offset= */ 0,
765 /* low_4gb= */ false,
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300766 vdex_filename.c_str(),
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100767 error_msg);
768 if (!mmap.IsValid()) {
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300769 *error_msg = "Failed to mmap file " + vdex_filename + ": " + *error_msg;
770 return nullptr;
771 }
772
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100773 std::unique_ptr<VdexFile> vdex_file(new VdexFile(std::move(mmap)));
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300774 if (!vdex_file->IsValid()) {
775 *error_msg = "Vdex file is not valid";
776 return nullptr;
777 }
778
779 DexFileUniqV tmp_dex_files;
780 if (!vdex_file->OpenAllDexFiles(&tmp_dex_files, error_msg)) {
781 *error_msg = "Failed to open Dex files from Vdex: " + *error_msg;
782 return nullptr;
783 }
784
785 vdex_file->Unquicken(MakeNonOwningPointerVector(tmp_dex_files),
Andreas Gampe9b031f72018-10-04 11:03:34 -0700786 /* decompile_return_instruction= */ true);
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300787
788 *dex_files = std::move(tmp_dex_files);
789 return vdex_file;
790 }
791
David Srbecky86decb62018-06-05 06:41:10 +0100792 bool AddStatsObject(const void* address) {
793 return seen_stats_objects_.insert(address).second; // Inserted new entry.
794 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800795
David Srbecky86decb62018-06-05 06:41:10 +0100796 void DumpStats(VariableIndentationOutputStream& os,
797 const std::string& name,
798 const Stats& stats,
799 double total) {
800 if (std::fabs(stats.Value()) > 0 || !stats.Children().empty()) {
801 double percent = 100.0 * stats.Value() / total;
802 os.Stream()
803 << std::setw(40 - os.GetIndentation()) << std::left << name << std::right << " "
804 << std::setw(8) << stats.Count() << " "
805 << std::setw(12) << std::fixed << std::setprecision(3) << stats.Value() / KB << "KB "
806 << std::setw(8) << std::fixed << std::setprecision(1) << percent << "%\n";
807
808 // Sort all children by largest value first, than by name.
809 std::map<std::pair<double, std::string>, const Stats&> sorted_children;
810 for (const auto& it : stats.Children()) {
811 sorted_children.emplace(std::make_pair(-it.second.Value(), it.first), it.second);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800812 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800813
David Srbecky86decb62018-06-05 06:41:10 +0100814 // Add "other" row to represent any amount not account for by the children.
815 Stats other;
816 other.AddBytes(stats.Value() - stats.SumChildrenValues(), stats.Count());
817 if (std::fabs(other.Value()) > 0 && !stats.Children().empty()) {
818 sorted_children.emplace(std::make_pair(-other.Value(), "(other)"), other);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800819 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800820
David Srbecky86decb62018-06-05 06:41:10 +0100821 // Print the data.
822 ScopedIndentation indent1(&os);
823 for (const auto& it : sorted_children) {
824 DumpStats(os, it.first.second, it.second, total);
825 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800826 }
David Srbecky86decb62018-06-05 06:41:10 +0100827 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800828
Brian Carlstromaded5f72011-10-07 17:15:04 -0700829 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800830 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800831 // We don't know the length of the code for each method, but we need to know where to stop
832 // when disassembling. What we do know is that a region of code will be followed by some other
833 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
834 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800835 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700836 const OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700837 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700838 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700839 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
840 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700841 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
842 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800843 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800844 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800845 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Mathieu Chartier3d092992018-05-24 13:36:23 -0700846 for (ClassAccessor accessor : dex_file->GetClasses()) {
Mathieu Chartier18e26872018-06-04 17:19:02 -0700847 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(accessor.GetClassDefIndex());
Mathieu Chartier3d092992018-05-24 13:36:23 -0700848 for (uint32_t class_method_index = 0;
849 class_method_index < accessor.NumMethods();
850 ++class_method_index) {
851 AddOffsets(oat_class.GetOatMethod(class_method_index));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800852 }
853 }
854 }
855
856 // If the last thing in the file is code for a method, there won't be an offset for the "next"
857 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
858 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800859 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800860 }
861
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700862 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
863 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
864 }
865
Elliott Hughese3c845c2012-02-28 17:23:01 -0800866 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800867 uint32_t code_offset = oat_method.GetCodeOffset();
Vladimir Marko33bff252017-11-01 14:35:42 +0000868 if (oat_file_.GetOatHeader().GetInstructionSet() == InstructionSet::kThumb2) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800869 code_offset &= ~0x1;
870 }
871 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800872 offsets_.insert(oat_method.GetVmapTableOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800873 }
874
Andreas Gampeb40d3612018-06-26 15:49:42 -0700875 bool DumpOatDexFile(std::ostream& os, const OatDexFile& oat_dex_file) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700876 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800877 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700878 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800879 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800880 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700881
Andreas Gampe2ba88952016-04-29 17:52:07 -0700882 const uint8_t* const oat_file_begin = oat_dex_file.GetOatFile()->Begin();
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000883 if (oat_dex_file.GetOatFile()->ContainsDexCode()) {
884 const uint8_t* const vdex_file_begin = oat_dex_file.GetOatFile()->DexBegin();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100885
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000886 // Print data range of the dex file embedded inside the corresponding vdex file.
887 const uint8_t* const dex_file_pointer = oat_dex_file.GetDexFilePointer();
888 uint32_t dex_offset = dchecked_integral_cast<uint32_t>(dex_file_pointer - vdex_file_begin);
889 os << StringPrintf(
890 "dex-file: 0x%08x..0x%08x\n",
891 dex_offset,
892 dchecked_integral_cast<uint32_t>(dex_offset + oat_dex_file.FileSize() - 1));
893 } else {
894 os << StringPrintf("dex-file not in VDEX file\n");
895 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700896
Andreas Gampe2ba88952016-04-29 17:52:07 -0700897 // Create the dex file early. A lot of print-out things depend on it.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700898 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700899 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
900 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700901 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700902 os << std::flush;
903 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700904 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100905
Andreas Gampe2ba88952016-04-29 17:52:07 -0700906 // Print lookup table, if it exists.
907 if (oat_dex_file.GetLookupTableData() != nullptr) {
908 uint32_t table_offset = dchecked_integral_cast<uint32_t>(
909 oat_dex_file.GetLookupTableData() - oat_file_begin);
David Sehr9aa352e2016-09-15 18:13:52 -0700910 uint32_t table_size = TypeLookupTable::RawDataLength(dex_file->NumClassDefs());
Andreas Gampe2ba88952016-04-29 17:52:07 -0700911 os << StringPrintf("type-table: 0x%08x..0x%08x\n",
912 table_offset,
913 table_offset + table_size - 1);
914 }
915
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100916 VariableIndentationOutputStream vios(&os);
917 ScopedIndentation indent1(&vios);
Mathieu Chartier98dad402018-06-04 18:00:12 -0700918 for (ClassAccessor accessor : dex_file->GetClasses()) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800919 // TODO: Support regex
Mathieu Chartier98dad402018-06-04 18:00:12 -0700920 const char* descriptor = accessor.GetDescriptor();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800921 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
922 continue;
923 }
924
Mathieu Chartier98dad402018-06-04 18:00:12 -0700925 const uint16_t class_def_index = accessor.GetClassDefIndex();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700926 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100927 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700928 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
Mathieu Chartier98dad402018-06-04 18:00:12 -0700929 static_cast<ssize_t>(class_def_index),
930 descriptor,
931 oat_class_offset,
932 accessor.GetClassIdx().index_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100933 << " (" << oat_class.GetStatus() << ")"
934 << " (" << oat_class.GetType() << ")\n";
935 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700936 if (options_.list_classes_) {
937 continue;
938 }
Mathieu Chartier98dad402018-06-04 18:00:12 -0700939 if (!DumpOatClass(&vios, oat_class, *dex_file, accessor, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700940 success = false;
941 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800942 if (stop_analysis) {
943 os << std::flush;
944 return success;
945 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700946 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700947 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700948 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700949 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700950 }
951
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300952 // Backwards compatible Dex file export. If dex_file is nullptr (valid Vdex file not present) the
953 // Dex resource is extracted from the oat_dex_file and its checksum is repaired since it's not
954 // unquickened. Otherwise the dex_file has been fully unquickened and is expected to verify the
955 // original checksum.
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300956 bool ExportDexFile(std::ostream& os,
957 const OatDexFile& oat_dex_file,
958 const DexFile* dex_file,
959 bool used_dexlayout) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800960 std::string error_msg;
961 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300962
963 // If dex_file (from unquicken or dexlayout) is not available, the output DexFile size is the
964 // same as the one extracted from the Oat container (pre-oreo)
965 size_t fsize = dex_file == nullptr ? oat_dex_file.FileSize() : dex_file->Size();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800966
967 // Some quick checks just in case
968 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
969 os << "Invalid dex file\n";
970 return false;
971 }
972
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300973 if (dex_file == nullptr) {
974 // Exported bytecode is quickened (dex-to-dex transformations present)
975 dex_file = OpenDexFile(&oat_dex_file, &error_msg);
976 if (dex_file == nullptr) {
977 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
978 return false;
979 }
980
981 // Recompute checksum
982 reinterpret_cast<DexFile::Header*>(const_cast<uint8_t*>(dex_file->Begin()))->checksum_ =
983 dex_file->CalculateChecksum();
984 } else {
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300985 // If dexlayout was used to convert CompactDex back to StandardDex, checksum will be updated
986 // due to `update_checksum_` option, otherwise we expect a reproducible checksum.
987 if (!used_dexlayout) {
988 // Vdex unquicken output should match original input bytecode
989 uint32_t orig_checksum =
990 reinterpret_cast<DexFile::Header*>(const_cast<uint8_t*>(dex_file->Begin()))->checksum_;
991 if (orig_checksum != dex_file->CalculateChecksum()) {
992 os << "Unexpected checksum from unquicken dex file '" << dex_file_location << "'\n";
993 return false;
994 }
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300995 }
996 }
997
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800998 // Verify output directory exists
999 if (!OS::DirectoryExists(options_.export_dex_location_)) {
1000 // TODO: Extend OS::DirectoryExists if symlink support is required
1001 os << options_.export_dex_location_ << " output directory not found or symlink\n";
1002 return false;
1003 }
1004
1005 // Beautify path names
1006 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
1007 return false;
1008 }
1009
1010 std::string dex_orig_name;
1011 size_t dex_orig_pos = dex_file_location.rfind('/');
1012 if (dex_orig_pos == std::string::npos)
1013 dex_orig_name = dex_file_location;
1014 else
1015 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
1016
1017 // A more elegant approach to efficiently name user installed apps is welcome
Andreas Gampef812d8c2017-02-17 10:19:44 -08001018 if (dex_orig_name.size() == 8 &&
1019 dex_orig_name.compare("base.apk") == 0 &&
1020 dex_orig_pos != std::string::npos) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001021 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
1022 size_t apk_orig_pos = dex_file_location.rfind('/');
1023 if (apk_orig_pos != std::string::npos) {
1024 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
1025 }
1026 }
1027
1028 std::string out_dex_path(options_.export_dex_location_);
1029 if (out_dex_path.back() != '/') {
1030 out_dex_path.append("/");
1031 }
1032 out_dex_path.append(dex_orig_name);
1033 out_dex_path.append("_export.dex");
1034 if (out_dex_path.length() > PATH_MAX) {
1035 return false;
1036 }
1037
1038 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
1039 if (file.get() == nullptr) {
1040 os << "Failed to open output dex file " << out_dex_path;
1041 return false;
1042 }
1043
Mathieu Chartier567dc6f2018-04-05 16:37:14 -07001044 bool success = file->WriteFully(dex_file->Begin(), fsize);
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001045 if (!success) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001046 os << "Failed to write dex file";
1047 file->Erase();
1048 return false;
1049 }
1050
1051 if (file->FlushCloseOrErase() != 0) {
1052 os << "Flush and close failed";
1053 return false;
1054 }
1055
1056 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
1057 os << std::flush;
1058
1059 return true;
1060 }
1061
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001062 bool DumpOatClass(VariableIndentationOutputStream* vios,
Mathieu Chartier98dad402018-06-04 18:00:12 -07001063 const OatFile::OatClass& oat_class,
1064 const DexFile& dex_file,
1065 const ClassAccessor& class_accessor,
1066 bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001067 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001068 bool addr_found = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001069 uint32_t class_method_index = 0;
Mathieu Chartier98dad402018-06-04 18:00:12 -07001070 for (const ClassAccessor::Method& method : class_accessor.GetMethods()) {
1071 if (!DumpOatMethod(vios,
1072 dex_file.GetClassDef(class_accessor.GetClassDefIndex()),
1073 class_method_index,
1074 oat_class,
1075 dex_file,
1076 method.GetIndex(),
1077 method.GetCodeItem(),
David Brazdil20c765f2018-10-27 21:45:15 +00001078 method.GetAccessFlags(),
Mathieu Chartier98dad402018-06-04 18:00:12 -07001079 &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001080 success = false;
1081 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001082 if (addr_found) {
1083 *stop_analysis = true;
1084 return success;
1085 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001086 class_method_index++;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001087 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001088 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001089 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001090 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001091
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001092 static constexpr uint32_t kPrologueBytes = 16;
1093
1094 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
1095 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
1096
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001097 bool DumpOatMethod(VariableIndentationOutputStream* vios,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001098 const dex::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -07001099 uint32_t class_method_index,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001100 const OatFile::OatClass& oat_class,
1101 const DexFile& dex_file,
1102 uint32_t dex_method_idx,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001103 const dex::CodeItem* code_item,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001104 uint32_t method_access_flags,
1105 bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001106 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001107
Mathieu Chartier698ebbc2018-01-05 11:00:42 -08001108 CodeItemDataAccessor code_item_accessor(dex_file, code_item);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001109
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001110 // TODO: Support regex
1111 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
1112 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00001113 return success;
1114 }
1115
David Sehr709b0702016-10-13 09:12:37 -07001116 std::string pretty_method = dex_file.PrettyMethod(dex_method_idx, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001117 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
1118 class_method_index, pretty_method.c_str(),
1119 dex_method_idx);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001120 if (options_.list_methods_) {
1121 return success;
1122 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001123
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001124 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
1125 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
1126 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
1127 uint32_t code_offset = oat_method.GetCodeOffset();
1128 uint32_t code_size = oat_method.GetQuickCodeSize();
1129 if (resolved_addr2instr_ != 0) {
1130 if (resolved_addr2instr_ > code_offset + code_size) {
1131 return success;
1132 } else {
1133 *addr_found = true; // stop analyzing file at next iteration
1134 }
1135 }
1136
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001137 // Everything below is indented at least once.
1138 ScopedIndentation indent1(vios);
1139
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001140 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001141 vios->Stream() << "DEX CODE:\n";
1142 ScopedIndentation indent2(vios);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001143 if (code_item_accessor.HasCodeItem()) {
1144 for (const DexInstructionPcPair& inst : code_item_accessor) {
1145 vios->Stream() << StringPrintf("0x%04x: ", inst.DexPc()) << inst->DumpHexLE(5)
1146 << StringPrintf("\t| %s\n", inst->DumpString(&dex_file).c_str());
1147 }
1148 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001149 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001150
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001151 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001152 std::unique_ptr<verifier::MethodVerifier> verifier;
1153 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001154 // We need to have the handle scope stay live until after the verifier since the verifier has
1155 // a handle to the dex cache from hs.
1156 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001157 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
1158 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001159 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001160 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001161 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -07001162 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001163 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001164 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001165 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001166 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001167 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001168 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001169 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001170 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001171 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
1172 oat_method_offsets_offset, oat_file_.Size());
1173 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001174 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001175 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001176 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001177
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001178 ScopedIndentation indent2(vios);
1179 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001180 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
1181 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001182 vios->Stream() << StringPrintf("WARNING: "
1183 "code offset 0x%08x is past end of file 0x%08zx.\n",
1184 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001185 success = false;
1186 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001187 vios->Stream() << "\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001188 }
1189 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001190 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001191 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
1192 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
David Srbecky86decb62018-06-05 06:41:10 +01001193 if (AddStatsObject(method_header)) {
1194 stats_.Child("QuickMethodHeader")->AddBytes(sizeof(*method_header));
1195 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001196 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001197 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001198 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001199 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001200 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001201 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001202 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
1203 method_header_offset, oat_file_.Size());
1204 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001205 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001206 return false;
1207 }
1208
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001209 ScopedIndentation indent2(vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001210 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001211 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001212 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001213 }
Mingyao Yang063fc772016-08-02 11:02:54 -07001214 uint32_t vmap_table_offset = method_header ==
1215 nullptr ? 0 : method_header->GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001216 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001217
1218 size_t vmap_table_offset_limit =
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +00001219 IsMethodGeneratedByDexToDexCompiler(oat_method, code_item_accessor)
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001220 ? oat_file_.GetVdexFile()->Size()
1221 : method_header->GetCode() - oat_file_.Begin();
1222 if (vmap_table_offset >= vmap_table_offset_limit) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001223 vios->Stream() << StringPrintf("WARNING: "
1224 "vmap table offset 0x%08x is past end of file 0x%08zx. "
1225 "vmap table offset was loaded from offset 0x%08x.\n",
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001226 vmap_table_offset,
1227 vmap_table_offset_limit,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001228 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001229 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001230 } else if (options_.dump_vmap_) {
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001231 DumpVmapData(vios, oat_method, code_item_accessor);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001232 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001233 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001234 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001235 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001236
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001237 ScopedIndentation indent2(vios);
1238 vios->Stream()
1239 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
1240 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
1241 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
1242 vios->Stream() << "\n";
1243 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
1244 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
1245 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001246 }
1247 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001248 // Based on spill masks from QuickMethodFrameInfo so placed
1249 // after it is dumped, but useful for understanding quick
1250 // code, so dumped here.
1251 ScopedIndentation indent2(vios);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001252 DumpVregLocations(vios->Stream(), oat_method, code_item_accessor);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001253 }
1254 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001255 vios->Stream() << "CODE: ";
Nicolas Geoffray57083762019-03-05 09:24:45 +00001256 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
1257 if (code_size_offset > oat_file_.Size()) {
1258 ScopedIndentation indent2(vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001259 vios->Stream() << StringPrintf("WARNING: "
Nicolas Geoffray57083762019-03-05 09:24:45 +00001260 "code size offset 0x%08x is past end of file 0x%08zx.",
1261 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001262 success = false;
Nicolas Geoffray57083762019-03-05 09:24:45 +00001263 } else {
1264 const void* code = oat_method.GetQuickCode();
1265 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1266 uint64_t aligned_code_end = aligned_code_begin + code_size;
1267 if (AddStatsObject(code)) {
1268 stats_.Child("Code")->AddBytes(code_size);
1269 }
1270
1271 if (options_.absolute_addresses_) {
1272 vios->Stream() << StringPrintf("%p ", code);
1273 }
1274 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
1275 code_offset,
1276 code_size_offset,
1277 code_size,
1278 code != nullptr ? "..." : "");
1279
1280 ScopedIndentation indent2(vios);
1281 if (aligned_code_begin > oat_file_.Size()) {
1282 vios->Stream() << StringPrintf("WARNING: "
1283 "start of code at 0x%08x is past end of file 0x%08zx.",
1284 aligned_code_begin, oat_file_.Size());
1285 success = false;
1286 } else if (aligned_code_end > oat_file_.Size()) {
1287 vios->Stream() << StringPrintf(
1288 "WARNING: "
1289 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
1290 "code size is 0x%08x loaded from offset 0x%08x.\n",
1291 aligned_code_end, oat_file_.Size(),
1292 code_size, code_size_offset);
1293 success = false;
1294 if (options_.disassemble_code_) {
1295 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
1296 DumpCode(vios, oat_method, code_item_accessor, true, kPrologueBytes);
1297 }
1298 }
1299 } else if (code_size > kMaxCodeSize) {
1300 vios->Stream() << StringPrintf(
1301 "WARNING: "
1302 "code size %d is bigger than max expected threshold of %d. "
1303 "code size is 0x%08x loaded from offset 0x%08x.\n",
1304 code_size, kMaxCodeSize,
1305 code_size, code_size_offset);
1306 success = false;
1307 if (options_.disassemble_code_) {
1308 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
1309 DumpCode(vios, oat_method, code_item_accessor, true, kPrologueBytes);
1310 }
1311 }
1312 } else if (options_.disassemble_code_) {
1313 DumpCode(vios, oat_method, code_item_accessor, !success, 0);
1314 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001315 }
1316 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001317 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001318 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001319 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001320
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001321 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1322 if (spill_mask == 0) {
1323 return;
1324 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001325 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001326 for (size_t i = 0; i < 32; i++) {
1327 if ((spill_mask & (1 << i)) != 0) {
1328 if (is_float) {
1329 os << "fr" << i;
1330 } else {
1331 os << "r" << i;
1332 }
1333 spill_mask ^= 1 << i; // clear bit
1334 if (spill_mask != 0) {
1335 os << ", ";
1336 } else {
1337 break;
1338 }
1339 }
1340 }
1341 os << ")";
1342 }
1343
Roland Levillain442b46a2015-02-18 16:54:21 +00001344 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001345 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001346 const OatFile::OatMethod& oat_method,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001347 const CodeItemDataAccessor& code_item_accessor) {
1348 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item_accessor)) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001349 // The optimizing compiler outputs its CodeInfo data in the vmap table.
David Srbecky6ee06e92018-07-25 21:45:54 +01001350 const uint8_t* raw_code_info = oat_method.GetVmapTable();
Roland Levillain442b46a2015-02-18 16:54:21 +00001351 if (raw_code_info != nullptr) {
1352 CodeInfo code_info(raw_code_info);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001353 DCHECK(code_item_accessor.HasCodeItem());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001354 ScopedIndentation indent1(vios);
David Srbecky8cd54542018-07-15 23:58:44 +01001355 DumpCodeInfo(vios, code_info, oat_method);
Roland Levillain442b46a2015-02-18 16:54:21 +00001356 }
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001357 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item_accessor)) {
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001358 // We don't encode the size in the table, so just emit that we have quickened
1359 // information.
1360 ScopedIndentation indent(vios);
1361 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001362 } else {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001363 // Otherwise, there is nothing to display.
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001364 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001365 }
1366
1367 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001368 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001369 const CodeInfo& code_info,
David Srbecky8cd54542018-07-15 23:58:44 +01001370 const OatFile::OatMethod& oat_method) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001371 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001372 oat_method.GetCodeOffset(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001373 options_.dump_code_info_stack_maps_,
David Srbecky8cd54542018-07-15 23:58:44 +01001374 instruction_set_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001375 }
1376
Andreas Gampe36a296f2017-06-13 14:11:11 -07001377 static int GetOutVROffset(uint16_t out_num, InstructionSet isa) {
1378 // According to stack model, the first out is above the Method referernce.
1379 return static_cast<size_t>(InstructionSetPointerSize(isa)) + out_num * sizeof(uint32_t);
1380 }
1381
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001382 static uint32_t GetVRegOffsetFromQuickCode(const CodeItemDataAccessor& code_item_accessor,
Andreas Gampe36a296f2017-06-13 14:11:11 -07001383 uint32_t core_spills,
1384 uint32_t fp_spills,
1385 size_t frame_size,
1386 int reg,
1387 InstructionSet isa) {
1388 PointerSize pointer_size = InstructionSetPointerSize(isa);
1389 if (kIsDebugBuild) {
1390 auto* runtime = Runtime::Current();
1391 if (runtime != nullptr) {
1392 CHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), pointer_size);
1393 }
1394 }
1395 DCHECK_ALIGNED(frame_size, kStackAlignment);
1396 DCHECK_NE(reg, -1);
1397 int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa)
1398 + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa)
1399 + sizeof(uint32_t); // Filler.
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001400 int num_regs = code_item_accessor.RegistersSize() - code_item_accessor.InsSize();
1401 int temp_threshold = code_item_accessor.RegistersSize();
Andreas Gampe36a296f2017-06-13 14:11:11 -07001402 const int max_num_special_temps = 1;
1403 if (reg == temp_threshold) {
1404 // The current method pointer corresponds to special location on stack.
1405 return 0;
1406 } else if (reg >= temp_threshold + max_num_special_temps) {
1407 /*
1408 * Special temporaries may have custom locations and the logic above deals with that.
1409 * However, non-special temporaries are placed relative to the outs.
1410 */
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001411 int temps_start = code_item_accessor.OutsSize() * sizeof(uint32_t)
Andreas Gampe36a296f2017-06-13 14:11:11 -07001412 + static_cast<size_t>(pointer_size) /* art method */;
1413 int relative_offset = (reg - (temp_threshold + max_num_special_temps)) * sizeof(uint32_t);
1414 return temps_start + relative_offset;
1415 } else if (reg < num_regs) {
1416 int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t);
1417 return locals_start + (reg * sizeof(uint32_t));
1418 } else {
1419 // Handle ins.
1420 return frame_size + ((reg - num_regs) * sizeof(uint32_t))
1421 + static_cast<size_t>(pointer_size) /* art method */;
1422 }
1423 }
1424
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001425 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001426 const CodeItemDataAccessor& code_item_accessor) {
1427 if (code_item_accessor.HasCodeItem()) {
1428 size_t num_locals_ins = code_item_accessor.RegistersSize();
1429 size_t num_ins = code_item_accessor.InsSize();
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001430 size_t num_locals = num_locals_ins - num_ins;
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001431 size_t num_outs = code_item_accessor.OutsSize();
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001432
1433 os << "vr_stack_locations:";
1434 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1435 // For readability, delimit the different kinds of VRs.
1436 if (reg == num_locals_ins) {
1437 os << "\n\tmethod*:";
1438 } else if (reg == num_locals && num_ins > 0) {
1439 os << "\n\tins:";
1440 } else if (reg == 0 && num_locals > 0) {
1441 os << "\n\tlocals:";
1442 }
1443
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001444 uint32_t offset = GetVRegOffsetFromQuickCode(code_item_accessor,
Andreas Gampe36a296f2017-06-13 14:11:11 -07001445 oat_method.GetCoreSpillMask(),
1446 oat_method.GetFpSpillMask(),
1447 oat_method.GetFrameSizeInBytes(),
1448 reg,
1449 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001450 os << " v" << reg << "[sp + #" << offset << "]";
1451 }
1452
1453 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1454 if (out_reg == 0) {
1455 os << "\n\touts:";
1456 }
1457
Andreas Gampe36a296f2017-06-13 14:11:11 -07001458 uint32_t offset = GetOutVROffset(out_reg, GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001459 os << " v" << out_reg << "[sp + #" << offset << "]";
1460 }
1461
1462 os << "\n";
1463 }
1464 }
1465
Roland Levillainf2650d12015-05-28 14:53:28 +01001466 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1467 // the optimizing compiler?
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001468 static bool IsMethodGeneratedByOptimizingCompiler(
1469 const OatFile::OatMethod& oat_method,
1470 const CodeItemDataAccessor& code_item_accessor) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001471 // If the native GC map is null and the Dex `code_item` is not
1472 // null, then this method has been compiled with the optimizing
1473 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001474 return oat_method.GetQuickCode() != nullptr &&
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001475 oat_method.GetVmapTable() != nullptr &&
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001476 code_item_accessor.HasCodeItem();
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001477 }
1478
1479 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1480 // the dextodex compiler?
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001481 static bool IsMethodGeneratedByDexToDexCompiler(
1482 const OatFile::OatMethod& oat_method,
1483 const CodeItemDataAccessor& code_item_accessor) {
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001484 // If the quick code is null, the Dex `code_item` is not
1485 // null, and the vmap table is not null, then this method has been compiled
1486 // with the dextodex compiler.
1487 return oat_method.GetQuickCode() == nullptr &&
1488 oat_method.GetVmapTable() != nullptr &&
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001489 code_item_accessor.HasCodeItem();
Roland Levillainf2650d12015-05-28 14:53:28 +01001490 }
1491
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001492 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001493 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001494 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001495 const DexFile* dex_file,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001496 const dex::ClassDef& class_def,
1497 const dex::CodeItem* code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001498 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001499 if ((method_access_flags & kAccNative) == 0) {
1500 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001501 Runtime* const runtime = Runtime::Current();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001502 DCHECK(options_.class_loader_ != nullptr);
Vladimir Marko421087b2018-02-27 11:00:17 +00001503 Handle<mirror::DexCache> dex_cache = hs->NewHandle(
1504 runtime->GetClassLinker()->RegisterDexFile(*dex_file, options_.class_loader_->Get()));
1505 CHECK(dex_cache != nullptr);
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00001506 ArtMethod* method = runtime->GetClassLinker()->ResolveMethodWithoutInvokeType(
1507 dex_method_idx, dex_cache, *options_.class_loader_);
Andreas Gampe03da7842018-04-12 11:12:52 -07001508 if (method == nullptr) {
1509 soa.Self()->ClearException();
1510 return nullptr;
1511 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001512 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001513 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
Andreas Gampe9b031f72018-10-04 11:03:34 -07001514 class_def, code_item, method, method_access_flags, /* api_level= */ 0);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001515 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001516
1517 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001518 }
1519
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001520 // The StackMapsHelper provides the stack maps in the native PC order.
1521 // For identical native PCs, the order from the CodeInfo is preserved.
1522 class StackMapsHelper {
1523 public:
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001524 explicit StackMapsHelper(const uint8_t* raw_code_info, InstructionSet instruction_set)
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001525 : code_info_(raw_code_info),
David Srbecky052f8ca2018-04-26 15:42:54 +01001526 number_of_stack_maps_(code_info_.GetNumberOfStackMaps()),
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001527 indexes_(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001528 offset_(static_cast<uint32_t>(-1)),
1529 stack_map_index_(0u),
1530 instruction_set_(instruction_set) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001531 if (number_of_stack_maps_ != 0u) {
1532 // Check if native PCs are ordered.
1533 bool ordered = true;
David Srbecky052f8ca2018-04-26 15:42:54 +01001534 StackMap last = code_info_.GetStackMapAt(0u);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001535 for (size_t i = 1; i != number_of_stack_maps_; ++i) {
David Srbecky052f8ca2018-04-26 15:42:54 +01001536 StackMap current = code_info_.GetStackMapAt(i);
1537 if (last.GetNativePcOffset(instruction_set) >
1538 current.GetNativePcOffset(instruction_set)) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001539 ordered = false;
1540 break;
1541 }
1542 last = current;
1543 }
1544 if (!ordered) {
1545 // Create indirection indexes for access in native PC order. We do not optimize
1546 // for the fact that there can currently be only two separately ordered ranges,
1547 // namely normal stack maps and catch-point stack maps.
1548 indexes_.resize(number_of_stack_maps_);
1549 std::iota(indexes_.begin(), indexes_.end(), 0u);
1550 std::sort(indexes_.begin(),
1551 indexes_.end(),
1552 [this](size_t lhs, size_t rhs) {
David Srbecky052f8ca2018-04-26 15:42:54 +01001553 StackMap left = code_info_.GetStackMapAt(lhs);
1554 uint32_t left_pc = left.GetNativePcOffset(instruction_set_);
1555 StackMap right = code_info_.GetStackMapAt(rhs);
1556 uint32_t right_pc = right.GetNativePcOffset(instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001557 // If the PCs are the same, compare indexes to preserve the original order.
1558 return (left_pc < right_pc) || (left_pc == right_pc && lhs < rhs);
1559 });
1560 }
David Srbecky052f8ca2018-04-26 15:42:54 +01001561 offset_ = GetStackMapAt(0).GetNativePcOffset(instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001562 }
1563 }
1564
1565 const CodeInfo& GetCodeInfo() const {
1566 return code_info_;
1567 }
1568
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001569 uint32_t GetOffset() const {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001570 return offset_;
1571 }
1572
1573 StackMap GetStackMap() const {
1574 return GetStackMapAt(stack_map_index_);
1575 }
1576
1577 void Next() {
1578 ++stack_map_index_;
1579 offset_ = (stack_map_index_ == number_of_stack_maps_)
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001580 ? static_cast<uint32_t>(-1)
David Srbecky052f8ca2018-04-26 15:42:54 +01001581 : GetStackMapAt(stack_map_index_).GetNativePcOffset(instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001582 }
1583
1584 private:
1585 StackMap GetStackMapAt(size_t i) const {
1586 if (!indexes_.empty()) {
1587 i = indexes_[i];
1588 }
1589 DCHECK_LT(i, number_of_stack_maps_);
David Srbecky052f8ca2018-04-26 15:42:54 +01001590 return code_info_.GetStackMapAt(i);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001591 }
1592
1593 const CodeInfo code_info_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001594 const size_t number_of_stack_maps_;
1595 dchecked_vector<size_t> indexes_; // Used if stack map native PCs are not ordered.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001596 uint32_t offset_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001597 size_t stack_map_index_;
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001598 const InstructionSet instruction_set_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001599 };
1600
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001601 void DumpCode(VariableIndentationOutputStream* vios,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001602 const OatFile::OatMethod& oat_method,
1603 const CodeItemDataAccessor& code_item_accessor,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001604 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001605 const void* quick_code = oat_method.GetQuickCode();
1606
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001607 if (code_size == 0) {
1608 code_size = oat_method.GetQuickCodeSize();
1609 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001610 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001611 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001612 return;
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001613 } else if (!bad_input && IsMethodGeneratedByOptimizingCompiler(oat_method,
1614 code_item_accessor)) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001615 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001616 StackMapsHelper helper(oat_method.GetVmapTable(), instruction_set_);
David Srbecky86decb62018-06-05 06:41:10 +01001617 if (AddStatsObject(oat_method.GetVmapTable())) {
David Srbecky42deda82018-08-10 11:23:27 +01001618 helper.GetCodeInfo().CollectSizeStats(oat_method.GetVmapTable(), &stats_);
David Srbecky86decb62018-06-05 06:41:10 +01001619 }
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001620 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1621 size_t offset = 0;
1622 while (offset < code_size) {
1623 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
1624 if (offset == helper.GetOffset()) {
1625 ScopedIndentation indent1(vios);
1626 StackMap stack_map = helper.GetStackMap();
1627 DCHECK(stack_map.IsValid());
1628 stack_map.Dump(vios,
1629 helper.GetCodeInfo(),
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001630 oat_method.GetCodeOffset(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001631 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001632 do {
1633 helper.Next();
1634 // There may be multiple stack maps at a given PC. We display only the first one.
1635 } while (offset == helper.GetOffset());
1636 }
1637 DCHECK_LT(offset, helper.GetOffset());
1638 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001639 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001640 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1641 size_t offset = 0;
1642 while (offset < code_size) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001643 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001644 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001645 }
1646 }
1647
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001648 std::pair<const uint8_t*, const uint8_t*> GetBootImageLiveObjectsDataRange(gc::Heap* heap) const
1649 REQUIRES_SHARED(Locks::mutator_lock_) {
1650 const std::vector<gc::space::ImageSpace*>& boot_image_spaces = heap->GetBootImageSpaces();
1651 const ImageHeader& main_header = boot_image_spaces[0]->GetImageHeader();
1652 ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_live_objects =
1653 ObjPtr<mirror::ObjectArray<mirror::Object>>::DownCast(
1654 main_header.GetImageRoot<kWithoutReadBarrier>(ImageHeader::kBootImageLiveObjects));
1655 DCHECK(boot_image_live_objects != nullptr);
1656 DCHECK(heap->ObjectIsInBootImageSpace(boot_image_live_objects));
1657 const uint8_t* boot_image_live_objects_address =
1658 reinterpret_cast<const uint8_t*>(boot_image_live_objects.Ptr());
1659 uint32_t begin_offset = mirror::ObjectArray<mirror::Object>::OffsetOfElement(0).Uint32Value();
1660 uint32_t end_offset = mirror::ObjectArray<mirror::Object>::OffsetOfElement(
1661 boot_image_live_objects->GetLength()).Uint32Value();
1662 return std::make_pair(boot_image_live_objects_address + begin_offset,
1663 boot_image_live_objects_address + end_offset);
1664 }
1665
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001666 void DumpDataBimgRelRoEntries(std::ostream& os) {
1667 os << ".data.bimg.rel.ro: ";
1668 if (oat_file_.GetBootImageRelocations().empty()) {
1669 os << "empty.\n\n";
1670 return;
1671 }
1672
1673 os << oat_file_.GetBootImageRelocations().size() << " entries.\n";
1674 Runtime* runtime = Runtime::Current();
1675 if (runtime != nullptr && !runtime->GetHeap()->GetBootImageSpaces().empty()) {
1676 const std::vector<gc::space::ImageSpace*>& boot_image_spaces =
1677 runtime->GetHeap()->GetBootImageSpaces();
1678 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001679 auto live_objects = GetBootImageLiveObjectsDataRange(runtime->GetHeap());
1680 const uint8_t* live_objects_begin = live_objects.first;
1681 const uint8_t* live_objects_end = live_objects.second;
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001682 for (const uint32_t& object_offset : oat_file_.GetBootImageRelocations()) {
1683 uint32_t entry_index = &object_offset - oat_file_.GetBootImageRelocations().data();
1684 uint32_t entry_offset = entry_index * sizeof(oat_file_.GetBootImageRelocations()[0]);
1685 os << StringPrintf(" 0x%x: 0x%08x", entry_offset, object_offset);
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001686 uint8_t* address = boot_image_spaces[0]->Begin() + object_offset;
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001687 bool found = false;
1688 for (gc::space::ImageSpace* space : boot_image_spaces) {
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001689 uint64_t local_offset = address - space->Begin();
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001690 if (local_offset < space->GetImageHeader().GetImageSize()) {
1691 if (space->GetImageHeader().GetObjectsSection().Contains(local_offset)) {
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001692 if (address >= live_objects_begin && address < live_objects_end) {
1693 size_t index =
1694 (address - live_objects_begin) / sizeof(mirror::HeapReference<mirror::Object>);
1695 os << StringPrintf(" 0x%08x BootImageLiveObject[%zu]",
Lokesh Gidra44044b12019-02-26 00:10:04 +00001696 object_offset,
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001697 index);
1698 } else {
1699 ObjPtr<mirror::Object> o = reinterpret_cast<mirror::Object*>(address);
1700 if (o->IsString()) {
1701 os << " String: " << o->AsString()->ToModifiedUtf8();
1702 } else if (o->IsClass()) {
1703 os << " Class: " << o->AsClass()->PrettyDescriptor();
1704 } else {
1705 os << StringPrintf(" 0x%08x %s",
1706 object_offset,
1707 o->GetClass()->PrettyDescriptor().c_str());
1708 }
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001709 }
1710 } else if (space->GetImageHeader().GetMethodsSection().Contains(local_offset)) {
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001711 ArtMethod* m = reinterpret_cast<ArtMethod*>(address);
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001712 os << " ArtMethod: " << m->PrettyMethod();
1713 } else {
1714 os << StringPrintf(" 0x%08x <unexpected section in %s>",
1715 object_offset,
1716 space->GetImageFilename().c_str());
1717 }
1718 found = true;
1719 break;
1720 }
1721 }
1722 if (!found) {
1723 os << StringPrintf(" 0x%08x <outside boot image spaces>", object_offset);
1724 }
1725 os << "\n";
1726 }
1727 } else {
1728 for (const uint32_t& object_offset : oat_file_.GetBootImageRelocations()) {
1729 uint32_t entry_index = &object_offset - oat_file_.GetBootImageRelocations().data();
1730 uint32_t entry_offset = entry_index * sizeof(oat_file_.GetBootImageRelocations()[0]);
1731 os << StringPrintf(" 0x%x: 0x%08x\n", entry_offset, object_offset);
1732 }
1733 }
1734 os << "\n";
1735 }
1736
Vladimir Markof3c52b42017-11-17 17:32:12 +00001737 template <typename NameGetter>
1738 void DumpBssEntries(std::ostream& os,
1739 const char* slot_type,
1740 const IndexBssMapping* mapping,
1741 uint32_t number_of_indexes,
1742 size_t slot_size,
1743 NameGetter name) {
1744 os << ".bss mapping for " << slot_type << ": ";
1745 if (mapping == nullptr) {
1746 os << "empty.\n";
1747 return;
1748 }
1749 size_t index_bits = IndexBssMappingEntry::IndexBits(number_of_indexes);
1750 size_t num_valid_indexes = 0u;
1751 for (const IndexBssMappingEntry& entry : *mapping) {
1752 num_valid_indexes += 1u + POPCOUNT(entry.GetMask(index_bits));
1753 }
1754 os << mapping->size() << " entries for " << num_valid_indexes << " valid indexes.\n";
1755 os << std::hex;
1756 for (const IndexBssMappingEntry& entry : *mapping) {
1757 uint32_t index = entry.GetIndex(index_bits);
1758 uint32_t mask = entry.GetMask(index_bits);
1759 size_t bss_offset = entry.bss_offset - POPCOUNT(mask) * slot_size;
1760 for (uint32_t n : LowToHighBits(mask)) {
1761 size_t current_index = index - (32u - index_bits) + n;
1762 os << " 0x" << bss_offset << ": " << slot_type << ": " << name(current_index) << "\n";
1763 bss_offset += slot_size;
1764 }
1765 DCHECK_EQ(bss_offset, entry.bss_offset);
1766 os << " 0x" << bss_offset << ": " << slot_type << ": " << name(index) << "\n";
1767 }
1768 os << std::dec;
1769 }
1770
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001771 const OatFile& oat_file_;
Andreas Gampeb40d3612018-06-26 15:49:42 -07001772 const std::vector<const OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001773 const OatDumperOptions& options_;
1774 uint32_t resolved_addr2instr_;
Andreas Gampe372f3a32016-08-19 10:49:06 -07001775 const InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001776 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001777 Disassembler* disassembler_;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001778 Stats stats_;
David Srbecky86decb62018-06-05 06:41:10 +01001779 std::unordered_set<const void*> seen_stats_objects_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001780};
1781
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001782class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001783 public:
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001784 ImageDumper(std::ostream* os,
1785 gc::space::ImageSpace& image_space,
1786 const ImageHeader& image_header,
1787 OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001788 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001789 vios_(os),
1790 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001791 image_space_(image_space),
1792 image_header_(image_header),
1793 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001794
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001795 bool Dump() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001796 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001797 std::ostream& indent_os = vios_.Stream();
1798
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001799 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001800
Jeff Haodcdc85b2015-12-04 14:06:18 -08001801 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1802
Vladimir Markoc10a0c62018-11-16 11:39:22 +00001803 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n";
1804 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n";
1805 os << "IMAGE CHECKSUM: " << std::hex << image_header_.GetImageChecksum() << std::dec << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001806
Vladimir Markoc10a0c62018-11-16 11:39:22 +00001807 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum()) << "\n";
1808 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n";
1809 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n";
1810 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n";
1811 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
1812
1813 os << "BOOT IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetBootImageBegin())
1814 << "\n";
Vladimir Marko0c78ef72018-11-21 14:09:35 +00001815 os << "BOOT IMAGE SIZE: " << image_header_.GetBootImageSize() << "\n\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001816
1817 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1818 auto section = static_cast<ImageHeader::ImageSections>(i);
1819 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1820 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001821
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001822 {
Vladimir Markoc13fbd82018-06-04 16:16:28 +01001823 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots().Ptr()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001824 static_assert(arraysize(image_roots_descriptions_) ==
1825 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001826 DCHECK_LE(image_header_.GetImageRoots()->GetLength(), ImageHeader::kImageRootsMax);
1827 for (int32_t i = 0, size = image_header_.GetImageRoots()->GetLength(); i != size; ++i) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001828 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1829 const char* image_root_description = image_roots_descriptions_[i];
Vladimir Markoc13fbd82018-06-04 16:16:28 +01001830 ObjPtr<mirror::Object> image_root_object = image_header_.GetImageRoot(image_root);
1831 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object.Ptr());
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001832 if (image_root_object != nullptr && image_root_object->IsObjectArray()) {
Vladimir Markoc13fbd82018-06-04 16:16:28 +01001833 ObjPtr<mirror::ObjectArray<mirror::Object>> image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001834 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001835 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001836 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
Vladimir Marko423bebb2019-03-26 15:17:21 +00001837 ObjPtr<mirror::Object> value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001838 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001839 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1840 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001841 run++;
1842 } else {
1843 break;
1844 }
1845 }
1846 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001847 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001848 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001849 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001850 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001851 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001852 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001853 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001854 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001855 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001856 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001857 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001858 }
1859 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001860 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001861
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001862 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001863 os << "METHOD ROOTS\n";
1864 static_assert(arraysize(image_methods_descriptions_) ==
1865 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1866 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1867 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1868 const char* description = image_methods_descriptions_[i];
1869 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001870 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001871 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001872 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001873 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001874
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001875 Runtime* const runtime = Runtime::Current();
1876 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001877 std::string image_filename = image_space_.GetImageFilename();
1878 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001879 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001880 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001881 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001882 const OatFile* oat_file = image_space_.GetOatFile();
Alex Lighta59dd802014-07-02 16:28:08 -07001883 if (oat_file == nullptr) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001884 oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location);
1885 }
1886 if (oat_file == nullptr) {
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001887 oat_file = OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01001888 oat_location,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001889 oat_location,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001890 /*executable=*/ false,
1891 /*low_4gb=*/ false,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001892 &error_msg);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001893 }
1894 if (oat_file == nullptr) {
1895 os << "OAT FILE NOT FOUND: " << error_msg << "\n";
1896 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001897 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001898 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001899
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001900 stats_.oat_file_bytes = oat_file->Size();
1901
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001902 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001903
Andreas Gampeb40d3612018-06-26 15:49:42 -07001904 for (const OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001905 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001906 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1907 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001908 }
1909
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001910 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001911
Jeff Haodcdc85b2015-12-04 14:06:18 -08001912 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001913 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001914 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001915 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001916 {
1917 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1918 heap->FlushAllocStack();
1919 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001920 // Since FlushAllocStack() above resets the (active) allocation
1921 // stack. Need to revoke the thread-local allocation stacks that
1922 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001923 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001924 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001925 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001926 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001927 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001928 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001929 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001930 {
Andreas Gampecc1b5352016-12-01 16:58:38 -08001931 ReaderMutexLock mu(self, *Locks::dex_lock_);
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001932 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001933 ObjPtr<mirror::DexCache> dex_cache =
1934 ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001935 if (dex_cache != nullptr) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001936 dex_caches_.insert(dex_cache.Ptr());
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001937 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001938 }
1939 }
Andreas Gampe0c183382017-07-13 22:26:24 -07001940 auto dump_visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
1941 DumpObject(obj);
1942 };
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001943 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001944 // Dump the normal objects before ArtMethods.
Andreas Gampe0c183382017-07-13 22:26:24 -07001945 image_space_.GetLiveBitmap()->Walk(dump_visitor);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001946 indent_os << "\n";
1947 // TODO: Dump fields.
1948 // Dump methods after.
Mathieu Chartier9d5956a2019-03-22 11:29:08 -07001949 image_header_.VisitPackedArtMethods([&](ArtMethod& method)
1950 REQUIRES_SHARED(Locks::mutator_lock_) {
1951 std::ostream& indent_os = vios_.Stream();
1952 indent_os << &method << " " << " ArtMethod: " << method.PrettyMethod() << "\n";
1953 DumpMethod(&method, indent_os);
1954 indent_os << "\n";
1955 }, image_space_.Begin(), image_header_.GetPointerSize());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001956 // Dump the large objects separately.
Andreas Gampe0c183382017-07-13 22:26:24 -07001957 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(dump_visitor);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001958 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001959 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001960 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001961 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001962 size_t data_size = image_header_.GetDataSize(); // stored size in file.
1963 if (file == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001964 LOG(WARNING) << "Failed to find image in " << image_filename;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001965 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001966 stats_.file_bytes = file->GetLength();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001967 // If the image is compressed, adjust to decompressed size.
1968 size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader);
Mathieu Chartiera5c01d72019-07-26 14:39:28 -07001969 if (!image_header_.HasCompressedBlock()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001970 DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image";
1971 }
1972 stats_.file_bytes += uncompressed_size - data_size;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001973 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001974 size_t header_bytes = sizeof(ImageHeader);
Vladimir Markocd87c3e2017-09-05 13:11:57 +01001975 const auto& object_section = image_header_.GetObjectsSection();
1976 const auto& field_section = image_header_.GetFieldsSection();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001977 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Markocd87c3e2017-09-05 13:11:57 +01001978 const auto& dex_cache_arrays_section = image_header_.GetDexCacheArraysSection();
1979 const auto& intern_section = image_header_.GetInternedStringsSection();
1980 const auto& class_table_section = image_header_.GetClassTableSection();
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07001981 const auto& sro_section = image_header_.GetImageStringReferenceOffsetsSection();
1982 const auto& metadata_section = image_header_.GetMetadataSection();
Vladimir Markocd87c3e2017-09-05 13:11:57 +01001983 const auto& bitmap_section = image_header_.GetImageBitmapSection();
Andreas Gampeace0dc12016-01-20 13:33:13 -08001984
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001985 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001986
1987 // Objects are kObjectAlignment-aligned.
1988 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1989 if (object_section.Offset() > header_bytes) {
1990 stats_.alignment_bytes += object_section.Offset() - header_bytes;
1991 }
1992
1993 // Field section is 4-byte aligned.
1994 constexpr size_t kFieldSectionAlignment = 4U;
1995 uint32_t end_objects = object_section.Offset() + object_section.Size();
1996 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
1997 stats_.alignment_bytes += field_section.Offset() - end_objects;
1998
1999 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
2000 uint32_t end_fields = field_section.Offset() + field_section.Size();
2001 CHECK_ALIGNED(method_section.Offset(), 4);
2002 stats_.alignment_bytes += method_section.Offset() - end_fields;
2003
2004 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
2005 uint32_t end_methods = method_section.Offset() + method_section.Size();
2006 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
2007 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
2008
2009 // Intern table is 8-byte aligned.
2010 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
Vladimir Markoe47f60c2018-02-21 13:43:28 +00002011 CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
Andreas Gampeace0dc12016-01-20 13:33:13 -08002012 stats_.alignment_bytes += intern_section.Offset() - end_caches;
2013
2014 // Add space between intern table and class table.
2015 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
2016 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
2017
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002018 // Add space between end of image data and bitmap. Expect the bitmap to be page-aligned.
2019 const size_t bitmap_offset = sizeof(ImageHeader) + data_size;
Andreas Gampeace0dc12016-01-20 13:33:13 -08002020 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002021 stats_.alignment_bytes += RoundUp(bitmap_offset, kPageSize) - bitmap_offset;
Andreas Gampeace0dc12016-01-20 13:33:13 -08002022
Mathieu Chartiere401d142015-04-22 13:56:20 -07002023 stats_.bitmap_bytes += bitmap_section.Size();
2024 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01002025 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01002026 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002027 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002028 stats_.class_table_bytes += class_table_section.Size();
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002029 stats_.sro_offset_bytes += sro_section.Size();
2030 stats_.metadata_bytes += metadata_section.Size();
2031
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002032 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002033 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002034
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002035 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002036
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002037 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002038 }
2039
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002040 private:
Mathieu Chartier3398c782016-09-30 10:27:43 -07002041 static void PrettyObjectValue(std::ostream& os,
2042 ObjPtr<mirror::Class> type,
2043 ObjPtr<mirror::Object> value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002044 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002045 CHECK(type != nullptr);
2046 if (value == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07002047 os << StringPrintf("null %s\n", type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002048 } else if (type->IsStringClass()) {
Vladimir Marko4617d582019-03-28 13:48:31 +00002049 ObjPtr<mirror::String> string = value->AsString();
2050 os << StringPrintf("%p String: %s\n",
2051 string.Ptr(),
Ian Rogers68b56852014-08-29 20:19:11 -07002052 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07002053 } else if (type->IsClassClass()) {
Vladimir Marko4617d582019-03-28 13:48:31 +00002054 ObjPtr<mirror::Class> klass = value->AsClass();
2055 os << StringPrintf("%p Class: %s\n",
2056 klass.Ptr(),
2057 mirror::Class::PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002058 } else {
David Sehr709b0702016-10-13 09:12:37 -07002059 os << StringPrintf("%p %s\n", value.Ptr(), type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002060 }
2061 }
2062
Mathieu Chartier3398c782016-09-30 10:27:43 -07002063 static void PrintField(std::ostream& os, ArtField* field, ObjPtr<mirror::Object> obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002064 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07002065 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08002066 switch (field->GetTypeAsPrimitiveType()) {
2067 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08002068 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002069 break;
2070 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002071 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002072 break;
2073 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002074 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002075 break;
2076 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002077 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002078 break;
2079 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07002080 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002081 break;
2082 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07002083 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002084 break;
2085 case Primitive::kPrimBoolean:
Roland Levillain5e8d5f02016-10-18 18:03:43 +01002086 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj) ? "true" : "false",
Fred Shih37f05ef2014-07-16 18:38:08 -07002087 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002088 break;
2089 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07002090 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002091 break;
2092 case Primitive::kPrimNot: {
2093 // Get the value, don't compute the type unless it is non-null as we don't want
2094 // to cause class loading.
Mathieu Chartier3398c782016-09-30 10:27:43 -07002095 ObjPtr<mirror::Object> value = field->GetObj(obj);
Ian Rogers08f1f502014-12-02 15:04:37 -08002096 if (value == nullptr) {
2097 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07002098 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08002099 // Grab the field type without causing resolution.
Vladimir Marko208f6702017-12-08 12:00:50 +00002100 ObjPtr<mirror::Class> field_type = field->LookupResolvedType();
Ian Rogers08f1f502014-12-02 15:04:37 -08002101 if (field_type != nullptr) {
2102 PrettyObjectValue(os, field_type, value);
2103 } else {
Mathieu Chartier3398c782016-09-30 10:27:43 -07002104 os << StringPrintf("%p %s\n",
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07002105 value.Ptr(),
Ian Rogers08f1f502014-12-02 15:04:37 -08002106 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
2107 }
Ian Rogers50239c72013-06-17 14:53:22 -07002108 }
Ian Rogers08f1f502014-12-02 15:04:37 -08002109 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07002110 }
Ian Rogers08f1f502014-12-02 15:04:37 -08002111 default:
2112 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
2113 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08002114 }
2115 }
2116
Andreas Gampe98104992018-10-16 12:49:47 -07002117 static void DumpFields(std::ostream& os, mirror::Object* obj, ObjPtr<mirror::Class> klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002118 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe98104992018-10-16 12:49:47 -07002119 ObjPtr<mirror::Class> super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002120 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002121 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08002122 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002123 for (ArtField& field : klass->GetIFields()) {
2124 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08002125 }
2126 }
2127
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002128 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002129 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002130 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002131
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002132 const void* GetQuickOatCodeBegin(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08002133 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002134 image_header_.GetPointerSize());
David Srbeckya54e0cc2019-04-15 15:03:26 +01002135 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2136 if (class_linker->IsQuickResolutionStub(quick_code) ||
2137 class_linker->IsQuickToInterpreterBridge(quick_code) ||
2138 class_linker->IsQuickGenericJniStub(quick_code) ||
2139 class_linker->IsJniDlsymLookupStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002140 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002141 }
Vladimir Marko33bff252017-11-01 14:35:42 +00002142 if (oat_dumper_->GetInstructionSet() == InstructionSet::kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002143 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002144 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002145 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002146 }
2147
Mathieu Chartiere401d142015-04-22 13:56:20 -07002148 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002149 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002150 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
2151 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002152 return 0;
2153 }
David Srbeckya54e0cc2019-04-15 15:03:26 +01002154 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2155 reinterpret_cast<uintptr_t>(oat_code_begin) - sizeof(OatQuickMethodHeader));
2156 return method_header->GetCodeSize();
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002157 }
2158
Mathieu Chartiere401d142015-04-22 13:56:20 -07002159 const void* GetQuickOatCodeEnd(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002160 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002161 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002162 if (oat_code_begin == nullptr) {
2163 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002164 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002165 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002166 }
2167
Andreas Gampe0c183382017-07-13 22:26:24 -07002168 void DumpObject(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002169 DCHECK(obj != nullptr);
Andreas Gampe0c183382017-07-13 22:26:24 -07002170 if (!InDumpSpace(obj)) {
Brian Carlstrom78128a62011-09-15 17:21:19 -07002171 return;
2172 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002173
2174 size_t object_bytes = obj->SizeOf();
2175 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
Andreas Gampe0c183382017-07-13 22:26:24 -07002176 stats_.object_bytes += object_bytes;
2177 stats_.alignment_bytes += alignment_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002178
Andreas Gampe0c183382017-07-13 22:26:24 -07002179 std::ostream& os = vios_.Stream();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002180
Vladimir Marko4617d582019-03-28 13:48:31 +00002181 ObjPtr<mirror::Class> obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08002182 if (obj_class->IsArrayClass()) {
David Sehr709b0702016-10-13 09:12:37 -07002183 os << StringPrintf("%p: %s length:%d\n", obj, obj_class->PrettyDescriptor().c_str(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002184 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08002185 } else if (obj->IsClass()) {
Vladimir Marko4617d582019-03-28 13:48:31 +00002186 ObjPtr<mirror::Class> klass = obj->AsClass();
2187 os << StringPrintf("%p: java.lang.Class \"%s\" (",
2188 obj,
David Sehr709b0702016-10-13 09:12:37 -07002189 mirror::Class::PrettyDescriptor(klass).c_str())
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002190 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08002191 } else if (obj_class->IsStringClass()) {
Vladimir Marko4617d582019-03-28 13:48:31 +00002192 os << StringPrintf("%p: java.lang.String %s\n",
2193 obj,
Ian Rogers68b56852014-08-29 20:19:11 -07002194 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002195 } else {
David Sehr709b0702016-10-13 09:12:37 -07002196 os << StringPrintf("%p: %s\n", obj, obj_class->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002197 }
Andreas Gampe0c183382017-07-13 22:26:24 -07002198 ScopedIndentation indent1(&vios_);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002199 DumpFields(os, obj, obj_class);
Andreas Gampe0c183382017-07-13 22:26:24 -07002200 const PointerSize image_pointer_size = image_header_.GetPointerSize();
Ian Rogersd5b32602012-02-26 16:40:04 -08002201 if (obj->IsObjectArray()) {
Vladimir Marko4617d582019-03-28 13:48:31 +00002202 ObjPtr<mirror::ObjectArray<mirror::Object>> obj_array = obj->AsObjectArray<mirror::Object>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002203 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Vladimir Marko423bebb2019-03-26 15:17:21 +00002204 ObjPtr<mirror::Object> value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002205 size_t run = 0;
2206 for (int32_t j = i + 1; j < length; j++) {
2207 if (value == obj_array->Get(j)) {
2208 run++;
2209 } else {
2210 break;
2211 }
2212 }
2213 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002214 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002215 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002216 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08002217 i = i + run;
2218 }
Vladimir Markoc524e9e2019-03-26 10:54:50 +00002219 ObjPtr<mirror::Class> value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002220 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002221 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08002222 }
2223 } else if (obj->IsClass()) {
Vladimir Marko38b8b252018-01-02 19:07:06 +00002224 ObjPtr<mirror::Class> klass = obj->AsClass();
Igor Murashkin86083f72017-10-27 10:59:04 -07002225
Vladimir Marko305c38b2018-02-14 11:50:07 +00002226 if (kBitstringSubtypeCheckEnabled) {
2227 os << "SUBTYPE_CHECK_BITS: ";
2228 SubtypeCheck<ObjPtr<mirror::Class>>::Dump(klass, os);
2229 os << "\n";
2230 }
Igor Murashkin86083f72017-10-27 10:59:04 -07002231
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002232 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002233 os << "STATICS:\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002234 ScopedIndentation indent2(&vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002235 for (ArtField& field : klass->GetSFields()) {
2236 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08002237 }
2238 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002239 } else {
Andreas Gampe0c183382017-07-13 22:26:24 -07002240 auto it = dex_caches_.find(obj);
2241 if (it != dex_caches_.end()) {
Vladimir Marko05792b92015-08-03 11:56:49 +01002242 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Vladimir Markocd87c3e2017-09-05 13:11:57 +01002243 const auto& field_section = image_header_.GetFieldsSection();
Andreas Gampe0c183382017-07-13 22:26:24 -07002244 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01002245 size_t num_methods = dex_cache->NumResolvedMethods();
2246 if (num_methods != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002247 os << "Methods (size=" << num_methods << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002248 ScopedIndentation indent2(&vios_);
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002249 mirror::MethodDexCacheType* resolved_methods = dex_cache->GetResolvedMethods();
Vladimir Marko05792b92015-08-03 11:56:49 +01002250 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002251 ArtMethod* elem = mirror::DexCache::GetNativePairPtrSize(
2252 resolved_methods, i, image_pointer_size).object;
Vladimir Marko05792b92015-08-03 11:56:49 +01002253 size_t run = 0;
2254 for (size_t j = i + 1;
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002255 j != length &&
2256 elem == mirror::DexCache::GetNativePairPtrSize(
2257 resolved_methods, j, image_pointer_size).object;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002258 ++j) {
2259 ++run;
2260 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002261 if (run == 0) {
2262 os << StringPrintf("%zd: ", i);
2263 } else {
2264 os << StringPrintf("%zd to %zd: ", i, i + run);
2265 i = i + run;
2266 }
2267 std::string msg;
2268 if (elem == nullptr) {
2269 msg = "null";
2270 } else if (method_section.Contains(
Andreas Gampe0c183382017-07-13 22:26:24 -07002271 reinterpret_cast<uint8_t*>(elem) - image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002272 msg = reinterpret_cast<ArtMethod*>(elem)->PrettyMethod();
Vladimir Marko05792b92015-08-03 11:56:49 +01002273 } else {
2274 msg = "<not in method section>";
2275 }
2276 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07002277 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002278 }
2279 size_t num_fields = dex_cache->NumResolvedFields();
2280 if (num_fields != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002281 os << "Fields (size=" << num_fields << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002282 ScopedIndentation indent2(&vios_);
Vladimir Marko05792b92015-08-03 11:56:49 +01002283 auto* resolved_fields = dex_cache->GetResolvedFields();
2284 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002285 ArtField* elem = mirror::DexCache::GetNativePairPtrSize(
Vladimir Markof44d36c2017-03-14 14:18:46 +00002286 resolved_fields, i, image_pointer_size).object;
Vladimir Marko05792b92015-08-03 11:56:49 +01002287 size_t run = 0;
2288 for (size_t j = i + 1;
Vladimir Markof44d36c2017-03-14 14:18:46 +00002289 j != length &&
2290 elem == mirror::DexCache::GetNativePairPtrSize(
2291 resolved_fields, j, image_pointer_size).object;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002292 ++j) {
2293 ++run;
2294 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002295 if (run == 0) {
2296 os << StringPrintf("%zd: ", i);
2297 } else {
2298 os << StringPrintf("%zd to %zd: ", i, i + run);
2299 i = i + run;
2300 }
2301 std::string msg;
2302 if (elem == nullptr) {
2303 msg = "null";
2304 } else if (field_section.Contains(
Andreas Gampe0c183382017-07-13 22:26:24 -07002305 reinterpret_cast<uint8_t*>(elem) - image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002306 msg = reinterpret_cast<ArtField*>(elem)->PrettyField();
Vladimir Marko05792b92015-08-03 11:56:49 +01002307 } else {
2308 msg = "<not in field section>";
2309 }
2310 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002311 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002312 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002313 size_t num_types = dex_cache->NumResolvedTypes();
2314 if (num_types != 0u) {
2315 os << "Types (size=" << num_types << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002316 ScopedIndentation indent2(&vios_);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002317 auto* resolved_types = dex_cache->GetResolvedTypes();
2318 for (size_t i = 0; i < num_types; ++i) {
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002319 auto pair = resolved_types[i].load(std::memory_order_relaxed);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002320 size_t run = 0;
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002321 for (size_t j = i + 1; j != num_types; ++j) {
2322 auto other_pair = resolved_types[j].load(std::memory_order_relaxed);
2323 if (pair.index != other_pair.index ||
2324 pair.object.Read() != other_pair.object.Read()) {
2325 break;
2326 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002327 ++run;
2328 }
2329 if (run == 0) {
2330 os << StringPrintf("%zd: ", i);
2331 } else {
2332 os << StringPrintf("%zd to %zd: ", i, i + run);
2333 i = i + run;
2334 }
2335 std::string msg;
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002336 auto* elem = pair.object.Read();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002337 if (elem == nullptr) {
2338 msg = "null";
2339 } else {
David Sehr709b0702016-10-13 09:12:37 -07002340 msg = elem->PrettyClass();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002341 }
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002342 os << StringPrintf("%p %u %s\n", elem, pair.index, msg.c_str());
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002343 }
2344 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002345 }
2346 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002347 std::string temp;
Andreas Gampe0c183382017-07-13 22:26:24 -07002348 stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002349 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002350
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002351 void DumpMethod(ArtMethod* method, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002352 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002353 DCHECK(method != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07002354 const PointerSize pointer_size = image_header_.GetPointerSize();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002355 if (method->IsNative()) {
David Srbeckya54e0cc2019-04-15 15:03:26 +01002356 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002357 bool first_occurrence;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002358 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2359 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002360 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002361 stats_.native_to_managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002362 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002363 if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
2364 image_header_.GetPointerSize())) {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002365 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002366 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002367 } else if (method->IsAbstract() || method->IsClassInitializer()) {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002368 // Don't print information for these.
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002369 } else if (method->IsRuntimeMethod()) {
2370 ImtConflictTable* table = method->GetImtConflictTable(image_header_.GetPointerSize());
2371 if (table != nullptr) {
2372 indent_os << "IMT conflict table " << table << " method: ";
2373 for (size_t i = 0, count = table->NumEntries(pointer_size); i < count; ++i) {
David Sehr709b0702016-10-13 09:12:37 -07002374 indent_os << ArtMethod::PrettyMethod(table->GetImplementationMethod(i, pointer_size))
2375 << " ";
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002376 }
2377 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002378 } else {
David Sehr0225f8e2018-01-31 08:52:24 +00002379 CodeItemDataAccessor code_item_accessor(method->DexInstructionData());
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08002380 size_t dex_instruction_bytes = code_item_accessor.InsnsSizeInCodeUnits() * 2;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002381 stats_.dex_instruction_bytes += dex_instruction_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002382
David Srbeckya54e0cc2019-04-15 15:03:26 +01002383 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
2384 const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
2385
Mathieu Chartiere401d142015-04-22 13:56:20 -07002386 bool first_occurrence;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002387 size_t vmap_table_bytes = 0u;
David Srbeckya54e0cc2019-04-15 15:03:26 +01002388 if (quick_oat_code_begin != nullptr) {
2389 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2390 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
2391 vmap_table_bytes = ComputeOatSize(method_header->GetOptimizedCodeInfoPtr(),
2392 &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002393 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002394 stats_.vmap_table_bytes += vmap_table_bytes;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002395 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002396 }
2397
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002398 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2399 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002400 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002401 stats_.managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002402 if (method->IsConstructor()) {
2403 if (method->IsStatic()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002404 stats_.class_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002405 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002406 stats_.large_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002407 }
2408 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002409 stats_.large_method_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002410 }
2411 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002412 stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002413
Igor Murashkin7617abd2015-07-10 18:27:47 -07002414 uint32_t method_access_flags = method->GetAccessFlags();
2415
Mathieu Chartiere401d142015-04-22 13:56:20 -07002416 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002417 indent_os << StringPrintf("SIZE: Dex Instructions=%zd StackMaps=%zd AccessFlags=0x%x\n",
2418 dex_instruction_bytes,
2419 vmap_table_bytes,
Igor Murashkin7617abd2015-07-10 18:27:47 -07002420 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002421
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002422 size_t total_size = dex_instruction_bytes +
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002423 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002424
2425 double expansion =
2426 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002427 stats_.ComputeOutliers(total_size, expansion, method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002428 }
2429 }
2430
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002431 std::set<const void*> already_seen_;
2432 // Compute the size of the given data within the oat file and whether this is the first time
2433 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002434 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002435 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002436 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002437 already_seen_.insert(oat_data);
2438 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002439 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002440 }
2441 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002442 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002443
2444 public:
2445 struct Stats {
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002446 size_t oat_file_bytes = 0u;
2447 size_t file_bytes = 0u;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002448
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002449 size_t header_bytes = 0u;
2450 size_t object_bytes = 0u;
2451 size_t art_field_bytes = 0u;
2452 size_t art_method_bytes = 0u;
2453 size_t dex_cache_arrays_bytes = 0u;
2454 size_t interned_strings_bytes = 0u;
2455 size_t class_table_bytes = 0u;
2456 size_t sro_offset_bytes = 0u;
2457 size_t metadata_bytes = 0u;
2458 size_t bitmap_bytes = 0u;
2459 size_t alignment_bytes = 0u;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002460
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002461 size_t managed_code_bytes = 0u;
2462 size_t managed_code_bytes_ignoring_deduplication = 0u;
2463 size_t native_to_managed_code_bytes = 0u;
2464 size_t class_initializer_code_bytes = 0u;
2465 size_t large_initializer_code_bytes = 0u;
2466 size_t large_method_code_bytes = 0u;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002467
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002468 size_t vmap_table_bytes = 0u;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002469
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002470 size_t dex_instruction_bytes = 0u;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002471
Mathieu Chartiere401d142015-04-22 13:56:20 -07002472 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002473 std::vector<size_t> method_outlier_size;
2474 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002475 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002476
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002477 Stats() {}
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002478
Elliott Hughesa0e18062012-04-13 15:59:59 -07002479 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002480 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002481 size_t bytes;
2482 size_t count;
2483 };
Andreas Gampec55bb392018-09-21 00:02:02 +00002484 using SizeAndCountTable = SafeMap<std::string, SizeAndCount>;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002485 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002486
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002487 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002488 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2489 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002490 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002491 it->second.count += 1;
2492 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002493 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002494 }
2495 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002496
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002497 double PercentOfOatBytes(size_t size) {
2498 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2499 }
2500
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002501 double PercentOfFileBytes(size_t size) {
2502 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2503 }
2504
2505 double PercentOfObjectBytes(size_t size) {
2506 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2507 }
2508
Mathieu Chartiere401d142015-04-22 13:56:20 -07002509 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002510 method_outlier_size.push_back(total_size);
2511 method_outlier_expansion.push_back(expansion);
2512 method_outlier.push_back(method);
2513 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002514
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002515 void DumpOutliers(std::ostream& os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002516 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002517 size_t sum_of_sizes = 0;
2518 size_t sum_of_sizes_squared = 0;
2519 size_t sum_of_expansion = 0;
2520 size_t sum_of_expansion_squared = 0;
2521 size_t n = method_outlier_size.size();
Mathieu Chartier1ebf8d32016-06-09 11:51:27 -07002522 if (n <= 1) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08002523 return;
2524 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002525 for (size_t i = 0; i < n; i++) {
2526 size_t cur_size = method_outlier_size[i];
2527 sum_of_sizes += cur_size;
2528 sum_of_sizes_squared += cur_size * cur_size;
2529 double cur_expansion = method_outlier_expansion[i];
2530 sum_of_expansion += cur_expansion;
2531 sum_of_expansion_squared += cur_expansion * cur_expansion;
2532 }
2533 size_t size_mean = sum_of_sizes / n;
2534 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2535 double expansion_mean = sum_of_expansion / n;
2536 double expansion_variance =
2537 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2538
2539 // Dump methods whose size is a certain number of standard deviations from the mean
2540 size_t dumped_values = 0;
2541 size_t skipped_values = 0;
2542 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2543 size_t cur_size_variance = i * i * size_variance;
2544 bool first = true;
2545 for (size_t j = 0; j < n; j++) {
2546 size_t cur_size = method_outlier_size[j];
2547 if (cur_size > size_mean) {
2548 size_t cur_var = cur_size - size_mean;
2549 cur_var = cur_var * cur_var;
2550 if (cur_var > cur_size_variance) {
2551 if (dumped_values > 20) {
2552 if (i == 1) {
2553 skipped_values++;
2554 } else {
2555 i = 2; // jump to counting for 1 standard deviation
2556 break;
2557 }
2558 } else {
2559 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002560 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002561 first = false;
2562 }
David Sehr709b0702016-10-13 09:12:37 -07002563 os << ArtMethod::PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002564 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002565 method_outlier_size[j] = 0; // don't consider this method again
2566 dumped_values++;
2567 }
2568 }
2569 }
2570 }
2571 }
2572 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002573 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002574 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002575 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002576 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002577
2578 // Dump methods whose expansion is a certain number of standard deviations from the mean
2579 dumped_values = 0;
2580 skipped_values = 0;
2581 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2582 double cur_expansion_variance = i * i * expansion_variance;
2583 bool first = true;
2584 for (size_t j = 0; j < n; j++) {
2585 double cur_expansion = method_outlier_expansion[j];
2586 if (cur_expansion > expansion_mean) {
2587 size_t cur_var = cur_expansion - expansion_mean;
2588 cur_var = cur_var * cur_var;
2589 if (cur_var > cur_expansion_variance) {
2590 if (dumped_values > 20) {
2591 if (i == 1) {
2592 skipped_values++;
2593 } else {
2594 i = 2; // jump to counting for 1 standard deviation
2595 break;
2596 }
2597 } else {
2598 if (first) {
2599 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002600 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002601 first = false;
2602 }
David Sehr709b0702016-10-13 09:12:37 -07002603 os << ArtMethod::PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002604 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002605 method_outlier_expansion[j] = 0.0; // don't consider this method again
2606 dumped_values++;
2607 }
2608 }
2609 }
2610 }
2611 }
2612 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002613 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002614 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002615 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002616 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002617 }
2618
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002619 void Dump(std::ostream& os, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002620 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002621 {
2622 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2623 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002624 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2625 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2626 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2627 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2628 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2629 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002630 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002631 "sro_bytes = %8zd (%2.0f%% of art file bytes)\n"
2632 "metadata_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002633 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2634 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002635 header_bytes, PercentOfFileBytes(header_bytes),
2636 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002637 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2638 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002639 dex_cache_arrays_bytes,
2640 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002641 interned_strings_bytes,
2642 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002643 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002644 sro_offset_bytes, PercentOfFileBytes(sro_offset_bytes),
2645 metadata_bytes, PercentOfFileBytes(metadata_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002646 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002647 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2648 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002649 CHECK_EQ(file_bytes,
2650 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2651 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002652 sro_offset_bytes + metadata_bytes + bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002653 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002654
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002655 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002656 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002657 for (const auto& sizes_and_count : sizes_and_counts) {
2658 const std::string& descriptor(sizes_and_count.first);
2659 double average = static_cast<double>(sizes_and_count.second.bytes) /
2660 static_cast<double>(sizes_and_count.second.count);
2661 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002662 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002663 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002664 descriptor.c_str(), sizes_and_count.second.bytes,
2665 sizes_and_count.second.count, average, percent);
2666 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002667 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002668 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002669 CHECK_EQ(object_bytes, object_bytes_total);
2670
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002671 os << StringPrintf("oat_file_bytes = %8zd\n"
2672 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002673 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2674 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2675 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2676 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002677 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002678 managed_code_bytes,
2679 PercentOfOatBytes(managed_code_bytes),
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002680 native_to_managed_code_bytes,
2681 PercentOfOatBytes(native_to_managed_code_bytes),
2682 class_initializer_code_bytes,
2683 PercentOfOatBytes(class_initializer_code_bytes),
2684 large_initializer_code_bytes,
2685 PercentOfOatBytes(large_initializer_code_bytes),
2686 large_method_code_bytes,
2687 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002688 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002689 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002690 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002691 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2692 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002693 }
2694
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002695 os << "\n" << StringPrintf("vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002696 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002697 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002698
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002699 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2700 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002701 static_cast<double>(managed_code_bytes) /
2702 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002703 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002704 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002705 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002706
2707 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002708 }
2709 } stats_;
2710
2711 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002712 enum {
2713 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2714 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2715 kLargeConstructorDexBytes = 4000,
2716 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2717 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2718 kLargeMethodDexBytes = 16000
2719 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002720
2721 // For performance, use the *os_ directly for anything that doesn't need indentation
2722 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002723 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002724 VariableIndentationOutputStream vios_;
2725 ScopedIndentation indent1_;
2726
Ian Rogers1d54e732013-05-02 21:10:01 -07002727 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002728 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002729 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002730 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002731 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002732
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002733 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002734};
2735
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002736static int DumpImage(gc::space::ImageSpace* image_space,
2737 OatDumperOptions* options,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002738 std::ostream* os) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002739 const ImageHeader& image_header = image_space->GetImageHeader();
2740 if (!image_header.IsValid()) {
Andreas Gampe221d9812018-01-22 17:48:56 -08002741 LOG(ERROR) << "Invalid image header " << image_space->GetImageLocation();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002742 return EXIT_FAILURE;
2743 }
2744 ImageDumper image_dumper(os, *image_space, image_header, options);
2745 if (!image_dumper.Dump()) {
2746 return EXIT_FAILURE;
2747 }
2748 return EXIT_SUCCESS;
2749}
2750
2751static int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002752 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002753 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002754 options->class_loader_ = &null_class_loader;
2755
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002756 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002757 if (options->app_image_ != nullptr) {
2758 if (options->app_oat_ == nullptr) {
2759 LOG(ERROR) << "Can not dump app image without app oat file";
Jeff Haodcdc85b2015-12-04 14:06:18 -08002760 return EXIT_FAILURE;
2761 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002762 // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
2763 // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
2764 // pointers into 32 bit pointer sized ArtMethods.
2765 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002766 std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01002767 options->app_oat_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002768 options->app_oat_,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002769 /*executable=*/ false,
2770 /*low_4gb=*/ true,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002771 &error_msg));
2772 if (oat_file == nullptr) {
2773 LOG(ERROR) << "Failed to open oat file " << options->app_oat_ << " with error " << error_msg;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002774 return EXIT_FAILURE;
2775 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002776 std::unique_ptr<gc::space::ImageSpace> space(
2777 gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg));
2778 if (space == nullptr) {
2779 LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error "
2780 << error_msg;
Mathieu Chartier6b689ce2019-07-18 14:17:47 -07002781 return EXIT_FAILURE;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002782 }
2783 // Open dex files for the image.
2784 std::vector<std::unique_ptr<const DexFile>> dex_files;
2785 if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
2786 LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
2787 << error_msg;
Mathieu Chartier6b689ce2019-07-18 14:17:47 -07002788 return EXIT_FAILURE;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002789 }
2790 // Dump the actual image.
2791 int result = DumpImage(space.get(), options, os);
2792 if (result != EXIT_SUCCESS) {
2793 return result;
2794 }
2795 // Fall through to dump the boot images.
2796 }
2797
2798 gc::Heap* heap = runtime->GetHeap();
2799 CHECK(heap->HasBootImageSpace()) << "No image spaces";
2800 for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
2801 int result = DumpImage(image_space, options, os);
2802 if (result != EXIT_SUCCESS) {
2803 return result;
2804 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002805 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002806 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002807}
2808
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002809static jobject InstallOatFile(Runtime* runtime,
2810 std::unique_ptr<OatFile> oat_file,
2811 std::vector<const DexFile*>* class_path)
2812 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002813 Thread* self = Thread::Current();
2814 CHECK(self != nullptr);
2815 // Need well-known-classes.
2816 WellKnownClasses::Init(self->GetJniEnv());
2817
Vladimir Marko421087b2018-02-27 11:00:17 +00002818 // Open dex files.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002819 OatFile* oat_file_ptr = oat_file.get();
Andreas Gampe00b25f32014-09-17 21:49:05 -07002820 ClassLinker* class_linker = runtime->GetClassLinker();
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002821 runtime->GetOatFileManager().RegisterOatFile(std::move(oat_file));
Andreas Gampeb40d3612018-06-26 15:49:42 -07002822 for (const OatDexFile* odf : oat_file_ptr->GetOatDexFiles()) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002823 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002824 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002825 CHECK(dex_file != nullptr) << error_msg;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002826 class_path->push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002827 }
2828
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002829 // Need a class loader. Fake that we're a compiler.
2830 // Note: this will run initializers through the unstarted runtime, so make sure it's
2831 // initialized.
2832 interpreter::UnstartedRuntime::Initialize();
2833
2834 jobject class_loader = class_linker->CreatePathClassLoader(self, *class_path);
2835
Vladimir Marko421087b2018-02-27 11:00:17 +00002836 // Need to register dex files to get a working dex cache.
2837 for (const DexFile* dex_file : *class_path) {
2838 ObjPtr<mirror::DexCache> dex_cache = class_linker->RegisterDexFile(
2839 *dex_file, self->DecodeJObject(class_loader)->AsClassLoader());
2840 CHECK(dex_cache != nullptr);
2841 }
2842
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002843 return class_loader;
2844}
2845
2846static int DumpOatWithRuntime(Runtime* runtime,
2847 std::unique_ptr<OatFile> oat_file,
2848 OatDumperOptions* options,
2849 std::ostream* os) {
2850 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2851 ScopedObjectAccess soa(Thread::Current());
2852
2853 OatFile* oat_file_ptr = oat_file.get();
2854 std::vector<const DexFile*> class_path;
2855 jobject class_loader = InstallOatFile(runtime, std::move(oat_file), &class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002856
2857 // Use the class loader while dumping.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002858 StackHandleScope<1> scope(soa.Self());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002859 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07002860 soa.Decode<mirror::ClassLoader>(class_loader));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002861 options->class_loader_ = &loader_handle;
2862
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002863 OatDumper oat_dumper(*oat_file_ptr, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002864 bool success = oat_dumper.Dump(*os);
2865 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2866}
2867
2868static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002869 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002870 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002871 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002872 options->class_loader_ = &null_class_loader;
2873
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002874 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002875 bool success = oat_dumper.Dump(*os);
2876 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2877}
2878
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002879static int DumpOat(Runtime* runtime,
2880 const char* oat_filename,
2881 const char* dex_filename,
2882 OatDumperOptions* options,
Andreas Gampe00b25f32014-09-17 21:49:05 -07002883 std::ostream* os) {
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002884 if (dex_filename == nullptr) {
2885 LOG(WARNING) << "No dex filename provided, "
2886 << "oatdump might fail if the oat file does not contain the dex code.";
2887 }
Vladimir Markob7bf8432019-12-03 13:18:50 +00002888 std::string dex_filename_str((dex_filename != nullptr) ? dex_filename : "");
2889 ArrayRef<const std::string> dex_filenames(&dex_filename_str,
2890 /*size=*/ (dex_filename != nullptr) ? 1u : 0u);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002891 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002892 std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01002893 oat_filename,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002894 oat_filename,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002895 /*executable=*/ false,
2896 /*low_4gb=*/ false,
Vladimir Markob7bf8432019-12-03 13:18:50 +00002897 dex_filenames,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002898 /*reservation=*/ nullptr,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002899 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002900 if (oat_file == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08002901 LOG(ERROR) << "Failed to open oat file from '" << oat_filename << "': " << error_msg;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002902 return EXIT_FAILURE;
2903 }
2904
2905 if (runtime != nullptr) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002906 return DumpOatWithRuntime(runtime, std::move(oat_file), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002907 } else {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002908 return DumpOatWithoutRuntime(oat_file.get(), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002909 }
2910}
2911
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002912static int SymbolizeOat(const char* oat_filename,
2913 const char* dex_filename,
2914 std::string& output_name,
2915 bool no_bits) {
Vladimir Markob7bf8432019-12-03 13:18:50 +00002916 std::string dex_filename_str((dex_filename != nullptr) ? dex_filename : "");
2917 ArrayRef<const std::string> dex_filenames(&dex_filename_str,
2918 /*size=*/ (dex_filename != nullptr) ? 1u : 0u);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002919 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002920 std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01002921 oat_filename,
Andreas Gampe08c277c2017-04-26 22:22:15 -07002922 oat_filename,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002923 /*executable=*/ false,
2924 /*low_4gb=*/ false,
Vladimir Markob7bf8432019-12-03 13:18:50 +00002925 dex_filenames,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002926 /*reservation=*/ nullptr,
Andreas Gampe08c277c2017-04-26 22:22:15 -07002927 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002928 if (oat_file == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08002929 LOG(ERROR) << "Failed to open oat file from '" << oat_filename << "': " << error_msg;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002930 return EXIT_FAILURE;
2931 }
2932
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002933 bool result;
2934 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
2935 // files for 64-bit code in the past.
2936 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
Andreas Gampe08c277c2017-04-26 22:22:15 -07002937 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file.get(), output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002938 result = oat_symbolizer.Symbolize();
2939 } else {
Andreas Gampe08c277c2017-04-26 22:22:15 -07002940 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file.get(), output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002941 result = oat_symbolizer.Symbolize();
2942 }
2943 if (!result) {
Andreas Gampe221d9812018-01-22 17:48:56 -08002944 LOG(ERROR) << "Failed to symbolize";
Andreas Gampe00b25f32014-09-17 21:49:05 -07002945 return EXIT_FAILURE;
2946 }
2947
2948 return EXIT_SUCCESS;
2949}
2950
Andreas Gampe9fded872016-09-25 16:08:35 -07002951class IMTDumper {
2952 public:
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002953 static bool Dump(Runtime* runtime,
2954 const std::string& imt_file,
2955 bool dump_imt_stats,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002956 const char* oat_filename,
2957 const char* dex_filename) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002958 Thread* self = Thread::Current();
2959
2960 ScopedObjectAccess soa(self);
2961 StackHandleScope<1> scope(self);
2962 MutableHandle<mirror::ClassLoader> class_loader = scope.NewHandle<mirror::ClassLoader>(nullptr);
2963 std::vector<const DexFile*> class_path;
2964
2965 if (oat_filename != nullptr) {
Vladimir Markob7bf8432019-12-03 13:18:50 +00002966 std::string dex_filename_str((dex_filename != nullptr) ? dex_filename : "");
2967 ArrayRef<const std::string> dex_filenames(&dex_filename_str,
2968 /*size=*/ (dex_filename != nullptr) ? 1u : 0u);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002969 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002970 std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01002971 oat_filename,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002972 oat_filename,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002973 /*executable=*/ false,
Andreas Gampe9b031f72018-10-04 11:03:34 -07002974 /*low_4gb=*/false,
Vladimir Markob7bf8432019-12-03 13:18:50 +00002975 dex_filenames,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002976 /*reservation=*/ nullptr,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002977 &error_msg));
2978 if (oat_file == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08002979 LOG(ERROR) << "Failed to open oat file from '" << oat_filename << "': " << error_msg;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002980 return false;
2981 }
2982
2983 class_loader.Assign(soa.Decode<mirror::ClassLoader>(
2984 InstallOatFile(runtime, std::move(oat_file), &class_path)));
2985 } else {
2986 class_loader.Assign(nullptr); // Boot classloader. Just here for explicit documentation.
2987 class_path = runtime->GetClassLinker()->GetBootClassPath();
2988 }
2989
2990 if (!imt_file.empty()) {
2991 return DumpImt(runtime, imt_file, class_loader);
2992 }
2993
2994 if (dump_imt_stats) {
2995 return DumpImtStats(runtime, class_path, class_loader);
2996 }
2997
2998 LOG(FATAL) << "Should not reach here";
2999 UNREACHABLE();
3000 }
3001
3002 private:
3003 static bool DumpImt(Runtime* runtime,
3004 const std::string& imt_file,
3005 Handle<mirror::ClassLoader> h_class_loader)
3006 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003007 std::vector<std::string> lines = ReadCommentedInputFromFile(imt_file);
3008 std::unordered_set<std::string> prepared;
3009
3010 for (const std::string& line : lines) {
3011 // A line should be either a class descriptor, in which case we will dump the complete IMT,
3012 // or a class descriptor and an interface method, in which case we will lookup the method,
3013 // determine its IMT slot, and check the class' IMT.
3014 size_t first_space = line.find(' ');
3015 if (first_space == std::string::npos) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003016 DumpIMTForClass(runtime, line, h_class_loader, &prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07003017 } else {
3018 DumpIMTForMethod(runtime,
3019 line.substr(0, first_space),
3020 line.substr(first_space + 1, std::string::npos),
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003021 h_class_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003022 &prepared);
3023 }
3024 std::cerr << std::endl;
3025 }
3026
3027 return true;
3028 }
3029
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003030 static bool DumpImtStats(Runtime* runtime,
3031 const std::vector<const DexFile*>& dex_files,
3032 Handle<mirror::ClassLoader> h_class_loader)
3033 REQUIRES_SHARED(Locks::mutator_lock_) {
3034 size_t without_imt = 0;
3035 size_t with_imt = 0;
Andreas Gampe9fded872016-09-25 16:08:35 -07003036 std::map<size_t, size_t> histogram;
3037
3038 ClassLinker* class_linker = runtime->GetClassLinker();
3039 const PointerSize pointer_size = class_linker->GetImagePointerSize();
3040 std::unordered_set<std::string> prepared;
3041
3042 Thread* self = Thread::Current();
Andreas Gampe9fded872016-09-25 16:08:35 -07003043 StackHandleScope<1> scope(self);
3044 MutableHandle<mirror::Class> h_klass(scope.NewHandle<mirror::Class>(nullptr));
3045
3046 for (const DexFile* dex_file : dex_files) {
3047 for (uint32_t class_def_index = 0;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003048 class_def_index != dex_file->NumClassDefs();
3049 ++class_def_index) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08003050 const dex::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Andreas Gampe9fded872016-09-25 16:08:35 -07003051 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003052 h_klass.Assign(class_linker->FindClass(self, descriptor, h_class_loader));
Andreas Gampefa4333d2017-02-14 11:10:34 -08003053 if (h_klass == nullptr) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003054 std::cerr << "Warning: could not load " << descriptor << std::endl;
3055 continue;
3056 }
3057
3058 if (HasNoIMT(runtime, h_klass, pointer_size, &prepared)) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003059 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003060 continue;
3061 }
3062
3063 ImTable* im_table = PrepareAndGetImTable(runtime, h_klass, pointer_size, &prepared);
3064 if (im_table == nullptr) {
3065 // Should not happen, but accept.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003066 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003067 continue;
3068 }
3069
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003070 with_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003071 for (size_t imt_index = 0; imt_index != ImTable::kSize; ++imt_index) {
3072 ArtMethod* ptr = im_table->Get(imt_index, pointer_size);
3073 if (ptr->IsRuntimeMethod()) {
3074 if (ptr->IsImtUnimplementedMethod()) {
3075 histogram[0]++;
3076 } else {
3077 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3078 histogram[current_table->NumEntries(pointer_size)]++;
3079 }
3080 } else {
3081 histogram[1]++;
3082 }
3083 }
3084 }
3085 }
3086
3087 std::cerr << "IMT stats:"
3088 << std::endl << std::endl;
3089
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003090 std::cerr << " " << with_imt << " classes with IMT."
Andreas Gampe9fded872016-09-25 16:08:35 -07003091 << std::endl << std::endl;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003092 std::cerr << " " << without_imt << " classes without IMT (or copy from Object)."
Andreas Gampe9fded872016-09-25 16:08:35 -07003093 << std::endl << std::endl;
3094
3095 double sum_one = 0;
3096 size_t count_one = 0;
3097
3098 std::cerr << " " << "IMT histogram" << std::endl;
3099 for (auto& bucket : histogram) {
3100 std::cerr << " " << bucket.first << " " << bucket.second << std::endl;
3101 if (bucket.first > 0) {
3102 sum_one += bucket.second * bucket.first;
3103 count_one += bucket.second;
3104 }
3105 }
3106
3107 double count_zero = count_one + histogram[0];
3108 std::cerr << " Stats:" << std::endl;
3109 std::cerr << " Average depth (including empty): " << (sum_one / count_zero) << std::endl;
3110 std::cerr << " Average depth (excluding empty): " << (sum_one / count_one) << std::endl;
3111
3112 return true;
3113 }
3114
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003115 // Return whether the given class has no IMT (or the one shared with java.lang.Object).
Andreas Gampe9fded872016-09-25 16:08:35 -07003116 static bool HasNoIMT(Runtime* runtime,
3117 Handle<mirror::Class> klass,
3118 const PointerSize pointer_size,
3119 std::unordered_set<std::string>* prepared)
3120 REQUIRES_SHARED(Locks::mutator_lock_) {
3121 if (klass->IsObjectClass() || !klass->ShouldHaveImt()) {
3122 return true;
3123 }
3124
3125 if (klass->GetImt(pointer_size) == nullptr) {
3126 PrepareClass(runtime, klass, prepared);
3127 }
3128
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003129 ObjPtr<mirror::Class> object_class = GetClassRoot<mirror::Object>();
Andreas Gampe9fded872016-09-25 16:08:35 -07003130 DCHECK(object_class->IsObjectClass());
3131
3132 bool result = klass->GetImt(pointer_size) == object_class->GetImt(pointer_size);
3133
Mathieu Chartier6beced42016-11-15 15:51:31 -08003134 if (klass->GetIfTable()->Count() == 0) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003135 DCHECK(result);
3136 }
3137
3138 return result;
3139 }
3140
3141 static void PrintTable(ImtConflictTable* table, PointerSize pointer_size)
3142 REQUIRES_SHARED(Locks::mutator_lock_) {
3143 if (table == nullptr) {
3144 std::cerr << " <No IMT?>" << std::endl;
3145 return;
3146 }
3147 size_t table_index = 0;
3148 for (;;) {
3149 ArtMethod* ptr = table->GetInterfaceMethod(table_index, pointer_size);
3150 if (ptr == nullptr) {
3151 return;
3152 }
3153 table_index++;
David Sehr709b0702016-10-13 09:12:37 -07003154 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003155 }
3156 }
3157
3158 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3159 Thread* self,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003160 Handle<mirror::ClassLoader> h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003161 const std::string& class_name,
3162 const PointerSize pointer_size,
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003163 /*out*/ ObjPtr<mirror::Class>* klass_out,
3164 /*inout*/ std::unordered_set<std::string>* prepared)
Andreas Gampe9fded872016-09-25 16:08:35 -07003165 REQUIRES_SHARED(Locks::mutator_lock_) {
3166 if (class_name.empty()) {
3167 return nullptr;
3168 }
3169
3170 std::string descriptor;
3171 if (class_name[0] == 'L') {
3172 descriptor = class_name;
3173 } else {
3174 descriptor = DotToDescriptor(class_name.c_str());
3175 }
3176
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003177 ObjPtr<mirror::Class> klass =
3178 runtime->GetClassLinker()->FindClass(self, descriptor.c_str(), h_loader);
Andreas Gampe9fded872016-09-25 16:08:35 -07003179
3180 if (klass == nullptr) {
3181 self->ClearException();
3182 std::cerr << "Did not find " << class_name << std::endl;
3183 *klass_out = nullptr;
3184 return nullptr;
3185 }
3186
3187 StackHandleScope<1> scope(Thread::Current());
3188 Handle<mirror::Class> h_klass = scope.NewHandle<mirror::Class>(klass);
3189
3190 ImTable* ret = PrepareAndGetImTable(runtime, h_klass, pointer_size, prepared);
3191 *klass_out = h_klass.Get();
3192 return ret;
3193 }
3194
3195 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3196 Handle<mirror::Class> h_klass,
3197 const PointerSize pointer_size,
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003198 /*inout*/ std::unordered_set<std::string>* prepared)
Andreas Gampe9fded872016-09-25 16:08:35 -07003199 REQUIRES_SHARED(Locks::mutator_lock_) {
3200 PrepareClass(runtime, h_klass, prepared);
3201 return h_klass->GetImt(pointer_size);
3202 }
3203
3204 static void DumpIMTForClass(Runtime* runtime,
3205 const std::string& class_name,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003206 Handle<mirror::ClassLoader> h_loader,
3207 std::unordered_set<std::string>* prepared)
3208 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003209 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003210 ObjPtr<mirror::Class> klass;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003211 ImTable* imt = PrepareAndGetImTable(runtime,
3212 Thread::Current(),
3213 h_loader,
3214 class_name,
3215 pointer_size,
3216 &klass,
3217 prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07003218 if (imt == nullptr) {
3219 return;
3220 }
3221
3222 std::cerr << class_name << std::endl << " IMT:" << std::endl;
3223 for (size_t index = 0; index < ImTable::kSize; ++index) {
3224 std::cerr << " " << index << ":" << std::endl;
3225 ArtMethod* ptr = imt->Get(index, pointer_size);
3226 if (ptr->IsRuntimeMethod()) {
3227 if (ptr->IsImtUnimplementedMethod()) {
3228 std::cerr << " <empty>" << std::endl;
3229 } else {
3230 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3231 PrintTable(current_table, pointer_size);
3232 }
3233 } else {
David Sehr709b0702016-10-13 09:12:37 -07003234 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003235 }
3236 }
3237
3238 std::cerr << " Interfaces:" << std::endl;
3239 // Run through iftable, find methods that slot here, see if they fit.
Vladimir Markoc524e9e2019-03-26 10:54:50 +00003240 ObjPtr<mirror::IfTable> if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003241 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
Vladimir Markoc524e9e2019-03-26 10:54:50 +00003242 ObjPtr<mirror::Class> iface = if_table->GetInterface(i);
Mathieu Chartier6beced42016-11-15 15:51:31 -08003243 std::string iface_name;
3244 std::cerr << " " << iface->GetDescriptor(&iface_name) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003245
Mathieu Chartier6beced42016-11-15 15:51:31 -08003246 for (ArtMethod& iface_method : iface->GetVirtualMethods(pointer_size)) {
3247 uint32_t class_hash, name_hash, signature_hash;
3248 ImTable::GetImtHashComponents(&iface_method, &class_hash, &name_hash, &signature_hash);
3249 uint32_t imt_slot = ImTable::GetImtIndex(&iface_method);
3250 std::cerr << " " << iface_method.PrettyMethod(true)
3251 << " slot=" << imt_slot
3252 << std::hex
3253 << " class_hash=0x" << class_hash
3254 << " name_hash=0x" << name_hash
3255 << " signature_hash=0x" << signature_hash
3256 << std::dec
3257 << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003258 }
3259 }
3260 }
3261
3262 static void DumpIMTForMethod(Runtime* runtime,
3263 const std::string& class_name,
3264 const std::string& method,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003265 Handle<mirror::ClassLoader> h_loader,
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003266 /*inout*/ std::unordered_set<std::string>* prepared)
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003267 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003268 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003269 ObjPtr<mirror::Class> klass;
Andreas Gampe9fded872016-09-25 16:08:35 -07003270 ImTable* imt = PrepareAndGetImTable(runtime,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003271 Thread::Current(),
3272 h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003273 class_name,
3274 pointer_size,
3275 &klass,
3276 prepared);
3277 if (imt == nullptr) {
3278 return;
3279 }
3280
3281 std::cerr << class_name << " <" << method << ">" << std::endl;
3282 for (size_t index = 0; index < ImTable::kSize; ++index) {
3283 ArtMethod* ptr = imt->Get(index, pointer_size);
3284 if (ptr->IsRuntimeMethod()) {
3285 if (ptr->IsImtUnimplementedMethod()) {
3286 continue;
3287 }
3288
3289 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3290 if (current_table == nullptr) {
3291 continue;
3292 }
3293
3294 size_t table_index = 0;
3295 for (;;) {
3296 ArtMethod* ptr2 = current_table->GetInterfaceMethod(table_index, pointer_size);
3297 if (ptr2 == nullptr) {
3298 break;
3299 }
3300 table_index++;
3301
David Sehr709b0702016-10-13 09:12:37 -07003302 std::string p_name = ptr2->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003303 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003304 std::cerr << " Slot "
3305 << index
3306 << " ("
3307 << current_table->NumEntries(pointer_size)
3308 << ")"
3309 << std::endl;
3310 PrintTable(current_table, pointer_size);
3311 return;
3312 }
3313 }
3314 } else {
David Sehr709b0702016-10-13 09:12:37 -07003315 std::string p_name = ptr->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003316 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003317 std::cerr << " Slot " << index << " (1)" << std::endl;
3318 std::cerr << " " << p_name << std::endl;
3319 } else {
3320 // Run through iftable, find methods that slot here, see if they fit.
Vladimir Markoc524e9e2019-03-26 10:54:50 +00003321 ObjPtr<mirror::IfTable> if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003322 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
Vladimir Markoc524e9e2019-03-26 10:54:50 +00003323 ObjPtr<mirror::Class> iface = if_table->GetInterface(i);
Mathieu Chartier6beced42016-11-15 15:51:31 -08003324 size_t num_methods = iface->NumDeclaredVirtualMethods();
3325 if (num_methods > 0) {
3326 for (ArtMethod& iface_method : iface->GetMethods(pointer_size)) {
3327 if (ImTable::GetImtIndex(&iface_method) == index) {
3328 std::string i_name = iface_method.PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003329 if (android::base::StartsWith(i_name, method.c_str())) {
Mathieu Chartier6beced42016-11-15 15:51:31 -08003330 std::cerr << " Slot " << index << " (1)" << std::endl;
3331 std::cerr << " " << p_name << " (" << i_name << ")" << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003332 }
3333 }
3334 }
3335 }
3336 }
3337 }
3338 }
3339 }
3340 }
3341
3342 // Read lines from the given stream, dropping comments and empty lines
3343 static std::vector<std::string> ReadCommentedInputStream(std::istream& in_stream) {
3344 std::vector<std::string> output;
3345 while (in_stream.good()) {
3346 std::string dot;
3347 std::getline(in_stream, dot);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003348 if (android::base::StartsWith(dot, "#") || dot.empty()) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003349 continue;
3350 }
3351 output.push_back(dot);
3352 }
3353 return output;
3354 }
3355
3356 // Read lines from the given file, dropping comments and empty lines.
3357 static std::vector<std::string> ReadCommentedInputFromFile(const std::string& input_filename) {
3358 std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
3359 if (input_file.get() == nullptr) {
3360 LOG(ERROR) << "Failed to open input file " << input_filename;
3361 return std::vector<std::string>();
3362 }
3363 std::vector<std::string> result = ReadCommentedInputStream(*input_file);
3364 input_file->close();
3365 return result;
3366 }
3367
3368 // Prepare a class, i.e., ensure it has a filled IMT. Will do so recursively for superclasses,
3369 // and note in the given set that the work was done.
3370 static void PrepareClass(Runtime* runtime,
3371 Handle<mirror::Class> h_klass,
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003372 /*inout*/ std::unordered_set<std::string>* done)
Andreas Gampe9fded872016-09-25 16:08:35 -07003373 REQUIRES_SHARED(Locks::mutator_lock_) {
3374 if (!h_klass->ShouldHaveImt()) {
3375 return;
3376 }
3377
3378 std::string name;
3379 name = h_klass->GetDescriptor(&name);
3380
3381 if (done->find(name) != done->end()) {
3382 return;
3383 }
3384 done->insert(name);
3385
3386 if (h_klass->HasSuperClass()) {
3387 StackHandleScope<1> h(Thread::Current());
3388 PrepareClass(runtime, h.NewHandle<mirror::Class>(h_klass->GetSuperClass()), done);
3389 }
3390
3391 if (!h_klass->IsTemp()) {
3392 runtime->GetClassLinker()->FillIMTAndConflictTables(h_klass.Get());
3393 }
3394 }
3395};
3396
Igor Murashkin37743352014-11-13 14:38:00 -08003397struct OatdumpArgs : public CmdlineArgs {
3398 protected:
3399 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003400
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003401 ParseStatus ParseCustom(const char* raw_option,
3402 size_t raw_option_length,
3403 std::string* error_msg) override {
3404 DCHECK_EQ(strlen(raw_option), raw_option_length);
Igor Murashkin37743352014-11-13 14:38:00 -08003405 {
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003406 ParseStatus base_parse = Base::ParseCustom(raw_option, raw_option_length, error_msg);
Igor Murashkin37743352014-11-13 14:38:00 -08003407 if (base_parse != kParseUnknownArgument) {
3408 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003409 }
3410 }
3411
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003412 std::string_view option(raw_option, raw_option_length);
3413 if (StartsWith(option, "--oat-file=")) {
3414 oat_filename_ = raw_option + strlen("--oat-file=");
3415 } else if (StartsWith(option, "--dex-file=")) {
3416 dex_filename_ = raw_option + strlen("--dex-file=");
3417 } else if (StartsWith(option, "--image=")) {
3418 image_location_ = raw_option + strlen("--image=");
Igor Murashkin37743352014-11-13 14:38:00 -08003419 } else if (option == "--no-dump:vmap") {
3420 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01003421 } else if (option =="--dump:code_info_stack_maps") {
3422 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003423 } else if (option == "--no-disassemble") {
3424 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003425 } else if (option =="--header-only") {
3426 dump_header_only_ = true;
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003427 } else if (StartsWith(option, "--symbolize=")) {
3428 oat_filename_ = raw_option + strlen("--symbolize=");
Igor Murashkin37743352014-11-13 14:38:00 -08003429 symbolize_ = true;
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003430 } else if (StartsWith(option, "--only-keep-debug")) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00003431 only_keep_debug_ = true;
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003432 } else if (StartsWith(option, "--class-filter=")) {
3433 class_filter_ = raw_option + strlen("--class-filter=");
3434 } else if (StartsWith(option, "--method-filter=")) {
3435 method_filter_ = raw_option + strlen("--method-filter=");
3436 } else if (StartsWith(option, "--list-classes")) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003437 list_classes_ = true;
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003438 } else if (StartsWith(option, "--list-methods")) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003439 list_methods_ = true;
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003440 } else if (StartsWith(option, "--export-dex-to=")) {
3441 export_dex_location_ = raw_option + strlen("--export-dex-to=");
3442 } else if (StartsWith(option, "--addr2instr=")) {
3443 if (!android::base::ParseUint(raw_option + strlen("--addr2instr="), &addr2instr_)) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003444 *error_msg = "Address conversion failed";
3445 return kParseError;
3446 }
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003447 } else if (StartsWith(option, "--app-image=")) {
3448 app_image_ = raw_option + strlen("--app-image=");
3449 } else if (StartsWith(option, "--app-oat=")) {
3450 app_oat_ = raw_option + strlen("--app-oat=");
3451 } else if (StartsWith(option, "--dump-imt=")) {
3452 imt_dump_ = std::string(option.substr(strlen("--dump-imt=")));
Andreas Gampe9fded872016-09-25 16:08:35 -07003453 } else if (option == "--dump-imt-stats") {
3454 imt_stat_dump_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003455 } else {
3456 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003457 }
3458
Igor Murashkin37743352014-11-13 14:38:00 -08003459 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003460 }
3461
Roland Levillainf73caca2018-08-24 17:19:07 +01003462 ParseStatus ParseChecks(std::string* error_msg) override {
Igor Murashkin37743352014-11-13 14:38:00 -08003463 // Infer boot image location from the image location if possible.
3464 if (boot_image_location_ == nullptr) {
3465 boot_image_location_ = image_location_;
3466 }
3467
3468 // Perform the parent checks.
3469 ParseStatus parent_checks = Base::ParseChecks(error_msg);
3470 if (parent_checks != kParseOk) {
3471 return parent_checks;
3472 }
3473
3474 // Perform our own checks.
3475 if (image_location_ == nullptr && oat_filename_ == nullptr) {
3476 *error_msg = "Either --image or --oat-file must be specified";
3477 return kParseError;
3478 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
3479 *error_msg = "Either --image or --oat-file must be specified but not both";
3480 return kParseError;
3481 }
3482
3483 return kParseOk;
3484 }
3485
Andreas Gampefa6a1b02018-09-07 08:11:55 -07003486 std::string GetUsage() const override {
Igor Murashkin37743352014-11-13 14:38:00 -08003487 std::string usage;
3488
3489 usage +=
3490 "Usage: oatdump [options] ...\n"
3491 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
3492 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
3493 "\n"
3494 // Either oat-file or image is required.
3495 " --oat-file=<file.oat>: specifies an input oat filename.\n"
Mathieu Chartier2afa19d2019-09-10 16:07:21 -07003496 " Example: --oat-file=/system/framework/arm64/boot.oat\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003497 "\n"
3498 " --image=<file.art>: specifies an input image location.\n"
3499 " Example: --image=/system/framework/boot.art\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003500 "\n"
3501 " --app-image=<file.art>: specifies an input app image. Must also have a specified\n"
Roland Levillain4f1c9e62017-06-28 16:44:30 +01003502 " boot image (with --image) and app oat file (with --app-oat).\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003503 " Example: --app-image=app.art\n"
3504 "\n"
3505 " --app-oat=<file.odex>: specifies an input app oat.\n"
3506 " Example: --app-oat=app.odex\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003507 "\n";
3508
3509 usage += Base::GetUsage();
3510
3511 usage += // Optional.
Igor Murashkin37743352014-11-13 14:38:00 -08003512 " --no-dump:vmap may be used to disable vmap dumping.\n"
3513 " Example: --no-dump:vmap\n"
3514 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01003515 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
3516 " Example: --dump:code_info_stack_maps\n"
3517 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003518 " --no-disassemble may be used to disable disassembly.\n"
3519 " Example: --no-disassemble\n"
3520 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00003521 " --header-only may be used to print only the oat header.\n"
3522 " Example: --header-only\n"
3523 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003524 " --list-classes may be used to list target file classes (can be used with filters).\n"
3525 " Example: --list-classes\n"
3526 " Example: --list-classes --class-filter=com.example.foo\n"
3527 "\n"
3528 " --list-methods may be used to list target file methods (can be used with filters).\n"
3529 " Example: --list-methods\n"
3530 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
3531 "\n"
3532 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
3533 " Example: --symbolize=/system/framework/boot.oat\n"
3534 "\n"
David Srbecky2fdd03c2016-03-10 15:32:37 +00003535 " --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
3536 " .rodata and .text sections are omitted in the output file to save space.\n"
3537 " Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
3538 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003539 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
3540 " Example: --class-filter=com.example.foo\n"
3541 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003542 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
3543 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003544 "\n"
3545 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
3546 " Example: --export-dex-to=/data/local/tmp\n"
3547 "\n"
3548 " --addr2instr=<address>: output matching method disassembled code from relative\n"
3549 " address (e.g. PC from crash dump)\n"
3550 " Example: --addr2instr=0x00001a3b\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003551 "\n"
3552 " --dump-imt=<file.txt>: output IMT collisions (if any) for the given receiver\n"
3553 " types and interface methods in the given file. The file\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003554 " is read line-wise, where each line should either be a class\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003555 " name or descriptor, or a class name/descriptor and a prefix\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003556 " of a complete method name (separated by a whitespace).\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003557 " Example: --dump-imt=imt.txt\n"
3558 "\n"
3559 " --dump-imt-stats: output IMT statistics for the given boot image\n"
3560 " Example: --dump-imt-stats"
Igor Murashkin37743352014-11-13 14:38:00 -08003561 "\n";
3562
3563 return usage;
3564 }
3565
3566 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07003567 const char* oat_filename_ = nullptr;
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003568 const char* dex_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003569 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00003570 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07003571 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003572 std::string elf_filename_prefix_;
Andreas Gampe9fded872016-09-25 16:08:35 -07003573 std::string imt_dump_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003574 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01003575 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003576 bool disassemble_code_ = true;
3577 bool symbolize_ = false;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003578 bool only_keep_debug_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003579 bool list_classes_ = false;
3580 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003581 bool dump_header_only_ = false;
Andreas Gampe9fded872016-09-25 16:08:35 -07003582 bool imt_stat_dump_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003583 uint32_t addr2instr_ = 0;
3584 const char* export_dex_location_ = nullptr;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003585 const char* app_image_ = nullptr;
3586 const char* app_oat_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003587};
3588
Igor Murashkin37743352014-11-13 14:38:00 -08003589struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
Roland Levillainf73caca2018-08-24 17:19:07 +01003590 bool NeedsRuntime() override {
Igor Murashkin37743352014-11-13 14:38:00 -08003591 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003592
Igor Murashkin37743352014-11-13 14:38:00 -08003593 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
3594 bool absolute_addresses = (args_->oat_filename_ == nullptr);
3595
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003596 oat_dumper_options_.reset(new OatDumperOptions(
Igor Murashkin37743352014-11-13 14:38:00 -08003597 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01003598 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08003599 args_->disassemble_code_,
3600 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003601 args_->class_filter_,
3602 args_->method_filter_,
3603 args_->list_classes_,
3604 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00003605 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003606 args_->export_dex_location_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003607 args_->app_image_,
3608 args_->app_oat_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003609 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08003610
Andreas Gampe9fded872016-09-25 16:08:35 -07003611 return (args_->boot_image_location_ != nullptr ||
3612 args_->image_location_ != nullptr ||
3613 !args_->imt_dump_.empty()) &&
Igor Murashkin37743352014-11-13 14:38:00 -08003614 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003615 }
3616
Roland Levillainf73caca2018-08-24 17:19:07 +01003617 bool ExecuteWithoutRuntime() override {
Igor Murashkin37743352014-11-13 14:38:00 -08003618 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08003619 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003620
Mathieu Chartierd424d082014-10-15 10:31:46 -07003621 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08003622
Andreas Gampec24f3992014-12-17 20:40:11 -08003623 if (args_->symbolize_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00003624 // ELF has special kind of section called SHT_NOBITS which allows us to create
3625 // sections which exist but their data is omitted from the ELF file to save space.
3626 // This is what "strip --only-keep-debug" does when it creates separate ELF file
3627 // with only debug data. We use it in similar way to exclude .rodata and .text.
3628 bool no_bits = args_->only_keep_debug_;
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003629 return SymbolizeOat(args_->oat_filename_, args_->dex_filename_, args_->output_name_, no_bits)
3630 == EXIT_SUCCESS;
Andreas Gampec24f3992014-12-17 20:40:11 -08003631 } else {
3632 return DumpOat(nullptr,
3633 args_->oat_filename_,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003634 args_->dex_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003635 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08003636 args_->os_) == EXIT_SUCCESS;
3637 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07003638 }
3639
Andreas Gampefa6a1b02018-09-07 08:11:55 -07003640 bool ExecuteWithRuntime(Runtime* runtime) override {
Igor Murashkin37743352014-11-13 14:38:00 -08003641 CHECK(args_ != nullptr);
3642
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003643 if (!args_->imt_dump_.empty() || args_->imt_stat_dump_) {
3644 return IMTDumper::Dump(runtime,
3645 args_->imt_dump_,
3646 args_->imt_stat_dump_,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003647 args_->oat_filename_,
3648 args_->dex_filename_);
Andreas Gampe9fded872016-09-25 16:08:35 -07003649 }
3650
Igor Murashkin37743352014-11-13 14:38:00 -08003651 if (args_->oat_filename_ != nullptr) {
3652 return DumpOat(runtime,
3653 args_->oat_filename_,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003654 args_->dex_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003655 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08003656 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003657 }
Igor Murashkin37743352014-11-13 14:38:00 -08003658
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003659 return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003660 }
3661
Igor Murashkin37743352014-11-13 14:38:00 -08003662 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
3663};
Andreas Gampe00b25f32014-09-17 21:49:05 -07003664
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003665} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07003666
3667int main(int argc, char** argv) {
Andreas Gampe221d9812018-01-22 17:48:56 -08003668 // Output all logging to stderr.
3669 android::base::SetLogger(android::base::StderrLogger);
3670
Igor Murashkin37743352014-11-13 14:38:00 -08003671 art::OatdumpMain main;
3672 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07003673}