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