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 | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 152 | void CreateClassPath(const char* class_path_cstr, |
| 153 | std::vector<const DexFile*>& class_path_vector) { |
| 154 | CHECK(class_path_cstr != NULL); |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 155 | std::vector<std::string> parsed; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 156 | Split(class_path_cstr, ':', parsed); |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 157 | for (size_t i = 0; i < parsed.size(); ++i) { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 158 | const DexFile* dex_file = DexFile::Open(parsed[i]); |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 159 | if (dex_file != NULL) { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 160 | class_path_vector.push_back(dex_file); |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 165 | Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) { |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 166 | UniquePtr<ParsedOptions> parsed(new ParsedOptions()); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 167 | const char* boot_class_path = NULL; |
| 168 | const char* class_path = NULL; |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 169 | parsed->boot_image_ = NULL; |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 170 | #ifdef NDEBUG |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 171 | // -Xcheck:jni is off by default for regular builds... |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 172 | parsed->check_jni_ = false; |
| 173 | #else |
| 174 | // ...but on by default in debug builds. |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 175 | #if 0 // TODO: disabled for oatexec until the shorty's used by check_jni are managed heap allocated. |
| 176 | // Instead we turn on -Xcheck_jni in common_test. |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 177 | parsed->check_jni_ = true; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 178 | #else |
| 179 | parsed->check_jni_ = false; |
| 180 | #endif |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 181 | #endif |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 182 | parsed->heap_initial_size_ = Heap::kInitialSize; |
| 183 | parsed->heap_maximum_size_ = Heap::kMaximumSize; |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 184 | parsed->stack_size_ = Thread::kDefaultStackSize; |
| 185 | |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 186 | parsed->hook_vfprintf_ = vfprintf; |
| 187 | parsed->hook_exit_ = exit; |
| 188 | parsed->hook_abort_ = abort; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 189 | |
| 190 | for (size_t i = 0; i < options.size(); ++i) { |
| 191 | const StringPiece& option = options[i].first; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 192 | if (option.starts_with("-Xbootclasspath:")) { |
| 193 | boot_class_path = option.substr(strlen("-Xbootclasspath:")).data(); |
| 194 | } else if (option == "bootclasspath") { |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 195 | const void* dex_vector = options[i].second; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 196 | const std::vector<const DexFile*>* v |
| 197 | = reinterpret_cast<const std::vector<const DexFile*>*>(dex_vector); |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 198 | if (v == NULL) { |
| 199 | if (ignore_unrecognized) { |
| 200 | continue; |
| 201 | } |
| 202 | // TODO: usage |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 203 | LOG(FATAL) << "Failed to parse " << option; |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 204 | return NULL; |
| 205 | } |
| 206 | parsed->boot_class_path_ = *v; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 207 | } else if (option == "-classpath" || option == "-cp") { |
| 208 | // TODO: support -Djava.class.path |
| 209 | i++; |
| 210 | if (i == options.size()) { |
| 211 | // TODO: usage |
| 212 | LOG(FATAL) << "Missing required class path value for " << option; |
| 213 | return NULL; |
| 214 | } |
| 215 | const StringPiece& value = options[i].first; |
| 216 | class_path = value.data(); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 217 | } else if (option.starts_with("-Xbootimage:")) { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 218 | // TODO: remove when intern_addr_ is removed, just use -Ximage: |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 219 | parsed->boot_image_ = option.substr(strlen("-Xbootimage:")).data(); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 220 | } else if (option.starts_with("-Ximage:")) { |
| 221 | parsed->images_.push_back(option.substr(strlen("-Ximage:")).data()); |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 222 | } else if (option.starts_with("-Xcheck:jni")) { |
| 223 | parsed->check_jni_ = true; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 224 | } else if (option.starts_with("-Xms")) { |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 225 | size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024); |
| 226 | if (size == 0) { |
| 227 | if (ignore_unrecognized) { |
| 228 | continue; |
| 229 | } |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 230 | // TODO: usage |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 231 | LOG(FATAL) << "Failed to parse " << option; |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 232 | return NULL; |
| 233 | } |
| 234 | parsed->heap_initial_size_ = size; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 235 | } else if (option.starts_with("-Xmx")) { |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 236 | size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024); |
| 237 | if (size == 0) { |
| 238 | if (ignore_unrecognized) { |
| 239 | continue; |
| 240 | } |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 241 | // TODO: usage |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 242 | LOG(FATAL) << "Failed to parse " << option; |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 243 | return NULL; |
| 244 | } |
| 245 | parsed->heap_maximum_size_ = size; |
| 246 | } else if (option.starts_with("-Xss")) { |
| 247 | size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1); |
| 248 | if (size == 0) { |
| 249 | if (ignore_unrecognized) { |
| 250 | continue; |
| 251 | } |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 252 | // TODO: usage |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 253 | LOG(FATAL) << "Failed to parse " << option; |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 254 | return NULL; |
| 255 | } |
| 256 | parsed->stack_size_ = size; |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 257 | } else if (option.starts_with("-D")) { |
| 258 | parsed->properties_.push_back(option.substr(strlen("-D")).data()); |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 259 | } else if (option.starts_with("-Xjnitrace:")) { |
| 260 | parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:")).data(); |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 261 | } else if (option.starts_with("-verbose:")) { |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 262 | std::vector<std::string> verbose_options; |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 263 | Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options); |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 264 | for (size_t i = 0; i < verbose_options.size(); ++i) { |
| 265 | parsed->verbose_.insert(verbose_options[i]); |
| 266 | } |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 267 | } else if (option == "vfprintf") { |
| 268 | parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second); |
| 269 | } else if (option == "exit") { |
| 270 | parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second); |
| 271 | } else if (option == "abort") { |
| 272 | parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 273 | } else { |
| 274 | if (!ignore_unrecognized) { |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 275 | // TODO: print usage via vfprintf |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 276 | LOG(FATAL) << "Unrecognized option " << option; |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 277 | return NULL; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 282 | // consider it an error if both bootclasspath and -Xbootclasspath: are supplied. |
| 283 | // TODO: remove bootclasspath which is only mostly just used by tests? |
| 284 | if (!parsed->boot_class_path_.empty() && boot_class_path != NULL) { |
| 285 | // TODO: usage |
| 286 | LOG(FATAL) << "bootclasspath and -Xbootclasspath: are mutually exclusive options."; |
| 287 | return NULL; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 288 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 289 | if (parsed->boot_class_path_.empty()) { |
| 290 | if (boot_class_path == NULL) { |
| 291 | boot_class_path = getenv("BOOTCLASSPATH"); |
| 292 | if (boot_class_path == NULL) { |
| 293 | boot_class_path = ""; |
| 294 | } |
| 295 | } |
| 296 | CreateClassPath(boot_class_path, parsed->boot_class_path_); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 297 | } |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 298 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 299 | if (class_path == NULL) { |
| 300 | class_path = getenv("CLASSPATH"); |
| 301 | if (class_path == NULL) { |
| 302 | class_path = ""; |
| 303 | } |
| 304 | } |
| 305 | CHECK_EQ(parsed->class_path_.size(), 0U); |
| 306 | CreateClassPath(class_path, parsed->class_path_); |
| 307 | |
| 308 | return parsed.release(); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 312 | // TODO: acquire a static mutex on Runtime to avoid racing. |
| 313 | if (Runtime::instance_ != NULL) { |
| 314 | return NULL; |
| 315 | } |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 316 | UniquePtr<Runtime> runtime(new Runtime()); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 317 | bool success = runtime->Init(options, ignore_unrecognized); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 318 | if (!success) { |
| 319 | return NULL; |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 320 | } |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 321 | instance_ = runtime.release(); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 322 | return instance_; |
| 323 | } |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 324 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 325 | void Runtime::Start() { |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 326 | instance_->InitLibraries(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 327 | instance_->signal_catcher_ = new SignalCatcher; |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 330 | bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) { |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 331 | CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize); |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 332 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 333 | UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized)); |
| 334 | if (options.get() == NULL) { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 335 | LOG(WARNING) << "Failed to parse options"; |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 336 | return false; |
| 337 | } |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 338 | vfprintf_ = options->hook_vfprintf_; |
| 339 | exit_ = options->hook_exit_; |
| 340 | abort_ = options->hook_abort_; |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 341 | |
Brian Carlstrom | b765be0 | 2011-08-17 23:54:10 -0700 | [diff] [blame] | 342 | stack_size_ = options->stack_size_; |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 343 | thread_list_ = ThreadList::Create(); |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 344 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 345 | intern_table_ = new InternTable; |
| 346 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 347 | if (!Heap::Init(options->heap_initial_size_, |
| 348 | options->heap_maximum_size_, |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 349 | options->boot_image_, |
| 350 | options->images_)) { |
| 351 | LOG(WARNING) << "Failed to create heap"; |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 352 | return false; |
| 353 | } |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 354 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 355 | BlockSignals(); |
| 356 | |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 357 | java_vm_ = new JavaVMExt(this, options.get()); |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 358 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 359 | if (!Thread::Startup()) { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 360 | LOG(WARNING) << "Failed to startup threads"; |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 361 | return false; |
| 362 | } |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 363 | |
| 364 | thread_list_->Register(Thread::Attach(this)); |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 365 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 366 | class_linker_ = ClassLinker::Create(options->boot_class_path_, |
| 367 | options->class_path_, |
| 368 | intern_table_, |
| 369 | Heap::GetBootSpace()); |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 370 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 371 | return true; |
| 372 | } |
| 373 | |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 374 | void Runtime::InitLibraries() { |
| 375 | Thread* self = Thread::Current(); |
| 376 | JNIEnv* env = self->GetJniEnv(); |
| 377 | |
| 378 | // Must be in the kNative state for JNI-based method registration. |
| 379 | ScopedThreadStateChange tsc(self, Thread::kNative); |
| 380 | |
| 381 | // First set up the native methods provided by the runtime itself. |
| 382 | RegisterRuntimeNativeMethods(env); |
| 383 | |
| 384 | // Now set up libcore, which is just a JNI library with a JNI_OnLoad. |
| 385 | // Most JNI libraries can just use System.loadLibrary, but you can't |
| 386 | // if you're the library that implements System.loadLibrary! |
| 387 | JniConstants::init(env); |
| 388 | LoadJniLibrary(instance_->GetJavaVM(), "javacore"); |
| 389 | } |
| 390 | |
| 391 | void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) { |
| 392 | #define REGISTER(FN) extern void FN(JNIEnv*); FN(env) |
| 393 | //REGISTER(register_dalvik_bytecode_OpcodeInfo); |
| 394 | //REGISTER(register_dalvik_system_DexFile); |
| 395 | //REGISTER(register_dalvik_system_VMDebug); |
| 396 | //REGISTER(register_dalvik_system_VMRuntime); |
| 397 | //REGISTER(register_dalvik_system_VMStack); |
| 398 | //REGISTER(register_dalvik_system_Zygote); |
| 399 | //REGISTER(register_java_lang_Class); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 400 | REGISTER(register_java_lang_Object); |
| 401 | REGISTER(register_java_lang_Runtime); |
| 402 | REGISTER(register_java_lang_String); |
| 403 | REGISTER(register_java_lang_System); |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 404 | //REGISTER(register_java_lang_Thread); |
| 405 | //REGISTER(register_java_lang_Throwable); |
| 406 | //REGISTER(register_java_lang_VMClassLoader); |
| 407 | //REGISTER(register_java_lang_reflect_AccessibleObject); |
| 408 | //REGISTER(register_java_lang_reflect_Array); |
| 409 | //REGISTER(register_java_lang_reflect_Constructor); |
| 410 | //REGISTER(register_java_lang_reflect_Field); |
| 411 | //REGISTER(register_java_lang_reflect_Method); |
| 412 | //REGISTER(register_java_lang_reflect_Proxy); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 413 | REGISTER(register_java_util_concurrent_atomic_AtomicLong); |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 414 | //REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer); |
| 415 | //REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal); |
| 416 | //REGISTER(register_sun_misc_Unsafe); |
| 417 | #undef REGISTER |
| 418 | } |
| 419 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 420 | void Runtime::DumpStatistics(std::ostream& os) { |
| 421 | // TODO: dump other runtime statistics? |
| 422 | os << "Loaded classes: " << class_linker_->NumLoadedClasses() << "\n"; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 423 | os << "Intern table size: " << GetInternTable()->Size() << "\n"; |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 424 | // LOGV("VM stats: meth=%d ifld=%d sfld=%d linear=%d", |
| 425 | // gDvm.numDeclaredMethods, |
| 426 | // gDvm.numDeclaredInstFields, |
| 427 | // gDvm.numDeclaredStaticFields, |
| 428 | // gDvm.pBootLoaderAlloc->curOffset); |
| 429 | // LOGI("GC precise methods: %d", dvmPointerSetGetCount(gDvm.preciseMethods)); |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 430 | os << "\n"; |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 431 | } |
| 432 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 433 | void Runtime::BlockSignals() { |
| 434 | sigset_t sigset; |
| 435 | if (sigemptyset(&sigset) == -1) { |
| 436 | PLOG(FATAL) << "sigemptyset failed"; |
| 437 | } |
| 438 | if (sigaddset(&sigset, SIGPIPE) == -1) { |
| 439 | PLOG(ERROR) << "sigaddset SIGPIPE failed"; |
| 440 | } |
| 441 | // SIGQUIT is used to dump the runtime's state (including stack traces). |
| 442 | if (sigaddset(&sigset, SIGQUIT) == -1) { |
| 443 | PLOG(ERROR) << "sigaddset SIGQUIT failed"; |
| 444 | } |
| 445 | // SIGUSR1 is used to initiate a heap dump. |
| 446 | if (sigaddset(&sigset, SIGUSR1) == -1) { |
| 447 | PLOG(ERROR) << "sigaddset SIGUSR1 failed"; |
| 448 | } |
| 449 | CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0); |
| 450 | } |
| 451 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 452 | void Runtime::AttachCurrentThread(const char* name, JNIEnv** penv, bool as_daemon) { |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 453 | if (as_daemon) { |
| 454 | UNIMPLEMENTED(WARNING) << "TODO: do something different for daemon threads"; |
| 455 | } |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 456 | Thread* t = Thread::Attach(instance_); |
| 457 | thread_list_->Register(t); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 460 | void Runtime::DetachCurrentThread() { |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 461 | Thread* self = Thread::Current(); |
| 462 | thread_list_->Unregister(self); |
| 463 | delete self; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 466 | void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const { |
| 467 | class_linker_->VisitRoots(visitor, arg); |
| 468 | intern_table_->VisitRoots(visitor, arg); |
| 469 | java_vm_->VisitRoots(visitor, arg); |
| 470 | thread_list_->VisitRoots(visitor, arg); |
| 471 | |
| 472 | //(*visitor)(&gDvm.outOfMemoryObj, 0, ROOT_VM_INTERNAL, arg); |
| 473 | //(*visitor)(&gDvm.internalErrorObj, 0, ROOT_VM_INTERNAL, arg); |
| 474 | //(*visitor)(&gDvm.noClassDefFoundErrorObj, 0, ROOT_VM_INTERNAL, arg); |
| 475 | UNIMPLEMENTED(WARNING) << "some roots not marked"; |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 478 | } // namespace art |