blob: b3b67e0d2e39a7eb4cefbdc95d8c95aebc0d075b [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrom78128a62011-09-15 17:21:19 -070016
17#include <stdio.h>
18#include <stdlib.h>
19
Brian Carlstrom27ec9612011-09-19 20:20:38 -070020#include <fstream>
21#include <iostream>
Igor Murashkin37743352014-11-13 14:38:00 -080022#include <map>
23#include <set>
Brian Carlstrom78128a62011-09-15 17:21:19 -070024#include <string>
Andreas Gampe54fc26c2014-09-04 21:47:42 -070025#include <unordered_map>
Andreas Gampe9fded872016-09-25 16:08:35 -070026#include <unordered_set>
Brian Carlstrom78128a62011-09-15 17:21:19 -070027#include <vector>
28
Andreas Gampe46ee31b2016-12-14 10:11:49 -080029#include "android-base/stringprintf.h"
Andreas Gampe9186ced2016-12-12 14:28:21 -080030#include "android-base/strings.h"
31
Ian Rogersd582fa42014-11-05 23:46:43 -080032#include "arch/instruction_set_features.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070033#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070034#include "art_method-inl.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000035#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080036#include "base/unix_file/fd_file.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070037#include "class_linker.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038#include "class_linker-inl.h"
David Srbecky5d950762016-03-07 20:47:29 +000039#include "debug/elf_debug_writer.h"
40#include "debug/method_debug_info.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070041#include "dex_file-inl.h"
Christina Wadsworthbc233ac2016-06-20 15:01:32 -070042#include "dex_instruction-inl.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080043#include "disassembler.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070044#include "elf_builder.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070045#include "gc/space/image_space.h"
46#include "gc/space/large_object_space.h"
47#include "gc/space/space-inl.h"
Mathieu Chartier4a26f172016-01-26 14:26:18 -080048#include "image-inl.h"
Andreas Gampe9fded872016-09-25 16:08:35 -070049#include "imtable-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080050#include "indenter.h"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -070051#include "interpreter/unstarted_runtime.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000052#include "linker/buffered_output_stream.h"
53#include "linker/file_output_stream.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080054#include "mirror/array-inl.h"
55#include "mirror/class-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010056#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080057#include "mirror/object-inl.h"
58#include "mirror/object_array-inl.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080059#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010060#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070061#include "oat_file_manager.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080062#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070063#include "safe_map.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070064#include "scoped_thread_state_change-inl.h"
Nicolas Geoffray9c055782016-07-14 09:24:30 +000065#include "ScopedLocalRef.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070066#include "stack.h"
Mathieu Chartierdc00f182016-07-14 10:10:44 -070067#include "stack_map.h"
68#include "string_reference.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080069#include "thread_list.h"
Andreas Gampe2ba88952016-04-29 17:52:07 -070070#include "type_lookup_table.h"
Nicolas Geoffray4acefd32016-10-24 13:14:58 +010071#include "vdex_file.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080072#include "verifier/method_verifier.h"
Nicolas Geoffraye70dd562016-10-30 21:03:35 +000073#include "verifier/verifier_deps.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070074#include "well_known_classes.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070075
Igor Murashkin37743352014-11-13 14:38:00 -080076#include <sys/stat.h>
77#include "cmdline.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070078
Igor Murashkin37743352014-11-13 14:38:00 -080079namespace art {
Brian Carlstrom78128a62011-09-15 17:21:19 -070080
Andreas Gampe46ee31b2016-12-14 10:11:49 -080081using android::base::StringPrintf;
82
Mathieu Chartiere401d142015-04-22 13:56:20 -070083const char* image_methods_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -080084 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -070085 "kImtConflictMethod",
Mathieu Chartier2d2621a2014-10-23 16:48:06 -070086 "kImtUnimplementedMethod",
Vladimir Markofd36f1f2016-08-03 18:49:58 +010087 "kSaveAllCalleeSavesMethod",
88 "kSaveRefsOnlyMethod",
89 "kSaveRefsAndArgsMethod",
Vladimir Marko952dbb12016-07-28 12:01:51 +010090 "kSaveEverythingMethod",
Mathieu Chartiere401d142015-04-22 13:56:20 -070091};
92
93const char* image_roots_descriptions_[] = {
Brian Carlstrom58ae9412011-10-04 00:56:06 -070094 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070095 "kClassRoots",
Vladimir Markoeca3eda2016-11-09 16:26:44 +000096 "kClassLoader",
Brian Carlstrom78128a62011-09-15 17:21:19 -070097};
98
Mathieu Chartierac8f4392015-08-27 13:54:20 -070099// Map is so that we don't allocate multiple dex files for the same OatDexFile.
100static std::map<const OatFile::OatDexFile*,
101 std::unique_ptr<const DexFile>> opened_dex_files;
102
103const DexFile* OpenDexFile(const OatFile::OatDexFile* oat_dex_file, std::string* error_msg) {
104 DCHECK(oat_dex_file != nullptr);
105 auto it = opened_dex_files.find(oat_dex_file);
106 if (it != opened_dex_files.end()) {
107 return it->second.get();
108 }
109 const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
110 opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
111 return ret;
112}
113
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800114template <typename ElfTypes>
David Srbeckybc90fd02015-04-22 19:40:27 +0100115class OatSymbolizer FINAL {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700116 public:
David Srbecky2fdd03c2016-03-10 15:32:37 +0000117 OatSymbolizer(const OatFile* oat_file, const std::string& output_name, bool no_bits) :
118 oat_file_(oat_file),
119 builder_(nullptr),
120 output_name_(output_name.empty() ? "symbolized.oat" : output_name),
121 no_bits_(no_bits) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700122 }
123
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700124 bool Symbolize() {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000125 const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800126 std::unique_ptr<const InstructionSetFeatures> features = InstructionSetFeatures::FromBitmap(
David Srbecky5d811202016-03-08 13:21:22 +0000127 isa, oat_file_->GetOatHeader().GetInstructionSetFeaturesBitmap());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000128
Andreas Gampe08c277c2017-04-26 22:22:15 -0700129 std::unique_ptr<File> elf_file(OS::CreateEmptyFile(output_name_.c_str()));
130 if (elf_file == nullptr) {
131 return false;
132 }
Andreas Gampe8bdda5a2017-06-08 15:30:36 -0700133 std::unique_ptr<BufferedOutputStream> output_stream =
134 std::make_unique<BufferedOutputStream>(std::make_unique<FileOutputStream>(elf_file.get()));
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800135 builder_.reset(new ElfBuilder<ElfTypes>(isa, features.get(), output_stream.get()));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000136
137 builder_->Start();
138
139 auto* rodata = builder_->GetRoData();
140 auto* text = builder_->GetText();
141 auto* bss = builder_->GetBss();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000142
David Srbecky6d8c8f02015-10-26 10:57:09 +0000143 const uint8_t* rodata_begin = oat_file_->Begin();
144 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
David Srbecky2fdd03c2016-03-10 15:32:37 +0000145 if (no_bits_) {
146 rodata->WriteNoBitsSection(rodata_size);
147 } else {
148 rodata->Start();
149 rodata->WriteFully(rodata_begin, rodata_size);
150 rodata->End();
151 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000152
David Srbecky6d8c8f02015-10-26 10:57:09 +0000153 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
154 const size_t text_size = oat_file_->End() - text_begin;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000155 if (no_bits_) {
156 text->WriteNoBitsSection(text_size);
157 } else {
158 text->Start();
159 text->WriteFully(text_begin, text_size);
160 text->End();
161 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000162
163 if (oat_file_->BssSize() != 0) {
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000164 bss->WriteNoBitsSection(oat_file_->BssSize());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000165 }
166
Douglas Leung316a2182015-09-17 15:26:25 -0700167 if (isa == kMips || isa == kMips64) {
168 builder_->WriteMIPSabiflagsSection();
169 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000170 builder_->PrepareDynamicSection(elf_file->GetPath(),
171 rodata_size,
172 text_size,
173 oat_file_->BssSize(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100174 oat_file_->BssMethodsOffset(),
Vladimir Markoaad75c62016-10-03 08:46:48 +0000175 oat_file_->BssRootsOffset());
Vladimir Marko944da602016-02-19 12:27:55 +0000176 builder_->WriteDynamicSection();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700177
David Srbecky5d950762016-03-07 20:47:29 +0000178 Walk();
179 for (const auto& trampoline : debug::MakeTrampolineInfos(oat_file_->GetOatHeader())) {
180 method_debug_infos_.push_back(trampoline);
181 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700182
David Srbecky5d950762016-03-07 20:47:29 +0000183 debug::WriteDebugInfo(builder_.get(),
184 ArrayRef<const debug::MethodDebugInfo>(method_debug_infos_),
185 dwarf::DW_DEBUG_FRAME_FORMAT,
186 true /* write_oat_patches */);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700187
David Srbecky6d8c8f02015-10-26 10:57:09 +0000188 builder_->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700189
Andreas Gampe08c277c2017-04-26 22:22:15 -0700190 bool ret_value = builder_->Good();
191
192 builder_.reset();
193 output_stream.reset();
194
195 if (elf_file->FlushCloseOrErase() != 0) {
196 return false;
197 }
198 elf_file.reset();
199
200 return ret_value;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700201 }
202
David Srbecky5d950762016-03-07 20:47:29 +0000203 void Walk() {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700204 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
205 for (size_t i = 0; i < oat_dex_files.size(); i++) {
206 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700207 CHECK(oat_dex_file != nullptr);
David Srbecky5d950762016-03-07 20:47:29 +0000208 WalkOatDexFile(oat_dex_file);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700209 }
210 }
211
David Srbecky5d950762016-03-07 20:47:29 +0000212 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700213 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700214 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
215 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700216 return;
217 }
218 for (size_t class_def_index = 0;
219 class_def_index < dex_file->NumClassDefs();
220 class_def_index++) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700221 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
222 OatClassType type = oat_class.GetType();
223 switch (type) {
224 case kOatClassAllCompiled:
225 case kOatClassSomeCompiled:
David Srbecky5d950762016-03-07 20:47:29 +0000226 WalkOatClass(oat_class, *dex_file, class_def_index);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700227 break;
228
229 case kOatClassNoneCompiled:
230 case kOatClassMax:
231 // Ignore.
232 break;
233 }
234 }
235 }
236
David Srbecky5d950762016-03-07 20:47:29 +0000237 void WalkOatClass(const OatFile::OatClass& oat_class,
238 const DexFile& dex_file,
239 uint32_t class_def_index) {
240 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700241 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700242 if (class_data == nullptr) { // empty class such as a marker interface?
243 return;
244 }
245 // Note: even if this is an interface or a native class, we still have to walk it, as there
246 // might be a static initializer.
247 ClassDataItemIterator it(dex_file, class_data);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700248 uint32_t class_method_idx = 0;
Mathieu Chartiere17cf242017-06-19 11:05:51 -0700249 it.SkipAllFields();
David Srbecky5d950762016-03-07 20:47:29 +0000250 for (; it.HasNextDirectMethod() || it.HasNextVirtualMethod(); it.Next()) {
251 WalkOatMethod(oat_class.GetOatMethod(class_method_idx++),
252 dex_file,
253 class_def_index,
254 it.GetMemberIndex(),
255 it.GetMethodCodeItem(),
256 it.GetMethodAccessFlags());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700257 }
258 DCHECK(!it.HasNext());
259 }
260
David Srbecky5d950762016-03-07 20:47:29 +0000261 void WalkOatMethod(const OatFile::OatMethod& oat_method,
262 const DexFile& dex_file,
263 uint32_t class_def_index,
264 uint32_t dex_method_index,
265 const DexFile::CodeItem* code_item,
266 uint32_t method_access_flags) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700267 if ((method_access_flags & kAccAbstract) != 0) {
268 // Abstract method, no code.
269 return;
270 }
David Srbecky5d950762016-03-07 20:47:29 +0000271 const OatHeader& oat_header = oat_file_->GetOatHeader();
272 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
273 if (method_header == nullptr || method_header->GetCodeSize() == 0) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700274 // No code.
275 return;
276 }
277
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100278 uint32_t entry_point = oat_method.GetCodeOffset() - oat_header.GetExecutableOffset();
279 // Clear Thumb2 bit.
280 const void* code_address = EntryPointToCodePointer(reinterpret_cast<void*>(entry_point));
281
David Srbecky5d950762016-03-07 20:47:29 +0000282 debug::MethodDebugInfo info = debug::MethodDebugInfo();
283 info.trampoline_name = nullptr;
284 info.dex_file = &dex_file;
285 info.class_def_index = class_def_index;
286 info.dex_method_index = dex_method_index;
287 info.access_flags = method_access_flags;
288 info.code_item = code_item;
289 info.isa = oat_header.GetInstructionSet();
290 info.deduped = !seen_offsets_.insert(oat_method.GetCodeOffset()).second;
291 info.is_native_debuggable = oat_header.IsNativeDebuggable();
292 info.is_optimized = method_header->IsOptimized();
293 info.is_code_address_text_relative = true;
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100294 info.code_address = reinterpret_cast<uintptr_t>(code_address);
David Srbecky5d950762016-03-07 20:47:29 +0000295 info.code_size = method_header->GetCodeSize();
296 info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
297 info.code_info = info.is_optimized ? method_header->GetOptimizedCodeInfoPtr() : nullptr;
298 info.cfi = ArrayRef<uint8_t>();
299 method_debug_infos_.push_back(info);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700300 }
301
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700302 private:
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700303 const OatFile* oat_file_;
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800304 std::unique_ptr<ElfBuilder<ElfTypes> > builder_;
David Srbecky5d950762016-03-07 20:47:29 +0000305 std::vector<debug::MethodDebugInfo> method_debug_infos_;
306 std::unordered_set<uint32_t> seen_offsets_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700307 const std::string output_name_;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000308 bool no_bits_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700309};
310
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700311class OatDumperOptions {
312 public:
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100313 OatDumperOptions(bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100314 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700315 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700316 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800317 const char* class_filter,
318 const char* method_filter,
319 bool list_classes,
320 bool list_methods,
David Brazdilc03d7b62016-03-02 12:18:03 +0000321 bool dump_header_only,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800322 const char* export_dex_location,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800323 const char* app_image,
324 const char* app_oat,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800325 uint32_t addr2instr)
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100326 : dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100327 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700328 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700329 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800330 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000331 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800332 list_classes_(list_classes),
333 list_methods_(list_methods),
David Brazdilc03d7b62016-03-02 12:18:03 +0000334 dump_header_only_(dump_header_only),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800335 export_dex_location_(export_dex_location),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800336 app_image_(app_image),
337 app_oat_(app_oat),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800338 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800339 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700340
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700341 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100342 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700343 const bool disassemble_code_;
344 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800345 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000346 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800347 const bool list_classes_;
348 const bool list_methods_;
David Brazdilc03d7b62016-03-02 12:18:03 +0000349 const bool dump_header_only_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800350 const char* const export_dex_location_;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800351 const char* const app_image_;
352 const char* const app_oat_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800353 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700354 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700355};
356
Elliott Hughese3c845c2012-02-28 17:23:01 -0800357class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700358 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100359 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000360 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800361 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700362 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800363 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800364 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
365 disassembler_(Disassembler::Create(instruction_set_,
Andreas Gampe372f3a32016-08-19 10:49:06 -0700366 new DisassemblerOptions(
367 options_.absolute_addresses_,
368 oat_file.Begin(),
369 oat_file.End(),
370 true /* can_read_literals_ */,
371 Is64BitInstructionSet(instruction_set_)
372 ? &Thread::DumpThreadOffset<PointerSize::k64>
373 : &Thread::DumpThreadOffset<PointerSize::k32>))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800374 CHECK(options_.class_loader_ != nullptr);
375 CHECK(options_.class_filter_ != nullptr);
376 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800377 AddAllOffsets();
378 }
379
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700380 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700381 delete disassembler_;
382 }
383
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800384 InstructionSet GetInstructionSet() {
385 return instruction_set_;
386 }
387
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700388 bool Dump(std::ostream& os) {
389 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800390 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700391
392 os << "MAGIC:\n";
393 os << oat_header.GetMagic() << "\n\n";
394
Jeff Hao4b07a6e2016-01-15 12:50:44 -0800395 os << "LOCATION:\n";
396 os << oat_file_.GetLocation() << "\n\n";
397
Brian Carlstromaded5f72011-10-07 17:15:04 -0700398 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800399 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700400
Elliott Hughesa72ec822012-03-05 17:12:22 -0800401 os << "INSTRUCTION SET:\n";
402 os << oat_header.GetInstructionSet() << "\n\n";
403
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700404 {
405 std::unique_ptr<const InstructionSetFeatures> features(
406 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
407 oat_header.GetInstructionSetFeaturesBitmap()));
408 os << "INSTRUCTION SET FEATURES:\n";
409 os << features->GetFeatureString() << "\n\n";
410 }
Dave Allison70202782013-10-22 17:52:19 -0700411
Brian Carlstromaded5f72011-10-07 17:15:04 -0700412 os << "DEX FILE COUNT:\n";
413 os << oat_header.GetDexFileCount() << "\n\n";
414
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800415#define DUMP_OAT_HEADER_OFFSET(label, offset) \
416 os << label " OFFSET:\n"; \
417 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800418 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800419 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
420 } \
421 os << StringPrintf("\n\n");
422
423 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
424 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
425 GetInterpreterToInterpreterBridgeOffset);
426 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
427 GetInterpreterToCompiledCodeBridgeOffset);
428 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
429 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800430 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
431 GetQuickGenericJniTrampolineOffset);
432 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
433 GetQuickImtConflictTrampolineOffset);
434 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
435 GetQuickResolutionTrampolineOffset);
436 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
437 GetQuickToInterpreterBridgeOffset);
438#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700439
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700440 os << "IMAGE PATCH DELTA:\n";
441 os << StringPrintf("%d (0x%08x)\n\n",
442 oat_header.GetImagePatchDelta(),
443 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700444
Brian Carlstrom28db0122012-10-18 16:20:41 -0700445 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
446 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
447
448 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800449 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700450
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700451 // Print the key-value store.
452 {
453 os << "KEY VALUE STORE:\n";
454 size_t index = 0;
455 const char* key;
456 const char* value;
457 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
458 os << key << " = " << value << "\n";
459 index++;
460 }
461 os << "\n";
462 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700463
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800464 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700465 os << "BEGIN:\n";
466 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700467
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700468 os << "END:\n";
469 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
470 }
471
472 os << "SIZE:\n";
473 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700474
475 os << std::flush;
476
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800477 // If set, adjust relative address to be searched
478 if (options_.addr2instr_ != 0) {
479 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
480 os << "SEARCH ADDRESS (executable offset + input):\n";
481 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
482 }
483
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700484 // Dumping the dex file overview is compact enough to do even if header only.
485 DexFileData cumulative;
486 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
487 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
488 CHECK(oat_dex_file != nullptr);
489 std::string error_msg;
490 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
491 if (dex_file == nullptr) {
492 os << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation() << "': "
493 << error_msg;
494 continue;
495 }
496 DexFileData data(*dex_file);
497 os << "Dex file data for " << dex_file->GetLocation() << "\n";
498 data.Dump(os);
499 os << "\n";
500 cumulative.Add(data);
501 }
502 os << "Cumulative dex file data\n";
503 cumulative.Dump(os);
504 os << "\n";
505
David Brazdilc03d7b62016-03-02 12:18:03 +0000506 if (!options_.dump_header_only_) {
Nicolas Geoffraye70dd562016-10-30 21:03:35 +0000507 VariableIndentationOutputStream vios(&os);
508 VdexFile::Header vdex_header = oat_file_.GetVdexFile()->GetHeader();
509 if (vdex_header.IsValid()) {
510 std::string error_msg;
511 std::vector<const DexFile*> dex_files;
512 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
513 const DexFile* dex_file = OpenDexFile(oat_dex_files_[i], &error_msg);
514 if (dex_file == nullptr) {
515 os << "Error opening dex file: " << error_msg << std::endl;
516 return false;
517 }
518 dex_files.push_back(dex_file);
519 }
520 verifier::VerifierDeps deps(dex_files, oat_file_.GetVdexFile()->GetVerifierDepsData());
521 deps.Dump(&vios);
522 } else {
523 os << "UNRECOGNIZED vdex file, magic "
524 << vdex_header.GetMagic()
525 << ", version "
526 << vdex_header.GetVersion()
527 << "\n";
528 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000529 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
530 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
531 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800532
David Brazdilc03d7b62016-03-02 12:18:03 +0000533 // If file export selected skip file analysis
534 if (options_.export_dex_location_) {
535 if (!ExportDexFile(os, *oat_dex_file)) {
536 success = false;
537 }
538 } else {
539 if (!DumpOatDexFile(os, *oat_dex_file)) {
540 success = false;
541 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800542 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700543 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700544 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000545
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800546 {
547 os << "OAT FILE STATS:\n";
548 VariableIndentationOutputStream vios(&os);
549 stats_.Dump(vios);
550 }
551
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700552 os << std::flush;
553 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700554 }
555
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800556 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700557 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
558 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800559 return 0; // Address not in oat file
560 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800561 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
562 reinterpret_cast<uintptr_t>(oat_file_.Begin());
563 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800564 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800565 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800566 return end_offset - begin_offset;
567 }
568
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800569 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700570 return oat_file_.GetOatHeader().GetInstructionSet();
571 }
572
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700573 const void* GetQuickOatCode(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800574 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
575 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700576 CHECK(oat_dex_file != nullptr);
577 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700578 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
579 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700580 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
581 << "': " << error_msg;
582 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800583 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700584 const DexFile::ClassDef* class_def =
David Sehr9aa352e2016-09-15 18:13:52 -0700585 OatDexFile::FindClassDef(*dex_file, descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700586 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700587 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100588 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800589 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100590 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800591 }
592 }
593 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700594 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800595 }
596
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800597 struct Stats {
598 enum ByteKind {
599 kByteKindCode,
600 kByteKindQuickMethodHeader,
601 kByteKindCodeInfoLocationCatalog,
602 kByteKindCodeInfoDexRegisterMap,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800603 kByteKindCodeInfoEncoding,
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800604 kByteKindCodeInfoInvokeInfo,
David Srbecky45aa5982016-03-18 02:15:09 +0000605 kByteKindCodeInfoStackMasks,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800606 kByteKindCodeInfoRegisterMasks,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800607 kByteKindStackMapNativePc,
608 kByteKindStackMapDexPc,
609 kByteKindStackMapDexRegisterMap,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800610 kByteKindStackMapInlineInfoIndex,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800611 kByteKindStackMapRegisterMaskIndex,
David Srbecky45aa5982016-03-18 02:15:09 +0000612 kByteKindStackMapStackMaskIndex,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700613 kByteKindInlineInfoMethodIndexIdx,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800614 kByteKindInlineInfoDexPc,
615 kByteKindInlineInfoExtraData,
616 kByteKindInlineInfoDexRegisterMap,
617 kByteKindInlineInfoIsLast,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800618 kByteKindCount,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800619 // Special ranges for std::accumulate convenience.
620 kByteKindStackMapFirst = kByteKindStackMapNativePc,
David Srbecky45aa5982016-03-18 02:15:09 +0000621 kByteKindStackMapLast = kByteKindStackMapStackMaskIndex,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700622 kByteKindInlineInfoFirst = kByteKindInlineInfoMethodIndexIdx,
Mathieu Chartier1e083792017-02-08 13:30:04 -0800623 kByteKindInlineInfoLast = kByteKindInlineInfoIsLast,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800624 };
625 int64_t bits[kByteKindCount] = {};
626 // Since code has deduplication, seen tracks already seen pointers to avoid double counting
627 // deduplicated code and tables.
628 std::unordered_set<const void*> seen;
629
630 // Returns true if it was newly added.
631 bool AddBitsIfUnique(ByteKind kind, int64_t count, const void* address) {
632 if (seen.insert(address).second == true) {
633 // True means the address was not already in the set.
634 AddBits(kind, count);
635 return true;
636 }
637 return false;
638 }
639
640 void AddBits(ByteKind kind, int64_t count) {
641 bits[kind] += count;
642 }
643
644 void Dump(VariableIndentationOutputStream& os) {
645 const int64_t sum = std::accumulate(bits, bits + kByteKindCount, 0u);
646 os.Stream() << "Dumping cumulative use of " << sum / kBitsPerByte << " accounted bytes\n";
647 if (sum > 0) {
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800648 Dump(os, "Code ", bits[kByteKindCode], sum);
649 Dump(os, "QuickMethodHeader ", bits[kByteKindQuickMethodHeader], sum);
650 Dump(os, "CodeInfoEncoding ", bits[kByteKindCodeInfoEncoding], sum);
651 Dump(os, "CodeInfoLocationCatalog ", bits[kByteKindCodeInfoLocationCatalog], sum);
652 Dump(os, "CodeInfoDexRegisterMap ", bits[kByteKindCodeInfoDexRegisterMap], sum);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800653 Dump(os, "CodeInfoStackMasks ", bits[kByteKindCodeInfoStackMasks], sum);
654 Dump(os, "CodeInfoRegisterMasks ", bits[kByteKindCodeInfoRegisterMasks], sum);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800655 Dump(os, "CodeInfoInvokeInfo ", bits[kByteKindCodeInfoInvokeInfo], sum);
Mathieu Chartier1e083792017-02-08 13:30:04 -0800656 // Stack map section.
657 const int64_t stack_map_bits = std::accumulate(bits + kByteKindStackMapFirst,
658 bits + kByteKindStackMapLast + 1,
659 0u);
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800660 Dump(os, "CodeInfoStackMap ", stack_map_bits, sum);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800661 {
662 ScopedIndentation indent1(&os);
663 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800664 "StackMapNativePc ",
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800665 bits[kByteKindStackMapNativePc],
666 stack_map_bits,
667 "stack map");
668 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800669 "StackMapDexPcEncoding ",
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800670 bits[kByteKindStackMapDexPc],
671 stack_map_bits,
672 "stack map");
673 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800674 "StackMapDexRegisterMap ",
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800675 bits[kByteKindStackMapDexRegisterMap],
676 stack_map_bits,
677 "stack map");
678 Dump(os,
Mathieu Chartier697dc662017-02-08 16:56:48 -0800679 "StackMapInlineInfoIndex ",
Mathieu Chartier1e083792017-02-08 13:30:04 -0800680 bits[kByteKindStackMapInlineInfoIndex],
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800681 stack_map_bits,
682 "stack map");
683 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800684 "StackMapRegisterMaskIndex ",
685 bits[kByteKindStackMapRegisterMaskIndex],
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800686 stack_map_bits,
687 "stack map");
688 Dump(os,
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800689 "StackMapStackMaskIndex ",
David Srbecky45aa5982016-03-18 02:15:09 +0000690 bits[kByteKindStackMapStackMaskIndex],
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800691 stack_map_bits,
692 "stack map");
693 }
Mathieu Chartier1e083792017-02-08 13:30:04 -0800694 // Inline info section.
695 const int64_t inline_info_bits = std::accumulate(bits + kByteKindInlineInfoFirst,
696 bits + kByteKindInlineInfoLast + 1,
697 0u);
698 Dump(os, "CodeInfoInlineInfo ", inline_info_bits, sum);
699 {
700 ScopedIndentation indent1(&os);
701 Dump(os,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700702 "InlineInfoMethodIndexIdx ",
703 bits[kByteKindInlineInfoMethodIndexIdx],
Mathieu Chartier1e083792017-02-08 13:30:04 -0800704 inline_info_bits,
705 "inline info");
706 Dump(os,
707 "InlineInfoDexPc ",
708 bits[kByteKindStackMapDexPc],
709 inline_info_bits,
710 "inline info");
711 Dump(os,
712 "InlineInfoExtraData ",
713 bits[kByteKindInlineInfoExtraData],
714 inline_info_bits,
715 "inline info");
716 Dump(os,
717 "InlineInfoDexRegisterMap ",
718 bits[kByteKindInlineInfoDexRegisterMap],
719 inline_info_bits,
720 "inline info");
721 Dump(os,
722 "InlineInfoIsLast ",
723 bits[kByteKindInlineInfoIsLast],
724 inline_info_bits,
725 "inline info");
726 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -0800727 }
728 os.Stream() << "\n" << std::flush;
729 }
730
731 private:
732 void Dump(VariableIndentationOutputStream& os,
733 const char* name,
734 int64_t size,
735 int64_t total,
736 const char* sum_of = "total") {
737 const double percent = (static_cast<double>(size) / static_cast<double>(total)) * 100;
738 os.Stream() << StringPrintf("%s = %8" PRId64 " (%2.0f%% of %s)\n",
739 name,
740 size / kBitsPerByte,
741 percent,
742 sum_of);
743 }
744 };
745
Brian Carlstromaded5f72011-10-07 17:15:04 -0700746 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800747 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800748 // We don't know the length of the code for each method, but we need to know where to stop
749 // when disassembling. What we do know is that a region of code will be followed by some other
750 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
751 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800752 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
753 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700754 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700755 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700756 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
757 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700758 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
759 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800760 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800761 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800762 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800763 for (size_t class_def_index = 0;
764 class_def_index < dex_file->NumClassDefs();
765 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800766 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100767 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700768 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700769 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800770 ClassDataItemIterator it(*dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -0700771 it.SkipAllFields();
Elliott Hughese3c845c2012-02-28 17:23:01 -0800772 uint32_t class_method_index = 0;
773 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100774 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800775 it.Next();
776 }
777 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100778 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800779 it.Next();
780 }
781 }
782 }
783 }
784
785 // If the last thing in the file is code for a method, there won't be an offset for the "next"
786 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
787 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800788 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800789 }
790
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700791 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
792 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
793 }
794
Elliott Hughese3c845c2012-02-28 17:23:01 -0800795 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800796 uint32_t code_offset = oat_method.GetCodeOffset();
797 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
798 code_offset &= ~0x1;
799 }
800 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800801 offsets_.insert(oat_method.GetVmapTableOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800802 }
803
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700804 // Dex file data, may be for multiple different dex files.
805 class DexFileData {
806 public:
807 DexFileData() {}
808
809 explicit DexFileData(const DexFile& dex_file)
810 : num_string_ids_(dex_file.NumStringIds()),
811 num_method_ids_(dex_file.NumMethodIds()),
812 num_field_ids_(dex_file.NumFieldIds()),
813 num_type_ids_(dex_file.NumTypeIds()),
814 num_class_defs_(dex_file.NumClassDefs()) {
815 for (size_t class_def_index = 0; class_def_index < num_class_defs_; ++class_def_index) {
816 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
817 WalkClass(dex_file, class_def);
818 }
819 }
820
821 void Add(const DexFileData& other) {
822 AddAll(unique_string_ids_from_code_, other.unique_string_ids_from_code_);
823 num_string_ids_from_code_ += other.num_string_ids_from_code_;
824 AddAll(dex_code_item_ptrs_, other.dex_code_item_ptrs_);
825 dex_code_bytes_ += other.dex_code_bytes_;
826 num_string_ids_ += other.num_string_ids_;
827 num_method_ids_ += other.num_method_ids_;
828 num_field_ids_ += other.num_field_ids_;
829 num_type_ids_ += other.num_type_ids_;
830 num_class_defs_ += other.num_class_defs_;
831 }
832
833 void Dump(std::ostream& os) {
834 os << "Num string ids: " << num_string_ids_ << "\n";
835 os << "Num method ids: " << num_method_ids_ << "\n";
836 os << "Num field ids: " << num_field_ids_ << "\n";
837 os << "Num type ids: " << num_type_ids_ << "\n";
838 os << "Num class defs: " << num_class_defs_ << "\n";
839 os << "Unique strings loaded from dex code: " << unique_string_ids_from_code_.size() << "\n";
840 os << "Total strings loaded from dex code: " << num_string_ids_from_code_ << "\n";
841 os << "Number of unique dex code items: " << dex_code_item_ptrs_.size() << "\n";
842 os << "Total number of dex code bytes: " << dex_code_bytes_ << "\n";
843 }
844
845 private:
Andreas Gampe9186ced2016-12-12 14:28:21 -0800846 // All of the elements from one container to another.
847 template <typename Dest, typename Src>
848 static void AddAll(Dest& dest, const Src& src) {
849 dest.insert(src.begin(), src.end());
850 }
851
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700852 void WalkClass(const DexFile& dex_file, const DexFile::ClassDef& class_def) {
853 const uint8_t* class_data = dex_file.GetClassData(class_def);
854 if (class_data == nullptr) { // empty class such as a marker interface?
855 return;
856 }
857 ClassDataItemIterator it(dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -0700858 it.SkipAllFields();
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700859 while (it.HasNextDirectMethod()) {
860 WalkCodeItem(dex_file, it.GetMethodCodeItem());
861 it.Next();
862 }
863 while (it.HasNextVirtualMethod()) {
864 WalkCodeItem(dex_file, it.GetMethodCodeItem());
865 it.Next();
866 }
867 DCHECK(!it.HasNext());
868 }
869
870 void WalkCodeItem(const DexFile& dex_file, const DexFile::CodeItem* code_item) {
871 if (code_item == nullptr) {
872 return;
873 }
874 const size_t code_item_size = code_item->insns_size_in_code_units_;
875 const uint16_t* code_ptr = code_item->insns_;
876 const uint16_t* code_end = code_item->insns_ + code_item_size;
877
878 // If we inserted a new dex code item pointer, add to total code bytes.
879 if (dex_code_item_ptrs_.insert(code_ptr).second) {
880 dex_code_bytes_ += code_item_size * sizeof(code_ptr[0]);
881 }
882
883 while (code_ptr < code_end) {
884 const Instruction* inst = Instruction::At(code_ptr);
885 switch (inst->Opcode()) {
886 case Instruction::CONST_STRING: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800887 const dex::StringIndex string_index(inst->VRegB_21c());
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700888 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
889 ++num_string_ids_from_code_;
890 break;
891 }
892 case Instruction::CONST_STRING_JUMBO: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800893 const dex::StringIndex string_index(inst->VRegB_31c());
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700894 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
895 ++num_string_ids_from_code_;
896 break;
897 }
898 default:
899 break;
900 }
901
902 code_ptr += inst->SizeInCodeUnits();
903 }
904 }
905
906 // Unique string ids loaded from dex code.
907 std::set<StringReference, StringReferenceComparator> unique_string_ids_from_code_;
908
909 // Total string ids loaded from dex code.
910 size_t num_string_ids_from_code_ = 0;
911
912 // Unique code pointers.
913 std::set<const void*> dex_code_item_ptrs_;
914
915 // Total "unique" dex code bytes.
916 size_t dex_code_bytes_ = 0;
917
918 // Other dex ids.
919 size_t num_string_ids_ = 0;
920 size_t num_method_ids_ = 0;
921 size_t num_field_ids_ = 0;
922 size_t num_type_ids_ = 0;
923 size_t num_class_defs_ = 0;
924 };
925
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700926 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
927 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800928 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700929 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800930 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800931 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700932
Andreas Gampe2ba88952016-04-29 17:52:07 -0700933 const uint8_t* const oat_file_begin = oat_dex_file.GetOatFile()->Begin();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100934 const uint8_t* const vdex_file_begin = oat_dex_file.GetOatFile()->DexBegin();
935
936 // Print data range of the dex file embedded inside the corresponding vdex file.
Andreas Gampe2ba88952016-04-29 17:52:07 -0700937 const uint8_t* const dex_file_pointer = oat_dex_file.GetDexFilePointer();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100938 uint32_t dex_offset = dchecked_integral_cast<uint32_t>(dex_file_pointer - vdex_file_begin);
Andreas Gampe2ba88952016-04-29 17:52:07 -0700939 os << StringPrintf("dex-file: 0x%08x..0x%08x\n",
940 dex_offset,
941 dchecked_integral_cast<uint32_t>(dex_offset + oat_dex_file.FileSize() - 1));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700942
Andreas Gampe2ba88952016-04-29 17:52:07 -0700943 // Create the dex file early. A lot of print-out things depend on it.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700944 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700945 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
946 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700947 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700948 os << std::flush;
949 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700950 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100951
Andreas Gampe2ba88952016-04-29 17:52:07 -0700952 // Print lookup table, if it exists.
953 if (oat_dex_file.GetLookupTableData() != nullptr) {
954 uint32_t table_offset = dchecked_integral_cast<uint32_t>(
955 oat_dex_file.GetLookupTableData() - oat_file_begin);
David Sehr9aa352e2016-09-15 18:13:52 -0700956 uint32_t table_size = TypeLookupTable::RawDataLength(dex_file->NumClassDefs());
Andreas Gampe2ba88952016-04-29 17:52:07 -0700957 os << StringPrintf("type-table: 0x%08x..0x%08x\n",
958 table_offset,
959 table_offset + table_size - 1);
960 }
961
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100962 VariableIndentationOutputStream vios(&os);
963 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800964 for (size_t class_def_index = 0;
965 class_def_index < dex_file->NumClassDefs();
966 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700967 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
968 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800969
970 // TODO: Support regex
971 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
972 continue;
973 }
974
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700975 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100976 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700977 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
Andreas Gampea5b09a62016-11-17 15:21:22 -0800978 class_def_index, descriptor, oat_class_offset, class_def.class_idx_.index_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100979 << " (" << oat_class.GetStatus() << ")"
980 << " (" << oat_class.GetType() << ")\n";
981 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700982 if (options_.list_classes_) {
983 continue;
984 }
985 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700986 success = false;
987 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800988 if (stop_analysis) {
989 os << std::flush;
990 return success;
991 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700992 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700993 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700994 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700995 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700996 }
997
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800998 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
999 std::string error_msg;
1000 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
1001
Mathieu Chartierac8f4392015-08-27 13:54:20 -07001002 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001003 if (dex_file == nullptr) {
1004 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
1005 return false;
1006 }
1007 size_t fsize = oat_dex_file.FileSize();
1008
1009 // Some quick checks just in case
1010 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
1011 os << "Invalid dex file\n";
1012 return false;
1013 }
1014
1015 // Verify output directory exists
1016 if (!OS::DirectoryExists(options_.export_dex_location_)) {
1017 // TODO: Extend OS::DirectoryExists if symlink support is required
1018 os << options_.export_dex_location_ << " output directory not found or symlink\n";
1019 return false;
1020 }
1021
1022 // Beautify path names
1023 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
1024 return false;
1025 }
1026
1027 std::string dex_orig_name;
1028 size_t dex_orig_pos = dex_file_location.rfind('/');
1029 if (dex_orig_pos == std::string::npos)
1030 dex_orig_name = dex_file_location;
1031 else
1032 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
1033
1034 // A more elegant approach to efficiently name user installed apps is welcome
Andreas Gampef812d8c2017-02-17 10:19:44 -08001035 if (dex_orig_name.size() == 8 &&
1036 dex_orig_name.compare("base.apk") == 0 &&
1037 dex_orig_pos != std::string::npos) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001038 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
1039 size_t apk_orig_pos = dex_file_location.rfind('/');
1040 if (apk_orig_pos != std::string::npos) {
1041 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
1042 }
1043 }
1044
1045 std::string out_dex_path(options_.export_dex_location_);
1046 if (out_dex_path.back() != '/') {
1047 out_dex_path.append("/");
1048 }
1049 out_dex_path.append(dex_orig_name);
1050 out_dex_path.append("_export.dex");
1051 if (out_dex_path.length() > PATH_MAX) {
1052 return false;
1053 }
1054
1055 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
1056 if (file.get() == nullptr) {
1057 os << "Failed to open output dex file " << out_dex_path;
1058 return false;
1059 }
1060
1061 if (!file->WriteFully(dex_file->Begin(), fsize)) {
1062 os << "Failed to write dex file";
1063 file->Erase();
1064 return false;
1065 }
1066
1067 if (file->FlushCloseOrErase() != 0) {
1068 os << "Flush and close failed";
1069 return false;
1070 }
1071
1072 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
1073 os << std::flush;
1074
1075 return true;
1076 }
1077
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001078 bool DumpOatClass(VariableIndentationOutputStream* vios,
1079 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001080 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001081 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001082 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -07001083 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001084 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001085 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001086 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -08001087 }
1088 ClassDataItemIterator it(dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -07001089 it.SkipAllFields();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001090 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001091 while (it.HasNextDirectMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001092 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001093 it.GetMemberIndex(), it.GetMethodCodeItem(),
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001094 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001095 success = false;
1096 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001097 if (addr_found) {
1098 *stop_analysis = true;
1099 return success;
1100 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001101 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -07001102 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001103 }
Ian Rogers0571d352011-11-03 19:51:38 -07001104 while (it.HasNextVirtualMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001105 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001106 it.GetMemberIndex(), it.GetMethodCodeItem(),
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001107 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001108 success = false;
1109 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001110 if (addr_found) {
1111 *stop_analysis = true;
1112 return success;
1113 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001114 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -07001115 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001116 }
Ian Rogers0571d352011-11-03 19:51:38 -07001117 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001118 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001119 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001120 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001121
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001122 static constexpr uint32_t kPrologueBytes = 16;
1123
1124 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
1125 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
1126
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001127 bool DumpOatMethod(VariableIndentationOutputStream* vios,
1128 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -07001129 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001130 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001131 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Mathieu Chartierdc00f182016-07-14 10:10:44 -07001132 uint32_t method_access_flags, bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001133 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001134
1135 // TODO: Support regex
1136 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
1137 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00001138 return success;
1139 }
1140
David Sehr709b0702016-10-13 09:12:37 -07001141 std::string pretty_method = dex_file.PrettyMethod(dex_method_idx, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001142 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
1143 class_method_index, pretty_method.c_str(),
1144 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001145 if (options_.list_methods_) return success;
1146
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001147 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
1148 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
1149 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
1150 uint32_t code_offset = oat_method.GetCodeOffset();
1151 uint32_t code_size = oat_method.GetQuickCodeSize();
1152 if (resolved_addr2instr_ != 0) {
1153 if (resolved_addr2instr_ > code_offset + code_size) {
1154 return success;
1155 } else {
1156 *addr_found = true; // stop analyzing file at next iteration
1157 }
1158 }
1159
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001160 // Everything below is indented at least once.
1161 ScopedIndentation indent1(vios);
1162
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001163 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001164 vios->Stream() << "DEX CODE:\n";
1165 ScopedIndentation indent2(vios);
1166 DumpDexCode(vios->Stream(), dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -07001167 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001168
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001169 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001170 std::unique_ptr<verifier::MethodVerifier> verifier;
1171 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001172 // We need to have the handle scope stay live until after the verifier since the verifier has
1173 // a handle to the dex cache from hs.
1174 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001175 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
1176 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001177 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001178 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001179 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -07001180 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001181 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001182 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001183 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001184 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001185 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001186 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001187 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001188 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001189 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
1190 oat_method_offsets_offset, oat_file_.Size());
1191 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001192 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001193 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001194 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001195
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001196 ScopedIndentation indent2(vios);
1197 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001198 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
1199 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001200 vios->Stream() << StringPrintf("WARNING: "
1201 "code offset 0x%08x is past end of file 0x%08zx.\n",
1202 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001203 success = false;
1204 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001205 vios->Stream() << "\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001206 }
1207 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001208 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001209 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
1210 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001211 stats_.AddBitsIfUnique(Stats::kByteKindQuickMethodHeader,
1212 sizeof(*method_header) * kBitsPerByte,
1213 method_header);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001214 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001215 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001216 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001217 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001218 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001219 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001220 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
1221 method_header_offset, oat_file_.Size());
1222 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001223 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001224 return false;
1225 }
1226
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001227 ScopedIndentation indent2(vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001228 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001229 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001230 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001231 }
Mingyao Yang063fc772016-08-02 11:02:54 -07001232 uint32_t vmap_table_offset = method_header ==
1233 nullptr ? 0 : method_header->GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001234 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001235
1236 size_t vmap_table_offset_limit =
1237 (kIsVdexEnabled && IsMethodGeneratedByDexToDexCompiler(oat_method, code_item))
1238 ? oat_file_.GetVdexFile()->Size()
1239 : method_header->GetCode() - oat_file_.Begin();
1240 if (vmap_table_offset >= vmap_table_offset_limit) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001241 vios->Stream() << StringPrintf("WARNING: "
1242 "vmap table offset 0x%08x is past end of file 0x%08zx. "
1243 "vmap table offset was loaded from offset 0x%08x.\n",
Nicolas Geoffray4acefd32016-10-24 13:14:58 +01001244 vmap_table_offset,
1245 vmap_table_offset_limit,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001246 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001247 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001248 } else if (options_.dump_vmap_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001249 DumpVmapData(vios, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001250 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001251 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001252 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001253 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001254
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001255 ScopedIndentation indent2(vios);
1256 vios->Stream()
1257 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
1258 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
1259 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
1260 vios->Stream() << "\n";
1261 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
1262 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
1263 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001264 }
1265 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001266 // Based on spill masks from QuickMethodFrameInfo so placed
1267 // after it is dumped, but useful for understanding quick
1268 // code, so dumped here.
1269 ScopedIndentation indent2(vios);
1270 DumpVregLocations(vios->Stream(), oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001271 }
1272 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001273 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001274 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
1275 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001276 ScopedIndentation indent2(vios);
1277 vios->Stream() << StringPrintf("WARNING: "
1278 "code size offset 0x%08x is past end of file 0x%08zx.",
1279 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001280 success = false;
1281 } else {
1282 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001283 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1284 uint64_t aligned_code_end = aligned_code_begin + code_size;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001285 stats_.AddBitsIfUnique(Stats::kByteKindCode, code_size * kBitsPerByte, code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001286
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001287 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001288 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001289 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001290 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
1291 code_offset,
1292 code_size_offset,
1293 code_size,
1294 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001295
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001296 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001297 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001298 vios->Stream() << StringPrintf("WARNING: "
1299 "start of code at 0x%08x is past end of file 0x%08zx.",
1300 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001301 success = false;
1302 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001303 vios->Stream() << StringPrintf(
1304 "WARNING: "
1305 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
1306 "code size is 0x%08x loaded from offset 0x%08x.\n",
1307 aligned_code_end, oat_file_.Size(),
1308 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001309 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001310 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001311 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001312 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001313 }
1314 }
1315 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001316 vios->Stream() << StringPrintf(
1317 "WARNING: "
1318 "code size %d is bigger than max expected threshold of %d. "
1319 "code size is 0x%08x loaded from offset 0x%08x.\n",
1320 code_size, kMaxCodeSize,
1321 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001322 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001323 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001324 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001325 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001326 }
1327 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001328 } else if (options_.disassemble_code_) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001329 DumpCode(vios, oat_method, code_item, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001330 }
1331 }
1332 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001333 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001334 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001335 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001336
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001337 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1338 if (spill_mask == 0) {
1339 return;
1340 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001341 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001342 for (size_t i = 0; i < 32; i++) {
1343 if ((spill_mask & (1 << i)) != 0) {
1344 if (is_float) {
1345 os << "fr" << i;
1346 } else {
1347 os << "r" << i;
1348 }
1349 spill_mask ^= 1 << i; // clear bit
1350 if (spill_mask != 0) {
1351 os << ", ";
1352 } else {
1353 break;
1354 }
1355 }
1356 }
1357 os << ")";
1358 }
1359
Roland Levillain442b46a2015-02-18 16:54:21 +00001360 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001361 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001362 const OatFile::OatMethod& oat_method,
1363 const DexFile::CodeItem* code_item) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001364 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1365 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001366 const void* raw_code_info = oat_method.GetVmapTable();
1367 if (raw_code_info != nullptr) {
1368 CodeInfo code_info(raw_code_info);
1369 DCHECK(code_item != nullptr);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001370 ScopedIndentation indent1(vios);
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001371 MethodInfo method_info = oat_method.GetOatQuickMethodHeader()->GetOptimizedMethodInfo();
1372 DumpCodeInfo(vios, code_info, oat_method, *code_item, method_info);
Roland Levillain442b46a2015-02-18 16:54:21 +00001373 }
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001374 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
1375 // We don't encode the size in the table, so just emit that we have quickened
1376 // information.
1377 ScopedIndentation indent(vios);
1378 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001379 } else {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001380 // Otherwise, there is nothing to display.
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001381 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001382 }
1383
1384 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001385 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001386 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001387 const OatFile::OatMethod& oat_method,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001388 const DexFile::CodeItem& code_item,
1389 const MethodInfo& method_info) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001390 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001391 oat_method.GetCodeOffset(),
1392 code_item.registers_size_,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001393 options_.dump_code_info_stack_maps_,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001394 instruction_set_,
1395 method_info);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001396 }
1397
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001398 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1399 const DexFile::CodeItem* code_item) {
1400 if (code_item != nullptr) {
1401 size_t num_locals_ins = code_item->registers_size_;
1402 size_t num_ins = code_item->ins_size_;
1403 size_t num_locals = num_locals_ins - num_ins;
1404 size_t num_outs = code_item->outs_size_;
1405
1406 os << "vr_stack_locations:";
1407 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1408 // For readability, delimit the different kinds of VRs.
1409 if (reg == num_locals_ins) {
1410 os << "\n\tmethod*:";
1411 } else if (reg == num_locals && num_ins > 0) {
1412 os << "\n\tins:";
1413 } else if (reg == 0 && num_locals > 0) {
1414 os << "\n\tlocals:";
1415 }
1416
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001417 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1418 code_item,
1419 oat_method.GetCoreSpillMask(),
1420 oat_method.GetFpSpillMask(),
1421 oat_method.GetFrameSizeInBytes(),
1422 reg,
1423 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001424 os << " v" << reg << "[sp + #" << offset << "]";
1425 }
1426
1427 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1428 if (out_reg == 0) {
1429 os << "\n\touts:";
1430 }
1431
1432 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1433 os << " v" << out_reg << "[sp + #" << offset << "]";
1434 }
1435
1436 os << "\n";
1437 }
1438 }
1439
Ian Rogersb23a7722012-10-09 16:54:26 -07001440 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001441 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001442 size_t i = 0;
1443 while (i < code_item->insns_size_in_code_units_) {
1444 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001445 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1446 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001447 i += instruction->SizeInCodeUnits();
1448 }
1449 }
1450 }
1451
Roland Levillainf2650d12015-05-28 14:53:28 +01001452 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1453 // the optimizing compiler?
1454 static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1455 const DexFile::CodeItem* code_item) {
1456 // If the native GC map is null and the Dex `code_item` is not
1457 // null, then this method has been compiled with the optimizing
1458 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001459 return oat_method.GetQuickCode() != nullptr &&
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001460 oat_method.GetVmapTable() != nullptr &&
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001461 code_item != nullptr;
1462 }
1463
1464 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1465 // the dextodex compiler?
1466 static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
1467 const DexFile::CodeItem* code_item) {
1468 // If the quick code is null, the Dex `code_item` is not
1469 // null, and the vmap table is not null, then this method has been compiled
1470 // with the dextodex compiler.
1471 return oat_method.GetQuickCode() == nullptr &&
1472 oat_method.GetVmapTable() != nullptr &&
1473 code_item != nullptr;
Roland Levillainf2650d12015-05-28 14:53:28 +01001474 }
1475
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001476 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001477 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001478 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001479 const DexFile* dex_file,
1480 const DexFile::ClassDef& class_def,
1481 const DexFile::CodeItem* code_item,
1482 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001483 if ((method_access_flags & kAccNative) == 0) {
1484 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001485 Runtime* const runtime = Runtime::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001486 Handle<mirror::DexCache> dex_cache(
Mathieu Chartierf284d442016-06-02 11:48:30 -07001487 hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file, nullptr)));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001488 CHECK(dex_cache != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001489 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001490 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001491 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
David Brazdil15fc7292016-09-02 14:13:18 +01001492 class_def, code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001493 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001494
1495 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001496 }
1497
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001498 // The StackMapsHelper provides the stack maps in the native PC order.
1499 // For identical native PCs, the order from the CodeInfo is preserved.
1500 class StackMapsHelper {
1501 public:
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001502 explicit StackMapsHelper(const uint8_t* raw_code_info, InstructionSet instruction_set)
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001503 : code_info_(raw_code_info),
1504 encoding_(code_info_.ExtractEncoding()),
1505 number_of_stack_maps_(code_info_.GetNumberOfStackMaps(encoding_)),
1506 indexes_(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001507 offset_(static_cast<uint32_t>(-1)),
1508 stack_map_index_(0u),
1509 instruction_set_(instruction_set) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001510 if (number_of_stack_maps_ != 0u) {
1511 // Check if native PCs are ordered.
1512 bool ordered = true;
1513 StackMap last = code_info_.GetStackMapAt(0u, encoding_);
1514 for (size_t i = 1; i != number_of_stack_maps_; ++i) {
1515 StackMap current = code_info_.GetStackMapAt(i, encoding_);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001516 if (last.GetNativePcOffset(encoding_.stack_map.encoding, instruction_set) >
1517 current.GetNativePcOffset(encoding_.stack_map.encoding, instruction_set)) {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001518 ordered = false;
1519 break;
1520 }
1521 last = current;
1522 }
1523 if (!ordered) {
1524 // Create indirection indexes for access in native PC order. We do not optimize
1525 // for the fact that there can currently be only two separately ordered ranges,
1526 // namely normal stack maps and catch-point stack maps.
1527 indexes_.resize(number_of_stack_maps_);
1528 std::iota(indexes_.begin(), indexes_.end(), 0u);
1529 std::sort(indexes_.begin(),
1530 indexes_.end(),
1531 [this](size_t lhs, size_t rhs) {
1532 StackMap left = code_info_.GetStackMapAt(lhs, encoding_);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001533 uint32_t left_pc = left.GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001534 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001535 StackMap right = code_info_.GetStackMapAt(rhs, encoding_);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001536 uint32_t right_pc = right.GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001537 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001538 // If the PCs are the same, compare indexes to preserve the original order.
1539 return (left_pc < right_pc) || (left_pc == right_pc && lhs < rhs);
1540 });
1541 }
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001542 offset_ = GetStackMapAt(0).GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001543 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001544 }
1545 }
1546
1547 const CodeInfo& GetCodeInfo() const {
1548 return code_info_;
1549 }
1550
1551 const CodeInfoEncoding& GetEncoding() const {
1552 return encoding_;
1553 }
1554
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001555 uint32_t GetOffset() const {
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001556 return offset_;
1557 }
1558
1559 StackMap GetStackMap() const {
1560 return GetStackMapAt(stack_map_index_);
1561 }
1562
1563 void Next() {
1564 ++stack_map_index_;
1565 offset_ = (stack_map_index_ == number_of_stack_maps_)
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001566 ? static_cast<uint32_t>(-1)
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001567 : GetStackMapAt(stack_map_index_).GetNativePcOffset(encoding_.stack_map.encoding,
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001568 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001569 }
1570
1571 private:
1572 StackMap GetStackMapAt(size_t i) const {
1573 if (!indexes_.empty()) {
1574 i = indexes_[i];
1575 }
1576 DCHECK_LT(i, number_of_stack_maps_);
1577 return code_info_.GetStackMapAt(i, encoding_);
1578 }
1579
1580 const CodeInfo code_info_;
1581 const CodeInfoEncoding encoding_;
1582 const size_t number_of_stack_maps_;
1583 dchecked_vector<size_t> indexes_; // Used if stack map native PCs are not ordered.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001584 uint32_t offset_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001585 size_t stack_map_index_;
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001586 const InstructionSet instruction_set_;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001587 };
1588
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001589 void DumpCode(VariableIndentationOutputStream* vios,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001590 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1591 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001592 const void* quick_code = oat_method.GetQuickCode();
1593
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001594 if (code_size == 0) {
1595 code_size = oat_method.GetQuickCodeSize();
1596 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001597 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001598 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001599 return;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001600 } else if (!bad_input && IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1601 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001602 StackMapsHelper helper(oat_method.GetVmapTable(), instruction_set_);
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001603 MethodInfo method_info(oat_method.GetOatQuickMethodHeader()->GetOptimizedMethodInfo());
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001604 {
1605 CodeInfoEncoding encoding(helper.GetEncoding());
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001606 StackMapEncoding stack_map_encoding(encoding.stack_map.encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001607 const size_t num_stack_maps = encoding.stack_map.num_entries;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001608 if (stats_.AddBitsIfUnique(Stats::kByteKindCodeInfoEncoding,
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001609 encoding.HeaderSize() * kBitsPerByte,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001610 oat_method.GetVmapTable())) {
Mathieu Chartier1e083792017-02-08 13:30:04 -08001611 // Stack maps
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001612 stats_.AddBits(
1613 Stats::kByteKindStackMapNativePc,
1614 stack_map_encoding.GetNativePcEncoding().BitSize() * num_stack_maps);
1615 stats_.AddBits(
1616 Stats::kByteKindStackMapDexPc,
1617 stack_map_encoding.GetDexPcEncoding().BitSize() * num_stack_maps);
1618 stats_.AddBits(
1619 Stats::kByteKindStackMapDexRegisterMap,
1620 stack_map_encoding.GetDexRegisterMapEncoding().BitSize() * num_stack_maps);
1621 stats_.AddBits(
Mathieu Chartier1e083792017-02-08 13:30:04 -08001622 Stats::kByteKindStackMapInlineInfoIndex,
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001623 stack_map_encoding.GetInlineInfoEncoding().BitSize() * num_stack_maps);
1624 stats_.AddBits(
Mathieu Chartier1a20b682017-01-31 14:25:16 -08001625 Stats::kByteKindStackMapRegisterMaskIndex,
1626 stack_map_encoding.GetRegisterMaskIndexEncoding().BitSize() * num_stack_maps);
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001627 stats_.AddBits(
David Srbecky45aa5982016-03-18 02:15:09 +00001628 Stats::kByteKindStackMapStackMaskIndex,
1629 stack_map_encoding.GetStackMaskIndexEncoding().BitSize() * num_stack_maps);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001630
1631 // Stack masks
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001632 stats_.AddBits(
David Srbecky45aa5982016-03-18 02:15:09 +00001633 Stats::kByteKindCodeInfoStackMasks,
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001634 encoding.stack_mask.encoding.BitSize() * encoding.stack_mask.num_entries);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001635
1636 // Register masks
Mathieu Chartier1a20b682017-01-31 14:25:16 -08001637 stats_.AddBits(
1638 Stats::kByteKindCodeInfoRegisterMasks,
Mathieu Chartier575d3e62017-02-06 11:00:40 -08001639 encoding.register_mask.encoding.BitSize() * encoding.register_mask.num_entries);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001640
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001641 // Invoke infos
1642 if (encoding.invoke_info.num_entries > 0u) {
1643 stats_.AddBits(
1644 Stats::kByteKindCodeInfoInvokeInfo,
1645 encoding.invoke_info.encoding.BitSize() * encoding.invoke_info.num_entries);
1646 }
1647
Mathieu Chartier1e083792017-02-08 13:30:04 -08001648 // Location catalog
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001649 const size_t location_catalog_bytes =
1650 helper.GetCodeInfo().GetDexRegisterLocationCatalogSize(encoding);
1651 stats_.AddBits(Stats::kByteKindCodeInfoLocationCatalog,
1652 kBitsPerByte * location_catalog_bytes);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001653 // Dex register bytes.
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001654 const size_t dex_register_bytes =
1655 helper.GetCodeInfo().GetDexRegisterMapsSize(encoding, code_item->registers_size_);
1656 stats_.AddBits(
1657 Stats::kByteKindCodeInfoDexRegisterMap,
1658 kBitsPerByte * dex_register_bytes);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001659
1660 // Inline infos.
1661 const size_t num_inline_infos = encoding.inline_info.num_entries;
1662 if (num_inline_infos > 0u) {
1663 stats_.AddBits(
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001664 Stats::kByteKindInlineInfoMethodIndexIdx,
1665 encoding.inline_info.encoding.GetMethodIndexIdxEncoding().BitSize() *
1666 num_inline_infos);
Mathieu Chartier1e083792017-02-08 13:30:04 -08001667 stats_.AddBits(
1668 Stats::kByteKindInlineInfoDexPc,
1669 encoding.inline_info.encoding.GetDexPcEncoding().BitSize() * num_inline_infos);
1670 stats_.AddBits(
1671 Stats::kByteKindInlineInfoExtraData,
1672 encoding.inline_info.encoding.GetExtraDataEncoding().BitSize() * num_inline_infos);
1673 stats_.AddBits(
1674 Stats::kByteKindInlineInfoDexRegisterMap,
1675 encoding.inline_info.encoding.GetDexRegisterMapEncoding().BitSize() *
1676 num_inline_infos);
1677 stats_.AddBits(Stats::kByteKindInlineInfoIsLast, num_inline_infos);
1678 }
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001679 }
1680 }
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001681 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1682 size_t offset = 0;
1683 while (offset < code_size) {
1684 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
1685 if (offset == helper.GetOffset()) {
1686 ScopedIndentation indent1(vios);
1687 StackMap stack_map = helper.GetStackMap();
1688 DCHECK(stack_map.IsValid());
1689 stack_map.Dump(vios,
1690 helper.GetCodeInfo(),
1691 helper.GetEncoding(),
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001692 method_info,
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001693 oat_method.GetCodeOffset(),
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001694 code_item->registers_size_,
1695 instruction_set_);
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001696 do {
1697 helper.Next();
1698 // There may be multiple stack maps at a given PC. We display only the first one.
1699 } while (offset == helper.GetOffset());
1700 }
1701 DCHECK_LT(offset, helper.GetOffset());
1702 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001703 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001704 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1705 size_t offset = 0;
1706 while (offset < code_size) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001707 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001708 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001709 }
1710 }
1711
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001712 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001713 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001714 const OatDumperOptions& options_;
1715 uint32_t resolved_addr2instr_;
Andreas Gampe372f3a32016-08-19 10:49:06 -07001716 const InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001717 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001718 Disassembler* disassembler_;
Mathieu Chartier5e7c6a92017-01-17 16:38:30 -08001719 Stats stats_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001720};
1721
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001722class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001723 public:
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001724 ImageDumper(std::ostream* os,
1725 gc::space::ImageSpace& image_space,
1726 const ImageHeader& image_header,
1727 OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001728 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001729 vios_(os),
1730 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001731 image_space_(image_space),
1732 image_header_(image_header),
1733 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001734
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001735 bool Dump() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001736 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001737 std::ostream& indent_os = vios_.Stream();
1738
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001739 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001740
Jeff Haodcdc85b2015-12-04 14:06:18 -08001741 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1742
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001743 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001744
Mathieu Chartiere401d142015-04-22 13:56:20 -07001745 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1746
1747 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1748 auto section = static_cast<ImageHeader::ImageSections>(i);
1749 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1750 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001751
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001752 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001753
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001754 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001755
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001756 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1757
1758 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1759
1760 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001761
Alex Lighta59dd802014-07-02 16:28:08 -07001762 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1763
Igor Murashkin46774762014-10-22 11:37:02 -07001764 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1765
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001766 {
1767 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001768 static_assert(arraysize(image_roots_descriptions_) ==
1769 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001770 DCHECK_LE(image_header_.GetImageRoots()->GetLength(), ImageHeader::kImageRootsMax);
1771 for (int32_t i = 0, size = image_header_.GetImageRoots()->GetLength(); i != size; ++i) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001772 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1773 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001774 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001775 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Vladimir Markoeca3eda2016-11-09 16:26:44 +00001776 if (image_root_object != nullptr && image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001777 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001778 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001779 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001780 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1781 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001782 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001783 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1784 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001785 run++;
1786 } else {
1787 break;
1788 }
1789 }
1790 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001791 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001792 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001793 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001794 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001795 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001796 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001797 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001798 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001799 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001800 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001801 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001802 }
1803 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001804 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001805
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001806 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001807 os << "METHOD ROOTS\n";
1808 static_assert(arraysize(image_methods_descriptions_) ==
1809 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1810 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1811 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1812 const char* description = image_methods_descriptions_[i];
1813 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001814 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001815 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001816 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001817 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001818
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001819 Runtime* const runtime = Runtime::Current();
1820 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001821 std::string image_filename = image_space_.GetImageFilename();
1822 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001823 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001824 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001825 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001826 const OatFile* oat_file = image_space_.GetOatFile();
Alex Lighta59dd802014-07-02 16:28:08 -07001827 if (oat_file == nullptr) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001828 oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location);
1829 }
1830 if (oat_file == nullptr) {
1831 oat_file = OatFile::Open(oat_location,
1832 oat_location,
1833 nullptr,
1834 nullptr,
1835 false,
1836 /*low_4gb*/false,
1837 nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001838 &error_msg);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001839 }
1840 if (oat_file == nullptr) {
1841 os << "OAT FILE NOT FOUND: " << error_msg << "\n";
1842 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001843 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001844 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001845
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001846 stats_.oat_file_bytes = oat_file->Size();
1847
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001848 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001849
Mathieu Chartier02e25112013-08-14 16:14:24 -07001850 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001851 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001852 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1853 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001854 }
1855
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001856 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001857
Jeff Haodcdc85b2015-12-04 14:06:18 -08001858 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001859 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001860 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001861 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001862 {
1863 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1864 heap->FlushAllocStack();
1865 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001866 // Since FlushAllocStack() above resets the (active) allocation
1867 // stack. Need to revoke the thread-local allocation stacks that
1868 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001869 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001870 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001871 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001872 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001873 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001874 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001875 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001876 {
Andreas Gampecc1b5352016-12-01 16:58:38 -08001877 ReaderMutexLock mu(self, *Locks::dex_lock_);
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001878 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001879 ObjPtr<mirror::DexCache> dex_cache =
1880 ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001881 if (dex_cache != nullptr) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001882 dex_caches_.insert(dex_cache.Ptr());
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001883 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001884 }
1885 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001886 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001887 // Dump the normal objects before ArtMethods.
1888 image_space_.GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1889 indent_os << "\n";
1890 // TODO: Dump fields.
1891 // Dump methods after.
Jeff Haodcdc85b2015-12-04 14:06:18 -08001892 DumpArtMethodVisitor visitor(this);
Mathieu Chartiere42888f2016-04-14 10:49:19 -07001893 image_header_.VisitPackedArtMethods(&visitor,
1894 image_space_.Begin(),
1895 image_header_.GetPointerSize());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001896 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001897 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001898 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001899 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001900 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001901 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001902 size_t data_size = image_header_.GetDataSize(); // stored size in file.
1903 if (file == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001904 LOG(WARNING) << "Failed to find image in " << image_filename;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001905 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001906 stats_.file_bytes = file->GetLength();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001907 // If the image is compressed, adjust to decompressed size.
1908 size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader);
1909 if (image_header_.GetStorageMode() == ImageHeader::kStorageModeUncompressed) {
1910 DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image";
1911 }
1912 stats_.file_bytes += uncompressed_size - data_size;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001913 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001914 size_t header_bytes = sizeof(ImageHeader);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001915 const auto& object_section = image_header_.GetImageSection(ImageHeader::kSectionObjects);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001916 const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1917 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001918 const auto& dex_cache_arrays_section = image_header_.GetImageSection(
1919 ImageHeader::kSectionDexCacheArrays);
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001920 const auto& intern_section = image_header_.GetImageSection(
1921 ImageHeader::kSectionInternedStrings);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001922 const auto& class_table_section = image_header_.GetImageSection(
1923 ImageHeader::kSectionClassTable);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001924 const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1925
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001926 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001927
1928 // Objects are kObjectAlignment-aligned.
1929 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1930 if (object_section.Offset() > header_bytes) {
1931 stats_.alignment_bytes += object_section.Offset() - header_bytes;
1932 }
1933
1934 // Field section is 4-byte aligned.
1935 constexpr size_t kFieldSectionAlignment = 4U;
1936 uint32_t end_objects = object_section.Offset() + object_section.Size();
1937 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
1938 stats_.alignment_bytes += field_section.Offset() - end_objects;
1939
1940 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
1941 uint32_t end_fields = field_section.Offset() + field_section.Size();
1942 CHECK_ALIGNED(method_section.Offset(), 4);
1943 stats_.alignment_bytes += method_section.Offset() - end_fields;
1944
1945 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
1946 uint32_t end_methods = method_section.Offset() + method_section.Size();
1947 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
1948 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
1949
1950 // Intern table is 8-byte aligned.
1951 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
1952 CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
1953 stats_.alignment_bytes += intern_section.Offset() - end_caches;
1954
1955 // Add space between intern table and class table.
1956 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
1957 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
1958
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001959 // Add space between end of image data and bitmap. Expect the bitmap to be page-aligned.
1960 const size_t bitmap_offset = sizeof(ImageHeader) + data_size;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001961 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001962 stats_.alignment_bytes += RoundUp(bitmap_offset, kPageSize) - bitmap_offset;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001963
Mathieu Chartiere401d142015-04-22 13:56:20 -07001964 stats_.bitmap_bytes += bitmap_section.Size();
1965 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01001966 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01001967 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001968 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001969 stats_.class_table_bytes += class_table_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001970 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001971 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001972
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001973 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001974
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001975 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001976 }
1977
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001978 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001979 class DumpArtMethodVisitor : public ArtMethodVisitor {
1980 public:
1981 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
1982
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001983 virtual void Visit(ArtMethod* method) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001984 std::ostream& indent_os = image_dumper_->vios_.Stream();
David Sehr709b0702016-10-13 09:12:37 -07001985 indent_os << method << " " << " ArtMethod: " << ArtMethod::PrettyMethod(method) << "\n";
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001986 image_dumper_->DumpMethod(method, indent_os);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001987 indent_os << "\n";
1988 }
1989
1990 private:
1991 ImageDumper* const image_dumper_;
1992 };
1993
Mathieu Chartier3398c782016-09-30 10:27:43 -07001994 static void PrettyObjectValue(std::ostream& os,
1995 ObjPtr<mirror::Class> type,
1996 ObjPtr<mirror::Object> value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001997 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001998 CHECK(type != nullptr);
1999 if (value == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07002000 os << StringPrintf("null %s\n", type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002001 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002002 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002003 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07002004 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07002005 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002006 mirror::Class* klass = value->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07002007 os << StringPrintf("%p Class: %s\n", klass, mirror::Class::PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002008 } else {
David Sehr709b0702016-10-13 09:12:37 -07002009 os << StringPrintf("%p %s\n", value.Ptr(), type->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002010 }
2011 }
2012
Mathieu Chartier3398c782016-09-30 10:27:43 -07002013 static void PrintField(std::ostream& os, ArtField* field, ObjPtr<mirror::Object> obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002014 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07002015 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08002016 switch (field->GetTypeAsPrimitiveType()) {
2017 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08002018 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002019 break;
2020 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002021 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002022 break;
2023 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002024 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002025 break;
2026 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002027 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002028 break;
2029 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07002030 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002031 break;
2032 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07002033 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002034 break;
2035 case Primitive::kPrimBoolean:
Roland Levillain5e8d5f02016-10-18 18:03:43 +01002036 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj) ? "true" : "false",
Fred Shih37f05ef2014-07-16 18:38:08 -07002037 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002038 break;
2039 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07002040 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08002041 break;
2042 case Primitive::kPrimNot: {
2043 // Get the value, don't compute the type unless it is non-null as we don't want
2044 // to cause class loading.
Mathieu Chartier3398c782016-09-30 10:27:43 -07002045 ObjPtr<mirror::Object> value = field->GetObj(obj);
Ian Rogers08f1f502014-12-02 15:04:37 -08002046 if (value == nullptr) {
2047 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07002048 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08002049 // Grab the field type without causing resolution.
Mathieu Chartier3398c782016-09-30 10:27:43 -07002050 ObjPtr<mirror::Class> field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08002051 if (field_type != nullptr) {
2052 PrettyObjectValue(os, field_type, value);
2053 } else {
Mathieu Chartier3398c782016-09-30 10:27:43 -07002054 os << StringPrintf("%p %s\n",
Mathieu Chartier1cc62e42016-10-03 18:01:28 -07002055 value.Ptr(),
Ian Rogers08f1f502014-12-02 15:04:37 -08002056 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
2057 }
Ian Rogers50239c72013-06-17 14:53:22 -07002058 }
Ian Rogers08f1f502014-12-02 15:04:37 -08002059 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07002060 }
Ian Rogers08f1f502014-12-02 15:04:37 -08002061 default:
2062 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
2063 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08002064 }
2065 }
2066
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002067 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002068 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002069 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002070 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002071 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08002072 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002073 for (ArtField& field : klass->GetIFields()) {
2074 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08002075 }
2076 }
2077
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002078 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002079 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002080 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002081
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002082 const void* GetQuickOatCodeBegin(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08002083 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002084 image_header_.GetPointerSize());
Ian Rogers6f3dbba2014-10-14 17:41:57 -07002085 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002086 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002087 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002088 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002089 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002090 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002091 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002092 }
2093
Mathieu Chartiere401d142015-04-22 13:56:20 -07002094 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002095 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002096 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
2097 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002098 return 0;
2099 }
2100 return oat_code_begin[-1];
2101 }
2102
Mathieu Chartiere401d142015-04-22 13:56:20 -07002103 const void* GetQuickOatCodeEnd(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002104 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08002105 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002106 if (oat_code_begin == nullptr) {
2107 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002108 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08002109 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002110 }
2111
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002112 static void Callback(mirror::Object* obj, void* arg) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002113 DCHECK(obj != nullptr);
2114 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002115 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002116 if (!state->InDumpSpace(obj)) {
2117 return;
2118 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002119
2120 size_t object_bytes = obj->SizeOf();
2121 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
2122 state->stats_.object_bytes += object_bytes;
2123 state->stats_.alignment_bytes += alignment_bytes;
2124
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002125 std::ostream& os = state->vios_.Stream();
2126
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002127 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08002128 if (obj_class->IsArrayClass()) {
David Sehr709b0702016-10-13 09:12:37 -07002129 os << StringPrintf("%p: %s length:%d\n", obj, obj_class->PrettyDescriptor().c_str(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002130 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08002131 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002132 mirror::Class* klass = obj->AsClass();
David Sehr709b0702016-10-13 09:12:37 -07002133 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj,
2134 mirror::Class::PrettyDescriptor(klass).c_str())
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002135 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08002136 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002137 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07002138 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002139 } else {
David Sehr709b0702016-10-13 09:12:37 -07002140 os << StringPrintf("%p: %s\n", obj, obj_class->PrettyDescriptor().c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08002141 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002142 ScopedIndentation indent1(&state->vios_);
2143 DumpFields(os, obj, obj_class);
Andreas Gampe542451c2016-07-26 09:02:02 -07002144 const PointerSize image_pointer_size = state->image_header_.GetPointerSize();
Ian Rogersd5b32602012-02-26 16:40:04 -08002145 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002146 auto* obj_array = obj->AsObjectArray<mirror::Object>();
2147 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002148 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002149 size_t run = 0;
2150 for (int32_t j = i + 1; j < length; j++) {
2151 if (value == obj_array->Get(j)) {
2152 run++;
2153 } else {
2154 break;
2155 }
2156 }
2157 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002158 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08002159 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002160 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08002161 i = i + run;
2162 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002163 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002164 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002165 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08002166 }
2167 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07002168 mirror::Class* klass = obj->AsClass();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002169 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002170 os << "STATICS:\n";
2171 ScopedIndentation indent2(&state->vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07002172 for (ArtField& field : klass->GetSFields()) {
2173 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08002174 }
2175 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002176 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01002177 auto it = state->dex_caches_.find(obj);
2178 if (it != state->dex_caches_.end()) {
2179 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002180 const auto& field_section = state->image_header_.GetImageSection(
2181 ImageHeader::kSectionArtFields);
2182 const auto& method_section = state->image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01002183 size_t num_methods = dex_cache->NumResolvedMethods();
2184 if (num_methods != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002185 os << "Methods (size=" << num_methods << "):\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002186 ScopedIndentation indent2(&state->vios_);
2187 auto* resolved_methods = dex_cache->GetResolvedMethods();
2188 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002189 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_methods,
2190 i,
2191 image_pointer_size);
Vladimir Marko05792b92015-08-03 11:56:49 +01002192 size_t run = 0;
2193 for (size_t j = i + 1;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002194 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_methods,
2195 j,
2196 image_pointer_size);
2197 ++j) {
2198 ++run;
2199 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002200 if (run == 0) {
2201 os << StringPrintf("%zd: ", i);
2202 } else {
2203 os << StringPrintf("%zd to %zd: ", i, i + run);
2204 i = i + run;
2205 }
2206 std::string msg;
2207 if (elem == nullptr) {
2208 msg = "null";
2209 } else if (method_section.Contains(
2210 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002211 msg = reinterpret_cast<ArtMethod*>(elem)->PrettyMethod();
Vladimir Marko05792b92015-08-03 11:56:49 +01002212 } else {
2213 msg = "<not in method section>";
2214 }
2215 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07002216 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002217 }
2218 size_t num_fields = dex_cache->NumResolvedFields();
2219 if (num_fields != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002220 os << "Fields (size=" << num_fields << "):\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002221 ScopedIndentation indent2(&state->vios_);
2222 auto* resolved_fields = dex_cache->GetResolvedFields();
2223 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
Vladimir Markof44d36c2017-03-14 14:18:46 +00002224 auto* elem = mirror::DexCache::GetNativePairPtrSize(
2225 resolved_fields, i, image_pointer_size).object;
Vladimir Marko05792b92015-08-03 11:56:49 +01002226 size_t run = 0;
2227 for (size_t j = i + 1;
Vladimir Markof44d36c2017-03-14 14:18:46 +00002228 j != length &&
2229 elem == mirror::DexCache::GetNativePairPtrSize(
2230 resolved_fields, j, image_pointer_size).object;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002231 ++j) {
2232 ++run;
2233 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002234 if (run == 0) {
2235 os << StringPrintf("%zd: ", i);
2236 } else {
2237 os << StringPrintf("%zd to %zd: ", i, i + run);
2238 i = i + run;
2239 }
2240 std::string msg;
2241 if (elem == nullptr) {
2242 msg = "null";
2243 } else if (field_section.Contains(
2244 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
David Sehr709b0702016-10-13 09:12:37 -07002245 msg = reinterpret_cast<ArtField*>(elem)->PrettyField();
Vladimir Marko05792b92015-08-03 11:56:49 +01002246 } else {
2247 msg = "<not in field section>";
2248 }
2249 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002250 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002251 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002252 size_t num_types = dex_cache->NumResolvedTypes();
2253 if (num_types != 0u) {
2254 os << "Types (size=" << num_types << "):\n";
2255 ScopedIndentation indent2(&state->vios_);
2256 auto* resolved_types = dex_cache->GetResolvedTypes();
2257 for (size_t i = 0; i < num_types; ++i) {
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002258 auto pair = resolved_types[i].load(std::memory_order_relaxed);
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002259 size_t run = 0;
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002260 for (size_t j = i + 1; j != num_types; ++j) {
2261 auto other_pair = resolved_types[j].load(std::memory_order_relaxed);
2262 if (pair.index != other_pair.index ||
2263 pair.object.Read() != other_pair.object.Read()) {
2264 break;
2265 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002266 ++run;
2267 }
2268 if (run == 0) {
2269 os << StringPrintf("%zd: ", i);
2270 } else {
2271 os << StringPrintf("%zd to %zd: ", i, i + run);
2272 i = i + run;
2273 }
2274 std::string msg;
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002275 auto* elem = pair.object.Read();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002276 if (elem == nullptr) {
2277 msg = "null";
2278 } else {
David Sehr709b0702016-10-13 09:12:37 -07002279 msg = elem->PrettyClass();
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002280 }
Vladimir Marko8d6768d2017-03-14 10:13:21 +00002281 os << StringPrintf("%p %u %s\n", elem, pair.index, msg.c_str());
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01002282 }
2283 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002284 }
2285 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002286 std::string temp;
2287 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002288 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002289
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002290 void DumpMethod(ArtMethod* method, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002291 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002292 DCHECK(method != nullptr);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002293 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
2294 const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
Andreas Gampe542451c2016-07-26 09:02:02 -07002295 const PointerSize pointer_size = image_header_.GetPointerSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002296 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2297 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002298 if (method->IsNative()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002299 bool first_occurrence;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002300 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2301 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002302 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002303 stats_.native_to_managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002304 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002305 if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
2306 image_header_.GetPointerSize())) {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002307 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002308 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002309 } else if (method->IsAbstract() || method->IsClassInitializer()) {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002310 // Don't print information for these.
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002311 } else if (method->IsRuntimeMethod()) {
2312 ImtConflictTable* table = method->GetImtConflictTable(image_header_.GetPointerSize());
2313 if (table != nullptr) {
2314 indent_os << "IMT conflict table " << table << " method: ";
2315 for (size_t i = 0, count = table->NumEntries(pointer_size); i < count; ++i) {
David Sehr709b0702016-10-13 09:12:37 -07002316 indent_os << ArtMethod::PrettyMethod(table->GetImplementationMethod(i, pointer_size))
2317 << " ";
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002318 }
2319 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002320 } else {
2321 const DexFile::CodeItem* code_item = method->GetCodeItem();
2322 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002323 stats_.dex_instruction_bytes += dex_instruction_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002324
2325 bool first_occurrence;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002326 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002327 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01002328 // Method compiled with the optimizing compiler have no vmap table.
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002329 vmap_table_bytes = ComputeOatSize(method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002330 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002331 stats_.vmap_table_bytes += vmap_table_bytes;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002332 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002333 }
2334
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002335 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2336 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002337 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002338 stats_.managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002339 if (method->IsConstructor()) {
2340 if (method->IsStatic()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002341 stats_.class_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002342 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002343 stats_.large_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002344 }
2345 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002346 stats_.large_method_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002347 }
2348 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002349 stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002350
Igor Murashkin7617abd2015-07-10 18:27:47 -07002351 uint32_t method_access_flags = method->GetAccessFlags();
2352
Mathieu Chartiere401d142015-04-22 13:56:20 -07002353 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002354 indent_os << StringPrintf("SIZE: Dex Instructions=%zd StackMaps=%zd AccessFlags=0x%x\n",
2355 dex_instruction_bytes,
2356 vmap_table_bytes,
Igor Murashkin7617abd2015-07-10 18:27:47 -07002357 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002358
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002359 size_t total_size = dex_instruction_bytes +
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002360 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002361
2362 double expansion =
2363 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002364 stats_.ComputeOutliers(total_size, expansion, method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002365 }
2366 }
2367
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002368 std::set<const void*> already_seen_;
2369 // Compute the size of the given data within the oat file and whether this is the first time
2370 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002371 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002372 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002373 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002374 already_seen_.insert(oat_data);
2375 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002376 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002377 }
2378 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002379 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002380
2381 public:
2382 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002383 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002384 size_t file_bytes;
2385
2386 size_t header_bytes;
2387 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002388 size_t art_field_bytes;
2389 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01002390 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002391 size_t interned_strings_bytes;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002392 size_t class_table_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07002393 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002394 size_t alignment_bytes;
2395
2396 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002397 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002398 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07002399 size_t class_initializer_code_bytes;
2400 size_t large_initializer_code_bytes;
2401 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002402
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002403 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002404
2405 size_t dex_instruction_bytes;
2406
Mathieu Chartiere401d142015-04-22 13:56:20 -07002407 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002408 std::vector<size_t> method_outlier_size;
2409 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002410 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002411
Roland Levillain3887c462015-08-12 18:15:42 +01002412 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002413 : oat_file_bytes(0),
2414 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002415 header_bytes(0),
2416 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002417 art_field_bytes(0),
2418 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01002419 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002420 interned_strings_bytes(0),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002421 class_table_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07002422 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002423 alignment_bytes(0),
2424 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002425 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002426 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07002427 class_initializer_code_bytes(0),
2428 large_initializer_code_bytes(0),
2429 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002430 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002431 dex_instruction_bytes(0) {}
2432
Elliott Hughesa0e18062012-04-13 15:59:59 -07002433 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002434 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002435 size_t bytes;
2436 size_t count;
2437 };
2438 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2439 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002440
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002441 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002442 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2443 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002444 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002445 it->second.count += 1;
2446 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002447 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002448 }
2449 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002450
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002451 double PercentOfOatBytes(size_t size) {
2452 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2453 }
2454
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002455 double PercentOfFileBytes(size_t size) {
2456 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2457 }
2458
2459 double PercentOfObjectBytes(size_t size) {
2460 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2461 }
2462
Mathieu Chartiere401d142015-04-22 13:56:20 -07002463 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002464 method_outlier_size.push_back(total_size);
2465 method_outlier_expansion.push_back(expansion);
2466 method_outlier.push_back(method);
2467 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002468
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002469 void DumpOutliers(std::ostream& os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002470 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002471 size_t sum_of_sizes = 0;
2472 size_t sum_of_sizes_squared = 0;
2473 size_t sum_of_expansion = 0;
2474 size_t sum_of_expansion_squared = 0;
2475 size_t n = method_outlier_size.size();
Mathieu Chartier1ebf8d32016-06-09 11:51:27 -07002476 if (n <= 1) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08002477 return;
2478 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002479 for (size_t i = 0; i < n; i++) {
2480 size_t cur_size = method_outlier_size[i];
2481 sum_of_sizes += cur_size;
2482 sum_of_sizes_squared += cur_size * cur_size;
2483 double cur_expansion = method_outlier_expansion[i];
2484 sum_of_expansion += cur_expansion;
2485 sum_of_expansion_squared += cur_expansion * cur_expansion;
2486 }
2487 size_t size_mean = sum_of_sizes / n;
2488 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2489 double expansion_mean = sum_of_expansion / n;
2490 double expansion_variance =
2491 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2492
2493 // Dump methods whose size is a certain number of standard deviations from the mean
2494 size_t dumped_values = 0;
2495 size_t skipped_values = 0;
2496 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2497 size_t cur_size_variance = i * i * size_variance;
2498 bool first = true;
2499 for (size_t j = 0; j < n; j++) {
2500 size_t cur_size = method_outlier_size[j];
2501 if (cur_size > size_mean) {
2502 size_t cur_var = cur_size - size_mean;
2503 cur_var = cur_var * cur_var;
2504 if (cur_var > cur_size_variance) {
2505 if (dumped_values > 20) {
2506 if (i == 1) {
2507 skipped_values++;
2508 } else {
2509 i = 2; // jump to counting for 1 standard deviation
2510 break;
2511 }
2512 } else {
2513 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002514 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002515 first = false;
2516 }
David Sehr709b0702016-10-13 09:12:37 -07002517 os << ArtMethod::PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002518 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002519 method_outlier_size[j] = 0; // don't consider this method again
2520 dumped_values++;
2521 }
2522 }
2523 }
2524 }
2525 }
2526 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002527 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002528 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002529 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002530 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002531
2532 // Dump methods whose expansion is a certain number of standard deviations from the mean
2533 dumped_values = 0;
2534 skipped_values = 0;
2535 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2536 double cur_expansion_variance = i * i * expansion_variance;
2537 bool first = true;
2538 for (size_t j = 0; j < n; j++) {
2539 double cur_expansion = method_outlier_expansion[j];
2540 if (cur_expansion > expansion_mean) {
2541 size_t cur_var = cur_expansion - expansion_mean;
2542 cur_var = cur_var * cur_var;
2543 if (cur_var > cur_expansion_variance) {
2544 if (dumped_values > 20) {
2545 if (i == 1) {
2546 skipped_values++;
2547 } else {
2548 i = 2; // jump to counting for 1 standard deviation
2549 break;
2550 }
2551 } else {
2552 if (first) {
2553 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002554 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002555 first = false;
2556 }
David Sehr709b0702016-10-13 09:12:37 -07002557 os << ArtMethod::PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002558 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002559 method_outlier_expansion[j] = 0.0; // don't consider this method again
2560 dumped_values++;
2561 }
2562 }
2563 }
2564 }
2565 }
2566 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002567 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002568 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002569 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002570 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002571 }
2572
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002573 void Dump(std::ostream& os, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002574 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002575 {
2576 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2577 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002578 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2579 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2580 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2581 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2582 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2583 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002584 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002585 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2586 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002587 header_bytes, PercentOfFileBytes(header_bytes),
2588 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002589 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2590 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002591 dex_cache_arrays_bytes,
2592 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002593 interned_strings_bytes,
2594 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002595 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002596 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002597 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2598 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002599 CHECK_EQ(file_bytes,
2600 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2601 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2602 bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002603 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002604
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002605 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002606 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002607 for (const auto& sizes_and_count : sizes_and_counts) {
2608 const std::string& descriptor(sizes_and_count.first);
2609 double average = static_cast<double>(sizes_and_count.second.bytes) /
2610 static_cast<double>(sizes_and_count.second.count);
2611 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002612 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002613 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002614 descriptor.c_str(), sizes_and_count.second.bytes,
2615 sizes_and_count.second.count, average, percent);
2616 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002617 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002618 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002619 CHECK_EQ(object_bytes, object_bytes_total);
2620
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002621 os << StringPrintf("oat_file_bytes = %8zd\n"
2622 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002623 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2624 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2625 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2626 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002627 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002628 managed_code_bytes,
2629 PercentOfOatBytes(managed_code_bytes),
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002630 native_to_managed_code_bytes,
2631 PercentOfOatBytes(native_to_managed_code_bytes),
2632 class_initializer_code_bytes,
2633 PercentOfOatBytes(class_initializer_code_bytes),
2634 large_initializer_code_bytes,
2635 PercentOfOatBytes(large_initializer_code_bytes),
2636 large_method_code_bytes,
2637 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002638 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002639 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002640 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002641 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2642 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002643 }
2644
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002645 os << "\n" << StringPrintf("vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002646 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002647 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002648
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002649 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2650 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002651 static_cast<double>(managed_code_bytes) /
2652 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002653 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002654 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002655 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002656
2657 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002658 }
2659 } stats_;
2660
2661 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002662 enum {
2663 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2664 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2665 kLargeConstructorDexBytes = 4000,
2666 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2667 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2668 kLargeMethodDexBytes = 16000
2669 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002670
2671 // For performance, use the *os_ directly for anything that doesn't need indentation
2672 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002673 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002674 VariableIndentationOutputStream vios_;
2675 ScopedIndentation indent1_;
2676
Ian Rogers1d54e732013-05-02 21:10:01 -07002677 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002678 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002679 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002680 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002681 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002682
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002683 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002684};
2685
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002686static int DumpImage(gc::space::ImageSpace* image_space,
2687 OatDumperOptions* options,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002688 std::ostream* os) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002689 const ImageHeader& image_header = image_space->GetImageHeader();
2690 if (!image_header.IsValid()) {
2691 fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
2692 return EXIT_FAILURE;
2693 }
2694 ImageDumper image_dumper(os, *image_space, image_header, options);
2695 if (!image_dumper.Dump()) {
2696 return EXIT_FAILURE;
2697 }
2698 return EXIT_SUCCESS;
2699}
2700
2701static int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002702 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002703 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002704 options->class_loader_ = &null_class_loader;
2705
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002706 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002707 if (options->app_image_ != nullptr) {
2708 if (options->app_oat_ == nullptr) {
2709 LOG(ERROR) << "Can not dump app image without app oat file";
Jeff Haodcdc85b2015-12-04 14:06:18 -08002710 return EXIT_FAILURE;
2711 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002712 // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
2713 // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
2714 // pointers into 32 bit pointer sized ArtMethods.
2715 std::string error_msg;
2716 std::unique_ptr<OatFile> oat_file(OatFile::Open(options->app_oat_,
2717 options->app_oat_,
2718 nullptr,
2719 nullptr,
2720 false,
2721 /*low_4gb*/true,
2722 nullptr,
2723 &error_msg));
2724 if (oat_file == nullptr) {
2725 LOG(ERROR) << "Failed to open oat file " << options->app_oat_ << " with error " << error_msg;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002726 return EXIT_FAILURE;
2727 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002728 std::unique_ptr<gc::space::ImageSpace> space(
2729 gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg));
2730 if (space == nullptr) {
2731 LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error "
2732 << error_msg;
2733 }
2734 // Open dex files for the image.
2735 std::vector<std::unique_ptr<const DexFile>> dex_files;
2736 if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
2737 LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
2738 << error_msg;
2739 }
2740 // Dump the actual image.
2741 int result = DumpImage(space.get(), options, os);
2742 if (result != EXIT_SUCCESS) {
2743 return result;
2744 }
2745 // Fall through to dump the boot images.
2746 }
2747
2748 gc::Heap* heap = runtime->GetHeap();
2749 CHECK(heap->HasBootImageSpace()) << "No image spaces";
2750 for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
2751 int result = DumpImage(image_space, options, os);
2752 if (result != EXIT_SUCCESS) {
2753 return result;
2754 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002755 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002756 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002757}
2758
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002759static jobject InstallOatFile(Runtime* runtime,
2760 std::unique_ptr<OatFile> oat_file,
2761 std::vector<const DexFile*>* class_path)
2762 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002763 Thread* self = Thread::Current();
2764 CHECK(self != nullptr);
2765 // Need well-known-classes.
2766 WellKnownClasses::Init(self->GetJniEnv());
2767
2768 // Need to register dex files to get a working dex cache.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002769 OatFile* oat_file_ptr = oat_file.get();
Andreas Gampe00b25f32014-09-17 21:49:05 -07002770 ClassLinker* class_linker = runtime->GetClassLinker();
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002771 runtime->GetOatFileManager().RegisterOatFile(std::move(oat_file));
2772 for (const OatFile::OatDexFile* odf : oat_file_ptr->GetOatDexFiles()) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002773 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002774 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002775 CHECK(dex_file != nullptr) << error_msg;
Vladimir Markocd556b02017-02-03 11:47:34 +00002776 ObjPtr<mirror::DexCache> dex_cache =
2777 class_linker->RegisterDexFile(*dex_file, nullptr);
2778 CHECK(dex_cache != nullptr);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002779 class_path->push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002780 }
2781
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002782 // Need a class loader. Fake that we're a compiler.
2783 // Note: this will run initializers through the unstarted runtime, so make sure it's
2784 // initialized.
2785 interpreter::UnstartedRuntime::Initialize();
2786
2787 jobject class_loader = class_linker->CreatePathClassLoader(self, *class_path);
2788
2789 return class_loader;
2790}
2791
2792static int DumpOatWithRuntime(Runtime* runtime,
2793 std::unique_ptr<OatFile> oat_file,
2794 OatDumperOptions* options,
2795 std::ostream* os) {
2796 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2797 ScopedObjectAccess soa(Thread::Current());
2798
2799 OatFile* oat_file_ptr = oat_file.get();
2800 std::vector<const DexFile*> class_path;
2801 jobject class_loader = InstallOatFile(runtime, std::move(oat_file), &class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002802
2803 // Use the class loader while dumping.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002804 StackHandleScope<1> scope(soa.Self());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002805 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -07002806 soa.Decode<mirror::ClassLoader>(class_loader));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002807 options->class_loader_ = &loader_handle;
2808
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002809 OatDumper oat_dumper(*oat_file_ptr, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002810 bool success = oat_dumper.Dump(*os);
2811 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2812}
2813
2814static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002815 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002816 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002817 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002818 options->class_loader_ = &null_class_loader;
2819
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002820 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002821 bool success = oat_dumper.Dump(*os);
2822 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2823}
2824
2825static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2826 std::ostream* os) {
2827 std::string error_msg;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002828 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
2829 oat_filename,
2830 nullptr,
2831 nullptr,
2832 false,
2833 /*low_4gb*/false,
2834 nullptr,
2835 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002836 if (oat_file == nullptr) {
2837 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2838 return EXIT_FAILURE;
2839 }
2840
2841 if (runtime != nullptr) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002842 return DumpOatWithRuntime(runtime, std::move(oat_file), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002843 } else {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002844 return DumpOatWithoutRuntime(oat_file.get(), options, os);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002845 }
2846}
2847
David Srbecky2fdd03c2016-03-10 15:32:37 +00002848static int SymbolizeOat(const char* oat_filename, std::string& output_name, bool no_bits) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002849 std::string error_msg;
Andreas Gampe08c277c2017-04-26 22:22:15 -07002850 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
2851 oat_filename,
2852 nullptr,
2853 nullptr,
2854 false,
2855 /*low_4gb*/false,
2856 nullptr,
2857 &error_msg));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002858 if (oat_file == nullptr) {
2859 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2860 return EXIT_FAILURE;
2861 }
2862
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002863 bool result;
2864 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
2865 // files for 64-bit code in the past.
2866 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
Andreas Gampe08c277c2017-04-26 22:22:15 -07002867 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file.get(), output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002868 result = oat_symbolizer.Symbolize();
2869 } else {
Andreas Gampe08c277c2017-04-26 22:22:15 -07002870 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file.get(), output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002871 result = oat_symbolizer.Symbolize();
2872 }
2873 if (!result) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002874 fprintf(stderr, "Failed to symbolize\n");
2875 return EXIT_FAILURE;
2876 }
2877
2878 return EXIT_SUCCESS;
2879}
2880
Andreas Gampe9fded872016-09-25 16:08:35 -07002881class IMTDumper {
2882 public:
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002883 static bool Dump(Runtime* runtime,
2884 const std::string& imt_file,
2885 bool dump_imt_stats,
2886 const char* oat_filename) {
2887 Thread* self = Thread::Current();
2888
2889 ScopedObjectAccess soa(self);
2890 StackHandleScope<1> scope(self);
2891 MutableHandle<mirror::ClassLoader> class_loader = scope.NewHandle<mirror::ClassLoader>(nullptr);
2892 std::vector<const DexFile*> class_path;
2893
2894 if (oat_filename != nullptr) {
2895 std::string error_msg;
2896 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
2897 oat_filename,
2898 nullptr,
2899 nullptr,
2900 false,
2901 /*low_4gb*/false,
2902 nullptr,
2903 &error_msg));
2904 if (oat_file == nullptr) {
2905 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2906 return false;
2907 }
2908
2909 class_loader.Assign(soa.Decode<mirror::ClassLoader>(
2910 InstallOatFile(runtime, std::move(oat_file), &class_path)));
2911 } else {
2912 class_loader.Assign(nullptr); // Boot classloader. Just here for explicit documentation.
2913 class_path = runtime->GetClassLinker()->GetBootClassPath();
2914 }
2915
2916 if (!imt_file.empty()) {
2917 return DumpImt(runtime, imt_file, class_loader);
2918 }
2919
2920 if (dump_imt_stats) {
2921 return DumpImtStats(runtime, class_path, class_loader);
2922 }
2923
2924 LOG(FATAL) << "Should not reach here";
2925 UNREACHABLE();
2926 }
2927
2928 private:
2929 static bool DumpImt(Runtime* runtime,
2930 const std::string& imt_file,
2931 Handle<mirror::ClassLoader> h_class_loader)
2932 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002933 std::vector<std::string> lines = ReadCommentedInputFromFile(imt_file);
2934 std::unordered_set<std::string> prepared;
2935
2936 for (const std::string& line : lines) {
2937 // A line should be either a class descriptor, in which case we will dump the complete IMT,
2938 // or a class descriptor and an interface method, in which case we will lookup the method,
2939 // determine its IMT slot, and check the class' IMT.
2940 size_t first_space = line.find(' ');
2941 if (first_space == std::string::npos) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002942 DumpIMTForClass(runtime, line, h_class_loader, &prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07002943 } else {
2944 DumpIMTForMethod(runtime,
2945 line.substr(0, first_space),
2946 line.substr(first_space + 1, std::string::npos),
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002947 h_class_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07002948 &prepared);
2949 }
2950 std::cerr << std::endl;
2951 }
2952
2953 return true;
2954 }
2955
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002956 static bool DumpImtStats(Runtime* runtime,
2957 const std::vector<const DexFile*>& dex_files,
2958 Handle<mirror::ClassLoader> h_class_loader)
2959 REQUIRES_SHARED(Locks::mutator_lock_) {
2960 size_t without_imt = 0;
2961 size_t with_imt = 0;
Andreas Gampe9fded872016-09-25 16:08:35 -07002962 std::map<size_t, size_t> histogram;
2963
2964 ClassLinker* class_linker = runtime->GetClassLinker();
2965 const PointerSize pointer_size = class_linker->GetImagePointerSize();
2966 std::unordered_set<std::string> prepared;
2967
2968 Thread* self = Thread::Current();
Andreas Gampe9fded872016-09-25 16:08:35 -07002969 StackHandleScope<1> scope(self);
2970 MutableHandle<mirror::Class> h_klass(scope.NewHandle<mirror::Class>(nullptr));
2971
2972 for (const DexFile* dex_file : dex_files) {
2973 for (uint32_t class_def_index = 0;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002974 class_def_index != dex_file->NumClassDefs();
2975 ++class_def_index) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002976 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
2977 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002978 h_klass.Assign(class_linker->FindClass(self, descriptor, h_class_loader));
Andreas Gampefa4333d2017-02-14 11:10:34 -08002979 if (h_klass == nullptr) {
Andreas Gampe9fded872016-09-25 16:08:35 -07002980 std::cerr << "Warning: could not load " << descriptor << std::endl;
2981 continue;
2982 }
2983
2984 if (HasNoIMT(runtime, h_klass, pointer_size, &prepared)) {
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002985 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07002986 continue;
2987 }
2988
2989 ImTable* im_table = PrepareAndGetImTable(runtime, h_klass, pointer_size, &prepared);
2990 if (im_table == nullptr) {
2991 // Should not happen, but accept.
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002992 without_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07002993 continue;
2994 }
2995
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07002996 with_imt++;
Andreas Gampe9fded872016-09-25 16:08:35 -07002997 for (size_t imt_index = 0; imt_index != ImTable::kSize; ++imt_index) {
2998 ArtMethod* ptr = im_table->Get(imt_index, pointer_size);
2999 if (ptr->IsRuntimeMethod()) {
3000 if (ptr->IsImtUnimplementedMethod()) {
3001 histogram[0]++;
3002 } else {
3003 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3004 histogram[current_table->NumEntries(pointer_size)]++;
3005 }
3006 } else {
3007 histogram[1]++;
3008 }
3009 }
3010 }
3011 }
3012
3013 std::cerr << "IMT stats:"
3014 << std::endl << std::endl;
3015
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003016 std::cerr << " " << with_imt << " classes with IMT."
Andreas Gampe9fded872016-09-25 16:08:35 -07003017 << std::endl << std::endl;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003018 std::cerr << " " << without_imt << " classes without IMT (or copy from Object)."
Andreas Gampe9fded872016-09-25 16:08:35 -07003019 << std::endl << std::endl;
3020
3021 double sum_one = 0;
3022 size_t count_one = 0;
3023
3024 std::cerr << " " << "IMT histogram" << std::endl;
3025 for (auto& bucket : histogram) {
3026 std::cerr << " " << bucket.first << " " << bucket.second << std::endl;
3027 if (bucket.first > 0) {
3028 sum_one += bucket.second * bucket.first;
3029 count_one += bucket.second;
3030 }
3031 }
3032
3033 double count_zero = count_one + histogram[0];
3034 std::cerr << " Stats:" << std::endl;
3035 std::cerr << " Average depth (including empty): " << (sum_one / count_zero) << std::endl;
3036 std::cerr << " Average depth (excluding empty): " << (sum_one / count_one) << std::endl;
3037
3038 return true;
3039 }
3040
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003041 // Return whether the given class has no IMT (or the one shared with java.lang.Object).
Andreas Gampe9fded872016-09-25 16:08:35 -07003042 static bool HasNoIMT(Runtime* runtime,
3043 Handle<mirror::Class> klass,
3044 const PointerSize pointer_size,
3045 std::unordered_set<std::string>* prepared)
3046 REQUIRES_SHARED(Locks::mutator_lock_) {
3047 if (klass->IsObjectClass() || !klass->ShouldHaveImt()) {
3048 return true;
3049 }
3050
3051 if (klass->GetImt(pointer_size) == nullptr) {
3052 PrepareClass(runtime, klass, prepared);
3053 }
3054
3055 mirror::Class* object_class = mirror::Class::GetJavaLangClass()->GetSuperClass();
3056 DCHECK(object_class->IsObjectClass());
3057
3058 bool result = klass->GetImt(pointer_size) == object_class->GetImt(pointer_size);
3059
Mathieu Chartier6beced42016-11-15 15:51:31 -08003060 if (klass->GetIfTable()->Count() == 0) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003061 DCHECK(result);
3062 }
3063
3064 return result;
3065 }
3066
3067 static void PrintTable(ImtConflictTable* table, PointerSize pointer_size)
3068 REQUIRES_SHARED(Locks::mutator_lock_) {
3069 if (table == nullptr) {
3070 std::cerr << " <No IMT?>" << std::endl;
3071 return;
3072 }
3073 size_t table_index = 0;
3074 for (;;) {
3075 ArtMethod* ptr = table->GetInterfaceMethod(table_index, pointer_size);
3076 if (ptr == nullptr) {
3077 return;
3078 }
3079 table_index++;
David Sehr709b0702016-10-13 09:12:37 -07003080 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003081 }
3082 }
3083
3084 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3085 Thread* self,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003086 Handle<mirror::ClassLoader> h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003087 const std::string& class_name,
3088 const PointerSize pointer_size,
3089 mirror::Class** klass_out,
3090 std::unordered_set<std::string>* prepared)
3091 REQUIRES_SHARED(Locks::mutator_lock_) {
3092 if (class_name.empty()) {
3093 return nullptr;
3094 }
3095
3096 std::string descriptor;
3097 if (class_name[0] == 'L') {
3098 descriptor = class_name;
3099 } else {
3100 descriptor = DotToDescriptor(class_name.c_str());
3101 }
3102
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003103 mirror::Class* klass = runtime->GetClassLinker()->FindClass(self, descriptor.c_str(), h_loader);
Andreas Gampe9fded872016-09-25 16:08:35 -07003104
3105 if (klass == nullptr) {
3106 self->ClearException();
3107 std::cerr << "Did not find " << class_name << std::endl;
3108 *klass_out = nullptr;
3109 return nullptr;
3110 }
3111
3112 StackHandleScope<1> scope(Thread::Current());
3113 Handle<mirror::Class> h_klass = scope.NewHandle<mirror::Class>(klass);
3114
3115 ImTable* ret = PrepareAndGetImTable(runtime, h_klass, pointer_size, prepared);
3116 *klass_out = h_klass.Get();
3117 return ret;
3118 }
3119
3120 static ImTable* PrepareAndGetImTable(Runtime* runtime,
3121 Handle<mirror::Class> h_klass,
3122 const PointerSize pointer_size,
3123 std::unordered_set<std::string>* prepared)
3124 REQUIRES_SHARED(Locks::mutator_lock_) {
3125 PrepareClass(runtime, h_klass, prepared);
3126 return h_klass->GetImt(pointer_size);
3127 }
3128
3129 static void DumpIMTForClass(Runtime* runtime,
3130 const std::string& class_name,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003131 Handle<mirror::ClassLoader> h_loader,
3132 std::unordered_set<std::string>* prepared)
3133 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003134 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
3135 mirror::Class* klass;
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003136 ImTable* imt = PrepareAndGetImTable(runtime,
3137 Thread::Current(),
3138 h_loader,
3139 class_name,
3140 pointer_size,
3141 &klass,
3142 prepared);
Andreas Gampe9fded872016-09-25 16:08:35 -07003143 if (imt == nullptr) {
3144 return;
3145 }
3146
3147 std::cerr << class_name << std::endl << " IMT:" << std::endl;
3148 for (size_t index = 0; index < ImTable::kSize; ++index) {
3149 std::cerr << " " << index << ":" << std::endl;
3150 ArtMethod* ptr = imt->Get(index, pointer_size);
3151 if (ptr->IsRuntimeMethod()) {
3152 if (ptr->IsImtUnimplementedMethod()) {
3153 std::cerr << " <empty>" << std::endl;
3154 } else {
3155 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3156 PrintTable(current_table, pointer_size);
3157 }
3158 } else {
David Sehr709b0702016-10-13 09:12:37 -07003159 std::cerr << " " << ptr->PrettyMethod(true) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003160 }
3161 }
3162
3163 std::cerr << " Interfaces:" << std::endl;
3164 // Run through iftable, find methods that slot here, see if they fit.
3165 mirror::IfTable* if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003166 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
3167 mirror::Class* iface = if_table->GetInterface(i);
3168 std::string iface_name;
3169 std::cerr << " " << iface->GetDescriptor(&iface_name) << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003170
Mathieu Chartier6beced42016-11-15 15:51:31 -08003171 for (ArtMethod& iface_method : iface->GetVirtualMethods(pointer_size)) {
3172 uint32_t class_hash, name_hash, signature_hash;
3173 ImTable::GetImtHashComponents(&iface_method, &class_hash, &name_hash, &signature_hash);
3174 uint32_t imt_slot = ImTable::GetImtIndex(&iface_method);
3175 std::cerr << " " << iface_method.PrettyMethod(true)
3176 << " slot=" << imt_slot
3177 << std::hex
3178 << " class_hash=0x" << class_hash
3179 << " name_hash=0x" << name_hash
3180 << " signature_hash=0x" << signature_hash
3181 << std::dec
3182 << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003183 }
3184 }
3185 }
3186
3187 static void DumpIMTForMethod(Runtime* runtime,
3188 const std::string& class_name,
3189 const std::string& method,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003190 Handle<mirror::ClassLoader> h_loader,
3191 std::unordered_set<std::string>* prepared)
3192 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003193 const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
3194 mirror::Class* klass;
3195 ImTable* imt = PrepareAndGetImTable(runtime,
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003196 Thread::Current(),
3197 h_loader,
Andreas Gampe9fded872016-09-25 16:08:35 -07003198 class_name,
3199 pointer_size,
3200 &klass,
3201 prepared);
3202 if (imt == nullptr) {
3203 return;
3204 }
3205
3206 std::cerr << class_name << " <" << method << ">" << std::endl;
3207 for (size_t index = 0; index < ImTable::kSize; ++index) {
3208 ArtMethod* ptr = imt->Get(index, pointer_size);
3209 if (ptr->IsRuntimeMethod()) {
3210 if (ptr->IsImtUnimplementedMethod()) {
3211 continue;
3212 }
3213
3214 ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3215 if (current_table == nullptr) {
3216 continue;
3217 }
3218
3219 size_t table_index = 0;
3220 for (;;) {
3221 ArtMethod* ptr2 = current_table->GetInterfaceMethod(table_index, pointer_size);
3222 if (ptr2 == nullptr) {
3223 break;
3224 }
3225 table_index++;
3226
David Sehr709b0702016-10-13 09:12:37 -07003227 std::string p_name = ptr2->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003228 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003229 std::cerr << " Slot "
3230 << index
3231 << " ("
3232 << current_table->NumEntries(pointer_size)
3233 << ")"
3234 << std::endl;
3235 PrintTable(current_table, pointer_size);
3236 return;
3237 }
3238 }
3239 } else {
David Sehr709b0702016-10-13 09:12:37 -07003240 std::string p_name = ptr->PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003241 if (android::base::StartsWith(p_name, method.c_str())) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003242 std::cerr << " Slot " << index << " (1)" << std::endl;
3243 std::cerr << " " << p_name << std::endl;
3244 } else {
3245 // Run through iftable, find methods that slot here, see if they fit.
3246 mirror::IfTable* if_table = klass->GetIfTable();
Mathieu Chartier6beced42016-11-15 15:51:31 -08003247 for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
3248 mirror::Class* iface = if_table->GetInterface(i);
3249 size_t num_methods = iface->NumDeclaredVirtualMethods();
3250 if (num_methods > 0) {
3251 for (ArtMethod& iface_method : iface->GetMethods(pointer_size)) {
3252 if (ImTable::GetImtIndex(&iface_method) == index) {
3253 std::string i_name = iface_method.PrettyMethod(true);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003254 if (android::base::StartsWith(i_name, method.c_str())) {
Mathieu Chartier6beced42016-11-15 15:51:31 -08003255 std::cerr << " Slot " << index << " (1)" << std::endl;
3256 std::cerr << " " << p_name << " (" << i_name << ")" << std::endl;
Andreas Gampe9fded872016-09-25 16:08:35 -07003257 }
3258 }
3259 }
3260 }
3261 }
3262 }
3263 }
3264 }
3265 }
3266
3267 // Read lines from the given stream, dropping comments and empty lines
3268 static std::vector<std::string> ReadCommentedInputStream(std::istream& in_stream) {
3269 std::vector<std::string> output;
3270 while (in_stream.good()) {
3271 std::string dot;
3272 std::getline(in_stream, dot);
Andreas Gampe9186ced2016-12-12 14:28:21 -08003273 if (android::base::StartsWith(dot, "#") || dot.empty()) {
Andreas Gampe9fded872016-09-25 16:08:35 -07003274 continue;
3275 }
3276 output.push_back(dot);
3277 }
3278 return output;
3279 }
3280
3281 // Read lines from the given file, dropping comments and empty lines.
3282 static std::vector<std::string> ReadCommentedInputFromFile(const std::string& input_filename) {
3283 std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
3284 if (input_file.get() == nullptr) {
3285 LOG(ERROR) << "Failed to open input file " << input_filename;
3286 return std::vector<std::string>();
3287 }
3288 std::vector<std::string> result = ReadCommentedInputStream(*input_file);
3289 input_file->close();
3290 return result;
3291 }
3292
3293 // Prepare a class, i.e., ensure it has a filled IMT. Will do so recursively for superclasses,
3294 // and note in the given set that the work was done.
3295 static void PrepareClass(Runtime* runtime,
3296 Handle<mirror::Class> h_klass,
3297 std::unordered_set<std::string>* done)
3298 REQUIRES_SHARED(Locks::mutator_lock_) {
3299 if (!h_klass->ShouldHaveImt()) {
3300 return;
3301 }
3302
3303 std::string name;
3304 name = h_klass->GetDescriptor(&name);
3305
3306 if (done->find(name) != done->end()) {
3307 return;
3308 }
3309 done->insert(name);
3310
3311 if (h_klass->HasSuperClass()) {
3312 StackHandleScope<1> h(Thread::Current());
3313 PrepareClass(runtime, h.NewHandle<mirror::Class>(h_klass->GetSuperClass()), done);
3314 }
3315
3316 if (!h_klass->IsTemp()) {
3317 runtime->GetClassLinker()->FillIMTAndConflictTables(h_klass.Get());
3318 }
3319 }
3320};
3321
Igor Murashkin37743352014-11-13 14:38:00 -08003322struct OatdumpArgs : public CmdlineArgs {
3323 protected:
3324 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003325
Igor Murashkin37743352014-11-13 14:38:00 -08003326 virtual ParseStatus ParseCustom(const StringPiece& option,
3327 std::string* error_msg) OVERRIDE {
3328 {
3329 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
3330 if (base_parse != kParseUnknownArgument) {
3331 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003332 }
3333 }
3334
Igor Murashkin37743352014-11-13 14:38:00 -08003335 if (option.starts_with("--oat-file=")) {
3336 oat_filename_ = option.substr(strlen("--oat-file=")).data();
3337 } else if (option.starts_with("--image=")) {
3338 image_location_ = option.substr(strlen("--image=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003339 } else if (option == "--no-dump:vmap") {
3340 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01003341 } else if (option =="--dump:code_info_stack_maps") {
3342 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003343 } else if (option == "--no-disassemble") {
3344 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003345 } else if (option =="--header-only") {
3346 dump_header_only_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003347 } else if (option.starts_with("--symbolize=")) {
3348 oat_filename_ = option.substr(strlen("--symbolize=")).data();
3349 symbolize_ = true;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003350 } else if (option.starts_with("--only-keep-debug")) {
3351 only_keep_debug_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003352 } else if (option.starts_with("--class-filter=")) {
3353 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08003354 } else if (option.starts_with("--method-filter=")) {
3355 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003356 } else if (option.starts_with("--list-classes")) {
3357 list_classes_ = true;
3358 } else if (option.starts_with("--list-methods")) {
3359 list_methods_ = true;
3360 } else if (option.starts_with("--export-dex-to=")) {
3361 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
3362 } else if (option.starts_with("--addr2instr=")) {
3363 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
3364 *error_msg = "Address conversion failed";
3365 return kParseError;
3366 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003367 } else if (option.starts_with("--app-image=")) {
3368 app_image_ = option.substr(strlen("--app-image=")).data();
3369 } else if (option.starts_with("--app-oat=")) {
3370 app_oat_ = option.substr(strlen("--app-oat=")).data();
Andreas Gampe9fded872016-09-25 16:08:35 -07003371 } else if (option.starts_with("--dump-imt=")) {
3372 imt_dump_ = option.substr(strlen("--dump-imt=")).data();
3373 } else if (option == "--dump-imt-stats") {
3374 imt_stat_dump_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08003375 } else {
3376 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003377 }
3378
Igor Murashkin37743352014-11-13 14:38:00 -08003379 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003380 }
3381
Igor Murashkin37743352014-11-13 14:38:00 -08003382 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
3383 // Infer boot image location from the image location if possible.
3384 if (boot_image_location_ == nullptr) {
3385 boot_image_location_ = image_location_;
3386 }
3387
3388 // Perform the parent checks.
3389 ParseStatus parent_checks = Base::ParseChecks(error_msg);
3390 if (parent_checks != kParseOk) {
3391 return parent_checks;
3392 }
3393
3394 // Perform our own checks.
3395 if (image_location_ == nullptr && oat_filename_ == nullptr) {
3396 *error_msg = "Either --image or --oat-file must be specified";
3397 return kParseError;
3398 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
3399 *error_msg = "Either --image or --oat-file must be specified but not both";
3400 return kParseError;
3401 }
3402
3403 return kParseOk;
3404 }
3405
3406 virtual std::string GetUsage() const {
3407 std::string usage;
3408
3409 usage +=
3410 "Usage: oatdump [options] ...\n"
3411 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
3412 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
3413 "\n"
3414 // Either oat-file or image is required.
3415 " --oat-file=<file.oat>: specifies an input oat filename.\n"
3416 " Example: --oat-file=/system/framework/boot.oat\n"
3417 "\n"
3418 " --image=<file.art>: specifies an input image location.\n"
3419 " Example: --image=/system/framework/boot.art\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003420 "\n"
3421 " --app-image=<file.art>: specifies an input app image. Must also have a specified\n"
3422 " boot image and app oat file.\n"
3423 " Example: --app-image=app.art\n"
3424 "\n"
3425 " --app-oat=<file.odex>: specifies an input app oat.\n"
3426 " Example: --app-oat=app.odex\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003427 "\n";
3428
3429 usage += Base::GetUsage();
3430
3431 usage += // Optional.
Igor Murashkin37743352014-11-13 14:38:00 -08003432 " --no-dump:vmap may be used to disable vmap dumping.\n"
3433 " Example: --no-dump:vmap\n"
3434 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01003435 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
3436 " Example: --dump:code_info_stack_maps\n"
3437 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003438 " --no-disassemble may be used to disable disassembly.\n"
3439 " Example: --no-disassemble\n"
3440 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00003441 " --header-only may be used to print only the oat header.\n"
3442 " Example: --header-only\n"
3443 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003444 " --list-classes may be used to list target file classes (can be used with filters).\n"
3445 " Example: --list-classes\n"
3446 " Example: --list-classes --class-filter=com.example.foo\n"
3447 "\n"
3448 " --list-methods may be used to list target file methods (can be used with filters).\n"
3449 " Example: --list-methods\n"
3450 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
3451 "\n"
3452 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
3453 " Example: --symbolize=/system/framework/boot.oat\n"
3454 "\n"
David Srbecky2fdd03c2016-03-10 15:32:37 +00003455 " --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
3456 " .rodata and .text sections are omitted in the output file to save space.\n"
3457 " Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
3458 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003459 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
3460 " Example: --class-filter=com.example.foo\n"
3461 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08003462 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
3463 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003464 "\n"
3465 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
3466 " Example: --export-dex-to=/data/local/tmp\n"
3467 "\n"
3468 " --addr2instr=<address>: output matching method disassembled code from relative\n"
3469 " address (e.g. PC from crash dump)\n"
3470 " Example: --addr2instr=0x00001a3b\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003471 "\n"
3472 " --dump-imt=<file.txt>: output IMT collisions (if any) for the given receiver\n"
3473 " types and interface methods in the given file. The file\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003474 " is read line-wise, where each line should either be a class\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003475 " name or descriptor, or a class name/descriptor and a prefix\n"
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003476 " of a complete method name (separated by a whitespace).\n"
Andreas Gampe9fded872016-09-25 16:08:35 -07003477 " Example: --dump-imt=imt.txt\n"
3478 "\n"
3479 " --dump-imt-stats: output IMT statistics for the given boot image\n"
3480 " Example: --dump-imt-stats"
Igor Murashkin37743352014-11-13 14:38:00 -08003481 "\n";
3482
3483 return usage;
3484 }
3485
3486 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07003487 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003488 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00003489 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07003490 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003491 std::string elf_filename_prefix_;
Andreas Gampe9fded872016-09-25 16:08:35 -07003492 std::string imt_dump_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003493 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01003494 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003495 bool disassemble_code_ = true;
3496 bool symbolize_ = false;
David Srbecky2fdd03c2016-03-10 15:32:37 +00003497 bool only_keep_debug_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003498 bool list_classes_ = false;
3499 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00003500 bool dump_header_only_ = false;
Andreas Gampe9fded872016-09-25 16:08:35 -07003501 bool imt_stat_dump_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003502 uint32_t addr2instr_ = 0;
3503 const char* export_dex_location_ = nullptr;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003504 const char* app_image_ = nullptr;
3505 const char* app_oat_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003506};
3507
Igor Murashkin37743352014-11-13 14:38:00 -08003508struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
3509 virtual bool NeedsRuntime() OVERRIDE {
3510 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003511
Igor Murashkin37743352014-11-13 14:38:00 -08003512 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
3513 bool absolute_addresses = (args_->oat_filename_ == nullptr);
3514
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003515 oat_dumper_options_.reset(new OatDumperOptions(
Igor Murashkin37743352014-11-13 14:38:00 -08003516 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01003517 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08003518 args_->disassemble_code_,
3519 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003520 args_->class_filter_,
3521 args_->method_filter_,
3522 args_->list_classes_,
3523 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00003524 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003525 args_->export_dex_location_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003526 args_->app_image_,
3527 args_->app_oat_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003528 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08003529
Andreas Gampe9fded872016-09-25 16:08:35 -07003530 return (args_->boot_image_location_ != nullptr ||
3531 args_->image_location_ != nullptr ||
3532 !args_->imt_dump_.empty()) &&
Igor Murashkin37743352014-11-13 14:38:00 -08003533 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003534 }
3535
Igor Murashkin37743352014-11-13 14:38:00 -08003536 virtual bool ExecuteWithoutRuntime() OVERRIDE {
3537 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08003538 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07003539
Mathieu Chartierd424d082014-10-15 10:31:46 -07003540 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08003541
Andreas Gampec24f3992014-12-17 20:40:11 -08003542 if (args_->symbolize_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00003543 // ELF has special kind of section called SHT_NOBITS which allows us to create
3544 // sections which exist but their data is omitted from the ELF file to save space.
3545 // This is what "strip --only-keep-debug" does when it creates separate ELF file
3546 // with only debug data. We use it in similar way to exclude .rodata and .text.
3547 bool no_bits = args_->only_keep_debug_;
3548 return SymbolizeOat(args_->oat_filename_, args_->output_name_, no_bits) == EXIT_SUCCESS;
Andreas Gampec24f3992014-12-17 20:40:11 -08003549 } else {
3550 return DumpOat(nullptr,
3551 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003552 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08003553 args_->os_) == EXIT_SUCCESS;
3554 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07003555 }
3556
Igor Murashkin37743352014-11-13 14:38:00 -08003557 virtual bool ExecuteWithRuntime(Runtime* runtime) {
3558 CHECK(args_ != nullptr);
3559
Andreas Gampeebfc1ac2016-09-29 19:50:27 -07003560 if (!args_->imt_dump_.empty() || args_->imt_stat_dump_) {
3561 return IMTDumper::Dump(runtime,
3562 args_->imt_dump_,
3563 args_->imt_stat_dump_,
3564 args_->oat_filename_);
Andreas Gampe9fded872016-09-25 16:08:35 -07003565 }
3566
Igor Murashkin37743352014-11-13 14:38:00 -08003567 if (args_->oat_filename_ != nullptr) {
3568 return DumpOat(runtime,
3569 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08003570 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08003571 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003572 }
Igor Murashkin37743352014-11-13 14:38:00 -08003573
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08003574 return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07003575 }
3576
Igor Murashkin37743352014-11-13 14:38:00 -08003577 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
3578};
Andreas Gampe00b25f32014-09-17 21:49:05 -07003579
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003580} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07003581
3582int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08003583 art::OatdumpMain main;
3584 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07003585}