Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
| 5 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 6 | #include <fstream> |
| 7 | #include <iostream> |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 8 | #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 | |
| 17 | namespace art { |
| 18 | |
| 19 | static 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 Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 50 | 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 Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 54 | exit(EXIT_FAILURE); |
| 55 | } |
| 56 | |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame^] | 57 | const char* image_roots_descriptions_[] = { |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 58 | "kJniStubArray", |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame^] | 59 | "kCalleeSaveMethod" |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 62 | class OatDump { |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 63 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 64 | public: |
| 65 | static void Dump(std::ostream& os, Space& image_space, const ImageHeader& image_header) { |
| 66 | os << "MAGIC:\n"; |
| 67 | os << image_header.GetMagic() << "\n\n"; |
| 68 | |
| 69 | os << "ROOTS:\n"; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame^] | 70 | CHECK(sizeof(image_roots_descriptions_)/(sizeof(char*)) == ImageHeader::kImageRootsMax); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 71 | for (int i = 0; i < ImageHeader::kImageRootsMax; i++) { |
| 72 | ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i); |
| 73 | os << StringPrintf("%s: %p\n", |
| 74 | image_roots_descriptions_[i], image_header.GetImageRoot(image_root)); |
| 75 | } |
| 76 | os << "\n"; |
| 77 | |
| 78 | os << "OBJECTS:\n" << std::flush; |
| 79 | OatDump state(image_space, os); |
| 80 | HeapBitmap* heap_bitmap = Heap::GetLiveBits(); |
| 81 | DCHECK(heap_bitmap != NULL); |
| 82 | heap_bitmap->Walk(OatDump::Callback, &state); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 85 | private: |
| 86 | |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame^] | 87 | OatDump(const Space& dump_space, std::ostream& os) : dump_space_(dump_space), os_(os) {} |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 88 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 89 | static void Callback(Object* obj, void* arg) { |
| 90 | DCHECK(obj != NULL); |
| 91 | DCHECK(arg != NULL); |
| 92 | OatDump* state = reinterpret_cast<OatDump*>(arg); |
| 93 | if (!state->InDumpSpace(obj)) { |
| 94 | return; |
| 95 | } |
| 96 | std::string summary; |
| 97 | StringAppendF(&summary, "%p: ", obj); |
| 98 | if (obj->IsClass()) { |
| 99 | Class* klass = obj->AsClass(); |
| 100 | StringAppendF(&summary, "CLASS %s", klass->GetDescriptor()->ToModifiedUtf8().c_str()); |
| 101 | } else if (obj->IsMethod()) { |
| 102 | Method* method = obj->AsMethod(); |
| 103 | StringAppendF(&summary, "METHOD %s", PrettyMethod(method).c_str()); |
| 104 | } else if (obj->IsField()) { |
| 105 | Field* field = obj->AsField(); |
| 106 | Class* type = field->GetType(); |
| 107 | std::string type_string; |
| 108 | type_string += (type == NULL) ? "<UNKNOWN>" : type->GetDescriptor()->ToModifiedUtf8(); |
| 109 | StringAppendF(&summary, "FIELD %s", PrettyField(field).c_str()); |
| 110 | } else if (obj->IsArrayInstance()) { |
| 111 | StringAppendF(&summary, "ARRAY %d", obj->AsArray()->GetLength()); |
| 112 | } else if (obj->IsString()) { |
| 113 | StringAppendF(&summary, "STRING %s", obj->AsString()->ToModifiedUtf8().c_str()); |
| 114 | } else { |
| 115 | StringAppendF(&summary, "OBJECT"); |
| 116 | } |
| 117 | StringAppendF(&summary, "\n"); |
| 118 | StringAppendF(&summary, "\tclass %p: %s\n", |
| 119 | obj->GetClass(), obj->GetClass()->GetDescriptor()->ToModifiedUtf8().c_str()); |
| 120 | if (obj->IsMethod()) { |
| 121 | Method* method = obj->AsMethod(); |
| 122 | const ByteArray* code = method->GetCodeArray(); |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame^] | 123 | if (method->IsPhony()) { |
| 124 | CHECK(code == NULL); |
| 125 | StringAppendF(&summary, "\tPHONY\n"); |
| 126 | } else { |
| 127 | StringAppendF(&summary, "\tCODE %p-%p\n", code->GetData(), code->GetData() + code->GetLength()); |
| 128 | const ByteArray* invoke = method->GetInvokeStubArray(); |
| 129 | StringAppendF(&summary, "\tJNI STUB %p-%p\n", invoke->GetData(), invoke->GetData() + invoke->GetLength()); |
| 130 | } |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 131 | if (method->IsNative()) { |
| 132 | if (method->IsRegistered()) { |
| 133 | StringAppendF(&summary, "\tNATIVE REGISTERED %p\n", method->GetNativeMethod()); |
| 134 | } else { |
| 135 | StringAppendF(&summary, "\tNATIVE UNREGISTERED\n"); |
| 136 | } |
| 137 | } |
| 138 | } |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 139 | state->os_ << summary << std::flush; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 140 | } |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 141 | |
| 142 | bool InDumpSpace(const Object* object) { |
| 143 | const byte* o = reinterpret_cast<const byte*>(object); |
| 144 | return (o >= dump_space_.GetBase() && o < dump_space_.GetLimit()); |
| 145 | } |
| 146 | const Space& dump_space_; |
| 147 | std::ostream& os_; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | int oatdump(int argc, char** argv) { |
| 151 | // Skip over argv[0]. |
| 152 | argv++; |
| 153 | argc--; |
| 154 | |
| 155 | if (argc == 0) { |
| 156 | fprintf(stderr, "no arguments specified\n"); |
| 157 | usage(); |
| 158 | } |
| 159 | |
| 160 | std::vector<const char*> dex_filenames; |
| 161 | const char* image_filename = NULL; |
| 162 | const char* boot_image_filename = NULL; |
| 163 | std::vector<const char*> boot_dex_filenames; |
| 164 | std::string strip_location_prefix; |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 165 | std::ostream* os = &std::cout; |
| 166 | UniquePtr<std::ofstream> out; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 167 | |
| 168 | for (int i = 0; i < argc; i++) { |
| 169 | const StringPiece option(argv[i]); |
| 170 | if (option.starts_with("--dex-file=")) { |
| 171 | dex_filenames.push_back(option.substr(strlen("--dex-file=")).data()); |
| 172 | } else if (option.starts_with("--image=")) { |
| 173 | image_filename = option.substr(strlen("--image=")).data(); |
| 174 | } else if (option.starts_with("--boot=")) { |
| 175 | boot_image_filename = option.substr(strlen("--boot=")).data(); |
| 176 | } else if (option.starts_with("--boot-dex-file=")) { |
| 177 | boot_dex_filenames.push_back(option.substr(strlen("--boot-dex-file=")).data()); |
| 178 | } else if (option.starts_with("--strip-prefix=")) { |
| 179 | strip_location_prefix = option.substr(strlen("--strip-prefix=")).data(); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 180 | } else if (option.starts_with("--output=")) { |
| 181 | const char* filename = option.substr(strlen("--output=")).data(); |
| 182 | out.reset(new std::ofstream(filename)); |
| 183 | if (!out->good()) { |
| 184 | fprintf(stderr, "failed to open output filename %s\n", filename); |
| 185 | usage(); |
| 186 | } |
| 187 | os = out.get(); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 188 | } else { |
| 189 | fprintf(stderr, "unknown argument %s\n", option.data()); |
| 190 | usage(); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | if (image_filename == NULL) { |
| 195 | fprintf(stderr, "--image file name not specified\n"); |
| 196 | return EXIT_FAILURE; |
| 197 | } |
| 198 | |
| 199 | if (dex_filenames.empty()) { |
| 200 | fprintf(stderr, "no --dex-file values specified\n"); |
| 201 | return EXIT_FAILURE; |
| 202 | } |
| 203 | |
| 204 | if (boot_image_filename != NULL && boot_dex_filenames.empty()) { |
| 205 | fprintf(stderr, "no --boot-dex-file values specified with --boot\n"); |
| 206 | return EXIT_FAILURE; |
| 207 | } |
| 208 | |
| 209 | std::vector<const DexFile*> dex_files; |
| 210 | DexFile::OpenDexFiles(dex_filenames, dex_files, strip_location_prefix); |
| 211 | |
| 212 | std::vector<const DexFile*> boot_dex_files; |
| 213 | DexFile::OpenDexFiles(boot_dex_filenames, boot_dex_files, strip_location_prefix); |
| 214 | |
| 215 | Runtime::Options options; |
| 216 | std::string image_option; |
| 217 | std::string boot_image_option; |
| 218 | if (boot_image_filename == NULL) { |
| 219 | // if we don't have multiple images, pass the main one as the boot to match dex2oat |
| 220 | boot_image_filename = image_filename; |
| 221 | boot_dex_files = dex_files; |
| 222 | image_filename = NULL; |
| 223 | dex_files.clear(); |
| 224 | } else { |
| 225 | image_option += "-Ximage:"; |
| 226 | image_option += image_filename; |
| 227 | options.push_back(std::make_pair("classpath", &dex_files)); |
| 228 | options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL))); |
| 229 | } |
| 230 | boot_image_option += "-Xbootimage:"; |
| 231 | boot_image_option += boot_image_filename; |
| 232 | options.push_back(std::make_pair("bootclasspath", &boot_dex_files)); |
| 233 | options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL))); |
| 234 | |
| 235 | UniquePtr<Runtime> runtime(Runtime::Create(options, false)); |
| 236 | if (runtime.get() == NULL) { |
| 237 | fprintf(stderr, "could not create runtime\n"); |
| 238 | return EXIT_FAILURE; |
| 239 | } |
| 240 | ClassLinker* class_linker = runtime->GetClassLinker(); |
| 241 | for (size_t i = 0; i < dex_files.size(); i++) { |
| 242 | class_linker->RegisterDexFile(*dex_files[i]); |
| 243 | } |
| 244 | |
| 245 | Space* image_space = Heap::GetSpaces()[Heap::GetSpaces().size()-2]; |
| 246 | CHECK(image_space != NULL); |
| 247 | const ImageHeader& image_header = image_space->GetImageHeader(); |
| 248 | if (!image_header.IsValid()) { |
| 249 | fprintf(stderr, "invalid image header %s\n", image_filename); |
| 250 | return EXIT_FAILURE; |
| 251 | } |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 252 | OatDump::Dump(*os, *image_space, image_header); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 253 | return EXIT_SUCCESS; |
| 254 | } |
| 255 | |
| 256 | } // namespace art |
| 257 | |
| 258 | int main(int argc, char** argv) { |
| 259 | return art::oatdump(argc, argv); |
| 260 | } |