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