blob: b9a3d3758458565bf4420f88a734afa20413fe20 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrom78128a62011-09-15 17:21:19 -070016
17#include <stdio.h>
18#include <stdlib.h>
19
Brian Carlstrom27ec9612011-09-19 20:20:38 -070020#include <fstream>
21#include <iostream>
Igor Murashkin37743352014-11-13 14:38:00 -080022#include <map>
23#include <set>
Brian Carlstrom78128a62011-09-15 17:21:19 -070024#include <string>
Andreas Gampe54fc26c2014-09-04 21:47:42 -070025#include <unordered_map>
Brian Carlstrom78128a62011-09-15 17:21:19 -070026#include <vector>
27
Ian Rogersd582fa42014-11-05 23:46:43 -080028#include "arch/instruction_set_features.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070029#include "art_field-inl.h"
Elliott Hughes76160052012-12-12 16:31:20 -080030#include "base/unix_file/fd_file.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070031#include "class_linker.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070033#include "dex_file-inl.h"
Elliott Hughese3c845c2012-02-28 17:23:01 -080034#include "dex_instruction.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080035#include "disassembler.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070036#include "elf_builder.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080037#include "gc_map.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070038#include "gc/space/image_space.h"
39#include "gc/space/large_object_space.h"
40#include "gc/space/space-inl.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070041#include "image.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080042#include "indenter.h"
Ian Rogers1809a722013-08-09 22:05:32 -070043#include "mapping_table.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070044#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
David Srbeckybc90fd02015-04-22 19:40:27 +010079class OatSymbolizer FINAL {
Andreas Gampe54fc26c2014-09-04 21:47:42 -070080 public:
David Srbeckybc90fd02015-04-22 19:40:27 +010081 class RodataWriter FINAL : public CodeOutput {
82 public:
83 explicit RodataWriter(const OatFile* oat_file) : oat_file_(oat_file) {}
Andreas Gampe54fc26c2014-09-04 21:47:42 -070084
David Srbeckybc90fd02015-04-22 19:40:27 +010085 bool Write(OutputStream* out) OVERRIDE {
86 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
87 return out->WriteFully(oat_file_->Begin(), rodata_size);
Andreas Gampe54fc26c2014-09-04 21:47:42 -070088 }
89
David Srbeckybc90fd02015-04-22 19:40:27 +010090 private:
91 const OatFile* oat_file_;
92 };
93
94 class TextWriter FINAL : public CodeOutput {
95 public:
96 explicit TextWriter(const OatFile* oat_file) : oat_file_(oat_file) {}
97
98 bool Write(OutputStream* out) OVERRIDE {
99 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
100 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
101 return out->WriteFully(text_begin, oat_file_->End() - text_begin);
102 }
103
104 private:
105 const OatFile* oat_file_;
106 };
107
108 explicit OatSymbolizer(const OatFile* oat_file, const std::string& output_name) :
109 oat_file_(oat_file), builder_(nullptr),
110 output_name_(output_name.empty() ? "symbolized.oat" : output_name) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700111 }
112
113 typedef void (OatSymbolizer::*Callback)(const DexFile::ClassDef&,
114 uint32_t,
115 const OatFile::OatMethod&,
116 const DexFile&,
117 uint32_t,
118 const DexFile::CodeItem*,
119 uint32_t);
120
121 bool Symbolize() {
David Srbeckybc90fd02015-04-22 19:40:27 +0100122 Elf32_Word rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
123 uint32_t size = static_cast<uint32_t>(oat_file_->End() - oat_file_->Begin());
124 uint32_t text_size = size - rodata_size;
125 uint32_t bss_size = oat_file_->BssSize();
126 RodataWriter rodata_writer(oat_file_);
127 TextWriter text_writer(oat_file_);
128 builder_.reset(new ElfBuilder<ElfTypes32>(
129 oat_file_->GetOatHeader().GetInstructionSet(),
130 rodata_size, &rodata_writer,
131 text_size, &text_writer,
132 bss_size));
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700133
134 Walk(&art::OatSymbolizer::RegisterForDedup);
135
136 NormalizeState();
137
138 Walk(&art::OatSymbolizer::AddSymbol);
139
David Srbeckybc90fd02015-04-22 19:40:27 +0100140 File* elf_output = OS::CreateEmptyFile(output_name_.c_str());
141 bool result = builder_->Write(elf_output);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700142
Andreas Gampe4303ba92014-11-06 01:00:46 -0800143 // Ignore I/O errors.
David Srbeckybc90fd02015-04-22 19:40:27 +0100144 UNUSED(elf_output->FlushClose());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700145
146 return result;
147 }
148
149 void Walk(Callback callback) {
150 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
151 for (size_t i = 0; i < oat_dex_files.size(); i++) {
152 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700153 CHECK(oat_dex_file != nullptr);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700154 WalkOatDexFile(oat_dex_file, callback);
155 }
156 }
157
158 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file, Callback callback) {
159 std::string error_msg;
160 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
161 if (dex_file.get() == nullptr) {
162 return;
163 }
164 for (size_t class_def_index = 0;
165 class_def_index < dex_file->NumClassDefs();
166 class_def_index++) {
167 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
168 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
169 OatClassType type = oat_class.GetType();
170 switch (type) {
171 case kOatClassAllCompiled:
172 case kOatClassSomeCompiled:
173 WalkOatClass(oat_class, *dex_file.get(), class_def, callback);
174 break;
175
176 case kOatClassNoneCompiled:
177 case kOatClassMax:
178 // Ignore.
179 break;
180 }
181 }
182 }
183
184 void WalkOatClass(const OatFile::OatClass& oat_class, const DexFile& dex_file,
185 const DexFile::ClassDef& class_def, Callback callback) {
Ian Rogers13735952014-10-08 12:43:28 -0700186 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700187 if (class_data == nullptr) { // empty class such as a marker interface?
188 return;
189 }
190 // Note: even if this is an interface or a native class, we still have to walk it, as there
191 // might be a static initializer.
192 ClassDataItemIterator it(dex_file, class_data);
193 SkipAllFields(&it);
194 uint32_t class_method_idx = 0;
195 while (it.HasNextDirectMethod()) {
196 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
197 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700198 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700199 class_method_idx++;
200 it.Next();
201 }
202 while (it.HasNextVirtualMethod()) {
203 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
204 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700205 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700206 class_method_idx++;
207 it.Next();
208 }
209 DCHECK(!it.HasNext());
210 }
211
212 void WalkOatMethod(const DexFile::ClassDef& class_def, uint32_t class_method_index,
213 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
214 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
215 uint32_t method_access_flags, Callback callback) {
216 if ((method_access_flags & kAccAbstract) != 0) {
217 // Abstract method, no code.
218 return;
219 }
220 if (oat_method.GetCodeOffset() == 0) {
221 // No code.
222 return;
223 }
224
225 (this->*callback)(class_def, class_method_index, oat_method, dex_file, dex_method_idx, code_item,
226 method_access_flags);
227 }
228
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700229 void RegisterForDedup(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
230 uint32_t class_method_index ATTRIBUTE_UNUSED,
231 const OatFile::OatMethod& oat_method,
232 const DexFile& dex_file ATTRIBUTE_UNUSED,
233 uint32_t dex_method_idx ATTRIBUTE_UNUSED,
234 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
235 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700236 state_[oat_method.GetCodeOffset()]++;
237 }
238
239 void NormalizeState() {
240 for (auto& x : state_) {
241 if (x.second == 1) {
242 state_[x.first] = 0;
243 }
244 }
245 }
246
247 enum class DedupState { // private
248 kNotDeduplicated,
249 kDeduplicatedFirst,
250 kDeduplicatedOther
251 };
252 DedupState IsDuplicated(uint32_t offset) {
253 if (state_[offset] == 0) {
254 return DedupState::kNotDeduplicated;
255 }
256 if (state_[offset] == 1) {
257 return DedupState::kDeduplicatedOther;
258 }
259 state_[offset] = 1;
260 return DedupState::kDeduplicatedFirst;
261 }
262
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700263 void AddSymbol(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
264 uint32_t class_method_index ATTRIBUTE_UNUSED,
265 const OatFile::OatMethod& oat_method,
266 const DexFile& dex_file,
267 uint32_t dex_method_idx,
268 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
269 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700270 DedupState dedup = IsDuplicated(oat_method.GetCodeOffset());
271 if (dedup != DedupState::kDeduplicatedOther) {
272 std::string pretty_name = PrettyMethod(dex_method_idx, dex_file, true);
273
274 if (dedup == DedupState::kDeduplicatedFirst) {
275 pretty_name = "[Dedup]" + pretty_name;
276 }
277
David Srbeckybc90fd02015-04-22 19:40:27 +0100278 auto* symtab = builder_->GetSymtab();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700279
David Srbeckybc90fd02015-04-22 19:40:27 +0100280 symtab->AddSymbol(pretty_name, builder_->GetText(),
Ian Rogers0279ebb2014-10-08 17:27:48 -0700281 oat_method.GetCodeOffset() - oat_file_->GetOatHeader().GetExecutableOffset(),
282 true, oat_method.GetQuickCodeSize(), STB_GLOBAL, STT_FUNC);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700283 }
284 }
285
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700286 private:
287 static void SkipAllFields(ClassDataItemIterator* it) {
288 while (it->HasNextStaticField()) {
289 it->Next();
290 }
291 while (it->HasNextInstanceField()) {
292 it->Next();
293 }
294 }
295
296 const OatFile* oat_file_;
David Srbecky533c2072015-04-22 12:20:22 +0100297 std::unique_ptr<ElfBuilder<ElfTypes32> > builder_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700298 std::unordered_map<uint32_t, uint32_t> state_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700299 const std::string output_name_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700300};
301
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700302class OatDumperOptions {
303 public:
304 OatDumperOptions(bool dump_raw_mapping_table,
305 bool dump_raw_gc_map,
306 bool dump_vmap,
307 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700308 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800309 const char* class_filter,
310 const char* method_filter,
311 bool list_classes,
312 bool list_methods,
313 const char* export_dex_location,
314 uint32_t addr2instr)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700315 : dump_raw_mapping_table_(dump_raw_mapping_table),
316 dump_raw_gc_map_(dump_raw_gc_map),
317 dump_vmap_(dump_vmap),
318 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700319 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800320 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000321 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800322 list_classes_(list_classes),
323 list_methods_(list_methods),
324 export_dex_location_(export_dex_location),
325 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800326 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700327
328 const bool dump_raw_mapping_table_;
329 const bool dump_raw_gc_map_;
330 const bool dump_vmap_;
331 const bool disassemble_code_;
332 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800333 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000334 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800335 const bool list_classes_;
336 const bool list_methods_;
337 const char* const export_dex_location_;
338 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700339 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700340};
341
Elliott Hughese3c845c2012-02-28 17:23:01 -0800342class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700343 public:
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800344 explicit OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000345 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800346 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700347 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800348 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800349 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
350 disassembler_(Disassembler::Create(instruction_set_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800351 new DisassemblerOptions(options_.absolute_addresses_,
Alexandre Ramesa37d9252014-10-27 11:28:14 +0000352 oat_file.Begin(),
353 true /* can_read_litals_ */))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800354 CHECK(options_.class_loader_ != nullptr);
355 CHECK(options_.class_filter_ != nullptr);
356 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800357 AddAllOffsets();
358 }
359
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700360 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700361 delete disassembler_;
362 }
363
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800364 InstructionSet GetInstructionSet() {
365 return instruction_set_;
366 }
367
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700368 bool Dump(std::ostream& os) {
369 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800370 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700371
372 os << "MAGIC:\n";
373 os << oat_header.GetMagic() << "\n\n";
374
375 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800376 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700377
Elliott Hughesa72ec822012-03-05 17:12:22 -0800378 os << "INSTRUCTION SET:\n";
379 os << oat_header.GetInstructionSet() << "\n\n";
380
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700381 {
382 std::unique_ptr<const InstructionSetFeatures> features(
383 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
384 oat_header.GetInstructionSetFeaturesBitmap()));
385 os << "INSTRUCTION SET FEATURES:\n";
386 os << features->GetFeatureString() << "\n\n";
387 }
Dave Allison70202782013-10-22 17:52:19 -0700388
Brian Carlstromaded5f72011-10-07 17:15:04 -0700389 os << "DEX FILE COUNT:\n";
390 os << oat_header.GetDexFileCount() << "\n\n";
391
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800392#define DUMP_OAT_HEADER_OFFSET(label, offset) \
393 os << label " OFFSET:\n"; \
394 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800395 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800396 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
397 } \
398 os << StringPrintf("\n\n");
399
400 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
401 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
402 GetInterpreterToInterpreterBridgeOffset);
403 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
404 GetInterpreterToCompiledCodeBridgeOffset);
405 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
406 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800407 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
408 GetQuickGenericJniTrampolineOffset);
409 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
410 GetQuickImtConflictTrampolineOffset);
411 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
412 GetQuickResolutionTrampolineOffset);
413 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
414 GetQuickToInterpreterBridgeOffset);
415#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700416
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700417 os << "IMAGE PATCH DELTA:\n";
418 os << StringPrintf("%d (0x%08x)\n\n",
419 oat_header.GetImagePatchDelta(),
420 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700421
Brian Carlstrom28db0122012-10-18 16:20:41 -0700422 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
423 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
424
425 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800426 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700427
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700428 // Print the key-value store.
429 {
430 os << "KEY VALUE STORE:\n";
431 size_t index = 0;
432 const char* key;
433 const char* value;
434 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
435 os << key << " = " << value << "\n";
436 index++;
437 }
438 os << "\n";
439 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700440
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800441 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700442 os << "BEGIN:\n";
443 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700444
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700445 os << "END:\n";
446 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
447 }
448
449 os << "SIZE:\n";
450 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700451
452 os << std::flush;
453
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800454 // If set, adjust relative address to be searched
455 if (options_.addr2instr_ != 0) {
456 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
457 os << "SEARCH ADDRESS (executable offset + input):\n";
458 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
459 }
460
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800461 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
462 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700463 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800464
465 // If file export selected skip file analysis
466 if (options_.export_dex_location_) {
467 if (!ExportDexFile(os, *oat_dex_file)) {
468 success = false;
469 }
470 } else {
471 if (!DumpOatDexFile(os, *oat_dex_file)) {
472 success = false;
473 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700474 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700475 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700476 os << std::flush;
477 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700478 }
479
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800480 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700481 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
482 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800483 return 0; // Address not in oat file
484 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800485 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
486 reinterpret_cast<uintptr_t>(oat_file_.Begin());
487 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800488 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800489 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800490 return end_offset - begin_offset;
491 }
492
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800493 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700494 return oat_file_.GetOatHeader().GetInstructionSet();
495 }
496
Ian Rogersef7d42f2014-01-06 12:55:46 -0800497 const void* GetQuickOatCode(mirror::ArtMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800498 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
499 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700500 CHECK(oat_dex_file != nullptr);
501 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700502 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700503 if (dex_file.get() == nullptr) {
504 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
505 << "': " << error_msg;
506 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800507 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700508 const DexFile::ClassDef* class_def =
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800509 dex_file->FindClassDef(descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700510 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700511 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100512 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800513 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100514 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800515 }
516 }
517 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700518 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800519 }
520
Brian Carlstromaded5f72011-10-07 17:15:04 -0700521 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800522 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800523 // We don't know the length of the code for each method, but we need to know where to stop
524 // when disassembling. What we do know is that a region of code will be followed by some other
525 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
526 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800527 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
528 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700529 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700530 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700531 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700532 if (dex_file.get() == nullptr) {
533 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
534 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800535 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800536 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800537 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800538 for (size_t class_def_index = 0;
539 class_def_index < dex_file->NumClassDefs();
540 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800541 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100542 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700543 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700544 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800545 ClassDataItemIterator it(*dex_file, class_data);
546 SkipAllFields(it);
547 uint32_t class_method_index = 0;
548 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100549 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800550 it.Next();
551 }
552 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100553 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800554 it.Next();
555 }
556 }
557 }
558 }
559
560 // If the last thing in the file is code for a method, there won't be an offset for the "next"
561 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
562 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800563 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800564 }
565
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700566 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
567 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
568 }
569
Elliott Hughese3c845c2012-02-28 17:23:01 -0800570 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800571 uint32_t code_offset = oat_method.GetCodeOffset();
572 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
573 code_offset &= ~0x1;
574 }
575 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800576 offsets_.insert(oat_method.GetMappingTableOffset());
577 offsets_.insert(oat_method.GetVmapTableOffset());
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800578 offsets_.insert(oat_method.GetGcMapOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800579 }
580
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700581 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
582 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800583 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700584 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800585 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800586 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700587
588 // Create the verifier early.
589
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700590 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700591 std::unique_ptr<const DexFile> dex_file(oat_dex_file.OpenDexFile(&error_msg));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700592 if (dex_file.get() == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700593 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700594 os << std::flush;
595 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700596 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800597 for (size_t class_def_index = 0;
598 class_def_index < dex_file->NumClassDefs();
599 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700600 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
601 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800602
603 // TODO: Support regex
604 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
605 continue;
606 }
607
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700608 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100609 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700610 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
611 class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100612 << " (" << oat_class.GetStatus() << ")"
613 << " (" << oat_class.GetType() << ")\n";
614 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800615 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
616 std::ostream indented_os(&indent_filter);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800617 if (options_.list_classes_) continue;
618 if (!DumpOatClass(indented_os, oat_class, *(dex_file.get()), class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700619 success = false;
620 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800621 if (stop_analysis) {
622 os << std::flush;
623 return success;
624 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700625 }
626
627 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700628 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700629 }
630
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800631 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
632 std::string error_msg;
633 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
634
635 std::unique_ptr<const DexFile> dex_file(oat_dex_file.OpenDexFile(&error_msg));
636 if (dex_file == nullptr) {
637 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
638 return false;
639 }
640 size_t fsize = oat_dex_file.FileSize();
641
642 // Some quick checks just in case
643 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
644 os << "Invalid dex file\n";
645 return false;
646 }
647
648 // Verify output directory exists
649 if (!OS::DirectoryExists(options_.export_dex_location_)) {
650 // TODO: Extend OS::DirectoryExists if symlink support is required
651 os << options_.export_dex_location_ << " output directory not found or symlink\n";
652 return false;
653 }
654
655 // Beautify path names
656 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
657 return false;
658 }
659
660 std::string dex_orig_name;
661 size_t dex_orig_pos = dex_file_location.rfind('/');
662 if (dex_orig_pos == std::string::npos)
663 dex_orig_name = dex_file_location;
664 else
665 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
666
667 // A more elegant approach to efficiently name user installed apps is welcome
668 if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
669 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
670 size_t apk_orig_pos = dex_file_location.rfind('/');
671 if (apk_orig_pos != std::string::npos) {
672 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
673 }
674 }
675
676 std::string out_dex_path(options_.export_dex_location_);
677 if (out_dex_path.back() != '/') {
678 out_dex_path.append("/");
679 }
680 out_dex_path.append(dex_orig_name);
681 out_dex_path.append("_export.dex");
682 if (out_dex_path.length() > PATH_MAX) {
683 return false;
684 }
685
686 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
687 if (file.get() == nullptr) {
688 os << "Failed to open output dex file " << out_dex_path;
689 return false;
690 }
691
692 if (!file->WriteFully(dex_file->Begin(), fsize)) {
693 os << "Failed to write dex file";
694 file->Erase();
695 return false;
696 }
697
698 if (file->FlushCloseOrErase() != 0) {
699 os << "Flush and close failed";
700 return false;
701 }
702
703 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
704 os << std::flush;
705
706 return true;
707 }
708
Elliott Hughese3c845c2012-02-28 17:23:01 -0800709 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700710 while (it.HasNextStaticField()) {
711 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700712 }
Ian Rogers0571d352011-11-03 19:51:38 -0700713 while (it.HasNextInstanceField()) {
714 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700715 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800716 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700717
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700718 bool DumpOatClass(std::ostream& os, const OatFile::OatClass& oat_class, const DexFile& dex_file,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800719 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700720 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800721 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -0700722 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700723 if (class_data == nullptr) { // empty class such as a marker interface?
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700724 os << std::flush;
725 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800726 }
727 ClassDataItemIterator it(dex_file, class_data);
728 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700729 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700730 while (it.HasNextDirectMethod()) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700731 if (!DumpOatMethod(os, class_def, class_method_index, oat_class, dex_file,
732 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800733 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700734 success = false;
735 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800736 if (addr_found) {
737 *stop_analysis = true;
738 return success;
739 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700740 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700741 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700742 }
Ian Rogers0571d352011-11-03 19:51:38 -0700743 while (it.HasNextVirtualMethod()) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700744 if (!DumpOatMethod(os, class_def, class_method_index, oat_class, dex_file,
745 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800746 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700747 success = false;
748 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800749 if (addr_found) {
750 *stop_analysis = true;
751 return success;
752 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700753 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700754 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700755 }
Ian Rogers0571d352011-11-03 19:51:38 -0700756 DCHECK(!it.HasNext());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700757 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700758 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700759 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800760
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700761 static constexpr uint32_t kPrologueBytes = 16;
762
763 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
764 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
765
766 bool DumpOatMethod(std::ostream& os, const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700767 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700768 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800769 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800770 uint32_t method_access_flags, bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700771 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800772
773 // TODO: Support regex
774 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
775 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000776 return success;
777 }
778
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800779 std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800780 os << StringPrintf("%d: %s (dex_method_idx=%d)\n",
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000781 class_method_index, pretty_method.c_str(),
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700782 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800783 if (options_.list_methods_) return success;
784
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800785 Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700786 std::unique_ptr<std::ostream> indent1_os(new std::ostream(&indent1_filter));
787 Indenter indent2_filter(indent1_os->rdbuf(), kIndentChar, kIndentBy1Count);
788 std::unique_ptr<std::ostream> indent2_os(new std::ostream(&indent2_filter));
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800789
790 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
791 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
792 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
793 uint32_t code_offset = oat_method.GetCodeOffset();
794 uint32_t code_size = oat_method.GetQuickCodeSize();
795 if (resolved_addr2instr_ != 0) {
796 if (resolved_addr2instr_ > code_offset + code_size) {
797 return success;
798 } else {
799 *addr_found = true; // stop analyzing file at next iteration
800 }
801 }
802
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800803 {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700804 *indent1_os << "DEX CODE:\n";
805 DumpDexCode(*indent2_os, dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700806 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700807
808 std::unique_ptr<verifier::MethodVerifier> verifier;
809 if (Runtime::Current() != nullptr) {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700810 *indent1_os << "VERIFIER TYPE ANALYSIS:\n";
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700811 verifier.reset(DumpVerifier(*indent2_os, dex_method_idx, &dex_file, class_def, code_item,
812 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -0700813 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800814 {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700815 *indent1_os << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800816 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700817 *indent1_os << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100818 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700819 *indent1_os << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
820 if (oat_method_offsets_offset > oat_file_.Size()) {
821 *indent1_os << StringPrintf(
822 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
823 oat_method_offsets_offset, oat_file_.Size());
824 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
825 os << std::flush;
826 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800827 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700828
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700829 *indent2_os << StringPrintf("code_offset: 0x%08x ", code_offset);
830 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
831 if (aligned_code_begin > oat_file_.Size()) {
832 *indent2_os << StringPrintf("WARNING: "
833 "code offset 0x%08x is past end of file 0x%08zx.\n",
834 aligned_code_begin, oat_file_.Size());
835 success = false;
836 }
837 *indent2_os << "\n";
838
839 *indent2_os << "gc_map: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800840 if (options_.absolute_addresses_) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800841 *indent2_os << StringPrintf("%p ", oat_method.GetGcMap());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700842 }
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800843 uint32_t gc_map_offset = oat_method.GetGcMapOffset();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700844 *indent2_os << StringPrintf("(offset=0x%08x)\n", gc_map_offset);
845 if (gc_map_offset > oat_file_.Size()) {
846 *indent2_os << StringPrintf("WARNING: "
847 "gc map table offset 0x%08x is past end of file 0x%08zx.\n",
848 gc_map_offset, oat_file_.Size());
849 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800850 } else if (options_.dump_raw_gc_map_) {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700851 Indenter indent3_filter(indent2_os->rdbuf(), kIndentChar, kIndentBy1Count);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800852 std::ostream indent3_os(&indent3_filter);
853 DumpGcMap(indent3_os, oat_method, code_item);
854 }
855 }
856 {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700857 *indent1_os << "OatQuickMethodHeader ";
858 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
859 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700860
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800861 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700862 *indent1_os << StringPrintf("%p ", method_header);
863 }
864 *indent1_os << StringPrintf("(offset=0x%08x)\n", method_header_offset);
865 if (method_header_offset > oat_file_.Size()) {
866 *indent1_os << StringPrintf(
867 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
868 method_header_offset, oat_file_.Size());
869 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
870 os << std::flush;
871 return false;
872 }
873
874 *indent2_os << "mapping_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800875 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700876 *indent2_os << StringPrintf("%p ", oat_method.GetMappingTable());
877 }
878 uint32_t mapping_table_offset = oat_method.GetMappingTableOffset();
879 *indent2_os << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset());
880 if (mapping_table_offset > oat_file_.Size()) {
881 *indent2_os << StringPrintf("WARNING: "
882 "mapping table offset 0x%08x is past end of file 0x%08zx. "
883 "mapping table offset was loaded from offset 0x%08x.\n",
884 mapping_table_offset, oat_file_.Size(),
885 oat_method.GetMappingTableOffsetOffset());
886 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800887 } else if (options_.dump_raw_mapping_table_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700888 Indenter indent3_filter(indent2_os->rdbuf(), kIndentChar, kIndentBy1Count);
889 std::ostream indent3_os(&indent3_filter);
890 DumpMappingTable(indent3_os, oat_method);
891 }
892
893 *indent2_os << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800894 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700895 *indent2_os << StringPrintf("%p ", oat_method.GetVmapTable());
896 }
897 uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
898 *indent2_os << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
899 if (vmap_table_offset > oat_file_.Size()) {
900 *indent2_os << StringPrintf("WARNING: "
901 "vmap table offset 0x%08x is past end of file 0x%08zx. "
902 "vmap table offset was loaded from offset 0x%08x.\n",
903 vmap_table_offset, oat_file_.Size(),
904 oat_method.GetVmapTableOffsetOffset());
905 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800906 } else if (options_.dump_vmap_) {
Roland Levillain442b46a2015-02-18 16:54:21 +0000907 DumpVmapData(*indent2_os, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700908 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800909 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700910 {
911 *indent1_os << "QuickMethodFrameInfo\n";
912
913 *indent2_os << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
914 *indent2_os << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
915 DumpSpillMask(*indent2_os, oat_method.GetCoreSpillMask(), false);
916 *indent2_os << "\n";
917 *indent2_os << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
918 DumpSpillMask(*indent2_os, oat_method.GetFpSpillMask(), true);
919 *indent2_os << "\n";
920 }
921 {
922 // Based on spill masks from QuickMethodFrameInfo so placed
923 // after it is dumped, but useful for understanding quick
924 // code, so dumped here.
925 DumpVregLocations(*indent2_os, oat_method, code_item);
926 }
927 {
928 *indent1_os << "CODE: ";
929 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
930 if (code_size_offset > oat_file_.Size()) {
931 *indent2_os << StringPrintf("WARNING: "
932 "code size offset 0x%08x is past end of file 0x%08zx.",
933 code_size_offset, oat_file_.Size());
934 success = false;
935 } else {
936 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700937 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
938 uint64_t aligned_code_end = aligned_code_begin + code_size;
939
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800940 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700941 *indent1_os << StringPrintf("%p ", code);
942 }
943 *indent1_os << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
944 code_offset,
945 code_size_offset,
946 code_size,
947 code != nullptr ? "..." : "");
948
949 if (aligned_code_begin > oat_file_.Size()) {
950 *indent2_os << StringPrintf("WARNING: "
951 "start of code at 0x%08x is past end of file 0x%08zx.",
952 aligned_code_begin, oat_file_.Size());
953 success = false;
954 } else if (aligned_code_end > oat_file_.Size()) {
955 *indent2_os << StringPrintf("WARNING: "
956 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
957 "code size is 0x%08x loaded from offset 0x%08x.\n",
958 aligned_code_end, oat_file_.Size(),
959 code_size, code_size_offset);
960 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800961 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700962 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
963 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, true, kPrologueBytes);
964 }
965 }
966 } else if (code_size > kMaxCodeSize) {
967 *indent2_os << StringPrintf("WARNING: "
968 "code size %d is bigger than max expected threshold of %d. "
969 "code size is 0x%08x loaded from offset 0x%08x.\n",
970 code_size, kMaxCodeSize,
971 code_size, code_size_offset);
972 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800973 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700974 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
975 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, true, kPrologueBytes);
976 }
977 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800978 } else if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700979 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, !success, 0);
980 }
981 }
982 }
983 os << std::flush;
984 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700985 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800986
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800987 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
988 if (spill_mask == 0) {
989 return;
990 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800991 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800992 for (size_t i = 0; i < 32; i++) {
993 if ((spill_mask & (1 << i)) != 0) {
994 if (is_float) {
995 os << "fr" << i;
996 } else {
997 os << "r" << i;
998 }
999 spill_mask ^= 1 << i; // clear bit
1000 if (spill_mask != 0) {
1001 os << ", ";
1002 } else {
1003 break;
1004 }
1005 }
1006 }
1007 os << ")";
1008 }
1009
Roland Levillain442b46a2015-02-18 16:54:21 +00001010 // Display data stored at the the vmap offset of an oat method.
1011 void DumpVmapData(std::ostream& os,
1012 const OatFile::OatMethod& oat_method,
1013 const DexFile::CodeItem* code_item) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001014 if (oat_method.GetGcMap() == nullptr) {
Roland Levillain442b46a2015-02-18 16:54:21 +00001015 // If the native GC map is null, then this method has been
1016 // compiled with the optimizing compiler. The optimizing
1017 // compiler currently outputs its stack maps in the vmap table.
1018 const void* raw_code_info = oat_method.GetVmapTable();
1019 if (raw_code_info != nullptr) {
1020 CodeInfo code_info(raw_code_info);
1021 DCHECK(code_item != nullptr);
1022 DumpCodeInfo(os, code_info, *code_item);
1023 }
1024 } else {
1025 // Otherwise, display the vmap table.
1026 const uint8_t* raw_table = oat_method.GetVmapTable();
1027 if (raw_table != nullptr) {
1028 VmapTable vmap_table(raw_table);
1029 DumpVmapTable(os, oat_method, vmap_table);
1030 }
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001031 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001032 }
1033
1034 // Display a CodeInfo object emitted by the optimizing compiler.
1035 void DumpCodeInfo(std::ostream& os,
1036 const CodeInfo& code_info,
1037 const DexFile::CodeItem& code_item) {
Nicolas Geoffray004c2302015-03-20 10:06:38 +00001038 code_info.Dump(os, code_item.registers_size_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001039 }
1040
Roland Levillain442b46a2015-02-18 16:54:21 +00001041 // Display a vmap table.
1042 void DumpVmapTable(std::ostream& os,
1043 const OatFile::OatMethod& oat_method,
1044 const VmapTable& vmap_table) {
1045 bool first = true;
1046 bool processing_fp = false;
1047 uint32_t spill_mask = oat_method.GetCoreSpillMask();
1048 for (size_t i = 0; i < vmap_table.Size(); i++) {
1049 uint16_t dex_reg = vmap_table[i];
1050 uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
1051 processing_fp ? kFloatVReg : kIntVReg);
1052 os << (first ? "v" : ", v") << dex_reg;
1053 if (!processing_fp) {
1054 os << "/r" << cpu_reg;
1055 } else {
1056 os << "/fr" << cpu_reg;
1057 }
1058 first = false;
1059 if (!processing_fp && dex_reg == 0xFFFF) {
1060 processing_fp = true;
1061 spill_mask = oat_method.GetFpSpillMask();
1062 }
1063 }
1064 os << "\n";
1065 }
1066
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001067 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1068 const DexFile::CodeItem* code_item) {
1069 if (code_item != nullptr) {
1070 size_t num_locals_ins = code_item->registers_size_;
1071 size_t num_ins = code_item->ins_size_;
1072 size_t num_locals = num_locals_ins - num_ins;
1073 size_t num_outs = code_item->outs_size_;
1074
1075 os << "vr_stack_locations:";
1076 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1077 // For readability, delimit the different kinds of VRs.
1078 if (reg == num_locals_ins) {
1079 os << "\n\tmethod*:";
1080 } else if (reg == num_locals && num_ins > 0) {
1081 os << "\n\tins:";
1082 } else if (reg == 0 && num_locals > 0) {
1083 os << "\n\tlocals:";
1084 }
1085
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001086 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1087 code_item,
1088 oat_method.GetCoreSpillMask(),
1089 oat_method.GetFpSpillMask(),
1090 oat_method.GetFrameSizeInBytes(),
1091 reg,
1092 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001093 os << " v" << reg << "[sp + #" << offset << "]";
1094 }
1095
1096 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1097 if (out_reg == 0) {
1098 os << "\n\touts:";
1099 }
1100
1101 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1102 os << " v" << out_reg << "[sp + #" << offset << "]";
1103 }
1104
1105 os << "\n";
1106 }
1107 }
1108
Ian Rogersb23a7722012-10-09 16:54:26 -07001109 void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001110 const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
Ian Rogers1809a722013-08-09 22:05:32 -07001111 const uint8_t* raw_table = oat_method.GetVmapTable();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001112 if (raw_table != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001113 const VmapTable vmap_table(raw_table);
1114 uint32_t vmap_offset;
Ian Rogers1809a722013-08-09 22:05:32 -07001115 if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001116 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
1117 uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
1118 : oat_method.GetCoreSpillMask();
1119 os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
Ian Rogersb23a7722012-10-09 16:54:26 -07001120 } else {
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001121 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1122 code_item,
1123 oat_method.GetCoreSpillMask(),
1124 oat_method.GetFpSpillMask(),
1125 oat_method.GetFrameSizeInBytes(),
1126 reg,
1127 GetInstructionSet());
Ian Rogersb23a7722012-10-09 16:54:26 -07001128 os << "[sp + #" << offset << "]";
1129 }
1130 }
1131 }
1132
Ian Rogersef7d42f2014-01-06 12:55:46 -08001133 void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
1134 const DexFile::CodeItem* code_item,
1135 size_t num_regs, const uint8_t* reg_bitmap) {
1136 bool first = true;
1137 for (size_t reg = 0; reg < num_regs; reg++) {
1138 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1139 if (first) {
1140 os << " v" << reg << " (";
1141 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1142 os << ")";
1143 first = false;
1144 } else {
1145 os << ", v" << reg << " (";
1146 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1147 os << ")";
1148 }
1149 }
1150 }
1151 if (first) {
1152 os << "No registers in GC map\n";
1153 } else {
1154 os << "\n";
1155 }
1156 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001157 void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
1158 const DexFile::CodeItem* code_item) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001159 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Ian Rogersef7d42f2014-01-06 12:55:46 -08001160 if (gc_map_raw == nullptr) {
1161 return; // No GC map.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001162 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001163 const void* quick_code = oat_method.GetQuickCode();
Elliott Hughes956af0f2014-12-11 14:34:28 -08001164 NativePcOffsetToReferenceMap map(gc_map_raw);
1165 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1166 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
1167 map.GetNativePcOffset(entry);
1168 os << StringPrintf("%p", native_pc);
1169 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001170 }
1171 }
1172
1173 void DumpMappingTable(std::ostream& os, const OatFile::OatMethod& oat_method) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001174 const void* quick_code = oat_method.GetQuickCode();
1175 if (quick_code == nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -08001176 return;
1177 }
Ian Rogers1809a722013-08-09 22:05:32 -07001178 MappingTable table(oat_method.GetMappingTable());
1179 if (table.TotalSize() != 0) {
1180 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1181 std::ostream indent_os(&indent_filter);
1182 if (table.PcToDexSize() != 0) {
1183 typedef MappingTable::PcToDexIterator It;
1184 os << "suspend point mappings {\n";
1185 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1186 indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
1187 }
1188 os << "}\n";
1189 }
1190 if (table.DexToPcSize() != 0) {
1191 typedef MappingTable::DexToPcIterator It;
1192 os << "catch entry mappings {\n";
1193 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1194 indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
1195 }
1196 os << "}\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001197 }
1198 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001199 }
1200
Ian Rogers1809a722013-08-09 22:05:32 -07001201 uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1202 size_t offset, bool suspend_point_mapping) {
1203 MappingTable table(oat_method.GetMappingTable());
1204 if (suspend_point_mapping && table.PcToDexSize() > 0) {
1205 typedef MappingTable::PcToDexIterator It;
1206 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1207 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001208 os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001209 return cur.DexPc();
1210 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001211 }
Ian Rogers1809a722013-08-09 22:05:32 -07001212 } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
1213 typedef MappingTable::DexToPcIterator It;
1214 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1215 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001216 os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001217 return cur.DexPc();
Ian Rogersb23a7722012-10-09 16:54:26 -07001218 }
1219 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001220 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001221 return DexFile::kDexNoIndex;
Ian Rogersb23a7722012-10-09 16:54:26 -07001222 }
1223
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001224 void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1225 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001226 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001227 if (gc_map_raw != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001228 NativePcOffsetToReferenceMap map(gc_map_raw);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001229 if (map.HasEntry(native_pc_offset)) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001230 size_t num_regs = map.RegWidth() * 8;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001231 const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
Ian Rogersb23a7722012-10-09 16:54:26 -07001232 bool first = true;
1233 for (size_t reg = 0; reg < num_regs; reg++) {
1234 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1235 if (first) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001236 os << "GC map objects: v" << reg << " (";
1237 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001238 os << ")";
1239 first = false;
1240 } else {
1241 os << ", v" << reg << " (";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001242 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001243 os << ")";
1244 }
1245 }
1246 }
1247 if (!first) {
1248 os << "\n";
1249 }
1250 }
1251 }
1252 }
1253
Mathieu Chartier590fee92013-09-13 13:46:47 -07001254 void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
1255 const OatFile::OatMethod& oat_method,
1256 const DexFile::CodeItem* code_item, uint32_t dex_pc) {
1257 DCHECK(verifier != nullptr);
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001258 std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001259 bool first = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001260 for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
1261 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
1262 if (kind != kUndefined) {
1263 if (first) {
1264 os << "VRegs: v";
1265 first = false;
1266 } else {
1267 os << ", v";
1268 }
1269 os << reg << " (";
1270 switch (kind) {
1271 case kImpreciseConstant:
1272 os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
1273 DescribeVReg(os, oat_method, code_item, reg, kind);
1274 break;
1275 case kConstant:
1276 os << "Constant: " << kinds.at((reg * 2) + 1);
1277 break;
1278 default:
1279 DescribeVReg(os, oat_method, code_item, reg, kind);
1280 break;
1281 }
1282 os << ")";
1283 }
1284 }
1285 if (!first) {
1286 os << "\n";
1287 }
1288 }
1289
1290
Ian Rogersb23a7722012-10-09 16:54:26 -07001291 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001292 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001293 size_t i = 0;
1294 while (i < code_item->insns_size_in_code_units_) {
1295 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001296 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1297 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001298 i += instruction->SizeInCodeUnits();
1299 }
1300 }
1301 }
1302
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001303 verifier::MethodVerifier* DumpVerifier(std::ostream& os, uint32_t dex_method_idx,
1304 const DexFile* dex_file,
1305 const DexFile::ClassDef& class_def,
1306 const DexFile::CodeItem* code_item,
1307 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001308 if ((method_access_flags & kAccNative) == 0) {
1309 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001310 StackHandleScope<1> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001311 Handle<mirror::DexCache> dex_cache(
1312 hs.NewHandle(Runtime::Current()->GetClassLinker()->FindDexCache(*dex_file)));
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001313 DCHECK(options_.class_loader_ != nullptr);
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001314 return verifier::MethodVerifier::VerifyMethodAndDump(soa.Self(), os, dex_method_idx, dex_file,
1315 dex_cache,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001316 *options_.class_loader_,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001317 &class_def, code_item,
1318 NullHandle<mirror::ArtMethod>(),
1319 method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001320 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001321
1322 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001323 }
1324
Mathieu Chartier590fee92013-09-13 13:46:47 -07001325 void DumpCode(std::ostream& os, verifier::MethodVerifier* verifier,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001326 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1327 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001328 const void* quick_code = oat_method.GetQuickCode();
1329
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001330 if (code_size == 0) {
1331 code_size = oat_method.GetQuickCodeSize();
1332 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001333 if (code_size == 0 || quick_code == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001334 os << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001335 return;
Elliott Hughes956af0f2014-12-11 14:34:28 -08001336 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001337 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1338 size_t offset = 0;
1339 while (offset < code_size) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001340 if (!bad_input) {
1341 DumpMappingAtOffset(os, oat_method, offset, false);
1342 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001343 offset += disassembler_->Dump(os, quick_native_pc + offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001344 if (!bad_input) {
1345 uint32_t dex_pc = DumpMappingAtOffset(os, oat_method, offset, true);
1346 if (dex_pc != DexFile::kDexNoIndex) {
1347 DumpGcMapAtNativePcOffset(os, oat_method, code_item, offset);
1348 if (verifier != nullptr) {
1349 DumpVRegsAtDexPc(os, verifier, oat_method, code_item, dex_pc);
1350 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001351 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001352 }
1353 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001354 }
1355 }
1356
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001357 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001358 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001359 const OatDumperOptions& options_;
1360 uint32_t resolved_addr2instr_;
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001361 InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001362 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001363 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001364};
1365
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001366class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001367 public:
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001368 explicit ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001369 const ImageHeader& image_header, OatDumperOptions* oat_dumper_options)
1370 : os_(os),
1371 image_space_(image_space),
1372 image_header_(image_header),
1373 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001374
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001375 bool Dump() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001376 std::ostream& os = *os_;
1377 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001378
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001379 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001380
Mathieu Chartier31e89252013-08-28 11:29:12 -07001381 os << "IMAGE BITMAP OFFSET: " << reinterpret_cast<void*>(image_header_.GetImageBitmapOffset())
1382 << " SIZE: " << reinterpret_cast<void*>(image_header_.GetImageBitmapSize()) << "\n\n";
1383
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001384 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001385
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001386 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001387
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001388 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1389
1390 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1391
1392 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001393
Alex Lighta59dd802014-07-02 16:28:08 -07001394 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1395
Igor Murashkin46774762014-10-22 11:37:02 -07001396 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1397
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001398 {
1399 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
1400 Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1401 std::ostream indent1_os(&indent1_filter);
1402 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
1403 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1404 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1405 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001406 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001407 indent1_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
1408 if (image_root_object->IsObjectArray()) {
1409 Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
1410 std::ostream indent2_os(&indent2_filter);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001411 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001412 = image_root_object->AsObjectArray<mirror::Object>();
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001413 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1414 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001415 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001416 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1417 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001418 run++;
1419 } else {
1420 break;
1421 }
1422 }
1423 if (run == 0) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001424 indent2_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001425 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001426 indent2_os << StringPrintf("%d to %zd: ", j, j + run);
1427 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001428 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001429 if (value != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001430 PrettyObjectValue(indent2_os, value->GetClass(), value);
1431 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001432 indent2_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001433 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001434 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001435 }
1436 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001437 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001438 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001439
Brian Carlstromaded5f72011-10-07 17:15:04 -07001440 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001441 std::string image_filename = image_space_.GetImageFilename();
1442 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001443 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001444 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001445 std::string error_msg;
Alex Lighta59dd802014-07-02 16:28:08 -07001446 const OatFile* oat_file = class_linker->FindOpenedOatFileFromOatLocation(oat_location);
1447 if (oat_file == nullptr) {
Richard Uhlere5fed032015-03-18 08:21:11 -07001448 oat_file = OatFile::Open(oat_location, oat_location,
1449 nullptr, nullptr, false, nullptr,
1450 &error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -07001451 if (oat_file == nullptr) {
1452 os << "NOT FOUND: " << error_msg << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001453 return false;
Alex Lighta59dd802014-07-02 16:28:08 -07001454 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001455 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001456 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001457
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001458 stats_.oat_file_bytes = oat_file->Size();
1459
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001460 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001461
Mathieu Chartier02e25112013-08-14 16:14:24 -07001462 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001463 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001464 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1465 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001466 }
1467
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001468 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001469
1470 // Loop through all the image spaces and dump their objects.
Ian Rogers1d54e732013-05-02 21:10:01 -07001471 gc::Heap* heap = Runtime::Current()->GetHeap();
1472 const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces();
Ian Rogers50b35e22012-10-04 10:09:15 -07001473 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001474 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001475 {
1476 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1477 heap->FlushAllocStack();
1478 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001479 // Since FlushAllocStack() above resets the (active) allocation
1480 // stack. Need to revoke the thread-local allocation stacks that
1481 // point into it.
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001482 {
1483 self->TransitionFromRunnableToSuspended(kNative);
1484 ThreadList* thread_list = Runtime::Current()->GetThreadList();
Mathieu Chartierbf9fc582015-03-13 17:21:25 -07001485 thread_list->SuspendAll(__FUNCTION__);
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001486 heap->RevokeAllThreadLocalAllocationStacks(self);
1487 thread_list->ResumeAll();
1488 self->TransitionFromSuspendedToRunnable();
1489 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001490 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001491 {
1492 std::ostream* saved_os = os_;
1493 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1494 std::ostream indent_os(&indent_filter);
1495 os_ = &indent_os;
1496 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001497 for (const auto& space : spaces) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001498 if (space->IsImageSpace()) {
Ian Rogers1d54e732013-05-02 21:10:01 -07001499 gc::space::ImageSpace* image_space = space->AsImageSpace();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001500 image_space->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1501 indent_os << "\n";
1502 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -07001503 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001504 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001505 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001506 indent_os << "\n";
1507 os_ = saved_os;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001508 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001509 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001510 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001511 if (file.get() == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001512 LOG(WARNING) << "Failed to find image in " << image_filename;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001513 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001514 if (file.get() != nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001515 stats_.file_bytes = file->GetLength();
Brian Carlstrom6f277752013-09-30 17:56:45 -07001516 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001517 size_t header_bytes = sizeof(ImageHeader);
1518 stats_.header_bytes = header_bytes;
1519 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
1520 stats_.alignment_bytes += alignment_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07001521 stats_.alignment_bytes += image_header_.GetImageBitmapOffset() - image_header_.GetImageSize();
1522 stats_.bitmap_bytes += image_header_.GetImageBitmapSize();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001523 stats_.Dump(os);
1524 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001525
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001526 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001527
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001528 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001529 }
1530
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001531 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001532 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001533 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001534 CHECK(type != nullptr);
1535 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001536 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001537 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001538 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001539 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001540 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001541 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001542 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001543 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Brian Carlstromea46f952013-07-30 01:26:50 -07001544 } else if (type->IsArtMethodClass()) {
1545 mirror::ArtMethod* method = value->AsArtMethod();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001546 os << StringPrintf("%p Method: %s\n", method, PrettyMethod(method).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001547 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001548 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001549 }
1550 }
1551
Mathieu Chartierc7853442015-03-27 14:35:38 -07001552 static void PrintField(std::ostream& os, ArtField* field, mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001553 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001554 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08001555 switch (field->GetTypeAsPrimitiveType()) {
1556 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08001557 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001558 break;
1559 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001560 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001561 break;
1562 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001563 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001564 break;
1565 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001566 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001567 break;
1568 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07001569 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001570 break;
1571 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07001572 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001573 break;
1574 case Primitive::kPrimBoolean:
Fred Shih37f05ef2014-07-16 18:38:08 -07001575 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1576 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001577 break;
1578 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07001579 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001580 break;
1581 case Primitive::kPrimNot: {
1582 // Get the value, don't compute the type unless it is non-null as we don't want
1583 // to cause class loading.
1584 mirror::Object* value = field->GetObj(obj);
1585 if (value == nullptr) {
1586 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07001587 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08001588 // Grab the field type without causing resolution.
Mathieu Chartierdaaf3262015-03-24 13:30:28 -07001589 mirror::Class* field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08001590 if (field_type != nullptr) {
1591 PrettyObjectValue(os, field_type, value);
1592 } else {
1593 os << StringPrintf("%p %s\n", value,
1594 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
1595 }
Ian Rogers50239c72013-06-17 14:53:22 -07001596 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001597 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07001598 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001599 default:
1600 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
1601 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08001602 }
1603 }
1604
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001605 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001606 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001607 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001608 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001609 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001610 }
Mathieu Chartierc7853442015-03-27 14:35:38 -07001611 ArtField* fields = klass->GetIFields();
1612 for (size_t i = 0, count = klass->NumInstanceFields(); i < count; i++) {
1613 PrintField(os, &fields[i], obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001614 }
1615 }
1616
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001617 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001618 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001619 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001620
Ian Rogersef7d42f2014-01-06 12:55:46 -08001621 const void* GetQuickOatCodeBegin(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001622 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001623 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
1624 InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet()));
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001625 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001626 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001627 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001628 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001629 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001630 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001631 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001632 }
1633
Ian Rogersef7d42f2014-01-06 12:55:46 -08001634 uint32_t GetQuickOatCodeSize(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001635 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001636 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1637 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001638 return 0;
1639 }
1640 return oat_code_begin[-1];
1641 }
1642
Ian Rogersef7d42f2014-01-06 12:55:46 -08001643 const void* GetQuickOatCodeEnd(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001644 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001645 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001646 if (oat_code_begin == nullptr) {
1647 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001648 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001649 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001650 }
1651
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001652 static void Callback(mirror::Object* obj, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001653 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001654 DCHECK(obj != nullptr);
1655 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001656 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001657 if (!state->InDumpSpace(obj)) {
1658 return;
1659 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001660
1661 size_t object_bytes = obj->SizeOf();
1662 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1663 state->stats_.object_bytes += object_bytes;
1664 state->stats_.alignment_bytes += alignment_bytes;
1665
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001666 std::ostream& os = *state->os_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001667 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001668 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001669 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
1670 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001671 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001672 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001673 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
1674 << klass->GetStatus() << ")\n";
Brian Carlstromea46f952013-07-30 01:26:50 -07001675 } else if (obj->IsArtMethod()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001676 os << StringPrintf("%p: java.lang.reflect.ArtMethod %s\n", obj,
Brian Carlstromea46f952013-07-30 01:26:50 -07001677 PrettyMethod(obj->AsArtMethod()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001678 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001679 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001680 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001681 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001682 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001683 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001684 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1685 std::ostream indent_os(&indent_filter);
1686 DumpFields(indent_os, obj, obj_class);
Ian Rogersd5b32602012-02-26 16:40:04 -08001687 if (obj->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001688 mirror::ObjectArray<mirror::Object>* obj_array = obj->AsObjectArray<mirror::Object>();
Ian Rogersd5b32602012-02-26 16:40:04 -08001689 int32_t length = obj_array->GetLength();
1690 for (int32_t i = 0; i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001691 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001692 size_t run = 0;
1693 for (int32_t j = i + 1; j < length; j++) {
1694 if (value == obj_array->Get(j)) {
1695 run++;
1696 } else {
1697 break;
1698 }
1699 }
1700 if (run == 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001701 indent_os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001702 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001703 indent_os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001704 i = i + run;
1705 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001706 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001707 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001708 PrettyObjectValue(indent_os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001709 }
1710 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001711 mirror::Class* klass = obj->AsClass();
1712 ArtField* sfields = klass->GetSFields();
1713 const size_t num_fields = klass->NumStaticFields();
1714 if (num_fields != 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001715 indent_os << "STATICS:\n";
1716 Indenter indent2_filter(indent_os.rdbuf(), kIndentChar, kIndentBy1Count);
1717 std::ostream indent2_os(&indent2_filter);
Mathieu Chartierc7853442015-03-27 14:35:38 -07001718 for (size_t i = 0; i < num_fields; i++) {
1719 PrintField(indent2_os, &sfields[i], sfields[i].GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001720 }
1721 }
Brian Carlstromea46f952013-07-30 01:26:50 -07001722 } else if (obj->IsArtMethod()) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001723 const size_t image_pointer_size = InstructionSetPointerSize(
1724 state->oat_dumper_->GetOatInstructionSet());
Brian Carlstromea46f952013-07-30 01:26:50 -07001725 mirror::ArtMethod* method = obj->AsArtMethod();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001726 if (method->IsNative()) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001727 DCHECK(method->GetNativeGcMap(image_pointer_size) == nullptr) << PrettyMethod(method);
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001728 DCHECK(method->GetMappingTable(image_pointer_size) == nullptr) << PrettyMethod(method);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001729 bool first_occurrence;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001730 const void* quick_oat_code = state->GetQuickOatCodeBegin(method);
1731 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1732 state->ComputeOatSize(quick_oat_code, &first_occurrence);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001733 if (first_occurrence) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001734 state->stats_.native_to_managed_code_bytes += quick_oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001735 }
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001736 if (quick_oat_code != method->GetEntryPointFromQuickCompiledCodePtrSize(
1737 image_pointer_size)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001738 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001739 }
Ian Rogers19846512012-02-24 11:42:47 -08001740 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
Jeff Hao88474b42013-10-23 16:24:40 -07001741 method->IsResolutionMethod() || method->IsImtConflictMethod() ||
Mathieu Chartier2d2621a2014-10-23 16:48:06 -07001742 method->IsImtUnimplementedMethod() || method->IsClassInitializer()) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001743 DCHECK(method->GetNativeGcMap(image_pointer_size) == nullptr) << PrettyMethod(method);
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001744 DCHECK(method->GetMappingTable(image_pointer_size) == nullptr) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001745 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001746 const DexFile::CodeItem* code_item = method->GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -07001747 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001748 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001749
Elliott Hughesa0e18062012-04-13 15:59:59 -07001750 bool first_occurrence;
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001751 size_t gc_map_bytes =
1752 state->ComputeOatSize(method->GetNativeGcMap(image_pointer_size), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001753 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001754 state->stats_.gc_map_bytes += gc_map_bytes;
1755 }
1756
1757 size_t pc_mapping_table_bytes =
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001758 state->ComputeOatSize(method->GetMappingTable(image_pointer_size), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001759 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001760 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
1761 }
1762
1763 size_t vmap_table_bytes =
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001764 state->ComputeOatSize(method->GetVmapTable(image_pointer_size), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001765 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001766 state->stats_.vmap_table_bytes += vmap_table_bytes;
1767 }
1768
Ian Rogersef7d42f2014-01-06 12:55:46 -08001769 const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method);
1770 const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method);
1771 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1772 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001773 if (first_occurrence) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001774 state->stats_.managed_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001775 if (method->IsConstructor()) {
1776 if (method->IsStatic()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001777 state->stats_.class_initializer_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001778 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001779 state->stats_.large_initializer_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001780 }
1781 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001782 state->stats_.large_method_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001783 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001784 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001785 state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001786
Ian Rogersef7d42f2014-01-06 12:55:46 -08001787 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001788 indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
1789 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001790
1791 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
Ian Rogersef7d42f2014-01-06 12:55:46 -08001792 vmap_table_bytes + quick_oat_code_size + object_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001793
1794 double expansion =
Ian Rogersef7d42f2014-01-06 12:55:46 -08001795 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001796 state->stats_.ComputeOutliers(total_size, expansion, method);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001797 }
1798 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07001799 std::string temp;
1800 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001801 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001802
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001803 std::set<const void*> already_seen_;
1804 // Compute the size of the given data within the oat file and whether this is the first time
1805 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07001806 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001807 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001808 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001809 already_seen_.insert(oat_data);
1810 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001811 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001812 }
1813 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001814 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001815
1816 public:
1817 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001818 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001819 size_t file_bytes;
1820
1821 size_t header_bytes;
1822 size_t object_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07001823 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001824 size_t alignment_bytes;
1825
1826 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001827 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001828 size_t managed_to_native_code_bytes;
1829 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001830 size_t class_initializer_code_bytes;
1831 size_t large_initializer_code_bytes;
1832 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001833
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001834 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001835 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001836 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001837
1838 size_t dex_instruction_bytes;
1839
Brian Carlstromea46f952013-07-30 01:26:50 -07001840 std::vector<mirror::ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001841 std::vector<size_t> method_outlier_size;
1842 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07001843 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001844
1845 explicit Stats()
1846 : oat_file_bytes(0),
1847 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001848 header_bytes(0),
1849 object_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07001850 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001851 alignment_bytes(0),
1852 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001853 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001854 managed_to_native_code_bytes(0),
1855 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07001856 class_initializer_code_bytes(0),
1857 large_initializer_code_bytes(0),
1858 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001859 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001860 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001861 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001862 dex_instruction_bytes(0) {}
1863
Elliott Hughesa0e18062012-04-13 15:59:59 -07001864 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001865 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07001866 size_t bytes;
1867 size_t count;
1868 };
1869 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
1870 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001871
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001872 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001873 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
1874 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001875 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07001876 it->second.count += 1;
1877 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001878 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07001879 }
1880 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001881
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001882 double PercentOfOatBytes(size_t size) {
1883 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
1884 }
1885
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001886 double PercentOfFileBytes(size_t size) {
1887 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
1888 }
1889
1890 double PercentOfObjectBytes(size_t size) {
1891 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
1892 }
1893
Brian Carlstromea46f952013-07-30 01:26:50 -07001894 void ComputeOutliers(size_t total_size, double expansion, mirror::ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001895 method_outlier_size.push_back(total_size);
1896 method_outlier_expansion.push_back(expansion);
1897 method_outlier.push_back(method);
1898 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001899
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001900 void DumpOutliers(std::ostream& os)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001901 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001902 size_t sum_of_sizes = 0;
1903 size_t sum_of_sizes_squared = 0;
1904 size_t sum_of_expansion = 0;
1905 size_t sum_of_expansion_squared = 0;
1906 size_t n = method_outlier_size.size();
1907 for (size_t i = 0; i < n; i++) {
1908 size_t cur_size = method_outlier_size[i];
1909 sum_of_sizes += cur_size;
1910 sum_of_sizes_squared += cur_size * cur_size;
1911 double cur_expansion = method_outlier_expansion[i];
1912 sum_of_expansion += cur_expansion;
1913 sum_of_expansion_squared += cur_expansion * cur_expansion;
1914 }
1915 size_t size_mean = sum_of_sizes / n;
1916 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
1917 double expansion_mean = sum_of_expansion / n;
1918 double expansion_variance =
1919 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
1920
1921 // Dump methods whose size is a certain number of standard deviations from the mean
1922 size_t dumped_values = 0;
1923 size_t skipped_values = 0;
1924 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
1925 size_t cur_size_variance = i * i * size_variance;
1926 bool first = true;
1927 for (size_t j = 0; j < n; j++) {
1928 size_t cur_size = method_outlier_size[j];
1929 if (cur_size > size_mean) {
1930 size_t cur_var = cur_size - size_mean;
1931 cur_var = cur_var * cur_var;
1932 if (cur_var > cur_size_variance) {
1933 if (dumped_values > 20) {
1934 if (i == 1) {
1935 skipped_values++;
1936 } else {
1937 i = 2; // jump to counting for 1 standard deviation
1938 break;
1939 }
1940 } else {
1941 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001942 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001943 first = false;
1944 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001945 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07001946 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001947 method_outlier_size[j] = 0; // don't consider this method again
1948 dumped_values++;
1949 }
1950 }
1951 }
1952 }
1953 }
1954 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001955 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001956 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001957 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001958 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001959
1960 // Dump methods whose expansion is a certain number of standard deviations from the mean
1961 dumped_values = 0;
1962 skipped_values = 0;
1963 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
1964 double cur_expansion_variance = i * i * expansion_variance;
1965 bool first = true;
1966 for (size_t j = 0; j < n; j++) {
1967 double cur_expansion = method_outlier_expansion[j];
1968 if (cur_expansion > expansion_mean) {
1969 size_t cur_var = cur_expansion - expansion_mean;
1970 cur_var = cur_var * cur_var;
1971 if (cur_var > cur_expansion_variance) {
1972 if (dumped_values > 20) {
1973 if (i == 1) {
1974 skipped_values++;
1975 } else {
1976 i = 2; // jump to counting for 1 standard deviation
1977 break;
1978 }
1979 } else {
1980 if (first) {
1981 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07001982 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001983 first = false;
1984 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001985 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07001986 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001987 method_outlier_expansion[j] = 0.0; // don't consider this method again
1988 dumped_values++;
1989 }
1990 }
1991 }
1992 }
1993 }
1994 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001995 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001996 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001997 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001998 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001999 }
2000
Ian Rogersb726dcb2012-09-05 08:57:23 -07002001 void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002002 {
2003 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2004 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
2005 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
2006 std::ostream indent_os(&indent_filter);
2007 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2008 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier32327092013-08-30 14:04:08 -07002009 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002010 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
2011 header_bytes, PercentOfFileBytes(header_bytes),
2012 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002013 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002014 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2015 << std::flush;
Mathieu Chartier32327092013-08-30 14:04:08 -07002016 CHECK_EQ(file_bytes, bitmap_bytes + header_bytes + object_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002017 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002018
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002019 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002020 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002021 for (const auto& sizes_and_count : sizes_and_counts) {
2022 const std::string& descriptor(sizes_and_count.first);
2023 double average = static_cast<double>(sizes_and_count.second.bytes) /
2024 static_cast<double>(sizes_and_count.second.count);
2025 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002026 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002027 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002028 descriptor.c_str(), sizes_and_count.second.bytes,
2029 sizes_and_count.second.count, average, percent);
2030 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002031 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002032 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002033 CHECK_EQ(object_bytes, object_bytes_total);
2034
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002035 os << StringPrintf("oat_file_bytes = %8zd\n"
2036 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2037 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2038 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2039 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2040 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2041 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002042 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002043 managed_code_bytes,
2044 PercentOfOatBytes(managed_code_bytes),
2045 managed_to_native_code_bytes,
2046 PercentOfOatBytes(managed_to_native_code_bytes),
2047 native_to_managed_code_bytes,
2048 PercentOfOatBytes(native_to_managed_code_bytes),
2049 class_initializer_code_bytes,
2050 PercentOfOatBytes(class_initializer_code_bytes),
2051 large_initializer_code_bytes,
2052 PercentOfOatBytes(large_initializer_code_bytes),
2053 large_method_code_bytes,
2054 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002055 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002056 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002057 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002058 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2059 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002060 }
2061
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002062 os << "\n" << StringPrintf("gc_map_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2063 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2064 "vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002065 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
2066 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
2067 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002068 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002069
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002070 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2071 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002072 static_cast<double>(managed_code_bytes) /
2073 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002074 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002075 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002076 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002077
2078 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002079 }
2080 } stats_;
2081
2082 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002083 enum {
2084 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2085 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2086 kLargeConstructorDexBytes = 4000,
2087 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2088 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2089 kLargeMethodDexBytes = 16000
2090 };
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002091 std::ostream* os_;
Ian Rogers1d54e732013-05-02 21:10:01 -07002092 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002093 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002094 std::unique_ptr<OatDumper> oat_dumper_;
2095 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002096
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002097 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002098};
2099
Andreas Gampe00b25f32014-09-17 21:49:05 -07002100static int DumpImage(Runtime* runtime, const char* image_location, OatDumperOptions* options,
2101 std::ostream* os) {
2102 // Dumping the image, no explicit class loader.
2103 NullHandle<mirror::ClassLoader> null_class_loader;
2104 options->class_loader_ = &null_class_loader;
2105
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002106 ScopedObjectAccess soa(Thread::Current());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002107 gc::Heap* heap = runtime->GetHeap();
Ian Rogers1d54e732013-05-02 21:10:01 -07002108 gc::space::ImageSpace* image_space = heap->GetImageSpace();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002109 CHECK(image_space != nullptr);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002110 const ImageHeader& image_header = image_space->GetImageHeader();
2111 if (!image_header.IsValid()) {
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07002112 fprintf(stderr, "Invalid image header %s\n", image_location);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002113 return EXIT_FAILURE;
2114 }
Igor Murashkin37743352014-11-13 14:38:00 -08002115
Andreas Gampe00b25f32014-09-17 21:49:05 -07002116 ImageDumper image_dumper(os, *image_space, image_header, options);
Igor Murashkin37743352014-11-13 14:38:00 -08002117
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002118 bool success = image_dumper.Dump();
2119 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002120}
2121
Andreas Gampe00b25f32014-09-17 21:49:05 -07002122static int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
2123 std::ostream* os) {
2124 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2125
2126 Thread* self = Thread::Current();
2127 CHECK(self != nullptr);
2128 // Need well-known-classes.
2129 WellKnownClasses::Init(self->GetJniEnv());
2130
2131 // Need to register dex files to get a working dex cache.
2132 ScopedObjectAccess soa(self);
2133 ClassLinker* class_linker = runtime->GetClassLinker();
2134 class_linker->RegisterOatFile(oat_file);
Richard Uhlerfbef44d2014-12-23 09:48:51 -08002135 std::vector<std::unique_ptr<const DexFile>> dex_files;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002136 for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
2137 std::string error_msg;
Richard Uhlerfbef44d2014-12-23 09:48:51 -08002138 std::unique_ptr<const DexFile> dex_file = odf->OpenDexFile(&error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002139 CHECK(dex_file != nullptr) << error_msg;
2140 class_linker->RegisterDexFile(*dex_file);
Richard Uhlerfbef44d2014-12-23 09:48:51 -08002141 dex_files.push_back(std::move(dex_file));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002142 }
2143
2144 // Need a class loader.
Andreas Gampe00b25f32014-09-17 21:49:05 -07002145 // Fake that we're a compiler.
Richard Uhlerfbef44d2014-12-23 09:48:51 -08002146 std::vector<const DexFile*> class_path;
2147 for (auto& dex_file : dex_files) {
2148 class_path.push_back(dex_file.get());
2149 }
Andreas Gampe81c6f8d2015-03-25 17:19:53 -07002150 jobject class_loader = class_linker->CreatePathClassLoader(self, class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002151
2152 // Use the class loader while dumping.
2153 StackHandleScope<1> scope(self);
2154 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
2155 soa.Decode<mirror::ClassLoader*>(class_loader));
2156 options->class_loader_ = &loader_handle;
2157
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002158 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002159 bool success = oat_dumper.Dump(*os);
2160 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2161}
2162
2163static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002164 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002165 // No image = no class loader.
2166 NullHandle<mirror::ClassLoader> null_class_loader;
2167 options->class_loader_ = &null_class_loader;
2168
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002169 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002170 bool success = oat_dumper.Dump(*os);
2171 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2172}
2173
2174static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2175 std::ostream* os) {
2176 std::string error_msg;
Igor Murashkin37743352014-11-13 14:38:00 -08002177 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
Richard Uhlere5fed032015-03-18 08:21:11 -07002178 nullptr, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002179 if (oat_file == nullptr) {
2180 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2181 return EXIT_FAILURE;
2182 }
2183
2184 if (runtime != nullptr) {
2185 return DumpOatWithRuntime(runtime, oat_file, options, os);
2186 } else {
2187 return DumpOatWithoutRuntime(oat_file, options, os);
2188 }
2189}
2190
2191static int SymbolizeOat(const char* oat_filename, std::string& output_name) {
2192 std::string error_msg;
Igor Murashkin37743352014-11-13 14:38:00 -08002193 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
Richard Uhlere5fed032015-03-18 08:21:11 -07002194 nullptr, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002195 if (oat_file == nullptr) {
2196 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2197 return EXIT_FAILURE;
2198 }
2199
2200 OatSymbolizer oat_symbolizer(oat_file, output_name);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002201 if (!oat_symbolizer.Symbolize()) {
2202 fprintf(stderr, "Failed to symbolize\n");
2203 return EXIT_FAILURE;
2204 }
2205
2206 return EXIT_SUCCESS;
2207}
2208
Igor Murashkin37743352014-11-13 14:38:00 -08002209struct OatdumpArgs : public CmdlineArgs {
2210 protected:
2211 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002212
Igor Murashkin37743352014-11-13 14:38:00 -08002213 virtual ParseStatus ParseCustom(const StringPiece& option,
2214 std::string* error_msg) OVERRIDE {
2215 {
2216 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
2217 if (base_parse != kParseUnknownArgument) {
2218 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002219 }
2220 }
2221
Igor Murashkin37743352014-11-13 14:38:00 -08002222 if (option.starts_with("--oat-file=")) {
2223 oat_filename_ = option.substr(strlen("--oat-file=")).data();
2224 } else if (option.starts_with("--image=")) {
2225 image_location_ = option.substr(strlen("--image=")).data();
2226 } else if (option =="--dump:raw_mapping_table") {
2227 dump_raw_mapping_table_ = true;
2228 } else if (option == "--dump:raw_gc_map") {
2229 dump_raw_gc_map_ = true;
2230 } else if (option == "--no-dump:vmap") {
2231 dump_vmap_ = false;
2232 } else if (option == "--no-disassemble") {
2233 disassemble_code_ = false;
2234 } else if (option.starts_with("--symbolize=")) {
2235 oat_filename_ = option.substr(strlen("--symbolize=")).data();
2236 symbolize_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002237 } else if (option.starts_with("--class-filter=")) {
2238 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002239 } else if (option.starts_with("--method-filter=")) {
2240 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002241 } else if (option.starts_with("--list-classes")) {
2242 list_classes_ = true;
2243 } else if (option.starts_with("--list-methods")) {
2244 list_methods_ = true;
2245 } else if (option.starts_with("--export-dex-to=")) {
2246 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
2247 } else if (option.starts_with("--addr2instr=")) {
2248 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
2249 *error_msg = "Address conversion failed";
2250 return kParseError;
2251 }
Igor Murashkin37743352014-11-13 14:38:00 -08002252 } else {
2253 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002254 }
2255
Igor Murashkin37743352014-11-13 14:38:00 -08002256 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002257 }
2258
Igor Murashkin37743352014-11-13 14:38:00 -08002259 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
2260 // Infer boot image location from the image location if possible.
2261 if (boot_image_location_ == nullptr) {
2262 boot_image_location_ = image_location_;
2263 }
2264
2265 // Perform the parent checks.
2266 ParseStatus parent_checks = Base::ParseChecks(error_msg);
2267 if (parent_checks != kParseOk) {
2268 return parent_checks;
2269 }
2270
2271 // Perform our own checks.
2272 if (image_location_ == nullptr && oat_filename_ == nullptr) {
2273 *error_msg = "Either --image or --oat-file must be specified";
2274 return kParseError;
2275 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
2276 *error_msg = "Either --image or --oat-file must be specified but not both";
2277 return kParseError;
2278 }
2279
2280 return kParseOk;
2281 }
2282
2283 virtual std::string GetUsage() const {
2284 std::string usage;
2285
2286 usage +=
2287 "Usage: oatdump [options] ...\n"
2288 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
2289 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
2290 "\n"
2291 // Either oat-file or image is required.
2292 " --oat-file=<file.oat>: specifies an input oat filename.\n"
2293 " Example: --oat-file=/system/framework/boot.oat\n"
2294 "\n"
2295 " --image=<file.art>: specifies an input image location.\n"
2296 " Example: --image=/system/framework/boot.art\n"
2297 "\n";
2298
2299 usage += Base::GetUsage();
2300
2301 usage += // Optional.
2302 " --dump:raw_mapping_table enables dumping of the mapping table.\n"
2303 " Example: --dump:raw_mapping_table\n"
2304 "\n"
2305 " --dump:raw_mapping_table enables dumping of the GC map.\n"
2306 " Example: --dump:raw_gc_map\n"
2307 "\n"
2308 " --no-dump:vmap may be used to disable vmap dumping.\n"
2309 " Example: --no-dump:vmap\n"
2310 "\n"
2311 " --no-disassemble may be used to disable disassembly.\n"
2312 " Example: --no-disassemble\n"
2313 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002314 " --list-classes may be used to list target file classes (can be used with filters).\n"
2315 " Example: --list-classes\n"
2316 " Example: --list-classes --class-filter=com.example.foo\n"
2317 "\n"
2318 " --list-methods may be used to list target file methods (can be used with filters).\n"
2319 " Example: --list-methods\n"
2320 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
2321 "\n"
2322 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
2323 " Example: --symbolize=/system/framework/boot.oat\n"
2324 "\n"
2325 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
2326 " Example: --class-filter=com.example.foo\n"
2327 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002328 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
2329 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002330 "\n"
2331 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
2332 " Example: --export-dex-to=/data/local/tmp\n"
2333 "\n"
2334 " --addr2instr=<address>: output matching method disassembled code from relative\n"
2335 " address (e.g. PC from crash dump)\n"
2336 " Example: --addr2instr=0x00001a3b\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002337 "\n";
2338
2339 return usage;
2340 }
2341
2342 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07002343 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002344 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00002345 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07002346 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002347 std::string elf_filename_prefix_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002348 bool dump_raw_mapping_table_ = false;
2349 bool dump_raw_gc_map_ = false;
2350 bool dump_vmap_ = true;
2351 bool disassemble_code_ = true;
2352 bool symbolize_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002353 bool list_classes_ = false;
2354 bool list_methods_ = false;
2355 uint32_t addr2instr_ = 0;
2356 const char* export_dex_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002357};
2358
Igor Murashkin37743352014-11-13 14:38:00 -08002359struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
2360 virtual bool NeedsRuntime() OVERRIDE {
2361 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002362
Igor Murashkin37743352014-11-13 14:38:00 -08002363 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
2364 bool absolute_addresses = (args_->oat_filename_ == nullptr);
2365
2366 oat_dumper_options_ = std::unique_ptr<OatDumperOptions>(new OatDumperOptions(
2367 args_->dump_raw_mapping_table_,
2368 args_->dump_raw_gc_map_,
2369 args_->dump_vmap_,
2370 args_->disassemble_code_,
2371 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002372 args_->class_filter_,
2373 args_->method_filter_,
2374 args_->list_classes_,
2375 args_->list_methods_,
2376 args_->export_dex_location_,
2377 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08002378
2379 return (args_->boot_image_location_ != nullptr || args_->image_location_ != nullptr) &&
2380 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002381 }
2382
Igor Murashkin37743352014-11-13 14:38:00 -08002383 virtual bool ExecuteWithoutRuntime() OVERRIDE {
2384 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08002385 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002386
Mathieu Chartierd424d082014-10-15 10:31:46 -07002387 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08002388
Andreas Gampec24f3992014-12-17 20:40:11 -08002389 if (args_->symbolize_) {
2390 return SymbolizeOat(args_->oat_filename_, args_->output_name_) == EXIT_SUCCESS;
2391 } else {
2392 return DumpOat(nullptr,
2393 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002394 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08002395 args_->os_) == EXIT_SUCCESS;
2396 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002397 }
2398
Igor Murashkin37743352014-11-13 14:38:00 -08002399 virtual bool ExecuteWithRuntime(Runtime* runtime) {
2400 CHECK(args_ != nullptr);
2401
2402 if (args_->oat_filename_ != nullptr) {
2403 return DumpOat(runtime,
2404 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002405 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08002406 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002407 }
Igor Murashkin37743352014-11-13 14:38:00 -08002408
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002409 return DumpImage(runtime, args_->image_location_, oat_dumper_options_.get(), args_->os_)
Igor Murashkin37743352014-11-13 14:38:00 -08002410 == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002411 }
2412
Igor Murashkin37743352014-11-13 14:38:00 -08002413 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
2414};
Andreas Gampe00b25f32014-09-17 21:49:05 -07002415
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002416} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07002417
2418int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08002419 art::OatdumpMain main;
2420 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002421}