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