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