blob: 7e920249a2badb3dd379b8e2f43f2a73675badef [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 Rogersad25ac52011-10-04 19:13:33 -070087 "kStaticResolutionStubArray",
Ian Rogers1cb0a1d2011-10-06 15:24:35 -070088 "kUnknownMethodResolutionStubArray",
Ian Rogers19846512012-02-24 11:42:47 -080089 "kResolutionMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070090 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -070091 "kRefsOnlySaveMethod",
92 "kRefsAndArgsSaveMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070093 "kOatLocation",
Brian Carlstrom58ae9412011-10-04 00:56:06 -070094 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070095 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070096};
97
Elliott Hughese3c845c2012-02-28 17:23:01 -080098class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -070099 public:
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700100 explicit OatDumper(const std::string& host_prefix, const OatFile& oat_file)
101 : host_prefix_(host_prefix),
102 oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800103 oat_dex_files_(oat_file.GetOatDexFiles()),
104 disassembler_(Disassembler::Create(oat_file_.GetOatHeader().GetInstructionSet())) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800105 AddAllOffsets();
106 }
107
108 void Dump(std::ostream& os) {
109 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700110
111 os << "MAGIC:\n";
112 os << oat_header.GetMagic() << "\n\n";
113
114 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800115 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700116
Elliott Hughesa72ec822012-03-05 17:12:22 -0800117 os << "INSTRUCTION SET:\n";
118 os << oat_header.GetInstructionSet() << "\n\n";
119
Brian Carlstromaded5f72011-10-07 17:15:04 -0700120 os << "DEX FILE COUNT:\n";
121 os << oat_header.GetDexFileCount() << "\n\n";
122
123 os << "EXECUTABLE OFFSET:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800124 os << StringPrintf("0x%08x\n\n", oat_header.GetExecutableOffset());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700125
Brian Carlstrom28db0122012-10-18 16:20:41 -0700126 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
127 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
128
129 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800130 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700131
132 os << "IMAGE FILE LOCATION:\n";
133 const std::string image_file_location(oat_header.GetImageFileLocation());
134 os << image_file_location;
135 if (!image_file_location.empty() && !host_prefix_.empty()) {
136 os << " (" << host_prefix_ << image_file_location << ")";
137 }
138 os << "\n\n";
139
Ian Rogers30fab402012-01-23 15:43:46 -0800140 os << "BEGIN:\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800141 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700142
Ian Rogers30fab402012-01-23 15:43:46 -0800143 os << "END:\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800144 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700145
146 os << std::flush;
147
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800148 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
149 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Brian Carlstromaded5f72011-10-07 17:15:04 -0700150 CHECK(oat_dex_file != NULL);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800151 DumpOatDexFile(os, *oat_dex_file);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700152 }
153 }
154
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800155 size_t ComputeSize(const void* oat_data) {
156 if (reinterpret_cast<const byte*>(oat_data) < oat_file_.Begin() ||
157 reinterpret_cast<const byte*>(oat_data) > oat_file_.End()) {
158 return 0; // Address not in oat file
159 }
160 uint32_t begin_offset = reinterpret_cast<size_t>(oat_data) -
161 reinterpret_cast<size_t>(oat_file_.Begin());
162 typedef std::set<uint32_t>::iterator It;
163 It it = offsets_.upper_bound(begin_offset);
164 CHECK(it != offsets_.end());
165 uint32_t end_offset = *it;
166 return end_offset - begin_offset;
167 }
168
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700169 InstructionSet GetInstructionSet() {
170 return oat_file_.GetOatHeader().GetInstructionSet();
171 }
172
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800173 const void* GetOatCode(mirror::AbstractMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800174 MethodHelper mh(m);
175 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
176 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
177 CHECK(oat_dex_file != NULL);
178 UniquePtr<const DexFile> dex_file(oat_dex_file->OpenDexFile());
179 if (dex_file.get() != NULL) {
180 uint32_t class_def_index;
181 bool found = dex_file->FindClassDefIndex(mh.GetDeclaringClassDescriptor(), class_def_index);
182 if (found) {
183 const OatFile::OatClass* oat_class = oat_dex_file->GetOatClass(class_def_index);
184 CHECK(oat_class != NULL);
185 size_t method_index = m->GetMethodIndex();
186 return oat_class->GetOatMethod(method_index).GetCode();
187 }
188 }
189 }
190 return NULL;
191 }
192
Brian Carlstromaded5f72011-10-07 17:15:04 -0700193 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800194 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800195 // We don't know the length of the code for each method, but we need to know where to stop
196 // when disassembling. What we do know is that a region of code will be followed by some other
197 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
198 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800199 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
200 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800201 CHECK(oat_dex_file != NULL);
202 UniquePtr<const DexFile> dex_file(oat_dex_file->OpenDexFile());
203 if (dex_file.get() == NULL) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800204 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800205 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800206 offsets_.insert(reinterpret_cast<uint32_t>(&dex_file->GetHeader()));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800207 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
208 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
209 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file->GetOatClass(class_def_index));
210 const byte* class_data = dex_file->GetClassData(class_def);
211 if (class_data != NULL) {
212 ClassDataItemIterator it(*dex_file, class_data);
213 SkipAllFields(it);
214 uint32_t class_method_index = 0;
215 while (it.HasNextDirectMethod()) {
216 AddOffsets(oat_class->GetOatMethod(class_method_index++));
217 it.Next();
218 }
219 while (it.HasNextVirtualMethod()) {
220 AddOffsets(oat_class->GetOatMethod(class_method_index++));
221 it.Next();
222 }
223 }
224 }
225 }
226
227 // If the last thing in the file is code for a method, there won't be an offset for the "next"
228 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
229 // for the end of the file.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800230 offsets_.insert(static_cast<uint32_t>(oat_file_.Size()));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800231 }
232
233 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800234 uint32_t code_offset = oat_method.GetCodeOffset();
235 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
236 code_offset &= ~0x1;
237 }
238 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800239 offsets_.insert(oat_method.GetMappingTableOffset());
240 offsets_.insert(oat_method.GetVmapTableOffset());
Ian Rogers0c7abda2012-09-19 13:33:42 -0700241 offsets_.insert(oat_method.GetNativeGcMapOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800242 offsets_.insert(oat_method.GetInvokeStubOffset());
243 }
244
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800245 void DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700246 os << "OAT DEX FILE:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800247 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800248 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800249 UniquePtr<const DexFile> dex_file(oat_dex_file.OpenDexFile());
250 if (dex_file.get() == NULL) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700251 os << "NOT FOUND\n\n";
252 return;
253 }
254 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
255 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
256 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700257 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file.GetOatClass(class_def_index));
258 CHECK(oat_class.get() != NULL);
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800259 os << StringPrintf("%zd: %s (type_idx=%d) (", class_def_index, descriptor, class_def.class_idx_)
mikaelpeltier6ffd0962012-10-25 15:37:45 +0200260 << oat_class->GetStatus() << ")"
261 // TODO: JACK CLASS ACCESS (HACK TO BE REMOVED)
262 << ( (class_def.access_flags_ & kAccClassJack) == kAccClassJack ? " (Jack)" : "" )
263 << "\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800264 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
265 std::ostream indented_os(&indent_filter);
266 DumpOatClass(indented_os, *oat_class.get(), *(dex_file.get()), class_def);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700267 }
268
269 os << std::flush;
270 }
271
Elliott Hughese3c845c2012-02-28 17:23:01 -0800272 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700273 while (it.HasNextStaticField()) {
274 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700275 }
Ian Rogers0571d352011-11-03 19:51:38 -0700276 while (it.HasNextInstanceField()) {
277 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700278 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800279 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700280
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800281 void DumpOatClass(std::ostream& os, const OatFile::OatClass& oat_class, const DexFile& dex_file,
282 const DexFile::ClassDef& class_def) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800283 const byte* class_data = dex_file.GetClassData(class_def);
284 if (class_data == NULL) { // empty class such as a marker interface?
285 return;
286 }
287 ClassDataItemIterator it(dex_file, class_data);
288 SkipAllFields(it);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800289 uint32_t class_def_idx = dex_file.GetIndexForClassDef(class_def);
290 uint32_t class_method_idx = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700291 while (it.HasNextDirectMethod()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800292 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
293 DumpOatMethod(os, class_def_idx, class_method_idx, oat_method, dex_file,
294 it.GetMemberIndex(), it.GetMethodCodeItem(), it.GetMemberAccessFlags());
295 class_method_idx++;
Ian Rogers0571d352011-11-03 19:51:38 -0700296 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700297 }
Ian Rogers0571d352011-11-03 19:51:38 -0700298 while (it.HasNextVirtualMethod()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800299 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
300 DumpOatMethod(os, class_def_idx, class_method_idx, oat_method, dex_file,
301 it.GetMemberIndex(), it.GetMethodCodeItem(), it.GetMemberAccessFlags());
302 class_method_idx++;
Ian Rogers0571d352011-11-03 19:51:38 -0700303 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700304 }
Ian Rogers0571d352011-11-03 19:51:38 -0700305 DCHECK(!it.HasNext());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700306 os << std::flush;
307 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800308
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800309 void DumpOatMethod(std::ostream& os, uint32_t class_def_idx, uint32_t class_method_index,
Elliott Hughese3c845c2012-02-28 17:23:01 -0800310 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800311 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
312 uint32_t method_access_flags) {
313 os << StringPrintf("%d: %s (dex_method_idx=%d)\n",
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700314 class_method_index, PrettyMethod(dex_method_idx, dex_file, true).c_str(),
315 dex_method_idx);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800316 Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
317 std::ostream indent1_os(&indent1_filter);
318 {
319 indent1_os << "DEX CODE:\n";
320 Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
321 std::ostream indent2_os(&indent2_filter);
322 DumpDexCode(indent2_os, dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700323 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800324 if (Runtime::Current() != NULL) {
325 indent1_os << "VERIFIER TYPE ANALYSIS:\n";
326 Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
327 std::ostream indent2_os(&indent2_filter);
328 DumpVerifier(indent2_os, dex_method_idx, &dex_file, class_def_idx, code_item, method_access_flags);
Ian Rogersb23a7722012-10-09 16:54:26 -0700329 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800330 {
331 indent1_os << "OAT DATA:\n";
332 Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
333 std::ostream indent2_os(&indent2_filter);
334
335 indent2_os << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
336 indent2_os << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
337 DumpSpillMask(indent2_os, oat_method.GetCoreSpillMask(), false);
338 indent2_os << StringPrintf("\nfp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
339 DumpSpillMask(indent2_os, oat_method.GetFpSpillMask(), true);
340 indent2_os << StringPrintf("\nvmap_table: %p (offset=0x%08x)\n",
341 oat_method.GetVmapTable(), oat_method.GetVmapTableOffset());
342 DumpVmap(indent2_os, oat_method);
343 indent2_os << StringPrintf("mapping_table: %p (offset=0x%08x)\n",
344 oat_method.GetMappingTable(), oat_method.GetMappingTableOffset());
345 const bool kDumpRawMappingTable = false;
346 if (kDumpRawMappingTable) {
347 Indenter indent3_filter(indent2_os.rdbuf(), kIndentChar, kIndentBy1Count);
348 std::ostream indent3_os(&indent3_filter);
349 DumpMappingTable(indent3_os, oat_method);
350 }
351 indent2_os << StringPrintf("gc_map: %p (offset=0x%08x)\n",
352 oat_method.GetNativeGcMap(), oat_method.GetNativeGcMapOffset());
353 const bool kDumpRawGcMap = false;
354 if (kDumpRawGcMap) {
355 Indenter indent3_filter(indent2_os.rdbuf(), kIndentChar, kIndentBy1Count);
356 std::ostream indent3_os(&indent3_filter);
357 DumpGcMap(indent3_os, oat_method, code_item);
358 }
359 }
360 {
361 indent1_os << StringPrintf("CODE: %p (offset=0x%08x size=%d)%s\n",
362 oat_method.GetCode(),
363 oat_method.GetCodeOffset(),
364 oat_method.GetCodeSize(),
365 oat_method.GetCode() != NULL ? "..." : "");
366 Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
367 std::ostream indent2_os(&indent2_filter);
368 DumpCode(indent2_os, oat_method, dex_method_idx, &dex_file, class_def_idx, code_item,
369 method_access_flags);
370 }
371 {
372 indent1_os << StringPrintf("INVOKE STUB: %p (offset=0x%08x size=%d)%s\n",
373 oat_method.GetInvokeStub(),
374 oat_method.GetInvokeStubOffset(),
375 oat_method.GetInvokeStubSize(),
376 oat_method.GetInvokeStub() != NULL ? "..." : "");
377 Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
378 std::ostream indent2_os(&indent2_filter);
379 DumpInvokeStub(indent2_os, oat_method);
380 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700381 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800382
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800383 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
384 if (spill_mask == 0) {
385 return;
386 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800387 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800388 for (size_t i = 0; i < 32; i++) {
389 if ((spill_mask & (1 << i)) != 0) {
390 if (is_float) {
391 os << "fr" << i;
392 } else {
393 os << "r" << i;
394 }
395 spill_mask ^= 1 << i; // clear bit
396 if (spill_mask != 0) {
397 os << ", ";
398 } else {
399 break;
400 }
401 }
402 }
403 os << ")";
404 }
405
Ian Rogersb23a7722012-10-09 16:54:26 -0700406 void DumpVmap(std::ostream& os, const OatFile::OatMethod& oat_method) {
407 const uint16_t* raw_table = oat_method.GetVmapTable();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800408 if (raw_table == NULL) {
409 return;
410 }
411 const VmapTable vmap_table(raw_table);
412 bool first = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800413 bool processing_fp = false;
414 uint32_t spill_mask = oat_method.GetCoreSpillMask();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800415 for (size_t i = 0; i < vmap_table.size(); i++) {
416 uint16_t dex_reg = vmap_table[i];
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800417 uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
418 processing_fp ? kFloatVReg : kIntVReg);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800419 os << (first ? "v" : ", v") << dex_reg;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800420 if (!processing_fp) {
421 os << "/r" << cpu_reg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800422 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800423 os << "/fr" << cpu_reg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800424 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800425 first = false;
426 if (!processing_fp && dex_reg == 0xFFFF) {
427 processing_fp = true;
428 spill_mask = oat_method.GetFpSpillMask();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800429 }
430 }
Elliott Hughesc073b072012-05-24 19:29:17 -0700431 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800432 }
433
Ian Rogersb23a7722012-10-09 16:54:26 -0700434 void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800435 const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
Ian Rogersb23a7722012-10-09 16:54:26 -0700436 const uint16_t* raw_table = oat_method.GetVmapTable();
437 if (raw_table != NULL) {
438 const VmapTable vmap_table(raw_table);
439 uint32_t vmap_offset;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800440 if (vmap_table.IsInContext(reg, vmap_offset, kind)) {
441 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
442 uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
443 : oat_method.GetCoreSpillMask();
444 os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
Ian Rogersb23a7722012-10-09 16:54:26 -0700445 } else {
446 uint32_t offset = StackVisitor::GetVRegOffset(code_item, oat_method.GetCoreSpillMask(),
447 oat_method.GetFpSpillMask(),
448 oat_method.GetFrameSizeInBytes(), reg);
449 os << "[sp + #" << offset << "]";
450 }
451 }
452 }
453
454 void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
455 const DexFile::CodeItem* code_item) {
456 const uint8_t* gc_map_raw = oat_method.GetNativeGcMap();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800457 if (gc_map_raw == NULL) {
458 return;
459 }
Ian Rogers0c7abda2012-09-19 13:33:42 -0700460 NativePcOffsetToReferenceMap map(gc_map_raw);
Ian Rogersb23a7722012-10-09 16:54:26 -0700461 const void* code = oat_method.GetCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800462 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
Ian Rogersbb6723f2012-09-21 09:49:06 -0700463 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code) +
464 map.GetNativePcOffset(entry);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800465 os << StringPrintf("%p", native_pc);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800466 size_t num_regs = map.RegWidth() * 8;
467 const uint8_t* reg_bitmap = map.GetBitMap(entry);
468 bool first = true;
469 for (size_t reg = 0; reg < num_regs; reg++) {
470 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
471 if (first) {
Ian Rogersb23a7722012-10-09 16:54:26 -0700472 os << " v" << reg << " (";
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800473 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -0700474 os << ")";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800475 first = false;
476 } else {
Ian Rogersb23a7722012-10-09 16:54:26 -0700477 os << ", v" << reg << " (";
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800478 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -0700479 os << ")";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800480 }
481 }
482 }
Elliott Hughesc073b072012-05-24 19:29:17 -0700483 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800484 }
485 }
486
487 void DumpMappingTable(std::ostream& os, const OatFile::OatMethod& oat_method) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800488 const uint32_t* raw_table = oat_method.GetMappingTable();
489 const void* code = oat_method.GetCode();
490 if (raw_table == NULL || code == NULL) {
491 return;
492 }
493
buzbee4d7a1892012-10-03 11:03:37 -0700494 ++raw_table;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800495 uint32_t length = *raw_table;
496 ++raw_table;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800497 if (length == 0) {
498 return;
499 }
Bill Buzbeea5b30242012-09-28 07:19:44 -0700500 uint32_t pc_to_dex_entries = *raw_table;
501 ++raw_table;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800502 if (pc_to_dex_entries != 0) {
503 os << "suspend point mappings {\n";
504 } else {
505 os << "catch entry mappings {\n";
506 }
507 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
508 std::ostream indent_os(&indent_filter);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800509 for (size_t i = 0; i < length; i += 2) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800510 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code) + raw_table[i];
Elliott Hughese3c845c2012-02-28 17:23:01 -0800511 uint32_t dex_pc = raw_table[i + 1];
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800512 indent_os << StringPrintf("%p -> 0x%04x\n", native_pc, dex_pc);
513 if (i + 2 == pc_to_dex_entries && pc_to_dex_entries != length) {
Bill Buzbeea5b30242012-09-28 07:19:44 -0700514 // Separate the pc -> dex from dex -> pc sections
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800515 indent_os << std::flush;
516 os << "}\ncatch entry mappings {\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800517 }
518 }
Ian Rogersb23a7722012-10-09 16:54:26 -0700519 os << "}\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800520 }
521
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800522 uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method, size_t offset,
523 bool suspend_point_mapping) {
Ian Rogersb23a7722012-10-09 16:54:26 -0700524 const uint32_t* raw_table = oat_method.GetMappingTable();
525 if (raw_table != NULL) {
526 ++raw_table;
527 uint32_t length = *raw_table;
528 ++raw_table;
529 uint32_t pc_to_dex_entries = *raw_table;
530 ++raw_table;
531 size_t start, end;
532 if (suspend_point_mapping) {
533 start = 0;
534 end = pc_to_dex_entries;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800535 } else {
Ian Rogersb23a7722012-10-09 16:54:26 -0700536 start = pc_to_dex_entries;
537 end = length;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800538 }
Ian Rogersb23a7722012-10-09 16:54:26 -0700539 for (size_t i = start; i < end; i += 2) {
540 if (offset == raw_table[i]) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800541 uint32_t dex_pc = raw_table[i + 1];
Ian Rogersb23a7722012-10-09 16:54:26 -0700542 if (suspend_point_mapping) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800543 os << "suspend point dex PC: 0x";
Ian Rogersb23a7722012-10-09 16:54:26 -0700544 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800545 os << "catch entry dex PC: 0x";
Ian Rogersb23a7722012-10-09 16:54:26 -0700546 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800547 os << std::hex << dex_pc << std::dec << "\n";
548 return dex_pc;
Ian Rogersb23a7722012-10-09 16:54:26 -0700549 }
550 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800551 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800552 return DexFile::kDexNoIndex;
Ian Rogersb23a7722012-10-09 16:54:26 -0700553 }
554
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800555 void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
556 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
Ian Rogersb23a7722012-10-09 16:54:26 -0700557 const uint8_t* gc_map_raw = oat_method.GetNativeGcMap();
558 if (gc_map_raw != NULL) {
559 NativePcOffsetToReferenceMap map(gc_map_raw);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800560 if (map.HasEntry(native_pc_offset)) {
Ian Rogersb23a7722012-10-09 16:54:26 -0700561 size_t num_regs = map.RegWidth() * 8;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800562 const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
Ian Rogersb23a7722012-10-09 16:54:26 -0700563 bool first = true;
564 for (size_t reg = 0; reg < num_regs; reg++) {
565 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
566 if (first) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800567 os << "GC map objects: v" << reg << " (";
568 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -0700569 os << ")";
570 first = false;
571 } else {
572 os << ", v" << reg << " (";
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800573 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -0700574 os << ")";
575 }
576 }
577 }
578 if (!first) {
579 os << "\n";
580 }
581 }
582 }
583 }
584
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800585 void DumpVRegsAtDexPc(std::ostream& os, const OatFile::OatMethod& oat_method,
586 uint32_t dex_method_idx, const DexFile* dex_file,
587 uint32_t class_def_idx, const DexFile::CodeItem* code_item,
588 uint32_t method_access_flags, uint32_t dex_pc) {
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800589 static UniquePtr<verifier::MethodVerifier> verifier;
590 static const DexFile* verified_dex_file = NULL;
591 static uint32_t verified_dex_method_idx = DexFile::kDexNoIndex;
592 if (dex_file != verified_dex_file || verified_dex_method_idx != dex_method_idx) {
593 ScopedObjectAccess soa(Thread::Current());
594 mirror::DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(*dex_file);
595 mirror::ClassLoader* class_loader = NULL;
596 verifier.reset(new verifier::MethodVerifier(dex_file, dex_cache, class_loader, class_def_idx,
597 code_item, dex_method_idx, NULL,
598 method_access_flags, true));
599 verifier->Verify();
600 verified_dex_file = dex_file;
601 verified_dex_method_idx = dex_method_idx;
602 }
603 std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800604 bool first = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800605 for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
606 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
607 if (kind != kUndefined) {
608 if (first) {
609 os << "VRegs: v";
610 first = false;
611 } else {
612 os << ", v";
613 }
614 os << reg << " (";
615 switch (kind) {
616 case kImpreciseConstant:
617 os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
618 DescribeVReg(os, oat_method, code_item, reg, kind);
619 break;
620 case kConstant:
621 os << "Constant: " << kinds.at((reg * 2) + 1);
622 break;
623 default:
624 DescribeVReg(os, oat_method, code_item, reg, kind);
625 break;
626 }
627 os << ")";
628 }
629 }
630 if (!first) {
631 os << "\n";
632 }
633 }
634
635
Ian Rogersb23a7722012-10-09 16:54:26 -0700636 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
637 if (code_item != NULL) {
638 size_t i = 0;
639 while (i < code_item->insns_size_in_code_units_) {
640 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800641 os << StringPrintf("0x%04zx: %s\n", i, instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -0700642 i += instruction->SizeInCodeUnits();
643 }
644 }
645 }
646
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800647 void DumpVerifier(std::ostream& os, uint32_t dex_method_idx, const DexFile* dex_file,
648 uint32_t class_def_idx, const DexFile::CodeItem* code_item,
649 uint32_t method_access_flags) {
650 if ((method_access_flags & kAccNative) == 0) {
651 ScopedObjectAccess soa(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800652 mirror::DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(*dex_file);
653 mirror::ClassLoader* class_loader = NULL;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800654 verifier::MethodVerifier::VerifyMethodAndDump(os, dex_method_idx, dex_file, dex_cache,
655 class_loader, class_def_idx, code_item, NULL,
656 method_access_flags);
657 }
658 }
659
660 void DumpCode(std::ostream& os, const OatFile::OatMethod& oat_method,
661 uint32_t dex_method_idx, const DexFile* dex_file,
662 uint32_t class_def_idx, const DexFile::CodeItem* code_item,
663 uint32_t method_access_flags) {
Ian Rogersb23a7722012-10-09 16:54:26 -0700664 const void* code = oat_method.GetCode();
665 size_t code_size = oat_method.GetCodeSize();
666 if (code == NULL || code_size == 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800667 os << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -0700668 return;
669 }
670 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code);
671 size_t offset = 0;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800672 const bool kDumpVRegs = (Runtime::Current() != NULL);
Ian Rogersb23a7722012-10-09 16:54:26 -0700673 while (offset < code_size) {
674 DumpMappingAtOffset(os, oat_method, offset, false);
675 offset += disassembler_->Dump(os, native_pc + offset);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800676 uint32_t dex_pc = DumpMappingAtOffset(os, oat_method, offset, true);
677 if (dex_pc != DexFile::kDexNoIndex) {
678 DumpGcMapAtNativePcOffset(os, oat_method, code_item, offset);
679 if (kDumpVRegs) {
680 DumpVRegsAtDexPc(os, oat_method, dex_method_idx, dex_file, class_def_idx, code_item,
681 method_access_flags, dex_pc);
682 }
683 }
Ian Rogersb23a7722012-10-09 16:54:26 -0700684 }
685 }
686
687 void DumpInvokeStub(std::ostream& os, const OatFile::OatMethod& oat_method) {
688 const uint8_t* begin = reinterpret_cast<const uint8_t*>(oat_method.GetInvokeStub());
689 const uint8_t* end = begin + oat_method.GetInvokeStubSize();
690 disassembler_->Dump(os, begin, end);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800691 }
692
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700693 const std::string host_prefix_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800694 const OatFile& oat_file_;
695 std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800696 std::set<uint32_t> offsets_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800697 UniquePtr<Disassembler> disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700698};
699
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800700class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700701 public:
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800702 explicit ImageDumper(std::ostream* os, const std::string& image_filename,
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800703 const std::string& host_prefix, Space& image_space,
Ian Rogersca190662012-06-26 15:45:57 -0700704 const ImageHeader& image_header)
705 : os_(os), image_filename_(image_filename), host_prefix_(host_prefix),
706 image_space_(image_space), image_header_(image_header) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700707
Ian Rogersb726dcb2012-09-05 08:57:23 -0700708 void Dump() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800709 std::ostream& os = *os_;
710 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -0700711
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800712 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700713
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800714 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700715
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800716 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700717
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800718 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
719
720 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
721
722 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800723
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800724 {
725 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
726 Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
727 std::ostream indent1_os(&indent1_filter);
728 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
729 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
730 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
731 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800732 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800733 indent1_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
734 if (image_root_object->IsObjectArray()) {
735 Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
736 std::ostream indent2_os(&indent2_filter);
737 // TODO: replace down_cast with AsObjectArray (g++ currently has a problem with this)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800738 mirror::ObjectArray<mirror::Object>* image_root_object_array
739 = down_cast<mirror::ObjectArray<mirror::Object>*>(image_root_object);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800740 // = image_root_object->AsObjectArray<Object>();
741 for (int i = 0; i < image_root_object_array->GetLength(); i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800742 mirror::Object* value = image_root_object_array->Get(i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800743 if (value != NULL) {
744 indent2_os << i << ": ";
745 PrettyObjectValue(indent2_os, value->GetClass(), value);
746 } else {
747 indent2_os << i << ": null\n";
748 }
Ian Rogersd5b32602012-02-26 16:40:04 -0800749 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700750 }
751 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700752 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800753 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700754
Brian Carlstromaded5f72011-10-07 17:15:04 -0700755 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800756 mirror::Object* oat_location_object = image_header_.GetImageRoot(ImageHeader::kOatLocation);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800757 std::string oat_location(oat_location_object->AsString()->ToModifiedUtf8());
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800758 os << "OAT LOCATION: " << oat_location;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800759 if (!host_prefix_.empty()) {
760 oat_location = host_prefix_ + oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800761 os << " (" << oat_location << ")";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700762 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800763 os << "\n";
Brian Carlstromae826982011-11-09 01:33:42 -0800764 const OatFile* oat_file = class_linker->FindOatFileFromOatLocation(oat_location);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700765 if (oat_file == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800766 os << "NOT FOUND\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700767 return;
768 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800769 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700770
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800771 stats_.oat_file_bytes = oat_file->Size();
772
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700773 oat_dumper_.reset(new OatDumper(host_prefix_, *oat_file));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800774
Ian Rogers05f28c62012-10-23 18:12:13 -0700775 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file->GetOatDexFiles();
776 for (size_t i = 0; i < oat_dex_files.size(); i++) {
777 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
778 CHECK(oat_dex_file != NULL);
779 std::pair<std::string, size_t> entry(oat_dex_file->GetDexFileLocation(),
780 oat_dex_file->FileSize());
781 stats_.oat_dex_file_sizes.push_back(entry);
782 }
783
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800784 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700785
786 // Loop through all the image spaces and dump their objects.
787 Heap* heap = Runtime::Current()->GetHeap();
788 const Spaces& spaces = heap->GetSpaces();
Ian Rogers50b35e22012-10-04 10:09:15 -0700789 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700790 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700791 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700792 heap->FlushAllocStack();
793 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800794 {
795 std::ostream* saved_os = os_;
796 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
797 std::ostream indent_os(&indent_filter);
798 os_ = &indent_os;
799 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
800 // TODO: C++0x auto
801 for (Spaces::const_iterator it = spaces.begin(); it != spaces.end(); ++it) {
802 Space* space = *it;
803 if (space->IsImageSpace()) {
804 ImageSpace* image_space = space->AsImageSpace();
805 image_space->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
806 indent_os << "\n";
807 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700808 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800809 // Dump the large objects separately.
810 heap->GetLargeObjectsSpace()->GetLiveObjects()->Walk(ImageDumper::Callback, this);
811 indent_os << "\n";
812 os_ = saved_os;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700813 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800814 os << "STATS:\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800815 UniquePtr<File> file(OS::OpenFile(image_filename_.c_str(), false));
Elliott Hughes76160052012-12-12 16:31:20 -0800816 stats_.file_bytes = file->GetLength();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800817 size_t header_bytes = sizeof(ImageHeader);
818 stats_.header_bytes = header_bytes;
819 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
820 stats_.alignment_bytes += alignment_bytes;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800821 stats_.Dump(os);
822 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800823
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800824 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800825
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800826 oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700827 }
828
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700829 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800830 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700831 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800832 CHECK(type != NULL);
833 if (value == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800834 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -0800835 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800836 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800837 os << StringPrintf("%p String: %s\n", string,
838 PrintableString(string->ToModifiedUtf8()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -0700839 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800840 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800841 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -0700842 } else if (type->IsFieldClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800843 mirror::Field* field = value->AsField();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800844 os << StringPrintf("%p Field: %s\n", field, PrettyField(field).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -0700845 } else if (type->IsMethodClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800846 mirror::AbstractMethod* method = value->AsMethod();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800847 os << StringPrintf("%p Method: %s\n", method, PrettyMethod(method).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -0800848 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800849 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -0800850 }
851 }
852
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800853 static void PrintField(std::ostream& os, mirror::Field* field, mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700854 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersd5b32602012-02-26 16:40:04 -0800855 FieldHelper fh(field);
Ian Rogers48efc2b2012-08-27 17:20:31 -0700856 const char* descriptor = fh.GetTypeDescriptor();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800857 os << StringPrintf("%s: ", fh.GetName());
Ian Rogers48efc2b2012-08-27 17:20:31 -0700858 if (descriptor[0] != 'L' && descriptor[0] != '[') {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800859 mirror::Class* type = fh.GetType();
Ian Rogers48efc2b2012-08-27 17:20:31 -0700860 if (type->IsPrimitiveLong()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800861 os << StringPrintf("%lld (0x%llx)\n", field->Get64(obj), field->Get64(obj));
Ian Rogers48efc2b2012-08-27 17:20:31 -0700862 } else if (type->IsPrimitiveDouble()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800863 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers48efc2b2012-08-27 17:20:31 -0700864 } else if (type->IsPrimitiveFloat()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800865 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers48efc2b2012-08-27 17:20:31 -0700866 } else {
867 DCHECK(type->IsPrimitive());
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800868 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers48efc2b2012-08-27 17:20:31 -0700869 }
Ian Rogersd5b32602012-02-26 16:40:04 -0800870 } else {
Ian Rogers48efc2b2012-08-27 17:20:31 -0700871 // Get the value, don't compute the type unless it is non-null as we don't want
872 // to cause class loading.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800873 mirror::Object* value = field->GetObj(obj);
Ian Rogers48efc2b2012-08-27 17:20:31 -0700874 if (value == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800875 os << StringPrintf("null %s\n", PrettyDescriptor(descriptor).c_str());
Ian Rogers48efc2b2012-08-27 17:20:31 -0700876 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800877 PrettyObjectValue(os, fh.GetType(), value);
Ian Rogers48efc2b2012-08-27 17:20:31 -0700878 }
Ian Rogersd5b32602012-02-26 16:40:04 -0800879 }
880 }
881
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800882 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700883 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800884 mirror::Class* super = klass->GetSuperClass();
Ian Rogersd5b32602012-02-26 16:40:04 -0800885 if (super != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800886 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -0800887 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800888 mirror::ObjectArray<mirror::Field>* fields = klass->GetIFields();
Ian Rogersd5b32602012-02-26 16:40:04 -0800889 if (fields != NULL) {
890 for (int32_t i = 0; i < fields->GetLength(); i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800891 mirror::Field* field = fields->Get(i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800892 PrintField(os, field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -0800893 }
894 }
895 }
896
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800897 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800898 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700899 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800900
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800901 const void* GetOatCodeBegin(mirror::AbstractMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700902 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800903 Runtime* runtime = Runtime::Current();
904 const void* code = m->GetCode();
Ian Rogersfb6adba2012-03-04 21:51:51 -0800905 if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800906 code = oat_dumper_->GetOatCode(m);
907 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700908 if (oat_dumper_->GetInstructionSet() == kThumb2) {
909 code = reinterpret_cast<void*>(reinterpret_cast<uint32_t>(code) & ~0x1);
910 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800911 return code;
912 }
913
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800914 uint32_t GetOatCodeSize(mirror::AbstractMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700915 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700916 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetOatCodeBegin(m));
917 if (oat_code_begin == NULL) {
918 return 0;
919 }
920 return oat_code_begin[-1];
921 }
922
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800923 const void* GetOatCodeEnd(mirror::AbstractMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700924 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700925 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetOatCodeBegin(m));
926 if (oat_code_begin == NULL) {
927 return NULL;
928 }
929 return oat_code_begin + GetOatCodeSize(m);
930 }
931
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800932 static void Callback(mirror::Object* obj, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700933 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom78128a62011-09-15 17:21:19 -0700934 DCHECK(obj != NULL);
935 DCHECK(arg != NULL);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800936 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700937 if (!state->InDumpSpace(obj)) {
938 return;
939 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700940
941 size_t object_bytes = obj->SizeOf();
942 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
943 state->stats_.object_bytes += object_bytes;
944 state->stats_.alignment_bytes += alignment_bytes;
945
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800946 std::ostream& os = *state->os_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800947 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -0800948 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800949 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
950 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -0800951 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800952 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800953 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
954 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -0800955 } else if (obj->IsField()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800956 os << StringPrintf("%p: java.lang.reflect.Field %s\n", obj,
957 PrettyField(obj->AsField()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -0800958 } else if (obj->IsMethod()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800959 os << StringPrintf("%p: java.lang.reflect.Method %s\n", obj,
960 PrettyMethod(obj->AsMethod()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -0800961 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800962 os << StringPrintf("%p: java.lang.String %s\n", obj,
963 PrintableString(obj->AsString()->ToModifiedUtf8()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -0800964 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800965 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -0800966 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800967 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
968 std::ostream indent_os(&indent_filter);
969 DumpFields(indent_os, obj, obj_class);
Ian Rogersd5b32602012-02-26 16:40:04 -0800970 if (obj->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800971 mirror::ObjectArray<mirror::Object>* obj_array = obj->AsObjectArray<mirror::Object>();
Ian Rogersd5b32602012-02-26 16:40:04 -0800972 int32_t length = obj_array->GetLength();
973 for (int32_t i = 0; i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800974 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -0800975 size_t run = 0;
976 for (int32_t j = i + 1; j < length; j++) {
977 if (value == obj_array->Get(j)) {
978 run++;
979 } else {
980 break;
981 }
982 }
983 if (run == 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800984 indent_os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -0800985 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800986 indent_os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -0800987 i = i + run;
988 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800989 mirror::Class* value_class = value == NULL ? obj_class->GetComponentType() : value->GetClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800990 PrettyObjectValue(indent_os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -0800991 }
992 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800993 mirror::ObjectArray<mirror::Field>* sfields = obj->AsClass()->GetSFields();
Ian Rogersd5b32602012-02-26 16:40:04 -0800994 if (sfields != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800995 indent_os << "STATICS:\n";
996 Indenter indent2_filter(indent_os.rdbuf(), kIndentChar, kIndentBy1Count);
997 std::ostream indent2_os(&indent2_filter);
Ian Rogersd5b32602012-02-26 16:40:04 -0800998 for (int32_t i = 0; i < sfields->GetLength(); i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800999 mirror::Field* field = sfields->Get(i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001000 PrintField(indent2_os, field, field->GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001001 }
1002 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001003 } else if (obj->IsMethod()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001004 mirror::AbstractMethod* method = obj->AsMethod();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001005 if (method->IsNative()) {
Ian Rogers0c7abda2012-09-19 13:33:42 -07001006 DCHECK(method->GetNativeGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001007 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001008 bool first_occurrence;
1009 size_t invoke_stub_size = state->ComputeOatSize(
1010 reinterpret_cast<const void*>(method->GetInvokeStub()), &first_occurrence);
1011 if (first_occurrence) {
1012 state->stats_.managed_to_native_code_bytes += invoke_stub_size;
1013 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001014 const void* oat_code = state->GetOatCodeBegin(method);
Ian Rogersdb7bdc12012-04-09 21:27:15 -07001015 uint32_t oat_code_size = state->GetOatCodeSize(method);
1016 state->ComputeOatSize(oat_code, &first_occurrence);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001017 if (first_occurrence) {
Ian Rogersdb7bdc12012-04-09 21:27:15 -07001018 state->stats_.native_to_managed_code_bytes += oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001019 }
1020 if (oat_code != method->GetCode()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001021 indent_os << StringPrintf("OAT CODE: %p\n", oat_code);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001022 }
Ian Rogers19846512012-02-24 11:42:47 -08001023 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
1024 method->IsResolutionMethod()) {
Ian Rogers0c7abda2012-09-19 13:33:42 -07001025 DCHECK(method->GetNativeGcMap() == NULL) << PrettyMethod(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001026 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001027 } else {
Ian Rogers0c7abda2012-09-19 13:33:42 -07001028 DCHECK(method->GetNativeGcMap() != NULL) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001029
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001030 const DexFile::CodeItem* code_item = MethodHelper(method).GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -07001031 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001032 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001033
Elliott Hughesa0e18062012-04-13 15:59:59 -07001034 bool first_occurrence;
Ian Rogers0c7abda2012-09-19 13:33:42 -07001035 size_t gc_map_bytes = state->ComputeOatSize(method->GetNativeGcMap(), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001036 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001037 state->stats_.gc_map_bytes += gc_map_bytes;
1038 }
1039
1040 size_t pc_mapping_table_bytes =
Elliott Hughesa0e18062012-04-13 15:59:59 -07001041 state->ComputeOatSize(method->GetMappingTableRaw(), &first_occurrence);
1042 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001043 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
1044 }
1045
1046 size_t vmap_table_bytes =
Elliott Hughesa0e18062012-04-13 15:59:59 -07001047 state->ComputeOatSize(method->GetVmapTableRaw(), &first_occurrence);
1048 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001049 state->stats_.vmap_table_bytes += vmap_table_bytes;
1050 }
1051
Ian Rogersdb7bdc12012-04-09 21:27:15 -07001052 // TODO: compute invoke stub using length from oat file.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001053 size_t invoke_stub_size = state->ComputeOatSize(
Elliott Hughesa0e18062012-04-13 15:59:59 -07001054 reinterpret_cast<const void*>(method->GetInvokeStub()), &first_occurrence);
1055 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001056 state->stats_.native_to_managed_code_bytes += invoke_stub_size;
1057 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001058 const void* oat_code_begin = state->GetOatCodeBegin(method);
1059 const void* oat_code_end = state->GetOatCodeEnd(method);
Ian Rogersdb7bdc12012-04-09 21:27:15 -07001060 uint32_t oat_code_size = state->GetOatCodeSize(method);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001061 state->ComputeOatSize(oat_code_begin, &first_occurrence);
1062 if (first_occurrence) {
Ian Rogersdb7bdc12012-04-09 21:27:15 -07001063 state->stats_.managed_code_bytes += oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001064 if (method->IsConstructor()) {
1065 if (method->IsStatic()) {
1066 state->stats_.class_initializer_code_bytes += oat_code_size;
1067 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
1068 state->stats_.large_initializer_code_bytes += oat_code_size;
1069 }
1070 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
1071 state->stats_.large_method_code_bytes += oat_code_size;
1072 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001073 }
Ian Rogersdb7bdc12012-04-09 21:27:15 -07001074 state->stats_.managed_code_bytes_ignoring_deduplication += oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001075
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001076 indent_os << StringPrintf("OAT CODE: %p-%p\n", oat_code_begin, oat_code_end);
1077 indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
1078 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001079
1080 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
Ian Rogersdb7bdc12012-04-09 21:27:15 -07001081 vmap_table_bytes + invoke_stub_size + oat_code_size + object_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001082
1083 double expansion =
Ian Rogersdb7bdc12012-04-09 21:27:15 -07001084 static_cast<double>(oat_code_size) / static_cast<double>(dex_instruction_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001085 state->stats_.ComputeOutliers(total_size, expansion, method);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001086 }
1087 }
Elliott Hughesa0e18062012-04-13 15:59:59 -07001088 state->stats_.Update(ClassHelper(obj_class).GetDescriptor(), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001089 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001090
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001091 std::set<const void*> already_seen_;
1092 // Compute the size of the given data within the oat file and whether this is the first time
1093 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07001094 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001095 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001096 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001097 already_seen_.insert(oat_data);
1098 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001099 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001100 }
1101 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001102 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001103
1104 public:
1105 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001106 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001107 size_t file_bytes;
1108
1109 size_t header_bytes;
1110 size_t object_bytes;
1111 size_t alignment_bytes;
1112
1113 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001114 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001115 size_t managed_to_native_code_bytes;
1116 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001117 size_t class_initializer_code_bytes;
1118 size_t large_initializer_code_bytes;
1119 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001120
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001121 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001122 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001123 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001124
1125 size_t dex_instruction_bytes;
1126
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001127 std::vector<mirror::AbstractMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001128 std::vector<size_t> method_outlier_size;
1129 std::vector<double> method_outlier_expansion;
Ian Rogers05f28c62012-10-23 18:12:13 -07001130 std::vector<std::pair<std::string, size_t> > oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001131
1132 explicit Stats()
1133 : oat_file_bytes(0),
1134 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001135 header_bytes(0),
1136 object_bytes(0),
1137 alignment_bytes(0),
1138 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001139 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001140 managed_to_native_code_bytes(0),
1141 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07001142 class_initializer_code_bytes(0),
1143 large_initializer_code_bytes(0),
1144 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001145 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001146 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001147 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001148 dex_instruction_bytes(0) {}
1149
Elliott Hughesa0e18062012-04-13 15:59:59 -07001150 struct SizeAndCount {
1151 SizeAndCount(size_t bytes, size_t count) : bytes(bytes), count(count) {}
1152 size_t bytes;
1153 size_t count;
1154 };
1155 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
1156 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001157
Elliott Hughesa0e18062012-04-13 15:59:59 -07001158 void Update(const std::string& descriptor, size_t object_bytes) {
1159 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
1160 if (it != sizes_and_counts.end()) {
1161 it->second.bytes += object_bytes;
1162 it->second.count += 1;
1163 } else {
1164 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes, 1));
1165 }
1166 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001167
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001168 double PercentOfOatBytes(size_t size) {
1169 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
1170 }
1171
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001172 double PercentOfFileBytes(size_t size) {
1173 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
1174 }
1175
1176 double PercentOfObjectBytes(size_t size) {
1177 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
1178 }
1179
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001180 void ComputeOutliers(size_t total_size, double expansion, mirror::AbstractMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001181 method_outlier_size.push_back(total_size);
1182 method_outlier_expansion.push_back(expansion);
1183 method_outlier.push_back(method);
1184 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001185
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001186 void DumpOutliers(std::ostream& os)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001187 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001188 size_t sum_of_sizes = 0;
1189 size_t sum_of_sizes_squared = 0;
1190 size_t sum_of_expansion = 0;
1191 size_t sum_of_expansion_squared = 0;
1192 size_t n = method_outlier_size.size();
1193 for (size_t i = 0; i < n; i++) {
1194 size_t cur_size = method_outlier_size[i];
1195 sum_of_sizes += cur_size;
1196 sum_of_sizes_squared += cur_size * cur_size;
1197 double cur_expansion = method_outlier_expansion[i];
1198 sum_of_expansion += cur_expansion;
1199 sum_of_expansion_squared += cur_expansion * cur_expansion;
1200 }
1201 size_t size_mean = sum_of_sizes / n;
1202 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
1203 double expansion_mean = sum_of_expansion / n;
1204 double expansion_variance =
1205 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
1206
1207 // Dump methods whose size is a certain number of standard deviations from the mean
1208 size_t dumped_values = 0;
1209 size_t skipped_values = 0;
1210 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
1211 size_t cur_size_variance = i * i * size_variance;
1212 bool first = true;
1213 for (size_t j = 0; j < n; j++) {
1214 size_t cur_size = method_outlier_size[j];
1215 if (cur_size > size_mean) {
1216 size_t cur_var = cur_size - size_mean;
1217 cur_var = cur_var * cur_var;
1218 if (cur_var > cur_size_variance) {
1219 if (dumped_values > 20) {
1220 if (i == 1) {
1221 skipped_values++;
1222 } else {
1223 i = 2; // jump to counting for 1 standard deviation
1224 break;
1225 }
1226 } else {
1227 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001228 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001229 first = false;
1230 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001231 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07001232 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001233 method_outlier_size[j] = 0; // don't consider this method again
1234 dumped_values++;
1235 }
1236 }
1237 }
1238 }
1239 }
1240 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001241 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001242 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001243 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001244 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001245
1246 // Dump methods whose expansion is a certain number of standard deviations from the mean
1247 dumped_values = 0;
1248 skipped_values = 0;
1249 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
1250 double cur_expansion_variance = i * i * expansion_variance;
1251 bool first = true;
1252 for (size_t j = 0; j < n; j++) {
1253 double cur_expansion = method_outlier_expansion[j];
1254 if (cur_expansion > expansion_mean) {
1255 size_t cur_var = cur_expansion - expansion_mean;
1256 cur_var = cur_var * cur_var;
1257 if (cur_var > cur_expansion_variance) {
1258 if (dumped_values > 20) {
1259 if (i == 1) {
1260 skipped_values++;
1261 } else {
1262 i = 2; // jump to counting for 1 standard deviation
1263 break;
1264 }
1265 } else {
1266 if (first) {
1267 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07001268 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001269 first = false;
1270 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001271 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07001272 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001273 method_outlier_expansion[j] = 0.0; // don't consider this method again
1274 dumped_values++;
1275 }
1276 }
1277 }
1278 }
1279 }
1280 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001281 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001282 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001283 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001284 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001285 }
1286
Ian Rogersb726dcb2012-09-05 08:57:23 -07001287 void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001288 {
1289 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
1290 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
1291 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1292 std::ostream indent_os(&indent_filter);
1293 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
1294 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
1295 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
1296 header_bytes, PercentOfFileBytes(header_bytes),
1297 object_bytes, PercentOfFileBytes(object_bytes),
1298 alignment_bytes, PercentOfFileBytes(alignment_bytes))
1299 << std::flush;
1300 CHECK_EQ(file_bytes, header_bytes + object_bytes + alignment_bytes);
1301 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001302
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001303 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001304 size_t object_bytes_total = 0;
Elliott Hughesa0e18062012-04-13 15:59:59 -07001305 typedef SizeAndCountTable::const_iterator It; // TODO: C++0x auto
1306 for (It it = sizes_and_counts.begin(), end = sizes_and_counts.end(); it != end; ++it) {
Elliott Hughes95572412011-12-13 18:14:20 -08001307 const std::string& descriptor(it->first);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001308 double average = static_cast<double>(it->second.bytes) / static_cast<double>(it->second.count);
1309 double percent = PercentOfObjectBytes(it->second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001310 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07001311 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
1312 descriptor.c_str(), it->second.bytes, it->second.count,
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001313 average, percent);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001314 object_bytes_total += it->second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001315 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001316 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001317 CHECK_EQ(object_bytes, object_bytes_total);
1318
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001319 os << StringPrintf("oat_file_bytes = %8zd\n"
1320 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1321 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1322 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
1323 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1324 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1325 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07001326 oat_file_bytes,
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001327 managed_code_bytes, PercentOfOatBytes(managed_code_bytes),
1328 managed_to_native_code_bytes, PercentOfOatBytes(managed_to_native_code_bytes),
Ian Rogers0d2d3782012-04-10 11:09:18 -07001329 native_to_managed_code_bytes, PercentOfOatBytes(native_to_managed_code_bytes),
1330 class_initializer_code_bytes, PercentOfOatBytes(class_initializer_code_bytes),
1331 large_initializer_code_bytes, PercentOfOatBytes(large_initializer_code_bytes),
1332 large_method_code_bytes, PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001333 << "DexFile sizes:\n";
Ian Rogers05f28c62012-10-23 18:12:13 -07001334 typedef std::vector<std::pair<std::string, size_t> >::const_iterator It2;
1335 for (It2 it = oat_dex_file_sizes.begin(); it != oat_dex_file_sizes.end();
1336 ++it) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001337 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
1338 it->first.c_str(), it->second, PercentOfOatBytes(it->second));
Ian Rogers05f28c62012-10-23 18:12:13 -07001339 }
1340
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001341 os << "\n" << StringPrintf("gc_map_bytes = %7zd (%2.0f%% of oat file bytes)\n"
1342 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
1343 "vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07001344 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
1345 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
1346 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001347 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001348
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001349 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
1350 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Elliott Hughesc073b072012-05-24 19:29:17 -07001351 static_cast<double>(managed_code_bytes) / static_cast<double>(dex_instruction_bytes),
1352 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07001353 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001354 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001355
1356 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001357 }
1358 } stats_;
1359
1360 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07001361 enum {
1362 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
1363 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1364 kLargeConstructorDexBytes = 4000,
1365 // Number of bytes for a method to be considered large. Based on the 4000 basic block
1366 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1367 kLargeMethodDexBytes = 16000
1368 };
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001369 UniquePtr<OatDumper> oat_dumper_;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001370 std::ostream* os_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001371 const std::string image_filename_;
1372 const std::string host_prefix_;
1373 Space& image_space_;
1374 const ImageHeader& image_header_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07001375
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001376 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001377};
1378
Elliott Hughes72395bf2012-04-24 13:45:26 -07001379static int oatdump(int argc, char** argv) {
Elliott Hughes0d39c122012-06-06 16:41:17 -07001380 InitLogging(argv);
Elliott Hughes72395bf2012-04-24 13:45:26 -07001381
Brian Carlstrom78128a62011-09-15 17:21:19 -07001382 // Skip over argv[0].
1383 argv++;
1384 argc--;
1385
1386 if (argc == 0) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001387 fprintf(stderr, "No arguments specified\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -07001388 usage();
1389 }
1390
Brian Carlstromaded5f72011-10-07 17:15:04 -07001391 const char* oat_filename = NULL;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001392 const char* image_filename = NULL;
1393 const char* boot_image_filename = NULL;
Logan Chien0cc6ab62012-03-20 22:57:52 +08001394 std::string elf_filename_prefix;
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001395 UniquePtr<std::string> host_prefix;
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001396 std::ostream* os = &std::cout;
1397 UniquePtr<std::ofstream> out;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001398
1399 for (int i = 0; i < argc; i++) {
1400 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -08001401 if (option.starts_with("--oat-file=")) {
1402 oat_filename = option.substr(strlen("--oat-file=")).data();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001403 } else if (option.starts_with("--image=")) {
Brian Carlstrom78128a62011-09-15 17:21:19 -07001404 image_filename = option.substr(strlen("--image=")).data();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001405 } else if (option.starts_with("--boot-image=")) {
1406 boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001407 } else if (option.starts_with("--host-prefix=")) {
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001408 host_prefix.reset(new std::string(option.substr(strlen("--host-prefix=")).data()));
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001409 } else if (option.starts_with("--output=")) {
1410 const char* filename = option.substr(strlen("--output=")).data();
1411 out.reset(new std::ofstream(filename));
1412 if (!out->good()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001413 fprintf(stderr, "Failed to open output filename %s\n", filename);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001414 usage();
1415 }
1416 os = out.get();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001417 } else {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001418 fprintf(stderr, "Unknown argument %s\n", option.data());
Brian Carlstrom78128a62011-09-15 17:21:19 -07001419 usage();
1420 }
1421 }
1422
Brian Carlstromaded5f72011-10-07 17:15:04 -07001423 if (image_filename == NULL && oat_filename == NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001424 fprintf(stderr, "Either --image or --oat must be specified\n");
1425 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001426 }
1427
Brian Carlstromaded5f72011-10-07 17:15:04 -07001428 if (image_filename != NULL && oat_filename != NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001429 fprintf(stderr, "Either --image or --oat must be specified but not both\n");
1430 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001431 }
1432
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001433 if (host_prefix.get() == NULL) {
Brian Carlstrom81f3ca12012-03-17 00:27:35 -07001434 const char* android_product_out = getenv("ANDROID_PRODUCT_OUT");
1435 if (android_product_out != NULL) {
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001436 host_prefix.reset(new std::string(android_product_out));
1437 } else {
1438 host_prefix.reset(new std::string(""));
Brian Carlstrom81f3ca12012-03-17 00:27:35 -07001439 }
1440 }
1441
Brian Carlstromaded5f72011-10-07 17:15:04 -07001442 if (oat_filename != NULL) {
Logan Chien0c717dd2012-03-28 18:31:07 +08001443 OatFile* oat_file =
Brian Carlstrom1cac3432012-12-12 10:56:22 -08001444 OatFile::Open(oat_filename, oat_filename, NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001445 if (oat_file == NULL) {
1446 fprintf(stderr, "Failed to open oat file from %s\n", oat_filename);
1447 return EXIT_FAILURE;
1448 }
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001449 OatDumper oat_dumper(*host_prefix.get(), *oat_file);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001450 oat_dumper.Dump(*os);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001451 return EXIT_SUCCESS;
1452 }
1453
Brian Carlstrom78128a62011-09-15 17:21:19 -07001454 Runtime::Options options;
1455 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001456 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001457 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001458 std::string boot_oat_option;
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001459 if (boot_image_filename != NULL) {
1460 boot_image_option += "-Ximage:";
1461 boot_image_option += boot_image_filename;
1462 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom78128a62011-09-15 17:21:19 -07001463 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001464 if (image_filename != NULL) {
1465 image_option += "-Ximage:";
1466 image_option += image_filename;
1467 options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL)));
1468 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001469
Brian Carlstrom13c492b2012-03-22 17:09:17 -07001470 if (!host_prefix->empty()) {
1471 options.push_back(std::make_pair("host-prefix", host_prefix->c_str()));
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001472 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001473
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001474 if (!Runtime::Create(options, false)) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001475 fprintf(stderr, "Failed to create runtime\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -07001476 return EXIT_FAILURE;
1477 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001478 UniquePtr<Runtime> runtime(Runtime::Current());
1479 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
1480 // give it away now and then switch to a more managable ScopedObjectAccess.
1481 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
1482 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom78128a62011-09-15 17:21:19 -07001483
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001484 Heap* heap = Runtime::Current()->GetHeap();
Brian Carlstromfddf6f62012-03-15 16:56:45 -07001485 ImageSpace* image_space = heap->GetImageSpace();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001486 CHECK(image_space != NULL);
1487 const ImageHeader& image_header = image_space->GetImageHeader();
1488 if (!image_header.IsValid()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001489 fprintf(stderr, "Invalid image header %s\n", image_filename);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001490 return EXIT_FAILURE;
1491 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001492 ImageDumper image_dumper(os, image_filename, *host_prefix.get(), *image_space, image_header);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001493 image_dumper.Dump();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001494 return EXIT_SUCCESS;
1495}
1496
1497} // namespace art
1498
1499int main(int argc, char** argv) {
1500 return art::oatdump(argc, argv);
1501}