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