blob: d7bef1e2205dfd1c1484e57536ccbc7756529b2c [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 Shapiro1fb86202011-06-27 17:43:13 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "runtime.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070018
Brian Carlstromdbf05b72011-12-15 00:55:24 -080019#include <signal.h>
Elliott Hughesd06a6c72012-05-30 17:59:06 -070020#include <sys/syscall.h>
Brian Carlstromdbf05b72011-12-15 00:55:24 -080021
Elliott Hughesffe67362011-07-17 12:09:27 -070022#include <cstdio>
23#include <cstdlib>
Brian Carlstrom8a436592011-08-15 21:27:23 -070024#include <limits>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070025#include <vector>
Elliott Hughesffe67362011-07-17 12:09:27 -070026
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070027#include "class_linker.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070028#include "class_loader.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070029#include "constants_arm.h"
30#include "constants_x86.h"
Elliott Hughes3bb81562011-10-21 18:52:59 -070031#include "debugger.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070032#include "heap.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070033#include "image.h"
Elliott Hughescf4c6c42011-09-01 15:16:42 -070034#include "intern_table.h"
Elliott Hughesc5f7c912011-08-18 14:00:42 -070035#include "jni_internal.h"
Elliott Hughes32d6e1e2011-10-11 14:47:44 -070036#include "monitor.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070037#include "oat_file.h"
Mathieu Chartiera6399032012-06-11 18:49:50 -070038#include "scoped_heap_lock.h"
Elliott Hughes726079d2011-10-07 18:43:44 -070039#include "ScopedLocalRef.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070040#include "signal_catcher.h"
Elliott Hughes457005c2012-04-16 13:54:25 -070041#include "signal_set.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070042#include "space.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070043#include "thread.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070044#include "thread_list.h"
jeffhaob5e81852012-03-12 11:15:45 -070045#include "trace.h"
Elliott Hughes3bb81562011-10-21 18:52:59 -070046#include "UniquePtr.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070047#include "verifier/method_verifier.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070048#include "well_known_classes.h"
Carl Shapiro61e019d2011-07-14 16:53:09 -070049
Elliott Hugheseac76672012-05-24 21:56:51 -070050#include "JniConstants.h" // Last to avoid LOG redefinition in ics-mr1-plus-art.
Elliott Hughes90a33692011-08-30 13:27:07 -070051
Carl Shapiro1fb86202011-06-27 17:43:13 -070052namespace art {
53
Carl Shapiro2ed144c2011-07-26 16:52:08 -070054Runtime* Runtime::instance_ = NULL;
55
Elliott Hughesdcc24742011-09-07 14:02:44 -070056Runtime::Runtime()
Elliott Hughesd9c67be2012-02-02 19:54:06 -080057 : is_compiler_(false),
58 is_zygote_(false),
Mathieu Chartier069387a2012-06-18 12:01:01 -070059 is_concurrent_gc_enabled_(true),
Elliott Hughes6cf23882012-06-15 15:42:07 -070060 default_stack_size_(0),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080061 heap_(NULL),
Elliott Hughesc33a32b2011-10-11 18:18:07 -070062 monitor_list_(NULL),
Elliott Hughesdcc24742011-09-07 14:02:44 -070063 thread_list_(NULL),
64 intern_table_(NULL),
65 class_linker_(NULL),
66 signal_catcher_(NULL),
67 java_vm_(NULL),
Elliott Hughes225f5a12012-06-11 11:23:48 -070068 pre_allocated_OutOfMemoryError_(NULL),
Brian Carlstrom16192862011-09-12 17:50:06 -070069 jni_stub_array_(NULL),
Brian Carlstrome24fa612011-09-29 00:53:55 -070070 abstract_method_error_stub_array_(NULL),
Ian Rogers19846512012-02-24 11:42:47 -080071 resolution_method_(NULL),
Ian Rogers7c3757f2012-02-15 17:18:53 -080072 system_class_loader_(NULL),
Elliott Hughes6b355752012-01-13 16:49:08 -080073 shutting_down_(false),
Elliott Hughesdcc24742011-09-07 14:02:44 -070074 started_(false),
Mathieu Chartier7664f5c2012-06-08 18:15:32 -070075 finished_starting_(false),
Elliott Hughesdcc24742011-09-07 14:02:44 -070076 vfprintf_(NULL),
77 exit_(NULL),
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070078 abort_(NULL),
jeffhao2692b572011-12-16 15:42:28 -080079 stats_enabled_(false),
Ian Rogers776ac1f2012-04-13 23:36:36 -070080 method_trace_(0),
81 method_trace_file_size_(0),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080082 tracer_(NULL),
83 use_compile_time_class_path_(false) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -070084 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
85 resolution_stub_array_[i] = NULL;
86 }
87 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
Elliott Hughes225f5a12012-06-11 11:23:48 -070088 callee_save_methods_[i] = NULL;
Ian Rogers4f0d07c2011-10-06 23:38:47 -070089 }
Elliott Hughesdcc24742011-09-07 14:02:44 -070090}
91
Carl Shapiro61e019d2011-07-14 16:53:09 -070092Runtime::~Runtime() {
Elliott Hughes6b355752012-01-13 16:49:08 -080093 shutting_down_ = true;
94
jeffhaob5e81852012-03-12 11:15:45 -070095 if (IsMethodTracingActive()) {
96 Trace::Shutdown();
97 }
98
Mathieu Chartiera6399032012-06-11 18:49:50 -070099 // Make sure to let the GC complete if it is running.
100 {
101 ScopedHeapLock heap_lock;
102 heap_->WaitForConcurrentGcToComplete();
103 }
104
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700105 // Make sure our internal threads are dead before we start tearing down things they're using.
Elliott Hughese52e49b2012-04-02 16:05:44 -0700106 Dbg::StopJdwp();
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700107 delete signal_catcher_;
108
Elliott Hughes038a8062011-09-18 14:12:41 -0700109 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
Elliott Hughes93e74e82011-09-13 11:07:03 -0700110 delete thread_list_;
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700111 delete monitor_list_;
Elliott Hughes93e74e82011-09-13 11:07:03 -0700112
Carl Shapiro61e019d2011-07-14 16:53:09 -0700113 delete class_linker_;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800114 delete heap_;
Elliott Hughes0a1038b2012-06-14 16:24:17 -0700115 verifier::MethodVerifier::Shutdown();
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700116 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700117 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700118 Thread::Shutdown();
Elliott Hughes7c6169d2012-05-02 16:11:48 -0700119 QuasiAtomic::Shutdown();
120
Carl Shapiro4acf4642011-07-26 18:54:13 -0700121 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700122 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -0700123 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700124}
125
Elliott Hughescac6cc72011-11-03 20:31:21 -0700126static bool gAborting = false;
127
Elliott Hughese0918552011-10-28 17:18:29 -0700128struct AbortState {
129 void Dump(std::ostream& os) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700130 if (gAborting) {
131 os << "Runtime aborting --- recursively, so no thread-specific detail!\n";
132 return;
133 }
134 gAborting = true;
Elliott Hughese0918552011-10-28 17:18:29 -0700135 os << "Runtime aborting...\n";
Elliott Hughes6c7d2442012-02-01 18:40:47 -0800136 if (Runtime::Current() == NULL) {
137 os << "(Runtime does not yet exist!)\n";
138 return;
139 }
Elliott Hughese0918552011-10-28 17:18:29 -0700140 Thread* self = Thread::Current();
141 if (self == NULL) {
142 os << "(Aborting thread was not attached to runtime!)\n";
143 } else {
Elliott Hughes899e7892012-01-24 14:57:32 -0800144 self->Dump(os);
145 if (self->IsExceptionPending()) {
146 os << "Pending " << PrettyTypeOf(self->GetException()) << " on thread:\n"
147 << self->GetException()->Dump();
148 }
Elliott Hughese0918552011-10-28 17:18:29 -0700149 }
150 }
151};
152
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800153static Mutex& GetAbortLock() {
154 static Mutex abort_lock("abort lock");
155 return abort_lock;
156}
157
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700158void Runtime::Abort() {
Elliott Hughes131aef82012-01-27 11:53:35 -0800159 // Ensure that we don't have multiple threads trying to abort at once,
160 // which would result in significantly worse diagnostics.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800161 MutexLock mu(GetAbortLock());
Elliott Hughes131aef82012-01-27 11:53:35 -0800162
Elliott Hughesffe67362011-07-17 12:09:27 -0700163 // Get any pending output out of the way.
164 fflush(NULL);
165
166 // Many people have difficulty distinguish aborts from crashes,
167 // so be explicit.
Elliott Hughese0918552011-10-28 17:18:29 -0700168 AbortState state;
Elliott Hughes9ee5f9c2012-02-03 18:33:16 -0800169 LOG(INTERNAL_FATAL) << Dumpable<AbortState>(state);
Elliott Hughesffe67362011-07-17 12:09:27 -0700170
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700171 // Call the abort hook if we have one.
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700172 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700173 LOG(INTERNAL_FATAL) << "Calling abort hook...";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700174 Runtime::Current()->abort_();
175 // notreached
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700176 LOG(INTERNAL_FATAL) << "Unexpectedly returned from abort hook!";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700177 }
178
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700179#if defined(__BIONIC__)
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700180 // TODO: finish merging patches to fix abort(3) in bionic, then lose this!
181 // Bionic doesn't implement POSIX semantics for abort(3) in a multi-threaded
182 // process, so if we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -0700183 // receive SIGABRT. debuggerd dumps the stack trace of the main
184 // thread, whether or not that was the thread that failed. By
185 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -0700186 // fault in the current thread, and get a useful log from debuggerd.
Elliott Hughes81ff3182012-03-23 20:35:56 -0700187 // We can also trivially tell the difference between a crash and
Elliott Hughesffe67362011-07-17 12:09:27 -0700188 // a deliberate abort by looking at the fault address.
189 *reinterpret_cast<char*>(0xdeadd00d) = 38;
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700190#elif defined(__APPLE__)
191 // TODO: check that this actually gives good stack traces on the Mac!
192 pthread_kill(pthread_self(), SIGABRT);
193#else
194 // TODO: we ought to be able to use pthread_kill(3) here (or abort(3),
195 // which POSIX defines in terms of raise(3), which POSIX defines in terms
196 // of pthread_kill(3)). On Linux, though, libcorkscrew can't unwind through
197 // libpthread, which means the stacks we dump would be useless. Calling
198 // tgkill(2) directly avoids that.
199 syscall(__NR_tgkill, getpid(), GetTid(), SIGABRT);
200#endif
Elliott Hughesffe67362011-07-17 12:09:27 -0700201 // notreached
202}
203
Elliott Hughesbf86d042011-08-31 17:53:14 -0700204void Runtime::CallExitHook(jint status) {
205 if (exit_ != NULL) {
Elliott Hughes34e06962012-04-09 13:55:55 -0700206 ScopedThreadStateChange tsc(Thread::Current(), kNative);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700207 exit_(status);
208 LOG(WARNING) << "Exit hook returned instead of exiting!";
209 }
210}
211
Brian Carlstrom8a436592011-08-15 21:27:23 -0700212// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
213// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
214// [gG] gigabytes.
215//
216// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700217// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
218// of 1024.
219//
220// The spec says the -Xmx and -Xms options must be multiples of 1024. It
221// doesn't say anything about -Xss.
222//
223// Returns 0 (a useless size) if "s" is malformed or specifies a low or
224// non-evenly-divisible value.
225//
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800226size_t ParseMemoryOption(const char* s, size_t div) {
Brian Carlstrom8a436592011-08-15 21:27:23 -0700227 // strtoul accepts a leading [+-], which we don't want,
228 // so make sure our string starts with a decimal digit.
229 if (isdigit(*s)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700230 char* s2;
231 size_t val = strtoul(s, &s2, 10);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700232 if (s2 != s) {
233 // s2 should be pointing just after the number.
234 // If this is the end of the string, the user
235 // has specified a number of bytes. Otherwise,
236 // there should be exactly one more character
237 // that specifies a multiplier.
238 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700239 // The remainder of the string is either a single multiplier
240 // character, or nothing to indicate that the value is in
241 // bytes.
242 char c = *s2++;
243 if (*s2 == '\0') {
244 size_t mul;
245 if (c == '\0') {
246 mul = 1;
247 } else if (c == 'k' || c == 'K') {
248 mul = KB;
249 } else if (c == 'm' || c == 'M') {
250 mul = MB;
251 } else if (c == 'g' || c == 'G') {
252 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700253 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700254 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700255 return 0;
256 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700257
258 if (val <= std::numeric_limits<size_t>::max() / mul) {
259 val *= mul;
260 } else {
261 // Clamp to a multiple of 1024.
262 val = std::numeric_limits<size_t>::max() & ~(1024-1);
263 }
264 } else {
265 // There's more than one character after the numeric part.
266 return 0;
267 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700268 }
269 // The man page says that a -Xm value must be a multiple of 1024.
270 if (val % div == 0) {
271 return val;
272 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700273 }
274 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700275 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700276}
277
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800278size_t ParseIntegerOrDie(const std::string& s) {
279 std::string::size_type colon = s.find(':');
280 if (colon == std::string::npos) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700281 LOG(FATAL) << "Missing integer: " << s;
282 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800283 const char* begin = &s[colon + 1];
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700284 char* end;
285 size_t result = strtoul(begin, &end, 10);
286 if (begin == end || *end != '\0') {
287 LOG(FATAL) << "Failed to parse integer in: " << s;
288 }
289 return result;
290}
291
Elliott Hughes0af55432011-08-17 18:37:28 -0700292void LoadJniLibrary(JavaVMExt* vm, const char* name) {
Elliott Hughes6cc332e2012-01-20 22:59:20 -0800293 std::string mapped_name(StringPrintf(OS_SHARED_LIB_FORMAT_STR, name));
Elliott Hughes75770752011-08-24 17:52:38 -0700294 std::string reason;
295 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700296 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
297 << reason;
298 }
299}
300
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700301Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700302 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800303 const char* boot_class_path_string = getenv("BOOTCLASSPATH");
304 if (boot_class_path_string != NULL) {
305 parsed->boot_class_path_string_ = boot_class_path_string;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700306 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800307 const char* class_path_string = getenv("CLASSPATH");
308 if (class_path_string != NULL) {
309 parsed->class_path_string_ = class_path_string;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700310 }
Elliott Hughes67d92002012-03-26 15:08:51 -0700311 // -Xcheck:jni is off by default for regular builds but on by default in debug builds.
312 parsed->check_jni_ = kIsDebugBuild;
Elliott Hughes85d15452011-09-16 17:33:01 -0700313
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700314 parsed->heap_initial_size_ = Heap::kInitialSize;
315 parsed->heap_maximum_size_ = Heap::kMaximumSize;
Elliott Hughes6cf23882012-06-15 15:42:07 -0700316 parsed->heap_growth_limit_ = 0; // 0 means no growth limit.
317 parsed->stack_size_ = 0; // 0 means default.
Brian Carlstromf734cf52011-08-17 16:28:14 -0700318
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800319 parsed->is_compiler_ = false;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700320 parsed->is_zygote_ = false;
Mathieu Chartier069387a2012-06-18 12:01:01 -0700321 parsed->is_concurrent_gc_enabled_ = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700322
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700323 parsed->jni_globals_max_ = 0;
Elliott Hughesfc861622011-10-17 17:57:47 -0700324 parsed->lock_profiling_threshold_ = 0;
325 parsed->hook_is_sensitive_thread_ = NULL;
326
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700327 parsed->hook_vfprintf_ = vfprintf;
328 parsed->hook_exit_ = exit;
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700329 parsed->hook_abort_ = NULL; // We don't call abort(3) by default; see Runtime::Abort.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700330
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800331// gLogVerbosity.class_linker = true; // TODO: don't check this in!
332// gLogVerbosity.compiler = true; // TODO: don't check this in!
333// gLogVerbosity.heap = true; // TODO: don't check this in!
334// gLogVerbosity.gc = true; // TODO: don't check this in!
335// gLogVerbosity.jdwp = true; // TODO: don't check this in!
336// gLogVerbosity.jni = true; // TODO: don't check this in!
337// gLogVerbosity.monitor = true; // TODO: don't check this in!
338// gLogVerbosity.startup = true; // TODO: don't check this in!
339// gLogVerbosity.third_party_jni = true; // TODO: don't check this in!
340// gLogVerbosity.threads = true; // TODO: don't check this in!
341
jeffhaob5e81852012-03-12 11:15:45 -0700342 parsed->method_trace_ = false;
343 parsed->method_trace_file_ = "/data/method-trace-file.bin";
344 parsed->method_trace_file_size_ = 10 * MB;
345
Brian Carlstrom8a436592011-08-15 21:27:23 -0700346 for (size_t i = 0; i < options.size(); ++i) {
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800347 const std::string option(options[i].first);
Brian Carlstrom0796af02011-10-12 14:31:45 -0700348 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700349 LOG(INFO) << "option[" << i << "]=" << option;
350 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800351 if (StartsWith(option, "-Xbootclasspath:")) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800352 parsed->boot_class_path_string_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700353 } else if (option == "-classpath" || option == "-cp") {
354 // TODO: support -Djava.class.path
355 i++;
356 if (i == options.size()) {
357 // TODO: usage
358 LOG(FATAL) << "Missing required class path value for " << option;
359 return NULL;
360 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800361 const StringPiece& value = options[i].first;
362 parsed->class_path_string_ = value.data();
363 } else if (option == "bootclasspath") {
364 parsed->boot_class_path_
365 = reinterpret_cast<const std::vector<const DexFile*>*>(options[i].second);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800366 } else if (StartsWith(option, "-Ximage:")) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800367 parsed->image_ = option.substr(strlen("-Ximage:")).data();
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800368 } else if (StartsWith(option, "-Xcheck:jni")) {
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700369 parsed->check_jni_ = true;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800370 } else if (StartsWith(option, "-Xrunjdwp:") || StartsWith(option, "-agentlib:jdwp=")) {
371 std::string tail(option.substr(option[1] == 'X' ? 10 : 15));
Elliott Hughes3bb81562011-10-21 18:52:59 -0700372 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
373 LOG(FATAL) << "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
374 << "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n";
375 return NULL;
376 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800377 } else if (StartsWith(option, "-Xms")) {
378 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).c_str(), 1024);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700379 if (size == 0) {
380 if (ignore_unrecognized) {
381 continue;
382 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700383 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700384 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700385 return NULL;
386 }
387 parsed->heap_initial_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800388 } else if (StartsWith(option, "-Xmx")) {
389 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).c_str(), 1024);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700390 if (size == 0) {
391 if (ignore_unrecognized) {
392 continue;
393 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700394 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700395 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700396 return NULL;
397 }
398 parsed->heap_maximum_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800399 } else if (StartsWith(option, "-XX:HeapGrowthLimit=")) {
400 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).c_str(), 1024);
jeffhaoc1160702011-10-27 15:48:45 -0700401 if (size == 0) {
402 if (ignore_unrecognized) {
403 continue;
404 }
405 // TODO: usage
406 LOG(FATAL) << "Failed to parse " << option;
407 return NULL;
408 }
409 parsed->heap_growth_limit_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800410 } else if (StartsWith(option, "-Xss")) {
411 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).c_str(), 1);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700412 if (size == 0) {
413 if (ignore_unrecognized) {
414 continue;
415 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700416 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700417 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700418 return NULL;
419 }
420 parsed->stack_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800421 } else if (StartsWith(option, "-D")) {
422 parsed->properties_.push_back(option.substr(strlen("-D")));
423 } else if (StartsWith(option, "-Xjnitrace:")) {
424 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:"));
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700425 } else if (option == "compiler") {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800426 parsed->is_compiler_ = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700427 } else if (option == "-Xzygote") {
428 parsed->is_zygote_ = true;
Mathieu Chartier069387a2012-06-18 12:01:01 -0700429 } else if (StartsWith(option, "-Xgc:")) {
430 std::vector<std::string> gc_options;
431 Split(option.substr(strlen("-Xgc:")), ',', gc_options);
432 for (size_t i = 0; i < gc_options.size(); ++i) {
433 if (gc_options[i] == "noconcurrent") {
434 parsed->is_concurrent_gc_enabled_ = false;
435 } else if (gc_options[i] == "concurrent") {
436 parsed->is_concurrent_gc_enabled_ = true;
437 } else {
438 LOG(WARNING) << "Ignoring unknown -Xgc option: " << gc_options[i];
439 }
440 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800441 } else if (StartsWith(option, "-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700442 std::vector<std::string> verbose_options;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800443 Split(option.substr(strlen("-verbose:")), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700444 for (size_t i = 0; i < verbose_options.size(); ++i) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800445 if (verbose_options[i] == "class") {
446 gLogVerbosity.class_linker = true;
447 } else if (verbose_options[i] == "compiler") {
448 gLogVerbosity.compiler = true;
449 } else if (verbose_options[i] == "heap") {
450 gLogVerbosity.heap = true;
451 } else if (verbose_options[i] == "gc") {
452 gLogVerbosity.gc = true;
453 } else if (verbose_options[i] == "jdwp") {
454 gLogVerbosity.jdwp = true;
455 } else if (verbose_options[i] == "jni") {
456 gLogVerbosity.jni = true;
457 } else if (verbose_options[i] == "monitor") {
458 gLogVerbosity.monitor = true;
459 } else if (verbose_options[i] == "startup") {
460 gLogVerbosity.startup = true;
461 } else if (verbose_options[i] == "third-party-jni") {
462 gLogVerbosity.third_party_jni = true;
463 } else if (verbose_options[i] == "threads") {
464 gLogVerbosity.threads = true;
465 } else {
466 LOG(WARNING) << "Ignoring unknown -verbose option: " << verbose_options[i];
467 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700468 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800469 } else if (StartsWith(option, "-Xjnigreflimit:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700470 parsed->jni_globals_max_ = ParseIntegerOrDie(option);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800471 } else if (StartsWith(option, "-Xlockprofthreshold:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700472 parsed->lock_profiling_threshold_ = ParseIntegerOrDie(option);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800473 } else if (StartsWith(option, "-Xstacktracefile:")) {
Elliott Hughes67d92002012-03-26 15:08:51 -0700474 if (kIsDebugBuild) {
475 // Ignore the zygote and always show stack traces in debug builds.
476 } else {
477 parsed->stack_trace_file_ = option.substr(strlen("-Xstacktracefile:"));
478 }
Elliott Hughesfc861622011-10-17 17:57:47 -0700479 } else if (option == "sensitiveThread") {
480 parsed->hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(options[i].second);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700481 } else if (option == "vfprintf") {
482 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
483 } else if (option == "exit") {
484 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
485 } else if (option == "abort") {
486 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700487 } else if (option == "host-prefix") {
488 parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second);
Elliott Hughesb3e66df2012-01-12 14:49:18 -0800489 } else if (option == "-Xgenregmap" || option == "-Xgc:precise") {
490 // We silently ignore these for backwards compatibility.
jeffhaob5e81852012-03-12 11:15:45 -0700491 } else if (option == "-Xmethod-trace") {
492 parsed->method_trace_ = true;
493 } else if (StartsWith(option, "-Xmethod-trace-file:")) {
494 parsed->method_trace_file_ = option.substr(strlen("-Xmethod-trace-file:"));
495 } else if (StartsWith(option, "-Xmethod-trace-file-size:")) {
496 parsed->method_trace_file_size_ = ParseIntegerOrDie(option);
Elliott Hughese119a362012-05-22 17:37:06 -0700497 } else if (option == "-Xprofile:threadcpuclock") {
498 Trace::SetDefaultClockSource(kProfilerClockSourceThreadCpu);
499 } else if (option == "-Xprofile:wallclock") {
500 Trace::SetDefaultClockSource(kProfilerClockSourceWall);
501 } else if (option == "-Xprofile:dualclock") {
502 Trace::SetDefaultClockSource(kProfilerClockSourceDual);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700503 } else {
504 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700505 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700506 LOG(ERROR) << "Unrecognized option " << option;
507 // TODO: this should exit, but for now tolerate unknown options
508 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700509 }
510 }
511 }
512
Brian Carlstrom223f20f2012-02-04 23:06:55 -0800513 if (!parsed->is_compiler_ && parsed->image_.empty()) {
514 parsed->image_ += GetAndroidRoot();
515 parsed->image_ += "/framework/boot.art";
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700516 }
jeffhaoc1160702011-10-27 15:48:45 -0700517 if (parsed->heap_growth_limit_ == 0) {
518 parsed->heap_growth_limit_ = parsed->heap_maximum_size_;
519 }
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700520
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700521 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700522}
523
524Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700525 // TODO: acquire a static mutex on Runtime to avoid racing.
526 if (Runtime::instance_ != NULL) {
527 return NULL;
528 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700529 instance_ = new Runtime;
530 if (!instance_->Init(options, ignore_unrecognized)) {
531 delete instance_;
532 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700533 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700534 return instance_;
535}
Elliott Hughes0af55432011-08-17 18:37:28 -0700536
Brian Carlstromaded5f72011-10-07 17:15:04 -0700537void CreateSystemClassLoader() {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800538 if (Runtime::Current()->UseCompileTimeClassPath()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700539 return;
540 }
541
542 Thread* self = Thread::Current();
543
544 // Must be in the kNative state for calling native methods.
Elliott Hughes34e06962012-04-09 13:55:55 -0700545 CHECK_EQ(self->GetState(), kNative);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700546
547 JNIEnv* env = self->GetJniEnv();
Elliott Hugheseac76672012-05-24 21:56:51 -0700548 jmethodID getSystemClassLoader = env->GetStaticMethodID(WellKnownClasses::java_lang_ClassLoader,
Brian Carlstromdf143242011-10-10 18:05:34 -0700549 "getSystemClassLoader",
550 "()Ljava/lang/ClassLoader;");
551 CHECK(getSystemClassLoader != NULL);
Elliott Hugheseac76672012-05-24 21:56:51 -0700552 ScopedLocalRef<jobject> class_loader(env, env->CallStaticObjectMethod(WellKnownClasses::java_lang_ClassLoader,
Brian Carlstromdf143242011-10-10 18:05:34 -0700553 getSystemClassLoader));
554 CHECK(class_loader.get() != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700555
Brian Carlstromdf143242011-10-10 18:05:34 -0700556 Thread::Current()->SetClassLoaderOverride(Decode<ClassLoader*>(env, class_loader.get()));
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500557
Elliott Hugheseac76672012-05-24 21:56:51 -0700558 jfieldID contextClassLoader = env->GetFieldID(WellKnownClasses::java_lang_Thread,
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500559 "contextClassLoader",
560 "Ljava/lang/ClassLoader;");
561 CHECK(contextClassLoader != NULL);
562 ScopedLocalRef<jobject> self_jobject(env, AddLocalReference<jobject>(env, self->GetPeer()));
563 env->SetObjectField(self_jobject.get(), contextClassLoader, class_loader.get());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700564}
565
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700566void Runtime::Start() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800567 VLOG(startup) << "Runtime::Start entering";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700568
569 CHECK(host_prefix_.empty()) << host_prefix_;
570
Elliott Hughes225f5a12012-06-11 11:23:48 -0700571 // Relocate the OatFiles (ELF images).
Logan Chien0c717dd2012-03-28 18:31:07 +0800572 class_linker_->RelocateExecutable();
573
Elliott Hughes225f5a12012-06-11 11:23:48 -0700574 // Restore main thread state to kNative as expected by native code.
575 Thread* self = Thread::Current();
576 self->SetState(kNative);
577
578 // Pre-allocate an OutOfMemoryError for the double-OOME case.
579 self->ThrowNewException("Ljava/lang/OutOfMemoryError;",
580 "OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack available");
581 pre_allocated_OutOfMemoryError_ = self->GetException();
582 self->ClearException();
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700583
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700584 started_ = true;
585
Brian Carlstromae826982011-11-09 01:33:42 -0800586 // InitNativeMethods needs to be after started_ so that the classes
587 // it touches will have methods linked to the oat file if necessary.
588 InitNativeMethods();
589
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500590 Thread::FinishStartup();
591
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700592 if (!is_zygote_) {
593 DidForkFromZygote();
594 }
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700595
Elliott Hughes85d15452011-09-16 17:33:01 -0700596 StartDaemonThreads();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700597
Brian Carlstromaded5f72011-10-07 17:15:04 -0700598 CreateSystemClassLoader();
599
Elliott Hughes225f5a12012-06-11 11:23:48 -0700600 self->GetJniEnv()->locals.AssertEmpty();
Elliott Hughes726079d2011-10-07 18:43:44 -0700601
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800602 VLOG(startup) << "Runtime::Start exiting";
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700603
604 finished_starting_ = true;
Elliott Hughes85d15452011-09-16 17:33:01 -0700605}
606
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700607void Runtime::DidForkFromZygote() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700608 is_zygote_ = false;
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700609
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700610 StartSignalCatcher();
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700611
612 // Start the JDWP thread. If the command-line debugger flags specified "suspend=y",
613 // this will pause the runtime, so we probably want this to come last.
614 Dbg::StartJdwp();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700615}
616
617void Runtime::StartSignalCatcher() {
618 if (!is_zygote_) {
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700619 signal_catcher_ = new SignalCatcher(stack_trace_file_);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700620 }
621}
622
Elliott Hughes85d15452011-09-16 17:33:01 -0700623void Runtime::StartDaemonThreads() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800624 VLOG(startup) << "Runtime::StartDaemonThreads entering";
Elliott Hughes85d15452011-09-16 17:33:01 -0700625
Elliott Hughes719b3232011-09-25 17:42:19 -0700626 Thread* self = Thread::Current();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700627
628 // Must be in the kNative state for calling native methods.
Elliott Hughes34e06962012-04-09 13:55:55 -0700629 CHECK_EQ(self->GetState(), kNative);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700630
Elliott Hughes719b3232011-09-25 17:42:19 -0700631 JNIEnv* env = self->GetJniEnv();
Elliott Hugheseac76672012-05-24 21:56:51 -0700632 env->CallStaticVoidMethod(WellKnownClasses::java_lang_Daemons, WellKnownClasses::java_lang_Daemons_start);
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800633 CHECK(!env->ExceptionCheck());
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700634
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800635 VLOG(startup) << "Runtime::StartDaemonThreads exiting";
Carl Shapiro61e019d2011-07-14 16:53:09 -0700636}
637
Elliott Hughes0af55432011-08-17 18:37:28 -0700638bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700639 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700640
Elliott Hughes90a33692011-08-30 13:27:07 -0700641 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
642 if (options.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700643 LOG(ERROR) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700644 return false;
645 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800646 VLOG(startup) << "Runtime::Init -verbose:startup enabled";
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700647
Elliott Hughes7c6169d2012-05-02 16:11:48 -0700648 QuasiAtomic::Startup();
649
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700650 SetJniGlobalsMax(options->jni_globals_max_);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800651 Monitor::Init(options->lock_profiling_threshold_, options->hook_is_sensitive_thread_);
Elliott Hughes32d6e1e2011-10-11 14:47:44 -0700652
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700653 host_prefix_ = options->host_prefix_;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800654 boot_class_path_string_ = options->boot_class_path_string_;
655 class_path_string_ = options->class_path_string_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700656 properties_ = options->properties_;
657
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800658 is_compiler_ = options->is_compiler_;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700659 is_zygote_ = options->is_zygote_;
Mathieu Chartier069387a2012-06-18 12:01:01 -0700660 is_concurrent_gc_enabled_ = options->is_concurrent_gc_enabled_;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700661
Elliott Hughes0af55432011-08-17 18:37:28 -0700662 vfprintf_ = options->hook_vfprintf_;
663 exit_ = options->hook_exit_;
664 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700665
Elliott Hughesbe759c62011-09-08 19:38:21 -0700666 default_stack_size_ = options->stack_size_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700667 stack_trace_file_ = options->stack_trace_file_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700668
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700669 monitor_list_ = new MonitorList;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800670 thread_list_ = new ThreadList;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700671 intern_table_ = new InternTable;
672
Elliott Hughes0a1038b2012-06-14 16:24:17 -0700673 verifier::MethodVerifier::Init();
Logan Chiendd361c92012-04-10 23:40:37 +0800674
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800675 heap_ = new Heap(options->heap_initial_size_,
676 options->heap_growth_limit_,
677 options->heap_maximum_size_,
678 options->image_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700679
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700680 BlockSignals();
Elliott Hughes457005c2012-04-16 13:54:25 -0700681 InitPlatformSignalHandlers();
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700682
Elliott Hughesa0957642011-09-02 14:27:33 -0700683 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700684
Elliott Hughesbe759c62011-09-08 19:38:21 -0700685 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700686
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700687 // ClassLinker needs an attached thread, but we can't fully attach a thread
Elliott Hughes462c9442012-03-23 18:47:50 -0700688 // without creating objects. We can't supply a thread group yet; it will be fixed later.
689 Thread::Attach("main", false, NULL);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700690
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700691 // Set us to runnable so tools using a runtime can allocate and GC by default
Elliott Hughes34e06962012-04-09 13:55:55 -0700692 Thread::Current()->SetState(kRunnable);
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700693
Ian Rogers141d6222012-04-05 12:23:06 -0700694 // Now we're attached, we can take the heap lock and validate the heap.
695 GetHeap()->EnableObjectValidation();
696
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800697 CHECK_GE(GetHeap()->GetSpaces().size(), 1U);
698 if (GetHeap()->GetSpaces()[0]->IsImageSpace()) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800699 class_linker_ = ClassLinker::CreateFromImage(intern_table_);
700 } else {
701 CHECK(options->boot_class_path_ != NULL);
702 CHECK_NE(options->boot_class_path_->size(), 0U);
703 class_linker_ = ClassLinker::CreateFromCompiler(*options->boot_class_path_, intern_table_);
704 }
705 CHECK(class_linker_ != NULL);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700706
jeffhaob5e81852012-03-12 11:15:45 -0700707 method_trace_ = options->method_trace_;
708 method_trace_file_ = options->method_trace_file_;
709 method_trace_file_size_ = options->method_trace_file_size_;
710
711 if (options->method_trace_) {
712 Trace::Start(options->method_trace_file_.c_str(), -1, options->method_trace_file_size_, 0, false);
713 }
714
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800715 VLOG(startup) << "Runtime::Init exiting";
Carl Shapiro1fb86202011-06-27 17:43:13 -0700716 return true;
717}
718
Elliott Hughes038a8062011-09-18 14:12:41 -0700719void Runtime::InitNativeMethods() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800720 VLOG(startup) << "Runtime::InitNativeMethods entering";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700721 Thread* self = Thread::Current();
722 JNIEnv* env = self->GetJniEnv();
723
Elliott Hughes418d20f2011-09-22 14:00:39 -0700724 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Elliott Hughes34e06962012-04-09 13:55:55 -0700725 CHECK_EQ(self->GetState(), kNative);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700726
Elliott Hughes418d20f2011-09-22 14:00:39 -0700727 // First set up JniConstants, which is used by both the runtime's built-in native
728 // methods and libcore.
Elliott Hughesfea966e2011-09-22 10:26:20 -0700729 JniConstants::init(env);
Elliott Hugheseac76672012-05-24 21:56:51 -0700730 WellKnownClasses::Init(env);
Elliott Hughesfea966e2011-09-22 10:26:20 -0700731
Elliott Hughes418d20f2011-09-22 14:00:39 -0700732 // Then set up the native methods provided by the runtime itself.
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700733 RegisterRuntimeNativeMethods(env);
734
Elliott Hughes418d20f2011-09-22 14:00:39 -0700735 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
736 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
737 // the library that implements System.loadLibrary!
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700738 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800739 VLOG(startup) << "Runtime::InitNativeMethods exiting";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700740}
741
742void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
743#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Ian Rogers5167c972012-02-03 10:41:20 -0800744 // Register Throwable first so that registration of other native methods can throw exceptions
745 REGISTER(register_java_lang_Throwable);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700746 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700747 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700748 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700749 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700750 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700751 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700752 REGISTER(register_java_lang_Object);
753 REGISTER(register_java_lang_Runtime);
754 REGISTER(register_java_lang_String);
755 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700756 REGISTER(register_java_lang_Thread);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700757 REGISTER(register_java_lang_VMClassLoader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700758 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700759 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700760 REGISTER(register_java_lang_reflect_Field);
761 REGISTER(register_java_lang_reflect_Method);
Jesse Wilson95caa792011-10-12 18:14:17 -0400762 REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700763 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700764 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700765 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700766 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700767#undef REGISTER
768}
769
Elliott Hughesc967f782012-04-16 10:23:15 -0700770void Runtime::DumpForSigQuit(std::ostream& os) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700771 GetClassLinker()->DumpForSigQuit(os);
772 GetInternTable()->DumpForSigQuit(os);
Elliott Hughesae80b492012-04-24 10:43:17 -0700773 GetJavaVM()->DumpForSigQuit(os);
Elliott Hughesc967f782012-04-16 10:23:15 -0700774 GetHeap()->DumpForSigQuit(os);
Elliott Hughes42ee1422011-09-06 12:33:32 -0700775 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700776
Elliott Hughesc967f782012-04-16 10:23:15 -0700777 thread_list_->DumpForSigQuit(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700778}
779
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800780void Runtime::DumpLockHolders(std::ostream& os) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800781 pid_t heap_lock_owner = GetHeap()->GetLockOwner();
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800782 pid_t thread_list_lock_owner = GetThreadList()->GetLockOwner();
783 pid_t classes_lock_owner = GetClassLinker()->GetClassesLockOwner();
784 pid_t dex_lock_owner = GetClassLinker()->GetDexLockOwner();
785 if ((heap_lock_owner | thread_list_lock_owner | classes_lock_owner | dex_lock_owner) != 0) {
786 os << "Heap lock owner tid: " << heap_lock_owner << "\n"
787 << "ThreadList lock owner tid: " << thread_list_lock_owner << "\n"
788 << "ClassLinker classes lock owner tid: " << classes_lock_owner << "\n"
789 << "ClassLinker dex lock owner tid: " << dex_lock_owner << "\n";
790 }
791}
792
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700793void Runtime::SetStatsEnabled(bool new_state) {
794 if (new_state == true) {
795 GetStats()->Clear(~0);
796 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
797 Thread::Current()->GetStats()->Clear(~0);
798 }
799 stats_enabled_ = new_state;
800}
801
802void Runtime::ResetStats(int kinds) {
803 GetStats()->Clear(kinds & 0xffff);
804 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
805 Thread::Current()->GetStats()->Clear(kinds >> 16);
806}
807
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700808int32_t Runtime::GetStat(int kind) {
809 RuntimeStats* stats;
810 if (kind < (1<<16)) {
811 stats = GetStats();
812 } else {
813 stats = Thread::Current()->GetStats();
814 kind >>= 16;
815 }
816 switch (kind) {
817 case KIND_ALLOCATED_OBJECTS:
818 return stats->allocated_objects;
819 case KIND_ALLOCATED_BYTES:
820 return stats->allocated_bytes;
821 case KIND_FREED_OBJECTS:
822 return stats->freed_objects;
823 case KIND_FREED_BYTES:
824 return stats->freed_bytes;
825 case KIND_GC_INVOCATIONS:
826 return stats->gc_for_alloc_count;
827 case KIND_CLASS_INIT_COUNT:
828 return stats->class_init_count;
829 case KIND_CLASS_INIT_TIME:
830 // Convert ns to us, reduce to 32 bits.
Elliott Hughes398f64b2012-03-26 18:05:48 -0700831 return static_cast<int>(stats->class_init_time_ns / 1000);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700832 case KIND_EXT_ALLOCATED_OBJECTS:
833 case KIND_EXT_ALLOCATED_BYTES:
834 case KIND_EXT_FREED_OBJECTS:
835 case KIND_EXT_FREED_BYTES:
836 return 0; // backward compatibility
837 default:
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700838 LOG(FATAL) << "Unknown statistic " << kind;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700839 return -1; // unreachable
840 }
841}
842
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700843void Runtime::BlockSignals() {
Elliott Hughes457005c2012-04-16 13:54:25 -0700844 SignalSet signals;
845 signals.Add(SIGPIPE);
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700846 // SIGQUIT is used to dump the runtime's state (including stack traces).
Elliott Hughes457005c2012-04-16 13:54:25 -0700847 signals.Add(SIGQUIT);
Elliott Hughes08795042012-04-03 14:48:52 -0700848 // SIGUSR1 is used to initiate a GC.
Elliott Hughes457005c2012-04-16 13:54:25 -0700849 signals.Add(SIGUSR1);
850 signals.Block();
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700851}
852
Elliott Hughes462c9442012-03-23 18:47:50 -0700853void Runtime::AttachCurrentThread(const char* thread_name, bool as_daemon, Object* thread_group) {
854 Thread::Attach(thread_name, as_daemon, thread_group);
Elliott Hughes22869a92012-03-27 14:08:24 -0700855 if (thread_name == NULL) {
856 LOG(WARNING) << *Thread::Current() << " attached without supplying a name";
857 }
Carl Shapiro61e019d2011-07-14 16:53:09 -0700858}
859
Elliott Hughesd92bec42011-09-02 17:04:36 -0700860void Runtime::DetachCurrentThread() {
Brian Carlstrom4d571432012-05-16 00:21:41 -0700861 Thread* self = Thread::Current();
862 if (self == NULL) {
863 LOG(FATAL) << "attempting to detach thread that is not attached";
864 }
865 if (self->GetTopOfStack().GetSP() != NULL) {
Elliott Hughes22869a92012-03-27 14:08:24 -0700866 LOG(FATAL) << *Thread::Current() << " attempting to detach while still running code";
867 }
Elliott Hughes02b48d12011-09-07 17:15:51 -0700868 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700869}
870
Brian Carlstrome24fa612011-09-29 00:53:55 -0700871void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700872 Dbg::VisitRoots(visitor, arg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700873 class_linker_->VisitRoots(visitor, arg);
874 intern_table_->VisitRoots(visitor, arg);
875 java_vm_->VisitRoots(visitor, arg);
876 thread_list_->VisitRoots(visitor, arg);
Elliott Hughes225f5a12012-06-11 11:23:48 -0700877 if (pre_allocated_OutOfMemoryError_ != NULL) {
878 visitor(pre_allocated_OutOfMemoryError_, arg);
879 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700880 visitor(jni_stub_array_, arg);
881 visitor(abstract_method_error_stub_array_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700882 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
883 visitor(resolution_stub_array_[i], arg);
884 }
Ian Rogers19846512012-02-24 11:42:47 -0800885 visitor(resolution_method_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700886 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
Elliott Hughes225f5a12012-06-11 11:23:48 -0700887 visitor(callee_save_methods_[i], arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700888 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700889}
890
Ian Rogers169c9a72011-11-13 20:13:17 -0800891void Runtime::SetJniDlsymLookupStub(ByteArray* jni_stub_array) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700892 CHECK(jni_stub_array != NULL) << " jni_stub_array=" << jni_stub_array;
893 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array)
894 << "jni_stub_array_=" << jni_stub_array_ << " jni_stub_array=" << jni_stub_array;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700895 jni_stub_array_ = jni_stub_array;
896}
897
Brian Carlstrome24fa612011-09-29 00:53:55 -0700898void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) {
899 CHECK(abstract_method_error_stub_array != NULL);
900 CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array);
901 abstract_method_error_stub_array_ = abstract_method_error_stub_array;
902}
903
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700904void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700905 CHECK(resolution_stub_array != NULL);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700906 CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array);
907 resolution_stub_array_[type] = resolution_stub_array;
Ian Rogersad25ac52011-10-04 19:13:33 -0700908}
909
Ian Rogers19846512012-02-24 11:42:47 -0800910Method* Runtime::CreateResolutionMethod() {
911 Class* method_class = Method::GetMethodClass();
912 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
913 method->SetDeclaringClass(method_class);
914 // TODO: use a special method for resolution method saves
915 method->SetDexMethodIndex(DexFile::kDexNoIndex16);
916 ByteArray* unknown_resolution_stub = GetResolutionStubArray(kUnknownMethod);
917 CHECK(unknown_resolution_stub != NULL);
918 method->SetCode(unknown_resolution_stub->GetData());
919 return method.get();
920}
921
Brian Carlstromae826982011-11-09 01:33:42 -0800922Method* Runtime::CreateCalleeSaveMethod(InstructionSet instruction_set, CalleeSaveType type) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700923 Class* method_class = Method::GetMethodClass();
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700924 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
Ian Rogersff1ed472011-09-20 13:46:24 -0700925 method->SetDeclaringClass(method_class);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800926 // TODO: use a special method for callee saves
Ian Rogers19846512012-02-24 11:42:47 -0800927 method->SetDexMethodIndex(DexFile::kDexNoIndex16);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700928 method->SetCode(NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800929 if ((instruction_set == kThumb2) || (instruction_set == kArm)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700930 uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6) | (1 << art::arm::R7) |
931 (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11);
932 uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3);
933 uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9);
934 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills :0) |
935 (type == kSaveAll ? all_spills :0) | (1 << art::arm::LR);
936 uint32_t fp_all_spills = (1 << art::arm::S0) | (1 << art::arm::S1) | (1 << art::arm::S2) |
937 (1 << art::arm::S3) | (1 << art::arm::S4) | (1 << art::arm::S5) |
938 (1 << art::arm::S6) | (1 << art::arm::S7) | (1 << art::arm::S8) |
939 (1 << art::arm::S9) | (1 << art::arm::S10) | (1 << art::arm::S11) |
940 (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) |
941 (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) |
942 (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) |
943 (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) |
944 (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) |
945 (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
946 (1 << art::arm::S30) | (1 << art::arm::S31);
947 uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
948 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
949 __builtin_popcount(fp_spills) /* fprs */ +
950 1 /* Method* */) * kPointerSize, kStackAlignment);
Ian Rogers15fdb8c2011-09-25 15:45:07 -0700951 method->SetFrameSizeInBytes(frame_size);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700952 method->SetCoreSpillMask(core_spills);
953 method->SetFpSpillMask(fp_spills);
Brian Carlstromae826982011-11-09 01:33:42 -0800954 } else if (instruction_set == kX86) {
Ian Rogers7caad772012-03-30 01:07:54 -0700955 uint32_t ref_spills = (1 << art::x86::EBP) | (1 << art::x86::ESI) | (1 << art::x86::EDI);
956 uint32_t arg_spills = (1 << art::x86::ECX) | (1 << art::x86::EDX) | (1 << art::x86::EBX);
957 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills : 0) |
958 (1 << art::x86::kNumberOfCpuRegisters); // fake return address callee save
959 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
960 1 /* Method* */) * kPointerSize, kStackAlignment);
961 method->SetFrameSizeInBytes(frame_size);
962 method->SetCoreSpillMask(core_spills);
Ian Rogersff1ed472011-09-20 13:46:24 -0700963 method->SetFpSpillMask(0);
964 } else {
965 UNIMPLEMENTED(FATAL);
966 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700967 return method.get();
Ian Rogersff1ed472011-09-20 13:46:24 -0700968}
969
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700970void Runtime::SetCalleeSaveMethod(Method* method, CalleeSaveType type) {
971 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
Elliott Hughes225f5a12012-06-11 11:23:48 -0700972 callee_save_methods_[type] = method;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700973}
974
jeffhao2692b572011-12-16 15:42:28 -0800975void Runtime::EnableMethodTracing(Trace* tracer) {
976 CHECK(!IsMethodTracingActive());
977 tracer_ = tracer;
978}
979
980void Runtime::DisableMethodTracing() {
981 CHECK(IsMethodTracingActive());
982 delete tracer_;
983 tracer_ = NULL;
984}
985
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800986const std::vector<const DexFile*>& Runtime::GetCompileTimeClassPath(const ClassLoader* class_loader) {
987 if (class_loader == NULL) {
988 return GetClassLinker()->GetBootClassPath();
989 }
990 CHECK(UseCompileTimeClassPath());
991 CompileTimeClassPaths::const_iterator it = compile_time_class_paths_.find(class_loader);
992 CHECK(it != compile_time_class_paths_.end());
993 return it->second;
994}
995
996void Runtime::SetCompileTimeClassPath(const ClassLoader* class_loader, std::vector<const DexFile*>& class_path) {
997 CHECK(!IsStarted());
998 use_compile_time_class_path_ = true;
Elliott Hughesa0e18062012-04-13 15:59:59 -0700999 compile_time_class_paths_.Put(class_loader, class_path);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001000}
1001
Carl Shapiro1fb86202011-06-27 17:43:13 -07001002} // namespace art