blob: 7607bf0f54b783929264a5ab08e007cf3fe08f88 [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 Rogers22d5e732014-07-15 22:23:51 -070032#include "field_helper.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080033#include "gc_map.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070034#include "gc/space/image_space.h"
35#include "gc/space/large_object_space.h"
36#include "gc/space/space-inl.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070037#include "image.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080038#include "indenter.h"
Ian Rogers1809a722013-08-09 22:05:32 -070039#include "mapping_table.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070040#include "mirror/art_field-inl.h"
41#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042#include "mirror/array-inl.h"
43#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044#include "mirror/object-inl.h"
45#include "mirror/object_array-inl.h"
Brian Carlstromc0a1b182014-03-04 23:19:06 -080046#include "noop_compiler_callbacks.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080047#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010048#include "oat_file-inl.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080049#include "os.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070050#include "runtime.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070051#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070052#include "scoped_thread_state_change.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080053#include "thread_list.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080054#include "verifier/dex_gc_map.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080055#include "verifier/method_verifier.h"
Ian Rogers1809a722013-08-09 22:05:32 -070056#include "vmap_table.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070057
58namespace art {
59
60static void usage() {
61 fprintf(stderr,
62 "Usage: oatdump [options] ...\n"
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +000063 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080064 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070065 "\n");
66 fprintf(stderr,
Brian Carlstroma6cc8932012-01-04 14:44:07 -080067 " --oat-file=<file.oat>: specifies an input oat filename.\n"
TDYa127a0a2a6c2012-10-16 22:47:38 -070068 " Example: --oat-file=/system/framework/boot.oat\n"
Brian Carlstromaded5f72011-10-07 17:15:04 -070069 "\n");
70 fprintf(stderr,
71 " --image=<file.art>: specifies an input image filename.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080072 " Example: --image=/system/framework/boot.art\n"
Brian Carlstrome24fa612011-09-29 00:53:55 -070073 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -070074 fprintf(stderr,
Brian Carlstrome24fa612011-09-29 00:53:55 -070075 " --boot-image=<file.art>: provide the image file for the boot class path.\n"
Brian Carlstrom29e7ac72011-12-05 23:42:57 -080076 " Example: --boot-image=/system/framework/boot.art\n"
Brian Carlstrom78128a62011-09-15 17:21:19 -070077 "\n");
Brian Carlstrome24fa612011-09-29 00:53:55 -070078 fprintf(stderr,
Brian Carlstrom34fa79e2014-09-14 20:34:17 -070079 " --instruction-set=(arm|arm64|mips|x86|x86_64): for locating the image\n"
80 " file based on the image location set.\n"
Brian Carlstrom0f5baa02014-05-22 11:54:18 -070081 " Example: --instruction-set=x86\n"
82 " Default: %s\n"
83 "\n",
84 GetInstructionSetString(kRuntimeISA));
85 fprintf(stderr,
Brian Carlstrom27ec9612011-09-19 20:20:38 -070086 " --output=<file> may be used to send the output to a file.\n"
87 " Example: --output=/tmp/oatdump.txt\n"
88 "\n");
Dave Allison404f59f2014-02-24 11:10:01 -080089 fprintf(stderr,
Brian Carlstrom34fa79e2014-09-14 20:34:17 -070090 " --dump:raw_mapping_table enables dumping of the mapping table.\n"
91 " Example: --dump:raw_mapping_table\n"
92 "\n");
93 fprintf(stderr,
94 " --dump:raw_mapping_table enables dumping of the GC map.\n"
95 " Example: --dump:raw_gc_map\n"
96 "\n");
97 fprintf(stderr,
98 " --no-dump:vmap may be used to disable vmap dumping.\n"
99 " Example: --no-dump:vmap\n"
100 "\n");
101 fprintf(stderr,
102 " --no-disassemble may be used to disable disassembly.\n"
103 " Example: --no-disassemble\n"
Dave Allison404f59f2014-02-24 11:10:01 -0800104 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -0700105 exit(EXIT_FAILURE);
106}
107
Ian Rogersff1ed472011-09-20 13:46:24 -0700108const char* image_roots_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -0800109 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -0700110 "kImtConflictMethod",
111 "kDefaultImt",
Brian Carlstrome24fa612011-09-29 00:53:55 -0700112 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -0700113 "kRefsOnlySaveMethod",
114 "kRefsAndArgsSaveMethod",
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700115 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700116 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -0700117};
118
Brian Carlstrom34fa79e2014-09-14 20:34:17 -0700119class OatDumperOptions {
120 public:
121 OatDumperOptions(bool dump_raw_mapping_table,
122 bool dump_raw_gc_map,
123 bool dump_vmap,
124 bool disassemble_code,
125 bool absolute_addresses)
126 : dump_raw_mapping_table_(dump_raw_mapping_table),
127 dump_raw_gc_map_(dump_raw_gc_map),
128 dump_vmap_(dump_vmap),
129 disassemble_code_(disassemble_code),
130 absolute_addresses_(absolute_addresses) {}
131
132 const bool dump_raw_mapping_table_;
133 const bool dump_raw_gc_map_;
134 const bool dump_vmap_;
135 const bool disassemble_code_;
136 const bool absolute_addresses_;
137};
138
Elliott Hughese3c845c2012-02-28 17:23:01 -0800139class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700140 public:
Brian Carlstrom34fa79e2014-09-14 20:34:17 -0700141 explicit OatDumper(const OatFile& oat_file, OatDumperOptions* options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000142 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800143 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom34fa79e2014-09-14 20:34:17 -0700144 options_(options),
145 disassembler_(Disassembler::Create(oat_file_.GetOatHeader().GetInstructionSet(),
146 new DisassemblerOptions(options_->absolute_addresses_,
147 oat_file.Begin()))) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800148 AddAllOffsets();
149 }
150
Brian Carlstrom34fa79e2014-09-14 20:34:17 -0700151 ~OatDumper() {
152 delete options_;
153 delete disassembler_;
154 }
155
156 bool Dump(std::ostream& os) {
157 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800158 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700159
160 os << "MAGIC:\n";
161 os << oat_header.GetMagic() << "\n\n";
162
163 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800164 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700165
Elliott Hughesa72ec822012-03-05 17:12:22 -0800166 os << "INSTRUCTION SET:\n";
167 os << oat_header.GetInstructionSet() << "\n\n";
168
Dave Allison70202782013-10-22 17:52:19 -0700169 os << "INSTRUCTION SET FEATURES:\n";
170 os << oat_header.GetInstructionSetFeatures().GetFeatureString() << "\n\n";
171
Brian Carlstromaded5f72011-10-07 17:15:04 -0700172 os << "DEX FILE COUNT:\n";
173 os << oat_header.GetDexFileCount() << "\n\n";
174
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800175#define DUMP_OAT_HEADER_OFFSET(label, offset) \
176 os << label " OFFSET:\n"; \
177 os << StringPrintf("0x%08x", oat_header.offset()); \
Brian Carlstrom34fa79e2014-09-14 20:34:17 -0700178 if (oat_header.offset() != 0 && options_->absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800179 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
180 } \
181 os << StringPrintf("\n\n");
182
183 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
184 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
185 GetInterpreterToInterpreterBridgeOffset);
186 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
187 GetInterpreterToCompiledCodeBridgeOffset);
188 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
189 GetJniDlsymLookupOffset);
190 DUMP_OAT_HEADER_OFFSET("PORTABLE IMT CONFLICT TRAMPOLINE",
191 GetPortableImtConflictTrampolineOffset);
192 DUMP_OAT_HEADER_OFFSET("PORTABLE RESOLUTION TRAMPOLINE",
193 GetPortableResolutionTrampolineOffset);
194 DUMP_OAT_HEADER_OFFSET("PORTABLE TO INTERPRETER BRIDGE",
195 GetPortableToInterpreterBridgeOffset);
196 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
197 GetQuickGenericJniTrampolineOffset);
198 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
199 GetQuickImtConflictTrampolineOffset);
200 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
201 GetQuickResolutionTrampolineOffset);
202 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
203 GetQuickToInterpreterBridgeOffset);
204#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700205
Brian Carlstrom34fa79e2014-09-14 20:34:17 -0700206 os << "IMAGE PATCH DELTA:\n";
207 os << StringPrintf("%d (0x%08x)\n\n",
208 oat_header.GetImagePatchDelta(),
209 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700210
Brian Carlstrom28db0122012-10-18 16:20:41 -0700211 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
212 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
213
214 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800215 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700216
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700217 // Print the key-value store.
218 {
219 os << "KEY VALUE STORE:\n";
220 size_t index = 0;
221 const char* key;
222 const char* value;
223 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
224 os << key << " = " << value << "\n";
225 index++;
226 }
227 os << "\n";
228 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700229
Brian Carlstrom34fa79e2014-09-14 20:34:17 -0700230 if (options_->absolute_addresses_) {
231 os << "BEGIN:\n";
232 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700233
Brian Carlstrom34fa79e2014-09-14 20:34:17 -0700234 os << "END:\n";
235 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
236 }
237
238 os << "SIZE:\n";
239 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700240
241 os << std::flush;
242
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800243 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
244 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe35439ba2014-08-28 14:41:02 -0700245 CHECK(oat_dex_file != nullptr);
Brian Carlstrom34fa79e2014-09-14 20:34:17 -0700246 if (!DumpOatDexFile(os, *oat_dex_file)) {
247 success = false;
248 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700249 }
Brian Carlstrom34fa79e2014-09-14 20:34:17 -0700250 os << std::flush;
251 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700252 }
253
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800254 size_t ComputeSize(const void* oat_data) {
255 if (reinterpret_cast<const byte*>(oat_data) < oat_file_.Begin() ||
256 reinterpret_cast<const byte*>(oat_data) > oat_file_.End()) {
257 return 0; // Address not in oat file
258 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800259 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
260 reinterpret_cast<uintptr_t>(oat_file_.Begin());
261 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800262 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800263 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800264 return end_offset - begin_offset;
265 }
266
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700267 InstructionSet GetInstructionSet() {
268 return oat_file_.GetOatHeader().GetInstructionSet();
269 }
270
Ian Rogersef7d42f2014-01-06 12:55:46 -0800271 const void* GetQuickOatCode(mirror::ArtMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800272 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
273 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700274 CHECK(oat_dex_file != nullptr);
275 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700276 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700277 if (dex_file.get() == nullptr) {
278 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
279 << "': " << error_msg;
280 } else {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700281 const DexFile::ClassDef* class_def =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700282 dex_file->FindClassDef(m->GetDeclaringClassDescriptor());
Andreas Gampe35439ba2014-08-28 14:41:02 -0700283 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700284 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100285 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800286 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100287 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800288 }
289 }
290 }
Andreas Gampe35439ba2014-08-28 14:41:02 -0700291 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800292 }
293
Brian Carlstromaded5f72011-10-07 17:15:04 -0700294 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800295 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800296 // We don't know the length of the code for each method, but we need to know where to stop
297 // when disassembling. What we do know is that a region of code will be followed by some other
298 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
299 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800300 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
301 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe35439ba2014-08-28 14:41:02 -0700302 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700303 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700304 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700305 if (dex_file.get() == nullptr) {
306 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
307 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800308 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800309 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800310 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800311 for (size_t class_def_index = 0;
312 class_def_index < dex_file->NumClassDefs();
313 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800314 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100315 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800316 const byte* class_data = dex_file->GetClassData(class_def);
Andreas Gampe35439ba2014-08-28 14:41:02 -0700317 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800318 ClassDataItemIterator it(*dex_file, class_data);
319 SkipAllFields(it);
320 uint32_t class_method_index = 0;
321 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100322 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800323 it.Next();
324 }
325 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100326 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800327 it.Next();
328 }
329 }
330 }
331 }
332
333 // If the last thing in the file is code for a method, there won't be an offset for the "next"
334 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
335 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800336 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800337 }
338
Brian Carlstrom34fa79e2014-09-14 20:34:17 -0700339 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
340 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
341 }
342
Elliott Hughese3c845c2012-02-28 17:23:01 -0800343 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800344 uint32_t code_offset = oat_method.GetCodeOffset();
345 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
346 code_offset &= ~0x1;
347 }
348 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800349 offsets_.insert(oat_method.GetMappingTableOffset());
350 offsets_.insert(oat_method.GetVmapTableOffset());
Ian Rogers0c7abda2012-09-19 13:33:42 -0700351 offsets_.insert(oat_method.GetNativeGcMapOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800352 }
353
Brian Carlstrom34fa79e2014-09-14 20:34:17 -0700354 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
355 bool success = true;
356 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800357 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800358 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700359
360 // Create the verifier early.
361
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700362 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700363 std::unique_ptr<const DexFile> dex_file(oat_dex_file.OpenDexFile(&error_msg));
Andreas Gampe35439ba2014-08-28 14:41:02 -0700364 if (dex_file.get() == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700365 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom34fa79e2014-09-14 20:34:17 -0700366 os << std::flush;
367 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700368 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800369 for (size_t class_def_index = 0;
370 class_def_index < dex_file->NumClassDefs();
371 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700372 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
373 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Brian Carlstrom34fa79e2014-09-14 20:34:17 -0700374 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100375 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom34fa79e2014-09-14 20:34:17 -0700376 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
377 class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100378 << " (" << oat_class.GetStatus() << ")"
379 << " (" << oat_class.GetType() << ")\n";
380 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800381 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
382 std::ostream indented_os(&indent_filter);
Brian Carlstrom34fa79e2014-09-14 20:34:17 -0700383 if (!DumpOatClass(indented_os, oat_class, *(dex_file.get()), class_def)) {
384 success = false;
385 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700386 }
387
388 os << std::flush;
Brian Carlstrom34fa79e2014-09-14 20:34:17 -0700389 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700390 }
391
Elliott Hughese3c845c2012-02-28 17:23:01 -0800392 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700393 while (it.HasNextStaticField()) {
394 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700395 }
Ian Rogers0571d352011-11-03 19:51:38 -0700396 while (it.HasNextInstanceField()) {
397 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700398 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800399 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700400
Brian Carlstrom34fa79e2014-09-14 20:34:17 -0700401 bool DumpOatClass(std::ostream& os, const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800402 const DexFile::ClassDef& class_def) {
Brian Carlstrom34fa79e2014-09-14 20:34:17 -0700403 bool success = true;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800404 const byte* class_data = dex_file.GetClassData(class_def);
Andreas Gampe35439ba2014-08-28 14:41:02 -0700405 if (class_data == nullptr) { // empty class such as a marker interface?
Brian Carlstrom34fa79e2014-09-14 20:34:17 -0700406 os << std::flush;
407 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800408 }
409 ClassDataItemIterator it(dex_file, class_data);
410 SkipAllFields(it);
Brian Carlstrom34fa79e2014-09-14 20:34:17 -0700411 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700412 while (it.HasNextDirectMethod()) {
Brian Carlstrom34fa79e2014-09-14 20:34:17 -0700413 if (!DumpOatMethod(os, class_def, class_method_index, oat_class, dex_file,
414 it.GetMemberIndex(), it.GetMethodCodeItem(),
415 it.GetRawMemberAccessFlags())) {
416 success = false;
417 }
418 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700419 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700420 }
Ian Rogers0571d352011-11-03 19:51:38 -0700421 while (it.HasNextVirtualMethod()) {
Brian Carlstrom34fa79e2014-09-14 20:34:17 -0700422 if (!DumpOatMethod(os, class_def, class_method_index, oat_class, dex_file,
423 it.GetMemberIndex(), it.GetMethodCodeItem(),
424 it.GetRawMemberAccessFlags())) {
425 success = false;
426 }
427 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700428 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700429 }
Ian Rogers0571d352011-11-03 19:51:38 -0700430 DCHECK(!it.HasNext());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700431 os << std::flush;
Brian Carlstrom34fa79e2014-09-14 20:34:17 -0700432 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700433 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800434
Brian Carlstrom34fa79e2014-09-14 20:34:17 -0700435 static constexpr uint32_t kPrologueBytes = 16;
436
437 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
438 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
439
440 bool DumpOatMethod(std::ostream& os, const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700441 uint32_t class_method_index,
Brian Carlstrom34fa79e2014-09-14 20:34:17 -0700442 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800443 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
444 uint32_t method_access_flags) {
Brian Carlstrom34fa79e2014-09-14 20:34:17 -0700445 bool success = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800446 os << StringPrintf("%d: %s (dex_method_idx=%d)\n",
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700447 class_method_index, PrettyMethod(dex_method_idx, dex_file, true).c_str(),
448 dex_method_idx);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800449 Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700450 std::unique_ptr<std::ostream> indent1_os(new std::ostream(&indent1_filter));
451 Indenter indent2_filter(indent1_os->rdbuf(), kIndentChar, kIndentBy1Count);
452 std::unique_ptr<std::ostream> indent2_os(new std::ostream(&indent2_filter));
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800453 {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700454 *indent1_os << "DEX CODE:\n";
455 DumpDexCode(*indent2_os, dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700456 }
Andreas Gampe35439ba2014-08-28 14:41:02 -0700457
458 std::unique_ptr<verifier::MethodVerifier> verifier;
459 if (Runtime::Current() != nullptr) {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700460 *indent1_os << "VERIFIER TYPE ANALYSIS:\n";
Andreas Gampe35439ba2014-08-28 14:41:02 -0700461 verifier.reset(DumpVerifier(*indent2_os, dex_method_idx, &dex_file, class_def, code_item,
462 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -0700463 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800464
Brian Carlstrom34fa79e2014-09-14 20:34:17 -0700465 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
466 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
467 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
468 {
469 *indent1_os << "OatMethodOffsets ";
470 if (options_->absolute_addresses_) {
471 *indent1_os << StringPrintf("%p ", oat_method_offsets);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800472 }
Brian Carlstrom34fa79e2014-09-14 20:34:17 -0700473 *indent1_os << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
474 if (oat_method_offsets_offset > oat_file_.Size()) {
475 *indent1_os << StringPrintf(
476 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
477 oat_method_offsets_offset, oat_file_.Size());
478 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
479 os << std::flush;
480 return false;
481 }
482
483 uint32_t code_offset = oat_method.GetCodeOffset();
484 *indent2_os << StringPrintf("code_offset: 0x%08x ", code_offset);
485 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
486 if (aligned_code_begin > oat_file_.Size()) {
487 *indent2_os << StringPrintf("WARNING: "
488 "code offset 0x%08x is past end of file 0x%08zx.\n",
489 aligned_code_begin, oat_file_.Size());
490 success = false;
491 }
492 *indent2_os << "\n";
493
494 *indent2_os << "gc_map: ";
495 if (options_->absolute_addresses_) {
496 *indent2_os << StringPrintf("%p ", oat_method.GetNativeGcMap());
497 }
498 uint32_t gc_map_offset = oat_method.GetNativeGcMapOffset();
499 *indent2_os << StringPrintf("(offset=0x%08x)\n", gc_map_offset);
500 if (gc_map_offset > oat_file_.Size()) {
501 *indent2_os << StringPrintf("WARNING: "
502 "gc map table offset 0x%08x is past end of file 0x%08zx.\n",
503 gc_map_offset, oat_file_.Size());
504 success = false;
505 } else if (options_->dump_raw_gc_map_) {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700506 Indenter indent3_filter(indent2_os->rdbuf(), kIndentChar, kIndentBy1Count);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800507 std::ostream indent3_os(&indent3_filter);
508 DumpGcMap(indent3_os, oat_method, code_item);
509 }
510 }
511 {
Brian Carlstrom34fa79e2014-09-14 20:34:17 -0700512 *indent1_os << "OatQuickMethodHeader ";
513 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
514 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700515
Brian Carlstrom34fa79e2014-09-14 20:34:17 -0700516 if (options_->absolute_addresses_) {
517 *indent1_os << StringPrintf("%p ", method_header);
518 }
519 *indent1_os << StringPrintf("(offset=0x%08x)\n", method_header_offset);
520 if (method_header_offset > oat_file_.Size()) {
521 *indent1_os << StringPrintf(
522 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
523 method_header_offset, oat_file_.Size());
524 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
525 os << std::flush;
526 return false;
527 }
528
529 *indent2_os << "mapping_table: ";
530 if (options_->absolute_addresses_) {
531 *indent2_os << StringPrintf("%p ", oat_method.GetMappingTable());
532 }
533 uint32_t mapping_table_offset = oat_method.GetMappingTableOffset();
534 *indent2_os << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset());
535 if (mapping_table_offset > oat_file_.Size()) {
536 *indent2_os << StringPrintf("WARNING: "
537 "mapping table offset 0x%08x is past end of file 0x%08zx. "
538 "mapping table offset was loaded from offset 0x%08x.\n",
539 mapping_table_offset, oat_file_.Size(),
540 oat_method.GetMappingTableOffsetOffset());
541 success = false;
542 } else if (options_->dump_raw_mapping_table_) {
543 Indenter indent3_filter(indent2_os->rdbuf(), kIndentChar, kIndentBy1Count);
544 std::ostream indent3_os(&indent3_filter);
545 DumpMappingTable(indent3_os, oat_method);
546 }
547
548 *indent2_os << "vmap_table: ";
549 if (options_->absolute_addresses_) {
550 *indent2_os << StringPrintf("%p ", oat_method.GetVmapTable());
551 }
552 uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
553 *indent2_os << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
554 if (vmap_table_offset > oat_file_.Size()) {
555 *indent2_os << StringPrintf("WARNING: "
556 "vmap table offset 0x%08x is past end of file 0x%08zx. "
557 "vmap table offset was loaded from offset 0x%08x.\n",
558 vmap_table_offset, oat_file_.Size(),
559 oat_method.GetVmapTableOffsetOffset());
560 success = false;
561 } else if (options_->dump_vmap_) {
562 DumpVmap(*indent2_os, oat_method);
563 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800564 }
Brian Carlstrom34fa79e2014-09-14 20:34:17 -0700565 {
566 *indent1_os << "QuickMethodFrameInfo\n";
567
568 *indent2_os << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
569 *indent2_os << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
570 DumpSpillMask(*indent2_os, oat_method.GetCoreSpillMask(), false);
571 *indent2_os << "\n";
572 *indent2_os << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
573 DumpSpillMask(*indent2_os, oat_method.GetFpSpillMask(), true);
574 *indent2_os << "\n";
575 }
576 {
577 *indent1_os << "CODE: ";
578 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
579 if (code_size_offset > oat_file_.Size()) {
580 *indent2_os << StringPrintf("WARNING: "
581 "code size offset 0x%08x is past end of file 0x%08zx.",
582 code_size_offset, oat_file_.Size());
583 success = false;
584 } else {
585 const void* code = oat_method.GetQuickCode();
586 uint32_t code_size = oat_method.GetQuickCodeSize();
587 if (code == nullptr) {
588 code = oat_method.GetPortableCode();
589 code_size = oat_method.GetPortableCodeSize();
590 code_size_offset = 0;
591 }
592 uint32_t code_offset = oat_method.GetCodeOffset();
593 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
594 uint64_t aligned_code_end = aligned_code_begin + code_size;
595
596 if (options_->absolute_addresses_) {
597 *indent1_os << StringPrintf("%p ", code);
598 }
599 *indent1_os << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
600 code_offset,
601 code_size_offset,
602 code_size,
603 code != nullptr ? "..." : "");
604
605 if (aligned_code_begin > oat_file_.Size()) {
606 *indent2_os << StringPrintf("WARNING: "
607 "start of code at 0x%08x is past end of file 0x%08zx.",
608 aligned_code_begin, oat_file_.Size());
609 success = false;
610 } else if (aligned_code_end > oat_file_.Size()) {
611 *indent2_os << StringPrintf("WARNING: "
612 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
613 "code size is 0x%08x loaded from offset 0x%08x.\n",
614 aligned_code_end, oat_file_.Size(),
615 code_size, code_size_offset);
616 success = false;
617 if (options_->disassemble_code_) {
618 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
619 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, true, kPrologueBytes);
620 }
621 }
622 } else if (code_size > kMaxCodeSize) {
623 *indent2_os << StringPrintf("WARNING: "
624 "code size %d is bigger than max expected threshold of %d. "
625 "code size is 0x%08x loaded from offset 0x%08x.\n",
626 code_size, kMaxCodeSize,
627 code_size, code_size_offset);
628 success = false;
629 if (options_->disassemble_code_) {
630 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
631 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, true, kPrologueBytes);
632 }
633 }
634 } else if (options_->disassemble_code_) {
635 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, !success, 0);
636 }
637 }
638 }
639 os << std::flush;
640 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700641 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800642
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800643 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
644 if (spill_mask == 0) {
645 return;
646 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800647 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800648 for (size_t i = 0; i < 32; i++) {
649 if ((spill_mask & (1 << i)) != 0) {
650 if (is_float) {
651 os << "fr" << i;
652 } else {
653 os << "r" << i;
654 }
655 spill_mask ^= 1 << i; // clear bit
656 if (spill_mask != 0) {
657 os << ", ";
658 } else {
659 break;
660 }
661 }
662 }
663 os << ")";
664 }
665
Ian Rogersb23a7722012-10-09 16:54:26 -0700666 void DumpVmap(std::ostream& os, const OatFile::OatMethod& oat_method) {
Ian Rogers1809a722013-08-09 22:05:32 -0700667 const uint8_t* raw_table = oat_method.GetVmapTable();
Andreas Gampe35439ba2014-08-28 14:41:02 -0700668 if (raw_table != nullptr) {
Ian Rogers1809a722013-08-09 22:05:32 -0700669 const VmapTable vmap_table(raw_table);
670 bool first = true;
671 bool processing_fp = false;
672 uint32_t spill_mask = oat_method.GetCoreSpillMask();
673 for (size_t i = 0; i < vmap_table.Size(); i++) {
674 uint16_t dex_reg = vmap_table[i];
675 uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
676 processing_fp ? kFloatVReg : kIntVReg);
677 os << (first ? "v" : ", v") << dex_reg;
678 if (!processing_fp) {
679 os << "/r" << cpu_reg;
680 } else {
681 os << "/fr" << cpu_reg;
682 }
683 first = false;
684 if (!processing_fp && dex_reg == 0xFFFF) {
685 processing_fp = true;
686 spill_mask = oat_method.GetFpSpillMask();
687 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800688 }
Ian Rogers1809a722013-08-09 22:05:32 -0700689 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800690 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800691 }
692
Ian Rogersb23a7722012-10-09 16:54:26 -0700693 void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800694 const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
Ian Rogers1809a722013-08-09 22:05:32 -0700695 const uint8_t* raw_table = oat_method.GetVmapTable();
Andreas Gampe35439ba2014-08-28 14:41:02 -0700696 if (raw_table != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -0700697 const VmapTable vmap_table(raw_table);
698 uint32_t vmap_offset;
Ian Rogers1809a722013-08-09 22:05:32 -0700699 if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800700 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
701 uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
702 : oat_method.GetCoreSpillMask();
703 os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
Ian Rogersb23a7722012-10-09 16:54:26 -0700704 } else {
705 uint32_t offset = StackVisitor::GetVRegOffset(code_item, oat_method.GetCoreSpillMask(),
706 oat_method.GetFpSpillMask(),
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000707 oat_method.GetFrameSizeInBytes(), reg,
708 GetInstructionSet());
Ian Rogersb23a7722012-10-09 16:54:26 -0700709 os << "[sp + #" << offset << "]";
710 }
711 }
712 }
713
Ian Rogersef7d42f2014-01-06 12:55:46 -0800714 void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
715 const DexFile::CodeItem* code_item,
716 size_t num_regs, const uint8_t* reg_bitmap) {
717 bool first = true;
718 for (size_t reg = 0; reg < num_regs; reg++) {
719 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
720 if (first) {
721 os << " v" << reg << " (";
722 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
723 os << ")";
724 first = false;
725 } else {
726 os << ", v" << reg << " (";
727 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
728 os << ")";
729 }
730 }
731 }
732 if (first) {
733 os << "No registers in GC map\n";
734 } else {
735 os << "\n";
736 }
737 }
Ian Rogersb23a7722012-10-09 16:54:26 -0700738 void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
739 const DexFile::CodeItem* code_item) {
740 const uint8_t* gc_map_raw = oat_method.GetNativeGcMap();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800741 if (gc_map_raw == nullptr) {
742 return; // No GC map.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800743 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800744 const void* quick_code = oat_method.GetQuickCode();
745 if (quick_code != nullptr) {
746 NativePcOffsetToReferenceMap map(gc_map_raw);
747 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
748 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
749 map.GetNativePcOffset(entry);
750 os << StringPrintf("%p", native_pc);
751 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800752 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800753 } else {
754 const void* portable_code = oat_method.GetPortableCode();
755 CHECK(portable_code != nullptr);
756 verifier::DexPcToReferenceMap map(gc_map_raw);
757 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
758 uint32_t dex_pc = map.GetDexPc(entry);
759 os << StringPrintf("0x%08x", dex_pc);
760 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
761 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800762 }
763 }
764
765 void DumpMappingTable(std::ostream& os, const OatFile::OatMethod& oat_method) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800766 const void* quick_code = oat_method.GetQuickCode();
767 if (quick_code == nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800768 return;
769 }
Ian Rogers1809a722013-08-09 22:05:32 -0700770 MappingTable table(oat_method.GetMappingTable());
771 if (table.TotalSize() != 0) {
772 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
773 std::ostream indent_os(&indent_filter);
774 if (table.PcToDexSize() != 0) {
775 typedef MappingTable::PcToDexIterator It;
776 os << "suspend point mappings {\n";
777 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
778 indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
779 }
780 os << "}\n";
781 }
782 if (table.DexToPcSize() != 0) {
783 typedef MappingTable::DexToPcIterator It;
784 os << "catch entry mappings {\n";
785 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
786 indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
787 }
788 os << "}\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800789 }
790 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800791 }
792
Ian Rogers1809a722013-08-09 22:05:32 -0700793 uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
794 size_t offset, bool suspend_point_mapping) {
795 MappingTable table(oat_method.GetMappingTable());
796 if (suspend_point_mapping && table.PcToDexSize() > 0) {
797 typedef MappingTable::PcToDexIterator It;
798 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
799 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -0700800 os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -0700801 return cur.DexPc();
802 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800803 }
Ian Rogers1809a722013-08-09 22:05:32 -0700804 } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
805 typedef MappingTable::DexToPcIterator It;
806 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
807 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -0700808 os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -0700809 return cur.DexPc();
Ian Rogersb23a7722012-10-09 16:54:26 -0700810 }
811 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800812 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800813 return DexFile::kDexNoIndex;
Ian Rogersb23a7722012-10-09 16:54:26 -0700814 }
815
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800816 void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
817 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
Ian Rogersb23a7722012-10-09 16:54:26 -0700818 const uint8_t* gc_map_raw = oat_method.GetNativeGcMap();
Andreas Gampe35439ba2014-08-28 14:41:02 -0700819 if (gc_map_raw != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -0700820 NativePcOffsetToReferenceMap map(gc_map_raw);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800821 if (map.HasEntry(native_pc_offset)) {
Ian Rogersb23a7722012-10-09 16:54:26 -0700822 size_t num_regs = map.RegWidth() * 8;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800823 const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
Ian Rogersb23a7722012-10-09 16:54:26 -0700824 bool first = true;
825 for (size_t reg = 0; reg < num_regs; reg++) {
826 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
827 if (first) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800828 os << "GC map objects: v" << reg << " (";
829 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -0700830 os << ")";
831 first = false;
832 } else {
833 os << ", v" << reg << " (";
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800834 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -0700835 os << ")";
836 }
837 }
838 }
839 if (!first) {
840 os << "\n";
841 }
842 }
843 }
844 }
845
Mathieu Chartier590fee92013-09-13 13:46:47 -0700846 void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
847 const OatFile::OatMethod& oat_method,
848 const DexFile::CodeItem* code_item, uint32_t dex_pc) {
849 DCHECK(verifier != nullptr);
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800850 std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800851 bool first = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800852 for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
853 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
854 if (kind != kUndefined) {
855 if (first) {
856 os << "VRegs: v";
857 first = false;
858 } else {
859 os << ", v";
860 }
861 os << reg << " (";
862 switch (kind) {
863 case kImpreciseConstant:
864 os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
865 DescribeVReg(os, oat_method, code_item, reg, kind);
866 break;
867 case kConstant:
868 os << "Constant: " << kinds.at((reg * 2) + 1);
869 break;
870 default:
871 DescribeVReg(os, oat_method, code_item, reg, kind);
872 break;
873 }
874 os << ")";
875 }
876 }
877 if (!first) {
878 os << "\n";
879 }
880 }
881
882
Ian Rogersb23a7722012-10-09 16:54:26 -0700883 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe35439ba2014-08-28 14:41:02 -0700884 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -0700885 size_t i = 0;
886 while (i < code_item->insns_size_in_code_units_) {
887 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800888 os << StringPrintf("0x%04zx: %s\n", i, instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -0700889 i += instruction->SizeInCodeUnits();
890 }
891 }
892 }
893
Andreas Gampe35439ba2014-08-28 14:41:02 -0700894 verifier::MethodVerifier* DumpVerifier(std::ostream& os, uint32_t dex_method_idx,
895 const DexFile* dex_file,
896 const DexFile::ClassDef& class_def,
897 const DexFile::CodeItem* code_item,
898 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800899 if ((method_access_flags & kAccNative) == 0) {
900 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700901 StackHandleScope<2> hs(soa.Self());
902 Handle<mirror::DexCache> dex_cache(
903 hs.NewHandle(Runtime::Current()->GetClassLinker()->FindDexCache(*dex_file)));
904 auto class_loader(hs.NewHandle<mirror::ClassLoader>(nullptr));
Andreas Gampe35439ba2014-08-28 14:41:02 -0700905 return verifier::MethodVerifier::VerifyMethodAndDump(os, dex_method_idx, dex_file, dex_cache,
906 class_loader, &class_def, code_item,
907 nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800908 }
Andreas Gampe35439ba2014-08-28 14:41:02 -0700909
910 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800911 }
912
Mathieu Chartier590fee92013-09-13 13:46:47 -0700913 void DumpCode(std::ostream& os, verifier::MethodVerifier* verifier,
Brian Carlstrom34fa79e2014-09-14 20:34:17 -0700914 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
915 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800916 const void* portable_code = oat_method.GetPortableCode();
917 const void* quick_code = oat_method.GetQuickCode();
918
Brian Carlstrom34fa79e2014-09-14 20:34:17 -0700919 if (code_size == 0) {
920 code_size = oat_method.GetQuickCodeSize();
921 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800922 if ((code_size == 0) || ((portable_code == nullptr) && (quick_code == nullptr))) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800923 os << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -0700924 return;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800925 } else if (quick_code != nullptr) {
926 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
927 size_t offset = 0;
928 while (offset < code_size) {
Brian Carlstrom34fa79e2014-09-14 20:34:17 -0700929 if (!bad_input) {
930 DumpMappingAtOffset(os, oat_method, offset, false);
931 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800932 offset += disassembler_->Dump(os, quick_native_pc + offset);
Brian Carlstrom34fa79e2014-09-14 20:34:17 -0700933 if (!bad_input) {
934 uint32_t dex_pc = DumpMappingAtOffset(os, oat_method, offset, true);
935 if (dex_pc != DexFile::kDexNoIndex) {
936 DumpGcMapAtNativePcOffset(os, oat_method, code_item, offset);
937 if (verifier != nullptr) {
938 DumpVRegsAtDexPc(os, verifier, oat_method, code_item, dex_pc);
939 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800940 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800941 }
942 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800943 } else {
944 CHECK(portable_code != nullptr);
945 CHECK_EQ(code_size, 0U); // TODO: disassembly of portable is currently not supported.
Ian Rogersb23a7722012-10-09 16:54:26 -0700946 }
947 }
948
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800949 const OatFile& oat_file_;
Brian Carlstrom34fa79e2014-09-14 20:34:17 -0700950 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
951 const OatDumperOptions* options_;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800952 std::set<uintptr_t> offsets_;
Brian Carlstrom34fa79e2014-09-14 20:34:17 -0700953 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700954};
955
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800956class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700957 public:
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800958 explicit ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space,
Brian Carlstrom34fa79e2014-09-14 20:34:17 -0700959 const ImageHeader& image_header, OatDumperOptions* oat_dumper_options)
960 : os_(os),
961 image_space_(image_space),
962 image_header_(image_header),
963 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700964
Brian Carlstrom34fa79e2014-09-14 20:34:17 -0700965 bool Dump() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800966 std::ostream& os = *os_;
967 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -0700968
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800969 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700970
Mathieu Chartier31e89252013-08-28 11:29:12 -0700971 os << "IMAGE BITMAP OFFSET: " << reinterpret_cast<void*>(image_header_.GetImageBitmapOffset())
972 << " SIZE: " << reinterpret_cast<void*>(image_header_.GetImageBitmapSize()) << "\n\n";
973
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800974 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -0700975
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800976 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700977
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800978 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
979
980 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
981
982 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800983
Alex Lighta59dd802014-07-02 16:28:08 -0700984 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
985
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800986 {
987 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
988 Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
989 std::ostream indent1_os(&indent1_filter);
990 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
991 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
992 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
993 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800994 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800995 indent1_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
996 if (image_root_object->IsObjectArray()) {
997 Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
998 std::ostream indent2_os(&indent2_filter);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800999 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001000 = image_root_object->AsObjectArray<mirror::Object>();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001001 for (int i = 0; i < image_root_object_array->GetLength(); i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001002 mirror::Object* value = image_root_object_array->Get(i);
Ian Rogersfa824272013-11-05 16:12:57 -08001003 size_t run = 0;
1004 for (int32_t j = i + 1; j < image_root_object_array->GetLength(); j++) {
1005 if (value == image_root_object_array->Get(j)) {
1006 run++;
1007 } else {
1008 break;
1009 }
1010 }
1011 if (run == 0) {
1012 indent2_os << StringPrintf("%d: ", i);
1013 } else {
1014 indent2_os << StringPrintf("%d to %zd: ", i, i + run);
1015 i = i + run;
1016 }
Andreas Gampe35439ba2014-08-28 14:41:02 -07001017 if (value != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001018 PrettyObjectValue(indent2_os, value->GetClass(), value);
1019 } else {
1020 indent2_os << i << ": null\n";
1021 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001022 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001023 }
1024 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001025 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001026 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001027
Brian Carlstromaded5f72011-10-07 17:15:04 -07001028 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001029 std::string image_filename = image_space_.GetImageFilename();
1030 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001031 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001032 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001033 std::string error_msg;
Alex Lighta59dd802014-07-02 16:28:08 -07001034 const OatFile* oat_file = class_linker->FindOpenedOatFileFromOatLocation(oat_location);
1035 if (oat_file == nullptr) {
Andreas Gampe35439ba2014-08-28 14:41:02 -07001036 oat_file = OatFile::Open(oat_location, oat_location, nullptr, false, &error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -07001037 if (oat_file == nullptr) {
1038 os << "NOT FOUND: " << error_msg << "\n";
Brian Carlstrom34fa79e2014-09-14 20:34:17 -07001039 return false;
Alex Lighta59dd802014-07-02 16:28:08 -07001040 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001041 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001042 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001043
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001044 stats_.oat_file_bytes = oat_file->Size();
1045
Brian Carlstrom34fa79e2014-09-14 20:34:17 -07001046 oat_dumper_.reset(new OatDumper(*oat_file, oat_dumper_options_.release()));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001047
Mathieu Chartier02e25112013-08-14 16:14:24 -07001048 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe35439ba2014-08-28 14:41:02 -07001049 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001050 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1051 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001052 }
1053
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001054 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001055
1056 // Loop through all the image spaces and dump their objects.
Ian Rogers1d54e732013-05-02 21:10:01 -07001057 gc::Heap* heap = Runtime::Current()->GetHeap();
1058 const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces();
Ian Rogers50b35e22012-10-04 10:09:15 -07001059 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001060 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001061 {
1062 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1063 heap->FlushAllocStack();
1064 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001065 // Since FlushAllocStack() above resets the (active) allocation
1066 // stack. Need to revoke the thread-local allocation stacks that
1067 // point into it.
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001068 {
1069 self->TransitionFromRunnableToSuspended(kNative);
1070 ThreadList* thread_list = Runtime::Current()->GetThreadList();
1071 thread_list->SuspendAll();
1072 heap->RevokeAllThreadLocalAllocationStacks(self);
1073 thread_list->ResumeAll();
1074 self->TransitionFromSuspendedToRunnable();
1075 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001076 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001077 {
1078 std::ostream* saved_os = os_;
1079 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1080 std::ostream indent_os(&indent_filter);
1081 os_ = &indent_os;
1082 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001083 for (const auto& space : spaces) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001084 if (space->IsImageSpace()) {
Ian Rogers1d54e732013-05-02 21:10:01 -07001085 gc::space::ImageSpace* image_space = space->AsImageSpace();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001086 image_space->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1087 indent_os << "\n";
1088 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -07001089 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001090 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001091 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001092 indent_os << "\n";
1093 os_ = saved_os;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001094 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001095 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001096 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Andreas Gampe35439ba2014-08-28 14:41:02 -07001097 if (file.get() == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001098 LOG(WARNING) << "Failed to find image in " << image_filename;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001099 }
Andreas Gampe35439ba2014-08-28 14:41:02 -07001100 if (file.get() != nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001101 stats_.file_bytes = file->GetLength();
Brian Carlstrom6f277752013-09-30 17:56:45 -07001102 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001103 size_t header_bytes = sizeof(ImageHeader);
1104 stats_.header_bytes = header_bytes;
1105 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
1106 stats_.alignment_bytes += alignment_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07001107 stats_.alignment_bytes += image_header_.GetImageBitmapOffset() - image_header_.GetImageSize();
1108 stats_.bitmap_bytes += image_header_.GetImageBitmapSize();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001109 stats_.Dump(os);
1110 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001111
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001112 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001113
Brian Carlstrom34fa79e2014-09-14 20:34:17 -07001114 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001115 }
1116
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001117 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001118 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001119 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe35439ba2014-08-28 14:41:02 -07001120 CHECK(type != nullptr);
1121 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001122 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001123 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001124 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001125 os << StringPrintf("%p String: %s\n", string,
Ian Rogerscc2f2392014-08-29 20:19:11 -07001126 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001127 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001128 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001129 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Brian Carlstromea46f952013-07-30 01:26:50 -07001130 } else if (type->IsArtFieldClass()) {
1131 mirror::ArtField* field = value->AsArtField();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001132 os << StringPrintf("%p Field: %s\n", field, PrettyField(field).c_str());
Brian Carlstromea46f952013-07-30 01:26:50 -07001133 } else if (type->IsArtMethodClass()) {
1134 mirror::ArtMethod* method = value->AsArtMethod();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001135 os << StringPrintf("%p Method: %s\n", method, PrettyMethod(method).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001136 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001137 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001138 }
1139 }
1140
Brian Carlstromea46f952013-07-30 01:26:50 -07001141 static void PrintField(std::ostream& os, mirror::ArtField* field, mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001142 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001143 const char* descriptor = field->GetTypeDescriptor();
1144 os << StringPrintf("%s: ", field->GetName());
Ian Rogers48efc2b2012-08-27 17:20:31 -07001145 if (descriptor[0] != 'L' && descriptor[0] != '[') {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001146 StackHandleScope<1> hs(Thread::Current());
1147 FieldHelper fh(hs.NewHandle(field));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001148 mirror::Class* type = fh.GetType();
Ian Rogers48efc2b2012-08-27 17:20:31 -07001149 if (type->IsPrimitiveLong()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001150 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers48efc2b2012-08-27 17:20:31 -07001151 } else if (type->IsPrimitiveDouble()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001152 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers48efc2b2012-08-27 17:20:31 -07001153 } else if (type->IsPrimitiveFloat()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001154 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers48efc2b2012-08-27 17:20:31 -07001155 } else {
1156 DCHECK(type->IsPrimitive());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001157 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers48efc2b2012-08-27 17:20:31 -07001158 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001159 } else {
Ian Rogers48efc2b2012-08-27 17:20:31 -07001160 // Get the value, don't compute the type unless it is non-null as we don't want
1161 // to cause class loading.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001162 mirror::Object* value = field->GetObj(obj);
Andreas Gampe35439ba2014-08-28 14:41:02 -07001163 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001164 os << StringPrintf("null %s\n", PrettyDescriptor(descriptor).c_str());
Ian Rogers48efc2b2012-08-27 17:20:31 -07001165 } else {
Ian Rogers50239c72013-06-17 14:53:22 -07001166 // Grab the field type without causing resolution.
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001167 StackHandleScope<1> hs(Thread::Current());
1168 FieldHelper fh(hs.NewHandle(field));
Ian Rogers50239c72013-06-17 14:53:22 -07001169 mirror::Class* field_type = fh.GetType(false);
Andreas Gampe35439ba2014-08-28 14:41:02 -07001170 if (field_type != nullptr) {
Ian Rogers50239c72013-06-17 14:53:22 -07001171 PrettyObjectValue(os, field_type, value);
1172 } else {
1173 os << StringPrintf("%p %s\n", value, PrettyDescriptor(descriptor).c_str());
1174 }
Ian Rogers48efc2b2012-08-27 17:20:31 -07001175 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001176 }
1177 }
1178
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001179 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001180 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001181 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe35439ba2014-08-28 14:41:02 -07001182 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001183 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001184 }
Brian Carlstromea46f952013-07-30 01:26:50 -07001185 mirror::ObjectArray<mirror::ArtField>* fields = klass->GetIFields();
Andreas Gampe35439ba2014-08-28 14:41:02 -07001186 if (fields != nullptr) {
Ian Rogersd5b32602012-02-26 16:40:04 -08001187 for (int32_t i = 0; i < fields->GetLength(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001188 mirror::ArtField* field = fields->Get(i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001189 PrintField(os, field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001190 }
1191 }
1192 }
1193
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001194 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001195 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001196 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001197
Ian Rogersef7d42f2014-01-06 12:55:46 -08001198 const void* GetQuickOatCodeBegin(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001199 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001200 const void* quick_code = m->GetEntryPointFromQuickCompiledCode();
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001201 if (quick_code == Runtime::Current()->GetClassLinker()->GetQuickResolutionTrampoline()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001202 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001203 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001204 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001205 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001206 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001207 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001208 }
1209
Ian Rogersef7d42f2014-01-06 12:55:46 -08001210 uint32_t GetQuickOatCodeSize(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001211 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001212 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1213 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001214 return 0;
1215 }
1216 return oat_code_begin[-1];
1217 }
1218
Ian Rogersef7d42f2014-01-06 12:55:46 -08001219 const void* GetQuickOatCodeEnd(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001220 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001221 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe35439ba2014-08-28 14:41:02 -07001222 if (oat_code_begin == nullptr) {
1223 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001224 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001225 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001226 }
1227
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001228 static void Callback(mirror::Object* obj, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001229 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe35439ba2014-08-28 14:41:02 -07001230 DCHECK(obj != nullptr);
1231 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001232 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001233 if (!state->InDumpSpace(obj)) {
1234 return;
1235 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001236
1237 size_t object_bytes = obj->SizeOf();
1238 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1239 state->stats_.object_bytes += object_bytes;
1240 state->stats_.alignment_bytes += alignment_bytes;
1241
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001242 std::ostream& os = *state->os_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001243 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001244 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001245 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
1246 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001247 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001248 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001249 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
1250 << klass->GetStatus() << ")\n";
Brian Carlstromea46f952013-07-30 01:26:50 -07001251 } else if (obj->IsArtField()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001252 os << StringPrintf("%p: java.lang.reflect.ArtField %s\n", obj,
Brian Carlstromea46f952013-07-30 01:26:50 -07001253 PrettyField(obj->AsArtField()).c_str());
1254 } else if (obj->IsArtMethod()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001255 os << StringPrintf("%p: java.lang.reflect.ArtMethod %s\n", obj,
Brian Carlstromea46f952013-07-30 01:26:50 -07001256 PrettyMethod(obj->AsArtMethod()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001257 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001258 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogerscc2f2392014-08-29 20:19:11 -07001259 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001260 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001261 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001262 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001263 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1264 std::ostream indent_os(&indent_filter);
1265 DumpFields(indent_os, obj, obj_class);
Ian Rogersd5b32602012-02-26 16:40:04 -08001266 if (obj->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001267 mirror::ObjectArray<mirror::Object>* obj_array = obj->AsObjectArray<mirror::Object>();
Ian Rogersd5b32602012-02-26 16:40:04 -08001268 int32_t length = obj_array->GetLength();
1269 for (int32_t i = 0; i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001270 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001271 size_t run = 0;
1272 for (int32_t j = i + 1; j < length; j++) {
1273 if (value == obj_array->Get(j)) {
1274 run++;
1275 } else {
1276 break;
1277 }
1278 }
1279 if (run == 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001280 indent_os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001281 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001282 indent_os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001283 i = i + run;
1284 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001285 mirror::Class* value_class =
Andreas Gampe35439ba2014-08-28 14:41:02 -07001286 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001287 PrettyObjectValue(indent_os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001288 }
1289 } else if (obj->IsClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001290 mirror::ObjectArray<mirror::ArtField>* sfields = obj->AsClass()->GetSFields();
Andreas Gampe35439ba2014-08-28 14:41:02 -07001291 if (sfields != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001292 indent_os << "STATICS:\n";
1293 Indenter indent2_filter(indent_os.rdbuf(), kIndentChar, kIndentBy1Count);
1294 std::ostream indent2_os(&indent2_filter);
Ian Rogersd5b32602012-02-26 16:40:04 -08001295 for (int32_t i = 0; i < sfields->GetLength(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001296 mirror::ArtField* field = sfields->Get(i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001297 PrintField(indent2_os, field, field->GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001298 }
1299 }
Brian Carlstromea46f952013-07-30 01:26:50 -07001300 } else if (obj->IsArtMethod()) {
1301 mirror::ArtMethod* method = obj->AsArtMethod();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001302 if (method->IsNative()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001303 // TODO: portable dumping.
1304 DCHECK(method->GetNativeGcMap() == nullptr) << PrettyMethod(method);
1305 DCHECK(method->GetMappingTable() == nullptr) << PrettyMethod(method);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001306 bool first_occurrence;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001307 const void* quick_oat_code = state->GetQuickOatCodeBegin(method);
1308 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1309 state->ComputeOatSize(quick_oat_code, &first_occurrence);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001310 if (first_occurrence) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001311 state->stats_.native_to_managed_code_bytes += quick_oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001312 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001313 if (quick_oat_code != method->GetEntryPointFromQuickCompiledCode()) {
1314 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001315 }
Ian Rogers19846512012-02-24 11:42:47 -08001316 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
Jeff Hao88474b42013-10-23 16:24:40 -07001317 method->IsResolutionMethod() || method->IsImtConflictMethod() ||
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001318 method->IsClassInitializer()) {
Andreas Gampe35439ba2014-08-28 14:41:02 -07001319 DCHECK(method->GetNativeGcMap() == nullptr) << PrettyMethod(method);
1320 DCHECK(method->GetMappingTable() == nullptr) << PrettyMethod(method);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001321 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001322 const DexFile::CodeItem* code_item = method->GetCodeItem();
Ian Rogersd81871c2011-10-03 13:57:23 -07001323 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001324 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001325
Elliott Hughesa0e18062012-04-13 15:59:59 -07001326 bool first_occurrence;
Ian Rogers0c7abda2012-09-19 13:33:42 -07001327 size_t gc_map_bytes = state->ComputeOatSize(method->GetNativeGcMap(), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001328 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001329 state->stats_.gc_map_bytes += gc_map_bytes;
1330 }
1331
1332 size_t pc_mapping_table_bytes =
Ian Rogers1809a722013-08-09 22:05:32 -07001333 state->ComputeOatSize(method->GetMappingTable(), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001334 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001335 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
1336 }
1337
1338 size_t vmap_table_bytes =
Ian Rogers1809a722013-08-09 22:05:32 -07001339 state->ComputeOatSize(method->GetVmapTable(), &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001340 if (first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001341 state->stats_.vmap_table_bytes += vmap_table_bytes;
1342 }
1343
Ian Rogersef7d42f2014-01-06 12:55:46 -08001344 // TODO: portable dumping.
1345 const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method);
1346 const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method);
1347 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1348 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Elliott Hughesa0e18062012-04-13 15:59:59 -07001349 if (first_occurrence) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001350 state->stats_.managed_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001351 if (method->IsConstructor()) {
1352 if (method->IsStatic()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001353 state->stats_.class_initializer_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001354 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001355 state->stats_.large_initializer_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001356 }
1357 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001358 state->stats_.large_method_code_bytes += quick_oat_code_size;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001359 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001360 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001361 state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001362
Ian Rogersef7d42f2014-01-06 12:55:46 -08001363 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001364 indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
1365 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001366
1367 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
Ian Rogersef7d42f2014-01-06 12:55:46 -08001368 vmap_table_bytes + quick_oat_code_size + object_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001369
1370 double expansion =
Ian Rogersef7d42f2014-01-06 12:55:46 -08001371 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001372 state->stats_.ComputeOutliers(total_size, expansion, method);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001373 }
1374 }
Ian Rogerscb6b0f32014-08-12 02:30:58 -07001375 std::string temp;
1376 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001377 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001378
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001379 std::set<const void*> already_seen_;
1380 // Compute the size of the given data within the oat file and whether this is the first time
1381 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07001382 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001383 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001384 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001385 already_seen_.insert(oat_data);
1386 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001387 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001388 }
1389 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001390 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001391
1392 public:
1393 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001394 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001395 size_t file_bytes;
1396
1397 size_t header_bytes;
1398 size_t object_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07001399 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001400 size_t alignment_bytes;
1401
1402 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001403 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001404 size_t managed_to_native_code_bytes;
1405 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001406 size_t class_initializer_code_bytes;
1407 size_t large_initializer_code_bytes;
1408 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001409
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001410 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001411 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001412 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001413
1414 size_t dex_instruction_bytes;
1415
Brian Carlstromea46f952013-07-30 01:26:50 -07001416 std::vector<mirror::ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001417 std::vector<size_t> method_outlier_size;
1418 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07001419 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001420
1421 explicit Stats()
1422 : oat_file_bytes(0),
1423 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001424 header_bytes(0),
1425 object_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07001426 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001427 alignment_bytes(0),
1428 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001429 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001430 managed_to_native_code_bytes(0),
1431 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07001432 class_initializer_code_bytes(0),
1433 large_initializer_code_bytes(0),
1434 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001435 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001436 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001437 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001438 dex_instruction_bytes(0) {}
1439
Elliott Hughesa0e18062012-04-13 15:59:59 -07001440 struct SizeAndCount {
1441 SizeAndCount(size_t bytes, size_t count) : bytes(bytes), count(count) {}
1442 size_t bytes;
1443 size_t count;
1444 };
1445 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
1446 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001447
Ian Rogersfc0e94b2013-09-23 23:51:32 -07001448 void Update(const char* descriptor, size_t object_bytes) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001449 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
1450 if (it != sizes_and_counts.end()) {
1451 it->second.bytes += object_bytes;
1452 it->second.count += 1;
1453 } else {
1454 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes, 1));
1455 }
1456 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001457
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001458 double PercentOfOatBytes(size_t size) {
1459 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
1460 }
1461
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001462 double PercentOfFileBytes(size_t size) {
1463 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
1464 }
1465
1466 double PercentOfObjectBytes(size_t size) {
1467 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
1468 }
1469
Brian Carlstromea46f952013-07-30 01:26:50 -07001470 void ComputeOutliers(size_t total_size, double expansion, mirror::ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001471 method_outlier_size.push_back(total_size);
1472 method_outlier_expansion.push_back(expansion);
1473 method_outlier.push_back(method);
1474 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001475
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001476 void DumpOutliers(std::ostream& os)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001477 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001478 size_t sum_of_sizes = 0;
1479 size_t sum_of_sizes_squared = 0;
1480 size_t sum_of_expansion = 0;
1481 size_t sum_of_expansion_squared = 0;
1482 size_t n = method_outlier_size.size();
1483 for (size_t i = 0; i < n; i++) {
1484 size_t cur_size = method_outlier_size[i];
1485 sum_of_sizes += cur_size;
1486 sum_of_sizes_squared += cur_size * cur_size;
1487 double cur_expansion = method_outlier_expansion[i];
1488 sum_of_expansion += cur_expansion;
1489 sum_of_expansion_squared += cur_expansion * cur_expansion;
1490 }
1491 size_t size_mean = sum_of_sizes / n;
1492 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
1493 double expansion_mean = sum_of_expansion / n;
1494 double expansion_variance =
1495 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
1496
1497 // Dump methods whose size is a certain number of standard deviations from the mean
1498 size_t dumped_values = 0;
1499 size_t skipped_values = 0;
1500 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
1501 size_t cur_size_variance = i * i * size_variance;
1502 bool first = true;
1503 for (size_t j = 0; j < n; j++) {
1504 size_t cur_size = method_outlier_size[j];
1505 if (cur_size > size_mean) {
1506 size_t cur_var = cur_size - size_mean;
1507 cur_var = cur_var * cur_var;
1508 if (cur_var > cur_size_variance) {
1509 if (dumped_values > 20) {
1510 if (i == 1) {
1511 skipped_values++;
1512 } else {
1513 i = 2; // jump to counting for 1 standard deviation
1514 break;
1515 }
1516 } else {
1517 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001518 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001519 first = false;
1520 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001521 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07001522 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001523 method_outlier_size[j] = 0; // don't consider this method again
1524 dumped_values++;
1525 }
1526 }
1527 }
1528 }
1529 }
1530 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001531 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001532 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001533 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001534 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001535
1536 // Dump methods whose expansion is a certain number of standard deviations from the mean
1537 dumped_values = 0;
1538 skipped_values = 0;
1539 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
1540 double cur_expansion_variance = i * i * expansion_variance;
1541 bool first = true;
1542 for (size_t j = 0; j < n; j++) {
1543 double cur_expansion = method_outlier_expansion[j];
1544 if (cur_expansion > expansion_mean) {
1545 size_t cur_var = cur_expansion - expansion_mean;
1546 cur_var = cur_var * cur_var;
1547 if (cur_var > cur_expansion_variance) {
1548 if (dumped_values > 20) {
1549 if (i == 1) {
1550 skipped_values++;
1551 } else {
1552 i = 2; // jump to counting for 1 standard deviation
1553 break;
1554 }
1555 } else {
1556 if (first) {
1557 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07001558 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001559 first = false;
1560 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001561 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07001562 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001563 method_outlier_expansion[j] = 0.0; // don't consider this method again
1564 dumped_values++;
1565 }
1566 }
1567 }
1568 }
1569 }
1570 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001571 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07001572 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001573 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001574 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001575 }
1576
Ian Rogersb726dcb2012-09-05 08:57:23 -07001577 void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001578 {
1579 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
1580 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
1581 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1582 std::ostream indent_os(&indent_filter);
1583 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
1584 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier32327092013-08-30 14:04:08 -07001585 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001586 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
1587 header_bytes, PercentOfFileBytes(header_bytes),
1588 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07001589 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001590 alignment_bytes, PercentOfFileBytes(alignment_bytes))
1591 << std::flush;
Mathieu Chartier32327092013-08-30 14:04:08 -07001592 CHECK_EQ(file_bytes, bitmap_bytes + header_bytes + object_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001593 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001594
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001595 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001596 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07001597 for (const auto& sizes_and_count : sizes_and_counts) {
1598 const std::string& descriptor(sizes_and_count.first);
1599 double average = static_cast<double>(sizes_and_count.second.bytes) /
1600 static_cast<double>(sizes_and_count.second.count);
1601 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001602 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07001603 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07001604 descriptor.c_str(), sizes_and_count.second.bytes,
1605 sizes_and_count.second.count, average, percent);
1606 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001607 }
Elliott Hughesc073b072012-05-24 19:29:17 -07001608 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001609 CHECK_EQ(object_bytes, object_bytes_total);
1610
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001611 os << StringPrintf("oat_file_bytes = %8zd\n"
1612 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1613 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1614 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
1615 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1616 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1617 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07001618 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001619 managed_code_bytes,
1620 PercentOfOatBytes(managed_code_bytes),
1621 managed_to_native_code_bytes,
1622 PercentOfOatBytes(managed_to_native_code_bytes),
1623 native_to_managed_code_bytes,
1624 PercentOfOatBytes(native_to_managed_code_bytes),
1625 class_initializer_code_bytes,
1626 PercentOfOatBytes(class_initializer_code_bytes),
1627 large_initializer_code_bytes,
1628 PercentOfOatBytes(large_initializer_code_bytes),
1629 large_method_code_bytes,
1630 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001631 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07001632 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001633 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07001634 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
1635 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07001636 }
1637
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001638 os << "\n" << StringPrintf("gc_map_bytes = %7zd (%2.0f%% of oat file bytes)\n"
1639 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
1640 "vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07001641 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
1642 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
1643 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001644 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001645
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001646 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
1647 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001648 static_cast<double>(managed_code_bytes) /
1649 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07001650 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07001651 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07001652 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001653
1654 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001655 }
1656 } stats_;
1657
1658 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07001659 enum {
1660 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
1661 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1662 kLargeConstructorDexBytes = 4000,
1663 // Number of bytes for a method to be considered large. Based on the 4000 basic block
1664 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1665 kLargeMethodDexBytes = 16000
1666 };
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001667 std::ostream* os_;
Ian Rogers1d54e732013-05-02 21:10:01 -07001668 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001669 const ImageHeader& image_header_;
Brian Carlstrom34fa79e2014-09-14 20:34:17 -07001670 std::unique_ptr<OatDumper> oat_dumper_;
1671 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07001672
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001673 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001674};
1675
Elliott Hughes72395bf2012-04-24 13:45:26 -07001676static int oatdump(int argc, char** argv) {
Elliott Hughes0d39c122012-06-06 16:41:17 -07001677 InitLogging(argv);
Elliott Hughes72395bf2012-04-24 13:45:26 -07001678
Brian Carlstrom78128a62011-09-15 17:21:19 -07001679 // Skip over argv[0].
1680 argv++;
1681 argc--;
1682
1683 if (argc == 0) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001684 fprintf(stderr, "No arguments specified\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -07001685 usage();
1686 }
1687
Andreas Gampe35439ba2014-08-28 14:41:02 -07001688 const char* oat_filename = nullptr;
1689 const char* image_location = nullptr;
1690 const char* boot_image_location = nullptr;
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07001691 InstructionSet instruction_set = kRuntimeISA;
Logan Chien0cc6ab62012-03-20 22:57:52 +08001692 std::string elf_filename_prefix;
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001693 std::ostream* os = &std::cout;
Ian Rogers700a4022014-05-19 16:49:03 -07001694 std::unique_ptr<std::ofstream> out;
Dave Allison404f59f2014-02-24 11:10:01 -08001695 bool dump_raw_mapping_table = false;
1696 bool dump_raw_gc_map = false;
Brian Carlstrom34fa79e2014-09-14 20:34:17 -07001697 bool dump_vmap = true;
1698 bool disassemble_code = true;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001699
1700 for (int i = 0; i < argc; i++) {
1701 const StringPiece option(argv[i]);
Brian Carlstroma6cc8932012-01-04 14:44:07 -08001702 if (option.starts_with("--oat-file=")) {
1703 oat_filename = option.substr(strlen("--oat-file=")).data();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001704 } else if (option.starts_with("--image=")) {
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07001705 image_location = option.substr(strlen("--image=")).data();
Brian Carlstrome24fa612011-09-29 00:53:55 -07001706 } else if (option.starts_with("--boot-image=")) {
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07001707 boot_image_location = option.substr(strlen("--boot-image=")).data();
1708 } else if (option.starts_with("--instruction-set=")) {
1709 StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data();
1710 if (instruction_set_str == "arm") {
1711 instruction_set = kThumb2;
1712 } else if (instruction_set_str == "arm64") {
1713 instruction_set = kArm64;
1714 } else if (instruction_set_str == "mips") {
1715 instruction_set = kMips;
1716 } else if (instruction_set_str == "x86") {
1717 instruction_set = kX86;
1718 } else if (instruction_set_str == "x86_64") {
1719 instruction_set = kX86_64;
1720 }
Brian Carlstrom34fa79e2014-09-14 20:34:17 -07001721 } else if (option =="--dump:raw_mapping_table") {
1722 dump_raw_mapping_table = true;
1723 } else if (option == "--dump:raw_gc_map") {
1724 dump_raw_gc_map = true;
1725 } else if (option == "--no-dump:vmap") {
1726 dump_vmap = false;
1727 } else if (option == "--no-disassemble") {
1728 disassemble_code = false;
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001729 } else if (option.starts_with("--output=")) {
1730 const char* filename = option.substr(strlen("--output=")).data();
1731 out.reset(new std::ofstream(filename));
1732 if (!out->good()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001733 fprintf(stderr, "Failed to open output filename %s\n", filename);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001734 usage();
1735 }
1736 os = out.get();
Brian Carlstrom78128a62011-09-15 17:21:19 -07001737 } else {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001738 fprintf(stderr, "Unknown argument %s\n", option.data());
Brian Carlstrom78128a62011-09-15 17:21:19 -07001739 usage();
1740 }
1741 }
1742
Andreas Gampe35439ba2014-08-28 14:41:02 -07001743 if (image_location == nullptr && oat_filename == nullptr) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001744 fprintf(stderr, "Either --image or --oat must be specified\n");
1745 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001746 }
1747
Andreas Gampe35439ba2014-08-28 14:41:02 -07001748 if (image_location != nullptr && oat_filename != nullptr) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001749 fprintf(stderr, "Either --image or --oat must be specified but not both\n");
1750 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001751 }
1752
Brian Carlstrom34fa79e2014-09-14 20:34:17 -07001753 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
1754 bool absolute_addresses = (oat_filename == nullptr);
1755 std::unique_ptr<OatDumperOptions> oat_dumper_options(new OatDumperOptions(dump_raw_mapping_table,
1756 dump_raw_gc_map,
1757 dump_vmap,
1758 disassemble_code,
1759 absolute_addresses));
Andreas Gampe35439ba2014-08-28 14:41:02 -07001760 if (oat_filename != nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001761 std::string error_msg;
Logan Chien0c717dd2012-03-28 18:31:07 +08001762 OatFile* oat_file =
Andreas Gampe35439ba2014-08-28 14:41:02 -07001763 OatFile::Open(oat_filename, oat_filename, nullptr, false, &error_msg);
1764 if (oat_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001765 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
Brian Carlstromaded5f72011-10-07 17:15:04 -07001766 return EXIT_FAILURE;
1767 }
Brian Carlstrom34fa79e2014-09-14 20:34:17 -07001768 OatDumper oat_dumper(*oat_file, oat_dumper_options.release());
1769 bool success = oat_dumper.Dump(*os);
1770 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001771 }
1772
Ian Rogerse63db272014-07-15 15:36:11 -07001773 RuntimeOptions options;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001774 std::string image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001775 std::string oat_option;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001776 std::string boot_image_option;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001777 std::string boot_oat_option;
Brian Carlstrom6c871802013-07-17 14:25:35 -07001778
1779 // We are more like a compiler than a run-time. We don't want to execute code.
Brian Carlstromc0a1b182014-03-04 23:19:06 -08001780 NoopCompilerCallbacks callbacks;
1781 options.push_back(std::make_pair("compilercallbacks", &callbacks));
Brian Carlstrom6c871802013-07-17 14:25:35 -07001782
Andreas Gampe35439ba2014-08-28 14:41:02 -07001783 if (boot_image_location != nullptr) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001784 boot_image_option += "-Ximage:";
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07001785 boot_image_option += boot_image_location;
Andreas Gampe35439ba2014-08-28 14:41:02 -07001786 options.push_back(std::make_pair(boot_image_option.c_str(), nullptr));
Brian Carlstrom78128a62011-09-15 17:21:19 -07001787 }
Andreas Gampe35439ba2014-08-28 14:41:02 -07001788 if (image_location != nullptr) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001789 image_option += "-Ximage:";
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07001790 image_option += image_location;
Andreas Gampe35439ba2014-08-28 14:41:02 -07001791 options.push_back(std::make_pair(image_option.c_str(), nullptr));
Brian Carlstromaded5f72011-10-07 17:15:04 -07001792 }
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07001793 options.push_back(
1794 std::make_pair("imageinstructionset",
1795 reinterpret_cast<const void*>(GetInstructionSetString(instruction_set))));
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001796
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001797 if (!Runtime::Create(options, false)) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001798 fprintf(stderr, "Failed to create runtime\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -07001799 return EXIT_FAILURE;
1800 }
Ian Rogers700a4022014-05-19 16:49:03 -07001801 std::unique_ptr<Runtime> runtime(Runtime::Current());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001802 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
Ian Rogersb9beb2e2014-05-09 16:57:40 -07001803 // give it away now and then switch to a more manageable ScopedObjectAccess.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001804 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
1805 ScopedObjectAccess soa(Thread::Current());
Ian Rogers1d54e732013-05-02 21:10:01 -07001806 gc::Heap* heap = Runtime::Current()->GetHeap();
1807 gc::space::ImageSpace* image_space = heap->GetImageSpace();
Andreas Gampe35439ba2014-08-28 14:41:02 -07001808 CHECK(image_space != nullptr);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001809 const ImageHeader& image_header = image_space->GetImageHeader();
1810 if (!image_header.IsValid()) {
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07001811 fprintf(stderr, "Invalid image header %s\n", image_location);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001812 return EXIT_FAILURE;
1813 }
Brian Carlstrom34fa79e2014-09-14 20:34:17 -07001814 ImageDumper image_dumper(os, *image_space, image_header, oat_dumper_options.release());
1815 bool success = image_dumper.Dump();
1816 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -07001817}
1818
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001819} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07001820
1821int main(int argc, char** argv) {
1822 return art::oatdump(argc, argv);
1823}