blob: 52cddb57864324f39a57925bcf87c155d90317b4 [file] [log] [blame]
Brian Carlstrom78128a62011-09-15 17:21:19 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#include <stdio.h>
4#include <stdlib.h>
5
Brian Carlstrom27ec9612011-09-19 20:20:38 -07006#include <fstream>
7#include <iostream>
Brian Carlstrom78128a62011-09-15 17:21:19 -07008#include <string>
9#include <vector>
10
11#include "class_linker.h"
12#include "image.h"
13#include "runtime.h"
14#include "space.h"
15#include "stringpiece.h"
16
17namespace art {
18
19static void usage() {
20 fprintf(stderr,
21 "Usage: oatdump [options] ...\n"
22 " Example: oatdump --dex-file=$ANDROID_PRODUCT_OUT/system/framework/core.jar --image=$ANDROID_PRODUCT_OUT/system/framework/boot.oat --strip-prefix=$ANDROID_PRODUCT_OUT\n"
23 " Example: adb shell oatdump --dex-file=/system/framework/core.jar --image=/system/framework/boot.oat\n"
24 "\n");
25 // TODO: remove this by making image contain boot DexFile information?
26 fprintf(stderr,
27 " --dex-file=<dex-file>: specifies a .dex file location. At least one .dex\n"
28 " file must be specified. \n"
29 " Example: --dex-file=/system/framework/core.jar\n"
30 "\n");
31 fprintf(stderr,
32 " --image=<file>: specifies the required input image filename.\n"
33 " Example: --image=/system/framework/boot.oat\n"
34 "\n");
35 fprintf(stderr,
36 " --boot=<oat-file>: provide the oat file for the boot class path.\n"
37 " Example: --boot=/system/framework/boot.oat\n"
38 "\n");
39 // TODO: remove this by making boot image contain boot DexFile information?
40 fprintf(stderr,
41 " --boot-dex-file=<dex-file>: specifies a .dex file that is part of the boot\n"
42 " image specified with --boot. \n"
43 " Example: --boot-dex-file=/system/framework/core.jar\n"
44 "\n");
45 fprintf(stderr,
46 " --strip-prefix may be used to strip a path prefix from dex file names in the\n"
47 " the generated image to match the target file system layout.\n"
48 " Example: --strip-prefix=out/target/product/crespo\n"
49 "\n");
Brian Carlstrom27ec9612011-09-19 20:20:38 -070050 fprintf(stderr,
51 " --output=<file> may be used to send the output to a file.\n"
52 " Example: --output=/tmp/oatdump.txt\n"
53 "\n");
Brian Carlstrom78128a62011-09-15 17:21:19 -070054 exit(EXIT_FAILURE);
55}
56
57const char* image_roots_descriptions_[ImageHeader::kImageRootsMax] = {
58 "kJniStubArray",
59};
60
Brian Carlstrom27ec9612011-09-19 20:20:38 -070061class OatDump {
Brian Carlstrom78128a62011-09-15 17:21:19 -070062
Brian Carlstrom27ec9612011-09-19 20:20:38 -070063 public:
64 static void Dump(std::ostream& os, Space& image_space, const ImageHeader& image_header) {
65 os << "MAGIC:\n";
66 os << image_header.GetMagic() << "\n\n";
67
68 os << "ROOTS:\n";
69 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
70 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
71 os << StringPrintf("%s: %p\n",
72 image_roots_descriptions_[i], image_header.GetImageRoot(image_root));
73 }
74 os << "\n";
75
76 os << "OBJECTS:\n" << std::flush;
77 OatDump state(image_space, os);
78 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
79 DCHECK(heap_bitmap != NULL);
80 heap_bitmap->Walk(OatDump::Callback, &state);
Brian Carlstrom78128a62011-09-15 17:21:19 -070081 }
82
Brian Carlstrom27ec9612011-09-19 20:20:38 -070083 private:
84
85 OatDump(const Space& dump_space, std::ostream& os) : dump_space_(dump_space_), os_(os) {}
86
Brian Carlstrom78128a62011-09-15 17:21:19 -070087 static void Callback(Object* obj, void* arg) {
88 DCHECK(obj != NULL);
89 DCHECK(arg != NULL);
90 OatDump* state = reinterpret_cast<OatDump*>(arg);
91 if (!state->InDumpSpace(obj)) {
92 return;
93 }
94 std::string summary;
95 StringAppendF(&summary, "%p: ", obj);
96 if (obj->IsClass()) {
97 Class* klass = obj->AsClass();
98 StringAppendF(&summary, "CLASS %s", klass->GetDescriptor()->ToModifiedUtf8().c_str());
99 } else if (obj->IsMethod()) {
100 Method* method = obj->AsMethod();
101 StringAppendF(&summary, "METHOD %s", PrettyMethod(method).c_str());
102 } else if (obj->IsField()) {
103 Field* field = obj->AsField();
104 Class* type = field->GetType();
105 std::string type_string;
106 type_string += (type == NULL) ? "<UNKNOWN>" : type->GetDescriptor()->ToModifiedUtf8();
107 StringAppendF(&summary, "FIELD %s", PrettyField(field).c_str());
108 } else if (obj->IsArrayInstance()) {
109 StringAppendF(&summary, "ARRAY %d", obj->AsArray()->GetLength());
110 } else if (obj->IsString()) {
111 StringAppendF(&summary, "STRING %s", obj->AsString()->ToModifiedUtf8().c_str());
112 } else {
113 StringAppendF(&summary, "OBJECT");
114 }
115 StringAppendF(&summary, "\n");
116 StringAppendF(&summary, "\tclass %p: %s\n",
117 obj->GetClass(), obj->GetClass()->GetDescriptor()->ToModifiedUtf8().c_str());
118 if (obj->IsMethod()) {
119 Method* method = obj->AsMethod();
120 const ByteArray* code = method->GetCodeArray();
121 StringAppendF(&summary, "\tCODE %p-%p\n", code->GetData(), code->GetData() + code->GetLength());
122 const ByteArray* invoke = method->GetInvokeStubArray();
123 StringAppendF(&summary, "\tJNI STUB %p-%p\n", invoke->GetData(), invoke->GetData() + invoke->GetLength());
124 if (method->IsNative()) {
125 if (method->IsRegistered()) {
126 StringAppendF(&summary, "\tNATIVE REGISTERED %p\n", method->GetNativeMethod());
127 } else {
128 StringAppendF(&summary, "\tNATIVE UNREGISTERED\n");
129 }
130 }
131 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700132 state->os_ << summary << std::flush;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700133 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700134
135 bool InDumpSpace(const Object* object) {
136 const byte* o = reinterpret_cast<const byte*>(object);
137 return (o >= dump_space_.GetBase() && o < dump_space_.GetLimit());
138 }
139 const Space& dump_space_;
140 std::ostream& os_;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700141};
142
143int oatdump(int argc, char** argv) {
144 // Skip over argv[0].
145 argv++;
146 argc--;
147
148 if (argc == 0) {
149 fprintf(stderr, "no arguments specified\n");
150 usage();
151 }
152
153 std::vector<const char*> dex_filenames;
154 const char* image_filename = NULL;
155 const char* boot_image_filename = NULL;
156 std::vector<const char*> boot_dex_filenames;
157 std::string strip_location_prefix;
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700158 std::ostream* os = &std::cout;
159 UniquePtr<std::ofstream> out;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700160
161 for (int i = 0; i < argc; i++) {
162 const StringPiece option(argv[i]);
163 if (option.starts_with("--dex-file=")) {
164 dex_filenames.push_back(option.substr(strlen("--dex-file=")).data());
165 } else if (option.starts_with("--image=")) {
166 image_filename = option.substr(strlen("--image=")).data();
167 } else if (option.starts_with("--boot=")) {
168 boot_image_filename = option.substr(strlen("--boot=")).data();
169 } else if (option.starts_with("--boot-dex-file=")) {
170 boot_dex_filenames.push_back(option.substr(strlen("--boot-dex-file=")).data());
171 } else if (option.starts_with("--strip-prefix=")) {
172 strip_location_prefix = option.substr(strlen("--strip-prefix=")).data();
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700173 } else if (option.starts_with("--output=")) {
174 const char* filename = option.substr(strlen("--output=")).data();
175 out.reset(new std::ofstream(filename));
176 if (!out->good()) {
177 fprintf(stderr, "failed to open output filename %s\n", filename);
178 usage();
179 }
180 os = out.get();
Brian Carlstrom78128a62011-09-15 17:21:19 -0700181 } else {
182 fprintf(stderr, "unknown argument %s\n", option.data());
183 usage();
184 }
185 }
186
187 if (image_filename == NULL) {
188 fprintf(stderr, "--image file name not specified\n");
189 return EXIT_FAILURE;
190 }
191
192 if (dex_filenames.empty()) {
193 fprintf(stderr, "no --dex-file values specified\n");
194 return EXIT_FAILURE;
195 }
196
197 if (boot_image_filename != NULL && boot_dex_filenames.empty()) {
198 fprintf(stderr, "no --boot-dex-file values specified with --boot\n");
199 return EXIT_FAILURE;
200 }
201
202 std::vector<const DexFile*> dex_files;
203 DexFile::OpenDexFiles(dex_filenames, dex_files, strip_location_prefix);
204
205 std::vector<const DexFile*> boot_dex_files;
206 DexFile::OpenDexFiles(boot_dex_filenames, boot_dex_files, strip_location_prefix);
207
208 Runtime::Options options;
209 std::string image_option;
210 std::string boot_image_option;
211 if (boot_image_filename == NULL) {
212 // if we don't have multiple images, pass the main one as the boot to match dex2oat
213 boot_image_filename = image_filename;
214 boot_dex_files = dex_files;
215 image_filename = NULL;
216 dex_files.clear();
217 } else {
218 image_option += "-Ximage:";
219 image_option += image_filename;
220 options.push_back(std::make_pair("classpath", &dex_files));
221 options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL)));
222 }
223 boot_image_option += "-Xbootimage:";
224 boot_image_option += boot_image_filename;
225 options.push_back(std::make_pair("bootclasspath", &boot_dex_files));
226 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
227
228 UniquePtr<Runtime> runtime(Runtime::Create(options, false));
229 if (runtime.get() == NULL) {
230 fprintf(stderr, "could not create runtime\n");
231 return EXIT_FAILURE;
232 }
233 ClassLinker* class_linker = runtime->GetClassLinker();
234 for (size_t i = 0; i < dex_files.size(); i++) {
235 class_linker->RegisterDexFile(*dex_files[i]);
236 }
237
238 Space* image_space = Heap::GetSpaces()[Heap::GetSpaces().size()-2];
239 CHECK(image_space != NULL);
240 const ImageHeader& image_header = image_space->GetImageHeader();
241 if (!image_header.IsValid()) {
242 fprintf(stderr, "invalid image header %s\n", image_filename);
243 return EXIT_FAILURE;
244 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700245 OatDump::Dump(*os, *image_space, image_header);
Brian Carlstrom78128a62011-09-15 17:21:19 -0700246 return EXIT_SUCCESS;
247}
248
249} // namespace art
250
251int main(int argc, char** argv) {
252 return art::oatdump(argc, argv);
253}