blob: 79fde5e366370a0bf93eddb818a31af89b3e3a5c [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>
Brian Carlstrom78128a62011-09-15 17:21:19 -070026#include <vector>
27
Ian Rogersd582fa42014-11-05 23:46:43 -080028#include "arch/instruction_set_features.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070029#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070030#include "art_method-inl.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000031#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080032#include "base/unix_file/fd_file.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070033#include "class_linker.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "class_linker-inl.h"
David Srbecky5d950762016-03-07 20:47:29 +000035#include "debug/elf_debug_writer.h"
36#include "debug/method_debug_info.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070037#include "dex_file-inl.h"
Elliott Hughese3c845c2012-02-28 17:23:01 -080038#include "dex_instruction.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080039#include "disassembler.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070040#include "elf_builder.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080041#include "gc_map.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070042#include "gc/space/image_space.h"
43#include "gc/space/large_object_space.h"
44#include "gc/space/space-inl.h"
Mathieu Chartier4a26f172016-01-26 14:26:18 -080045#include "image-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080046#include "indenter.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000047#include "linker/buffered_output_stream.h"
48#include "linker/file_output_stream.h"
Ian Rogers1809a722013-08-09 22:05:32 -070049#include "mapping_table.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080050#include "mirror/array-inl.h"
51#include "mirror/class-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010052#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080053#include "mirror/object-inl.h"
54#include "mirror/object_array-inl.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080055#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010056#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070057#include "oat_file_manager.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080058#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070059#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070060#include "scoped_thread_state_change.h"
Nicolas Geoffray6bc43742015-10-12 18:11:10 +010061#include "stack_map.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070062#include "ScopedLocalRef.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080063#include "thread_list.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080064#include "verifier/dex_gc_map.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080065#include "verifier/method_verifier.h"
Ian Rogers1809a722013-08-09 22:05:32 -070066#include "vmap_table.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070067#include "well_known_classes.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070068
Igor Murashkin37743352014-11-13 14:38:00 -080069#include <sys/stat.h>
70#include "cmdline.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070071
Igor Murashkin37743352014-11-13 14:38:00 -080072namespace art {
Brian Carlstrom78128a62011-09-15 17:21:19 -070073
Mathieu Chartiere401d142015-04-22 13:56:20 -070074const char* image_methods_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -080075 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -070076 "kImtConflictMethod",
Mathieu Chartier2d2621a2014-10-23 16:48:06 -070077 "kImtUnimplementedMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070078 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -070079 "kRefsOnlySaveMethod",
80 "kRefsAndArgsSaveMethod",
Mathieu Chartiere401d142015-04-22 13:56:20 -070081};
82
83const char* image_roots_descriptions_[] = {
Brian Carlstrom58ae9412011-10-04 00:56:06 -070084 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070085 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070086};
87
Mathieu Chartierac8f4392015-08-27 13:54:20 -070088// Map is so that we don't allocate multiple dex files for the same OatDexFile.
89static std::map<const OatFile::OatDexFile*,
90 std::unique_ptr<const DexFile>> opened_dex_files;
91
92const DexFile* OpenDexFile(const OatFile::OatDexFile* oat_dex_file, std::string* error_msg) {
93 DCHECK(oat_dex_file != nullptr);
94 auto it = opened_dex_files.find(oat_dex_file);
95 if (it != opened_dex_files.end()) {
96 return it->second.get();
97 }
98 const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
99 opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
100 return ret;
101}
102
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800103template <typename ElfTypes>
David Srbeckybc90fd02015-04-22 19:40:27 +0100104class OatSymbolizer FINAL {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700105 public:
David Srbecky2fdd03c2016-03-10 15:32:37 +0000106 OatSymbolizer(const OatFile* oat_file, const std::string& output_name, bool no_bits) :
107 oat_file_(oat_file),
108 builder_(nullptr),
109 output_name_(output_name.empty() ? "symbolized.oat" : output_name),
110 no_bits_(no_bits) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700111 }
112
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700113 bool Symbolize() {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000114 const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
David Srbecky5d811202016-03-08 13:21:22 +0000115 const InstructionSetFeatures* features = InstructionSetFeatures::FromBitmap(
116 isa, oat_file_->GetOatHeader().GetInstructionSetFeaturesBitmap());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000117
118 File* elf_file = OS::CreateEmptyFile(output_name_.c_str());
119 std::unique_ptr<BufferedOutputStream> output_stream(
Vladimir Marko10c13562015-11-25 14:33:36 +0000120 MakeUnique<BufferedOutputStream>(MakeUnique<FileOutputStream>(elf_file)));
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800121 builder_.reset(new ElfBuilder<ElfTypes>(isa, features, output_stream.get()));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000122
123 builder_->Start();
124
125 auto* rodata = builder_->GetRoData();
126 auto* text = builder_->GetText();
127 auto* bss = builder_->GetBss();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000128
David Srbecky6d8c8f02015-10-26 10:57:09 +0000129 const uint8_t* rodata_begin = oat_file_->Begin();
130 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
David Srbecky2fdd03c2016-03-10 15:32:37 +0000131 if (no_bits_) {
132 rodata->WriteNoBitsSection(rodata_size);
133 } else {
134 rodata->Start();
135 rodata->WriteFully(rodata_begin, rodata_size);
136 rodata->End();
137 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000138
David Srbecky6d8c8f02015-10-26 10:57:09 +0000139 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
140 const size_t text_size = oat_file_->End() - text_begin;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000141 if (no_bits_) {
142 text->WriteNoBitsSection(text_size);
143 } else {
144 text->Start();
145 text->WriteFully(text_begin, text_size);
146 text->End();
147 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000148
149 if (oat_file_->BssSize() != 0) {
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000150 bss->WriteNoBitsSection(oat_file_->BssSize());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000151 }
152
Douglas Leung316a2182015-09-17 15:26:25 -0700153 if (isa == kMips || isa == kMips64) {
154 builder_->WriteMIPSabiflagsSection();
155 }
Vladimir Marko944da602016-02-19 12:27:55 +0000156 builder_->PrepareDynamicSection(
157 elf_file->GetPath(), rodata_size, text_size, oat_file_->BssSize());
158 builder_->WriteDynamicSection();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700159
David Srbecky5d950762016-03-07 20:47:29 +0000160 Walk();
161 for (const auto& trampoline : debug::MakeTrampolineInfos(oat_file_->GetOatHeader())) {
162 method_debug_infos_.push_back(trampoline);
163 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700164
David Srbecky5d950762016-03-07 20:47:29 +0000165 debug::WriteDebugInfo(builder_.get(),
166 ArrayRef<const debug::MethodDebugInfo>(method_debug_infos_),
167 dwarf::DW_DEBUG_FRAME_FORMAT,
168 true /* write_oat_patches */);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700169
David Srbecky6d8c8f02015-10-26 10:57:09 +0000170 builder_->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700171
Vladimir Marko10c13562015-11-25 14:33:36 +0000172 return builder_->Good();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700173 }
174
David Srbecky5d950762016-03-07 20:47:29 +0000175 void Walk() {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700176 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
177 for (size_t i = 0; i < oat_dex_files.size(); i++) {
178 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700179 CHECK(oat_dex_file != nullptr);
David Srbecky5d950762016-03-07 20:47:29 +0000180 WalkOatDexFile(oat_dex_file);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700181 }
182 }
183
David Srbecky5d950762016-03-07 20:47:29 +0000184 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700185 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700186 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
187 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700188 return;
189 }
190 for (size_t class_def_index = 0;
191 class_def_index < dex_file->NumClassDefs();
192 class_def_index++) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700193 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
194 OatClassType type = oat_class.GetType();
195 switch (type) {
196 case kOatClassAllCompiled:
197 case kOatClassSomeCompiled:
David Srbecky5d950762016-03-07 20:47:29 +0000198 WalkOatClass(oat_class, *dex_file, class_def_index);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700199 break;
200
201 case kOatClassNoneCompiled:
202 case kOatClassMax:
203 // Ignore.
204 break;
205 }
206 }
207 }
208
David Srbecky5d950762016-03-07 20:47:29 +0000209 void WalkOatClass(const OatFile::OatClass& oat_class,
210 const DexFile& dex_file,
211 uint32_t class_def_index) {
212 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700213 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700214 if (class_data == nullptr) { // empty class such as a marker interface?
215 return;
216 }
217 // Note: even if this is an interface or a native class, we still have to walk it, as there
218 // might be a static initializer.
219 ClassDataItemIterator it(dex_file, class_data);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700220 uint32_t class_method_idx = 0;
David Srbecky5d950762016-03-07 20:47:29 +0000221 for (; it.HasNextStaticField(); it.Next()) { /* skip */ }
222 for (; it.HasNextInstanceField(); it.Next()) { /* skip */ }
223 for (; it.HasNextDirectMethod() || it.HasNextVirtualMethod(); it.Next()) {
224 WalkOatMethod(oat_class.GetOatMethod(class_method_idx++),
225 dex_file,
226 class_def_index,
227 it.GetMemberIndex(),
228 it.GetMethodCodeItem(),
229 it.GetMethodAccessFlags());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700230 }
231 DCHECK(!it.HasNext());
232 }
233
David Srbecky5d950762016-03-07 20:47:29 +0000234 void WalkOatMethod(const OatFile::OatMethod& oat_method,
235 const DexFile& dex_file,
236 uint32_t class_def_index,
237 uint32_t dex_method_index,
238 const DexFile::CodeItem* code_item,
239 uint32_t method_access_flags) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700240 if ((method_access_flags & kAccAbstract) != 0) {
241 // Abstract method, no code.
242 return;
243 }
David Srbecky5d950762016-03-07 20:47:29 +0000244 const OatHeader& oat_header = oat_file_->GetOatHeader();
245 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
246 if (method_header == nullptr || method_header->GetCodeSize() == 0) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700247 // No code.
248 return;
249 }
250
David Srbecky5d950762016-03-07 20:47:29 +0000251 debug::MethodDebugInfo info = debug::MethodDebugInfo();
252 info.trampoline_name = nullptr;
253 info.dex_file = &dex_file;
254 info.class_def_index = class_def_index;
255 info.dex_method_index = dex_method_index;
256 info.access_flags = method_access_flags;
257 info.code_item = code_item;
258 info.isa = oat_header.GetInstructionSet();
259 info.deduped = !seen_offsets_.insert(oat_method.GetCodeOffset()).second;
260 info.is_native_debuggable = oat_header.IsNativeDebuggable();
261 info.is_optimized = method_header->IsOptimized();
262 info.is_code_address_text_relative = true;
263 info.code_address = oat_method.GetCodeOffset() - oat_header.GetExecutableOffset();
264 info.code_size = method_header->GetCodeSize();
265 info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
266 info.code_info = info.is_optimized ? method_header->GetOptimizedCodeInfoPtr() : nullptr;
267 info.cfi = ArrayRef<uint8_t>();
268 method_debug_infos_.push_back(info);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700269 }
270
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700271 private:
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700272 const OatFile* oat_file_;
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800273 std::unique_ptr<ElfBuilder<ElfTypes> > builder_;
David Srbecky5d950762016-03-07 20:47:29 +0000274 std::vector<debug::MethodDebugInfo> method_debug_infos_;
275 std::unordered_set<uint32_t> seen_offsets_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700276 const std::string output_name_;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000277 bool no_bits_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700278};
279
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700280class OatDumperOptions {
281 public:
282 OatDumperOptions(bool dump_raw_mapping_table,
283 bool dump_raw_gc_map,
284 bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100285 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700286 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700287 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800288 const char* class_filter,
289 const char* method_filter,
290 bool list_classes,
291 bool list_methods,
David Brazdilc03d7b62016-03-02 12:18:03 +0000292 bool dump_header_only,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800293 const char* export_dex_location,
294 uint32_t addr2instr)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700295 : dump_raw_mapping_table_(dump_raw_mapping_table),
296 dump_raw_gc_map_(dump_raw_gc_map),
297 dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100298 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700299 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700300 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800301 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000302 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800303 list_classes_(list_classes),
304 list_methods_(list_methods),
David Brazdilc03d7b62016-03-02 12:18:03 +0000305 dump_header_only_(dump_header_only),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800306 export_dex_location_(export_dex_location),
307 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800308 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700309
310 const bool dump_raw_mapping_table_;
311 const bool dump_raw_gc_map_;
312 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100313 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700314 const bool disassemble_code_;
315 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800316 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000317 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800318 const bool list_classes_;
319 const bool list_methods_;
David Brazdilc03d7b62016-03-02 12:18:03 +0000320 const bool dump_header_only_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800321 const char* const export_dex_location_;
322 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700323 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700324};
325
Elliott Hughese3c845c2012-02-28 17:23:01 -0800326class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700327 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100328 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000329 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800330 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700331 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800332 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800333 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
334 disassembler_(Disassembler::Create(instruction_set_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800335 new DisassemblerOptions(options_.absolute_addresses_,
Alexandre Ramesa37d9252014-10-27 11:28:14 +0000336 oat_file.Begin(),
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100337 true /* can_read_literals_ */))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800338 CHECK(options_.class_loader_ != nullptr);
339 CHECK(options_.class_filter_ != nullptr);
340 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800341 AddAllOffsets();
342 }
343
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700344 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700345 delete disassembler_;
346 }
347
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800348 InstructionSet GetInstructionSet() {
349 return instruction_set_;
350 }
351
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700352 bool Dump(std::ostream& os) {
353 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800354 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700355
356 os << "MAGIC:\n";
357 os << oat_header.GetMagic() << "\n\n";
358
Jeff Hao4b07a6e2016-01-15 12:50:44 -0800359 os << "LOCATION:\n";
360 os << oat_file_.GetLocation() << "\n\n";
361
Brian Carlstromaded5f72011-10-07 17:15:04 -0700362 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800363 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700364
Elliott Hughesa72ec822012-03-05 17:12:22 -0800365 os << "INSTRUCTION SET:\n";
366 os << oat_header.GetInstructionSet() << "\n\n";
367
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700368 {
369 std::unique_ptr<const InstructionSetFeatures> features(
370 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
371 oat_header.GetInstructionSetFeaturesBitmap()));
372 os << "INSTRUCTION SET FEATURES:\n";
373 os << features->GetFeatureString() << "\n\n";
374 }
Dave Allison70202782013-10-22 17:52:19 -0700375
Brian Carlstromaded5f72011-10-07 17:15:04 -0700376 os << "DEX FILE COUNT:\n";
377 os << oat_header.GetDexFileCount() << "\n\n";
378
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800379#define DUMP_OAT_HEADER_OFFSET(label, offset) \
380 os << label " OFFSET:\n"; \
381 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800382 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800383 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
384 } \
385 os << StringPrintf("\n\n");
386
387 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
388 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
389 GetInterpreterToInterpreterBridgeOffset);
390 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
391 GetInterpreterToCompiledCodeBridgeOffset);
392 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
393 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800394 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
395 GetQuickGenericJniTrampolineOffset);
396 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
397 GetQuickImtConflictTrampolineOffset);
398 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
399 GetQuickResolutionTrampolineOffset);
400 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
401 GetQuickToInterpreterBridgeOffset);
402#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700403
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700404 os << "IMAGE PATCH DELTA:\n";
405 os << StringPrintf("%d (0x%08x)\n\n",
406 oat_header.GetImagePatchDelta(),
407 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700408
Brian Carlstrom28db0122012-10-18 16:20:41 -0700409 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
410 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
411
412 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800413 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700414
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700415 // Print the key-value store.
416 {
417 os << "KEY VALUE STORE:\n";
418 size_t index = 0;
419 const char* key;
420 const char* value;
421 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
422 os << key << " = " << value << "\n";
423 index++;
424 }
425 os << "\n";
426 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700427
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800428 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700429 os << "BEGIN:\n";
430 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700431
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700432 os << "END:\n";
433 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
434 }
435
436 os << "SIZE:\n";
437 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700438
439 os << std::flush;
440
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800441 // If set, adjust relative address to be searched
442 if (options_.addr2instr_ != 0) {
443 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
444 os << "SEARCH ADDRESS (executable offset + input):\n";
445 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
446 }
447
David Brazdilc03d7b62016-03-02 12:18:03 +0000448 if (!options_.dump_header_only_) {
449 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
450 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
451 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800452
David Brazdilc03d7b62016-03-02 12:18:03 +0000453 // If file export selected skip file analysis
454 if (options_.export_dex_location_) {
455 if (!ExportDexFile(os, *oat_dex_file)) {
456 success = false;
457 }
458 } else {
459 if (!DumpOatDexFile(os, *oat_dex_file)) {
460 success = false;
461 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800462 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700463 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700464 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000465
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700466 os << std::flush;
467 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700468 }
469
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800470 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700471 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
472 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800473 return 0; // Address not in oat file
474 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800475 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
476 reinterpret_cast<uintptr_t>(oat_file_.Begin());
477 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800478 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800479 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800480 return end_offset - begin_offset;
481 }
482
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800483 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700484 return oat_file_.GetOatHeader().GetInstructionSet();
485 }
486
Mathieu Chartier90443472015-07-16 20:32:27 -0700487 const void* GetQuickOatCode(ArtMethod* m) SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800488 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
489 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700490 CHECK(oat_dex_file != nullptr);
491 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700492 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
493 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700494 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
495 << "': " << error_msg;
496 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800497 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700498 const DexFile::ClassDef* class_def =
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800499 dex_file->FindClassDef(descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700500 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700501 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100502 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800503 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100504 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800505 }
506 }
507 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700508 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800509 }
510
Brian Carlstromaded5f72011-10-07 17:15:04 -0700511 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800512 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800513 // We don't know the length of the code for each method, but we need to know where to stop
514 // when disassembling. What we do know is that a region of code will be followed by some other
515 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
516 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800517 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
518 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700519 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700520 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700521 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
522 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700523 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
524 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800525 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800526 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800527 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800528 for (size_t class_def_index = 0;
529 class_def_index < dex_file->NumClassDefs();
530 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800531 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100532 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700533 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700534 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800535 ClassDataItemIterator it(*dex_file, class_data);
536 SkipAllFields(it);
537 uint32_t class_method_index = 0;
538 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100539 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800540 it.Next();
541 }
542 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100543 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800544 it.Next();
545 }
546 }
547 }
548 }
549
550 // If the last thing in the file is code for a method, there won't be an offset for the "next"
551 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
552 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800553 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800554 }
555
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700556 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
557 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
558 }
559
Elliott Hughese3c845c2012-02-28 17:23:01 -0800560 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800561 uint32_t code_offset = oat_method.GetCodeOffset();
562 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
563 code_offset &= ~0x1;
564 }
565 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800566 offsets_.insert(oat_method.GetMappingTableOffset());
567 offsets_.insert(oat_method.GetVmapTableOffset());
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800568 offsets_.insert(oat_method.GetGcMapOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800569 }
570
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700571 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
572 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800573 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700574 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800575 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800576 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700577
578 // Create the verifier early.
579
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700580 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700581 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
582 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700583 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700584 os << std::flush;
585 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700586 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100587
588 VariableIndentationOutputStream vios(&os);
589 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800590 for (size_t class_def_index = 0;
591 class_def_index < dex_file->NumClassDefs();
592 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700593 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
594 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800595
596 // TODO: Support regex
597 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
598 continue;
599 }
600
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700601 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100602 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700603 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
604 class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100605 << " (" << oat_class.GetStatus() << ")"
606 << " (" << oat_class.GetType() << ")\n";
607 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800608 if (options_.list_classes_) continue;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700609 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700610 success = false;
611 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800612 if (stop_analysis) {
613 os << std::flush;
614 return success;
615 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700616 }
617
618 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700619 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700620 }
621
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800622 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
623 std::string error_msg;
624 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
625
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700626 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800627 if (dex_file == nullptr) {
628 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
629 return false;
630 }
631 size_t fsize = oat_dex_file.FileSize();
632
633 // Some quick checks just in case
634 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
635 os << "Invalid dex file\n";
636 return false;
637 }
638
639 // Verify output directory exists
640 if (!OS::DirectoryExists(options_.export_dex_location_)) {
641 // TODO: Extend OS::DirectoryExists if symlink support is required
642 os << options_.export_dex_location_ << " output directory not found or symlink\n";
643 return false;
644 }
645
646 // Beautify path names
647 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
648 return false;
649 }
650
651 std::string dex_orig_name;
652 size_t dex_orig_pos = dex_file_location.rfind('/');
653 if (dex_orig_pos == std::string::npos)
654 dex_orig_name = dex_file_location;
655 else
656 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
657
658 // A more elegant approach to efficiently name user installed apps is welcome
659 if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
660 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
661 size_t apk_orig_pos = dex_file_location.rfind('/');
662 if (apk_orig_pos != std::string::npos) {
663 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
664 }
665 }
666
667 std::string out_dex_path(options_.export_dex_location_);
668 if (out_dex_path.back() != '/') {
669 out_dex_path.append("/");
670 }
671 out_dex_path.append(dex_orig_name);
672 out_dex_path.append("_export.dex");
673 if (out_dex_path.length() > PATH_MAX) {
674 return false;
675 }
676
677 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
678 if (file.get() == nullptr) {
679 os << "Failed to open output dex file " << out_dex_path;
680 return false;
681 }
682
683 if (!file->WriteFully(dex_file->Begin(), fsize)) {
684 os << "Failed to write dex file";
685 file->Erase();
686 return false;
687 }
688
689 if (file->FlushCloseOrErase() != 0) {
690 os << "Flush and close failed";
691 return false;
692 }
693
694 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
695 os << std::flush;
696
697 return true;
698 }
699
Elliott Hughese3c845c2012-02-28 17:23:01 -0800700 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700701 while (it.HasNextStaticField()) {
702 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700703 }
Ian Rogers0571d352011-11-03 19:51:38 -0700704 while (it.HasNextInstanceField()) {
705 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700706 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800707 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700708
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100709 bool DumpOatClass(VariableIndentationOutputStream* vios,
710 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800711 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700712 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800713 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -0700714 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700715 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100716 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700717 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800718 }
719 ClassDataItemIterator it(dex_file, class_data);
720 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700721 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700722 while (it.HasNextDirectMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100723 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700724 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800725 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700726 success = false;
727 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800728 if (addr_found) {
729 *stop_analysis = true;
730 return success;
731 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700732 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700733 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700734 }
Ian Rogers0571d352011-11-03 19:51:38 -0700735 while (it.HasNextVirtualMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100736 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700737 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800738 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700739 success = false;
740 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800741 if (addr_found) {
742 *stop_analysis = true;
743 return success;
744 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700745 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700746 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700747 }
Ian Rogers0571d352011-11-03 19:51:38 -0700748 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100749 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700750 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700751 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800752
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700753 static constexpr uint32_t kPrologueBytes = 16;
754
755 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
756 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
757
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100758 bool DumpOatMethod(VariableIndentationOutputStream* vios,
759 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700760 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700761 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800762 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800763 uint32_t method_access_flags, bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700764 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800765
766 // TODO: Support regex
767 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
768 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000769 return success;
770 }
771
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800772 std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100773 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
774 class_method_index, pretty_method.c_str(),
775 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800776 if (options_.list_methods_) return success;
777
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800778 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
779 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
780 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
781 uint32_t code_offset = oat_method.GetCodeOffset();
782 uint32_t code_size = oat_method.GetQuickCodeSize();
783 if (resolved_addr2instr_ != 0) {
784 if (resolved_addr2instr_ > code_offset + code_size) {
785 return success;
786 } else {
787 *addr_found = true; // stop analyzing file at next iteration
788 }
789 }
790
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100791 // Everything below is indented at least once.
792 ScopedIndentation indent1(vios);
793
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800794 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100795 vios->Stream() << "DEX CODE:\n";
796 ScopedIndentation indent2(vios);
797 DumpDexCode(vios->Stream(), dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700798 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700799
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700800 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700801 std::unique_ptr<verifier::MethodVerifier> verifier;
802 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700803 // We need to have the handle scope stay live until after the verifier since the verifier has
804 // a handle to the dex cache from hs.
805 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100806 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
807 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700808 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100809 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700810 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -0700811 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800812 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100813 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800814 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100815 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100816 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100817 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700818 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100819 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700820 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
821 oat_method_offsets_offset, oat_file_.Size());
822 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100823 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700824 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800825 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700826
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100827 ScopedIndentation indent2(vios);
828 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700829 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
830 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100831 vios->Stream() << StringPrintf("WARNING: "
832 "code offset 0x%08x is past end of file 0x%08zx.\n",
833 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700834 success = false;
835 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100836 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700837
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100838 vios->Stream() << "gc_map: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800839 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100840 vios->Stream() << StringPrintf("%p ", oat_method.GetGcMap());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700841 }
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800842 uint32_t gc_map_offset = oat_method.GetGcMapOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100843 vios->Stream() << StringPrintf("(offset=0x%08x)\n", gc_map_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700844 if (gc_map_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100845 vios->Stream() << StringPrintf("WARNING: "
846 "gc map table offset 0x%08x is past end of file 0x%08zx.\n",
847 gc_map_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700848 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800849 } else if (options_.dump_raw_gc_map_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100850 ScopedIndentation indent3(vios);
851 DumpGcMap(vios->Stream(), oat_method, code_item);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800852 }
853 }
854 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100855 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700856 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
857 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700858
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800859 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100860 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700861 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100862 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700863 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100864 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700865 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
866 method_header_offset, oat_file_.Size());
867 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100868 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700869 return false;
870 }
871
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100872 ScopedIndentation indent2(vios);
873 vios->Stream() << "mapping_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800874 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100875 vios->Stream() << StringPrintf("%p ", oat_method.GetMappingTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700876 }
877 uint32_t mapping_table_offset = oat_method.GetMappingTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100878 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700879 if (mapping_table_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100880 vios->Stream() << StringPrintf("WARNING: "
881 "mapping table offset 0x%08x is past end of file 0x%08zx. "
882 "mapping table offset was loaded from offset 0x%08x.\n",
883 mapping_table_offset, oat_file_.Size(),
884 oat_method.GetMappingTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700885 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800886 } else if (options_.dump_raw_mapping_table_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100887 ScopedIndentation indent3(vios);
888 DumpMappingTable(vios, oat_method);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700889 }
890
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100891 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800892 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100893 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700894 }
895 uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100896 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700897 if (vmap_table_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100898 vios->Stream() << StringPrintf("WARNING: "
899 "vmap table offset 0x%08x is past end of file 0x%08zx. "
900 "vmap table offset was loaded from offset 0x%08x.\n",
901 vmap_table_offset, oat_file_.Size(),
902 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700903 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800904 } else if (options_.dump_vmap_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100905 DumpVmapData(vios, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700906 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800907 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700908 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100909 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700910
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100911 ScopedIndentation indent2(vios);
912 vios->Stream()
913 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
914 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
915 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
916 vios->Stream() << "\n";
917 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
918 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
919 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700920 }
921 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100922 // Based on spill masks from QuickMethodFrameInfo so placed
923 // after it is dumped, but useful for understanding quick
924 // code, so dumped here.
925 ScopedIndentation indent2(vios);
926 DumpVregLocations(vios->Stream(), oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700927 }
928 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100929 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700930 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
931 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100932 ScopedIndentation indent2(vios);
933 vios->Stream() << StringPrintf("WARNING: "
934 "code size offset 0x%08x is past end of file 0x%08zx.",
935 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700936 success = false;
937 } else {
938 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700939 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
940 uint64_t aligned_code_end = aligned_code_begin + code_size;
941
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800942 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100943 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700944 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100945 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
946 code_offset,
947 code_size_offset,
948 code_size,
949 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700950
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100951 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700952 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100953 vios->Stream() << StringPrintf("WARNING: "
954 "start of code at 0x%08x is past end of file 0x%08zx.",
955 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700956 success = false;
957 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100958 vios->Stream() << StringPrintf(
959 "WARNING: "
960 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
961 "code size is 0x%08x loaded from offset 0x%08x.\n",
962 aligned_code_end, oat_file_.Size(),
963 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700964 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800965 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700966 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100967 DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700968 }
969 }
970 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100971 vios->Stream() << StringPrintf(
972 "WARNING: "
973 "code size %d is bigger than max expected threshold of %d. "
974 "code size is 0x%08x loaded from offset 0x%08x.\n",
975 code_size, kMaxCodeSize,
976 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700977 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800978 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700979 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100980 DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700981 }
982 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800983 } else if (options_.disassemble_code_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100984 DumpCode(vios, verifier.get(), oat_method, code_item, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700985 }
986 }
987 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100988 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700989 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700990 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800991
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800992 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
993 if (spill_mask == 0) {
994 return;
995 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800996 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800997 for (size_t i = 0; i < 32; i++) {
998 if ((spill_mask & (1 << i)) != 0) {
999 if (is_float) {
1000 os << "fr" << i;
1001 } else {
1002 os << "r" << i;
1003 }
1004 spill_mask ^= 1 << i; // clear bit
1005 if (spill_mask != 0) {
1006 os << ", ";
1007 } else {
1008 break;
1009 }
1010 }
1011 }
1012 os << ")";
1013 }
1014
Roland Levillain442b46a2015-02-18 16:54:21 +00001015 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001016 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001017 const OatFile::OatMethod& oat_method,
1018 const DexFile::CodeItem* code_item) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001019 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1020 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001021 const void* raw_code_info = oat_method.GetVmapTable();
1022 if (raw_code_info != nullptr) {
1023 CodeInfo code_info(raw_code_info);
1024 DCHECK(code_item != nullptr);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001025 ScopedIndentation indent1(vios);
1026 DumpCodeInfo(vios, code_info, oat_method, *code_item);
Roland Levillain442b46a2015-02-18 16:54:21 +00001027 }
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001028 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
1029 // We don't encode the size in the table, so just emit that we have quickened
1030 // information.
1031 ScopedIndentation indent(vios);
1032 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001033 } else {
1034 // Otherwise, display the vmap table.
1035 const uint8_t* raw_table = oat_method.GetVmapTable();
1036 if (raw_table != nullptr) {
1037 VmapTable vmap_table(raw_table);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001038 DumpVmapTable(vios->Stream(), oat_method, vmap_table);
Roland Levillain442b46a2015-02-18 16:54:21 +00001039 }
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001040 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001041 }
1042
1043 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001044 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001045 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001046 const OatFile::OatMethod& oat_method,
Roland Levillain442b46a2015-02-18 16:54:21 +00001047 const DexFile::CodeItem& code_item) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001048 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001049 oat_method.GetCodeOffset(),
1050 code_item.registers_size_,
1051 options_.dump_code_info_stack_maps_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001052 }
1053
Roland Levillain442b46a2015-02-18 16:54:21 +00001054 // Display a vmap table.
1055 void DumpVmapTable(std::ostream& os,
1056 const OatFile::OatMethod& oat_method,
1057 const VmapTable& vmap_table) {
1058 bool first = true;
1059 bool processing_fp = false;
1060 uint32_t spill_mask = oat_method.GetCoreSpillMask();
1061 for (size_t i = 0; i < vmap_table.Size(); i++) {
1062 uint16_t dex_reg = vmap_table[i];
1063 uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
1064 processing_fp ? kFloatVReg : kIntVReg);
1065 os << (first ? "v" : ", v") << dex_reg;
1066 if (!processing_fp) {
1067 os << "/r" << cpu_reg;
1068 } else {
1069 os << "/fr" << cpu_reg;
1070 }
1071 first = false;
1072 if (!processing_fp && dex_reg == 0xFFFF) {
1073 processing_fp = true;
1074 spill_mask = oat_method.GetFpSpillMask();
1075 }
1076 }
1077 os << "\n";
1078 }
1079
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001080 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1081 const DexFile::CodeItem* code_item) {
1082 if (code_item != nullptr) {
1083 size_t num_locals_ins = code_item->registers_size_;
1084 size_t num_ins = code_item->ins_size_;
1085 size_t num_locals = num_locals_ins - num_ins;
1086 size_t num_outs = code_item->outs_size_;
1087
1088 os << "vr_stack_locations:";
1089 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1090 // For readability, delimit the different kinds of VRs.
1091 if (reg == num_locals_ins) {
1092 os << "\n\tmethod*:";
1093 } else if (reg == num_locals && num_ins > 0) {
1094 os << "\n\tins:";
1095 } else if (reg == 0 && num_locals > 0) {
1096 os << "\n\tlocals:";
1097 }
1098
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001099 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1100 code_item,
1101 oat_method.GetCoreSpillMask(),
1102 oat_method.GetFpSpillMask(),
1103 oat_method.GetFrameSizeInBytes(),
1104 reg,
1105 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001106 os << " v" << reg << "[sp + #" << offset << "]";
1107 }
1108
1109 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1110 if (out_reg == 0) {
1111 os << "\n\touts:";
1112 }
1113
1114 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1115 os << " v" << out_reg << "[sp + #" << offset << "]";
1116 }
1117
1118 os << "\n";
1119 }
1120 }
1121
Ian Rogersb23a7722012-10-09 16:54:26 -07001122 void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001123 const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
Ian Rogers1809a722013-08-09 22:05:32 -07001124 const uint8_t* raw_table = oat_method.GetVmapTable();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001125 if (raw_table != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001126 const VmapTable vmap_table(raw_table);
1127 uint32_t vmap_offset;
Ian Rogers1809a722013-08-09 22:05:32 -07001128 if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001129 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
1130 uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
1131 : oat_method.GetCoreSpillMask();
1132 os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
Ian Rogersb23a7722012-10-09 16:54:26 -07001133 } else {
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001134 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1135 code_item,
1136 oat_method.GetCoreSpillMask(),
1137 oat_method.GetFpSpillMask(),
1138 oat_method.GetFrameSizeInBytes(),
1139 reg,
1140 GetInstructionSet());
Ian Rogersb23a7722012-10-09 16:54:26 -07001141 os << "[sp + #" << offset << "]";
1142 }
1143 }
1144 }
1145
Ian Rogersef7d42f2014-01-06 12:55:46 -08001146 void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
1147 const DexFile::CodeItem* code_item,
1148 size_t num_regs, const uint8_t* reg_bitmap) {
1149 bool first = true;
1150 for (size_t reg = 0; reg < num_regs; reg++) {
1151 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1152 if (first) {
1153 os << " v" << reg << " (";
1154 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1155 os << ")";
1156 first = false;
1157 } else {
1158 os << ", v" << reg << " (";
1159 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1160 os << ")";
1161 }
1162 }
1163 }
1164 if (first) {
1165 os << "No registers in GC map\n";
1166 } else {
1167 os << "\n";
1168 }
1169 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001170 void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
1171 const DexFile::CodeItem* code_item) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001172 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Ian Rogersef7d42f2014-01-06 12:55:46 -08001173 if (gc_map_raw == nullptr) {
1174 return; // No GC map.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001175 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001176 const void* quick_code = oat_method.GetQuickCode();
Elliott Hughes956af0f2014-12-11 14:34:28 -08001177 NativePcOffsetToReferenceMap map(gc_map_raw);
1178 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1179 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
1180 map.GetNativePcOffset(entry);
1181 os << StringPrintf("%p", native_pc);
1182 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001183 }
1184 }
1185
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001186 void DumpMappingTable(VariableIndentationOutputStream* vios,
1187 const OatFile::OatMethod& oat_method) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001188 const void* quick_code = oat_method.GetQuickCode();
1189 if (quick_code == nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -08001190 return;
1191 }
Ian Rogers1809a722013-08-09 22:05:32 -07001192 MappingTable table(oat_method.GetMappingTable());
1193 if (table.TotalSize() != 0) {
Ian Rogers1809a722013-08-09 22:05:32 -07001194 if (table.PcToDexSize() != 0) {
1195 typedef MappingTable::PcToDexIterator It;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001196 vios->Stream() << "suspend point mappings {\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001197 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001198 ScopedIndentation indent1(vios);
1199 vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001200 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001201 vios->Stream() << "}\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001202 }
1203 if (table.DexToPcSize() != 0) {
1204 typedef MappingTable::DexToPcIterator It;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001205 vios->Stream() << "catch entry mappings {\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001206 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001207 ScopedIndentation indent1(vios);
1208 vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001209 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001210 vios->Stream() << "}\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001211 }
1212 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001213 }
1214
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001215 uint32_t DumpInformationAtOffset(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001216 const OatFile::OatMethod& oat_method,
1217 const DexFile::CodeItem* code_item,
1218 size_t offset,
1219 bool suspend_point_mapping) {
1220 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1221 if (suspend_point_mapping) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001222 ScopedIndentation indent1(vios);
1223 DumpDexRegisterMapAtOffset(vios, oat_method, code_item, offset);
Roland Levillainf2650d12015-05-28 14:53:28 +01001224 }
1225 // The return value is not used in the case of a method compiled
1226 // with the optimizing compiler.
1227 return DexFile::kDexNoIndex;
1228 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001229 return DumpMappingAtOffset(vios->Stream(), oat_method, offset, suspend_point_mapping);
Roland Levillainf2650d12015-05-28 14:53:28 +01001230 }
1231 }
1232
Ian Rogers1809a722013-08-09 22:05:32 -07001233 uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1234 size_t offset, bool suspend_point_mapping) {
1235 MappingTable table(oat_method.GetMappingTable());
1236 if (suspend_point_mapping && table.PcToDexSize() > 0) {
1237 typedef MappingTable::PcToDexIterator It;
1238 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1239 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001240 os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001241 return cur.DexPc();
1242 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001243 }
Ian Rogers1809a722013-08-09 22:05:32 -07001244 } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
1245 typedef MappingTable::DexToPcIterator It;
1246 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1247 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001248 os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001249 return cur.DexPc();
Ian Rogersb23a7722012-10-09 16:54:26 -07001250 }
1251 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001252 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001253 return DexFile::kDexNoIndex;
Ian Rogersb23a7722012-10-09 16:54:26 -07001254 }
1255
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001256 void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1257 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001258 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001259 if (gc_map_raw != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001260 NativePcOffsetToReferenceMap map(gc_map_raw);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001261 if (map.HasEntry(native_pc_offset)) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001262 size_t num_regs = map.RegWidth() * 8;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001263 const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
Ian Rogersb23a7722012-10-09 16:54:26 -07001264 bool first = true;
1265 for (size_t reg = 0; reg < num_regs; reg++) {
1266 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1267 if (first) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001268 os << "GC map objects: v" << reg << " (";
1269 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001270 os << ")";
1271 first = false;
1272 } else {
1273 os << ", v" << reg << " (";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001274 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001275 os << ")";
1276 }
1277 }
1278 }
1279 if (!first) {
1280 os << "\n";
1281 }
1282 }
1283 }
1284 }
1285
Mathieu Chartier590fee92013-09-13 13:46:47 -07001286 void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
1287 const OatFile::OatMethod& oat_method,
1288 const DexFile::CodeItem* code_item, uint32_t dex_pc) {
1289 DCHECK(verifier != nullptr);
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001290 std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001291 bool first = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001292 for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
1293 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
1294 if (kind != kUndefined) {
1295 if (first) {
1296 os << "VRegs: v";
1297 first = false;
1298 } else {
1299 os << ", v";
1300 }
1301 os << reg << " (";
1302 switch (kind) {
1303 case kImpreciseConstant:
1304 os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
1305 DescribeVReg(os, oat_method, code_item, reg, kind);
1306 break;
1307 case kConstant:
1308 os << "Constant: " << kinds.at((reg * 2) + 1);
1309 break;
1310 default:
1311 DescribeVReg(os, oat_method, code_item, reg, kind);
1312 break;
1313 }
1314 os << ")";
1315 }
1316 }
1317 if (!first) {
1318 os << "\n";
1319 }
1320 }
1321
1322
Ian Rogersb23a7722012-10-09 16:54:26 -07001323 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001324 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001325 size_t i = 0;
1326 while (i < code_item->insns_size_in_code_units_) {
1327 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001328 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1329 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001330 i += instruction->SizeInCodeUnits();
1331 }
1332 }
1333 }
1334
Roland Levillainf2650d12015-05-28 14:53:28 +01001335 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1336 // the optimizing compiler?
1337 static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1338 const DexFile::CodeItem* code_item) {
1339 // If the native GC map is null and the Dex `code_item` is not
1340 // null, then this method has been compiled with the optimizing
1341 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001342 return oat_method.GetQuickCode() != nullptr &&
1343 oat_method.GetGcMap() == nullptr &&
1344 code_item != nullptr;
1345 }
1346
1347 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1348 // the dextodex compiler?
1349 static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
1350 const DexFile::CodeItem* code_item) {
1351 // If the quick code is null, the Dex `code_item` is not
1352 // null, and the vmap table is not null, then this method has been compiled
1353 // with the dextodex compiler.
1354 return oat_method.GetQuickCode() == nullptr &&
1355 oat_method.GetVmapTable() != nullptr &&
1356 code_item != nullptr;
Roland Levillainf2650d12015-05-28 14:53:28 +01001357 }
1358
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001359 void DumpDexRegisterMapAtOffset(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001360 const OatFile::OatMethod& oat_method,
1361 const DexFile::CodeItem* code_item,
1362 size_t offset) {
1363 // This method is only relevant for oat methods compiled with the
1364 // optimizing compiler.
1365 DCHECK(IsMethodGeneratedByOptimizingCompiler(oat_method, code_item));
1366
1367 // The optimizing compiler outputs its CodeInfo data in the vmap table.
1368 const void* raw_code_info = oat_method.GetVmapTable();
1369 if (raw_code_info != nullptr) {
1370 CodeInfo code_info(raw_code_info);
David Brazdilf677ebf2015-05-29 16:29:43 +01001371 StackMapEncoding encoding = code_info.ExtractEncoding();
1372 StackMap stack_map = code_info.GetStackMapForNativePcOffset(offset, encoding);
Roland Levillainf2650d12015-05-28 14:53:28 +01001373 if (stack_map.IsValid()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001374 stack_map.Dump(vios, code_info, encoding, oat_method.GetCodeOffset(),
1375 code_item->registers_size_);
Roland Levillainf2650d12015-05-28 14:53:28 +01001376 }
1377 }
1378 }
1379
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001380 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001381 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001382 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001383 const DexFile* dex_file,
1384 const DexFile::ClassDef& class_def,
1385 const DexFile::CodeItem* code_item,
1386 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001387 if ((method_access_flags & kAccNative) == 0) {
1388 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001389 Runtime* const runtime = Runtime::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001390 Handle<mirror::DexCache> dex_cache(
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001391 hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file,
1392 runtime->GetLinearAlloc())));
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001393 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001394 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001395 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
1396 &class_def, code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001397 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001398
1399 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001400 }
1401
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001402 void DumpCode(VariableIndentationOutputStream* vios,
1403 verifier::MethodVerifier* verifier,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001404 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1405 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001406 const void* quick_code = oat_method.GetQuickCode();
1407
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001408 if (code_size == 0) {
1409 code_size = oat_method.GetQuickCodeSize();
1410 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001411 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001412 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001413 return;
Elliott Hughes956af0f2014-12-11 14:34:28 -08001414 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001415 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1416 size_t offset = 0;
1417 while (offset < code_size) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001418 if (!bad_input) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001419 DumpInformationAtOffset(vios, oat_method, code_item, offset, false);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001420 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001421 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001422 if (!bad_input) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001423 uint32_t dex_pc =
1424 DumpInformationAtOffset(vios, oat_method, code_item, offset, true);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001425 if (dex_pc != DexFile::kDexNoIndex) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001426 DumpGcMapAtNativePcOffset(vios->Stream(), oat_method, code_item, offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001427 if (verifier != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001428 DumpVRegsAtDexPc(vios->Stream(), verifier, oat_method, code_item, dex_pc);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001429 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001430 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001431 }
1432 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001433 }
1434 }
1435
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001436 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001437 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001438 const OatDumperOptions& options_;
1439 uint32_t resolved_addr2instr_;
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001440 InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001441 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001442 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001443};
1444
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001445class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001446 public:
Roland Levillain3887c462015-08-12 18:15:42 +01001447 ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space,
1448 const ImageHeader& image_header, OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001449 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001450 vios_(os),
1451 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001452 image_space_(image_space),
1453 image_header_(image_header),
1454 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001455
Mathieu Chartier90443472015-07-16 20:32:27 -07001456 bool Dump() SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001457 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001458 std::ostream& indent_os = vios_.Stream();
1459
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001460 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001461
Jeff Haodcdc85b2015-12-04 14:06:18 -08001462 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1463
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001464 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001465
Mathieu Chartiere401d142015-04-22 13:56:20 -07001466 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1467
1468 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1469 auto section = static_cast<ImageHeader::ImageSections>(i);
1470 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1471 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001472
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001473 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001474
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001475 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001476
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001477 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1478
1479 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1480
1481 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001482
Alex Lighta59dd802014-07-02 16:28:08 -07001483 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1484
Igor Murashkin46774762014-10-22 11:37:02 -07001485 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1486
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001487 {
1488 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001489 static_assert(arraysize(image_roots_descriptions_) ==
1490 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001491 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1492 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1493 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001494 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001495 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001496 if (image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001497 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001498 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001499 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001500 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1501 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001502 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001503 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1504 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001505 run++;
1506 } else {
1507 break;
1508 }
1509 }
1510 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001511 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001512 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001513 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001514 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001515 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001516 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001517 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001518 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001519 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001520 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001521 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001522 }
1523 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001524 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001525
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001526 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001527 os << "METHOD ROOTS\n";
1528 static_assert(arraysize(image_methods_descriptions_) ==
1529 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1530 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1531 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1532 const char* description = image_methods_descriptions_[i];
1533 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001534 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001535 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001536 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001537 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001538
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001539 Runtime* const runtime = Runtime::Current();
1540 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001541 std::string image_filename = image_space_.GetImageFilename();
1542 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001543 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001544 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001545 std::string error_msg;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001546 const OatFile* oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(
1547 oat_location);
Alex Lighta59dd802014-07-02 16:28:08 -07001548 if (oat_file == nullptr) {
Richard Uhlere5fed032015-03-18 08:21:11 -07001549 oat_file = OatFile::Open(oat_location, oat_location,
1550 nullptr, nullptr, false, nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001551 &error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -07001552 if (oat_file == nullptr) {
1553 os << "NOT FOUND: " << error_msg << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001554 return false;
Alex Lighta59dd802014-07-02 16:28:08 -07001555 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001556 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001557 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001558
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001559 stats_.oat_file_bytes = oat_file->Size();
1560
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001561 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001562
Mathieu Chartier02e25112013-08-14 16:14:24 -07001563 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001564 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001565 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1566 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001567 }
1568
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001569 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001570
Jeff Haodcdc85b2015-12-04 14:06:18 -08001571 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001572 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001573 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001574 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001575 {
1576 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1577 heap->FlushAllocStack();
1578 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001579 // Since FlushAllocStack() above resets the (active) allocation
1580 // stack. Need to revoke the thread-local allocation stacks that
1581 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001582 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001583 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001584 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001585 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001586 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001587 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001588 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001589 {
1590 ReaderMutexLock mu(self, *class_linker->DexLock());
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001591 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001592 mirror::DexCache* dex_cache =
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001593 down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001594 if (dex_cache != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01001595 dex_caches_.insert(dex_cache);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001596 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001597 }
1598 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001599 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001600 // Dump the normal objects before ArtMethods.
1601 image_space_.GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1602 indent_os << "\n";
1603 // TODO: Dump fields.
1604 // Dump methods after.
1605 const auto& methods_section = image_header_.GetMethodsSection();
1606 const size_t pointer_size =
1607 InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet());
1608 DumpArtMethodVisitor visitor(this);
1609 methods_section.VisitPackedArtMethods(&visitor, image_space_.Begin(), pointer_size);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001610 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001611 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001612 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001613 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001614 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001615 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001616 if (file.get() == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001617 LOG(WARNING) << "Failed to find image in " << image_filename;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001618 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001619 if (file.get() != nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001620 stats_.file_bytes = file->GetLength();
Brian Carlstrom6f277752013-09-30 17:56:45 -07001621 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001622 size_t header_bytes = sizeof(ImageHeader);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001623 const auto& object_section = image_header_.GetImageSection(ImageHeader::kSectionObjects);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001624 const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1625 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001626 const auto& dex_cache_arrays_section = image_header_.GetImageSection(
1627 ImageHeader::kSectionDexCacheArrays);
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001628 const auto& intern_section = image_header_.GetImageSection(
1629 ImageHeader::kSectionInternedStrings);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001630 const auto& class_table_section = image_header_.GetImageSection(
1631 ImageHeader::kSectionClassTable);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001632 const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1633
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001634 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001635
1636 // Objects are kObjectAlignment-aligned.
1637 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1638 if (object_section.Offset() > header_bytes) {
1639 stats_.alignment_bytes += object_section.Offset() - header_bytes;
1640 }
1641
1642 // Field section is 4-byte aligned.
1643 constexpr size_t kFieldSectionAlignment = 4U;
1644 uint32_t end_objects = object_section.Offset() + object_section.Size();
1645 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
1646 stats_.alignment_bytes += field_section.Offset() - end_objects;
1647
1648 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
1649 uint32_t end_fields = field_section.Offset() + field_section.Size();
1650 CHECK_ALIGNED(method_section.Offset(), 4);
1651 stats_.alignment_bytes += method_section.Offset() - end_fields;
1652
1653 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
1654 uint32_t end_methods = method_section.Offset() + method_section.Size();
1655 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
1656 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
1657
1658 // Intern table is 8-byte aligned.
1659 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
1660 CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
1661 stats_.alignment_bytes += intern_section.Offset() - end_caches;
1662
1663 // Add space between intern table and class table.
1664 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
1665 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
1666
1667 // Add space between class table and bitmap. Expect the bitmap to be page-aligned.
1668 uint32_t end_ctable = class_table_section.Offset() + class_table_section.Size();
1669 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
1670 stats_.alignment_bytes += bitmap_section.Offset() - end_ctable;
1671
Mathieu Chartiere401d142015-04-22 13:56:20 -07001672 stats_.bitmap_bytes += bitmap_section.Size();
1673 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01001674 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01001675 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001676 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001677 stats_.class_table_bytes += class_table_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001678 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001679 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001680
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001681 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001682
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001683 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001684 }
1685
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001686 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001687 class DumpArtMethodVisitor : public ArtMethodVisitor {
1688 public:
1689 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
1690
1691 virtual void Visit(ArtMethod* method) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
1692 std::ostream& indent_os = image_dumper_->vios_.Stream();
1693 indent_os << method << " " << " ArtMethod: " << PrettyMethod(method) << "\n";
1694 image_dumper_->DumpMethod(method, image_dumper_, indent_os);
1695 indent_os << "\n";
1696 }
1697
1698 private:
1699 ImageDumper* const image_dumper_;
1700 };
1701
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001702 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Mathieu Chartier90443472015-07-16 20:32:27 -07001703 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001704 CHECK(type != nullptr);
1705 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001706 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001707 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001708 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001709 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001710 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001711 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001712 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001713 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001714 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001715 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001716 }
1717 }
1718
Mathieu Chartierc7853442015-03-27 14:35:38 -07001719 static void PrintField(std::ostream& os, ArtField* field, mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -07001720 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001721 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08001722 switch (field->GetTypeAsPrimitiveType()) {
1723 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08001724 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001725 break;
1726 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001727 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001728 break;
1729 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001730 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001731 break;
1732 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001733 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001734 break;
1735 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07001736 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001737 break;
1738 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07001739 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001740 break;
1741 case Primitive::kPrimBoolean:
Fred Shih37f05ef2014-07-16 18:38:08 -07001742 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1743 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001744 break;
1745 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07001746 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001747 break;
1748 case Primitive::kPrimNot: {
1749 // Get the value, don't compute the type unless it is non-null as we don't want
1750 // to cause class loading.
1751 mirror::Object* value = field->GetObj(obj);
1752 if (value == nullptr) {
1753 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07001754 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08001755 // Grab the field type without causing resolution.
Mathieu Chartierdaaf3262015-03-24 13:30:28 -07001756 mirror::Class* field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08001757 if (field_type != nullptr) {
1758 PrettyObjectValue(os, field_type, value);
1759 } else {
1760 os << StringPrintf("%p %s\n", value,
1761 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
1762 }
Ian Rogers50239c72013-06-17 14:53:22 -07001763 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001764 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07001765 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001766 default:
1767 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
1768 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08001769 }
1770 }
1771
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001772 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Mathieu Chartier90443472015-07-16 20:32:27 -07001773 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001774 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001775 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001776 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001777 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001778 for (ArtField& field : klass->GetIFields()) {
1779 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001780 }
1781 }
1782
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001783 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001784 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001785 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001786
Mathieu Chartiere401d142015-04-22 13:56:20 -07001787 const void* GetQuickOatCodeBegin(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001788 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001789 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
1790 InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet()));
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001791 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001792 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001793 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001794 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001795 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001796 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001797 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001798 }
1799
Mathieu Chartiere401d142015-04-22 13:56:20 -07001800 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001801 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001802 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1803 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001804 return 0;
1805 }
1806 return oat_code_begin[-1];
1807 }
1808
Mathieu Chartiere401d142015-04-22 13:56:20 -07001809 const void* GetQuickOatCodeEnd(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001810 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001811 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001812 if (oat_code_begin == nullptr) {
1813 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001814 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001815 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001816 }
1817
Mathieu Chartier90443472015-07-16 20:32:27 -07001818 static void Callback(mirror::Object* obj, void* arg) SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001819 DCHECK(obj != nullptr);
1820 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001821 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001822 if (!state->InDumpSpace(obj)) {
1823 return;
1824 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001825
1826 size_t object_bytes = obj->SizeOf();
1827 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1828 state->stats_.object_bytes += object_bytes;
1829 state->stats_.alignment_bytes += alignment_bytes;
1830
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001831 std::ostream& os = state->vios_.Stream();
1832
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001833 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001834 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001835 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
1836 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001837 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001838 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001839 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
1840 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08001841 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001842 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001843 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001844 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001845 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001846 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001847 ScopedIndentation indent1(&state->vios_);
1848 DumpFields(os, obj, obj_class);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001849 const auto image_pointer_size =
1850 InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet());
Ian Rogersd5b32602012-02-26 16:40:04 -08001851 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001852 auto* obj_array = obj->AsObjectArray<mirror::Object>();
1853 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001854 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001855 size_t run = 0;
1856 for (int32_t j = i + 1; j < length; j++) {
1857 if (value == obj_array->Get(j)) {
1858 run++;
1859 } else {
1860 break;
1861 }
1862 }
1863 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001864 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001865 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001866 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001867 i = i + run;
1868 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001869 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001870 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001871 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001872 }
1873 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001874 mirror::Class* klass = obj->AsClass();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001875 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001876 os << "STATICS:\n";
1877 ScopedIndentation indent2(&state->vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001878 for (ArtField& field : klass->GetSFields()) {
1879 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001880 }
1881 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001882 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01001883 auto it = state->dex_caches_.find(obj);
1884 if (it != state->dex_caches_.end()) {
1885 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001886 const auto& field_section = state->image_header_.GetImageSection(
1887 ImageHeader::kSectionArtFields);
1888 const auto& method_section = state->image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001889 size_t num_methods = dex_cache->NumResolvedMethods();
1890 if (num_methods != 0u) {
1891 os << "Methods (size=" << num_methods << "):";
1892 ScopedIndentation indent2(&state->vios_);
1893 auto* resolved_methods = dex_cache->GetResolvedMethods();
1894 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
1895 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_methods, i, image_pointer_size);
1896 size_t run = 0;
1897 for (size_t j = i + 1;
1898 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_methods,
1899 j,
1900 image_pointer_size);
1901 ++j, ++run) {}
1902 if (run == 0) {
1903 os << StringPrintf("%zd: ", i);
1904 } else {
1905 os << StringPrintf("%zd to %zd: ", i, i + run);
1906 i = i + run;
1907 }
1908 std::string msg;
1909 if (elem == nullptr) {
1910 msg = "null";
1911 } else if (method_section.Contains(
1912 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
1913 msg = PrettyMethod(reinterpret_cast<ArtMethod*>(elem));
1914 } else {
1915 msg = "<not in method section>";
1916 }
1917 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07001918 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001919 }
1920 size_t num_fields = dex_cache->NumResolvedFields();
1921 if (num_fields != 0u) {
1922 os << "Fields (size=" << num_fields << "):";
1923 ScopedIndentation indent2(&state->vios_);
1924 auto* resolved_fields = dex_cache->GetResolvedFields();
1925 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
1926 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_fields, i, image_pointer_size);
1927 size_t run = 0;
1928 for (size_t j = i + 1;
1929 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_fields,
1930 j,
1931 image_pointer_size);
1932 ++j, ++run) {}
1933 if (run == 0) {
1934 os << StringPrintf("%zd: ", i);
1935 } else {
1936 os << StringPrintf("%zd to %zd: ", i, i + run);
1937 i = i + run;
1938 }
1939 std::string msg;
1940 if (elem == nullptr) {
1941 msg = "null";
1942 } else if (field_section.Contains(
1943 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
1944 msg = PrettyField(reinterpret_cast<ArtField*>(elem));
1945 } else {
1946 msg = "<not in field section>";
1947 }
1948 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07001949 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001950 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001951 }
1952 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07001953 std::string temp;
1954 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001955 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001956
Mathieu Chartiere401d142015-04-22 13:56:20 -07001957 void DumpMethod(ArtMethod* method, ImageDumper* state, std::ostream& indent_os)
Mathieu Chartier90443472015-07-16 20:32:27 -07001958 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001959 DCHECK(method != nullptr);
1960 const auto image_pointer_size =
1961 InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet());
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01001962 const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method);
1963 const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01001964 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
1965 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07001966 if (method->IsNative()) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01001967 if (!Runtime::Current()->GetClassLinker()->IsQuickGenericJniStub(quick_oat_code_begin)) {
1968 DCHECK(method_header->GetNativeGcMap() == nullptr) << PrettyMethod(method);
1969 DCHECK(method_header->GetMappingTable() == nullptr) << PrettyMethod(method);
1970 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001971 bool first_occurrence;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001972 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01001973 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001974 if (first_occurrence) {
1975 state->stats_.native_to_managed_code_bytes += quick_oat_code_size;
1976 }
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01001977 if (quick_oat_code_begin !=
1978 method->GetEntryPointFromQuickCompiledCodePtrSize(image_pointer_size)) {
1979 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001980 }
1981 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
1982 method->IsResolutionMethod() || method->IsImtConflictMethod() ||
1983 method->IsImtUnimplementedMethod() || method->IsClassInitializer()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001984 } else {
1985 const DexFile::CodeItem* code_item = method->GetCodeItem();
1986 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
1987 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
1988
1989 bool first_occurrence;
1990 size_t gc_map_bytes = state->ComputeOatSize(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01001991 method_header->GetNativeGcMap(), &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001992 if (first_occurrence) {
1993 state->stats_.gc_map_bytes += gc_map_bytes;
1994 }
1995
1996 size_t pc_mapping_table_bytes = state->ComputeOatSize(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01001997 method_header->GetMappingTable(), &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001998 if (first_occurrence) {
1999 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
2000 }
2001
Roland Levillain6d7f1792015-07-02 10:59:15 +01002002 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002003 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01002004 // Method compiled with the optimizing compiler have no vmap table.
2005 vmap_table_bytes = state->ComputeOatSize(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002006 method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002007 if (first_occurrence) {
2008 state->stats_.vmap_table_bytes += vmap_table_bytes;
2009 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002010 }
2011
Mathieu Chartiere401d142015-04-22 13:56:20 -07002012 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
2013 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
2014 if (first_occurrence) {
2015 state->stats_.managed_code_bytes += quick_oat_code_size;
2016 if (method->IsConstructor()) {
2017 if (method->IsStatic()) {
2018 state->stats_.class_initializer_code_bytes += quick_oat_code_size;
2019 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
2020 state->stats_.large_initializer_code_bytes += quick_oat_code_size;
2021 }
2022 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
2023 state->stats_.large_method_code_bytes += quick_oat_code_size;
2024 }
2025 }
2026 state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
2027
Igor Murashkin7617abd2015-07-10 18:27:47 -07002028 uint32_t method_access_flags = method->GetAccessFlags();
2029
Mathieu Chartiere401d142015-04-22 13:56:20 -07002030 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Igor Murashkin7617abd2015-07-10 18:27:47 -07002031 indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd AccessFlags=0x%x\n",
2032 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes,
2033 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002034
2035 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
Vladimir Marko14632852015-08-17 12:07:23 +01002036 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_pointer_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002037
2038 double expansion =
2039 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
2040 state->stats_.ComputeOutliers(total_size, expansion, method);
2041 }
2042 }
2043
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002044 std::set<const void*> already_seen_;
2045 // Compute the size of the given data within the oat file and whether this is the first time
2046 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002047 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002048 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002049 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002050 already_seen_.insert(oat_data);
2051 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002052 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002053 }
2054 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002055 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002056
2057 public:
2058 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002059 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002060 size_t file_bytes;
2061
2062 size_t header_bytes;
2063 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002064 size_t art_field_bytes;
2065 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01002066 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002067 size_t interned_strings_bytes;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002068 size_t class_table_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07002069 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002070 size_t alignment_bytes;
2071
2072 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002073 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002074 size_t managed_to_native_code_bytes;
2075 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07002076 size_t class_initializer_code_bytes;
2077 size_t large_initializer_code_bytes;
2078 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002079
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002080 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002081 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002082 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002083
2084 size_t dex_instruction_bytes;
2085
Mathieu Chartiere401d142015-04-22 13:56:20 -07002086 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002087 std::vector<size_t> method_outlier_size;
2088 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002089 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002090
Roland Levillain3887c462015-08-12 18:15:42 +01002091 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002092 : oat_file_bytes(0),
2093 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002094 header_bytes(0),
2095 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002096 art_field_bytes(0),
2097 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01002098 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002099 interned_strings_bytes(0),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002100 class_table_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07002101 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002102 alignment_bytes(0),
2103 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002104 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002105 managed_to_native_code_bytes(0),
2106 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07002107 class_initializer_code_bytes(0),
2108 large_initializer_code_bytes(0),
2109 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002110 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002111 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002112 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002113 dex_instruction_bytes(0) {}
2114
Elliott Hughesa0e18062012-04-13 15:59:59 -07002115 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002116 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002117 size_t bytes;
2118 size_t count;
2119 };
2120 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2121 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002122
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002123 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002124 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2125 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002126 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002127 it->second.count += 1;
2128 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002129 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002130 }
2131 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002132
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002133 double PercentOfOatBytes(size_t size) {
2134 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2135 }
2136
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002137 double PercentOfFileBytes(size_t size) {
2138 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2139 }
2140
2141 double PercentOfObjectBytes(size_t size) {
2142 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2143 }
2144
Mathieu Chartiere401d142015-04-22 13:56:20 -07002145 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002146 method_outlier_size.push_back(total_size);
2147 method_outlier_expansion.push_back(expansion);
2148 method_outlier.push_back(method);
2149 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002150
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002151 void DumpOutliers(std::ostream& os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002152 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002153 size_t sum_of_sizes = 0;
2154 size_t sum_of_sizes_squared = 0;
2155 size_t sum_of_expansion = 0;
2156 size_t sum_of_expansion_squared = 0;
2157 size_t n = method_outlier_size.size();
Jeff Haodcdc85b2015-12-04 14:06:18 -08002158 if (n == 0) {
2159 return;
2160 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002161 for (size_t i = 0; i < n; i++) {
2162 size_t cur_size = method_outlier_size[i];
2163 sum_of_sizes += cur_size;
2164 sum_of_sizes_squared += cur_size * cur_size;
2165 double cur_expansion = method_outlier_expansion[i];
2166 sum_of_expansion += cur_expansion;
2167 sum_of_expansion_squared += cur_expansion * cur_expansion;
2168 }
2169 size_t size_mean = sum_of_sizes / n;
2170 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2171 double expansion_mean = sum_of_expansion / n;
2172 double expansion_variance =
2173 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2174
2175 // Dump methods whose size is a certain number of standard deviations from the mean
2176 size_t dumped_values = 0;
2177 size_t skipped_values = 0;
2178 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2179 size_t cur_size_variance = i * i * size_variance;
2180 bool first = true;
2181 for (size_t j = 0; j < n; j++) {
2182 size_t cur_size = method_outlier_size[j];
2183 if (cur_size > size_mean) {
2184 size_t cur_var = cur_size - size_mean;
2185 cur_var = cur_var * cur_var;
2186 if (cur_var > cur_size_variance) {
2187 if (dumped_values > 20) {
2188 if (i == 1) {
2189 skipped_values++;
2190 } else {
2191 i = 2; // jump to counting for 1 standard deviation
2192 break;
2193 }
2194 } else {
2195 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002196 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002197 first = false;
2198 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002199 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002200 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002201 method_outlier_size[j] = 0; // don't consider this method again
2202 dumped_values++;
2203 }
2204 }
2205 }
2206 }
2207 }
2208 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002209 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002210 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002211 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002212 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002213
2214 // Dump methods whose expansion is a certain number of standard deviations from the mean
2215 dumped_values = 0;
2216 skipped_values = 0;
2217 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2218 double cur_expansion_variance = i * i * expansion_variance;
2219 bool first = true;
2220 for (size_t j = 0; j < n; j++) {
2221 double cur_expansion = method_outlier_expansion[j];
2222 if (cur_expansion > expansion_mean) {
2223 size_t cur_var = cur_expansion - expansion_mean;
2224 cur_var = cur_var * cur_var;
2225 if (cur_var > cur_expansion_variance) {
2226 if (dumped_values > 20) {
2227 if (i == 1) {
2228 skipped_values++;
2229 } else {
2230 i = 2; // jump to counting for 1 standard deviation
2231 break;
2232 }
2233 } else {
2234 if (first) {
2235 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002236 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002237 first = false;
2238 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002239 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002240 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002241 method_outlier_expansion[j] = 0.0; // don't consider this method again
2242 dumped_values++;
2243 }
2244 }
2245 }
2246 }
2247 }
2248 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002249 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002250 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002251 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002252 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002253 }
2254
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002255 void Dump(std::ostream& os, std::ostream& indent_os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002256 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002257 {
2258 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2259 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002260 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2261 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2262 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2263 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2264 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2265 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002266 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002267 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2268 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002269 header_bytes, PercentOfFileBytes(header_bytes),
2270 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002271 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2272 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002273 dex_cache_arrays_bytes,
2274 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002275 interned_strings_bytes,
2276 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002277 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002278 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002279 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2280 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002281 CHECK_EQ(file_bytes,
2282 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2283 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2284 bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002285 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002286
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002287 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002288 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002289 for (const auto& sizes_and_count : sizes_and_counts) {
2290 const std::string& descriptor(sizes_and_count.first);
2291 double average = static_cast<double>(sizes_and_count.second.bytes) /
2292 static_cast<double>(sizes_and_count.second.count);
2293 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002294 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002295 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002296 descriptor.c_str(), sizes_and_count.second.bytes,
2297 sizes_and_count.second.count, average, percent);
2298 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002299 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002300 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002301 CHECK_EQ(object_bytes, object_bytes_total);
2302
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002303 os << StringPrintf("oat_file_bytes = %8zd\n"
2304 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2305 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2306 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2307 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2308 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2309 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002310 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002311 managed_code_bytes,
2312 PercentOfOatBytes(managed_code_bytes),
2313 managed_to_native_code_bytes,
2314 PercentOfOatBytes(managed_to_native_code_bytes),
2315 native_to_managed_code_bytes,
2316 PercentOfOatBytes(native_to_managed_code_bytes),
2317 class_initializer_code_bytes,
2318 PercentOfOatBytes(class_initializer_code_bytes),
2319 large_initializer_code_bytes,
2320 PercentOfOatBytes(large_initializer_code_bytes),
2321 large_method_code_bytes,
2322 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002323 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002324 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002325 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002326 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2327 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002328 }
2329
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002330 os << "\n" << StringPrintf("gc_map_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2331 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2332 "vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002333 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
2334 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
2335 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002336 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002337
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002338 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2339 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002340 static_cast<double>(managed_code_bytes) /
2341 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002342 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002343 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002344 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002345
2346 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002347 }
2348 } stats_;
2349
2350 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002351 enum {
2352 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2353 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2354 kLargeConstructorDexBytes = 4000,
2355 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2356 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2357 kLargeMethodDexBytes = 16000
2358 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002359
2360 // For performance, use the *os_ directly for anything that doesn't need indentation
2361 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002362 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002363 VariableIndentationOutputStream vios_;
2364 ScopedIndentation indent1_;
2365
Ian Rogers1d54e732013-05-02 21:10:01 -07002366 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002367 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002368 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002369 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002370 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002371
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002372 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002373};
2374
Jeff Haodcdc85b2015-12-04 14:06:18 -08002375static int DumpImage(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002376 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002377 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002378 options->class_loader_ = &null_class_loader;
2379
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002380 ScopedObjectAccess soa(Thread::Current());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002381 gc::Heap* heap = runtime->GetHeap();
Jeff Haodcdc85b2015-12-04 14:06:18 -08002382 std::vector<gc::space::ImageSpace*> image_spaces = heap->GetBootImageSpaces();
2383 CHECK(!image_spaces.empty());
2384 for (gc::space::ImageSpace* image_space : image_spaces) {
2385 const ImageHeader& image_header = image_space->GetImageHeader();
2386 if (!image_header.IsValid()) {
2387 fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
2388 return EXIT_FAILURE;
2389 }
2390
2391 ImageDumper image_dumper(os, *image_space, image_header, options);
2392 if (!image_dumper.Dump()) {
2393 return EXIT_FAILURE;
2394 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002395 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002396 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002397}
2398
Andreas Gampe00b25f32014-09-17 21:49:05 -07002399static int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
2400 std::ostream* os) {
2401 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2402
2403 Thread* self = Thread::Current();
2404 CHECK(self != nullptr);
2405 // Need well-known-classes.
2406 WellKnownClasses::Init(self->GetJniEnv());
2407
2408 // Need to register dex files to get a working dex cache.
2409 ScopedObjectAccess soa(self);
2410 ClassLinker* class_linker = runtime->GetClassLinker();
Mathieu Chartierd57d4542015-10-14 10:55:30 -07002411 runtime->GetOatFileManager().RegisterOatFile(std::unique_ptr<const OatFile>(oat_file));
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002412 std::vector<const DexFile*> class_path;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002413 for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
2414 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002415 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002416 CHECK(dex_file != nullptr) << error_msg;
Mathieu Chartierd57d4542015-10-14 10:55:30 -07002417 class_linker->RegisterDexFile(*dex_file, runtime->GetLinearAlloc());
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002418 class_path.push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002419 }
2420
2421 // Need a class loader.
Andreas Gampe00b25f32014-09-17 21:49:05 -07002422 // Fake that we're a compiler.
Mathieu Chartier966878d2016-01-14 14:33:29 -08002423 jobject class_loader = class_linker->CreatePathClassLoader(self, class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002424
2425 // Use the class loader while dumping.
2426 StackHandleScope<1> scope(self);
2427 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
2428 soa.Decode<mirror::ClassLoader*>(class_loader));
2429 options->class_loader_ = &loader_handle;
2430
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002431 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002432 bool success = oat_dumper.Dump(*os);
2433 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2434}
2435
2436static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002437 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002438 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002439 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002440 options->class_loader_ = &null_class_loader;
2441
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002442 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002443 bool success = oat_dumper.Dump(*os);
2444 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2445}
2446
2447static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2448 std::ostream* os) {
2449 std::string error_msg;
Igor Murashkin37743352014-11-13 14:38:00 -08002450 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07002451 nullptr, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002452 if (oat_file == nullptr) {
2453 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2454 return EXIT_FAILURE;
2455 }
2456
2457 if (runtime != nullptr) {
2458 return DumpOatWithRuntime(runtime, oat_file, options, os);
2459 } else {
2460 return DumpOatWithoutRuntime(oat_file, options, os);
2461 }
2462}
2463
David Srbecky2fdd03c2016-03-10 15:32:37 +00002464static int SymbolizeOat(const char* oat_filename, std::string& output_name, bool no_bits) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002465 std::string error_msg;
Igor Murashkin37743352014-11-13 14:38:00 -08002466 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07002467 nullptr, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002468 if (oat_file == nullptr) {
2469 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2470 return EXIT_FAILURE;
2471 }
2472
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002473 bool result;
2474 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
2475 // files for 64-bit code in the past.
2476 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002477 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file, output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002478 result = oat_symbolizer.Symbolize();
2479 } else {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002480 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file, output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002481 result = oat_symbolizer.Symbolize();
2482 }
2483 if (!result) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002484 fprintf(stderr, "Failed to symbolize\n");
2485 return EXIT_FAILURE;
2486 }
2487
2488 return EXIT_SUCCESS;
2489}
2490
Igor Murashkin37743352014-11-13 14:38:00 -08002491struct OatdumpArgs : public CmdlineArgs {
2492 protected:
2493 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002494
Igor Murashkin37743352014-11-13 14:38:00 -08002495 virtual ParseStatus ParseCustom(const StringPiece& option,
2496 std::string* error_msg) OVERRIDE {
2497 {
2498 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
2499 if (base_parse != kParseUnknownArgument) {
2500 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002501 }
2502 }
2503
Igor Murashkin37743352014-11-13 14:38:00 -08002504 if (option.starts_with("--oat-file=")) {
2505 oat_filename_ = option.substr(strlen("--oat-file=")).data();
2506 } else if (option.starts_with("--image=")) {
2507 image_location_ = option.substr(strlen("--image=")).data();
2508 } else if (option =="--dump:raw_mapping_table") {
2509 dump_raw_mapping_table_ = true;
2510 } else if (option == "--dump:raw_gc_map") {
2511 dump_raw_gc_map_ = true;
2512 } else if (option == "--no-dump:vmap") {
2513 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01002514 } else if (option =="--dump:code_info_stack_maps") {
2515 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002516 } else if (option == "--no-disassemble") {
2517 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00002518 } else if (option =="--header-only") {
2519 dump_header_only_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002520 } else if (option.starts_with("--symbolize=")) {
2521 oat_filename_ = option.substr(strlen("--symbolize=")).data();
2522 symbolize_ = true;
David Srbecky2fdd03c2016-03-10 15:32:37 +00002523 } else if (option.starts_with("--only-keep-debug")) {
2524 only_keep_debug_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002525 } else if (option.starts_with("--class-filter=")) {
2526 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002527 } else if (option.starts_with("--method-filter=")) {
2528 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002529 } else if (option.starts_with("--list-classes")) {
2530 list_classes_ = true;
2531 } else if (option.starts_with("--list-methods")) {
2532 list_methods_ = true;
2533 } else if (option.starts_with("--export-dex-to=")) {
2534 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
2535 } else if (option.starts_with("--addr2instr=")) {
2536 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
2537 *error_msg = "Address conversion failed";
2538 return kParseError;
2539 }
Igor Murashkin37743352014-11-13 14:38:00 -08002540 } else {
2541 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002542 }
2543
Igor Murashkin37743352014-11-13 14:38:00 -08002544 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002545 }
2546
Igor Murashkin37743352014-11-13 14:38:00 -08002547 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
2548 // Infer boot image location from the image location if possible.
2549 if (boot_image_location_ == nullptr) {
2550 boot_image_location_ = image_location_;
2551 }
2552
2553 // Perform the parent checks.
2554 ParseStatus parent_checks = Base::ParseChecks(error_msg);
2555 if (parent_checks != kParseOk) {
2556 return parent_checks;
2557 }
2558
2559 // Perform our own checks.
2560 if (image_location_ == nullptr && oat_filename_ == nullptr) {
2561 *error_msg = "Either --image or --oat-file must be specified";
2562 return kParseError;
2563 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
2564 *error_msg = "Either --image or --oat-file must be specified but not both";
2565 return kParseError;
2566 }
2567
2568 return kParseOk;
2569 }
2570
2571 virtual std::string GetUsage() const {
2572 std::string usage;
2573
2574 usage +=
2575 "Usage: oatdump [options] ...\n"
2576 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
2577 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
2578 "\n"
2579 // Either oat-file or image is required.
2580 " --oat-file=<file.oat>: specifies an input oat filename.\n"
2581 " Example: --oat-file=/system/framework/boot.oat\n"
2582 "\n"
2583 " --image=<file.art>: specifies an input image location.\n"
2584 " Example: --image=/system/framework/boot.art\n"
2585 "\n";
2586
2587 usage += Base::GetUsage();
2588
2589 usage += // Optional.
2590 " --dump:raw_mapping_table enables dumping of the mapping table.\n"
2591 " Example: --dump:raw_mapping_table\n"
2592 "\n"
Mathieu Chartier19510f02015-05-26 14:44:35 -07002593 " --dump:raw_gc_map enables dumping of the GC map.\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002594 " Example: --dump:raw_gc_map\n"
2595 "\n"
2596 " --no-dump:vmap may be used to disable vmap dumping.\n"
2597 " Example: --no-dump:vmap\n"
2598 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01002599 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
2600 " Example: --dump:code_info_stack_maps\n"
2601 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002602 " --no-disassemble may be used to disable disassembly.\n"
2603 " Example: --no-disassemble\n"
2604 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00002605 " --header-only may be used to print only the oat header.\n"
2606 " Example: --header-only\n"
2607 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002608 " --list-classes may be used to list target file classes (can be used with filters).\n"
2609 " Example: --list-classes\n"
2610 " Example: --list-classes --class-filter=com.example.foo\n"
2611 "\n"
2612 " --list-methods may be used to list target file methods (can be used with filters).\n"
2613 " Example: --list-methods\n"
2614 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
2615 "\n"
2616 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
2617 " Example: --symbolize=/system/framework/boot.oat\n"
2618 "\n"
David Srbecky2fdd03c2016-03-10 15:32:37 +00002619 " --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
2620 " .rodata and .text sections are omitted in the output file to save space.\n"
2621 " Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
2622 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002623 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
2624 " Example: --class-filter=com.example.foo\n"
2625 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002626 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
2627 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002628 "\n"
2629 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
2630 " Example: --export-dex-to=/data/local/tmp\n"
2631 "\n"
2632 " --addr2instr=<address>: output matching method disassembled code from relative\n"
2633 " address (e.g. PC from crash dump)\n"
2634 " Example: --addr2instr=0x00001a3b\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002635 "\n";
2636
2637 return usage;
2638 }
2639
2640 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07002641 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002642 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00002643 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07002644 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002645 std::string elf_filename_prefix_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002646 bool dump_raw_mapping_table_ = false;
2647 bool dump_raw_gc_map_ = false;
2648 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01002649 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002650 bool disassemble_code_ = true;
2651 bool symbolize_ = false;
David Srbecky2fdd03c2016-03-10 15:32:37 +00002652 bool only_keep_debug_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002653 bool list_classes_ = false;
2654 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00002655 bool dump_header_only_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002656 uint32_t addr2instr_ = 0;
2657 const char* export_dex_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002658};
2659
Igor Murashkin37743352014-11-13 14:38:00 -08002660struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
2661 virtual bool NeedsRuntime() OVERRIDE {
2662 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002663
Igor Murashkin37743352014-11-13 14:38:00 -08002664 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
2665 bool absolute_addresses = (args_->oat_filename_ == nullptr);
2666
2667 oat_dumper_options_ = std::unique_ptr<OatDumperOptions>(new OatDumperOptions(
2668 args_->dump_raw_mapping_table_,
2669 args_->dump_raw_gc_map_,
2670 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01002671 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08002672 args_->disassemble_code_,
2673 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002674 args_->class_filter_,
2675 args_->method_filter_,
2676 args_->list_classes_,
2677 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00002678 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002679 args_->export_dex_location_,
2680 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08002681
2682 return (args_->boot_image_location_ != nullptr || args_->image_location_ != nullptr) &&
2683 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002684 }
2685
Igor Murashkin37743352014-11-13 14:38:00 -08002686 virtual bool ExecuteWithoutRuntime() OVERRIDE {
2687 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08002688 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002689
Mathieu Chartierd424d082014-10-15 10:31:46 -07002690 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08002691
Andreas Gampec24f3992014-12-17 20:40:11 -08002692 if (args_->symbolize_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002693 // ELF has special kind of section called SHT_NOBITS which allows us to create
2694 // sections which exist but their data is omitted from the ELF file to save space.
2695 // This is what "strip --only-keep-debug" does when it creates separate ELF file
2696 // with only debug data. We use it in similar way to exclude .rodata and .text.
2697 bool no_bits = args_->only_keep_debug_;
2698 return SymbolizeOat(args_->oat_filename_, args_->output_name_, no_bits) == EXIT_SUCCESS;
Andreas Gampec24f3992014-12-17 20:40:11 -08002699 } else {
2700 return DumpOat(nullptr,
2701 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002702 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08002703 args_->os_) == EXIT_SUCCESS;
2704 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002705 }
2706
Igor Murashkin37743352014-11-13 14:38:00 -08002707 virtual bool ExecuteWithRuntime(Runtime* runtime) {
2708 CHECK(args_ != nullptr);
2709
2710 if (args_->oat_filename_ != nullptr) {
2711 return DumpOat(runtime,
2712 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002713 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08002714 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002715 }
Igor Murashkin37743352014-11-13 14:38:00 -08002716
Jeff Haodcdc85b2015-12-04 14:06:18 -08002717 return DumpImage(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002718 }
2719
Igor Murashkin37743352014-11-13 14:38:00 -08002720 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
2721};
Andreas Gampe00b25f32014-09-17 21:49:05 -07002722
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002723} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07002724
2725int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08002726 art::OatdumpMain main;
2727 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002728}