blob: 37c9dce86c6af6f3cba9db6ae5a87630e4412c01 [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),
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -070059 default_stack_size_(Thread::kDefaultStackSize),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080060 heap_(NULL),
Elliott Hughesc33a32b2011-10-11 18:18:07 -070061 monitor_list_(NULL),
Elliott Hughesdcc24742011-09-07 14:02:44 -070062 thread_list_(NULL),
63 intern_table_(NULL),
64 class_linker_(NULL),
65 signal_catcher_(NULL),
66 java_vm_(NULL),
Elliott Hughes225f5a12012-06-11 11:23:48 -070067 pre_allocated_OutOfMemoryError_(NULL),
Brian Carlstrom16192862011-09-12 17:50:06 -070068 jni_stub_array_(NULL),
Brian Carlstrome24fa612011-09-29 00:53:55 -070069 abstract_method_error_stub_array_(NULL),
Ian Rogers19846512012-02-24 11:42:47 -080070 resolution_method_(NULL),
Ian Rogers7c3757f2012-02-15 17:18:53 -080071 system_class_loader_(NULL),
Elliott Hughes6b355752012-01-13 16:49:08 -080072 shutting_down_(false),
Elliott Hughesdcc24742011-09-07 14:02:44 -070073 started_(false),
Mathieu Chartier7664f5c2012-06-08 18:15:32 -070074 finished_starting_(false),
Elliott Hughesdcc24742011-09-07 14:02:44 -070075 vfprintf_(NULL),
76 exit_(NULL),
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070077 abort_(NULL),
jeffhao2692b572011-12-16 15:42:28 -080078 stats_enabled_(false),
Ian Rogers776ac1f2012-04-13 23:36:36 -070079 method_trace_(0),
80 method_trace_file_size_(0),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080081 tracer_(NULL),
82 use_compile_time_class_path_(false) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -070083 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
84 resolution_stub_array_[i] = NULL;
85 }
86 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
Elliott Hughes225f5a12012-06-11 11:23:48 -070087 callee_save_methods_[i] = NULL;
Ian Rogers4f0d07c2011-10-06 23:38:47 -070088 }
Elliott Hughesdcc24742011-09-07 14:02:44 -070089}
90
Carl Shapiro61e019d2011-07-14 16:53:09 -070091Runtime::~Runtime() {
Elliott Hughes6b355752012-01-13 16:49:08 -080092 shutting_down_ = true;
93
jeffhaob5e81852012-03-12 11:15:45 -070094 if (IsMethodTracingActive()) {
95 Trace::Shutdown();
96 }
97
Mathieu Chartiera6399032012-06-11 18:49:50 -070098 // Make sure to let the GC complete if it is running.
99 {
100 ScopedHeapLock heap_lock;
101 heap_->WaitForConcurrentGcToComplete();
102 }
103
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700104 // Make sure our internal threads are dead before we start tearing down things they're using.
Elliott Hughese52e49b2012-04-02 16:05:44 -0700105 Dbg::StopJdwp();
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700106 delete signal_catcher_;
107
Elliott Hughes038a8062011-09-18 14:12:41 -0700108 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
Elliott Hughes93e74e82011-09-13 11:07:03 -0700109 delete thread_list_;
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700110 delete monitor_list_;
Elliott Hughes93e74e82011-09-13 11:07:03 -0700111
Carl Shapiro61e019d2011-07-14 16:53:09 -0700112 delete class_linker_;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800113 delete heap_;
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -0700114#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
Ian Rogers776ac1f2012-04-13 23:36:36 -0700115 verifier::MethodVerifier::DeleteInferredRegCategoryMaps();
Logan Chiendd361c92012-04-10 23:40:37 +0800116#endif
Ian Rogers776ac1f2012-04-13 23:36:36 -0700117 verifier::MethodVerifier::DeleteGcMaps();
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700118 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700119 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700120 Thread::Shutdown();
Elliott Hughes7c6169d2012-05-02 16:11:48 -0700121 QuasiAtomic::Shutdown();
122
Carl Shapiro4acf4642011-07-26 18:54:13 -0700123 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700124 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -0700125 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700126}
127
Elliott Hughescac6cc72011-11-03 20:31:21 -0700128static bool gAborting = false;
129
Elliott Hughese0918552011-10-28 17:18:29 -0700130struct AbortState {
131 void Dump(std::ostream& os) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700132 if (gAborting) {
133 os << "Runtime aborting --- recursively, so no thread-specific detail!\n";
134 return;
135 }
136 gAborting = true;
Elliott Hughese0918552011-10-28 17:18:29 -0700137 os << "Runtime aborting...\n";
Elliott Hughes6c7d2442012-02-01 18:40:47 -0800138 if (Runtime::Current() == NULL) {
139 os << "(Runtime does not yet exist!)\n";
140 return;
141 }
Elliott Hughese0918552011-10-28 17:18:29 -0700142 Thread* self = Thread::Current();
143 if (self == NULL) {
144 os << "(Aborting thread was not attached to runtime!)\n";
145 } else {
Elliott Hughes899e7892012-01-24 14:57:32 -0800146 self->Dump(os);
147 if (self->IsExceptionPending()) {
148 os << "Pending " << PrettyTypeOf(self->GetException()) << " on thread:\n"
149 << self->GetException()->Dump();
150 }
Elliott Hughese0918552011-10-28 17:18:29 -0700151 }
152 }
153};
154
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800155static Mutex& GetAbortLock() {
156 static Mutex abort_lock("abort lock");
157 return abort_lock;
158}
159
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700160void Runtime::Abort() {
Elliott Hughes131aef82012-01-27 11:53:35 -0800161 // Ensure that we don't have multiple threads trying to abort at once,
162 // which would result in significantly worse diagnostics.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800163 MutexLock mu(GetAbortLock());
Elliott Hughes131aef82012-01-27 11:53:35 -0800164
Elliott Hughesffe67362011-07-17 12:09:27 -0700165 // Get any pending output out of the way.
166 fflush(NULL);
167
168 // Many people have difficulty distinguish aborts from crashes,
169 // so be explicit.
Elliott Hughese0918552011-10-28 17:18:29 -0700170 AbortState state;
Elliott Hughes9ee5f9c2012-02-03 18:33:16 -0800171 LOG(INTERNAL_FATAL) << Dumpable<AbortState>(state);
Elliott Hughesffe67362011-07-17 12:09:27 -0700172
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700173 // Call the abort hook if we have one.
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700174 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700175 LOG(INTERNAL_FATAL) << "Calling abort hook...";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700176 Runtime::Current()->abort_();
177 // notreached
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700178 LOG(INTERNAL_FATAL) << "Unexpectedly returned from abort hook!";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700179 }
180
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700181#if defined(__BIONIC__)
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700182 // TODO: finish merging patches to fix abort(3) in bionic, then lose this!
183 // Bionic doesn't implement POSIX semantics for abort(3) in a multi-threaded
184 // process, so if we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -0700185 // receive SIGABRT. debuggerd dumps the stack trace of the main
186 // thread, whether or not that was the thread that failed. By
187 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -0700188 // fault in the current thread, and get a useful log from debuggerd.
Elliott Hughes81ff3182012-03-23 20:35:56 -0700189 // We can also trivially tell the difference between a crash and
Elliott Hughesffe67362011-07-17 12:09:27 -0700190 // a deliberate abort by looking at the fault address.
191 *reinterpret_cast<char*>(0xdeadd00d) = 38;
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700192#elif defined(__APPLE__)
193 // TODO: check that this actually gives good stack traces on the Mac!
194 pthread_kill(pthread_self(), SIGABRT);
195#else
196 // TODO: we ought to be able to use pthread_kill(3) here (or abort(3),
197 // which POSIX defines in terms of raise(3), which POSIX defines in terms
198 // of pthread_kill(3)). On Linux, though, libcorkscrew can't unwind through
199 // libpthread, which means the stacks we dump would be useless. Calling
200 // tgkill(2) directly avoids that.
201 syscall(__NR_tgkill, getpid(), GetTid(), SIGABRT);
202#endif
Elliott Hughesffe67362011-07-17 12:09:27 -0700203 // notreached
204}
205
Elliott Hughesbf86d042011-08-31 17:53:14 -0700206void Runtime::CallExitHook(jint status) {
207 if (exit_ != NULL) {
Elliott Hughes34e06962012-04-09 13:55:55 -0700208 ScopedThreadStateChange tsc(Thread::Current(), kNative);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700209 exit_(status);
210 LOG(WARNING) << "Exit hook returned instead of exiting!";
211 }
212}
213
Brian Carlstrom8a436592011-08-15 21:27:23 -0700214// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
215// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
216// [gG] gigabytes.
217//
218// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700219// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
220// of 1024.
221//
222// The spec says the -Xmx and -Xms options must be multiples of 1024. It
223// doesn't say anything about -Xss.
224//
225// Returns 0 (a useless size) if "s" is malformed or specifies a low or
226// non-evenly-divisible value.
227//
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800228size_t ParseMemoryOption(const char* s, size_t div) {
Brian Carlstrom8a436592011-08-15 21:27:23 -0700229 // strtoul accepts a leading [+-], which we don't want,
230 // so make sure our string starts with a decimal digit.
231 if (isdigit(*s)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700232 char* s2;
233 size_t val = strtoul(s, &s2, 10);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700234 if (s2 != s) {
235 // s2 should be pointing just after the number.
236 // If this is the end of the string, the user
237 // has specified a number of bytes. Otherwise,
238 // there should be exactly one more character
239 // that specifies a multiplier.
240 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700241 // The remainder of the string is either a single multiplier
242 // character, or nothing to indicate that the value is in
243 // bytes.
244 char c = *s2++;
245 if (*s2 == '\0') {
246 size_t mul;
247 if (c == '\0') {
248 mul = 1;
249 } else if (c == 'k' || c == 'K') {
250 mul = KB;
251 } else if (c == 'm' || c == 'M') {
252 mul = MB;
253 } else if (c == 'g' || c == 'G') {
254 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700255 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700256 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700257 return 0;
258 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700259
260 if (val <= std::numeric_limits<size_t>::max() / mul) {
261 val *= mul;
262 } else {
263 // Clamp to a multiple of 1024.
264 val = std::numeric_limits<size_t>::max() & ~(1024-1);
265 }
266 } else {
267 // There's more than one character after the numeric part.
268 return 0;
269 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700270 }
271 // The man page says that a -Xm value must be a multiple of 1024.
272 if (val % div == 0) {
273 return val;
274 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700275 }
276 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700277 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700278}
279
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800280size_t ParseIntegerOrDie(const std::string& s) {
281 std::string::size_type colon = s.find(':');
282 if (colon == std::string::npos) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700283 LOG(FATAL) << "Missing integer: " << s;
284 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800285 const char* begin = &s[colon + 1];
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700286 char* end;
287 size_t result = strtoul(begin, &end, 10);
288 if (begin == end || *end != '\0') {
289 LOG(FATAL) << "Failed to parse integer in: " << s;
290 }
291 return result;
292}
293
Elliott Hughes0af55432011-08-17 18:37:28 -0700294void LoadJniLibrary(JavaVMExt* vm, const char* name) {
Elliott Hughes6cc332e2012-01-20 22:59:20 -0800295 std::string mapped_name(StringPrintf(OS_SHARED_LIB_FORMAT_STR, name));
Elliott Hughes75770752011-08-24 17:52:38 -0700296 std::string reason;
297 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700298 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
299 << reason;
300 }
301}
302
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700303Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700304 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800305 const char* boot_class_path_string = getenv("BOOTCLASSPATH");
306 if (boot_class_path_string != NULL) {
307 parsed->boot_class_path_string_ = boot_class_path_string;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700308 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800309 const char* class_path_string = getenv("CLASSPATH");
310 if (class_path_string != NULL) {
311 parsed->class_path_string_ = class_path_string;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700312 }
Elliott Hughes67d92002012-03-26 15:08:51 -0700313 // -Xcheck:jni is off by default for regular builds but on by default in debug builds.
314 parsed->check_jni_ = kIsDebugBuild;
Elliott Hughes85d15452011-09-16 17:33:01 -0700315
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700316 parsed->heap_initial_size_ = Heap::kInitialSize;
317 parsed->heap_maximum_size_ = Heap::kMaximumSize;
jeffhaoc1160702011-10-27 15:48:45 -0700318 parsed->heap_growth_limit_ = 0; // 0 means no growth limit
Brian Carlstromf734cf52011-08-17 16:28:14 -0700319 parsed->stack_size_ = Thread::kDefaultStackSize;
320
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800321 parsed->is_compiler_ = false;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700322 parsed->is_zygote_ = false;
323
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700324 parsed->jni_globals_max_ = 0;
Elliott Hughesfc861622011-10-17 17:57:47 -0700325 parsed->lock_profiling_threshold_ = 0;
326 parsed->hook_is_sensitive_thread_ = NULL;
327
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700328 parsed->hook_vfprintf_ = vfprintf;
329 parsed->hook_exit_ = exit;
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700330 parsed->hook_abort_ = NULL; // We don't call abort(3) by default; see Runtime::Abort.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700331
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800332// gLogVerbosity.class_linker = true; // TODO: don't check this in!
333// gLogVerbosity.compiler = true; // TODO: don't check this in!
334// gLogVerbosity.heap = true; // TODO: don't check this in!
335// gLogVerbosity.gc = true; // TODO: don't check this in!
336// gLogVerbosity.jdwp = true; // TODO: don't check this in!
337// gLogVerbosity.jni = true; // TODO: don't check this in!
338// gLogVerbosity.monitor = true; // TODO: don't check this in!
339// gLogVerbosity.startup = true; // TODO: don't check this in!
340// gLogVerbosity.third_party_jni = true; // TODO: don't check this in!
341// gLogVerbosity.threads = true; // TODO: don't check this in!
342
jeffhaob5e81852012-03-12 11:15:45 -0700343 parsed->method_trace_ = false;
344 parsed->method_trace_file_ = "/data/method-trace-file.bin";
345 parsed->method_trace_file_size_ = 10 * MB;
346
Brian Carlstrom8a436592011-08-15 21:27:23 -0700347 for (size_t i = 0; i < options.size(); ++i) {
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800348 const std::string option(options[i].first);
Brian Carlstrom0796af02011-10-12 14:31:45 -0700349 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700350 LOG(INFO) << "option[" << i << "]=" << option;
351 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800352 if (StartsWith(option, "-Xbootclasspath:")) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800353 parsed->boot_class_path_string_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700354 } else if (option == "-classpath" || option == "-cp") {
355 // TODO: support -Djava.class.path
356 i++;
357 if (i == options.size()) {
358 // TODO: usage
359 LOG(FATAL) << "Missing required class path value for " << option;
360 return NULL;
361 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800362 const StringPiece& value = options[i].first;
363 parsed->class_path_string_ = value.data();
364 } else if (option == "bootclasspath") {
365 parsed->boot_class_path_
366 = reinterpret_cast<const std::vector<const DexFile*>*>(options[i].second);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800367 } else if (StartsWith(option, "-Ximage:")) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800368 parsed->image_ = option.substr(strlen("-Ximage:")).data();
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800369 } else if (StartsWith(option, "-Xcheck:jni")) {
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700370 parsed->check_jni_ = true;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800371 } else if (StartsWith(option, "-Xrunjdwp:") || StartsWith(option, "-agentlib:jdwp=")) {
372 std::string tail(option.substr(option[1] == 'X' ? 10 : 15));
Elliott Hughes3bb81562011-10-21 18:52:59 -0700373 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
374 LOG(FATAL) << "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
375 << "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n";
376 return NULL;
377 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800378 } else if (StartsWith(option, "-Xms")) {
379 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).c_str(), 1024);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700380 if (size == 0) {
381 if (ignore_unrecognized) {
382 continue;
383 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700384 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700385 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700386 return NULL;
387 }
388 parsed->heap_initial_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800389 } else if (StartsWith(option, "-Xmx")) {
390 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).c_str(), 1024);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700391 if (size == 0) {
392 if (ignore_unrecognized) {
393 continue;
394 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700395 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700396 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700397 return NULL;
398 }
399 parsed->heap_maximum_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800400 } else if (StartsWith(option, "-XX:HeapGrowthLimit=")) {
401 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).c_str(), 1024);
jeffhaoc1160702011-10-27 15:48:45 -0700402 if (size == 0) {
403 if (ignore_unrecognized) {
404 continue;
405 }
406 // TODO: usage
407 LOG(FATAL) << "Failed to parse " << option;
408 return NULL;
409 }
410 parsed->heap_growth_limit_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800411 } else if (StartsWith(option, "-Xss")) {
412 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).c_str(), 1);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700413 if (size == 0) {
414 if (ignore_unrecognized) {
415 continue;
416 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700417 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700418 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700419 return NULL;
420 }
421 parsed->stack_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800422 } else if (StartsWith(option, "-D")) {
423 parsed->properties_.push_back(option.substr(strlen("-D")));
424 } else if (StartsWith(option, "-Xjnitrace:")) {
425 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:"));
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700426 } else if (option == "compiler") {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800427 parsed->is_compiler_ = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700428 } else if (option == "-Xzygote") {
429 parsed->is_zygote_ = true;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800430 } else if (StartsWith(option, "-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700431 std::vector<std::string> verbose_options;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800432 Split(option.substr(strlen("-verbose:")), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700433 for (size_t i = 0; i < verbose_options.size(); ++i) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800434 if (verbose_options[i] == "class") {
435 gLogVerbosity.class_linker = true;
436 } else if (verbose_options[i] == "compiler") {
437 gLogVerbosity.compiler = true;
438 } else if (verbose_options[i] == "heap") {
439 gLogVerbosity.heap = true;
440 } else if (verbose_options[i] == "gc") {
441 gLogVerbosity.gc = true;
442 } else if (verbose_options[i] == "jdwp") {
443 gLogVerbosity.jdwp = true;
444 } else if (verbose_options[i] == "jni") {
445 gLogVerbosity.jni = true;
446 } else if (verbose_options[i] == "monitor") {
447 gLogVerbosity.monitor = true;
448 } else if (verbose_options[i] == "startup") {
449 gLogVerbosity.startup = true;
450 } else if (verbose_options[i] == "third-party-jni") {
451 gLogVerbosity.third_party_jni = true;
452 } else if (verbose_options[i] == "threads") {
453 gLogVerbosity.threads = true;
454 } else {
455 LOG(WARNING) << "Ignoring unknown -verbose option: " << verbose_options[i];
456 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700457 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800458 } else if (StartsWith(option, "-Xjnigreflimit:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700459 parsed->jni_globals_max_ = ParseIntegerOrDie(option);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800460 } else if (StartsWith(option, "-Xlockprofthreshold:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700461 parsed->lock_profiling_threshold_ = ParseIntegerOrDie(option);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800462 } else if (StartsWith(option, "-Xstacktracefile:")) {
Elliott Hughes67d92002012-03-26 15:08:51 -0700463 if (kIsDebugBuild) {
464 // Ignore the zygote and always show stack traces in debug builds.
465 } else {
466 parsed->stack_trace_file_ = option.substr(strlen("-Xstacktracefile:"));
467 }
Elliott Hughesfc861622011-10-17 17:57:47 -0700468 } else if (option == "sensitiveThread") {
469 parsed->hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(options[i].second);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700470 } else if (option == "vfprintf") {
471 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
472 } else if (option == "exit") {
473 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
474 } else if (option == "abort") {
475 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700476 } else if (option == "host-prefix") {
477 parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second);
Elliott Hughesb3e66df2012-01-12 14:49:18 -0800478 } else if (option == "-Xgenregmap" || option == "-Xgc:precise") {
479 // We silently ignore these for backwards compatibility.
jeffhaob5e81852012-03-12 11:15:45 -0700480 } else if (option == "-Xmethod-trace") {
481 parsed->method_trace_ = true;
482 } else if (StartsWith(option, "-Xmethod-trace-file:")) {
483 parsed->method_trace_file_ = option.substr(strlen("-Xmethod-trace-file:"));
484 } else if (StartsWith(option, "-Xmethod-trace-file-size:")) {
485 parsed->method_trace_file_size_ = ParseIntegerOrDie(option);
Elliott Hughese119a362012-05-22 17:37:06 -0700486 } else if (option == "-Xprofile:threadcpuclock") {
487 Trace::SetDefaultClockSource(kProfilerClockSourceThreadCpu);
488 } else if (option == "-Xprofile:wallclock") {
489 Trace::SetDefaultClockSource(kProfilerClockSourceWall);
490 } else if (option == "-Xprofile:dualclock") {
491 Trace::SetDefaultClockSource(kProfilerClockSourceDual);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700492 } else {
493 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700494 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700495 LOG(ERROR) << "Unrecognized option " << option;
496 // TODO: this should exit, but for now tolerate unknown options
497 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700498 }
499 }
500 }
501
Brian Carlstrom223f20f2012-02-04 23:06:55 -0800502 if (!parsed->is_compiler_ && parsed->image_.empty()) {
503 parsed->image_ += GetAndroidRoot();
504 parsed->image_ += "/framework/boot.art";
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700505 }
jeffhaoc1160702011-10-27 15:48:45 -0700506 if (parsed->heap_growth_limit_ == 0) {
507 parsed->heap_growth_limit_ = parsed->heap_maximum_size_;
508 }
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700509
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700510 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700511}
512
513Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700514 // TODO: acquire a static mutex on Runtime to avoid racing.
515 if (Runtime::instance_ != NULL) {
516 return NULL;
517 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700518 instance_ = new Runtime;
519 if (!instance_->Init(options, ignore_unrecognized)) {
520 delete instance_;
521 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700522 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700523 return instance_;
524}
Elliott Hughes0af55432011-08-17 18:37:28 -0700525
Brian Carlstromaded5f72011-10-07 17:15:04 -0700526void CreateSystemClassLoader() {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800527 if (Runtime::Current()->UseCompileTimeClassPath()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700528 return;
529 }
530
531 Thread* self = Thread::Current();
532
533 // Must be in the kNative state for calling native methods.
Elliott Hughes34e06962012-04-09 13:55:55 -0700534 CHECK_EQ(self->GetState(), kNative);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700535
536 JNIEnv* env = self->GetJniEnv();
Elliott Hugheseac76672012-05-24 21:56:51 -0700537 jmethodID getSystemClassLoader = env->GetStaticMethodID(WellKnownClasses::java_lang_ClassLoader,
Brian Carlstromdf143242011-10-10 18:05:34 -0700538 "getSystemClassLoader",
539 "()Ljava/lang/ClassLoader;");
540 CHECK(getSystemClassLoader != NULL);
Elliott Hugheseac76672012-05-24 21:56:51 -0700541 ScopedLocalRef<jobject> class_loader(env, env->CallStaticObjectMethod(WellKnownClasses::java_lang_ClassLoader,
Brian Carlstromdf143242011-10-10 18:05:34 -0700542 getSystemClassLoader));
543 CHECK(class_loader.get() != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700544
Brian Carlstromdf143242011-10-10 18:05:34 -0700545 Thread::Current()->SetClassLoaderOverride(Decode<ClassLoader*>(env, class_loader.get()));
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500546
Elliott Hugheseac76672012-05-24 21:56:51 -0700547 jfieldID contextClassLoader = env->GetFieldID(WellKnownClasses::java_lang_Thread,
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500548 "contextClassLoader",
549 "Ljava/lang/ClassLoader;");
550 CHECK(contextClassLoader != NULL);
551 ScopedLocalRef<jobject> self_jobject(env, AddLocalReference<jobject>(env, self->GetPeer()));
552 env->SetObjectField(self_jobject.get(), contextClassLoader, class_loader.get());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700553}
554
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700555void Runtime::Start() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800556 VLOG(startup) << "Runtime::Start entering";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700557
558 CHECK(host_prefix_.empty()) << host_prefix_;
559
Elliott Hughes225f5a12012-06-11 11:23:48 -0700560 // Relocate the OatFiles (ELF images).
Logan Chien0c717dd2012-03-28 18:31:07 +0800561 class_linker_->RelocateExecutable();
562
Elliott Hughes225f5a12012-06-11 11:23:48 -0700563 // Restore main thread state to kNative as expected by native code.
564 Thread* self = Thread::Current();
565 self->SetState(kNative);
566
567 // Pre-allocate an OutOfMemoryError for the double-OOME case.
568 self->ThrowNewException("Ljava/lang/OutOfMemoryError;",
569 "OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack available");
570 pre_allocated_OutOfMemoryError_ = self->GetException();
571 self->ClearException();
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700572
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700573 started_ = true;
574
Brian Carlstromae826982011-11-09 01:33:42 -0800575 // InitNativeMethods needs to be after started_ so that the classes
576 // it touches will have methods linked to the oat file if necessary.
577 InitNativeMethods();
578
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500579 Thread::FinishStartup();
580
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700581 if (!is_zygote_) {
582 DidForkFromZygote();
583 }
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700584
Elliott Hughes85d15452011-09-16 17:33:01 -0700585 StartDaemonThreads();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700586
Brian Carlstromaded5f72011-10-07 17:15:04 -0700587 CreateSystemClassLoader();
588
Elliott Hughes225f5a12012-06-11 11:23:48 -0700589 self->GetJniEnv()->locals.AssertEmpty();
Elliott Hughes726079d2011-10-07 18:43:44 -0700590
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800591 VLOG(startup) << "Runtime::Start exiting";
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700592
593 finished_starting_ = true;
Elliott Hughes85d15452011-09-16 17:33:01 -0700594}
595
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700596void Runtime::DidForkFromZygote() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700597 is_zygote_ = false;
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700598
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700599 StartSignalCatcher();
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700600
601 // Start the JDWP thread. If the command-line debugger flags specified "suspend=y",
602 // this will pause the runtime, so we probably want this to come last.
603 Dbg::StartJdwp();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700604}
605
606void Runtime::StartSignalCatcher() {
607 if (!is_zygote_) {
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700608 signal_catcher_ = new SignalCatcher(stack_trace_file_);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700609 }
610}
611
Elliott Hughes85d15452011-09-16 17:33:01 -0700612void Runtime::StartDaemonThreads() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800613 VLOG(startup) << "Runtime::StartDaemonThreads entering";
Elliott Hughes85d15452011-09-16 17:33:01 -0700614
Elliott Hughes719b3232011-09-25 17:42:19 -0700615 Thread* self = Thread::Current();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700616
617 // Must be in the kNative state for calling native methods.
Elliott Hughes34e06962012-04-09 13:55:55 -0700618 CHECK_EQ(self->GetState(), kNative);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700619
Elliott Hughes719b3232011-09-25 17:42:19 -0700620 JNIEnv* env = self->GetJniEnv();
Elliott Hugheseac76672012-05-24 21:56:51 -0700621 env->CallStaticVoidMethod(WellKnownClasses::java_lang_Daemons, WellKnownClasses::java_lang_Daemons_start);
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800622 CHECK(!env->ExceptionCheck());
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700623
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800624 VLOG(startup) << "Runtime::StartDaemonThreads exiting";
Carl Shapiro61e019d2011-07-14 16:53:09 -0700625}
626
Elliott Hughes0af55432011-08-17 18:37:28 -0700627bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700628 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700629
Elliott Hughes90a33692011-08-30 13:27:07 -0700630 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
631 if (options.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700632 LOG(ERROR) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700633 return false;
634 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800635 VLOG(startup) << "Runtime::Init -verbose:startup enabled";
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700636
Elliott Hughes7c6169d2012-05-02 16:11:48 -0700637 QuasiAtomic::Startup();
638
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700639 SetJniGlobalsMax(options->jni_globals_max_);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800640 Monitor::Init(options->lock_profiling_threshold_, options->hook_is_sensitive_thread_);
Elliott Hughes32d6e1e2011-10-11 14:47:44 -0700641
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700642 host_prefix_ = options->host_prefix_;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800643 boot_class_path_string_ = options->boot_class_path_string_;
644 class_path_string_ = options->class_path_string_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700645 properties_ = options->properties_;
646
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800647 is_compiler_ = options->is_compiler_;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700648 is_zygote_ = options->is_zygote_;
649
Elliott Hughes0af55432011-08-17 18:37:28 -0700650 vfprintf_ = options->hook_vfprintf_;
651 exit_ = options->hook_exit_;
652 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700653
Elliott Hughesbe759c62011-09-08 19:38:21 -0700654 default_stack_size_ = options->stack_size_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700655 stack_trace_file_ = options->stack_trace_file_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700656
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700657 monitor_list_ = new MonitorList;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800658 thread_list_ = new ThreadList;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700659 intern_table_ = new InternTable;
660
Ian Rogers776ac1f2012-04-13 23:36:36 -0700661 verifier::MethodVerifier::InitGcMaps();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800662
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -0700663#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
Ian Rogers776ac1f2012-04-13 23:36:36 -0700664 verifier::MethodVerifier::InitInferredRegCategoryMaps();
Logan Chiendd361c92012-04-10 23:40:37 +0800665#endif
666
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800667 heap_ = new Heap(options->heap_initial_size_,
668 options->heap_growth_limit_,
669 options->heap_maximum_size_,
670 options->image_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700671
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700672 BlockSignals();
Elliott Hughes457005c2012-04-16 13:54:25 -0700673 InitPlatformSignalHandlers();
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700674
Elliott Hughesa0957642011-09-02 14:27:33 -0700675 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700676
Elliott Hughesbe759c62011-09-08 19:38:21 -0700677 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700678
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700679 // ClassLinker needs an attached thread, but we can't fully attach a thread
Elliott Hughes462c9442012-03-23 18:47:50 -0700680 // without creating objects. We can't supply a thread group yet; it will be fixed later.
681 Thread::Attach("main", false, NULL);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700682
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700683 // Set us to runnable so tools using a runtime can allocate and GC by default
Elliott Hughes34e06962012-04-09 13:55:55 -0700684 Thread::Current()->SetState(kRunnable);
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700685
Ian Rogers141d6222012-04-05 12:23:06 -0700686 // Now we're attached, we can take the heap lock and validate the heap.
687 GetHeap()->EnableObjectValidation();
688
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800689 CHECK_GE(GetHeap()->GetSpaces().size(), 1U);
690 if (GetHeap()->GetSpaces()[0]->IsImageSpace()) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800691 class_linker_ = ClassLinker::CreateFromImage(intern_table_);
692 } else {
693 CHECK(options->boot_class_path_ != NULL);
694 CHECK_NE(options->boot_class_path_->size(), 0U);
695 class_linker_ = ClassLinker::CreateFromCompiler(*options->boot_class_path_, intern_table_);
696 }
697 CHECK(class_linker_ != NULL);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700698
jeffhaob5e81852012-03-12 11:15:45 -0700699 method_trace_ = options->method_trace_;
700 method_trace_file_ = options->method_trace_file_;
701 method_trace_file_size_ = options->method_trace_file_size_;
702
703 if (options->method_trace_) {
704 Trace::Start(options->method_trace_file_.c_str(), -1, options->method_trace_file_size_, 0, false);
705 }
706
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800707 VLOG(startup) << "Runtime::Init exiting";
Carl Shapiro1fb86202011-06-27 17:43:13 -0700708 return true;
709}
710
Elliott Hughes038a8062011-09-18 14:12:41 -0700711void Runtime::InitNativeMethods() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800712 VLOG(startup) << "Runtime::InitNativeMethods entering";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700713 Thread* self = Thread::Current();
714 JNIEnv* env = self->GetJniEnv();
715
Elliott Hughes418d20f2011-09-22 14:00:39 -0700716 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Elliott Hughes34e06962012-04-09 13:55:55 -0700717 CHECK_EQ(self->GetState(), kNative);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700718
Elliott Hughes418d20f2011-09-22 14:00:39 -0700719 // First set up JniConstants, which is used by both the runtime's built-in native
720 // methods and libcore.
Elliott Hughesfea966e2011-09-22 10:26:20 -0700721 JniConstants::init(env);
Elliott Hugheseac76672012-05-24 21:56:51 -0700722 WellKnownClasses::Init(env);
Elliott Hughesfea966e2011-09-22 10:26:20 -0700723
Elliott Hughes418d20f2011-09-22 14:00:39 -0700724 // Then set up the native methods provided by the runtime itself.
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700725 RegisterRuntimeNativeMethods(env);
726
Elliott Hughes418d20f2011-09-22 14:00:39 -0700727 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
728 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
729 // the library that implements System.loadLibrary!
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700730 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800731 VLOG(startup) << "Runtime::InitNativeMethods exiting";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700732}
733
734void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
735#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Ian Rogers5167c972012-02-03 10:41:20 -0800736 // Register Throwable first so that registration of other native methods can throw exceptions
737 REGISTER(register_java_lang_Throwable);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700738 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700739 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700740 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700741 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700742 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700743 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700744 REGISTER(register_java_lang_Object);
745 REGISTER(register_java_lang_Runtime);
746 REGISTER(register_java_lang_String);
747 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700748 REGISTER(register_java_lang_Thread);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700749 REGISTER(register_java_lang_VMClassLoader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700750 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700751 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700752 REGISTER(register_java_lang_reflect_Field);
753 REGISTER(register_java_lang_reflect_Method);
Jesse Wilson95caa792011-10-12 18:14:17 -0400754 REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700755 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700756 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700757 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700758 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700759#undef REGISTER
760}
761
Elliott Hughesc967f782012-04-16 10:23:15 -0700762void Runtime::DumpForSigQuit(std::ostream& os) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700763 GetClassLinker()->DumpForSigQuit(os);
764 GetInternTable()->DumpForSigQuit(os);
Elliott Hughesae80b492012-04-24 10:43:17 -0700765 GetJavaVM()->DumpForSigQuit(os);
Elliott Hughesc967f782012-04-16 10:23:15 -0700766 GetHeap()->DumpForSigQuit(os);
Elliott Hughes42ee1422011-09-06 12:33:32 -0700767 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700768
Elliott Hughesc967f782012-04-16 10:23:15 -0700769 thread_list_->DumpForSigQuit(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700770}
771
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800772void Runtime::DumpLockHolders(std::ostream& os) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800773 pid_t heap_lock_owner = GetHeap()->GetLockOwner();
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800774 pid_t thread_list_lock_owner = GetThreadList()->GetLockOwner();
775 pid_t classes_lock_owner = GetClassLinker()->GetClassesLockOwner();
776 pid_t dex_lock_owner = GetClassLinker()->GetDexLockOwner();
777 if ((heap_lock_owner | thread_list_lock_owner | classes_lock_owner | dex_lock_owner) != 0) {
778 os << "Heap lock owner tid: " << heap_lock_owner << "\n"
779 << "ThreadList lock owner tid: " << thread_list_lock_owner << "\n"
780 << "ClassLinker classes lock owner tid: " << classes_lock_owner << "\n"
781 << "ClassLinker dex lock owner tid: " << dex_lock_owner << "\n";
782 }
783}
784
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700785void Runtime::SetStatsEnabled(bool new_state) {
786 if (new_state == true) {
787 GetStats()->Clear(~0);
788 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
789 Thread::Current()->GetStats()->Clear(~0);
790 }
791 stats_enabled_ = new_state;
792}
793
794void Runtime::ResetStats(int kinds) {
795 GetStats()->Clear(kinds & 0xffff);
796 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
797 Thread::Current()->GetStats()->Clear(kinds >> 16);
798}
799
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700800int32_t Runtime::GetStat(int kind) {
801 RuntimeStats* stats;
802 if (kind < (1<<16)) {
803 stats = GetStats();
804 } else {
805 stats = Thread::Current()->GetStats();
806 kind >>= 16;
807 }
808 switch (kind) {
809 case KIND_ALLOCATED_OBJECTS:
810 return stats->allocated_objects;
811 case KIND_ALLOCATED_BYTES:
812 return stats->allocated_bytes;
813 case KIND_FREED_OBJECTS:
814 return stats->freed_objects;
815 case KIND_FREED_BYTES:
816 return stats->freed_bytes;
817 case KIND_GC_INVOCATIONS:
818 return stats->gc_for_alloc_count;
819 case KIND_CLASS_INIT_COUNT:
820 return stats->class_init_count;
821 case KIND_CLASS_INIT_TIME:
822 // Convert ns to us, reduce to 32 bits.
Elliott Hughes398f64b2012-03-26 18:05:48 -0700823 return static_cast<int>(stats->class_init_time_ns / 1000);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700824 case KIND_EXT_ALLOCATED_OBJECTS:
825 case KIND_EXT_ALLOCATED_BYTES:
826 case KIND_EXT_FREED_OBJECTS:
827 case KIND_EXT_FREED_BYTES:
828 return 0; // backward compatibility
829 default:
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700830 LOG(FATAL) << "Unknown statistic " << kind;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700831 return -1; // unreachable
832 }
833}
834
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700835void Runtime::BlockSignals() {
Elliott Hughes457005c2012-04-16 13:54:25 -0700836 SignalSet signals;
837 signals.Add(SIGPIPE);
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700838 // SIGQUIT is used to dump the runtime's state (including stack traces).
Elliott Hughes457005c2012-04-16 13:54:25 -0700839 signals.Add(SIGQUIT);
Elliott Hughes08795042012-04-03 14:48:52 -0700840 // SIGUSR1 is used to initiate a GC.
Elliott Hughes457005c2012-04-16 13:54:25 -0700841 signals.Add(SIGUSR1);
842 signals.Block();
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700843}
844
Elliott Hughes462c9442012-03-23 18:47:50 -0700845void Runtime::AttachCurrentThread(const char* thread_name, bool as_daemon, Object* thread_group) {
846 Thread::Attach(thread_name, as_daemon, thread_group);
Elliott Hughes22869a92012-03-27 14:08:24 -0700847 if (thread_name == NULL) {
848 LOG(WARNING) << *Thread::Current() << " attached without supplying a name";
849 }
Carl Shapiro61e019d2011-07-14 16:53:09 -0700850}
851
Elliott Hughesd92bec42011-09-02 17:04:36 -0700852void Runtime::DetachCurrentThread() {
Brian Carlstrom4d571432012-05-16 00:21:41 -0700853 Thread* self = Thread::Current();
854 if (self == NULL) {
855 LOG(FATAL) << "attempting to detach thread that is not attached";
856 }
857 if (self->GetTopOfStack().GetSP() != NULL) {
Elliott Hughes22869a92012-03-27 14:08:24 -0700858 LOG(FATAL) << *Thread::Current() << " attempting to detach while still running code";
859 }
Elliott Hughes02b48d12011-09-07 17:15:51 -0700860 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700861}
862
Brian Carlstrome24fa612011-09-29 00:53:55 -0700863void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700864 Dbg::VisitRoots(visitor, arg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700865 class_linker_->VisitRoots(visitor, arg);
866 intern_table_->VisitRoots(visitor, arg);
867 java_vm_->VisitRoots(visitor, arg);
868 thread_list_->VisitRoots(visitor, arg);
Elliott Hughes225f5a12012-06-11 11:23:48 -0700869 if (pre_allocated_OutOfMemoryError_ != NULL) {
870 visitor(pre_allocated_OutOfMemoryError_, arg);
871 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700872 visitor(jni_stub_array_, arg);
873 visitor(abstract_method_error_stub_array_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700874 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
875 visitor(resolution_stub_array_[i], arg);
876 }
Ian Rogers19846512012-02-24 11:42:47 -0800877 visitor(resolution_method_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700878 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
Elliott Hughes225f5a12012-06-11 11:23:48 -0700879 visitor(callee_save_methods_[i], arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700880 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700881}
882
Ian Rogers169c9a72011-11-13 20:13:17 -0800883void Runtime::SetJniDlsymLookupStub(ByteArray* jni_stub_array) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700884 CHECK(jni_stub_array != NULL) << " jni_stub_array=" << jni_stub_array;
885 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array)
886 << "jni_stub_array_=" << jni_stub_array_ << " jni_stub_array=" << jni_stub_array;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700887 jni_stub_array_ = jni_stub_array;
888}
889
Brian Carlstrome24fa612011-09-29 00:53:55 -0700890void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) {
891 CHECK(abstract_method_error_stub_array != NULL);
892 CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array);
893 abstract_method_error_stub_array_ = abstract_method_error_stub_array;
894}
895
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700896void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700897 CHECK(resolution_stub_array != NULL);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700898 CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array);
899 resolution_stub_array_[type] = resolution_stub_array;
Ian Rogersad25ac52011-10-04 19:13:33 -0700900}
901
Ian Rogers19846512012-02-24 11:42:47 -0800902Method* Runtime::CreateResolutionMethod() {
903 Class* method_class = Method::GetMethodClass();
904 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
905 method->SetDeclaringClass(method_class);
906 // TODO: use a special method for resolution method saves
907 method->SetDexMethodIndex(DexFile::kDexNoIndex16);
908 ByteArray* unknown_resolution_stub = GetResolutionStubArray(kUnknownMethod);
909 CHECK(unknown_resolution_stub != NULL);
910 method->SetCode(unknown_resolution_stub->GetData());
911 return method.get();
912}
913
Brian Carlstromae826982011-11-09 01:33:42 -0800914Method* Runtime::CreateCalleeSaveMethod(InstructionSet instruction_set, CalleeSaveType type) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700915 Class* method_class = Method::GetMethodClass();
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700916 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
Ian Rogersff1ed472011-09-20 13:46:24 -0700917 method->SetDeclaringClass(method_class);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800918 // TODO: use a special method for callee saves
Ian Rogers19846512012-02-24 11:42:47 -0800919 method->SetDexMethodIndex(DexFile::kDexNoIndex16);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700920 method->SetCode(NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800921 if ((instruction_set == kThumb2) || (instruction_set == kArm)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700922 uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6) | (1 << art::arm::R7) |
923 (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11);
924 uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3);
925 uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9);
926 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills :0) |
927 (type == kSaveAll ? all_spills :0) | (1 << art::arm::LR);
928 uint32_t fp_all_spills = (1 << art::arm::S0) | (1 << art::arm::S1) | (1 << art::arm::S2) |
929 (1 << art::arm::S3) | (1 << art::arm::S4) | (1 << art::arm::S5) |
930 (1 << art::arm::S6) | (1 << art::arm::S7) | (1 << art::arm::S8) |
931 (1 << art::arm::S9) | (1 << art::arm::S10) | (1 << art::arm::S11) |
932 (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) |
933 (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) |
934 (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) |
935 (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) |
936 (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) |
937 (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
938 (1 << art::arm::S30) | (1 << art::arm::S31);
939 uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
940 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
941 __builtin_popcount(fp_spills) /* fprs */ +
942 1 /* Method* */) * kPointerSize, kStackAlignment);
Ian Rogers15fdb8c2011-09-25 15:45:07 -0700943 method->SetFrameSizeInBytes(frame_size);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700944 method->SetCoreSpillMask(core_spills);
945 method->SetFpSpillMask(fp_spills);
Brian Carlstromae826982011-11-09 01:33:42 -0800946 } else if (instruction_set == kX86) {
Ian Rogers7caad772012-03-30 01:07:54 -0700947 uint32_t ref_spills = (1 << art::x86::EBP) | (1 << art::x86::ESI) | (1 << art::x86::EDI);
948 uint32_t arg_spills = (1 << art::x86::ECX) | (1 << art::x86::EDX) | (1 << art::x86::EBX);
949 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills : 0) |
950 (1 << art::x86::kNumberOfCpuRegisters); // fake return address callee save
951 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
952 1 /* Method* */) * kPointerSize, kStackAlignment);
953 method->SetFrameSizeInBytes(frame_size);
954 method->SetCoreSpillMask(core_spills);
Ian Rogersff1ed472011-09-20 13:46:24 -0700955 method->SetFpSpillMask(0);
956 } else {
957 UNIMPLEMENTED(FATAL);
958 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700959 return method.get();
Ian Rogersff1ed472011-09-20 13:46:24 -0700960}
961
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700962void Runtime::SetCalleeSaveMethod(Method* method, CalleeSaveType type) {
963 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
Elliott Hughes225f5a12012-06-11 11:23:48 -0700964 callee_save_methods_[type] = method;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700965}
966
jeffhao2692b572011-12-16 15:42:28 -0800967void Runtime::EnableMethodTracing(Trace* tracer) {
968 CHECK(!IsMethodTracingActive());
969 tracer_ = tracer;
970}
971
972void Runtime::DisableMethodTracing() {
973 CHECK(IsMethodTracingActive());
974 delete tracer_;
975 tracer_ = NULL;
976}
977
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800978const std::vector<const DexFile*>& Runtime::GetCompileTimeClassPath(const ClassLoader* class_loader) {
979 if (class_loader == NULL) {
980 return GetClassLinker()->GetBootClassPath();
981 }
982 CHECK(UseCompileTimeClassPath());
983 CompileTimeClassPaths::const_iterator it = compile_time_class_paths_.find(class_loader);
984 CHECK(it != compile_time_class_paths_.end());
985 return it->second;
986}
987
988void Runtime::SetCompileTimeClassPath(const ClassLoader* class_loader, std::vector<const DexFile*>& class_path) {
989 CHECK(!IsStarted());
990 use_compile_time_class_path_ = true;
Elliott Hughesa0e18062012-04-13 15:59:59 -0700991 compile_time_class_paths_.Put(class_loader, class_path);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800992}
993
Carl Shapiro1fb86202011-06-27 17:43:13 -0700994} // namespace art