Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3 | #include "runtime.h" |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 4 | |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 5 | #include <cstdio> |
| 6 | #include <cstdlib> |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 7 | #include <limits> |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 8 | #include <vector> |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 9 | |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 10 | #include "JniConstants.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 11 | #include "class_linker.h" |
| 12 | #include "heap.h" |
Elliott Hughes | c5f7c91 | 2011-08-18 14:00:42 -0700 | [diff] [blame] | 13 | #include "jni_internal.h" |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 14 | #include "scoped_ptr.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 15 | #include "thread.h" |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 16 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 17 | namespace art { |
| 18 | |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 19 | Runtime* Runtime::instance_ = NULL; |
| 20 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 21 | Runtime::~Runtime() { |
Elliott Hughes | c5f7c91 | 2011-08-18 14:00:42 -0700 | [diff] [blame] | 22 | // TODO: use smart pointers instead. (we'll need the pimpl idiom.) |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 23 | delete class_linker_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 24 | Heap::Destroy(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 25 | delete thread_list_; |
Elliott Hughes | c5f7c91 | 2011-08-18 14:00:42 -0700 | [diff] [blame] | 26 | delete java_vm_; |
Carl Shapiro | 4acf464 | 2011-07-26 18:54:13 -0700 | [diff] [blame] | 27 | // TODO: acquire a static mutex on Runtime to avoid racing. |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 28 | CHECK(instance_ == NULL || instance_ == this); |
Carl Shapiro | 4acf464 | 2011-07-26 18:54:13 -0700 | [diff] [blame] | 29 | instance_ = NULL; |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 30 | } |
| 31 | |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 32 | void Runtime::Abort(const char* file, int line) { |
| 33 | // Get any pending output out of the way. |
| 34 | fflush(NULL); |
| 35 | |
| 36 | // Many people have difficulty distinguish aborts from crashes, |
| 37 | // so be explicit. |
| 38 | LogMessage(file, line, ERROR, -1).stream() << "Runtime aborting..."; |
| 39 | |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 40 | // Perform any platform-specific pre-abort actions. |
| 41 | PlatformAbort(file, line); |
| 42 | |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 43 | // use abort hook if we have one |
| 44 | if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) { |
| 45 | Runtime::Current()->abort_(); |
| 46 | // notreached |
| 47 | } |
| 48 | |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 49 | // If we call abort(3) on a device, all threads in the process |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 50 | // receive SIGABRT. debuggerd dumps the stack trace of the main |
| 51 | // thread, whether or not that was the thread that failed. By |
| 52 | // stuffing a value into a bogus address, we cause a segmentation |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 53 | // fault in the current thread, and get a useful log from debuggerd. |
| 54 | // We can also trivially tell the difference between a VM crash and |
| 55 | // a deliberate abort by looking at the fault address. |
| 56 | *reinterpret_cast<char*>(0xdeadd00d) = 38; |
| 57 | abort(); |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 58 | // notreached |
| 59 | } |
| 60 | |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 61 | // Splits a C string using the given delimiter characters into a vector of |
| 62 | // strings. Empty strings will be omitted. |
| 63 | void Split(const char* str, const char* delim, std::vector<std::string>& vec) { |
| 64 | DCHECK(str != NULL); |
| 65 | DCHECK(delim != NULL); |
| 66 | scoped_ptr_malloc<char> tmp(strdup(str)); |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 67 | char* full = tmp.get(); |
| 68 | char* p = full; |
Carl Shapiro | d3c1575 | 2011-07-27 16:27:22 -0700 | [diff] [blame] | 69 | while (p != NULL) { |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 70 | p = strpbrk(full, delim); |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 71 | if (p != NULL) { |
| 72 | p[0] = '\0'; |
| 73 | } |
| 74 | if (full[0] != '\0') { |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 75 | vec.push_back(std::string(full)); |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 76 | } |
Carl Shapiro | d3c1575 | 2011-07-27 16:27:22 -0700 | [diff] [blame] | 77 | if (p != NULL) { |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 78 | full = p + 1; |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 83 | // Splits a colon delimited list of pathname elements into a vector of |
| 84 | // strings. Empty strings will be omitted. |
| 85 | void ParseClassPath(const char* class_path, std::vector<std::string>& vec) { |
| 86 | Split(class_path, ":", vec); |
| 87 | } |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 88 | |
| 89 | // Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify |
| 90 | // memory sizes. [kK] indicates kilobytes, [mM] megabytes, and |
| 91 | // [gG] gigabytes. |
| 92 | // |
| 93 | // "s" should point just past the "-Xm?" part of the string. |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 94 | // "div" specifies a divisor, e.g. 1024 if the value must be a multiple |
| 95 | // of 1024. |
| 96 | // |
| 97 | // The spec says the -Xmx and -Xms options must be multiples of 1024. It |
| 98 | // doesn't say anything about -Xss. |
| 99 | // |
| 100 | // Returns 0 (a useless size) if "s" is malformed or specifies a low or |
| 101 | // non-evenly-divisible value. |
| 102 | // |
| 103 | size_t ParseMemoryOption(const char *s, size_t div) { |
| 104 | // strtoul accepts a leading [+-], which we don't want, |
| 105 | // so make sure our string starts with a decimal digit. |
| 106 | if (isdigit(*s)) { |
| 107 | const char *s2; |
| 108 | size_t val = strtoul(s, (char **)&s2, 10); |
| 109 | if (s2 != s) { |
| 110 | // s2 should be pointing just after the number. |
| 111 | // If this is the end of the string, the user |
| 112 | // has specified a number of bytes. Otherwise, |
| 113 | // there should be exactly one more character |
| 114 | // that specifies a multiplier. |
| 115 | if (*s2 != '\0') { |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 116 | // The remainder of the string is either a single multiplier |
| 117 | // character, or nothing to indicate that the value is in |
| 118 | // bytes. |
| 119 | char c = *s2++; |
| 120 | if (*s2 == '\0') { |
| 121 | size_t mul; |
| 122 | if (c == '\0') { |
| 123 | mul = 1; |
| 124 | } else if (c == 'k' || c == 'K') { |
| 125 | mul = KB; |
| 126 | } else if (c == 'm' || c == 'M') { |
| 127 | mul = MB; |
| 128 | } else if (c == 'g' || c == 'G') { |
| 129 | mul = GB; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 130 | } else { |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 131 | // Unknown multiplier character. |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 132 | return 0; |
| 133 | } |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 134 | |
| 135 | if (val <= std::numeric_limits<size_t>::max() / mul) { |
| 136 | val *= mul; |
| 137 | } else { |
| 138 | // Clamp to a multiple of 1024. |
| 139 | val = std::numeric_limits<size_t>::max() & ~(1024-1); |
| 140 | } |
| 141 | } else { |
| 142 | // There's more than one character after the numeric part. |
| 143 | return 0; |
| 144 | } |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 145 | } |
| 146 | // The man page says that a -Xm value must be a multiple of 1024. |
| 147 | if (val % div == 0) { |
| 148 | return val; |
| 149 | } |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 150 | } |
| 151 | } |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 152 | return 0; |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 155 | void LoadJniLibrary(JavaVMExt* vm, const char* name) { |
| 156 | // TODO: OS_SHARED_LIB_FORMAT_STR |
| 157 | std::string mapped_name(StringPrintf("lib%s.so", name)); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 158 | std::string reason; |
| 159 | if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) { |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 160 | LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": " |
| 161 | << reason; |
| 162 | } |
| 163 | } |
| 164 | |
Elliott Hughes | 40ef99e | 2011-08-11 17:44:34 -0700 | [diff] [blame] | 165 | DexFile* Open(const std::string& filename) { |
| 166 | if (filename.size() < 4) { |
| 167 | LOG(WARNING) << "Ignoring short classpath entry '" << filename << "'"; |
| 168 | return NULL; |
| 169 | } |
| 170 | std::string suffix(filename.substr(filename.size() - 4)); |
| 171 | if (suffix == ".zip" || suffix == ".jar" || suffix == ".apk") { |
| 172 | return DexFile::OpenZip(filename); |
| 173 | } else { |
| 174 | return DexFile::OpenFile(filename); |
| 175 | } |
| 176 | } |
| 177 | |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 178 | void CreateBootClassPath(const char* boot_class_path_cstr, |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame^] | 179 | std::vector<const DexFile*>& boot_class_path_vector) { |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 180 | CHECK(boot_class_path_cstr != NULL); |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 181 | std::vector<std::string> parsed; |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 182 | ParseClassPath(boot_class_path_cstr, parsed); |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 183 | for (size_t i = 0; i < parsed.size(); ++i) { |
Elliott Hughes | 40ef99e | 2011-08-11 17:44:34 -0700 | [diff] [blame] | 184 | DexFile* dex_file = Open(parsed[i]); |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 185 | if (dex_file != NULL) { |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 186 | boot_class_path_vector.push_back(dex_file); |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 191 | Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) { |
| 192 | scoped_ptr<ParsedOptions> parsed(new ParsedOptions()); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 193 | const char* boot_class_path = getenv("BOOTCLASSPATH"); |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 194 | parsed->boot_image_ = NULL; |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 195 | #ifdef NDEBUG |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 196 | // -Xcheck:jni is off by default for regular builds... |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 197 | parsed->check_jni_ = false; |
| 198 | #else |
| 199 | // ...but on by default in debug builds. |
| 200 | parsed->check_jni_ = true; |
| 201 | #endif |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 202 | parsed->heap_initial_size_ = Heap::kInitialSize; |
| 203 | parsed->heap_maximum_size_ = Heap::kMaximumSize; |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 204 | parsed->stack_size_ = Thread::kDefaultStackSize; |
| 205 | |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 206 | parsed->hook_vfprintf_ = vfprintf; |
| 207 | parsed->hook_exit_ = exit; |
| 208 | parsed->hook_abort_ = abort; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 209 | |
| 210 | for (size_t i = 0; i < options.size(); ++i) { |
| 211 | const StringPiece& option = options[i].first; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 212 | if (option.starts_with("-Xbootclasspath:")) { |
| 213 | boot_class_path = option.substr(strlen("-Xbootclasspath:")).data(); |
| 214 | } else if (option == "bootclasspath") { |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 215 | const void* dex_vector = options[i].second; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame^] | 216 | const std::vector<const DexFile*>* v |
| 217 | = reinterpret_cast<const std::vector<const DexFile*>*>(dex_vector); |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 218 | if (v == NULL) { |
| 219 | if (ignore_unrecognized) { |
| 220 | continue; |
| 221 | } |
| 222 | // TODO: usage |
| 223 | LOG(FATAL) << "Could not parse " << option; |
| 224 | return NULL; |
| 225 | } |
| 226 | parsed->boot_class_path_ = *v; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 227 | } else if (option.starts_with("-Xbootimage:")) { |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 228 | parsed->boot_image_ = option.substr(strlen("-Xbootimage:")).data(); |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 229 | } else if (option.starts_with("-Xcheck:jni")) { |
| 230 | parsed->check_jni_ = true; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 231 | } else if (option.starts_with("-Xms")) { |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 232 | size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024); |
| 233 | if (size == 0) { |
| 234 | if (ignore_unrecognized) { |
| 235 | continue; |
| 236 | } |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 237 | // TODO: usage |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 238 | LOG(FATAL) << "Could not parse " << option; |
| 239 | return NULL; |
| 240 | } |
| 241 | parsed->heap_initial_size_ = size; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 242 | } else if (option.starts_with("-Xmx")) { |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 243 | size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024); |
| 244 | if (size == 0) { |
| 245 | if (ignore_unrecognized) { |
| 246 | continue; |
| 247 | } |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 248 | // TODO: usage |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 249 | LOG(FATAL) << "Could not parse " << option; |
| 250 | return NULL; |
| 251 | } |
| 252 | parsed->heap_maximum_size_ = size; |
| 253 | } else if (option.starts_with("-Xss")) { |
| 254 | size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1); |
| 255 | if (size == 0) { |
| 256 | if (ignore_unrecognized) { |
| 257 | continue; |
| 258 | } |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 259 | // TODO: usage |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 260 | LOG(FATAL) << "Could not parse " << option; |
| 261 | return NULL; |
| 262 | } |
| 263 | parsed->stack_size_ = size; |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 264 | } else if (option.starts_with("-D")) { |
| 265 | parsed->properties_.push_back(option.substr(strlen("-D")).data()); |
| 266 | } else if (option.starts_with("-verbose:")) { |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 267 | std::vector<std::string> verbose_options; |
| 268 | Split(option.substr(strlen("-verbose:")).data(), ",", verbose_options); |
| 269 | for (size_t i = 0; i < verbose_options.size(); ++i) { |
| 270 | parsed->verbose_.insert(verbose_options[i]); |
| 271 | } |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 272 | } else if (option == "vfprintf") { |
| 273 | parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second); |
| 274 | } else if (option == "exit") { |
| 275 | parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second); |
| 276 | } else if (option == "abort") { |
| 277 | parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 278 | } else { |
| 279 | if (!ignore_unrecognized) { |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 280 | // TODO: print usage via vfprintf |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 281 | LOG(FATAL) << "Unrecognized option " << option; |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 282 | return NULL; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | if (boot_class_path == NULL) { |
| 288 | boot_class_path = ""; |
| 289 | } |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 290 | if (parsed->boot_class_path_.size() == 0) { |
| 291 | CreateBootClassPath(boot_class_path, parsed->boot_class_path_); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 292 | } |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 293 | return parsed.release(); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 296 | Runtime* Runtime::Create(const std::vector<const DexFile*>& boot_class_path) { |
| 297 | Runtime::Options options; |
| 298 | options.push_back(std::make_pair("bootclasspath", &boot_class_path)); |
| 299 | return Runtime::Create(options, false); |
| 300 | } |
| 301 | |
| 302 | Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 303 | // TODO: acquire a static mutex on Runtime to avoid racing. |
| 304 | if (Runtime::instance_ != NULL) { |
| 305 | return NULL; |
| 306 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 307 | scoped_ptr<Runtime> runtime(new Runtime()); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 308 | bool success = runtime->Init(options, ignore_unrecognized); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 309 | if (!success) { |
| 310 | return NULL; |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 311 | } |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 312 | instance_ = runtime.release(); |
| 313 | |
| 314 | // Most JNI libraries can just use System.loadLibrary, but you can't |
| 315 | // if you're the library that implements System.loadLibrary! |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 316 | Thread* self = Thread::Current(); |
| 317 | Thread::State old_state = self->GetState(); |
| 318 | self->SetState(Thread::kNative); |
| 319 | JniConstants::init(self->GetJniEnv()); |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 320 | LoadJniLibrary(instance_->GetJavaVM(), "javacore"); |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 321 | self->SetState(old_state); |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 322 | |
| 323 | return instance_; |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 326 | bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) { |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 327 | CHECK_EQ(kPageSize, sysconf(_SC_PAGE_SIZE)); |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 328 | |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 329 | scoped_ptr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized)); |
| 330 | if (options == NULL) { |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 331 | return false; |
| 332 | } |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 333 | vfprintf_ = options->hook_vfprintf_; |
| 334 | exit_ = options->hook_exit_; |
| 335 | abort_ = options->hook_abort_; |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 336 | |
Brian Carlstrom | b765be0 | 2011-08-17 23:54:10 -0700 | [diff] [blame] | 337 | stack_size_ = options->stack_size_; |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 338 | thread_list_ = ThreadList::Create(); |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 339 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 340 | if (!Heap::Init(options->heap_initial_size_, |
| 341 | options->heap_maximum_size_, |
| 342 | options->boot_image_)) { |
| 343 | return false; |
| 344 | } |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 345 | |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 346 | bool verbose_jni = options->verbose_.find("jni") != options->verbose_.end(); |
Elliott Hughes | c5f7c91 | 2011-08-18 14:00:42 -0700 | [diff] [blame] | 347 | java_vm_ = new JavaVMExt(this, options->check_jni_, verbose_jni); |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 348 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 349 | if (!Thread::Init()) { |
| 350 | return false; |
| 351 | } |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 352 | Thread* current_thread = Thread::Attach(this); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 353 | thread_list_->Register(current_thread); |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 354 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 355 | class_linker_ = ClassLinker::Create(options->boot_class_path_, Heap::GetBootSpace()); |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 356 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 357 | return true; |
| 358 | } |
| 359 | |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 360 | bool Runtime::AttachCurrentThread(const char* name, JNIEnv** penv, bool as_daemon) { |
| 361 | if (as_daemon) { |
| 362 | UNIMPLEMENTED(WARNING) << "TODO: do something different for daemon threads"; |
| 363 | } |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 364 | return Thread::Attach(instance_) != NULL; |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 367 | bool Runtime::DetachCurrentThread() { |
Elliott Hughes | 53b6131 | 2011-08-12 18:28:20 -0700 | [diff] [blame] | 368 | UNIMPLEMENTED(WARNING); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 369 | return true; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | } // namespace art |