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 | |
Brian Carlstrom | dbf05b7 | 2011-12-15 00:55:24 -0800 | [diff] [blame] | 5 | #include <signal.h> |
| 6 | |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 7 | #include <cstdio> |
| 8 | #include <cstdlib> |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 9 | #include <limits> |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 10 | #include <vector> |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 11 | |
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" |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 14 | #include "debugger.h" |
Elliott Hughes | 4d6850c | 2012-01-18 15:55:06 -0800 | [diff] [blame] | 15 | #include "dex_verifier.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 16 | #include "heap.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 17 | #include "image.h" |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 18 | #include "intern_table.h" |
Elliott Hughes | c5f7c91 | 2011-08-18 14:00:42 -0700 | [diff] [blame] | 19 | #include "jni_internal.h" |
Elliott Hughes | 32d6e1e | 2011-10-11 14:47:44 -0700 | [diff] [blame] | 20 | #include "monitor.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 21 | #include "oat_file.h" |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 22 | #include "ScopedLocalRef.h" |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 23 | #include "signal_catcher.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 24 | #include "space.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 25 | #include "thread.h" |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 26 | #include "thread_list.h" |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 27 | #include "UniquePtr.h" |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 28 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 29 | // TODO: this drags in cutil/log.h, which conflicts with our logging.h. |
| 30 | #include "JniConstants.h" |
| 31 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 32 | namespace art { |
| 33 | |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 34 | Runtime* Runtime::instance_ = NULL; |
| 35 | |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 36 | Runtime::Runtime() |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 37 | : is_zygote_(false), |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 38 | default_stack_size_(Thread::kDefaultStackSize), |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 39 | monitor_list_(NULL), |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 40 | thread_list_(NULL), |
| 41 | intern_table_(NULL), |
| 42 | class_linker_(NULL), |
| 43 | signal_catcher_(NULL), |
| 44 | java_vm_(NULL), |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 45 | jni_stub_array_(NULL), |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 46 | abstract_method_error_stub_array_(NULL), |
Elliott Hughes | 6b35575 | 2012-01-13 16:49:08 -0800 | [diff] [blame] | 47 | shutting_down_(false), |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 48 | started_(false), |
| 49 | vfprintf_(NULL), |
| 50 | exit_(NULL), |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 51 | abort_(NULL), |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 52 | stats_enabled_(false), |
| 53 | tracer_(NULL) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 54 | for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) { |
| 55 | resolution_stub_array_[i] = NULL; |
| 56 | } |
| 57 | for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) { |
| 58 | callee_save_method_[i] = NULL; |
| 59 | } |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 62 | Runtime::~Runtime() { |
Elliott Hughes | 6b35575 | 2012-01-13 16:49:08 -0800 | [diff] [blame] | 63 | shutting_down_ = true; |
| 64 | |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 65 | Dbg::StopJdwp(); |
| 66 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 67 | // Make sure our internal threads are dead before we start tearing down things they're using. |
| 68 | delete signal_catcher_; |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 69 | // TODO: GC thread. |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 70 | |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 71 | // 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] | 72 | delete thread_list_; |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 73 | delete monitor_list_; |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 74 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 75 | delete class_linker_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 76 | Heap::Destroy(); |
Elliott Hughes | 4d6850c | 2012-01-18 15:55:06 -0800 | [diff] [blame] | 77 | verifier::DexVerifier::DeleteGcMaps(); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 78 | delete intern_table_; |
Elliott Hughes | c5f7c91 | 2011-08-18 14:00:42 -0700 | [diff] [blame] | 79 | delete java_vm_; |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 80 | Thread::Shutdown(); |
Carl Shapiro | 4acf464 | 2011-07-26 18:54:13 -0700 | [diff] [blame] | 81 | // TODO: acquire a static mutex on Runtime to avoid racing. |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 82 | CHECK(instance_ == NULL || instance_ == this); |
Carl Shapiro | 4acf464 | 2011-07-26 18:54:13 -0700 | [diff] [blame] | 83 | instance_ = NULL; |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 86 | static bool gAborting = false; |
| 87 | |
Elliott Hughes | e091855 | 2011-10-28 17:18:29 -0700 | [diff] [blame] | 88 | struct AbortState { |
| 89 | void Dump(std::ostream& os) { |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 90 | if (gAborting) { |
| 91 | os << "Runtime aborting --- recursively, so no thread-specific detail!\n"; |
| 92 | return; |
| 93 | } |
| 94 | gAborting = true; |
Elliott Hughes | e091855 | 2011-10-28 17:18:29 -0700 | [diff] [blame] | 95 | os << "Runtime aborting...\n"; |
| 96 | Thread* self = Thread::Current(); |
| 97 | if (self == NULL) { |
| 98 | os << "(Aborting thread was not attached to runtime!)\n"; |
| 99 | } else { |
| 100 | self->Dump(os, true); |
| 101 | } |
| 102 | } |
| 103 | }; |
| 104 | |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 105 | void Runtime::Abort(const char* file, int line) { |
| 106 | // Get any pending output out of the way. |
| 107 | fflush(NULL); |
| 108 | |
| 109 | // Many people have difficulty distinguish aborts from crashes, |
| 110 | // so be explicit. |
Elliott Hughes | e091855 | 2011-10-28 17:18:29 -0700 | [diff] [blame] | 111 | AbortState state; |
| 112 | LOG(ERROR) << Dumpable<AbortState>(state); |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 113 | |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 114 | // Perform any platform-specific pre-abort actions. |
| 115 | PlatformAbort(file, line); |
| 116 | |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 117 | // use abort hook if we have one |
| 118 | if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) { |
| 119 | Runtime::Current()->abort_(); |
| 120 | // notreached |
| 121 | } |
| 122 | |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 123 | // 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] | 124 | // receive SIGABRT. debuggerd dumps the stack trace of the main |
| 125 | // thread, whether or not that was the thread that failed. By |
| 126 | // stuffing a value into a bogus address, we cause a segmentation |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 127 | // fault in the current thread, and get a useful log from debuggerd. |
| 128 | // We can also trivially tell the difference between a VM crash and |
| 129 | // a deliberate abort by looking at the fault address. |
| 130 | *reinterpret_cast<char*>(0xdeadd00d) = 38; |
| 131 | abort(); |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 132 | // notreached |
| 133 | } |
| 134 | |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 135 | void Runtime::CallExitHook(jint status) { |
| 136 | if (exit_ != NULL) { |
| 137 | ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative); |
| 138 | exit_(status); |
| 139 | LOG(WARNING) << "Exit hook returned instead of exiting!"; |
| 140 | } |
| 141 | } |
| 142 | |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 143 | // Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify |
| 144 | // memory sizes. [kK] indicates kilobytes, [mM] megabytes, and |
| 145 | // [gG] gigabytes. |
| 146 | // |
| 147 | // "s" should point just past the "-Xm?" part of the string. |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 148 | // "div" specifies a divisor, e.g. 1024 if the value must be a multiple |
| 149 | // of 1024. |
| 150 | // |
| 151 | // The spec says the -Xmx and -Xms options must be multiples of 1024. It |
| 152 | // doesn't say anything about -Xss. |
| 153 | // |
| 154 | // Returns 0 (a useless size) if "s" is malformed or specifies a low or |
| 155 | // non-evenly-divisible value. |
| 156 | // |
Elliott Hughes | c1f143d | 2011-12-01 17:31:10 -0800 | [diff] [blame] | 157 | size_t ParseMemoryOption(const char* s, size_t div) { |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 158 | // strtoul accepts a leading [+-], which we don't want, |
| 159 | // so make sure our string starts with a decimal digit. |
| 160 | if (isdigit(*s)) { |
Elliott Hughes | c1f143d | 2011-12-01 17:31:10 -0800 | [diff] [blame] | 161 | const char* s2; |
| 162 | size_t val = strtoul(s, (char**)&s2, 10); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 163 | if (s2 != s) { |
| 164 | // s2 should be pointing just after the number. |
| 165 | // If this is the end of the string, the user |
| 166 | // has specified a number of bytes. Otherwise, |
| 167 | // there should be exactly one more character |
| 168 | // that specifies a multiplier. |
| 169 | if (*s2 != '\0') { |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 170 | // The remainder of the string is either a single multiplier |
| 171 | // character, or nothing to indicate that the value is in |
| 172 | // bytes. |
| 173 | char c = *s2++; |
| 174 | if (*s2 == '\0') { |
| 175 | size_t mul; |
| 176 | if (c == '\0') { |
| 177 | mul = 1; |
| 178 | } else if (c == 'k' || c == 'K') { |
| 179 | mul = KB; |
| 180 | } else if (c == 'm' || c == 'M') { |
| 181 | mul = MB; |
| 182 | } else if (c == 'g' || c == 'G') { |
| 183 | mul = GB; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 184 | } else { |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 185 | // Unknown multiplier character. |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 186 | return 0; |
| 187 | } |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 188 | |
| 189 | if (val <= std::numeric_limits<size_t>::max() / mul) { |
| 190 | val *= mul; |
| 191 | } else { |
| 192 | // Clamp to a multiple of 1024. |
| 193 | val = std::numeric_limits<size_t>::max() & ~(1024-1); |
| 194 | } |
| 195 | } else { |
| 196 | // There's more than one character after the numeric part. |
| 197 | return 0; |
| 198 | } |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 199 | } |
| 200 | // The man page says that a -Xm value must be a multiple of 1024. |
| 201 | if (val % div == 0) { |
| 202 | return val; |
| 203 | } |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 204 | } |
| 205 | } |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 206 | return 0; |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Elliott Hughes | bb1e8f0 | 2011-10-18 14:14:25 -0700 | [diff] [blame] | 209 | size_t ParseIntegerOrDie(const StringPiece& s) { |
| 210 | StringPiece::size_type colon = s.find(':'); |
| 211 | if (colon == StringPiece::npos) { |
| 212 | LOG(FATAL) << "Missing integer: " << s; |
| 213 | } |
| 214 | const char* begin = &s.data()[colon + 1]; |
| 215 | char* end; |
| 216 | size_t result = strtoul(begin, &end, 10); |
| 217 | if (begin == end || *end != '\0') { |
| 218 | LOG(FATAL) << "Failed to parse integer in: " << s; |
| 219 | } |
| 220 | return result; |
| 221 | } |
| 222 | |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 223 | void LoadJniLibrary(JavaVMExt* vm, const char* name) { |
| 224 | // TODO: OS_SHARED_LIB_FORMAT_STR |
| 225 | std::string mapped_name(StringPrintf("lib%s.so", name)); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 226 | std::string reason; |
| 227 | if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) { |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 228 | LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": " |
| 229 | << reason; |
| 230 | } |
| 231 | } |
| 232 | |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 233 | Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) { |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 234 | UniquePtr<ParsedOptions> parsed(new ParsedOptions()); |
Brian Carlstrom | 5de8fe5 | 2011-10-16 14:10:09 -0700 | [diff] [blame] | 235 | bool compiler = false; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 236 | const char* boot_class_path = getenv("BOOTCLASSPATH"); |
| 237 | if (boot_class_path != NULL) { |
Ian Rogers | 4f6b57a | 2012-01-17 16:23:27 -0800 | [diff] [blame] | 238 | parsed->boot_class_path_ = boot_class_path; |
| 239 | } else { |
| 240 | parsed->boot_class_path_ = "."; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 241 | } |
| 242 | const char* class_path = getenv("CLASSPATH"); |
| 243 | if (class_path != NULL) { |
Ian Rogers | 4f6b57a | 2012-01-17 16:23:27 -0800 | [diff] [blame] | 244 | parsed->class_path_ = class_path; |
| 245 | } else { |
| 246 | parsed->class_path_ = "."; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 247 | } |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 248 | #ifdef NDEBUG |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 249 | // -Xcheck:jni is off by default for regular builds... |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 250 | parsed->check_jni_ = false; |
| 251 | #else |
| 252 | // ...but on by default in debug builds. |
| 253 | parsed->check_jni_ = true; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 254 | #endif |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 255 | |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 256 | parsed->heap_initial_size_ = Heap::kInitialSize; |
| 257 | parsed->heap_maximum_size_ = Heap::kMaximumSize; |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 258 | parsed->heap_growth_limit_ = 0; // 0 means no growth limit |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 259 | parsed->stack_size_ = Thread::kDefaultStackSize; |
| 260 | |
Elliott Hughes | 9ca7a1f | 2011-10-11 14:29:52 -0700 | [diff] [blame] | 261 | parsed->is_zygote_ = false; |
| 262 | |
Elliott Hughes | bb1e8f0 | 2011-10-18 14:14:25 -0700 | [diff] [blame] | 263 | parsed->jni_globals_max_ = 0; |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 264 | parsed->lock_profiling_threshold_ = 0; |
| 265 | parsed->hook_is_sensitive_thread_ = NULL; |
| 266 | |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 267 | parsed->hook_vfprintf_ = vfprintf; |
| 268 | parsed->hook_exit_ = exit; |
| 269 | parsed->hook_abort_ = abort; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 270 | |
| 271 | for (size_t i = 0; i < options.size(); ++i) { |
| 272 | const StringPiece& option = options[i].first; |
Brian Carlstrom | 0796af0 | 2011-10-12 14:31:45 -0700 | [diff] [blame] | 273 | if (true && options[0].first == "-Xzygote") { |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 274 | LOG(INFO) << "option[" << i << "]=" << option; |
| 275 | } |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 276 | if (option.starts_with("-Xbootclasspath:")) { |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 277 | parsed->boot_class_path_ = option.substr(strlen("-Xbootclasspath:")).data(); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 278 | } else if (option == "-classpath" || option == "-cp") { |
| 279 | // TODO: support -Djava.class.path |
| 280 | i++; |
| 281 | if (i == options.size()) { |
| 282 | // TODO: usage |
| 283 | LOG(FATAL) << "Missing required class path value for " << option; |
| 284 | return NULL; |
| 285 | } |
| 286 | const StringPiece& value = options[i].first; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 287 | parsed->class_path_ = value.data(); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 288 | } else if (option.starts_with("-Ximage:")) { |
| 289 | parsed->images_.push_back(option.substr(strlen("-Ximage:")).data()); |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 290 | } else if (option.starts_with("-Xcheck:jni")) { |
| 291 | parsed->check_jni_ = true; |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 292 | } else if (option.starts_with("-Xrunjdwp:") || option.starts_with("-agentlib:jdwp=")) { |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 293 | std::string tail(option.substr(option[1] == 'X' ? 10 : 15).ToString()); |
Elliott Hughes | 3bb8156 | 2011-10-21 18:52:59 -0700 | [diff] [blame] | 294 | if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) { |
| 295 | LOG(FATAL) << "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n" |
| 296 | << "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n"; |
| 297 | return NULL; |
| 298 | } |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 299 | } else if (option.starts_with("-Xms")) { |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 300 | size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024); |
| 301 | if (size == 0) { |
| 302 | if (ignore_unrecognized) { |
| 303 | continue; |
| 304 | } |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 305 | // TODO: usage |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 306 | LOG(FATAL) << "Failed to parse " << option; |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 307 | return NULL; |
| 308 | } |
| 309 | parsed->heap_initial_size_ = size; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 310 | } else if (option.starts_with("-Xmx")) { |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 311 | size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024); |
| 312 | if (size == 0) { |
| 313 | if (ignore_unrecognized) { |
| 314 | continue; |
| 315 | } |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 316 | // TODO: usage |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 317 | LOG(FATAL) << "Failed to parse " << option; |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 318 | return NULL; |
| 319 | } |
| 320 | parsed->heap_maximum_size_ = size; |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 321 | } else if (option.starts_with("-XX:HeapGrowthLimit=")) { |
| 322 | size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).data(), 1024); |
| 323 | if (size == 0) { |
| 324 | if (ignore_unrecognized) { |
| 325 | continue; |
| 326 | } |
| 327 | // TODO: usage |
| 328 | LOG(FATAL) << "Failed to parse " << option; |
| 329 | return NULL; |
| 330 | } |
| 331 | parsed->heap_growth_limit_ = size; |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 332 | } else if (option.starts_with("-Xss")) { |
| 333 | size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1); |
| 334 | if (size == 0) { |
| 335 | if (ignore_unrecognized) { |
| 336 | continue; |
| 337 | } |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 338 | // TODO: usage |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 339 | LOG(FATAL) << "Failed to parse " << option; |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 340 | return NULL; |
| 341 | } |
| 342 | parsed->stack_size_ = size; |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 343 | } else if (option.starts_with("-D")) { |
| 344 | parsed->properties_.push_back(option.substr(strlen("-D")).data()); |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 345 | } else if (option.starts_with("-Xjnitrace:")) { |
| 346 | parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:")).data(); |
Brian Carlstrom | 5de8fe5 | 2011-10-16 14:10:09 -0700 | [diff] [blame] | 347 | } else if (option == "compiler") { |
| 348 | compiler = true; |
Elliott Hughes | 9ca7a1f | 2011-10-11 14:29:52 -0700 | [diff] [blame] | 349 | } else if (option == "-Xzygote") { |
| 350 | parsed->is_zygote_ = true; |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 351 | } else if (option.starts_with("-verbose:")) { |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 352 | std::vector<std::string> verbose_options; |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 353 | Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options); |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 354 | for (size_t i = 0; i < verbose_options.size(); ++i) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 355 | if (verbose_options[i] == "class") { |
| 356 | gLogVerbosity.class_linker = true; |
| 357 | } else if (verbose_options[i] == "compiler") { |
| 358 | gLogVerbosity.compiler = true; |
| 359 | } else if (verbose_options[i] == "heap") { |
| 360 | gLogVerbosity.heap = true; |
| 361 | } else if (verbose_options[i] == "gc") { |
| 362 | gLogVerbosity.gc = true; |
| 363 | } else if (verbose_options[i] == "jdwp") { |
| 364 | gLogVerbosity.jdwp = true; |
| 365 | } else if (verbose_options[i] == "jni") { |
| 366 | gLogVerbosity.jni = true; |
| 367 | } else if (verbose_options[i] == "monitor") { |
| 368 | gLogVerbosity.monitor = true; |
| 369 | } else if (verbose_options[i] == "startup") { |
| 370 | gLogVerbosity.startup = true; |
| 371 | } else if (verbose_options[i] == "third-party-jni") { |
| 372 | gLogVerbosity.third_party_jni = true; |
| 373 | } else if (verbose_options[i] == "threads") { |
| 374 | gLogVerbosity.threads = true; |
| 375 | } else { |
| 376 | LOG(WARNING) << "Ignoring unknown -verbose option: " << verbose_options[i]; |
| 377 | } |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 378 | } |
Elliott Hughes | bb1e8f0 | 2011-10-18 14:14:25 -0700 | [diff] [blame] | 379 | } else if (option.starts_with("-Xjnigreflimit:")) { |
| 380 | parsed->jni_globals_max_ = ParseIntegerOrDie(option); |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 381 | } else if (option.starts_with("-Xlockprofthreshold:")) { |
Elliott Hughes | bb1e8f0 | 2011-10-18 14:14:25 -0700 | [diff] [blame] | 382 | parsed->lock_profiling_threshold_ = ParseIntegerOrDie(option); |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 383 | } else if (option.starts_with("-Xstacktracefile:")) { |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 384 | // always show stack traces in debug builds |
| 385 | #ifdef NDEBUG |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 386 | parsed->stack_trace_file_ = option.substr(strlen("-Xstacktracefile:")).data(); |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 387 | #endif |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 388 | } else if (option == "sensitiveThread") { |
| 389 | parsed->hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(options[i].second); |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 390 | } else if (option == "vfprintf") { |
| 391 | parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second); |
| 392 | } else if (option == "exit") { |
| 393 | parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second); |
| 394 | } else if (option == "abort") { |
| 395 | parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 396 | } else if (option == "host-prefix") { |
| 397 | parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second); |
Elliott Hughes | b3e66df | 2012-01-12 14:49:18 -0800 | [diff] [blame] | 398 | } else if (option == "-Xgenregmap" || option == "-Xgc:precise") { |
| 399 | // We silently ignore these for backwards compatibility. |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 400 | } else { |
| 401 | if (!ignore_unrecognized) { |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 402 | // TODO: print usage via vfprintf |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 403 | LOG(ERROR) << "Unrecognized option " << option; |
| 404 | // TODO: this should exit, but for now tolerate unknown options |
| 405 | //return NULL; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | } |
| 409 | |
Brian Carlstrom | 5de8fe5 | 2011-10-16 14:10:09 -0700 | [diff] [blame] | 410 | if (!compiler && parsed->images_.empty()) { |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 411 | parsed->images_.push_back("/system/framework/boot.art"); |
Brian Carlstrom | 5de8fe5 | 2011-10-16 14:10:09 -0700 | [diff] [blame] | 412 | } |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 413 | if (parsed->heap_growth_limit_ == 0) { |
| 414 | parsed->heap_growth_limit_ = parsed->heap_maximum_size_; |
| 415 | } |
Brian Carlstrom | 5de8fe5 | 2011-10-16 14:10:09 -0700 | [diff] [blame] | 416 | |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 417 | LOG(INFO) << "Build type: " |
| 418 | #ifndef NDEBUG |
| 419 | << "debug" |
| 420 | #else |
| 421 | << "optimized" |
| 422 | #endif |
| 423 | << "; CheckJNI: " << (parsed->check_jni_ ? "on" : "off"); |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 424 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 425 | return parsed.release(); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 429 | // TODO: acquire a static mutex on Runtime to avoid racing. |
| 430 | if (Runtime::instance_ != NULL) { |
| 431 | return NULL; |
| 432 | } |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 433 | instance_ = new Runtime; |
| 434 | if (!instance_->Init(options, ignore_unrecognized)) { |
| 435 | delete instance_; |
| 436 | instance_ = NULL; |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 437 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 438 | return instance_; |
| 439 | } |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 440 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 441 | void CreateSystemClassLoader() { |
| 442 | if (ClassLoader::UseCompileTimeClassPath()) { |
| 443 | return; |
| 444 | } |
| 445 | |
| 446 | Thread* self = Thread::Current(); |
| 447 | |
| 448 | // Must be in the kNative state for calling native methods. |
| 449 | CHECK_EQ(self->GetState(), Thread::kNative); |
| 450 | |
| 451 | JNIEnv* env = self->GetJniEnv(); |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 452 | ScopedLocalRef<jclass> ClassLoader_class(env, env->FindClass("java/lang/ClassLoader")); |
| 453 | CHECK(ClassLoader_class.get() != NULL); |
| 454 | jmethodID getSystemClassLoader = env->GetStaticMethodID(ClassLoader_class.get(), |
| 455 | "getSystemClassLoader", |
| 456 | "()Ljava/lang/ClassLoader;"); |
| 457 | CHECK(getSystemClassLoader != NULL); |
| 458 | ScopedLocalRef<jobject> class_loader(env, env->CallStaticObjectMethod(ClassLoader_class.get(), |
| 459 | getSystemClassLoader)); |
| 460 | CHECK(class_loader.get() != NULL); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 461 | |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 462 | Thread::Current()->SetClassLoaderOverride(Decode<ClassLoader*>(env, class_loader.get())); |
Jesse Wilson | 1b2b2f2 | 2011-11-22 11:47:44 -0500 | [diff] [blame] | 463 | |
| 464 | ScopedLocalRef<jclass> Thread_class(env, env->FindClass("java/lang/Thread")); |
| 465 | CHECK(Thread_class.get() != NULL); |
| 466 | jfieldID contextClassLoader = env->GetFieldID(Thread_class.get(), |
| 467 | "contextClassLoader", |
| 468 | "Ljava/lang/ClassLoader;"); |
| 469 | CHECK(contextClassLoader != NULL); |
| 470 | ScopedLocalRef<jobject> self_jobject(env, AddLocalReference<jobject>(env, self->GetPeer())); |
| 471 | env->SetObjectField(self_jobject.get(), contextClassLoader, class_loader.get()); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 474 | void Runtime::Start() { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 475 | VLOG(startup) << "Runtime::Start entering"; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 476 | |
| 477 | CHECK(host_prefix_.empty()) << host_prefix_; |
| 478 | |
Brian Carlstrom | f28bc5b | 2011-10-26 01:15:03 -0700 | [diff] [blame] | 479 | // Restore main thread state to kNative as expected by native code |
| 480 | Thread::Current()->SetState(Thread::kNative); |
| 481 | |
Brian Carlstrom | 5d40f18 | 2011-09-26 22:29:18 -0700 | [diff] [blame] | 482 | started_ = true; |
| 483 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 484 | // InitNativeMethods needs to be after started_ so that the classes |
| 485 | // it touches will have methods linked to the oat file if necessary. |
| 486 | InitNativeMethods(); |
| 487 | |
Jesse Wilson | 9a6bae8 | 2011-11-14 14:57:30 -0500 | [diff] [blame] | 488 | Thread::FinishStartup(); |
| 489 | |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 490 | if (!is_zygote_) { |
| 491 | DidForkFromZygote(); |
| 492 | } |
Elliott Hughes | 9ca7a1f | 2011-10-11 14:29:52 -0700 | [diff] [blame] | 493 | |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 494 | StartDaemonThreads(); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 495 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 496 | CreateSystemClassLoader(); |
| 497 | |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 498 | Thread::Current()->GetJniEnv()->locals.AssertEmpty(); |
| 499 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 500 | VLOG(startup) << "Runtime::Start exiting"; |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 501 | } |
| 502 | |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 503 | void Runtime::DidForkFromZygote() { |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 504 | is_zygote_ = false; |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 505 | |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 506 | StartSignalCatcher(); |
Elliott Hughes | d1cc836 | 2011-10-24 16:58:50 -0700 | [diff] [blame] | 507 | |
| 508 | // Start the JDWP thread. If the command-line debugger flags specified "suspend=y", |
| 509 | // this will pause the runtime, so we probably want this to come last. |
| 510 | Dbg::StartJdwp(); |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | void Runtime::StartSignalCatcher() { |
| 514 | if (!is_zygote_) { |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 515 | signal_catcher_ = new SignalCatcher(stack_trace_file_); |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 519 | void Runtime::StartDaemonThreads() { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 520 | VLOG(startup) << "Runtime::StartDaemonThreads entering"; |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 521 | |
Elliott Hughes | 719b323 | 2011-09-25 17:42:19 -0700 | [diff] [blame] | 522 | Thread* self = Thread::Current(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 523 | |
| 524 | // Must be in the kNative state for calling native methods. |
| 525 | CHECK_EQ(self->GetState(), Thread::kNative); |
| 526 | |
Elliott Hughes | 719b323 | 2011-09-25 17:42:19 -0700 | [diff] [blame] | 527 | JNIEnv* env = self->GetJniEnv(); |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 528 | ScopedLocalRef<jclass> c(env, env->FindClass("java/lang/Daemons")); |
| 529 | CHECK(c.get() != NULL); |
| 530 | jmethodID mid = env->GetStaticMethodID(c.get(), "start", "()V"); |
Elliott Hughes | 719b323 | 2011-09-25 17:42:19 -0700 | [diff] [blame] | 531 | CHECK(mid != NULL); |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 532 | env->CallStaticVoidMethod(c.get(), mid); |
Elliott Hughes | 9ca7a1f | 2011-10-11 14:29:52 -0700 | [diff] [blame] | 533 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 534 | VLOG(startup) << "Runtime::StartDaemonThreads exiting"; |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 535 | } |
| 536 | |
Elliott Hughes | 6b35575 | 2012-01-13 16:49:08 -0800 | [diff] [blame] | 537 | bool Runtime::IsShuttingDown() const { |
| 538 | return shutting_down_; |
| 539 | } |
| 540 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 541 | bool Runtime::IsStarted() const { |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 542 | return started_; |
| 543 | } |
| 544 | |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 545 | bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) { |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 546 | CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize); |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 547 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 548 | UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized)); |
| 549 | if (options.get() == NULL) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 550 | LOG(ERROR) << "Failed to parse options"; |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 551 | return false; |
| 552 | } |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 553 | VLOG(startup) << "Runtime::Init -verbose:startup enabled"; |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 554 | |
Elliott Hughes | bb1e8f0 | 2011-10-18 14:14:25 -0700 | [diff] [blame] | 555 | SetJniGlobalsMax(options->jni_globals_max_); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 556 | Monitor::Init(options->lock_profiling_threshold_, options->hook_is_sensitive_thread_); |
Elliott Hughes | 32d6e1e | 2011-10-11 14:47:44 -0700 | [diff] [blame] | 557 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 558 | host_prefix_ = options->host_prefix_; |
| 559 | boot_class_path_ = options->boot_class_path_; |
| 560 | class_path_ = options->class_path_; |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 561 | properties_ = options->properties_; |
| 562 | |
Elliott Hughes | 9ca7a1f | 2011-10-11 14:29:52 -0700 | [diff] [blame] | 563 | is_zygote_ = options->is_zygote_; |
| 564 | |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 565 | vfprintf_ = options->hook_vfprintf_; |
| 566 | exit_ = options->hook_exit_; |
| 567 | abort_ = options->hook_abort_; |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 568 | |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 569 | default_stack_size_ = options->stack_size_; |
Elliott Hughes | 94ce37a | 2011-10-18 15:07:48 -0700 | [diff] [blame] | 570 | stack_trace_file_ = options->stack_trace_file_; |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 571 | |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 572 | monitor_list_ = new MonitorList; |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 573 | thread_list_ = new ThreadList; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 574 | intern_table_ = new InternTable; |
| 575 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 576 | Heap::Init(options->heap_initial_size_, |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 577 | options->heap_maximum_size_, |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 578 | options->heap_growth_limit_, |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 579 | options->images_); |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 580 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 581 | BlockSignals(); |
| 582 | |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 583 | java_vm_ = new JavaVMExt(this, options.get()); |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 584 | |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 585 | Thread::Startup(); |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 586 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 587 | // ClassLinker needs an attached thread, but we can't fully attach a thread |
| 588 | // without creating objects. |
| 589 | Thread::Attach(this, "main", false); |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 590 | |
Brian Carlstrom | f28bc5b | 2011-10-26 01:15:03 -0700 | [diff] [blame] | 591 | // Set us to runnable so tools using a runtime can allocate and GC by default |
| 592 | Thread::Current()->SetState(Thread::kRunnable); |
| 593 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 594 | CHECK_GE(Heap::GetSpaces().size(), 1U); |
| 595 | class_linker_ = ((Heap::GetSpaces()[0]->IsImageSpace()) |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 596 | ? ClassLinker::Create(intern_table_) |
| 597 | : ClassLinker::Create(options->boot_class_path_, intern_table_)); |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 598 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 599 | VLOG(startup) << "Runtime::Init exiting"; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 600 | return true; |
| 601 | } |
| 602 | |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 603 | void Runtime::InitNativeMethods() { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 604 | VLOG(startup) << "Runtime::InitNativeMethods entering"; |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 605 | Thread* self = Thread::Current(); |
| 606 | JNIEnv* env = self->GetJniEnv(); |
| 607 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 608 | // 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] | 609 | CHECK_EQ(self->GetState(), Thread::kNative); |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 610 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 611 | // First set up JniConstants, which is used by both the runtime's built-in native |
| 612 | // methods and libcore. |
Elliott Hughes | fea966e | 2011-09-22 10:26:20 -0700 | [diff] [blame] | 613 | JniConstants::init(env); |
| 614 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 615 | // Then set up the native methods provided by the runtime itself. |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 616 | RegisterRuntimeNativeMethods(env); |
| 617 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 618 | // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad. |
| 619 | // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's |
| 620 | // the library that implements System.loadLibrary! |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 621 | LoadJniLibrary(instance_->GetJavaVM(), "javacore"); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 622 | VLOG(startup) << "Runtime::InitNativeMethods exiting"; |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) { |
| 626 | #define REGISTER(FN) extern void FN(JNIEnv*); FN(env) |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 627 | REGISTER(register_dalvik_system_DexFile); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 628 | REGISTER(register_dalvik_system_VMDebug); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 629 | REGISTER(register_dalvik_system_VMRuntime); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 630 | REGISTER(register_dalvik_system_VMStack); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 631 | REGISTER(register_dalvik_system_Zygote); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 632 | REGISTER(register_java_lang_Class); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 633 | REGISTER(register_java_lang_Object); |
| 634 | REGISTER(register_java_lang_Runtime); |
| 635 | REGISTER(register_java_lang_String); |
| 636 | REGISTER(register_java_lang_System); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 637 | REGISTER(register_java_lang_Thread); |
Elliott Hughes | 1240dad | 2011-09-09 16:24:50 -0700 | [diff] [blame] | 638 | REGISTER(register_java_lang_Throwable); |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 639 | REGISTER(register_java_lang_VMClassLoader); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 640 | REGISTER(register_java_lang_reflect_Array); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 641 | REGISTER(register_java_lang_reflect_Constructor); |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 642 | REGISTER(register_java_lang_reflect_Field); |
| 643 | REGISTER(register_java_lang_reflect_Method); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 644 | REGISTER(register_java_lang_reflect_Proxy); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 645 | REGISTER(register_java_util_concurrent_atomic_AtomicLong); |
Brian Carlstrom | 395520e | 2011-09-25 19:35:00 -0700 | [diff] [blame] | 646 | REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer); |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 647 | REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 648 | REGISTER(register_sun_misc_Unsafe); |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 649 | #undef REGISTER |
| 650 | } |
| 651 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 652 | void Runtime::Dump(std::ostream& os) { |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 653 | // TODO: dump other runtime statistics? |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 654 | GetClassLinker()->DumpForSigQuit(os); |
| 655 | GetInternTable()->DumpForSigQuit(os); |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 656 | os << "\n"; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 657 | |
| 658 | thread_list_->Dump(os); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 659 | } |
| 660 | |
Elliott Hughes | 21a5bf2 | 2011-12-07 14:35:20 -0800 | [diff] [blame] | 661 | void Runtime::DumpLockHolders(std::ostream& os) { |
| 662 | pid_t heap_lock_owner = Heap::GetLockOwner(); |
| 663 | pid_t thread_list_lock_owner = GetThreadList()->GetLockOwner(); |
| 664 | pid_t classes_lock_owner = GetClassLinker()->GetClassesLockOwner(); |
| 665 | pid_t dex_lock_owner = GetClassLinker()->GetDexLockOwner(); |
| 666 | if ((heap_lock_owner | thread_list_lock_owner | classes_lock_owner | dex_lock_owner) != 0) { |
| 667 | os << "Heap lock owner tid: " << heap_lock_owner << "\n" |
| 668 | << "ThreadList lock owner tid: " << thread_list_lock_owner << "\n" |
| 669 | << "ClassLinker classes lock owner tid: " << classes_lock_owner << "\n" |
| 670 | << "ClassLinker dex lock owner tid: " << dex_lock_owner << "\n"; |
| 671 | } |
| 672 | } |
| 673 | |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 674 | void Runtime::SetStatsEnabled(bool new_state) { |
| 675 | if (new_state == true) { |
| 676 | GetStats()->Clear(~0); |
| 677 | // TODO: wouldn't it make more sense to clear _all_ threads' stats? |
| 678 | Thread::Current()->GetStats()->Clear(~0); |
| 679 | } |
| 680 | stats_enabled_ = new_state; |
| 681 | } |
| 682 | |
| 683 | void Runtime::ResetStats(int kinds) { |
| 684 | GetStats()->Clear(kinds & 0xffff); |
| 685 | // TODO: wouldn't it make more sense to clear _all_ threads' stats? |
| 686 | Thread::Current()->GetStats()->Clear(kinds >> 16); |
| 687 | } |
| 688 | |
| 689 | RuntimeStats* Runtime::GetStats() { |
| 690 | return &stats_; |
| 691 | } |
| 692 | |
| 693 | int32_t Runtime::GetStat(int kind) { |
| 694 | RuntimeStats* stats; |
| 695 | if (kind < (1<<16)) { |
| 696 | stats = GetStats(); |
| 697 | } else { |
| 698 | stats = Thread::Current()->GetStats(); |
| 699 | kind >>= 16; |
| 700 | } |
| 701 | switch (kind) { |
| 702 | case KIND_ALLOCATED_OBJECTS: |
| 703 | return stats->allocated_objects; |
| 704 | case KIND_ALLOCATED_BYTES: |
| 705 | return stats->allocated_bytes; |
| 706 | case KIND_FREED_OBJECTS: |
| 707 | return stats->freed_objects; |
| 708 | case KIND_FREED_BYTES: |
| 709 | return stats->freed_bytes; |
| 710 | case KIND_GC_INVOCATIONS: |
| 711 | return stats->gc_for_alloc_count; |
| 712 | case KIND_CLASS_INIT_COUNT: |
| 713 | return stats->class_init_count; |
| 714 | case KIND_CLASS_INIT_TIME: |
| 715 | // Convert ns to us, reduce to 32 bits. |
| 716 | return (int) (stats->class_init_time_ns / 1000); |
| 717 | case KIND_EXT_ALLOCATED_OBJECTS: |
| 718 | case KIND_EXT_ALLOCATED_BYTES: |
| 719 | case KIND_EXT_FREED_OBJECTS: |
| 720 | case KIND_EXT_FREED_BYTES: |
| 721 | return 0; // backward compatibility |
| 722 | default: |
| 723 | CHECK(false); |
| 724 | return -1; // unreachable |
| 725 | } |
| 726 | } |
| 727 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 728 | void Runtime::BlockSignals() { |
| 729 | sigset_t sigset; |
| 730 | if (sigemptyset(&sigset) == -1) { |
| 731 | PLOG(FATAL) << "sigemptyset failed"; |
| 732 | } |
| 733 | if (sigaddset(&sigset, SIGPIPE) == -1) { |
| 734 | PLOG(ERROR) << "sigaddset SIGPIPE failed"; |
| 735 | } |
| 736 | // SIGQUIT is used to dump the runtime's state (including stack traces). |
| 737 | if (sigaddset(&sigset, SIGQUIT) == -1) { |
| 738 | PLOG(ERROR) << "sigaddset SIGQUIT failed"; |
| 739 | } |
| 740 | // SIGUSR1 is used to initiate a heap dump. |
| 741 | if (sigaddset(&sigset, SIGUSR1) == -1) { |
| 742 | PLOG(ERROR) << "sigaddset SIGUSR1 failed"; |
| 743 | } |
| 744 | CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0); |
| 745 | } |
| 746 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 747 | void Runtime::AttachCurrentThread(const char* name, bool as_daemon) { |
| 748 | Thread::Attach(instance_, name, as_daemon); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 749 | } |
| 750 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 751 | void Runtime::DetachCurrentThread() { |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 752 | // TODO: check we're not calling DetachCurrentThread from a call stack that |
| 753 | // 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] | 754 | thread_list_->Unregister(); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 755 | } |
| 756 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 757 | void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const { |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 758 | Dbg::VisitRoots(visitor, arg); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 759 | class_linker_->VisitRoots(visitor, arg); |
| 760 | intern_table_->VisitRoots(visitor, arg); |
| 761 | java_vm_->VisitRoots(visitor, arg); |
| 762 | thread_list_->VisitRoots(visitor, arg); |
| 763 | visitor(jni_stub_array_, arg); |
| 764 | visitor(abstract_method_error_stub_array_, arg); |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 765 | for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) { |
| 766 | visitor(resolution_stub_array_[i], arg); |
| 767 | } |
| 768 | for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) { |
| 769 | visitor(callee_save_method_[i], arg); |
| 770 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 771 | } |
| 772 | |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 773 | bool Runtime::HasJniDlsymLookupStub() const { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 774 | return jni_stub_array_ != NULL; |
| 775 | } |
| 776 | |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 777 | ByteArray* Runtime::GetJniDlsymLookupStub() const { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 778 | CHECK(jni_stub_array_ != NULL); |
| 779 | return jni_stub_array_; |
| 780 | } |
| 781 | |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 782 | void Runtime::SetJniDlsymLookupStub(ByteArray* jni_stub_array) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 783 | CHECK(jni_stub_array != NULL) << " jni_stub_array=" << jni_stub_array; |
| 784 | CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array) |
| 785 | << "jni_stub_array_=" << jni_stub_array_ << " jni_stub_array=" << jni_stub_array; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 786 | jni_stub_array_ = jni_stub_array; |
| 787 | } |
| 788 | |
| 789 | bool Runtime::HasAbstractMethodErrorStubArray() const { |
| 790 | return abstract_method_error_stub_array_ != NULL; |
| 791 | } |
| 792 | |
| 793 | ByteArray* Runtime::GetAbstractMethodErrorStubArray() const { |
| 794 | CHECK(abstract_method_error_stub_array_ != NULL); |
| 795 | return abstract_method_error_stub_array_; |
| 796 | } |
| 797 | |
| 798 | void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) { |
| 799 | CHECK(abstract_method_error_stub_array != NULL); |
| 800 | CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array); |
| 801 | abstract_method_error_stub_array_ = abstract_method_error_stub_array; |
| 802 | } |
| 803 | |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 804 | |
| 805 | Runtime::TrampolineType Runtime::GetTrampolineType(Method* method) { |
| 806 | if (method == NULL) { |
| 807 | return Runtime::kUnknownMethod; |
| 808 | } else if (method->IsStatic()) { |
| 809 | return Runtime::kStaticMethod; |
| 810 | } else { |
| 811 | return Runtime::kInstanceMethod; |
| 812 | } |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 813 | } |
| 814 | |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 815 | bool Runtime::HasResolutionStubArray(TrampolineType type) const { |
| 816 | return resolution_stub_array_[type] != NULL; |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 817 | } |
| 818 | |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 819 | ByteArray* Runtime::GetResolutionStubArray(TrampolineType type) const { |
| 820 | CHECK(HasResolutionStubArray(type)); |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 821 | DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastTrampolineMethodType)); |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 822 | return resolution_stub_array_[type]; |
| 823 | } |
| 824 | |
| 825 | void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) { |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 826 | CHECK(resolution_stub_array != NULL); |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 827 | CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array); |
| 828 | resolution_stub_array_[type] = resolution_stub_array; |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 829 | } |
| 830 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 831 | Method* Runtime::CreateCalleeSaveMethod(InstructionSet instruction_set, CalleeSaveType type) { |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 832 | Class* method_class = Method::GetMethodClass(); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 833 | SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject())); |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 834 | method->SetDeclaringClass(method_class); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 835 | // TODO: use a special method for callee saves |
| 836 | method->SetMethodIndex(DexFile::kDexNoIndex16); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 837 | method->SetCode(NULL); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 838 | if ((instruction_set == kThumb2) || (instruction_set == kArm)) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 839 | uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6) | (1 << art::arm::R7) | |
| 840 | (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11); |
| 841 | uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3); |
| 842 | uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9); |
| 843 | uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills :0) | |
| 844 | (type == kSaveAll ? all_spills :0) | (1 << art::arm::LR); |
| 845 | uint32_t fp_all_spills = (1 << art::arm::S0) | (1 << art::arm::S1) | (1 << art::arm::S2) | |
| 846 | (1 << art::arm::S3) | (1 << art::arm::S4) | (1 << art::arm::S5) | |
| 847 | (1 << art::arm::S6) | (1 << art::arm::S7) | (1 << art::arm::S8) | |
| 848 | (1 << art::arm::S9) | (1 << art::arm::S10) | (1 << art::arm::S11) | |
| 849 | (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) | |
| 850 | (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) | |
| 851 | (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) | |
| 852 | (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) | |
| 853 | (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) | |
| 854 | (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) | |
| 855 | (1 << art::arm::S30) | (1 << art::arm::S31); |
| 856 | uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0; |
| 857 | size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ + |
| 858 | __builtin_popcount(fp_spills) /* fprs */ + |
| 859 | 1 /* Method* */) * kPointerSize, kStackAlignment); |
Ian Rogers | 15fdb8c | 2011-09-25 15:45:07 -0700 | [diff] [blame] | 860 | method->SetFrameSizeInBytes(frame_size); |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 861 | method->SetCoreSpillMask(core_spills); |
| 862 | method->SetFpSpillMask(fp_spills); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 863 | } else if (instruction_set == kX86) { |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 864 | method->SetFrameSizeInBytes(32); |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 865 | 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] | 866 | (1 << art::x86::EDI)); |
| 867 | method->SetFpSpillMask(0); |
| 868 | } else { |
| 869 | UNIMPLEMENTED(FATAL); |
| 870 | } |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 871 | return method.get(); |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 872 | } |
| 873 | |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 874 | bool Runtime::HasCalleeSaveMethod(CalleeSaveType type) const { |
| 875 | return callee_save_method_[type] != NULL; |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 876 | } |
| 877 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 878 | // 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] | 879 | Method* Runtime::GetCalleeSaveMethod(CalleeSaveType type) const { |
| 880 | CHECK(HasCalleeSaveMethod(type)); |
| 881 | return callee_save_method_[type]; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 882 | } |
| 883 | |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 884 | void Runtime::SetCalleeSaveMethod(Method* method, CalleeSaveType type) { |
| 885 | DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType)); |
| 886 | callee_save_method_[type] = method; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 887 | } |
| 888 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 889 | void Runtime::EnableMethodTracing(Trace* tracer) { |
| 890 | CHECK(!IsMethodTracingActive()); |
| 891 | tracer_ = tracer; |
| 892 | } |
| 893 | |
| 894 | void Runtime::DisableMethodTracing() { |
| 895 | CHECK(IsMethodTracingActive()); |
| 896 | delete tracer_; |
| 897 | tracer_ = NULL; |
| 898 | } |
| 899 | |
| 900 | bool Runtime::IsMethodTracingActive() const { |
| 901 | return (tracer_ != NULL); |
| 902 | } |
| 903 | |
| 904 | Trace* Runtime::GetTracer() const { |
| 905 | CHECK(IsMethodTracingActive()); |
| 906 | return tracer_; |
| 907 | } |
| 908 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 909 | } // namespace art |