blob: df31ded81235d2f19c6722618942df73d63d1967 [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 Srbecky81b1d782021-03-07 21:33:28 +000042#include "base/stats-inl.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 Marko5868ada2020-05-12 11:50:34 +010047#include "class_root-inl.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 Markofa458ac2020-02-12 14:08:07 +0000199 DO_TRAMPOLINE(JniDlsymLookupCriticalTrampoline);
Vladimir Marko1b404a82017-09-01 13:35:26 +0100200 DO_TRAMPOLINE(QuickGenericJniTrampoline);
201 DO_TRAMPOLINE(QuickImtConflictTrampoline);
202 DO_TRAMPOLINE(QuickResolutionTrampoline);
203 DO_TRAMPOLINE(QuickToInterpreterBridge);
Nicolas Geoffrayc39af942021-01-25 08:43:57 +0000204 DO_TRAMPOLINE(NterpTrampoline);
Vladimir Marko1b404a82017-09-01 13:35:26 +0100205 #undef DO_TRAMPOLINE
206
David Srbecky5d950762016-03-07 20:47:29 +0000207 Walk();
Vladimir Marko1b404a82017-09-01 13:35:26 +0100208
209 // TODO: Try to symbolize link-time thunks?
210 // This would require disassembling all methods to find branches outside the method code.
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700211
David Srbecky32210b92017-12-04 14:39:21 +0000212 // TODO: Add symbols for dex bytecode in the .dex section.
213
214 debug::DebugInfo debug_info{};
215 debug_info.compiled_methods = ArrayRef<const debug::MethodDebugInfo>(method_debug_infos_);
216
David Srbecky7370d922019-02-12 14:00:30 +0000217 debug::WriteDebugInfo(builder_.get(), debug_info);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700218
David Srbecky6d8c8f02015-10-26 10:57:09 +0000219 builder_->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700220
Andreas Gampe08c277c2017-04-26 22:22:15 -0700221 bool ret_value = builder_->Good();
222
223 builder_.reset();
224 output_stream.reset();
225
226 if (elf_file->FlushCloseOrErase() != 0) {
227 return false;
228 }
229 elf_file.reset();
230
231 return ret_value;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700232 }
233
David Srbecky5d950762016-03-07 20:47:29 +0000234 void Walk() {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700235 std::vector<const OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700236 for (size_t i = 0; i < oat_dex_files.size(); i++) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700237 const OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700238 CHECK(oat_dex_file != nullptr);
David Srbecky5d950762016-03-07 20:47:29 +0000239 WalkOatDexFile(oat_dex_file);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700240 }
241 }
242
Andreas Gampeb40d3612018-06-26 15:49:42 -0700243 void WalkOatDexFile(const OatDexFile* oat_dex_file) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700244 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700245 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
246 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700247 return;
248 }
249 for (size_t class_def_index = 0;
250 class_def_index < dex_file->NumClassDefs();
251 class_def_index++) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700252 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
253 OatClassType type = oat_class.GetType();
254 switch (type) {
Vladimir Marko3d76ebe2021-04-19 15:07:50 +0000255 case OatClassType::kAllCompiled:
256 case OatClassType::kSomeCompiled:
David Srbecky5d950762016-03-07 20:47:29 +0000257 WalkOatClass(oat_class, *dex_file, class_def_index);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700258 break;
259
Vladimir Marko3d76ebe2021-04-19 15:07:50 +0000260 case OatClassType::kNoneCompiled:
261 case OatClassType::kOatClassMax:
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700262 // Ignore.
263 break;
264 }
265 }
266 }
267
David Srbecky5d950762016-03-07 20:47:29 +0000268 void WalkOatClass(const OatFile::OatClass& oat_class,
269 const DexFile& dex_file,
270 uint32_t class_def_index) {
Mathieu Chartier18e26872018-06-04 17:19:02 -0700271 ClassAccessor accessor(dex_file, class_def_index);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700272 // Note: even if this is an interface or a native class, we still have to walk it, as there
273 // might be a static initializer.
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700274 uint32_t class_method_idx = 0;
Mathieu Chartier3d092992018-05-24 13:36:23 -0700275 for (const ClassAccessor::Method& method : accessor.GetMethods()) {
David Srbecky5d950762016-03-07 20:47:29 +0000276 WalkOatMethod(oat_class.GetOatMethod(class_method_idx++),
277 dex_file,
278 class_def_index,
Mathieu Chartier3d092992018-05-24 13:36:23 -0700279 method.GetIndex(),
280 method.GetCodeItem(),
281 method.GetAccessFlags());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700282 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700283 }
284
David Srbecky5d950762016-03-07 20:47:29 +0000285 void WalkOatMethod(const OatFile::OatMethod& oat_method,
286 const DexFile& dex_file,
287 uint32_t class_def_index,
288 uint32_t dex_method_index,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800289 const dex::CodeItem* code_item,
David Srbecky5d950762016-03-07 20:47:29 +0000290 uint32_t method_access_flags) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700291 if ((method_access_flags & kAccAbstract) != 0) {
292 // Abstract method, no code.
293 return;
294 }
David Srbecky5d950762016-03-07 20:47:29 +0000295 const OatHeader& oat_header = oat_file_->GetOatHeader();
296 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
297 if (method_header == nullptr || method_header->GetCodeSize() == 0) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700298 // No code.
299 return;
300 }
301
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100302 uint32_t entry_point = oat_method.GetCodeOffset() - oat_header.GetExecutableOffset();
303 // Clear Thumb2 bit.
304 const void* code_address = EntryPointToCodePointer(reinterpret_cast<void*>(entry_point));
305
Vladimir Marko1b404a82017-09-01 13:35:26 +0100306 debug::MethodDebugInfo info = {};
David Srbeckyc684f332018-01-19 17:38:06 +0000307 DCHECK(info.custom_name.empty());
David Srbecky5d950762016-03-07 20:47:29 +0000308 info.dex_file = &dex_file;
309 info.class_def_index = class_def_index;
310 info.dex_method_index = dex_method_index;
311 info.access_flags = method_access_flags;
312 info.code_item = code_item;
313 info.isa = oat_header.GetInstructionSet();
314 info.deduped = !seen_offsets_.insert(oat_method.GetCodeOffset()).second;
315 info.is_native_debuggable = oat_header.IsNativeDebuggable();
316 info.is_optimized = method_header->IsOptimized();
317 info.is_code_address_text_relative = true;
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100318 info.code_address = reinterpret_cast<uintptr_t>(code_address);
David Srbecky5d950762016-03-07 20:47:29 +0000319 info.code_size = method_header->GetCodeSize();
320 info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
321 info.code_info = info.is_optimized ? method_header->GetOptimizedCodeInfoPtr() : nullptr;
322 info.cfi = ArrayRef<uint8_t>();
323 method_debug_infos_.push_back(info);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700324 }
325
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700326 private:
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700327 const OatFile* oat_file_;
David Srbecky2faab002019-02-12 16:35:48 +0000328 std::unique_ptr<ElfBuilder<ElfTypes>> builder_;
David Srbecky5d950762016-03-07 20:47:29 +0000329 std::vector<debug::MethodDebugInfo> method_debug_infos_;
330 std::unordered_set<uint32_t> seen_offsets_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700331 const std::string output_name_;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000332 bool no_bits_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700333};
334
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700335class OatDumperOptions {
336 public:
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100337 OatDumperOptions(bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100338 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700339 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700340 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800341 const char* class_filter,
342 const char* method_filter,
343 bool list_classes,
344 bool list_methods,
David Brazdilc03d7b62016-03-02 12:18:03 +0000345 bool dump_header_only,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800346 const char* export_dex_location,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800347 const char* app_image,
348 const char* app_oat,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800349 uint32_t addr2instr)
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100350 : dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100351 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700352 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700353 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800354 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000355 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800356 list_classes_(list_classes),
357 list_methods_(list_methods),
David Brazdilc03d7b62016-03-02 12:18:03 +0000358 dump_header_only_(dump_header_only),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800359 export_dex_location_(export_dex_location),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800360 app_image_(app_image),
361 app_oat_(app_oat),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800362 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800363 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700364
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700365 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100366 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700367 const bool disassemble_code_;
368 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800369 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000370 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800371 const bool list_classes_;
372 const bool list_methods_;
David Brazdilc03d7b62016-03-02 12:18:03 +0000373 const bool dump_header_only_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800374 const char* const export_dex_location_;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800375 const char* const app_image_;
376 const char* const app_oat_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800377 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700378 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700379};
380
Elliott Hughese3c845c2012-02-28 17:23:01 -0800381class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700382 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100383 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000384 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800385 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700386 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800387 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800388 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
389 disassembler_(Disassembler::Create(instruction_set_,
Andreas Gampe372f3a32016-08-19 10:49:06 -0700390 new DisassemblerOptions(
391 options_.absolute_addresses_,
392 oat_file.Begin(),
393 oat_file.End(),
Andreas Gampe9b031f72018-10-04 11:03:34 -0700394 /* can_read_literals_= */ true,
Andreas Gampe372f3a32016-08-19 10:49:06 -0700395 Is64BitInstructionSet(instruction_set_)
396 ? &Thread::DumpThreadOffset<PointerSize::k64>
397 : &Thread::DumpThreadOffset<PointerSize::k32>))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800398 CHECK(options_.class_loader_ != nullptr);
399 CHECK(options_.class_filter_ != nullptr);
400 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800401 AddAllOffsets();
402 }
403
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700404 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700405 delete disassembler_;
406 }
407
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800408 InstructionSet GetInstructionSet() {
409 return instruction_set_;
410 }
411
Andreas Gampec55bb392018-09-21 00:02:02 +0000412 using DexFileUniqV = std::vector<std::unique_ptr<const DexFile>>;
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300413
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700414 bool Dump(std::ostream& os) {
415 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800416 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700417
418 os << "MAGIC:\n";
419 os << oat_header.GetMagic() << "\n\n";
420
Jeff Hao4b07a6e2016-01-15 12:50:44 -0800421 os << "LOCATION:\n";
422 os << oat_file_.GetLocation() << "\n\n";
423
Brian Carlstromaded5f72011-10-07 17:15:04 -0700424 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800425 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700426
Elliott Hughesa72ec822012-03-05 17:12:22 -0800427 os << "INSTRUCTION SET:\n";
428 os << oat_header.GetInstructionSet() << "\n\n";
429
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700430 {
431 std::unique_ptr<const InstructionSetFeatures> features(
432 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
433 oat_header.GetInstructionSetFeaturesBitmap()));
434 os << "INSTRUCTION SET FEATURES:\n";
435 os << features->GetFeatureString() << "\n\n";
436 }
Dave Allison70202782013-10-22 17:52:19 -0700437
Brian Carlstromaded5f72011-10-07 17:15:04 -0700438 os << "DEX FILE COUNT:\n";
439 os << oat_header.GetDexFileCount() << "\n\n";
440
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800441#define DUMP_OAT_HEADER_OFFSET(label, offset) \
442 os << label " OFFSET:\n"; \
443 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800444 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800445 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
446 } \
447 os << StringPrintf("\n\n");
448
449 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
Vladimir Marko7dac8642019-11-06 17:09:30 +0000450 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP TRAMPOLINE",
451 GetJniDlsymLookupTrampolineOffset);
Vladimir Markofa458ac2020-02-12 14:08:07 +0000452 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP CRITICAL TRAMPOLINE",
453 GetJniDlsymLookupCriticalTrampolineOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800454 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
455 GetQuickGenericJniTrampolineOffset);
456 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
457 GetQuickImtConflictTrampolineOffset);
458 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
459 GetQuickResolutionTrampolineOffset);
460 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
461 GetQuickToInterpreterBridgeOffset);
Nicolas Geoffrayc39af942021-01-25 08:43:57 +0000462 DUMP_OAT_HEADER_OFFSET("NTERP_TRAMPOLINE",
463 GetNterpTrampolineOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800464#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700465
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700466 // Print the key-value store.
467 {
468 os << "KEY VALUE STORE:\n";
469 size_t index = 0;
470 const char* key;
471 const char* value;
472 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
473 os << key << " = " << value << "\n";
474 index++;
475 }
476 os << "\n";
477 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700478
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800479 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700480 os << "BEGIN:\n";
481 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700482
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700483 os << "END:\n";
484 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
485 }
486
487 os << "SIZE:\n";
488 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700489
490 os << std::flush;
491
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800492 // If set, adjust relative address to be searched
493 if (options_.addr2instr_ != 0) {
494 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
495 os << "SEARCH ADDRESS (executable offset + input):\n";
496 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
497 }
498
Vladimir Marko812fb4d2018-03-14 13:07:21 +0000499 // Dump .data.bimg.rel.ro entries.
500 DumpDataBimgRelRoEntries(os);
501
502 // Dump .bss summary, individual entries are dumped per dex file.
503 os << ".bss: ";
504 if (oat_file_.GetBssMethods().empty() && oat_file_.GetBssGcRoots().empty()) {
505 os << "empty.\n\n";
506 } else {
507 os << oat_file_.GetBssMethods().size() << " methods, ";
508 os << oat_file_.GetBssGcRoots().size() << " GC roots.\n\n";
509 }
510
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700511 // Dumping the dex file overview is compact enough to do even if header only.
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700512 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700513 const OatDexFile* oat_dex_file = oat_dex_files_[i];
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700514 CHECK(oat_dex_file != nullptr);
515 std::string error_msg;
516 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
517 if (dex_file == nullptr) {
518 os << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation() << "': "
519 << error_msg;
520 continue;
521 }
Mathieu Chartierba4c1182018-05-24 14:05:55 -0700522
Mathieu Chartier120aa282017-08-05 16:03:03 -0700523 const DexLayoutSections* const layout_sections = oat_dex_file->GetDexLayoutSections();
524 if (layout_sections != nullptr) {
525 os << "Layout data\n";
526 os << *layout_sections;
527 os << "\n";
528 }
529
Vladimir Markobacb8e42018-06-13 14:45:30 +0100530 if (!options_.dump_header_only_) {
531 // Dump .bss entries.
532 DumpBssEntries(
533 os,
534 "ArtMethod",
535 oat_dex_file->GetMethodBssMapping(),
536 dex_file->NumMethodIds(),
537 static_cast<size_t>(GetInstructionSetPointerSize(instruction_set_)),
538 [=](uint32_t index) { return dex_file->PrettyMethod(index); });
539 DumpBssEntries(
540 os,
541 "Class",
542 oat_dex_file->GetTypeBssMapping(),
543 dex_file->NumTypeIds(),
544 sizeof(GcRoot<mirror::Class>),
545 [=](uint32_t index) { return dex_file->PrettyType(dex::TypeIndex(index)); });
546 DumpBssEntries(
547 os,
Vladimir Markobaade402020-09-30 14:45:39 +0000548 "Public Class",
549 oat_dex_file->GetPublicTypeBssMapping(),
550 dex_file->NumTypeIds(),
551 sizeof(GcRoot<mirror::Class>),
552 [=](uint32_t index) { return dex_file->PrettyType(dex::TypeIndex(index)); });
553 DumpBssEntries(
554 os,
555 "Package Class",
556 oat_dex_file->GetPackageTypeBssMapping(),
557 dex_file->NumTypeIds(),
558 sizeof(GcRoot<mirror::Class>),
559 [=](uint32_t index) { return dex_file->PrettyType(dex::TypeIndex(index)); });
560 DumpBssEntries(
561 os,
Vladimir Markobacb8e42018-06-13 14:45:30 +0100562 "String",
563 oat_dex_file->GetStringBssMapping(),
564 dex_file->NumStringIds(),
565 sizeof(GcRoot<mirror::Class>),
566 [=](uint32_t index) { return dex_file->StringDataByIdx(dex::StringIndex(index)); });
567 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700568 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700569
David Brazdilc03d7b62016-03-02 12:18:03 +0000570 if (!options_.dump_header_only_) {
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000571 VariableIndentationOutputStream vios(&os);
Nicolas Geoffraya129d8a2021-03-18 22:23:04 +0000572 VdexFile::VdexFileHeader vdex_header = oat_file_.GetVdexFile()->GetVdexFileHeader();
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000573 if (vdex_header.IsValid()) {
574 std::string error_msg;
575 std::vector<const DexFile*> dex_files;
576 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
577 const DexFile* dex_file = OpenDexFile(oat_dex_files_[i], &error_msg);
578 if (dex_file == nullptr) {
579 os << "Error opening dex file: " << error_msg << std::endl;
580 return false;
581 }
582 dex_files.push_back(dex_file);
583 }
Vladimir Markoc3908792020-04-06 14:52:04 +0100584 verifier::VerifierDeps deps(dex_files, /*output_only=*/ false);
585 if (!deps.ParseStoredData(dex_files, oat_file_.GetVdexFile()->GetVerifierDepsData())) {
586 os << "Error parsing verifier dependencies." << std::endl;
587 return false;
588 }
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000589 deps.Dump(&vios);
590 } else {
591 os << "UNRECOGNIZED vdex file, magic "
592 << vdex_header.GetMagic()
Nicolas Geoffraya129d8a2021-03-18 22:23:04 +0000593 << ", version "
594 << vdex_header.GetVdexVersion()
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000595 << "\n";
596 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000597 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700598 const OatDexFile* oat_dex_file = oat_dex_files_[i];
David Brazdilc03d7b62016-03-02 12:18:03 +0000599 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300600 if (!DumpOatDexFile(os, *oat_dex_file)) {
601 success = false;
602 }
603 }
604 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800605
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300606 if (options_.export_dex_location_) {
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000607 std::string error_msg;
608 std::string vdex_filename = GetVdexFilename(oat_file_.GetLocation());
609 if (!OS::FileExists(vdex_filename.c_str())) {
610 os << "File " << vdex_filename.c_str() << " does not exist\n";
611 return false;
612 }
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300613
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000614 DexFileUniqV vdex_dex_files;
Nicolas Geoffraya129d8a2021-03-18 22:23:04 +0000615 std::unique_ptr<const VdexFile> vdex_file = OpenVdex(vdex_filename,
616 &vdex_dex_files,
617 &error_msg);
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000618 if (vdex_file.get() == nullptr) {
619 os << "Failed to open vdex file: " << error_msg << "\n";
620 return false;
621 }
622 if (oat_dex_files_.size() != vdex_dex_files.size()) {
623 os << "Dex files number in Vdex file does not match Dex files number in Oat file: "
624 << vdex_dex_files.size() << " vs " << oat_dex_files_.size() << '\n';
625 return false;
626 }
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300627
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000628 size_t i = 0;
629 for (const auto& vdex_dex_file : vdex_dex_files) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700630 const OatDexFile* oat_dex_file = oat_dex_files_[i];
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000631 CHECK(oat_dex_file != nullptr);
632 CHECK(vdex_dex_file != nullptr);
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300633
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300634 // If a CompactDex file is detected within a Vdex container, DexLayout is used to convert
635 // back to a StandardDex file. Since the converted DexFile will most likely not reproduce
636 // the original input Dex file, the `update_checksum_` option is used to recompute the
637 // checksum. If the vdex container does not contain cdex resources (`used_dexlayout` is
638 // false), ExportDexFile() enforces a reproducible checksum verification.
639 if (vdex_dex_file->IsCompactDexFile()) {
640 Options options;
641 options.compact_dex_level_ = CompactDexLevel::kCompactDexLevelNone;
642 options.update_checksum_ = true;
Andreas Gampe9b031f72018-10-04 11:03:34 -0700643 DexLayout dex_layout(options, /*info=*/ nullptr, /*out_file=*/ nullptr, /*header=*/ nullptr);
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300644 std::unique_ptr<art::DexContainer> dex_container;
645 bool result = dex_layout.ProcessDexFile(vdex_dex_file->GetLocation().c_str(),
646 vdex_dex_file.get(),
647 i,
648 &dex_container,
649 &error_msg);
650 if (!result) {
651 os << "DexLayout failed to process Dex file: " + error_msg;
652 success = false;
653 break;
654 }
655 DexContainer::Section* main_section = dex_container->GetMainSection();
656 CHECK_EQ(dex_container->GetDataSection()->Size(), 0u);
657
658 const ArtDexFileLoader dex_file_loader;
659 std::unique_ptr<const DexFile> dex(dex_file_loader.Open(
660 main_section->Begin(),
661 main_section->Size(),
662 vdex_dex_file->GetLocation(),
663 vdex_file->GetLocationChecksum(i),
Andreas Gampe9b031f72018-10-04 11:03:34 -0700664 /*oat_dex_file=*/ nullptr,
665 /*verify=*/ false,
666 /*verify_checksum=*/ true,
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300667 &error_msg));
668 if (dex == nullptr) {
669 os << "Failed to load DexFile from layout container: " + error_msg;
670 success = false;
671 break;
672 }
673 if (dex->IsCompactDexFile()) {
674 os <<"CompactDex conversion to StandardDex failed";
675 success = false;
676 break;
677 }
678
Andreas Gampe9b031f72018-10-04 11:03:34 -0700679 if (!ExportDexFile(os, *oat_dex_file, dex.get(), /*used_dexlayout=*/ true)) {
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300680 success = false;
681 break;
682 }
683 } else {
Andreas Gampe9b031f72018-10-04 11:03:34 -0700684 if (!ExportDexFile(os, *oat_dex_file, vdex_dex_file.get(), /*used_dexlayout=*/ false)) {
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300685 success = false;
686 break;
687 }
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300688 }
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000689 i++;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700690 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700691 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000692
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800693 {
694 os << "OAT FILE STATS:\n";
695 VariableIndentationOutputStream vios(&os);
David Srbecky86decb62018-06-05 06:41:10 +0100696 stats_.AddBytes(oat_file_.Size());
David Srbecky81b1d782021-03-07 21:33:28 +0000697 stats_.DumpSizes(vios, "OatFile");
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800698 }
699
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700700 os << std::flush;
701 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700702 }
703
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800704 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700705 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
706 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800707 return 0; // Address not in oat file
708 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800709 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
710 reinterpret_cast<uintptr_t>(oat_file_.Begin());
711 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800712 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800713 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800714 return end_offset - begin_offset;
715 }
716
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800717 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700718 return oat_file_.GetOatHeader().GetInstructionSet();
719 }
720
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700721 const void* GetQuickOatCode(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800722 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700723 const OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700724 CHECK(oat_dex_file != nullptr);
725 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700726 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
727 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700728 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
729 << "': " << error_msg;
730 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800731 const char* descriptor = m->GetDeclaringClassDescriptor();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800732 const dex::ClassDef* class_def =
David Sehr9aa352e2016-09-15 18:13:52 -0700733 OatDexFile::FindClassDef(*dex_file, descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700734 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700735 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100736 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Vladimir Markoe00e2012019-05-14 14:42:02 +0100737 uint32_t oat_method_index;
738 if (m->IsStatic() || m->IsDirect()) {
739 // Simple case where the oat method index was stashed at load time.
740 oat_method_index = m->GetMethodIndex();
741 } else {
742 // Compute the oat_method_index by search for its position in the class def.
743 ClassAccessor accessor(*dex_file, *class_def);
744 oat_method_index = accessor.NumDirectMethods();
745 bool found_virtual = false;
746 for (ClassAccessor::Method dex_method : accessor.GetVirtualMethods()) {
747 // Check method index instead of identity in case of duplicate method definitions.
748 if (dex_method.GetIndex() == m->GetDexMethodIndex()) {
749 found_virtual = true;
750 break;
751 }
752 ++oat_method_index;
753 }
754 CHECK(found_virtual) << "Didn't find oat method index for virtual method: "
755 << dex_file->PrettyMethod(m->GetDexMethodIndex());
756 }
757 return oat_class.GetOatMethod(oat_method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800758 }
759 }
760 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700761 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800762 }
763
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300764 // Returns nullptr and updates error_msg if the Vdex file cannot be opened, otherwise all Dex
Nicolas Geoffraya129d8a2021-03-18 22:23:04 +0000765 // files are stored in dex_files.
766 std::unique_ptr<const VdexFile> OpenVdex(const std::string& vdex_filename,
767 /* out */ DexFileUniqV* dex_files,
768 /* out */ std::string* error_msg) {
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300769 std::unique_ptr<const File> file(OS::OpenFileForReading(vdex_filename.c_str()));
770 if (file == nullptr) {
771 *error_msg = "Could not open file " + vdex_filename + " for reading.";
772 return nullptr;
773 }
774
775 int64_t vdex_length = file->GetLength();
776 if (vdex_length == -1) {
777 *error_msg = "Could not read the length of file " + vdex_filename;
778 return nullptr;
779 }
780
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100781 MemMap mmap = MemMap::MapFile(
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300782 file->GetLength(),
783 PROT_READ | PROT_WRITE,
784 MAP_PRIVATE,
785 file->Fd(),
Andreas Gampe9b031f72018-10-04 11:03:34 -0700786 /* start offset= */ 0,
787 /* low_4gb= */ false,
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300788 vdex_filename.c_str(),
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100789 error_msg);
790 if (!mmap.IsValid()) {
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300791 *error_msg = "Failed to mmap file " + vdex_filename + ": " + *error_msg;
792 return nullptr;
793 }
794
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100795 std::unique_ptr<VdexFile> vdex_file(new VdexFile(std::move(mmap)));
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300796 if (!vdex_file->IsValid()) {
797 *error_msg = "Vdex file is not valid";
798 return nullptr;
799 }
800
801 DexFileUniqV tmp_dex_files;
802 if (!vdex_file->OpenAllDexFiles(&tmp_dex_files, error_msg)) {
803 *error_msg = "Failed to open Dex files from Vdex: " + *error_msg;
804 return nullptr;
805 }
806
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300807 *dex_files = std::move(tmp_dex_files);
808 return vdex_file;
809 }
810
David Srbecky86decb62018-06-05 06:41:10 +0100811 bool AddStatsObject(const void* address) {
812 return seen_stats_objects_.insert(address).second; // Inserted new entry.
813 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800814
Brian Carlstromaded5f72011-10-07 17:15:04 -0700815 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800816 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800817 // We don't know the length of the code for each method, but we need to know where to stop
818 // when disassembling. What we do know is that a region of code will be followed by some other
819 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
820 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800821 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
Andreas Gampeb40d3612018-06-26 15:49:42 -0700822 const OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700823 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700824 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700825 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
826 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700827 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
828 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800829 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800830 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800831 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Mathieu Chartier3d092992018-05-24 13:36:23 -0700832 for (ClassAccessor accessor : dex_file->GetClasses()) {
Mathieu Chartier18e26872018-06-04 17:19:02 -0700833 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(accessor.GetClassDefIndex());
Mathieu Chartier3d092992018-05-24 13:36:23 -0700834 for (uint32_t class_method_index = 0;
835 class_method_index < accessor.NumMethods();
836 ++class_method_index) {
837 AddOffsets(oat_class.GetOatMethod(class_method_index));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800838 }
839 }
840 }
841
842 // If the last thing in the file is code for a method, there won't be an offset for the "next"
843 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
844 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800845 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800846 }
847
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700848 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
849 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
850 }
851
Elliott Hughese3c845c2012-02-28 17:23:01 -0800852 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800853 uint32_t code_offset = oat_method.GetCodeOffset();
Vladimir Marko33bff252017-11-01 14:35:42 +0000854 if (oat_file_.GetOatHeader().GetInstructionSet() == InstructionSet::kThumb2) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800855 code_offset &= ~0x1;
856 }
857 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800858 offsets_.insert(oat_method.GetVmapTableOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800859 }
860
Andreas Gampeb40d3612018-06-26 15:49:42 -0700861 bool DumpOatDexFile(std::ostream& os, const OatDexFile& oat_dex_file) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700862 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800863 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700864 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800865 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800866 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700867
Andreas Gampe2ba88952016-04-29 17:52:07 -0700868 const uint8_t* const oat_file_begin = oat_dex_file.GetOatFile()->Begin();
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000869 if (oat_dex_file.GetOatFile()->ContainsDexCode()) {
870 const uint8_t* const vdex_file_begin = oat_dex_file.GetOatFile()->DexBegin();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100871
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000872 // Print data range of the dex file embedded inside the corresponding vdex file.
873 const uint8_t* const dex_file_pointer = oat_dex_file.GetDexFilePointer();
874 uint32_t dex_offset = dchecked_integral_cast<uint32_t>(dex_file_pointer - vdex_file_begin);
875 os << StringPrintf(
876 "dex-file: 0x%08x..0x%08x\n",
877 dex_offset,
878 dchecked_integral_cast<uint32_t>(dex_offset + oat_dex_file.FileSize() - 1));
879 } else {
880 os << StringPrintf("dex-file not in VDEX file\n");
881 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700882
Andreas Gampe2ba88952016-04-29 17:52:07 -0700883 // Create the dex file early. A lot of print-out things depend on it.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700884 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700885 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
886 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700887 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700888 os << std::flush;
889 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700890 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100891
Andreas Gampe2ba88952016-04-29 17:52:07 -0700892 // Print lookup table, if it exists.
893 if (oat_dex_file.GetLookupTableData() != nullptr) {
894 uint32_t table_offset = dchecked_integral_cast<uint32_t>(
895 oat_dex_file.GetLookupTableData() - oat_file_begin);
David Sehr9aa352e2016-09-15 18:13:52 -0700896 uint32_t table_size = TypeLookupTable::RawDataLength(dex_file->NumClassDefs());
Andreas Gampe2ba88952016-04-29 17:52:07 -0700897 os << StringPrintf("type-table: 0x%08x..0x%08x\n",
898 table_offset,
899 table_offset + table_size - 1);
900 }
901
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100902 VariableIndentationOutputStream vios(&os);
903 ScopedIndentation indent1(&vios);
Mathieu Chartier98dad402018-06-04 18:00:12 -0700904 for (ClassAccessor accessor : dex_file->GetClasses()) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800905 // TODO: Support regex
Mathieu Chartier98dad402018-06-04 18:00:12 -0700906 const char* descriptor = accessor.GetDescriptor();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800907 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
908 continue;
909 }
910
Mathieu Chartier98dad402018-06-04 18:00:12 -0700911 const uint16_t class_def_index = accessor.GetClassDefIndex();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700912 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100913 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700914 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
Mathieu Chartier98dad402018-06-04 18:00:12 -0700915 static_cast<ssize_t>(class_def_index),
916 descriptor,
917 oat_class_offset,
918 accessor.GetClassIdx().index_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100919 << " (" << oat_class.GetStatus() << ")"
920 << " (" << oat_class.GetType() << ")\n";
921 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700922 if (options_.list_classes_) {
923 continue;
924 }
Mathieu Chartier98dad402018-06-04 18:00:12 -0700925 if (!DumpOatClass(&vios, oat_class, *dex_file, accessor, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700926 success = false;
927 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800928 if (stop_analysis) {
929 os << std::flush;
930 return success;
931 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700932 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700933 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700934 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700935 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700936 }
937
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300938 // Backwards compatible Dex file export. If dex_file is nullptr (valid Vdex file not present) the
939 // Dex resource is extracted from the oat_dex_file and its checksum is repaired since it's not
940 // unquickened. Otherwise the dex_file has been fully unquickened and is expected to verify the
941 // original checksum.
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300942 bool ExportDexFile(std::ostream& os,
943 const OatDexFile& oat_dex_file,
944 const DexFile* dex_file,
945 bool used_dexlayout) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800946 std::string error_msg;
947 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300948
949 // If dex_file (from unquicken or dexlayout) is not available, the output DexFile size is the
950 // same as the one extracted from the Oat container (pre-oreo)
951 size_t fsize = dex_file == nullptr ? oat_dex_file.FileSize() : dex_file->Size();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800952
953 // Some quick checks just in case
954 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
955 os << "Invalid dex file\n";
956 return false;
957 }
958
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300959 if (dex_file == nullptr) {
960 // Exported bytecode is quickened (dex-to-dex transformations present)
961 dex_file = OpenDexFile(&oat_dex_file, &error_msg);
962 if (dex_file == nullptr) {
963 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
964 return false;
965 }
966
967 // Recompute checksum
968 reinterpret_cast<DexFile::Header*>(const_cast<uint8_t*>(dex_file->Begin()))->checksum_ =
969 dex_file->CalculateChecksum();
970 } else {
Anestis Bechtsoudis8583bec2018-09-08 22:32:29 +0300971 // If dexlayout was used to convert CompactDex back to StandardDex, checksum will be updated
972 // due to `update_checksum_` option, otherwise we expect a reproducible checksum.
973 if (!used_dexlayout) {
974 // Vdex unquicken output should match original input bytecode
975 uint32_t orig_checksum =
976 reinterpret_cast<DexFile::Header*>(const_cast<uint8_t*>(dex_file->Begin()))->checksum_;
977 if (orig_checksum != dex_file->CalculateChecksum()) {
978 os << "Unexpected checksum from unquicken dex file '" << dex_file_location << "'\n";
979 return false;
980 }
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300981 }
982 }
983
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800984 // Verify output directory exists
985 if (!OS::DirectoryExists(options_.export_dex_location_)) {
986 // TODO: Extend OS::DirectoryExists if symlink support is required
987 os << options_.export_dex_location_ << " output directory not found or symlink\n";
988 return false;
989 }
990
991 // Beautify path names
992 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
993 return false;
994 }
995
996 std::string dex_orig_name;
997 size_t dex_orig_pos = dex_file_location.rfind('/');
998 if (dex_orig_pos == std::string::npos)
999 dex_orig_name = dex_file_location;
1000 else
1001 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
1002
1003 // A more elegant approach to efficiently name user installed apps is welcome
Andreas Gampef812d8c2017-02-17 10:19:44 -08001004 if (dex_orig_name.size() == 8 &&
1005 dex_orig_name.compare("base.apk") == 0 &&
1006 dex_orig_pos != std::string::npos) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001007 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
1008 size_t apk_orig_pos = dex_file_location.rfind('/');
1009 if (apk_orig_pos != std::string::npos) {
1010 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
1011 }
1012 }
1013
1014 std::string out_dex_path(options_.export_dex_location_);
1015 if (out_dex_path.back() != '/') {
1016 out_dex_path.append("/");
1017 }
1018 out_dex_path.append(dex_orig_name);
1019 out_dex_path.append("_export.dex");
1020 if (out_dex_path.length() > PATH_MAX) {
1021 return false;
1022 }
1023
1024 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
1025 if (file.get() == nullptr) {
1026 os << "Failed to open output dex file " << out_dex_path;
1027 return false;
1028 }
1029
Mathieu Chartier567dc6f2018-04-05 16:37:14 -07001030 bool success = file->WriteFully(dex_file->Begin(), fsize);
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001031 if (!success) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001032 os << "Failed to write dex file";
1033 file->Erase();
1034 return false;
1035 }
1036
1037 if (file->FlushCloseOrErase() != 0) {
1038 os << "Flush and close failed";
1039 return false;
1040 }
1041
1042 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
1043 os << std::flush;
1044
1045 return true;
1046 }
1047
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001048 bool DumpOatClass(VariableIndentationOutputStream* vios,
Mathieu Chartier98dad402018-06-04 18:00:12 -07001049 const OatFile::OatClass& oat_class,
1050 const DexFile& dex_file,
1051 const ClassAccessor& class_accessor,
1052 bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001053 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001054 bool addr_found = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001055 uint32_t class_method_index = 0;
Mathieu Chartier98dad402018-06-04 18:00:12 -07001056 for (const ClassAccessor::Method& method : class_accessor.GetMethods()) {
1057 if (!DumpOatMethod(vios,
1058 dex_file.GetClassDef(class_accessor.GetClassDefIndex()),
1059 class_method_index,
1060 oat_class,
1061 dex_file,
1062 method.GetIndex(),
1063 method.GetCodeItem(),
David Brazdil20c765f2018-10-27 21:45:15 +00001064 method.GetAccessFlags(),
Mathieu Chartier98dad402018-06-04 18:00:12 -07001065 &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001066 success = false;
1067 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001068 if (addr_found) {
1069 *stop_analysis = true;
1070 return success;
1071 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001072 class_method_index++;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001073 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001074 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001075 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001076 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001077
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001078 static constexpr uint32_t kPrologueBytes = 16;
1079
1080 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
1081 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
1082
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001083 bool DumpOatMethod(VariableIndentationOutputStream* vios,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001084 const dex::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -07001085 uint32_t class_method_index,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001086 const OatFile::OatClass& oat_class,
1087 const DexFile& dex_file,
1088 uint32_t dex_method_idx,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001089 const dex::CodeItem* code_item,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001090 uint32_t method_access_flags,
1091 bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001092 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001093
Mathieu Chartier698ebbc2018-01-05 11:00:42 -08001094 CodeItemDataAccessor code_item_accessor(dex_file, code_item);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001095
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001096 // TODO: Support regex
1097 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
1098 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00001099 return success;
1100 }
1101
David Sehr709b0702016-10-13 09:12:37 -07001102 std::string pretty_method = dex_file.PrettyMethod(dex_method_idx, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001103 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
1104 class_method_index, pretty_method.c_str(),
1105 dex_method_idx);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001106 if (options_.list_methods_) {
1107 return success;
1108 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001109
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001110 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
1111 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
1112 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
1113 uint32_t code_offset = oat_method.GetCodeOffset();
1114 uint32_t code_size = oat_method.GetQuickCodeSize();
1115 if (resolved_addr2instr_ != 0) {
1116 if (resolved_addr2instr_ > code_offset + code_size) {
1117 return success;
1118 } else {
1119 *addr_found = true; // stop analyzing file at next iteration
1120 }
1121 }
1122
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001123 // Everything below is indented at least once.
1124 ScopedIndentation indent1(vios);
1125
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001126 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001127 vios->Stream() << "DEX CODE:\n";
1128 ScopedIndentation indent2(vios);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001129 if (code_item_accessor.HasCodeItem()) {
Nicolas Geoffraya9850802021-05-11 11:03:25 +01001130 uint32_t max_pc = code_item_accessor.InsnsSizeInCodeUnits();
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001131 for (const DexInstructionPcPair& inst : code_item_accessor) {
Nicolas Geoffraya9850802021-05-11 11:03:25 +01001132 if (inst.DexPc() + inst->SizeInCodeUnits() > max_pc) {
1133 LOG(WARNING) << "GLITCH: run-away instruction at idx=0x" << std::hex << inst.DexPc();
1134 break;
1135 }
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001136 vios->Stream() << StringPrintf("0x%04x: ", inst.DexPc()) << inst->DumpHexLE(5)
1137 << StringPrintf("\t| %s\n", inst->DumpString(&dex_file).c_str());
1138 }
1139 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001140 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001141
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001142 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001143 std::unique_ptr<verifier::MethodVerifier> verifier;
1144 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001145 // We need to have the handle scope stay live until after the verifier since the verifier has
1146 // a handle to the dex cache from hs.
1147 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001148 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
1149 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001150 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001151 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001152 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -07001153 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001154 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001155 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001156 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001157 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001158 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001159 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001160 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001161 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001162 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
1163 oat_method_offsets_offset, oat_file_.Size());
1164 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001165 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001166 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001167 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001168
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001169 ScopedIndentation indent2(vios);
1170 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001171 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
1172 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001173 vios->Stream() << StringPrintf("WARNING: "
1174 "code offset 0x%08x is past end of file 0x%08zx.\n",
1175 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001176 success = false;
1177 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001178 vios->Stream() << "\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001179 }
1180 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001181 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001182 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
1183 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
David Srbecky86decb62018-06-05 06:41:10 +01001184 if (AddStatsObject(method_header)) {
David Srbecky81b1d782021-03-07 21:33:28 +00001185 stats_["QuickMethodHeader"].AddBytes(sizeof(*method_header));
David Srbecky86decb62018-06-05 06:41:10 +01001186 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001187 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001188 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001189 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001190 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
David Srbecky113d6ea2021-03-02 22:49:46 +00001191 if (method_header_offset > oat_file_.Size() ||
1192 sizeof(OatQuickMethodHeader) > oat_file_.Size() - method_header_offset) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001193 vios->Stream() << StringPrintf(
David Srbecky113d6ea2021-03-02 22:49:46 +00001194 "WARNING: oat quick method header at offset 0x%08x is past end of file 0x%08zx.\n",
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001195 method_header_offset, oat_file_.Size());
1196 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001197 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001198 return false;
1199 }
1200
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001201 ScopedIndentation indent2(vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001202 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001203 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001204 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001205 }
David Srbecky113d6ea2021-03-02 22:49:46 +00001206 uint32_t vmap_table_offset =
1207 (method_header == nullptr) ? 0 : method_header->GetCodeInfoOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001208 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001209
1210 size_t vmap_table_offset_limit =
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +00001211 IsMethodGeneratedByDexToDexCompiler(oat_method, code_item_accessor)
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001212 ? oat_file_.GetVdexFile()->Size()
1213 : method_header->GetCode() - oat_file_.Begin();
1214 if (vmap_table_offset >= vmap_table_offset_limit) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001215 vios->Stream() << StringPrintf("WARNING: "
David Srbecky113d6ea2021-03-02 22:49:46 +00001216 "vmap table offset 0x%08x is past end of file 0x%08zx. ",
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001217 vmap_table_offset,
David Srbecky113d6ea2021-03-02 22:49:46 +00001218 vmap_table_offset_limit);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001219 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001220 } else if (options_.dump_vmap_) {
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001221 DumpVmapData(vios, oat_method, code_item_accessor);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001222 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001223 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001224 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001225 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001226
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001227 ScopedIndentation indent2(vios);
1228 vios->Stream()
1229 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
1230 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
1231 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
1232 vios->Stream() << "\n";
1233 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
1234 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
1235 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001236 }
1237 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001238 // Based on spill masks from QuickMethodFrameInfo so placed
1239 // after it is dumped, but useful for understanding quick
1240 // code, so dumped here.
1241 ScopedIndentation indent2(vios);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001242 DumpVregLocations(vios->Stream(), oat_method, code_item_accessor);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001243 }
1244 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001245 vios->Stream() << "CODE: ";
David Srbecky113d6ea2021-03-02 22:49:46 +00001246 {
Nicolas Geoffray57083762019-03-05 09:24:45 +00001247 const void* code = oat_method.GetQuickCode();
1248 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1249 uint64_t aligned_code_end = aligned_code_begin + code_size;
1250 if (AddStatsObject(code)) {
David Srbecky81b1d782021-03-07 21:33:28 +00001251 stats_["Code"].AddBytes(code_size);
Nicolas Geoffray57083762019-03-05 09:24:45 +00001252 }
1253
1254 if (options_.absolute_addresses_) {
1255 vios->Stream() << StringPrintf("%p ", code);
1256 }
David Srbecky113d6ea2021-03-02 22:49:46 +00001257 vios->Stream() << StringPrintf("(code_offset=0x%08x size=%u)%s\n",
Nicolas Geoffray57083762019-03-05 09:24:45 +00001258 code_offset,
Nicolas Geoffray57083762019-03-05 09:24:45 +00001259 code_size,
1260 code != nullptr ? "..." : "");
1261
1262 ScopedIndentation indent2(vios);
1263 if (aligned_code_begin > oat_file_.Size()) {
1264 vios->Stream() << StringPrintf("WARNING: "
1265 "start of code at 0x%08x is past end of file 0x%08zx.",
1266 aligned_code_begin, oat_file_.Size());
1267 success = false;
1268 } else if (aligned_code_end > oat_file_.Size()) {
1269 vios->Stream() << StringPrintf(
1270 "WARNING: "
1271 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
David Srbecky113d6ea2021-03-02 22:49:46 +00001272 "code size is 0x%08x.\n",
1273 aligned_code_end,
1274 oat_file_.Size(),
1275 code_size);
Nicolas Geoffray57083762019-03-05 09:24:45 +00001276 success = false;
1277 if (options_.disassemble_code_) {
David Srbecky113d6ea2021-03-02 22:49:46 +00001278 if (aligned_code_begin + kPrologueBytes <= oat_file_.Size()) {
Nicolas Geoffray57083762019-03-05 09:24:45 +00001279 DumpCode(vios, oat_method, code_item_accessor, true, kPrologueBytes);
1280 }
1281 }
1282 } else if (code_size > kMaxCodeSize) {
1283 vios->Stream() << StringPrintf(
1284 "WARNING: "
1285 "code size %d is bigger than max expected threshold of %d. "
David Srbecky113d6ea2021-03-02 22:49:46 +00001286 "code size is 0x%08x.\n",
1287 code_size,
1288 kMaxCodeSize,
1289 code_size);
Nicolas Geoffray57083762019-03-05 09:24:45 +00001290 success = false;
1291 if (options_.disassemble_code_) {
David Srbecky113d6ea2021-03-02 22:49:46 +00001292 if (aligned_code_begin + kPrologueBytes <= oat_file_.Size()) {
Nicolas Geoffray57083762019-03-05 09:24:45 +00001293 DumpCode(vios, oat_method, code_item_accessor, true, kPrologueBytes);
1294 }
1295 }
1296 } else if (options_.disassemble_code_) {
1297 DumpCode(vios, oat_method, code_item_accessor, !success, 0);
1298 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001299 }
1300 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001301 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001302 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001303 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001304
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001305 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1306 if (spill_mask == 0) {
1307 return;
1308 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001309 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001310 for (size_t i = 0; i < 32; i++) {
1311 if ((spill_mask & (1 << i)) != 0) {
1312 if (is_float) {
1313 os << "fr" << i;
1314 } else {
1315 os << "r" << i;
1316 }
1317 spill_mask ^= 1 << i; // clear bit
1318 if (spill_mask != 0) {
1319 os << ", ";
1320 } else {
1321 break;
1322 }
1323 }
1324 }
1325 os << ")";
1326 }
1327
Roland Levillain442b46a2015-02-18 16:54:21 +00001328 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001329 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001330 const OatFile::OatMethod& oat_method,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001331 const CodeItemDataAccessor& code_item_accessor) {
1332 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item_accessor)) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001333 // The optimizing compiler outputs its CodeInfo data in the vmap table.
David Srbecky6ee06e92018-07-25 21:45:54 +01001334 const uint8_t* raw_code_info = oat_method.GetVmapTable();
Roland Levillain442b46a2015-02-18 16:54:21 +00001335 if (raw_code_info != nullptr) {
1336 CodeInfo code_info(raw_code_info);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001337 DCHECK(code_item_accessor.HasCodeItem());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001338 ScopedIndentation indent1(vios);
David Srbecky8cd54542018-07-15 23:58:44 +01001339 DumpCodeInfo(vios, code_info, oat_method);
Roland Levillain442b46a2015-02-18 16:54:21 +00001340 }
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001341 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item_accessor)) {
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001342 // We don't encode the size in the table, so just emit that we have quickened
1343 // information.
1344 ScopedIndentation indent(vios);
1345 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001346 } else {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001347 // Otherwise, there is nothing to display.
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001348 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001349 }
1350
1351 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001352 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001353 const CodeInfo& code_info,
David Srbecky8cd54542018-07-15 23:58:44 +01001354 const OatFile::OatMethod& oat_method) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001355 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001356 oat_method.GetCodeOffset(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001357 options_.dump_code_info_stack_maps_,
David Srbecky8cd54542018-07-15 23:58:44 +01001358 instruction_set_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001359 }
1360
Andreas Gampe36a296f2017-06-13 14:11:11 -07001361 static int GetOutVROffset(uint16_t out_num, InstructionSet isa) {
1362 // According to stack model, the first out is above the Method referernce.
1363 return static_cast<size_t>(InstructionSetPointerSize(isa)) + out_num * sizeof(uint32_t);
1364 }
1365
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001366 static uint32_t GetVRegOffsetFromQuickCode(const CodeItemDataAccessor& code_item_accessor,
Andreas Gampe36a296f2017-06-13 14:11:11 -07001367 uint32_t core_spills,
1368 uint32_t fp_spills,
1369 size_t frame_size,
1370 int reg,
1371 InstructionSet isa) {
1372 PointerSize pointer_size = InstructionSetPointerSize(isa);
1373 if (kIsDebugBuild) {
1374 auto* runtime = Runtime::Current();
1375 if (runtime != nullptr) {
1376 CHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), pointer_size);
1377 }
1378 }
1379 DCHECK_ALIGNED(frame_size, kStackAlignment);
1380 DCHECK_NE(reg, -1);
1381 int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa)
1382 + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa)
1383 + sizeof(uint32_t); // Filler.
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001384 int num_regs = code_item_accessor.RegistersSize() - code_item_accessor.InsSize();
1385 int temp_threshold = code_item_accessor.RegistersSize();
Andreas Gampe36a296f2017-06-13 14:11:11 -07001386 const int max_num_special_temps = 1;
1387 if (reg == temp_threshold) {
1388 // The current method pointer corresponds to special location on stack.
1389 return 0;
1390 } else if (reg >= temp_threshold + max_num_special_temps) {
1391 /*
1392 * Special temporaries may have custom locations and the logic above deals with that.
1393 * However, non-special temporaries are placed relative to the outs.
1394 */
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001395 int temps_start = code_item_accessor.OutsSize() * sizeof(uint32_t)
Andreas Gampe36a296f2017-06-13 14:11:11 -07001396 + static_cast<size_t>(pointer_size) /* art method */;
1397 int relative_offset = (reg - (temp_threshold + max_num_special_temps)) * sizeof(uint32_t);
1398 return temps_start + relative_offset;
1399 } else if (reg < num_regs) {
1400 int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t);
1401 return locals_start + (reg * sizeof(uint32_t));
1402 } else {
1403 // Handle ins.
1404 return frame_size + ((reg - num_regs) * sizeof(uint32_t))
1405 + static_cast<size_t>(pointer_size) /* art method */;
1406 }
1407 }
1408
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001409 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001410 const CodeItemDataAccessor& code_item_accessor) {
1411 if (code_item_accessor.HasCodeItem()) {
1412 size_t num_locals_ins = code_item_accessor.RegistersSize();
1413 size_t num_ins = code_item_accessor.InsSize();
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001414 size_t num_locals = num_locals_ins - num_ins;
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001415 size_t num_outs = code_item_accessor.OutsSize();
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001416
1417 os << "vr_stack_locations:";
1418 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1419 // For readability, delimit the different kinds of VRs.
1420 if (reg == num_locals_ins) {
1421 os << "\n\tmethod*:";
1422 } else if (reg == num_locals && num_ins > 0) {
1423 os << "\n\tins:";
1424 } else if (reg == 0 && num_locals > 0) {
1425 os << "\n\tlocals:";
1426 }
1427
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001428 uint32_t offset = GetVRegOffsetFromQuickCode(code_item_accessor,
Andreas Gampe36a296f2017-06-13 14:11:11 -07001429 oat_method.GetCoreSpillMask(),
1430 oat_method.GetFpSpillMask(),
1431 oat_method.GetFrameSizeInBytes(),
1432 reg,
1433 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001434 os << " v" << reg << "[sp + #" << offset << "]";
1435 }
1436
1437 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1438 if (out_reg == 0) {
1439 os << "\n\touts:";
1440 }
1441
Andreas Gampe36a296f2017-06-13 14:11:11 -07001442 uint32_t offset = GetOutVROffset(out_reg, GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001443 os << " v" << out_reg << "[sp + #" << offset << "]";
1444 }
1445
1446 os << "\n";
1447 }
1448 }
1449
Roland Levillainf2650d12015-05-28 14:53:28 +01001450 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1451 // the optimizing compiler?
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001452 static bool IsMethodGeneratedByOptimizingCompiler(
1453 const OatFile::OatMethod& oat_method,
1454 const CodeItemDataAccessor& code_item_accessor) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001455 // If the native GC map is null and the Dex `code_item` is not
1456 // null, then this method has been compiled with the optimizing
1457 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001458 return oat_method.GetQuickCode() != nullptr &&
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001459 oat_method.GetVmapTable() != nullptr &&
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001460 code_item_accessor.HasCodeItem();
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001461 }
1462
1463 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1464 // the dextodex compiler?
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001465 static bool IsMethodGeneratedByDexToDexCompiler(
1466 const OatFile::OatMethod& oat_method,
1467 const CodeItemDataAccessor& code_item_accessor) {
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001468 // If the quick code is null, the Dex `code_item` is not
1469 // null, and the vmap table is not null, then this method has been compiled
1470 // with the dextodex compiler.
1471 return oat_method.GetQuickCode() == nullptr &&
1472 oat_method.GetVmapTable() != nullptr &&
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001473 code_item_accessor.HasCodeItem();
Roland Levillainf2650d12015-05-28 14:53:28 +01001474 }
1475
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001476 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001477 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001478 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001479 const DexFile* dex_file,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001480 const dex::ClassDef& class_def,
1481 const dex::CodeItem* code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001482 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001483 if ((method_access_flags & kAccNative) == 0) {
1484 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001485 Runtime* const runtime = Runtime::Current();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001486 DCHECK(options_.class_loader_ != nullptr);
Vladimir Marko421087b2018-02-27 11:00:17 +00001487 Handle<mirror::DexCache> dex_cache = hs->NewHandle(
1488 runtime->GetClassLinker()->RegisterDexFile(*dex_file, options_.class_loader_->Get()));
1489 CHECK(dex_cache != nullptr);
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00001490 ArtMethod* method = runtime->GetClassLinker()->ResolveMethodWithoutInvokeType(
1491 dex_method_idx, dex_cache, *options_.class_loader_);
Andreas Gampe03da7842018-04-12 11:12:52 -07001492 if (method == nullptr) {
1493 soa.Self()->ClearException();
1494 return nullptr;
1495 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001496 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001497 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
Andreas Gampe9b031f72018-10-04 11:03:34 -07001498 class_def, code_item, method, method_access_flags, /* api_level= */ 0);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001499 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001500
1501 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001502 }
1503
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001504 void DumpCode(VariableIndentationOutputStream* vios,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001505 const OatFile::OatMethod& oat_method,
1506 const CodeItemDataAccessor& code_item_accessor,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001507 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001508 const void* quick_code = oat_method.GetQuickCode();
1509
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001510 if (code_size == 0) {
1511 code_size = oat_method.GetQuickCodeSize();
1512 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001513 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001514 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001515 return;
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001516 } else if (!bad_input && IsMethodGeneratedByOptimizingCompiler(oat_method,
1517 code_item_accessor)) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001518 // The optimizing compiler outputs its CodeInfo data in the vmap table.
David Srbecky634b04a2021-02-23 00:26:35 +00001519 CodeInfo code_info(oat_method.GetVmapTable());
David Srbecky86decb62018-06-05 06:41:10 +01001520 if (AddStatsObject(oat_method.GetVmapTable())) {
David Srbecky634b04a2021-02-23 00:26:35 +00001521 code_info.CollectSizeStats(oat_method.GetVmapTable(), stats_["CodeInfo"]);
David Srbecky86decb62018-06-05 06:41:10 +01001522 }
David Srbecky634b04a2021-02-23 00:26:35 +00001523 std::unordered_map<uint32_t, std::vector<StackMap>> stack_maps;
1524 for (const StackMap& it : code_info.GetStackMaps()) {
1525 stack_maps[it.GetNativePcOffset(instruction_set_)].push_back(it);
1526 }
1527
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001528 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1529 size_t offset = 0;
1530 while (offset < code_size) {
1531 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
David Srbecky634b04a2021-02-23 00:26:35 +00001532 auto it = stack_maps.find(offset);
1533 if (it != stack_maps.end()) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001534 ScopedIndentation indent1(vios);
David Srbecky634b04a2021-02-23 00:26:35 +00001535 for (StackMap stack_map : it->second) {
1536 stack_map.Dump(vios, code_info, oat_method.GetCodeOffset(), instruction_set_);
1537 }
1538 stack_maps.erase(it);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001539 }
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001540 }
David Srbecky634b04a2021-02-23 00:26:35 +00001541 DCHECK_EQ(stack_maps.size(), 0u); // Check that all stack maps have been printed.
Elliott Hughes956af0f2014-12-11 14:34:28 -08001542 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001543 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1544 size_t offset = 0;
1545 while (offset < code_size) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001546 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001547 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001548 }
1549 }
1550
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001551 std::pair<const uint8_t*, const uint8_t*> GetBootImageLiveObjectsDataRange(gc::Heap* heap) const
1552 REQUIRES_SHARED(Locks::mutator_lock_) {
1553 const std::vector<gc::space::ImageSpace*>& boot_image_spaces = heap->GetBootImageSpaces();
1554 const ImageHeader& main_header = boot_image_spaces[0]->GetImageHeader();
1555 ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_live_objects =
1556 ObjPtr<mirror::ObjectArray<mirror::Object>>::DownCast(
1557 main_header.GetImageRoot<kWithoutReadBarrier>(ImageHeader::kBootImageLiveObjects));
1558 DCHECK(boot_image_live_objects != nullptr);
1559 DCHECK(heap->ObjectIsInBootImageSpace(boot_image_live_objects));
1560 const uint8_t* boot_image_live_objects_address =
1561 reinterpret_cast<const uint8_t*>(boot_image_live_objects.Ptr());
1562 uint32_t begin_offset = mirror::ObjectArray<mirror::Object>::OffsetOfElement(0).Uint32Value();
1563 uint32_t end_offset = mirror::ObjectArray<mirror::Object>::OffsetOfElement(
1564 boot_image_live_objects->GetLength()).Uint32Value();
1565 return std::make_pair(boot_image_live_objects_address + begin_offset,
1566 boot_image_live_objects_address + end_offset);
1567 }
1568
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001569 void DumpDataBimgRelRoEntries(std::ostream& os) {
1570 os << ".data.bimg.rel.ro: ";
1571 if (oat_file_.GetBootImageRelocations().empty()) {
1572 os << "empty.\n\n";
1573 return;
1574 }
1575
1576 os << oat_file_.GetBootImageRelocations().size() << " entries.\n";
1577 Runtime* runtime = Runtime::Current();
1578 if (runtime != nullptr && !runtime->GetHeap()->GetBootImageSpaces().empty()) {
1579 const std::vector<gc::space::ImageSpace*>& boot_image_spaces =
1580 runtime->GetHeap()->GetBootImageSpaces();
1581 ScopedObjectAccess soa(Thread::Current());
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001582 auto live_objects = GetBootImageLiveObjectsDataRange(runtime->GetHeap());
1583 const uint8_t* live_objects_begin = live_objects.first;
1584 const uint8_t* live_objects_end = live_objects.second;
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001585 for (const uint32_t& object_offset : oat_file_.GetBootImageRelocations()) {
1586 uint32_t entry_index = &object_offset - oat_file_.GetBootImageRelocations().data();
1587 uint32_t entry_offset = entry_index * sizeof(oat_file_.GetBootImageRelocations()[0]);
1588 os << StringPrintf(" 0x%x: 0x%08x", entry_offset, object_offset);
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001589 uint8_t* address = boot_image_spaces[0]->Begin() + object_offset;
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001590 bool found = false;
1591 for (gc::space::ImageSpace* space : boot_image_spaces) {
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001592 uint64_t local_offset = address - space->Begin();
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001593 if (local_offset < space->GetImageHeader().GetImageSize()) {
1594 if (space->GetImageHeader().GetObjectsSection().Contains(local_offset)) {
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001595 if (address >= live_objects_begin && address < live_objects_end) {
1596 size_t index =
1597 (address - live_objects_begin) / sizeof(mirror::HeapReference<mirror::Object>);
1598 os << StringPrintf(" 0x%08x BootImageLiveObject[%zu]",
Lokesh Gidra44044b12019-02-26 00:10:04 +00001599 object_offset,
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001600 index);
1601 } else {
1602 ObjPtr<mirror::Object> o = reinterpret_cast<mirror::Object*>(address);
1603 if (o->IsString()) {
1604 os << " String: " << o->AsString()->ToModifiedUtf8();
1605 } else if (o->IsClass()) {
1606 os << " Class: " << o->AsClass()->PrettyDescriptor();
1607 } else {
1608 os << StringPrintf(" 0x%08x %s",
1609 object_offset,
1610 o->GetClass()->PrettyDescriptor().c_str());
1611 }
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001612 }
1613 } else if (space->GetImageHeader().GetMethodsSection().Contains(local_offset)) {
Vladimir Markoffe26cc2019-02-26 09:51:56 +00001614 ArtMethod* m = reinterpret_cast<ArtMethod*>(address);
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001615 os << " ArtMethod: " << m->PrettyMethod();
1616 } else {
1617 os << StringPrintf(" 0x%08x <unexpected section in %s>",
1618 object_offset,
1619 space->GetImageFilename().c_str());
1620 }
1621 found = true;
1622 break;
1623 }
1624 }
1625 if (!found) {
1626 os << StringPrintf(" 0x%08x <outside boot image spaces>", object_offset);
1627 }
1628 os << "\n";
1629 }
1630 } else {
1631 for (const uint32_t& object_offset : oat_file_.GetBootImageRelocations()) {
1632 uint32_t entry_index = &object_offset - oat_file_.GetBootImageRelocations().data();
1633 uint32_t entry_offset = entry_index * sizeof(oat_file_.GetBootImageRelocations()[0]);
1634 os << StringPrintf(" 0x%x: 0x%08x\n", entry_offset, object_offset);
1635 }
1636 }
1637 os << "\n";
1638 }
1639
Vladimir Markof3c52b42017-11-17 17:32:12 +00001640 template <typename NameGetter>
1641 void DumpBssEntries(std::ostream& os,
1642 const char* slot_type,
1643 const IndexBssMapping* mapping,
1644 uint32_t number_of_indexes,
1645 size_t slot_size,
1646 NameGetter name) {
1647 os << ".bss mapping for " << slot_type << ": ";
1648 if (mapping == nullptr) {
1649 os << "empty.\n";
1650 return;
1651 }
1652 size_t index_bits = IndexBssMappingEntry::IndexBits(number_of_indexes);
1653 size_t num_valid_indexes = 0u;
1654 for (const IndexBssMappingEntry& entry : *mapping) {
1655 num_valid_indexes += 1u + POPCOUNT(entry.GetMask(index_bits));
1656 }
1657 os << mapping->size() << " entries for " << num_valid_indexes << " valid indexes.\n";
1658 os << std::hex;
1659 for (const IndexBssMappingEntry& entry : *mapping) {
1660 uint32_t index = entry.GetIndex(index_bits);
1661 uint32_t mask = entry.GetMask(index_bits);
1662 size_t bss_offset = entry.bss_offset - POPCOUNT(mask) * slot_size;
1663 for (uint32_t n : LowToHighBits(mask)) {
1664 size_t current_index = index - (32u - index_bits) + n;
1665 os << " 0x" << bss_offset << ": " << slot_type << ": " << name(current_index) << "\n";
1666 bss_offset += slot_size;
1667 }
1668 DCHECK_EQ(bss_offset, entry.bss_offset);
1669 os << " 0x" << bss_offset << ": " << slot_type << ": " << name(index) << "\n";
1670 }
1671 os << std::dec;
1672 }
1673
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001674 const OatFile& oat_file_;
Andreas Gampeb40d3612018-06-26 15:49:42 -07001675 const std::vector<const OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001676 const OatDumperOptions& options_;
1677 uint32_t resolved_addr2instr_;
Andreas Gampe372f3a32016-08-19 10:49:06 -07001678 const InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001679 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001680 Disassembler* disassembler_;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001681 Stats stats_;
David Srbecky86decb62018-06-05 06:41:10 +01001682 std::unordered_set<const void*> seen_stats_objects_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001683};
1684
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001685class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001686 public:
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001687 ImageDumper(std::ostream* os,
1688 gc::space::ImageSpace& image_space,
1689 const ImageHeader& image_header,
1690 OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001691 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001692 vios_(os),
1693 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001694 image_space_(image_space),
1695 image_header_(image_header),
1696 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001697
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001698 bool Dump() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001699 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001700 std::ostream& indent_os = vios_.Stream();
1701
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001702 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001703
Jeff Haodcdc85b2015-12-04 14:06:18 -08001704 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1705
Vladimir Markoc10a0c62018-11-16 11:39:22 +00001706 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n";
1707 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n";
1708 os << "IMAGE CHECKSUM: " << std::hex << image_header_.GetImageChecksum() << std::dec << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001709
Vladimir Markoc10a0c62018-11-16 11:39:22 +00001710 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum()) << "\n";
1711 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n";
1712 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n";
1713 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n";
1714 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
1715
1716 os << "BOOT IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetBootImageBegin())
1717 << "\n";
Vladimir Marko0c78ef72018-11-21 14:09:35 +00001718 os << "BOOT IMAGE SIZE: " << image_header_.GetBootImageSize() << "\n\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001719
1720 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1721 auto section = static_cast<ImageHeader::ImageSections>(i);
1722 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1723 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001724
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001725 {
Vladimir Markoc13fbd82018-06-04 16:16:28 +01001726 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots().Ptr()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001727 static_assert(arraysize(image_roots_descriptions_) ==
1728 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001729 DCHECK_LE(image_header_.GetImageRoots()->GetLength(), ImageHeader::kImageRootsMax);
1730 for (int32_t i = 0, size = image_header_.GetImageRoots()->GetLength(); i != size; ++i) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001731 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1732 const char* image_root_description = image_roots_descriptions_[i];
Vladimir Markoc13fbd82018-06-04 16:16:28 +01001733 ObjPtr<mirror::Object> image_root_object = image_header_.GetImageRoot(image_root);
1734 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object.Ptr());
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001735 if (image_root_object != nullptr && image_root_object->IsObjectArray()) {
Vladimir Markoc13fbd82018-06-04 16:16:28 +01001736 ObjPtr<mirror::ObjectArray<mirror::Object>> image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001737 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001738 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001739 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
Vladimir Marko423bebb2019-03-26 15:17:21 +00001740 ObjPtr<mirror::Object> value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001741 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001742 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1743 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001744 run++;
1745 } else {
1746 break;
1747 }
1748 }
1749 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001750 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001751 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001752 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001753 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001754 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001755 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001756 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001757 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001758 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001759 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001760 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001761 }
1762 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001763 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001764
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001765 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001766 os << "METHOD ROOTS\n";
1767 static_assert(arraysize(image_methods_descriptions_) ==
1768 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1769 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1770 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1771 const char* description = image_methods_descriptions_[i];
1772 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001773 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001774 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001775 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001776 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001777
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001778 Runtime* const runtime = Runtime::Current();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001779 std::string image_filename = image_space_.GetImageFilename();
1780 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001781 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001782 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001783 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001784 const OatFile* oat_file = image_space_.GetOatFile();
Alex Lighta59dd802014-07-02 16:28:08 -07001785 if (oat_file == nullptr) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001786 oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location);
1787 }
1788 if (oat_file == nullptr) {
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001789 oat_file = OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01001790 oat_location,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001791 oat_location,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001792 /*executable=*/ false,
1793 /*low_4gb=*/ false,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001794 &error_msg);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001795 }
1796 if (oat_file == nullptr) {
1797 os << "OAT FILE NOT FOUND: " << error_msg << "\n";
1798 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001799 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001800 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001801
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001802 stats_.oat_file_bytes = oat_file->Size();
David Srbeckybc1748f2021-02-22 10:51:32 +00001803 stats_.oat_file_stats.AddBytes(oat_file->Size());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001804
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001805 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001806
Andreas Gampeb40d3612018-06-26 15:49:42 -07001807 for (const OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001808 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001809 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1810 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001811 }
1812
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001813 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001814
Jeff Haodcdc85b2015-12-04 14:06:18 -08001815 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001816 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001817 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001818 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001819 {
1820 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1821 heap->FlushAllocStack();
1822 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001823 // Since FlushAllocStack() above resets the (active) allocation
1824 // stack. Need to revoke the thread-local allocation stacks that
1825 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001826 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001827 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001828 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001829 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001830 {
Andreas Gampe0c183382017-07-13 22:26:24 -07001831 auto dump_visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
1832 DumpObject(obj);
1833 };
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001834 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001835 // Dump the normal objects before ArtMethods.
Andreas Gampe0c183382017-07-13 22:26:24 -07001836 image_space_.GetLiveBitmap()->Walk(dump_visitor);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001837 indent_os << "\n";
1838 // TODO: Dump fields.
1839 // Dump methods after.
Mathieu Chartier9d5956a2019-03-22 11:29:08 -07001840 image_header_.VisitPackedArtMethods([&](ArtMethod& method)
1841 REQUIRES_SHARED(Locks::mutator_lock_) {
1842 std::ostream& indent_os = vios_.Stream();
1843 indent_os << &method << " " << " ArtMethod: " << method.PrettyMethod() << "\n";
1844 DumpMethod(&method, indent_os);
1845 indent_os << "\n";
1846 }, image_space_.Begin(), image_header_.GetPointerSize());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001847 // Dump the large objects separately.
Andreas Gampe0c183382017-07-13 22:26:24 -07001848 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(dump_visitor);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001849 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001850 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001851 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001852 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001853 size_t data_size = image_header_.GetDataSize(); // stored size in file.
1854 if (file == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001855 LOG(WARNING) << "Failed to find image in " << image_filename;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001856 } else {
David Srbeckybc1748f2021-02-22 10:51:32 +00001857 size_t file_bytes = file->GetLength();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001858 // If the image is compressed, adjust to decompressed size.
1859 size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader);
Mathieu Chartiera5c01d72019-07-26 14:39:28 -07001860 if (!image_header_.HasCompressedBlock()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001861 DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image";
1862 }
David Srbeckybc1748f2021-02-22 10:51:32 +00001863 file_bytes += uncompressed_size - data_size;
1864 stats_.art_file_stats.AddBytes(file_bytes);
1865 stats_.art_file_stats["Header"].AddBytes(sizeof(ImageHeader));
Andreas Gampeace0dc12016-01-20 13:33:13 -08001866 }
1867
David Srbeckybc1748f2021-02-22 10:51:32 +00001868 size_t pointer_size = static_cast<size_t>(image_header_.GetPointerSize());
1869 CHECK_ALIGNED(image_header_.GetFieldsSection().Offset(), 4);
1870 CHECK_ALIGNED_PARAM(image_header_.GetMethodsSection().Offset(), pointer_size);
1871 CHECK_ALIGNED(image_header_.GetInternedStringsSection().Offset(), 8);
1872 CHECK_ALIGNED(image_header_.GetImageBitmapSection().Offset(), kPageSize);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001873
David Srbeckybc1748f2021-02-22 10:51:32 +00001874 for (size_t i = 0; i < ImageHeader::ImageSections::kSectionCount; i++) {
1875 ImageHeader::ImageSections index = ImageHeader::ImageSections(i);
1876 const char* name = ImageHeader::GetImageSectionName(index);
1877 stats_.art_file_stats[name].AddBytes(image_header_.GetImageSection(index).Size());
1878 }
Andreas Gampeace0dc12016-01-20 13:33:13 -08001879
David Srbeckybc1748f2021-02-22 10:51:32 +00001880 stats_.object_stats.AddBytes(image_header_.GetObjectsSection().Size());
1881 stats_.Dump(os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001882 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001883
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001884 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001885
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001886 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001887 }
1888
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001889 private:
Mathieu Chartier3398c782016-09-30 10:27:43 -07001890 static void PrettyObjectValue(std::ostream& os,
1891 ObjPtr<mirror::Class> type,
1892 ObjPtr<mirror::Object> value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001893 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001894 CHECK(type != nullptr);
1895 if (value == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07001896 os << StringPrintf("null %s\n", type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001897 } else if (type->IsStringClass()) {
Vladimir Marko4617d582019-03-28 13:48:31 +00001898 ObjPtr<mirror::String> string = value->AsString();
1899 os << StringPrintf("%p String: %s\n",
1900 string.Ptr(),
Ian Rogers68b56852014-08-29 20:19:11 -07001901 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001902 } else if (type->IsClassClass()) {
Vladimir Marko4617d582019-03-28 13:48:31 +00001903 ObjPtr<mirror::Class> klass = value->AsClass();
1904 os << StringPrintf("%p Class: %s\n",
1905 klass.Ptr(),
1906 mirror::Class::PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001907 } else {
David Sehr709b0702016-10-13 09:12:37 -07001908 os << StringPrintf("%p %s\n", value.Ptr(), type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001909 }
1910 }
1911
Mathieu Chartier3398c782016-09-30 10:27:43 -07001912 static void PrintField(std::ostream& os, ArtField* field, ObjPtr<mirror::Object> obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001913 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001914 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08001915 switch (field->GetTypeAsPrimitiveType()) {
1916 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08001917 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001918 break;
1919 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001920 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001921 break;
1922 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001923 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001924 break;
1925 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001926 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001927 break;
1928 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07001929 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001930 break;
1931 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07001932 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001933 break;
1934 case Primitive::kPrimBoolean:
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001935 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj) ? "true" : "false",
Fred Shih37f05ef2014-07-16 18:38:08 -07001936 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001937 break;
1938 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07001939 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001940 break;
1941 case Primitive::kPrimNot: {
1942 // Get the value, don't compute the type unless it is non-null as we don't want
1943 // to cause class loading.
Mathieu Chartier3398c782016-09-30 10:27:43 -07001944 ObjPtr<mirror::Object> value = field->GetObj(obj);
Ian Rogers08f1f502014-12-02 15:04:37 -08001945 if (value == nullptr) {
1946 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07001947 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08001948 // Grab the field type without causing resolution.
Vladimir Marko208f6702017-12-08 12:00:50 +00001949 ObjPtr<mirror::Class> field_type = field->LookupResolvedType();
Ian Rogers08f1f502014-12-02 15:04:37 -08001950 if (field_type != nullptr) {
1951 PrettyObjectValue(os, field_type, value);
1952 } else {
Mathieu Chartier3398c782016-09-30 10:27:43 -07001953 os << StringPrintf("%p %s\n",
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07001954 value.Ptr(),
Ian Rogers08f1f502014-12-02 15:04:37 -08001955 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
1956 }
Ian Rogers50239c72013-06-17 14:53:22 -07001957 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001958 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07001959 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001960 default:
1961 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
1962 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08001963 }
1964 }
1965
Andreas Gampe98104992018-10-16 12:49:47 -07001966 static void DumpFields(std::ostream& os, mirror::Object* obj, ObjPtr<mirror::Class> klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001967 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe98104992018-10-16 12:49:47 -07001968 ObjPtr<mirror::Class> super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001969 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001970 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001971 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001972 for (ArtField& field : klass->GetIFields()) {
1973 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001974 }
1975 }
1976
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001977 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001978 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001979 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001980
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001981 const void* GetQuickOatCodeBegin(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001982 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001983 image_header_.GetPointerSize());
David Srbeckya54e0cc2019-04-15 15:03:26 +01001984 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1985 if (class_linker->IsQuickResolutionStub(quick_code) ||
1986 class_linker->IsQuickToInterpreterBridge(quick_code) ||
Nicolas Geoffrayc39af942021-01-25 08:43:57 +00001987 class_linker->IsNterpTrampoline(quick_code) ||
David Srbeckya54e0cc2019-04-15 15:03:26 +01001988 class_linker->IsQuickGenericJniStub(quick_code) ||
Vladimir Markofa458ac2020-02-12 14:08:07 +00001989 class_linker->IsJniDlsymLookupStub(quick_code) ||
1990 class_linker->IsJniDlsymLookupCriticalStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001991 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001992 }
Vladimir Marko33bff252017-11-01 14:35:42 +00001993 if (oat_dumper_->GetInstructionSet() == InstructionSet::kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001994 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001995 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001996 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001997 }
1998
Mathieu Chartiere401d142015-04-22 13:56:20 -07001999 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002000 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002001 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
2002 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002003 return 0;
2004 }
David Srbeckya54e0cc2019-04-15 15:03:26 +01002005 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2006 reinterpret_cast<uintptr_t>(oat_code_begin) - sizeof(OatQuickMethodHeader));
2007 return method_header->GetCodeSize();
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002008 }
2009
Mathieu Chartiere401d142015-04-22 13:56:20 -07002010 const void* GetQuickOatCodeEnd(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002011 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002012 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002013 if (oat_code_begin == nullptr) {
2014 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002015 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002016 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002017 }
2018
Andreas Gampe0c183382017-07-13 22:26:24 -07002019 void DumpObject(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002020 DCHECK(obj != nullptr);
Andreas Gampe0c183382017-07-13 22:26:24 -07002021 if (!InDumpSpace(obj)) {
Brian Carlstrom78128a62011-09-15 17:21:19 -07002022 return;
2023 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002024
Andreas Gampe0c183382017-07-13 22:26:24 -07002025 std::ostream& os = vios_.Stream();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002026
Vladimir Marko4617d582019-03-28 13:48:31 +00002027 ObjPtr<mirror::Class> obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08002028 if (obj_class->IsArrayClass()) {
David Sehr709b0702016-10-13 09:12:37 -07002029 os << StringPrintf("%p: %s length:%d\n", obj, obj_class->PrettyDescriptor().c_str(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002030 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08002031 } else if (obj->IsClass()) {
Vladimir Marko4617d582019-03-28 13:48:31 +00002032 ObjPtr<mirror::Class> klass = obj->AsClass();
2033 os << StringPrintf("%p: java.lang.Class \"%s\" (",
2034 obj,
David Sehr709b0702016-10-13 09:12:37 -07002035 mirror::Class::PrettyDescriptor(klass).c_str())
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002036 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08002037 } else if (obj_class->IsStringClass()) {
Vladimir Marko4617d582019-03-28 13:48:31 +00002038 os << StringPrintf("%p: java.lang.String %s\n",
2039 obj,
Ian Rogers68b56852014-08-29 20:19:11 -07002040 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002041 } else {
David Sehr709b0702016-10-13 09:12:37 -07002042 os << StringPrintf("%p: %s\n", obj, obj_class->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002043 }
Andreas Gampe0c183382017-07-13 22:26:24 -07002044 ScopedIndentation indent1(&vios_);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002045 DumpFields(os, obj, obj_class);
Ian Rogersd5b32602012-02-26 16:40:04 -08002046 if (obj->IsObjectArray()) {
Vladimir Marko4617d582019-03-28 13:48:31 +00002047 ObjPtr<mirror::ObjectArray<mirror::Object>> obj_array = obj->AsObjectArray<mirror::Object>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002048 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Vladimir Marko423bebb2019-03-26 15:17:21 +00002049 ObjPtr<mirror::Object> value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002050 size_t run = 0;
2051 for (int32_t j = i + 1; j < length; j++) {
2052 if (value == obj_array->Get(j)) {
2053 run++;
2054 } else {
2055 break;
2056 }
2057 }
2058 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002059 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002060 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002061 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08002062 i = i + run;
2063 }
Vladimir Markoc524e9e2019-03-26 10:54:50 +00002064 ObjPtr<mirror::Class> value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002065 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002066 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08002067 }
2068 } else if (obj->IsClass()) {
Vladimir Marko38b8b252018-01-02 19:07:06 +00002069 ObjPtr<mirror::Class> klass = obj->AsClass();
Igor Murashkin86083f72017-10-27 10:59:04 -07002070
Vladimir Marko305c38b2018-02-14 11:50:07 +00002071 if (kBitstringSubtypeCheckEnabled) {
2072 os << "SUBTYPE_CHECK_BITS: ";
2073 SubtypeCheck<ObjPtr<mirror::Class>>::Dump(klass, os);
2074 os << "\n";
2075 }
Igor Murashkin86083f72017-10-27 10:59:04 -07002076
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002077 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002078 os << "STATICS:\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002079 ScopedIndentation indent2(&vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002080 for (ArtField& field : klass->GetSFields()) {
2081 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08002082 }
2083 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002084 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002085 std::string temp;
David Srbeckybc1748f2021-02-22 10:51:32 +00002086 const char* desc = obj_class->GetDescriptor(&temp);
2087 desc = stats_.descriptors.emplace(desc).first->c_str(); // Dedup and keep alive.
2088 stats_.object_stats[desc].AddBytes(obj->SizeOf());
Brian Carlstrom78128a62011-09-15 17:21:19 -07002089 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002090
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002091 void DumpMethod(ArtMethod* method, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002092 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002093 DCHECK(method != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07002094 const PointerSize pointer_size = image_header_.GetPointerSize();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002095 if (method->IsNative()) {
David Srbeckya54e0cc2019-04-15 15:03:26 +01002096 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002097 bool first_occurrence;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002098 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2099 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002100 if (first_occurrence) {
David Srbeckybc1748f2021-02-22 10:51:32 +00002101 stats_.oat_file_stats["native_code"].AddBytes(quick_oat_code_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002102 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002103 if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
2104 image_header_.GetPointerSize())) {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002105 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002106 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002107 } else if (method->IsAbstract() || method->IsClassInitializer()) {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002108 // Don't print information for these.
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002109 } else if (method->IsRuntimeMethod()) {
Vladimir Marko86c87522020-05-11 16:55:55 +01002110 if (method == Runtime::Current()->GetResolutionMethod()) {
2111 const void* resolution_trampoline =
2112 method->GetEntryPointFromQuickCompiledCodePtrSize(image_header_.GetPointerSize());
2113 indent_os << StringPrintf("Resolution trampoline: %p\n", resolution_trampoline);
2114 const void* critical_native_resolution_trampoline =
2115 method->GetEntryPointFromJniPtrSize(image_header_.GetPointerSize());
2116 indent_os << StringPrintf("Resolution trampoline for @CriticalNative: %p\n",
2117 critical_native_resolution_trampoline);
2118 } else {
2119 ImtConflictTable* table = method->GetImtConflictTable(image_header_.GetPointerSize());
2120 if (table != nullptr) {
2121 indent_os << "IMT conflict table " << table << " method: ";
2122 for (size_t i = 0, count = table->NumEntries(pointer_size); i < count; ++i) {
2123 indent_os << ArtMethod::PrettyMethod(table->GetImplementationMethod(i, pointer_size))
2124 << " ";
2125 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002126 }
2127 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002128 } else {
David Sehr0225f8e2018-01-31 08:52:24 +00002129 CodeItemDataAccessor code_item_accessor(method->DexInstructionData());
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08002130 size_t dex_instruction_bytes = code_item_accessor.InsnsSizeInCodeUnits() * 2;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002131 stats_.dex_instruction_bytes += dex_instruction_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002132
David Srbeckya54e0cc2019-04-15 15:03:26 +01002133 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
2134 const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
2135
Mathieu Chartiere401d142015-04-22 13:56:20 -07002136 bool first_occurrence;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002137 size_t vmap_table_bytes = 0u;
David Srbeckya54e0cc2019-04-15 15:03:26 +01002138 if (quick_oat_code_begin != nullptr) {
2139 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2140 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
2141 vmap_table_bytes = ComputeOatSize(method_header->GetOptimizedCodeInfoPtr(),
2142 &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002143 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002144 stats_.vmap_table_bytes += vmap_table_bytes;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002145 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002146 }
2147
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002148 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2149 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002150 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002151 stats_.managed_code_bytes += quick_oat_code_size;
David Srbeckybc1748f2021-02-22 10:51:32 +00002152 art::Stats& managed_code_stats = stats_.oat_file_stats["managed_code"];
2153 managed_code_stats.AddBytes(quick_oat_code_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002154 if (method->IsConstructor()) {
2155 if (method->IsStatic()) {
David Srbeckybc1748f2021-02-22 10:51:32 +00002156 managed_code_stats["class_initializer"].AddBytes(quick_oat_code_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002157 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
David Srbeckybc1748f2021-02-22 10:51:32 +00002158 managed_code_stats["large_initializer"].AddBytes(quick_oat_code_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002159 }
2160 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
David Srbeckybc1748f2021-02-22 10:51:32 +00002161 managed_code_stats["large_method"].AddBytes(quick_oat_code_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002162 }
2163 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002164 stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002165
Igor Murashkin7617abd2015-07-10 18:27:47 -07002166 uint32_t method_access_flags = method->GetAccessFlags();
2167
Mathieu Chartiere401d142015-04-22 13:56:20 -07002168 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002169 indent_os << StringPrintf("SIZE: Dex Instructions=%zd StackMaps=%zd AccessFlags=0x%x\n",
2170 dex_instruction_bytes,
2171 vmap_table_bytes,
Igor Murashkin7617abd2015-07-10 18:27:47 -07002172 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002173
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002174 size_t total_size = dex_instruction_bytes +
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002175 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002176
2177 double expansion =
2178 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002179 stats_.ComputeOutliers(total_size, expansion, method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002180 }
2181 }
2182
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002183 std::set<const void*> already_seen_;
2184 // Compute the size of the given data within the oat file and whether this is the first time
2185 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002186 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002187 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002188 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002189 already_seen_.insert(oat_data);
2190 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002191 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002192 }
2193 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002194 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002195
2196 public:
2197 struct Stats {
David Srbeckybc1748f2021-02-22 10:51:32 +00002198 art::Stats art_file_stats;
2199 art::Stats oat_file_stats;
2200 art::Stats object_stats;
2201 std::set<std::string> descriptors;
2202
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002203 size_t oat_file_bytes = 0u;
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002204 size_t managed_code_bytes = 0u;
2205 size_t managed_code_bytes_ignoring_deduplication = 0u;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002206
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002207 size_t vmap_table_bytes = 0u;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002208
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002209 size_t dex_instruction_bytes = 0u;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002210
Mathieu Chartiere401d142015-04-22 13:56:20 -07002211 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002212 std::vector<size_t> method_outlier_size;
2213 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002214 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002215
Mathieu Chartier1ca718e2018-10-23 12:55:34 -07002216 Stats() {}
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002217
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002218 double PercentOfOatBytes(size_t size) {
2219 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2220 }
2221
Mathieu Chartiere401d142015-04-22 13:56:20 -07002222 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002223 method_outlier_size.push_back(total_size);
2224 method_outlier_expansion.push_back(expansion);
2225 method_outlier.push_back(method);
2226 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002227
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002228 void DumpOutliers(std::ostream& os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002229 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002230 size_t sum_of_sizes = 0;
2231 size_t sum_of_sizes_squared = 0;
2232 size_t sum_of_expansion = 0;
2233 size_t sum_of_expansion_squared = 0;
2234 size_t n = method_outlier_size.size();
Mathieu Chartier1ebf8d32016-06-09 11:51:27 -07002235 if (n <= 1) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08002236 return;
2237 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002238 for (size_t i = 0; i < n; i++) {
2239 size_t cur_size = method_outlier_size[i];
2240 sum_of_sizes += cur_size;
2241 sum_of_sizes_squared += cur_size * cur_size;
2242 double cur_expansion = method_outlier_expansion[i];
2243 sum_of_expansion += cur_expansion;
2244 sum_of_expansion_squared += cur_expansion * cur_expansion;
2245 }
2246 size_t size_mean = sum_of_sizes / n;
2247 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2248 double expansion_mean = sum_of_expansion / n;
2249 double expansion_variance =
2250 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2251
2252 // Dump methods whose size is a certain number of standard deviations from the mean
2253 size_t dumped_values = 0;
2254 size_t skipped_values = 0;
2255 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2256 size_t cur_size_variance = i * i * size_variance;
2257 bool first = true;
2258 for (size_t j = 0; j < n; j++) {
2259 size_t cur_size = method_outlier_size[j];
2260 if (cur_size > size_mean) {
2261 size_t cur_var = cur_size - size_mean;
2262 cur_var = cur_var * cur_var;
2263 if (cur_var > cur_size_variance) {
2264 if (dumped_values > 20) {
2265 if (i == 1) {
2266 skipped_values++;
2267 } else {
2268 i = 2; // jump to counting for 1 standard deviation
2269 break;
2270 }
2271 } else {
2272 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002273 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002274 first = false;
2275 }
David Sehr709b0702016-10-13 09:12:37 -07002276 os << ArtMethod::PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002277 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002278 method_outlier_size[j] = 0; // don't consider this method again
2279 dumped_values++;
2280 }
2281 }
2282 }
2283 }
2284 }
2285 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002286 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002287 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002288 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002289 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002290
2291 // Dump methods whose expansion is a certain number of standard deviations from the mean
2292 dumped_values = 0;
2293 skipped_values = 0;
2294 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2295 double cur_expansion_variance = i * i * expansion_variance;
2296 bool first = true;
2297 for (size_t j = 0; j < n; j++) {
2298 double cur_expansion = method_outlier_expansion[j];
2299 if (cur_expansion > expansion_mean) {
2300 size_t cur_var = cur_expansion - expansion_mean;
2301 cur_var = cur_var * cur_var;
2302 if (cur_var > cur_expansion_variance) {
2303 if (dumped_values > 20) {
2304 if (i == 1) {
2305 skipped_values++;
2306 } else {
2307 i = 2; // jump to counting for 1 standard deviation
2308 break;
2309 }
2310 } else {
2311 if (first) {
2312 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002313 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002314 first = false;
2315 }
David Sehr709b0702016-10-13 09:12:37 -07002316 os << ArtMethod::PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002317 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002318 method_outlier_expansion[j] = 0.0; // don't consider this method again
2319 dumped_values++;
2320 }
2321 }
2322 }
2323 }
2324 }
2325 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002326 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002327 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002328 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002329 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002330 }
2331
David Srbeckybc1748f2021-02-22 10:51:32 +00002332 void Dump(std::ostream& os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002333 REQUIRES_SHARED(Locks::mutator_lock_) {
David Srbeckybc1748f2021-02-22 10:51:32 +00002334 VariableIndentationOutputStream vios(&os);
2335 art_file_stats.DumpSizes(vios, "ArtFile");
Elliott Hughesc073b072012-05-24 19:29:17 -07002336 os << "\n" << std::flush;
David Srbeckybc1748f2021-02-22 10:51:32 +00002337 object_stats.DumpSizes(vios, "Objects");
2338 os << "\n" << std::flush;
2339 oat_file_stats.DumpSizes(vios, "OatFile");
2340 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002341
Mathieu Chartier02e25112013-08-14 16:14:24 -07002342 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002343 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002344 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2345 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002346 }
2347
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002348 os << "\n" << StringPrintf("vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002349 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002350 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002351
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002352 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2353 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002354 static_cast<double>(managed_code_bytes) /
2355 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002356 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002357 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002358 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002359
2360 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002361 }
2362 } stats_;
2363
2364 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002365 enum {
2366 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2367 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2368 kLargeConstructorDexBytes = 4000,
2369 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2370 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2371 kLargeMethodDexBytes = 16000
2372 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002373
2374 // For performance, use the *os_ directly for anything that doesn't need indentation
2375 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002376 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002377 VariableIndentationOutputStream vios_;
2378 ScopedIndentation indent1_;
2379
Ian Rogers1d54e732013-05-02 21:10:01 -07002380 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002381 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002382 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002383 OatDumperOptions* oat_dumper_options_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002384
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002385 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002386};
2387
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002388static int DumpImage(gc::space::ImageSpace* image_space,
2389 OatDumperOptions* options,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002390 std::ostream* os) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002391 const ImageHeader& image_header = image_space->GetImageHeader();
2392 if (!image_header.IsValid()) {
Andreas Gampe221d9812018-01-22 17:48:56 -08002393 LOG(ERROR) << "Invalid image header " << image_space->GetImageLocation();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002394 return EXIT_FAILURE;
2395 }
2396 ImageDumper image_dumper(os, *image_space, image_header, options);
2397 if (!image_dumper.Dump()) {
2398 return EXIT_FAILURE;
2399 }
2400 return EXIT_SUCCESS;
2401}
2402
2403static int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002404 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002405 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002406 options->class_loader_ = &null_class_loader;
2407
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002408 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002409 if (options->app_image_ != nullptr) {
2410 if (options->app_oat_ == nullptr) {
2411 LOG(ERROR) << "Can not dump app image without app oat file";
Jeff Haodcdc85b2015-12-04 14:06:18 -08002412 return EXIT_FAILURE;
2413 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002414 // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
2415 // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
2416 // pointers into 32 bit pointer sized ArtMethods.
2417 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002418 std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01002419 options->app_oat_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002420 options->app_oat_,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002421 /*executable=*/ false,
2422 /*low_4gb=*/ true,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002423 &error_msg));
2424 if (oat_file == nullptr) {
2425 LOG(ERROR) << "Failed to open oat file " << options->app_oat_ << " with error " << error_msg;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002426 return EXIT_FAILURE;
2427 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002428 std::unique_ptr<gc::space::ImageSpace> space(
2429 gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg));
2430 if (space == nullptr) {
2431 LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error "
2432 << error_msg;
Mathieu Chartier6b689ce2019-07-18 14:17:47 -07002433 return EXIT_FAILURE;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002434 }
2435 // Open dex files for the image.
2436 std::vector<std::unique_ptr<const DexFile>> dex_files;
2437 if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
2438 LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
2439 << error_msg;
Mathieu Chartier6b689ce2019-07-18 14:17:47 -07002440 return EXIT_FAILURE;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002441 }
2442 // Dump the actual image.
2443 int result = DumpImage(space.get(), options, os);
2444 if (result != EXIT_SUCCESS) {
2445 return result;
2446 }
2447 // Fall through to dump the boot images.
2448 }
2449
2450 gc::Heap* heap = runtime->GetHeap();
Vladimir Markodd09f712019-12-06 12:37:10 +00002451 if (!heap->HasBootImageSpace()) {
2452 LOG(ERROR) << "No image spaces";
2453 return EXIT_FAILURE;
2454 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002455 for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
2456 int result = DumpImage(image_space, options, os);
2457 if (result != EXIT_SUCCESS) {
2458 return result;
2459 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002460 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002461 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002462}
2463
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002464static jobject InstallOatFile(Runtime* runtime,
2465 std::unique_ptr<OatFile> oat_file,
2466 std::vector<const DexFile*>* class_path)
2467 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002468 Thread* self = Thread::Current();
2469 CHECK(self != nullptr);
2470 // Need well-known-classes.
2471 WellKnownClasses::Init(self->GetJniEnv());
2472
Vladimir Marko421087b2018-02-27 11:00:17 +00002473 // Open dex files.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002474 OatFile* oat_file_ptr = oat_file.get();
Andreas Gampe00b25f32014-09-17 21:49:05 -07002475 ClassLinker* class_linker = runtime->GetClassLinker();
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002476 runtime->GetOatFileManager().RegisterOatFile(std::move(oat_file));
Andreas Gampeb40d3612018-06-26 15:49:42 -07002477 for (const OatDexFile* odf : oat_file_ptr->GetOatDexFiles()) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002478 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002479 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002480 CHECK(dex_file != nullptr) << error_msg;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002481 class_path->push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002482 }
2483
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002484 // Need a class loader. Fake that we're a compiler.
2485 // Note: this will run initializers through the unstarted runtime, so make sure it's
2486 // initialized.
2487 interpreter::UnstartedRuntime::Initialize();
2488
2489 jobject class_loader = class_linker->CreatePathClassLoader(self, *class_path);
2490
Vladimir Marko421087b2018-02-27 11:00:17 +00002491 // Need to register dex files to get a working dex cache.
2492 for (const DexFile* dex_file : *class_path) {
2493 ObjPtr<mirror::DexCache> dex_cache = class_linker->RegisterDexFile(
2494 *dex_file, self->DecodeJObject(class_loader)->AsClassLoader());
2495 CHECK(dex_cache != nullptr);
2496 }
2497
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002498 return class_loader;
2499}
2500
2501static int DumpOatWithRuntime(Runtime* runtime,
2502 std::unique_ptr<OatFile> oat_file,
2503 OatDumperOptions* options,
2504 std::ostream* os) {
2505 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2506 ScopedObjectAccess soa(Thread::Current());
2507
2508 OatFile* oat_file_ptr = oat_file.get();
2509 std::vector<const DexFile*> class_path;
2510 jobject class_loader = InstallOatFile(runtime, std::move(oat_file), &class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002511
2512 // Use the class loader while dumping.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002513 StackHandleScope<1> scope(soa.Self());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002514 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07002515 soa.Decode<mirror::ClassLoader>(class_loader));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002516 options->class_loader_ = &loader_handle;
2517
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002518 OatDumper oat_dumper(*oat_file_ptr, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002519 bool success = oat_dumper.Dump(*os);
2520 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2521}
2522
2523static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002524 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002525 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002526 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002527 options->class_loader_ = &null_class_loader;
2528
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002529 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002530 bool success = oat_dumper.Dump(*os);
2531 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2532}
2533
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002534static int DumpOat(Runtime* runtime,
2535 const char* oat_filename,
2536 const char* dex_filename,
2537 OatDumperOptions* options,
Andreas Gampe00b25f32014-09-17 21:49:05 -07002538 std::ostream* os) {
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002539 if (dex_filename == nullptr) {
2540 LOG(WARNING) << "No dex filename provided, "
2541 << "oatdump might fail if the oat file does not contain the dex code.";
2542 }
Vladimir Markob7bf8432019-12-03 13:18:50 +00002543 std::string dex_filename_str((dex_filename != nullptr) ? dex_filename : "");
2544 ArrayRef<const std::string> dex_filenames(&dex_filename_str,
2545 /*size=*/ (dex_filename != nullptr) ? 1u : 0u);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002546 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002547 std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01002548 oat_filename,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002549 oat_filename,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002550 /*executable=*/ false,
2551 /*low_4gb=*/ false,
Vladimir Markob7bf8432019-12-03 13:18:50 +00002552 dex_filenames,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002553 /*reservation=*/ nullptr,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002554 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002555 if (oat_file == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08002556 LOG(ERROR) << "Failed to open oat file from '" << oat_filename << "': " << error_msg;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002557 return EXIT_FAILURE;
2558 }
2559
2560 if (runtime != nullptr) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002561 return DumpOatWithRuntime(runtime, std::move(oat_file), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002562 } else {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002563 return DumpOatWithoutRuntime(oat_file.get(), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002564 }
2565}
2566
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002567static int SymbolizeOat(const char* oat_filename,
2568 const char* dex_filename,
2569 std::string& output_name,
2570 bool no_bits) {
Vladimir Markob7bf8432019-12-03 13:18:50 +00002571 std::string dex_filename_str((dex_filename != nullptr) ? dex_filename : "");
2572 ArrayRef<const std::string> dex_filenames(&dex_filename_str,
2573 /*size=*/ (dex_filename != nullptr) ? 1u : 0u);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002574 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002575 std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01002576 oat_filename,
Andreas Gampe08c277c2017-04-26 22:22:15 -07002577 oat_filename,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002578 /*executable=*/ false,
2579 /*low_4gb=*/ false,
Vladimir Markob7bf8432019-12-03 13:18:50 +00002580 dex_filenames,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002581 /*reservation=*/ nullptr,
Andreas Gampe08c277c2017-04-26 22:22:15 -07002582 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002583 if (oat_file == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08002584 LOG(ERROR) << "Failed to open oat file from '" << oat_filename << "': " << error_msg;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002585 return EXIT_FAILURE;
2586 }
2587
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002588 bool result;
2589 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
2590 // files for 64-bit code in the past.
2591 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
Andreas Gampe08c277c2017-04-26 22:22:15 -07002592 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file.get(), output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002593 result = oat_symbolizer.Symbolize();
2594 } else {
Andreas Gampe08c277c2017-04-26 22:22:15 -07002595 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file.get(), output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002596 result = oat_symbolizer.Symbolize();
2597 }
2598 if (!result) {
Andreas Gampe221d9812018-01-22 17:48:56 -08002599 LOG(ERROR) << "Failed to symbolize";
Andreas Gampe00b25f32014-09-17 21:49:05 -07002600 return EXIT_FAILURE;
2601 }
2602
2603 return EXIT_SUCCESS;
2604}
2605
Andreas Gampe9fded872016-09-25 16:08:35 -07002606class IMTDumper {
2607 public:
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002608 static bool Dump(Runtime* runtime,
2609 const std::string& imt_file,
2610 bool dump_imt_stats,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00002611 const char* oat_filename,
2612 const char* dex_filename) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002613 Thread* self = Thread::Current();
2614
2615 ScopedObjectAccess soa(self);
2616 StackHandleScope<1> scope(self);
2617 MutableHandle<mirror::ClassLoader> class_loader = scope.NewHandle<mirror::ClassLoader>(nullptr);
2618 std::vector<const DexFile*> class_path;
2619
2620 if (oat_filename != nullptr) {
Vladimir Markob7bf8432019-12-03 13:18:50 +00002621 std::string dex_filename_str((dex_filename != nullptr) ? dex_filename : "");
2622 ArrayRef<const std::string> dex_filenames(&dex_filename_str,
2623 /*size=*/ (dex_filename != nullptr) ? 1u : 0u);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002624 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002625 std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01002626 oat_filename,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002627 oat_filename,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002628 /*executable=*/ false,
Andreas Gampe9b031f72018-10-04 11:03:34 -07002629 /*low_4gb=*/false,
Vladimir Markob7bf8432019-12-03 13:18:50 +00002630 dex_filenames,
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002631 /*reservation=*/ nullptr,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002632 &error_msg));
2633 if (oat_file == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08002634 LOG(ERROR) << "Failed to open oat file from '" << oat_filename << "': " << error_msg;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002635 return false;
2636 }
2637
2638 class_loader.Assign(soa.Decode<mirror::ClassLoader>(
2639 InstallOatFile(runtime, std::move(oat_file), &class_path)));
2640 } else {
2641 class_loader.Assign(nullptr); // Boot classloader. Just here for explicit documentation.
2642 class_path = runtime->GetClassLinker()->GetBootClassPath();
2643 }
2644
2645 if (!imt_file.empty()) {
2646 return DumpImt(runtime, imt_file, class_loader);
2647 }
2648
2649 if (dump_imt_stats) {
2650 return DumpImtStats(runtime, class_path, class_loader);
2651 }
2652
2653 LOG(FATAL) << "Should not reach here";
2654 UNREACHABLE();
2655 }
2656
2657 private:
2658 static bool DumpImt(Runtime* runtime,
2659 const std::string& imt_file,
2660 Handle<mirror::ClassLoader> h_class_loader)
2661 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002662 std::vector<std::string> lines = ReadCommentedInputFromFile(imt_file);
2663 std::unordered_set<std::string> prepared;
2664
2665 for (const std::string& line : lines) {
2666 // A line should be either a class descriptor, in which case we will dump the complete IMT,
2667 // or a class descriptor and an interface method, in which case we will lookup the method,
2668 // determine its IMT slot, and check the class' IMT.
2669 size_t first_space = line.find(' ');
2670 if (first_space == std::string::npos) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002671 DumpIMTForClass(runtime, line, h_class_loader, &prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07002672 } else {
2673 DumpIMTForMethod(runtime,
2674 line.substr(0, first_space),
2675 line.substr(first_space + 1, std::string::npos),
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002676 h_class_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07002677 &prepared);
2678 }
2679 std::cerr << std::endl;
2680 }
2681
2682 return true;
2683 }
2684
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002685 static bool DumpImtStats(Runtime* runtime,
2686 const std::vector<const DexFile*>& dex_files,
2687 Handle<mirror::ClassLoader> h_class_loader)
2688 REQUIRES_SHARED(Locks::mutator_lock_) {
2689 size_t without_imt = 0;
2690 size_t with_imt = 0;
Andreas Gampe9fded872016-09-25 16:08:35 -07002691 std::map<size_t, size_t> histogram;
2692
2693 ClassLinker* class_linker = runtime->GetClassLinker();
2694 const PointerSize pointer_size = class_linker->GetImagePointerSize();
2695 std::unordered_set<std::string> prepared;
2696
2697 Thread* self = Thread::Current();
Andreas Gampe9fded872016-09-25 16:08:35 -07002698 StackHandleScope<1> scope(self);
2699 MutableHandle<mirror::Class> h_klass(scope.NewHandle<mirror::Class>(nullptr));
2700
2701 for (const DexFile* dex_file : dex_files) {
2702 for (uint32_t class_def_index = 0;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002703 class_def_index != dex_file->NumClassDefs();
2704 ++class_def_index) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08002705 const dex::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Andreas Gampe9fded872016-09-25 16:08:35 -07002706 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002707 h_klass.Assign(class_linker->FindClass(self, descriptor, h_class_loader));
Andreas Gampefa4333d2017-02-14 11:10:34 -08002708 if (h_klass == nullptr) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002709 std::cerr << "Warning: could not load " << descriptor << std::endl;
2710 continue;
2711 }
2712
2713 if (HasNoIMT(runtime, h_klass, pointer_size, &prepared)) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002714 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07002715 continue;
2716 }
2717
2718 ImTable* im_table = PrepareAndGetImTable(runtime, h_klass, pointer_size, &prepared);
2719 if (im_table == nullptr) {
2720 // Should not happen, but accept.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002721 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07002722 continue;
2723 }
2724
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002725 with_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07002726 for (size_t imt_index = 0; imt_index != ImTable::kSize; ++imt_index) {
2727 ArtMethod* ptr = im_table->Get(imt_index, pointer_size);
2728 if (ptr->IsRuntimeMethod()) {
2729 if (ptr->IsImtUnimplementedMethod()) {
2730 histogram[0]++;
2731 } else {
2732 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
2733 histogram[current_table->NumEntries(pointer_size)]++;
2734 }
2735 } else {
2736 histogram[1]++;
2737 }
2738 }
2739 }
2740 }
2741
2742 std::cerr << "IMT stats:"
2743 << std::endl << std::endl;
2744
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002745 std::cerr << " " << with_imt << " classes with IMT."
Andreas Gampe9fded872016-09-25 16:08:35 -07002746 << std::endl << std::endl;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002747 std::cerr << " " << without_imt << " classes without IMT (or copy from Object)."
Andreas Gampe9fded872016-09-25 16:08:35 -07002748 << std::endl << std::endl;
2749
2750 double sum_one = 0;
2751 size_t count_one = 0;
2752
2753 std::cerr << " " << "IMT histogram" << std::endl;
2754 for (auto& bucket : histogram) {
2755 std::cerr << " " << bucket.first << " " << bucket.second << std::endl;
2756 if (bucket.first > 0) {
2757 sum_one += bucket.second * bucket.first;
2758 count_one += bucket.second;
2759 }
2760 }
2761
2762 double count_zero = count_one + histogram[0];
2763 std::cerr << " Stats:" << std::endl;
2764 std::cerr << " Average depth (including empty): " << (sum_one / count_zero) << std::endl;
2765 std::cerr << " Average depth (excluding empty): " << (sum_one / count_one) << std::endl;
2766
2767 return true;
2768 }
2769
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002770 // Return whether the given class has no IMT (or the one shared with java.lang.Object).
Andreas Gampe9fded872016-09-25 16:08:35 -07002771 static bool HasNoIMT(Runtime* runtime,
2772 Handle<mirror::Class> klass,
2773 const PointerSize pointer_size,
2774 std::unordered_set<std::string>* prepared)
2775 REQUIRES_SHARED(Locks::mutator_lock_) {
2776 if (klass->IsObjectClass() || !klass->ShouldHaveImt()) {
2777 return true;
2778 }
2779
2780 if (klass->GetImt(pointer_size) == nullptr) {
2781 PrepareClass(runtime, klass, prepared);
2782 }
2783
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01002784 ObjPtr<mirror::Class> object_class = GetClassRoot<mirror::Object>();
Andreas Gampe9fded872016-09-25 16:08:35 -07002785 DCHECK(object_class->IsObjectClass());
2786
2787 bool result = klass->GetImt(pointer_size) == object_class->GetImt(pointer_size);
2788
Mathieu Chartier6beced42016-11-15 15:51:31 -08002789 if (klass->GetIfTable()->Count() == 0) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002790 DCHECK(result);
2791 }
2792
2793 return result;
2794 }
2795
2796 static void PrintTable(ImtConflictTable* table, PointerSize pointer_size)
2797 REQUIRES_SHARED(Locks::mutator_lock_) {
2798 if (table == nullptr) {
2799 std::cerr << " <No IMT?>" << std::endl;
2800 return;
2801 }
2802 size_t table_index = 0;
2803 for (;;) {
2804 ArtMethod* ptr = table->GetInterfaceMethod(table_index, pointer_size);
2805 if (ptr == nullptr) {
2806 return;
2807 }
2808 table_index++;
David Sehr709b0702016-10-13 09:12:37 -07002809 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07002810 }
2811 }
2812
2813 static ImTable* PrepareAndGetImTable(Runtime* runtime,
2814 Thread* self,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002815 Handle<mirror::ClassLoader> h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07002816 const std::string& class_name,
2817 const PointerSize pointer_size,
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01002818 /*out*/ ObjPtr<mirror::Class>* klass_out,
2819 /*inout*/ std::unordered_set<std::string>* prepared)
Andreas Gampe9fded872016-09-25 16:08:35 -07002820 REQUIRES_SHARED(Locks::mutator_lock_) {
2821 if (class_name.empty()) {
2822 return nullptr;
2823 }
2824
2825 std::string descriptor;
2826 if (class_name[0] == 'L') {
2827 descriptor = class_name;
2828 } else {
2829 descriptor = DotToDescriptor(class_name.c_str());
2830 }
2831
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01002832 ObjPtr<mirror::Class> klass =
2833 runtime->GetClassLinker()->FindClass(self, descriptor.c_str(), h_loader);
Andreas Gampe9fded872016-09-25 16:08:35 -07002834
2835 if (klass == nullptr) {
2836 self->ClearException();
2837 std::cerr << "Did not find " << class_name << std::endl;
2838 *klass_out = nullptr;
2839 return nullptr;
2840 }
2841
2842 StackHandleScope<1> scope(Thread::Current());
2843 Handle<mirror::Class> h_klass = scope.NewHandle<mirror::Class>(klass);
2844
2845 ImTable* ret = PrepareAndGetImTable(runtime, h_klass, pointer_size, prepared);
2846 *klass_out = h_klass.Get();
2847 return ret;
2848 }
2849
2850 static ImTable* PrepareAndGetImTable(Runtime* runtime,
2851 Handle<mirror::Class> h_klass,
2852 const PointerSize pointer_size,
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01002853 /*inout*/ std::unordered_set<std::string>* prepared)
Andreas Gampe9fded872016-09-25 16:08:35 -07002854 REQUIRES_SHARED(Locks::mutator_lock_) {
2855 PrepareClass(runtime, h_klass, prepared);
2856 return h_klass->GetImt(pointer_size);
2857 }
2858
2859 static void DumpIMTForClass(Runtime* runtime,
2860 const std::string& class_name,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002861 Handle<mirror::ClassLoader> h_loader,
2862 std::unordered_set<std::string>* prepared)
2863 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002864 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01002865 ObjPtr<mirror::Class> klass;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002866 ImTable* imt = PrepareAndGetImTable(runtime,
2867 Thread::Current(),
2868 h_loader,
2869 class_name,
2870 pointer_size,
2871 &klass,
2872 prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07002873 if (imt == nullptr) {
2874 return;
2875 }
2876
2877 std::cerr << class_name << std::endl << " IMT:" << std::endl;
2878 for (size_t index = 0; index < ImTable::kSize; ++index) {
2879 std::cerr << " " << index << ":" << std::endl;
2880 ArtMethod* ptr = imt->Get(index, pointer_size);
2881 if (ptr->IsRuntimeMethod()) {
2882 if (ptr->IsImtUnimplementedMethod()) {
2883 std::cerr << " <empty>" << std::endl;
2884 } else {
2885 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
2886 PrintTable(current_table, pointer_size);
2887 }
2888 } else {
David Sehr709b0702016-10-13 09:12:37 -07002889 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07002890 }
2891 }
2892
2893 std::cerr << " Interfaces:" << std::endl;
2894 // Run through iftable, find methods that slot here, see if they fit.
Vladimir Markoc524e9e2019-03-26 10:54:50 +00002895 ObjPtr<mirror::IfTable> if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08002896 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
Vladimir Markoc524e9e2019-03-26 10:54:50 +00002897 ObjPtr<mirror::Class> iface = if_table->GetInterface(i);
Mathieu Chartier6beced42016-11-15 15:51:31 -08002898 std::string iface_name;
2899 std::cerr << " " << iface->GetDescriptor(&iface_name) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07002900
Mathieu Chartier6beced42016-11-15 15:51:31 -08002901 for (ArtMethod& iface_method : iface->GetVirtualMethods(pointer_size)) {
2902 uint32_t class_hash, name_hash, signature_hash;
2903 ImTable::GetImtHashComponents(&iface_method, &class_hash, &name_hash, &signature_hash);
2904 uint32_t imt_slot = ImTable::GetImtIndex(&iface_method);
2905 std::cerr << " " << iface_method.PrettyMethod(true)
2906 << " slot=" << imt_slot
2907 << std::hex
2908 << " class_hash=0x" << class_hash
2909 << " name_hash=0x" << name_hash
2910 << " signature_hash=0x" << signature_hash
2911 << std::dec
2912 << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07002913 }
2914 }
2915 }
2916
2917 static void DumpIMTForMethod(Runtime* runtime,
2918 const std::string& class_name,
2919 const std::string& method,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002920 Handle<mirror::ClassLoader> h_loader,
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01002921 /*inout*/ std::unordered_set<std::string>* prepared)
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002922 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002923 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01002924 ObjPtr<mirror::Class> klass;
Andreas Gampe9fded872016-09-25 16:08:35 -07002925 ImTable* imt = PrepareAndGetImTable(runtime,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002926 Thread::Current(),
2927 h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07002928 class_name,
2929 pointer_size,
2930 &klass,
2931 prepared);
2932 if (imt == nullptr) {
2933 return;
2934 }
2935
2936 std::cerr << class_name << " <" << method << ">" << std::endl;
2937 for (size_t index = 0; index < ImTable::kSize; ++index) {
2938 ArtMethod* ptr = imt->Get(index, pointer_size);
2939 if (ptr->IsRuntimeMethod()) {
2940 if (ptr->IsImtUnimplementedMethod()) {
2941 continue;
2942 }
2943
2944 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
2945 if (current_table == nullptr) {
2946 continue;
2947 }
2948
2949 size_t table_index = 0;
2950 for (;;) {
2951 ArtMethod* ptr2 = current_table->GetInterfaceMethod(table_index, pointer_size);
2952 if (ptr2 == nullptr) {
2953 break;
2954 }
2955 table_index++;
2956
David Sehr709b0702016-10-13 09:12:37 -07002957 std::string p_name = ptr2->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08002958 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002959 std::cerr << " Slot "
2960 << index
2961 << " ("
2962 << current_table->NumEntries(pointer_size)
2963 << ")"
2964 << std::endl;
2965 PrintTable(current_table, pointer_size);
2966 return;
2967 }
2968 }
2969 } else {
David Sehr709b0702016-10-13 09:12:37 -07002970 std::string p_name = ptr->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08002971 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002972 std::cerr << " Slot " << index << " (1)" << std::endl;
2973 std::cerr << " " << p_name << std::endl;
2974 } else {
2975 // Run through iftable, find methods that slot here, see if they fit.
Vladimir Markoc524e9e2019-03-26 10:54:50 +00002976 ObjPtr<mirror::IfTable> if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08002977 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
Vladimir Markoc524e9e2019-03-26 10:54:50 +00002978 ObjPtr<mirror::Class> iface = if_table->GetInterface(i);
Mathieu Chartier6beced42016-11-15 15:51:31 -08002979 size_t num_methods = iface->NumDeclaredVirtualMethods();
2980 if (num_methods > 0) {
2981 for (ArtMethod& iface_method : iface->GetMethods(pointer_size)) {
2982 if (ImTable::GetImtIndex(&iface_method) == index) {
2983 std::string i_name = iface_method.PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08002984 if (android::base::StartsWith(i_name, method.c_str())) {
Mathieu Chartier6beced42016-11-15 15:51:31 -08002985 std::cerr << " Slot " << index << " (1)" << std::endl;
2986 std::cerr << " " << p_name << " (" << i_name << ")" << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07002987 }
2988 }
2989 }
2990 }
2991 }
2992 }
2993 }
2994 }
2995 }
2996
2997 // Read lines from the given stream, dropping comments and empty lines
2998 static std::vector<std::string> ReadCommentedInputStream(std::istream& in_stream) {
2999 std::vector<std::string> output;
3000 while (in_stream.good()) {
3001 std::string dot;
3002 std::getline(in_stream, dot);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003003 if (android::base::StartsWith(dot, "#") || dot.empty()) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003004 continue;
3005 }
3006 output.push_back(dot);
3007 }
3008 return output;
3009 }
3010
3011 // Read lines from the given file, dropping comments and empty lines.
3012 static std::vector<std::string> ReadCommentedInputFromFile(const std::string& input_filename) {
3013 std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
3014 if (input_file.get() == nullptr) {
3015 LOG(ERROR) << "Failed to open input file " << input_filename;
3016 return std::vector<std::string>();
3017 }
3018 std::vector<std::string> result = ReadCommentedInputStream(*input_file);
3019 input_file->close();
3020 return result;
3021 }
3022
3023 // Prepare a class, i.e., ensure it has a filled IMT. Will do so recursively for superclasses,
3024 // and note in the given set that the work was done.
3025 static void PrepareClass(Runtime* runtime,
3026 Handle<mirror::Class> h_klass,
Vladimir Markoa8bba7d2018-05-30 15:18:48 +01003027 /*inout*/ std::unordered_set<std::string>* done)
Andreas Gampe9fded872016-09-25 16:08:35 -07003028 REQUIRES_SHARED(Locks::mutator_lock_) {
3029 if (!h_klass->ShouldHaveImt()) {
3030 return;
3031 }
3032
3033 std::string name;
3034 name = h_klass->GetDescriptor(&name);
3035
3036 if (done->find(name) != done->end()) {
3037 return;
3038 }
3039 done->insert(name);
3040
3041 if (h_klass->HasSuperClass()) {
3042 StackHandleScope<1> h(Thread::Current());
3043 PrepareClass(runtime, h.NewHandle<mirror::Class>(h_klass->GetSuperClass()), done);
3044 }
3045
3046 if (!h_klass->IsTemp()) {
3047 runtime->GetClassLinker()->FillIMTAndConflictTables(h_klass.Get());
3048 }
3049 }
3050};
3051
Igor Murashkin37743352014-11-13 14:38:00 -08003052struct OatdumpArgs : public CmdlineArgs {
3053 protected:
3054 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003055
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003056 ParseStatus ParseCustom(const char* raw_option,
3057 size_t raw_option_length,
3058 std::string* error_msg) override {
3059 DCHECK_EQ(strlen(raw_option), raw_option_length);
Igor Murashkin37743352014-11-13 14:38:00 -08003060 {
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003061 ParseStatus base_parse = Base::ParseCustom(raw_option, raw_option_length, error_msg);
Igor Murashkin37743352014-11-13 14:38:00 -08003062 if (base_parse != kParseUnknownArgument) {
3063 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003064 }
3065 }
3066
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003067 std::string_view option(raw_option, raw_option_length);
3068 if (StartsWith(option, "--oat-file=")) {
3069 oat_filename_ = raw_option + strlen("--oat-file=");
3070 } else if (StartsWith(option, "--dex-file=")) {
3071 dex_filename_ = raw_option + strlen("--dex-file=");
3072 } else if (StartsWith(option, "--image=")) {
3073 image_location_ = raw_option + strlen("--image=");
Igor Murashkin37743352014-11-13 14:38:00 -08003074 } else if (option == "--no-dump:vmap") {
3075 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01003076 } else if (option =="--dump:code_info_stack_maps") {
3077 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003078 } else if (option == "--no-disassemble") {
3079 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003080 } else if (option =="--header-only") {
3081 dump_header_only_ = true;
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003082 } else if (StartsWith(option, "--symbolize=")) {
3083 oat_filename_ = raw_option + strlen("--symbolize=");
Igor Murashkin37743352014-11-13 14:38:00 -08003084 symbolize_ = true;
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003085 } else if (StartsWith(option, "--only-keep-debug")) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00003086 only_keep_debug_ = true;
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003087 } else if (StartsWith(option, "--class-filter=")) {
3088 class_filter_ = raw_option + strlen("--class-filter=");
3089 } else if (StartsWith(option, "--method-filter=")) {
3090 method_filter_ = raw_option + strlen("--method-filter=");
3091 } else if (StartsWith(option, "--list-classes")) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003092 list_classes_ = true;
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003093 } else if (StartsWith(option, "--list-methods")) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003094 list_methods_ = true;
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003095 } else if (StartsWith(option, "--export-dex-to=")) {
3096 export_dex_location_ = raw_option + strlen("--export-dex-to=");
3097 } else if (StartsWith(option, "--addr2instr=")) {
3098 if (!android::base::ParseUint(raw_option + strlen("--addr2instr="), &addr2instr_)) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003099 *error_msg = "Address conversion failed";
3100 return kParseError;
3101 }
Vladimir Marko8581e2a2019-02-06 15:54:55 +00003102 } else if (StartsWith(option, "--app-image=")) {
3103 app_image_ = raw_option + strlen("--app-image=");
3104 } else if (StartsWith(option, "--app-oat=")) {
3105 app_oat_ = raw_option + strlen("--app-oat=");
3106 } else if (StartsWith(option, "--dump-imt=")) {
3107 imt_dump_ = std::string(option.substr(strlen("--dump-imt=")));
Andreas Gampe9fded872016-09-25 16:08:35 -07003108 } else if (option == "--dump-imt-stats") {
3109 imt_stat_dump_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003110 } else {
3111 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003112 }
3113
Igor Murashkin37743352014-11-13 14:38:00 -08003114 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003115 }
3116
Roland Levillainf73caca2018-08-24 17:19:07 +01003117 ParseStatus ParseChecks(std::string* error_msg) override {
Igor Murashkin37743352014-11-13 14:38:00 -08003118 // Infer boot image location from the image location if possible.
3119 if (boot_image_location_ == nullptr) {
3120 boot_image_location_ = image_location_;
3121 }
3122
3123 // Perform the parent checks.
3124 ParseStatus parent_checks = Base::ParseChecks(error_msg);
3125 if (parent_checks != kParseOk) {
3126 return parent_checks;
3127 }
3128
3129 // Perform our own checks.
3130 if (image_location_ == nullptr && oat_filename_ == nullptr) {
3131 *error_msg = "Either --image or --oat-file must be specified";
3132 return kParseError;
3133 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
3134 *error_msg = "Either --image or --oat-file must be specified but not both";
3135 return kParseError;
3136 }
3137
3138 return kParseOk;
3139 }
3140
Andreas Gampefa6a1b02018-09-07 08:11:55 -07003141 std::string GetUsage() const override {
Igor Murashkin37743352014-11-13 14:38:00 -08003142 std::string usage;
3143
3144 usage +=
3145 "Usage: oatdump [options] ...\n"
3146 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
3147 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
3148 "\n"
3149 // Either oat-file or image is required.
3150 " --oat-file=<file.oat>: specifies an input oat filename.\n"
Mathieu Chartier2afa19d2019-09-10 16:07:21 -07003151 " Example: --oat-file=/system/framework/arm64/boot.oat\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003152 "\n"
3153 " --image=<file.art>: specifies an input image location.\n"
3154 " Example: --image=/system/framework/boot.art\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003155 "\n"
3156 " --app-image=<file.art>: specifies an input app image. Must also have a specified\n"
Roland Levillain4f1c9e62017-06-28 16:44:30 +01003157 " boot image (with --image) and app oat file (with --app-oat).\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003158 " Example: --app-image=app.art\n"
3159 "\n"
3160 " --app-oat=<file.odex>: specifies an input app oat.\n"
3161 " Example: --app-oat=app.odex\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003162 "\n";
3163
3164 usage += Base::GetUsage();
3165
3166 usage += // Optional.
Igor Murashkin37743352014-11-13 14:38:00 -08003167 " --no-dump:vmap may be used to disable vmap dumping.\n"
3168 " Example: --no-dump:vmap\n"
3169 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01003170 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
3171 " Example: --dump:code_info_stack_maps\n"
3172 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003173 " --no-disassemble may be used to disable disassembly.\n"
3174 " Example: --no-disassemble\n"
3175 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00003176 " --header-only may be used to print only the oat header.\n"
3177 " Example: --header-only\n"
3178 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003179 " --list-classes may be used to list target file classes (can be used with filters).\n"
3180 " Example: --list-classes\n"
3181 " Example: --list-classes --class-filter=com.example.foo\n"
3182 "\n"
3183 " --list-methods may be used to list target file methods (can be used with filters).\n"
3184 " Example: --list-methods\n"
3185 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
3186 "\n"
3187 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
3188 " Example: --symbolize=/system/framework/boot.oat\n"
3189 "\n"
David Srbecky2fdd03c2016-03-10 15:32:37 +00003190 " --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
3191 " .rodata and .text sections are omitted in the output file to save space.\n"
3192 " Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
3193 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003194 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
3195 " Example: --class-filter=com.example.foo\n"
3196 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003197 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
3198 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003199 "\n"
3200 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
3201 " Example: --export-dex-to=/data/local/tmp\n"
3202 "\n"
3203 " --addr2instr=<address>: output matching method disassembled code from relative\n"
3204 " address (e.g. PC from crash dump)\n"
3205 " Example: --addr2instr=0x00001a3b\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003206 "\n"
3207 " --dump-imt=<file.txt>: output IMT collisions (if any) for the given receiver\n"
3208 " types and interface methods in the given file. The file\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003209 " is read line-wise, where each line should either be a class\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003210 " name or descriptor, or a class name/descriptor and a prefix\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003211 " of a complete method name (separated by a whitespace).\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003212 " Example: --dump-imt=imt.txt\n"
3213 "\n"
3214 " --dump-imt-stats: output IMT statistics for the given boot image\n"
3215 " Example: --dump-imt-stats"
Igor Murashkin37743352014-11-13 14:38:00 -08003216 "\n";
3217
3218 return usage;
3219 }
3220
3221 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07003222 const char* oat_filename_ = nullptr;
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003223 const char* dex_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003224 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00003225 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07003226 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003227 std::string elf_filename_prefix_;
Andreas Gampe9fded872016-09-25 16:08:35 -07003228 std::string imt_dump_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003229 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01003230 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003231 bool disassemble_code_ = true;
3232 bool symbolize_ = false;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003233 bool only_keep_debug_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003234 bool list_classes_ = false;
3235 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003236 bool dump_header_only_ = false;
Andreas Gampe9fded872016-09-25 16:08:35 -07003237 bool imt_stat_dump_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003238 uint32_t addr2instr_ = 0;
3239 const char* export_dex_location_ = nullptr;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003240 const char* app_image_ = nullptr;
3241 const char* app_oat_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003242};
3243
Igor Murashkin37743352014-11-13 14:38:00 -08003244struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
Roland Levillainf73caca2018-08-24 17:19:07 +01003245 bool NeedsRuntime() override {
Igor Murashkin37743352014-11-13 14:38:00 -08003246 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003247
Igor Murashkin37743352014-11-13 14:38:00 -08003248 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
3249 bool absolute_addresses = (args_->oat_filename_ == nullptr);
3250
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003251 oat_dumper_options_.reset(new OatDumperOptions(
Igor Murashkin37743352014-11-13 14:38:00 -08003252 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01003253 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08003254 args_->disassemble_code_,
3255 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003256 args_->class_filter_,
3257 args_->method_filter_,
3258 args_->list_classes_,
3259 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00003260 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003261 args_->export_dex_location_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003262 args_->app_image_,
3263 args_->app_oat_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003264 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08003265
Andreas Gampe9fded872016-09-25 16:08:35 -07003266 return (args_->boot_image_location_ != nullptr ||
3267 args_->image_location_ != nullptr ||
3268 !args_->imt_dump_.empty()) &&
Igor Murashkin37743352014-11-13 14:38:00 -08003269 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003270 }
3271
Roland Levillainf73caca2018-08-24 17:19:07 +01003272 bool ExecuteWithoutRuntime() override {
Igor Murashkin37743352014-11-13 14:38:00 -08003273 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08003274 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003275
Mathieu Chartierd424d082014-10-15 10:31:46 -07003276 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08003277
Andreas Gampec24f3992014-12-17 20:40:11 -08003278 if (args_->symbolize_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00003279 // ELF has special kind of section called SHT_NOBITS which allows us to create
3280 // sections which exist but their data is omitted from the ELF file to save space.
3281 // This is what "strip --only-keep-debug" does when it creates separate ELF file
3282 // with only debug data. We use it in similar way to exclude .rodata and .text.
3283 bool no_bits = args_->only_keep_debug_;
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003284 return SymbolizeOat(args_->oat_filename_, args_->dex_filename_, args_->output_name_, no_bits)
3285 == EXIT_SUCCESS;
Andreas Gampec24f3992014-12-17 20:40:11 -08003286 } else {
3287 return DumpOat(nullptr,
3288 args_->oat_filename_,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003289 args_->dex_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003290 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08003291 args_->os_) == EXIT_SUCCESS;
3292 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07003293 }
3294
Andreas Gampefa6a1b02018-09-07 08:11:55 -07003295 bool ExecuteWithRuntime(Runtime* runtime) override {
Igor Murashkin37743352014-11-13 14:38:00 -08003296 CHECK(args_ != nullptr);
3297
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003298 if (!args_->imt_dump_.empty() || args_->imt_stat_dump_) {
3299 return IMTDumper::Dump(runtime,
3300 args_->imt_dump_,
3301 args_->imt_stat_dump_,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003302 args_->oat_filename_,
3303 args_->dex_filename_);
Andreas Gampe9fded872016-09-25 16:08:35 -07003304 }
3305
Igor Murashkin37743352014-11-13 14:38:00 -08003306 if (args_->oat_filename_ != nullptr) {
3307 return DumpOat(runtime,
3308 args_->oat_filename_,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003309 args_->dex_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003310 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08003311 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003312 }
Igor Murashkin37743352014-11-13 14:38:00 -08003313
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003314 return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003315 }
3316
Igor Murashkin37743352014-11-13 14:38:00 -08003317 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
3318};
Andreas Gampe00b25f32014-09-17 21:49:05 -07003319
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003320} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07003321
3322int main(int argc, char** argv) {
Andreas Gampe221d9812018-01-22 17:48:56 -08003323 // Output all logging to stderr.
3324 android::base::SetLogger(android::base::StderrLogger);
3325
Igor Murashkin37743352014-11-13 14:38:00 -08003326 art::OatdumpMain main;
3327 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07003328}