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