blob: 62af7a99a6e369496174e8b3680ed41bdb4621cd [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>
Brian Carlstrom78128a62011-09-15 17:21:19 -070022#include <string>
23#include <vector>
24
Elliott Hughese222ee02012-12-13 14:41:43 -080025#include "base/stringpiece.h"
Elliott Hughes76160052012-12-12 16:31:20 -080026#include "base/unix_file/fd_file.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070027#include "class_linker.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070029#include "dex_file-inl.h"
Elliott Hughese3c845c2012-02-28 17:23:01 -080030#include "dex_instruction.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080031#include "disassembler.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080032#include "gc_map.h"
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070033#include "gc/large_object_space.h"
34#include "gc/space.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070035#include "image.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080036#include "indenter.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "mirror/abstract_method-inl.h"
38#include "mirror/array-inl.h"
39#include "mirror/class-inl.h"
40#include "mirror/field-inl.h"
41#include "mirror/object-inl.h"
42#include "mirror/object_array-inl.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080043#include "oat.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080044#include "object_utils.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080045#include "os.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070046#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070047#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070048#include "scoped_thread_state_change.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080049#include "verifier/method_verifier.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070050
51namespace art {
52
53static void usage() {
54 fprintf(stderr,
55 "Usage: oatdump [options] ...\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080056 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art --host-prefix=$ANDROID_PRODUCT_OUT\n"
57 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070058 "\n");
59 fprintf(stderr,
Brian Carlstroma6cc8932012-01-04 14:44:07 -080060 " --oat-file=<file.oat>: specifies an input oat filename.\n"
TDYa127a0a2a6c2012-10-16 22:47:38 -070061 " Example: --oat-file=/system/framework/boot.oat\n"
Brian Carlstromaded5f72011-10-07 17:15:04 -070062 "\n");
63 fprintf(stderr,
64 " --image=<file.art>: specifies an input image filename.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080065 " Example: --image=/system/framework/boot.art\n"
Brian Carlstrome24fa612011-09-29 00:53:55 -070066 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -070067 fprintf(stderr,
Brian Carlstrome24fa612011-09-29 00:53:55 -070068 " --boot-image=<file.art>: provide the image file for the boot class path.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080069 " Example: --boot-image=/system/framework/boot.art\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070070 "\n");
Brian Carlstrome24fa612011-09-29 00:53:55 -070071 fprintf(stderr,
Brian Carlstrom58ae9412011-10-04 00:56:06 -070072 " --host-prefix may be used to translate host paths to target paths during\n"
73 " cross compilation.\n"
74 " Example: --host-prefix=out/target/product/crespo\n"
Brian Carlstromfe487d02012-02-29 18:49:16 -080075 " Default: $ANDROID_PRODUCT_OUT\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070076 "\n");
Brian Carlstrom27ec9612011-09-19 20:20:38 -070077 fprintf(stderr,
78 " --output=<file> may be used to send the output to a file.\n"
79 " Example: --output=/tmp/oatdump.txt\n"
80 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -070081 exit(EXIT_FAILURE);
82}
83
Ian Rogersff1ed472011-09-20 13:46:24 -070084const char* image_roots_descriptions_[] = {
Brian Carlstrom78128a62011-09-15 17:21:19 -070085 "kJniStubArray",
Brian Carlstrome24fa612011-09-29 00:53:55 -070086 "kAbstractMethodErrorStubArray",
Ian Rogers19846512012-02-24 11:42:47 -080087 "kResolutionMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070088 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -070089 "kRefsOnlySaveMethod",
90 "kRefsAndArgsSaveMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070091 "kOatLocation",
Brian Carlstrom58ae9412011-10-04 00:56:06 -070092 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070093 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070094};
95
Elliott Hughese3c845c2012-02-28 17:23:01 -080096class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -070097 public:
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070098 explicit OatDumper(const std::string& host_prefix, const OatFile& oat_file)
99 : host_prefix_(host_prefix),
100 oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800101 oat_dex_files_(oat_file.GetOatDexFiles()),
102 disassembler_(Disassembler::Create(oat_file_.GetOatHeader().GetInstructionSet())) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800103 AddAllOffsets();
104 }
105
106 void Dump(std::ostream& os) {
107 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700108
109 os << "MAGIC:\n";
110 os << oat_header.GetMagic() << "\n\n";
111
112 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800113 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700114
Elliott Hughesa72ec822012-03-05 17:12:22 -0800115 os << "INSTRUCTION SET:\n";
116 os << oat_header.GetInstructionSet() << "\n\n";
117
Brian Carlstromaded5f72011-10-07 17:15:04 -0700118 os << "DEX FILE COUNT:\n";
119 os << oat_header.GetDexFileCount() << "\n\n";
120
121 os << "EXECUTABLE OFFSET:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800122 os << StringPrintf("0x%08x\n\n", oat_header.GetExecutableOffset());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700123
Brian Carlstrom28db0122012-10-18 16:20:41 -0700124 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
125 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
126
127 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800128 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700129
130 os << "IMAGE FILE LOCATION:\n";
131 const std::string image_file_location(oat_header.GetImageFileLocation());
132 os << image_file_location;
133 if (!image_file_location.empty() && !host_prefix_.empty()) {
134 os << " (" << host_prefix_ << image_file_location << ")";
135 }
136 os << "\n\n";
137
Ian Rogers30fab402012-01-23 15:43:46 -0800138 os << "BEGIN:\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800139 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700140
Ian Rogers30fab402012-01-23 15:43:46 -0800141 os << "END:\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800142 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700143
144 os << std::flush;
145
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800146 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
147 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Brian Carlstromaded5f72011-10-07 17:15:04 -0700148 CHECK(oat_dex_file != NULL);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800149 DumpOatDexFile(os, *oat_dex_file);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700150 }
151 }
152
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800153 size_t ComputeSize(const void* oat_data) {
154 if (reinterpret_cast<const byte*>(oat_data) < oat_file_.Begin() ||
155 reinterpret_cast<const byte*>(oat_data) > oat_file_.End()) {
156 return 0; // Address not in oat file
157 }
158 uint32_t begin_offset = reinterpret_cast<size_t>(oat_data) -
159 reinterpret_cast<size_t>(oat_file_.Begin());
160 typedef std::set<uint32_t>::iterator It;
161 It it = offsets_.upper_bound(begin_offset);
162 CHECK(it != offsets_.end());
163 uint32_t end_offset = *it;
164 return end_offset - begin_offset;
165 }
166
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700167 InstructionSet GetInstructionSet() {
168 return oat_file_.GetOatHeader().GetInstructionSet();
169 }
170
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800171 const void* GetOatCode(mirror::AbstractMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800172 MethodHelper mh(m);
173 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
174 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
175 CHECK(oat_dex_file != NULL);
176 UniquePtr<const DexFile> dex_file(oat_dex_file->OpenDexFile());
177 if (dex_file.get() != NULL) {
178 uint32_t class_def_index;
179 bool found = dex_file->FindClassDefIndex(mh.GetDeclaringClassDescriptor(), class_def_index);
180 if (found) {
181 const OatFile::OatClass* oat_class = oat_dex_file->GetOatClass(class_def_index);
182 CHECK(oat_class != NULL);
183 size_t method_index = m->GetMethodIndex();
184 return oat_class->GetOatMethod(method_index).GetCode();
185 }
186 }
187 }
188 return NULL;
189 }
190
Brian Carlstromaded5f72011-10-07 17:15:04 -0700191 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800192 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800193 // We don't know the length of the code for each method, but we need to know where to stop
194 // when disassembling. What we do know is that a region of code will be followed by some other
195 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
196 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800197 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
198 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800199 CHECK(oat_dex_file != NULL);
200 UniquePtr<const DexFile> dex_file(oat_dex_file->OpenDexFile());
201 if (dex_file.get() == NULL) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800202 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800203 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800204 offsets_.insert(reinterpret_cast<uint32_t>(&dex_file->GetHeader()));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800205 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
206 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
207 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file->GetOatClass(class_def_index));
208 const byte* class_data = dex_file->GetClassData(class_def);
209 if (class_data != NULL) {
210 ClassDataItemIterator it(*dex_file, class_data);
211 SkipAllFields(it);
212 uint32_t class_method_index = 0;
213 while (it.HasNextDirectMethod()) {
214 AddOffsets(oat_class->GetOatMethod(class_method_index++));
215 it.Next();
216 }
217 while (it.HasNextVirtualMethod()) {
218 AddOffsets(oat_class->GetOatMethod(class_method_index++));
219 it.Next();
220 }
221 }
222 }
223 }
224
225 // If the last thing in the file is code for a method, there won't be an offset for the "next"
226 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
227 // for the end of the file.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800228 offsets_.insert(static_cast<uint32_t>(oat_file_.Size()));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800229 }
230
231 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800232 uint32_t code_offset = oat_method.GetCodeOffset();
233 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
234 code_offset &= ~0x1;
235 }
236 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800237 offsets_.insert(oat_method.GetMappingTableOffset());
238 offsets_.insert(oat_method.GetVmapTableOffset());
Ian Rogers0c7abda2012-09-19 13:33:42 -0700239 offsets_.insert(oat_method.GetNativeGcMapOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800240 }
241
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800242 void DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700243 os << "OAT DEX FILE:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800244 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800245 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800246 UniquePtr<const DexFile> dex_file(oat_dex_file.OpenDexFile());
247 if (dex_file.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700248 os << "NOT FOUND\n\n";
249 return;
250 }
251 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
252 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
253 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700254 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file.GetOatClass(class_def_index));
255 CHECK(oat_class.get() != NULL);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800256 os << StringPrintf("%zd: %s (type_idx=%d) (", class_def_index, descriptor, class_def.class_idx_)
mikaelpeltier6ffd0962012-10-25 15:37:45 +0200257 << oat_class->GetStatus() << ")"
258 // TODO: JACK CLASS ACCESS (HACK TO BE REMOVED)
259 << ( (class_def.access_flags_ & kAccClassJack) == kAccClassJack ? " (Jack)" : "" )
260 << "\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800261 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
262 std::ostream indented_os(&indent_filter);
263 DumpOatClass(indented_os, *oat_class.get(), *(dex_file.get()), class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700264 }
265
266 os << std::flush;
267 }
268
Elliott Hughese3c845c2012-02-28 17:23:01 -0800269 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700270 while (it.HasNextStaticField()) {
271 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700272 }
Ian Rogers0571d352011-11-03 19:51:38 -0700273 while (it.HasNextInstanceField()) {
274 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700275 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800276 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700277
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800278 void DumpOatClass(std::ostream& os, const OatFile::OatClass& oat_class, const DexFile& dex_file,
279 const DexFile::ClassDef& class_def) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800280 const byte* class_data = dex_file.GetClassData(class_def);
281 if (class_data == NULL) { // empty class such as a marker interface?
282 return;
283 }
284 ClassDataItemIterator it(dex_file, class_data);
285 SkipAllFields(it);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800286 uint32_t class_def_idx = dex_file.GetIndexForClassDef(class_def);
287 uint32_t class_method_idx = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700288 while (it.HasNextDirectMethod()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800289 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
290 DumpOatMethod(os, class_def_idx, class_method_idx, oat_method, dex_file,
291 it.GetMemberIndex(), it.GetMethodCodeItem(), it.GetMemberAccessFlags());
292 class_method_idx++;
Ian Rogers0571d352011-11-03 19:51:38 -0700293 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700294 }
Ian Rogers0571d352011-11-03 19:51:38 -0700295 while (it.HasNextVirtualMethod()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800296 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
297 DumpOatMethod(os, class_def_idx, class_method_idx, oat_method, dex_file,
298 it.GetMemberIndex(), it.GetMethodCodeItem(), it.GetMemberAccessFlags());
299 class_method_idx++;
Ian Rogers0571d352011-11-03 19:51:38 -0700300 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700301 }
Ian Rogers0571d352011-11-03 19:51:38 -0700302 DCHECK(!it.HasNext());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700303 os << std::flush;
304 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800305
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800306 void DumpOatMethod(std::ostream& os, uint32_t class_def_idx, uint32_t class_method_index,
Elliott Hughese3c845c2012-02-28 17:23:01 -0800307 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800308 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
309 uint32_t method_access_flags) {
310 os << StringPrintf("%d: %s (dex_method_idx=%d)\n",
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700311 class_method_index, PrettyMethod(dex_method_idx, dex_file, true).c_str(),
312 dex_method_idx);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800313 Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
314 std::ostream indent1_os(&indent1_filter);
315 {
316 indent1_os << "DEX CODE:\n";
317 Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
318 std::ostream indent2_os(&indent2_filter);
319 DumpDexCode(indent2_os, dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700320 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800321 if (Runtime::Current() != NULL) {
322 indent1_os << "VERIFIER TYPE ANALYSIS:\n";
323 Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
324 std::ostream indent2_os(&indent2_filter);
325 DumpVerifier(indent2_os, dex_method_idx, &dex_file, class_def_idx, code_item, method_access_flags);
Ian Rogersb23a7722012-10-09 16:54:26 -0700326 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800327 {
328 indent1_os << "OAT DATA:\n";
329 Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
330 std::ostream indent2_os(&indent2_filter);
331
332 indent2_os << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
333 indent2_os << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
334 DumpSpillMask(indent2_os, oat_method.GetCoreSpillMask(), false);
335 indent2_os << StringPrintf("\nfp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
336 DumpSpillMask(indent2_os, oat_method.GetFpSpillMask(), true);
337 indent2_os << StringPrintf("\nvmap_table: %p (offset=0x%08x)\n",
338 oat_method.GetVmapTable(), oat_method.GetVmapTableOffset());
339 DumpVmap(indent2_os, oat_method);
340 indent2_os << StringPrintf("mapping_table: %p (offset=0x%08x)\n",
341 oat_method.GetMappingTable(), oat_method.GetMappingTableOffset());
342 const bool kDumpRawMappingTable = false;
343 if (kDumpRawMappingTable) {
344 Indenter indent3_filter(indent2_os.rdbuf(), kIndentChar, kIndentBy1Count);
345 std::ostream indent3_os(&indent3_filter);
346 DumpMappingTable(indent3_os, oat_method);
347 }
348 indent2_os << StringPrintf("gc_map: %p (offset=0x%08x)\n",
349 oat_method.GetNativeGcMap(), oat_method.GetNativeGcMapOffset());
350 const bool kDumpRawGcMap = false;
351 if (kDumpRawGcMap) {
352 Indenter indent3_filter(indent2_os.rdbuf(), kIndentChar, kIndentBy1Count);
353 std::ostream indent3_os(&indent3_filter);
354 DumpGcMap(indent3_os, oat_method, code_item);
355 }
356 }
357 {
358 indent1_os << StringPrintf("CODE: %p (offset=0x%08x size=%d)%s\n",
359 oat_method.GetCode(),
360 oat_method.GetCodeOffset(),
361 oat_method.GetCodeSize(),
362 oat_method.GetCode() != NULL ? "..." : "");
363 Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
364 std::ostream indent2_os(&indent2_filter);
365 DumpCode(indent2_os, oat_method, dex_method_idx, &dex_file, class_def_idx, code_item,
366 method_access_flags);
367 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700368 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800369
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800370 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
371 if (spill_mask == 0) {
372 return;
373 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800374 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800375 for (size_t i = 0; i < 32; i++) {
376 if ((spill_mask & (1 << i)) != 0) {
377 if (is_float) {
378 os << "fr" << i;
379 } else {
380 os << "r" << i;
381 }
382 spill_mask ^= 1 << i; // clear bit
383 if (spill_mask != 0) {
384 os << ", ";
385 } else {
386 break;
387 }
388 }
389 }
390 os << ")";
391 }
392
Ian Rogersb23a7722012-10-09 16:54:26 -0700393 void DumpVmap(std::ostream& os, const OatFile::OatMethod& oat_method) {
394 const uint16_t* raw_table = oat_method.GetVmapTable();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800395 if (raw_table == NULL) {
396 return;
397 }
398 const VmapTable vmap_table(raw_table);
399 bool first = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800400 bool processing_fp = false;
401 uint32_t spill_mask = oat_method.GetCoreSpillMask();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800402 for (size_t i = 0; i < vmap_table.size(); i++) {
403 uint16_t dex_reg = vmap_table[i];
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800404 uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
405 processing_fp ? kFloatVReg : kIntVReg);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800406 os << (first ? "v" : ", v") << dex_reg;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800407 if (!processing_fp) {
408 os << "/r" << cpu_reg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800409 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800410 os << "/fr" << cpu_reg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800411 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800412 first = false;
413 if (!processing_fp && dex_reg == 0xFFFF) {
414 processing_fp = true;
415 spill_mask = oat_method.GetFpSpillMask();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800416 }
417 }
Elliott Hughesc073b072012-05-24 19:29:17 -0700418 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800419 }
420
Ian Rogersb23a7722012-10-09 16:54:26 -0700421 void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800422 const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
Ian Rogersb23a7722012-10-09 16:54:26 -0700423 const uint16_t* raw_table = oat_method.GetVmapTable();
424 if (raw_table != NULL) {
425 const VmapTable vmap_table(raw_table);
426 uint32_t vmap_offset;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800427 if (vmap_table.IsInContext(reg, vmap_offset, kind)) {
428 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
429 uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
430 : oat_method.GetCoreSpillMask();
431 os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
Ian Rogersb23a7722012-10-09 16:54:26 -0700432 } else {
433 uint32_t offset = StackVisitor::GetVRegOffset(code_item, oat_method.GetCoreSpillMask(),
434 oat_method.GetFpSpillMask(),
435 oat_method.GetFrameSizeInBytes(), reg);
436 os << "[sp + #" << offset << "]";
437 }
438 }
439 }
440
441 void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
442 const DexFile::CodeItem* code_item) {
443 const uint8_t* gc_map_raw = oat_method.GetNativeGcMap();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800444 if (gc_map_raw == NULL) {
445 return;
446 }
Ian Rogers0c7abda2012-09-19 13:33:42 -0700447 NativePcOffsetToReferenceMap map(gc_map_raw);
Ian Rogersb23a7722012-10-09 16:54:26 -0700448 const void* code = oat_method.GetCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800449 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
Ian Rogersbb6723f2012-09-21 09:49:06 -0700450 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code) +
451 map.GetNativePcOffset(entry);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800452 os << StringPrintf("%p", native_pc);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800453 size_t num_regs = map.RegWidth() * 8;
454 const uint8_t* reg_bitmap = map.GetBitMap(entry);
455 bool first = true;
456 for (size_t reg = 0; reg < num_regs; reg++) {
457 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
458 if (first) {
Ian Rogersb23a7722012-10-09 16:54:26 -0700459 os << " v" << reg << " (";
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800460 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -0700461 os << ")";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800462 first = false;
463 } else {
Ian Rogersb23a7722012-10-09 16:54:26 -0700464 os << ", v" << reg << " (";
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800465 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -0700466 os << ")";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800467 }
468 }
469 }
Elliott Hughesc073b072012-05-24 19:29:17 -0700470 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800471 }
472 }
473
474 void DumpMappingTable(std::ostream& os, const OatFile::OatMethod& oat_method) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800475 const uint32_t* raw_table = oat_method.GetMappingTable();
476 const void* code = oat_method.GetCode();
477 if (raw_table == NULL || code == NULL) {
478 return;
479 }
480
buzbee4d7a1892012-10-03 11:03:37 -0700481 ++raw_table;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800482 uint32_t length = *raw_table;
483 ++raw_table;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800484 if (length == 0) {
485 return;
486 }
Bill Buzbeea5b30242012-09-28 07:19:44 -0700487 uint32_t pc_to_dex_entries = *raw_table;
488 ++raw_table;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800489 if (pc_to_dex_entries != 0) {
490 os << "suspend point mappings {\n";
491 } else {
492 os << "catch entry mappings {\n";
493 }
494 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
495 std::ostream indent_os(&indent_filter);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800496 for (size_t i = 0; i < length; i += 2) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800497 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code) + raw_table[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800498 uint32_t dex_pc = raw_table[i + 1];
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800499 indent_os << StringPrintf("%p -> 0x%04x\n", native_pc, dex_pc);
500 if (i + 2 == pc_to_dex_entries && pc_to_dex_entries != length) {
Bill Buzbeea5b30242012-09-28 07:19:44 -0700501 // Separate the pc -> dex from dex -> pc sections
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800502 indent_os << std::flush;
503 os << "}\ncatch entry mappings {\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800504 }
505 }
Ian Rogersb23a7722012-10-09 16:54:26 -0700506 os << "}\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800507 }
508
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800509 uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method, size_t offset,
510 bool suspend_point_mapping) {
Ian Rogersb23a7722012-10-09 16:54:26 -0700511 const uint32_t* raw_table = oat_method.GetMappingTable();
512 if (raw_table != NULL) {
513 ++raw_table;
514 uint32_t length = *raw_table;
515 ++raw_table;
516 uint32_t pc_to_dex_entries = *raw_table;
517 ++raw_table;
518 size_t start, end;
519 if (suspend_point_mapping) {
520 start = 0;
521 end = pc_to_dex_entries;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800522 } else {
Ian Rogersb23a7722012-10-09 16:54:26 -0700523 start = pc_to_dex_entries;
524 end = length;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800525 }
Ian Rogersb23a7722012-10-09 16:54:26 -0700526 for (size_t i = start; i < end; i += 2) {
527 if (offset == raw_table[i]) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800528 uint32_t dex_pc = raw_table[i + 1];
Ian Rogersb23a7722012-10-09 16:54:26 -0700529 if (suspend_point_mapping) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800530 os << "suspend point dex PC: 0x";
Ian Rogersb23a7722012-10-09 16:54:26 -0700531 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800532 os << "catch entry dex PC: 0x";
Ian Rogersb23a7722012-10-09 16:54:26 -0700533 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800534 os << std::hex << dex_pc << std::dec << "\n";
535 return dex_pc;
Ian Rogersb23a7722012-10-09 16:54:26 -0700536 }
537 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800538 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800539 return DexFile::kDexNoIndex;
Ian Rogersb23a7722012-10-09 16:54:26 -0700540 }
541
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800542 void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
543 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
Ian Rogersb23a7722012-10-09 16:54:26 -0700544 const uint8_t* gc_map_raw = oat_method.GetNativeGcMap();
545 if (gc_map_raw != NULL) {
546 NativePcOffsetToReferenceMap map(gc_map_raw);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800547 if (map.HasEntry(native_pc_offset)) {
Ian Rogersb23a7722012-10-09 16:54:26 -0700548 size_t num_regs = map.RegWidth() * 8;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800549 const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
Ian Rogersb23a7722012-10-09 16:54:26 -0700550 bool first = true;
551 for (size_t reg = 0; reg < num_regs; reg++) {
552 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
553 if (first) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800554 os << "GC map objects: v" << reg << " (";
555 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -0700556 os << ")";
557 first = false;
558 } else {
559 os << ", v" << reg << " (";
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800560 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -0700561 os << ")";
562 }
563 }
564 }
565 if (!first) {
566 os << "\n";
567 }
568 }
569 }
570 }
571
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800572 void DumpVRegsAtDexPc(std::ostream& os, const OatFile::OatMethod& oat_method,
573 uint32_t dex_method_idx, const DexFile* dex_file,
574 uint32_t class_def_idx, const DexFile::CodeItem* code_item,
575 uint32_t method_access_flags, uint32_t dex_pc) {
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800576 static UniquePtr<verifier::MethodVerifier> verifier;
577 static const DexFile* verified_dex_file = NULL;
578 static uint32_t verified_dex_method_idx = DexFile::kDexNoIndex;
579 if (dex_file != verified_dex_file || verified_dex_method_idx != dex_method_idx) {
580 ScopedObjectAccess soa(Thread::Current());
581 mirror::DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(*dex_file);
582 mirror::ClassLoader* class_loader = NULL;
583 verifier.reset(new verifier::MethodVerifier(dex_file, dex_cache, class_loader, class_def_idx,
584 code_item, dex_method_idx, NULL,
Jeff Haoee988952013-04-16 14:23:47 -0700585 method_access_flags, true, true));
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800586 verifier->Verify();
587 verified_dex_file = dex_file;
588 verified_dex_method_idx = dex_method_idx;
589 }
590 std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800591 bool first = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800592 for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
593 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
594 if (kind != kUndefined) {
595 if (first) {
596 os << "VRegs: v";
597 first = false;
598 } else {
599 os << ", v";
600 }
601 os << reg << " (";
602 switch (kind) {
603 case kImpreciseConstant:
604 os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
605 DescribeVReg(os, oat_method, code_item, reg, kind);
606 break;
607 case kConstant:
608 os << "Constant: " << kinds.at((reg * 2) + 1);
609 break;
610 default:
611 DescribeVReg(os, oat_method, code_item, reg, kind);
612 break;
613 }
614 os << ")";
615 }
616 }
617 if (!first) {
618 os << "\n";
619 }
620 }
621
622
Ian Rogersb23a7722012-10-09 16:54:26 -0700623 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
624 if (code_item != NULL) {
625 size_t i = 0;
626 while (i < code_item->insns_size_in_code_units_) {
627 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800628 os << StringPrintf("0x%04zx: %s\n", i, instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -0700629 i += instruction->SizeInCodeUnits();
630 }
631 }
632 }
633
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800634 void DumpVerifier(std::ostream& os, uint32_t dex_method_idx, const DexFile* dex_file,
635 uint32_t class_def_idx, const DexFile::CodeItem* code_item,
636 uint32_t method_access_flags) {
637 if ((method_access_flags & kAccNative) == 0) {
638 ScopedObjectAccess soa(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800639 mirror::DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(*dex_file);
640 mirror::ClassLoader* class_loader = NULL;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800641 verifier::MethodVerifier::VerifyMethodAndDump(os, dex_method_idx, dex_file, dex_cache,
642 class_loader, class_def_idx, code_item, NULL,
643 method_access_flags);
644 }
645 }
646
647 void DumpCode(std::ostream& os, const OatFile::OatMethod& oat_method,
648 uint32_t dex_method_idx, const DexFile* dex_file,
649 uint32_t class_def_idx, const DexFile::CodeItem* code_item,
650 uint32_t method_access_flags) {
Ian Rogersb23a7722012-10-09 16:54:26 -0700651 const void* code = oat_method.GetCode();
652 size_t code_size = oat_method.GetCodeSize();
653 if (code == NULL || code_size == 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800654 os << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -0700655 return;
656 }
657 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code);
658 size_t offset = 0;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800659 const bool kDumpVRegs = (Runtime::Current() != NULL);
Ian Rogersb23a7722012-10-09 16:54:26 -0700660 while (offset < code_size) {
661 DumpMappingAtOffset(os, oat_method, offset, false);
662 offset += disassembler_->Dump(os, native_pc + offset);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800663 uint32_t dex_pc = DumpMappingAtOffset(os, oat_method, offset, true);
664 if (dex_pc != DexFile::kDexNoIndex) {
665 DumpGcMapAtNativePcOffset(os, oat_method, code_item, offset);
666 if (kDumpVRegs) {
667 DumpVRegsAtDexPc(os, oat_method, dex_method_idx, dex_file, class_def_idx, code_item,
668 method_access_flags, dex_pc);
669 }
670 }
Ian Rogersb23a7722012-10-09 16:54:26 -0700671 }
672 }
673
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700674 const std::string host_prefix_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800675 const OatFile& oat_file_;
676 std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800677 std::set<uint32_t> offsets_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800678 UniquePtr<Disassembler> disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700679};
680
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800681class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700682 public:
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800683 explicit ImageDumper(std::ostream* os, const std::string& image_filename,
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800684 const std::string& host_prefix, Space& image_space,
Ian Rogersca190662012-06-26 15:45:57 -0700685 const ImageHeader& image_header)
686 : os_(os), image_filename_(image_filename), host_prefix_(host_prefix),
687 image_space_(image_space), image_header_(image_header) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700688
Ian Rogersb726dcb2012-09-05 08:57:23 -0700689 void Dump() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800690 std::ostream& os = *os_;
691 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -0700692
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800693 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700694
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800695 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700696
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800697 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700698
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800699 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
700
701 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
702
703 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800704
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800705 {
706 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
707 Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
708 std::ostream indent1_os(&indent1_filter);
709 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
710 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
711 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
712 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800713 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800714 indent1_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
715 if (image_root_object->IsObjectArray()) {
716 Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
717 std::ostream indent2_os(&indent2_filter);
718 // TODO: replace down_cast with AsObjectArray (g++ currently has a problem with this)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800719 mirror::ObjectArray<mirror::Object>* image_root_object_array
720 = down_cast<mirror::ObjectArray<mirror::Object>*>(image_root_object);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800721 // = image_root_object->AsObjectArray<Object>();
722 for (int i = 0; i < image_root_object_array->GetLength(); i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800723 mirror::Object* value = image_root_object_array->Get(i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800724 if (value != NULL) {
725 indent2_os << i << ": ";
726 PrettyObjectValue(indent2_os, value->GetClass(), value);
727 } else {
728 indent2_os << i << ": null\n";
729 }
Ian Rogersd5b32602012-02-26 16:40:04 -0800730 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700731 }
732 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700733 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800734 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700735
Brian Carlstromaded5f72011-10-07 17:15:04 -0700736 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800737 mirror::Object* oat_location_object = image_header_.GetImageRoot(ImageHeader::kOatLocation);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800738 std::string oat_location(oat_location_object->AsString()->ToModifiedUtf8());
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800739 os << "OAT LOCATION: " << oat_location;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800740 if (!host_prefix_.empty()) {
741 oat_location = host_prefix_ + oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800742 os << " (" << oat_location << ")";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700743 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800744 os << "\n";
Brian Carlstromae826982011-11-09 01:33:42 -0800745 const OatFile* oat_file = class_linker->FindOatFileFromOatLocation(oat_location);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700746 if (oat_file == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800747 os << "NOT FOUND\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700748 return;
749 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800750 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700751
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800752 stats_.oat_file_bytes = oat_file->Size();
753
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700754 oat_dumper_.reset(new OatDumper(host_prefix_, *oat_file));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800755
Ian Rogers05f28c62012-10-23 18:12:13 -0700756 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file->GetOatDexFiles();
757 for (size_t i = 0; i < oat_dex_files.size(); i++) {
758 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
759 CHECK(oat_dex_file != NULL);
760 std::pair<std::string, size_t> entry(oat_dex_file->GetDexFileLocation(),
761 oat_dex_file->FileSize());
762 stats_.oat_dex_file_sizes.push_back(entry);
763 }
764
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800765 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700766
767 // Loop through all the image spaces and dump their objects.
768 Heap* heap = Runtime::Current()->GetHeap();
769 const Spaces& spaces = heap->GetSpaces();
Ian Rogers50b35e22012-10-04 10:09:15 -0700770 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700771 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700772 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700773 heap->FlushAllocStack();
774 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800775 {
776 std::ostream* saved_os = os_;
777 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
778 std::ostream indent_os(&indent_filter);
779 os_ = &indent_os;
780 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
781 // TODO: C++0x auto
782 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
783 Space* space = *it;
784 if (space->IsImageSpace()) {
785 ImageSpace* image_space = space->AsImageSpace();
786 image_space->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
787 indent_os << "\n";
788 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700789 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800790 // Dump the large objects separately.
791 heap->GetLargeObjectsSpace()->GetLiveObjects()->Walk(ImageDumper::Callback, this);
792 indent_os << "\n";
793 os_ = saved_os;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700794 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800795 os << "STATS:\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800796 UniquePtr<File> file(OS::OpenFile(image_filename_.c_str(), false));
Elliott Hughes76160052012-12-12 16:31:20 -0800797 stats_.file_bytes = file->GetLength();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800798 size_t header_bytes = sizeof(ImageHeader);
799 stats_.header_bytes = header_bytes;
800 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
801 stats_.alignment_bytes += alignment_bytes;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800802 stats_.Dump(os);
803 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800804
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800805 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800806
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800807 oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700808 }
809
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700810 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800811 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700812 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800813 CHECK(type != NULL);
814 if (value == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800815 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -0800816 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800817 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800818 os << StringPrintf("%p String: %s\n", string,
819 PrintableString(string->ToModifiedUtf8()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -0700820 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800821 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800822 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -0700823 } else if (type->IsFieldClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800824 mirror::Field* field = value->AsField();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800825 os << StringPrintf("%p Field: %s\n", field, PrettyField(field).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -0700826 } else if (type->IsMethodClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800827 mirror::AbstractMethod* method = value->AsMethod();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800828 os << StringPrintf("%p Method: %s\n", method, PrettyMethod(method).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -0800829 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800830 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -0800831 }
832 }
833
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800834 static void PrintField(std::ostream& os, mirror::Field* field, mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700835 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800836 FieldHelper fh(field);
Ian Rogers48efc2b2012-08-27 17:20:31 -0700837 const char* descriptor = fh.GetTypeDescriptor();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800838 os << StringPrintf("%s: ", fh.GetName());
Ian Rogers48efc2b2012-08-27 17:20:31 -0700839 if (descriptor[0] != 'L' && descriptor[0] != '[') {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800840 mirror::Class* type = fh.GetType();
Ian Rogers48efc2b2012-08-27 17:20:31 -0700841 if (type->IsPrimitiveLong()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800842 os << StringPrintf("%lld (0x%llx)\n", field->Get64(obj), field->Get64(obj));
Ian Rogers48efc2b2012-08-27 17:20:31 -0700843 } else if (type->IsPrimitiveDouble()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800844 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers48efc2b2012-08-27 17:20:31 -0700845 } else if (type->IsPrimitiveFloat()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800846 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers48efc2b2012-08-27 17:20:31 -0700847 } else {
848 DCHECK(type->IsPrimitive());
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800849 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers48efc2b2012-08-27 17:20:31 -0700850 }
Ian Rogersd5b32602012-02-26 16:40:04 -0800851 } else {
Ian Rogers48efc2b2012-08-27 17:20:31 -0700852 // Get the value, don't compute the type unless it is non-null as we don't want
853 // to cause class loading.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800854 mirror::Object* value = field->GetObj(obj);
Ian Rogers48efc2b2012-08-27 17:20:31 -0700855 if (value == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800856 os << StringPrintf("null %s\n", PrettyDescriptor(descriptor).c_str());
Ian Rogers48efc2b2012-08-27 17:20:31 -0700857 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800858 PrettyObjectValue(os, fh.GetType(), value);
Ian Rogers48efc2b2012-08-27 17:20:31 -0700859 }
Ian Rogersd5b32602012-02-26 16:40:04 -0800860 }
861 }
862
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800863 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700864 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800865 mirror::Class* super = klass->GetSuperClass();
Ian Rogersd5b32602012-02-26 16:40:04 -0800866 if (super != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800867 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -0800868 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800869 mirror::ObjectArray<mirror::Field>* fields = klass->GetIFields();
Ian Rogersd5b32602012-02-26 16:40:04 -0800870 if (fields != NULL) {
871 for (int32_t i = 0; i < fields->GetLength(); i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800872 mirror::Field* field = fields->Get(i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800873 PrintField(os, field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -0800874 }
875 }
876 }
877
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800878 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800879 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700880 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800881
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800882 const void* GetOatCodeBegin(mirror::AbstractMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700883 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800884 const void* code = m->GetCode();
Jeff Hao58df3272013-04-22 15:28:53 -0700885 if (code == GetResolutionTrampoline()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800886 code = oat_dumper_->GetOatCode(m);
887 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700888 if (oat_dumper_->GetInstructionSet() == kThumb2) {
889 code = reinterpret_cast<void*>(reinterpret_cast<uint32_t>(code) & ~0x1);
890 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800891 return code;
892 }
893
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800894 uint32_t GetOatCodeSize(mirror::AbstractMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700895 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700896 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetOatCodeBegin(m));
897 if (oat_code_begin == NULL) {
898 return 0;
899 }
900 return oat_code_begin[-1];
901 }
902
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800903 const void* GetOatCodeEnd(mirror::AbstractMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700904 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700905 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetOatCodeBegin(m));
906 if (oat_code_begin == NULL) {
907 return NULL;
908 }
909 return oat_code_begin + GetOatCodeSize(m);
910 }
911
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800912 static void Callback(mirror::Object* obj, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700913 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700914 DCHECK(obj != NULL);
915 DCHECK(arg != NULL);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800916 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700917 if (!state->InDumpSpace(obj)) {
918 return;
919 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700920
921 size_t object_bytes = obj->SizeOf();
922 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
923 state->stats_.object_bytes += object_bytes;
924 state->stats_.alignment_bytes += alignment_bytes;
925
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800926 std::ostream& os = *state->os_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800927 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -0800928 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800929 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
930 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -0800931 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800932 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800933 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
934 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -0800935 } else if (obj->IsField()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800936 os << StringPrintf("%p: java.lang.reflect.Field %s\n", obj,
937 PrettyField(obj->AsField()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -0800938 } else if (obj->IsMethod()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800939 os << StringPrintf("%p: java.lang.reflect.Method %s\n", obj,
940 PrettyMethod(obj->AsMethod()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -0800941 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800942 os << StringPrintf("%p: java.lang.String %s\n", obj,
943 PrintableString(obj->AsString()->ToModifiedUtf8()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -0800944 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800945 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -0800946 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800947 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
948 std::ostream indent_os(&indent_filter);
949 DumpFields(indent_os, obj, obj_class);
Ian Rogersd5b32602012-02-26 16:40:04 -0800950 if (obj->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800951 mirror::ObjectArray<mirror::Object>* obj_array = obj->AsObjectArray<mirror::Object>();
Ian Rogersd5b32602012-02-26 16:40:04 -0800952 int32_t length = obj_array->GetLength();
953 for (int32_t i = 0; i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800954 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -0800955 size_t run = 0;
956 for (int32_t j = i + 1; j < length; j++) {
957 if (value == obj_array->Get(j)) {
958 run++;
959 } else {
960 break;
961 }
962 }
963 if (run == 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800964 indent_os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -0800965 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800966 indent_os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -0800967 i = i + run;
968 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800969 mirror::Class* value_class = value == NULL ? obj_class->GetComponentType() : value->GetClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800970 PrettyObjectValue(indent_os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -0800971 }
972 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800973 mirror::ObjectArray<mirror::Field>* sfields = obj->AsClass()->GetSFields();
Ian Rogersd5b32602012-02-26 16:40:04 -0800974 if (sfields != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800975 indent_os << "STATICS:\n";
976 Indenter indent2_filter(indent_os.rdbuf(), kIndentChar, kIndentBy1Count);
977 std::ostream indent2_os(&indent2_filter);
Ian Rogersd5b32602012-02-26 16:40:04 -0800978 for (int32_t i = 0; i < sfields->GetLength(); i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800979 mirror::Field* field = sfields->Get(i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800980 PrintField(indent2_os, field, field->GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -0800981 }
982 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700983 } else if (obj->IsMethod()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800984 mirror::AbstractMethod* method = obj->AsMethod();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700985 if (method->IsNative()) {
Ian Rogers0c7abda2012-09-19 13:33:42 -0700986 DCHECK(method->GetNativeGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700987 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800988 bool first_occurrence;
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700989 const void* oat_code = state->GetOatCodeBegin(method);
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700990 uint32_t oat_code_size = state->GetOatCodeSize(method);
991 state->ComputeOatSize(oat_code, &first_occurrence);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800992 if (first_occurrence) {
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700993 state->stats_.native_to_managed_code_bytes += oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800994 }
995 if (oat_code != method->GetCode()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800996 indent_os << StringPrintf("OAT CODE: %p\n", oat_code);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800997 }
Ian Rogers19846512012-02-24 11:42:47 -0800998 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
Jeff Hao58df3272013-04-22 15:28:53 -0700999 method->IsResolutionMethod() || MethodHelper(method).IsClassInitializer()) {
Ian Rogers0c7abda2012-09-19 13:33:42 -07001000 DCHECK(method->GetNativeGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001001 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001002 } else {
Anwar Ghuloumede18072013-04-22 16:39:48 -07001003 CHECK((method->GetCode() == NULL) || (method->GetNativeGcMap() != NULL));
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001004
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001005 const DexFile::CodeItem* code_item = MethodHelper(method).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -07001006 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001007 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001008
Elliott Hughesa0e18062012-04-13 15:59:59 -07001009 bool first_occurrence;
Ian Rogers0c7abda2012-09-19 13:33:42 -07001010 size_t gc_map_bytes = state->ComputeOatSize(method->GetNativeGcMap(), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001011 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001012 state->stats_.gc_map_bytes += gc_map_bytes;
1013 }
1014
1015 size_t pc_mapping_table_bytes =
Elliott Hughesa0e18062012-04-13 15:59:59 -07001016 state->ComputeOatSize(method->GetMappingTableRaw(), &first_occurrence);
1017 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001018 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
1019 }
1020
1021 size_t vmap_table_bytes =
Elliott Hughesa0e18062012-04-13 15:59:59 -07001022 state->ComputeOatSize(method->GetVmapTableRaw(), &first_occurrence);
1023 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001024 state->stats_.vmap_table_bytes += vmap_table_bytes;
1025 }
1026
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001027 const void* oat_code_begin = state->GetOatCodeBegin(method);
1028 const void* oat_code_end = state->GetOatCodeEnd(method);
Ian Rogersdb7bdc12012-04-09 21:27:15 -07001029 uint32_t oat_code_size = state->GetOatCodeSize(method);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001030 state->ComputeOatSize(oat_code_begin, &first_occurrence);
1031 if (first_occurrence) {
Ian Rogersdb7bdc12012-04-09 21:27:15 -07001032 state->stats_.managed_code_bytes += oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001033 if (method->IsConstructor()) {
1034 if (method->IsStatic()) {
1035 state->stats_.class_initializer_code_bytes += oat_code_size;
1036 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
1037 state->stats_.large_initializer_code_bytes += oat_code_size;
1038 }
1039 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
1040 state->stats_.large_method_code_bytes += oat_code_size;
1041 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001042 }
Ian Rogersdb7bdc12012-04-09 21:27:15 -07001043 state->stats_.managed_code_bytes_ignoring_deduplication += oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001044
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001045 indent_os << StringPrintf("OAT CODE: %p-%p\n", oat_code_begin, oat_code_end);
1046 indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
1047 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001048
1049 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
Jeff Hao74180ca2013-03-27 15:29:11 -07001050 vmap_table_bytes + oat_code_size + object_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001051
1052 double expansion =
Ian Rogersdb7bdc12012-04-09 21:27:15 -07001053 static_cast<double>(oat_code_size) / static_cast<double>(dex_instruction_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001054 state->stats_.ComputeOutliers(total_size, expansion, method);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001055 }
1056 }
Elliott Hughesa0e18062012-04-13 15:59:59 -07001057 state->stats_.Update(ClassHelper(obj_class).GetDescriptor(), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001058 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001059
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001060 std::set<const void*> already_seen_;
1061 // Compute the size of the given data within the oat file and whether this is the first time
1062 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07001063 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001064 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001065 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001066 already_seen_.insert(oat_data);
1067 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001068 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001069 }
1070 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001071 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001072
1073 public:
1074 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001075 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001076 size_t file_bytes;
1077
1078 size_t header_bytes;
1079 size_t object_bytes;
1080 size_t alignment_bytes;
1081
1082 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001083 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001084 size_t managed_to_native_code_bytes;
1085 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001086 size_t class_initializer_code_bytes;
1087 size_t large_initializer_code_bytes;
1088 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001089
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001090 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001091 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001092 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001093
1094 size_t dex_instruction_bytes;
1095
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001096 std::vector<mirror::AbstractMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001097 std::vector<size_t> method_outlier_size;
1098 std::vector<double> method_outlier_expansion;
Ian Rogers05f28c62012-10-23 18:12:13 -07001099 std::vector<std::pair<std::string, size_t> > oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001100
1101 explicit Stats()
1102 : oat_file_bytes(0),
1103 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001104 header_bytes(0),
1105 object_bytes(0),
1106 alignment_bytes(0),
1107 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001108 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001109 managed_to_native_code_bytes(0),
1110 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07001111 class_initializer_code_bytes(0),
1112 large_initializer_code_bytes(0),
1113 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001114 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001115 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001116 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001117 dex_instruction_bytes(0) {}
1118
Elliott Hughesa0e18062012-04-13 15:59:59 -07001119 struct SizeAndCount {
1120 SizeAndCount(size_t bytes, size_t count) : bytes(bytes), count(count) {}
1121 size_t bytes;
1122 size_t count;
1123 };
1124 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
1125 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001126
Elliott Hughesa0e18062012-04-13 15:59:59 -07001127 void Update(const std::string& descriptor, size_t object_bytes) {
1128 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
1129 if (it != sizes_and_counts.end()) {
1130 it->second.bytes += object_bytes;
1131 it->second.count += 1;
1132 } else {
1133 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes, 1));
1134 }
1135 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001136
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001137 double PercentOfOatBytes(size_t size) {
1138 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
1139 }
1140
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001141 double PercentOfFileBytes(size_t size) {
1142 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
1143 }
1144
1145 double PercentOfObjectBytes(size_t size) {
1146 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
1147 }
1148
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001149 void ComputeOutliers(size_t total_size, double expansion, mirror::AbstractMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001150 method_outlier_size.push_back(total_size);
1151 method_outlier_expansion.push_back(expansion);
1152 method_outlier.push_back(method);
1153 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001154
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001155 void DumpOutliers(std::ostream& os)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001156 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001157 size_t sum_of_sizes = 0;
1158 size_t sum_of_sizes_squared = 0;
1159 size_t sum_of_expansion = 0;
1160 size_t sum_of_expansion_squared = 0;
1161 size_t n = method_outlier_size.size();
1162 for (size_t i = 0; i < n; i++) {
1163 size_t cur_size = method_outlier_size[i];
1164 sum_of_sizes += cur_size;
1165 sum_of_sizes_squared += cur_size * cur_size;
1166 double cur_expansion = method_outlier_expansion[i];
1167 sum_of_expansion += cur_expansion;
1168 sum_of_expansion_squared += cur_expansion * cur_expansion;
1169 }
1170 size_t size_mean = sum_of_sizes / n;
1171 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
1172 double expansion_mean = sum_of_expansion / n;
1173 double expansion_variance =
1174 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
1175
1176 // Dump methods whose size is a certain number of standard deviations from the mean
1177 size_t dumped_values = 0;
1178 size_t skipped_values = 0;
1179 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
1180 size_t cur_size_variance = i * i * size_variance;
1181 bool first = true;
1182 for (size_t j = 0; j < n; j++) {
1183 size_t cur_size = method_outlier_size[j];
1184 if (cur_size > size_mean) {
1185 size_t cur_var = cur_size - size_mean;
1186 cur_var = cur_var * cur_var;
1187 if (cur_var > cur_size_variance) {
1188 if (dumped_values > 20) {
1189 if (i == 1) {
1190 skipped_values++;
1191 } else {
1192 i = 2; // jump to counting for 1 standard deviation
1193 break;
1194 }
1195 } else {
1196 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001197 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001198 first = false;
1199 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001200 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07001201 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001202 method_outlier_size[j] = 0; // don't consider this method again
1203 dumped_values++;
1204 }
1205 }
1206 }
1207 }
1208 }
1209 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001210 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001211 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001212 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001213 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001214
1215 // Dump methods whose expansion is a certain number of standard deviations from the mean
1216 dumped_values = 0;
1217 skipped_values = 0;
1218 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
1219 double cur_expansion_variance = i * i * expansion_variance;
1220 bool first = true;
1221 for (size_t j = 0; j < n; j++) {
1222 double cur_expansion = method_outlier_expansion[j];
1223 if (cur_expansion > expansion_mean) {
1224 size_t cur_var = cur_expansion - expansion_mean;
1225 cur_var = cur_var * cur_var;
1226 if (cur_var > cur_expansion_variance) {
1227 if (dumped_values > 20) {
1228 if (i == 1) {
1229 skipped_values++;
1230 } else {
1231 i = 2; // jump to counting for 1 standard deviation
1232 break;
1233 }
1234 } else {
1235 if (first) {
1236 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07001237 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001238 first = false;
1239 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001240 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07001241 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001242 method_outlier_expansion[j] = 0.0; // don't consider this method again
1243 dumped_values++;
1244 }
1245 }
1246 }
1247 }
1248 }
1249 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001250 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001251 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001252 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001253 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001254 }
1255
Ian Rogersb726dcb2012-09-05 08:57:23 -07001256 void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001257 {
1258 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
1259 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
1260 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1261 std::ostream indent_os(&indent_filter);
1262 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
1263 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
1264 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
1265 header_bytes, PercentOfFileBytes(header_bytes),
1266 object_bytes, PercentOfFileBytes(object_bytes),
1267 alignment_bytes, PercentOfFileBytes(alignment_bytes))
1268 << std::flush;
1269 CHECK_EQ(file_bytes, header_bytes + object_bytes + alignment_bytes);
1270 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001271
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001272 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001273 size_t object_bytes_total = 0;
Elliott Hughesa0e18062012-04-13 15:59:59 -07001274 typedef SizeAndCountTable::const_iterator It; // TODO: C++0x auto
1275 for (It it = sizes_and_counts.begin(), end = sizes_and_counts.end(); it != end; ++it) {
Elliott Hughes95572412011-12-13 18:14:20 -08001276 const std::string& descriptor(it->first);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001277 double average = static_cast<double>(it->second.bytes) / static_cast<double>(it->second.count);
1278 double percent = PercentOfObjectBytes(it->second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001279 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07001280 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
1281 descriptor.c_str(), it->second.bytes, it->second.count,
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001282 average, percent);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001283 object_bytes_total += it->second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001284 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001285 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001286 CHECK_EQ(object_bytes, object_bytes_total);
1287
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001288 os << StringPrintf("oat_file_bytes = %8zd\n"
1289 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1290 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1291 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
1292 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1293 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1294 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07001295 oat_file_bytes,
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001296 managed_code_bytes, PercentOfOatBytes(managed_code_bytes),
1297 managed_to_native_code_bytes, PercentOfOatBytes(managed_to_native_code_bytes),
Ian Rogers0d2d3782012-04-10 11:09:18 -07001298 native_to_managed_code_bytes, PercentOfOatBytes(native_to_managed_code_bytes),
1299 class_initializer_code_bytes, PercentOfOatBytes(class_initializer_code_bytes),
1300 large_initializer_code_bytes, PercentOfOatBytes(large_initializer_code_bytes),
1301 large_method_code_bytes, PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001302 << "DexFile sizes:\n";
Ian Rogers05f28c62012-10-23 18:12:13 -07001303 typedef std::vector<std::pair<std::string, size_t> >::const_iterator It2;
1304 for (It2 it = oat_dex_file_sizes.begin(); it != oat_dex_file_sizes.end();
1305 ++it) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001306 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
1307 it->first.c_str(), it->second, PercentOfOatBytes(it->second));
Ian Rogers05f28c62012-10-23 18:12:13 -07001308 }
1309
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001310 os << "\n" << StringPrintf("gc_map_bytes = %7zd (%2.0f%% of oat file bytes)\n"
1311 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
1312 "vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07001313 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
1314 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
1315 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001316 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001317
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001318 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
1319 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Elliott Hughesc073b072012-05-24 19:29:17 -07001320 static_cast<double>(managed_code_bytes) / static_cast<double>(dex_instruction_bytes),
1321 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07001322 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001323 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001324
1325 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001326 }
1327 } stats_;
1328
1329 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07001330 enum {
1331 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
1332 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1333 kLargeConstructorDexBytes = 4000,
1334 // Number of bytes for a method to be considered large. Based on the 4000 basic block
1335 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1336 kLargeMethodDexBytes = 16000
1337 };
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001338 UniquePtr<OatDumper> oat_dumper_;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001339 std::ostream* os_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001340 const std::string image_filename_;
1341 const std::string host_prefix_;
1342 Space& image_space_;
1343 const ImageHeader& image_header_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07001344
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001345 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001346};
1347
Elliott Hughes72395bf2012-04-24 13:45:26 -07001348static int oatdump(int argc, char** argv) {
Elliott Hughes0d39c122012-06-06 16:41:17 -07001349 InitLogging(argv);
Elliott Hughes72395bf2012-04-24 13:45:26 -07001350
Brian Carlstrom78128a62011-09-15 17:21:19 -07001351 // Skip over argv[0].
1352 argv++;
1353 argc--;
1354
1355 if (argc == 0) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001356 fprintf(stderr, "No arguments specified\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -07001357 usage();
1358 }
1359
Brian Carlstromaded5f72011-10-07 17:15:04 -07001360 const char* oat_filename = NULL;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001361 const char* image_filename = NULL;
1362 const char* boot_image_filename = NULL;
Logan Chien0cc6ab62012-03-20 22:57:52 +08001363 std::string elf_filename_prefix;
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001364 UniquePtr<std::string> host_prefix;
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001365 std::ostream* os = &std::cout;
1366 UniquePtr<std::ofstream> out;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001367
1368 for (int i = 0; i < argc; i++) {
1369 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -08001370 if (option.starts_with("--oat-file=")) {
1371 oat_filename = option.substr(strlen("--oat-file=")).data();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001372 } else if (option.starts_with("--image=")) {
Brian Carlstrom78128a62011-09-15 17:21:19 -07001373 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001374 } else if (option.starts_with("--boot-image=")) {
1375 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001376 } else if (option.starts_with("--host-prefix=")) {
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001377 host_prefix.reset(new std::string(option.substr(strlen("--host-prefix=")).data()));
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001378 } else if (option.starts_with("--output=")) {
1379 const char* filename = option.substr(strlen("--output=")).data();
1380 out.reset(new std::ofstream(filename));
1381 if (!out->good()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001382 fprintf(stderr, "Failed to open output filename %s\n", filename);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001383 usage();
1384 }
1385 os = out.get();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001386 } else {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001387 fprintf(stderr, "Unknown argument %s\n", option.data());
Brian Carlstrom78128a62011-09-15 17:21:19 -07001388 usage();
1389 }
1390 }
1391
Brian Carlstromaded5f72011-10-07 17:15:04 -07001392 if (image_filename == NULL && oat_filename == NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001393 fprintf(stderr, "Either --image or --oat must be specified\n");
1394 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001395 }
1396
Brian Carlstromaded5f72011-10-07 17:15:04 -07001397 if (image_filename != NULL && oat_filename != NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001398 fprintf(stderr, "Either --image or --oat must be specified but not both\n");
1399 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001400 }
1401
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001402 if (host_prefix.get() == NULL) {
Brian Carlstrom81f3ca12012-03-17 00:27:35 -07001403 const char* android_product_out = getenv("ANDROID_PRODUCT_OUT");
1404 if (android_product_out != NULL) {
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001405 host_prefix.reset(new std::string(android_product_out));
1406 } else {
1407 host_prefix.reset(new std::string(""));
Brian Carlstrom81f3ca12012-03-17 00:27:35 -07001408 }
1409 }
1410
Brian Carlstromaded5f72011-10-07 17:15:04 -07001411 if (oat_filename != NULL) {
Logan Chien0c717dd2012-03-28 18:31:07 +08001412 OatFile* oat_file =
Brian Carlstrom1cac3432012-12-12 10:56:22 -08001413 OatFile::Open(oat_filename, oat_filename, NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001414 if (oat_file == NULL) {
1415 fprintf(stderr, "Failed to open oat file from %s\n", oat_filename);
1416 return EXIT_FAILURE;
1417 }
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001418 OatDumper oat_dumper(*host_prefix.get(), *oat_file);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001419 oat_dumper.Dump(*os);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001420 return EXIT_SUCCESS;
1421 }
1422
Brian Carlstrom78128a62011-09-15 17:21:19 -07001423 Runtime::Options options;
1424 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001425 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001426 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001427 std::string boot_oat_option;
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001428 if (boot_image_filename != NULL) {
1429 boot_image_option += "-Ximage:";
1430 boot_image_option += boot_image_filename;
1431 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom78128a62011-09-15 17:21:19 -07001432 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001433 if (image_filename != NULL) {
1434 image_option += "-Ximage:";
1435 image_option += image_filename;
1436 options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL)));
1437 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001438
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001439 if (!host_prefix->empty()) {
1440 options.push_back(std::make_pair("host-prefix", host_prefix->c_str()));
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001441 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001442
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001443 if (!Runtime::Create(options, false)) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001444 fprintf(stderr, "Failed to create runtime\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -07001445 return EXIT_FAILURE;
1446 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001447 UniquePtr<Runtime> runtime(Runtime::Current());
1448 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
1449 // give it away now and then switch to a more managable ScopedObjectAccess.
1450 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
1451 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom78128a62011-09-15 17:21:19 -07001452
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001453 Heap* heap = Runtime::Current()->GetHeap();
Brian Carlstromfddf6f62012-03-15 16:56:45 -07001454 ImageSpace* image_space = heap->GetImageSpace();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001455 CHECK(image_space != NULL);
1456 const ImageHeader& image_header = image_space->GetImageHeader();
1457 if (!image_header.IsValid()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001458 fprintf(stderr, "Invalid image header %s\n", image_filename);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001459 return EXIT_FAILURE;
1460 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001461 ImageDumper image_dumper(os, image_filename, *host_prefix.get(), *image_space, image_header);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001462 image_dumper.Dump();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001463 return EXIT_SUCCESS;
1464}
1465
1466} // namespace art
1467
1468int main(int argc, char** argv) {
1469 return art::oatdump(argc, argv);
1470}