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 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 10 | #include "ScopedLocalRef.h" |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 11 | #include "UniquePtr.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 12 | #include "class_linker.h" |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 13 | #include "class_loader.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 14 | #include "heap.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 15 | #include "image.h" |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 16 | #include "intern_table.h" |
Elliott Hughes | c5f7c91 | 2011-08-18 14:00:42 -0700 | [diff] [blame] | 17 | #include "jni_internal.h" |
Elliott Hughes | 32d6e1e | 2011-10-11 14:47:44 -0700 | [diff] [blame^] | 18 | #include "monitor.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 19 | #include "oat_file.h" |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 20 | #include "ScopedLocalRef.h" |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 21 | #include "signal_catcher.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 22 | #include "space.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 23 | #include "thread.h" |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 24 | #include "thread_list.h" |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 25 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 26 | // TODO: this drags in cutil/log.h, which conflicts with our logging.h. |
| 27 | #include "JniConstants.h" |
| 28 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 29 | namespace art { |
| 30 | |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 31 | Runtime* Runtime::instance_ = NULL; |
| 32 | |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 33 | Runtime::Runtime() |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 34 | : verbose_startup_(false), |
Elliott Hughes | 9ca7a1f | 2011-10-11 14:29:52 -0700 | [diff] [blame] | 35 | is_zygote_(false), |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 36 | default_stack_size_(Thread::kDefaultStackSize), |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 37 | thread_list_(NULL), |
| 38 | intern_table_(NULL), |
| 39 | class_linker_(NULL), |
| 40 | signal_catcher_(NULL), |
| 41 | java_vm_(NULL), |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 42 | jni_stub_array_(NULL), |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 43 | abstract_method_error_stub_array_(NULL), |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 44 | started_(false), |
| 45 | vfprintf_(NULL), |
| 46 | exit_(NULL), |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 47 | abort_(NULL), |
| 48 | stats_enabled_(false) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 49 | for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) { |
| 50 | resolution_stub_array_[i] = NULL; |
| 51 | } |
| 52 | for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) { |
| 53 | callee_save_method_[i] = NULL; |
| 54 | } |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 57 | Runtime::~Runtime() { |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 58 | // Make sure our internal threads are dead before we start tearing down things they're using. |
| 59 | delete signal_catcher_; |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 60 | // TODO: GC thread. |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 61 | |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 62 | // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended. |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 63 | delete thread_list_; |
| 64 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 65 | delete class_linker_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 66 | Heap::Destroy(); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 67 | delete intern_table_; |
Elliott Hughes | c5f7c91 | 2011-08-18 14:00:42 -0700 | [diff] [blame] | 68 | delete java_vm_; |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 69 | Thread::Shutdown(); |
Carl Shapiro | 4acf464 | 2011-07-26 18:54:13 -0700 | [diff] [blame] | 70 | // TODO: acquire a static mutex on Runtime to avoid racing. |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 71 | CHECK(instance_ == NULL || instance_ == this); |
Carl Shapiro | 4acf464 | 2011-07-26 18:54:13 -0700 | [diff] [blame] | 72 | instance_ = NULL; |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 75 | void Runtime::Abort(const char* file, int line) { |
| 76 | // Get any pending output out of the way. |
| 77 | fflush(NULL); |
| 78 | |
| 79 | // Many people have difficulty distinguish aborts from crashes, |
| 80 | // so be explicit. |
| 81 | LogMessage(file, line, ERROR, -1).stream() << "Runtime aborting..."; |
| 82 | |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 83 | // Perform any platform-specific pre-abort actions. |
| 84 | PlatformAbort(file, line); |
| 85 | |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 86 | // use abort hook if we have one |
| 87 | if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) { |
| 88 | Runtime::Current()->abort_(); |
| 89 | // notreached |
| 90 | } |
| 91 | |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 92 | // 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] | 93 | // receive SIGABRT. debuggerd dumps the stack trace of the main |
| 94 | // thread, whether or not that was the thread that failed. By |
| 95 | // stuffing a value into a bogus address, we cause a segmentation |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 96 | // fault in the current thread, and get a useful log from debuggerd. |
| 97 | // We can also trivially tell the difference between a VM crash and |
| 98 | // a deliberate abort by looking at the fault address. |
| 99 | *reinterpret_cast<char*>(0xdeadd00d) = 38; |
| 100 | abort(); |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 101 | // notreached |
| 102 | } |
| 103 | |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 104 | void Runtime::CallExitHook(jint status) { |
| 105 | if (exit_ != NULL) { |
| 106 | ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative); |
| 107 | exit_(status); |
| 108 | LOG(WARNING) << "Exit hook returned instead of exiting!"; |
| 109 | } |
| 110 | } |
| 111 | |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 112 | // Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify |
| 113 | // memory sizes. [kK] indicates kilobytes, [mM] megabytes, and |
| 114 | // [gG] gigabytes. |
| 115 | // |
| 116 | // "s" should point just past the "-Xm?" part of the string. |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 117 | // "div" specifies a divisor, e.g. 1024 if the value must be a multiple |
| 118 | // of 1024. |
| 119 | // |
| 120 | // The spec says the -Xmx and -Xms options must be multiples of 1024. It |
| 121 | // doesn't say anything about -Xss. |
| 122 | // |
| 123 | // Returns 0 (a useless size) if "s" is malformed or specifies a low or |
| 124 | // non-evenly-divisible value. |
| 125 | // |
| 126 | size_t ParseMemoryOption(const char *s, size_t div) { |
| 127 | // strtoul accepts a leading [+-], which we don't want, |
| 128 | // so make sure our string starts with a decimal digit. |
| 129 | if (isdigit(*s)) { |
| 130 | const char *s2; |
| 131 | size_t val = strtoul(s, (char **)&s2, 10); |
| 132 | if (s2 != s) { |
| 133 | // s2 should be pointing just after the number. |
| 134 | // If this is the end of the string, the user |
| 135 | // has specified a number of bytes. Otherwise, |
| 136 | // there should be exactly one more character |
| 137 | // that specifies a multiplier. |
| 138 | if (*s2 != '\0') { |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 139 | // The remainder of the string is either a single multiplier |
| 140 | // character, or nothing to indicate that the value is in |
| 141 | // bytes. |
| 142 | char c = *s2++; |
| 143 | if (*s2 == '\0') { |
| 144 | size_t mul; |
| 145 | if (c == '\0') { |
| 146 | mul = 1; |
| 147 | } else if (c == 'k' || c == 'K') { |
| 148 | mul = KB; |
| 149 | } else if (c == 'm' || c == 'M') { |
| 150 | mul = MB; |
| 151 | } else if (c == 'g' || c == 'G') { |
| 152 | mul = GB; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 153 | } else { |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 154 | // Unknown multiplier character. |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 155 | return 0; |
| 156 | } |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 157 | |
| 158 | if (val <= std::numeric_limits<size_t>::max() / mul) { |
| 159 | val *= mul; |
| 160 | } else { |
| 161 | // Clamp to a multiple of 1024. |
| 162 | val = std::numeric_limits<size_t>::max() & ~(1024-1); |
| 163 | } |
| 164 | } else { |
| 165 | // There's more than one character after the numeric part. |
| 166 | return 0; |
| 167 | } |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 168 | } |
| 169 | // The man page says that a -Xm value must be a multiple of 1024. |
| 170 | if (val % div == 0) { |
| 171 | return val; |
| 172 | } |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 173 | } |
| 174 | } |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 175 | return 0; |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 178 | void LoadJniLibrary(JavaVMExt* vm, const char* name) { |
| 179 | // TODO: OS_SHARED_LIB_FORMAT_STR |
| 180 | std::string mapped_name(StringPrintf("lib%s.so", name)); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 181 | std::string reason; |
| 182 | if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) { |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 183 | LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": " |
| 184 | << reason; |
| 185 | } |
| 186 | } |
| 187 | |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 188 | Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) { |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 189 | UniquePtr<ParsedOptions> parsed(new ParsedOptions()); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 190 | const char* boot_class_path = getenv("BOOTCLASSPATH"); |
| 191 | if (boot_class_path != NULL) { |
| 192 | parsed->boot_class_path_ = getenv("BOOTCLASSPATH"); |
| 193 | } |
| 194 | const char* class_path = getenv("CLASSPATH"); |
| 195 | if (class_path != NULL) { |
| 196 | parsed->class_path_ = getenv("CLASSPATH"); |
| 197 | } |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 198 | #ifdef NDEBUG |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 199 | // -Xcheck:jni is off by default for regular builds... |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 200 | parsed->check_jni_ = false; |
| 201 | #else |
| 202 | // ...but on by default in debug builds. |
| 203 | parsed->check_jni_ = true; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 204 | #endif |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 205 | |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 206 | parsed->heap_initial_size_ = Heap::kInitialSize; |
| 207 | parsed->heap_maximum_size_ = Heap::kMaximumSize; |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 208 | parsed->stack_size_ = Thread::kDefaultStackSize; |
| 209 | |
Elliott Hughes | 9ca7a1f | 2011-10-11 14:29:52 -0700 | [diff] [blame] | 210 | parsed->is_zygote_ = false; |
| 211 | |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 212 | parsed->hook_vfprintf_ = vfprintf; |
| 213 | parsed->hook_exit_ = exit; |
| 214 | parsed->hook_abort_ = abort; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 215 | |
| 216 | for (size_t i = 0; i < options.size(); ++i) { |
| 217 | const StringPiece& option = options[i].first; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 218 | if (false) { |
| 219 | LOG(INFO) << "option[" << i << "]=" << option; |
| 220 | } |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 221 | if (option.starts_with("-Xbootclasspath:")) { |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 222 | parsed->boot_class_path_ = option.substr(strlen("-Xbootclasspath:")).data(); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 223 | } else if (option == "-classpath" || option == "-cp") { |
| 224 | // TODO: support -Djava.class.path |
| 225 | i++; |
| 226 | if (i == options.size()) { |
| 227 | // TODO: usage |
| 228 | LOG(FATAL) << "Missing required class path value for " << option; |
| 229 | return NULL; |
| 230 | } |
| 231 | const StringPiece& value = options[i].first; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 232 | parsed->class_path_ = value.data(); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 233 | } else if (option.starts_with("-Ximage:")) { |
| 234 | parsed->images_.push_back(option.substr(strlen("-Ximage:")).data()); |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 235 | } else if (option.starts_with("-Xcheck:jni")) { |
| 236 | parsed->check_jni_ = true; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 237 | } else if (option.starts_with("-Xms")) { |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 238 | size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024); |
| 239 | if (size == 0) { |
| 240 | if (ignore_unrecognized) { |
| 241 | continue; |
| 242 | } |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 243 | // TODO: usage |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 244 | LOG(FATAL) << "Failed to parse " << option; |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 245 | return NULL; |
| 246 | } |
| 247 | parsed->heap_initial_size_ = size; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 248 | } else if (option.starts_with("-Xmx")) { |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 249 | size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024); |
| 250 | if (size == 0) { |
| 251 | if (ignore_unrecognized) { |
| 252 | continue; |
| 253 | } |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 254 | // TODO: usage |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 255 | LOG(FATAL) << "Failed to parse " << option; |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 256 | return NULL; |
| 257 | } |
| 258 | parsed->heap_maximum_size_ = size; |
| 259 | } else if (option.starts_with("-Xss")) { |
| 260 | size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1); |
| 261 | if (size == 0) { |
| 262 | if (ignore_unrecognized) { |
| 263 | continue; |
| 264 | } |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 265 | // TODO: usage |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 266 | LOG(FATAL) << "Failed to parse " << option; |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 267 | return NULL; |
| 268 | } |
| 269 | parsed->stack_size_ = size; |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 270 | } else if (option.starts_with("-D")) { |
| 271 | parsed->properties_.push_back(option.substr(strlen("-D")).data()); |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 272 | } else if (option.starts_with("-Xjnitrace:")) { |
| 273 | parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:")).data(); |
Elliott Hughes | 9ca7a1f | 2011-10-11 14:29:52 -0700 | [diff] [blame] | 274 | } else if (option == "-Xzygote") { |
| 275 | parsed->is_zygote_ = true; |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 276 | } else if (option.starts_with("-verbose:")) { |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 277 | std::vector<std::string> verbose_options; |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 278 | Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options); |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 279 | for (size_t i = 0; i < verbose_options.size(); ++i) { |
| 280 | parsed->verbose_.insert(verbose_options[i]); |
| 281 | } |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 282 | } else if (option == "vfprintf") { |
| 283 | parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second); |
| 284 | } else if (option == "exit") { |
| 285 | parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second); |
| 286 | } else if (option == "abort") { |
| 287 | parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 288 | } else if (option == "host-prefix") { |
| 289 | parsed->host_prefix_ = reinterpret_cast<const char*>(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 | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 293 | LOG(ERROR) << "Unrecognized option " << option; |
| 294 | // TODO: this should exit, but for now tolerate unknown options |
| 295 | //return NULL; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | } |
| 299 | |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 300 | LOG(INFO) << "CheckJNI is " << (parsed->check_jni_ ? "on" : "off"); |
| 301 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 302 | return parsed.release(); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 306 | // TODO: acquire a static mutex on Runtime to avoid racing. |
| 307 | if (Runtime::instance_ != NULL) { |
| 308 | return NULL; |
| 309 | } |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 310 | instance_ = new Runtime; |
| 311 | if (!instance_->Init(options, ignore_unrecognized)) { |
| 312 | delete instance_; |
| 313 | instance_ = NULL; |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 314 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 315 | return instance_; |
| 316 | } |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 317 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 318 | void CreateSystemClassLoader() { |
| 319 | if (ClassLoader::UseCompileTimeClassPath()) { |
| 320 | return; |
| 321 | } |
| 322 | |
| 323 | Thread* self = Thread::Current(); |
| 324 | |
| 325 | // Must be in the kNative state for calling native methods. |
| 326 | CHECK_EQ(self->GetState(), Thread::kNative); |
| 327 | |
| 328 | JNIEnv* env = self->GetJniEnv(); |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 329 | ScopedLocalRef<jclass> ClassLoader_class(env, env->FindClass("java/lang/ClassLoader")); |
| 330 | CHECK(ClassLoader_class.get() != NULL); |
| 331 | jmethodID getSystemClassLoader = env->GetStaticMethodID(ClassLoader_class.get(), |
| 332 | "getSystemClassLoader", |
| 333 | "()Ljava/lang/ClassLoader;"); |
| 334 | CHECK(getSystemClassLoader != NULL); |
| 335 | ScopedLocalRef<jobject> class_loader(env, env->CallStaticObjectMethod(ClassLoader_class.get(), |
| 336 | getSystemClassLoader)); |
| 337 | CHECK(class_loader.get() != NULL); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 338 | |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 339 | Thread::Current()->SetClassLoaderOverride(Decode<ClassLoader*>(env, class_loader.get())); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 342 | void Runtime::Start() { |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 343 | if (IsVerboseStartup()) { |
| 344 | LOG(INFO) << "Runtime::Start entering"; |
| 345 | } |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 346 | |
| 347 | CHECK(host_prefix_.empty()) << host_prefix_; |
| 348 | |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 349 | InitNativeMethods(); |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 350 | |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 351 | Thread::FinishStartup(); |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 352 | |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 353 | class_linker_->RunRootClinits(); |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 354 | |
Brian Carlstrom | 5d40f18 | 2011-09-26 22:29:18 -0700 | [diff] [blame] | 355 | // Class::AllocObject asserts that all objects allocated better be |
| 356 | // initialized after Runtime::IsStarted is true, so this needs to |
| 357 | // come after ClassLinker::RunRootClinits. |
| 358 | started_ = true; |
| 359 | |
Elliott Hughes | 9ca7a1f | 2011-10-11 14:29:52 -0700 | [diff] [blame] | 360 | if (!is_zygote_) { |
| 361 | signal_catcher_ = new SignalCatcher; |
| 362 | } |
| 363 | |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 364 | StartDaemonThreads(); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 365 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 366 | CreateSystemClassLoader(); |
| 367 | |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 368 | Thread::Current()->GetJniEnv()->locals.AssertEmpty(); |
| 369 | |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 370 | if (IsVerboseStartup()) { |
| 371 | LOG(INFO) << "Runtime::Start exiting"; |
| 372 | } |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | void Runtime::StartDaemonThreads() { |
Elliott Hughes | 9ca7a1f | 2011-10-11 14:29:52 -0700 | [diff] [blame] | 376 | if (IsVerboseStartup()) { |
| 377 | LOG(INFO) << "Runtime::StartDaemonThreads entering"; |
| 378 | } |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 379 | |
Elliott Hughes | 719b323 | 2011-09-25 17:42:19 -0700 | [diff] [blame] | 380 | Thread* self = Thread::Current(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 381 | |
| 382 | // Must be in the kNative state for calling native methods. |
| 383 | CHECK_EQ(self->GetState(), Thread::kNative); |
| 384 | |
Elliott Hughes | 719b323 | 2011-09-25 17:42:19 -0700 | [diff] [blame] | 385 | JNIEnv* env = self->GetJniEnv(); |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 386 | ScopedLocalRef<jclass> c(env, env->FindClass("java/lang/Daemons")); |
| 387 | CHECK(c.get() != NULL); |
| 388 | jmethodID mid = env->GetStaticMethodID(c.get(), "start", "()V"); |
Elliott Hughes | 719b323 | 2011-09-25 17:42:19 -0700 | [diff] [blame] | 389 | CHECK(mid != NULL); |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 390 | env->CallStaticVoidMethod(c.get(), mid); |
Elliott Hughes | 9ca7a1f | 2011-10-11 14:29:52 -0700 | [diff] [blame] | 391 | |
| 392 | if (IsVerboseStartup()) { |
| 393 | LOG(INFO) << "Runtime::StartDaemonThreads exiting"; |
| 394 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 397 | bool Runtime::IsStarted() const { |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 398 | return started_; |
| 399 | } |
| 400 | |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 401 | bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) { |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 402 | CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize); |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 403 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 404 | UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized)); |
| 405 | if (options.get() == NULL) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 406 | LOG(ERROR) << "Failed to parse options"; |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 407 | return false; |
| 408 | } |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 409 | verbose_startup_ = options->IsVerbose("startup"); |
| 410 | if (IsVerboseStartup()) { |
| 411 | LOG(INFO) << "Runtime::Init -verbose:startup enabled"; |
| 412 | } |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 413 | |
Elliott Hughes | 32d6e1e | 2011-10-11 14:47:44 -0700 | [diff] [blame^] | 414 | Monitor::SetVerbose(options->IsVerbose("monitor")); |
| 415 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 416 | host_prefix_ = options->host_prefix_; |
| 417 | boot_class_path_ = options->boot_class_path_; |
| 418 | class_path_ = options->class_path_; |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 419 | properties_ = options->properties_; |
| 420 | |
Elliott Hughes | 9ca7a1f | 2011-10-11 14:29:52 -0700 | [diff] [blame] | 421 | is_zygote_ = options->is_zygote_; |
| 422 | |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 423 | vfprintf_ = options->hook_vfprintf_; |
| 424 | exit_ = options->hook_exit_; |
| 425 | abort_ = options->hook_abort_; |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 426 | |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 427 | default_stack_size_ = options->stack_size_; |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 428 | |
Elliott Hughes | 14357e8 | 2011-09-26 10:42:15 -0700 | [diff] [blame] | 429 | thread_list_ = new ThreadList(options->IsVerbose("thread")); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 430 | intern_table_ = new InternTable; |
| 431 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 432 | Heap::Init(options->heap_initial_size_, options->heap_maximum_size_, options->images_); |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 433 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 434 | BlockSignals(); |
| 435 | |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 436 | java_vm_ = new JavaVMExt(this, options.get()); |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 437 | |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 438 | Thread::Startup(); |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 439 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 440 | // ClassLinker needs an attached thread, but we can't fully attach a thread |
| 441 | // without creating objects. |
| 442 | Thread::Attach(this, "main", false); |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 443 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 444 | CHECK_GE(Heap::GetSpaces().size(), 1U); |
| 445 | class_linker_ = ((Heap::GetSpaces()[0]->IsImageSpace()) |
| 446 | ? ClassLinker::Create(intern_table_) |
| 447 | : ClassLinker::Create(options->boot_class_path_, intern_table_)); |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 448 | |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 449 | if (IsVerboseStartup()) { |
| 450 | LOG(INFO) << "Runtime::Init exiting"; |
| 451 | } |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 452 | return true; |
| 453 | } |
| 454 | |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 455 | void Runtime::InitNativeMethods() { |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 456 | if (IsVerboseStartup()) { |
| 457 | LOG(INFO) << "Runtime::InitNativeMethods entering"; |
| 458 | } |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 459 | Thread* self = Thread::Current(); |
| 460 | JNIEnv* env = self->GetJniEnv(); |
| 461 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 462 | // Must be in the kNative state for calling native methods (JNI_OnLoad code). |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 463 | CHECK_EQ(self->GetState(), Thread::kNative); |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 464 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 465 | // First set up JniConstants, which is used by both the runtime's built-in native |
| 466 | // methods and libcore. |
Elliott Hughes | fea966e | 2011-09-22 10:26:20 -0700 | [diff] [blame] | 467 | JniConstants::init(env); |
| 468 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 469 | // Then set up the native methods provided by the runtime itself. |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 470 | RegisterRuntimeNativeMethods(env); |
| 471 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 472 | // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad. |
| 473 | // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's |
| 474 | // the library that implements System.loadLibrary! |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 475 | LoadJniLibrary(instance_->GetJavaVM(), "javacore"); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 476 | if (IsVerboseStartup()) { |
| 477 | LOG(INFO) << "Runtime::InitNativeMethods exiting"; |
| 478 | } |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) { |
| 482 | #define REGISTER(FN) extern void FN(JNIEnv*); FN(env) |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 483 | REGISTER(register_dalvik_system_DexFile); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 484 | REGISTER(register_dalvik_system_VMDebug); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 485 | REGISTER(register_dalvik_system_VMRuntime); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 486 | REGISTER(register_dalvik_system_VMStack); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 487 | REGISTER(register_dalvik_system_Zygote); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 488 | REGISTER(register_java_lang_Class); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 489 | REGISTER(register_java_lang_Object); |
| 490 | REGISTER(register_java_lang_Runtime); |
| 491 | REGISTER(register_java_lang_String); |
| 492 | REGISTER(register_java_lang_System); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 493 | REGISTER(register_java_lang_Thread); |
Elliott Hughes | 1240dad | 2011-09-09 16:24:50 -0700 | [diff] [blame] | 494 | REGISTER(register_java_lang_Throwable); |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 495 | REGISTER(register_java_lang_VMClassLoader); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 496 | REGISTER(register_java_lang_reflect_Array); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 497 | REGISTER(register_java_lang_reflect_Constructor); |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 498 | REGISTER(register_java_lang_reflect_Field); |
| 499 | REGISTER(register_java_lang_reflect_Method); |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 500 | //REGISTER(register_java_lang_reflect_Proxy); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 501 | REGISTER(register_java_util_concurrent_atomic_AtomicLong); |
Brian Carlstrom | 395520e | 2011-09-25 19:35:00 -0700 | [diff] [blame] | 502 | REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer); |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 503 | //REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 504 | REGISTER(register_sun_misc_Unsafe); |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 505 | #undef REGISTER |
| 506 | } |
| 507 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 508 | void Runtime::Dump(std::ostream& os) { |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 509 | // TODO: dump other runtime statistics? |
| 510 | os << "Loaded classes: " << class_linker_->NumLoadedClasses() << "\n"; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 511 | os << "Intern table size: " << GetInternTable()->Size() << "\n"; |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 512 | // LOGV("VM stats: meth=%d ifld=%d sfld=%d linear=%d", |
| 513 | // gDvm.numDeclaredMethods, |
| 514 | // gDvm.numDeclaredInstFields, |
| 515 | // gDvm.numDeclaredStaticFields, |
| 516 | // gDvm.pBootLoaderAlloc->curOffset); |
| 517 | // LOGI("GC precise methods: %d", dvmPointerSetGetCount(gDvm.preciseMethods)); |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 518 | os << "\n"; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 519 | |
| 520 | thread_list_->Dump(os); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 523 | void Runtime::SetStatsEnabled(bool new_state) { |
| 524 | if (new_state == true) { |
| 525 | GetStats()->Clear(~0); |
| 526 | // TODO: wouldn't it make more sense to clear _all_ threads' stats? |
| 527 | Thread::Current()->GetStats()->Clear(~0); |
| 528 | } |
| 529 | stats_enabled_ = new_state; |
| 530 | } |
| 531 | |
| 532 | void Runtime::ResetStats(int kinds) { |
| 533 | GetStats()->Clear(kinds & 0xffff); |
| 534 | // TODO: wouldn't it make more sense to clear _all_ threads' stats? |
| 535 | Thread::Current()->GetStats()->Clear(kinds >> 16); |
| 536 | } |
| 537 | |
| 538 | RuntimeStats* Runtime::GetStats() { |
| 539 | return &stats_; |
| 540 | } |
| 541 | |
| 542 | int32_t Runtime::GetStat(int kind) { |
| 543 | RuntimeStats* stats; |
| 544 | if (kind < (1<<16)) { |
| 545 | stats = GetStats(); |
| 546 | } else { |
| 547 | stats = Thread::Current()->GetStats(); |
| 548 | kind >>= 16; |
| 549 | } |
| 550 | switch (kind) { |
| 551 | case KIND_ALLOCATED_OBJECTS: |
| 552 | return stats->allocated_objects; |
| 553 | case KIND_ALLOCATED_BYTES: |
| 554 | return stats->allocated_bytes; |
| 555 | case KIND_FREED_OBJECTS: |
| 556 | return stats->freed_objects; |
| 557 | case KIND_FREED_BYTES: |
| 558 | return stats->freed_bytes; |
| 559 | case KIND_GC_INVOCATIONS: |
| 560 | return stats->gc_for_alloc_count; |
| 561 | case KIND_CLASS_INIT_COUNT: |
| 562 | return stats->class_init_count; |
| 563 | case KIND_CLASS_INIT_TIME: |
| 564 | // Convert ns to us, reduce to 32 bits. |
| 565 | return (int) (stats->class_init_time_ns / 1000); |
| 566 | case KIND_EXT_ALLOCATED_OBJECTS: |
| 567 | case KIND_EXT_ALLOCATED_BYTES: |
| 568 | case KIND_EXT_FREED_OBJECTS: |
| 569 | case KIND_EXT_FREED_BYTES: |
| 570 | return 0; // backward compatibility |
| 571 | default: |
| 572 | CHECK(false); |
| 573 | return -1; // unreachable |
| 574 | } |
| 575 | } |
| 576 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 577 | void Runtime::BlockSignals() { |
| 578 | sigset_t sigset; |
| 579 | if (sigemptyset(&sigset) == -1) { |
| 580 | PLOG(FATAL) << "sigemptyset failed"; |
| 581 | } |
| 582 | if (sigaddset(&sigset, SIGPIPE) == -1) { |
| 583 | PLOG(ERROR) << "sigaddset SIGPIPE failed"; |
| 584 | } |
| 585 | // SIGQUIT is used to dump the runtime's state (including stack traces). |
| 586 | if (sigaddset(&sigset, SIGQUIT) == -1) { |
| 587 | PLOG(ERROR) << "sigaddset SIGQUIT failed"; |
| 588 | } |
| 589 | // SIGUSR1 is used to initiate a heap dump. |
| 590 | if (sigaddset(&sigset, SIGUSR1) == -1) { |
| 591 | PLOG(ERROR) << "sigaddset SIGUSR1 failed"; |
| 592 | } |
| 593 | CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0); |
| 594 | } |
| 595 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 596 | void Runtime::AttachCurrentThread(const char* name, bool as_daemon) { |
| 597 | Thread::Attach(instance_, name, as_daemon); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 598 | } |
| 599 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 600 | void Runtime::DetachCurrentThread() { |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 601 | // TODO: check we're not calling DetachCurrentThread from a call stack that |
| 602 | // includes managed frames. (It's only valid if the stack is all-native.) |
Elliott Hughes | 02b48d1 | 2011-09-07 17:15:51 -0700 | [diff] [blame] | 603 | thread_list_->Unregister(); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 604 | } |
| 605 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 606 | void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const { |
| 607 | class_linker_->VisitRoots(visitor, arg); |
| 608 | intern_table_->VisitRoots(visitor, arg); |
| 609 | java_vm_->VisitRoots(visitor, arg); |
| 610 | thread_list_->VisitRoots(visitor, arg); |
| 611 | visitor(jni_stub_array_, arg); |
| 612 | visitor(abstract_method_error_stub_array_, arg); |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 613 | for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) { |
| 614 | visitor(resolution_stub_array_[i], arg); |
| 615 | } |
| 616 | for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) { |
| 617 | visitor(callee_save_method_[i], arg); |
| 618 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 619 | |
| 620 | //(*visitor)(&gDvm.outOfMemoryObj, 0, ROOT_VM_INTERNAL, arg); |
| 621 | //(*visitor)(&gDvm.internalErrorObj, 0, ROOT_VM_INTERNAL, arg); |
| 622 | //(*visitor)(&gDvm.noClassDefFoundErrorObj, 0, ROOT_VM_INTERNAL, arg); |
| 623 | UNIMPLEMENTED(WARNING) << "some roots not marked"; |
| 624 | } |
| 625 | |
| 626 | bool Runtime::HasJniStubArray() const { |
| 627 | return jni_stub_array_ != NULL; |
| 628 | } |
| 629 | |
| 630 | ByteArray* Runtime::GetJniStubArray() const { |
| 631 | CHECK(jni_stub_array_ != NULL); |
| 632 | return jni_stub_array_; |
| 633 | } |
| 634 | |
| 635 | void Runtime::SetJniStubArray(ByteArray* jni_stub_array) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 636 | CHECK(jni_stub_array != NULL) << " jni_stub_array=" << jni_stub_array; |
| 637 | CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array) |
| 638 | << "jni_stub_array_=" << jni_stub_array_ << " jni_stub_array=" << jni_stub_array; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 639 | jni_stub_array_ = jni_stub_array; |
| 640 | } |
| 641 | |
| 642 | bool Runtime::HasAbstractMethodErrorStubArray() const { |
| 643 | return abstract_method_error_stub_array_ != NULL; |
| 644 | } |
| 645 | |
| 646 | ByteArray* Runtime::GetAbstractMethodErrorStubArray() const { |
| 647 | CHECK(abstract_method_error_stub_array_ != NULL); |
| 648 | return abstract_method_error_stub_array_; |
| 649 | } |
| 650 | |
| 651 | void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) { |
| 652 | CHECK(abstract_method_error_stub_array != NULL); |
| 653 | CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array); |
| 654 | abstract_method_error_stub_array_ = abstract_method_error_stub_array; |
| 655 | } |
| 656 | |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 657 | |
| 658 | Runtime::TrampolineType Runtime::GetTrampolineType(Method* method) { |
| 659 | if (method == NULL) { |
| 660 | return Runtime::kUnknownMethod; |
| 661 | } else if (method->IsStatic()) { |
| 662 | return Runtime::kStaticMethod; |
| 663 | } else { |
| 664 | return Runtime::kInstanceMethod; |
| 665 | } |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 666 | } |
| 667 | |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 668 | bool Runtime::HasResolutionStubArray(TrampolineType type) const { |
| 669 | return resolution_stub_array_[type] != NULL; |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 670 | } |
| 671 | |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 672 | ByteArray* Runtime::GetResolutionStubArray(TrampolineType type) const { |
| 673 | CHECK(HasResolutionStubArray(type)); |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 674 | DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastTrampolineMethodType)); |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 675 | return resolution_stub_array_[type]; |
| 676 | } |
| 677 | |
| 678 | void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) { |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 679 | CHECK(resolution_stub_array != NULL); |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 680 | CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array); |
| 681 | resolution_stub_array_[type] = resolution_stub_array; |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 682 | } |
| 683 | |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 684 | Method* Runtime::CreateCalleeSaveMethod(InstructionSet insns, CalleeSaveType type) { |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 685 | Class* method_class = Method::GetMethodClass(); |
| 686 | Method* method = down_cast<Method*>(method_class->AllocObject()); |
| 687 | method->SetDeclaringClass(method_class); |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 688 | const char* name; |
| 689 | if (type == kSaveAll) { |
| 690 | name = "$$$callee_save_method$$$"; |
| 691 | } else if (type == kRefsOnly) { |
| 692 | name = "$$$refs_only_callee_save_method$$$"; |
| 693 | } else { |
| 694 | DCHECK(type == kRefsAndArgs); |
| 695 | name = "$$$refs_and_args_callee_save_method$$$"; |
| 696 | } |
| 697 | method->SetName(intern_table_->InternStrong(name)); |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 698 | method->SetSignature(intern_table_->InternStrong("()V")); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 699 | method->SetCode(NULL); |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 700 | if ((insns == kThumb2) || (insns == kArm)) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 701 | uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6) | (1 << art::arm::R7) | |
| 702 | (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11); |
| 703 | uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3); |
| 704 | uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9); |
| 705 | uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills :0) | |
| 706 | (type == kSaveAll ? all_spills :0) | (1 << art::arm::LR); |
| 707 | uint32_t fp_all_spills = (1 << art::arm::S0) | (1 << art::arm::S1) | (1 << art::arm::S2) | |
| 708 | (1 << art::arm::S3) | (1 << art::arm::S4) | (1 << art::arm::S5) | |
| 709 | (1 << art::arm::S6) | (1 << art::arm::S7) | (1 << art::arm::S8) | |
| 710 | (1 << art::arm::S9) | (1 << art::arm::S10) | (1 << art::arm::S11) | |
| 711 | (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) | |
| 712 | (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) | |
| 713 | (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) | |
| 714 | (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) | |
| 715 | (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) | |
| 716 | (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) | |
| 717 | (1 << art::arm::S30) | (1 << art::arm::S31); |
| 718 | uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0; |
| 719 | size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ + |
| 720 | __builtin_popcount(fp_spills) /* fprs */ + |
| 721 | 1 /* Method* */) * kPointerSize, kStackAlignment); |
Ian Rogers | 15fdb8c | 2011-09-25 15:45:07 -0700 | [diff] [blame] | 722 | method->SetFrameSizeInBytes(frame_size); |
| 723 | method->SetReturnPcOffsetInBytes(frame_size - kPointerSize); |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 724 | method->SetCoreSpillMask(core_spills); |
| 725 | method->SetFpSpillMask(fp_spills); |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 726 | } else if (insns == kX86) { |
| 727 | method->SetFrameSizeInBytes(32); |
| 728 | method->SetReturnPcOffsetInBytes(28); |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 729 | method->SetCoreSpillMask((1 << art::x86::EBX) | (1 << art::x86::EBP) | (1 << art::x86::ESI) | |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 730 | (1 << art::x86::EDI)); |
| 731 | method->SetFpSpillMask(0); |
| 732 | } else { |
| 733 | UNIMPLEMENTED(FATAL); |
| 734 | } |
| 735 | return method; |
| 736 | } |
| 737 | |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 738 | bool Runtime::HasCalleeSaveMethod(CalleeSaveType type) const { |
| 739 | return callee_save_method_[type] != NULL; |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 740 | } |
| 741 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 742 | // Returns a special method that describes all callee saves being spilled to the stack. |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 743 | Method* Runtime::GetCalleeSaveMethod(CalleeSaveType type) const { |
| 744 | CHECK(HasCalleeSaveMethod(type)); |
| 745 | return callee_save_method_[type]; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 746 | } |
| 747 | |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 748 | void Runtime::SetCalleeSaveMethod(Method* method, CalleeSaveType type) { |
| 749 | DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType)); |
| 750 | callee_save_method_[type] = method; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 751 | } |
| 752 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 753 | } // namespace art |