blob: aab4f8bc0cf9fa79bd9fb42b665898a44f7cfb6a [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"
Elliott Hughes76160052012-12-12 16:31:20 -080029#include "base/unix_file/fd_file.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070030#include "class_linker.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070032#include "dex_file-inl.h"
Elliott Hughese3c845c2012-02-28 17:23:01 -080033#include "dex_instruction.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080034#include "disassembler.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070035#include "elf_builder.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080036#include "gc_map.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070037#include "gc/space/image_space.h"
38#include "gc/space/large_object_space.h"
39#include "gc/space/space-inl.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070040#include "image.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080041#include "indenter.h"
Ian Rogers1809a722013-08-09 22:05:32 -070042#include "mapping_table.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070043#include "mirror/art_field-inl.h"
44#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045#include "mirror/array-inl.h"
46#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047#include "mirror/object-inl.h"
48#include "mirror/object_array-inl.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080049#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010050#include "oat_file-inl.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080051#include "os.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070052#include "output_stream.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070053#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070054#include "scoped_thread_state_change.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070055#include "ScopedLocalRef.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080056#include "thread_list.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080057#include "verifier/dex_gc_map.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080058#include "verifier/method_verifier.h"
Ian Rogers1809a722013-08-09 22:05:32 -070059#include "vmap_table.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070060#include "well_known_classes.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070061
Igor Murashkin37743352014-11-13 14:38:00 -080062#include <sys/stat.h>
63#include "cmdline.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070064
Igor Murashkin37743352014-11-13 14:38:00 -080065namespace art {
Brian Carlstrom78128a62011-09-15 17:21:19 -070066
Ian Rogersff1ed472011-09-20 13:46:24 -070067const char* image_roots_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -080068 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -070069 "kImtConflictMethod",
Mathieu Chartier2d2621a2014-10-23 16:48:06 -070070 "kImtUnimplementedMethod",
Jeff Hao88474b42013-10-23 16:24:40 -070071 "kDefaultImt",
Brian Carlstrome24fa612011-09-29 00:53:55 -070072 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -070073 "kRefsOnlySaveMethod",
74 "kRefsAndArgsSaveMethod",
Brian Carlstrom58ae9412011-10-04 00:56:06 -070075 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070076 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070077};
78
Ian Rogers0279ebb2014-10-08 17:27:48 -070079class OatSymbolizer FINAL : public CodeOutput {
Andreas Gampe54fc26c2014-09-04 21:47:42 -070080 public:
Ian Rogers0279ebb2014-10-08 17:27:48 -070081 explicit OatSymbolizer(const OatFile* oat_file, const std::string& output_name) :
82 oat_file_(oat_file), builder_(nullptr), elf_output_(nullptr),
83 output_name_(output_name.empty() ? "symbolized.oat" : output_name) {
84 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -070085
86 bool Init() {
87 Elf32_Word oat_data_size = oat_file_->GetOatHeader().GetExecutableOffset();
88
89 uint32_t diff = static_cast<uint32_t>(oat_file_->End() - oat_file_->Begin());
90 uint32_t oat_exec_size = diff - oat_data_size;
Vladimir Marko5c42c292015-02-25 12:02:49 +000091 uint32_t oat_bss_size = oat_file_->BssSize();
Andreas Gampe54fc26c2014-09-04 21:47:42 -070092
Andreas Gampe54fc26c2014-09-04 21:47:42 -070093 elf_output_ = OS::CreateEmptyFile(output_name_.c_str());
94
95 builder_.reset(new ElfBuilder<Elf32_Word, Elf32_Sword, Elf32_Addr, Elf32_Dyn,
96 Elf32_Sym, Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr>(
97 this,
98 elf_output_,
99 oat_file_->GetOatHeader().GetInstructionSet(),
100 0,
101 oat_data_size,
102 oat_data_size,
103 oat_exec_size,
Vladimir Marko5c42c292015-02-25 12:02:49 +0000104 RoundUp(oat_data_size + oat_exec_size, kPageSize),
105 oat_bss_size,
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700106 true,
107 false));
108
109 if (!builder_->Init()) {
110 builder_.reset(nullptr);
111 return false;
112 }
113
114 return true;
115 }
116
117 typedef void (OatSymbolizer::*Callback)(const DexFile::ClassDef&,
118 uint32_t,
119 const OatFile::OatMethod&,
120 const DexFile&,
121 uint32_t,
122 const DexFile::CodeItem*,
123 uint32_t);
124
125 bool Symbolize() {
126 if (builder_.get() == nullptr) {
127 return false;
128 }
129
130 Walk(&art::OatSymbolizer::RegisterForDedup);
131
132 NormalizeState();
133
134 Walk(&art::OatSymbolizer::AddSymbol);
135
136 bool result = builder_->Write();
137
Andreas Gampe4303ba92014-11-06 01:00:46 -0800138 // Ignore I/O errors.
139 UNUSED(elf_output_->FlushClose());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700140
141 return result;
142 }
143
144 void Walk(Callback callback) {
145 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
146 for (size_t i = 0; i < oat_dex_files.size(); i++) {
147 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
148 CHECK(oat_dex_file != NULL);
149 WalkOatDexFile(oat_dex_file, callback);
150 }
151 }
152
153 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file, Callback callback) {
154 std::string error_msg;
155 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
156 if (dex_file.get() == nullptr) {
157 return;
158 }
159 for (size_t class_def_index = 0;
160 class_def_index < dex_file->NumClassDefs();
161 class_def_index++) {
162 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
163 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
164 OatClassType type = oat_class.GetType();
165 switch (type) {
166 case kOatClassAllCompiled:
167 case kOatClassSomeCompiled:
168 WalkOatClass(oat_class, *dex_file.get(), class_def, callback);
169 break;
170
171 case kOatClassNoneCompiled:
172 case kOatClassMax:
173 // Ignore.
174 break;
175 }
176 }
177 }
178
179 void WalkOatClass(const OatFile::OatClass& oat_class, const DexFile& dex_file,
180 const DexFile::ClassDef& class_def, Callback callback) {
Ian Rogers13735952014-10-08 12:43:28 -0700181 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700182 if (class_data == nullptr) { // empty class such as a marker interface?
183 return;
184 }
185 // Note: even if this is an interface or a native class, we still have to walk it, as there
186 // might be a static initializer.
187 ClassDataItemIterator it(dex_file, class_data);
188 SkipAllFields(&it);
189 uint32_t class_method_idx = 0;
190 while (it.HasNextDirectMethod()) {
191 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
192 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700193 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700194 class_method_idx++;
195 it.Next();
196 }
197 while (it.HasNextVirtualMethod()) {
198 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
199 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700200 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700201 class_method_idx++;
202 it.Next();
203 }
204 DCHECK(!it.HasNext());
205 }
206
207 void WalkOatMethod(const DexFile::ClassDef& class_def, uint32_t class_method_index,
208 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
209 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
210 uint32_t method_access_flags, Callback callback) {
211 if ((method_access_flags & kAccAbstract) != 0) {
212 // Abstract method, no code.
213 return;
214 }
215 if (oat_method.GetCodeOffset() == 0) {
216 // No code.
217 return;
218 }
219
220 (this->*callback)(class_def, class_method_index, oat_method, dex_file, dex_method_idx, code_item,
221 method_access_flags);
222 }
223
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700224 void RegisterForDedup(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
225 uint32_t class_method_index ATTRIBUTE_UNUSED,
226 const OatFile::OatMethod& oat_method,
227 const DexFile& dex_file ATTRIBUTE_UNUSED,
228 uint32_t dex_method_idx ATTRIBUTE_UNUSED,
229 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
230 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700231 state_[oat_method.GetCodeOffset()]++;
232 }
233
234 void NormalizeState() {
235 for (auto& x : state_) {
236 if (x.second == 1) {
237 state_[x.first] = 0;
238 }
239 }
240 }
241
242 enum class DedupState { // private
243 kNotDeduplicated,
244 kDeduplicatedFirst,
245 kDeduplicatedOther
246 };
247 DedupState IsDuplicated(uint32_t offset) {
248 if (state_[offset] == 0) {
249 return DedupState::kNotDeduplicated;
250 }
251 if (state_[offset] == 1) {
252 return DedupState::kDeduplicatedOther;
253 }
254 state_[offset] = 1;
255 return DedupState::kDeduplicatedFirst;
256 }
257
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700258 void AddSymbol(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
259 uint32_t class_method_index ATTRIBUTE_UNUSED,
260 const OatFile::OatMethod& oat_method,
261 const DexFile& dex_file,
262 uint32_t dex_method_idx,
263 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
264 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700265 DedupState dedup = IsDuplicated(oat_method.GetCodeOffset());
266 if (dedup != DedupState::kDeduplicatedOther) {
267 std::string pretty_name = PrettyMethod(dex_method_idx, dex_file, true);
268
269 if (dedup == DedupState::kDeduplicatedFirst) {
270 pretty_name = "[Dedup]" + pretty_name;
271 }
272
273 ElfSymtabBuilder<Elf32_Word, Elf32_Sword, Elf32_Addr,
Ian Rogers0279ebb2014-10-08 17:27:48 -0700274 Elf32_Sym, Elf32_Shdr>* symtab = builder_->GetSymtabBuilder();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700275
Ian Rogers0279ebb2014-10-08 17:27:48 -0700276 symtab->AddSymbol(pretty_name, &builder_->GetTextBuilder(),
277 oat_method.GetCodeOffset() - oat_file_->GetOatHeader().GetExecutableOffset(),
278 true, oat_method.GetQuickCodeSize(), STB_GLOBAL, STT_FUNC);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700279 }
280 }
281
Vladimir Markof4da6752014-08-01 19:04:18 +0100282 // Set oat data offset. Required by ElfBuilder/CodeOutput.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700283 void SetCodeOffset(size_t offset ATTRIBUTE_UNUSED) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100284 // Nothing to do.
285 }
286
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700287 // Write oat code. Required by ElfBuilder/CodeOutput.
288 bool Write(OutputStream* out) {
289 return out->WriteFully(oat_file_->Begin(), oat_file_->End() - oat_file_->Begin());
290 }
291
292 private:
293 static void SkipAllFields(ClassDataItemIterator* it) {
294 while (it->HasNextStaticField()) {
295 it->Next();
296 }
297 while (it->HasNextInstanceField()) {
298 it->Next();
299 }
300 }
301
302 const OatFile* oat_file_;
303 std::unique_ptr<ElfBuilder<Elf32_Word, Elf32_Sword, Elf32_Addr, Elf32_Dyn,
304 Elf32_Sym, Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr> > builder_;
305 File* elf_output_;
306 std::unordered_map<uint32_t, uint32_t> state_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700307 const std::string output_name_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700308};
309
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700310class OatDumperOptions {
311 public:
312 OatDumperOptions(bool dump_raw_mapping_table,
313 bool dump_raw_gc_map,
314 bool dump_vmap,
315 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700316 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800317 const char* class_filter,
318 const char* method_filter,
319 bool list_classes,
320 bool list_methods,
321 const char* export_dex_location,
322 uint32_t addr2instr)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700323 : dump_raw_mapping_table_(dump_raw_mapping_table),
324 dump_raw_gc_map_(dump_raw_gc_map),
325 dump_vmap_(dump_vmap),
326 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700327 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800328 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000329 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800330 list_classes_(list_classes),
331 list_methods_(list_methods),
332 export_dex_location_(export_dex_location),
333 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800334 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700335
336 const bool dump_raw_mapping_table_;
337 const bool dump_raw_gc_map_;
338 const bool dump_vmap_;
339 const bool disassemble_code_;
340 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800341 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000342 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800343 const bool list_classes_;
344 const bool list_methods_;
345 const char* const export_dex_location_;
346 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700347 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700348};
349
Elliott Hughese3c845c2012-02-28 17:23:01 -0800350class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700351 public:
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800352 explicit OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000353 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800354 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700355 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800356 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800357 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
358 disassembler_(Disassembler::Create(instruction_set_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800359 new DisassemblerOptions(options_.absolute_addresses_,
Alexandre Ramesa37d9252014-10-27 11:28:14 +0000360 oat_file.Begin(),
361 true /* can_read_litals_ */))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800362 CHECK(options_.class_loader_ != nullptr);
363 CHECK(options_.class_filter_ != nullptr);
364 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800365 AddAllOffsets();
366 }
367
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700368 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700369 delete disassembler_;
370 }
371
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800372 InstructionSet GetInstructionSet() {
373 return instruction_set_;
374 }
375
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700376 bool Dump(std::ostream& os) {
377 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800378 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700379
380 os << "MAGIC:\n";
381 os << oat_header.GetMagic() << "\n\n";
382
383 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800384 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700385
Elliott Hughesa72ec822012-03-05 17:12:22 -0800386 os << "INSTRUCTION SET:\n";
387 os << oat_header.GetInstructionSet() << "\n\n";
388
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700389 {
390 std::unique_ptr<const InstructionSetFeatures> features(
391 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
392 oat_header.GetInstructionSetFeaturesBitmap()));
393 os << "INSTRUCTION SET FEATURES:\n";
394 os << features->GetFeatureString() << "\n\n";
395 }
Dave Allison70202782013-10-22 17:52:19 -0700396
Brian Carlstromaded5f72011-10-07 17:15:04 -0700397 os << "DEX FILE COUNT:\n";
398 os << oat_header.GetDexFileCount() << "\n\n";
399
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800400#define DUMP_OAT_HEADER_OFFSET(label, offset) \
401 os << label " OFFSET:\n"; \
402 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800403 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800404 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
405 } \
406 os << StringPrintf("\n\n");
407
408 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
409 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
410 GetInterpreterToInterpreterBridgeOffset);
411 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
412 GetInterpreterToCompiledCodeBridgeOffset);
413 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
414 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800415 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
416 GetQuickGenericJniTrampolineOffset);
417 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
418 GetQuickImtConflictTrampolineOffset);
419 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
420 GetQuickResolutionTrampolineOffset);
421 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
422 GetQuickToInterpreterBridgeOffset);
423#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700424
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700425 os << "IMAGE PATCH DELTA:\n";
426 os << StringPrintf("%d (0x%08x)\n\n",
427 oat_header.GetImagePatchDelta(),
428 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700429
Brian Carlstrom28db0122012-10-18 16:20:41 -0700430 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
431 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
432
433 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800434 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700435
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700436 // Print the key-value store.
437 {
438 os << "KEY VALUE STORE:\n";
439 size_t index = 0;
440 const char* key;
441 const char* value;
442 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
443 os << key << " = " << value << "\n";
444 index++;
445 }
446 os << "\n";
447 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700448
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800449 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700450 os << "BEGIN:\n";
451 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700452
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700453 os << "END:\n";
454 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
455 }
456
457 os << "SIZE:\n";
458 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700459
460 os << std::flush;
461
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800462 // If set, adjust relative address to be searched
463 if (options_.addr2instr_ != 0) {
464 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
465 os << "SEARCH ADDRESS (executable offset + input):\n";
466 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
467 }
468
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800469 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
470 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700471 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800472
473 // If file export selected skip file analysis
474 if (options_.export_dex_location_) {
475 if (!ExportDexFile(os, *oat_dex_file)) {
476 success = false;
477 }
478 } else {
479 if (!DumpOatDexFile(os, *oat_dex_file)) {
480 success = false;
481 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700482 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700483 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700484 os << std::flush;
485 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700486 }
487
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800488 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700489 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
490 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800491 return 0; // Address not in oat file
492 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800493 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
494 reinterpret_cast<uintptr_t>(oat_file_.Begin());
495 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800496 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800497 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800498 return end_offset - begin_offset;
499 }
500
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800501 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700502 return oat_file_.GetOatHeader().GetInstructionSet();
503 }
504
Ian Rogersef7d42f2014-01-06 12:55:46 -0800505 const void* GetQuickOatCode(mirror::ArtMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800506 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
507 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700508 CHECK(oat_dex_file != nullptr);
509 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700510 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700511 if (dex_file.get() == nullptr) {
512 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
513 << "': " << error_msg;
514 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800515 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700516 const DexFile::ClassDef* class_def =
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800517 dex_file->FindClassDef(descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700518 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700519 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100520 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800521 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100522 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800523 }
524 }
525 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700526 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800527 }
528
Brian Carlstromaded5f72011-10-07 17:15:04 -0700529 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800530 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800531 // We don't know the length of the code for each method, but we need to know where to stop
532 // when disassembling. What we do know is that a region of code will be followed by some other
533 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
534 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800535 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
536 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700537 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700538 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700539 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700540 if (dex_file.get() == nullptr) {
541 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
542 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800543 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800544 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800545 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800546 for (size_t class_def_index = 0;
547 class_def_index < dex_file->NumClassDefs();
548 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800549 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100550 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700551 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700552 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800553 ClassDataItemIterator it(*dex_file, class_data);
554 SkipAllFields(it);
555 uint32_t class_method_index = 0;
556 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100557 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800558 it.Next();
559 }
560 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100561 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800562 it.Next();
563 }
564 }
565 }
566 }
567
568 // If the last thing in the file is code for a method, there won't be an offset for the "next"
569 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
570 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800571 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800572 }
573
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700574 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
575 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
576 }
577
Elliott Hughese3c845c2012-02-28 17:23:01 -0800578 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800579 uint32_t code_offset = oat_method.GetCodeOffset();
580 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
581 code_offset &= ~0x1;
582 }
583 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800584 offsets_.insert(oat_method.GetMappingTableOffset());
585 offsets_.insert(oat_method.GetVmapTableOffset());
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800586 offsets_.insert(oat_method.GetGcMapOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800587 }
588
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700589 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
590 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800591 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700592 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800593 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800594 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700595
596 // Create the verifier early.
597
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700598 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700599 std::unique_ptr<const DexFile> dex_file(oat_dex_file.OpenDexFile(&error_msg));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700600 if (dex_file.get() == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700601 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700602 os << std::flush;
603 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700604 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800605 for (size_t class_def_index = 0;
606 class_def_index < dex_file->NumClassDefs();
607 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700608 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
609 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800610
611 // TODO: Support regex
612 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
613 continue;
614 }
615
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700616 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100617 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700618 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
619 class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100620 << " (" << oat_class.GetStatus() << ")"
621 << " (" << oat_class.GetType() << ")\n";
622 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800623 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
624 std::ostream indented_os(&indent_filter);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800625 if (options_.list_classes_) continue;
626 if (!DumpOatClass(indented_os, oat_class, *(dex_file.get()), class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700627 success = false;
628 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800629 if (stop_analysis) {
630 os << std::flush;
631 return success;
632 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700633 }
634
635 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700636 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700637 }
638
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800639 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
640 std::string error_msg;
641 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
642
643 std::unique_ptr<const DexFile> dex_file(oat_dex_file.OpenDexFile(&error_msg));
644 if (dex_file == nullptr) {
645 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
646 return false;
647 }
648 size_t fsize = oat_dex_file.FileSize();
649
650 // Some quick checks just in case
651 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
652 os << "Invalid dex file\n";
653 return false;
654 }
655
656 // Verify output directory exists
657 if (!OS::DirectoryExists(options_.export_dex_location_)) {
658 // TODO: Extend OS::DirectoryExists if symlink support is required
659 os << options_.export_dex_location_ << " output directory not found or symlink\n";
660 return false;
661 }
662
663 // Beautify path names
664 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
665 return false;
666 }
667
668 std::string dex_orig_name;
669 size_t dex_orig_pos = dex_file_location.rfind('/');
670 if (dex_orig_pos == std::string::npos)
671 dex_orig_name = dex_file_location;
672 else
673 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
674
675 // A more elegant approach to efficiently name user installed apps is welcome
676 if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
677 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
678 size_t apk_orig_pos = dex_file_location.rfind('/');
679 if (apk_orig_pos != std::string::npos) {
680 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
681 }
682 }
683
684 std::string out_dex_path(options_.export_dex_location_);
685 if (out_dex_path.back() != '/') {
686 out_dex_path.append("/");
687 }
688 out_dex_path.append(dex_orig_name);
689 out_dex_path.append("_export.dex");
690 if (out_dex_path.length() > PATH_MAX) {
691 return false;
692 }
693
694 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
695 if (file.get() == nullptr) {
696 os << "Failed to open output dex file " << out_dex_path;
697 return false;
698 }
699
700 if (!file->WriteFully(dex_file->Begin(), fsize)) {
701 os << "Failed to write dex file";
702 file->Erase();
703 return false;
704 }
705
706 if (file->FlushCloseOrErase() != 0) {
707 os << "Flush and close failed";
708 return false;
709 }
710
711 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
712 os << std::flush;
713
714 return true;
715 }
716
Elliott Hughese3c845c2012-02-28 17:23:01 -0800717 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700718 while (it.HasNextStaticField()) {
719 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700720 }
Ian Rogers0571d352011-11-03 19:51:38 -0700721 while (it.HasNextInstanceField()) {
722 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700723 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800724 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700725
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700726 bool DumpOatClass(std::ostream& os, const OatFile::OatClass& oat_class, const DexFile& dex_file,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800727 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700728 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800729 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -0700730 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700731 if (class_data == nullptr) { // empty class such as a marker interface?
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700732 os << std::flush;
733 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800734 }
735 ClassDataItemIterator it(dex_file, class_data);
736 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700737 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700738 while (it.HasNextDirectMethod()) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700739 if (!DumpOatMethod(os, class_def, class_method_index, oat_class, dex_file,
740 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800741 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700742 success = false;
743 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800744 if (addr_found) {
745 *stop_analysis = true;
746 return success;
747 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700748 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700749 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700750 }
Ian Rogers0571d352011-11-03 19:51:38 -0700751 while (it.HasNextVirtualMethod()) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700752 if (!DumpOatMethod(os, class_def, class_method_index, oat_class, dex_file,
753 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800754 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700755 success = false;
756 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800757 if (addr_found) {
758 *stop_analysis = true;
759 return success;
760 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700761 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700762 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700763 }
Ian Rogers0571d352011-11-03 19:51:38 -0700764 DCHECK(!it.HasNext());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700765 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700766 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700767 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800768
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700769 static constexpr uint32_t kPrologueBytes = 16;
770
771 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
772 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
773
774 bool DumpOatMethod(std::ostream& os, const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700775 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700776 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800777 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800778 uint32_t method_access_flags, bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700779 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800780
781 // TODO: Support regex
782 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
783 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000784 return success;
785 }
786
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800787 std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800788 os << StringPrintf("%d: %s (dex_method_idx=%d)\n",
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000789 class_method_index, pretty_method.c_str(),
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700790 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800791 if (options_.list_methods_) return success;
792
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800793 Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700794 std::unique_ptr<std::ostream> indent1_os(new std::ostream(&indent1_filter));
795 Indenter indent2_filter(indent1_os->rdbuf(), kIndentChar, kIndentBy1Count);
796 std::unique_ptr<std::ostream> indent2_os(new std::ostream(&indent2_filter));
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800797
798 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
799 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
800 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
801 uint32_t code_offset = oat_method.GetCodeOffset();
802 uint32_t code_size = oat_method.GetQuickCodeSize();
803 if (resolved_addr2instr_ != 0) {
804 if (resolved_addr2instr_ > code_offset + code_size) {
805 return success;
806 } else {
807 *addr_found = true; // stop analyzing file at next iteration
808 }
809 }
810
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800811 {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700812 *indent1_os << "DEX CODE:\n";
813 DumpDexCode(*indent2_os, dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700814 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700815
816 std::unique_ptr<verifier::MethodVerifier> verifier;
817 if (Runtime::Current() != nullptr) {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700818 *indent1_os << "VERIFIER TYPE ANALYSIS:\n";
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700819 verifier.reset(DumpVerifier(*indent2_os, dex_method_idx, &dex_file, class_def, code_item,
820 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -0700821 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800822 {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700823 *indent1_os << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800824 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700825 *indent1_os << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100826 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700827 *indent1_os << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
828 if (oat_method_offsets_offset > oat_file_.Size()) {
829 *indent1_os << StringPrintf(
830 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
831 oat_method_offsets_offset, oat_file_.Size());
832 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
833 os << std::flush;
834 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800835 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700836
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700837 *indent2_os << StringPrintf("code_offset: 0x%08x ", code_offset);
838 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
839 if (aligned_code_begin > oat_file_.Size()) {
840 *indent2_os << StringPrintf("WARNING: "
841 "code offset 0x%08x is past end of file 0x%08zx.\n",
842 aligned_code_begin, oat_file_.Size());
843 success = false;
844 }
845 *indent2_os << "\n";
846
847 *indent2_os << "gc_map: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800848 if (options_.absolute_addresses_) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800849 *indent2_os << StringPrintf("%p ", oat_method.GetGcMap());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700850 }
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800851 uint32_t gc_map_offset = oat_method.GetGcMapOffset();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700852 *indent2_os << StringPrintf("(offset=0x%08x)\n", gc_map_offset);
853 if (gc_map_offset > oat_file_.Size()) {
854 *indent2_os << StringPrintf("WARNING: "
855 "gc map table offset 0x%08x is past end of file 0x%08zx.\n",
856 gc_map_offset, oat_file_.Size());
857 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800858 } else if (options_.dump_raw_gc_map_) {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700859 Indenter indent3_filter(indent2_os->rdbuf(), kIndentChar, kIndentBy1Count);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800860 std::ostream indent3_os(&indent3_filter);
861 DumpGcMap(indent3_os, oat_method, code_item);
862 }
863 }
864 {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700865 *indent1_os << "OatQuickMethodHeader ";
866 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
867 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700868
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800869 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700870 *indent1_os << StringPrintf("%p ", method_header);
871 }
872 *indent1_os << StringPrintf("(offset=0x%08x)\n", method_header_offset);
873 if (method_header_offset > oat_file_.Size()) {
874 *indent1_os << StringPrintf(
875 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
876 method_header_offset, oat_file_.Size());
877 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
878 os << std::flush;
879 return false;
880 }
881
882 *indent2_os << "mapping_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800883 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700884 *indent2_os << StringPrintf("%p ", oat_method.GetMappingTable());
885 }
886 uint32_t mapping_table_offset = oat_method.GetMappingTableOffset();
887 *indent2_os << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset());
888 if (mapping_table_offset > oat_file_.Size()) {
889 *indent2_os << StringPrintf("WARNING: "
890 "mapping table offset 0x%08x is past end of file 0x%08zx. "
891 "mapping table offset was loaded from offset 0x%08x.\n",
892 mapping_table_offset, oat_file_.Size(),
893 oat_method.GetMappingTableOffsetOffset());
894 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800895 } else if (options_.dump_raw_mapping_table_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700896 Indenter indent3_filter(indent2_os->rdbuf(), kIndentChar, kIndentBy1Count);
897 std::ostream indent3_os(&indent3_filter);
898 DumpMappingTable(indent3_os, oat_method);
899 }
900
901 *indent2_os << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800902 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700903 *indent2_os << StringPrintf("%p ", oat_method.GetVmapTable());
904 }
905 uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
906 *indent2_os << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
907 if (vmap_table_offset > oat_file_.Size()) {
908 *indent2_os << StringPrintf("WARNING: "
909 "vmap table offset 0x%08x is past end of file 0x%08zx. "
910 "vmap table offset was loaded from offset 0x%08x.\n",
911 vmap_table_offset, oat_file_.Size(),
912 oat_method.GetVmapTableOffsetOffset());
913 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800914 } else if (options_.dump_vmap_) {
Roland Levillain442b46a2015-02-18 16:54:21 +0000915 DumpVmapData(*indent2_os, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700916 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800917 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700918 {
919 *indent1_os << "QuickMethodFrameInfo\n";
920
921 *indent2_os << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
922 *indent2_os << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
923 DumpSpillMask(*indent2_os, oat_method.GetCoreSpillMask(), false);
924 *indent2_os << "\n";
925 *indent2_os << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
926 DumpSpillMask(*indent2_os, oat_method.GetFpSpillMask(), true);
927 *indent2_os << "\n";
928 }
929 {
930 // Based on spill masks from QuickMethodFrameInfo so placed
931 // after it is dumped, but useful for understanding quick
932 // code, so dumped here.
933 DumpVregLocations(*indent2_os, oat_method, code_item);
934 }
935 {
936 *indent1_os << "CODE: ";
937 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
938 if (code_size_offset > oat_file_.Size()) {
939 *indent2_os << StringPrintf("WARNING: "
940 "code size offset 0x%08x is past end of file 0x%08zx.",
941 code_size_offset, oat_file_.Size());
942 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_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700949 *indent1_os << StringPrintf("%p ", code);
950 }
951 *indent1_os << 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 ? "..." : "");
956
957 if (aligned_code_begin > oat_file_.Size()) {
958 *indent2_os << StringPrintf("WARNING: "
959 "start of code at 0x%08x is past end of file 0x%08zx.",
960 aligned_code_begin, oat_file_.Size());
961 success = false;
962 } else if (aligned_code_end > oat_file_.Size()) {
963 *indent2_os << StringPrintf("WARNING: "
964 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
965 "code size is 0x%08x loaded from offset 0x%08x.\n",
966 aligned_code_end, oat_file_.Size(),
967 code_size, code_size_offset);
968 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800969 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700970 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
971 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, true, kPrologueBytes);
972 }
973 }
974 } else if (code_size > kMaxCodeSize) {
975 *indent2_os << StringPrintf("WARNING: "
976 "code size %d is bigger than max expected threshold of %d. "
977 "code size is 0x%08x loaded from offset 0x%08x.\n",
978 code_size, kMaxCodeSize,
979 code_size, code_size_offset);
980 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800981 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700982 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
983 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, true, kPrologueBytes);
984 }
985 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800986 } else if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700987 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, !success, 0);
988 }
989 }
990 }
991 os << std::flush;
992 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700993 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800994
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800995 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
996 if (spill_mask == 0) {
997 return;
998 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800999 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001000 for (size_t i = 0; i < 32; i++) {
1001 if ((spill_mask & (1 << i)) != 0) {
1002 if (is_float) {
1003 os << "fr" << i;
1004 } else {
1005 os << "r" << i;
1006 }
1007 spill_mask ^= 1 << i; // clear bit
1008 if (spill_mask != 0) {
1009 os << ", ";
1010 } else {
1011 break;
1012 }
1013 }
1014 }
1015 os << ")";
1016 }
1017
Roland Levillain442b46a2015-02-18 16:54:21 +00001018 // Display data stored at the the vmap offset of an oat method.
1019 void DumpVmapData(std::ostream& os,
1020 const OatFile::OatMethod& oat_method,
1021 const DexFile::CodeItem* code_item) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001022 if (oat_method.GetGcMap() == nullptr) {
Roland Levillain442b46a2015-02-18 16:54:21 +00001023 // If the native GC map is null, then this method has been
1024 // compiled with the optimizing compiler. The optimizing
1025 // compiler currently outputs its stack maps in the vmap table.
1026 const void* raw_code_info = oat_method.GetVmapTable();
1027 if (raw_code_info != nullptr) {
1028 CodeInfo code_info(raw_code_info);
1029 DCHECK(code_item != nullptr);
1030 DumpCodeInfo(os, code_info, *code_item);
1031 }
1032 } else {
1033 // Otherwise, display the vmap table.
1034 const uint8_t* raw_table = oat_method.GetVmapTable();
1035 if (raw_table != nullptr) {
1036 VmapTable vmap_table(raw_table);
1037 DumpVmapTable(os, oat_method, vmap_table);
1038 }
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001039 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001040 }
1041
1042 // Display a CodeInfo object emitted by the optimizing compiler.
1043 void DumpCodeInfo(std::ostream& os,
1044 const CodeInfo& code_info,
1045 const DexFile::CodeItem& code_item) {
1046 uint16_t number_of_dex_registers = code_item.registers_size_;
1047 uint32_t code_info_size = code_info.GetOverallSize();
1048 size_t number_of_stack_maps = code_info.GetNumberOfStackMaps();
1049 os << " Optimized CodeInfo (size=" << code_info_size
1050 << ", number_of_dex_registers=" << number_of_dex_registers
1051 << ", number_of_stack_maps=" << number_of_stack_maps << ")\n";
1052 for (size_t i = 0; i < number_of_stack_maps; ++i) {
1053 StackMap stack_map = code_info.GetStackMapAt(i);
1054 // TODO: Display stack_mask value.
1055 os << " StackMap " << i
1056 << std::hex
1057 << " (dex_pc=0x" << stack_map.GetDexPc()
1058 << ", native_pc_offset=0x" << stack_map.GetNativePcOffset()
1059 << ", register_mask=0x" << stack_map.GetRegisterMask()
1060 << std::dec
1061 << ")\n";
1062 if (stack_map.HasDexRegisterMap()) {
1063 DexRegisterMap dex_register_map =
1064 code_info.GetDexRegisterMapOf(stack_map, number_of_dex_registers);
1065 for (size_t j = 0; j < number_of_dex_registers; ++j) {
1066 os << " v" << j << ": "
1067 << DexRegisterMap::PrettyDescriptor(dex_register_map.GetLocationKind(j))
1068 << " (" << dex_register_map.GetValue(j) << ")\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001069 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001070 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001071 // TODO: Display more information from code_info.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001072 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001073 }
1074
Roland Levillain442b46a2015-02-18 16:54:21 +00001075 // Display a vmap table.
1076 void DumpVmapTable(std::ostream& os,
1077 const OatFile::OatMethod& oat_method,
1078 const VmapTable& vmap_table) {
1079 bool first = true;
1080 bool processing_fp = false;
1081 uint32_t spill_mask = oat_method.GetCoreSpillMask();
1082 for (size_t i = 0; i < vmap_table.Size(); i++) {
1083 uint16_t dex_reg = vmap_table[i];
1084 uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
1085 processing_fp ? kFloatVReg : kIntVReg);
1086 os << (first ? "v" : ", v") << dex_reg;
1087 if (!processing_fp) {
1088 os << "/r" << cpu_reg;
1089 } else {
1090 os << "/fr" << cpu_reg;
1091 }
1092 first = false;
1093 if (!processing_fp && dex_reg == 0xFFFF) {
1094 processing_fp = true;
1095 spill_mask = oat_method.GetFpSpillMask();
1096 }
1097 }
1098 os << "\n";
1099 }
1100
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001101 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1102 const DexFile::CodeItem* code_item) {
1103 if (code_item != nullptr) {
1104 size_t num_locals_ins = code_item->registers_size_;
1105 size_t num_ins = code_item->ins_size_;
1106 size_t num_locals = num_locals_ins - num_ins;
1107 size_t num_outs = code_item->outs_size_;
1108
1109 os << "vr_stack_locations:";
1110 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1111 // For readability, delimit the different kinds of VRs.
1112 if (reg == num_locals_ins) {
1113 os << "\n\tmethod*:";
1114 } else if (reg == num_locals && num_ins > 0) {
1115 os << "\n\tins:";
1116 } else if (reg == 0 && num_locals > 0) {
1117 os << "\n\tlocals:";
1118 }
1119
1120 uint32_t offset = StackVisitor::GetVRegOffset(code_item, oat_method.GetCoreSpillMask(),
1121 oat_method.GetFpSpillMask(),
1122 oat_method.GetFrameSizeInBytes(), reg,
1123 GetInstructionSet());
1124 os << " v" << reg << "[sp + #" << offset << "]";
1125 }
1126
1127 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1128 if (out_reg == 0) {
1129 os << "\n\touts:";
1130 }
1131
1132 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1133 os << " v" << out_reg << "[sp + #" << offset << "]";
1134 }
1135
1136 os << "\n";
1137 }
1138 }
1139
Ian Rogersb23a7722012-10-09 16:54:26 -07001140 void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001141 const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
Ian Rogers1809a722013-08-09 22:05:32 -07001142 const uint8_t* raw_table = oat_method.GetVmapTable();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001143 if (raw_table != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001144 const VmapTable vmap_table(raw_table);
1145 uint32_t vmap_offset;
Ian Rogers1809a722013-08-09 22:05:32 -07001146 if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001147 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
1148 uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
1149 : oat_method.GetCoreSpillMask();
1150 os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
Ian Rogersb23a7722012-10-09 16:54:26 -07001151 } else {
1152 uint32_t offset = StackVisitor::GetVRegOffset(code_item, oat_method.GetCoreSpillMask(),
1153 oat_method.GetFpSpillMask(),
Nicolas Geoffray42fcd982014-04-22 11:03:52 +00001154 oat_method.GetFrameSizeInBytes(), reg,
1155 GetInstructionSet());
Ian Rogersb23a7722012-10-09 16:54:26 -07001156 os << "[sp + #" << offset << "]";
1157 }
1158 }
1159 }
1160
Ian Rogersef7d42f2014-01-06 12:55:46 -08001161 void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
1162 const DexFile::CodeItem* code_item,
1163 size_t num_regs, const uint8_t* reg_bitmap) {
1164 bool first = true;
1165 for (size_t reg = 0; reg < num_regs; reg++) {
1166 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1167 if (first) {
1168 os << " v" << reg << " (";
1169 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1170 os << ")";
1171 first = false;
1172 } else {
1173 os << ", v" << reg << " (";
1174 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1175 os << ")";
1176 }
1177 }
1178 }
1179 if (first) {
1180 os << "No registers in GC map\n";
1181 } else {
1182 os << "\n";
1183 }
1184 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001185 void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
1186 const DexFile::CodeItem* code_item) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001187 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Ian Rogersef7d42f2014-01-06 12:55:46 -08001188 if (gc_map_raw == nullptr) {
1189 return; // No GC map.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001190 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001191 const void* quick_code = oat_method.GetQuickCode();
Elliott Hughes956af0f2014-12-11 14:34:28 -08001192 NativePcOffsetToReferenceMap map(gc_map_raw);
1193 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1194 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
1195 map.GetNativePcOffset(entry);
1196 os << StringPrintf("%p", native_pc);
1197 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001198 }
1199 }
1200
1201 void DumpMappingTable(std::ostream& os, const OatFile::OatMethod& oat_method) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001202 const void* quick_code = oat_method.GetQuickCode();
1203 if (quick_code == nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -08001204 return;
1205 }
Ian Rogers1809a722013-08-09 22:05:32 -07001206 MappingTable table(oat_method.GetMappingTable());
1207 if (table.TotalSize() != 0) {
1208 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1209 std::ostream indent_os(&indent_filter);
1210 if (table.PcToDexSize() != 0) {
1211 typedef MappingTable::PcToDexIterator It;
1212 os << "suspend point mappings {\n";
1213 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1214 indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
1215 }
1216 os << "}\n";
1217 }
1218 if (table.DexToPcSize() != 0) {
1219 typedef MappingTable::DexToPcIterator It;
1220 os << "catch entry mappings {\n";
1221 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1222 indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
1223 }
1224 os << "}\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001225 }
1226 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001227 }
1228
Ian Rogers1809a722013-08-09 22:05:32 -07001229 uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1230 size_t offset, bool suspend_point_mapping) {
1231 MappingTable table(oat_method.GetMappingTable());
1232 if (suspend_point_mapping && table.PcToDexSize() > 0) {
1233 typedef MappingTable::PcToDexIterator It;
1234 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1235 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001236 os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001237 return cur.DexPc();
1238 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001239 }
Ian Rogers1809a722013-08-09 22:05:32 -07001240 } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
1241 typedef MappingTable::DexToPcIterator It;
1242 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1243 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001244 os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001245 return cur.DexPc();
Ian Rogersb23a7722012-10-09 16:54:26 -07001246 }
1247 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001248 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001249 return DexFile::kDexNoIndex;
Ian Rogersb23a7722012-10-09 16:54:26 -07001250 }
1251
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001252 void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1253 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001254 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001255 if (gc_map_raw != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001256 NativePcOffsetToReferenceMap map(gc_map_raw);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001257 if (map.HasEntry(native_pc_offset)) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001258 size_t num_regs = map.RegWidth() * 8;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001259 const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
Ian Rogersb23a7722012-10-09 16:54:26 -07001260 bool first = true;
1261 for (size_t reg = 0; reg < num_regs; reg++) {
1262 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1263 if (first) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001264 os << "GC map objects: v" << reg << " (";
1265 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001266 os << ")";
1267 first = false;
1268 } else {
1269 os << ", v" << reg << " (";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001270 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001271 os << ")";
1272 }
1273 }
1274 }
1275 if (!first) {
1276 os << "\n";
1277 }
1278 }
1279 }
1280 }
1281
Mathieu Chartier590fee92013-09-13 13:46:47 -07001282 void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
1283 const OatFile::OatMethod& oat_method,
1284 const DexFile::CodeItem* code_item, uint32_t dex_pc) {
1285 DCHECK(verifier != nullptr);
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001286 std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001287 bool first = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001288 for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
1289 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
1290 if (kind != kUndefined) {
1291 if (first) {
1292 os << "VRegs: v";
1293 first = false;
1294 } else {
1295 os << ", v";
1296 }
1297 os << reg << " (";
1298 switch (kind) {
1299 case kImpreciseConstant:
1300 os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
1301 DescribeVReg(os, oat_method, code_item, reg, kind);
1302 break;
1303 case kConstant:
1304 os << "Constant: " << kinds.at((reg * 2) + 1);
1305 break;
1306 default:
1307 DescribeVReg(os, oat_method, code_item, reg, kind);
1308 break;
1309 }
1310 os << ")";
1311 }
1312 }
1313 if (!first) {
1314 os << "\n";
1315 }
1316 }
1317
1318
Ian Rogersb23a7722012-10-09 16:54:26 -07001319 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001320 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001321 size_t i = 0;
1322 while (i < code_item->insns_size_in_code_units_) {
1323 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001324 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1325 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001326 i += instruction->SizeInCodeUnits();
1327 }
1328 }
1329 }
1330
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001331 verifier::MethodVerifier* DumpVerifier(std::ostream& os, uint32_t dex_method_idx,
1332 const DexFile* dex_file,
1333 const DexFile::ClassDef& class_def,
1334 const DexFile::CodeItem* code_item,
1335 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001336 if ((method_access_flags & kAccNative) == 0) {
1337 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001338 StackHandleScope<1> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001339 Handle<mirror::DexCache> dex_cache(
1340 hs.NewHandle(Runtime::Current()->GetClassLinker()->FindDexCache(*dex_file)));
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001341 DCHECK(options_.class_loader_ != nullptr);
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001342 return verifier::MethodVerifier::VerifyMethodAndDump(soa.Self(), os, dex_method_idx, dex_file,
1343 dex_cache,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001344 *options_.class_loader_,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001345 &class_def, code_item,
1346 NullHandle<mirror::ArtMethod>(),
1347 method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001348 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001349
1350 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001351 }
1352
Mathieu Chartier590fee92013-09-13 13:46:47 -07001353 void DumpCode(std::ostream& os, verifier::MethodVerifier* verifier,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001354 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1355 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001356 const void* quick_code = oat_method.GetQuickCode();
1357
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001358 if (code_size == 0) {
1359 code_size = oat_method.GetQuickCodeSize();
1360 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001361 if (code_size == 0 || quick_code == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001362 os << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001363 return;
Elliott Hughes956af0f2014-12-11 14:34:28 -08001364 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001365 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1366 size_t offset = 0;
1367 while (offset < code_size) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001368 if (!bad_input) {
1369 DumpMappingAtOffset(os, oat_method, offset, false);
1370 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001371 offset += disassembler_->Dump(os, quick_native_pc + offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001372 if (!bad_input) {
1373 uint32_t dex_pc = DumpMappingAtOffset(os, oat_method, offset, true);
1374 if (dex_pc != DexFile::kDexNoIndex) {
1375 DumpGcMapAtNativePcOffset(os, oat_method, code_item, offset);
1376 if (verifier != nullptr) {
1377 DumpVRegsAtDexPc(os, verifier, oat_method, code_item, dex_pc);
1378 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001379 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001380 }
1381 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001382 }
1383 }
1384
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001385 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001386 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001387 const OatDumperOptions& options_;
1388 uint32_t resolved_addr2instr_;
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001389 InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001390 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001391 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001392};
1393
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001394class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001395 public:
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001396 explicit ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001397 const ImageHeader& image_header, OatDumperOptions* oat_dumper_options)
1398 : os_(os),
1399 image_space_(image_space),
1400 image_header_(image_header),
1401 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001402
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001403 bool Dump() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001404 std::ostream& os = *os_;
1405 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001406
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001407 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001408
Mathieu Chartier31e89252013-08-28 11:29:12 -07001409 os << "IMAGE BITMAP OFFSET: " << reinterpret_cast<void*>(image_header_.GetImageBitmapOffset())
1410 << " SIZE: " << reinterpret_cast<void*>(image_header_.GetImageBitmapSize()) << "\n\n";
1411
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001412 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001413
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001414 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001415
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001416 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1417
1418 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1419
1420 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001421
Alex Lighta59dd802014-07-02 16:28:08 -07001422 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1423
Igor Murashkin46774762014-10-22 11:37:02 -07001424 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1425
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001426 {
1427 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
1428 Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1429 std::ostream indent1_os(&indent1_filter);
1430 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
1431 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1432 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1433 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001434 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001435 indent1_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
1436 if (image_root_object->IsObjectArray()) {
1437 Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
1438 std::ostream indent2_os(&indent2_filter);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001439 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001440 = image_root_object->AsObjectArray<mirror::Object>();
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001441 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1442 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001443 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001444 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1445 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001446 run++;
1447 } else {
1448 break;
1449 }
1450 }
1451 if (run == 0) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001452 indent2_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001453 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001454 indent2_os << StringPrintf("%d to %zd: ", j, j + run);
1455 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001456 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001457 if (value != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001458 PrettyObjectValue(indent2_os, value->GetClass(), value);
1459 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001460 indent2_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001461 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001462 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001463 }
1464 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001465 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001466 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001467
Brian Carlstromaded5f72011-10-07 17:15:04 -07001468 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001469 std::string image_filename = image_space_.GetImageFilename();
1470 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001471 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001472 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001473 std::string error_msg;
Alex Lighta59dd802014-07-02 16:28:08 -07001474 const OatFile* oat_file = class_linker->FindOpenedOatFileFromOatLocation(oat_location);
1475 if (oat_file == nullptr) {
Igor Murashkin46774762014-10-22 11:37:02 -07001476 oat_file = OatFile::Open(oat_location, oat_location, nullptr, nullptr, false, &error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -07001477 if (oat_file == nullptr) {
1478 os << "NOT FOUND: " << error_msg << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001479 return false;
Alex Lighta59dd802014-07-02 16:28:08 -07001480 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001481 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001482 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001483
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001484 stats_.oat_file_bytes = oat_file->Size();
1485
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001486 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001487
Mathieu Chartier02e25112013-08-14 16:14:24 -07001488 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001489 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001490 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1491 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001492 }
1493
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001494 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001495
1496 // Loop through all the image spaces and dump their objects.
Ian Rogers1d54e732013-05-02 21:10:01 -07001497 gc::Heap* heap = Runtime::Current()->GetHeap();
1498 const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces();
Ian Rogers50b35e22012-10-04 10:09:15 -07001499 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001500 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001501 {
1502 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1503 heap->FlushAllocStack();
1504 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001505 // Since FlushAllocStack() above resets the (active) allocation
1506 // stack. Need to revoke the thread-local allocation stacks that
1507 // point into it.
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001508 {
1509 self->TransitionFromRunnableToSuspended(kNative);
1510 ThreadList* thread_list = Runtime::Current()->GetThreadList();
1511 thread_list->SuspendAll();
1512 heap->RevokeAllThreadLocalAllocationStacks(self);
1513 thread_list->ResumeAll();
1514 self->TransitionFromSuspendedToRunnable();
1515 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001516 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001517 {
1518 std::ostream* saved_os = os_;
1519 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1520 std::ostream indent_os(&indent_filter);
1521 os_ = &indent_os;
1522 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001523 for (const auto& space : spaces) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001524 if (space->IsImageSpace()) {
Ian Rogers1d54e732013-05-02 21:10:01 -07001525 gc::space::ImageSpace* image_space = space->AsImageSpace();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001526 image_space->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1527 indent_os << "\n";
1528 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -07001529 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001530 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001531 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001532 indent_os << "\n";
1533 os_ = saved_os;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001534 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001535 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001536 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001537 if (file.get() == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001538 LOG(WARNING) << "Failed to find image in " << image_filename;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001539 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001540 if (file.get() != nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001541 stats_.file_bytes = file->GetLength();
Brian Carlstrom6f277752013-09-30 17:56:45 -07001542 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001543 size_t header_bytes = sizeof(ImageHeader);
1544 stats_.header_bytes = header_bytes;
1545 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
1546 stats_.alignment_bytes += alignment_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07001547 stats_.alignment_bytes += image_header_.GetImageBitmapOffset() - image_header_.GetImageSize();
1548 stats_.bitmap_bytes += image_header_.GetImageBitmapSize();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001549 stats_.Dump(os);
1550 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001551
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001552 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001553
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001554 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001555 }
1556
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001557 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001558 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001559 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001560 CHECK(type != nullptr);
1561 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001562 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001563 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001564 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001565 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001566 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001567 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001568 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001569 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Brian Carlstromea46f952013-07-30 01:26:50 -07001570 } else if (type->IsArtFieldClass()) {
1571 mirror::ArtField* field = value->AsArtField();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001572 os << StringPrintf("%p Field: %s\n", field, PrettyField(field).c_str());
Brian Carlstromea46f952013-07-30 01:26:50 -07001573 } else if (type->IsArtMethodClass()) {
1574 mirror::ArtMethod* method = value->AsArtMethod();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001575 os << StringPrintf("%p Method: %s\n", method, PrettyMethod(method).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001576 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001577 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001578 }
1579 }
1580
Brian Carlstromea46f952013-07-30 01:26:50 -07001581 static void PrintField(std::ostream& os, mirror::ArtField* field, mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001582 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001583 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08001584 switch (field->GetTypeAsPrimitiveType()) {
1585 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08001586 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001587 break;
1588 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001589 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001590 break;
1591 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001592 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001593 break;
1594 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001595 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001596 break;
1597 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07001598 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001599 break;
1600 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07001601 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001602 break;
1603 case Primitive::kPrimBoolean:
Fred Shih37f05ef2014-07-16 18:38:08 -07001604 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1605 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001606 break;
1607 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07001608 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001609 break;
1610 case Primitive::kPrimNot: {
1611 // Get the value, don't compute the type unless it is non-null as we don't want
1612 // to cause class loading.
1613 mirror::Object* value = field->GetObj(obj);
1614 if (value == nullptr) {
1615 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07001616 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08001617 // Grab the field type without causing resolution.
1618 mirror::Class* field_type = field->GetType(false);
1619 if (field_type != nullptr) {
1620 PrettyObjectValue(os, field_type, value);
1621 } else {
1622 os << StringPrintf("%p %s\n", value,
1623 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
1624 }
Ian Rogers50239c72013-06-17 14:53:22 -07001625 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001626 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07001627 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001628 default:
1629 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
1630 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08001631 }
1632 }
1633
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001634 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001635 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001636 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001637 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001638 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001639 }
Brian Carlstromea46f952013-07-30 01:26:50 -07001640 mirror::ObjectArray<mirror::ArtField>* fields = klass->GetIFields();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001641 if (fields != nullptr) {
Ian Rogersd5b32602012-02-26 16:40:04 -08001642 for (int32_t i = 0; i < fields->GetLength(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001643 mirror::ArtField* field = fields->Get(i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001644 PrintField(os, field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001645 }
1646 }
1647 }
1648
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001649 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001650 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001651 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001652
Ian Rogersef7d42f2014-01-06 12:55:46 -08001653 const void* GetQuickOatCodeBegin(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001654 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001655 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
1656 InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet()));
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001657 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001658 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001659 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001660 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001661 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001662 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001663 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001664 }
1665
Ian Rogersef7d42f2014-01-06 12:55:46 -08001666 uint32_t GetQuickOatCodeSize(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001667 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001668 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1669 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001670 return 0;
1671 }
1672 return oat_code_begin[-1];
1673 }
1674
Ian Rogersef7d42f2014-01-06 12:55:46 -08001675 const void* GetQuickOatCodeEnd(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001676 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001677 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001678 if (oat_code_begin == nullptr) {
1679 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001680 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001681 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001682 }
1683
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001684 static void Callback(mirror::Object* obj, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001685 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001686 DCHECK(obj != nullptr);
1687 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001688 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001689 if (!state->InDumpSpace(obj)) {
1690 return;
1691 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001692
1693 size_t object_bytes = obj->SizeOf();
1694 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1695 state->stats_.object_bytes += object_bytes;
1696 state->stats_.alignment_bytes += alignment_bytes;
1697
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001698 std::ostream& os = *state->os_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001699 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001700 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001701 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
1702 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001703 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001704 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001705 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
1706 << klass->GetStatus() << ")\n";
Brian Carlstromea46f952013-07-30 01:26:50 -07001707 } else if (obj->IsArtField()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001708 os << StringPrintf("%p: java.lang.reflect.ArtField %s\n", obj,
Brian Carlstromea46f952013-07-30 01:26:50 -07001709 PrettyField(obj->AsArtField()).c_str());
1710 } else if (obj->IsArtMethod()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001711 os << StringPrintf("%p: java.lang.reflect.ArtMethod %s\n", obj,
Brian Carlstromea46f952013-07-30 01:26:50 -07001712 PrettyMethod(obj->AsArtMethod()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001713 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001714 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001715 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001716 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001717 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001718 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001719 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1720 std::ostream indent_os(&indent_filter);
1721 DumpFields(indent_os, obj, obj_class);
Ian Rogersd5b32602012-02-26 16:40:04 -08001722 if (obj->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001723 mirror::ObjectArray<mirror::Object>* obj_array = obj->AsObjectArray<mirror::Object>();
Ian Rogersd5b32602012-02-26 16:40:04 -08001724 int32_t length = obj_array->GetLength();
1725 for (int32_t i = 0; i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001726 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001727 size_t run = 0;
1728 for (int32_t j = i + 1; j < length; j++) {
1729 if (value == obj_array->Get(j)) {
1730 run++;
1731 } else {
1732 break;
1733 }
1734 }
1735 if (run == 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001736 indent_os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001737 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001738 indent_os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001739 i = i + run;
1740 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001741 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001742 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001743 PrettyObjectValue(indent_os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001744 }
1745 } else if (obj->IsClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001746 mirror::ObjectArray<mirror::ArtField>* sfields = obj->AsClass()->GetSFields();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001747 if (sfields != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001748 indent_os << "STATICS:\n";
1749 Indenter indent2_filter(indent_os.rdbuf(), kIndentChar, kIndentBy1Count);
1750 std::ostream indent2_os(&indent2_filter);
Ian Rogersd5b32602012-02-26 16:40:04 -08001751 for (int32_t i = 0; i < sfields->GetLength(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001752 mirror::ArtField* field = sfields->Get(i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001753 PrintField(indent2_os, field, field->GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001754 }
1755 }
Brian Carlstromea46f952013-07-30 01:26:50 -07001756 } else if (obj->IsArtMethod()) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001757 const size_t image_pointer_size = InstructionSetPointerSize(
1758 state->oat_dumper_->GetOatInstructionSet());
Brian Carlstromea46f952013-07-30 01:26:50 -07001759 mirror::ArtMethod* method = obj->AsArtMethod();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001760 if (method->IsNative()) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001761 DCHECK(method->GetNativeGcMap(image_pointer_size) == nullptr) << PrettyMethod(method);
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001762 DCHECK(method->GetMappingTable(image_pointer_size) == nullptr) << PrettyMethod(method);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001763 bool first_occurrence;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001764 const void* quick_oat_code = state->GetQuickOatCodeBegin(method);
1765 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1766 state->ComputeOatSize(quick_oat_code, &first_occurrence);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001767 if (first_occurrence) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001768 state->stats_.native_to_managed_code_bytes += quick_oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001769 }
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001770 if (quick_oat_code != method->GetEntryPointFromQuickCompiledCodePtrSize(
1771 image_pointer_size)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001772 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001773 }
Ian Rogers19846512012-02-24 11:42:47 -08001774 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
Jeff Hao88474b42013-10-23 16:24:40 -07001775 method->IsResolutionMethod() || method->IsImtConflictMethod() ||
Mathieu Chartier2d2621a2014-10-23 16:48:06 -07001776 method->IsImtUnimplementedMethod() || method->IsClassInitializer()) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001777 DCHECK(method->GetNativeGcMap(image_pointer_size) == nullptr) << PrettyMethod(method);
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001778 DCHECK(method->GetMappingTable(image_pointer_size) == nullptr) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001779 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001780 const DexFile::CodeItem* code_item = method->GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -07001781 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001782 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001783
Elliott Hughesa0e18062012-04-13 15:59:59 -07001784 bool first_occurrence;
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001785 size_t gc_map_bytes =
1786 state->ComputeOatSize(method->GetNativeGcMap(image_pointer_size), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001787 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001788 state->stats_.gc_map_bytes += gc_map_bytes;
1789 }
1790
1791 size_t pc_mapping_table_bytes =
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001792 state->ComputeOatSize(method->GetMappingTable(image_pointer_size), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001793 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001794 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
1795 }
1796
1797 size_t vmap_table_bytes =
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001798 state->ComputeOatSize(method->GetVmapTable(image_pointer_size), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001799 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001800 state->stats_.vmap_table_bytes += vmap_table_bytes;
1801 }
1802
Ian Rogersef7d42f2014-01-06 12:55:46 -08001803 const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method);
1804 const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method);
1805 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1806 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001807 if (first_occurrence) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001808 state->stats_.managed_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001809 if (method->IsConstructor()) {
1810 if (method->IsStatic()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001811 state->stats_.class_initializer_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001812 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001813 state->stats_.large_initializer_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001814 }
1815 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001816 state->stats_.large_method_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001817 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001818 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001819 state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001820
Ian Rogersef7d42f2014-01-06 12:55:46 -08001821 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001822 indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
1823 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001824
1825 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
Ian Rogersef7d42f2014-01-06 12:55:46 -08001826 vmap_table_bytes + quick_oat_code_size + object_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001827
1828 double expansion =
Ian Rogersef7d42f2014-01-06 12:55:46 -08001829 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001830 state->stats_.ComputeOutliers(total_size, expansion, method);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001831 }
1832 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07001833 std::string temp;
1834 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001835 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001836
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001837 std::set<const void*> already_seen_;
1838 // Compute the size of the given data within the oat file and whether this is the first time
1839 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07001840 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001841 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001842 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001843 already_seen_.insert(oat_data);
1844 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001845 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001846 }
1847 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001848 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001849
1850 public:
1851 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001852 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001853 size_t file_bytes;
1854
1855 size_t header_bytes;
1856 size_t object_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07001857 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001858 size_t alignment_bytes;
1859
1860 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001861 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001862 size_t managed_to_native_code_bytes;
1863 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001864 size_t class_initializer_code_bytes;
1865 size_t large_initializer_code_bytes;
1866 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001867
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001868 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001869 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001870 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001871
1872 size_t dex_instruction_bytes;
1873
Brian Carlstromea46f952013-07-30 01:26:50 -07001874 std::vector<mirror::ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001875 std::vector<size_t> method_outlier_size;
1876 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07001877 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001878
1879 explicit Stats()
1880 : oat_file_bytes(0),
1881 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001882 header_bytes(0),
1883 object_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07001884 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001885 alignment_bytes(0),
1886 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001887 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001888 managed_to_native_code_bytes(0),
1889 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07001890 class_initializer_code_bytes(0),
1891 large_initializer_code_bytes(0),
1892 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001893 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001894 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001895 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001896 dex_instruction_bytes(0) {}
1897
Elliott Hughesa0e18062012-04-13 15:59:59 -07001898 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001899 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07001900 size_t bytes;
1901 size_t count;
1902 };
1903 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
1904 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001905
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001906 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001907 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
1908 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001909 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07001910 it->second.count += 1;
1911 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001912 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07001913 }
1914 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001915
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001916 double PercentOfOatBytes(size_t size) {
1917 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
1918 }
1919
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001920 double PercentOfFileBytes(size_t size) {
1921 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
1922 }
1923
1924 double PercentOfObjectBytes(size_t size) {
1925 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
1926 }
1927
Brian Carlstromea46f952013-07-30 01:26:50 -07001928 void ComputeOutliers(size_t total_size, double expansion, mirror::ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001929 method_outlier_size.push_back(total_size);
1930 method_outlier_expansion.push_back(expansion);
1931 method_outlier.push_back(method);
1932 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001933
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001934 void DumpOutliers(std::ostream& os)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001935 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001936 size_t sum_of_sizes = 0;
1937 size_t sum_of_sizes_squared = 0;
1938 size_t sum_of_expansion = 0;
1939 size_t sum_of_expansion_squared = 0;
1940 size_t n = method_outlier_size.size();
1941 for (size_t i = 0; i < n; i++) {
1942 size_t cur_size = method_outlier_size[i];
1943 sum_of_sizes += cur_size;
1944 sum_of_sizes_squared += cur_size * cur_size;
1945 double cur_expansion = method_outlier_expansion[i];
1946 sum_of_expansion += cur_expansion;
1947 sum_of_expansion_squared += cur_expansion * cur_expansion;
1948 }
1949 size_t size_mean = sum_of_sizes / n;
1950 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
1951 double expansion_mean = sum_of_expansion / n;
1952 double expansion_variance =
1953 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
1954
1955 // Dump methods whose size is a certain number of standard deviations from the mean
1956 size_t dumped_values = 0;
1957 size_t skipped_values = 0;
1958 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
1959 size_t cur_size_variance = i * i * size_variance;
1960 bool first = true;
1961 for (size_t j = 0; j < n; j++) {
1962 size_t cur_size = method_outlier_size[j];
1963 if (cur_size > size_mean) {
1964 size_t cur_var = cur_size - size_mean;
1965 cur_var = cur_var * cur_var;
1966 if (cur_var > cur_size_variance) {
1967 if (dumped_values > 20) {
1968 if (i == 1) {
1969 skipped_values++;
1970 } else {
1971 i = 2; // jump to counting for 1 standard deviation
1972 break;
1973 }
1974 } else {
1975 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001976 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001977 first = false;
1978 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001979 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07001980 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001981 method_outlier_size[j] = 0; // don't consider this method again
1982 dumped_values++;
1983 }
1984 }
1985 }
1986 }
1987 }
1988 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001989 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001990 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001991 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001992 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001993
1994 // Dump methods whose expansion is a certain number of standard deviations from the mean
1995 dumped_values = 0;
1996 skipped_values = 0;
1997 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
1998 double cur_expansion_variance = i * i * expansion_variance;
1999 bool first = true;
2000 for (size_t j = 0; j < n; j++) {
2001 double cur_expansion = method_outlier_expansion[j];
2002 if (cur_expansion > expansion_mean) {
2003 size_t cur_var = cur_expansion - expansion_mean;
2004 cur_var = cur_var * cur_var;
2005 if (cur_var > cur_expansion_variance) {
2006 if (dumped_values > 20) {
2007 if (i == 1) {
2008 skipped_values++;
2009 } else {
2010 i = 2; // jump to counting for 1 standard deviation
2011 break;
2012 }
2013 } else {
2014 if (first) {
2015 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002016 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002017 first = false;
2018 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002019 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002020 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002021 method_outlier_expansion[j] = 0.0; // don't consider this method again
2022 dumped_values++;
2023 }
2024 }
2025 }
2026 }
2027 }
2028 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002029 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002030 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002031 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002032 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002033 }
2034
Ian Rogersb726dcb2012-09-05 08:57:23 -07002035 void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002036 {
2037 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2038 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
2039 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
2040 std::ostream indent_os(&indent_filter);
2041 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2042 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier32327092013-08-30 14:04:08 -07002043 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002044 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
2045 header_bytes, PercentOfFileBytes(header_bytes),
2046 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002047 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002048 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2049 << std::flush;
Mathieu Chartier32327092013-08-30 14:04:08 -07002050 CHECK_EQ(file_bytes, bitmap_bytes + header_bytes + object_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002051 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002052
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002053 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002054 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002055 for (const auto& sizes_and_count : sizes_and_counts) {
2056 const std::string& descriptor(sizes_and_count.first);
2057 double average = static_cast<double>(sizes_and_count.second.bytes) /
2058 static_cast<double>(sizes_and_count.second.count);
2059 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002060 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002061 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002062 descriptor.c_str(), sizes_and_count.second.bytes,
2063 sizes_and_count.second.count, average, percent);
2064 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002065 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002066 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002067 CHECK_EQ(object_bytes, object_bytes_total);
2068
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002069 os << StringPrintf("oat_file_bytes = %8zd\n"
2070 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2071 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2072 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2073 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2074 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2075 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002076 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002077 managed_code_bytes,
2078 PercentOfOatBytes(managed_code_bytes),
2079 managed_to_native_code_bytes,
2080 PercentOfOatBytes(managed_to_native_code_bytes),
2081 native_to_managed_code_bytes,
2082 PercentOfOatBytes(native_to_managed_code_bytes),
2083 class_initializer_code_bytes,
2084 PercentOfOatBytes(class_initializer_code_bytes),
2085 large_initializer_code_bytes,
2086 PercentOfOatBytes(large_initializer_code_bytes),
2087 large_method_code_bytes,
2088 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002089 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002090 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002091 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002092 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2093 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002094 }
2095
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002096 os << "\n" << StringPrintf("gc_map_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2097 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2098 "vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002099 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
2100 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
2101 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002102 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002103
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002104 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2105 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002106 static_cast<double>(managed_code_bytes) /
2107 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002108 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002109 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002110 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002111
2112 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002113 }
2114 } stats_;
2115
2116 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002117 enum {
2118 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2119 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2120 kLargeConstructorDexBytes = 4000,
2121 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2122 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2123 kLargeMethodDexBytes = 16000
2124 };
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002125 std::ostream* os_;
Ian Rogers1d54e732013-05-02 21:10:01 -07002126 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002127 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002128 std::unique_ptr<OatDumper> oat_dumper_;
2129 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002130
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002131 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002132};
2133
Andreas Gampe00b25f32014-09-17 21:49:05 -07002134static int DumpImage(Runtime* runtime, const char* image_location, OatDumperOptions* options,
2135 std::ostream* os) {
2136 // Dumping the image, no explicit class loader.
2137 NullHandle<mirror::ClassLoader> null_class_loader;
2138 options->class_loader_ = &null_class_loader;
2139
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002140 ScopedObjectAccess soa(Thread::Current());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002141 gc::Heap* heap = runtime->GetHeap();
Ian Rogers1d54e732013-05-02 21:10:01 -07002142 gc::space::ImageSpace* image_space = heap->GetImageSpace();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002143 CHECK(image_space != nullptr);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002144 const ImageHeader& image_header = image_space->GetImageHeader();
2145 if (!image_header.IsValid()) {
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07002146 fprintf(stderr, "Invalid image header %s\n", image_location);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002147 return EXIT_FAILURE;
2148 }
Igor Murashkin37743352014-11-13 14:38:00 -08002149
Andreas Gampe00b25f32014-09-17 21:49:05 -07002150 ImageDumper image_dumper(os, *image_space, image_header, options);
Igor Murashkin37743352014-11-13 14:38:00 -08002151
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002152 bool success = image_dumper.Dump();
2153 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002154}
2155
Andreas Gampe00b25f32014-09-17 21:49:05 -07002156static int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
2157 std::ostream* os) {
2158 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2159
2160 Thread* self = Thread::Current();
2161 CHECK(self != nullptr);
2162 // Need well-known-classes.
2163 WellKnownClasses::Init(self->GetJniEnv());
2164
2165 // Need to register dex files to get a working dex cache.
2166 ScopedObjectAccess soa(self);
2167 ClassLinker* class_linker = runtime->GetClassLinker();
2168 class_linker->RegisterOatFile(oat_file);
Richard Uhlerfbef44d2014-12-23 09:48:51 -08002169 std::vector<std::unique_ptr<const DexFile>> dex_files;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002170 for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
2171 std::string error_msg;
Richard Uhlerfbef44d2014-12-23 09:48:51 -08002172 std::unique_ptr<const DexFile> dex_file = odf->OpenDexFile(&error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002173 CHECK(dex_file != nullptr) << error_msg;
2174 class_linker->RegisterDexFile(*dex_file);
Richard Uhlerfbef44d2014-12-23 09:48:51 -08002175 dex_files.push_back(std::move(dex_file));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002176 }
2177
2178 // Need a class loader.
2179 soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader);
2180 ScopedLocalRef<jobject> class_loader_local(soa.Env(),
2181 soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader));
2182 jobject class_loader = soa.Env()->NewGlobalRef(class_loader_local.get());
2183 // Fake that we're a compiler.
Richard Uhlerfbef44d2014-12-23 09:48:51 -08002184 std::vector<const DexFile*> class_path;
2185 for (auto& dex_file : dex_files) {
2186 class_path.push_back(dex_file.get());
2187 }
2188 runtime->SetCompileTimeClassPath(class_loader, class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002189
2190 // Use the class loader while dumping.
2191 StackHandleScope<1> scope(self);
2192 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
2193 soa.Decode<mirror::ClassLoader*>(class_loader));
2194 options->class_loader_ = &loader_handle;
2195
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002196 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002197 bool success = oat_dumper.Dump(*os);
2198 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2199}
2200
2201static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002202 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002203 // No image = no class loader.
2204 NullHandle<mirror::ClassLoader> null_class_loader;
2205 options->class_loader_ = &null_class_loader;
2206
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002207 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002208 bool success = oat_dumper.Dump(*os);
2209 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2210}
2211
2212static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2213 std::ostream* os) {
2214 std::string error_msg;
Igor Murashkin37743352014-11-13 14:38:00 -08002215 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
2216 &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002217 if (oat_file == nullptr) {
2218 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2219 return EXIT_FAILURE;
2220 }
2221
2222 if (runtime != nullptr) {
2223 return DumpOatWithRuntime(runtime, oat_file, options, os);
2224 } else {
2225 return DumpOatWithoutRuntime(oat_file, options, os);
2226 }
2227}
2228
2229static int SymbolizeOat(const char* oat_filename, std::string& output_name) {
2230 std::string error_msg;
Igor Murashkin37743352014-11-13 14:38:00 -08002231 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
2232 &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002233 if (oat_file == nullptr) {
2234 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2235 return EXIT_FAILURE;
2236 }
2237
2238 OatSymbolizer oat_symbolizer(oat_file, output_name);
2239 if (!oat_symbolizer.Init()) {
2240 fprintf(stderr, "Failed to initialize symbolizer\n");
2241 return EXIT_FAILURE;
2242 }
2243 if (!oat_symbolizer.Symbolize()) {
2244 fprintf(stderr, "Failed to symbolize\n");
2245 return EXIT_FAILURE;
2246 }
2247
2248 return EXIT_SUCCESS;
2249}
2250
Igor Murashkin37743352014-11-13 14:38:00 -08002251struct OatdumpArgs : public CmdlineArgs {
2252 protected:
2253 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002254
Igor Murashkin37743352014-11-13 14:38:00 -08002255 virtual ParseStatus ParseCustom(const StringPiece& option,
2256 std::string* error_msg) OVERRIDE {
2257 {
2258 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
2259 if (base_parse != kParseUnknownArgument) {
2260 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002261 }
2262 }
2263
Igor Murashkin37743352014-11-13 14:38:00 -08002264 if (option.starts_with("--oat-file=")) {
2265 oat_filename_ = option.substr(strlen("--oat-file=")).data();
2266 } else if (option.starts_with("--image=")) {
2267 image_location_ = option.substr(strlen("--image=")).data();
2268 } else if (option =="--dump:raw_mapping_table") {
2269 dump_raw_mapping_table_ = true;
2270 } else if (option == "--dump:raw_gc_map") {
2271 dump_raw_gc_map_ = true;
2272 } else if (option == "--no-dump:vmap") {
2273 dump_vmap_ = false;
2274 } else if (option == "--no-disassemble") {
2275 disassemble_code_ = false;
2276 } else if (option.starts_with("--symbolize=")) {
2277 oat_filename_ = option.substr(strlen("--symbolize=")).data();
2278 symbolize_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002279 } else if (option.starts_with("--class-filter=")) {
2280 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002281 } else if (option.starts_with("--method-filter=")) {
2282 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002283 } else if (option.starts_with("--list-classes")) {
2284 list_classes_ = true;
2285 } else if (option.starts_with("--list-methods")) {
2286 list_methods_ = true;
2287 } else if (option.starts_with("--export-dex-to=")) {
2288 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
2289 } else if (option.starts_with("--addr2instr=")) {
2290 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
2291 *error_msg = "Address conversion failed";
2292 return kParseError;
2293 }
Igor Murashkin37743352014-11-13 14:38:00 -08002294 } else {
2295 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002296 }
2297
Igor Murashkin37743352014-11-13 14:38:00 -08002298 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002299 }
2300
Igor Murashkin37743352014-11-13 14:38:00 -08002301 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
2302 // Infer boot image location from the image location if possible.
2303 if (boot_image_location_ == nullptr) {
2304 boot_image_location_ = image_location_;
2305 }
2306
2307 // Perform the parent checks.
2308 ParseStatus parent_checks = Base::ParseChecks(error_msg);
2309 if (parent_checks != kParseOk) {
2310 return parent_checks;
2311 }
2312
2313 // Perform our own checks.
2314 if (image_location_ == nullptr && oat_filename_ == nullptr) {
2315 *error_msg = "Either --image or --oat-file must be specified";
2316 return kParseError;
2317 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
2318 *error_msg = "Either --image or --oat-file must be specified but not both";
2319 return kParseError;
2320 }
2321
2322 return kParseOk;
2323 }
2324
2325 virtual std::string GetUsage() const {
2326 std::string usage;
2327
2328 usage +=
2329 "Usage: oatdump [options] ...\n"
2330 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
2331 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
2332 "\n"
2333 // Either oat-file or image is required.
2334 " --oat-file=<file.oat>: specifies an input oat filename.\n"
2335 " Example: --oat-file=/system/framework/boot.oat\n"
2336 "\n"
2337 " --image=<file.art>: specifies an input image location.\n"
2338 " Example: --image=/system/framework/boot.art\n"
2339 "\n";
2340
2341 usage += Base::GetUsage();
2342
2343 usage += // Optional.
2344 " --dump:raw_mapping_table enables dumping of the mapping table.\n"
2345 " Example: --dump:raw_mapping_table\n"
2346 "\n"
2347 " --dump:raw_mapping_table enables dumping of the GC map.\n"
2348 " Example: --dump:raw_gc_map\n"
2349 "\n"
2350 " --no-dump:vmap may be used to disable vmap dumping.\n"
2351 " Example: --no-dump:vmap\n"
2352 "\n"
2353 " --no-disassemble may be used to disable disassembly.\n"
2354 " Example: --no-disassemble\n"
2355 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002356 " --list-classes may be used to list target file classes (can be used with filters).\n"
2357 " Example: --list-classes\n"
2358 " Example: --list-classes --class-filter=com.example.foo\n"
2359 "\n"
2360 " --list-methods may be used to list target file methods (can be used with filters).\n"
2361 " Example: --list-methods\n"
2362 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
2363 "\n"
2364 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
2365 " Example: --symbolize=/system/framework/boot.oat\n"
2366 "\n"
2367 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
2368 " Example: --class-filter=com.example.foo\n"
2369 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002370 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
2371 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002372 "\n"
2373 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
2374 " Example: --export-dex-to=/data/local/tmp\n"
2375 "\n"
2376 " --addr2instr=<address>: output matching method disassembled code from relative\n"
2377 " address (e.g. PC from crash dump)\n"
2378 " Example: --addr2instr=0x00001a3b\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002379 "\n";
2380
2381 return usage;
2382 }
2383
2384 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07002385 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002386 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00002387 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07002388 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002389 std::string elf_filename_prefix_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002390 bool dump_raw_mapping_table_ = false;
2391 bool dump_raw_gc_map_ = false;
2392 bool dump_vmap_ = true;
2393 bool disassemble_code_ = true;
2394 bool symbolize_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002395 bool list_classes_ = false;
2396 bool list_methods_ = false;
2397 uint32_t addr2instr_ = 0;
2398 const char* export_dex_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002399};
2400
Igor Murashkin37743352014-11-13 14:38:00 -08002401struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
2402 virtual bool NeedsRuntime() OVERRIDE {
2403 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002404
Igor Murashkin37743352014-11-13 14:38:00 -08002405 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
2406 bool absolute_addresses = (args_->oat_filename_ == nullptr);
2407
2408 oat_dumper_options_ = std::unique_ptr<OatDumperOptions>(new OatDumperOptions(
2409 args_->dump_raw_mapping_table_,
2410 args_->dump_raw_gc_map_,
2411 args_->dump_vmap_,
2412 args_->disassemble_code_,
2413 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002414 args_->class_filter_,
2415 args_->method_filter_,
2416 args_->list_classes_,
2417 args_->list_methods_,
2418 args_->export_dex_location_,
2419 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08002420
2421 return (args_->boot_image_location_ != nullptr || args_->image_location_ != nullptr) &&
2422 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002423 }
2424
Igor Murashkin37743352014-11-13 14:38:00 -08002425 virtual bool ExecuteWithoutRuntime() OVERRIDE {
2426 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08002427 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002428
Mathieu Chartierd424d082014-10-15 10:31:46 -07002429 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08002430
Andreas Gampec24f3992014-12-17 20:40:11 -08002431 if (args_->symbolize_) {
2432 return SymbolizeOat(args_->oat_filename_, args_->output_name_) == EXIT_SUCCESS;
2433 } else {
2434 return DumpOat(nullptr,
2435 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002436 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08002437 args_->os_) == EXIT_SUCCESS;
2438 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002439 }
2440
Igor Murashkin37743352014-11-13 14:38:00 -08002441 virtual bool ExecuteWithRuntime(Runtime* runtime) {
2442 CHECK(args_ != nullptr);
2443
2444 if (args_->oat_filename_ != nullptr) {
2445 return DumpOat(runtime,
2446 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002447 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08002448 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002449 }
Igor Murashkin37743352014-11-13 14:38:00 -08002450
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002451 return DumpImage(runtime, args_->image_location_, oat_dumper_options_.get(), args_->os_)
Igor Murashkin37743352014-11-13 14:38:00 -08002452 == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002453 }
2454
Igor Murashkin37743352014-11-13 14:38:00 -08002455 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
2456};
Andreas Gampe00b25f32014-09-17 21:49:05 -07002457
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002458} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07002459
2460int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08002461 art::OatdumpMain main;
2462 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002463}