blob: 931cca742a8c1140ce937ac45de87cedc45fcfc5 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrom78128a62011-09-15 17:21:19 -070016
17#include <stdio.h>
18#include <stdlib.h>
19
Brian Carlstrom27ec9612011-09-19 20:20:38 -070020#include <fstream>
21#include <iostream>
Igor Murashkin37743352014-11-13 14:38:00 -080022#include <map>
23#include <set>
Brian Carlstrom78128a62011-09-15 17:21:19 -070024#include <string>
Andreas Gampe54fc26c2014-09-04 21:47:42 -070025#include <unordered_map>
Brian Carlstrom78128a62011-09-15 17:21:19 -070026#include <vector>
27
Ian Rogersd582fa42014-11-05 23:46:43 -080028#include "arch/instruction_set_features.h"
Elliott Hughes76160052012-12-12 16:31:20 -080029#include "base/unix_file/fd_file.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070030#include "class_linker.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070032#include "dex_file-inl.h"
Elliott Hughese3c845c2012-02-28 17:23:01 -080033#include "dex_instruction.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080034#include "disassembler.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070035#include "elf_builder.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080036#include "gc_map.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070037#include "gc/space/image_space.h"
38#include "gc/space/large_object_space.h"
39#include "gc/space/space-inl.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070040#include "image.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080041#include "indenter.h"
Ian Rogers1809a722013-08-09 22:05:32 -070042#include "mapping_table.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070043#include "mirror/art_field-inl.h"
44#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045#include "mirror/array-inl.h"
46#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047#include "mirror/object-inl.h"
48#include "mirror/object_array-inl.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080049#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010050#include "oat_file-inl.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080051#include "os.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070052#include "output_stream.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070053#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070054#include "scoped_thread_state_change.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070055#include "ScopedLocalRef.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080056#include "thread_list.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080057#include "verifier/dex_gc_map.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080058#include "verifier/method_verifier.h"
Ian Rogers1809a722013-08-09 22:05:32 -070059#include "vmap_table.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070060#include "well_known_classes.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070061
Igor Murashkin37743352014-11-13 14:38:00 -080062#include <sys/stat.h>
63#include "cmdline.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070064
Igor Murashkin37743352014-11-13 14:38:00 -080065namespace art {
Brian Carlstrom78128a62011-09-15 17:21:19 -070066
Ian Rogersff1ed472011-09-20 13:46:24 -070067const char* image_roots_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -080068 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -070069 "kImtConflictMethod",
Mathieu Chartier2d2621a2014-10-23 16:48:06 -070070 "kImtUnimplementedMethod",
Jeff Hao88474b42013-10-23 16:24:40 -070071 "kDefaultImt",
Brian Carlstrome24fa612011-09-29 00:53:55 -070072 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -070073 "kRefsOnlySaveMethod",
74 "kRefsAndArgsSaveMethod",
Brian Carlstrom58ae9412011-10-04 00:56:06 -070075 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070076 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070077};
78
Ian Rogers0279ebb2014-10-08 17:27:48 -070079class OatSymbolizer FINAL : public CodeOutput {
Andreas Gampe54fc26c2014-09-04 21:47:42 -070080 public:
Ian Rogers0279ebb2014-10-08 17:27:48 -070081 explicit OatSymbolizer(const OatFile* oat_file, const std::string& output_name) :
82 oat_file_(oat_file), builder_(nullptr), elf_output_(nullptr),
83 output_name_(output_name.empty() ? "symbolized.oat" : output_name) {
84 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -070085
86 bool Init() {
87 Elf32_Word oat_data_size = oat_file_->GetOatHeader().GetExecutableOffset();
88
89 uint32_t diff = static_cast<uint32_t>(oat_file_->End() - oat_file_->Begin());
90 uint32_t oat_exec_size = diff - oat_data_size;
91
Andreas Gampe54fc26c2014-09-04 21:47:42 -070092 elf_output_ = OS::CreateEmptyFile(output_name_.c_str());
93
94 builder_.reset(new ElfBuilder<Elf32_Word, Elf32_Sword, Elf32_Addr, Elf32_Dyn,
95 Elf32_Sym, Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr>(
96 this,
97 elf_output_,
98 oat_file_->GetOatHeader().GetInstructionSet(),
99 0,
100 oat_data_size,
101 oat_data_size,
102 oat_exec_size,
103 true,
104 false));
105
106 if (!builder_->Init()) {
107 builder_.reset(nullptr);
108 return false;
109 }
110
111 return true;
112 }
113
114 typedef void (OatSymbolizer::*Callback)(const DexFile::ClassDef&,
115 uint32_t,
116 const OatFile::OatMethod&,
117 const DexFile&,
118 uint32_t,
119 const DexFile::CodeItem*,
120 uint32_t);
121
122 bool Symbolize() {
123 if (builder_.get() == nullptr) {
124 return false;
125 }
126
127 Walk(&art::OatSymbolizer::RegisterForDedup);
128
129 NormalizeState();
130
131 Walk(&art::OatSymbolizer::AddSymbol);
132
133 bool result = builder_->Write();
134
Andreas Gampe4303ba92014-11-06 01:00:46 -0800135 // Ignore I/O errors.
136 UNUSED(elf_output_->FlushClose());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700137
138 return result;
139 }
140
141 void Walk(Callback callback) {
142 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
143 for (size_t i = 0; i < oat_dex_files.size(); i++) {
144 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
145 CHECK(oat_dex_file != NULL);
146 WalkOatDexFile(oat_dex_file, callback);
147 }
148 }
149
150 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file, Callback callback) {
151 std::string error_msg;
152 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
153 if (dex_file.get() == nullptr) {
154 return;
155 }
156 for (size_t class_def_index = 0;
157 class_def_index < dex_file->NumClassDefs();
158 class_def_index++) {
159 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
160 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
161 OatClassType type = oat_class.GetType();
162 switch (type) {
163 case kOatClassAllCompiled:
164 case kOatClassSomeCompiled:
165 WalkOatClass(oat_class, *dex_file.get(), class_def, callback);
166 break;
167
168 case kOatClassNoneCompiled:
169 case kOatClassMax:
170 // Ignore.
171 break;
172 }
173 }
174 }
175
176 void WalkOatClass(const OatFile::OatClass& oat_class, const DexFile& dex_file,
177 const DexFile::ClassDef& class_def, Callback callback) {
Ian Rogers13735952014-10-08 12:43:28 -0700178 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700179 if (class_data == nullptr) { // empty class such as a marker interface?
180 return;
181 }
182 // Note: even if this is an interface or a native class, we still have to walk it, as there
183 // might be a static initializer.
184 ClassDataItemIterator it(dex_file, class_data);
185 SkipAllFields(&it);
186 uint32_t class_method_idx = 0;
187 while (it.HasNextDirectMethod()) {
188 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
189 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700190 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700191 class_method_idx++;
192 it.Next();
193 }
194 while (it.HasNextVirtualMethod()) {
195 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
196 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700197 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700198 class_method_idx++;
199 it.Next();
200 }
201 DCHECK(!it.HasNext());
202 }
203
204 void WalkOatMethod(const DexFile::ClassDef& class_def, uint32_t class_method_index,
205 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
206 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
207 uint32_t method_access_flags, Callback callback) {
208 if ((method_access_flags & kAccAbstract) != 0) {
209 // Abstract method, no code.
210 return;
211 }
212 if (oat_method.GetCodeOffset() == 0) {
213 // No code.
214 return;
215 }
216
217 (this->*callback)(class_def, class_method_index, oat_method, dex_file, dex_method_idx, code_item,
218 method_access_flags);
219 }
220
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700221 void RegisterForDedup(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
222 uint32_t class_method_index ATTRIBUTE_UNUSED,
223 const OatFile::OatMethod& oat_method,
224 const DexFile& dex_file ATTRIBUTE_UNUSED,
225 uint32_t dex_method_idx ATTRIBUTE_UNUSED,
226 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
227 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700228 state_[oat_method.GetCodeOffset()]++;
229 }
230
231 void NormalizeState() {
232 for (auto& x : state_) {
233 if (x.second == 1) {
234 state_[x.first] = 0;
235 }
236 }
237 }
238
239 enum class DedupState { // private
240 kNotDeduplicated,
241 kDeduplicatedFirst,
242 kDeduplicatedOther
243 };
244 DedupState IsDuplicated(uint32_t offset) {
245 if (state_[offset] == 0) {
246 return DedupState::kNotDeduplicated;
247 }
248 if (state_[offset] == 1) {
249 return DedupState::kDeduplicatedOther;
250 }
251 state_[offset] = 1;
252 return DedupState::kDeduplicatedFirst;
253 }
254
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700255 void AddSymbol(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
256 uint32_t class_method_index ATTRIBUTE_UNUSED,
257 const OatFile::OatMethod& oat_method,
258 const DexFile& dex_file,
259 uint32_t dex_method_idx,
260 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
261 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700262 DedupState dedup = IsDuplicated(oat_method.GetCodeOffset());
263 if (dedup != DedupState::kDeduplicatedOther) {
264 std::string pretty_name = PrettyMethod(dex_method_idx, dex_file, true);
265
266 if (dedup == DedupState::kDeduplicatedFirst) {
267 pretty_name = "[Dedup]" + pretty_name;
268 }
269
270 ElfSymtabBuilder<Elf32_Word, Elf32_Sword, Elf32_Addr,
Ian Rogers0279ebb2014-10-08 17:27:48 -0700271 Elf32_Sym, Elf32_Shdr>* symtab = builder_->GetSymtabBuilder();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700272
Ian Rogers0279ebb2014-10-08 17:27:48 -0700273 symtab->AddSymbol(pretty_name, &builder_->GetTextBuilder(),
274 oat_method.GetCodeOffset() - oat_file_->GetOatHeader().GetExecutableOffset(),
275 true, oat_method.GetQuickCodeSize(), STB_GLOBAL, STT_FUNC);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700276 }
277 }
278
Vladimir Markof4da6752014-08-01 19:04:18 +0100279 // Set oat data offset. Required by ElfBuilder/CodeOutput.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700280 void SetCodeOffset(size_t offset ATTRIBUTE_UNUSED) {
Vladimir Markof4da6752014-08-01 19:04:18 +0100281 // Nothing to do.
282 }
283
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700284 // Write oat code. Required by ElfBuilder/CodeOutput.
285 bool Write(OutputStream* out) {
286 return out->WriteFully(oat_file_->Begin(), oat_file_->End() - oat_file_->Begin());
287 }
288
289 private:
290 static void SkipAllFields(ClassDataItemIterator* it) {
291 while (it->HasNextStaticField()) {
292 it->Next();
293 }
294 while (it->HasNextInstanceField()) {
295 it->Next();
296 }
297 }
298
299 const OatFile* oat_file_;
300 std::unique_ptr<ElfBuilder<Elf32_Word, Elf32_Sword, Elf32_Addr, Elf32_Dyn,
301 Elf32_Sym, Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr> > builder_;
302 File* elf_output_;
303 std::unordered_map<uint32_t, uint32_t> state_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700304 const std::string output_name_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700305};
306
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700307class OatDumperOptions {
308 public:
309 OatDumperOptions(bool dump_raw_mapping_table,
310 bool dump_raw_gc_map,
311 bool dump_vmap,
312 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700313 bool absolute_addresses,
Igor Murashkin37743352014-11-13 14:38:00 -0800314 const char* method_filter)
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),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000320 method_filter_(method_filter),
Igor Murashkin37743352014-11-13 14:38:00 -0800321 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700322
323 const bool dump_raw_mapping_table_;
324 const bool dump_raw_gc_map_;
325 const bool dump_vmap_;
326 const bool disassemble_code_;
327 const bool absolute_addresses_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000328 const char* const method_filter_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700329 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700330};
331
Elliott Hughese3c845c2012-02-28 17:23:01 -0800332class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700333 public:
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700334 explicit OatDumper(const OatFile& oat_file, OatDumperOptions* options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000335 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800336 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700337 options_(options),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800338 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
339 disassembler_(Disassembler::Create(instruction_set_,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700340 new DisassemblerOptions(options_->absolute_addresses_,
Alexandre Ramesa37d9252014-10-27 11:28:14 +0000341 oat_file.Begin(),
342 true /* can_read_litals_ */))) {
Andreas Gampe00b25f32014-09-17 21:49:05 -0700343 CHECK(options_->class_loader_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800344 AddAllOffsets();
345 }
346
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700347 ~OatDumper() {
348 delete options_;
349 delete disassembler_;
350 }
351
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800352 InstructionSet GetInstructionSet() {
353 return instruction_set_;
354 }
355
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700356 bool Dump(std::ostream& os) {
357 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800358 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700359
360 os << "MAGIC:\n";
361 os << oat_header.GetMagic() << "\n\n";
362
363 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800364 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700365
Elliott Hughesa72ec822012-03-05 17:12:22 -0800366 os << "INSTRUCTION SET:\n";
367 os << oat_header.GetInstructionSet() << "\n\n";
368
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700369 {
370 std::unique_ptr<const InstructionSetFeatures> features(
371 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
372 oat_header.GetInstructionSetFeaturesBitmap()));
373 os << "INSTRUCTION SET FEATURES:\n";
374 os << features->GetFeatureString() << "\n\n";
375 }
Dave Allison70202782013-10-22 17:52:19 -0700376
Brian Carlstromaded5f72011-10-07 17:15:04 -0700377 os << "DEX FILE COUNT:\n";
378 os << oat_header.GetDexFileCount() << "\n\n";
379
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800380#define DUMP_OAT_HEADER_OFFSET(label, offset) \
381 os << label " OFFSET:\n"; \
382 os << StringPrintf("0x%08x", oat_header.offset()); \
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700383 if (oat_header.offset() != 0 && options_->absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800384 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
385 } \
386 os << StringPrintf("\n\n");
387
388 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
389 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
390 GetInterpreterToInterpreterBridgeOffset);
391 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
392 GetInterpreterToCompiledCodeBridgeOffset);
393 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
394 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800395 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
396 GetQuickGenericJniTrampolineOffset);
397 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
398 GetQuickImtConflictTrampolineOffset);
399 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
400 GetQuickResolutionTrampolineOffset);
401 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
402 GetQuickToInterpreterBridgeOffset);
403#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700404
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700405 os << "IMAGE PATCH DELTA:\n";
406 os << StringPrintf("%d (0x%08x)\n\n",
407 oat_header.GetImagePatchDelta(),
408 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700409
Brian Carlstrom28db0122012-10-18 16:20:41 -0700410 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
411 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
412
413 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800414 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700415
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700416 // Print the key-value store.
417 {
418 os << "KEY VALUE STORE:\n";
419 size_t index = 0;
420 const char* key;
421 const char* value;
422 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
423 os << key << " = " << value << "\n";
424 index++;
425 }
426 os << "\n";
427 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700428
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700429 if (options_->absolute_addresses_) {
430 os << "BEGIN:\n";
431 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700432
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700433 os << "END:\n";
434 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
435 }
436
437 os << "SIZE:\n";
438 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700439
440 os << std::flush;
441
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800442 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
443 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700444 CHECK(oat_dex_file != nullptr);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700445 if (!DumpOatDexFile(os, *oat_dex_file)) {
446 success = false;
447 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700448 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700449 os << std::flush;
450 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700451 }
452
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800453 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700454 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
455 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800456 return 0; // Address not in oat file
457 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800458 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
459 reinterpret_cast<uintptr_t>(oat_file_.Begin());
460 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800461 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800462 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800463 return end_offset - begin_offset;
464 }
465
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800466 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700467 return oat_file_.GetOatHeader().GetInstructionSet();
468 }
469
Ian Rogersef7d42f2014-01-06 12:55:46 -0800470 const void* GetQuickOatCode(mirror::ArtMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800471 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
472 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700473 CHECK(oat_dex_file != nullptr);
474 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700475 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700476 if (dex_file.get() == nullptr) {
477 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
478 << "': " << error_msg;
479 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800480 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700481 const DexFile::ClassDef* class_def =
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800482 dex_file->FindClassDef(descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700483 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700484 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100485 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800486 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100487 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800488 }
489 }
490 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700491 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800492 }
493
Brian Carlstromaded5f72011-10-07 17:15:04 -0700494 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800495 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800496 // We don't know the length of the code for each method, but we need to know where to stop
497 // when disassembling. What we do know is that a region of code will be followed by some other
498 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
499 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800500 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
501 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700502 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700503 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700504 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700505 if (dex_file.get() == nullptr) {
506 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
507 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800508 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800509 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800510 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800511 for (size_t class_def_index = 0;
512 class_def_index < dex_file->NumClassDefs();
513 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800514 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100515 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700516 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700517 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800518 ClassDataItemIterator it(*dex_file, class_data);
519 SkipAllFields(it);
520 uint32_t class_method_index = 0;
521 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100522 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800523 it.Next();
524 }
525 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100526 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800527 it.Next();
528 }
529 }
530 }
531 }
532
533 // If the last thing in the file is code for a method, there won't be an offset for the "next"
534 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
535 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800536 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800537 }
538
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700539 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
540 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
541 }
542
Elliott Hughese3c845c2012-02-28 17:23:01 -0800543 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800544 uint32_t code_offset = oat_method.GetCodeOffset();
545 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
546 code_offset &= ~0x1;
547 }
548 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800549 offsets_.insert(oat_method.GetMappingTableOffset());
550 offsets_.insert(oat_method.GetVmapTableOffset());
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800551 offsets_.insert(oat_method.GetGcMapOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800552 }
553
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700554 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
555 bool success = true;
556 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800557 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800558 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700559
560 // Create the verifier early.
561
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700562 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700563 std::unique_ptr<const DexFile> dex_file(oat_dex_file.OpenDexFile(&error_msg));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700564 if (dex_file.get() == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700565 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700566 os << std::flush;
567 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700568 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800569 for (size_t class_def_index = 0;
570 class_def_index < dex_file->NumClassDefs();
571 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700572 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
573 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700574 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100575 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700576 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
577 class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100578 << " (" << oat_class.GetStatus() << ")"
579 << " (" << oat_class.GetType() << ")\n";
580 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800581 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
582 std::ostream indented_os(&indent_filter);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700583 if (!DumpOatClass(indented_os, oat_class, *(dex_file.get()), class_def)) {
584 success = false;
585 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700586 }
587
588 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700589 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700590 }
591
Elliott Hughese3c845c2012-02-28 17:23:01 -0800592 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700593 while (it.HasNextStaticField()) {
594 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700595 }
Ian Rogers0571d352011-11-03 19:51:38 -0700596 while (it.HasNextInstanceField()) {
597 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700598 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800599 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700600
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700601 bool DumpOatClass(std::ostream& os, const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800602 const DexFile::ClassDef& class_def) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700603 bool success = true;
Ian Rogers13735952014-10-08 12:43:28 -0700604 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700605 if (class_data == nullptr) { // empty class such as a marker interface?
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700606 os << std::flush;
607 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800608 }
609 ClassDataItemIterator it(dex_file, class_data);
610 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700611 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700612 while (it.HasNextDirectMethod()) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700613 if (!DumpOatMethod(os, class_def, class_method_index, oat_class, dex_file,
614 it.GetMemberIndex(), it.GetMethodCodeItem(),
615 it.GetRawMemberAccessFlags())) {
616 success = false;
617 }
618 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700619 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700620 }
Ian Rogers0571d352011-11-03 19:51:38 -0700621 while (it.HasNextVirtualMethod()) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700622 if (!DumpOatMethod(os, class_def, class_method_index, oat_class, dex_file,
623 it.GetMemberIndex(), it.GetMethodCodeItem(),
624 it.GetRawMemberAccessFlags())) {
625 success = false;
626 }
627 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700628 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700629 }
Ian Rogers0571d352011-11-03 19:51:38 -0700630 DCHECK(!it.HasNext());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700631 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700632 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700633 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800634
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700635 static constexpr uint32_t kPrologueBytes = 16;
636
637 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
638 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
639
640 bool DumpOatMethod(std::ostream& os, const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700641 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700642 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800643 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
644 uint32_t method_access_flags) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700645 bool success = true;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000646 std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
647 if (pretty_method.find(options_->method_filter_) == std::string::npos) {
648 return success;
649 }
650
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800651 os << StringPrintf("%d: %s (dex_method_idx=%d)\n",
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000652 class_method_index, pretty_method.c_str(),
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700653 dex_method_idx);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800654 Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700655 std::unique_ptr<std::ostream> indent1_os(new std::ostream(&indent1_filter));
656 Indenter indent2_filter(indent1_os->rdbuf(), kIndentChar, kIndentBy1Count);
657 std::unique_ptr<std::ostream> indent2_os(new std::ostream(&indent2_filter));
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800658 {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700659 *indent1_os << "DEX CODE:\n";
660 DumpDexCode(*indent2_os, dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700661 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700662
663 std::unique_ptr<verifier::MethodVerifier> verifier;
664 if (Runtime::Current() != nullptr) {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700665 *indent1_os << "VERIFIER TYPE ANALYSIS:\n";
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700666 verifier.reset(DumpVerifier(*indent2_os, dex_method_idx, &dex_file, class_def, code_item,
667 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -0700668 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700669
670 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
671 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
672 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800673 {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700674 *indent1_os << "OatMethodOffsets ";
675 if (options_->absolute_addresses_) {
676 *indent1_os << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100677 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700678 *indent1_os << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
679 if (oat_method_offsets_offset > oat_file_.Size()) {
680 *indent1_os << StringPrintf(
681 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
682 oat_method_offsets_offset, oat_file_.Size());
683 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
684 os << std::flush;
685 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800686 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700687
688 uint32_t code_offset = oat_method.GetCodeOffset();
689 *indent2_os << StringPrintf("code_offset: 0x%08x ", code_offset);
690 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
691 if (aligned_code_begin > oat_file_.Size()) {
692 *indent2_os << StringPrintf("WARNING: "
693 "code offset 0x%08x is past end of file 0x%08zx.\n",
694 aligned_code_begin, oat_file_.Size());
695 success = false;
696 }
697 *indent2_os << "\n";
698
699 *indent2_os << "gc_map: ";
700 if (options_->absolute_addresses_) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800701 *indent2_os << StringPrintf("%p ", oat_method.GetGcMap());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700702 }
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800703 uint32_t gc_map_offset = oat_method.GetGcMapOffset();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700704 *indent2_os << StringPrintf("(offset=0x%08x)\n", gc_map_offset);
705 if (gc_map_offset > oat_file_.Size()) {
706 *indent2_os << StringPrintf("WARNING: "
707 "gc map table offset 0x%08x is past end of file 0x%08zx.\n",
708 gc_map_offset, oat_file_.Size());
709 success = false;
710 } else if (options_->dump_raw_gc_map_) {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700711 Indenter indent3_filter(indent2_os->rdbuf(), kIndentChar, kIndentBy1Count);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800712 std::ostream indent3_os(&indent3_filter);
713 DumpGcMap(indent3_os, oat_method, code_item);
714 }
715 }
716 {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700717 *indent1_os << "OatQuickMethodHeader ";
718 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
719 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700720
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700721 if (options_->absolute_addresses_) {
722 *indent1_os << StringPrintf("%p ", method_header);
723 }
724 *indent1_os << StringPrintf("(offset=0x%08x)\n", method_header_offset);
725 if (method_header_offset > oat_file_.Size()) {
726 *indent1_os << StringPrintf(
727 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
728 method_header_offset, oat_file_.Size());
729 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
730 os << std::flush;
731 return false;
732 }
733
734 *indent2_os << "mapping_table: ";
735 if (options_->absolute_addresses_) {
736 *indent2_os << StringPrintf("%p ", oat_method.GetMappingTable());
737 }
738 uint32_t mapping_table_offset = oat_method.GetMappingTableOffset();
739 *indent2_os << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset());
740 if (mapping_table_offset > oat_file_.Size()) {
741 *indent2_os << StringPrintf("WARNING: "
742 "mapping table offset 0x%08x is past end of file 0x%08zx. "
743 "mapping table offset was loaded from offset 0x%08x.\n",
744 mapping_table_offset, oat_file_.Size(),
745 oat_method.GetMappingTableOffsetOffset());
746 success = false;
747 } else if (options_->dump_raw_mapping_table_) {
748 Indenter indent3_filter(indent2_os->rdbuf(), kIndentChar, kIndentBy1Count);
749 std::ostream indent3_os(&indent3_filter);
750 DumpMappingTable(indent3_os, oat_method);
751 }
752
753 *indent2_os << "vmap_table: ";
754 if (options_->absolute_addresses_) {
755 *indent2_os << StringPrintf("%p ", oat_method.GetVmapTable());
756 }
757 uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
758 *indent2_os << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
759 if (vmap_table_offset > oat_file_.Size()) {
760 *indent2_os << StringPrintf("WARNING: "
761 "vmap table offset 0x%08x is past end of file 0x%08zx. "
762 "vmap table offset was loaded from offset 0x%08x.\n",
763 vmap_table_offset, oat_file_.Size(),
764 oat_method.GetVmapTableOffsetOffset());
765 success = false;
766 } else if (options_->dump_vmap_) {
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +0100767 DumpVmap(*indent2_os, oat_method);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700768 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800769 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700770 {
771 *indent1_os << "QuickMethodFrameInfo\n";
772
773 *indent2_os << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
774 *indent2_os << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
775 DumpSpillMask(*indent2_os, oat_method.GetCoreSpillMask(), false);
776 *indent2_os << "\n";
777 *indent2_os << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
778 DumpSpillMask(*indent2_os, oat_method.GetFpSpillMask(), true);
779 *indent2_os << "\n";
780 }
781 {
782 // Based on spill masks from QuickMethodFrameInfo so placed
783 // after it is dumped, but useful for understanding quick
784 // code, so dumped here.
785 DumpVregLocations(*indent2_os, oat_method, code_item);
786 }
787 {
788 *indent1_os << "CODE: ";
789 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
790 if (code_size_offset > oat_file_.Size()) {
791 *indent2_os << StringPrintf("WARNING: "
792 "code size offset 0x%08x is past end of file 0x%08zx.",
793 code_size_offset, oat_file_.Size());
794 success = false;
795 } else {
796 const void* code = oat_method.GetQuickCode();
797 uint32_t code_size = oat_method.GetQuickCodeSize();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700798 uint32_t code_offset = oat_method.GetCodeOffset();
799 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
800 uint64_t aligned_code_end = aligned_code_begin + code_size;
801
802 if (options_->absolute_addresses_) {
803 *indent1_os << StringPrintf("%p ", code);
804 }
805 *indent1_os << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
806 code_offset,
807 code_size_offset,
808 code_size,
809 code != nullptr ? "..." : "");
810
811 if (aligned_code_begin > oat_file_.Size()) {
812 *indent2_os << StringPrintf("WARNING: "
813 "start of code at 0x%08x is past end of file 0x%08zx.",
814 aligned_code_begin, oat_file_.Size());
815 success = false;
816 } else if (aligned_code_end > oat_file_.Size()) {
817 *indent2_os << StringPrintf("WARNING: "
818 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
819 "code size is 0x%08x loaded from offset 0x%08x.\n",
820 aligned_code_end, oat_file_.Size(),
821 code_size, code_size_offset);
822 success = false;
823 if (options_->disassemble_code_) {
824 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
825 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, true, kPrologueBytes);
826 }
827 }
828 } else if (code_size > kMaxCodeSize) {
829 *indent2_os << StringPrintf("WARNING: "
830 "code size %d is bigger than max expected threshold of %d. "
831 "code size is 0x%08x loaded from offset 0x%08x.\n",
832 code_size, kMaxCodeSize,
833 code_size, code_size_offset);
834 success = false;
835 if (options_->disassemble_code_) {
836 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
837 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, true, kPrologueBytes);
838 }
839 }
840 } else if (options_->disassemble_code_) {
841 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, !success, 0);
842 }
843 }
844 }
845 os << std::flush;
846 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700847 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800848
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800849 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
850 if (spill_mask == 0) {
851 return;
852 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800853 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800854 for (size_t i = 0; i < 32; i++) {
855 if ((spill_mask & (1 << i)) != 0) {
856 if (is_float) {
857 os << "fr" << i;
858 } else {
859 os << "r" << i;
860 }
861 spill_mask ^= 1 << i; // clear bit
862 if (spill_mask != 0) {
863 os << ", ";
864 } else {
865 break;
866 }
867 }
868 }
869 os << ")";
870 }
871
Ian Rogersb23a7722012-10-09 16:54:26 -0700872 void DumpVmap(std::ostream& os, const OatFile::OatMethod& oat_method) {
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +0100873 // If the native GC map is null, then this method has been compiled with the
874 // optimizing compiler. The optimizing compiler currently outputs its stack map
875 // in the vmap table, and the code below does not work with such a stack map.
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800876 if (oat_method.GetGcMap() == nullptr) {
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +0100877 return;
878 }
Ian Rogers1809a722013-08-09 22:05:32 -0700879 const uint8_t* raw_table = oat_method.GetVmapTable();
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700880 if (raw_table != nullptr) {
Ian Rogers1809a722013-08-09 22:05:32 -0700881 const VmapTable vmap_table(raw_table);
882 bool first = true;
883 bool processing_fp = false;
884 uint32_t spill_mask = oat_method.GetCoreSpillMask();
885 for (size_t i = 0; i < vmap_table.Size(); i++) {
886 uint16_t dex_reg = vmap_table[i];
887 uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
888 processing_fp ? kFloatVReg : kIntVReg);
889 os << (first ? "v" : ", v") << dex_reg;
890 if (!processing_fp) {
891 os << "/r" << cpu_reg;
892 } else {
893 os << "/fr" << cpu_reg;
894 }
895 first = false;
896 if (!processing_fp && dex_reg == 0xFFFF) {
897 processing_fp = true;
898 spill_mask = oat_method.GetFpSpillMask();
899 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800900 }
Ian Rogers1809a722013-08-09 22:05:32 -0700901 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800902 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800903 }
904
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -0700905 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
906 const DexFile::CodeItem* code_item) {
907 if (code_item != nullptr) {
908 size_t num_locals_ins = code_item->registers_size_;
909 size_t num_ins = code_item->ins_size_;
910 size_t num_locals = num_locals_ins - num_ins;
911 size_t num_outs = code_item->outs_size_;
912
913 os << "vr_stack_locations:";
914 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
915 // For readability, delimit the different kinds of VRs.
916 if (reg == num_locals_ins) {
917 os << "\n\tmethod*:";
918 } else if (reg == num_locals && num_ins > 0) {
919 os << "\n\tins:";
920 } else if (reg == 0 && num_locals > 0) {
921 os << "\n\tlocals:";
922 }
923
924 uint32_t offset = StackVisitor::GetVRegOffset(code_item, oat_method.GetCoreSpillMask(),
925 oat_method.GetFpSpillMask(),
926 oat_method.GetFrameSizeInBytes(), reg,
927 GetInstructionSet());
928 os << " v" << reg << "[sp + #" << offset << "]";
929 }
930
931 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
932 if (out_reg == 0) {
933 os << "\n\touts:";
934 }
935
936 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
937 os << " v" << out_reg << "[sp + #" << offset << "]";
938 }
939
940 os << "\n";
941 }
942 }
943
Ian Rogersb23a7722012-10-09 16:54:26 -0700944 void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800945 const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
Ian Rogers1809a722013-08-09 22:05:32 -0700946 const uint8_t* raw_table = oat_method.GetVmapTable();
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700947 if (raw_table != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -0700948 const VmapTable vmap_table(raw_table);
949 uint32_t vmap_offset;
Ian Rogers1809a722013-08-09 22:05:32 -0700950 if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800951 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
952 uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
953 : oat_method.GetCoreSpillMask();
954 os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
Ian Rogersb23a7722012-10-09 16:54:26 -0700955 } else {
956 uint32_t offset = StackVisitor::GetVRegOffset(code_item, oat_method.GetCoreSpillMask(),
957 oat_method.GetFpSpillMask(),
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000958 oat_method.GetFrameSizeInBytes(), reg,
959 GetInstructionSet());
Ian Rogersb23a7722012-10-09 16:54:26 -0700960 os << "[sp + #" << offset << "]";
961 }
962 }
963 }
964
Ian Rogersef7d42f2014-01-06 12:55:46 -0800965 void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
966 const DexFile::CodeItem* code_item,
967 size_t num_regs, const uint8_t* reg_bitmap) {
968 bool first = true;
969 for (size_t reg = 0; reg < num_regs; reg++) {
970 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
971 if (first) {
972 os << " v" << reg << " (";
973 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
974 os << ")";
975 first = false;
976 } else {
977 os << ", v" << reg << " (";
978 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
979 os << ")";
980 }
981 }
982 }
983 if (first) {
984 os << "No registers in GC map\n";
985 } else {
986 os << "\n";
987 }
988 }
Ian Rogersb23a7722012-10-09 16:54:26 -0700989 void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
990 const DexFile::CodeItem* code_item) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800991 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800992 if (gc_map_raw == nullptr) {
993 return; // No GC map.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800994 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800995 const void* quick_code = oat_method.GetQuickCode();
Elliott Hughes956af0f2014-12-11 14:34:28 -0800996 NativePcOffsetToReferenceMap map(gc_map_raw);
997 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
998 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
999 map.GetNativePcOffset(entry);
1000 os << StringPrintf("%p", native_pc);
1001 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001002 }
1003 }
1004
1005 void DumpMappingTable(std::ostream& os, const OatFile::OatMethod& oat_method) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001006 const void* quick_code = oat_method.GetQuickCode();
1007 if (quick_code == nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -08001008 return;
1009 }
Ian Rogers1809a722013-08-09 22:05:32 -07001010 MappingTable table(oat_method.GetMappingTable());
1011 if (table.TotalSize() != 0) {
1012 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1013 std::ostream indent_os(&indent_filter);
1014 if (table.PcToDexSize() != 0) {
1015 typedef MappingTable::PcToDexIterator It;
1016 os << "suspend point mappings {\n";
1017 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1018 indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
1019 }
1020 os << "}\n";
1021 }
1022 if (table.DexToPcSize() != 0) {
1023 typedef MappingTable::DexToPcIterator It;
1024 os << "catch entry mappings {\n";
1025 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1026 indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
1027 }
1028 os << "}\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001029 }
1030 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001031 }
1032
Ian Rogers1809a722013-08-09 22:05:32 -07001033 uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1034 size_t offset, bool suspend_point_mapping) {
1035 MappingTable table(oat_method.GetMappingTable());
1036 if (suspend_point_mapping && table.PcToDexSize() > 0) {
1037 typedef MappingTable::PcToDexIterator It;
1038 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1039 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001040 os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001041 return cur.DexPc();
1042 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001043 }
Ian Rogers1809a722013-08-09 22:05:32 -07001044 } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
1045 typedef MappingTable::DexToPcIterator It;
1046 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1047 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001048 os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001049 return cur.DexPc();
Ian Rogersb23a7722012-10-09 16:54:26 -07001050 }
1051 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001052 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001053 return DexFile::kDexNoIndex;
Ian Rogersb23a7722012-10-09 16:54:26 -07001054 }
1055
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001056 void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1057 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001058 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001059 if (gc_map_raw != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001060 NativePcOffsetToReferenceMap map(gc_map_raw);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001061 if (map.HasEntry(native_pc_offset)) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001062 size_t num_regs = map.RegWidth() * 8;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001063 const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
Ian Rogersb23a7722012-10-09 16:54:26 -07001064 bool first = true;
1065 for (size_t reg = 0; reg < num_regs; reg++) {
1066 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1067 if (first) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001068 os << "GC map objects: v" << reg << " (";
1069 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001070 os << ")";
1071 first = false;
1072 } else {
1073 os << ", v" << reg << " (";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001074 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001075 os << ")";
1076 }
1077 }
1078 }
1079 if (!first) {
1080 os << "\n";
1081 }
1082 }
1083 }
1084 }
1085
Mathieu Chartier590fee92013-09-13 13:46:47 -07001086 void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
1087 const OatFile::OatMethod& oat_method,
1088 const DexFile::CodeItem* code_item, uint32_t dex_pc) {
1089 DCHECK(verifier != nullptr);
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001090 std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001091 bool first = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001092 for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
1093 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
1094 if (kind != kUndefined) {
1095 if (first) {
1096 os << "VRegs: v";
1097 first = false;
1098 } else {
1099 os << ", v";
1100 }
1101 os << reg << " (";
1102 switch (kind) {
1103 case kImpreciseConstant:
1104 os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
1105 DescribeVReg(os, oat_method, code_item, reg, kind);
1106 break;
1107 case kConstant:
1108 os << "Constant: " << kinds.at((reg * 2) + 1);
1109 break;
1110 default:
1111 DescribeVReg(os, oat_method, code_item, reg, kind);
1112 break;
1113 }
1114 os << ")";
1115 }
1116 }
1117 if (!first) {
1118 os << "\n";
1119 }
1120 }
1121
1122
Ian Rogersb23a7722012-10-09 16:54:26 -07001123 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001124 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001125 size_t i = 0;
1126 while (i < code_item->insns_size_in_code_units_) {
1127 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001128 os << StringPrintf("0x%04zx: %s\n", i, instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001129 i += instruction->SizeInCodeUnits();
1130 }
1131 }
1132 }
1133
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001134 verifier::MethodVerifier* DumpVerifier(std::ostream& os, uint32_t dex_method_idx,
1135 const DexFile* dex_file,
1136 const DexFile::ClassDef& class_def,
1137 const DexFile::CodeItem* code_item,
1138 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001139 if ((method_access_flags & kAccNative) == 0) {
1140 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001141 StackHandleScope<1> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001142 Handle<mirror::DexCache> dex_cache(
1143 hs.NewHandle(Runtime::Current()->GetClassLinker()->FindDexCache(*dex_file)));
Andreas Gampe00b25f32014-09-17 21:49:05 -07001144 DCHECK(options_->class_loader_ != nullptr);
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001145 return verifier::MethodVerifier::VerifyMethodAndDump(soa.Self(), os, dex_method_idx, dex_file,
1146 dex_cache,
Andreas Gampe00b25f32014-09-17 21:49:05 -07001147 *options_->class_loader_,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001148 &class_def, code_item,
1149 NullHandle<mirror::ArtMethod>(),
1150 method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001151 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001152
1153 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001154 }
1155
Mathieu Chartier590fee92013-09-13 13:46:47 -07001156 void DumpCode(std::ostream& os, verifier::MethodVerifier* verifier,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001157 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1158 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001159 const void* quick_code = oat_method.GetQuickCode();
1160
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001161 if (code_size == 0) {
1162 code_size = oat_method.GetQuickCodeSize();
1163 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001164 if (code_size == 0 || quick_code == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001165 os << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001166 return;
Elliott Hughes956af0f2014-12-11 14:34:28 -08001167 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001168 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1169 size_t offset = 0;
1170 while (offset < code_size) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001171 if (!bad_input) {
1172 DumpMappingAtOffset(os, oat_method, offset, false);
1173 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001174 offset += disassembler_->Dump(os, quick_native_pc + offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001175 if (!bad_input) {
1176 uint32_t dex_pc = DumpMappingAtOffset(os, oat_method, offset, true);
1177 if (dex_pc != DexFile::kDexNoIndex) {
1178 DumpGcMapAtNativePcOffset(os, oat_method, code_item, offset);
1179 if (verifier != nullptr) {
1180 DumpVRegsAtDexPc(os, verifier, oat_method, code_item, dex_pc);
1181 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001182 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001183 }
1184 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001185 }
1186 }
1187
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001188 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001189 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
1190 const OatDumperOptions* options_;
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001191 InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001192 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001193 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001194};
1195
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001196class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001197 public:
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001198 explicit ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001199 const ImageHeader& image_header, OatDumperOptions* oat_dumper_options)
1200 : os_(os),
1201 image_space_(image_space),
1202 image_header_(image_header),
1203 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001204
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001205 bool Dump() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001206 std::ostream& os = *os_;
1207 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001208
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001209 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001210
Mathieu Chartier31e89252013-08-28 11:29:12 -07001211 os << "IMAGE BITMAP OFFSET: " << reinterpret_cast<void*>(image_header_.GetImageBitmapOffset())
1212 << " SIZE: " << reinterpret_cast<void*>(image_header_.GetImageBitmapSize()) << "\n\n";
1213
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001214 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001215
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001216 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001217
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001218 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1219
1220 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1221
1222 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001223
Alex Lighta59dd802014-07-02 16:28:08 -07001224 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1225
Igor Murashkin46774762014-10-22 11:37:02 -07001226 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1227
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001228 {
1229 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
1230 Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1231 std::ostream indent1_os(&indent1_filter);
1232 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
1233 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1234 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1235 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001236 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001237 indent1_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
1238 if (image_root_object->IsObjectArray()) {
1239 Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
1240 std::ostream indent2_os(&indent2_filter);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001241 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001242 = image_root_object->AsObjectArray<mirror::Object>();
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001243 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1244 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001245 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001246 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1247 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001248 run++;
1249 } else {
1250 break;
1251 }
1252 }
1253 if (run == 0) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001254 indent2_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001255 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001256 indent2_os << StringPrintf("%d to %zd: ", j, j + run);
1257 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001258 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001259 if (value != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001260 PrettyObjectValue(indent2_os, value->GetClass(), value);
1261 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001262 indent2_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001263 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001264 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001265 }
1266 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001267 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001268 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001269
Brian Carlstromaded5f72011-10-07 17:15:04 -07001270 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001271 std::string image_filename = image_space_.GetImageFilename();
1272 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001273 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001274 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001275 std::string error_msg;
Alex Lighta59dd802014-07-02 16:28:08 -07001276 const OatFile* oat_file = class_linker->FindOpenedOatFileFromOatLocation(oat_location);
1277 if (oat_file == nullptr) {
Igor Murashkin46774762014-10-22 11:37:02 -07001278 oat_file = OatFile::Open(oat_location, oat_location, nullptr, nullptr, false, &error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -07001279 if (oat_file == nullptr) {
1280 os << "NOT FOUND: " << error_msg << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001281 return false;
Alex Lighta59dd802014-07-02 16:28:08 -07001282 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001283 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001284 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001285
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001286 stats_.oat_file_bytes = oat_file->Size();
1287
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001288 oat_dumper_.reset(new OatDumper(*oat_file, oat_dumper_options_.release()));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001289
Mathieu Chartier02e25112013-08-14 16:14:24 -07001290 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001291 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001292 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1293 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001294 }
1295
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001296 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001297
1298 // Loop through all the image spaces and dump their objects.
Ian Rogers1d54e732013-05-02 21:10:01 -07001299 gc::Heap* heap = Runtime::Current()->GetHeap();
1300 const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces();
Ian Rogers50b35e22012-10-04 10:09:15 -07001301 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001302 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001303 {
1304 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1305 heap->FlushAllocStack();
1306 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001307 // Since FlushAllocStack() above resets the (active) allocation
1308 // stack. Need to revoke the thread-local allocation stacks that
1309 // point into it.
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001310 {
1311 self->TransitionFromRunnableToSuspended(kNative);
1312 ThreadList* thread_list = Runtime::Current()->GetThreadList();
1313 thread_list->SuspendAll();
1314 heap->RevokeAllThreadLocalAllocationStacks(self);
1315 thread_list->ResumeAll();
1316 self->TransitionFromSuspendedToRunnable();
1317 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001318 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001319 {
1320 std::ostream* saved_os = os_;
1321 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1322 std::ostream indent_os(&indent_filter);
1323 os_ = &indent_os;
1324 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001325 for (const auto& space : spaces) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001326 if (space->IsImageSpace()) {
Ian Rogers1d54e732013-05-02 21:10:01 -07001327 gc::space::ImageSpace* image_space = space->AsImageSpace();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001328 image_space->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1329 indent_os << "\n";
1330 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -07001331 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001332 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001333 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001334 indent_os << "\n";
1335 os_ = saved_os;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001336 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001337 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001338 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001339 if (file.get() == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001340 LOG(WARNING) << "Failed to find image in " << image_filename;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001341 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001342 if (file.get() != nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001343 stats_.file_bytes = file->GetLength();
Brian Carlstrom6f277752013-09-30 17:56:45 -07001344 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001345 size_t header_bytes = sizeof(ImageHeader);
1346 stats_.header_bytes = header_bytes;
1347 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
1348 stats_.alignment_bytes += alignment_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07001349 stats_.alignment_bytes += image_header_.GetImageBitmapOffset() - image_header_.GetImageSize();
1350 stats_.bitmap_bytes += image_header_.GetImageBitmapSize();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001351 stats_.Dump(os);
1352 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001353
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001354 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001355
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001356 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001357 }
1358
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001359 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001360 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001361 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001362 CHECK(type != nullptr);
1363 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001364 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001365 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001366 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001367 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001368 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001369 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001370 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001371 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Brian Carlstromea46f952013-07-30 01:26:50 -07001372 } else if (type->IsArtFieldClass()) {
1373 mirror::ArtField* field = value->AsArtField();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001374 os << StringPrintf("%p Field: %s\n", field, PrettyField(field).c_str());
Brian Carlstromea46f952013-07-30 01:26:50 -07001375 } else if (type->IsArtMethodClass()) {
1376 mirror::ArtMethod* method = value->AsArtMethod();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001377 os << StringPrintf("%p Method: %s\n", method, PrettyMethod(method).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001378 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001379 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001380 }
1381 }
1382
Brian Carlstromea46f952013-07-30 01:26:50 -07001383 static void PrintField(std::ostream& os, mirror::ArtField* field, mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001384 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001385 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08001386 switch (field->GetTypeAsPrimitiveType()) {
1387 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08001388 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001389 break;
1390 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001391 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001392 break;
1393 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001394 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001395 break;
1396 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001397 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001398 break;
1399 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07001400 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001401 break;
1402 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07001403 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001404 break;
1405 case Primitive::kPrimBoolean:
Fred Shih37f05ef2014-07-16 18:38:08 -07001406 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1407 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001408 break;
1409 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07001410 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001411 break;
1412 case Primitive::kPrimNot: {
1413 // Get the value, don't compute the type unless it is non-null as we don't want
1414 // to cause class loading.
1415 mirror::Object* value = field->GetObj(obj);
1416 if (value == nullptr) {
1417 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07001418 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08001419 // Grab the field type without causing resolution.
1420 mirror::Class* field_type = field->GetType(false);
1421 if (field_type != nullptr) {
1422 PrettyObjectValue(os, field_type, value);
1423 } else {
1424 os << StringPrintf("%p %s\n", value,
1425 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
1426 }
Ian Rogers50239c72013-06-17 14:53:22 -07001427 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001428 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07001429 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001430 default:
1431 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
1432 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08001433 }
1434 }
1435
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001436 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001437 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001438 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001439 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001440 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001441 }
Brian Carlstromea46f952013-07-30 01:26:50 -07001442 mirror::ObjectArray<mirror::ArtField>* fields = klass->GetIFields();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001443 if (fields != nullptr) {
Ian Rogersd5b32602012-02-26 16:40:04 -08001444 for (int32_t i = 0; i < fields->GetLength(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001445 mirror::ArtField* field = fields->Get(i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001446 PrintField(os, field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001447 }
1448 }
1449 }
1450
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001451 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001452 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001453 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001454
Ian Rogersef7d42f2014-01-06 12:55:46 -08001455 const void* GetQuickOatCodeBegin(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001456 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001457 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
1458 InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet()));
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001459 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001460 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001461 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001462 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001463 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001464 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001465 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001466 }
1467
Ian Rogersef7d42f2014-01-06 12:55:46 -08001468 uint32_t GetQuickOatCodeSize(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001469 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001470 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1471 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001472 return 0;
1473 }
1474 return oat_code_begin[-1];
1475 }
1476
Ian Rogersef7d42f2014-01-06 12:55:46 -08001477 const void* GetQuickOatCodeEnd(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001478 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001479 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001480 if (oat_code_begin == nullptr) {
1481 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001482 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001483 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001484 }
1485
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001486 static void Callback(mirror::Object* obj, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001487 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001488 DCHECK(obj != nullptr);
1489 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001490 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001491 if (!state->InDumpSpace(obj)) {
1492 return;
1493 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001494
1495 size_t object_bytes = obj->SizeOf();
1496 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1497 state->stats_.object_bytes += object_bytes;
1498 state->stats_.alignment_bytes += alignment_bytes;
1499
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001500 std::ostream& os = *state->os_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001501 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001502 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001503 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
1504 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001505 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001506 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001507 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
1508 << klass->GetStatus() << ")\n";
Brian Carlstromea46f952013-07-30 01:26:50 -07001509 } else if (obj->IsArtField()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001510 os << StringPrintf("%p: java.lang.reflect.ArtField %s\n", obj,
Brian Carlstromea46f952013-07-30 01:26:50 -07001511 PrettyField(obj->AsArtField()).c_str());
1512 } else if (obj->IsArtMethod()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001513 os << StringPrintf("%p: java.lang.reflect.ArtMethod %s\n", obj,
Brian Carlstromea46f952013-07-30 01:26:50 -07001514 PrettyMethod(obj->AsArtMethod()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001515 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001516 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001517 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001518 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001519 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001520 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001521 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1522 std::ostream indent_os(&indent_filter);
1523 DumpFields(indent_os, obj, obj_class);
Ian Rogersd5b32602012-02-26 16:40:04 -08001524 if (obj->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001525 mirror::ObjectArray<mirror::Object>* obj_array = obj->AsObjectArray<mirror::Object>();
Ian Rogersd5b32602012-02-26 16:40:04 -08001526 int32_t length = obj_array->GetLength();
1527 for (int32_t i = 0; i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001528 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001529 size_t run = 0;
1530 for (int32_t j = i + 1; j < length; j++) {
1531 if (value == obj_array->Get(j)) {
1532 run++;
1533 } else {
1534 break;
1535 }
1536 }
1537 if (run == 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001538 indent_os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001539 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001540 indent_os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001541 i = i + run;
1542 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001543 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001544 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001545 PrettyObjectValue(indent_os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001546 }
1547 } else if (obj->IsClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001548 mirror::ObjectArray<mirror::ArtField>* sfields = obj->AsClass()->GetSFields();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001549 if (sfields != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001550 indent_os << "STATICS:\n";
1551 Indenter indent2_filter(indent_os.rdbuf(), kIndentChar, kIndentBy1Count);
1552 std::ostream indent2_os(&indent2_filter);
Ian Rogersd5b32602012-02-26 16:40:04 -08001553 for (int32_t i = 0; i < sfields->GetLength(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001554 mirror::ArtField* field = sfields->Get(i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001555 PrintField(indent2_os, field, field->GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001556 }
1557 }
Brian Carlstromea46f952013-07-30 01:26:50 -07001558 } else if (obj->IsArtMethod()) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001559 const size_t image_pointer_size = InstructionSetPointerSize(
1560 state->oat_dumper_->GetOatInstructionSet());
Brian Carlstromea46f952013-07-30 01:26:50 -07001561 mirror::ArtMethod* method = obj->AsArtMethod();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001562 if (method->IsNative()) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001563 DCHECK(method->GetNativeGcMap(image_pointer_size) == nullptr) << PrettyMethod(method);
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001564 DCHECK(method->GetMappingTable(image_pointer_size) == nullptr) << PrettyMethod(method);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001565 bool first_occurrence;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001566 const void* quick_oat_code = state->GetQuickOatCodeBegin(method);
1567 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1568 state->ComputeOatSize(quick_oat_code, &first_occurrence);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001569 if (first_occurrence) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001570 state->stats_.native_to_managed_code_bytes += quick_oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001571 }
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001572 if (quick_oat_code != method->GetEntryPointFromQuickCompiledCodePtrSize(
1573 image_pointer_size)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001574 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001575 }
Ian Rogers19846512012-02-24 11:42:47 -08001576 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
Jeff Hao88474b42013-10-23 16:24:40 -07001577 method->IsResolutionMethod() || method->IsImtConflictMethod() ||
Mathieu Chartier2d2621a2014-10-23 16:48:06 -07001578 method->IsImtUnimplementedMethod() || method->IsClassInitializer()) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001579 DCHECK(method->GetNativeGcMap(image_pointer_size) == nullptr) << PrettyMethod(method);
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001580 DCHECK(method->GetMappingTable(image_pointer_size) == nullptr) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001581 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001582 const DexFile::CodeItem* code_item = method->GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -07001583 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001584 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001585
Elliott Hughesa0e18062012-04-13 15:59:59 -07001586 bool first_occurrence;
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001587 size_t gc_map_bytes =
1588 state->ComputeOatSize(method->GetNativeGcMap(image_pointer_size), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001589 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001590 state->stats_.gc_map_bytes += gc_map_bytes;
1591 }
1592
1593 size_t pc_mapping_table_bytes =
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001594 state->ComputeOatSize(method->GetMappingTable(image_pointer_size), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001595 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001596 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
1597 }
1598
1599 size_t vmap_table_bytes =
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001600 state->ComputeOatSize(method->GetVmapTable(image_pointer_size), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001601 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001602 state->stats_.vmap_table_bytes += vmap_table_bytes;
1603 }
1604
Ian Rogersef7d42f2014-01-06 12:55:46 -08001605 const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method);
1606 const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method);
1607 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1608 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001609 if (first_occurrence) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001610 state->stats_.managed_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001611 if (method->IsConstructor()) {
1612 if (method->IsStatic()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001613 state->stats_.class_initializer_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001614 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001615 state->stats_.large_initializer_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001616 }
1617 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001618 state->stats_.large_method_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001619 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001620 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001621 state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001622
Ian Rogersef7d42f2014-01-06 12:55:46 -08001623 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001624 indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
1625 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001626
1627 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
Ian Rogersef7d42f2014-01-06 12:55:46 -08001628 vmap_table_bytes + quick_oat_code_size + object_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001629
1630 double expansion =
Ian Rogersef7d42f2014-01-06 12:55:46 -08001631 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001632 state->stats_.ComputeOutliers(total_size, expansion, method);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001633 }
1634 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07001635 std::string temp;
1636 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001637 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001638
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001639 std::set<const void*> already_seen_;
1640 // Compute the size of the given data within the oat file and whether this is the first time
1641 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07001642 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001643 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001644 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001645 already_seen_.insert(oat_data);
1646 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001647 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001648 }
1649 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001650 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001651
1652 public:
1653 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001654 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001655 size_t file_bytes;
1656
1657 size_t header_bytes;
1658 size_t object_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07001659 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001660 size_t alignment_bytes;
1661
1662 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001663 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001664 size_t managed_to_native_code_bytes;
1665 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001666 size_t class_initializer_code_bytes;
1667 size_t large_initializer_code_bytes;
1668 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001669
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001670 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001671 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001672 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001673
1674 size_t dex_instruction_bytes;
1675
Brian Carlstromea46f952013-07-30 01:26:50 -07001676 std::vector<mirror::ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001677 std::vector<size_t> method_outlier_size;
1678 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07001679 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001680
1681 explicit Stats()
1682 : oat_file_bytes(0),
1683 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001684 header_bytes(0),
1685 object_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07001686 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001687 alignment_bytes(0),
1688 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001689 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001690 managed_to_native_code_bytes(0),
1691 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07001692 class_initializer_code_bytes(0),
1693 large_initializer_code_bytes(0),
1694 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001695 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001696 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001697 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001698 dex_instruction_bytes(0) {}
1699
Elliott Hughesa0e18062012-04-13 15:59:59 -07001700 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001701 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07001702 size_t bytes;
1703 size_t count;
1704 };
1705 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
1706 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001707
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001708 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001709 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
1710 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001711 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07001712 it->second.count += 1;
1713 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001714 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07001715 }
1716 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001717
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001718 double PercentOfOatBytes(size_t size) {
1719 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
1720 }
1721
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001722 double PercentOfFileBytes(size_t size) {
1723 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
1724 }
1725
1726 double PercentOfObjectBytes(size_t size) {
1727 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
1728 }
1729
Brian Carlstromea46f952013-07-30 01:26:50 -07001730 void ComputeOutliers(size_t total_size, double expansion, mirror::ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001731 method_outlier_size.push_back(total_size);
1732 method_outlier_expansion.push_back(expansion);
1733 method_outlier.push_back(method);
1734 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001735
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001736 void DumpOutliers(std::ostream& os)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001737 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001738 size_t sum_of_sizes = 0;
1739 size_t sum_of_sizes_squared = 0;
1740 size_t sum_of_expansion = 0;
1741 size_t sum_of_expansion_squared = 0;
1742 size_t n = method_outlier_size.size();
1743 for (size_t i = 0; i < n; i++) {
1744 size_t cur_size = method_outlier_size[i];
1745 sum_of_sizes += cur_size;
1746 sum_of_sizes_squared += cur_size * cur_size;
1747 double cur_expansion = method_outlier_expansion[i];
1748 sum_of_expansion += cur_expansion;
1749 sum_of_expansion_squared += cur_expansion * cur_expansion;
1750 }
1751 size_t size_mean = sum_of_sizes / n;
1752 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
1753 double expansion_mean = sum_of_expansion / n;
1754 double expansion_variance =
1755 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
1756
1757 // Dump methods whose size is a certain number of standard deviations from the mean
1758 size_t dumped_values = 0;
1759 size_t skipped_values = 0;
1760 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
1761 size_t cur_size_variance = i * i * size_variance;
1762 bool first = true;
1763 for (size_t j = 0; j < n; j++) {
1764 size_t cur_size = method_outlier_size[j];
1765 if (cur_size > size_mean) {
1766 size_t cur_var = cur_size - size_mean;
1767 cur_var = cur_var * cur_var;
1768 if (cur_var > cur_size_variance) {
1769 if (dumped_values > 20) {
1770 if (i == 1) {
1771 skipped_values++;
1772 } else {
1773 i = 2; // jump to counting for 1 standard deviation
1774 break;
1775 }
1776 } else {
1777 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001778 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001779 first = false;
1780 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001781 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07001782 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001783 method_outlier_size[j] = 0; // don't consider this method again
1784 dumped_values++;
1785 }
1786 }
1787 }
1788 }
1789 }
1790 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001791 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001792 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001793 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001794 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001795
1796 // Dump methods whose expansion is a certain number of standard deviations from the mean
1797 dumped_values = 0;
1798 skipped_values = 0;
1799 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
1800 double cur_expansion_variance = i * i * expansion_variance;
1801 bool first = true;
1802 for (size_t j = 0; j < n; j++) {
1803 double cur_expansion = method_outlier_expansion[j];
1804 if (cur_expansion > expansion_mean) {
1805 size_t cur_var = cur_expansion - expansion_mean;
1806 cur_var = cur_var * cur_var;
1807 if (cur_var > cur_expansion_variance) {
1808 if (dumped_values > 20) {
1809 if (i == 1) {
1810 skipped_values++;
1811 } else {
1812 i = 2; // jump to counting for 1 standard deviation
1813 break;
1814 }
1815 } else {
1816 if (first) {
1817 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07001818 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001819 first = false;
1820 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001821 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07001822 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001823 method_outlier_expansion[j] = 0.0; // don't consider this method again
1824 dumped_values++;
1825 }
1826 }
1827 }
1828 }
1829 }
1830 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001831 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001832 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001833 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001834 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001835 }
1836
Ian Rogersb726dcb2012-09-05 08:57:23 -07001837 void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001838 {
1839 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
1840 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
1841 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1842 std::ostream indent_os(&indent_filter);
1843 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
1844 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier32327092013-08-30 14:04:08 -07001845 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001846 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
1847 header_bytes, PercentOfFileBytes(header_bytes),
1848 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07001849 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001850 alignment_bytes, PercentOfFileBytes(alignment_bytes))
1851 << std::flush;
Mathieu Chartier32327092013-08-30 14:04:08 -07001852 CHECK_EQ(file_bytes, bitmap_bytes + header_bytes + object_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001853 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001854
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001855 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001856 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07001857 for (const auto& sizes_and_count : sizes_and_counts) {
1858 const std::string& descriptor(sizes_and_count.first);
1859 double average = static_cast<double>(sizes_and_count.second.bytes) /
1860 static_cast<double>(sizes_and_count.second.count);
1861 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001862 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07001863 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07001864 descriptor.c_str(), sizes_and_count.second.bytes,
1865 sizes_and_count.second.count, average, percent);
1866 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001867 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001868 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001869 CHECK_EQ(object_bytes, object_bytes_total);
1870
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001871 os << StringPrintf("oat_file_bytes = %8zd\n"
1872 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1873 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1874 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
1875 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1876 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1877 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07001878 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001879 managed_code_bytes,
1880 PercentOfOatBytes(managed_code_bytes),
1881 managed_to_native_code_bytes,
1882 PercentOfOatBytes(managed_to_native_code_bytes),
1883 native_to_managed_code_bytes,
1884 PercentOfOatBytes(native_to_managed_code_bytes),
1885 class_initializer_code_bytes,
1886 PercentOfOatBytes(class_initializer_code_bytes),
1887 large_initializer_code_bytes,
1888 PercentOfOatBytes(large_initializer_code_bytes),
1889 large_method_code_bytes,
1890 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001891 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07001892 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001893 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07001894 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
1895 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07001896 }
1897
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001898 os << "\n" << StringPrintf("gc_map_bytes = %7zd (%2.0f%% of oat file bytes)\n"
1899 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
1900 "vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07001901 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
1902 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
1903 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001904 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001905
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001906 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
1907 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001908 static_cast<double>(managed_code_bytes) /
1909 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07001910 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07001911 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001912 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001913
1914 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001915 }
1916 } stats_;
1917
1918 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07001919 enum {
1920 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
1921 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1922 kLargeConstructorDexBytes = 4000,
1923 // Number of bytes for a method to be considered large. Based on the 4000 basic block
1924 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1925 kLargeMethodDexBytes = 16000
1926 };
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001927 std::ostream* os_;
Ian Rogers1d54e732013-05-02 21:10:01 -07001928 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001929 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001930 std::unique_ptr<OatDumper> oat_dumper_;
1931 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07001932
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001933 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001934};
1935
Andreas Gampe00b25f32014-09-17 21:49:05 -07001936static int DumpImage(Runtime* runtime, const char* image_location, OatDumperOptions* options,
1937 std::ostream* os) {
1938 // Dumping the image, no explicit class loader.
1939 NullHandle<mirror::ClassLoader> null_class_loader;
1940 options->class_loader_ = &null_class_loader;
1941
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001942 ScopedObjectAccess soa(Thread::Current());
Andreas Gampe00b25f32014-09-17 21:49:05 -07001943 gc::Heap* heap = runtime->GetHeap();
Ian Rogers1d54e732013-05-02 21:10:01 -07001944 gc::space::ImageSpace* image_space = heap->GetImageSpace();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001945 CHECK(image_space != nullptr);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001946 const ImageHeader& image_header = image_space->GetImageHeader();
1947 if (!image_header.IsValid()) {
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07001948 fprintf(stderr, "Invalid image header %s\n", image_location);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001949 return EXIT_FAILURE;
1950 }
Igor Murashkin37743352014-11-13 14:38:00 -08001951
Andreas Gampe00b25f32014-09-17 21:49:05 -07001952 ImageDumper image_dumper(os, *image_space, image_header, options);
Igor Murashkin37743352014-11-13 14:38:00 -08001953
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001954 bool success = image_dumper.Dump();
1955 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001956}
1957
Andreas Gampe00b25f32014-09-17 21:49:05 -07001958static int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
1959 std::ostream* os) {
1960 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
1961
1962 Thread* self = Thread::Current();
1963 CHECK(self != nullptr);
1964 // Need well-known-classes.
1965 WellKnownClasses::Init(self->GetJniEnv());
1966
1967 // Need to register dex files to get a working dex cache.
1968 ScopedObjectAccess soa(self);
1969 ClassLinker* class_linker = runtime->GetClassLinker();
1970 class_linker->RegisterOatFile(oat_file);
Richard Uhlerfbef44d2014-12-23 09:48:51 -08001971 std::vector<std::unique_ptr<const DexFile>> dex_files;
Andreas Gampe00b25f32014-09-17 21:49:05 -07001972 for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
1973 std::string error_msg;
Richard Uhlerfbef44d2014-12-23 09:48:51 -08001974 std::unique_ptr<const DexFile> dex_file = odf->OpenDexFile(&error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07001975 CHECK(dex_file != nullptr) << error_msg;
1976 class_linker->RegisterDexFile(*dex_file);
Richard Uhlerfbef44d2014-12-23 09:48:51 -08001977 dex_files.push_back(std::move(dex_file));
Andreas Gampe00b25f32014-09-17 21:49:05 -07001978 }
1979
1980 // Need a class loader.
1981 soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader);
1982 ScopedLocalRef<jobject> class_loader_local(soa.Env(),
1983 soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader));
1984 jobject class_loader = soa.Env()->NewGlobalRef(class_loader_local.get());
1985 // Fake that we're a compiler.
Richard Uhlerfbef44d2014-12-23 09:48:51 -08001986 std::vector<const DexFile*> class_path;
1987 for (auto& dex_file : dex_files) {
1988 class_path.push_back(dex_file.get());
1989 }
1990 runtime->SetCompileTimeClassPath(class_loader, class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07001991
1992 // Use the class loader while dumping.
1993 StackHandleScope<1> scope(self);
1994 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
1995 soa.Decode<mirror::ClassLoader*>(class_loader));
1996 options->class_loader_ = &loader_handle;
1997
1998 OatDumper oat_dumper(*oat_file, options);
1999 bool success = oat_dumper.Dump(*os);
2000 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2001}
2002
2003static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
2004 // No image = no class loader.
2005 NullHandle<mirror::ClassLoader> null_class_loader;
2006 options->class_loader_ = &null_class_loader;
2007
2008 OatDumper oat_dumper(*oat_file, options);
2009 bool success = oat_dumper.Dump(*os);
2010 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2011}
2012
2013static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2014 std::ostream* os) {
2015 std::string error_msg;
Igor Murashkin37743352014-11-13 14:38:00 -08002016 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
2017 &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002018 if (oat_file == nullptr) {
2019 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2020 return EXIT_FAILURE;
2021 }
2022
2023 if (runtime != nullptr) {
2024 return DumpOatWithRuntime(runtime, oat_file, options, os);
2025 } else {
2026 return DumpOatWithoutRuntime(oat_file, options, os);
2027 }
2028}
2029
2030static int SymbolizeOat(const char* oat_filename, std::string& output_name) {
2031 std::string error_msg;
Igor Murashkin37743352014-11-13 14:38:00 -08002032 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
2033 &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002034 if (oat_file == nullptr) {
2035 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2036 return EXIT_FAILURE;
2037 }
2038
2039 OatSymbolizer oat_symbolizer(oat_file, output_name);
2040 if (!oat_symbolizer.Init()) {
2041 fprintf(stderr, "Failed to initialize symbolizer\n");
2042 return EXIT_FAILURE;
2043 }
2044 if (!oat_symbolizer.Symbolize()) {
2045 fprintf(stderr, "Failed to symbolize\n");
2046 return EXIT_FAILURE;
2047 }
2048
2049 return EXIT_SUCCESS;
2050}
2051
Igor Murashkin37743352014-11-13 14:38:00 -08002052struct OatdumpArgs : public CmdlineArgs {
2053 protected:
2054 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002055
Igor Murashkin37743352014-11-13 14:38:00 -08002056 virtual ParseStatus ParseCustom(const StringPiece& option,
2057 std::string* error_msg) OVERRIDE {
2058 {
2059 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
2060 if (base_parse != kParseUnknownArgument) {
2061 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002062 }
2063 }
2064
Igor Murashkin37743352014-11-13 14:38:00 -08002065 if (option.starts_with("--oat-file=")) {
2066 oat_filename_ = option.substr(strlen("--oat-file=")).data();
2067 } else if (option.starts_with("--image=")) {
2068 image_location_ = option.substr(strlen("--image=")).data();
2069 } else if (option =="--dump:raw_mapping_table") {
2070 dump_raw_mapping_table_ = true;
2071 } else if (option == "--dump:raw_gc_map") {
2072 dump_raw_gc_map_ = true;
2073 } else if (option == "--no-dump:vmap") {
2074 dump_vmap_ = false;
2075 } else if (option == "--no-disassemble") {
2076 disassemble_code_ = false;
2077 } else if (option.starts_with("--symbolize=")) {
2078 oat_filename_ = option.substr(strlen("--symbolize=")).data();
2079 symbolize_ = true;
2080 } else if (option.starts_with("--method-filter=")) {
2081 method_filter_ = option.substr(strlen("--method-filter=")).data();
2082 } else {
2083 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002084 }
2085
Igor Murashkin37743352014-11-13 14:38:00 -08002086 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002087 }
2088
Igor Murashkin37743352014-11-13 14:38:00 -08002089 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
2090 // Infer boot image location from the image location if possible.
2091 if (boot_image_location_ == nullptr) {
2092 boot_image_location_ = image_location_;
2093 }
2094
2095 // Perform the parent checks.
2096 ParseStatus parent_checks = Base::ParseChecks(error_msg);
2097 if (parent_checks != kParseOk) {
2098 return parent_checks;
2099 }
2100
2101 // Perform our own checks.
2102 if (image_location_ == nullptr && oat_filename_ == nullptr) {
2103 *error_msg = "Either --image or --oat-file must be specified";
2104 return kParseError;
2105 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
2106 *error_msg = "Either --image or --oat-file must be specified but not both";
2107 return kParseError;
2108 }
2109
2110 return kParseOk;
2111 }
2112
2113 virtual std::string GetUsage() const {
2114 std::string usage;
2115
2116 usage +=
2117 "Usage: oatdump [options] ...\n"
2118 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
2119 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
2120 "\n"
2121 // Either oat-file or image is required.
2122 " --oat-file=<file.oat>: specifies an input oat filename.\n"
2123 " Example: --oat-file=/system/framework/boot.oat\n"
2124 "\n"
2125 " --image=<file.art>: specifies an input image location.\n"
2126 " Example: --image=/system/framework/boot.art\n"
2127 "\n";
2128
2129 usage += Base::GetUsage();
2130
2131 usage += // Optional.
2132 " --dump:raw_mapping_table enables dumping of the mapping table.\n"
2133 " Example: --dump:raw_mapping_table\n"
2134 "\n"
2135 " --dump:raw_mapping_table enables dumping of the GC map.\n"
2136 " Example: --dump:raw_gc_map\n"
2137 "\n"
2138 " --no-dump:vmap may be used to disable vmap dumping.\n"
2139 " Example: --no-dump:vmap\n"
2140 "\n"
2141 " --no-disassemble may be used to disable disassembly.\n"
2142 " Example: --no-disassemble\n"
2143 "\n"
2144 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
2145 " Example: --method-filter=foo\n"
2146 "\n";
2147
2148 return usage;
2149 }
2150
2151 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07002152 const char* oat_filename_ = nullptr;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00002153 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07002154 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002155 std::string elf_filename_prefix_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002156 bool dump_raw_mapping_table_ = false;
2157 bool dump_raw_gc_map_ = false;
2158 bool dump_vmap_ = true;
2159 bool disassemble_code_ = true;
2160 bool symbolize_ = false;
2161};
2162
Igor Murashkin37743352014-11-13 14:38:00 -08002163struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
2164 virtual bool NeedsRuntime() OVERRIDE {
2165 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002166
Igor Murashkin37743352014-11-13 14:38:00 -08002167 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
2168 bool absolute_addresses = (args_->oat_filename_ == nullptr);
2169
2170 oat_dumper_options_ = std::unique_ptr<OatDumperOptions>(new OatDumperOptions(
2171 args_->dump_raw_mapping_table_,
2172 args_->dump_raw_gc_map_,
2173 args_->dump_vmap_,
2174 args_->disassemble_code_,
2175 absolute_addresses,
2176 args_->method_filter_));
2177
2178 return (args_->boot_image_location_ != nullptr || args_->image_location_ != nullptr) &&
2179 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002180 }
2181
Igor Murashkin37743352014-11-13 14:38:00 -08002182 virtual bool ExecuteWithoutRuntime() OVERRIDE {
2183 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08002184 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002185
Mathieu Chartierd424d082014-10-15 10:31:46 -07002186 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08002187
Andreas Gampec24f3992014-12-17 20:40:11 -08002188 if (args_->symbolize_) {
2189 return SymbolizeOat(args_->oat_filename_, args_->output_name_) == EXIT_SUCCESS;
2190 } else {
2191 return DumpOat(nullptr,
2192 args_->oat_filename_,
2193 oat_dumper_options_.release(),
2194 args_->os_) == EXIT_SUCCESS;
2195 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002196 }
2197
Igor Murashkin37743352014-11-13 14:38:00 -08002198 virtual bool ExecuteWithRuntime(Runtime* runtime) {
2199 CHECK(args_ != nullptr);
2200
2201 if (args_->oat_filename_ != nullptr) {
2202 return DumpOat(runtime,
2203 args_->oat_filename_,
2204 oat_dumper_options_.release(),
2205 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002206 }
Igor Murashkin37743352014-11-13 14:38:00 -08002207
2208 return DumpImage(runtime, args_->image_location_, oat_dumper_options_.release(), args_->os_)
2209 == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002210 }
2211
Igor Murashkin37743352014-11-13 14:38:00 -08002212 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
2213};
Andreas Gampe00b25f32014-09-17 21:49:05 -07002214
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002215} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07002216
2217int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08002218 art::OatdumpMain main;
2219 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002220}