blob: c553a18561e49aea9295a4d03bc9c3bca14f440a [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrom78128a62011-09-15 17:21:19 -070016
17#include <stdio.h>
18#include <stdlib.h>
19
Brian Carlstrom27ec9612011-09-19 20:20:38 -070020#include <fstream>
21#include <iostream>
Igor Murashkin37743352014-11-13 14:38:00 -080022#include <map>
23#include <set>
Brian Carlstrom78128a62011-09-15 17:21:19 -070024#include <string>
Andreas Gampe54fc26c2014-09-04 21:47:42 -070025#include <unordered_map>
Brian Carlstrom78128a62011-09-15 17:21:19 -070026#include <vector>
27
Ian Rogersd582fa42014-11-05 23:46:43 -080028#include "arch/instruction_set_features.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070029#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070030#include "art_method-inl.h"
Elliott Hughes76160052012-12-12 16:31:20 -080031#include "base/unix_file/fd_file.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070032#include "class_linker.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070034#include "dex_file-inl.h"
Elliott Hughese3c845c2012-02-28 17:23:01 -080035#include "dex_instruction.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080036#include "disassembler.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070037#include "elf_builder.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080038#include "gc_map.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070039#include "gc/space/image_space.h"
40#include "gc/space/large_object_space.h"
41#include "gc/space/space-inl.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070042#include "image.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080043#include "indenter.h"
Ian Rogers1809a722013-08-09 22:05:32 -070044#include "mapping_table.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045#include "mirror/array-inl.h"
46#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047#include "mirror/object-inl.h"
48#include "mirror/object_array-inl.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080049#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010050#include "oat_file-inl.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080051#include "os.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070052#include "output_stream.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070053#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070054#include "scoped_thread_state_change.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070055#include "ScopedLocalRef.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080056#include "thread_list.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080057#include "verifier/dex_gc_map.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080058#include "verifier/method_verifier.h"
Ian Rogers1809a722013-08-09 22:05:32 -070059#include "vmap_table.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070060#include "well_known_classes.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070061
Igor Murashkin37743352014-11-13 14:38:00 -080062#include <sys/stat.h>
63#include "cmdline.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070064
Igor Murashkin37743352014-11-13 14:38:00 -080065namespace art {
Brian Carlstrom78128a62011-09-15 17:21:19 -070066
Mathieu Chartiere401d142015-04-22 13:56:20 -070067const char* image_methods_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -080068 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -070069 "kImtConflictMethod",
Mathieu Chartier2d2621a2014-10-23 16:48:06 -070070 "kImtUnimplementedMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070071 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -070072 "kRefsOnlySaveMethod",
73 "kRefsAndArgsSaveMethod",
Mathieu Chartiere401d142015-04-22 13:56:20 -070074};
75
76const char* image_roots_descriptions_[] = {
Brian Carlstrom58ae9412011-10-04 00:56:06 -070077 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070078 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070079};
80
Mathieu Chartierac8f4392015-08-27 13:54:20 -070081// Map is so that we don't allocate multiple dex files for the same OatDexFile.
82static std::map<const OatFile::OatDexFile*,
83 std::unique_ptr<const DexFile>> opened_dex_files;
84
85const DexFile* OpenDexFile(const OatFile::OatDexFile* oat_dex_file, std::string* error_msg) {
86 DCHECK(oat_dex_file != nullptr);
87 auto it = opened_dex_files.find(oat_dex_file);
88 if (it != opened_dex_files.end()) {
89 return it->second.get();
90 }
91 const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
92 opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
93 return ret;
94}
95
David Srbeckybc90fd02015-04-22 19:40:27 +010096class OatSymbolizer FINAL {
Andreas Gampe54fc26c2014-09-04 21:47:42 -070097 public:
David Srbeckybc90fd02015-04-22 19:40:27 +010098 class RodataWriter FINAL : public CodeOutput {
99 public:
100 explicit RodataWriter(const OatFile* oat_file) : oat_file_(oat_file) {}
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700101
David Srbeckybc90fd02015-04-22 19:40:27 +0100102 bool Write(OutputStream* out) OVERRIDE {
103 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
104 return out->WriteFully(oat_file_->Begin(), rodata_size);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700105 }
106
David Srbeckybc90fd02015-04-22 19:40:27 +0100107 private:
108 const OatFile* oat_file_;
109 };
110
111 class TextWriter FINAL : public CodeOutput {
112 public:
113 explicit TextWriter(const OatFile* oat_file) : oat_file_(oat_file) {}
114
115 bool Write(OutputStream* out) OVERRIDE {
116 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
117 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
118 return out->WriteFully(text_begin, oat_file_->End() - text_begin);
119 }
120
121 private:
122 const OatFile* oat_file_;
123 };
124
Roland Levillain3887c462015-08-12 18:15:42 +0100125 OatSymbolizer(const OatFile* oat_file, const std::string& output_name) :
David Srbeckybc90fd02015-04-22 19:40:27 +0100126 oat_file_(oat_file), builder_(nullptr),
127 output_name_(output_name.empty() ? "symbolized.oat" : output_name) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700128 }
129
130 typedef void (OatSymbolizer::*Callback)(const DexFile::ClassDef&,
131 uint32_t,
132 const OatFile::OatMethod&,
133 const DexFile&,
134 uint32_t,
135 const DexFile::CodeItem*,
136 uint32_t);
137
138 bool Symbolize() {
David Srbeckybc90fd02015-04-22 19:40:27 +0100139 Elf32_Word rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
140 uint32_t size = static_cast<uint32_t>(oat_file_->End() - oat_file_->Begin());
141 uint32_t text_size = size - rodata_size;
142 uint32_t bss_size = oat_file_->BssSize();
143 RodataWriter rodata_writer(oat_file_);
144 TextWriter text_writer(oat_file_);
145 builder_.reset(new ElfBuilder<ElfTypes32>(
146 oat_file_->GetOatHeader().GetInstructionSet(),
147 rodata_size, &rodata_writer,
148 text_size, &text_writer,
149 bss_size));
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700150
151 Walk(&art::OatSymbolizer::RegisterForDedup);
152
153 NormalizeState();
154
155 Walk(&art::OatSymbolizer::AddSymbol);
156
David Srbeckybc90fd02015-04-22 19:40:27 +0100157 File* elf_output = OS::CreateEmptyFile(output_name_.c_str());
158 bool result = builder_->Write(elf_output);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700159
Andreas Gampe4303ba92014-11-06 01:00:46 -0800160 // Ignore I/O errors.
David Srbeckybc90fd02015-04-22 19:40:27 +0100161 UNUSED(elf_output->FlushClose());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700162
163 return result;
164 }
165
166 void Walk(Callback callback) {
167 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
168 for (size_t i = 0; i < oat_dex_files.size(); i++) {
169 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700170 CHECK(oat_dex_file != nullptr);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700171 WalkOatDexFile(oat_dex_file, callback);
172 }
173 }
174
175 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file, Callback callback) {
176 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700177 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
178 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700179 return;
180 }
181 for (size_t class_def_index = 0;
182 class_def_index < dex_file->NumClassDefs();
183 class_def_index++) {
184 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
185 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
186 OatClassType type = oat_class.GetType();
187 switch (type) {
188 case kOatClassAllCompiled:
189 case kOatClassSomeCompiled:
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700190 WalkOatClass(oat_class, *dex_file, class_def, callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700191 break;
192
193 case kOatClassNoneCompiled:
194 case kOatClassMax:
195 // Ignore.
196 break;
197 }
198 }
199 }
200
201 void WalkOatClass(const OatFile::OatClass& oat_class, const DexFile& dex_file,
202 const DexFile::ClassDef& class_def, Callback callback) {
Ian Rogers13735952014-10-08 12:43:28 -0700203 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700204 if (class_data == nullptr) { // empty class such as a marker interface?
205 return;
206 }
207 // Note: even if this is an interface or a native class, we still have to walk it, as there
208 // might be a static initializer.
209 ClassDataItemIterator it(dex_file, class_data);
210 SkipAllFields(&it);
211 uint32_t class_method_idx = 0;
212 while (it.HasNextDirectMethod()) {
213 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
214 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700215 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700216 class_method_idx++;
217 it.Next();
218 }
219 while (it.HasNextVirtualMethod()) {
220 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
221 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700222 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700223 class_method_idx++;
224 it.Next();
225 }
226 DCHECK(!it.HasNext());
227 }
228
229 void WalkOatMethod(const DexFile::ClassDef& class_def, uint32_t class_method_index,
230 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
231 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
232 uint32_t method_access_flags, Callback callback) {
233 if ((method_access_flags & kAccAbstract) != 0) {
234 // Abstract method, no code.
235 return;
236 }
237 if (oat_method.GetCodeOffset() == 0) {
238 // No code.
239 return;
240 }
241
242 (this->*callback)(class_def, class_method_index, oat_method, dex_file, dex_method_idx, code_item,
243 method_access_flags);
244 }
245
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700246 void RegisterForDedup(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
247 uint32_t class_method_index ATTRIBUTE_UNUSED,
248 const OatFile::OatMethod& oat_method,
249 const DexFile& dex_file ATTRIBUTE_UNUSED,
250 uint32_t dex_method_idx ATTRIBUTE_UNUSED,
251 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
252 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700253 state_[oat_method.GetCodeOffset()]++;
254 }
255
256 void NormalizeState() {
257 for (auto& x : state_) {
258 if (x.second == 1) {
259 state_[x.first] = 0;
260 }
261 }
262 }
263
264 enum class DedupState { // private
265 kNotDeduplicated,
266 kDeduplicatedFirst,
267 kDeduplicatedOther
268 };
269 DedupState IsDuplicated(uint32_t offset) {
270 if (state_[offset] == 0) {
271 return DedupState::kNotDeduplicated;
272 }
273 if (state_[offset] == 1) {
274 return DedupState::kDeduplicatedOther;
275 }
276 state_[offset] = 1;
277 return DedupState::kDeduplicatedFirst;
278 }
279
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700280 void AddSymbol(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
281 uint32_t class_method_index ATTRIBUTE_UNUSED,
282 const OatFile::OatMethod& oat_method,
283 const DexFile& dex_file,
284 uint32_t dex_method_idx,
285 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
286 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700287 DedupState dedup = IsDuplicated(oat_method.GetCodeOffset());
288 if (dedup != DedupState::kDeduplicatedOther) {
289 std::string pretty_name = PrettyMethod(dex_method_idx, dex_file, true);
290
291 if (dedup == DedupState::kDeduplicatedFirst) {
292 pretty_name = "[Dedup]" + pretty_name;
293 }
294
David Srbeckybc90fd02015-04-22 19:40:27 +0100295 auto* symtab = builder_->GetSymtab();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700296
David Srbeckybc90fd02015-04-22 19:40:27 +0100297 symtab->AddSymbol(pretty_name, builder_->GetText(),
Ian Rogers0279ebb2014-10-08 17:27:48 -0700298 oat_method.GetCodeOffset() - oat_file_->GetOatHeader().GetExecutableOffset(),
299 true, oat_method.GetQuickCodeSize(), STB_GLOBAL, STT_FUNC);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700300 }
301 }
302
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700303 private:
304 static void SkipAllFields(ClassDataItemIterator* it) {
305 while (it->HasNextStaticField()) {
306 it->Next();
307 }
308 while (it->HasNextInstanceField()) {
309 it->Next();
310 }
311 }
312
313 const OatFile* oat_file_;
David Srbecky533c2072015-04-22 12:20:22 +0100314 std::unique_ptr<ElfBuilder<ElfTypes32> > builder_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700315 std::unordered_map<uint32_t, uint32_t> state_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700316 const std::string output_name_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700317};
318
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700319class OatDumperOptions {
320 public:
321 OatDumperOptions(bool dump_raw_mapping_table,
322 bool dump_raw_gc_map,
323 bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100324 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700325 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700326 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800327 const char* class_filter,
328 const char* method_filter,
329 bool list_classes,
330 bool list_methods,
331 const char* export_dex_location,
332 uint32_t addr2instr)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700333 : dump_raw_mapping_table_(dump_raw_mapping_table),
334 dump_raw_gc_map_(dump_raw_gc_map),
335 dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100336 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700337 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700338 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800339 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000340 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800341 list_classes_(list_classes),
342 list_methods_(list_methods),
343 export_dex_location_(export_dex_location),
344 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800345 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700346
347 const bool dump_raw_mapping_table_;
348 const bool dump_raw_gc_map_;
349 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100350 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700351 const bool disassemble_code_;
352 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800353 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000354 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800355 const bool list_classes_;
356 const bool list_methods_;
357 const char* const export_dex_location_;
358 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700359 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700360};
361
Elliott Hughese3c845c2012-02-28 17:23:01 -0800362class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700363 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100364 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000365 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800366 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700367 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800368 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800369 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
370 disassembler_(Disassembler::Create(instruction_set_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800371 new DisassemblerOptions(options_.absolute_addresses_,
Alexandre Ramesa37d9252014-10-27 11:28:14 +0000372 oat_file.Begin(),
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100373 true /* can_read_literals_ */))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800374 CHECK(options_.class_loader_ != nullptr);
375 CHECK(options_.class_filter_ != nullptr);
376 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800377 AddAllOffsets();
378 }
379
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700380 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700381 delete disassembler_;
382 }
383
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800384 InstructionSet GetInstructionSet() {
385 return instruction_set_;
386 }
387
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700388 bool Dump(std::ostream& os) {
389 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800390 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700391
392 os << "MAGIC:\n";
393 os << oat_header.GetMagic() << "\n\n";
394
395 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800396 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700397
Elliott Hughesa72ec822012-03-05 17:12:22 -0800398 os << "INSTRUCTION SET:\n";
399 os << oat_header.GetInstructionSet() << "\n\n";
400
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700401 {
402 std::unique_ptr<const InstructionSetFeatures> features(
403 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
404 oat_header.GetInstructionSetFeaturesBitmap()));
405 os << "INSTRUCTION SET FEATURES:\n";
406 os << features->GetFeatureString() << "\n\n";
407 }
Dave Allison70202782013-10-22 17:52:19 -0700408
Brian Carlstromaded5f72011-10-07 17:15:04 -0700409 os << "DEX FILE COUNT:\n";
410 os << oat_header.GetDexFileCount() << "\n\n";
411
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800412#define DUMP_OAT_HEADER_OFFSET(label, offset) \
413 os << label " OFFSET:\n"; \
414 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800415 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800416 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
417 } \
418 os << StringPrintf("\n\n");
419
420 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
421 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
422 GetInterpreterToInterpreterBridgeOffset);
423 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
424 GetInterpreterToCompiledCodeBridgeOffset);
425 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
426 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800427 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
428 GetQuickGenericJniTrampolineOffset);
429 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
430 GetQuickImtConflictTrampolineOffset);
431 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
432 GetQuickResolutionTrampolineOffset);
433 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
434 GetQuickToInterpreterBridgeOffset);
435#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700436
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700437 os << "IMAGE PATCH DELTA:\n";
438 os << StringPrintf("%d (0x%08x)\n\n",
439 oat_header.GetImagePatchDelta(),
440 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700441
Brian Carlstrom28db0122012-10-18 16:20:41 -0700442 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
443 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
444
445 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800446 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700447
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700448 // Print the key-value store.
449 {
450 os << "KEY VALUE STORE:\n";
451 size_t index = 0;
452 const char* key;
453 const char* value;
454 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
455 os << key << " = " << value << "\n";
456 index++;
457 }
458 os << "\n";
459 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700460
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800461 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700462 os << "BEGIN:\n";
463 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700464
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700465 os << "END:\n";
466 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
467 }
468
469 os << "SIZE:\n";
470 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700471
472 os << std::flush;
473
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800474 // If set, adjust relative address to be searched
475 if (options_.addr2instr_ != 0) {
476 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
477 os << "SEARCH ADDRESS (executable offset + input):\n";
478 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
479 }
480
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800481 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
482 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700483 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800484
485 // If file export selected skip file analysis
486 if (options_.export_dex_location_) {
487 if (!ExportDexFile(os, *oat_dex_file)) {
488 success = false;
489 }
490 } else {
491 if (!DumpOatDexFile(os, *oat_dex_file)) {
492 success = false;
493 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700494 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700495 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700496 os << std::flush;
497 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700498 }
499
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800500 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700501 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
502 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800503 return 0; // Address not in oat file
504 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800505 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
506 reinterpret_cast<uintptr_t>(oat_file_.Begin());
507 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800508 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800509 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800510 return end_offset - begin_offset;
511 }
512
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800513 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700514 return oat_file_.GetOatHeader().GetInstructionSet();
515 }
516
Mathieu Chartier90443472015-07-16 20:32:27 -0700517 const void* GetQuickOatCode(ArtMethod* m) SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800518 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
519 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700520 CHECK(oat_dex_file != nullptr);
521 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700522 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
523 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700524 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
525 << "': " << error_msg;
526 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800527 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700528 const DexFile::ClassDef* class_def =
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800529 dex_file->FindClassDef(descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700530 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700531 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100532 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800533 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100534 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800535 }
536 }
537 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700538 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800539 }
540
Brian Carlstromaded5f72011-10-07 17:15:04 -0700541 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800542 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800543 // We don't know the length of the code for each method, but we need to know where to stop
544 // when disassembling. What we do know is that a region of code will be followed by some other
545 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
546 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800547 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
548 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700549 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700550 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700551 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
552 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700553 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
554 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800555 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800556 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800557 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800558 for (size_t class_def_index = 0;
559 class_def_index < dex_file->NumClassDefs();
560 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800561 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100562 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700563 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700564 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800565 ClassDataItemIterator it(*dex_file, class_data);
566 SkipAllFields(it);
567 uint32_t class_method_index = 0;
568 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100569 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800570 it.Next();
571 }
572 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100573 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800574 it.Next();
575 }
576 }
577 }
578 }
579
580 // If the last thing in the file is code for a method, there won't be an offset for the "next"
581 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
582 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800583 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800584 }
585
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700586 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
587 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
588 }
589
Elliott Hughese3c845c2012-02-28 17:23:01 -0800590 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800591 uint32_t code_offset = oat_method.GetCodeOffset();
592 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
593 code_offset &= ~0x1;
594 }
595 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800596 offsets_.insert(oat_method.GetMappingTableOffset());
597 offsets_.insert(oat_method.GetVmapTableOffset());
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800598 offsets_.insert(oat_method.GetGcMapOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800599 }
600
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700601 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
602 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800603 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700604 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800605 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800606 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700607
608 // Create the verifier early.
609
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700610 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700611 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
612 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700613 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700614 os << std::flush;
615 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700616 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100617
618 VariableIndentationOutputStream vios(&os);
619 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800620 for (size_t class_def_index = 0;
621 class_def_index < dex_file->NumClassDefs();
622 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700623 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
624 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800625
626 // TODO: Support regex
627 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
628 continue;
629 }
630
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700631 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100632 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700633 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
634 class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100635 << " (" << oat_class.GetStatus() << ")"
636 << " (" << oat_class.GetType() << ")\n";
637 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800638 if (options_.list_classes_) continue;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700639 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700640 success = false;
641 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800642 if (stop_analysis) {
643 os << std::flush;
644 return success;
645 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700646 }
647
648 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700649 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700650 }
651
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800652 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
653 std::string error_msg;
654 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
655
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700656 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800657 if (dex_file == nullptr) {
658 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
659 return false;
660 }
661 size_t fsize = oat_dex_file.FileSize();
662
663 // Some quick checks just in case
664 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
665 os << "Invalid dex file\n";
666 return false;
667 }
668
669 // Verify output directory exists
670 if (!OS::DirectoryExists(options_.export_dex_location_)) {
671 // TODO: Extend OS::DirectoryExists if symlink support is required
672 os << options_.export_dex_location_ << " output directory not found or symlink\n";
673 return false;
674 }
675
676 // Beautify path names
677 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
678 return false;
679 }
680
681 std::string dex_orig_name;
682 size_t dex_orig_pos = dex_file_location.rfind('/');
683 if (dex_orig_pos == std::string::npos)
684 dex_orig_name = dex_file_location;
685 else
686 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
687
688 // A more elegant approach to efficiently name user installed apps is welcome
689 if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
690 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
691 size_t apk_orig_pos = dex_file_location.rfind('/');
692 if (apk_orig_pos != std::string::npos) {
693 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
694 }
695 }
696
697 std::string out_dex_path(options_.export_dex_location_);
698 if (out_dex_path.back() != '/') {
699 out_dex_path.append("/");
700 }
701 out_dex_path.append(dex_orig_name);
702 out_dex_path.append("_export.dex");
703 if (out_dex_path.length() > PATH_MAX) {
704 return false;
705 }
706
707 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
708 if (file.get() == nullptr) {
709 os << "Failed to open output dex file " << out_dex_path;
710 return false;
711 }
712
713 if (!file->WriteFully(dex_file->Begin(), fsize)) {
714 os << "Failed to write dex file";
715 file->Erase();
716 return false;
717 }
718
719 if (file->FlushCloseOrErase() != 0) {
720 os << "Flush and close failed";
721 return false;
722 }
723
724 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
725 os << std::flush;
726
727 return true;
728 }
729
Elliott Hughese3c845c2012-02-28 17:23:01 -0800730 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700731 while (it.HasNextStaticField()) {
732 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700733 }
Ian Rogers0571d352011-11-03 19:51:38 -0700734 while (it.HasNextInstanceField()) {
735 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700736 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800737 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700738
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100739 bool DumpOatClass(VariableIndentationOutputStream* vios,
740 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800741 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700742 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800743 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -0700744 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700745 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100746 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700747 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800748 }
749 ClassDataItemIterator it(dex_file, class_data);
750 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700751 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700752 while (it.HasNextDirectMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100753 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700754 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800755 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700756 success = false;
757 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800758 if (addr_found) {
759 *stop_analysis = true;
760 return success;
761 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700762 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700763 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700764 }
Ian Rogers0571d352011-11-03 19:51:38 -0700765 while (it.HasNextVirtualMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100766 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700767 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800768 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700769 success = false;
770 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800771 if (addr_found) {
772 *stop_analysis = true;
773 return success;
774 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700775 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700776 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700777 }
Ian Rogers0571d352011-11-03 19:51:38 -0700778 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100779 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700780 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700781 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800782
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700783 static constexpr uint32_t kPrologueBytes = 16;
784
785 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
786 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
787
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100788 bool DumpOatMethod(VariableIndentationOutputStream* vios,
789 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700790 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700791 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800792 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800793 uint32_t method_access_flags, bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700794 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800795
796 // TODO: Support regex
797 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
798 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000799 return success;
800 }
801
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800802 std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100803 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
804 class_method_index, pretty_method.c_str(),
805 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800806 if (options_.list_methods_) return success;
807
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800808 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
809 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
810 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
811 uint32_t code_offset = oat_method.GetCodeOffset();
812 uint32_t code_size = oat_method.GetQuickCodeSize();
813 if (resolved_addr2instr_ != 0) {
814 if (resolved_addr2instr_ > code_offset + code_size) {
815 return success;
816 } else {
817 *addr_found = true; // stop analyzing file at next iteration
818 }
819 }
820
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100821 // Everything below is indented at least once.
822 ScopedIndentation indent1(vios);
823
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800824 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100825 vios->Stream() << "DEX CODE:\n";
826 ScopedIndentation indent2(vios);
827 DumpDexCode(vios->Stream(), dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700828 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700829
Mathieu Chartier3ae6b1d2015-08-14 14:03:10 -0700830 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700831 std::unique_ptr<verifier::MethodVerifier> verifier;
832 if (Runtime::Current() != nullptr) {
Mathieu Chartier3ae6b1d2015-08-14 14:03:10 -0700833 // We need to have the handle scope stay live until after the verifier since the verifier has
834 // a handle to the dex cache from hs.
835 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100836 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
837 ScopedIndentation indent2(vios);
Mathieu Chartier3ae6b1d2015-08-14 14:03:10 -0700838 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100839 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700840 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -0700841 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800842 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100843 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800844 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100845 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100846 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100847 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700848 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100849 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700850 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
851 oat_method_offsets_offset, oat_file_.Size());
852 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100853 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700854 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800855 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700856
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100857 ScopedIndentation indent2(vios);
858 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700859 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
860 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100861 vios->Stream() << StringPrintf("WARNING: "
862 "code offset 0x%08x is past end of file 0x%08zx.\n",
863 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700864 success = false;
865 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100866 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700867
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100868 vios->Stream() << "gc_map: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800869 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100870 vios->Stream() << StringPrintf("%p ", oat_method.GetGcMap());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700871 }
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800872 uint32_t gc_map_offset = oat_method.GetGcMapOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100873 vios->Stream() << StringPrintf("(offset=0x%08x)\n", gc_map_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700874 if (gc_map_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100875 vios->Stream() << StringPrintf("WARNING: "
876 "gc map table offset 0x%08x is past end of file 0x%08zx.\n",
877 gc_map_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700878 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800879 } else if (options_.dump_raw_gc_map_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100880 ScopedIndentation indent3(vios);
881 DumpGcMap(vios->Stream(), oat_method, code_item);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800882 }
883 }
884 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100885 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700886 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
887 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700888
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800889 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100890 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700891 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100892 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700893 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100894 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700895 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
896 method_header_offset, oat_file_.Size());
897 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100898 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700899 return false;
900 }
901
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100902 ScopedIndentation indent2(vios);
903 vios->Stream() << "mapping_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800904 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100905 vios->Stream() << StringPrintf("%p ", oat_method.GetMappingTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700906 }
907 uint32_t mapping_table_offset = oat_method.GetMappingTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100908 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700909 if (mapping_table_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100910 vios->Stream() << StringPrintf("WARNING: "
911 "mapping table offset 0x%08x is past end of file 0x%08zx. "
912 "mapping table offset was loaded from offset 0x%08x.\n",
913 mapping_table_offset, oat_file_.Size(),
914 oat_method.GetMappingTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700915 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800916 } else if (options_.dump_raw_mapping_table_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100917 ScopedIndentation indent3(vios);
918 DumpMappingTable(vios, oat_method);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700919 }
920
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100921 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800922 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100923 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700924 }
925 uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100926 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700927 if (vmap_table_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100928 vios->Stream() << StringPrintf("WARNING: "
929 "vmap table offset 0x%08x is past end of file 0x%08zx. "
930 "vmap table offset was loaded from offset 0x%08x.\n",
931 vmap_table_offset, oat_file_.Size(),
932 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700933 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800934 } else if (options_.dump_vmap_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100935 DumpVmapData(vios, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700936 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800937 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700938 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100939 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700940
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100941 ScopedIndentation indent2(vios);
942 vios->Stream()
943 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
944 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
945 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
946 vios->Stream() << "\n";
947 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
948 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
949 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700950 }
951 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100952 // Based on spill masks from QuickMethodFrameInfo so placed
953 // after it is dumped, but useful for understanding quick
954 // code, so dumped here.
955 ScopedIndentation indent2(vios);
956 DumpVregLocations(vios->Stream(), oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700957 }
958 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100959 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700960 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
961 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100962 ScopedIndentation indent2(vios);
963 vios->Stream() << StringPrintf("WARNING: "
964 "code size offset 0x%08x is past end of file 0x%08zx.",
965 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700966 success = false;
967 } else {
968 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700969 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
970 uint64_t aligned_code_end = aligned_code_begin + code_size;
971
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800972 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100973 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700974 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100975 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
976 code_offset,
977 code_size_offset,
978 code_size,
979 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700980
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100981 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700982 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100983 vios->Stream() << StringPrintf("WARNING: "
984 "start of code at 0x%08x is past end of file 0x%08zx.",
985 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700986 success = false;
987 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100988 vios->Stream() << StringPrintf(
989 "WARNING: "
990 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
991 "code size is 0x%08x loaded from offset 0x%08x.\n",
992 aligned_code_end, oat_file_.Size(),
993 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700994 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800995 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700996 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100997 DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700998 }
999 }
1000 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001001 vios->Stream() << StringPrintf(
1002 "WARNING: "
1003 "code size %d is bigger than max expected threshold of %d. "
1004 "code size is 0x%08x loaded from offset 0x%08x.\n",
1005 code_size, kMaxCodeSize,
1006 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001007 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001008 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001009 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001010 DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001011 }
1012 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001013 } else if (options_.disassemble_code_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001014 DumpCode(vios, verifier.get(), oat_method, code_item, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001015 }
1016 }
1017 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001018 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001019 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001020 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001021
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001022 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1023 if (spill_mask == 0) {
1024 return;
1025 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001026 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001027 for (size_t i = 0; i < 32; i++) {
1028 if ((spill_mask & (1 << i)) != 0) {
1029 if (is_float) {
1030 os << "fr" << i;
1031 } else {
1032 os << "r" << i;
1033 }
1034 spill_mask ^= 1 << i; // clear bit
1035 if (spill_mask != 0) {
1036 os << ", ";
1037 } else {
1038 break;
1039 }
1040 }
1041 }
1042 os << ")";
1043 }
1044
Roland Levillain442b46a2015-02-18 16:54:21 +00001045 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001046 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001047 const OatFile::OatMethod& oat_method,
1048 const DexFile::CodeItem* code_item) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001049 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1050 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001051 const void* raw_code_info = oat_method.GetVmapTable();
1052 if (raw_code_info != nullptr) {
1053 CodeInfo code_info(raw_code_info);
1054 DCHECK(code_item != nullptr);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001055 ScopedIndentation indent1(vios);
1056 DumpCodeInfo(vios, code_info, oat_method, *code_item);
Roland Levillain442b46a2015-02-18 16:54:21 +00001057 }
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001058 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
1059 // We don't encode the size in the table, so just emit that we have quickened
1060 // information.
1061 ScopedIndentation indent(vios);
1062 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001063 } else {
1064 // Otherwise, display the vmap table.
1065 const uint8_t* raw_table = oat_method.GetVmapTable();
1066 if (raw_table != nullptr) {
1067 VmapTable vmap_table(raw_table);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001068 DumpVmapTable(vios->Stream(), oat_method, vmap_table);
Roland Levillain442b46a2015-02-18 16:54:21 +00001069 }
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001070 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001071 }
1072
1073 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001074 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001075 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001076 const OatFile::OatMethod& oat_method,
Roland Levillain442b46a2015-02-18 16:54:21 +00001077 const DexFile::CodeItem& code_item) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001078 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001079 oat_method.GetCodeOffset(),
1080 code_item.registers_size_,
1081 options_.dump_code_info_stack_maps_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001082 }
1083
Roland Levillain442b46a2015-02-18 16:54:21 +00001084 // Display a vmap table.
1085 void DumpVmapTable(std::ostream& os,
1086 const OatFile::OatMethod& oat_method,
1087 const VmapTable& vmap_table) {
1088 bool first = true;
1089 bool processing_fp = false;
1090 uint32_t spill_mask = oat_method.GetCoreSpillMask();
1091 for (size_t i = 0; i < vmap_table.Size(); i++) {
1092 uint16_t dex_reg = vmap_table[i];
1093 uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
1094 processing_fp ? kFloatVReg : kIntVReg);
1095 os << (first ? "v" : ", v") << dex_reg;
1096 if (!processing_fp) {
1097 os << "/r" << cpu_reg;
1098 } else {
1099 os << "/fr" << cpu_reg;
1100 }
1101 first = false;
1102 if (!processing_fp && dex_reg == 0xFFFF) {
1103 processing_fp = true;
1104 spill_mask = oat_method.GetFpSpillMask();
1105 }
1106 }
1107 os << "\n";
1108 }
1109
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001110 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1111 const DexFile::CodeItem* code_item) {
1112 if (code_item != nullptr) {
1113 size_t num_locals_ins = code_item->registers_size_;
1114 size_t num_ins = code_item->ins_size_;
1115 size_t num_locals = num_locals_ins - num_ins;
1116 size_t num_outs = code_item->outs_size_;
1117
1118 os << "vr_stack_locations:";
1119 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1120 // For readability, delimit the different kinds of VRs.
1121 if (reg == num_locals_ins) {
1122 os << "\n\tmethod*:";
1123 } else if (reg == num_locals && num_ins > 0) {
1124 os << "\n\tins:";
1125 } else if (reg == 0 && num_locals > 0) {
1126 os << "\n\tlocals:";
1127 }
1128
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001129 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1130 code_item,
1131 oat_method.GetCoreSpillMask(),
1132 oat_method.GetFpSpillMask(),
1133 oat_method.GetFrameSizeInBytes(),
1134 reg,
1135 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001136 os << " v" << reg << "[sp + #" << offset << "]";
1137 }
1138
1139 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1140 if (out_reg == 0) {
1141 os << "\n\touts:";
1142 }
1143
1144 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1145 os << " v" << out_reg << "[sp + #" << offset << "]";
1146 }
1147
1148 os << "\n";
1149 }
1150 }
1151
Ian Rogersb23a7722012-10-09 16:54:26 -07001152 void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001153 const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
Ian Rogers1809a722013-08-09 22:05:32 -07001154 const uint8_t* raw_table = oat_method.GetVmapTable();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001155 if (raw_table != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001156 const VmapTable vmap_table(raw_table);
1157 uint32_t vmap_offset;
Ian Rogers1809a722013-08-09 22:05:32 -07001158 if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001159 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
1160 uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
1161 : oat_method.GetCoreSpillMask();
1162 os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
Ian Rogersb23a7722012-10-09 16:54:26 -07001163 } else {
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001164 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1165 code_item,
1166 oat_method.GetCoreSpillMask(),
1167 oat_method.GetFpSpillMask(),
1168 oat_method.GetFrameSizeInBytes(),
1169 reg,
1170 GetInstructionSet());
Ian Rogersb23a7722012-10-09 16:54:26 -07001171 os << "[sp + #" << offset << "]";
1172 }
1173 }
1174 }
1175
Ian Rogersef7d42f2014-01-06 12:55:46 -08001176 void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
1177 const DexFile::CodeItem* code_item,
1178 size_t num_regs, const uint8_t* reg_bitmap) {
1179 bool first = true;
1180 for (size_t reg = 0; reg < num_regs; reg++) {
1181 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1182 if (first) {
1183 os << " v" << reg << " (";
1184 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1185 os << ")";
1186 first = false;
1187 } else {
1188 os << ", v" << reg << " (";
1189 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1190 os << ")";
1191 }
1192 }
1193 }
1194 if (first) {
1195 os << "No registers in GC map\n";
1196 } else {
1197 os << "\n";
1198 }
1199 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001200 void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
1201 const DexFile::CodeItem* code_item) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001202 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Ian Rogersef7d42f2014-01-06 12:55:46 -08001203 if (gc_map_raw == nullptr) {
1204 return; // No GC map.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001205 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001206 const void* quick_code = oat_method.GetQuickCode();
Elliott Hughes956af0f2014-12-11 14:34:28 -08001207 NativePcOffsetToReferenceMap map(gc_map_raw);
1208 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1209 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
1210 map.GetNativePcOffset(entry);
1211 os << StringPrintf("%p", native_pc);
1212 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001213 }
1214 }
1215
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001216 void DumpMappingTable(VariableIndentationOutputStream* vios,
1217 const OatFile::OatMethod& oat_method) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001218 const void* quick_code = oat_method.GetQuickCode();
1219 if (quick_code == nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -08001220 return;
1221 }
Ian Rogers1809a722013-08-09 22:05:32 -07001222 MappingTable table(oat_method.GetMappingTable());
1223 if (table.TotalSize() != 0) {
Ian Rogers1809a722013-08-09 22:05:32 -07001224 if (table.PcToDexSize() != 0) {
1225 typedef MappingTable::PcToDexIterator It;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001226 vios->Stream() << "suspend point mappings {\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001227 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001228 ScopedIndentation indent1(vios);
1229 vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001230 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001231 vios->Stream() << "}\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001232 }
1233 if (table.DexToPcSize() != 0) {
1234 typedef MappingTable::DexToPcIterator It;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001235 vios->Stream() << "catch entry mappings {\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001236 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001237 ScopedIndentation indent1(vios);
1238 vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001239 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001240 vios->Stream() << "}\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001241 }
1242 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001243 }
1244
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001245 uint32_t DumpInformationAtOffset(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001246 const OatFile::OatMethod& oat_method,
1247 const DexFile::CodeItem* code_item,
1248 size_t offset,
1249 bool suspend_point_mapping) {
1250 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1251 if (suspend_point_mapping) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001252 ScopedIndentation indent1(vios);
1253 DumpDexRegisterMapAtOffset(vios, oat_method, code_item, offset);
Roland Levillainf2650d12015-05-28 14:53:28 +01001254 }
1255 // The return value is not used in the case of a method compiled
1256 // with the optimizing compiler.
1257 return DexFile::kDexNoIndex;
1258 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001259 return DumpMappingAtOffset(vios->Stream(), oat_method, offset, suspend_point_mapping);
Roland Levillainf2650d12015-05-28 14:53:28 +01001260 }
1261 }
1262
Ian Rogers1809a722013-08-09 22:05:32 -07001263 uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1264 size_t offset, bool suspend_point_mapping) {
1265 MappingTable table(oat_method.GetMappingTable());
1266 if (suspend_point_mapping && table.PcToDexSize() > 0) {
1267 typedef MappingTable::PcToDexIterator It;
1268 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1269 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001270 os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001271 return cur.DexPc();
1272 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001273 }
Ian Rogers1809a722013-08-09 22:05:32 -07001274 } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
1275 typedef MappingTable::DexToPcIterator It;
1276 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1277 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001278 os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001279 return cur.DexPc();
Ian Rogersb23a7722012-10-09 16:54:26 -07001280 }
1281 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001282 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001283 return DexFile::kDexNoIndex;
Ian Rogersb23a7722012-10-09 16:54:26 -07001284 }
1285
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001286 void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1287 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001288 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001289 if (gc_map_raw != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001290 NativePcOffsetToReferenceMap map(gc_map_raw);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001291 if (map.HasEntry(native_pc_offset)) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001292 size_t num_regs = map.RegWidth() * 8;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001293 const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
Ian Rogersb23a7722012-10-09 16:54:26 -07001294 bool first = true;
1295 for (size_t reg = 0; reg < num_regs; reg++) {
1296 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1297 if (first) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001298 os << "GC map objects: v" << reg << " (";
1299 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001300 os << ")";
1301 first = false;
1302 } else {
1303 os << ", v" << reg << " (";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001304 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001305 os << ")";
1306 }
1307 }
1308 }
1309 if (!first) {
1310 os << "\n";
1311 }
1312 }
1313 }
1314 }
1315
Mathieu Chartier590fee92013-09-13 13:46:47 -07001316 void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
1317 const OatFile::OatMethod& oat_method,
1318 const DexFile::CodeItem* code_item, uint32_t dex_pc) {
1319 DCHECK(verifier != nullptr);
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001320 std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001321 bool first = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001322 for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
1323 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
1324 if (kind != kUndefined) {
1325 if (first) {
1326 os << "VRegs: v";
1327 first = false;
1328 } else {
1329 os << ", v";
1330 }
1331 os << reg << " (";
1332 switch (kind) {
1333 case kImpreciseConstant:
1334 os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
1335 DescribeVReg(os, oat_method, code_item, reg, kind);
1336 break;
1337 case kConstant:
1338 os << "Constant: " << kinds.at((reg * 2) + 1);
1339 break;
1340 default:
1341 DescribeVReg(os, oat_method, code_item, reg, kind);
1342 break;
1343 }
1344 os << ")";
1345 }
1346 }
1347 if (!first) {
1348 os << "\n";
1349 }
1350 }
1351
1352
Ian Rogersb23a7722012-10-09 16:54:26 -07001353 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001354 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001355 size_t i = 0;
1356 while (i < code_item->insns_size_in_code_units_) {
1357 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001358 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1359 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001360 i += instruction->SizeInCodeUnits();
1361 }
1362 }
1363 }
1364
Roland Levillainf2650d12015-05-28 14:53:28 +01001365 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1366 // the optimizing compiler?
1367 static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1368 const DexFile::CodeItem* code_item) {
1369 // If the native GC map is null and the Dex `code_item` is not
1370 // null, then this method has been compiled with the optimizing
1371 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001372 return oat_method.GetQuickCode() != nullptr &&
1373 oat_method.GetGcMap() == nullptr &&
1374 code_item != nullptr;
1375 }
1376
1377 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1378 // the dextodex compiler?
1379 static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
1380 const DexFile::CodeItem* code_item) {
1381 // If the quick code is null, the Dex `code_item` is not
1382 // null, and the vmap table is not null, then this method has been compiled
1383 // with the dextodex compiler.
1384 return oat_method.GetQuickCode() == nullptr &&
1385 oat_method.GetVmapTable() != nullptr &&
1386 code_item != nullptr;
Roland Levillainf2650d12015-05-28 14:53:28 +01001387 }
1388
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001389 void DumpDexRegisterMapAtOffset(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001390 const OatFile::OatMethod& oat_method,
1391 const DexFile::CodeItem* code_item,
1392 size_t offset) {
1393 // This method is only relevant for oat methods compiled with the
1394 // optimizing compiler.
1395 DCHECK(IsMethodGeneratedByOptimizingCompiler(oat_method, code_item));
1396
1397 // The optimizing compiler outputs its CodeInfo data in the vmap table.
1398 const void* raw_code_info = oat_method.GetVmapTable();
1399 if (raw_code_info != nullptr) {
1400 CodeInfo code_info(raw_code_info);
David Brazdilf677ebf2015-05-29 16:29:43 +01001401 StackMapEncoding encoding = code_info.ExtractEncoding();
1402 StackMap stack_map = code_info.GetStackMapForNativePcOffset(offset, encoding);
Roland Levillainf2650d12015-05-28 14:53:28 +01001403 if (stack_map.IsValid()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001404 stack_map.Dump(vios, code_info, encoding, oat_method.GetCodeOffset(),
1405 code_item->registers_size_);
Roland Levillainf2650d12015-05-28 14:53:28 +01001406 }
1407 }
1408 }
1409
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001410 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier3ae6b1d2015-08-14 14:03:10 -07001411 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001412 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001413 const DexFile* dex_file,
1414 const DexFile::ClassDef& class_def,
1415 const DexFile::CodeItem* code_item,
1416 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001417 if ((method_access_flags & kAccNative) == 0) {
1418 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001419 Handle<mirror::DexCache> dex_cache(
Mathieu Chartier3ae6b1d2015-08-14 14:03:10 -07001420 hs->NewHandle(Runtime::Current()->GetClassLinker()->RegisterDexFile(*dex_file)));
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001421 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001422 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001423 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
1424 &class_def, code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001425 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001426
1427 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001428 }
1429
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001430 void DumpCode(VariableIndentationOutputStream* vios,
1431 verifier::MethodVerifier* verifier,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001432 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1433 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001434 const void* quick_code = oat_method.GetQuickCode();
1435
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001436 if (code_size == 0) {
1437 code_size = oat_method.GetQuickCodeSize();
1438 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001439 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001440 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001441 return;
Elliott Hughes956af0f2014-12-11 14:34:28 -08001442 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001443 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1444 size_t offset = 0;
1445 while (offset < code_size) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001446 if (!bad_input) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001447 DumpInformationAtOffset(vios, oat_method, code_item, offset, false);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001448 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001449 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001450 if (!bad_input) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001451 uint32_t dex_pc =
1452 DumpInformationAtOffset(vios, oat_method, code_item, offset, true);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001453 if (dex_pc != DexFile::kDexNoIndex) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001454 DumpGcMapAtNativePcOffset(vios->Stream(), oat_method, code_item, offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001455 if (verifier != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001456 DumpVRegsAtDexPc(vios->Stream(), verifier, oat_method, code_item, dex_pc);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001457 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001458 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001459 }
1460 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001461 }
1462 }
1463
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001464 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001465 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001466 const OatDumperOptions& options_;
1467 uint32_t resolved_addr2instr_;
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001468 InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001469 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001470 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001471};
1472
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001473class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001474 public:
Roland Levillain3887c462015-08-12 18:15:42 +01001475 ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space,
1476 const ImageHeader& image_header, OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001477 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001478 vios_(os),
1479 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001480 image_space_(image_space),
1481 image_header_(image_header),
1482 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001483
Mathieu Chartier90443472015-07-16 20:32:27 -07001484 bool Dump() SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001485 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001486 std::ostream& indent_os = vios_.Stream();
1487
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001488 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001489
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001490 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001491
Mathieu Chartiere401d142015-04-22 13:56:20 -07001492 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1493
1494 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1495 auto section = static_cast<ImageHeader::ImageSections>(i);
1496 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1497 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001498
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001499 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001500
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001501 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001502
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001503 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1504
1505 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1506
1507 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001508
Alex Lighta59dd802014-07-02 16:28:08 -07001509 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1510
Igor Murashkin46774762014-10-22 11:37:02 -07001511 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1512
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001513 {
1514 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001515 static_assert(arraysize(image_roots_descriptions_) ==
1516 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001517 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1518 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1519 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001520 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001521 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001522 if (image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001523 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001524 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001525 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001526 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1527 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001528 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001529 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1530 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001531 run++;
1532 } else {
1533 break;
1534 }
1535 }
1536 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001537 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001538 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001539 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001540 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001541 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001542 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001543 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001544 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001545 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001546 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001547 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001548 }
1549 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001550 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001551
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001552 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001553 os << "METHOD ROOTS\n";
1554 static_assert(arraysize(image_methods_descriptions_) ==
1555 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1556 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1557 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1558 const char* description = image_methods_descriptions_[i];
1559 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001560 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001561 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001562 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001563 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001564
Brian Carlstromaded5f72011-10-07 17:15:04 -07001565 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001566 std::string image_filename = image_space_.GetImageFilename();
1567 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001568 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001569 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001570 std::string error_msg;
Alex Lighta59dd802014-07-02 16:28:08 -07001571 const OatFile* oat_file = class_linker->FindOpenedOatFileFromOatLocation(oat_location);
1572 if (oat_file == nullptr) {
Richard Uhlere5fed032015-03-18 08:21:11 -07001573 oat_file = OatFile::Open(oat_location, oat_location,
1574 nullptr, nullptr, false, nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001575 &error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -07001576 if (oat_file == nullptr) {
1577 os << "NOT FOUND: " << error_msg << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001578 return false;
Alex Lighta59dd802014-07-02 16:28:08 -07001579 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001580 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001581 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001582
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001583 stats_.oat_file_bytes = oat_file->Size();
1584
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001585 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001586
Mathieu Chartier02e25112013-08-14 16:14:24 -07001587 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001588 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001589 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1590 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001591 }
1592
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001593 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001594
1595 // Loop through all the image spaces and dump their objects.
Ian Rogers1d54e732013-05-02 21:10:01 -07001596 gc::Heap* heap = Runtime::Current()->GetHeap();
1597 const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces();
Ian Rogers50b35e22012-10-04 10:09:15 -07001598 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001599 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001600 {
1601 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1602 heap->FlushAllocStack();
1603 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001604 // Since FlushAllocStack() above resets the (active) allocation
1605 // stack. Need to revoke the thread-local allocation stacks that
1606 // point into it.
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001607 {
1608 self->TransitionFromRunnableToSuspended(kNative);
1609 ThreadList* thread_list = Runtime::Current()->GetThreadList();
Mathieu Chartierbf9fc582015-03-13 17:21:25 -07001610 thread_list->SuspendAll(__FUNCTION__);
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001611 heap->RevokeAllThreadLocalAllocationStacks(self);
1612 thread_list->ResumeAll();
1613 self->TransitionFromSuspendedToRunnable();
1614 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001615 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001616 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001617 // Mark dex caches.
1618 dex_cache_arrays_.clear();
1619 {
1620 ReaderMutexLock mu(self, *class_linker->DexLock());
Mathieu Chartier3ae6b1d2015-08-14 14:03:10 -07001621 for (jobject weak_root : class_linker->GetDexCaches()) {
1622 mirror::DexCache* dex_cache =
1623 down_cast<mirror::DexCache*>(self->DecodeJObject(weak_root));
1624 if (dex_cache != nullptr) {
1625 dex_cache_arrays_.insert(dex_cache->GetResolvedFields());
1626 dex_cache_arrays_.insert(dex_cache->GetResolvedMethods());
1627 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001628 }
1629 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001630 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001631 for (const auto& space : spaces) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001632 if (space->IsImageSpace()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001633 auto* image_space = space->AsImageSpace();
1634 // Dump the normal objects before ArtMethods.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001635 image_space->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1636 indent_os << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001637 // TODO: Dump fields.
1638 // Dump methods after.
1639 const auto& methods_section = image_header_.GetMethodsSection();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001640 const size_t pointer_size =
Mathieu Chartiere401d142015-04-22 13:56:20 -07001641 InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet());
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001642 DumpArtMethodVisitor visitor(this);
Vladimir Markocf36d492015-08-12 19:27:26 +01001643 methods_section.VisitPackedArtMethods(&visitor, image_space->Begin(), pointer_size);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001644 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -07001645 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001646 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001647 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001648 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001649 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001650 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001651 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001652 if (file.get() == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001653 LOG(WARNING) << "Failed to find image in " << image_filename;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001654 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001655 if (file.get() != nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001656 stats_.file_bytes = file->GetLength();
Brian Carlstrom6f277752013-09-30 17:56:45 -07001657 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001658 size_t header_bytes = sizeof(ImageHeader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001659 const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1660 const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1661 const auto& method_section = image_header_.GetMethodsSection();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001662 const auto& intern_section = image_header_.GetImageSection(
1663 ImageHeader::kSectionInternedStrings);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001664 stats_.header_bytes = header_bytes;
Vladimir Markocf36d492015-08-12 19:27:26 +01001665 stats_.alignment_bytes += RoundUp(header_bytes, kObjectAlignment) - header_bytes;
1666 // Add padding between the field and method section.
1667 // (Field section is 4-byte aligned, method section is 8-byte aligned on 64-bit targets.)
1668 stats_.alignment_bytes +=
1669 method_section.Offset() - (field_section.Offset() + field_section.Size());
1670 // Add padding between the method section and the intern table.
1671 // (Method section is 4-byte aligned on 32-bit targets, intern table is 8-byte aligned.)
1672 stats_.alignment_bytes +=
1673 intern_section.Offset() - (method_section.Offset() + method_section.Size());
Mathieu Chartiere401d142015-04-22 13:56:20 -07001674 stats_.alignment_bytes += bitmap_section.Offset() - image_header_.GetImageSize();
1675 stats_.bitmap_bytes += bitmap_section.Size();
1676 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01001677 stats_.art_method_bytes += method_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001678 stats_.interned_strings_bytes += intern_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001679 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001680 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001681
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001682 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001683
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001684 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001685 }
1686
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001687 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001688 class DumpArtMethodVisitor : public ArtMethodVisitor {
1689 public:
1690 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
1691
1692 virtual void Visit(ArtMethod* method) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
1693 std::ostream& indent_os = image_dumper_->vios_.Stream();
1694 indent_os << method << " " << " ArtMethod: " << PrettyMethod(method) << "\n";
1695 image_dumper_->DumpMethod(method, image_dumper_, indent_os);
1696 indent_os << "\n";
1697 }
1698
1699 private:
1700 ImageDumper* const image_dumper_;
1701 };
1702
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001703 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Mathieu Chartier90443472015-07-16 20:32:27 -07001704 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001705 CHECK(type != nullptr);
1706 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001707 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001708 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001709 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001710 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001711 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001712 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001713 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001714 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001715 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001716 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001717 }
1718 }
1719
Mathieu Chartierc7853442015-03-27 14:35:38 -07001720 static void PrintField(std::ostream& os, ArtField* field, mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -07001721 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001722 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08001723 switch (field->GetTypeAsPrimitiveType()) {
1724 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08001725 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001726 break;
1727 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001728 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001729 break;
1730 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001731 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001732 break;
1733 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001734 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001735 break;
1736 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07001737 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001738 break;
1739 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07001740 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001741 break;
1742 case Primitive::kPrimBoolean:
Fred Shih37f05ef2014-07-16 18:38:08 -07001743 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1744 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001745 break;
1746 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07001747 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001748 break;
1749 case Primitive::kPrimNot: {
1750 // Get the value, don't compute the type unless it is non-null as we don't want
1751 // to cause class loading.
1752 mirror::Object* value = field->GetObj(obj);
1753 if (value == nullptr) {
1754 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07001755 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08001756 // Grab the field type without causing resolution.
Mathieu Chartierdaaf3262015-03-24 13:30:28 -07001757 mirror::Class* field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08001758 if (field_type != nullptr) {
1759 PrettyObjectValue(os, field_type, value);
1760 } else {
1761 os << StringPrintf("%p %s\n", value,
1762 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
1763 }
Ian Rogers50239c72013-06-17 14:53:22 -07001764 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001765 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07001766 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001767 default:
1768 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
1769 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08001770 }
1771 }
1772
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001773 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Mathieu Chartier90443472015-07-16 20:32:27 -07001774 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001775 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001776 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001777 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001778 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001779 for (ArtField& field : klass->GetIFields()) {
1780 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001781 }
1782 }
1783
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001784 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001785 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001786 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001787
Mathieu Chartiere401d142015-04-22 13:56:20 -07001788 const void* GetQuickOatCodeBegin(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001789 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001790 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
1791 InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet()));
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001792 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001793 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001794 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001795 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001796 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001797 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001798 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001799 }
1800
Mathieu Chartiere401d142015-04-22 13:56:20 -07001801 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001802 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001803 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1804 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001805 return 0;
1806 }
1807 return oat_code_begin[-1];
1808 }
1809
Mathieu Chartiere401d142015-04-22 13:56:20 -07001810 const void* GetQuickOatCodeEnd(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001811 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001812 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001813 if (oat_code_begin == nullptr) {
1814 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001815 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001816 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001817 }
1818
Mathieu Chartier90443472015-07-16 20:32:27 -07001819 static void Callback(mirror::Object* obj, void* arg) SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001820 DCHECK(obj != nullptr);
1821 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001822 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001823 if (!state->InDumpSpace(obj)) {
1824 return;
1825 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001826
1827 size_t object_bytes = obj->SizeOf();
1828 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1829 state->stats_.object_bytes += object_bytes;
1830 state->stats_.alignment_bytes += alignment_bytes;
1831
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001832 std::ostream& os = state->vios_.Stream();
1833
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001834 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001835 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001836 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
1837 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001838 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001839 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001840 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
1841 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08001842 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001843 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001844 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001845 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001846 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001847 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001848 ScopedIndentation indent1(&state->vios_);
1849 DumpFields(os, obj, obj_class);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001850 const auto image_pointer_size =
1851 InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet());
Ian Rogersd5b32602012-02-26 16:40:04 -08001852 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001853 auto* obj_array = obj->AsObjectArray<mirror::Object>();
1854 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001855 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001856 size_t run = 0;
1857 for (int32_t j = i + 1; j < length; j++) {
1858 if (value == obj_array->Get(j)) {
1859 run++;
1860 } else {
1861 break;
1862 }
1863 }
1864 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001865 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001866 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001867 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001868 i = i + run;
1869 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001870 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001871 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001872 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001873 }
1874 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001875 mirror::Class* klass = obj->AsClass();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001876 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001877 os << "STATICS:\n";
1878 ScopedIndentation indent2(&state->vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001879 for (ArtField& field : klass->GetSFields()) {
1880 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001881 }
1882 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001883 } else {
1884 auto it = state->dex_cache_arrays_.find(obj);
1885 if (it != state->dex_cache_arrays_.end()) {
1886 const auto& field_section = state->image_header_.GetImageSection(
1887 ImageHeader::kSectionArtFields);
1888 const auto& method_section = state->image_header_.GetMethodsSection();
1889 auto* arr = down_cast<mirror::PointerArray*>(obj);
1890 for (int32_t i = 0, length = arr->GetLength(); i < length; i++) {
1891 void* elem = arr->GetElementPtrSize<void*>(i, image_pointer_size);
1892 size_t run = 0;
1893 for (int32_t j = i + 1; j < length &&
1894 elem == arr->GetElementPtrSize<void*>(j, image_pointer_size); j++, run++) { }
1895 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001896 os << StringPrintf("%d: ", i);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001897 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001898 os << StringPrintf("%d to %zd: ", i, i + run);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001899 i = i + run;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001900 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001901 auto offset = reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin();
1902 std::string msg;
1903 if (field_section.Contains(offset)) {
1904 msg = PrettyField(reinterpret_cast<ArtField*>(elem));
1905 } else if (method_section.Contains(offset)) {
1906 msg = PrettyMethod(reinterpret_cast<ArtMethod*>(elem));
1907 } else {
1908 msg = "Unknown type";
1909 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001910 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001911 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001912 }
1913 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07001914 std::string temp;
1915 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001916 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001917
Mathieu Chartiere401d142015-04-22 13:56:20 -07001918 void DumpMethod(ArtMethod* method, ImageDumper* state, std::ostream& indent_os)
Mathieu Chartier90443472015-07-16 20:32:27 -07001919 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001920 DCHECK(method != nullptr);
1921 const auto image_pointer_size =
1922 InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet());
1923 if (method->IsNative()) {
1924 DCHECK(method->GetNativeGcMap(image_pointer_size) == nullptr) << PrettyMethod(method);
1925 DCHECK(method->GetMappingTable(image_pointer_size) == nullptr) << PrettyMethod(method);
1926 bool first_occurrence;
1927 const void* quick_oat_code = state->GetQuickOatCodeBegin(method);
1928 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1929 state->ComputeOatSize(quick_oat_code, &first_occurrence);
1930 if (first_occurrence) {
1931 state->stats_.native_to_managed_code_bytes += quick_oat_code_size;
1932 }
1933 if (quick_oat_code != method->GetEntryPointFromQuickCompiledCodePtrSize(image_pointer_size)) {
1934 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code);
1935 }
1936 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
1937 method->IsResolutionMethod() || method->IsImtConflictMethod() ||
1938 method->IsImtUnimplementedMethod() || method->IsClassInitializer()) {
1939 DCHECK(method->GetNativeGcMap(image_pointer_size) == nullptr) << PrettyMethod(method);
1940 DCHECK(method->GetMappingTable(image_pointer_size) == nullptr) << PrettyMethod(method);
1941 } else {
1942 const DexFile::CodeItem* code_item = method->GetCodeItem();
1943 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
1944 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
1945
1946 bool first_occurrence;
1947 size_t gc_map_bytes = state->ComputeOatSize(
1948 method->GetNativeGcMap(image_pointer_size), &first_occurrence);
1949 if (first_occurrence) {
1950 state->stats_.gc_map_bytes += gc_map_bytes;
1951 }
1952
1953 size_t pc_mapping_table_bytes = state->ComputeOatSize(
1954 method->GetMappingTable(image_pointer_size), &first_occurrence);
1955 if (first_occurrence) {
1956 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
1957 }
1958
Roland Levillain6d7f1792015-07-02 10:59:15 +01001959 size_t vmap_table_bytes = 0u;
1960 if (!method->IsOptimized(image_pointer_size)) {
1961 // Method compiled with the optimizing compiler have no vmap table.
1962 vmap_table_bytes = state->ComputeOatSize(
1963 method->GetVmapTable(image_pointer_size), &first_occurrence);
1964 if (first_occurrence) {
1965 state->stats_.vmap_table_bytes += vmap_table_bytes;
1966 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001967 }
1968
1969 const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method);
1970 const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method);
1971 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1972 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
1973 if (first_occurrence) {
1974 state->stats_.managed_code_bytes += quick_oat_code_size;
1975 if (method->IsConstructor()) {
1976 if (method->IsStatic()) {
1977 state->stats_.class_initializer_code_bytes += quick_oat_code_size;
1978 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
1979 state->stats_.large_initializer_code_bytes += quick_oat_code_size;
1980 }
1981 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
1982 state->stats_.large_method_code_bytes += quick_oat_code_size;
1983 }
1984 }
1985 state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
1986
Igor Murashkin7617abd2015-07-10 18:27:47 -07001987 uint32_t method_access_flags = method->GetAccessFlags();
1988
Mathieu Chartiere401d142015-04-22 13:56:20 -07001989 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Igor Murashkin7617abd2015-07-10 18:27:47 -07001990 indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd AccessFlags=0x%x\n",
1991 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes,
1992 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001993
1994 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
Vladimir Marko14632852015-08-17 12:07:23 +01001995 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_pointer_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001996
1997 double expansion =
1998 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
1999 state->stats_.ComputeOutliers(total_size, expansion, method);
2000 }
2001 }
2002
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002003 std::set<const void*> already_seen_;
2004 // Compute the size of the given data within the oat file and whether this is the first time
2005 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002006 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002007 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002008 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002009 already_seen_.insert(oat_data);
2010 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002011 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002012 }
2013 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002014 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002015
2016 public:
2017 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002018 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002019 size_t file_bytes;
2020
2021 size_t header_bytes;
2022 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002023 size_t art_field_bytes;
2024 size_t art_method_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002025 size_t interned_strings_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07002026 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002027 size_t alignment_bytes;
2028
2029 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002030 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002031 size_t managed_to_native_code_bytes;
2032 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07002033 size_t class_initializer_code_bytes;
2034 size_t large_initializer_code_bytes;
2035 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002036
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002037 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002038 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002039 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002040
2041 size_t dex_instruction_bytes;
2042
Mathieu Chartiere401d142015-04-22 13:56:20 -07002043 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002044 std::vector<size_t> method_outlier_size;
2045 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002046 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002047
Roland Levillain3887c462015-08-12 18:15:42 +01002048 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002049 : oat_file_bytes(0),
2050 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002051 header_bytes(0),
2052 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002053 art_field_bytes(0),
2054 art_method_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002055 interned_strings_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07002056 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002057 alignment_bytes(0),
2058 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002059 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002060 managed_to_native_code_bytes(0),
2061 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07002062 class_initializer_code_bytes(0),
2063 large_initializer_code_bytes(0),
2064 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002065 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002066 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002067 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002068 dex_instruction_bytes(0) {}
2069
Elliott Hughesa0e18062012-04-13 15:59:59 -07002070 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002071 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002072 size_t bytes;
2073 size_t count;
2074 };
2075 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2076 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002077
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002078 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002079 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2080 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002081 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002082 it->second.count += 1;
2083 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002084 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002085 }
2086 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002087
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002088 double PercentOfOatBytes(size_t size) {
2089 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2090 }
2091
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002092 double PercentOfFileBytes(size_t size) {
2093 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2094 }
2095
2096 double PercentOfObjectBytes(size_t size) {
2097 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2098 }
2099
Mathieu Chartiere401d142015-04-22 13:56:20 -07002100 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002101 method_outlier_size.push_back(total_size);
2102 method_outlier_expansion.push_back(expansion);
2103 method_outlier.push_back(method);
2104 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002105
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002106 void DumpOutliers(std::ostream& os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002107 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002108 size_t sum_of_sizes = 0;
2109 size_t sum_of_sizes_squared = 0;
2110 size_t sum_of_expansion = 0;
2111 size_t sum_of_expansion_squared = 0;
2112 size_t n = method_outlier_size.size();
2113 for (size_t i = 0; i < n; i++) {
2114 size_t cur_size = method_outlier_size[i];
2115 sum_of_sizes += cur_size;
2116 sum_of_sizes_squared += cur_size * cur_size;
2117 double cur_expansion = method_outlier_expansion[i];
2118 sum_of_expansion += cur_expansion;
2119 sum_of_expansion_squared += cur_expansion * cur_expansion;
2120 }
2121 size_t size_mean = sum_of_sizes / n;
2122 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2123 double expansion_mean = sum_of_expansion / n;
2124 double expansion_variance =
2125 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2126
2127 // Dump methods whose size is a certain number of standard deviations from the mean
2128 size_t dumped_values = 0;
2129 size_t skipped_values = 0;
2130 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2131 size_t cur_size_variance = i * i * size_variance;
2132 bool first = true;
2133 for (size_t j = 0; j < n; j++) {
2134 size_t cur_size = method_outlier_size[j];
2135 if (cur_size > size_mean) {
2136 size_t cur_var = cur_size - size_mean;
2137 cur_var = cur_var * cur_var;
2138 if (cur_var > cur_size_variance) {
2139 if (dumped_values > 20) {
2140 if (i == 1) {
2141 skipped_values++;
2142 } else {
2143 i = 2; // jump to counting for 1 standard deviation
2144 break;
2145 }
2146 } else {
2147 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002148 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002149 first = false;
2150 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002151 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002152 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002153 method_outlier_size[j] = 0; // don't consider this method again
2154 dumped_values++;
2155 }
2156 }
2157 }
2158 }
2159 }
2160 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002161 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002162 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002163 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002164 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002165
2166 // Dump methods whose expansion is a certain number of standard deviations from the mean
2167 dumped_values = 0;
2168 skipped_values = 0;
2169 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2170 double cur_expansion_variance = i * i * expansion_variance;
2171 bool first = true;
2172 for (size_t j = 0; j < n; j++) {
2173 double cur_expansion = method_outlier_expansion[j];
2174 if (cur_expansion > expansion_mean) {
2175 size_t cur_var = cur_expansion - expansion_mean;
2176 cur_var = cur_var * cur_var;
2177 if (cur_var > cur_expansion_variance) {
2178 if (dumped_values > 20) {
2179 if (i == 1) {
2180 skipped_values++;
2181 } else {
2182 i = 2; // jump to counting for 1 standard deviation
2183 break;
2184 }
2185 } else {
2186 if (first) {
2187 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002188 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002189 first = false;
2190 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002191 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002192 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002193 method_outlier_expansion[j] = 0.0; // don't consider this method again
2194 dumped_values++;
2195 }
2196 }
2197 }
2198 }
2199 }
2200 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002201 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002202 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002203 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002204 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002205 }
2206
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002207 void Dump(std::ostream& os, std::ostream& indent_os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002208 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002209 {
2210 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2211 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002212 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2213 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2214 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2215 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2216 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
2217 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2218 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002219 header_bytes, PercentOfFileBytes(header_bytes),
2220 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002221 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2222 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002223 interned_strings_bytes,
2224 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002225 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002226 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2227 << std::flush;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002228 CHECK_EQ(file_bytes, header_bytes + object_bytes + art_field_bytes + art_method_bytes +
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002229 interned_strings_bytes + bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002230 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002231
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002232 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002233 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002234 for (const auto& sizes_and_count : sizes_and_counts) {
2235 const std::string& descriptor(sizes_and_count.first);
2236 double average = static_cast<double>(sizes_and_count.second.bytes) /
2237 static_cast<double>(sizes_and_count.second.count);
2238 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002239 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002240 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002241 descriptor.c_str(), sizes_and_count.second.bytes,
2242 sizes_and_count.second.count, average, percent);
2243 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002244 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002245 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002246 CHECK_EQ(object_bytes, object_bytes_total);
2247
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002248 os << StringPrintf("oat_file_bytes = %8zd\n"
2249 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2250 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2251 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2252 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2253 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2254 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002255 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002256 managed_code_bytes,
2257 PercentOfOatBytes(managed_code_bytes),
2258 managed_to_native_code_bytes,
2259 PercentOfOatBytes(managed_to_native_code_bytes),
2260 native_to_managed_code_bytes,
2261 PercentOfOatBytes(native_to_managed_code_bytes),
2262 class_initializer_code_bytes,
2263 PercentOfOatBytes(class_initializer_code_bytes),
2264 large_initializer_code_bytes,
2265 PercentOfOatBytes(large_initializer_code_bytes),
2266 large_method_code_bytes,
2267 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002268 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002269 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002270 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002271 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2272 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002273 }
2274
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002275 os << "\n" << StringPrintf("gc_map_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2276 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2277 "vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002278 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
2279 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
2280 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002281 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002282
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002283 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2284 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002285 static_cast<double>(managed_code_bytes) /
2286 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002287 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002288 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002289 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002290
2291 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002292 }
2293 } stats_;
2294
2295 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002296 enum {
2297 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2298 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2299 kLargeConstructorDexBytes = 4000,
2300 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2301 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2302 kLargeMethodDexBytes = 16000
2303 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002304
2305 // For performance, use the *os_ directly for anything that doesn't need indentation
2306 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002307 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002308 VariableIndentationOutputStream vios_;
2309 ScopedIndentation indent1_;
2310
Ian Rogers1d54e732013-05-02 21:10:01 -07002311 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002312 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002313 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002314 OatDumperOptions* oat_dumper_options_;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002315 std::set<mirror::Object*> dex_cache_arrays_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002316
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002317 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002318};
2319
Andreas Gampe00b25f32014-09-17 21:49:05 -07002320static int DumpImage(Runtime* runtime, const char* image_location, OatDumperOptions* options,
2321 std::ostream* os) {
2322 // Dumping the image, no explicit class loader.
2323 NullHandle<mirror::ClassLoader> null_class_loader;
2324 options->class_loader_ = &null_class_loader;
2325
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002326 ScopedObjectAccess soa(Thread::Current());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002327 gc::Heap* heap = runtime->GetHeap();
Ian Rogers1d54e732013-05-02 21:10:01 -07002328 gc::space::ImageSpace* image_space = heap->GetImageSpace();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002329 CHECK(image_space != nullptr);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002330 const ImageHeader& image_header = image_space->GetImageHeader();
2331 if (!image_header.IsValid()) {
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07002332 fprintf(stderr, "Invalid image header %s\n", image_location);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002333 return EXIT_FAILURE;
2334 }
Igor Murashkin37743352014-11-13 14:38:00 -08002335
Andreas Gampe00b25f32014-09-17 21:49:05 -07002336 ImageDumper image_dumper(os, *image_space, image_header, options);
Igor Murashkin37743352014-11-13 14:38:00 -08002337
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002338 bool success = image_dumper.Dump();
2339 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002340}
2341
Andreas Gampe00b25f32014-09-17 21:49:05 -07002342static int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
2343 std::ostream* os) {
2344 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2345
2346 Thread* self = Thread::Current();
2347 CHECK(self != nullptr);
2348 // Need well-known-classes.
2349 WellKnownClasses::Init(self->GetJniEnv());
2350
2351 // Need to register dex files to get a working dex cache.
2352 ScopedObjectAccess soa(self);
2353 ClassLinker* class_linker = runtime->GetClassLinker();
2354 class_linker->RegisterOatFile(oat_file);
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002355 std::vector<const DexFile*> class_path;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002356 for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
2357 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002358 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002359 CHECK(dex_file != nullptr) << error_msg;
2360 class_linker->RegisterDexFile(*dex_file);
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002361 class_path.push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002362 }
2363
2364 // Need a class loader.
Andreas Gampe00b25f32014-09-17 21:49:05 -07002365 // Fake that we're a compiler.
Andreas Gampe81c6f8d2015-03-25 17:19:53 -07002366 jobject class_loader = class_linker->CreatePathClassLoader(self, class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002367
2368 // Use the class loader while dumping.
2369 StackHandleScope<1> scope(self);
2370 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
2371 soa.Decode<mirror::ClassLoader*>(class_loader));
2372 options->class_loader_ = &loader_handle;
2373
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002374 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002375 bool success = oat_dumper.Dump(*os);
2376 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2377}
2378
2379static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002380 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002381 // No image = no class loader.
2382 NullHandle<mirror::ClassLoader> null_class_loader;
2383 options->class_loader_ = &null_class_loader;
2384
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002385 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002386 bool success = oat_dumper.Dump(*os);
2387 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2388}
2389
2390static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2391 std::ostream* os) {
2392 std::string error_msg;
Igor Murashkin37743352014-11-13 14:38:00 -08002393 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07002394 nullptr, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002395 if (oat_file == nullptr) {
2396 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2397 return EXIT_FAILURE;
2398 }
2399
2400 if (runtime != nullptr) {
2401 return DumpOatWithRuntime(runtime, oat_file, options, os);
2402 } else {
2403 return DumpOatWithoutRuntime(oat_file, options, os);
2404 }
2405}
2406
2407static int SymbolizeOat(const char* oat_filename, std::string& output_name) {
2408 std::string error_msg;
Igor Murashkin37743352014-11-13 14:38:00 -08002409 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07002410 nullptr, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002411 if (oat_file == nullptr) {
2412 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2413 return EXIT_FAILURE;
2414 }
2415
2416 OatSymbolizer oat_symbolizer(oat_file, output_name);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002417 if (!oat_symbolizer.Symbolize()) {
2418 fprintf(stderr, "Failed to symbolize\n");
2419 return EXIT_FAILURE;
2420 }
2421
2422 return EXIT_SUCCESS;
2423}
2424
Igor Murashkin37743352014-11-13 14:38:00 -08002425struct OatdumpArgs : public CmdlineArgs {
2426 protected:
2427 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002428
Igor Murashkin37743352014-11-13 14:38:00 -08002429 virtual ParseStatus ParseCustom(const StringPiece& option,
2430 std::string* error_msg) OVERRIDE {
2431 {
2432 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
2433 if (base_parse != kParseUnknownArgument) {
2434 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002435 }
2436 }
2437
Igor Murashkin37743352014-11-13 14:38:00 -08002438 if (option.starts_with("--oat-file=")) {
2439 oat_filename_ = option.substr(strlen("--oat-file=")).data();
2440 } else if (option.starts_with("--image=")) {
2441 image_location_ = option.substr(strlen("--image=")).data();
2442 } else if (option =="--dump:raw_mapping_table") {
2443 dump_raw_mapping_table_ = true;
2444 } else if (option == "--dump:raw_gc_map") {
2445 dump_raw_gc_map_ = true;
2446 } else if (option == "--no-dump:vmap") {
2447 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01002448 } else if (option =="--dump:code_info_stack_maps") {
2449 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002450 } else if (option == "--no-disassemble") {
2451 disassemble_code_ = false;
2452 } else if (option.starts_with("--symbolize=")) {
2453 oat_filename_ = option.substr(strlen("--symbolize=")).data();
2454 symbolize_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002455 } else if (option.starts_with("--class-filter=")) {
2456 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002457 } else if (option.starts_with("--method-filter=")) {
2458 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002459 } else if (option.starts_with("--list-classes")) {
2460 list_classes_ = true;
2461 } else if (option.starts_with("--list-methods")) {
2462 list_methods_ = true;
2463 } else if (option.starts_with("--export-dex-to=")) {
2464 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
2465 } else if (option.starts_with("--addr2instr=")) {
2466 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
2467 *error_msg = "Address conversion failed";
2468 return kParseError;
2469 }
Igor Murashkin37743352014-11-13 14:38:00 -08002470 } else {
2471 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002472 }
2473
Igor Murashkin37743352014-11-13 14:38:00 -08002474 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002475 }
2476
Igor Murashkin37743352014-11-13 14:38:00 -08002477 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
2478 // Infer boot image location from the image location if possible.
2479 if (boot_image_location_ == nullptr) {
2480 boot_image_location_ = image_location_;
2481 }
2482
2483 // Perform the parent checks.
2484 ParseStatus parent_checks = Base::ParseChecks(error_msg);
2485 if (parent_checks != kParseOk) {
2486 return parent_checks;
2487 }
2488
2489 // Perform our own checks.
2490 if (image_location_ == nullptr && oat_filename_ == nullptr) {
2491 *error_msg = "Either --image or --oat-file must be specified";
2492 return kParseError;
2493 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
2494 *error_msg = "Either --image or --oat-file must be specified but not both";
2495 return kParseError;
2496 }
2497
2498 return kParseOk;
2499 }
2500
2501 virtual std::string GetUsage() const {
2502 std::string usage;
2503
2504 usage +=
2505 "Usage: oatdump [options] ...\n"
2506 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
2507 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
2508 "\n"
2509 // Either oat-file or image is required.
2510 " --oat-file=<file.oat>: specifies an input oat filename.\n"
2511 " Example: --oat-file=/system/framework/boot.oat\n"
2512 "\n"
2513 " --image=<file.art>: specifies an input image location.\n"
2514 " Example: --image=/system/framework/boot.art\n"
2515 "\n";
2516
2517 usage += Base::GetUsage();
2518
2519 usage += // Optional.
2520 " --dump:raw_mapping_table enables dumping of the mapping table.\n"
2521 " Example: --dump:raw_mapping_table\n"
2522 "\n"
Mathieu Chartier19510f02015-05-26 14:44:35 -07002523 " --dump:raw_gc_map enables dumping of the GC map.\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002524 " Example: --dump:raw_gc_map\n"
2525 "\n"
2526 " --no-dump:vmap may be used to disable vmap dumping.\n"
2527 " Example: --no-dump:vmap\n"
2528 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01002529 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
2530 " Example: --dump:code_info_stack_maps\n"
2531 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002532 " --no-disassemble may be used to disable disassembly.\n"
2533 " Example: --no-disassemble\n"
2534 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002535 " --list-classes may be used to list target file classes (can be used with filters).\n"
2536 " Example: --list-classes\n"
2537 " Example: --list-classes --class-filter=com.example.foo\n"
2538 "\n"
2539 " --list-methods may be used to list target file methods (can be used with filters).\n"
2540 " Example: --list-methods\n"
2541 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
2542 "\n"
2543 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
2544 " Example: --symbolize=/system/framework/boot.oat\n"
2545 "\n"
2546 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
2547 " Example: --class-filter=com.example.foo\n"
2548 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002549 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
2550 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002551 "\n"
2552 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
2553 " Example: --export-dex-to=/data/local/tmp\n"
2554 "\n"
2555 " --addr2instr=<address>: output matching method disassembled code from relative\n"
2556 " address (e.g. PC from crash dump)\n"
2557 " Example: --addr2instr=0x00001a3b\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002558 "\n";
2559
2560 return usage;
2561 }
2562
2563 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07002564 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002565 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00002566 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07002567 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002568 std::string elf_filename_prefix_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002569 bool dump_raw_mapping_table_ = false;
2570 bool dump_raw_gc_map_ = false;
2571 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01002572 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002573 bool disassemble_code_ = true;
2574 bool symbolize_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002575 bool list_classes_ = false;
2576 bool list_methods_ = false;
2577 uint32_t addr2instr_ = 0;
2578 const char* export_dex_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002579};
2580
Igor Murashkin37743352014-11-13 14:38:00 -08002581struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
2582 virtual bool NeedsRuntime() OVERRIDE {
2583 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002584
Igor Murashkin37743352014-11-13 14:38:00 -08002585 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
2586 bool absolute_addresses = (args_->oat_filename_ == nullptr);
2587
2588 oat_dumper_options_ = std::unique_ptr<OatDumperOptions>(new OatDumperOptions(
2589 args_->dump_raw_mapping_table_,
2590 args_->dump_raw_gc_map_,
2591 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01002592 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08002593 args_->disassemble_code_,
2594 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002595 args_->class_filter_,
2596 args_->method_filter_,
2597 args_->list_classes_,
2598 args_->list_methods_,
2599 args_->export_dex_location_,
2600 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08002601
2602 return (args_->boot_image_location_ != nullptr || args_->image_location_ != nullptr) &&
2603 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002604 }
2605
Igor Murashkin37743352014-11-13 14:38:00 -08002606 virtual bool ExecuteWithoutRuntime() OVERRIDE {
2607 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08002608 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002609
Mathieu Chartierd424d082014-10-15 10:31:46 -07002610 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08002611
Andreas Gampec24f3992014-12-17 20:40:11 -08002612 if (args_->symbolize_) {
2613 return SymbolizeOat(args_->oat_filename_, args_->output_name_) == EXIT_SUCCESS;
2614 } else {
2615 return DumpOat(nullptr,
2616 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002617 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08002618 args_->os_) == EXIT_SUCCESS;
2619 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002620 }
2621
Igor Murashkin37743352014-11-13 14:38:00 -08002622 virtual bool ExecuteWithRuntime(Runtime* runtime) {
2623 CHECK(args_ != nullptr);
2624
2625 if (args_->oat_filename_ != nullptr) {
2626 return DumpOat(runtime,
2627 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002628 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08002629 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002630 }
Igor Murashkin37743352014-11-13 14:38:00 -08002631
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002632 return DumpImage(runtime, args_->image_location_, oat_dumper_options_.get(), args_->os_)
Igor Murashkin37743352014-11-13 14:38:00 -08002633 == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002634 }
2635
Igor Murashkin37743352014-11-13 14:38:00 -08002636 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
2637};
Andreas Gampe00b25f32014-09-17 21:49:05 -07002638
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002639} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07002640
2641int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08002642 art::OatdumpMain main;
2643 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002644}