blob: 44050ff8edfdd272dd406a7a2bd97deafaa7e9fd [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrom78128a62011-09-15 17:21:19 -070016
17#include <stdio.h>
18#include <stdlib.h>
19
Brian Carlstrom27ec9612011-09-19 20:20:38 -070020#include <fstream>
21#include <iostream>
Igor Murashkin37743352014-11-13 14:38:00 -080022#include <map>
23#include <set>
Brian Carlstrom78128a62011-09-15 17:21:19 -070024#include <string>
Andreas Gampe54fc26c2014-09-04 21:47:42 -070025#include <unordered_map>
Andreas Gampe9fded872016-09-25 16:08:35 -070026#include <unordered_set>
Brian Carlstrom78128a62011-09-15 17:21:19 -070027#include <vector>
28
Andreas Gampe221d9812018-01-22 17:48:56 -080029#include "android-base/logging.h"
Andreas Gampe46ee31b2016-12-14 10:11:49 -080030#include "android-base/stringprintf.h"
Andreas Gampe9186ced2016-12-12 14:28:21 -080031#include "android-base/strings.h"
32
Ian Rogersd582fa42014-11-05 23:46:43 -080033#include "arch/instruction_set_features.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070034#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070035#include "art_method-inl.h"
Vladimir Markof3c52b42017-11-17 17:32:12 +000036#include "base/bit_utils_iterator.h"
David Sehr9c4a0152018-04-05 12:23:54 -070037#include "base/indenter.h"
David Sehrc431b9d2018-03-02 12:01:51 -080038#include "base/os.h"
David Sehr67bf42e2018-02-26 16:43:04 -080039#include "base/safe_map.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000040#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080041#include "base/unix_file/fd_file.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042#include "class_linker-inl.h"
Steven Morelande431e272017-07-18 16:53:49 -070043#include "class_linker.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010044#include "compiled_method.h"
David Srbecky32210b92017-12-04 14:39:21 +000045#include "debug/debug_info.h"
David Srbecky5d950762016-03-07 20:47:29 +000046#include "debug/elf_debug_writer.h"
47#include "debug/method_debug_info.h"
David Sehr9e734c72018-01-04 17:56:19 -080048#include "dex/code_item_accessors-inl.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080049#include "dex/descriptors_names.h"
David Sehr9e734c72018-01-04 17:56:19 -080050#include "dex/dex_file-inl.h"
51#include "dex/dex_instruction-inl.h"
David Sehr312f3b22018-03-19 08:39:26 -070052#include "dex/string_reference.h"
David Sehr9c4a0152018-04-05 12:23:54 -070053#include "dex/type_lookup_table.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080054#include "disassembler.h"
Andreas Gampe0c183382017-07-13 22:26:24 -070055#include "gc/accounting/space_bitmap-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070056#include "gc/space/image_space.h"
57#include "gc/space/large_object_space.h"
58#include "gc/space/space-inl.h"
Mathieu Chartier4a26f172016-01-26 14:26:18 -080059#include "image-inl.h"
Andreas Gampe9fded872016-09-25 16:08:35 -070060#include "imtable-inl.h"
Igor Murashkin86083f72017-10-27 10:59:04 -070061#include "subtype_check.h"
Vladimir Markof3c52b42017-11-17 17:32:12 +000062#include "index_bss_mapping.h"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -070063#include "interpreter/unstarted_runtime.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000064#include "linker/buffered_output_stream.h"
Vladimir Marko74527972016-11-29 15:57:32 +000065#include "linker/elf_builder.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000066#include "linker/file_output_stream.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080067#include "mirror/array-inl.h"
68#include "mirror/class-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010069#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080070#include "mirror/object-inl.h"
71#include "mirror/object_array-inl.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080072#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010073#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070074#include "oat_file_manager.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070075#include "scoped_thread_state_change-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070076#include "stack.h"
Mathieu Chartierdc00f182016-07-14 10:10:44 -070077#include "stack_map.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080078#include "thread_list.h"
Nicolas Geoffray4acefd32016-10-24 13:14:58 +010079#include "vdex_file.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080080#include "verifier/method_verifier.h"
Nicolas Geoffraye70dd562016-10-30 21:03:35 +000081#include "verifier/verifier_deps.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070082#include "well_known_classes.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070083
Igor Murashkin37743352014-11-13 14:38:00 -080084#include <sys/stat.h>
85#include "cmdline.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070086
Igor Murashkin37743352014-11-13 14:38:00 -080087namespace art {
Brian Carlstrom78128a62011-09-15 17:21:19 -070088
Andreas Gampe46ee31b2016-12-14 10:11:49 -080089using android::base::StringPrintf;
90
Mathieu Chartiere401d142015-04-22 13:56:20 -070091const char* image_methods_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -080092 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -070093 "kImtConflictMethod",
Mathieu Chartier2d2621a2014-10-23 16:48:06 -070094 "kImtUnimplementedMethod",
Vladimir Markofd36f1f2016-08-03 18:49:58 +010095 "kSaveAllCalleeSavesMethod",
96 "kSaveRefsOnlyMethod",
97 "kSaveRefsAndArgsMethod",
Vladimir Marko952dbb12016-07-28 12:01:51 +010098 "kSaveEverythingMethod",
Mingyao Yang0a87a652017-04-12 13:43:15 -070099 "kSaveEverythingMethodForClinit",
100 "kSaveEverythingMethodForSuspendCheck",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700101};
102
103const char* image_roots_descriptions_[] = {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700104 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700105 "kClassRoots",
Vladimir Markoeca3eda2016-11-09 16:26:44 +0000106 "kClassLoader",
Brian Carlstrom78128a62011-09-15 17:21:19 -0700107};
108
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700109// Map is so that we don't allocate multiple dex files for the same OatDexFile.
110static std::map<const OatFile::OatDexFile*,
111 std::unique_ptr<const DexFile>> opened_dex_files;
112
113const DexFile* OpenDexFile(const OatFile::OatDexFile* oat_dex_file, std::string* error_msg) {
114 DCHECK(oat_dex_file != nullptr);
115 auto it = opened_dex_files.find(oat_dex_file);
116 if (it != opened_dex_files.end()) {
117 return it->second.get();
118 }
119 const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
120 opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
121 return ret;
122}
123
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800124template <typename ElfTypes>
David Srbeckybc90fd02015-04-22 19:40:27 +0100125class OatSymbolizer FINAL {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700126 public:
David Srbecky2fdd03c2016-03-10 15:32:37 +0000127 OatSymbolizer(const OatFile* oat_file, const std::string& output_name, bool no_bits) :
128 oat_file_(oat_file),
129 builder_(nullptr),
130 output_name_(output_name.empty() ? "symbolized.oat" : output_name),
131 no_bits_(no_bits) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700132 }
133
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700134 bool Symbolize() {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000135 const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800136 std::unique_ptr<const InstructionSetFeatures> features = InstructionSetFeatures::FromBitmap(
David Srbecky5d811202016-03-08 13:21:22 +0000137 isa, oat_file_->GetOatHeader().GetInstructionSetFeaturesBitmap());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000138
Andreas Gampe08c277c2017-04-26 22:22:15 -0700139 std::unique_ptr<File> elf_file(OS::CreateEmptyFile(output_name_.c_str()));
140 if (elf_file == nullptr) {
141 return false;
142 }
Vladimir Marko74527972016-11-29 15:57:32 +0000143 std::unique_ptr<linker::BufferedOutputStream> output_stream =
144 std::make_unique<linker::BufferedOutputStream>(
145 std::make_unique<linker::FileOutputStream>(elf_file.get()));
146 builder_.reset(new linker::ElfBuilder<ElfTypes>(isa, features.get(), output_stream.get()));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000147
148 builder_->Start();
149
150 auto* rodata = builder_->GetRoData();
151 auto* text = builder_->GetText();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000152
David Srbecky6d8c8f02015-10-26 10:57:09 +0000153 const uint8_t* rodata_begin = oat_file_->Begin();
154 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
David Srbeckye155f4b2017-12-06 15:18:38 +0000155 if (!no_bits_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +0000156 rodata->Start();
157 rodata->WriteFully(rodata_begin, rodata_size);
158 rodata->End();
159 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000160
David Srbecky6d8c8f02015-10-26 10:57:09 +0000161 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
162 const size_t text_size = oat_file_->End() - text_begin;
David Srbeckye155f4b2017-12-06 15:18:38 +0000163 if (!no_bits_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +0000164 text->Start();
165 text->WriteFully(text_begin, text_size);
166 text->End();
167 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000168
Vladimir Marko33bff252017-11-01 14:35:42 +0000169 if (isa == InstructionSet::kMips || isa == InstructionSet::kMips64) {
Douglas Leung316a2182015-09-17 15:26:25 -0700170 builder_->WriteMIPSabiflagsSection();
171 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000172 builder_->PrepareDynamicSection(elf_file->GetPath(),
173 rodata_size,
174 text_size,
Vladimir Markob066d432018-01-03 13:14:37 +0000175 oat_file_->DataBimgRelRoSize(),
Vladimir Markoaad75c62016-10-03 08:46:48 +0000176 oat_file_->BssSize(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100177 oat_file_->BssMethodsOffset(),
David Srbeckyec2cdf42017-12-08 16:21:25 +0000178 oat_file_->BssRootsOffset(),
179 oat_file_->VdexSize());
Vladimir Marko944da602016-02-19 12:27:55 +0000180 builder_->WriteDynamicSection();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700181
Vladimir Marko1b404a82017-09-01 13:35:26 +0100182 const OatHeader& oat_header = oat_file_->GetOatHeader();
183 #define DO_TRAMPOLINE(fn_name) \
184 if (oat_header.Get ## fn_name ## Offset() != 0) { \
185 debug::MethodDebugInfo info = {}; \
David Srbeckyc684f332018-01-19 17:38:06 +0000186 info.custom_name = #fn_name; \
Vladimir Marko1b404a82017-09-01 13:35:26 +0100187 info.isa = oat_header.GetInstructionSet(); \
188 info.is_code_address_text_relative = true; \
189 size_t code_offset = oat_header.Get ## fn_name ## Offset(); \
190 code_offset -= CompiledCode::CodeDelta(oat_header.GetInstructionSet()); \
191 info.code_address = code_offset - oat_header.GetExecutableOffset(); \
192 info.code_size = 0; /* The symbol lasts until the next symbol. */ \
193 method_debug_infos_.push_back(std::move(info)); \
194 }
195 DO_TRAMPOLINE(InterpreterToInterpreterBridge)
196 DO_TRAMPOLINE(InterpreterToCompiledCodeBridge)
197 DO_TRAMPOLINE(JniDlsymLookup);
198 DO_TRAMPOLINE(QuickGenericJniTrampoline);
199 DO_TRAMPOLINE(QuickImtConflictTrampoline);
200 DO_TRAMPOLINE(QuickResolutionTrampoline);
201 DO_TRAMPOLINE(QuickToInterpreterBridge);
202 #undef DO_TRAMPOLINE
203
David Srbecky5d950762016-03-07 20:47:29 +0000204 Walk();
Vladimir Marko1b404a82017-09-01 13:35:26 +0100205
206 // TODO: Try to symbolize link-time thunks?
207 // This would require disassembling all methods to find branches outside the method code.
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700208
David Srbecky32210b92017-12-04 14:39:21 +0000209 // TODO: Add symbols for dex bytecode in the .dex section.
210
211 debug::DebugInfo debug_info{};
212 debug_info.compiled_methods = ArrayRef<const debug::MethodDebugInfo>(method_debug_infos_);
213
David Srbecky5d950762016-03-07 20:47:29 +0000214 debug::WriteDebugInfo(builder_.get(),
David Srbecky32210b92017-12-04 14:39:21 +0000215 debug_info,
David Srbecky5d950762016-03-07 20:47:29 +0000216 dwarf::DW_DEBUG_FRAME_FORMAT,
217 true /* write_oat_patches */);
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 Gampe54fc26c2014-09-04 21:47:42 -0700235 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
236 for (size_t i = 0; i < oat_dex_files.size(); i++) {
237 const OatFile::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
David Srbecky5d950762016-03-07 20:47:29 +0000243 void WalkOatDexFile(const OatFile::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) {
255 case kOatClassAllCompiled:
256 case kOatClassSomeCompiled:
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
260 case kOatClassNoneCompiled:
261 case kOatClassMax:
262 // 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) {
271 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700272 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700273 if (class_data == nullptr) { // empty class such as a marker interface?
274 return;
275 }
276 // Note: even if this is an interface or a native class, we still have to walk it, as there
277 // might be a static initializer.
278 ClassDataItemIterator it(dex_file, class_data);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700279 uint32_t class_method_idx = 0;
Mathieu Chartiere17cf242017-06-19 11:05:51 -0700280 it.SkipAllFields();
Mathieu Chartierb7c273c2017-11-10 18:07:56 -0800281 for (; it.HasNextMethod(); it.Next()) {
David Srbecky5d950762016-03-07 20:47:29 +0000282 WalkOatMethod(oat_class.GetOatMethod(class_method_idx++),
283 dex_file,
284 class_def_index,
285 it.GetMemberIndex(),
286 it.GetMethodCodeItem(),
287 it.GetMethodAccessFlags());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700288 }
289 DCHECK(!it.HasNext());
290 }
291
David Srbecky5d950762016-03-07 20:47:29 +0000292 void WalkOatMethod(const OatFile::OatMethod& oat_method,
293 const DexFile& dex_file,
294 uint32_t class_def_index,
295 uint32_t dex_method_index,
296 const DexFile::CodeItem* code_item,
297 uint32_t method_access_flags) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700298 if ((method_access_flags & kAccAbstract) != 0) {
299 // Abstract method, no code.
300 return;
301 }
David Srbecky5d950762016-03-07 20:47:29 +0000302 const OatHeader& oat_header = oat_file_->GetOatHeader();
303 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
304 if (method_header == nullptr || method_header->GetCodeSize() == 0) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700305 // No code.
306 return;
307 }
308
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100309 uint32_t entry_point = oat_method.GetCodeOffset() - oat_header.GetExecutableOffset();
310 // Clear Thumb2 bit.
311 const void* code_address = EntryPointToCodePointer(reinterpret_cast<void*>(entry_point));
312
Vladimir Marko1b404a82017-09-01 13:35:26 +0100313 debug::MethodDebugInfo info = {};
David Srbeckyc684f332018-01-19 17:38:06 +0000314 DCHECK(info.custom_name.empty());
David Srbecky5d950762016-03-07 20:47:29 +0000315 info.dex_file = &dex_file;
316 info.class_def_index = class_def_index;
317 info.dex_method_index = dex_method_index;
318 info.access_flags = method_access_flags;
319 info.code_item = code_item;
320 info.isa = oat_header.GetInstructionSet();
321 info.deduped = !seen_offsets_.insert(oat_method.GetCodeOffset()).second;
322 info.is_native_debuggable = oat_header.IsNativeDebuggable();
323 info.is_optimized = method_header->IsOptimized();
324 info.is_code_address_text_relative = true;
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100325 info.code_address = reinterpret_cast<uintptr_t>(code_address);
David Srbecky5d950762016-03-07 20:47:29 +0000326 info.code_size = method_header->GetCodeSize();
327 info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
328 info.code_info = info.is_optimized ? method_header->GetOptimizedCodeInfoPtr() : nullptr;
329 info.cfi = ArrayRef<uint8_t>();
330 method_debug_infos_.push_back(info);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700331 }
332
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700333 private:
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700334 const OatFile* oat_file_;
Vladimir Marko74527972016-11-29 15:57:32 +0000335 std::unique_ptr<linker::ElfBuilder<ElfTypes>> builder_;
David Srbecky5d950762016-03-07 20:47:29 +0000336 std::vector<debug::MethodDebugInfo> method_debug_infos_;
337 std::unordered_set<uint32_t> seen_offsets_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700338 const std::string output_name_;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000339 bool no_bits_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700340};
341
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700342class OatDumperOptions {
343 public:
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100344 OatDumperOptions(bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100345 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700346 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700347 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800348 const char* class_filter,
349 const char* method_filter,
350 bool list_classes,
351 bool list_methods,
David Brazdilc03d7b62016-03-02 12:18:03 +0000352 bool dump_header_only,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800353 const char* export_dex_location,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800354 const char* app_image,
355 const char* app_oat,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800356 uint32_t addr2instr)
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100357 : dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100358 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700359 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700360 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800361 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000362 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800363 list_classes_(list_classes),
364 list_methods_(list_methods),
David Brazdilc03d7b62016-03-02 12:18:03 +0000365 dump_header_only_(dump_header_only),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800366 export_dex_location_(export_dex_location),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800367 app_image_(app_image),
368 app_oat_(app_oat),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800369 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800370 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700371
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700372 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100373 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700374 const bool disassemble_code_;
375 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800376 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000377 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800378 const bool list_classes_;
379 const bool list_methods_;
David Brazdilc03d7b62016-03-02 12:18:03 +0000380 const bool dump_header_only_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800381 const char* const export_dex_location_;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800382 const char* const app_image_;
383 const char* const app_oat_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800384 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700385 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700386};
387
Elliott Hughese3c845c2012-02-28 17:23:01 -0800388class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700389 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100390 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000391 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800392 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700393 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800394 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800395 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
396 disassembler_(Disassembler::Create(instruction_set_,
Andreas Gampe372f3a32016-08-19 10:49:06 -0700397 new DisassemblerOptions(
398 options_.absolute_addresses_,
399 oat_file.Begin(),
400 oat_file.End(),
401 true /* can_read_literals_ */,
402 Is64BitInstructionSet(instruction_set_)
403 ? &Thread::DumpThreadOffset<PointerSize::k64>
404 : &Thread::DumpThreadOffset<PointerSize::k32>))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800405 CHECK(options_.class_loader_ != nullptr);
406 CHECK(options_.class_filter_ != nullptr);
407 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800408 AddAllOffsets();
409 }
410
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700411 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700412 delete disassembler_;
413 }
414
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800415 InstructionSet GetInstructionSet() {
416 return instruction_set_;
417 }
418
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300419 typedef std::vector<std::unique_ptr<const DexFile>> DexFileUniqV;
420
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700421 bool Dump(std::ostream& os) {
422 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800423 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700424
425 os << "MAGIC:\n";
426 os << oat_header.GetMagic() << "\n\n";
427
Jeff Hao4b07a6e2016-01-15 12:50:44 -0800428 os << "LOCATION:\n";
429 os << oat_file_.GetLocation() << "\n\n";
430
Brian Carlstromaded5f72011-10-07 17:15:04 -0700431 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800432 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700433
Elliott Hughesa72ec822012-03-05 17:12:22 -0800434 os << "INSTRUCTION SET:\n";
435 os << oat_header.GetInstructionSet() << "\n\n";
436
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700437 {
438 std::unique_ptr<const InstructionSetFeatures> features(
439 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
440 oat_header.GetInstructionSetFeaturesBitmap()));
441 os << "INSTRUCTION SET FEATURES:\n";
442 os << features->GetFeatureString() << "\n\n";
443 }
Dave Allison70202782013-10-22 17:52:19 -0700444
Brian Carlstromaded5f72011-10-07 17:15:04 -0700445 os << "DEX FILE COUNT:\n";
446 os << oat_header.GetDexFileCount() << "\n\n";
447
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800448#define DUMP_OAT_HEADER_OFFSET(label, offset) \
449 os << label " OFFSET:\n"; \
450 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800451 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800452 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
453 } \
454 os << StringPrintf("\n\n");
455
456 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
457 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
458 GetInterpreterToInterpreterBridgeOffset);
459 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
460 GetInterpreterToCompiledCodeBridgeOffset);
461 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
462 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800463 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
464 GetQuickGenericJniTrampolineOffset);
465 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
466 GetQuickImtConflictTrampolineOffset);
467 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
468 GetQuickResolutionTrampolineOffset);
469 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
470 GetQuickToInterpreterBridgeOffset);
471#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700472
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700473 os << "IMAGE PATCH DELTA:\n";
474 os << StringPrintf("%d (0x%08x)\n\n",
475 oat_header.GetImagePatchDelta(),
476 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700477
Brian Carlstrom28db0122012-10-18 16:20:41 -0700478 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
479 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
480
481 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800482 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700483
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700484 // Print the key-value store.
485 {
486 os << "KEY VALUE STORE:\n";
487 size_t index = 0;
488 const char* key;
489 const char* value;
490 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
491 os << key << " = " << value << "\n";
492 index++;
493 }
494 os << "\n";
495 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700496
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800497 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700498 os << "BEGIN:\n";
499 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700500
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700501 os << "END:\n";
502 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
503 }
504
505 os << "SIZE:\n";
506 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700507
508 os << std::flush;
509
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800510 // If set, adjust relative address to be searched
511 if (options_.addr2instr_ != 0) {
512 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
513 os << "SEARCH ADDRESS (executable offset + input):\n";
514 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
515 }
516
Vladimir Marko812fb4d2018-03-14 13:07:21 +0000517 // Dump .data.bimg.rel.ro entries.
518 DumpDataBimgRelRoEntries(os);
519
520 // Dump .bss summary, individual entries are dumped per dex file.
521 os << ".bss: ";
522 if (oat_file_.GetBssMethods().empty() && oat_file_.GetBssGcRoots().empty()) {
523 os << "empty.\n\n";
524 } else {
525 os << oat_file_.GetBssMethods().size() << " methods, ";
526 os << oat_file_.GetBssGcRoots().size() << " GC roots.\n\n";
527 }
528
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700529 // Dumping the dex file overview is compact enough to do even if header only.
530 DexFileData cumulative;
531 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
532 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
533 CHECK(oat_dex_file != nullptr);
534 std::string error_msg;
535 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
536 if (dex_file == nullptr) {
537 os << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation() << "': "
538 << error_msg;
539 continue;
540 }
541 DexFileData data(*dex_file);
542 os << "Dex file data for " << dex_file->GetLocation() << "\n";
543 data.Dump(os);
544 os << "\n";
Mathieu Chartier120aa282017-08-05 16:03:03 -0700545 const DexLayoutSections* const layout_sections = oat_dex_file->GetDexLayoutSections();
546 if (layout_sections != nullptr) {
547 os << "Layout data\n";
548 os << *layout_sections;
549 os << "\n";
550 }
551
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700552 cumulative.Add(data);
Vladimir Markof3c52b42017-11-17 17:32:12 +0000553
554 // Dump .bss entries.
555 DumpBssEntries(
556 os,
557 "ArtMethod",
558 oat_dex_file->GetMethodBssMapping(),
559 dex_file->NumMethodIds(),
560 static_cast<size_t>(GetInstructionSetPointerSize(instruction_set_)),
561 [=](uint32_t index) { return dex_file->PrettyMethod(index); });
562 DumpBssEntries(
563 os,
564 "Class",
565 oat_dex_file->GetTypeBssMapping(),
566 dex_file->NumTypeIds(),
567 sizeof(GcRoot<mirror::Class>),
568 [=](uint32_t index) { return dex_file->PrettyType(dex::TypeIndex(index)); });
569 DumpBssEntries(
570 os,
571 "String",
572 oat_dex_file->GetStringBssMapping(),
573 dex_file->NumStringIds(),
574 sizeof(GcRoot<mirror::Class>),
575 [=](uint32_t index) { return dex_file->StringDataByIdx(dex::StringIndex(index)); });
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700576 }
577 os << "Cumulative dex file data\n";
578 cumulative.Dump(os);
579 os << "\n";
580
David Brazdilc03d7b62016-03-02 12:18:03 +0000581 if (!options_.dump_header_only_) {
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000582 VariableIndentationOutputStream vios(&os);
Nicolas Geoffray3a293552018-03-02 10:52:16 +0000583 VdexFile::VerifierDepsHeader vdex_header = oat_file_.GetVdexFile()->GetVerifierDepsHeader();
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000584 if (vdex_header.IsValid()) {
585 std::string error_msg;
586 std::vector<const DexFile*> dex_files;
587 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
588 const DexFile* dex_file = OpenDexFile(oat_dex_files_[i], &error_msg);
589 if (dex_file == nullptr) {
590 os << "Error opening dex file: " << error_msg << std::endl;
591 return false;
592 }
593 dex_files.push_back(dex_file);
594 }
595 verifier::VerifierDeps deps(dex_files, oat_file_.GetVdexFile()->GetVerifierDepsData());
596 deps.Dump(&vios);
597 } else {
598 os << "UNRECOGNIZED vdex file, magic "
599 << vdex_header.GetMagic()
Nicolas Geoffray3a293552018-03-02 10:52:16 +0000600 << ", verifier deps version "
601 << vdex_header.GetVerifierDepsVersion()
602 << ", dex section version "
603 << vdex_header.GetDexSectionVersion()
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000604 << "\n";
605 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000606 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
607 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
608 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300609 if (!DumpOatDexFile(os, *oat_dex_file)) {
610 success = false;
611 }
612 }
613 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800614
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300615 if (options_.export_dex_location_) {
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000616 std::string error_msg;
617 std::string vdex_filename = GetVdexFilename(oat_file_.GetLocation());
618 if (!OS::FileExists(vdex_filename.c_str())) {
619 os << "File " << vdex_filename.c_str() << " does not exist\n";
620 return false;
621 }
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300622
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000623 DexFileUniqV vdex_dex_files;
624 std::unique_ptr<const VdexFile> vdex_file = OpenVdexUnquicken(vdex_filename,
625 &vdex_dex_files,
626 &error_msg);
627 if (vdex_file.get() == nullptr) {
628 os << "Failed to open vdex file: " << error_msg << "\n";
629 return false;
630 }
631 if (oat_dex_files_.size() != vdex_dex_files.size()) {
632 os << "Dex files number in Vdex file does not match Dex files number in Oat file: "
633 << vdex_dex_files.size() << " vs " << oat_dex_files_.size() << '\n';
634 return false;
635 }
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300636
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000637 size_t i = 0;
638 for (const auto& vdex_dex_file : vdex_dex_files) {
639 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
640 CHECK(oat_dex_file != nullptr);
641 CHECK(vdex_dex_file != nullptr);
642 if (!ExportDexFile(os, *oat_dex_file, vdex_dex_file.get())) {
643 success = false;
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300644 }
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000645 i++;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700646 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700647 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000648
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800649 {
650 os << "OAT FILE STATS:\n";
651 VariableIndentationOutputStream vios(&os);
652 stats_.Dump(vios);
653 }
654
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700655 os << std::flush;
656 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700657 }
658
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800659 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700660 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
661 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800662 return 0; // Address not in oat file
663 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800664 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
665 reinterpret_cast<uintptr_t>(oat_file_.Begin());
666 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800667 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800668 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800669 return end_offset - begin_offset;
670 }
671
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800672 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700673 return oat_file_.GetOatHeader().GetInstructionSet();
674 }
675
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700676 const void* GetQuickOatCode(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800677 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
678 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700679 CHECK(oat_dex_file != nullptr);
680 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700681 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
682 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700683 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
684 << "': " << error_msg;
685 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800686 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700687 const DexFile::ClassDef* class_def =
David Sehr9aa352e2016-09-15 18:13:52 -0700688 OatDexFile::FindClassDef(*dex_file, descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700689 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700690 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100691 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800692 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100693 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800694 }
695 }
696 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700697 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800698 }
699
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300700 // Returns nullptr and updates error_msg if the Vdex file cannot be opened, otherwise all Dex
701 // files are fully unquickened and stored in dex_files
702 std::unique_ptr<const VdexFile> OpenVdexUnquicken(const std::string& vdex_filename,
703 /* out */ DexFileUniqV* dex_files,
704 /* out */ std::string* error_msg) {
705 std::unique_ptr<const File> file(OS::OpenFileForReading(vdex_filename.c_str()));
706 if (file == nullptr) {
707 *error_msg = "Could not open file " + vdex_filename + " for reading.";
708 return nullptr;
709 }
710
711 int64_t vdex_length = file->GetLength();
712 if (vdex_length == -1) {
713 *error_msg = "Could not read the length of file " + vdex_filename;
714 return nullptr;
715 }
716
717 std::unique_ptr<MemMap> mmap(MemMap::MapFile(
718 file->GetLength(),
719 PROT_READ | PROT_WRITE,
720 MAP_PRIVATE,
721 file->Fd(),
722 /* start offset */ 0,
723 /* low_4gb */ false,
724 vdex_filename.c_str(),
725 error_msg));
726 if (mmap == nullptr) {
727 *error_msg = "Failed to mmap file " + vdex_filename + ": " + *error_msg;
728 return nullptr;
729 }
730
731 std::unique_ptr<VdexFile> vdex_file(new VdexFile(mmap.release()));
732 if (!vdex_file->IsValid()) {
733 *error_msg = "Vdex file is not valid";
734 return nullptr;
735 }
736
737 DexFileUniqV tmp_dex_files;
738 if (!vdex_file->OpenAllDexFiles(&tmp_dex_files, error_msg)) {
739 *error_msg = "Failed to open Dex files from Vdex: " + *error_msg;
740 return nullptr;
741 }
742
743 vdex_file->Unquicken(MakeNonOwningPointerVector(tmp_dex_files),
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +0300744 /* decompile_return_instruction */ true);
745
746 *dex_files = std::move(tmp_dex_files);
747 return vdex_file;
748 }
749
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800750 struct Stats {
751 enum ByteKind {
752 kByteKindCode,
753 kByteKindQuickMethodHeader,
754 kByteKindCodeInfoLocationCatalog,
755 kByteKindCodeInfoDexRegisterMap,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800756 kByteKindCodeInfoEncoding,
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800757 kByteKindCodeInfoInvokeInfo,
David Srbecky45aa5982016-03-18 02:15:09 +0000758 kByteKindCodeInfoStackMasks,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800759 kByteKindCodeInfoRegisterMasks,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800760 kByteKindStackMapNativePc,
761 kByteKindStackMapDexPc,
762 kByteKindStackMapDexRegisterMap,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800763 kByteKindStackMapInlineInfoIndex,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800764 kByteKindStackMapRegisterMaskIndex,
David Srbecky45aa5982016-03-18 02:15:09 +0000765 kByteKindStackMapStackMaskIndex,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700766 kByteKindInlineInfoMethodIndexIdx,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800767 kByteKindInlineInfoDexPc,
768 kByteKindInlineInfoExtraData,
769 kByteKindInlineInfoDexRegisterMap,
770 kByteKindInlineInfoIsLast,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800771 kByteKindCount,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800772 // Special ranges for std::accumulate convenience.
773 kByteKindStackMapFirst = kByteKindStackMapNativePc,
David Srbecky45aa5982016-03-18 02:15:09 +0000774 kByteKindStackMapLast = kByteKindStackMapStackMaskIndex,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700775 kByteKindInlineInfoFirst = kByteKindInlineInfoMethodIndexIdx,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800776 kByteKindInlineInfoLast = kByteKindInlineInfoIsLast,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800777 };
778 int64_t bits[kByteKindCount] = {};
779 // Since code has deduplication, seen tracks already seen pointers to avoid double counting
780 // deduplicated code and tables.
781 std::unordered_set<const void*> seen;
782
783 // Returns true if it was newly added.
784 bool AddBitsIfUnique(ByteKind kind, int64_t count, const void* address) {
785 if (seen.insert(address).second == true) {
786 // True means the address was not already in the set.
787 AddBits(kind, count);
788 return true;
789 }
790 return false;
791 }
792
793 void AddBits(ByteKind kind, int64_t count) {
794 bits[kind] += count;
795 }
796
797 void Dump(VariableIndentationOutputStream& os) {
798 const int64_t sum = std::accumulate(bits, bits + kByteKindCount, 0u);
799 os.Stream() << "Dumping cumulative use of " << sum / kBitsPerByte << " accounted bytes\n";
800 if (sum > 0) {
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800801 Dump(os, "Code ", bits[kByteKindCode], sum);
802 Dump(os, "QuickMethodHeader ", bits[kByteKindQuickMethodHeader], sum);
803 Dump(os, "CodeInfoEncoding ", bits[kByteKindCodeInfoEncoding], sum);
804 Dump(os, "CodeInfoLocationCatalog ", bits[kByteKindCodeInfoLocationCatalog], sum);
805 Dump(os, "CodeInfoDexRegisterMap ", bits[kByteKindCodeInfoDexRegisterMap], sum);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800806 Dump(os, "CodeInfoStackMasks ", bits[kByteKindCodeInfoStackMasks], sum);
807 Dump(os, "CodeInfoRegisterMasks ", bits[kByteKindCodeInfoRegisterMasks], sum);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800808 Dump(os, "CodeInfoInvokeInfo ", bits[kByteKindCodeInfoInvokeInfo], sum);
Mathieu Chartier1e083792017-02-08 13:30:04 -0800809 // Stack map section.
810 const int64_t stack_map_bits = std::accumulate(bits + kByteKindStackMapFirst,
811 bits + kByteKindStackMapLast + 1,
812 0u);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800813 Dump(os, "CodeInfoStackMap ", stack_map_bits, sum);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800814 {
815 ScopedIndentation indent1(&os);
816 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800817 "StackMapNativePc ",
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800818 bits[kByteKindStackMapNativePc],
819 stack_map_bits,
820 "stack map");
821 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800822 "StackMapDexPcEncoding ",
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800823 bits[kByteKindStackMapDexPc],
824 stack_map_bits,
825 "stack map");
826 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800827 "StackMapDexRegisterMap ",
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800828 bits[kByteKindStackMapDexRegisterMap],
829 stack_map_bits,
830 "stack map");
831 Dump(os,
Mathieu Chartier697dc662017-02-08 16:56:48 -0800832 "StackMapInlineInfoIndex ",
Mathieu Chartier1e083792017-02-08 13:30:04 -0800833 bits[kByteKindStackMapInlineInfoIndex],
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800834 stack_map_bits,
835 "stack map");
836 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800837 "StackMapRegisterMaskIndex ",
838 bits[kByteKindStackMapRegisterMaskIndex],
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800839 stack_map_bits,
840 "stack map");
841 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800842 "StackMapStackMaskIndex ",
David Srbecky45aa5982016-03-18 02:15:09 +0000843 bits[kByteKindStackMapStackMaskIndex],
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800844 stack_map_bits,
845 "stack map");
846 }
Mathieu Chartier1e083792017-02-08 13:30:04 -0800847 // Inline info section.
848 const int64_t inline_info_bits = std::accumulate(bits + kByteKindInlineInfoFirst,
849 bits + kByteKindInlineInfoLast + 1,
850 0u);
851 Dump(os, "CodeInfoInlineInfo ", inline_info_bits, sum);
852 {
853 ScopedIndentation indent1(&os);
854 Dump(os,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700855 "InlineInfoMethodIndexIdx ",
856 bits[kByteKindInlineInfoMethodIndexIdx],
Mathieu Chartier1e083792017-02-08 13:30:04 -0800857 inline_info_bits,
858 "inline info");
859 Dump(os,
860 "InlineInfoDexPc ",
861 bits[kByteKindStackMapDexPc],
862 inline_info_bits,
863 "inline info");
864 Dump(os,
865 "InlineInfoExtraData ",
866 bits[kByteKindInlineInfoExtraData],
867 inline_info_bits,
868 "inline info");
869 Dump(os,
870 "InlineInfoDexRegisterMap ",
871 bits[kByteKindInlineInfoDexRegisterMap],
872 inline_info_bits,
873 "inline info");
874 Dump(os,
875 "InlineInfoIsLast ",
876 bits[kByteKindInlineInfoIsLast],
877 inline_info_bits,
878 "inline info");
879 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800880 }
881 os.Stream() << "\n" << std::flush;
882 }
883
884 private:
885 void Dump(VariableIndentationOutputStream& os,
886 const char* name,
887 int64_t size,
888 int64_t total,
889 const char* sum_of = "total") {
890 const double percent = (static_cast<double>(size) / static_cast<double>(total)) * 100;
891 os.Stream() << StringPrintf("%s = %8" PRId64 " (%2.0f%% of %s)\n",
892 name,
893 size / kBitsPerByte,
894 percent,
895 sum_of);
896 }
897 };
898
Brian Carlstromaded5f72011-10-07 17:15:04 -0700899 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800900 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800901 // We don't know the length of the code for each method, but we need to know where to stop
902 // when disassembling. What we do know is that a region of code will be followed by some other
903 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
904 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800905 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
906 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700907 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700908 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700909 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
910 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700911 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
912 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800913 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800914 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800915 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800916 for (size_t class_def_index = 0;
917 class_def_index < dex_file->NumClassDefs();
918 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800919 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100920 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700921 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700922 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800923 ClassDataItemIterator it(*dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -0700924 it.SkipAllFields();
Elliott Hughese3c845c2012-02-28 17:23:01 -0800925 uint32_t class_method_index = 0;
Mathieu Chartierb7c273c2017-11-10 18:07:56 -0800926 while (it.HasNextMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100927 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800928 it.Next();
929 }
930 }
931 }
932 }
933
934 // If the last thing in the file is code for a method, there won't be an offset for the "next"
935 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
936 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800937 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800938 }
939
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700940 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
941 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
942 }
943
Elliott Hughese3c845c2012-02-28 17:23:01 -0800944 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800945 uint32_t code_offset = oat_method.GetCodeOffset();
Vladimir Marko33bff252017-11-01 14:35:42 +0000946 if (oat_file_.GetOatHeader().GetInstructionSet() == InstructionSet::kThumb2) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800947 code_offset &= ~0x1;
948 }
949 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800950 offsets_.insert(oat_method.GetVmapTableOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800951 }
952
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700953 // Dex file data, may be for multiple different dex files.
954 class DexFileData {
955 public:
956 DexFileData() {}
957
958 explicit DexFileData(const DexFile& dex_file)
959 : num_string_ids_(dex_file.NumStringIds()),
960 num_method_ids_(dex_file.NumMethodIds()),
961 num_field_ids_(dex_file.NumFieldIds()),
962 num_type_ids_(dex_file.NumTypeIds()),
963 num_class_defs_(dex_file.NumClassDefs()) {
964 for (size_t class_def_index = 0; class_def_index < num_class_defs_; ++class_def_index) {
965 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
966 WalkClass(dex_file, class_def);
967 }
968 }
969
970 void Add(const DexFileData& other) {
971 AddAll(unique_string_ids_from_code_, other.unique_string_ids_from_code_);
972 num_string_ids_from_code_ += other.num_string_ids_from_code_;
973 AddAll(dex_code_item_ptrs_, other.dex_code_item_ptrs_);
974 dex_code_bytes_ += other.dex_code_bytes_;
975 num_string_ids_ += other.num_string_ids_;
976 num_method_ids_ += other.num_method_ids_;
977 num_field_ids_ += other.num_field_ids_;
978 num_type_ids_ += other.num_type_ids_;
979 num_class_defs_ += other.num_class_defs_;
980 }
981
982 void Dump(std::ostream& os) {
983 os << "Num string ids: " << num_string_ids_ << "\n";
984 os << "Num method ids: " << num_method_ids_ << "\n";
985 os << "Num field ids: " << num_field_ids_ << "\n";
986 os << "Num type ids: " << num_type_ids_ << "\n";
987 os << "Num class defs: " << num_class_defs_ << "\n";
988 os << "Unique strings loaded from dex code: " << unique_string_ids_from_code_.size() << "\n";
989 os << "Total strings loaded from dex code: " << num_string_ids_from_code_ << "\n";
990 os << "Number of unique dex code items: " << dex_code_item_ptrs_.size() << "\n";
991 os << "Total number of dex code bytes: " << dex_code_bytes_ << "\n";
992 }
993
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800994 private:
Andreas Gampe9186ced2016-12-12 14:28:21 -0800995 // All of the elements from one container to another.
996 template <typename Dest, typename Src>
997 static void AddAll(Dest& dest, const Src& src) {
998 dest.insert(src.begin(), src.end());
999 }
1000
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001001 void WalkClass(const DexFile& dex_file, const DexFile::ClassDef& class_def) {
1002 const uint8_t* class_data = dex_file.GetClassData(class_def);
1003 if (class_data == nullptr) { // empty class such as a marker interface?
1004 return;
1005 }
1006 ClassDataItemIterator it(dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -07001007 it.SkipAllFields();
Mathieu Chartierb7c273c2017-11-10 18:07:56 -08001008 while (it.HasNextMethod()) {
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001009 WalkCodeItem(dex_file, it.GetMethodCodeItem());
1010 it.Next();
1011 }
1012 DCHECK(!it.HasNext());
1013 }
1014
1015 void WalkCodeItem(const DexFile& dex_file, const DexFile::CodeItem* code_item) {
1016 if (code_item == nullptr) {
1017 return;
1018 }
Mathieu Chartier698ebbc2018-01-05 11:00:42 -08001019 CodeItemInstructionAccessor instructions(dex_file, code_item);
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001020
1021 // If we inserted a new dex code item pointer, add to total code bytes.
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001022 const uint16_t* code_ptr = instructions.Insns();
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001023 if (dex_code_item_ptrs_.insert(code_ptr).second) {
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001024 dex_code_bytes_ += instructions.InsnsSizeInCodeUnits() * sizeof(code_ptr[0]);
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001025 }
1026
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001027 for (const DexInstructionPcPair& inst : instructions) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001028 switch (inst->Opcode()) {
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001029 case Instruction::CONST_STRING: {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001030 const dex::StringIndex string_index(inst->VRegB_21c());
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001031 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
1032 ++num_string_ids_from_code_;
1033 break;
1034 }
1035 case Instruction::CONST_STRING_JUMBO: {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07001036 const dex::StringIndex string_index(inst->VRegB_31c());
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001037 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
1038 ++num_string_ids_from_code_;
1039 break;
1040 }
1041 default:
1042 break;
1043 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001044 }
1045 }
1046
1047 // Unique string ids loaded from dex code.
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001048 std::set<StringReference> unique_string_ids_from_code_;
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001049
1050 // Total string ids loaded from dex code.
1051 size_t num_string_ids_from_code_ = 0;
1052
1053 // Unique code pointers.
1054 std::set<const void*> dex_code_item_ptrs_;
1055
1056 // Total "unique" dex code bytes.
1057 size_t dex_code_bytes_ = 0;
1058
1059 // Other dex ids.
1060 size_t num_string_ids_ = 0;
1061 size_t num_method_ids_ = 0;
1062 size_t num_field_ids_ = 0;
1063 size_t num_type_ids_ = 0;
1064 size_t num_class_defs_ = 0;
1065 };
1066
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001067 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
1068 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001069 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001070 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -08001071 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -08001072 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -07001073
Andreas Gampe2ba88952016-04-29 17:52:07 -07001074 const uint8_t* const oat_file_begin = oat_dex_file.GetOatFile()->Begin();
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00001075 if (oat_dex_file.GetOatFile()->ContainsDexCode()) {
1076 const uint8_t* const vdex_file_begin = oat_dex_file.GetOatFile()->DexBegin();
David Brazdil7b49e6c2016-09-01 11:06:18 +01001077
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00001078 // Print data range of the dex file embedded inside the corresponding vdex file.
1079 const uint8_t* const dex_file_pointer = oat_dex_file.GetDexFilePointer();
1080 uint32_t dex_offset = dchecked_integral_cast<uint32_t>(dex_file_pointer - vdex_file_begin);
1081 os << StringPrintf(
1082 "dex-file: 0x%08x..0x%08x\n",
1083 dex_offset,
1084 dchecked_integral_cast<uint32_t>(dex_offset + oat_dex_file.FileSize() - 1));
1085 } else {
1086 os << StringPrintf("dex-file not in VDEX file\n");
1087 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001088
Andreas Gampe2ba88952016-04-29 17:52:07 -07001089 // Create the dex file early. A lot of print-out things depend on it.
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001090 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07001091 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
1092 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001093 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001094 os << std::flush;
1095 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001096 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001097
Andreas Gampe2ba88952016-04-29 17:52:07 -07001098 // Print lookup table, if it exists.
1099 if (oat_dex_file.GetLookupTableData() != nullptr) {
1100 uint32_t table_offset = dchecked_integral_cast<uint32_t>(
1101 oat_dex_file.GetLookupTableData() - oat_file_begin);
David Sehr9aa352e2016-09-15 18:13:52 -07001102 uint32_t table_size = TypeLookupTable::RawDataLength(dex_file->NumClassDefs());
Andreas Gampe2ba88952016-04-29 17:52:07 -07001103 os << StringPrintf("type-table: 0x%08x..0x%08x\n",
1104 table_offset,
1105 table_offset + table_size - 1);
1106 }
1107
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001108 VariableIndentationOutputStream vios(&os);
1109 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001110 for (size_t class_def_index = 0;
1111 class_def_index < dex_file->NumClassDefs();
1112 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001113 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
1114 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001115
1116 // TODO: Support regex
1117 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
1118 continue;
1119 }
1120
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001121 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001122 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001123 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
Andreas Gampea5b09a62016-11-17 15:21:22 -08001124 class_def_index, descriptor, oat_class_offset, class_def.class_idx_.index_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +01001125 << " (" << oat_class.GetStatus() << ")"
1126 << " (" << oat_class.GetType() << ")\n";
1127 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001128 if (options_.list_classes_) {
1129 continue;
1130 }
1131 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001132 success = false;
1133 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001134 if (stop_analysis) {
1135 os << std::flush;
1136 return success;
1137 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001138 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001139 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001140 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001141 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001142 }
1143
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +03001144 // Backwards compatible Dex file export. If dex_file is nullptr (valid Vdex file not present) the
1145 // Dex resource is extracted from the oat_dex_file and its checksum is repaired since it's not
1146 // unquickened. Otherwise the dex_file has been fully unquickened and is expected to verify the
1147 // original checksum.
1148 bool ExportDexFile(std::ostream& os,
1149 const OatFile::OatDexFile& oat_dex_file,
1150 const DexFile* dex_file) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001151 std::string error_msg;
1152 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001153 size_t fsize = oat_dex_file.FileSize();
1154
1155 // Some quick checks just in case
1156 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
1157 os << "Invalid dex file\n";
1158 return false;
1159 }
1160
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +03001161 if (dex_file == nullptr) {
1162 // Exported bytecode is quickened (dex-to-dex transformations present)
1163 dex_file = OpenDexFile(&oat_dex_file, &error_msg);
1164 if (dex_file == nullptr) {
1165 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
1166 return false;
1167 }
1168
1169 // Recompute checksum
1170 reinterpret_cast<DexFile::Header*>(const_cast<uint8_t*>(dex_file->Begin()))->checksum_ =
1171 dex_file->CalculateChecksum();
1172 } else {
1173 // Vdex unquicken output should match original input bytecode
1174 uint32_t orig_checksum =
1175 reinterpret_cast<DexFile::Header*>(const_cast<uint8_t*>(dex_file->Begin()))->checksum_;
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001176 CHECK_EQ(orig_checksum, dex_file->CalculateChecksum());
Anestis Bechtsoudisa1f56a82017-10-08 23:37:10 +03001177 if (orig_checksum != dex_file->CalculateChecksum()) {
1178 os << "Unexpected checksum from unquicken dex file '" << dex_file_location << "'\n";
1179 return false;
1180 }
1181 }
1182
Mathieu Chartier567dc6f2018-04-05 16:37:14 -07001183 // Update header for shared section.
1184 uint32_t shared_section_offset = 0u;
1185 uint32_t shared_section_size = 0u;
1186 if (dex_file->IsCompactDexFile()) {
1187 CompactDexFile::Header* const header =
1188 reinterpret_cast<CompactDexFile::Header*>(const_cast<uint8_t*>(dex_file->Begin()));
1189 shared_section_offset = header->data_off_;
1190 shared_section_size = header->data_size_;
1191 // The shared section will be serialized right after the dex file.
1192 header->data_off_ = header->file_size_;
1193 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001194 // Verify output directory exists
1195 if (!OS::DirectoryExists(options_.export_dex_location_)) {
1196 // TODO: Extend OS::DirectoryExists if symlink support is required
1197 os << options_.export_dex_location_ << " output directory not found or symlink\n";
1198 return false;
1199 }
1200
1201 // Beautify path names
1202 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
1203 return false;
1204 }
1205
1206 std::string dex_orig_name;
1207 size_t dex_orig_pos = dex_file_location.rfind('/');
1208 if (dex_orig_pos == std::string::npos)
1209 dex_orig_name = dex_file_location;
1210 else
1211 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
1212
1213 // A more elegant approach to efficiently name user installed apps is welcome
Andreas Gampef812d8c2017-02-17 10:19:44 -08001214 if (dex_orig_name.size() == 8 &&
1215 dex_orig_name.compare("base.apk") == 0 &&
1216 dex_orig_pos != std::string::npos) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001217 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
1218 size_t apk_orig_pos = dex_file_location.rfind('/');
1219 if (apk_orig_pos != std::string::npos) {
1220 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
1221 }
1222 }
1223
1224 std::string out_dex_path(options_.export_dex_location_);
1225 if (out_dex_path.back() != '/') {
1226 out_dex_path.append("/");
1227 }
1228 out_dex_path.append(dex_orig_name);
1229 out_dex_path.append("_export.dex");
1230 if (out_dex_path.length() > PATH_MAX) {
1231 return false;
1232 }
1233
1234 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
1235 if (file.get() == nullptr) {
1236 os << "Failed to open output dex file " << out_dex_path;
1237 return false;
1238 }
1239
Mathieu Chartier567dc6f2018-04-05 16:37:14 -07001240 bool success = file->WriteFully(dex_file->Begin(), fsize);
Mathieu Chartierc3a22aa2018-01-19 18:58:34 -08001241 if (!success) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001242 os << "Failed to write dex file";
1243 file->Erase();
1244 return false;
1245 }
1246
Mathieu Chartier567dc6f2018-04-05 16:37:14 -07001247 if (shared_section_size != 0) {
1248 success = file->WriteFully(dex_file->Begin() + shared_section_offset, shared_section_size);
1249 if (!success) {
1250 os << "Failed to write shared data section";
1251 file->Erase();
1252 return false;
1253 }
1254 }
1255
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001256 if (file->FlushCloseOrErase() != 0) {
1257 os << "Flush and close failed";
1258 return false;
1259 }
1260
1261 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
1262 os << std::flush;
1263
1264 return true;
1265 }
1266
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001267 bool DumpOatClass(VariableIndentationOutputStream* vios,
1268 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001269 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001270 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001271 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -07001272 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001273 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001274 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001275 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -08001276 }
1277 ClassDataItemIterator it(dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -07001278 it.SkipAllFields();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001279 uint32_t class_method_index = 0;
Mathieu Chartierb7c273c2017-11-10 18:07:56 -08001280 while (it.HasNextMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001281 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001282 it.GetMemberIndex(), it.GetMethodCodeItem(),
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001283 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001284 success = false;
1285 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001286 if (addr_found) {
1287 *stop_analysis = true;
1288 return success;
1289 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001290 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -07001291 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001292 }
Ian Rogers0571d352011-11-03 19:51:38 -07001293 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001294 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001295 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001296 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001297
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001298 static constexpr uint32_t kPrologueBytes = 16;
1299
1300 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
1301 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
1302
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001303 bool DumpOatMethod(VariableIndentationOutputStream* vios,
1304 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -07001305 uint32_t class_method_index,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001306 const OatFile::OatClass& oat_class,
1307 const DexFile& dex_file,
1308 uint32_t dex_method_idx,
1309 const DexFile::CodeItem* code_item,
1310 uint32_t method_access_flags,
1311 bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001312 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001313
Mathieu Chartier698ebbc2018-01-05 11:00:42 -08001314 CodeItemDataAccessor code_item_accessor(dex_file, code_item);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001315
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001316 // TODO: Support regex
1317 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
1318 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00001319 return success;
1320 }
1321
David Sehr709b0702016-10-13 09:12:37 -07001322 std::string pretty_method = dex_file.PrettyMethod(dex_method_idx, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001323 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
1324 class_method_index, pretty_method.c_str(),
1325 dex_method_idx);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001326 if (options_.list_methods_) {
1327 return success;
1328 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001329
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001330 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
1331 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
1332 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
1333 uint32_t code_offset = oat_method.GetCodeOffset();
1334 uint32_t code_size = oat_method.GetQuickCodeSize();
1335 if (resolved_addr2instr_ != 0) {
1336 if (resolved_addr2instr_ > code_offset + code_size) {
1337 return success;
1338 } else {
1339 *addr_found = true; // stop analyzing file at next iteration
1340 }
1341 }
1342
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001343 // Everything below is indented at least once.
1344 ScopedIndentation indent1(vios);
1345
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001346 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001347 vios->Stream() << "DEX CODE:\n";
1348 ScopedIndentation indent2(vios);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001349 if (code_item_accessor.HasCodeItem()) {
1350 for (const DexInstructionPcPair& inst : code_item_accessor) {
1351 vios->Stream() << StringPrintf("0x%04x: ", inst.DexPc()) << inst->DumpHexLE(5)
1352 << StringPrintf("\t| %s\n", inst->DumpString(&dex_file).c_str());
1353 }
1354 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001355 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001356
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001357 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001358 std::unique_ptr<verifier::MethodVerifier> verifier;
1359 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001360 // We need to have the handle scope stay live until after the verifier since the verifier has
1361 // a handle to the dex cache from hs.
1362 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001363 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
1364 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001365 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001366 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001367 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -07001368 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001369 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001370 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001371 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001372 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001373 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001374 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001375 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001376 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001377 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
1378 oat_method_offsets_offset, oat_file_.Size());
1379 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001380 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001381 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001382 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001383
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001384 ScopedIndentation indent2(vios);
1385 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001386 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
1387 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001388 vios->Stream() << StringPrintf("WARNING: "
1389 "code offset 0x%08x is past end of file 0x%08zx.\n",
1390 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001391 success = false;
1392 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001393 vios->Stream() << "\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001394 }
1395 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001396 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001397 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
1398 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001399 stats_.AddBitsIfUnique(Stats::kByteKindQuickMethodHeader,
1400 sizeof(*method_header) * kBitsPerByte,
1401 method_header);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001402 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001403 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001404 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001405 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001406 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001407 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001408 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
1409 method_header_offset, oat_file_.Size());
1410 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001411 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001412 return false;
1413 }
1414
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001415 ScopedIndentation indent2(vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001416 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001417 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001418 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001419 }
Mingyao Yang063fc772016-08-02 11:02:54 -07001420 uint32_t vmap_table_offset = method_header ==
1421 nullptr ? 0 : method_header->GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001422 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001423
1424 size_t vmap_table_offset_limit =
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +00001425 IsMethodGeneratedByDexToDexCompiler(oat_method, code_item_accessor)
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001426 ? oat_file_.GetVdexFile()->Size()
1427 : method_header->GetCode() - oat_file_.Begin();
1428 if (vmap_table_offset >= vmap_table_offset_limit) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001429 vios->Stream() << StringPrintf("WARNING: "
1430 "vmap table offset 0x%08x is past end of file 0x%08zx. "
1431 "vmap table offset was loaded from offset 0x%08x.\n",
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001432 vmap_table_offset,
1433 vmap_table_offset_limit,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001434 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001435 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001436 } else if (options_.dump_vmap_) {
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001437 DumpVmapData(vios, oat_method, code_item_accessor);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001438 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001439 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001440 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001441 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001442
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001443 ScopedIndentation indent2(vios);
1444 vios->Stream()
1445 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
1446 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
1447 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
1448 vios->Stream() << "\n";
1449 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
1450 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
1451 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001452 }
1453 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001454 // Based on spill masks from QuickMethodFrameInfo so placed
1455 // after it is dumped, but useful for understanding quick
1456 // code, so dumped here.
1457 ScopedIndentation indent2(vios);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001458 DumpVregLocations(vios->Stream(), oat_method, code_item_accessor);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001459 }
1460 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001461 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001462 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
1463 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001464 ScopedIndentation indent2(vios);
1465 vios->Stream() << StringPrintf("WARNING: "
1466 "code size offset 0x%08x is past end of file 0x%08zx.",
1467 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001468 success = false;
1469 } else {
1470 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001471 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1472 uint64_t aligned_code_end = aligned_code_begin + code_size;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001473 stats_.AddBitsIfUnique(Stats::kByteKindCode, code_size * kBitsPerByte, code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001474
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001475 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001476 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001477 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001478 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
1479 code_offset,
1480 code_size_offset,
1481 code_size,
1482 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001483
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001484 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001485 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001486 vios->Stream() << StringPrintf("WARNING: "
1487 "start of code at 0x%08x is past end of file 0x%08zx.",
1488 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001489 success = false;
1490 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001491 vios->Stream() << StringPrintf(
1492 "WARNING: "
1493 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
1494 "code size is 0x%08x loaded from offset 0x%08x.\n",
1495 aligned_code_end, oat_file_.Size(),
1496 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001497 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001498 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001499 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001500 DumpCode(vios, oat_method, code_item_accessor, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001501 }
1502 }
1503 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001504 vios->Stream() << StringPrintf(
1505 "WARNING: "
1506 "code size %d is bigger than max expected threshold of %d. "
1507 "code size is 0x%08x loaded from offset 0x%08x.\n",
1508 code_size, kMaxCodeSize,
1509 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001510 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001511 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001512 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001513 DumpCode(vios, oat_method, code_item_accessor, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001514 }
1515 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001516 } else if (options_.disassemble_code_) {
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001517 DumpCode(vios, oat_method, code_item_accessor, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001518 }
1519 }
1520 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001521 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001522 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001523 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001524
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001525 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1526 if (spill_mask == 0) {
1527 return;
1528 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001529 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001530 for (size_t i = 0; i < 32; i++) {
1531 if ((spill_mask & (1 << i)) != 0) {
1532 if (is_float) {
1533 os << "fr" << i;
1534 } else {
1535 os << "r" << i;
1536 }
1537 spill_mask ^= 1 << i; // clear bit
1538 if (spill_mask != 0) {
1539 os << ", ";
1540 } else {
1541 break;
1542 }
1543 }
1544 }
1545 os << ")";
1546 }
1547
Roland Levillain442b46a2015-02-18 16:54:21 +00001548 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001549 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001550 const OatFile::OatMethod& oat_method,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001551 const CodeItemDataAccessor& code_item_accessor) {
1552 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item_accessor)) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001553 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001554 const void* raw_code_info = oat_method.GetVmapTable();
1555 if (raw_code_info != nullptr) {
1556 CodeInfo code_info(raw_code_info);
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001557 DCHECK(code_item_accessor.HasCodeItem());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001558 ScopedIndentation indent1(vios);
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001559 MethodInfo method_info = oat_method.GetOatQuickMethodHeader()->GetOptimizedMethodInfo();
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001560 DumpCodeInfo(vios, code_info, oat_method, code_item_accessor, method_info);
Roland Levillain442b46a2015-02-18 16:54:21 +00001561 }
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001562 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item_accessor)) {
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001563 // We don't encode the size in the table, so just emit that we have quickened
1564 // information.
1565 ScopedIndentation indent(vios);
1566 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001567 } else {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001568 // Otherwise, there is nothing to display.
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001569 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001570 }
1571
1572 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001573 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001574 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001575 const OatFile::OatMethod& oat_method,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001576 const CodeItemDataAccessor& code_item_accessor,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001577 const MethodInfo& method_info) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001578 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001579 oat_method.GetCodeOffset(),
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001580 code_item_accessor.RegistersSize(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001581 options_.dump_code_info_stack_maps_,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001582 instruction_set_,
1583 method_info);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001584 }
1585
Andreas Gampe36a296f2017-06-13 14:11:11 -07001586 static int GetOutVROffset(uint16_t out_num, InstructionSet isa) {
1587 // According to stack model, the first out is above the Method referernce.
1588 return static_cast<size_t>(InstructionSetPointerSize(isa)) + out_num * sizeof(uint32_t);
1589 }
1590
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001591 static uint32_t GetVRegOffsetFromQuickCode(const CodeItemDataAccessor& code_item_accessor,
Andreas Gampe36a296f2017-06-13 14:11:11 -07001592 uint32_t core_spills,
1593 uint32_t fp_spills,
1594 size_t frame_size,
1595 int reg,
1596 InstructionSet isa) {
1597 PointerSize pointer_size = InstructionSetPointerSize(isa);
1598 if (kIsDebugBuild) {
1599 auto* runtime = Runtime::Current();
1600 if (runtime != nullptr) {
1601 CHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), pointer_size);
1602 }
1603 }
1604 DCHECK_ALIGNED(frame_size, kStackAlignment);
1605 DCHECK_NE(reg, -1);
1606 int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa)
1607 + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa)
1608 + sizeof(uint32_t); // Filler.
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001609 int num_regs = code_item_accessor.RegistersSize() - code_item_accessor.InsSize();
1610 int temp_threshold = code_item_accessor.RegistersSize();
Andreas Gampe36a296f2017-06-13 14:11:11 -07001611 const int max_num_special_temps = 1;
1612 if (reg == temp_threshold) {
1613 // The current method pointer corresponds to special location on stack.
1614 return 0;
1615 } else if (reg >= temp_threshold + max_num_special_temps) {
1616 /*
1617 * Special temporaries may have custom locations and the logic above deals with that.
1618 * However, non-special temporaries are placed relative to the outs.
1619 */
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001620 int temps_start = code_item_accessor.OutsSize() * sizeof(uint32_t)
Andreas Gampe36a296f2017-06-13 14:11:11 -07001621 + static_cast<size_t>(pointer_size) /* art method */;
1622 int relative_offset = (reg - (temp_threshold + max_num_special_temps)) * sizeof(uint32_t);
1623 return temps_start + relative_offset;
1624 } else if (reg < num_regs) {
1625 int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t);
1626 return locals_start + (reg * sizeof(uint32_t));
1627 } else {
1628 // Handle ins.
1629 return frame_size + ((reg - num_regs) * sizeof(uint32_t))
1630 + static_cast<size_t>(pointer_size) /* art method */;
1631 }
1632 }
1633
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001634 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001635 const CodeItemDataAccessor& code_item_accessor) {
1636 if (code_item_accessor.HasCodeItem()) {
1637 size_t num_locals_ins = code_item_accessor.RegistersSize();
1638 size_t num_ins = code_item_accessor.InsSize();
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001639 size_t num_locals = num_locals_ins - num_ins;
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001640 size_t num_outs = code_item_accessor.OutsSize();
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001641
1642 os << "vr_stack_locations:";
1643 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1644 // For readability, delimit the different kinds of VRs.
1645 if (reg == num_locals_ins) {
1646 os << "\n\tmethod*:";
1647 } else if (reg == num_locals && num_ins > 0) {
1648 os << "\n\tins:";
1649 } else if (reg == 0 && num_locals > 0) {
1650 os << "\n\tlocals:";
1651 }
1652
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001653 uint32_t offset = GetVRegOffsetFromQuickCode(code_item_accessor,
Andreas Gampe36a296f2017-06-13 14:11:11 -07001654 oat_method.GetCoreSpillMask(),
1655 oat_method.GetFpSpillMask(),
1656 oat_method.GetFrameSizeInBytes(),
1657 reg,
1658 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001659 os << " v" << reg << "[sp + #" << offset << "]";
1660 }
1661
1662 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1663 if (out_reg == 0) {
1664 os << "\n\touts:";
1665 }
1666
Andreas Gampe36a296f2017-06-13 14:11:11 -07001667 uint32_t offset = GetOutVROffset(out_reg, GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001668 os << " v" << out_reg << "[sp + #" << offset << "]";
1669 }
1670
1671 os << "\n";
1672 }
1673 }
1674
Roland Levillainf2650d12015-05-28 14:53:28 +01001675 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1676 // the optimizing compiler?
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001677 static bool IsMethodGeneratedByOptimizingCompiler(
1678 const OatFile::OatMethod& oat_method,
1679 const CodeItemDataAccessor& code_item_accessor) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001680 // If the native GC map is null and the Dex `code_item` is not
1681 // null, then this method has been compiled with the optimizing
1682 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001683 return oat_method.GetQuickCode() != nullptr &&
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001684 oat_method.GetVmapTable() != nullptr &&
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001685 code_item_accessor.HasCodeItem();
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001686 }
1687
1688 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1689 // the dextodex compiler?
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001690 static bool IsMethodGeneratedByDexToDexCompiler(
1691 const OatFile::OatMethod& oat_method,
1692 const CodeItemDataAccessor& code_item_accessor) {
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001693 // If the quick code is null, the Dex `code_item` is not
1694 // null, and the vmap table is not null, then this method has been compiled
1695 // with the dextodex compiler.
1696 return oat_method.GetQuickCode() == nullptr &&
1697 oat_method.GetVmapTable() != nullptr &&
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001698 code_item_accessor.HasCodeItem();
Roland Levillainf2650d12015-05-28 14:53:28 +01001699 }
1700
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001701 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001702 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001703 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001704 const DexFile* dex_file,
1705 const DexFile::ClassDef& class_def,
1706 const DexFile::CodeItem* code_item,
1707 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001708 if ((method_access_flags & kAccNative) == 0) {
1709 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001710 Runtime* const runtime = Runtime::Current();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001711 DCHECK(options_.class_loader_ != nullptr);
Vladimir Marko421087b2018-02-27 11:00:17 +00001712 Handle<mirror::DexCache> dex_cache = hs->NewHandle(
1713 runtime->GetClassLinker()->RegisterDexFile(*dex_file, options_.class_loader_->Get()));
1714 CHECK(dex_cache != nullptr);
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00001715 ArtMethod* method = runtime->GetClassLinker()->ResolveMethodWithoutInvokeType(
1716 dex_method_idx, dex_cache, *options_.class_loader_);
Andreas Gampe03da7842018-04-12 11:12:52 -07001717 if (method == nullptr) {
1718 soa.Self()->ClearException();
1719 return nullptr;
1720 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001721 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001722 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
Nicolas Geoffrayb041a402017-11-13 15:16:22 +00001723 class_def, code_item, method, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001724 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001725
1726 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001727 }
1728
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001729 // The StackMapsHelper provides the stack maps in the native PC order.
1730 // For identical native PCs, the order from the CodeInfo is preserved.
1731 class StackMapsHelper {
1732 public:
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001733 explicit StackMapsHelper(const uint8_t* raw_code_info, InstructionSet instruction_set)
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001734 : code_info_(raw_code_info),
1735 encoding_(code_info_.ExtractEncoding()),
1736 number_of_stack_maps_(code_info_.GetNumberOfStackMaps(encoding_)),
1737 indexes_(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001738 offset_(static_cast<uint32_t>(-1)),
1739 stack_map_index_(0u),
1740 instruction_set_(instruction_set) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001741 if (number_of_stack_maps_ != 0u) {
1742 // Check if native PCs are ordered.
1743 bool ordered = true;
1744 StackMap last = code_info_.GetStackMapAt(0u, encoding_);
1745 for (size_t i = 1; i != number_of_stack_maps_; ++i) {
1746 StackMap current = code_info_.GetStackMapAt(i, encoding_);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001747 if (last.GetNativePcOffset(encoding_.stack_map.encoding, instruction_set) >
1748 current.GetNativePcOffset(encoding_.stack_map.encoding, instruction_set)) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001749 ordered = false;
1750 break;
1751 }
1752 last = current;
1753 }
1754 if (!ordered) {
1755 // Create indirection indexes for access in native PC order. We do not optimize
1756 // for the fact that there can currently be only two separately ordered ranges,
1757 // namely normal stack maps and catch-point stack maps.
1758 indexes_.resize(number_of_stack_maps_);
1759 std::iota(indexes_.begin(), indexes_.end(), 0u);
1760 std::sort(indexes_.begin(),
1761 indexes_.end(),
1762 [this](size_t lhs, size_t rhs) {
1763 StackMap left = code_info_.GetStackMapAt(lhs, encoding_);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001764 uint32_t left_pc = left.GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001765 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001766 StackMap right = code_info_.GetStackMapAt(rhs, encoding_);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001767 uint32_t right_pc = right.GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001768 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001769 // If the PCs are the same, compare indexes to preserve the original order.
1770 return (left_pc < right_pc) || (left_pc == right_pc && lhs < rhs);
1771 });
1772 }
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001773 offset_ = GetStackMapAt(0).GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001774 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001775 }
1776 }
1777
1778 const CodeInfo& GetCodeInfo() const {
1779 return code_info_;
1780 }
1781
1782 const CodeInfoEncoding& GetEncoding() const {
1783 return encoding_;
1784 }
1785
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001786 uint32_t GetOffset() const {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001787 return offset_;
1788 }
1789
1790 StackMap GetStackMap() const {
1791 return GetStackMapAt(stack_map_index_);
1792 }
1793
1794 void Next() {
1795 ++stack_map_index_;
1796 offset_ = (stack_map_index_ == number_of_stack_maps_)
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001797 ? static_cast<uint32_t>(-1)
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001798 : GetStackMapAt(stack_map_index_).GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001799 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001800 }
1801
1802 private:
1803 StackMap GetStackMapAt(size_t i) const {
1804 if (!indexes_.empty()) {
1805 i = indexes_[i];
1806 }
1807 DCHECK_LT(i, number_of_stack_maps_);
1808 return code_info_.GetStackMapAt(i, encoding_);
1809 }
1810
1811 const CodeInfo code_info_;
1812 const CodeInfoEncoding encoding_;
1813 const size_t number_of_stack_maps_;
1814 dchecked_vector<size_t> indexes_; // Used if stack map native PCs are not ordered.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001815 uint32_t offset_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001816 size_t stack_map_index_;
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001817 const InstructionSet instruction_set_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001818 };
1819
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001820 void DumpCode(VariableIndentationOutputStream* vios,
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001821 const OatFile::OatMethod& oat_method,
1822 const CodeItemDataAccessor& code_item_accessor,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001823 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001824 const void* quick_code = oat_method.GetQuickCode();
1825
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001826 if (code_size == 0) {
1827 code_size = oat_method.GetQuickCodeSize();
1828 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001829 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001830 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001831 return;
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001832 } else if (!bad_input && IsMethodGeneratedByOptimizingCompiler(oat_method,
1833 code_item_accessor)) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001834 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001835 StackMapsHelper helper(oat_method.GetVmapTable(), instruction_set_);
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001836 MethodInfo method_info(oat_method.GetOatQuickMethodHeader()->GetOptimizedMethodInfo());
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001837 {
1838 CodeInfoEncoding encoding(helper.GetEncoding());
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001839 StackMapEncoding stack_map_encoding(encoding.stack_map.encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001840 const size_t num_stack_maps = encoding.stack_map.num_entries;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001841 if (stats_.AddBitsIfUnique(Stats::kByteKindCodeInfoEncoding,
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001842 encoding.HeaderSize() * kBitsPerByte,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001843 oat_method.GetVmapTable())) {
Mathieu Chartier1e083792017-02-08 13:30:04 -08001844 // Stack maps
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001845 stats_.AddBits(
1846 Stats::kByteKindStackMapNativePc,
1847 stack_map_encoding.GetNativePcEncoding().BitSize() * num_stack_maps);
1848 stats_.AddBits(
1849 Stats::kByteKindStackMapDexPc,
1850 stack_map_encoding.GetDexPcEncoding().BitSize() * num_stack_maps);
1851 stats_.AddBits(
1852 Stats::kByteKindStackMapDexRegisterMap,
1853 stack_map_encoding.GetDexRegisterMapEncoding().BitSize() * num_stack_maps);
1854 stats_.AddBits(
Mathieu Chartier1e083792017-02-08 13:30:04 -08001855 Stats::kByteKindStackMapInlineInfoIndex,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001856 stack_map_encoding.GetInlineInfoEncoding().BitSize() * num_stack_maps);
1857 stats_.AddBits(
Mathieu Chartier1a20b682017-01-31 14:25:16 -08001858 Stats::kByteKindStackMapRegisterMaskIndex,
1859 stack_map_encoding.GetRegisterMaskIndexEncoding().BitSize() * num_stack_maps);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001860 stats_.AddBits(
David Srbecky45aa5982016-03-18 02:15:09 +00001861 Stats::kByteKindStackMapStackMaskIndex,
1862 stack_map_encoding.GetStackMaskIndexEncoding().BitSize() * num_stack_maps);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001863
1864 // Stack masks
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001865 stats_.AddBits(
David Srbecky45aa5982016-03-18 02:15:09 +00001866 Stats::kByteKindCodeInfoStackMasks,
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001867 encoding.stack_mask.encoding.BitSize() * encoding.stack_mask.num_entries);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001868
1869 // Register masks
Mathieu Chartier1a20b682017-01-31 14:25:16 -08001870 stats_.AddBits(
1871 Stats::kByteKindCodeInfoRegisterMasks,
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001872 encoding.register_mask.encoding.BitSize() * encoding.register_mask.num_entries);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001873
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001874 // Invoke infos
1875 if (encoding.invoke_info.num_entries > 0u) {
1876 stats_.AddBits(
1877 Stats::kByteKindCodeInfoInvokeInfo,
1878 encoding.invoke_info.encoding.BitSize() * encoding.invoke_info.num_entries);
1879 }
1880
Mathieu Chartier1e083792017-02-08 13:30:04 -08001881 // Location catalog
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001882 const size_t location_catalog_bytes =
1883 helper.GetCodeInfo().GetDexRegisterLocationCatalogSize(encoding);
1884 stats_.AddBits(Stats::kByteKindCodeInfoLocationCatalog,
1885 kBitsPerByte * location_catalog_bytes);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001886 // Dex register bytes.
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001887 const size_t dex_register_bytes =
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001888 helper.GetCodeInfo().GetDexRegisterMapsSize(encoding,
1889 code_item_accessor.RegistersSize());
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001890 stats_.AddBits(
1891 Stats::kByteKindCodeInfoDexRegisterMap,
1892 kBitsPerByte * dex_register_bytes);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001893
1894 // Inline infos.
1895 const size_t num_inline_infos = encoding.inline_info.num_entries;
1896 if (num_inline_infos > 0u) {
1897 stats_.AddBits(
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001898 Stats::kByteKindInlineInfoMethodIndexIdx,
1899 encoding.inline_info.encoding.GetMethodIndexIdxEncoding().BitSize() *
1900 num_inline_infos);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001901 stats_.AddBits(
1902 Stats::kByteKindInlineInfoDexPc,
1903 encoding.inline_info.encoding.GetDexPcEncoding().BitSize() * num_inline_infos);
1904 stats_.AddBits(
1905 Stats::kByteKindInlineInfoExtraData,
1906 encoding.inline_info.encoding.GetExtraDataEncoding().BitSize() * num_inline_infos);
1907 stats_.AddBits(
1908 Stats::kByteKindInlineInfoDexRegisterMap,
1909 encoding.inline_info.encoding.GetDexRegisterMapEncoding().BitSize() *
1910 num_inline_infos);
1911 stats_.AddBits(Stats::kByteKindInlineInfoIsLast, num_inline_infos);
1912 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001913 }
1914 }
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001915 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1916 size_t offset = 0;
1917 while (offset < code_size) {
1918 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
1919 if (offset == helper.GetOffset()) {
1920 ScopedIndentation indent1(vios);
1921 StackMap stack_map = helper.GetStackMap();
1922 DCHECK(stack_map.IsValid());
1923 stack_map.Dump(vios,
1924 helper.GetCodeInfo(),
1925 helper.GetEncoding(),
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001926 method_info,
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001927 oat_method.GetCodeOffset(),
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08001928 code_item_accessor.RegistersSize(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001929 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001930 do {
1931 helper.Next();
1932 // There may be multiple stack maps at a given PC. We display only the first one.
1933 } while (offset == helper.GetOffset());
1934 }
1935 DCHECK_LT(offset, helper.GetOffset());
1936 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001937 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001938 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1939 size_t offset = 0;
1940 while (offset < code_size) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001941 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001942 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001943 }
1944 }
1945
Vladimir Marko812fb4d2018-03-14 13:07:21 +00001946 void DumpDataBimgRelRoEntries(std::ostream& os) {
1947 os << ".data.bimg.rel.ro: ";
1948 if (oat_file_.GetBootImageRelocations().empty()) {
1949 os << "empty.\n\n";
1950 return;
1951 }
1952
1953 os << oat_file_.GetBootImageRelocations().size() << " entries.\n";
1954 Runtime* runtime = Runtime::Current();
1955 if (runtime != nullptr && !runtime->GetHeap()->GetBootImageSpaces().empty()) {
1956 const std::vector<gc::space::ImageSpace*>& boot_image_spaces =
1957 runtime->GetHeap()->GetBootImageSpaces();
1958 ScopedObjectAccess soa(Thread::Current());
1959 for (const uint32_t& object_offset : oat_file_.GetBootImageRelocations()) {
1960 uint32_t entry_index = &object_offset - oat_file_.GetBootImageRelocations().data();
1961 uint32_t entry_offset = entry_index * sizeof(oat_file_.GetBootImageRelocations()[0]);
1962 os << StringPrintf(" 0x%x: 0x%08x", entry_offset, object_offset);
1963 uint8_t* object = boot_image_spaces[0]->Begin() + object_offset;
1964 bool found = false;
1965 for (gc::space::ImageSpace* space : boot_image_spaces) {
1966 uint64_t local_offset = object - space->Begin();
1967 if (local_offset < space->GetImageHeader().GetImageSize()) {
1968 if (space->GetImageHeader().GetObjectsSection().Contains(local_offset)) {
1969 ObjPtr<mirror::Object> o = reinterpret_cast<mirror::Object*>(object);
1970 if (o->IsString()) {
1971 os << " String: " << o->AsString()->ToModifiedUtf8();
1972 } else if (o->IsClass()) {
1973 os << " Class: " << o->AsClass()->PrettyDescriptor();
1974 } else {
1975 os << StringPrintf(" 0x%08x %s",
1976 object_offset,
1977 o->GetClass()->PrettyDescriptor().c_str());
1978 }
1979 } else if (space->GetImageHeader().GetMethodsSection().Contains(local_offset)) {
1980 ArtMethod* m = reinterpret_cast<ArtMethod*>(object);
1981 os << " ArtMethod: " << m->PrettyMethod();
1982 } else {
1983 os << StringPrintf(" 0x%08x <unexpected section in %s>",
1984 object_offset,
1985 space->GetImageFilename().c_str());
1986 }
1987 found = true;
1988 break;
1989 }
1990 }
1991 if (!found) {
1992 os << StringPrintf(" 0x%08x <outside boot image spaces>", object_offset);
1993 }
1994 os << "\n";
1995 }
1996 } else {
1997 for (const uint32_t& object_offset : oat_file_.GetBootImageRelocations()) {
1998 uint32_t entry_index = &object_offset - oat_file_.GetBootImageRelocations().data();
1999 uint32_t entry_offset = entry_index * sizeof(oat_file_.GetBootImageRelocations()[0]);
2000 os << StringPrintf(" 0x%x: 0x%08x\n", entry_offset, object_offset);
2001 }
2002 }
2003 os << "\n";
2004 }
2005
Vladimir Markof3c52b42017-11-17 17:32:12 +00002006 template <typename NameGetter>
2007 void DumpBssEntries(std::ostream& os,
2008 const char* slot_type,
2009 const IndexBssMapping* mapping,
2010 uint32_t number_of_indexes,
2011 size_t slot_size,
2012 NameGetter name) {
2013 os << ".bss mapping for " << slot_type << ": ";
2014 if (mapping == nullptr) {
2015 os << "empty.\n";
2016 return;
2017 }
2018 size_t index_bits = IndexBssMappingEntry::IndexBits(number_of_indexes);
2019 size_t num_valid_indexes = 0u;
2020 for (const IndexBssMappingEntry& entry : *mapping) {
2021 num_valid_indexes += 1u + POPCOUNT(entry.GetMask(index_bits));
2022 }
2023 os << mapping->size() << " entries for " << num_valid_indexes << " valid indexes.\n";
2024 os << std::hex;
2025 for (const IndexBssMappingEntry& entry : *mapping) {
2026 uint32_t index = entry.GetIndex(index_bits);
2027 uint32_t mask = entry.GetMask(index_bits);
2028 size_t bss_offset = entry.bss_offset - POPCOUNT(mask) * slot_size;
2029 for (uint32_t n : LowToHighBits(mask)) {
2030 size_t current_index = index - (32u - index_bits) + n;
2031 os << " 0x" << bss_offset << ": " << slot_type << ": " << name(current_index) << "\n";
2032 bss_offset += slot_size;
2033 }
2034 DCHECK_EQ(bss_offset, entry.bss_offset);
2035 os << " 0x" << bss_offset << ": " << slot_type << ": " << name(index) << "\n";
2036 }
2037 os << std::dec;
2038 }
2039
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002040 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002041 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002042 const OatDumperOptions& options_;
2043 uint32_t resolved_addr2instr_;
Andreas Gampe372f3a32016-08-19 10:49:06 -07002044 const InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08002045 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002046 Disassembler* disassembler_;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08002047 Stats stats_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07002048};
2049
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002050class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002051 public:
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002052 ImageDumper(std::ostream* os,
2053 gc::space::ImageSpace& image_space,
2054 const ImageHeader& image_header,
2055 OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002056 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002057 vios_(os),
2058 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002059 image_space_(image_space),
2060 image_header_(image_header),
2061 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002062
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002063 bool Dump() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002064 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002065 std::ostream& indent_os = vios_.Stream();
2066
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002067 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07002068
Jeff Haodcdc85b2015-12-04 14:06:18 -08002069 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
2070
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002071 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07002072
Mathieu Chartiere401d142015-04-22 13:56:20 -07002073 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
2074
2075 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
2076 auto section = static_cast<ImageHeader::ImageSections>(i);
2077 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
2078 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07002079
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002080 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002081
Brian Carlstrom700c8d32012-11-05 10:42:02 -08002082 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07002083
Brian Carlstrom700c8d32012-11-05 10:42:02 -08002084 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
2085
2086 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
2087
2088 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002089
Alex Lighta59dd802014-07-02 16:28:08 -07002090 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
2091
Igor Murashkin46774762014-10-22 11:37:02 -07002092 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
2093
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002094 {
2095 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07002096 static_assert(arraysize(image_roots_descriptions_) ==
2097 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Vladimir Markoeca3eda2016-11-09 16:26:44 +00002098 DCHECK_LE(image_header_.GetImageRoots()->GetLength(), ImageHeader::kImageRootsMax);
2099 for (int32_t i = 0, size = image_header_.GetImageRoots()->GetLength(); i != size; ++i) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002100 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
2101 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002102 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002103 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Vladimir Markoeca3eda2016-11-09 16:26:44 +00002104 if (image_root_object != nullptr && image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002105 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08002106 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002107 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002108 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
2109 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08002110 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002111 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
2112 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08002113 run++;
2114 } else {
2115 break;
2116 }
2117 }
2118 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002119 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08002120 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002121 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002122 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08002123 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002124 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002125 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002126 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002127 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002128 }
Ian Rogersd5b32602012-02-26 16:40:04 -08002129 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07002130 }
2131 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002132 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002133
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002134 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002135 os << "METHOD ROOTS\n";
2136 static_assert(arraysize(image_methods_descriptions_) ==
2137 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
2138 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
2139 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
2140 const char* description = image_methods_descriptions_[i];
2141 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002142 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002143 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002144 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002145 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002146
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07002147 Runtime* const runtime = Runtime::Current();
2148 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002149 std::string image_filename = image_space_.GetImageFilename();
2150 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002151 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002152 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07002153 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002154 const OatFile* oat_file = image_space_.GetOatFile();
Alex Lighta59dd802014-07-02 16:28:08 -07002155 if (oat_file == nullptr) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002156 oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location);
2157 }
2158 if (oat_file == nullptr) {
Nicolas Geoffray30025092018-04-19 14:43:29 +01002159 oat_file = OatFile::Open(/* zip_fd */ -1,
2160 oat_location,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002161 oat_location,
2162 nullptr,
2163 nullptr,
2164 false,
2165 /*low_4gb*/false,
2166 nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07002167 &error_msg);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002168 }
2169 if (oat_file == nullptr) {
2170 os << "OAT FILE NOT FOUND: " << error_msg << "\n";
2171 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07002172 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002173 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07002174
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002175 stats_.oat_file_bytes = oat_file->Size();
2176
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002177 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002178
Mathieu Chartier02e25112013-08-14 16:14:24 -07002179 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002180 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07002181 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
2182 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07002183 }
2184
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002185 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07002186
Jeff Haodcdc85b2015-12-04 14:06:18 -08002187 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07002188 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07002189 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07002190 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08002191 {
2192 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
2193 heap->FlushAllocStack();
2194 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08002195 // Since FlushAllocStack() above resets the (active) allocation
2196 // stack. Need to revoke the thread-local allocation stacks that
2197 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07002198 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07002199 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07002200 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07002201 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002202 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002203 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01002204 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002205 {
Andreas Gampecc1b5352016-12-01 16:58:38 -08002206 ReaderMutexLock mu(self, *Locks::dex_lock_);
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08002207 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07002208 ObjPtr<mirror::DexCache> dex_cache =
2209 ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07002210 if (dex_cache != nullptr) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07002211 dex_caches_.insert(dex_cache.Ptr());
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07002212 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002213 }
2214 }
Andreas Gampe0c183382017-07-13 22:26:24 -07002215 auto dump_visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
2216 DumpObject(obj);
2217 };
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002218 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08002219 // Dump the normal objects before ArtMethods.
Andreas Gampe0c183382017-07-13 22:26:24 -07002220 image_space_.GetLiveBitmap()->Walk(dump_visitor);
Jeff Haodcdc85b2015-12-04 14:06:18 -08002221 indent_os << "\n";
2222 // TODO: Dump fields.
2223 // Dump methods after.
Jeff Haodcdc85b2015-12-04 14:06:18 -08002224 DumpArtMethodVisitor visitor(this);
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002225 image_header_.VisitPackedArtMethods(&visitor,
2226 image_space_.Begin(),
2227 image_header_.GetPointerSize());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002228 // Dump the large objects separately.
Andreas Gampe0c183382017-07-13 22:26:24 -07002229 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(dump_visitor);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002230 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07002231 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002232 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07002233 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002234 size_t data_size = image_header_.GetDataSize(); // stored size in file.
2235 if (file == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002236 LOG(WARNING) << "Failed to find image in " << image_filename;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002237 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002238 stats_.file_bytes = file->GetLength();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002239 // If the image is compressed, adjust to decompressed size.
2240 size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader);
2241 if (image_header_.GetStorageMode() == ImageHeader::kStorageModeUncompressed) {
2242 DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image";
2243 }
2244 stats_.file_bytes += uncompressed_size - data_size;
Brian Carlstrom6f277752013-09-30 17:56:45 -07002245 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002246 size_t header_bytes = sizeof(ImageHeader);
Vladimir Markocd87c3e2017-09-05 13:11:57 +01002247 const auto& object_section = image_header_.GetObjectsSection();
2248 const auto& field_section = image_header_.GetFieldsSection();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002249 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Markocd87c3e2017-09-05 13:11:57 +01002250 const auto& dex_cache_arrays_section = image_header_.GetDexCacheArraysSection();
2251 const auto& intern_section = image_header_.GetInternedStringsSection();
2252 const auto& class_table_section = image_header_.GetClassTableSection();
2253 const auto& bitmap_section = image_header_.GetImageBitmapSection();
Andreas Gampeace0dc12016-01-20 13:33:13 -08002254
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002255 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08002256
2257 // Objects are kObjectAlignment-aligned.
2258 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
2259 if (object_section.Offset() > header_bytes) {
2260 stats_.alignment_bytes += object_section.Offset() - header_bytes;
2261 }
2262
2263 // Field section is 4-byte aligned.
2264 constexpr size_t kFieldSectionAlignment = 4U;
2265 uint32_t end_objects = object_section.Offset() + object_section.Size();
2266 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
2267 stats_.alignment_bytes += field_section.Offset() - end_objects;
2268
2269 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
2270 uint32_t end_fields = field_section.Offset() + field_section.Size();
2271 CHECK_ALIGNED(method_section.Offset(), 4);
2272 stats_.alignment_bytes += method_section.Offset() - end_fields;
2273
2274 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
2275 uint32_t end_methods = method_section.Offset() + method_section.Size();
2276 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
2277 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
2278
2279 // Intern table is 8-byte aligned.
2280 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
Vladimir Markoe47f60c2018-02-21 13:43:28 +00002281 CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
Andreas Gampeace0dc12016-01-20 13:33:13 -08002282 stats_.alignment_bytes += intern_section.Offset() - end_caches;
2283
2284 // Add space between intern table and class table.
2285 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
2286 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
2287
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002288 // Add space between end of image data and bitmap. Expect the bitmap to be page-aligned.
2289 const size_t bitmap_offset = sizeof(ImageHeader) + data_size;
Andreas Gampeace0dc12016-01-20 13:33:13 -08002290 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002291 stats_.alignment_bytes += RoundUp(bitmap_offset, kPageSize) - bitmap_offset;
Andreas Gampeace0dc12016-01-20 13:33:13 -08002292
Mathieu Chartiere401d142015-04-22 13:56:20 -07002293 stats_.bitmap_bytes += bitmap_section.Size();
2294 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01002295 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01002296 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002297 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002298 stats_.class_table_bytes += class_table_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002299 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002300 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002301
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002302 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002303
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002304 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002305 }
2306
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002307 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002308 class DumpArtMethodVisitor : public ArtMethodVisitor {
2309 public:
2310 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
2311
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002312 virtual void Visit(ArtMethod* method) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002313 std::ostream& indent_os = image_dumper_->vios_.Stream();
David Sehr709b0702016-10-13 09:12:37 -07002314 indent_os << method << " " << " ArtMethod: " << ArtMethod::PrettyMethod(method) << "\n";
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002315 image_dumper_->DumpMethod(method, indent_os);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002316 indent_os << "\n";
2317 }
2318
2319 private:
2320 ImageDumper* const image_dumper_;
2321 };
2322
Mathieu Chartier3398c782016-09-30 10:27:43 -07002323 static void PrettyObjectValue(std::ostream& os,
2324 ObjPtr<mirror::Class> type,
2325 ObjPtr<mirror::Object> value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002326 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002327 CHECK(type != nullptr);
2328 if (value == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07002329 os << StringPrintf("null %s\n", type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002330 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002331 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002332 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07002333 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07002334 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002335 mirror::Class* klass = value->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07002336 os << StringPrintf("%p Class: %s\n", klass, mirror::Class::PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002337 } else {
David Sehr709b0702016-10-13 09:12:37 -07002338 os << StringPrintf("%p %s\n", value.Ptr(), type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002339 }
2340 }
2341
Mathieu Chartier3398c782016-09-30 10:27:43 -07002342 static void PrintField(std::ostream& os, ArtField* field, ObjPtr<mirror::Object> obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002343 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07002344 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08002345 switch (field->GetTypeAsPrimitiveType()) {
2346 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08002347 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002348 break;
2349 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002350 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002351 break;
2352 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002353 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002354 break;
2355 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002356 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002357 break;
2358 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07002359 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002360 break;
2361 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07002362 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002363 break;
2364 case Primitive::kPrimBoolean:
Roland Levillain5e8d5f02016-10-18 18:03:43 +01002365 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj) ? "true" : "false",
Fred Shih37f05ef2014-07-16 18:38:08 -07002366 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002367 break;
2368 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07002369 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002370 break;
2371 case Primitive::kPrimNot: {
2372 // Get the value, don't compute the type unless it is non-null as we don't want
2373 // to cause class loading.
Mathieu Chartier3398c782016-09-30 10:27:43 -07002374 ObjPtr<mirror::Object> value = field->GetObj(obj);
Ian Rogers08f1f502014-12-02 15:04:37 -08002375 if (value == nullptr) {
2376 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07002377 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08002378 // Grab the field type without causing resolution.
Vladimir Marko208f6702017-12-08 12:00:50 +00002379 ObjPtr<mirror::Class> field_type = field->LookupResolvedType();
Ian Rogers08f1f502014-12-02 15:04:37 -08002380 if (field_type != nullptr) {
2381 PrettyObjectValue(os, field_type, value);
2382 } else {
Mathieu Chartier3398c782016-09-30 10:27:43 -07002383 os << StringPrintf("%p %s\n",
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07002384 value.Ptr(),
Ian Rogers08f1f502014-12-02 15:04:37 -08002385 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
2386 }
Ian Rogers50239c72013-06-17 14:53:22 -07002387 }
Ian Rogers08f1f502014-12-02 15:04:37 -08002388 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07002389 }
Ian Rogers08f1f502014-12-02 15:04:37 -08002390 default:
2391 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
2392 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08002393 }
2394 }
2395
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002396 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002397 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002398 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002399 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002400 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08002401 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002402 for (ArtField& field : klass->GetIFields()) {
2403 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08002404 }
2405 }
2406
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002407 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002408 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002409 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002410
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002411 const void* GetQuickOatCodeBegin(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08002412 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002413 image_header_.GetPointerSize());
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002414 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002415 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002416 }
Vladimir Marko33bff252017-11-01 14:35:42 +00002417 if (oat_dumper_->GetInstructionSet() == InstructionSet::kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002418 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002419 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002420 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002421 }
2422
Mathieu Chartiere401d142015-04-22 13:56:20 -07002423 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002424 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002425 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
2426 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002427 return 0;
2428 }
2429 return oat_code_begin[-1];
2430 }
2431
Mathieu Chartiere401d142015-04-22 13:56:20 -07002432 const void* GetQuickOatCodeEnd(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002433 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002434 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002435 if (oat_code_begin == nullptr) {
2436 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002437 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002438 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002439 }
2440
Andreas Gampe0c183382017-07-13 22:26:24 -07002441 void DumpObject(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002442 DCHECK(obj != nullptr);
Andreas Gampe0c183382017-07-13 22:26:24 -07002443 if (!InDumpSpace(obj)) {
Brian Carlstrom78128a62011-09-15 17:21:19 -07002444 return;
2445 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002446
2447 size_t object_bytes = obj->SizeOf();
2448 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
Andreas Gampe0c183382017-07-13 22:26:24 -07002449 stats_.object_bytes += object_bytes;
2450 stats_.alignment_bytes += alignment_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002451
Andreas Gampe0c183382017-07-13 22:26:24 -07002452 std::ostream& os = vios_.Stream();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002453
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002454 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08002455 if (obj_class->IsArrayClass()) {
David Sehr709b0702016-10-13 09:12:37 -07002456 os << StringPrintf("%p: %s length:%d\n", obj, obj_class->PrettyDescriptor().c_str(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002457 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08002458 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002459 mirror::Class* klass = obj->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07002460 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj,
2461 mirror::Class::PrettyDescriptor(klass).c_str())
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002462 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08002463 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002464 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07002465 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002466 } else {
David Sehr709b0702016-10-13 09:12:37 -07002467 os << StringPrintf("%p: %s\n", obj, obj_class->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002468 }
Andreas Gampe0c183382017-07-13 22:26:24 -07002469 ScopedIndentation indent1(&vios_);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002470 DumpFields(os, obj, obj_class);
Andreas Gampe0c183382017-07-13 22:26:24 -07002471 const PointerSize image_pointer_size = image_header_.GetPointerSize();
Ian Rogersd5b32602012-02-26 16:40:04 -08002472 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002473 auto* obj_array = obj->AsObjectArray<mirror::Object>();
2474 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002475 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002476 size_t run = 0;
2477 for (int32_t j = i + 1; j < length; j++) {
2478 if (value == obj_array->Get(j)) {
2479 run++;
2480 } else {
2481 break;
2482 }
2483 }
2484 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002485 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002486 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002487 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08002488 i = i + run;
2489 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002490 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002491 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002492 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08002493 }
2494 } else if (obj->IsClass()) {
Vladimir Marko38b8b252018-01-02 19:07:06 +00002495 ObjPtr<mirror::Class> klass = obj->AsClass();
Igor Murashkin86083f72017-10-27 10:59:04 -07002496
Vladimir Marko305c38b2018-02-14 11:50:07 +00002497 if (kBitstringSubtypeCheckEnabled) {
2498 os << "SUBTYPE_CHECK_BITS: ";
2499 SubtypeCheck<ObjPtr<mirror::Class>>::Dump(klass, os);
2500 os << "\n";
2501 }
Igor Murashkin86083f72017-10-27 10:59:04 -07002502
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002503 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002504 os << "STATICS:\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002505 ScopedIndentation indent2(&vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002506 for (ArtField& field : klass->GetSFields()) {
2507 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08002508 }
2509 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002510 } else {
Andreas Gampe0c183382017-07-13 22:26:24 -07002511 auto it = dex_caches_.find(obj);
2512 if (it != dex_caches_.end()) {
Vladimir Marko05792b92015-08-03 11:56:49 +01002513 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Vladimir Markocd87c3e2017-09-05 13:11:57 +01002514 const auto& field_section = image_header_.GetFieldsSection();
Andreas Gampe0c183382017-07-13 22:26:24 -07002515 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01002516 size_t num_methods = dex_cache->NumResolvedMethods();
2517 if (num_methods != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002518 os << "Methods (size=" << num_methods << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002519 ScopedIndentation indent2(&vios_);
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002520 mirror::MethodDexCacheType* resolved_methods = dex_cache->GetResolvedMethods();
Vladimir Marko05792b92015-08-03 11:56:49 +01002521 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002522 ArtMethod* elem = mirror::DexCache::GetNativePairPtrSize(
2523 resolved_methods, i, image_pointer_size).object;
Vladimir Marko05792b92015-08-03 11:56:49 +01002524 size_t run = 0;
2525 for (size_t j = i + 1;
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002526 j != length &&
2527 elem == mirror::DexCache::GetNativePairPtrSize(
2528 resolved_methods, j, image_pointer_size).object;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002529 ++j) {
2530 ++run;
2531 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002532 if (run == 0) {
2533 os << StringPrintf("%zd: ", i);
2534 } else {
2535 os << StringPrintf("%zd to %zd: ", i, i + run);
2536 i = i + run;
2537 }
2538 std::string msg;
2539 if (elem == nullptr) {
2540 msg = "null";
2541 } else if (method_section.Contains(
Andreas Gampe0c183382017-07-13 22:26:24 -07002542 reinterpret_cast<uint8_t*>(elem) - image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002543 msg = reinterpret_cast<ArtMethod*>(elem)->PrettyMethod();
Vladimir Marko05792b92015-08-03 11:56:49 +01002544 } else {
2545 msg = "<not in method section>";
2546 }
2547 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07002548 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002549 }
2550 size_t num_fields = dex_cache->NumResolvedFields();
2551 if (num_fields != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002552 os << "Fields (size=" << num_fields << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002553 ScopedIndentation indent2(&vios_);
Vladimir Marko05792b92015-08-03 11:56:49 +01002554 auto* resolved_fields = dex_cache->GetResolvedFields();
2555 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002556 ArtField* elem = mirror::DexCache::GetNativePairPtrSize(
Vladimir Markof44d36c2017-03-14 14:18:46 +00002557 resolved_fields, i, image_pointer_size).object;
Vladimir Marko05792b92015-08-03 11:56:49 +01002558 size_t run = 0;
2559 for (size_t j = i + 1;
Vladimir Markof44d36c2017-03-14 14:18:46 +00002560 j != length &&
2561 elem == mirror::DexCache::GetNativePairPtrSize(
2562 resolved_fields, j, image_pointer_size).object;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002563 ++j) {
2564 ++run;
2565 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002566 if (run == 0) {
2567 os << StringPrintf("%zd: ", i);
2568 } else {
2569 os << StringPrintf("%zd to %zd: ", i, i + run);
2570 i = i + run;
2571 }
2572 std::string msg;
2573 if (elem == nullptr) {
2574 msg = "null";
2575 } else if (field_section.Contains(
Andreas Gampe0c183382017-07-13 22:26:24 -07002576 reinterpret_cast<uint8_t*>(elem) - image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002577 msg = reinterpret_cast<ArtField*>(elem)->PrettyField();
Vladimir Marko05792b92015-08-03 11:56:49 +01002578 } else {
2579 msg = "<not in field section>";
2580 }
2581 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002582 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002583 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002584 size_t num_types = dex_cache->NumResolvedTypes();
2585 if (num_types != 0u) {
2586 os << "Types (size=" << num_types << "):\n";
Andreas Gampe0c183382017-07-13 22:26:24 -07002587 ScopedIndentation indent2(&vios_);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002588 auto* resolved_types = dex_cache->GetResolvedTypes();
2589 for (size_t i = 0; i < num_types; ++i) {
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002590 auto pair = resolved_types[i].load(std::memory_order_relaxed);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002591 size_t run = 0;
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002592 for (size_t j = i + 1; j != num_types; ++j) {
2593 auto other_pair = resolved_types[j].load(std::memory_order_relaxed);
2594 if (pair.index != other_pair.index ||
2595 pair.object.Read() != other_pair.object.Read()) {
2596 break;
2597 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002598 ++run;
2599 }
2600 if (run == 0) {
2601 os << StringPrintf("%zd: ", i);
2602 } else {
2603 os << StringPrintf("%zd to %zd: ", i, i + run);
2604 i = i + run;
2605 }
2606 std::string msg;
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002607 auto* elem = pair.object.Read();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002608 if (elem == nullptr) {
2609 msg = "null";
2610 } else {
David Sehr709b0702016-10-13 09:12:37 -07002611 msg = elem->PrettyClass();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002612 }
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002613 os << StringPrintf("%p %u %s\n", elem, pair.index, msg.c_str());
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002614 }
2615 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002616 }
2617 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002618 std::string temp;
Andreas Gampe0c183382017-07-13 22:26:24 -07002619 stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002620 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002621
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002622 void DumpMethod(ArtMethod* method, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002623 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002624 DCHECK(method != nullptr);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002625 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
2626 const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
Andreas Gampe542451c2016-07-26 09:02:02 -07002627 const PointerSize pointer_size = image_header_.GetPointerSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002628 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2629 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002630 if (method->IsNative()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002631 bool first_occurrence;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002632 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2633 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002634 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002635 stats_.native_to_managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002636 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002637 if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
2638 image_header_.GetPointerSize())) {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002639 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002640 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002641 } else if (method->IsAbstract() || method->IsClassInitializer()) {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002642 // Don't print information for these.
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002643 } else if (method->IsRuntimeMethod()) {
2644 ImtConflictTable* table = method->GetImtConflictTable(image_header_.GetPointerSize());
2645 if (table != nullptr) {
2646 indent_os << "IMT conflict table " << table << " method: ";
2647 for (size_t i = 0, count = table->NumEntries(pointer_size); i < count; ++i) {
David Sehr709b0702016-10-13 09:12:37 -07002648 indent_os << ArtMethod::PrettyMethod(table->GetImplementationMethod(i, pointer_size))
2649 << " ";
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002650 }
2651 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002652 } else {
David Sehr0225f8e2018-01-31 08:52:24 +00002653 CodeItemDataAccessor code_item_accessor(method->DexInstructionData());
Mathieu Chartierd5c0a0a2017-11-10 14:16:34 -08002654 size_t dex_instruction_bytes = code_item_accessor.InsnsSizeInCodeUnits() * 2;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002655 stats_.dex_instruction_bytes += dex_instruction_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002656
2657 bool first_occurrence;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002658 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002659 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01002660 // Method compiled with the optimizing compiler have no vmap table.
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002661 vmap_table_bytes = ComputeOatSize(method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002662 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002663 stats_.vmap_table_bytes += vmap_table_bytes;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002664 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002665 }
2666
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002667 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2668 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002669 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002670 stats_.managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002671 if (method->IsConstructor()) {
2672 if (method->IsStatic()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002673 stats_.class_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002674 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002675 stats_.large_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002676 }
2677 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002678 stats_.large_method_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002679 }
2680 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002681 stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002682
Igor Murashkin7617abd2015-07-10 18:27:47 -07002683 uint32_t method_access_flags = method->GetAccessFlags();
2684
Mathieu Chartiere401d142015-04-22 13:56:20 -07002685 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002686 indent_os << StringPrintf("SIZE: Dex Instructions=%zd StackMaps=%zd AccessFlags=0x%x\n",
2687 dex_instruction_bytes,
2688 vmap_table_bytes,
Igor Murashkin7617abd2015-07-10 18:27:47 -07002689 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002690
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002691 size_t total_size = dex_instruction_bytes +
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002692 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002693
2694 double expansion =
2695 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002696 stats_.ComputeOutliers(total_size, expansion, method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002697 }
2698 }
2699
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002700 std::set<const void*> already_seen_;
2701 // Compute the size of the given data within the oat file and whether this is the first time
2702 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002703 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002704 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002705 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002706 already_seen_.insert(oat_data);
2707 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002708 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002709 }
2710 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002711 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002712
2713 public:
2714 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002715 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002716 size_t file_bytes;
2717
2718 size_t header_bytes;
2719 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002720 size_t art_field_bytes;
2721 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01002722 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002723 size_t interned_strings_bytes;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002724 size_t class_table_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07002725 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002726 size_t alignment_bytes;
2727
2728 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002729 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002730 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07002731 size_t class_initializer_code_bytes;
2732 size_t large_initializer_code_bytes;
2733 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002734
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002735 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002736
2737 size_t dex_instruction_bytes;
2738
Mathieu Chartiere401d142015-04-22 13:56:20 -07002739 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002740 std::vector<size_t> method_outlier_size;
2741 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002742 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002743
Roland Levillain3887c462015-08-12 18:15:42 +01002744 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002745 : oat_file_bytes(0),
2746 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002747 header_bytes(0),
2748 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002749 art_field_bytes(0),
2750 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01002751 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002752 interned_strings_bytes(0),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002753 class_table_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07002754 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002755 alignment_bytes(0),
2756 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002757 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002758 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07002759 class_initializer_code_bytes(0),
2760 large_initializer_code_bytes(0),
2761 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002762 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002763 dex_instruction_bytes(0) {}
2764
Elliott Hughesa0e18062012-04-13 15:59:59 -07002765 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002766 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002767 size_t bytes;
2768 size_t count;
2769 };
2770 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2771 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002772
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002773 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002774 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2775 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002776 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002777 it->second.count += 1;
2778 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002779 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002780 }
2781 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002782
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002783 double PercentOfOatBytes(size_t size) {
2784 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2785 }
2786
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002787 double PercentOfFileBytes(size_t size) {
2788 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2789 }
2790
2791 double PercentOfObjectBytes(size_t size) {
2792 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2793 }
2794
Mathieu Chartiere401d142015-04-22 13:56:20 -07002795 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002796 method_outlier_size.push_back(total_size);
2797 method_outlier_expansion.push_back(expansion);
2798 method_outlier.push_back(method);
2799 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002800
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002801 void DumpOutliers(std::ostream& os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002802 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002803 size_t sum_of_sizes = 0;
2804 size_t sum_of_sizes_squared = 0;
2805 size_t sum_of_expansion = 0;
2806 size_t sum_of_expansion_squared = 0;
2807 size_t n = method_outlier_size.size();
Mathieu Chartier1ebf8d32016-06-09 11:51:27 -07002808 if (n <= 1) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08002809 return;
2810 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002811 for (size_t i = 0; i < n; i++) {
2812 size_t cur_size = method_outlier_size[i];
2813 sum_of_sizes += cur_size;
2814 sum_of_sizes_squared += cur_size * cur_size;
2815 double cur_expansion = method_outlier_expansion[i];
2816 sum_of_expansion += cur_expansion;
2817 sum_of_expansion_squared += cur_expansion * cur_expansion;
2818 }
2819 size_t size_mean = sum_of_sizes / n;
2820 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2821 double expansion_mean = sum_of_expansion / n;
2822 double expansion_variance =
2823 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2824
2825 // Dump methods whose size is a certain number of standard deviations from the mean
2826 size_t dumped_values = 0;
2827 size_t skipped_values = 0;
2828 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2829 size_t cur_size_variance = i * i * size_variance;
2830 bool first = true;
2831 for (size_t j = 0; j < n; j++) {
2832 size_t cur_size = method_outlier_size[j];
2833 if (cur_size > size_mean) {
2834 size_t cur_var = cur_size - size_mean;
2835 cur_var = cur_var * cur_var;
2836 if (cur_var > cur_size_variance) {
2837 if (dumped_values > 20) {
2838 if (i == 1) {
2839 skipped_values++;
2840 } else {
2841 i = 2; // jump to counting for 1 standard deviation
2842 break;
2843 }
2844 } else {
2845 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002846 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002847 first = false;
2848 }
David Sehr709b0702016-10-13 09:12:37 -07002849 os << ArtMethod::PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002850 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002851 method_outlier_size[j] = 0; // don't consider this method again
2852 dumped_values++;
2853 }
2854 }
2855 }
2856 }
2857 }
2858 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002859 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002860 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002861 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002862 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002863
2864 // Dump methods whose expansion is a certain number of standard deviations from the mean
2865 dumped_values = 0;
2866 skipped_values = 0;
2867 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2868 double cur_expansion_variance = i * i * expansion_variance;
2869 bool first = true;
2870 for (size_t j = 0; j < n; j++) {
2871 double cur_expansion = method_outlier_expansion[j];
2872 if (cur_expansion > expansion_mean) {
2873 size_t cur_var = cur_expansion - expansion_mean;
2874 cur_var = cur_var * cur_var;
2875 if (cur_var > cur_expansion_variance) {
2876 if (dumped_values > 20) {
2877 if (i == 1) {
2878 skipped_values++;
2879 } else {
2880 i = 2; // jump to counting for 1 standard deviation
2881 break;
2882 }
2883 } else {
2884 if (first) {
2885 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002886 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002887 first = false;
2888 }
David Sehr709b0702016-10-13 09:12:37 -07002889 os << ArtMethod::PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002890 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002891 method_outlier_expansion[j] = 0.0; // don't consider this method again
2892 dumped_values++;
2893 }
2894 }
2895 }
2896 }
2897 }
2898 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002899 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002900 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002901 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002902 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002903 }
2904
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002905 void Dump(std::ostream& os, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002906 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002907 {
2908 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2909 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002910 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2911 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2912 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2913 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2914 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2915 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002916 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002917 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2918 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002919 header_bytes, PercentOfFileBytes(header_bytes),
2920 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002921 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2922 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002923 dex_cache_arrays_bytes,
2924 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002925 interned_strings_bytes,
2926 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002927 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002928 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002929 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2930 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002931 CHECK_EQ(file_bytes,
2932 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2933 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2934 bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002935 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002936
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002937 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002938 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002939 for (const auto& sizes_and_count : sizes_and_counts) {
2940 const std::string& descriptor(sizes_and_count.first);
2941 double average = static_cast<double>(sizes_and_count.second.bytes) /
2942 static_cast<double>(sizes_and_count.second.count);
2943 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002944 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002945 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002946 descriptor.c_str(), sizes_and_count.second.bytes,
2947 sizes_and_count.second.count, average, percent);
2948 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002949 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002950 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002951 CHECK_EQ(object_bytes, object_bytes_total);
2952
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002953 os << StringPrintf("oat_file_bytes = %8zd\n"
2954 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002955 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2956 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2957 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2958 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002959 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002960 managed_code_bytes,
2961 PercentOfOatBytes(managed_code_bytes),
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002962 native_to_managed_code_bytes,
2963 PercentOfOatBytes(native_to_managed_code_bytes),
2964 class_initializer_code_bytes,
2965 PercentOfOatBytes(class_initializer_code_bytes),
2966 large_initializer_code_bytes,
2967 PercentOfOatBytes(large_initializer_code_bytes),
2968 large_method_code_bytes,
2969 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002970 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002971 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002972 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002973 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2974 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002975 }
2976
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002977 os << "\n" << StringPrintf("vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002978 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002979 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002980
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002981 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2982 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002983 static_cast<double>(managed_code_bytes) /
2984 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002985 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002986 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002987 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002988
2989 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002990 }
2991 } stats_;
2992
2993 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002994 enum {
2995 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2996 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2997 kLargeConstructorDexBytes = 4000,
2998 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2999 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
3000 kLargeMethodDexBytes = 16000
3001 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01003002
3003 // For performance, use the *os_ directly for anything that doesn't need indentation
3004 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003005 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01003006 VariableIndentationOutputStream vios_;
3007 ScopedIndentation indent1_;
3008
Ian Rogers1d54e732013-05-02 21:10:01 -07003009 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08003010 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07003011 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07003012 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003013 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07003014
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08003015 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07003016};
3017
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003018static int DumpImage(gc::space::ImageSpace* image_space,
3019 OatDumperOptions* options,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07003020 std::ostream* os) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003021 const ImageHeader& image_header = image_space->GetImageHeader();
3022 if (!image_header.IsValid()) {
Andreas Gampe221d9812018-01-22 17:48:56 -08003023 LOG(ERROR) << "Invalid image header " << image_space->GetImageLocation();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003024 return EXIT_FAILURE;
3025 }
3026 ImageDumper image_dumper(os, *image_space, image_header, options);
3027 if (!image_dumper.Dump()) {
3028 return EXIT_FAILURE;
3029 }
3030 return EXIT_SUCCESS;
3031}
3032
3033static int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07003034 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08003035 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003036 options->class_loader_ = &null_class_loader;
3037
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003038 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003039 if (options->app_image_ != nullptr) {
3040 if (options->app_oat_ == nullptr) {
3041 LOG(ERROR) << "Can not dump app image without app oat file";
Jeff Haodcdc85b2015-12-04 14:06:18 -08003042 return EXIT_FAILURE;
3043 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003044 // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
3045 // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
3046 // pointers into 32 bit pointer sized ArtMethods.
3047 std::string error_msg;
Nicolas Geoffray30025092018-04-19 14:43:29 +01003048 std::unique_ptr<OatFile> oat_file(OatFile::Open(/* zip_fd */ -1,
3049 options->app_oat_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003050 options->app_oat_,
3051 nullptr,
3052 nullptr,
3053 false,
3054 /*low_4gb*/true,
3055 nullptr,
3056 &error_msg));
3057 if (oat_file == nullptr) {
3058 LOG(ERROR) << "Failed to open oat file " << options->app_oat_ << " with error " << error_msg;
Jeff Haodcdc85b2015-12-04 14:06:18 -08003059 return EXIT_FAILURE;
3060 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003061 std::unique_ptr<gc::space::ImageSpace> space(
3062 gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg));
3063 if (space == nullptr) {
3064 LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error "
3065 << error_msg;
3066 }
3067 // Open dex files for the image.
3068 std::vector<std::unique_ptr<const DexFile>> dex_files;
3069 if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
3070 LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
3071 << error_msg;
3072 }
3073 // Dump the actual image.
3074 int result = DumpImage(space.get(), options, os);
3075 if (result != EXIT_SUCCESS) {
3076 return result;
3077 }
3078 // Fall through to dump the boot images.
3079 }
3080
3081 gc::Heap* heap = runtime->GetHeap();
3082 CHECK(heap->HasBootImageSpace()) << "No image spaces";
3083 for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
3084 int result = DumpImage(image_space, options, os);
3085 if (result != EXIT_SUCCESS) {
3086 return result;
3087 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07003088 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08003089 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07003090}
3091
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003092static jobject InstallOatFile(Runtime* runtime,
3093 std::unique_ptr<OatFile> oat_file,
3094 std::vector<const DexFile*>* class_path)
3095 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07003096 Thread* self = Thread::Current();
3097 CHECK(self != nullptr);
3098 // Need well-known-classes.
3099 WellKnownClasses::Init(self->GetJniEnv());
3100
Vladimir Marko421087b2018-02-27 11:00:17 +00003101 // Open dex files.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003102 OatFile* oat_file_ptr = oat_file.get();
Andreas Gampe00b25f32014-09-17 21:49:05 -07003103 ClassLinker* class_linker = runtime->GetClassLinker();
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003104 runtime->GetOatFileManager().RegisterOatFile(std::move(oat_file));
3105 for (const OatFile::OatDexFile* odf : oat_file_ptr->GetOatDexFiles()) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07003106 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07003107 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003108 CHECK(dex_file != nullptr) << error_msg;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003109 class_path->push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003110 }
3111
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003112 // Need a class loader. Fake that we're a compiler.
3113 // Note: this will run initializers through the unstarted runtime, so make sure it's
3114 // initialized.
3115 interpreter::UnstartedRuntime::Initialize();
3116
3117 jobject class_loader = class_linker->CreatePathClassLoader(self, *class_path);
3118
Vladimir Marko421087b2018-02-27 11:00:17 +00003119 // Need to register dex files to get a working dex cache.
3120 for (const DexFile* dex_file : *class_path) {
3121 ObjPtr<mirror::DexCache> dex_cache = class_linker->RegisterDexFile(
3122 *dex_file, self->DecodeJObject(class_loader)->AsClassLoader());
3123 CHECK(dex_cache != nullptr);
3124 }
3125
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003126 return class_loader;
3127}
3128
3129static int DumpOatWithRuntime(Runtime* runtime,
3130 std::unique_ptr<OatFile> oat_file,
3131 OatDumperOptions* options,
3132 std::ostream* os) {
3133 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
3134 ScopedObjectAccess soa(Thread::Current());
3135
3136 OatFile* oat_file_ptr = oat_file.get();
3137 std::vector<const DexFile*> class_path;
3138 jobject class_loader = InstallOatFile(runtime, std::move(oat_file), &class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003139
3140 // Use the class loader while dumping.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003141 StackHandleScope<1> scope(soa.Self());
Andreas Gampe00b25f32014-09-17 21:49:05 -07003142 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07003143 soa.Decode<mirror::ClassLoader>(class_loader));
Andreas Gampe00b25f32014-09-17 21:49:05 -07003144 options->class_loader_ = &loader_handle;
3145
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003146 OatDumper oat_dumper(*oat_file_ptr, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003147 bool success = oat_dumper.Dump(*os);
3148 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
3149}
3150
3151static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003152 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003153 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08003154 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003155 options->class_loader_ = &null_class_loader;
3156
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003157 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003158 bool success = oat_dumper.Dump(*os);
3159 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
3160}
3161
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003162static int DumpOat(Runtime* runtime,
3163 const char* oat_filename,
3164 const char* dex_filename,
3165 OatDumperOptions* options,
Andreas Gampe00b25f32014-09-17 21:49:05 -07003166 std::ostream* os) {
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003167 if (dex_filename == nullptr) {
3168 LOG(WARNING) << "No dex filename provided, "
3169 << "oatdump might fail if the oat file does not contain the dex code.";
3170 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07003171 std::string error_msg;
Nicolas Geoffray30025092018-04-19 14:43:29 +01003172 std::unique_ptr<OatFile> oat_file(OatFile::Open(/* zip_fd */ -1,
3173 oat_filename,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003174 oat_filename,
3175 nullptr,
3176 nullptr,
3177 false,
3178 /*low_4gb*/false,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003179 dex_filename,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003180 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07003181 if (oat_file == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08003182 LOG(ERROR) << "Failed to open oat file from '" << oat_filename << "': " << error_msg;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003183 return EXIT_FAILURE;
3184 }
3185
3186 if (runtime != nullptr) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003187 return DumpOatWithRuntime(runtime, std::move(oat_file), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003188 } else {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003189 return DumpOatWithoutRuntime(oat_file.get(), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003190 }
3191}
3192
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003193static int SymbolizeOat(const char* oat_filename,
3194 const char* dex_filename,
3195 std::string& output_name,
3196 bool no_bits) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07003197 std::string error_msg;
Nicolas Geoffray30025092018-04-19 14:43:29 +01003198 std::unique_ptr<OatFile> oat_file(OatFile::Open(/* zip_fd */ -1,
3199 oat_filename,
Andreas Gampe08c277c2017-04-26 22:22:15 -07003200 oat_filename,
3201 nullptr,
3202 nullptr,
3203 false,
3204 /*low_4gb*/false,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003205 dex_filename,
Andreas Gampe08c277c2017-04-26 22:22:15 -07003206 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07003207 if (oat_file == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08003208 LOG(ERROR) << "Failed to open oat file from '" << oat_filename << "': " << error_msg;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003209 return EXIT_FAILURE;
3210 }
3211
Andreas Gampe2d8614b2016-03-07 16:31:34 -08003212 bool result;
3213 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
3214 // files for 64-bit code in the past.
3215 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
Andreas Gampe08c277c2017-04-26 22:22:15 -07003216 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file.get(), output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08003217 result = oat_symbolizer.Symbolize();
3218 } else {
Andreas Gampe08c277c2017-04-26 22:22:15 -07003219 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file.get(), output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08003220 result = oat_symbolizer.Symbolize();
3221 }
3222 if (!result) {
Andreas Gampe221d9812018-01-22 17:48:56 -08003223 LOG(ERROR) << "Failed to symbolize";
Andreas Gampe00b25f32014-09-17 21:49:05 -07003224 return EXIT_FAILURE;
3225 }
3226
3227 return EXIT_SUCCESS;
3228}
3229
Andreas Gampe9fded872016-09-25 16:08:35 -07003230class IMTDumper {
3231 public:
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003232 static bool Dump(Runtime* runtime,
3233 const std::string& imt_file,
3234 bool dump_imt_stats,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003235 const char* oat_filename,
3236 const char* dex_filename) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003237 Thread* self = Thread::Current();
3238
3239 ScopedObjectAccess soa(self);
3240 StackHandleScope<1> scope(self);
3241 MutableHandle<mirror::ClassLoader> class_loader = scope.NewHandle<mirror::ClassLoader>(nullptr);
3242 std::vector<const DexFile*> class_path;
3243
3244 if (oat_filename != nullptr) {
3245 std::string error_msg;
Nicolas Geoffray30025092018-04-19 14:43:29 +01003246 std::unique_ptr<OatFile> oat_file(OatFile::Open(/* zip_fd */ -1,
3247 oat_filename,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003248 oat_filename,
3249 nullptr,
3250 nullptr,
3251 false,
3252 /*low_4gb*/false,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003253 dex_filename,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003254 &error_msg));
3255 if (oat_file == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08003256 LOG(ERROR) << "Failed to open oat file from '" << oat_filename << "': " << error_msg;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003257 return false;
3258 }
3259
3260 class_loader.Assign(soa.Decode<mirror::ClassLoader>(
3261 InstallOatFile(runtime, std::move(oat_file), &class_path)));
3262 } else {
3263 class_loader.Assign(nullptr); // Boot classloader. Just here for explicit documentation.
3264 class_path = runtime->GetClassLinker()->GetBootClassPath();
3265 }
3266
3267 if (!imt_file.empty()) {
3268 return DumpImt(runtime, imt_file, class_loader);
3269 }
3270
3271 if (dump_imt_stats) {
3272 return DumpImtStats(runtime, class_path, class_loader);
3273 }
3274
3275 LOG(FATAL) << "Should not reach here";
3276 UNREACHABLE();
3277 }
3278
3279 private:
3280 static bool DumpImt(Runtime* runtime,
3281 const std::string& imt_file,
3282 Handle<mirror::ClassLoader> h_class_loader)
3283 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003284 std::vector<std::string> lines = ReadCommentedInputFromFile(imt_file);
3285 std::unordered_set<std::string> prepared;
3286
3287 for (const std::string& line : lines) {
3288 // A line should be either a class descriptor, in which case we will dump the complete IMT,
3289 // or a class descriptor and an interface method, in which case we will lookup the method,
3290 // determine its IMT slot, and check the class' IMT.
3291 size_t first_space = line.find(' ');
3292 if (first_space == std::string::npos) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003293 DumpIMTForClass(runtime, line, h_class_loader, &prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07003294 } else {
3295 DumpIMTForMethod(runtime,
3296 line.substr(0, first_space),
3297 line.substr(first_space + 1, std::string::npos),
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003298 h_class_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003299 &prepared);
3300 }
3301 std::cerr << std::endl;
3302 }
3303
3304 return true;
3305 }
3306
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003307 static bool DumpImtStats(Runtime* runtime,
3308 const std::vector<const DexFile*>& dex_files,
3309 Handle<mirror::ClassLoader> h_class_loader)
3310 REQUIRES_SHARED(Locks::mutator_lock_) {
3311 size_t without_imt = 0;
3312 size_t with_imt = 0;
Andreas Gampe9fded872016-09-25 16:08:35 -07003313 std::map<size_t, size_t> histogram;
3314
3315 ClassLinker* class_linker = runtime->GetClassLinker();
3316 const PointerSize pointer_size = class_linker->GetImagePointerSize();
3317 std::unordered_set<std::string> prepared;
3318
3319 Thread* self = Thread::Current();
Andreas Gampe9fded872016-09-25 16:08:35 -07003320 StackHandleScope<1> scope(self);
3321 MutableHandle<mirror::Class> h_klass(scope.NewHandle<mirror::Class>(nullptr));
3322
3323 for (const DexFile* dex_file : dex_files) {
3324 for (uint32_t class_def_index = 0;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003325 class_def_index != dex_file->NumClassDefs();
3326 ++class_def_index) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003327 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
3328 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003329 h_klass.Assign(class_linker->FindClass(self, descriptor, h_class_loader));
Andreas Gampefa4333d2017-02-14 11:10:34 -08003330 if (h_klass == nullptr) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003331 std::cerr << "Warning: could not load " << descriptor << std::endl;
3332 continue;
3333 }
3334
3335 if (HasNoIMT(runtime, h_klass, pointer_size, &prepared)) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003336 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003337 continue;
3338 }
3339
3340 ImTable* im_table = PrepareAndGetImTable(runtime, h_klass, pointer_size, &prepared);
3341 if (im_table == nullptr) {
3342 // Should not happen, but accept.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003343 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003344 continue;
3345 }
3346
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003347 with_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07003348 for (size_t imt_index = 0; imt_index != ImTable::kSize; ++imt_index) {
3349 ArtMethod* ptr = im_table->Get(imt_index, pointer_size);
3350 if (ptr->IsRuntimeMethod()) {
3351 if (ptr->IsImtUnimplementedMethod()) {
3352 histogram[0]++;
3353 } else {
3354 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3355 histogram[current_table->NumEntries(pointer_size)]++;
3356 }
3357 } else {
3358 histogram[1]++;
3359 }
3360 }
3361 }
3362 }
3363
3364 std::cerr << "IMT stats:"
3365 << std::endl << std::endl;
3366
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003367 std::cerr << " " << with_imt << " classes with IMT."
Andreas Gampe9fded872016-09-25 16:08:35 -07003368 << std::endl << std::endl;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003369 std::cerr << " " << without_imt << " classes without IMT (or copy from Object)."
Andreas Gampe9fded872016-09-25 16:08:35 -07003370 << std::endl << std::endl;
3371
3372 double sum_one = 0;
3373 size_t count_one = 0;
3374
3375 std::cerr << " " << "IMT histogram" << std::endl;
3376 for (auto& bucket : histogram) {
3377 std::cerr << " " << bucket.first << " " << bucket.second << std::endl;
3378 if (bucket.first > 0) {
3379 sum_one += bucket.second * bucket.first;
3380 count_one += bucket.second;
3381 }
3382 }
3383
3384 double count_zero = count_one + histogram[0];
3385 std::cerr << " Stats:" << std::endl;
3386 std::cerr << " Average depth (including empty): " << (sum_one / count_zero) << std::endl;
3387 std::cerr << " Average depth (excluding empty): " << (sum_one / count_one) << std::endl;
3388
3389 return true;
3390 }
3391
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003392 // Return whether the given class has no IMT (or the one shared with java.lang.Object).
Andreas Gampe9fded872016-09-25 16:08:35 -07003393 static bool HasNoIMT(Runtime* runtime,
3394 Handle<mirror::Class> klass,
3395 const PointerSize pointer_size,
3396 std::unordered_set<std::string>* prepared)
3397 REQUIRES_SHARED(Locks::mutator_lock_) {
3398 if (klass->IsObjectClass() || !klass->ShouldHaveImt()) {
3399 return true;
3400 }
3401
3402 if (klass->GetImt(pointer_size) == nullptr) {
3403 PrepareClass(runtime, klass, prepared);
3404 }
3405
3406 mirror::Class* object_class = mirror::Class::GetJavaLangClass()->GetSuperClass();
3407 DCHECK(object_class->IsObjectClass());
3408
3409 bool result = klass->GetImt(pointer_size) == object_class->GetImt(pointer_size);
3410
Mathieu Chartier6beced42016-11-15 15:51:31 -08003411 if (klass->GetIfTable()->Count() == 0) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003412 DCHECK(result);
3413 }
3414
3415 return result;
3416 }
3417
3418 static void PrintTable(ImtConflictTable* table, PointerSize pointer_size)
3419 REQUIRES_SHARED(Locks::mutator_lock_) {
3420 if (table == nullptr) {
3421 std::cerr << " <No IMT?>" << std::endl;
3422 return;
3423 }
3424 size_t table_index = 0;
3425 for (;;) {
3426 ArtMethod* ptr = table->GetInterfaceMethod(table_index, pointer_size);
3427 if (ptr == nullptr) {
3428 return;
3429 }
3430 table_index++;
David Sehr709b0702016-10-13 09:12:37 -07003431 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003432 }
3433 }
3434
3435 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3436 Thread* self,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003437 Handle<mirror::ClassLoader> h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003438 const std::string& class_name,
3439 const PointerSize pointer_size,
3440 mirror::Class** klass_out,
3441 std::unordered_set<std::string>* prepared)
3442 REQUIRES_SHARED(Locks::mutator_lock_) {
3443 if (class_name.empty()) {
3444 return nullptr;
3445 }
3446
3447 std::string descriptor;
3448 if (class_name[0] == 'L') {
3449 descriptor = class_name;
3450 } else {
3451 descriptor = DotToDescriptor(class_name.c_str());
3452 }
3453
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003454 mirror::Class* klass = runtime->GetClassLinker()->FindClass(self, descriptor.c_str(), h_loader);
Andreas Gampe9fded872016-09-25 16:08:35 -07003455
3456 if (klass == nullptr) {
3457 self->ClearException();
3458 std::cerr << "Did not find " << class_name << std::endl;
3459 *klass_out = nullptr;
3460 return nullptr;
3461 }
3462
3463 StackHandleScope<1> scope(Thread::Current());
3464 Handle<mirror::Class> h_klass = scope.NewHandle<mirror::Class>(klass);
3465
3466 ImTable* ret = PrepareAndGetImTable(runtime, h_klass, pointer_size, prepared);
3467 *klass_out = h_klass.Get();
3468 return ret;
3469 }
3470
3471 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3472 Handle<mirror::Class> h_klass,
3473 const PointerSize pointer_size,
3474 std::unordered_set<std::string>* prepared)
3475 REQUIRES_SHARED(Locks::mutator_lock_) {
3476 PrepareClass(runtime, h_klass, prepared);
3477 return h_klass->GetImt(pointer_size);
3478 }
3479
3480 static void DumpIMTForClass(Runtime* runtime,
3481 const std::string& class_name,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003482 Handle<mirror::ClassLoader> h_loader,
3483 std::unordered_set<std::string>* prepared)
3484 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003485 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
3486 mirror::Class* klass;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003487 ImTable* imt = PrepareAndGetImTable(runtime,
3488 Thread::Current(),
3489 h_loader,
3490 class_name,
3491 pointer_size,
3492 &klass,
3493 prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07003494 if (imt == nullptr) {
3495 return;
3496 }
3497
3498 std::cerr << class_name << std::endl << " IMT:" << std::endl;
3499 for (size_t index = 0; index < ImTable::kSize; ++index) {
3500 std::cerr << " " << index << ":" << std::endl;
3501 ArtMethod* ptr = imt->Get(index, pointer_size);
3502 if (ptr->IsRuntimeMethod()) {
3503 if (ptr->IsImtUnimplementedMethod()) {
3504 std::cerr << " <empty>" << std::endl;
3505 } else {
3506 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3507 PrintTable(current_table, pointer_size);
3508 }
3509 } else {
David Sehr709b0702016-10-13 09:12:37 -07003510 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003511 }
3512 }
3513
3514 std::cerr << " Interfaces:" << std::endl;
3515 // Run through iftable, find methods that slot here, see if they fit.
3516 mirror::IfTable* if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003517 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
3518 mirror::Class* iface = if_table->GetInterface(i);
3519 std::string iface_name;
3520 std::cerr << " " << iface->GetDescriptor(&iface_name) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003521
Mathieu Chartier6beced42016-11-15 15:51:31 -08003522 for (ArtMethod& iface_method : iface->GetVirtualMethods(pointer_size)) {
3523 uint32_t class_hash, name_hash, signature_hash;
3524 ImTable::GetImtHashComponents(&iface_method, &class_hash, &name_hash, &signature_hash);
3525 uint32_t imt_slot = ImTable::GetImtIndex(&iface_method);
3526 std::cerr << " " << iface_method.PrettyMethod(true)
3527 << " slot=" << imt_slot
3528 << std::hex
3529 << " class_hash=0x" << class_hash
3530 << " name_hash=0x" << name_hash
3531 << " signature_hash=0x" << signature_hash
3532 << std::dec
3533 << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003534 }
3535 }
3536 }
3537
3538 static void DumpIMTForMethod(Runtime* runtime,
3539 const std::string& class_name,
3540 const std::string& method,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003541 Handle<mirror::ClassLoader> h_loader,
3542 std::unordered_set<std::string>* prepared)
3543 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003544 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
3545 mirror::Class* klass;
3546 ImTable* imt = PrepareAndGetImTable(runtime,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003547 Thread::Current(),
3548 h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003549 class_name,
3550 pointer_size,
3551 &klass,
3552 prepared);
3553 if (imt == nullptr) {
3554 return;
3555 }
3556
3557 std::cerr << class_name << " <" << method << ">" << std::endl;
3558 for (size_t index = 0; index < ImTable::kSize; ++index) {
3559 ArtMethod* ptr = imt->Get(index, pointer_size);
3560 if (ptr->IsRuntimeMethod()) {
3561 if (ptr->IsImtUnimplementedMethod()) {
3562 continue;
3563 }
3564
3565 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3566 if (current_table == nullptr) {
3567 continue;
3568 }
3569
3570 size_t table_index = 0;
3571 for (;;) {
3572 ArtMethod* ptr2 = current_table->GetInterfaceMethod(table_index, pointer_size);
3573 if (ptr2 == nullptr) {
3574 break;
3575 }
3576 table_index++;
3577
David Sehr709b0702016-10-13 09:12:37 -07003578 std::string p_name = ptr2->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003579 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003580 std::cerr << " Slot "
3581 << index
3582 << " ("
3583 << current_table->NumEntries(pointer_size)
3584 << ")"
3585 << std::endl;
3586 PrintTable(current_table, pointer_size);
3587 return;
3588 }
3589 }
3590 } else {
David Sehr709b0702016-10-13 09:12:37 -07003591 std::string p_name = ptr->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003592 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003593 std::cerr << " Slot " << index << " (1)" << std::endl;
3594 std::cerr << " " << p_name << std::endl;
3595 } else {
3596 // Run through iftable, find methods that slot here, see if they fit.
3597 mirror::IfTable* if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003598 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
3599 mirror::Class* iface = if_table->GetInterface(i);
3600 size_t num_methods = iface->NumDeclaredVirtualMethods();
3601 if (num_methods > 0) {
3602 for (ArtMethod& iface_method : iface->GetMethods(pointer_size)) {
3603 if (ImTable::GetImtIndex(&iface_method) == index) {
3604 std::string i_name = iface_method.PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003605 if (android::base::StartsWith(i_name, method.c_str())) {
Mathieu Chartier6beced42016-11-15 15:51:31 -08003606 std::cerr << " Slot " << index << " (1)" << std::endl;
3607 std::cerr << " " << p_name << " (" << i_name << ")" << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003608 }
3609 }
3610 }
3611 }
3612 }
3613 }
3614 }
3615 }
3616 }
3617
3618 // Read lines from the given stream, dropping comments and empty lines
3619 static std::vector<std::string> ReadCommentedInputStream(std::istream& in_stream) {
3620 std::vector<std::string> output;
3621 while (in_stream.good()) {
3622 std::string dot;
3623 std::getline(in_stream, dot);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003624 if (android::base::StartsWith(dot, "#") || dot.empty()) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003625 continue;
3626 }
3627 output.push_back(dot);
3628 }
3629 return output;
3630 }
3631
3632 // Read lines from the given file, dropping comments and empty lines.
3633 static std::vector<std::string> ReadCommentedInputFromFile(const std::string& input_filename) {
3634 std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
3635 if (input_file.get() == nullptr) {
3636 LOG(ERROR) << "Failed to open input file " << input_filename;
3637 return std::vector<std::string>();
3638 }
3639 std::vector<std::string> result = ReadCommentedInputStream(*input_file);
3640 input_file->close();
3641 return result;
3642 }
3643
3644 // Prepare a class, i.e., ensure it has a filled IMT. Will do so recursively for superclasses,
3645 // and note in the given set that the work was done.
3646 static void PrepareClass(Runtime* runtime,
3647 Handle<mirror::Class> h_klass,
3648 std::unordered_set<std::string>* done)
3649 REQUIRES_SHARED(Locks::mutator_lock_) {
3650 if (!h_klass->ShouldHaveImt()) {
3651 return;
3652 }
3653
3654 std::string name;
3655 name = h_klass->GetDescriptor(&name);
3656
3657 if (done->find(name) != done->end()) {
3658 return;
3659 }
3660 done->insert(name);
3661
3662 if (h_klass->HasSuperClass()) {
3663 StackHandleScope<1> h(Thread::Current());
3664 PrepareClass(runtime, h.NewHandle<mirror::Class>(h_klass->GetSuperClass()), done);
3665 }
3666
3667 if (!h_klass->IsTemp()) {
3668 runtime->GetClassLinker()->FillIMTAndConflictTables(h_klass.Get());
3669 }
3670 }
3671};
3672
Igor Murashkin37743352014-11-13 14:38:00 -08003673struct OatdumpArgs : public CmdlineArgs {
3674 protected:
3675 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003676
Igor Murashkin37743352014-11-13 14:38:00 -08003677 virtual ParseStatus ParseCustom(const StringPiece& option,
3678 std::string* error_msg) OVERRIDE {
3679 {
3680 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
3681 if (base_parse != kParseUnknownArgument) {
3682 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003683 }
3684 }
3685
Igor Murashkin37743352014-11-13 14:38:00 -08003686 if (option.starts_with("--oat-file=")) {
3687 oat_filename_ = option.substr(strlen("--oat-file=")).data();
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003688 } else if (option.starts_with("--dex-file=")) {
3689 dex_filename_ = option.substr(strlen("--dex-file=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003690 } else if (option.starts_with("--image=")) {
3691 image_location_ = option.substr(strlen("--image=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003692 } else if (option == "--no-dump:vmap") {
3693 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01003694 } else if (option =="--dump:code_info_stack_maps") {
3695 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003696 } else if (option == "--no-disassemble") {
3697 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003698 } else if (option =="--header-only") {
3699 dump_header_only_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003700 } else if (option.starts_with("--symbolize=")) {
3701 oat_filename_ = option.substr(strlen("--symbolize=")).data();
3702 symbolize_ = true;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003703 } else if (option.starts_with("--only-keep-debug")) {
3704 only_keep_debug_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003705 } else if (option.starts_with("--class-filter=")) {
3706 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003707 } else if (option.starts_with("--method-filter=")) {
3708 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003709 } else if (option.starts_with("--list-classes")) {
3710 list_classes_ = true;
3711 } else if (option.starts_with("--list-methods")) {
3712 list_methods_ = true;
3713 } else if (option.starts_with("--export-dex-to=")) {
3714 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
3715 } else if (option.starts_with("--addr2instr=")) {
3716 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
3717 *error_msg = "Address conversion failed";
3718 return kParseError;
3719 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003720 } else if (option.starts_with("--app-image=")) {
3721 app_image_ = option.substr(strlen("--app-image=")).data();
3722 } else if (option.starts_with("--app-oat=")) {
3723 app_oat_ = option.substr(strlen("--app-oat=")).data();
Andreas Gampe9fded872016-09-25 16:08:35 -07003724 } else if (option.starts_with("--dump-imt=")) {
3725 imt_dump_ = option.substr(strlen("--dump-imt=")).data();
3726 } else if (option == "--dump-imt-stats") {
3727 imt_stat_dump_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003728 } else {
3729 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003730 }
3731
Igor Murashkin37743352014-11-13 14:38:00 -08003732 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003733 }
3734
Igor Murashkin37743352014-11-13 14:38:00 -08003735 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
3736 // Infer boot image location from the image location if possible.
3737 if (boot_image_location_ == nullptr) {
3738 boot_image_location_ = image_location_;
3739 }
3740
3741 // Perform the parent checks.
3742 ParseStatus parent_checks = Base::ParseChecks(error_msg);
3743 if (parent_checks != kParseOk) {
3744 return parent_checks;
3745 }
3746
3747 // Perform our own checks.
3748 if (image_location_ == nullptr && oat_filename_ == nullptr) {
3749 *error_msg = "Either --image or --oat-file must be specified";
3750 return kParseError;
3751 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
3752 *error_msg = "Either --image or --oat-file must be specified but not both";
3753 return kParseError;
3754 }
3755
3756 return kParseOk;
3757 }
3758
3759 virtual std::string GetUsage() const {
3760 std::string usage;
3761
3762 usage +=
3763 "Usage: oatdump [options] ...\n"
3764 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
3765 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
3766 "\n"
3767 // Either oat-file or image is required.
3768 " --oat-file=<file.oat>: specifies an input oat filename.\n"
3769 " Example: --oat-file=/system/framework/boot.oat\n"
3770 "\n"
3771 " --image=<file.art>: specifies an input image location.\n"
3772 " Example: --image=/system/framework/boot.art\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003773 "\n"
3774 " --app-image=<file.art>: specifies an input app image. Must also have a specified\n"
Roland Levillain4f1c9e62017-06-28 16:44:30 +01003775 " boot image (with --image) and app oat file (with --app-oat).\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003776 " Example: --app-image=app.art\n"
3777 "\n"
3778 " --app-oat=<file.odex>: specifies an input app oat.\n"
3779 " Example: --app-oat=app.odex\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003780 "\n";
3781
3782 usage += Base::GetUsage();
3783
3784 usage += // Optional.
Igor Murashkin37743352014-11-13 14:38:00 -08003785 " --no-dump:vmap may be used to disable vmap dumping.\n"
3786 " Example: --no-dump:vmap\n"
3787 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01003788 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
3789 " Example: --dump:code_info_stack_maps\n"
3790 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003791 " --no-disassemble may be used to disable disassembly.\n"
3792 " Example: --no-disassemble\n"
3793 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00003794 " --header-only may be used to print only the oat header.\n"
3795 " Example: --header-only\n"
3796 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003797 " --list-classes may be used to list target file classes (can be used with filters).\n"
3798 " Example: --list-classes\n"
3799 " Example: --list-classes --class-filter=com.example.foo\n"
3800 "\n"
3801 " --list-methods may be used to list target file methods (can be used with filters).\n"
3802 " Example: --list-methods\n"
3803 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
3804 "\n"
3805 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
3806 " Example: --symbolize=/system/framework/boot.oat\n"
3807 "\n"
David Srbecky2fdd03c2016-03-10 15:32:37 +00003808 " --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
3809 " .rodata and .text sections are omitted in the output file to save space.\n"
3810 " Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
3811 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003812 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
3813 " Example: --class-filter=com.example.foo\n"
3814 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003815 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
3816 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003817 "\n"
3818 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
3819 " Example: --export-dex-to=/data/local/tmp\n"
3820 "\n"
3821 " --addr2instr=<address>: output matching method disassembled code from relative\n"
3822 " address (e.g. PC from crash dump)\n"
3823 " Example: --addr2instr=0x00001a3b\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003824 "\n"
3825 " --dump-imt=<file.txt>: output IMT collisions (if any) for the given receiver\n"
3826 " types and interface methods in the given file. The file\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003827 " is read line-wise, where each line should either be a class\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003828 " name or descriptor, or a class name/descriptor and a prefix\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003829 " of a complete method name (separated by a whitespace).\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003830 " Example: --dump-imt=imt.txt\n"
3831 "\n"
3832 " --dump-imt-stats: output IMT statistics for the given boot image\n"
3833 " Example: --dump-imt-stats"
Igor Murashkin37743352014-11-13 14:38:00 -08003834 "\n";
3835
3836 return usage;
3837 }
3838
3839 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07003840 const char* oat_filename_ = nullptr;
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003841 const char* dex_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003842 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00003843 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07003844 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003845 std::string elf_filename_prefix_;
Andreas Gampe9fded872016-09-25 16:08:35 -07003846 std::string imt_dump_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003847 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01003848 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003849 bool disassemble_code_ = true;
3850 bool symbolize_ = false;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003851 bool only_keep_debug_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003852 bool list_classes_ = false;
3853 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003854 bool dump_header_only_ = false;
Andreas Gampe9fded872016-09-25 16:08:35 -07003855 bool imt_stat_dump_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003856 uint32_t addr2instr_ = 0;
3857 const char* export_dex_location_ = nullptr;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003858 const char* app_image_ = nullptr;
3859 const char* app_oat_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003860};
3861
Igor Murashkin37743352014-11-13 14:38:00 -08003862struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
3863 virtual bool NeedsRuntime() OVERRIDE {
3864 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003865
Igor Murashkin37743352014-11-13 14:38:00 -08003866 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
3867 bool absolute_addresses = (args_->oat_filename_ == nullptr);
3868
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003869 oat_dumper_options_.reset(new OatDumperOptions(
Igor Murashkin37743352014-11-13 14:38:00 -08003870 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01003871 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08003872 args_->disassemble_code_,
3873 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003874 args_->class_filter_,
3875 args_->method_filter_,
3876 args_->list_classes_,
3877 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00003878 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003879 args_->export_dex_location_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003880 args_->app_image_,
3881 args_->app_oat_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003882 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08003883
Andreas Gampe9fded872016-09-25 16:08:35 -07003884 return (args_->boot_image_location_ != nullptr ||
3885 args_->image_location_ != nullptr ||
3886 !args_->imt_dump_.empty()) &&
Igor Murashkin37743352014-11-13 14:38:00 -08003887 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003888 }
3889
Igor Murashkin37743352014-11-13 14:38:00 -08003890 virtual bool ExecuteWithoutRuntime() OVERRIDE {
3891 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08003892 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003893
Mathieu Chartierd424d082014-10-15 10:31:46 -07003894 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08003895
Andreas Gampec24f3992014-12-17 20:40:11 -08003896 if (args_->symbolize_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00003897 // ELF has special kind of section called SHT_NOBITS which allows us to create
3898 // sections which exist but their data is omitted from the ELF file to save space.
3899 // This is what "strip --only-keep-debug" does when it creates separate ELF file
3900 // with only debug data. We use it in similar way to exclude .rodata and .text.
3901 bool no_bits = args_->only_keep_debug_;
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003902 return SymbolizeOat(args_->oat_filename_, args_->dex_filename_, args_->output_name_, no_bits)
3903 == EXIT_SUCCESS;
Andreas Gampec24f3992014-12-17 20:40:11 -08003904 } else {
3905 return DumpOat(nullptr,
3906 args_->oat_filename_,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003907 args_->dex_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003908 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08003909 args_->os_) == EXIT_SUCCESS;
3910 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07003911 }
3912
Igor Murashkin37743352014-11-13 14:38:00 -08003913 virtual bool ExecuteWithRuntime(Runtime* runtime) {
3914 CHECK(args_ != nullptr);
3915
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003916 if (!args_->imt_dump_.empty() || args_->imt_stat_dump_) {
3917 return IMTDumper::Dump(runtime,
3918 args_->imt_dump_,
3919 args_->imt_stat_dump_,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003920 args_->oat_filename_,
3921 args_->dex_filename_);
Andreas Gampe9fded872016-09-25 16:08:35 -07003922 }
3923
Igor Murashkin37743352014-11-13 14:38:00 -08003924 if (args_->oat_filename_ != nullptr) {
3925 return DumpOat(runtime,
3926 args_->oat_filename_,
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00003927 args_->dex_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003928 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08003929 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003930 }
Igor Murashkin37743352014-11-13 14:38:00 -08003931
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003932 return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003933 }
3934
Igor Murashkin37743352014-11-13 14:38:00 -08003935 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
3936};
Andreas Gampe00b25f32014-09-17 21:49:05 -07003937
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003938} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07003939
3940int main(int argc, char** argv) {
Andreas Gampe221d9812018-01-22 17:48:56 -08003941 // Output all logging to stderr.
3942 android::base::SetLogger(android::base::StderrLogger);
3943
Igor Murashkin37743352014-11-13 14:38:00 -08003944 art::OatdumpMain main;
3945 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07003946}