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