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 | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 10 | #include "UniquePtr.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 11 | #include "class_linker.h" |
| 12 | #include "heap.h" |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 13 | #include "intern_table.h" |
Elliott Hughes | c5f7c91 | 2011-08-18 14:00:42 -0700 | [diff] [blame] | 14 | #include "jni_internal.h" |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 15 | #include "signal_catcher.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 16 | #include "thread.h" |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 17 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 18 | // TODO: this drags in cutil/log.h, which conflicts with our logging.h. |
| 19 | #include "JniConstants.h" |
| 20 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 21 | namespace art { |
| 22 | |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 23 | Runtime* Runtime::instance_ = NULL; |
| 24 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 25 | Runtime::~Runtime() { |
Elliott Hughes | c5f7c91 | 2011-08-18 14:00:42 -0700 | [diff] [blame] | 26 | // TODO: use smart pointers instead. (we'll need the pimpl idiom.) |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 27 | delete class_linker_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 28 | Heap::Destroy(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 29 | delete signal_catcher_; |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 30 | delete thread_list_; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 31 | delete intern_table_; |
Elliott Hughes | c5f7c91 | 2011-08-18 14:00:42 -0700 | [diff] [blame] | 32 | delete java_vm_; |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 33 | Thread::Shutdown(); |
Carl Shapiro | 4acf464 | 2011-07-26 18:54:13 -0700 | [diff] [blame] | 34 | // TODO: acquire a static mutex on Runtime to avoid racing. |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 35 | CHECK(instance_ == NULL || instance_ == this); |
Carl Shapiro | 4acf464 | 2011-07-26 18:54:13 -0700 | [diff] [blame] | 36 | instance_ = NULL; |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 39 | void Runtime::Abort(const char* file, int line) { |
| 40 | // Get any pending output out of the way. |
| 41 | fflush(NULL); |
| 42 | |
| 43 | // Many people have difficulty distinguish aborts from crashes, |
| 44 | // so be explicit. |
| 45 | LogMessage(file, line, ERROR, -1).stream() << "Runtime aborting..."; |
| 46 | |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 47 | // Perform any platform-specific pre-abort actions. |
| 48 | PlatformAbort(file, line); |
| 49 | |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 50 | // use abort hook if we have one |
| 51 | if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) { |
| 52 | Runtime::Current()->abort_(); |
| 53 | // notreached |
| 54 | } |
| 55 | |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 56 | // 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] | 57 | // receive SIGABRT. debuggerd dumps the stack trace of the main |
| 58 | // thread, whether or not that was the thread that failed. By |
| 59 | // stuffing a value into a bogus address, we cause a segmentation |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 60 | // fault in the current thread, and get a useful log from debuggerd. |
| 61 | // We can also trivially tell the difference between a VM crash and |
| 62 | // a deliberate abort by looking at the fault address. |
| 63 | *reinterpret_cast<char*>(0xdeadd00d) = 38; |
| 64 | abort(); |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 65 | // notreached |
| 66 | } |
| 67 | |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 68 | void Runtime::CallExitHook(jint status) { |
| 69 | if (exit_ != NULL) { |
| 70 | ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative); |
| 71 | exit_(status); |
| 72 | LOG(WARNING) << "Exit hook returned instead of exiting!"; |
| 73 | } |
| 74 | } |
| 75 | |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 76 | // Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify |
| 77 | // memory sizes. [kK] indicates kilobytes, [mM] megabytes, and |
| 78 | // [gG] gigabytes. |
| 79 | // |
| 80 | // "s" should point just past the "-Xm?" part of the string. |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 81 | // "div" specifies a divisor, e.g. 1024 if the value must be a multiple |
| 82 | // of 1024. |
| 83 | // |
| 84 | // The spec says the -Xmx and -Xms options must be multiples of 1024. It |
| 85 | // doesn't say anything about -Xss. |
| 86 | // |
| 87 | // Returns 0 (a useless size) if "s" is malformed or specifies a low or |
| 88 | // non-evenly-divisible value. |
| 89 | // |
| 90 | size_t ParseMemoryOption(const char *s, size_t div) { |
| 91 | // strtoul accepts a leading [+-], which we don't want, |
| 92 | // so make sure our string starts with a decimal digit. |
| 93 | if (isdigit(*s)) { |
| 94 | const char *s2; |
| 95 | size_t val = strtoul(s, (char **)&s2, 10); |
| 96 | if (s2 != s) { |
| 97 | // s2 should be pointing just after the number. |
| 98 | // If this is the end of the string, the user |
| 99 | // has specified a number of bytes. Otherwise, |
| 100 | // there should be exactly one more character |
| 101 | // that specifies a multiplier. |
| 102 | if (*s2 != '\0') { |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 103 | // The remainder of the string is either a single multiplier |
| 104 | // character, or nothing to indicate that the value is in |
| 105 | // bytes. |
| 106 | char c = *s2++; |
| 107 | if (*s2 == '\0') { |
| 108 | size_t mul; |
| 109 | if (c == '\0') { |
| 110 | mul = 1; |
| 111 | } else if (c == 'k' || c == 'K') { |
| 112 | mul = KB; |
| 113 | } else if (c == 'm' || c == 'M') { |
| 114 | mul = MB; |
| 115 | } else if (c == 'g' || c == 'G') { |
| 116 | mul = GB; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 117 | } else { |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 118 | // Unknown multiplier character. |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 119 | return 0; |
| 120 | } |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 121 | |
| 122 | if (val <= std::numeric_limits<size_t>::max() / mul) { |
| 123 | val *= mul; |
| 124 | } else { |
| 125 | // Clamp to a multiple of 1024. |
| 126 | val = std::numeric_limits<size_t>::max() & ~(1024-1); |
| 127 | } |
| 128 | } else { |
| 129 | // There's more than one character after the numeric part. |
| 130 | return 0; |
| 131 | } |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 132 | } |
| 133 | // The man page says that a -Xm value must be a multiple of 1024. |
| 134 | if (val % div == 0) { |
| 135 | return val; |
| 136 | } |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 137 | } |
| 138 | } |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 139 | return 0; |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 142 | void LoadJniLibrary(JavaVMExt* vm, const char* name) { |
| 143 | // TODO: OS_SHARED_LIB_FORMAT_STR |
| 144 | std::string mapped_name(StringPrintf("lib%s.so", name)); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 145 | std::string reason; |
| 146 | if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) { |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 147 | LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": " |
| 148 | << reason; |
| 149 | } |
| 150 | } |
| 151 | |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 152 | const DexFile* Open(const std::string& filename) { |
Elliott Hughes | 40ef99e | 2011-08-11 17:44:34 -0700 | [diff] [blame] | 153 | if (filename.size() < 4) { |
| 154 | LOG(WARNING) << "Ignoring short classpath entry '" << filename << "'"; |
| 155 | return NULL; |
| 156 | } |
| 157 | std::string suffix(filename.substr(filename.size() - 4)); |
| 158 | if (suffix == ".zip" || suffix == ".jar" || suffix == ".apk") { |
| 159 | return DexFile::OpenZip(filename); |
| 160 | } else { |
| 161 | return DexFile::OpenFile(filename); |
| 162 | } |
| 163 | } |
| 164 | |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 165 | void CreateBootClassPath(const char* boot_class_path_cstr, |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 166 | std::vector<const DexFile*>& boot_class_path_vector) { |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 167 | CHECK(boot_class_path_cstr != NULL); |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 168 | std::vector<std::string> parsed; |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 169 | Split(boot_class_path_cstr, ':', parsed); |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 170 | for (size_t i = 0; i < parsed.size(); ++i) { |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 171 | const DexFile* dex_file = Open(parsed[i]); |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 172 | if (dex_file != NULL) { |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 173 | boot_class_path_vector.push_back(dex_file); |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 178 | Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) { |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 179 | UniquePtr<ParsedOptions> parsed(new ParsedOptions()); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 180 | const char* boot_class_path = getenv("BOOTCLASSPATH"); |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 181 | parsed->boot_image_ = NULL; |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 182 | #ifdef NDEBUG |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 183 | // -Xcheck:jni is off by default for regular builds... |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 184 | parsed->check_jni_ = false; |
| 185 | #else |
| 186 | // ...but on by default in debug builds. |
| 187 | parsed->check_jni_ = true; |
| 188 | #endif |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 189 | parsed->heap_initial_size_ = Heap::kInitialSize; |
| 190 | parsed->heap_maximum_size_ = Heap::kMaximumSize; |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 191 | parsed->stack_size_ = Thread::kDefaultStackSize; |
| 192 | |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 193 | parsed->hook_vfprintf_ = vfprintf; |
| 194 | parsed->hook_exit_ = exit; |
| 195 | parsed->hook_abort_ = abort; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 196 | |
| 197 | for (size_t i = 0; i < options.size(); ++i) { |
| 198 | const StringPiece& option = options[i].first; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 199 | if (option.starts_with("-Xbootclasspath:")) { |
| 200 | boot_class_path = option.substr(strlen("-Xbootclasspath:")).data(); |
| 201 | } else if (option == "bootclasspath") { |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 202 | const void* dex_vector = options[i].second; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 203 | const std::vector<const DexFile*>* v |
| 204 | = reinterpret_cast<const std::vector<const DexFile*>*>(dex_vector); |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 205 | if (v == NULL) { |
| 206 | if (ignore_unrecognized) { |
| 207 | continue; |
| 208 | } |
| 209 | // TODO: usage |
| 210 | LOG(FATAL) << "Could not parse " << option; |
| 211 | return NULL; |
| 212 | } |
| 213 | parsed->boot_class_path_ = *v; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 214 | } else if (option.starts_with("-Xbootimage:")) { |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 215 | parsed->boot_image_ = option.substr(strlen("-Xbootimage:")).data(); |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 216 | } else if (option.starts_with("-Xcheck:jni")) { |
| 217 | parsed->check_jni_ = true; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 218 | } else if (option.starts_with("-Xms")) { |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 219 | size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024); |
| 220 | if (size == 0) { |
| 221 | if (ignore_unrecognized) { |
| 222 | continue; |
| 223 | } |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 224 | // TODO: usage |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 225 | LOG(FATAL) << "Could not parse " << option; |
| 226 | return NULL; |
| 227 | } |
| 228 | parsed->heap_initial_size_ = size; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 229 | } else if (option.starts_with("-Xmx")) { |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 230 | size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024); |
| 231 | if (size == 0) { |
| 232 | if (ignore_unrecognized) { |
| 233 | continue; |
| 234 | } |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 235 | // TODO: usage |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 236 | LOG(FATAL) << "Could not parse " << option; |
| 237 | return NULL; |
| 238 | } |
| 239 | parsed->heap_maximum_size_ = size; |
| 240 | } else if (option.starts_with("-Xss")) { |
| 241 | size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1); |
| 242 | if (size == 0) { |
| 243 | if (ignore_unrecognized) { |
| 244 | continue; |
| 245 | } |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 246 | // TODO: usage |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 247 | LOG(FATAL) << "Could not parse " << option; |
| 248 | return NULL; |
| 249 | } |
| 250 | parsed->stack_size_ = size; |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 251 | } else if (option.starts_with("-D")) { |
| 252 | parsed->properties_.push_back(option.substr(strlen("-D")).data()); |
| 253 | } else if (option.starts_with("-verbose:")) { |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 254 | std::vector<std::string> verbose_options; |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 255 | Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options); |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 256 | for (size_t i = 0; i < verbose_options.size(); ++i) { |
| 257 | parsed->verbose_.insert(verbose_options[i]); |
| 258 | } |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 259 | } else if (option == "vfprintf") { |
| 260 | parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second); |
| 261 | } else if (option == "exit") { |
| 262 | parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second); |
| 263 | } else if (option == "abort") { |
| 264 | parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 265 | } else { |
| 266 | if (!ignore_unrecognized) { |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 267 | // TODO: print usage via vfprintf |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 268 | LOG(FATAL) << "Unrecognized option " << option; |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 269 | return NULL; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | if (boot_class_path == NULL) { |
| 275 | boot_class_path = ""; |
| 276 | } |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 277 | if (parsed->boot_class_path_.size() == 0) { |
| 278 | CreateBootClassPath(boot_class_path, parsed->boot_class_path_); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 279 | } |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 280 | return parsed.release(); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 283 | Runtime* Runtime::Create(const std::vector<const DexFile*>& boot_class_path) { |
| 284 | Runtime::Options options; |
| 285 | options.push_back(std::make_pair("bootclasspath", &boot_class_path)); |
| 286 | return Runtime::Create(options, false); |
| 287 | } |
| 288 | |
| 289 | Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 290 | // TODO: acquire a static mutex on Runtime to avoid racing. |
| 291 | if (Runtime::instance_ != NULL) { |
| 292 | return NULL; |
| 293 | } |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 294 | UniquePtr<Runtime> runtime(new Runtime()); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 295 | bool success = runtime->Init(options, ignore_unrecognized); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 296 | if (!success) { |
| 297 | return NULL; |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 298 | } |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 299 | instance_ = runtime.release(); |
| 300 | |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 301 | instance_->InitLibraries(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 302 | instance_->signal_catcher_ = new SignalCatcher; |
| 303 | |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 304 | return instance_; |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 307 | bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) { |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 308 | CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize); |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 309 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 310 | UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized)); |
| 311 | if (options.get() == NULL) { |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 312 | return false; |
| 313 | } |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 314 | vfprintf_ = options->hook_vfprintf_; |
| 315 | exit_ = options->hook_exit_; |
| 316 | abort_ = options->hook_abort_; |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 317 | |
Brian Carlstrom | b765be0 | 2011-08-17 23:54:10 -0700 | [diff] [blame] | 318 | stack_size_ = options->stack_size_; |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 319 | thread_list_ = ThreadList::Create(); |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 320 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 321 | intern_table_ = new InternTable; |
| 322 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 323 | if (!Heap::Init(options->heap_initial_size_, |
| 324 | options->heap_maximum_size_, |
| 325 | options->boot_image_)) { |
| 326 | return false; |
| 327 | } |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 328 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 329 | BlockSignals(); |
| 330 | |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 331 | bool verbose_jni = options->verbose_.find("jni") != options->verbose_.end(); |
Elliott Hughes | c5f7c91 | 2011-08-18 14:00:42 -0700 | [diff] [blame] | 332 | java_vm_ = new JavaVMExt(this, options->check_jni_, verbose_jni); |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 333 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 334 | if (!Thread::Startup()) { |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 335 | return false; |
| 336 | } |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 337 | Thread* current_thread = Thread::Attach(this); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 338 | thread_list_->Register(current_thread); |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 339 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 340 | class_linker_ = ClassLinker::Create(options->boot_class_path_, intern_table_, Heap::GetBootSpace()); |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 341 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 342 | return true; |
| 343 | } |
| 344 | |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 345 | void Runtime::InitLibraries() { |
| 346 | Thread* self = Thread::Current(); |
| 347 | JNIEnv* env = self->GetJniEnv(); |
| 348 | |
| 349 | // Must be in the kNative state for JNI-based method registration. |
| 350 | ScopedThreadStateChange tsc(self, Thread::kNative); |
| 351 | |
| 352 | // First set up the native methods provided by the runtime itself. |
| 353 | RegisterRuntimeNativeMethods(env); |
| 354 | |
| 355 | // Now set up libcore, which is just a JNI library with a JNI_OnLoad. |
| 356 | // Most JNI libraries can just use System.loadLibrary, but you can't |
| 357 | // if you're the library that implements System.loadLibrary! |
| 358 | JniConstants::init(env); |
| 359 | LoadJniLibrary(instance_->GetJavaVM(), "javacore"); |
| 360 | } |
| 361 | |
| 362 | void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) { |
| 363 | #define REGISTER(FN) extern void FN(JNIEnv*); FN(env) |
| 364 | //REGISTER(register_dalvik_bytecode_OpcodeInfo); |
| 365 | //REGISTER(register_dalvik_system_DexFile); |
| 366 | //REGISTER(register_dalvik_system_VMDebug); |
| 367 | //REGISTER(register_dalvik_system_VMRuntime); |
| 368 | //REGISTER(register_dalvik_system_VMStack); |
| 369 | //REGISTER(register_dalvik_system_Zygote); |
| 370 | //REGISTER(register_java_lang_Class); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 371 | REGISTER(register_java_lang_Object); |
| 372 | REGISTER(register_java_lang_Runtime); |
| 373 | REGISTER(register_java_lang_String); |
| 374 | REGISTER(register_java_lang_System); |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 375 | //REGISTER(register_java_lang_Thread); |
| 376 | //REGISTER(register_java_lang_Throwable); |
| 377 | //REGISTER(register_java_lang_VMClassLoader); |
| 378 | //REGISTER(register_java_lang_reflect_AccessibleObject); |
| 379 | //REGISTER(register_java_lang_reflect_Array); |
| 380 | //REGISTER(register_java_lang_reflect_Constructor); |
| 381 | //REGISTER(register_java_lang_reflect_Field); |
| 382 | //REGISTER(register_java_lang_reflect_Method); |
| 383 | //REGISTER(register_java_lang_reflect_Proxy); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 384 | REGISTER(register_java_util_concurrent_atomic_AtomicLong); |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 385 | //REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer); |
| 386 | //REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal); |
| 387 | //REGISTER(register_sun_misc_Unsafe); |
| 388 | #undef REGISTER |
| 389 | } |
| 390 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 391 | void Runtime::DumpStatistics(std::ostream& os) { |
| 392 | // TODO: dump other runtime statistics? |
| 393 | os << "Loaded classes: " << class_linker_->NumLoadedClasses() << "\n"; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 394 | os << "Intern table size: " << GetInternTable()->Size() << "\n"; |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 395 | // LOGV("VM stats: meth=%d ifld=%d sfld=%d linear=%d", |
| 396 | // gDvm.numDeclaredMethods, |
| 397 | // gDvm.numDeclaredInstFields, |
| 398 | // gDvm.numDeclaredStaticFields, |
| 399 | // gDvm.pBootLoaderAlloc->curOffset); |
| 400 | // LOGI("GC precise methods: %d", dvmPointerSetGetCount(gDvm.preciseMethods)); |
| 401 | } |
| 402 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 403 | void Runtime::BlockSignals() { |
| 404 | sigset_t sigset; |
| 405 | if (sigemptyset(&sigset) == -1) { |
| 406 | PLOG(FATAL) << "sigemptyset failed"; |
| 407 | } |
| 408 | if (sigaddset(&sigset, SIGPIPE) == -1) { |
| 409 | PLOG(ERROR) << "sigaddset SIGPIPE failed"; |
| 410 | } |
| 411 | // SIGQUIT is used to dump the runtime's state (including stack traces). |
| 412 | if (sigaddset(&sigset, SIGQUIT) == -1) { |
| 413 | PLOG(ERROR) << "sigaddset SIGQUIT failed"; |
| 414 | } |
| 415 | // SIGUSR1 is used to initiate a heap dump. |
| 416 | if (sigaddset(&sigset, SIGUSR1) == -1) { |
| 417 | PLOG(ERROR) << "sigaddset SIGUSR1 failed"; |
| 418 | } |
| 419 | CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0); |
| 420 | } |
| 421 | |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 422 | bool Runtime::AttachCurrentThread(const char* name, JNIEnv** penv, bool as_daemon) { |
| 423 | if (as_daemon) { |
| 424 | UNIMPLEMENTED(WARNING) << "TODO: do something different for daemon threads"; |
| 425 | } |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 426 | return Thread::Attach(instance_) != NULL; |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 429 | bool Runtime::DetachCurrentThread() { |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 430 | Thread* self = Thread::Current(); |
| 431 | thread_list_->Unregister(self); |
| 432 | delete self; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 433 | return true; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame^] | 436 | void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const { |
| 437 | class_linker_->VisitRoots(visitor, arg); |
| 438 | intern_table_->VisitRoots(visitor, arg); |
| 439 | java_vm_->VisitRoots(visitor, arg); |
| 440 | thread_list_->VisitRoots(visitor, arg); |
| 441 | |
| 442 | //(*visitor)(&gDvm.outOfMemoryObj, 0, ROOT_VM_INTERNAL, arg); |
| 443 | //(*visitor)(&gDvm.internalErrorObj, 0, ROOT_VM_INTERNAL, arg); |
| 444 | //(*visitor)(&gDvm.noClassDefFoundErrorObj, 0, ROOT_VM_INTERNAL, arg); |
| 445 | UNIMPLEMENTED(WARNING) << "some roots not marked"; |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 448 | } // namespace art |