blob: 25691f1c17e673966f9dc7bca4af46673592bb30 [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 Srbeckya1b4c5f2016-03-31 18:17:59 +0100251 uint32_t entry_point = oat_method.GetCodeOffset() - oat_header.GetExecutableOffset();
252 // Clear Thumb2 bit.
253 const void* code_address = EntryPointToCodePointer(reinterpret_cast<void*>(entry_point));
254
David Srbecky5d950762016-03-07 20:47:29 +0000255 debug::MethodDebugInfo info = debug::MethodDebugInfo();
256 info.trampoline_name = nullptr;
257 info.dex_file = &dex_file;
258 info.class_def_index = class_def_index;
259 info.dex_method_index = dex_method_index;
260 info.access_flags = method_access_flags;
261 info.code_item = code_item;
262 info.isa = oat_header.GetInstructionSet();
263 info.deduped = !seen_offsets_.insert(oat_method.GetCodeOffset()).second;
264 info.is_native_debuggable = oat_header.IsNativeDebuggable();
265 info.is_optimized = method_header->IsOptimized();
266 info.is_code_address_text_relative = true;
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100267 info.code_address = reinterpret_cast<uintptr_t>(code_address);
David Srbecky5d950762016-03-07 20:47:29 +0000268 info.code_size = method_header->GetCodeSize();
269 info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
270 info.code_info = info.is_optimized ? method_header->GetOptimizedCodeInfoPtr() : nullptr;
271 info.cfi = ArrayRef<uint8_t>();
272 method_debug_infos_.push_back(info);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700273 }
274
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700275 private:
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700276 const OatFile* oat_file_;
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800277 std::unique_ptr<ElfBuilder<ElfTypes> > builder_;
David Srbecky5d950762016-03-07 20:47:29 +0000278 std::vector<debug::MethodDebugInfo> method_debug_infos_;
279 std::unordered_set<uint32_t> seen_offsets_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700280 const std::string output_name_;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000281 bool no_bits_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700282};
283
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700284class OatDumperOptions {
285 public:
286 OatDumperOptions(bool dump_raw_mapping_table,
287 bool dump_raw_gc_map,
288 bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100289 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700290 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700291 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800292 const char* class_filter,
293 const char* method_filter,
294 bool list_classes,
295 bool list_methods,
David Brazdilc03d7b62016-03-02 12:18:03 +0000296 bool dump_header_only,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800297 const char* export_dex_location,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800298 const char* app_image,
299 const char* app_oat,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800300 uint32_t addr2instr)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700301 : dump_raw_mapping_table_(dump_raw_mapping_table),
302 dump_raw_gc_map_(dump_raw_gc_map),
303 dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100304 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700305 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700306 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800307 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000308 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800309 list_classes_(list_classes),
310 list_methods_(list_methods),
David Brazdilc03d7b62016-03-02 12:18:03 +0000311 dump_header_only_(dump_header_only),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800312 export_dex_location_(export_dex_location),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800313 app_image_(app_image),
314 app_oat_(app_oat),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800315 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800316 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700317
318 const bool dump_raw_mapping_table_;
319 const bool dump_raw_gc_map_;
320 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100321 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700322 const bool disassemble_code_;
323 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800324 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000325 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800326 const bool list_classes_;
327 const bool list_methods_;
David Brazdilc03d7b62016-03-02 12:18:03 +0000328 const bool dump_header_only_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800329 const char* const export_dex_location_;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800330 const char* const app_image_;
331 const char* const app_oat_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800332 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700333 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700334};
335
Elliott Hughese3c845c2012-02-28 17:23:01 -0800336class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700337 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100338 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000339 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800340 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700341 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800342 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800343 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
344 disassembler_(Disassembler::Create(instruction_set_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800345 new DisassemblerOptions(options_.absolute_addresses_,
Alexandre Ramesa37d9252014-10-27 11:28:14 +0000346 oat_file.Begin(),
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100347 true /* can_read_literals_ */))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800348 CHECK(options_.class_loader_ != nullptr);
349 CHECK(options_.class_filter_ != nullptr);
350 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800351 AddAllOffsets();
352 }
353
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700354 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700355 delete disassembler_;
356 }
357
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800358 InstructionSet GetInstructionSet() {
359 return instruction_set_;
360 }
361
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700362 bool Dump(std::ostream& os) {
363 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800364 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700365
366 os << "MAGIC:\n";
367 os << oat_header.GetMagic() << "\n\n";
368
Jeff Hao4b07a6e2016-01-15 12:50:44 -0800369 os << "LOCATION:\n";
370 os << oat_file_.GetLocation() << "\n\n";
371
Brian Carlstromaded5f72011-10-07 17:15:04 -0700372 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800373 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700374
Elliott Hughesa72ec822012-03-05 17:12:22 -0800375 os << "INSTRUCTION SET:\n";
376 os << oat_header.GetInstructionSet() << "\n\n";
377
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700378 {
379 std::unique_ptr<const InstructionSetFeatures> features(
380 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
381 oat_header.GetInstructionSetFeaturesBitmap()));
382 os << "INSTRUCTION SET FEATURES:\n";
383 os << features->GetFeatureString() << "\n\n";
384 }
Dave Allison70202782013-10-22 17:52:19 -0700385
Brian Carlstromaded5f72011-10-07 17:15:04 -0700386 os << "DEX FILE COUNT:\n";
387 os << oat_header.GetDexFileCount() << "\n\n";
388
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800389#define DUMP_OAT_HEADER_OFFSET(label, offset) \
390 os << label " OFFSET:\n"; \
391 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800392 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800393 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
394 } \
395 os << StringPrintf("\n\n");
396
397 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
398 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
399 GetInterpreterToInterpreterBridgeOffset);
400 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
401 GetInterpreterToCompiledCodeBridgeOffset);
402 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
403 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800404 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
405 GetQuickGenericJniTrampolineOffset);
406 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
407 GetQuickImtConflictTrampolineOffset);
408 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
409 GetQuickResolutionTrampolineOffset);
410 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
411 GetQuickToInterpreterBridgeOffset);
412#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700413
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700414 os << "IMAGE PATCH DELTA:\n";
415 os << StringPrintf("%d (0x%08x)\n\n",
416 oat_header.GetImagePatchDelta(),
417 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700418
Brian Carlstrom28db0122012-10-18 16:20:41 -0700419 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
420 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
421
422 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800423 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700424
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700425 // Print the key-value store.
426 {
427 os << "KEY VALUE STORE:\n";
428 size_t index = 0;
429 const char* key;
430 const char* value;
431 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
432 os << key << " = " << value << "\n";
433 index++;
434 }
435 os << "\n";
436 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700437
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800438 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700439 os << "BEGIN:\n";
440 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700441
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700442 os << "END:\n";
443 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
444 }
445
446 os << "SIZE:\n";
447 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700448
449 os << std::flush;
450
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800451 // If set, adjust relative address to be searched
452 if (options_.addr2instr_ != 0) {
453 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
454 os << "SEARCH ADDRESS (executable offset + input):\n";
455 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
456 }
457
David Brazdilc03d7b62016-03-02 12:18:03 +0000458 if (!options_.dump_header_only_) {
459 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
460 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
461 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800462
David Brazdilc03d7b62016-03-02 12:18:03 +0000463 // If file export selected skip file analysis
464 if (options_.export_dex_location_) {
465 if (!ExportDexFile(os, *oat_dex_file)) {
466 success = false;
467 }
468 } else {
469 if (!DumpOatDexFile(os, *oat_dex_file)) {
470 success = false;
471 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800472 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700473 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700474 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000475
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700476 os << std::flush;
477 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700478 }
479
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800480 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700481 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
482 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800483 return 0; // Address not in oat file
484 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800485 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
486 reinterpret_cast<uintptr_t>(oat_file_.Begin());
487 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800488 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800489 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800490 return end_offset - begin_offset;
491 }
492
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800493 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700494 return oat_file_.GetOatHeader().GetInstructionSet();
495 }
496
Mathieu Chartier90443472015-07-16 20:32:27 -0700497 const void* GetQuickOatCode(ArtMethod* m) SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800498 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
499 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700500 CHECK(oat_dex_file != nullptr);
501 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700502 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
503 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700504 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
505 << "': " << error_msg;
506 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800507 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700508 const DexFile::ClassDef* class_def =
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800509 dex_file->FindClassDef(descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700510 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700511 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100512 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800513 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100514 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800515 }
516 }
517 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700518 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800519 }
520
Brian Carlstromaded5f72011-10-07 17:15:04 -0700521 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800522 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800523 // We don't know the length of the code for each method, but we need to know where to stop
524 // when disassembling. What we do know is that a region of code will be followed by some other
525 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
526 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800527 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
528 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700529 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700530 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700531 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
532 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700533 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
534 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800535 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800536 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800537 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800538 for (size_t class_def_index = 0;
539 class_def_index < dex_file->NumClassDefs();
540 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800541 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100542 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700543 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700544 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800545 ClassDataItemIterator it(*dex_file, class_data);
546 SkipAllFields(it);
547 uint32_t class_method_index = 0;
548 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100549 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800550 it.Next();
551 }
552 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100553 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800554 it.Next();
555 }
556 }
557 }
558 }
559
560 // If the last thing in the file is code for a method, there won't be an offset for the "next"
561 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
562 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800563 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800564 }
565
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700566 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
567 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
568 }
569
Elliott Hughese3c845c2012-02-28 17:23:01 -0800570 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800571 uint32_t code_offset = oat_method.GetCodeOffset();
572 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
573 code_offset &= ~0x1;
574 }
575 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800576 offsets_.insert(oat_method.GetMappingTableOffset());
577 offsets_.insert(oat_method.GetVmapTableOffset());
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800578 offsets_.insert(oat_method.GetGcMapOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800579 }
580
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700581 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
582 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800583 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700584 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800585 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800586 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700587
588 // Create the verifier early.
589
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700590 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700591 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
592 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700593 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700594 os << std::flush;
595 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700596 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100597
598 VariableIndentationOutputStream vios(&os);
599 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800600 for (size_t class_def_index = 0;
601 class_def_index < dex_file->NumClassDefs();
602 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700603 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
604 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800605
606 // TODO: Support regex
607 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
608 continue;
609 }
610
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700611 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100612 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700613 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
614 class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100615 << " (" << oat_class.GetStatus() << ")"
616 << " (" << oat_class.GetType() << ")\n";
617 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800618 if (options_.list_classes_) continue;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700619 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700620 success = false;
621 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800622 if (stop_analysis) {
623 os << std::flush;
624 return success;
625 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700626 }
627
628 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700629 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700630 }
631
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800632 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
633 std::string error_msg;
634 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
635
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700636 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800637 if (dex_file == nullptr) {
638 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
639 return false;
640 }
641 size_t fsize = oat_dex_file.FileSize();
642
643 // Some quick checks just in case
644 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
645 os << "Invalid dex file\n";
646 return false;
647 }
648
649 // Verify output directory exists
650 if (!OS::DirectoryExists(options_.export_dex_location_)) {
651 // TODO: Extend OS::DirectoryExists if symlink support is required
652 os << options_.export_dex_location_ << " output directory not found or symlink\n";
653 return false;
654 }
655
656 // Beautify path names
657 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
658 return false;
659 }
660
661 std::string dex_orig_name;
662 size_t dex_orig_pos = dex_file_location.rfind('/');
663 if (dex_orig_pos == std::string::npos)
664 dex_orig_name = dex_file_location;
665 else
666 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
667
668 // A more elegant approach to efficiently name user installed apps is welcome
669 if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
670 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
671 size_t apk_orig_pos = dex_file_location.rfind('/');
672 if (apk_orig_pos != std::string::npos) {
673 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
674 }
675 }
676
677 std::string out_dex_path(options_.export_dex_location_);
678 if (out_dex_path.back() != '/') {
679 out_dex_path.append("/");
680 }
681 out_dex_path.append(dex_orig_name);
682 out_dex_path.append("_export.dex");
683 if (out_dex_path.length() > PATH_MAX) {
684 return false;
685 }
686
687 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
688 if (file.get() == nullptr) {
689 os << "Failed to open output dex file " << out_dex_path;
690 return false;
691 }
692
693 if (!file->WriteFully(dex_file->Begin(), fsize)) {
694 os << "Failed to write dex file";
695 file->Erase();
696 return false;
697 }
698
699 if (file->FlushCloseOrErase() != 0) {
700 os << "Flush and close failed";
701 return false;
702 }
703
704 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
705 os << std::flush;
706
707 return true;
708 }
709
Elliott Hughese3c845c2012-02-28 17:23:01 -0800710 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700711 while (it.HasNextStaticField()) {
712 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700713 }
Ian Rogers0571d352011-11-03 19:51:38 -0700714 while (it.HasNextInstanceField()) {
715 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700716 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800717 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700718
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100719 bool DumpOatClass(VariableIndentationOutputStream* vios,
720 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800721 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700722 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800723 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -0700724 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700725 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100726 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700727 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800728 }
729 ClassDataItemIterator it(dex_file, class_data);
730 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700731 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700732 while (it.HasNextDirectMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100733 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700734 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800735 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700736 success = false;
737 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800738 if (addr_found) {
739 *stop_analysis = true;
740 return success;
741 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700742 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700743 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700744 }
Ian Rogers0571d352011-11-03 19:51:38 -0700745 while (it.HasNextVirtualMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100746 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700747 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800748 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700749 success = false;
750 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800751 if (addr_found) {
752 *stop_analysis = true;
753 return success;
754 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700755 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700756 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700757 }
Ian Rogers0571d352011-11-03 19:51:38 -0700758 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100759 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700760 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700761 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800762
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700763 static constexpr uint32_t kPrologueBytes = 16;
764
765 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
766 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
767
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100768 bool DumpOatMethod(VariableIndentationOutputStream* vios,
769 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700770 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700771 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800772 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800773 uint32_t method_access_flags, bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700774 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800775
776 // TODO: Support regex
777 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
778 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000779 return success;
780 }
781
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800782 std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100783 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
784 class_method_index, pretty_method.c_str(),
785 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800786 if (options_.list_methods_) return success;
787
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800788 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
789 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
790 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
791 uint32_t code_offset = oat_method.GetCodeOffset();
792 uint32_t code_size = oat_method.GetQuickCodeSize();
793 if (resolved_addr2instr_ != 0) {
794 if (resolved_addr2instr_ > code_offset + code_size) {
795 return success;
796 } else {
797 *addr_found = true; // stop analyzing file at next iteration
798 }
799 }
800
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100801 // Everything below is indented at least once.
802 ScopedIndentation indent1(vios);
803
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800804 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100805 vios->Stream() << "DEX CODE:\n";
806 ScopedIndentation indent2(vios);
807 DumpDexCode(vios->Stream(), dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700808 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700809
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700810 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700811 std::unique_ptr<verifier::MethodVerifier> verifier;
812 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700813 // We need to have the handle scope stay live until after the verifier since the verifier has
814 // a handle to the dex cache from hs.
815 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100816 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
817 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700818 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100819 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700820 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -0700821 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800822 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100823 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800824 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100825 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100826 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100827 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700828 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100829 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700830 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
831 oat_method_offsets_offset, oat_file_.Size());
832 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100833 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700834 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800835 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700836
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100837 ScopedIndentation indent2(vios);
838 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700839 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
840 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100841 vios->Stream() << StringPrintf("WARNING: "
842 "code offset 0x%08x is past end of file 0x%08zx.\n",
843 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700844 success = false;
845 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100846 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700847
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100848 vios->Stream() << "gc_map: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800849 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100850 vios->Stream() << StringPrintf("%p ", oat_method.GetGcMap());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700851 }
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800852 uint32_t gc_map_offset = oat_method.GetGcMapOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100853 vios->Stream() << StringPrintf("(offset=0x%08x)\n", gc_map_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700854 if (gc_map_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100855 vios->Stream() << StringPrintf("WARNING: "
856 "gc map table offset 0x%08x is past end of file 0x%08zx.\n",
857 gc_map_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700858 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800859 } else if (options_.dump_raw_gc_map_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100860 ScopedIndentation indent3(vios);
861 DumpGcMap(vios->Stream(), oat_method, code_item);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800862 }
863 }
864 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100865 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700866 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
867 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700868
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800869 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100870 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700871 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100872 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700873 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100874 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700875 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
876 method_header_offset, oat_file_.Size());
877 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100878 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700879 return false;
880 }
881
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100882 ScopedIndentation indent2(vios);
883 vios->Stream() << "mapping_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800884 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100885 vios->Stream() << StringPrintf("%p ", oat_method.GetMappingTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700886 }
887 uint32_t mapping_table_offset = oat_method.GetMappingTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100888 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700889 if (mapping_table_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100890 vios->Stream() << StringPrintf("WARNING: "
891 "mapping table offset 0x%08x is past end of file 0x%08zx. "
892 "mapping table offset was loaded from offset 0x%08x.\n",
893 mapping_table_offset, oat_file_.Size(),
894 oat_method.GetMappingTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700895 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800896 } else if (options_.dump_raw_mapping_table_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100897 ScopedIndentation indent3(vios);
898 DumpMappingTable(vios, oat_method);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700899 }
900
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100901 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800902 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100903 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700904 }
905 uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100906 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700907 if (vmap_table_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100908 vios->Stream() << StringPrintf("WARNING: "
909 "vmap table offset 0x%08x is past end of file 0x%08zx. "
910 "vmap table offset was loaded from offset 0x%08x.\n",
911 vmap_table_offset, oat_file_.Size(),
912 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700913 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800914 } else if (options_.dump_vmap_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100915 DumpVmapData(vios, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700916 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800917 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700918 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100919 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700920
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100921 ScopedIndentation indent2(vios);
922 vios->Stream()
923 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
924 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
925 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
926 vios->Stream() << "\n";
927 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
928 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
929 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700930 }
931 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100932 // Based on spill masks from QuickMethodFrameInfo so placed
933 // after it is dumped, but useful for understanding quick
934 // code, so dumped here.
935 ScopedIndentation indent2(vios);
936 DumpVregLocations(vios->Stream(), oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700937 }
938 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100939 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700940 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
941 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100942 ScopedIndentation indent2(vios);
943 vios->Stream() << StringPrintf("WARNING: "
944 "code size offset 0x%08x is past end of file 0x%08zx.",
945 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700946 success = false;
947 } else {
948 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700949 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
950 uint64_t aligned_code_end = aligned_code_begin + code_size;
951
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800952 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100953 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700954 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100955 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
956 code_offset,
957 code_size_offset,
958 code_size,
959 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700960
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100961 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700962 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100963 vios->Stream() << StringPrintf("WARNING: "
964 "start of code at 0x%08x is past end of file 0x%08zx.",
965 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700966 success = false;
967 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100968 vios->Stream() << StringPrintf(
969 "WARNING: "
970 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
971 "code size is 0x%08x loaded from offset 0x%08x.\n",
972 aligned_code_end, oat_file_.Size(),
973 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700974 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800975 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700976 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100977 DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700978 }
979 }
980 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100981 vios->Stream() << StringPrintf(
982 "WARNING: "
983 "code size %d is bigger than max expected threshold of %d. "
984 "code size is 0x%08x loaded from offset 0x%08x.\n",
985 code_size, kMaxCodeSize,
986 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700987 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800988 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700989 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100990 DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700991 }
992 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800993 } else if (options_.disassemble_code_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100994 DumpCode(vios, verifier.get(), oat_method, code_item, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700995 }
996 }
997 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100998 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700999 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001000 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001001
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001002 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1003 if (spill_mask == 0) {
1004 return;
1005 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001006 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001007 for (size_t i = 0; i < 32; i++) {
1008 if ((spill_mask & (1 << i)) != 0) {
1009 if (is_float) {
1010 os << "fr" << i;
1011 } else {
1012 os << "r" << i;
1013 }
1014 spill_mask ^= 1 << i; // clear bit
1015 if (spill_mask != 0) {
1016 os << ", ";
1017 } else {
1018 break;
1019 }
1020 }
1021 }
1022 os << ")";
1023 }
1024
Roland Levillain442b46a2015-02-18 16:54:21 +00001025 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001026 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001027 const OatFile::OatMethod& oat_method,
1028 const DexFile::CodeItem* code_item) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001029 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1030 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001031 const void* raw_code_info = oat_method.GetVmapTable();
1032 if (raw_code_info != nullptr) {
1033 CodeInfo code_info(raw_code_info);
1034 DCHECK(code_item != nullptr);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001035 ScopedIndentation indent1(vios);
1036 DumpCodeInfo(vios, code_info, oat_method, *code_item);
Roland Levillain442b46a2015-02-18 16:54:21 +00001037 }
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001038 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
1039 // We don't encode the size in the table, so just emit that we have quickened
1040 // information.
1041 ScopedIndentation indent(vios);
1042 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001043 } else {
1044 // Otherwise, display the vmap table.
1045 const uint8_t* raw_table = oat_method.GetVmapTable();
1046 if (raw_table != nullptr) {
1047 VmapTable vmap_table(raw_table);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001048 DumpVmapTable(vios->Stream(), oat_method, vmap_table);
Roland Levillain442b46a2015-02-18 16:54:21 +00001049 }
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001050 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001051 }
1052
1053 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001054 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001055 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001056 const OatFile::OatMethod& oat_method,
Roland Levillain442b46a2015-02-18 16:54:21 +00001057 const DexFile::CodeItem& code_item) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001058 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001059 oat_method.GetCodeOffset(),
1060 code_item.registers_size_,
1061 options_.dump_code_info_stack_maps_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001062 }
1063
Roland Levillain442b46a2015-02-18 16:54:21 +00001064 // Display a vmap table.
1065 void DumpVmapTable(std::ostream& os,
1066 const OatFile::OatMethod& oat_method,
1067 const VmapTable& vmap_table) {
1068 bool first = true;
1069 bool processing_fp = false;
1070 uint32_t spill_mask = oat_method.GetCoreSpillMask();
1071 for (size_t i = 0; i < vmap_table.Size(); i++) {
1072 uint16_t dex_reg = vmap_table[i];
1073 uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
1074 processing_fp ? kFloatVReg : kIntVReg);
1075 os << (first ? "v" : ", v") << dex_reg;
1076 if (!processing_fp) {
1077 os << "/r" << cpu_reg;
1078 } else {
1079 os << "/fr" << cpu_reg;
1080 }
1081 first = false;
1082 if (!processing_fp && dex_reg == 0xFFFF) {
1083 processing_fp = true;
1084 spill_mask = oat_method.GetFpSpillMask();
1085 }
1086 }
1087 os << "\n";
1088 }
1089
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001090 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1091 const DexFile::CodeItem* code_item) {
1092 if (code_item != nullptr) {
1093 size_t num_locals_ins = code_item->registers_size_;
1094 size_t num_ins = code_item->ins_size_;
1095 size_t num_locals = num_locals_ins - num_ins;
1096 size_t num_outs = code_item->outs_size_;
1097
1098 os << "vr_stack_locations:";
1099 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1100 // For readability, delimit the different kinds of VRs.
1101 if (reg == num_locals_ins) {
1102 os << "\n\tmethod*:";
1103 } else if (reg == num_locals && num_ins > 0) {
1104 os << "\n\tins:";
1105 } else if (reg == 0 && num_locals > 0) {
1106 os << "\n\tlocals:";
1107 }
1108
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001109 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1110 code_item,
1111 oat_method.GetCoreSpillMask(),
1112 oat_method.GetFpSpillMask(),
1113 oat_method.GetFrameSizeInBytes(),
1114 reg,
1115 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001116 os << " v" << reg << "[sp + #" << offset << "]";
1117 }
1118
1119 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1120 if (out_reg == 0) {
1121 os << "\n\touts:";
1122 }
1123
1124 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1125 os << " v" << out_reg << "[sp + #" << offset << "]";
1126 }
1127
1128 os << "\n";
1129 }
1130 }
1131
Ian Rogersb23a7722012-10-09 16:54:26 -07001132 void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001133 const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
Ian Rogers1809a722013-08-09 22:05:32 -07001134 const uint8_t* raw_table = oat_method.GetVmapTable();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001135 if (raw_table != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001136 const VmapTable vmap_table(raw_table);
1137 uint32_t vmap_offset;
Ian Rogers1809a722013-08-09 22:05:32 -07001138 if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001139 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
1140 uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
1141 : oat_method.GetCoreSpillMask();
1142 os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
Ian Rogersb23a7722012-10-09 16:54:26 -07001143 } else {
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001144 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1145 code_item,
1146 oat_method.GetCoreSpillMask(),
1147 oat_method.GetFpSpillMask(),
1148 oat_method.GetFrameSizeInBytes(),
1149 reg,
1150 GetInstructionSet());
Ian Rogersb23a7722012-10-09 16:54:26 -07001151 os << "[sp + #" << offset << "]";
1152 }
1153 }
1154 }
1155
Ian Rogersef7d42f2014-01-06 12:55:46 -08001156 void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
1157 const DexFile::CodeItem* code_item,
1158 size_t num_regs, const uint8_t* reg_bitmap) {
1159 bool first = true;
1160 for (size_t reg = 0; reg < num_regs; reg++) {
1161 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1162 if (first) {
1163 os << " v" << reg << " (";
1164 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1165 os << ")";
1166 first = false;
1167 } else {
1168 os << ", v" << reg << " (";
1169 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1170 os << ")";
1171 }
1172 }
1173 }
1174 if (first) {
1175 os << "No registers in GC map\n";
1176 } else {
1177 os << "\n";
1178 }
1179 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001180 void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
1181 const DexFile::CodeItem* code_item) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001182 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Ian Rogersef7d42f2014-01-06 12:55:46 -08001183 if (gc_map_raw == nullptr) {
1184 return; // No GC map.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001185 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001186 const void* quick_code = oat_method.GetQuickCode();
Elliott Hughes956af0f2014-12-11 14:34:28 -08001187 NativePcOffsetToReferenceMap map(gc_map_raw);
1188 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1189 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
1190 map.GetNativePcOffset(entry);
1191 os << StringPrintf("%p", native_pc);
1192 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001193 }
1194 }
1195
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001196 void DumpMappingTable(VariableIndentationOutputStream* vios,
1197 const OatFile::OatMethod& oat_method) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001198 const void* quick_code = oat_method.GetQuickCode();
1199 if (quick_code == nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -08001200 return;
1201 }
Ian Rogers1809a722013-08-09 22:05:32 -07001202 MappingTable table(oat_method.GetMappingTable());
1203 if (table.TotalSize() != 0) {
Ian Rogers1809a722013-08-09 22:05:32 -07001204 if (table.PcToDexSize() != 0) {
1205 typedef MappingTable::PcToDexIterator It;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001206 vios->Stream() << "suspend point mappings {\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001207 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001208 ScopedIndentation indent1(vios);
1209 vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001210 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001211 vios->Stream() << "}\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001212 }
1213 if (table.DexToPcSize() != 0) {
1214 typedef MappingTable::DexToPcIterator It;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001215 vios->Stream() << "catch entry mappings {\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001216 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001217 ScopedIndentation indent1(vios);
1218 vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001219 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001220 vios->Stream() << "}\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001221 }
1222 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001223 }
1224
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001225 uint32_t DumpInformationAtOffset(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001226 const OatFile::OatMethod& oat_method,
1227 const DexFile::CodeItem* code_item,
1228 size_t offset,
1229 bool suspend_point_mapping) {
1230 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1231 if (suspend_point_mapping) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001232 ScopedIndentation indent1(vios);
1233 DumpDexRegisterMapAtOffset(vios, oat_method, code_item, offset);
Roland Levillainf2650d12015-05-28 14:53:28 +01001234 }
1235 // The return value is not used in the case of a method compiled
1236 // with the optimizing compiler.
1237 return DexFile::kDexNoIndex;
1238 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001239 return DumpMappingAtOffset(vios->Stream(), oat_method, offset, suspend_point_mapping);
Roland Levillainf2650d12015-05-28 14:53:28 +01001240 }
1241 }
1242
Ian Rogers1809a722013-08-09 22:05:32 -07001243 uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1244 size_t offset, bool suspend_point_mapping) {
1245 MappingTable table(oat_method.GetMappingTable());
1246 if (suspend_point_mapping && table.PcToDexSize() > 0) {
1247 typedef MappingTable::PcToDexIterator It;
1248 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1249 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001250 os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001251 return cur.DexPc();
1252 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001253 }
Ian Rogers1809a722013-08-09 22:05:32 -07001254 } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
1255 typedef MappingTable::DexToPcIterator It;
1256 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1257 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001258 os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001259 return cur.DexPc();
Ian Rogersb23a7722012-10-09 16:54:26 -07001260 }
1261 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001262 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001263 return DexFile::kDexNoIndex;
Ian Rogersb23a7722012-10-09 16:54:26 -07001264 }
1265
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001266 void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1267 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001268 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001269 if (gc_map_raw != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001270 NativePcOffsetToReferenceMap map(gc_map_raw);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001271 if (map.HasEntry(native_pc_offset)) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001272 size_t num_regs = map.RegWidth() * 8;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001273 const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
Ian Rogersb23a7722012-10-09 16:54:26 -07001274 bool first = true;
1275 for (size_t reg = 0; reg < num_regs; reg++) {
1276 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1277 if (first) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001278 os << "GC map objects: v" << reg << " (";
1279 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001280 os << ")";
1281 first = false;
1282 } else {
1283 os << ", v" << reg << " (";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001284 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001285 os << ")";
1286 }
1287 }
1288 }
1289 if (!first) {
1290 os << "\n";
1291 }
1292 }
1293 }
1294 }
1295
Mathieu Chartier590fee92013-09-13 13:46:47 -07001296 void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
1297 const OatFile::OatMethod& oat_method,
1298 const DexFile::CodeItem* code_item, uint32_t dex_pc) {
1299 DCHECK(verifier != nullptr);
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001300 std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001301 bool first = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001302 for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
1303 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
1304 if (kind != kUndefined) {
1305 if (first) {
1306 os << "VRegs: v";
1307 first = false;
1308 } else {
1309 os << ", v";
1310 }
1311 os << reg << " (";
1312 switch (kind) {
1313 case kImpreciseConstant:
1314 os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
1315 DescribeVReg(os, oat_method, code_item, reg, kind);
1316 break;
1317 case kConstant:
1318 os << "Constant: " << kinds.at((reg * 2) + 1);
1319 break;
1320 default:
1321 DescribeVReg(os, oat_method, code_item, reg, kind);
1322 break;
1323 }
1324 os << ")";
1325 }
1326 }
1327 if (!first) {
1328 os << "\n";
1329 }
1330 }
1331
1332
Ian Rogersb23a7722012-10-09 16:54:26 -07001333 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001334 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001335 size_t i = 0;
1336 while (i < code_item->insns_size_in_code_units_) {
1337 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001338 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1339 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001340 i += instruction->SizeInCodeUnits();
1341 }
1342 }
1343 }
1344
Roland Levillainf2650d12015-05-28 14:53:28 +01001345 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1346 // the optimizing compiler?
1347 static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1348 const DexFile::CodeItem* code_item) {
1349 // If the native GC map is null and the Dex `code_item` is not
1350 // null, then this method has been compiled with the optimizing
1351 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001352 return oat_method.GetQuickCode() != nullptr &&
1353 oat_method.GetGcMap() == nullptr &&
1354 code_item != nullptr;
1355 }
1356
1357 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1358 // the dextodex compiler?
1359 static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
1360 const DexFile::CodeItem* code_item) {
1361 // If the quick code is null, the Dex `code_item` is not
1362 // null, and the vmap table is not null, then this method has been compiled
1363 // with the dextodex compiler.
1364 return oat_method.GetQuickCode() == nullptr &&
1365 oat_method.GetVmapTable() != nullptr &&
1366 code_item != nullptr;
Roland Levillainf2650d12015-05-28 14:53:28 +01001367 }
1368
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001369 void DumpDexRegisterMapAtOffset(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001370 const OatFile::OatMethod& oat_method,
1371 const DexFile::CodeItem* code_item,
1372 size_t offset) {
1373 // This method is only relevant for oat methods compiled with the
1374 // optimizing compiler.
1375 DCHECK(IsMethodGeneratedByOptimizingCompiler(oat_method, code_item));
1376
1377 // The optimizing compiler outputs its CodeInfo data in the vmap table.
1378 const void* raw_code_info = oat_method.GetVmapTable();
1379 if (raw_code_info != nullptr) {
1380 CodeInfo code_info(raw_code_info);
David Brazdilf677ebf2015-05-29 16:29:43 +01001381 StackMapEncoding encoding = code_info.ExtractEncoding();
1382 StackMap stack_map = code_info.GetStackMapForNativePcOffset(offset, encoding);
Roland Levillainf2650d12015-05-28 14:53:28 +01001383 if (stack_map.IsValid()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001384 stack_map.Dump(vios, code_info, encoding, oat_method.GetCodeOffset(),
1385 code_item->registers_size_);
Roland Levillainf2650d12015-05-28 14:53:28 +01001386 }
1387 }
1388 }
1389
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001390 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001391 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001392 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001393 const DexFile* dex_file,
1394 const DexFile::ClassDef& class_def,
1395 const DexFile::CodeItem* code_item,
1396 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001397 if ((method_access_flags & kAccNative) == 0) {
1398 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001399 Runtime* const runtime = Runtime::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001400 Handle<mirror::DexCache> dex_cache(
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001401 hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file,
1402 runtime->GetLinearAlloc())));
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001403 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001404 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001405 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
1406 &class_def, code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001407 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001408
1409 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001410 }
1411
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001412 void DumpCode(VariableIndentationOutputStream* vios,
1413 verifier::MethodVerifier* verifier,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001414 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1415 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001416 const void* quick_code = oat_method.GetQuickCode();
1417
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001418 if (code_size == 0) {
1419 code_size = oat_method.GetQuickCodeSize();
1420 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001421 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001422 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001423 return;
Elliott Hughes956af0f2014-12-11 14:34:28 -08001424 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001425 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1426 size_t offset = 0;
1427 while (offset < code_size) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001428 if (!bad_input) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001429 DumpInformationAtOffset(vios, oat_method, code_item, offset, false);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001430 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001431 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001432 if (!bad_input) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001433 uint32_t dex_pc =
1434 DumpInformationAtOffset(vios, oat_method, code_item, offset, true);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001435 if (dex_pc != DexFile::kDexNoIndex) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001436 DumpGcMapAtNativePcOffset(vios->Stream(), oat_method, code_item, offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001437 if (verifier != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001438 DumpVRegsAtDexPc(vios->Stream(), verifier, oat_method, code_item, dex_pc);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001439 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001440 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001441 }
1442 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001443 }
1444 }
1445
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001446 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001447 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001448 const OatDumperOptions& options_;
1449 uint32_t resolved_addr2instr_;
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001450 InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001451 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001452 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001453};
1454
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001455class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001456 public:
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001457 ImageDumper(std::ostream* os,
1458 gc::space::ImageSpace& image_space,
1459 const ImageHeader& image_header,
1460 OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001461 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001462 vios_(os),
1463 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001464 image_space_(image_space),
1465 image_header_(image_header),
1466 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001467
Mathieu Chartier90443472015-07-16 20:32:27 -07001468 bool Dump() SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001469 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001470 std::ostream& indent_os = vios_.Stream();
1471
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001472 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001473
Jeff Haodcdc85b2015-12-04 14:06:18 -08001474 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1475
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001476 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001477
Mathieu Chartiere401d142015-04-22 13:56:20 -07001478 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1479
1480 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1481 auto section = static_cast<ImageHeader::ImageSections>(i);
1482 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1483 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001484
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001485 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001486
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001487 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001488
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001489 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1490
1491 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1492
1493 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001494
Alex Lighta59dd802014-07-02 16:28:08 -07001495 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1496
Igor Murashkin46774762014-10-22 11:37:02 -07001497 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1498
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001499 {
1500 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001501 static_assert(arraysize(image_roots_descriptions_) ==
1502 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001503 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1504 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1505 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001506 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001507 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001508 if (image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001509 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001510 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001511 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001512 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1513 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001514 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001515 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1516 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001517 run++;
1518 } else {
1519 break;
1520 }
1521 }
1522 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001523 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001524 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001525 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001526 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001527 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001528 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001529 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001530 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001531 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001532 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001533 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001534 }
1535 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001536 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001537
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001538 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001539 os << "METHOD ROOTS\n";
1540 static_assert(arraysize(image_methods_descriptions_) ==
1541 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1542 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1543 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1544 const char* description = image_methods_descriptions_[i];
1545 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001546 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001547 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001548 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001549 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001550
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001551 Runtime* const runtime = Runtime::Current();
1552 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001553 std::string image_filename = image_space_.GetImageFilename();
1554 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001555 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001556 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001557 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001558 const OatFile* oat_file = image_space_.GetOatFile();
Alex Lighta59dd802014-07-02 16:28:08 -07001559 if (oat_file == nullptr) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001560 oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location);
1561 }
1562 if (oat_file == nullptr) {
1563 oat_file = OatFile::Open(oat_location,
1564 oat_location,
1565 nullptr,
1566 nullptr,
1567 false,
1568 /*low_4gb*/false,
1569 nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001570 &error_msg);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001571 }
1572 if (oat_file == nullptr) {
1573 os << "OAT FILE NOT FOUND: " << error_msg << "\n";
1574 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001575 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001576 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001577
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001578 stats_.oat_file_bytes = oat_file->Size();
1579
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001580 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001581
Mathieu Chartier02e25112013-08-14 16:14:24 -07001582 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001583 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001584 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1585 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001586 }
1587
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001588 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001589
Jeff Haodcdc85b2015-12-04 14:06:18 -08001590 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001591 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001592 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001593 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001594 {
1595 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1596 heap->FlushAllocStack();
1597 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001598 // Since FlushAllocStack() above resets the (active) allocation
1599 // stack. Need to revoke the thread-local allocation stacks that
1600 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001601 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001602 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001603 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001604 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001605 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001606 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001607 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001608 {
1609 ReaderMutexLock mu(self, *class_linker->DexLock());
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001610 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001611 mirror::DexCache* dex_cache =
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001612 down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001613 if (dex_cache != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01001614 dex_caches_.insert(dex_cache);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001615 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001616 }
1617 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001618 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001619 // Dump the normal objects before ArtMethods.
1620 image_space_.GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1621 indent_os << "\n";
1622 // TODO: Dump fields.
1623 // Dump methods after.
1624 const auto& methods_section = image_header_.GetMethodsSection();
Jeff Haodcdc85b2015-12-04 14:06:18 -08001625 DumpArtMethodVisitor visitor(this);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001626 methods_section.VisitPackedArtMethods(&visitor,
1627 image_space_.Begin(),
1628 image_header_.GetPointerSize());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001629 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001630 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001631 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001632 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001633 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001634 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001635 size_t data_size = image_header_.GetDataSize(); // stored size in file.
1636 if (file == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001637 LOG(WARNING) << "Failed to find image in " << image_filename;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001638 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001639 stats_.file_bytes = file->GetLength();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001640 // If the image is compressed, adjust to decompressed size.
1641 size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader);
1642 if (image_header_.GetStorageMode() == ImageHeader::kStorageModeUncompressed) {
1643 DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image";
1644 }
1645 stats_.file_bytes += uncompressed_size - data_size;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001646 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001647 size_t header_bytes = sizeof(ImageHeader);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001648 const auto& object_section = image_header_.GetImageSection(ImageHeader::kSectionObjects);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001649 const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1650 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001651 const auto& dex_cache_arrays_section = image_header_.GetImageSection(
1652 ImageHeader::kSectionDexCacheArrays);
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001653 const auto& intern_section = image_header_.GetImageSection(
1654 ImageHeader::kSectionInternedStrings);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001655 const auto& class_table_section = image_header_.GetImageSection(
1656 ImageHeader::kSectionClassTable);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001657 const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1658
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001659 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001660
1661 // Objects are kObjectAlignment-aligned.
1662 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1663 if (object_section.Offset() > header_bytes) {
1664 stats_.alignment_bytes += object_section.Offset() - header_bytes;
1665 }
1666
1667 // Field section is 4-byte aligned.
1668 constexpr size_t kFieldSectionAlignment = 4U;
1669 uint32_t end_objects = object_section.Offset() + object_section.Size();
1670 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
1671 stats_.alignment_bytes += field_section.Offset() - end_objects;
1672
1673 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
1674 uint32_t end_fields = field_section.Offset() + field_section.Size();
1675 CHECK_ALIGNED(method_section.Offset(), 4);
1676 stats_.alignment_bytes += method_section.Offset() - end_fields;
1677
1678 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
1679 uint32_t end_methods = method_section.Offset() + method_section.Size();
1680 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
1681 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
1682
1683 // Intern table is 8-byte aligned.
1684 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
1685 CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
1686 stats_.alignment_bytes += intern_section.Offset() - end_caches;
1687
1688 // Add space between intern table and class table.
1689 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
1690 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
1691
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001692 // Add space between end of image data and bitmap. Expect the bitmap to be page-aligned.
1693 const size_t bitmap_offset = sizeof(ImageHeader) + data_size;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001694 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001695 stats_.alignment_bytes += RoundUp(bitmap_offset, kPageSize) - bitmap_offset;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001696
Mathieu Chartiere401d142015-04-22 13:56:20 -07001697 stats_.bitmap_bytes += bitmap_section.Size();
1698 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01001699 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01001700 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001701 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001702 stats_.class_table_bytes += class_table_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001703 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001704 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001705
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001706 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001707
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001708 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001709 }
1710
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001711 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001712 class DumpArtMethodVisitor : public ArtMethodVisitor {
1713 public:
1714 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
1715
1716 virtual void Visit(ArtMethod* method) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
1717 std::ostream& indent_os = image_dumper_->vios_.Stream();
1718 indent_os << method << " " << " ArtMethod: " << PrettyMethod(method) << "\n";
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001719 image_dumper_->DumpMethod(method, indent_os);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001720 indent_os << "\n";
1721 }
1722
1723 private:
1724 ImageDumper* const image_dumper_;
1725 };
1726
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001727 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Mathieu Chartier90443472015-07-16 20:32:27 -07001728 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001729 CHECK(type != nullptr);
1730 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001731 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001732 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001733 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001734 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001735 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001736 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001737 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001738 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001739 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001740 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001741 }
1742 }
1743
Mathieu Chartierc7853442015-03-27 14:35:38 -07001744 static void PrintField(std::ostream& os, ArtField* field, mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -07001745 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001746 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08001747 switch (field->GetTypeAsPrimitiveType()) {
1748 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08001749 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001750 break;
1751 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001752 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001753 break;
1754 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001755 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001756 break;
1757 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001758 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001759 break;
1760 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07001761 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001762 break;
1763 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07001764 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001765 break;
1766 case Primitive::kPrimBoolean:
Fred Shih37f05ef2014-07-16 18:38:08 -07001767 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1768 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001769 break;
1770 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07001771 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001772 break;
1773 case Primitive::kPrimNot: {
1774 // Get the value, don't compute the type unless it is non-null as we don't want
1775 // to cause class loading.
1776 mirror::Object* value = field->GetObj(obj);
1777 if (value == nullptr) {
1778 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07001779 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08001780 // Grab the field type without causing resolution.
Mathieu Chartierdaaf3262015-03-24 13:30:28 -07001781 mirror::Class* field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08001782 if (field_type != nullptr) {
1783 PrettyObjectValue(os, field_type, value);
1784 } else {
1785 os << StringPrintf("%p %s\n", value,
1786 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
1787 }
Ian Rogers50239c72013-06-17 14:53:22 -07001788 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001789 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07001790 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001791 default:
1792 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
1793 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08001794 }
1795 }
1796
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001797 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Mathieu Chartier90443472015-07-16 20:32:27 -07001798 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001799 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001800 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001801 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001802 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001803 for (ArtField& field : klass->GetIFields()) {
1804 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001805 }
1806 }
1807
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001808 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001809 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001810 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001811
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001812 const void* GetQuickOatCodeBegin(ArtMethod* m) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001813 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001814 image_header_.GetPointerSize());
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001815 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001816 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001817 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001818 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001819 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001820 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001821 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001822 }
1823
Mathieu Chartiere401d142015-04-22 13:56:20 -07001824 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001825 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001826 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1827 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001828 return 0;
1829 }
1830 return oat_code_begin[-1];
1831 }
1832
Mathieu Chartiere401d142015-04-22 13:56:20 -07001833 const void* GetQuickOatCodeEnd(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001834 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001835 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001836 if (oat_code_begin == nullptr) {
1837 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001838 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001839 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001840 }
1841
Mathieu Chartier90443472015-07-16 20:32:27 -07001842 static void Callback(mirror::Object* obj, void* arg) SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001843 DCHECK(obj != nullptr);
1844 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001845 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001846 if (!state->InDumpSpace(obj)) {
1847 return;
1848 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001849
1850 size_t object_bytes = obj->SizeOf();
1851 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1852 state->stats_.object_bytes += object_bytes;
1853 state->stats_.alignment_bytes += alignment_bytes;
1854
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001855 std::ostream& os = state->vios_.Stream();
1856
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001857 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001858 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001859 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
1860 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001861 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001862 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001863 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
1864 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08001865 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001866 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001867 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001868 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001869 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001870 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001871 ScopedIndentation indent1(&state->vios_);
1872 DumpFields(os, obj, obj_class);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001873 const size_t image_pointer_size = state->image_header_.GetPointerSize();
Ian Rogersd5b32602012-02-26 16:40:04 -08001874 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001875 auto* obj_array = obj->AsObjectArray<mirror::Object>();
1876 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001877 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001878 size_t run = 0;
1879 for (int32_t j = i + 1; j < length; j++) {
1880 if (value == obj_array->Get(j)) {
1881 run++;
1882 } else {
1883 break;
1884 }
1885 }
1886 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001887 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001888 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001889 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001890 i = i + run;
1891 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001892 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001893 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001894 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001895 }
1896 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001897 mirror::Class* klass = obj->AsClass();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001898 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001899 os << "STATICS:\n";
1900 ScopedIndentation indent2(&state->vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001901 for (ArtField& field : klass->GetSFields()) {
1902 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001903 }
1904 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001905 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01001906 auto it = state->dex_caches_.find(obj);
1907 if (it != state->dex_caches_.end()) {
1908 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001909 const auto& field_section = state->image_header_.GetImageSection(
1910 ImageHeader::kSectionArtFields);
1911 const auto& method_section = state->image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001912 size_t num_methods = dex_cache->NumResolvedMethods();
1913 if (num_methods != 0u) {
1914 os << "Methods (size=" << num_methods << "):";
1915 ScopedIndentation indent2(&state->vios_);
1916 auto* resolved_methods = dex_cache->GetResolvedMethods();
1917 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001918 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_methods,
1919 i,
1920 image_pointer_size);
Vladimir Marko05792b92015-08-03 11:56:49 +01001921 size_t run = 0;
1922 for (size_t j = i + 1;
1923 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_methods,
1924 j,
1925 image_pointer_size);
1926 ++j, ++run) {}
1927 if (run == 0) {
1928 os << StringPrintf("%zd: ", i);
1929 } else {
1930 os << StringPrintf("%zd to %zd: ", i, i + run);
1931 i = i + run;
1932 }
1933 std::string msg;
1934 if (elem == nullptr) {
1935 msg = "null";
1936 } else if (method_section.Contains(
1937 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
1938 msg = PrettyMethod(reinterpret_cast<ArtMethod*>(elem));
1939 } else {
1940 msg = "<not in method section>";
1941 }
1942 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07001943 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001944 }
1945 size_t num_fields = dex_cache->NumResolvedFields();
1946 if (num_fields != 0u) {
1947 os << "Fields (size=" << num_fields << "):";
1948 ScopedIndentation indent2(&state->vios_);
1949 auto* resolved_fields = dex_cache->GetResolvedFields();
1950 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
1951 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_fields, i, image_pointer_size);
1952 size_t run = 0;
1953 for (size_t j = i + 1;
1954 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_fields,
1955 j,
1956 image_pointer_size);
1957 ++j, ++run) {}
1958 if (run == 0) {
1959 os << StringPrintf("%zd: ", i);
1960 } else {
1961 os << StringPrintf("%zd to %zd: ", i, i + run);
1962 i = i + run;
1963 }
1964 std::string msg;
1965 if (elem == nullptr) {
1966 msg = "null";
1967 } else if (field_section.Contains(
1968 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
1969 msg = PrettyField(reinterpret_cast<ArtField*>(elem));
1970 } else {
1971 msg = "<not in field section>";
1972 }
1973 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07001974 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001975 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001976 }
1977 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07001978 std::string temp;
1979 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001980 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001981
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001982 void DumpMethod(ArtMethod* method, std::ostream& indent_os)
Mathieu Chartier90443472015-07-16 20:32:27 -07001983 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001984 DCHECK(method != nullptr);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001985 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
1986 const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01001987 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
1988 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07001989 if (method->IsNative()) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01001990 if (!Runtime::Current()->GetClassLinker()->IsQuickGenericJniStub(quick_oat_code_begin)) {
1991 DCHECK(method_header->GetNativeGcMap() == nullptr) << PrettyMethod(method);
1992 DCHECK(method_header->GetMappingTable() == nullptr) << PrettyMethod(method);
1993 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001994 bool first_occurrence;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001995 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
1996 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001997 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001998 stats_.native_to_managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001999 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002000 if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
2001 image_header_.GetPointerSize())) {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002002 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002003 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002004 } else if (method->IsAbstract() ||
2005 method->IsCalleeSaveMethod() ||
2006 method->IsResolutionMethod() ||
2007 (method == Runtime::Current()->GetImtConflictMethod()) ||
2008 method->IsImtUnimplementedMethod() ||
2009 method->IsClassInitializer()) {
2010 // Don't print information for these.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002011 } else {
2012 const DexFile::CodeItem* code_item = method->GetCodeItem();
2013 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002014 stats_.dex_instruction_bytes += dex_instruction_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002015
2016 bool first_occurrence;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002017 size_t gc_map_bytes = ComputeOatSize(method_header->GetNativeGcMap(), &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002018 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002019 stats_.gc_map_bytes += gc_map_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002020 }
2021
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002022 size_t pc_mapping_table_bytes = ComputeOatSize(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002023 method_header->GetMappingTable(), &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002024 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002025 stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002026 }
2027
Roland Levillain6d7f1792015-07-02 10:59:15 +01002028 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002029 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01002030 // Method compiled with the optimizing compiler have no vmap table.
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002031 vmap_table_bytes = ComputeOatSize(method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002032 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002033 stats_.vmap_table_bytes += vmap_table_bytes;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002034 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002035 }
2036
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002037 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2038 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002039 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002040 stats_.managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002041 if (method->IsConstructor()) {
2042 if (method->IsStatic()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002043 stats_.class_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002044 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002045 stats_.large_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002046 }
2047 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002048 stats_.large_method_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002049 }
2050 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002051 stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002052
Igor Murashkin7617abd2015-07-10 18:27:47 -07002053 uint32_t method_access_flags = method->GetAccessFlags();
2054
Mathieu Chartiere401d142015-04-22 13:56:20 -07002055 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Igor Murashkin7617abd2015-07-10 18:27:47 -07002056 indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd AccessFlags=0x%x\n",
2057 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes,
2058 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002059
2060 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002061 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002062
2063 double expansion =
2064 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002065 stats_.ComputeOutliers(total_size, expansion, method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002066 }
2067 }
2068
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002069 std::set<const void*> already_seen_;
2070 // Compute the size of the given data within the oat file and whether this is the first time
2071 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002072 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002073 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002074 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002075 already_seen_.insert(oat_data);
2076 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002077 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002078 }
2079 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002080 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002081
2082 public:
2083 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002084 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002085 size_t file_bytes;
2086
2087 size_t header_bytes;
2088 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002089 size_t art_field_bytes;
2090 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01002091 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002092 size_t interned_strings_bytes;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002093 size_t class_table_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07002094 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002095 size_t alignment_bytes;
2096
2097 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002098 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002099 size_t managed_to_native_code_bytes;
2100 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07002101 size_t class_initializer_code_bytes;
2102 size_t large_initializer_code_bytes;
2103 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002104
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002105 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002106 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002107 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002108
2109 size_t dex_instruction_bytes;
2110
Mathieu Chartiere401d142015-04-22 13:56:20 -07002111 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002112 std::vector<size_t> method_outlier_size;
2113 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002114 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002115
Roland Levillain3887c462015-08-12 18:15:42 +01002116 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002117 : oat_file_bytes(0),
2118 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002119 header_bytes(0),
2120 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002121 art_field_bytes(0),
2122 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01002123 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002124 interned_strings_bytes(0),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002125 class_table_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07002126 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002127 alignment_bytes(0),
2128 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002129 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002130 managed_to_native_code_bytes(0),
2131 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07002132 class_initializer_code_bytes(0),
2133 large_initializer_code_bytes(0),
2134 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002135 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002136 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002137 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002138 dex_instruction_bytes(0) {}
2139
Elliott Hughesa0e18062012-04-13 15:59:59 -07002140 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002141 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002142 size_t bytes;
2143 size_t count;
2144 };
2145 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2146 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002147
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002148 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002149 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2150 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002151 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002152 it->second.count += 1;
2153 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002154 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002155 }
2156 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002157
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002158 double PercentOfOatBytes(size_t size) {
2159 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2160 }
2161
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002162 double PercentOfFileBytes(size_t size) {
2163 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2164 }
2165
2166 double PercentOfObjectBytes(size_t size) {
2167 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2168 }
2169
Mathieu Chartiere401d142015-04-22 13:56:20 -07002170 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002171 method_outlier_size.push_back(total_size);
2172 method_outlier_expansion.push_back(expansion);
2173 method_outlier.push_back(method);
2174 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002175
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002176 void DumpOutliers(std::ostream& os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002177 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002178 size_t sum_of_sizes = 0;
2179 size_t sum_of_sizes_squared = 0;
2180 size_t sum_of_expansion = 0;
2181 size_t sum_of_expansion_squared = 0;
2182 size_t n = method_outlier_size.size();
Jeff Haodcdc85b2015-12-04 14:06:18 -08002183 if (n == 0) {
2184 return;
2185 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002186 for (size_t i = 0; i < n; i++) {
2187 size_t cur_size = method_outlier_size[i];
2188 sum_of_sizes += cur_size;
2189 sum_of_sizes_squared += cur_size * cur_size;
2190 double cur_expansion = method_outlier_expansion[i];
2191 sum_of_expansion += cur_expansion;
2192 sum_of_expansion_squared += cur_expansion * cur_expansion;
2193 }
2194 size_t size_mean = sum_of_sizes / n;
2195 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2196 double expansion_mean = sum_of_expansion / n;
2197 double expansion_variance =
2198 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2199
2200 // Dump methods whose size is a certain number of standard deviations from the mean
2201 size_t dumped_values = 0;
2202 size_t skipped_values = 0;
2203 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2204 size_t cur_size_variance = i * i * size_variance;
2205 bool first = true;
2206 for (size_t j = 0; j < n; j++) {
2207 size_t cur_size = method_outlier_size[j];
2208 if (cur_size > size_mean) {
2209 size_t cur_var = cur_size - size_mean;
2210 cur_var = cur_var * cur_var;
2211 if (cur_var > cur_size_variance) {
2212 if (dumped_values > 20) {
2213 if (i == 1) {
2214 skipped_values++;
2215 } else {
2216 i = 2; // jump to counting for 1 standard deviation
2217 break;
2218 }
2219 } else {
2220 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002221 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002222 first = false;
2223 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002224 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002225 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002226 method_outlier_size[j] = 0; // don't consider this method again
2227 dumped_values++;
2228 }
2229 }
2230 }
2231 }
2232 }
2233 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002234 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002235 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002236 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002237 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002238
2239 // Dump methods whose expansion is a certain number of standard deviations from the mean
2240 dumped_values = 0;
2241 skipped_values = 0;
2242 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2243 double cur_expansion_variance = i * i * expansion_variance;
2244 bool first = true;
2245 for (size_t j = 0; j < n; j++) {
2246 double cur_expansion = method_outlier_expansion[j];
2247 if (cur_expansion > expansion_mean) {
2248 size_t cur_var = cur_expansion - expansion_mean;
2249 cur_var = cur_var * cur_var;
2250 if (cur_var > cur_expansion_variance) {
2251 if (dumped_values > 20) {
2252 if (i == 1) {
2253 skipped_values++;
2254 } else {
2255 i = 2; // jump to counting for 1 standard deviation
2256 break;
2257 }
2258 } else {
2259 if (first) {
2260 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002261 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002262 first = false;
2263 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002264 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002265 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002266 method_outlier_expansion[j] = 0.0; // don't consider this method again
2267 dumped_values++;
2268 }
2269 }
2270 }
2271 }
2272 }
2273 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002274 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002275 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002276 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002277 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002278 }
2279
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002280 void Dump(std::ostream& os, std::ostream& indent_os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002281 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002282 {
2283 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2284 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002285 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2286 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2287 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2288 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2289 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2290 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002291 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002292 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2293 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002294 header_bytes, PercentOfFileBytes(header_bytes),
2295 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002296 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2297 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002298 dex_cache_arrays_bytes,
2299 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002300 interned_strings_bytes,
2301 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002302 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002303 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002304 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2305 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002306 CHECK_EQ(file_bytes,
2307 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2308 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2309 bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002310 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002311
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002312 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002313 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002314 for (const auto& sizes_and_count : sizes_and_counts) {
2315 const std::string& descriptor(sizes_and_count.first);
2316 double average = static_cast<double>(sizes_and_count.second.bytes) /
2317 static_cast<double>(sizes_and_count.second.count);
2318 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002319 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002320 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002321 descriptor.c_str(), sizes_and_count.second.bytes,
2322 sizes_and_count.second.count, average, percent);
2323 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002324 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002325 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002326 CHECK_EQ(object_bytes, object_bytes_total);
2327
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002328 os << StringPrintf("oat_file_bytes = %8zd\n"
2329 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2330 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2331 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2332 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2333 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2334 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002335 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002336 managed_code_bytes,
2337 PercentOfOatBytes(managed_code_bytes),
2338 managed_to_native_code_bytes,
2339 PercentOfOatBytes(managed_to_native_code_bytes),
2340 native_to_managed_code_bytes,
2341 PercentOfOatBytes(native_to_managed_code_bytes),
2342 class_initializer_code_bytes,
2343 PercentOfOatBytes(class_initializer_code_bytes),
2344 large_initializer_code_bytes,
2345 PercentOfOatBytes(large_initializer_code_bytes),
2346 large_method_code_bytes,
2347 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002348 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002349 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002350 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002351 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2352 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002353 }
2354
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002355 os << "\n" << StringPrintf("gc_map_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2356 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2357 "vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002358 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
2359 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
2360 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002361 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002362
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002363 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2364 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002365 static_cast<double>(managed_code_bytes) /
2366 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002367 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002368 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002369 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002370
2371 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002372 }
2373 } stats_;
2374
2375 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002376 enum {
2377 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2378 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2379 kLargeConstructorDexBytes = 4000,
2380 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2381 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2382 kLargeMethodDexBytes = 16000
2383 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002384
2385 // For performance, use the *os_ directly for anything that doesn't need indentation
2386 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002387 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002388 VariableIndentationOutputStream vios_;
2389 ScopedIndentation indent1_;
2390
Ian Rogers1d54e732013-05-02 21:10:01 -07002391 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002392 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002393 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002394 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002395 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002396
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002397 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002398};
2399
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002400static int DumpImage(gc::space::ImageSpace* image_space,
2401 OatDumperOptions* options,
2402 std::ostream* os) SHARED_REQUIRES(Locks::mutator_lock_) {
2403 const ImageHeader& image_header = image_space->GetImageHeader();
2404 if (!image_header.IsValid()) {
2405 fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
2406 return EXIT_FAILURE;
2407 }
2408 ImageDumper image_dumper(os, *image_space, image_header, options);
2409 if (!image_dumper.Dump()) {
2410 return EXIT_FAILURE;
2411 }
2412 return EXIT_SUCCESS;
2413}
2414
2415static int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002416 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002417 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002418 options->class_loader_ = &null_class_loader;
2419
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002420 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002421 if (options->app_image_ != nullptr) {
2422 if (options->app_oat_ == nullptr) {
2423 LOG(ERROR) << "Can not dump app image without app oat file";
Jeff Haodcdc85b2015-12-04 14:06:18 -08002424 return EXIT_FAILURE;
2425 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002426 // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
2427 // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
2428 // pointers into 32 bit pointer sized ArtMethods.
2429 std::string error_msg;
2430 std::unique_ptr<OatFile> oat_file(OatFile::Open(options->app_oat_,
2431 options->app_oat_,
2432 nullptr,
2433 nullptr,
2434 false,
2435 /*low_4gb*/true,
2436 nullptr,
2437 &error_msg));
2438 if (oat_file == nullptr) {
2439 LOG(ERROR) << "Failed to open oat file " << options->app_oat_ << " with error " << error_msg;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002440 return EXIT_FAILURE;
2441 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002442 std::unique_ptr<gc::space::ImageSpace> space(
2443 gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg));
2444 if (space == nullptr) {
2445 LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error "
2446 << error_msg;
2447 }
2448 // Open dex files for the image.
2449 std::vector<std::unique_ptr<const DexFile>> dex_files;
2450 if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
2451 LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
2452 << error_msg;
2453 }
2454 // Dump the actual image.
2455 int result = DumpImage(space.get(), options, os);
2456 if (result != EXIT_SUCCESS) {
2457 return result;
2458 }
2459 // Fall through to dump the boot images.
2460 }
2461
2462 gc::Heap* heap = runtime->GetHeap();
2463 CHECK(heap->HasBootImageSpace()) << "No image spaces";
2464 for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
2465 int result = DumpImage(image_space, options, os);
2466 if (result != EXIT_SUCCESS) {
2467 return result;
2468 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002469 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002470 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002471}
2472
Andreas Gampe00b25f32014-09-17 21:49:05 -07002473static int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
2474 std::ostream* os) {
2475 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2476
2477 Thread* self = Thread::Current();
2478 CHECK(self != nullptr);
2479 // Need well-known-classes.
2480 WellKnownClasses::Init(self->GetJniEnv());
2481
2482 // Need to register dex files to get a working dex cache.
2483 ScopedObjectAccess soa(self);
2484 ClassLinker* class_linker = runtime->GetClassLinker();
Mathieu Chartierd57d4542015-10-14 10:55:30 -07002485 runtime->GetOatFileManager().RegisterOatFile(std::unique_ptr<const OatFile>(oat_file));
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002486 std::vector<const DexFile*> class_path;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002487 for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
2488 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002489 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002490 CHECK(dex_file != nullptr) << error_msg;
Mathieu Chartierd57d4542015-10-14 10:55:30 -07002491 class_linker->RegisterDexFile(*dex_file, runtime->GetLinearAlloc());
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002492 class_path.push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002493 }
2494
2495 // Need a class loader.
Andreas Gampe00b25f32014-09-17 21:49:05 -07002496 // Fake that we're a compiler.
Mathieu Chartier966878d2016-01-14 14:33:29 -08002497 jobject class_loader = class_linker->CreatePathClassLoader(self, class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002498
2499 // Use the class loader while dumping.
2500 StackHandleScope<1> scope(self);
2501 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
2502 soa.Decode<mirror::ClassLoader*>(class_loader));
2503 options->class_loader_ = &loader_handle;
2504
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002505 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002506 bool success = oat_dumper.Dump(*os);
2507 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2508}
2509
2510static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002511 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002512 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002513 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002514 options->class_loader_ = &null_class_loader;
2515
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002516 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002517 bool success = oat_dumper.Dump(*os);
2518 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2519}
2520
2521static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2522 std::ostream* os) {
2523 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002524 OatFile* oat_file = OatFile::Open(oat_filename,
2525 oat_filename,
2526 nullptr,
2527 nullptr,
2528 false,
2529 /*low_4gb*/false,
2530 nullptr,
2531 &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002532 if (oat_file == nullptr) {
2533 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2534 return EXIT_FAILURE;
2535 }
2536
2537 if (runtime != nullptr) {
2538 return DumpOatWithRuntime(runtime, oat_file, options, os);
2539 } else {
2540 return DumpOatWithoutRuntime(oat_file, options, os);
2541 }
2542}
2543
David Srbecky2fdd03c2016-03-10 15:32:37 +00002544static int SymbolizeOat(const char* oat_filename, std::string& output_name, bool no_bits) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002545 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002546 OatFile* oat_file = OatFile::Open(oat_filename,
2547 oat_filename,
2548 nullptr,
2549 nullptr,
2550 false,
2551 /*low_4gb*/false,
2552 nullptr,
2553 &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002554 if (oat_file == nullptr) {
2555 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2556 return EXIT_FAILURE;
2557 }
2558
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002559 bool result;
2560 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
2561 // files for 64-bit code in the past.
2562 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002563 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file, output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002564 result = oat_symbolizer.Symbolize();
2565 } else {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002566 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file, output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002567 result = oat_symbolizer.Symbolize();
2568 }
2569 if (!result) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002570 fprintf(stderr, "Failed to symbolize\n");
2571 return EXIT_FAILURE;
2572 }
2573
2574 return EXIT_SUCCESS;
2575}
2576
Igor Murashkin37743352014-11-13 14:38:00 -08002577struct OatdumpArgs : public CmdlineArgs {
2578 protected:
2579 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002580
Igor Murashkin37743352014-11-13 14:38:00 -08002581 virtual ParseStatus ParseCustom(const StringPiece& option,
2582 std::string* error_msg) OVERRIDE {
2583 {
2584 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
2585 if (base_parse != kParseUnknownArgument) {
2586 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002587 }
2588 }
2589
Igor Murashkin37743352014-11-13 14:38:00 -08002590 if (option.starts_with("--oat-file=")) {
2591 oat_filename_ = option.substr(strlen("--oat-file=")).data();
2592 } else if (option.starts_with("--image=")) {
2593 image_location_ = option.substr(strlen("--image=")).data();
2594 } else if (option =="--dump:raw_mapping_table") {
2595 dump_raw_mapping_table_ = true;
2596 } else if (option == "--dump:raw_gc_map") {
2597 dump_raw_gc_map_ = true;
2598 } else if (option == "--no-dump:vmap") {
2599 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01002600 } else if (option =="--dump:code_info_stack_maps") {
2601 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002602 } else if (option == "--no-disassemble") {
2603 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00002604 } else if (option =="--header-only") {
2605 dump_header_only_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002606 } else if (option.starts_with("--symbolize=")) {
2607 oat_filename_ = option.substr(strlen("--symbolize=")).data();
2608 symbolize_ = true;
David Srbecky2fdd03c2016-03-10 15:32:37 +00002609 } else if (option.starts_with("--only-keep-debug")) {
2610 only_keep_debug_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002611 } else if (option.starts_with("--class-filter=")) {
2612 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002613 } else if (option.starts_with("--method-filter=")) {
2614 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002615 } else if (option.starts_with("--list-classes")) {
2616 list_classes_ = true;
2617 } else if (option.starts_with("--list-methods")) {
2618 list_methods_ = true;
2619 } else if (option.starts_with("--export-dex-to=")) {
2620 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
2621 } else if (option.starts_with("--addr2instr=")) {
2622 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
2623 *error_msg = "Address conversion failed";
2624 return kParseError;
2625 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002626 } else if (option.starts_with("--app-image=")) {
2627 app_image_ = option.substr(strlen("--app-image=")).data();
2628 } else if (option.starts_with("--app-oat=")) {
2629 app_oat_ = option.substr(strlen("--app-oat=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002630 } else {
2631 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002632 }
2633
Igor Murashkin37743352014-11-13 14:38:00 -08002634 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002635 }
2636
Igor Murashkin37743352014-11-13 14:38:00 -08002637 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
2638 // Infer boot image location from the image location if possible.
2639 if (boot_image_location_ == nullptr) {
2640 boot_image_location_ = image_location_;
2641 }
2642
2643 // Perform the parent checks.
2644 ParseStatus parent_checks = Base::ParseChecks(error_msg);
2645 if (parent_checks != kParseOk) {
2646 return parent_checks;
2647 }
2648
2649 // Perform our own checks.
2650 if (image_location_ == nullptr && oat_filename_ == nullptr) {
2651 *error_msg = "Either --image or --oat-file must be specified";
2652 return kParseError;
2653 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
2654 *error_msg = "Either --image or --oat-file must be specified but not both";
2655 return kParseError;
2656 }
2657
2658 return kParseOk;
2659 }
2660
2661 virtual std::string GetUsage() const {
2662 std::string usage;
2663
2664 usage +=
2665 "Usage: oatdump [options] ...\n"
2666 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
2667 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
2668 "\n"
2669 // Either oat-file or image is required.
2670 " --oat-file=<file.oat>: specifies an input oat filename.\n"
2671 " Example: --oat-file=/system/framework/boot.oat\n"
2672 "\n"
2673 " --image=<file.art>: specifies an input image location.\n"
2674 " Example: --image=/system/framework/boot.art\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002675 "\n"
2676 " --app-image=<file.art>: specifies an input app image. Must also have a specified\n"
2677 " boot image and app oat file.\n"
2678 " Example: --app-image=app.art\n"
2679 "\n"
2680 " --app-oat=<file.odex>: specifies an input app oat.\n"
2681 " Example: --app-oat=app.odex\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002682 "\n";
2683
2684 usage += Base::GetUsage();
2685
2686 usage += // Optional.
2687 " --dump:raw_mapping_table enables dumping of the mapping table.\n"
2688 " Example: --dump:raw_mapping_table\n"
2689 "\n"
Mathieu Chartier19510f02015-05-26 14:44:35 -07002690 " --dump:raw_gc_map enables dumping of the GC map.\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002691 " Example: --dump:raw_gc_map\n"
2692 "\n"
2693 " --no-dump:vmap may be used to disable vmap dumping.\n"
2694 " Example: --no-dump:vmap\n"
2695 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01002696 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
2697 " Example: --dump:code_info_stack_maps\n"
2698 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002699 " --no-disassemble may be used to disable disassembly.\n"
2700 " Example: --no-disassemble\n"
2701 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00002702 " --header-only may be used to print only the oat header.\n"
2703 " Example: --header-only\n"
2704 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002705 " --list-classes may be used to list target file classes (can be used with filters).\n"
2706 " Example: --list-classes\n"
2707 " Example: --list-classes --class-filter=com.example.foo\n"
2708 "\n"
2709 " --list-methods may be used to list target file methods (can be used with filters).\n"
2710 " Example: --list-methods\n"
2711 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
2712 "\n"
2713 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
2714 " Example: --symbolize=/system/framework/boot.oat\n"
2715 "\n"
David Srbecky2fdd03c2016-03-10 15:32:37 +00002716 " --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
2717 " .rodata and .text sections are omitted in the output file to save space.\n"
2718 " Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
2719 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002720 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
2721 " Example: --class-filter=com.example.foo\n"
2722 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002723 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
2724 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002725 "\n"
2726 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
2727 " Example: --export-dex-to=/data/local/tmp\n"
2728 "\n"
2729 " --addr2instr=<address>: output matching method disassembled code from relative\n"
2730 " address (e.g. PC from crash dump)\n"
2731 " Example: --addr2instr=0x00001a3b\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002732 "\n";
2733
2734 return usage;
2735 }
2736
2737 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07002738 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002739 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00002740 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07002741 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002742 std::string elf_filename_prefix_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002743 bool dump_raw_mapping_table_ = false;
2744 bool dump_raw_gc_map_ = false;
2745 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01002746 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002747 bool disassemble_code_ = true;
2748 bool symbolize_ = false;
David Srbecky2fdd03c2016-03-10 15:32:37 +00002749 bool only_keep_debug_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002750 bool list_classes_ = false;
2751 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00002752 bool dump_header_only_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002753 uint32_t addr2instr_ = 0;
2754 const char* export_dex_location_ = nullptr;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002755 const char* app_image_ = nullptr;
2756 const char* app_oat_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002757};
2758
Igor Murashkin37743352014-11-13 14:38:00 -08002759struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
2760 virtual bool NeedsRuntime() OVERRIDE {
2761 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002762
Igor Murashkin37743352014-11-13 14:38:00 -08002763 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
2764 bool absolute_addresses = (args_->oat_filename_ == nullptr);
2765
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002766 oat_dumper_options_.reset(new OatDumperOptions(
Igor Murashkin37743352014-11-13 14:38:00 -08002767 args_->dump_raw_mapping_table_,
2768 args_->dump_raw_gc_map_,
2769 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01002770 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08002771 args_->disassemble_code_,
2772 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002773 args_->class_filter_,
2774 args_->method_filter_,
2775 args_->list_classes_,
2776 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00002777 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002778 args_->export_dex_location_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002779 args_->app_image_,
2780 args_->app_oat_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002781 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08002782
2783 return (args_->boot_image_location_ != nullptr || args_->image_location_ != nullptr) &&
2784 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002785 }
2786
Igor Murashkin37743352014-11-13 14:38:00 -08002787 virtual bool ExecuteWithoutRuntime() OVERRIDE {
2788 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08002789 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002790
Mathieu Chartierd424d082014-10-15 10:31:46 -07002791 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08002792
Andreas Gampec24f3992014-12-17 20:40:11 -08002793 if (args_->symbolize_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002794 // ELF has special kind of section called SHT_NOBITS which allows us to create
2795 // sections which exist but their data is omitted from the ELF file to save space.
2796 // This is what "strip --only-keep-debug" does when it creates separate ELF file
2797 // with only debug data. We use it in similar way to exclude .rodata and .text.
2798 bool no_bits = args_->only_keep_debug_;
2799 return SymbolizeOat(args_->oat_filename_, args_->output_name_, no_bits) == EXIT_SUCCESS;
Andreas Gampec24f3992014-12-17 20:40:11 -08002800 } else {
2801 return DumpOat(nullptr,
2802 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002803 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08002804 args_->os_) == EXIT_SUCCESS;
2805 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002806 }
2807
Igor Murashkin37743352014-11-13 14:38:00 -08002808 virtual bool ExecuteWithRuntime(Runtime* runtime) {
2809 CHECK(args_ != nullptr);
2810
2811 if (args_->oat_filename_ != nullptr) {
2812 return DumpOat(runtime,
2813 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002814 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08002815 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002816 }
Igor Murashkin37743352014-11-13 14:38:00 -08002817
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002818 return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002819 }
2820
Igor Murashkin37743352014-11-13 14:38:00 -08002821 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
2822};
Andreas Gampe00b25f32014-09-17 21:49:05 -07002823
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002824} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07002825
2826int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08002827 art::OatdumpMain main;
2828 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002829}