blob: 48a9d912fe867aed917758d0396a819d8bf32368 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrom78128a62011-09-15 17:21:19 -070016
17#include <stdio.h>
18#include <stdlib.h>
19
Brian Carlstrom27ec9612011-09-19 20:20:38 -070020#include <fstream>
21#include <iostream>
Igor Murashkin37743352014-11-13 14:38:00 -080022#include <map>
23#include <set>
Brian Carlstrom78128a62011-09-15 17:21:19 -070024#include <string>
Andreas Gampe54fc26c2014-09-04 21:47:42 -070025#include <unordered_map>
Brian Carlstrom78128a62011-09-15 17:21:19 -070026#include <vector>
27
Ian Rogersd582fa42014-11-05 23:46:43 -080028#include "arch/instruction_set_features.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070029#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070030#include "art_method-inl.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000031#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080032#include "base/unix_file/fd_file.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070033#include "class_linker.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "class_linker-inl.h"
David Srbecky5d950762016-03-07 20:47:29 +000035#include "debug/elf_debug_writer.h"
36#include "debug/method_debug_info.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070037#include "dex_file-inl.h"
Elliott Hughese3c845c2012-02-28 17:23:01 -080038#include "dex_instruction.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080039#include "disassembler.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070040#include "elf_builder.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080041#include "gc_map.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070042#include "gc/space/image_space.h"
43#include "gc/space/large_object_space.h"
44#include "gc/space/space-inl.h"
Mathieu Chartier4a26f172016-01-26 14:26:18 -080045#include "image-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080046#include "indenter.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000047#include "linker/buffered_output_stream.h"
48#include "linker/file_output_stream.h"
Ian Rogers1809a722013-08-09 22:05:32 -070049#include "mapping_table.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080050#include "mirror/array-inl.h"
51#include "mirror/class-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010052#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080053#include "mirror/object-inl.h"
54#include "mirror/object_array-inl.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080055#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010056#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070057#include "oat_file_manager.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080058#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070059#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070060#include "scoped_thread_state_change.h"
Nicolas Geoffray6bc43742015-10-12 18:11:10 +010061#include "stack_map.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070062#include "ScopedLocalRef.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080063#include "thread_list.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080064#include "verifier/dex_gc_map.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080065#include "verifier/method_verifier.h"
Ian Rogers1809a722013-08-09 22:05:32 -070066#include "vmap_table.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070067#include "well_known_classes.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070068
Igor Murashkin37743352014-11-13 14:38:00 -080069#include <sys/stat.h>
70#include "cmdline.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070071
Igor Murashkin37743352014-11-13 14:38:00 -080072namespace art {
Brian Carlstrom78128a62011-09-15 17:21:19 -070073
Mathieu Chartiere401d142015-04-22 13:56:20 -070074const char* image_methods_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -080075 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -070076 "kImtConflictMethod",
Mathieu Chartier2d2621a2014-10-23 16:48:06 -070077 "kImtUnimplementedMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070078 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -070079 "kRefsOnlySaveMethod",
80 "kRefsAndArgsSaveMethod",
Mathieu Chartiere401d142015-04-22 13:56:20 -070081};
82
83const char* image_roots_descriptions_[] = {
Brian Carlstrom58ae9412011-10-04 00:56:06 -070084 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070085 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070086};
87
Mathieu Chartierac8f4392015-08-27 13:54:20 -070088// Map is so that we don't allocate multiple dex files for the same OatDexFile.
89static std::map<const OatFile::OatDexFile*,
90 std::unique_ptr<const DexFile>> opened_dex_files;
91
92const DexFile* OpenDexFile(const OatFile::OatDexFile* oat_dex_file, std::string* error_msg) {
93 DCHECK(oat_dex_file != nullptr);
94 auto it = opened_dex_files.find(oat_dex_file);
95 if (it != opened_dex_files.end()) {
96 return it->second.get();
97 }
98 const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
99 opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
100 return ret;
101}
102
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800103template <typename ElfTypes>
David Srbeckybc90fd02015-04-22 19:40:27 +0100104class OatSymbolizer FINAL {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700105 public:
David Srbecky2fdd03c2016-03-10 15:32:37 +0000106 OatSymbolizer(const OatFile* oat_file, const std::string& output_name, bool no_bits) :
107 oat_file_(oat_file),
108 builder_(nullptr),
109 output_name_(output_name.empty() ? "symbolized.oat" : output_name),
110 no_bits_(no_bits) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700111 }
112
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700113 bool Symbolize() {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000114 const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
David Srbecky5d811202016-03-08 13:21:22 +0000115 const InstructionSetFeatures* features = InstructionSetFeatures::FromBitmap(
116 isa, oat_file_->GetOatHeader().GetInstructionSetFeaturesBitmap());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000117
118 File* elf_file = OS::CreateEmptyFile(output_name_.c_str());
119 std::unique_ptr<BufferedOutputStream> output_stream(
Vladimir Marko10c13562015-11-25 14:33:36 +0000120 MakeUnique<BufferedOutputStream>(MakeUnique<FileOutputStream>(elf_file)));
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800121 builder_.reset(new ElfBuilder<ElfTypes>(isa, features, output_stream.get()));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000122
123 builder_->Start();
124
125 auto* rodata = builder_->GetRoData();
126 auto* text = builder_->GetText();
127 auto* bss = builder_->GetBss();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000128
David Srbecky6d8c8f02015-10-26 10:57:09 +0000129 const uint8_t* rodata_begin = oat_file_->Begin();
130 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
David Srbecky2fdd03c2016-03-10 15:32:37 +0000131 if (no_bits_) {
132 rodata->WriteNoBitsSection(rodata_size);
133 } else {
134 rodata->Start();
135 rodata->WriteFully(rodata_begin, rodata_size);
136 rodata->End();
137 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000138
David Srbecky6d8c8f02015-10-26 10:57:09 +0000139 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
140 const size_t text_size = oat_file_->End() - text_begin;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000141 if (no_bits_) {
142 text->WriteNoBitsSection(text_size);
143 } else {
144 text->Start();
145 text->WriteFully(text_begin, text_size);
146 text->End();
147 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000148
149 if (oat_file_->BssSize() != 0) {
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000150 bss->WriteNoBitsSection(oat_file_->BssSize());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000151 }
152
Douglas Leung316a2182015-09-17 15:26:25 -0700153 if (isa == kMips || isa == kMips64) {
154 builder_->WriteMIPSabiflagsSection();
155 }
Vladimir Marko944da602016-02-19 12:27:55 +0000156 builder_->PrepareDynamicSection(
157 elf_file->GetPath(), rodata_size, text_size, oat_file_->BssSize());
158 builder_->WriteDynamicSection();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700159
David Srbecky5d950762016-03-07 20:47:29 +0000160 Walk();
161 for (const auto& trampoline : debug::MakeTrampolineInfos(oat_file_->GetOatHeader())) {
162 method_debug_infos_.push_back(trampoline);
163 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700164
David Srbecky5d950762016-03-07 20:47:29 +0000165 debug::WriteDebugInfo(builder_.get(),
166 ArrayRef<const debug::MethodDebugInfo>(method_debug_infos_),
167 dwarf::DW_DEBUG_FRAME_FORMAT,
168 true /* write_oat_patches */);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700169
David Srbecky6d8c8f02015-10-26 10:57:09 +0000170 builder_->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700171
Vladimir Marko10c13562015-11-25 14:33:36 +0000172 return builder_->Good();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700173 }
174
David Srbecky5d950762016-03-07 20:47:29 +0000175 void Walk() {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700176 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
177 for (size_t i = 0; i < oat_dex_files.size(); i++) {
178 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700179 CHECK(oat_dex_file != nullptr);
David Srbecky5d950762016-03-07 20:47:29 +0000180 WalkOatDexFile(oat_dex_file);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700181 }
182 }
183
David Srbecky5d950762016-03-07 20:47:29 +0000184 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700185 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700186 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
187 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700188 return;
189 }
190 for (size_t class_def_index = 0;
191 class_def_index < dex_file->NumClassDefs();
192 class_def_index++) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700193 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
194 OatClassType type = oat_class.GetType();
195 switch (type) {
196 case kOatClassAllCompiled:
197 case kOatClassSomeCompiled:
David Srbecky5d950762016-03-07 20:47:29 +0000198 WalkOatClass(oat_class, *dex_file, class_def_index);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700199 break;
200
201 case kOatClassNoneCompiled:
202 case kOatClassMax:
203 // Ignore.
204 break;
205 }
206 }
207 }
208
David Srbecky5d950762016-03-07 20:47:29 +0000209 void WalkOatClass(const OatFile::OatClass& oat_class,
210 const DexFile& dex_file,
211 uint32_t class_def_index) {
212 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700213 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700214 if (class_data == nullptr) { // empty class such as a marker interface?
215 return;
216 }
217 // Note: even if this is an interface or a native class, we still have to walk it, as there
218 // might be a static initializer.
219 ClassDataItemIterator it(dex_file, class_data);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700220 uint32_t class_method_idx = 0;
David Srbecky5d950762016-03-07 20:47:29 +0000221 for (; it.HasNextStaticField(); it.Next()) { /* skip */ }
222 for (; it.HasNextInstanceField(); it.Next()) { /* skip */ }
223 for (; it.HasNextDirectMethod() || it.HasNextVirtualMethod(); it.Next()) {
224 WalkOatMethod(oat_class.GetOatMethod(class_method_idx++),
225 dex_file,
226 class_def_index,
227 it.GetMemberIndex(),
228 it.GetMethodCodeItem(),
229 it.GetMethodAccessFlags());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700230 }
231 DCHECK(!it.HasNext());
232 }
233
David Srbecky5d950762016-03-07 20:47:29 +0000234 void WalkOatMethod(const OatFile::OatMethod& oat_method,
235 const DexFile& dex_file,
236 uint32_t class_def_index,
237 uint32_t dex_method_index,
238 const DexFile::CodeItem* code_item,
239 uint32_t method_access_flags) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700240 if ((method_access_flags & kAccAbstract) != 0) {
241 // Abstract method, no code.
242 return;
243 }
David Srbecky5d950762016-03-07 20:47:29 +0000244 const OatHeader& oat_header = oat_file_->GetOatHeader();
245 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
246 if (method_header == nullptr || method_header->GetCodeSize() == 0) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700247 // No code.
248 return;
249 }
250
David Srbecky5d950762016-03-07 20:47:29 +0000251 debug::MethodDebugInfo info = debug::MethodDebugInfo();
252 info.trampoline_name = nullptr;
253 info.dex_file = &dex_file;
254 info.class_def_index = class_def_index;
255 info.dex_method_index = dex_method_index;
256 info.access_flags = method_access_flags;
257 info.code_item = code_item;
258 info.isa = oat_header.GetInstructionSet();
259 info.deduped = !seen_offsets_.insert(oat_method.GetCodeOffset()).second;
260 info.is_native_debuggable = oat_header.IsNativeDebuggable();
261 info.is_optimized = method_header->IsOptimized();
262 info.is_code_address_text_relative = true;
263 info.code_address = oat_method.GetCodeOffset() - oat_header.GetExecutableOffset();
264 info.code_size = method_header->GetCodeSize();
265 info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
266 info.code_info = info.is_optimized ? method_header->GetOptimizedCodeInfoPtr() : nullptr;
267 info.cfi = ArrayRef<uint8_t>();
268 method_debug_infos_.push_back(info);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700269 }
270
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700271 private:
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700272 const OatFile* oat_file_;
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800273 std::unique_ptr<ElfBuilder<ElfTypes> > builder_;
David Srbecky5d950762016-03-07 20:47:29 +0000274 std::vector<debug::MethodDebugInfo> method_debug_infos_;
275 std::unordered_set<uint32_t> seen_offsets_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700276 const std::string output_name_;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000277 bool no_bits_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700278};
279
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700280class OatDumperOptions {
281 public:
282 OatDumperOptions(bool dump_raw_mapping_table,
283 bool dump_raw_gc_map,
284 bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100285 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700286 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700287 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800288 const char* class_filter,
289 const char* method_filter,
290 bool list_classes,
291 bool list_methods,
David Brazdilc03d7b62016-03-02 12:18:03 +0000292 bool dump_header_only,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800293 const char* export_dex_location,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800294 const char* app_image,
295 const char* app_oat,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800296 uint32_t addr2instr)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700297 : dump_raw_mapping_table_(dump_raw_mapping_table),
298 dump_raw_gc_map_(dump_raw_gc_map),
299 dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100300 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700301 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700302 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800303 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000304 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800305 list_classes_(list_classes),
306 list_methods_(list_methods),
David Brazdilc03d7b62016-03-02 12:18:03 +0000307 dump_header_only_(dump_header_only),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800308 export_dex_location_(export_dex_location),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800309 app_image_(app_image),
310 app_oat_(app_oat),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800311 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800312 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700313
314 const bool dump_raw_mapping_table_;
315 const bool dump_raw_gc_map_;
316 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100317 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700318 const bool disassemble_code_;
319 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800320 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000321 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800322 const bool list_classes_;
323 const bool list_methods_;
David Brazdilc03d7b62016-03-02 12:18:03 +0000324 const bool dump_header_only_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800325 const char* const export_dex_location_;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800326 const char* const app_image_;
327 const char* const app_oat_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800328 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700329 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700330};
331
Elliott Hughese3c845c2012-02-28 17:23:01 -0800332class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700333 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100334 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000335 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800336 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700337 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800338 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800339 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
340 disassembler_(Disassembler::Create(instruction_set_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800341 new DisassemblerOptions(options_.absolute_addresses_,
Alexandre Ramesa37d9252014-10-27 11:28:14 +0000342 oat_file.Begin(),
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100343 true /* can_read_literals_ */))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800344 CHECK(options_.class_loader_ != nullptr);
345 CHECK(options_.class_filter_ != nullptr);
346 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800347 AddAllOffsets();
348 }
349
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700350 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700351 delete disassembler_;
352 }
353
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800354 InstructionSet GetInstructionSet() {
355 return instruction_set_;
356 }
357
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700358 bool Dump(std::ostream& os) {
359 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800360 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700361
362 os << "MAGIC:\n";
363 os << oat_header.GetMagic() << "\n\n";
364
Jeff Hao4b07a6e2016-01-15 12:50:44 -0800365 os << "LOCATION:\n";
366 os << oat_file_.GetLocation() << "\n\n";
367
Brian Carlstromaded5f72011-10-07 17:15:04 -0700368 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800369 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700370
Elliott Hughesa72ec822012-03-05 17:12:22 -0800371 os << "INSTRUCTION SET:\n";
372 os << oat_header.GetInstructionSet() << "\n\n";
373
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700374 {
375 std::unique_ptr<const InstructionSetFeatures> features(
376 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
377 oat_header.GetInstructionSetFeaturesBitmap()));
378 os << "INSTRUCTION SET FEATURES:\n";
379 os << features->GetFeatureString() << "\n\n";
380 }
Dave Allison70202782013-10-22 17:52:19 -0700381
Brian Carlstromaded5f72011-10-07 17:15:04 -0700382 os << "DEX FILE COUNT:\n";
383 os << oat_header.GetDexFileCount() << "\n\n";
384
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800385#define DUMP_OAT_HEADER_OFFSET(label, offset) \
386 os << label " OFFSET:\n"; \
387 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800388 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800389 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
390 } \
391 os << StringPrintf("\n\n");
392
393 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
394 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
395 GetInterpreterToInterpreterBridgeOffset);
396 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
397 GetInterpreterToCompiledCodeBridgeOffset);
398 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
399 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800400 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
401 GetQuickGenericJniTrampolineOffset);
402 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
403 GetQuickImtConflictTrampolineOffset);
404 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
405 GetQuickResolutionTrampolineOffset);
406 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
407 GetQuickToInterpreterBridgeOffset);
408#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700409
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700410 os << "IMAGE PATCH DELTA:\n";
411 os << StringPrintf("%d (0x%08x)\n\n",
412 oat_header.GetImagePatchDelta(),
413 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700414
Brian Carlstrom28db0122012-10-18 16:20:41 -0700415 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
416 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
417
418 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800419 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700420
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700421 // Print the key-value store.
422 {
423 os << "KEY VALUE STORE:\n";
424 size_t index = 0;
425 const char* key;
426 const char* value;
427 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
428 os << key << " = " << value << "\n";
429 index++;
430 }
431 os << "\n";
432 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700433
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800434 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700435 os << "BEGIN:\n";
436 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700437
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700438 os << "END:\n";
439 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
440 }
441
442 os << "SIZE:\n";
443 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700444
445 os << std::flush;
446
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800447 // If set, adjust relative address to be searched
448 if (options_.addr2instr_ != 0) {
449 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
450 os << "SEARCH ADDRESS (executable offset + input):\n";
451 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
452 }
453
David Brazdilc03d7b62016-03-02 12:18:03 +0000454 if (!options_.dump_header_only_) {
455 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
456 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
457 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800458
David Brazdilc03d7b62016-03-02 12:18:03 +0000459 // If file export selected skip file analysis
460 if (options_.export_dex_location_) {
461 if (!ExportDexFile(os, *oat_dex_file)) {
462 success = false;
463 }
464 } else {
465 if (!DumpOatDexFile(os, *oat_dex_file)) {
466 success = false;
467 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800468 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700469 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700470 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000471
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700472 os << std::flush;
473 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700474 }
475
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800476 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700477 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
478 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800479 return 0; // Address not in oat file
480 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800481 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
482 reinterpret_cast<uintptr_t>(oat_file_.Begin());
483 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800484 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800485 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800486 return end_offset - begin_offset;
487 }
488
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800489 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700490 return oat_file_.GetOatHeader().GetInstructionSet();
491 }
492
Mathieu Chartier90443472015-07-16 20:32:27 -0700493 const void* GetQuickOatCode(ArtMethod* m) SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800494 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
495 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700496 CHECK(oat_dex_file != nullptr);
497 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700498 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
499 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700500 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
501 << "': " << error_msg;
502 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800503 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700504 const DexFile::ClassDef* class_def =
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800505 dex_file->FindClassDef(descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700506 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700507 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100508 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800509 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100510 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800511 }
512 }
513 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700514 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800515 }
516
Brian Carlstromaded5f72011-10-07 17:15:04 -0700517 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800518 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800519 // We don't know the length of the code for each method, but we need to know where to stop
520 // when disassembling. What we do know is that a region of code will be followed by some other
521 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
522 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800523 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
524 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700525 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700526 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700527 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
528 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700529 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
530 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800531 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800532 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800533 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800534 for (size_t class_def_index = 0;
535 class_def_index < dex_file->NumClassDefs();
536 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800537 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100538 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700539 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700540 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800541 ClassDataItemIterator it(*dex_file, class_data);
542 SkipAllFields(it);
543 uint32_t class_method_index = 0;
544 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100545 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800546 it.Next();
547 }
548 while (it.HasNextVirtualMethod()) {
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 }
553 }
554 }
555
556 // If the last thing in the file is code for a method, there won't be an offset for the "next"
557 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
558 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800559 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800560 }
561
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700562 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
563 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
564 }
565
Elliott Hughese3c845c2012-02-28 17:23:01 -0800566 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800567 uint32_t code_offset = oat_method.GetCodeOffset();
568 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
569 code_offset &= ~0x1;
570 }
571 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800572 offsets_.insert(oat_method.GetMappingTableOffset());
573 offsets_.insert(oat_method.GetVmapTableOffset());
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800574 offsets_.insert(oat_method.GetGcMapOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800575 }
576
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700577 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
578 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800579 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700580 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800581 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800582 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700583
584 // Create the verifier early.
585
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700586 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700587 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
588 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700589 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700590 os << std::flush;
591 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700592 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100593
594 VariableIndentationOutputStream vios(&os);
595 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800596 for (size_t class_def_index = 0;
597 class_def_index < dex_file->NumClassDefs();
598 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700599 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
600 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800601
602 // TODO: Support regex
603 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
604 continue;
605 }
606
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700607 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100608 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700609 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
610 class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100611 << " (" << oat_class.GetStatus() << ")"
612 << " (" << oat_class.GetType() << ")\n";
613 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800614 if (options_.list_classes_) continue;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700615 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700616 success = false;
617 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800618 if (stop_analysis) {
619 os << std::flush;
620 return success;
621 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700622 }
623
624 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700625 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700626 }
627
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800628 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
629 std::string error_msg;
630 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
631
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700632 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800633 if (dex_file == nullptr) {
634 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
635 return false;
636 }
637 size_t fsize = oat_dex_file.FileSize();
638
639 // Some quick checks just in case
640 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
641 os << "Invalid dex file\n";
642 return false;
643 }
644
645 // Verify output directory exists
646 if (!OS::DirectoryExists(options_.export_dex_location_)) {
647 // TODO: Extend OS::DirectoryExists if symlink support is required
648 os << options_.export_dex_location_ << " output directory not found or symlink\n";
649 return false;
650 }
651
652 // Beautify path names
653 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
654 return false;
655 }
656
657 std::string dex_orig_name;
658 size_t dex_orig_pos = dex_file_location.rfind('/');
659 if (dex_orig_pos == std::string::npos)
660 dex_orig_name = dex_file_location;
661 else
662 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
663
664 // A more elegant approach to efficiently name user installed apps is welcome
665 if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
666 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
667 size_t apk_orig_pos = dex_file_location.rfind('/');
668 if (apk_orig_pos != std::string::npos) {
669 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
670 }
671 }
672
673 std::string out_dex_path(options_.export_dex_location_);
674 if (out_dex_path.back() != '/') {
675 out_dex_path.append("/");
676 }
677 out_dex_path.append(dex_orig_name);
678 out_dex_path.append("_export.dex");
679 if (out_dex_path.length() > PATH_MAX) {
680 return false;
681 }
682
683 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
684 if (file.get() == nullptr) {
685 os << "Failed to open output dex file " << out_dex_path;
686 return false;
687 }
688
689 if (!file->WriteFully(dex_file->Begin(), fsize)) {
690 os << "Failed to write dex file";
691 file->Erase();
692 return false;
693 }
694
695 if (file->FlushCloseOrErase() != 0) {
696 os << "Flush and close failed";
697 return false;
698 }
699
700 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
701 os << std::flush;
702
703 return true;
704 }
705
Elliott Hughese3c845c2012-02-28 17:23:01 -0800706 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700707 while (it.HasNextStaticField()) {
708 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700709 }
Ian Rogers0571d352011-11-03 19:51:38 -0700710 while (it.HasNextInstanceField()) {
711 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700712 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800713 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700714
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100715 bool DumpOatClass(VariableIndentationOutputStream* vios,
716 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800717 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700718 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800719 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -0700720 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700721 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100722 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700723 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800724 }
725 ClassDataItemIterator it(dex_file, class_data);
726 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700727 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700728 while (it.HasNextDirectMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100729 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700730 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800731 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700732 success = false;
733 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800734 if (addr_found) {
735 *stop_analysis = true;
736 return success;
737 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700738 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700739 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700740 }
Ian Rogers0571d352011-11-03 19:51:38 -0700741 while (it.HasNextVirtualMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100742 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700743 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800744 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700745 success = false;
746 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800747 if (addr_found) {
748 *stop_analysis = true;
749 return success;
750 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700751 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700752 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700753 }
Ian Rogers0571d352011-11-03 19:51:38 -0700754 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100755 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700756 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700757 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800758
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700759 static constexpr uint32_t kPrologueBytes = 16;
760
761 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
762 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
763
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100764 bool DumpOatMethod(VariableIndentationOutputStream* vios,
765 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700766 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700767 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800768 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800769 uint32_t method_access_flags, bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700770 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800771
772 // TODO: Support regex
773 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
774 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000775 return success;
776 }
777
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800778 std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100779 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
780 class_method_index, pretty_method.c_str(),
781 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800782 if (options_.list_methods_) return success;
783
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800784 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
785 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
786 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
787 uint32_t code_offset = oat_method.GetCodeOffset();
788 uint32_t code_size = oat_method.GetQuickCodeSize();
789 if (resolved_addr2instr_ != 0) {
790 if (resolved_addr2instr_ > code_offset + code_size) {
791 return success;
792 } else {
793 *addr_found = true; // stop analyzing file at next iteration
794 }
795 }
796
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100797 // Everything below is indented at least once.
798 ScopedIndentation indent1(vios);
799
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800800 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100801 vios->Stream() << "DEX CODE:\n";
802 ScopedIndentation indent2(vios);
803 DumpDexCode(vios->Stream(), dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700804 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700805
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700806 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700807 std::unique_ptr<verifier::MethodVerifier> verifier;
808 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700809 // We need to have the handle scope stay live until after the verifier since the verifier has
810 // a handle to the dex cache from hs.
811 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100812 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
813 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700814 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100815 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700816 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -0700817 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800818 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100819 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800820 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100821 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100822 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100823 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700824 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100825 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700826 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
827 oat_method_offsets_offset, oat_file_.Size());
828 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100829 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700830 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800831 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700832
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100833 ScopedIndentation indent2(vios);
834 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700835 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
836 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100837 vios->Stream() << StringPrintf("WARNING: "
838 "code offset 0x%08x is past end of file 0x%08zx.\n",
839 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700840 success = false;
841 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100842 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700843
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100844 vios->Stream() << "gc_map: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800845 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100846 vios->Stream() << StringPrintf("%p ", oat_method.GetGcMap());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700847 }
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800848 uint32_t gc_map_offset = oat_method.GetGcMapOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100849 vios->Stream() << StringPrintf("(offset=0x%08x)\n", gc_map_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700850 if (gc_map_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100851 vios->Stream() << StringPrintf("WARNING: "
852 "gc map table offset 0x%08x is past end of file 0x%08zx.\n",
853 gc_map_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700854 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800855 } else if (options_.dump_raw_gc_map_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100856 ScopedIndentation indent3(vios);
857 DumpGcMap(vios->Stream(), oat_method, code_item);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800858 }
859 }
860 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100861 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700862 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
863 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700864
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800865 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100866 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700867 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100868 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700869 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100870 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700871 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
872 method_header_offset, oat_file_.Size());
873 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100874 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700875 return false;
876 }
877
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100878 ScopedIndentation indent2(vios);
879 vios->Stream() << "mapping_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800880 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100881 vios->Stream() << StringPrintf("%p ", oat_method.GetMappingTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700882 }
883 uint32_t mapping_table_offset = oat_method.GetMappingTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100884 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700885 if (mapping_table_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100886 vios->Stream() << StringPrintf("WARNING: "
887 "mapping table offset 0x%08x is past end of file 0x%08zx. "
888 "mapping table offset was loaded from offset 0x%08x.\n",
889 mapping_table_offset, oat_file_.Size(),
890 oat_method.GetMappingTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700891 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800892 } else if (options_.dump_raw_mapping_table_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100893 ScopedIndentation indent3(vios);
894 DumpMappingTable(vios, oat_method);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700895 }
896
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100897 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800898 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100899 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700900 }
901 uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100902 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700903 if (vmap_table_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100904 vios->Stream() << StringPrintf("WARNING: "
905 "vmap table offset 0x%08x is past end of file 0x%08zx. "
906 "vmap table offset was loaded from offset 0x%08x.\n",
907 vmap_table_offset, oat_file_.Size(),
908 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700909 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800910 } else if (options_.dump_vmap_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100911 DumpVmapData(vios, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700912 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800913 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700914 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100915 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700916
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100917 ScopedIndentation indent2(vios);
918 vios->Stream()
919 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
920 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
921 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
922 vios->Stream() << "\n";
923 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
924 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
925 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700926 }
927 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100928 // Based on spill masks from QuickMethodFrameInfo so placed
929 // after it is dumped, but useful for understanding quick
930 // code, so dumped here.
931 ScopedIndentation indent2(vios);
932 DumpVregLocations(vios->Stream(), oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700933 }
934 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100935 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700936 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
937 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100938 ScopedIndentation indent2(vios);
939 vios->Stream() << StringPrintf("WARNING: "
940 "code size offset 0x%08x is past end of file 0x%08zx.",
941 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700942 success = false;
943 } else {
944 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700945 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
946 uint64_t aligned_code_end = aligned_code_begin + code_size;
947
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800948 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100949 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700950 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100951 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
952 code_offset,
953 code_size_offset,
954 code_size,
955 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700956
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100957 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700958 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100959 vios->Stream() << StringPrintf("WARNING: "
960 "start of code at 0x%08x is past end of file 0x%08zx.",
961 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700962 success = false;
963 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100964 vios->Stream() << StringPrintf(
965 "WARNING: "
966 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
967 "code size is 0x%08x loaded from offset 0x%08x.\n",
968 aligned_code_end, oat_file_.Size(),
969 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700970 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800971 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700972 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100973 DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700974 }
975 }
976 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100977 vios->Stream() << StringPrintf(
978 "WARNING: "
979 "code size %d is bigger than max expected threshold of %d. "
980 "code size is 0x%08x loaded from offset 0x%08x.\n",
981 code_size, kMaxCodeSize,
982 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700983 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800984 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700985 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100986 DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700987 }
988 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800989 } else if (options_.disassemble_code_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100990 DumpCode(vios, verifier.get(), oat_method, code_item, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700991 }
992 }
993 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100994 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700995 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700996 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800997
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800998 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
999 if (spill_mask == 0) {
1000 return;
1001 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001002 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001003 for (size_t i = 0; i < 32; i++) {
1004 if ((spill_mask & (1 << i)) != 0) {
1005 if (is_float) {
1006 os << "fr" << i;
1007 } else {
1008 os << "r" << i;
1009 }
1010 spill_mask ^= 1 << i; // clear bit
1011 if (spill_mask != 0) {
1012 os << ", ";
1013 } else {
1014 break;
1015 }
1016 }
1017 }
1018 os << ")";
1019 }
1020
Roland Levillain442b46a2015-02-18 16:54:21 +00001021 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001022 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001023 const OatFile::OatMethod& oat_method,
1024 const DexFile::CodeItem* code_item) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001025 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1026 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001027 const void* raw_code_info = oat_method.GetVmapTable();
1028 if (raw_code_info != nullptr) {
1029 CodeInfo code_info(raw_code_info);
1030 DCHECK(code_item != nullptr);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001031 ScopedIndentation indent1(vios);
1032 DumpCodeInfo(vios, code_info, oat_method, *code_item);
Roland Levillain442b46a2015-02-18 16:54:21 +00001033 }
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001034 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
1035 // We don't encode the size in the table, so just emit that we have quickened
1036 // information.
1037 ScopedIndentation indent(vios);
1038 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001039 } else {
1040 // Otherwise, display the vmap table.
1041 const uint8_t* raw_table = oat_method.GetVmapTable();
1042 if (raw_table != nullptr) {
1043 VmapTable vmap_table(raw_table);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001044 DumpVmapTable(vios->Stream(), oat_method, vmap_table);
Roland Levillain442b46a2015-02-18 16:54:21 +00001045 }
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001046 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001047 }
1048
1049 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001050 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001051 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001052 const OatFile::OatMethod& oat_method,
Roland Levillain442b46a2015-02-18 16:54:21 +00001053 const DexFile::CodeItem& code_item) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001054 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001055 oat_method.GetCodeOffset(),
1056 code_item.registers_size_,
1057 options_.dump_code_info_stack_maps_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001058 }
1059
Roland Levillain442b46a2015-02-18 16:54:21 +00001060 // Display a vmap table.
1061 void DumpVmapTable(std::ostream& os,
1062 const OatFile::OatMethod& oat_method,
1063 const VmapTable& vmap_table) {
1064 bool first = true;
1065 bool processing_fp = false;
1066 uint32_t spill_mask = oat_method.GetCoreSpillMask();
1067 for (size_t i = 0; i < vmap_table.Size(); i++) {
1068 uint16_t dex_reg = vmap_table[i];
1069 uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
1070 processing_fp ? kFloatVReg : kIntVReg);
1071 os << (first ? "v" : ", v") << dex_reg;
1072 if (!processing_fp) {
1073 os << "/r" << cpu_reg;
1074 } else {
1075 os << "/fr" << cpu_reg;
1076 }
1077 first = false;
1078 if (!processing_fp && dex_reg == 0xFFFF) {
1079 processing_fp = true;
1080 spill_mask = oat_method.GetFpSpillMask();
1081 }
1082 }
1083 os << "\n";
1084 }
1085
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001086 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1087 const DexFile::CodeItem* code_item) {
1088 if (code_item != nullptr) {
1089 size_t num_locals_ins = code_item->registers_size_;
1090 size_t num_ins = code_item->ins_size_;
1091 size_t num_locals = num_locals_ins - num_ins;
1092 size_t num_outs = code_item->outs_size_;
1093
1094 os << "vr_stack_locations:";
1095 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1096 // For readability, delimit the different kinds of VRs.
1097 if (reg == num_locals_ins) {
1098 os << "\n\tmethod*:";
1099 } else if (reg == num_locals && num_ins > 0) {
1100 os << "\n\tins:";
1101 } else if (reg == 0 && num_locals > 0) {
1102 os << "\n\tlocals:";
1103 }
1104
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001105 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1106 code_item,
1107 oat_method.GetCoreSpillMask(),
1108 oat_method.GetFpSpillMask(),
1109 oat_method.GetFrameSizeInBytes(),
1110 reg,
1111 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001112 os << " v" << reg << "[sp + #" << offset << "]";
1113 }
1114
1115 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1116 if (out_reg == 0) {
1117 os << "\n\touts:";
1118 }
1119
1120 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1121 os << " v" << out_reg << "[sp + #" << offset << "]";
1122 }
1123
1124 os << "\n";
1125 }
1126 }
1127
Ian Rogersb23a7722012-10-09 16:54:26 -07001128 void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001129 const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
Ian Rogers1809a722013-08-09 22:05:32 -07001130 const uint8_t* raw_table = oat_method.GetVmapTable();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001131 if (raw_table != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001132 const VmapTable vmap_table(raw_table);
1133 uint32_t vmap_offset;
Ian Rogers1809a722013-08-09 22:05:32 -07001134 if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001135 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
1136 uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
1137 : oat_method.GetCoreSpillMask();
1138 os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
Ian Rogersb23a7722012-10-09 16:54:26 -07001139 } else {
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001140 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1141 code_item,
1142 oat_method.GetCoreSpillMask(),
1143 oat_method.GetFpSpillMask(),
1144 oat_method.GetFrameSizeInBytes(),
1145 reg,
1146 GetInstructionSet());
Ian Rogersb23a7722012-10-09 16:54:26 -07001147 os << "[sp + #" << offset << "]";
1148 }
1149 }
1150 }
1151
Ian Rogersef7d42f2014-01-06 12:55:46 -08001152 void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
1153 const DexFile::CodeItem* code_item,
1154 size_t num_regs, const uint8_t* reg_bitmap) {
1155 bool first = true;
1156 for (size_t reg = 0; reg < num_regs; reg++) {
1157 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1158 if (first) {
1159 os << " v" << reg << " (";
1160 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1161 os << ")";
1162 first = false;
1163 } else {
1164 os << ", v" << reg << " (";
1165 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1166 os << ")";
1167 }
1168 }
1169 }
1170 if (first) {
1171 os << "No registers in GC map\n";
1172 } else {
1173 os << "\n";
1174 }
1175 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001176 void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
1177 const DexFile::CodeItem* code_item) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001178 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Ian Rogersef7d42f2014-01-06 12:55:46 -08001179 if (gc_map_raw == nullptr) {
1180 return; // No GC map.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001181 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001182 const void* quick_code = oat_method.GetQuickCode();
Elliott Hughes956af0f2014-12-11 14:34:28 -08001183 NativePcOffsetToReferenceMap map(gc_map_raw);
1184 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1185 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
1186 map.GetNativePcOffset(entry);
1187 os << StringPrintf("%p", native_pc);
1188 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001189 }
1190 }
1191
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001192 void DumpMappingTable(VariableIndentationOutputStream* vios,
1193 const OatFile::OatMethod& oat_method) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001194 const void* quick_code = oat_method.GetQuickCode();
1195 if (quick_code == nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -08001196 return;
1197 }
Ian Rogers1809a722013-08-09 22:05:32 -07001198 MappingTable table(oat_method.GetMappingTable());
1199 if (table.TotalSize() != 0) {
Ian Rogers1809a722013-08-09 22:05:32 -07001200 if (table.PcToDexSize() != 0) {
1201 typedef MappingTable::PcToDexIterator It;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001202 vios->Stream() << "suspend point mappings {\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001203 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001204 ScopedIndentation indent1(vios);
1205 vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001206 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001207 vios->Stream() << "}\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001208 }
1209 if (table.DexToPcSize() != 0) {
1210 typedef MappingTable::DexToPcIterator It;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001211 vios->Stream() << "catch entry mappings {\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001212 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001213 ScopedIndentation indent1(vios);
1214 vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001215 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001216 vios->Stream() << "}\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001217 }
1218 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001219 }
1220
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001221 uint32_t DumpInformationAtOffset(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001222 const OatFile::OatMethod& oat_method,
1223 const DexFile::CodeItem* code_item,
1224 size_t offset,
1225 bool suspend_point_mapping) {
1226 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1227 if (suspend_point_mapping) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001228 ScopedIndentation indent1(vios);
1229 DumpDexRegisterMapAtOffset(vios, oat_method, code_item, offset);
Roland Levillainf2650d12015-05-28 14:53:28 +01001230 }
1231 // The return value is not used in the case of a method compiled
1232 // with the optimizing compiler.
1233 return DexFile::kDexNoIndex;
1234 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001235 return DumpMappingAtOffset(vios->Stream(), oat_method, offset, suspend_point_mapping);
Roland Levillainf2650d12015-05-28 14:53:28 +01001236 }
1237 }
1238
Ian Rogers1809a722013-08-09 22:05:32 -07001239 uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1240 size_t offset, bool suspend_point_mapping) {
1241 MappingTable table(oat_method.GetMappingTable());
1242 if (suspend_point_mapping && table.PcToDexSize() > 0) {
1243 typedef MappingTable::PcToDexIterator It;
1244 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1245 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001246 os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001247 return cur.DexPc();
1248 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001249 }
Ian Rogers1809a722013-08-09 22:05:32 -07001250 } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
1251 typedef MappingTable::DexToPcIterator It;
1252 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1253 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001254 os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001255 return cur.DexPc();
Ian Rogersb23a7722012-10-09 16:54:26 -07001256 }
1257 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001258 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001259 return DexFile::kDexNoIndex;
Ian Rogersb23a7722012-10-09 16:54:26 -07001260 }
1261
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001262 void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1263 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001264 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001265 if (gc_map_raw != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001266 NativePcOffsetToReferenceMap map(gc_map_raw);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001267 if (map.HasEntry(native_pc_offset)) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001268 size_t num_regs = map.RegWidth() * 8;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001269 const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
Ian Rogersb23a7722012-10-09 16:54:26 -07001270 bool first = true;
1271 for (size_t reg = 0; reg < num_regs; reg++) {
1272 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1273 if (first) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001274 os << "GC map objects: v" << reg << " (";
1275 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001276 os << ")";
1277 first = false;
1278 } else {
1279 os << ", v" << reg << " (";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001280 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001281 os << ")";
1282 }
1283 }
1284 }
1285 if (!first) {
1286 os << "\n";
1287 }
1288 }
1289 }
1290 }
1291
Mathieu Chartier590fee92013-09-13 13:46:47 -07001292 void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
1293 const OatFile::OatMethod& oat_method,
1294 const DexFile::CodeItem* code_item, uint32_t dex_pc) {
1295 DCHECK(verifier != nullptr);
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001296 std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001297 bool first = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001298 for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
1299 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
1300 if (kind != kUndefined) {
1301 if (first) {
1302 os << "VRegs: v";
1303 first = false;
1304 } else {
1305 os << ", v";
1306 }
1307 os << reg << " (";
1308 switch (kind) {
1309 case kImpreciseConstant:
1310 os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
1311 DescribeVReg(os, oat_method, code_item, reg, kind);
1312 break;
1313 case kConstant:
1314 os << "Constant: " << kinds.at((reg * 2) + 1);
1315 break;
1316 default:
1317 DescribeVReg(os, oat_method, code_item, reg, kind);
1318 break;
1319 }
1320 os << ")";
1321 }
1322 }
1323 if (!first) {
1324 os << "\n";
1325 }
1326 }
1327
1328
Ian Rogersb23a7722012-10-09 16:54:26 -07001329 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001330 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001331 size_t i = 0;
1332 while (i < code_item->insns_size_in_code_units_) {
1333 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001334 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1335 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001336 i += instruction->SizeInCodeUnits();
1337 }
1338 }
1339 }
1340
Roland Levillainf2650d12015-05-28 14:53:28 +01001341 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1342 // the optimizing compiler?
1343 static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1344 const DexFile::CodeItem* code_item) {
1345 // If the native GC map is null and the Dex `code_item` is not
1346 // null, then this method has been compiled with the optimizing
1347 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001348 return oat_method.GetQuickCode() != nullptr &&
1349 oat_method.GetGcMap() == nullptr &&
1350 code_item != nullptr;
1351 }
1352
1353 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1354 // the dextodex compiler?
1355 static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
1356 const DexFile::CodeItem* code_item) {
1357 // If the quick code is null, the Dex `code_item` is not
1358 // null, and the vmap table is not null, then this method has been compiled
1359 // with the dextodex compiler.
1360 return oat_method.GetQuickCode() == nullptr &&
1361 oat_method.GetVmapTable() != nullptr &&
1362 code_item != nullptr;
Roland Levillainf2650d12015-05-28 14:53:28 +01001363 }
1364
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001365 void DumpDexRegisterMapAtOffset(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001366 const OatFile::OatMethod& oat_method,
1367 const DexFile::CodeItem* code_item,
1368 size_t offset) {
1369 // This method is only relevant for oat methods compiled with the
1370 // optimizing compiler.
1371 DCHECK(IsMethodGeneratedByOptimizingCompiler(oat_method, code_item));
1372
1373 // The optimizing compiler outputs its CodeInfo data in the vmap table.
1374 const void* raw_code_info = oat_method.GetVmapTable();
1375 if (raw_code_info != nullptr) {
1376 CodeInfo code_info(raw_code_info);
David Brazdilf677ebf2015-05-29 16:29:43 +01001377 StackMapEncoding encoding = code_info.ExtractEncoding();
1378 StackMap stack_map = code_info.GetStackMapForNativePcOffset(offset, encoding);
Roland Levillainf2650d12015-05-28 14:53:28 +01001379 if (stack_map.IsValid()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001380 stack_map.Dump(vios, code_info, encoding, oat_method.GetCodeOffset(),
1381 code_item->registers_size_);
Roland Levillainf2650d12015-05-28 14:53:28 +01001382 }
1383 }
1384 }
1385
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001386 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001387 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001388 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001389 const DexFile* dex_file,
1390 const DexFile::ClassDef& class_def,
1391 const DexFile::CodeItem* code_item,
1392 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001393 if ((method_access_flags & kAccNative) == 0) {
1394 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001395 Runtime* const runtime = Runtime::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001396 Handle<mirror::DexCache> dex_cache(
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001397 hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file,
1398 runtime->GetLinearAlloc())));
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001399 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001400 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001401 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
1402 &class_def, code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001403 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001404
1405 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001406 }
1407
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001408 void DumpCode(VariableIndentationOutputStream* vios,
1409 verifier::MethodVerifier* verifier,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001410 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1411 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001412 const void* quick_code = oat_method.GetQuickCode();
1413
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001414 if (code_size == 0) {
1415 code_size = oat_method.GetQuickCodeSize();
1416 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001417 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001418 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001419 return;
Elliott Hughes956af0f2014-12-11 14:34:28 -08001420 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001421 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1422 size_t offset = 0;
1423 while (offset < code_size) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001424 if (!bad_input) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001425 DumpInformationAtOffset(vios, oat_method, code_item, offset, false);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001426 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001427 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001428 if (!bad_input) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001429 uint32_t dex_pc =
1430 DumpInformationAtOffset(vios, oat_method, code_item, offset, true);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001431 if (dex_pc != DexFile::kDexNoIndex) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001432 DumpGcMapAtNativePcOffset(vios->Stream(), oat_method, code_item, offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001433 if (verifier != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001434 DumpVRegsAtDexPc(vios->Stream(), verifier, oat_method, code_item, dex_pc);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001435 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001436 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001437 }
1438 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001439 }
1440 }
1441
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001442 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001443 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001444 const OatDumperOptions& options_;
1445 uint32_t resolved_addr2instr_;
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001446 InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001447 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001448 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001449};
1450
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001451class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001452 public:
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001453 ImageDumper(std::ostream* os,
1454 gc::space::ImageSpace& image_space,
1455 const ImageHeader& image_header,
1456 OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001457 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001458 vios_(os),
1459 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001460 image_space_(image_space),
1461 image_header_(image_header),
1462 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001463
Mathieu Chartier90443472015-07-16 20:32:27 -07001464 bool Dump() SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001465 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001466 std::ostream& indent_os = vios_.Stream();
1467
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001468 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001469
Jeff Haodcdc85b2015-12-04 14:06:18 -08001470 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1471
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001472 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001473
Mathieu Chartiere401d142015-04-22 13:56:20 -07001474 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1475
1476 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1477 auto section = static_cast<ImageHeader::ImageSections>(i);
1478 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1479 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001480
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001481 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001482
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001483 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001484
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001485 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1486
1487 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1488
1489 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001490
Alex Lighta59dd802014-07-02 16:28:08 -07001491 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1492
Igor Murashkin46774762014-10-22 11:37:02 -07001493 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1494
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001495 {
1496 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001497 static_assert(arraysize(image_roots_descriptions_) ==
1498 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001499 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1500 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1501 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001502 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001503 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001504 if (image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001505 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001506 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001507 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001508 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1509 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001510 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001511 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1512 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001513 run++;
1514 } else {
1515 break;
1516 }
1517 }
1518 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001519 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001520 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001521 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001522 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001523 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001524 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001525 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001526 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001527 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001528 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001529 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001530 }
1531 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001532 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001533
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001534 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001535 os << "METHOD ROOTS\n";
1536 static_assert(arraysize(image_methods_descriptions_) ==
1537 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1538 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1539 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1540 const char* description = image_methods_descriptions_[i];
1541 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001542 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001543 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001544 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001545 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001546
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001547 Runtime* const runtime = Runtime::Current();
1548 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001549 std::string image_filename = image_space_.GetImageFilename();
1550 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001551 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001552 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001553 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001554 const OatFile* oat_file = image_space_.GetOatFile();
Alex Lighta59dd802014-07-02 16:28:08 -07001555 if (oat_file == nullptr) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001556 oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location);
1557 }
1558 if (oat_file == nullptr) {
1559 oat_file = OatFile::Open(oat_location,
1560 oat_location,
1561 nullptr,
1562 nullptr,
1563 false,
1564 /*low_4gb*/false,
1565 nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001566 &error_msg);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001567 }
1568 if (oat_file == nullptr) {
1569 os << "OAT FILE NOT FOUND: " << error_msg << "\n";
1570 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001571 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001572 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001573
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001574 stats_.oat_file_bytes = oat_file->Size();
1575
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001576 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001577
Mathieu Chartier02e25112013-08-14 16:14:24 -07001578 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001579 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001580 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1581 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001582 }
1583
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001584 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001585
Jeff Haodcdc85b2015-12-04 14:06:18 -08001586 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001587 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001588 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001589 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001590 {
1591 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1592 heap->FlushAllocStack();
1593 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001594 // Since FlushAllocStack() above resets the (active) allocation
1595 // stack. Need to revoke the thread-local allocation stacks that
1596 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001597 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001598 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001599 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001600 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001601 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001602 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001603 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001604 {
1605 ReaderMutexLock mu(self, *class_linker->DexLock());
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001606 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001607 mirror::DexCache* dex_cache =
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001608 down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001609 if (dex_cache != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01001610 dex_caches_.insert(dex_cache);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001611 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001612 }
1613 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001614 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001615 // Dump the normal objects before ArtMethods.
1616 image_space_.GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1617 indent_os << "\n";
1618 // TODO: Dump fields.
1619 // Dump methods after.
1620 const auto& methods_section = image_header_.GetMethodsSection();
Jeff Haodcdc85b2015-12-04 14:06:18 -08001621 DumpArtMethodVisitor visitor(this);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001622 methods_section.VisitPackedArtMethods(&visitor,
1623 image_space_.Begin(),
1624 image_header_.GetPointerSize());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001625 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001626 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001627 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001628 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001629 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001630 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001631 size_t data_size = image_header_.GetDataSize(); // stored size in file.
1632 if (file == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001633 LOG(WARNING) << "Failed to find image in " << image_filename;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001634 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001635 stats_.file_bytes = file->GetLength();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001636 // If the image is compressed, adjust to decompressed size.
1637 size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader);
1638 if (image_header_.GetStorageMode() == ImageHeader::kStorageModeUncompressed) {
1639 DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image";
1640 }
1641 stats_.file_bytes += uncompressed_size - data_size;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001642 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001643 size_t header_bytes = sizeof(ImageHeader);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001644 const auto& object_section = image_header_.GetImageSection(ImageHeader::kSectionObjects);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001645 const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1646 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001647 const auto& dex_cache_arrays_section = image_header_.GetImageSection(
1648 ImageHeader::kSectionDexCacheArrays);
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001649 const auto& intern_section = image_header_.GetImageSection(
1650 ImageHeader::kSectionInternedStrings);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001651 const auto& class_table_section = image_header_.GetImageSection(
1652 ImageHeader::kSectionClassTable);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001653 const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1654
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001655 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001656
1657 // Objects are kObjectAlignment-aligned.
1658 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1659 if (object_section.Offset() > header_bytes) {
1660 stats_.alignment_bytes += object_section.Offset() - header_bytes;
1661 }
1662
1663 // Field section is 4-byte aligned.
1664 constexpr size_t kFieldSectionAlignment = 4U;
1665 uint32_t end_objects = object_section.Offset() + object_section.Size();
1666 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
1667 stats_.alignment_bytes += field_section.Offset() - end_objects;
1668
1669 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
1670 uint32_t end_fields = field_section.Offset() + field_section.Size();
1671 CHECK_ALIGNED(method_section.Offset(), 4);
1672 stats_.alignment_bytes += method_section.Offset() - end_fields;
1673
1674 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
1675 uint32_t end_methods = method_section.Offset() + method_section.Size();
1676 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
1677 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
1678
1679 // Intern table is 8-byte aligned.
1680 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
1681 CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
1682 stats_.alignment_bytes += intern_section.Offset() - end_caches;
1683
1684 // Add space between intern table and class table.
1685 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
1686 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
1687
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001688 // Add space between end of image data and bitmap. Expect the bitmap to be page-aligned.
1689 const size_t bitmap_offset = sizeof(ImageHeader) + data_size;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001690 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001691 stats_.alignment_bytes += RoundUp(bitmap_offset, kPageSize) - bitmap_offset;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001692
Mathieu Chartiere401d142015-04-22 13:56:20 -07001693 stats_.bitmap_bytes += bitmap_section.Size();
1694 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01001695 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01001696 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001697 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001698 stats_.class_table_bytes += class_table_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001699 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001700 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001701
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001702 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001703
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001704 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001705 }
1706
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001707 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001708 class DumpArtMethodVisitor : public ArtMethodVisitor {
1709 public:
1710 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
1711
1712 virtual void Visit(ArtMethod* method) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
1713 std::ostream& indent_os = image_dumper_->vios_.Stream();
1714 indent_os << method << " " << " ArtMethod: " << PrettyMethod(method) << "\n";
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001715 image_dumper_->DumpMethod(method, indent_os);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001716 indent_os << "\n";
1717 }
1718
1719 private:
1720 ImageDumper* const image_dumper_;
1721 };
1722
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001723 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Mathieu Chartier90443472015-07-16 20:32:27 -07001724 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001725 CHECK(type != nullptr);
1726 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001727 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001728 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001729 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001730 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001731 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001732 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001733 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001734 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001735 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001736 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001737 }
1738 }
1739
Mathieu Chartierc7853442015-03-27 14:35:38 -07001740 static void PrintField(std::ostream& os, ArtField* field, mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -07001741 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001742 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08001743 switch (field->GetTypeAsPrimitiveType()) {
1744 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08001745 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001746 break;
1747 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001748 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001749 break;
1750 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001751 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001752 break;
1753 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001754 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001755 break;
1756 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07001757 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001758 break;
1759 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07001760 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001761 break;
1762 case Primitive::kPrimBoolean:
Fred Shih37f05ef2014-07-16 18:38:08 -07001763 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1764 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001765 break;
1766 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07001767 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001768 break;
1769 case Primitive::kPrimNot: {
1770 // Get the value, don't compute the type unless it is non-null as we don't want
1771 // to cause class loading.
1772 mirror::Object* value = field->GetObj(obj);
1773 if (value == nullptr) {
1774 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07001775 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08001776 // Grab the field type without causing resolution.
Mathieu Chartierdaaf3262015-03-24 13:30:28 -07001777 mirror::Class* field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08001778 if (field_type != nullptr) {
1779 PrettyObjectValue(os, field_type, value);
1780 } else {
1781 os << StringPrintf("%p %s\n", value,
1782 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
1783 }
Ian Rogers50239c72013-06-17 14:53:22 -07001784 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001785 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07001786 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001787 default:
1788 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
1789 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08001790 }
1791 }
1792
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001793 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Mathieu Chartier90443472015-07-16 20:32:27 -07001794 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001795 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001796 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001797 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001798 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001799 for (ArtField& field : klass->GetIFields()) {
1800 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001801 }
1802 }
1803
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001804 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001805 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001806 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001807
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001808 const void* GetQuickOatCodeBegin(ArtMethod* m) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001809 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001810 image_header_.GetPointerSize());
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001811 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001812 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001813 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001814 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001815 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001816 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001817 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001818 }
1819
Mathieu Chartiere401d142015-04-22 13:56:20 -07001820 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001821 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001822 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1823 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001824 return 0;
1825 }
1826 return oat_code_begin[-1];
1827 }
1828
Mathieu Chartiere401d142015-04-22 13:56:20 -07001829 const void* GetQuickOatCodeEnd(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001830 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001831 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001832 if (oat_code_begin == nullptr) {
1833 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001834 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001835 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001836 }
1837
Mathieu Chartier90443472015-07-16 20:32:27 -07001838 static void Callback(mirror::Object* obj, void* arg) SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001839 DCHECK(obj != nullptr);
1840 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001841 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001842 if (!state->InDumpSpace(obj)) {
1843 return;
1844 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001845
1846 size_t object_bytes = obj->SizeOf();
1847 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1848 state->stats_.object_bytes += object_bytes;
1849 state->stats_.alignment_bytes += alignment_bytes;
1850
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001851 std::ostream& os = state->vios_.Stream();
1852
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001853 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001854 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001855 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
1856 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001857 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001858 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001859 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
1860 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08001861 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001862 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001863 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001864 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001865 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001866 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001867 ScopedIndentation indent1(&state->vios_);
1868 DumpFields(os, obj, obj_class);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001869 const size_t image_pointer_size = state->image_header_.GetPointerSize();
Ian Rogersd5b32602012-02-26 16:40:04 -08001870 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001871 auto* obj_array = obj->AsObjectArray<mirror::Object>();
1872 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001873 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001874 size_t run = 0;
1875 for (int32_t j = i + 1; j < length; j++) {
1876 if (value == obj_array->Get(j)) {
1877 run++;
1878 } else {
1879 break;
1880 }
1881 }
1882 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001883 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001884 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001885 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001886 i = i + run;
1887 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001888 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001889 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001890 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001891 }
1892 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001893 mirror::Class* klass = obj->AsClass();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001894 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001895 os << "STATICS:\n";
1896 ScopedIndentation indent2(&state->vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001897 for (ArtField& field : klass->GetSFields()) {
1898 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001899 }
1900 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001901 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01001902 auto it = state->dex_caches_.find(obj);
1903 if (it != state->dex_caches_.end()) {
1904 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001905 const auto& field_section = state->image_header_.GetImageSection(
1906 ImageHeader::kSectionArtFields);
1907 const auto& method_section = state->image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001908 size_t num_methods = dex_cache->NumResolvedMethods();
1909 if (num_methods != 0u) {
1910 os << "Methods (size=" << num_methods << "):";
1911 ScopedIndentation indent2(&state->vios_);
1912 auto* resolved_methods = dex_cache->GetResolvedMethods();
1913 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001914 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_methods,
1915 i,
1916 image_pointer_size);
Vladimir Marko05792b92015-08-03 11:56:49 +01001917 size_t run = 0;
1918 for (size_t j = i + 1;
1919 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_methods,
1920 j,
1921 image_pointer_size);
1922 ++j, ++run) {}
1923 if (run == 0) {
1924 os << StringPrintf("%zd: ", i);
1925 } else {
1926 os << StringPrintf("%zd to %zd: ", i, i + run);
1927 i = i + run;
1928 }
1929 std::string msg;
1930 if (elem == nullptr) {
1931 msg = "null";
1932 } else if (method_section.Contains(
1933 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
1934 msg = PrettyMethod(reinterpret_cast<ArtMethod*>(elem));
1935 } else {
1936 msg = "<not in method section>";
1937 }
1938 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07001939 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001940 }
1941 size_t num_fields = dex_cache->NumResolvedFields();
1942 if (num_fields != 0u) {
1943 os << "Fields (size=" << num_fields << "):";
1944 ScopedIndentation indent2(&state->vios_);
1945 auto* resolved_fields = dex_cache->GetResolvedFields();
1946 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
1947 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_fields, i, image_pointer_size);
1948 size_t run = 0;
1949 for (size_t j = i + 1;
1950 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_fields,
1951 j,
1952 image_pointer_size);
1953 ++j, ++run) {}
1954 if (run == 0) {
1955 os << StringPrintf("%zd: ", i);
1956 } else {
1957 os << StringPrintf("%zd to %zd: ", i, i + run);
1958 i = i + run;
1959 }
1960 std::string msg;
1961 if (elem == nullptr) {
1962 msg = "null";
1963 } else if (field_section.Contains(
1964 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
1965 msg = PrettyField(reinterpret_cast<ArtField*>(elem));
1966 } else {
1967 msg = "<not in field section>";
1968 }
1969 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07001970 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001971 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001972 }
1973 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07001974 std::string temp;
1975 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001976 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001977
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001978 void DumpMethod(ArtMethod* method, std::ostream& indent_os)
Mathieu Chartier90443472015-07-16 20:32:27 -07001979 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001980 DCHECK(method != nullptr);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001981 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
1982 const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01001983 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
1984 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07001985 if (method->IsNative()) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01001986 if (!Runtime::Current()->GetClassLinker()->IsQuickGenericJniStub(quick_oat_code_begin)) {
1987 DCHECK(method_header->GetNativeGcMap() == nullptr) << PrettyMethod(method);
1988 DCHECK(method_header->GetMappingTable() == nullptr) << PrettyMethod(method);
1989 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001990 bool first_occurrence;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001991 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
1992 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001993 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001994 stats_.native_to_managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001995 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001996 if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
1997 image_header_.GetPointerSize())) {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01001998 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001999 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002000 } else if (method->IsAbstract() ||
2001 method->IsCalleeSaveMethod() ||
2002 method->IsResolutionMethod() ||
2003 (method == Runtime::Current()->GetImtConflictMethod()) ||
2004 method->IsImtUnimplementedMethod() ||
2005 method->IsClassInitializer()) {
2006 // Don't print information for these.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002007 } else {
2008 const DexFile::CodeItem* code_item = method->GetCodeItem();
2009 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002010 stats_.dex_instruction_bytes += dex_instruction_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002011
2012 bool first_occurrence;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002013 size_t gc_map_bytes = ComputeOatSize(method_header->GetNativeGcMap(), &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002014 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002015 stats_.gc_map_bytes += gc_map_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002016 }
2017
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002018 size_t pc_mapping_table_bytes = ComputeOatSize(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002019 method_header->GetMappingTable(), &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002020 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002021 stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002022 }
2023
Roland Levillain6d7f1792015-07-02 10:59:15 +01002024 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002025 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01002026 // Method compiled with the optimizing compiler have no vmap table.
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002027 vmap_table_bytes = ComputeOatSize(method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002028 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002029 stats_.vmap_table_bytes += vmap_table_bytes;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002030 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002031 }
2032
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002033 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2034 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002035 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002036 stats_.managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002037 if (method->IsConstructor()) {
2038 if (method->IsStatic()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002039 stats_.class_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002040 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002041 stats_.large_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002042 }
2043 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002044 stats_.large_method_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002045 }
2046 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002047 stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002048
Igor Murashkin7617abd2015-07-10 18:27:47 -07002049 uint32_t method_access_flags = method->GetAccessFlags();
2050
Mathieu Chartiere401d142015-04-22 13:56:20 -07002051 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Igor Murashkin7617abd2015-07-10 18:27:47 -07002052 indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd AccessFlags=0x%x\n",
2053 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes,
2054 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002055
2056 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002057 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002058
2059 double expansion =
2060 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002061 stats_.ComputeOutliers(total_size, expansion, method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002062 }
2063 }
2064
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002065 std::set<const void*> already_seen_;
2066 // Compute the size of the given data within the oat file and whether this is the first time
2067 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002068 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002069 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002070 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002071 already_seen_.insert(oat_data);
2072 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002073 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002074 }
2075 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002076 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002077
2078 public:
2079 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002080 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002081 size_t file_bytes;
2082
2083 size_t header_bytes;
2084 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002085 size_t art_field_bytes;
2086 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01002087 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002088 size_t interned_strings_bytes;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002089 size_t class_table_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07002090 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002091 size_t alignment_bytes;
2092
2093 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002094 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002095 size_t managed_to_native_code_bytes;
2096 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07002097 size_t class_initializer_code_bytes;
2098 size_t large_initializer_code_bytes;
2099 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002100
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002101 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002102 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002103 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002104
2105 size_t dex_instruction_bytes;
2106
Mathieu Chartiere401d142015-04-22 13:56:20 -07002107 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002108 std::vector<size_t> method_outlier_size;
2109 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002110 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002111
Roland Levillain3887c462015-08-12 18:15:42 +01002112 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002113 : oat_file_bytes(0),
2114 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002115 header_bytes(0),
2116 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002117 art_field_bytes(0),
2118 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01002119 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002120 interned_strings_bytes(0),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002121 class_table_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07002122 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002123 alignment_bytes(0),
2124 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002125 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002126 managed_to_native_code_bytes(0),
2127 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07002128 class_initializer_code_bytes(0),
2129 large_initializer_code_bytes(0),
2130 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002131 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002132 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002133 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002134 dex_instruction_bytes(0) {}
2135
Elliott Hughesa0e18062012-04-13 15:59:59 -07002136 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002137 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002138 size_t bytes;
2139 size_t count;
2140 };
2141 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2142 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002143
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002144 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002145 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2146 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002147 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002148 it->second.count += 1;
2149 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002150 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002151 }
2152 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002153
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002154 double PercentOfOatBytes(size_t size) {
2155 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2156 }
2157
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002158 double PercentOfFileBytes(size_t size) {
2159 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2160 }
2161
2162 double PercentOfObjectBytes(size_t size) {
2163 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2164 }
2165
Mathieu Chartiere401d142015-04-22 13:56:20 -07002166 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002167 method_outlier_size.push_back(total_size);
2168 method_outlier_expansion.push_back(expansion);
2169 method_outlier.push_back(method);
2170 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002171
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002172 void DumpOutliers(std::ostream& os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002173 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002174 size_t sum_of_sizes = 0;
2175 size_t sum_of_sizes_squared = 0;
2176 size_t sum_of_expansion = 0;
2177 size_t sum_of_expansion_squared = 0;
2178 size_t n = method_outlier_size.size();
Jeff Haodcdc85b2015-12-04 14:06:18 -08002179 if (n == 0) {
2180 return;
2181 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002182 for (size_t i = 0; i < n; i++) {
2183 size_t cur_size = method_outlier_size[i];
2184 sum_of_sizes += cur_size;
2185 sum_of_sizes_squared += cur_size * cur_size;
2186 double cur_expansion = method_outlier_expansion[i];
2187 sum_of_expansion += cur_expansion;
2188 sum_of_expansion_squared += cur_expansion * cur_expansion;
2189 }
2190 size_t size_mean = sum_of_sizes / n;
2191 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2192 double expansion_mean = sum_of_expansion / n;
2193 double expansion_variance =
2194 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2195
2196 // Dump methods whose size is a certain number of standard deviations from the mean
2197 size_t dumped_values = 0;
2198 size_t skipped_values = 0;
2199 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2200 size_t cur_size_variance = i * i * size_variance;
2201 bool first = true;
2202 for (size_t j = 0; j < n; j++) {
2203 size_t cur_size = method_outlier_size[j];
2204 if (cur_size > size_mean) {
2205 size_t cur_var = cur_size - size_mean;
2206 cur_var = cur_var * cur_var;
2207 if (cur_var > cur_size_variance) {
2208 if (dumped_values > 20) {
2209 if (i == 1) {
2210 skipped_values++;
2211 } else {
2212 i = 2; // jump to counting for 1 standard deviation
2213 break;
2214 }
2215 } else {
2216 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002217 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002218 first = false;
2219 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002220 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002221 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002222 method_outlier_size[j] = 0; // don't consider this method again
2223 dumped_values++;
2224 }
2225 }
2226 }
2227 }
2228 }
2229 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002230 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002231 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002232 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002233 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002234
2235 // Dump methods whose expansion is a certain number of standard deviations from the mean
2236 dumped_values = 0;
2237 skipped_values = 0;
2238 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2239 double cur_expansion_variance = i * i * expansion_variance;
2240 bool first = true;
2241 for (size_t j = 0; j < n; j++) {
2242 double cur_expansion = method_outlier_expansion[j];
2243 if (cur_expansion > expansion_mean) {
2244 size_t cur_var = cur_expansion - expansion_mean;
2245 cur_var = cur_var * cur_var;
2246 if (cur_var > cur_expansion_variance) {
2247 if (dumped_values > 20) {
2248 if (i == 1) {
2249 skipped_values++;
2250 } else {
2251 i = 2; // jump to counting for 1 standard deviation
2252 break;
2253 }
2254 } else {
2255 if (first) {
2256 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002257 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002258 first = false;
2259 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002260 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002261 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002262 method_outlier_expansion[j] = 0.0; // don't consider this method again
2263 dumped_values++;
2264 }
2265 }
2266 }
2267 }
2268 }
2269 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002270 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002271 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002272 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002273 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002274 }
2275
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002276 void Dump(std::ostream& os, std::ostream& indent_os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002277 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002278 {
2279 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2280 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002281 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2282 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2283 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2284 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2285 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2286 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002287 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002288 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2289 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002290 header_bytes, PercentOfFileBytes(header_bytes),
2291 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002292 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2293 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002294 dex_cache_arrays_bytes,
2295 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002296 interned_strings_bytes,
2297 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002298 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002299 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002300 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2301 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002302 CHECK_EQ(file_bytes,
2303 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2304 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2305 bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002306 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002307
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002308 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002309 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002310 for (const auto& sizes_and_count : sizes_and_counts) {
2311 const std::string& descriptor(sizes_and_count.first);
2312 double average = static_cast<double>(sizes_and_count.second.bytes) /
2313 static_cast<double>(sizes_and_count.second.count);
2314 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002315 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002316 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002317 descriptor.c_str(), sizes_and_count.second.bytes,
2318 sizes_and_count.second.count, average, percent);
2319 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002320 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002321 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002322 CHECK_EQ(object_bytes, object_bytes_total);
2323
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002324 os << StringPrintf("oat_file_bytes = %8zd\n"
2325 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2326 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2327 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2328 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2329 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2330 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002331 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002332 managed_code_bytes,
2333 PercentOfOatBytes(managed_code_bytes),
2334 managed_to_native_code_bytes,
2335 PercentOfOatBytes(managed_to_native_code_bytes),
2336 native_to_managed_code_bytes,
2337 PercentOfOatBytes(native_to_managed_code_bytes),
2338 class_initializer_code_bytes,
2339 PercentOfOatBytes(class_initializer_code_bytes),
2340 large_initializer_code_bytes,
2341 PercentOfOatBytes(large_initializer_code_bytes),
2342 large_method_code_bytes,
2343 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002344 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002345 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002346 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002347 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2348 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002349 }
2350
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002351 os << "\n" << StringPrintf("gc_map_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2352 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2353 "vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002354 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
2355 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
2356 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002357 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002358
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002359 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2360 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002361 static_cast<double>(managed_code_bytes) /
2362 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002363 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002364 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002365 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002366
2367 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002368 }
2369 } stats_;
2370
2371 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002372 enum {
2373 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2374 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2375 kLargeConstructorDexBytes = 4000,
2376 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2377 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2378 kLargeMethodDexBytes = 16000
2379 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002380
2381 // For performance, use the *os_ directly for anything that doesn't need indentation
2382 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002383 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002384 VariableIndentationOutputStream vios_;
2385 ScopedIndentation indent1_;
2386
Ian Rogers1d54e732013-05-02 21:10:01 -07002387 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002388 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002389 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002390 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002391 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002392
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002393 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002394};
2395
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002396static int DumpImage(gc::space::ImageSpace* image_space,
2397 OatDumperOptions* options,
2398 std::ostream* os) SHARED_REQUIRES(Locks::mutator_lock_) {
2399 const ImageHeader& image_header = image_space->GetImageHeader();
2400 if (!image_header.IsValid()) {
2401 fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
2402 return EXIT_FAILURE;
2403 }
2404 ImageDumper image_dumper(os, *image_space, image_header, options);
2405 if (!image_dumper.Dump()) {
2406 return EXIT_FAILURE;
2407 }
2408 return EXIT_SUCCESS;
2409}
2410
2411static int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002412 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002413 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002414 options->class_loader_ = &null_class_loader;
2415
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002416 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002417 if (options->app_image_ != nullptr) {
2418 if (options->app_oat_ == nullptr) {
2419 LOG(ERROR) << "Can not dump app image without app oat file";
Jeff Haodcdc85b2015-12-04 14:06:18 -08002420 return EXIT_FAILURE;
2421 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002422 // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
2423 // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
2424 // pointers into 32 bit pointer sized ArtMethods.
2425 std::string error_msg;
2426 std::unique_ptr<OatFile> oat_file(OatFile::Open(options->app_oat_,
2427 options->app_oat_,
2428 nullptr,
2429 nullptr,
2430 false,
2431 /*low_4gb*/true,
2432 nullptr,
2433 &error_msg));
2434 if (oat_file == nullptr) {
2435 LOG(ERROR) << "Failed to open oat file " << options->app_oat_ << " with error " << error_msg;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002436 return EXIT_FAILURE;
2437 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002438 std::unique_ptr<gc::space::ImageSpace> space(
2439 gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg));
2440 if (space == nullptr) {
2441 LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error "
2442 << error_msg;
2443 }
2444 // Open dex files for the image.
2445 std::vector<std::unique_ptr<const DexFile>> dex_files;
2446 if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
2447 LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
2448 << error_msg;
2449 }
2450 // Dump the actual image.
2451 int result = DumpImage(space.get(), options, os);
2452 if (result != EXIT_SUCCESS) {
2453 return result;
2454 }
2455 // Fall through to dump the boot images.
2456 }
2457
2458 gc::Heap* heap = runtime->GetHeap();
2459 CHECK(heap->HasBootImageSpace()) << "No image spaces";
2460 for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
2461 int result = DumpImage(image_space, options, os);
2462 if (result != EXIT_SUCCESS) {
2463 return result;
2464 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002465 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002466 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002467}
2468
Andreas Gampe00b25f32014-09-17 21:49:05 -07002469static int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
2470 std::ostream* os) {
2471 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2472
2473 Thread* self = Thread::Current();
2474 CHECK(self != nullptr);
2475 // Need well-known-classes.
2476 WellKnownClasses::Init(self->GetJniEnv());
2477
2478 // Need to register dex files to get a working dex cache.
2479 ScopedObjectAccess soa(self);
2480 ClassLinker* class_linker = runtime->GetClassLinker();
Mathieu Chartierd57d4542015-10-14 10:55:30 -07002481 runtime->GetOatFileManager().RegisterOatFile(std::unique_ptr<const OatFile>(oat_file));
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002482 std::vector<const DexFile*> class_path;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002483 for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
2484 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002485 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002486 CHECK(dex_file != nullptr) << error_msg;
Mathieu Chartierd57d4542015-10-14 10:55:30 -07002487 class_linker->RegisterDexFile(*dex_file, runtime->GetLinearAlloc());
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002488 class_path.push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002489 }
2490
2491 // Need a class loader.
Andreas Gampe00b25f32014-09-17 21:49:05 -07002492 // Fake that we're a compiler.
Mathieu Chartier966878d2016-01-14 14:33:29 -08002493 jobject class_loader = class_linker->CreatePathClassLoader(self, class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002494
2495 // Use the class loader while dumping.
2496 StackHandleScope<1> scope(self);
2497 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
2498 soa.Decode<mirror::ClassLoader*>(class_loader));
2499 options->class_loader_ = &loader_handle;
2500
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002501 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002502 bool success = oat_dumper.Dump(*os);
2503 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2504}
2505
2506static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002507 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002508 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002509 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002510 options->class_loader_ = &null_class_loader;
2511
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002512 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002513 bool success = oat_dumper.Dump(*os);
2514 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2515}
2516
2517static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2518 std::ostream* os) {
2519 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002520 OatFile* oat_file = OatFile::Open(oat_filename,
2521 oat_filename,
2522 nullptr,
2523 nullptr,
2524 false,
2525 /*low_4gb*/false,
2526 nullptr,
2527 &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002528 if (oat_file == nullptr) {
2529 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2530 return EXIT_FAILURE;
2531 }
2532
2533 if (runtime != nullptr) {
2534 return DumpOatWithRuntime(runtime, oat_file, options, os);
2535 } else {
2536 return DumpOatWithoutRuntime(oat_file, options, os);
2537 }
2538}
2539
David Srbecky2fdd03c2016-03-10 15:32:37 +00002540static int SymbolizeOat(const char* oat_filename, std::string& output_name, bool no_bits) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002541 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002542 OatFile* oat_file = OatFile::Open(oat_filename,
2543 oat_filename,
2544 nullptr,
2545 nullptr,
2546 false,
2547 /*low_4gb*/false,
2548 nullptr,
2549 &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002550 if (oat_file == nullptr) {
2551 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2552 return EXIT_FAILURE;
2553 }
2554
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002555 bool result;
2556 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
2557 // files for 64-bit code in the past.
2558 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002559 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file, output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002560 result = oat_symbolizer.Symbolize();
2561 } else {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002562 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file, output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002563 result = oat_symbolizer.Symbolize();
2564 }
2565 if (!result) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002566 fprintf(stderr, "Failed to symbolize\n");
2567 return EXIT_FAILURE;
2568 }
2569
2570 return EXIT_SUCCESS;
2571}
2572
Igor Murashkin37743352014-11-13 14:38:00 -08002573struct OatdumpArgs : public CmdlineArgs {
2574 protected:
2575 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002576
Igor Murashkin37743352014-11-13 14:38:00 -08002577 virtual ParseStatus ParseCustom(const StringPiece& option,
2578 std::string* error_msg) OVERRIDE {
2579 {
2580 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
2581 if (base_parse != kParseUnknownArgument) {
2582 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002583 }
2584 }
2585
Igor Murashkin37743352014-11-13 14:38:00 -08002586 if (option.starts_with("--oat-file=")) {
2587 oat_filename_ = option.substr(strlen("--oat-file=")).data();
2588 } else if (option.starts_with("--image=")) {
2589 image_location_ = option.substr(strlen("--image=")).data();
2590 } else if (option =="--dump:raw_mapping_table") {
2591 dump_raw_mapping_table_ = true;
2592 } else if (option == "--dump:raw_gc_map") {
2593 dump_raw_gc_map_ = true;
2594 } else if (option == "--no-dump:vmap") {
2595 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01002596 } else if (option =="--dump:code_info_stack_maps") {
2597 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002598 } else if (option == "--no-disassemble") {
2599 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00002600 } else if (option =="--header-only") {
2601 dump_header_only_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002602 } else if (option.starts_with("--symbolize=")) {
2603 oat_filename_ = option.substr(strlen("--symbolize=")).data();
2604 symbolize_ = true;
David Srbecky2fdd03c2016-03-10 15:32:37 +00002605 } else if (option.starts_with("--only-keep-debug")) {
2606 only_keep_debug_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002607 } else if (option.starts_with("--class-filter=")) {
2608 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002609 } else if (option.starts_with("--method-filter=")) {
2610 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002611 } else if (option.starts_with("--list-classes")) {
2612 list_classes_ = true;
2613 } else if (option.starts_with("--list-methods")) {
2614 list_methods_ = true;
2615 } else if (option.starts_with("--export-dex-to=")) {
2616 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
2617 } else if (option.starts_with("--addr2instr=")) {
2618 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
2619 *error_msg = "Address conversion failed";
2620 return kParseError;
2621 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002622 } else if (option.starts_with("--app-image=")) {
2623 app_image_ = option.substr(strlen("--app-image=")).data();
2624 } else if (option.starts_with("--app-oat=")) {
2625 app_oat_ = option.substr(strlen("--app-oat=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002626 } else {
2627 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002628 }
2629
Igor Murashkin37743352014-11-13 14:38:00 -08002630 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002631 }
2632
Igor Murashkin37743352014-11-13 14:38:00 -08002633 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
2634 // Infer boot image location from the image location if possible.
2635 if (boot_image_location_ == nullptr) {
2636 boot_image_location_ = image_location_;
2637 }
2638
2639 // Perform the parent checks.
2640 ParseStatus parent_checks = Base::ParseChecks(error_msg);
2641 if (parent_checks != kParseOk) {
2642 return parent_checks;
2643 }
2644
2645 // Perform our own checks.
2646 if (image_location_ == nullptr && oat_filename_ == nullptr) {
2647 *error_msg = "Either --image or --oat-file must be specified";
2648 return kParseError;
2649 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
2650 *error_msg = "Either --image or --oat-file must be specified but not both";
2651 return kParseError;
2652 }
2653
2654 return kParseOk;
2655 }
2656
2657 virtual std::string GetUsage() const {
2658 std::string usage;
2659
2660 usage +=
2661 "Usage: oatdump [options] ...\n"
2662 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
2663 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
2664 "\n"
2665 // Either oat-file or image is required.
2666 " --oat-file=<file.oat>: specifies an input oat filename.\n"
2667 " Example: --oat-file=/system/framework/boot.oat\n"
2668 "\n"
2669 " --image=<file.art>: specifies an input image location.\n"
2670 " Example: --image=/system/framework/boot.art\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002671 "\n"
2672 " --app-image=<file.art>: specifies an input app image. Must also have a specified\n"
2673 " boot image and app oat file.\n"
2674 " Example: --app-image=app.art\n"
2675 "\n"
2676 " --app-oat=<file.odex>: specifies an input app oat.\n"
2677 " Example: --app-oat=app.odex\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002678 "\n";
2679
2680 usage += Base::GetUsage();
2681
2682 usage += // Optional.
2683 " --dump:raw_mapping_table enables dumping of the mapping table.\n"
2684 " Example: --dump:raw_mapping_table\n"
2685 "\n"
Mathieu Chartier19510f02015-05-26 14:44:35 -07002686 " --dump:raw_gc_map enables dumping of the GC map.\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002687 " Example: --dump:raw_gc_map\n"
2688 "\n"
2689 " --no-dump:vmap may be used to disable vmap dumping.\n"
2690 " Example: --no-dump:vmap\n"
2691 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01002692 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
2693 " Example: --dump:code_info_stack_maps\n"
2694 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002695 " --no-disassemble may be used to disable disassembly.\n"
2696 " Example: --no-disassemble\n"
2697 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00002698 " --header-only may be used to print only the oat header.\n"
2699 " Example: --header-only\n"
2700 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002701 " --list-classes may be used to list target file classes (can be used with filters).\n"
2702 " Example: --list-classes\n"
2703 " Example: --list-classes --class-filter=com.example.foo\n"
2704 "\n"
2705 " --list-methods may be used to list target file methods (can be used with filters).\n"
2706 " Example: --list-methods\n"
2707 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
2708 "\n"
2709 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
2710 " Example: --symbolize=/system/framework/boot.oat\n"
2711 "\n"
David Srbecky2fdd03c2016-03-10 15:32:37 +00002712 " --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
2713 " .rodata and .text sections are omitted in the output file to save space.\n"
2714 " Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
2715 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002716 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
2717 " Example: --class-filter=com.example.foo\n"
2718 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002719 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
2720 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002721 "\n"
2722 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
2723 " Example: --export-dex-to=/data/local/tmp\n"
2724 "\n"
2725 " --addr2instr=<address>: output matching method disassembled code from relative\n"
2726 " address (e.g. PC from crash dump)\n"
2727 " Example: --addr2instr=0x00001a3b\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002728 "\n";
2729
2730 return usage;
2731 }
2732
2733 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07002734 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002735 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00002736 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07002737 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002738 std::string elf_filename_prefix_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002739 bool dump_raw_mapping_table_ = false;
2740 bool dump_raw_gc_map_ = false;
2741 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01002742 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002743 bool disassemble_code_ = true;
2744 bool symbolize_ = false;
David Srbecky2fdd03c2016-03-10 15:32:37 +00002745 bool only_keep_debug_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002746 bool list_classes_ = false;
2747 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00002748 bool dump_header_only_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002749 uint32_t addr2instr_ = 0;
2750 const char* export_dex_location_ = nullptr;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002751 const char* app_image_ = nullptr;
2752 const char* app_oat_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002753};
2754
Igor Murashkin37743352014-11-13 14:38:00 -08002755struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
2756 virtual bool NeedsRuntime() OVERRIDE {
2757 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002758
Igor Murashkin37743352014-11-13 14:38:00 -08002759 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
2760 bool absolute_addresses = (args_->oat_filename_ == nullptr);
2761
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002762 oat_dumper_options_.reset(new OatDumperOptions(
Igor Murashkin37743352014-11-13 14:38:00 -08002763 args_->dump_raw_mapping_table_,
2764 args_->dump_raw_gc_map_,
2765 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01002766 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08002767 args_->disassemble_code_,
2768 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002769 args_->class_filter_,
2770 args_->method_filter_,
2771 args_->list_classes_,
2772 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00002773 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002774 args_->export_dex_location_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002775 args_->app_image_,
2776 args_->app_oat_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002777 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08002778
2779 return (args_->boot_image_location_ != nullptr || args_->image_location_ != nullptr) &&
2780 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002781 }
2782
Igor Murashkin37743352014-11-13 14:38:00 -08002783 virtual bool ExecuteWithoutRuntime() OVERRIDE {
2784 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08002785 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002786
Mathieu Chartierd424d082014-10-15 10:31:46 -07002787 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08002788
Andreas Gampec24f3992014-12-17 20:40:11 -08002789 if (args_->symbolize_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002790 // ELF has special kind of section called SHT_NOBITS which allows us to create
2791 // sections which exist but their data is omitted from the ELF file to save space.
2792 // This is what "strip --only-keep-debug" does when it creates separate ELF file
2793 // with only debug data. We use it in similar way to exclude .rodata and .text.
2794 bool no_bits = args_->only_keep_debug_;
2795 return SymbolizeOat(args_->oat_filename_, args_->output_name_, no_bits) == EXIT_SUCCESS;
Andreas Gampec24f3992014-12-17 20:40:11 -08002796 } else {
2797 return DumpOat(nullptr,
2798 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002799 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08002800 args_->os_) == EXIT_SUCCESS;
2801 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002802 }
2803
Igor Murashkin37743352014-11-13 14:38:00 -08002804 virtual bool ExecuteWithRuntime(Runtime* runtime) {
2805 CHECK(args_ != nullptr);
2806
2807 if (args_->oat_filename_ != nullptr) {
2808 return DumpOat(runtime,
2809 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002810 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08002811 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002812 }
Igor Murashkin37743352014-11-13 14:38:00 -08002813
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002814 return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002815 }
2816
Igor Murashkin37743352014-11-13 14:38:00 -08002817 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
2818};
Andreas Gampe00b25f32014-09-17 21:49:05 -07002819
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002820} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07002821
2822int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08002823 art::OatdumpMain main;
2824 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002825}