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