blob: 1a6695167eab20d4918138076f30b6f883b09622 [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"
Elliott Hughes726079d2011-10-07 18:43:44 -070038#include "ScopedLocalRef.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070039#include "signal_catcher.h"
Elliott Hughes457005c2012-04-16 13:54:25 -070040#include "signal_set.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070041#include "space.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070042#include "thread.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070043#include "thread_list.h"
jeffhaob5e81852012-03-12 11:15:45 -070044#include "trace.h"
Elliott Hughes3bb81562011-10-21 18:52:59 -070045#include "UniquePtr.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070046#include "verifier/method_verifier.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070047#include "well_known_classes.h"
Carl Shapiro61e019d2011-07-14 16:53:09 -070048
Elliott Hugheseac76672012-05-24 21:56:51 -070049#include "JniConstants.h" // Last to avoid LOG redefinition in ics-mr1-plus-art.
Elliott Hughes90a33692011-08-30 13:27:07 -070050
Carl Shapiro1fb86202011-06-27 17:43:13 -070051namespace art {
52
Carl Shapiro2ed144c2011-07-26 16:52:08 -070053Runtime* Runtime::instance_ = NULL;
54
Elliott Hughesdcc24742011-09-07 14:02:44 -070055Runtime::Runtime()
Elliott Hughesd9c67be2012-02-02 19:54:06 -080056 : is_compiler_(false),
57 is_zygote_(false),
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -070058 default_stack_size_(Thread::kDefaultStackSize),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080059 heap_(NULL),
Elliott Hughesc33a32b2011-10-11 18:18:07 -070060 monitor_list_(NULL),
Elliott Hughesdcc24742011-09-07 14:02:44 -070061 thread_list_(NULL),
62 intern_table_(NULL),
63 class_linker_(NULL),
64 signal_catcher_(NULL),
65 java_vm_(NULL),
Brian Carlstrom16192862011-09-12 17:50:06 -070066 jni_stub_array_(NULL),
Brian Carlstrome24fa612011-09-29 00:53:55 -070067 abstract_method_error_stub_array_(NULL),
Ian Rogers19846512012-02-24 11:42:47 -080068 resolution_method_(NULL),
Ian Rogers7c3757f2012-02-15 17:18:53 -080069 system_class_loader_(NULL),
Elliott Hughes6b355752012-01-13 16:49:08 -080070 shutting_down_(false),
Elliott Hughesdcc24742011-09-07 14:02:44 -070071 started_(false),
Mathieu Chartier7664f5c2012-06-08 18:15:32 -070072 finished_starting_(false),
Elliott Hughesdcc24742011-09-07 14:02:44 -070073 vfprintf_(NULL),
74 exit_(NULL),
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070075 abort_(NULL),
jeffhao2692b572011-12-16 15:42:28 -080076 stats_enabled_(false),
Ian Rogers776ac1f2012-04-13 23:36:36 -070077 method_trace_(0),
78 method_trace_file_size_(0),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080079 tracer_(NULL),
80 use_compile_time_class_path_(false) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -070081 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
82 resolution_stub_array_[i] = NULL;
83 }
84 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
85 callee_save_method_[i] = NULL;
86 }
Elliott Hughesdcc24742011-09-07 14:02:44 -070087}
88
Carl Shapiro61e019d2011-07-14 16:53:09 -070089Runtime::~Runtime() {
Elliott Hughes6b355752012-01-13 16:49:08 -080090 shutting_down_ = true;
91
jeffhaob5e81852012-03-12 11:15:45 -070092 if (IsMethodTracingActive()) {
93 Trace::Shutdown();
94 }
95
Elliott Hughes5fe594f2011-09-08 12:33:17 -070096 // Make sure our internal threads are dead before we start tearing down things they're using.
Elliott Hughese52e49b2012-04-02 16:05:44 -070097 Dbg::StopJdwp();
Elliott Hughes5fe594f2011-09-08 12:33:17 -070098 delete signal_catcher_;
99
Elliott Hughes038a8062011-09-18 14:12:41 -0700100 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
Elliott Hughes93e74e82011-09-13 11:07:03 -0700101 delete thread_list_;
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700102 delete monitor_list_;
Elliott Hughes93e74e82011-09-13 11:07:03 -0700103
Carl Shapiro61e019d2011-07-14 16:53:09 -0700104 delete class_linker_;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800105 delete heap_;
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -0700106#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
Ian Rogers776ac1f2012-04-13 23:36:36 -0700107 verifier::MethodVerifier::DeleteInferredRegCategoryMaps();
Logan Chiendd361c92012-04-10 23:40:37 +0800108#endif
Ian Rogers776ac1f2012-04-13 23:36:36 -0700109 verifier::MethodVerifier::DeleteGcMaps();
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700110 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700111 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700112 Thread::Shutdown();
Elliott Hughes7c6169d2012-05-02 16:11:48 -0700113 QuasiAtomic::Shutdown();
114
Carl Shapiro4acf4642011-07-26 18:54:13 -0700115 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700116 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -0700117 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700118}
119
Elliott Hughescac6cc72011-11-03 20:31:21 -0700120static bool gAborting = false;
121
Elliott Hughese0918552011-10-28 17:18:29 -0700122struct AbortState {
123 void Dump(std::ostream& os) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700124 if (gAborting) {
125 os << "Runtime aborting --- recursively, so no thread-specific detail!\n";
126 return;
127 }
128 gAborting = true;
Elliott Hughese0918552011-10-28 17:18:29 -0700129 os << "Runtime aborting...\n";
Elliott Hughes6c7d2442012-02-01 18:40:47 -0800130 if (Runtime::Current() == NULL) {
131 os << "(Runtime does not yet exist!)\n";
132 return;
133 }
Elliott Hughese0918552011-10-28 17:18:29 -0700134 Thread* self = Thread::Current();
135 if (self == NULL) {
136 os << "(Aborting thread was not attached to runtime!)\n";
137 } else {
Elliott Hughes899e7892012-01-24 14:57:32 -0800138 self->Dump(os);
139 if (self->IsExceptionPending()) {
140 os << "Pending " << PrettyTypeOf(self->GetException()) << " on thread:\n"
141 << self->GetException()->Dump();
142 }
Elliott Hughese0918552011-10-28 17:18:29 -0700143 }
144 }
145};
146
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800147static Mutex& GetAbortLock() {
148 static Mutex abort_lock("abort lock");
149 return abort_lock;
150}
151
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700152void Runtime::Abort() {
Elliott Hughes131aef82012-01-27 11:53:35 -0800153 // Ensure that we don't have multiple threads trying to abort at once,
154 // which would result in significantly worse diagnostics.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800155 MutexLock mu(GetAbortLock());
Elliott Hughes131aef82012-01-27 11:53:35 -0800156
Elliott Hughesffe67362011-07-17 12:09:27 -0700157 // Get any pending output out of the way.
158 fflush(NULL);
159
160 // Many people have difficulty distinguish aborts from crashes,
161 // so be explicit.
Elliott Hughese0918552011-10-28 17:18:29 -0700162 AbortState state;
Elliott Hughes9ee5f9c2012-02-03 18:33:16 -0800163 LOG(INTERNAL_FATAL) << Dumpable<AbortState>(state);
Elliott Hughesffe67362011-07-17 12:09:27 -0700164
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700165 // Call the abort hook if we have one.
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700166 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700167 LOG(INTERNAL_FATAL) << "Calling abort hook...";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700168 Runtime::Current()->abort_();
169 // notreached
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700170 LOG(INTERNAL_FATAL) << "Unexpectedly returned from abort hook!";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700171 }
172
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700173#if defined(__BIONIC__)
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700174 // TODO: finish merging patches to fix abort(3) in bionic, then lose this!
175 // Bionic doesn't implement POSIX semantics for abort(3) in a multi-threaded
176 // process, so if we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -0700177 // receive SIGABRT. debuggerd dumps the stack trace of the main
178 // thread, whether or not that was the thread that failed. By
179 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -0700180 // fault in the current thread, and get a useful log from debuggerd.
Elliott Hughes81ff3182012-03-23 20:35:56 -0700181 // We can also trivially tell the difference between a crash and
Elliott Hughesffe67362011-07-17 12:09:27 -0700182 // a deliberate abort by looking at the fault address.
183 *reinterpret_cast<char*>(0xdeadd00d) = 38;
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700184#elif defined(__APPLE__)
185 // TODO: check that this actually gives good stack traces on the Mac!
186 pthread_kill(pthread_self(), SIGABRT);
187#else
188 // TODO: we ought to be able to use pthread_kill(3) here (or abort(3),
189 // which POSIX defines in terms of raise(3), which POSIX defines in terms
190 // of pthread_kill(3)). On Linux, though, libcorkscrew can't unwind through
191 // libpthread, which means the stacks we dump would be useless. Calling
192 // tgkill(2) directly avoids that.
193 syscall(__NR_tgkill, getpid(), GetTid(), SIGABRT);
194#endif
Elliott Hughesffe67362011-07-17 12:09:27 -0700195 // notreached
196}
197
Elliott Hughesbf86d042011-08-31 17:53:14 -0700198void Runtime::CallExitHook(jint status) {
199 if (exit_ != NULL) {
Elliott Hughes34e06962012-04-09 13:55:55 -0700200 ScopedThreadStateChange tsc(Thread::Current(), kNative);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700201 exit_(status);
202 LOG(WARNING) << "Exit hook returned instead of exiting!";
203 }
204}
205
Brian Carlstrom8a436592011-08-15 21:27:23 -0700206// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
207// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
208// [gG] gigabytes.
209//
210// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700211// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
212// of 1024.
213//
214// The spec says the -Xmx and -Xms options must be multiples of 1024. It
215// doesn't say anything about -Xss.
216//
217// Returns 0 (a useless size) if "s" is malformed or specifies a low or
218// non-evenly-divisible value.
219//
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800220size_t ParseMemoryOption(const char* s, size_t div) {
Brian Carlstrom8a436592011-08-15 21:27:23 -0700221 // strtoul accepts a leading [+-], which we don't want,
222 // so make sure our string starts with a decimal digit.
223 if (isdigit(*s)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700224 char* s2;
225 size_t val = strtoul(s, &s2, 10);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700226 if (s2 != s) {
227 // s2 should be pointing just after the number.
228 // If this is the end of the string, the user
229 // has specified a number of bytes. Otherwise,
230 // there should be exactly one more character
231 // that specifies a multiplier.
232 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700233 // The remainder of the string is either a single multiplier
234 // character, or nothing to indicate that the value is in
235 // bytes.
236 char c = *s2++;
237 if (*s2 == '\0') {
238 size_t mul;
239 if (c == '\0') {
240 mul = 1;
241 } else if (c == 'k' || c == 'K') {
242 mul = KB;
243 } else if (c == 'm' || c == 'M') {
244 mul = MB;
245 } else if (c == 'g' || c == 'G') {
246 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700247 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700248 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700249 return 0;
250 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700251
252 if (val <= std::numeric_limits<size_t>::max() / mul) {
253 val *= mul;
254 } else {
255 // Clamp to a multiple of 1024.
256 val = std::numeric_limits<size_t>::max() & ~(1024-1);
257 }
258 } else {
259 // There's more than one character after the numeric part.
260 return 0;
261 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700262 }
263 // The man page says that a -Xm value must be a multiple of 1024.
264 if (val % div == 0) {
265 return val;
266 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700267 }
268 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700269 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700270}
271
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800272size_t ParseIntegerOrDie(const std::string& s) {
273 std::string::size_type colon = s.find(':');
274 if (colon == std::string::npos) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700275 LOG(FATAL) << "Missing integer: " << s;
276 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800277 const char* begin = &s[colon + 1];
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700278 char* end;
279 size_t result = strtoul(begin, &end, 10);
280 if (begin == end || *end != '\0') {
281 LOG(FATAL) << "Failed to parse integer in: " << s;
282 }
283 return result;
284}
285
Elliott Hughes0af55432011-08-17 18:37:28 -0700286void LoadJniLibrary(JavaVMExt* vm, const char* name) {
Elliott Hughes6cc332e2012-01-20 22:59:20 -0800287 std::string mapped_name(StringPrintf(OS_SHARED_LIB_FORMAT_STR, name));
Elliott Hughes75770752011-08-24 17:52:38 -0700288 std::string reason;
289 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700290 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
291 << reason;
292 }
293}
294
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700295Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700296 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800297 const char* boot_class_path_string = getenv("BOOTCLASSPATH");
298 if (boot_class_path_string != NULL) {
299 parsed->boot_class_path_string_ = boot_class_path_string;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700300 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800301 const char* class_path_string = getenv("CLASSPATH");
302 if (class_path_string != NULL) {
303 parsed->class_path_string_ = class_path_string;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700304 }
Elliott Hughes67d92002012-03-26 15:08:51 -0700305 // -Xcheck:jni is off by default for regular builds but on by default in debug builds.
306 parsed->check_jni_ = kIsDebugBuild;
Elliott Hughes85d15452011-09-16 17:33:01 -0700307
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700308 parsed->heap_initial_size_ = Heap::kInitialSize;
309 parsed->heap_maximum_size_ = Heap::kMaximumSize;
jeffhaoc1160702011-10-27 15:48:45 -0700310 parsed->heap_growth_limit_ = 0; // 0 means no growth limit
Brian Carlstromf734cf52011-08-17 16:28:14 -0700311 parsed->stack_size_ = Thread::kDefaultStackSize;
312
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800313 parsed->is_compiler_ = false;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700314 parsed->is_zygote_ = false;
315
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700316 parsed->jni_globals_max_ = 0;
Elliott Hughesfc861622011-10-17 17:57:47 -0700317 parsed->lock_profiling_threshold_ = 0;
318 parsed->hook_is_sensitive_thread_ = NULL;
319
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700320 parsed->hook_vfprintf_ = vfprintf;
321 parsed->hook_exit_ = exit;
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700322 parsed->hook_abort_ = NULL; // We don't call abort(3) by default; see Runtime::Abort.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700323
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800324// gLogVerbosity.class_linker = true; // TODO: don't check this in!
325// gLogVerbosity.compiler = true; // TODO: don't check this in!
326// gLogVerbosity.heap = true; // TODO: don't check this in!
327// gLogVerbosity.gc = true; // TODO: don't check this in!
328// gLogVerbosity.jdwp = true; // TODO: don't check this in!
329// gLogVerbosity.jni = true; // TODO: don't check this in!
330// gLogVerbosity.monitor = true; // TODO: don't check this in!
331// gLogVerbosity.startup = true; // TODO: don't check this in!
332// gLogVerbosity.third_party_jni = true; // TODO: don't check this in!
333// gLogVerbosity.threads = true; // TODO: don't check this in!
334
jeffhaob5e81852012-03-12 11:15:45 -0700335 parsed->method_trace_ = false;
336 parsed->method_trace_file_ = "/data/method-trace-file.bin";
337 parsed->method_trace_file_size_ = 10 * MB;
338
Brian Carlstrom8a436592011-08-15 21:27:23 -0700339 for (size_t i = 0; i < options.size(); ++i) {
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800340 const std::string option(options[i].first);
Brian Carlstrom0796af02011-10-12 14:31:45 -0700341 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700342 LOG(INFO) << "option[" << i << "]=" << option;
343 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800344 if (StartsWith(option, "-Xbootclasspath:")) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800345 parsed->boot_class_path_string_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700346 } else if (option == "-classpath" || option == "-cp") {
347 // TODO: support -Djava.class.path
348 i++;
349 if (i == options.size()) {
350 // TODO: usage
351 LOG(FATAL) << "Missing required class path value for " << option;
352 return NULL;
353 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800354 const StringPiece& value = options[i].first;
355 parsed->class_path_string_ = value.data();
356 } else if (option == "bootclasspath") {
357 parsed->boot_class_path_
358 = reinterpret_cast<const std::vector<const DexFile*>*>(options[i].second);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800359 } else if (StartsWith(option, "-Ximage:")) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800360 parsed->image_ = option.substr(strlen("-Ximage:")).data();
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800361 } else if (StartsWith(option, "-Xcheck:jni")) {
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700362 parsed->check_jni_ = true;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800363 } else if (StartsWith(option, "-Xrunjdwp:") || StartsWith(option, "-agentlib:jdwp=")) {
364 std::string tail(option.substr(option[1] == 'X' ? 10 : 15));
Elliott Hughes3bb81562011-10-21 18:52:59 -0700365 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
366 LOG(FATAL) << "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
367 << "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n";
368 return NULL;
369 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800370 } else if (StartsWith(option, "-Xms")) {
371 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).c_str(), 1024);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700372 if (size == 0) {
373 if (ignore_unrecognized) {
374 continue;
375 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700376 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700377 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700378 return NULL;
379 }
380 parsed->heap_initial_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800381 } else if (StartsWith(option, "-Xmx")) {
382 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).c_str(), 1024);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700383 if (size == 0) {
384 if (ignore_unrecognized) {
385 continue;
386 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700387 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700388 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700389 return NULL;
390 }
391 parsed->heap_maximum_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800392 } else if (StartsWith(option, "-XX:HeapGrowthLimit=")) {
393 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).c_str(), 1024);
jeffhaoc1160702011-10-27 15:48:45 -0700394 if (size == 0) {
395 if (ignore_unrecognized) {
396 continue;
397 }
398 // TODO: usage
399 LOG(FATAL) << "Failed to parse " << option;
400 return NULL;
401 }
402 parsed->heap_growth_limit_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800403 } else if (StartsWith(option, "-Xss")) {
404 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).c_str(), 1);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700405 if (size == 0) {
406 if (ignore_unrecognized) {
407 continue;
408 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700409 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700410 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700411 return NULL;
412 }
413 parsed->stack_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800414 } else if (StartsWith(option, "-D")) {
415 parsed->properties_.push_back(option.substr(strlen("-D")));
416 } else if (StartsWith(option, "-Xjnitrace:")) {
417 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:"));
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700418 } else if (option == "compiler") {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800419 parsed->is_compiler_ = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700420 } else if (option == "-Xzygote") {
421 parsed->is_zygote_ = true;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800422 } else if (StartsWith(option, "-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700423 std::vector<std::string> verbose_options;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800424 Split(option.substr(strlen("-verbose:")), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700425 for (size_t i = 0; i < verbose_options.size(); ++i) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800426 if (verbose_options[i] == "class") {
427 gLogVerbosity.class_linker = true;
428 } else if (verbose_options[i] == "compiler") {
429 gLogVerbosity.compiler = true;
430 } else if (verbose_options[i] == "heap") {
431 gLogVerbosity.heap = true;
432 } else if (verbose_options[i] == "gc") {
433 gLogVerbosity.gc = true;
434 } else if (verbose_options[i] == "jdwp") {
435 gLogVerbosity.jdwp = true;
436 } else if (verbose_options[i] == "jni") {
437 gLogVerbosity.jni = true;
438 } else if (verbose_options[i] == "monitor") {
439 gLogVerbosity.monitor = true;
440 } else if (verbose_options[i] == "startup") {
441 gLogVerbosity.startup = true;
442 } else if (verbose_options[i] == "third-party-jni") {
443 gLogVerbosity.third_party_jni = true;
444 } else if (verbose_options[i] == "threads") {
445 gLogVerbosity.threads = true;
446 } else {
447 LOG(WARNING) << "Ignoring unknown -verbose option: " << verbose_options[i];
448 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700449 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800450 } else if (StartsWith(option, "-Xjnigreflimit:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700451 parsed->jni_globals_max_ = ParseIntegerOrDie(option);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800452 } else if (StartsWith(option, "-Xlockprofthreshold:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700453 parsed->lock_profiling_threshold_ = ParseIntegerOrDie(option);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800454 } else if (StartsWith(option, "-Xstacktracefile:")) {
Elliott Hughes67d92002012-03-26 15:08:51 -0700455 if (kIsDebugBuild) {
456 // Ignore the zygote and always show stack traces in debug builds.
457 } else {
458 parsed->stack_trace_file_ = option.substr(strlen("-Xstacktracefile:"));
459 }
Elliott Hughesfc861622011-10-17 17:57:47 -0700460 } else if (option == "sensitiveThread") {
461 parsed->hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(options[i].second);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700462 } else if (option == "vfprintf") {
463 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
464 } else if (option == "exit") {
465 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
466 } else if (option == "abort") {
467 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700468 } else if (option == "host-prefix") {
469 parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second);
Elliott Hughesb3e66df2012-01-12 14:49:18 -0800470 } else if (option == "-Xgenregmap" || option == "-Xgc:precise") {
471 // We silently ignore these for backwards compatibility.
jeffhaob5e81852012-03-12 11:15:45 -0700472 } else if (option == "-Xmethod-trace") {
473 parsed->method_trace_ = true;
474 } else if (StartsWith(option, "-Xmethod-trace-file:")) {
475 parsed->method_trace_file_ = option.substr(strlen("-Xmethod-trace-file:"));
476 } else if (StartsWith(option, "-Xmethod-trace-file-size:")) {
477 parsed->method_trace_file_size_ = ParseIntegerOrDie(option);
Elliott Hughese119a362012-05-22 17:37:06 -0700478 } else if (option == "-Xprofile:threadcpuclock") {
479 Trace::SetDefaultClockSource(kProfilerClockSourceThreadCpu);
480 } else if (option == "-Xprofile:wallclock") {
481 Trace::SetDefaultClockSource(kProfilerClockSourceWall);
482 } else if (option == "-Xprofile:dualclock") {
483 Trace::SetDefaultClockSource(kProfilerClockSourceDual);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700484 } else {
485 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700486 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700487 LOG(ERROR) << "Unrecognized option " << option;
488 // TODO: this should exit, but for now tolerate unknown options
489 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700490 }
491 }
492 }
493
Brian Carlstrom223f20f2012-02-04 23:06:55 -0800494 if (!parsed->is_compiler_ && parsed->image_.empty()) {
495 parsed->image_ += GetAndroidRoot();
496 parsed->image_ += "/framework/boot.art";
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700497 }
jeffhaoc1160702011-10-27 15:48:45 -0700498 if (parsed->heap_growth_limit_ == 0) {
499 parsed->heap_growth_limit_ = parsed->heap_maximum_size_;
500 }
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700501
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700502 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700503}
504
505Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700506 // TODO: acquire a static mutex on Runtime to avoid racing.
507 if (Runtime::instance_ != NULL) {
508 return NULL;
509 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700510 instance_ = new Runtime;
511 if (!instance_->Init(options, ignore_unrecognized)) {
512 delete instance_;
513 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700514 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700515 return instance_;
516}
Elliott Hughes0af55432011-08-17 18:37:28 -0700517
Brian Carlstromaded5f72011-10-07 17:15:04 -0700518void CreateSystemClassLoader() {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800519 if (Runtime::Current()->UseCompileTimeClassPath()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700520 return;
521 }
522
523 Thread* self = Thread::Current();
524
525 // Must be in the kNative state for calling native methods.
Elliott Hughes34e06962012-04-09 13:55:55 -0700526 CHECK_EQ(self->GetState(), kNative);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700527
528 JNIEnv* env = self->GetJniEnv();
Elliott Hugheseac76672012-05-24 21:56:51 -0700529 jmethodID getSystemClassLoader = env->GetStaticMethodID(WellKnownClasses::java_lang_ClassLoader,
Brian Carlstromdf143242011-10-10 18:05:34 -0700530 "getSystemClassLoader",
531 "()Ljava/lang/ClassLoader;");
532 CHECK(getSystemClassLoader != NULL);
Elliott Hugheseac76672012-05-24 21:56:51 -0700533 ScopedLocalRef<jobject> class_loader(env, env->CallStaticObjectMethod(WellKnownClasses::java_lang_ClassLoader,
Brian Carlstromdf143242011-10-10 18:05:34 -0700534 getSystemClassLoader));
535 CHECK(class_loader.get() != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700536
Brian Carlstromdf143242011-10-10 18:05:34 -0700537 Thread::Current()->SetClassLoaderOverride(Decode<ClassLoader*>(env, class_loader.get()));
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500538
Elliott Hugheseac76672012-05-24 21:56:51 -0700539 jfieldID contextClassLoader = env->GetFieldID(WellKnownClasses::java_lang_Thread,
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500540 "contextClassLoader",
541 "Ljava/lang/ClassLoader;");
542 CHECK(contextClassLoader != NULL);
543 ScopedLocalRef<jobject> self_jobject(env, AddLocalReference<jobject>(env, self->GetPeer()));
544 env->SetObjectField(self_jobject.get(), contextClassLoader, class_loader.get());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700545}
546
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700547void Runtime::Start() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800548 VLOG(startup) << "Runtime::Start entering";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700549
550 CHECK(host_prefix_.empty()) << host_prefix_;
551
Logan Chien0c717dd2012-03-28 18:31:07 +0800552 // Relocate the OatFiles (ELF images)
553 class_linker_->RelocateExecutable();
554
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700555 // Restore main thread state to kNative as expected by native code
Elliott Hughes34e06962012-04-09 13:55:55 -0700556 Thread::Current()->SetState(kNative);
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700557
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700558 started_ = true;
559
Brian Carlstromae826982011-11-09 01:33:42 -0800560 // InitNativeMethods needs to be after started_ so that the classes
561 // it touches will have methods linked to the oat file if necessary.
562 InitNativeMethods();
563
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500564 Thread::FinishStartup();
565
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700566 if (!is_zygote_) {
567 DidForkFromZygote();
568 }
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700569
Elliott Hughes85d15452011-09-16 17:33:01 -0700570 StartDaemonThreads();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700571
Brian Carlstromaded5f72011-10-07 17:15:04 -0700572 CreateSystemClassLoader();
573
Elliott Hughes726079d2011-10-07 18:43:44 -0700574 Thread::Current()->GetJniEnv()->locals.AssertEmpty();
575
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800576 VLOG(startup) << "Runtime::Start exiting";
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700577
578 finished_starting_ = true;
Elliott Hughes85d15452011-09-16 17:33:01 -0700579}
580
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700581void Runtime::DidForkFromZygote() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700582 is_zygote_ = false;
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700583
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700584 StartSignalCatcher();
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700585
586 // Start the JDWP thread. If the command-line debugger flags specified "suspend=y",
587 // this will pause the runtime, so we probably want this to come last.
588 Dbg::StartJdwp();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700589}
590
591void Runtime::StartSignalCatcher() {
592 if (!is_zygote_) {
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700593 signal_catcher_ = new SignalCatcher(stack_trace_file_);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700594 }
595}
596
Elliott Hughes85d15452011-09-16 17:33:01 -0700597void Runtime::StartDaemonThreads() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800598 VLOG(startup) << "Runtime::StartDaemonThreads entering";
Elliott Hughes85d15452011-09-16 17:33:01 -0700599
Elliott Hughes719b3232011-09-25 17:42:19 -0700600 Thread* self = Thread::Current();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700601
602 // Must be in the kNative state for calling native methods.
Elliott Hughes34e06962012-04-09 13:55:55 -0700603 CHECK_EQ(self->GetState(), kNative);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700604
Elliott Hughes719b3232011-09-25 17:42:19 -0700605 JNIEnv* env = self->GetJniEnv();
Elliott Hugheseac76672012-05-24 21:56:51 -0700606 env->CallStaticVoidMethod(WellKnownClasses::java_lang_Daemons, WellKnownClasses::java_lang_Daemons_start);
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800607 CHECK(!env->ExceptionCheck());
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700608
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800609 VLOG(startup) << "Runtime::StartDaemonThreads exiting";
Carl Shapiro61e019d2011-07-14 16:53:09 -0700610}
611
Elliott Hughes0af55432011-08-17 18:37:28 -0700612bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700613 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700614
Elliott Hughes90a33692011-08-30 13:27:07 -0700615 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
616 if (options.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700617 LOG(ERROR) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700618 return false;
619 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800620 VLOG(startup) << "Runtime::Init -verbose:startup enabled";
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700621
Elliott Hughes7c6169d2012-05-02 16:11:48 -0700622 QuasiAtomic::Startup();
623
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700624 SetJniGlobalsMax(options->jni_globals_max_);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800625 Monitor::Init(options->lock_profiling_threshold_, options->hook_is_sensitive_thread_);
Elliott Hughes32d6e1e2011-10-11 14:47:44 -0700626
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700627 host_prefix_ = options->host_prefix_;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800628 boot_class_path_string_ = options->boot_class_path_string_;
629 class_path_string_ = options->class_path_string_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700630 properties_ = options->properties_;
631
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800632 is_compiler_ = options->is_compiler_;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700633 is_zygote_ = options->is_zygote_;
634
Elliott Hughes0af55432011-08-17 18:37:28 -0700635 vfprintf_ = options->hook_vfprintf_;
636 exit_ = options->hook_exit_;
637 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700638
Elliott Hughesbe759c62011-09-08 19:38:21 -0700639 default_stack_size_ = options->stack_size_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700640 stack_trace_file_ = options->stack_trace_file_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700641
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700642 monitor_list_ = new MonitorList;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800643 thread_list_ = new ThreadList;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700644 intern_table_ = new InternTable;
645
Ian Rogers776ac1f2012-04-13 23:36:36 -0700646 verifier::MethodVerifier::InitGcMaps();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800647
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -0700648#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
Ian Rogers776ac1f2012-04-13 23:36:36 -0700649 verifier::MethodVerifier::InitInferredRegCategoryMaps();
Logan Chiendd361c92012-04-10 23:40:37 +0800650#endif
651
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800652 heap_ = new Heap(options->heap_initial_size_,
653 options->heap_growth_limit_,
654 options->heap_maximum_size_,
655 options->image_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700656
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700657 BlockSignals();
Elliott Hughes457005c2012-04-16 13:54:25 -0700658 InitPlatformSignalHandlers();
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700659
Elliott Hughesa0957642011-09-02 14:27:33 -0700660 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700661
Elliott Hughesbe759c62011-09-08 19:38:21 -0700662 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700663
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700664 // ClassLinker needs an attached thread, but we can't fully attach a thread
Elliott Hughes462c9442012-03-23 18:47:50 -0700665 // without creating objects. We can't supply a thread group yet; it will be fixed later.
666 Thread::Attach("main", false, NULL);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700667
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700668 // Set us to runnable so tools using a runtime can allocate and GC by default
Elliott Hughes34e06962012-04-09 13:55:55 -0700669 Thread::Current()->SetState(kRunnable);
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700670
Ian Rogers141d6222012-04-05 12:23:06 -0700671 // Now we're attached, we can take the heap lock and validate the heap.
672 GetHeap()->EnableObjectValidation();
673
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800674 CHECK_GE(GetHeap()->GetSpaces().size(), 1U);
675 if (GetHeap()->GetSpaces()[0]->IsImageSpace()) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800676 class_linker_ = ClassLinker::CreateFromImage(intern_table_);
677 } else {
678 CHECK(options->boot_class_path_ != NULL);
679 CHECK_NE(options->boot_class_path_->size(), 0U);
680 class_linker_ = ClassLinker::CreateFromCompiler(*options->boot_class_path_, intern_table_);
681 }
682 CHECK(class_linker_ != NULL);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700683
jeffhaob5e81852012-03-12 11:15:45 -0700684 method_trace_ = options->method_trace_;
685 method_trace_file_ = options->method_trace_file_;
686 method_trace_file_size_ = options->method_trace_file_size_;
687
688 if (options->method_trace_) {
689 Trace::Start(options->method_trace_file_.c_str(), -1, options->method_trace_file_size_, 0, false);
690 }
691
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800692 VLOG(startup) << "Runtime::Init exiting";
Carl Shapiro1fb86202011-06-27 17:43:13 -0700693 return true;
694}
695
Elliott Hughes038a8062011-09-18 14:12:41 -0700696void Runtime::InitNativeMethods() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800697 VLOG(startup) << "Runtime::InitNativeMethods entering";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700698 Thread* self = Thread::Current();
699 JNIEnv* env = self->GetJniEnv();
700
Elliott Hughes418d20f2011-09-22 14:00:39 -0700701 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Elliott Hughes34e06962012-04-09 13:55:55 -0700702 CHECK_EQ(self->GetState(), kNative);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700703
Elliott Hughes418d20f2011-09-22 14:00:39 -0700704 // First set up JniConstants, which is used by both the runtime's built-in native
705 // methods and libcore.
Elliott Hughesfea966e2011-09-22 10:26:20 -0700706 JniConstants::init(env);
Elliott Hugheseac76672012-05-24 21:56:51 -0700707 WellKnownClasses::Init(env);
Elliott Hughesfea966e2011-09-22 10:26:20 -0700708
Elliott Hughes418d20f2011-09-22 14:00:39 -0700709 // Then set up the native methods provided by the runtime itself.
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700710 RegisterRuntimeNativeMethods(env);
711
Elliott Hughes418d20f2011-09-22 14:00:39 -0700712 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
713 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
714 // the library that implements System.loadLibrary!
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700715 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800716 VLOG(startup) << "Runtime::InitNativeMethods exiting";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700717}
718
719void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
720#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Ian Rogers5167c972012-02-03 10:41:20 -0800721 // Register Throwable first so that registration of other native methods can throw exceptions
722 REGISTER(register_java_lang_Throwable);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700723 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700724 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700725 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700726 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700727 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700728 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700729 REGISTER(register_java_lang_Object);
730 REGISTER(register_java_lang_Runtime);
731 REGISTER(register_java_lang_String);
732 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700733 REGISTER(register_java_lang_Thread);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700734 REGISTER(register_java_lang_VMClassLoader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700735 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700736 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700737 REGISTER(register_java_lang_reflect_Field);
738 REGISTER(register_java_lang_reflect_Method);
Jesse Wilson95caa792011-10-12 18:14:17 -0400739 REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700740 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700741 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700742 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700743 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700744#undef REGISTER
745}
746
Elliott Hughesc967f782012-04-16 10:23:15 -0700747void Runtime::DumpForSigQuit(std::ostream& os) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700748 GetClassLinker()->DumpForSigQuit(os);
749 GetInternTable()->DumpForSigQuit(os);
Elliott Hughesae80b492012-04-24 10:43:17 -0700750 GetJavaVM()->DumpForSigQuit(os);
Elliott Hughesc967f782012-04-16 10:23:15 -0700751 GetHeap()->DumpForSigQuit(os);
Elliott Hughes42ee1422011-09-06 12:33:32 -0700752 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700753
Elliott Hughesc967f782012-04-16 10:23:15 -0700754 thread_list_->DumpForSigQuit(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700755}
756
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800757void Runtime::DumpLockHolders(std::ostream& os) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800758 pid_t heap_lock_owner = GetHeap()->GetLockOwner();
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800759 pid_t thread_list_lock_owner = GetThreadList()->GetLockOwner();
760 pid_t classes_lock_owner = GetClassLinker()->GetClassesLockOwner();
761 pid_t dex_lock_owner = GetClassLinker()->GetDexLockOwner();
762 if ((heap_lock_owner | thread_list_lock_owner | classes_lock_owner | dex_lock_owner) != 0) {
763 os << "Heap lock owner tid: " << heap_lock_owner << "\n"
764 << "ThreadList lock owner tid: " << thread_list_lock_owner << "\n"
765 << "ClassLinker classes lock owner tid: " << classes_lock_owner << "\n"
766 << "ClassLinker dex lock owner tid: " << dex_lock_owner << "\n";
767 }
768}
769
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700770void Runtime::SetStatsEnabled(bool new_state) {
771 if (new_state == true) {
772 GetStats()->Clear(~0);
773 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
774 Thread::Current()->GetStats()->Clear(~0);
775 }
776 stats_enabled_ = new_state;
777}
778
779void Runtime::ResetStats(int kinds) {
780 GetStats()->Clear(kinds & 0xffff);
781 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
782 Thread::Current()->GetStats()->Clear(kinds >> 16);
783}
784
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700785int32_t Runtime::GetStat(int kind) {
786 RuntimeStats* stats;
787 if (kind < (1<<16)) {
788 stats = GetStats();
789 } else {
790 stats = Thread::Current()->GetStats();
791 kind >>= 16;
792 }
793 switch (kind) {
794 case KIND_ALLOCATED_OBJECTS:
795 return stats->allocated_objects;
796 case KIND_ALLOCATED_BYTES:
797 return stats->allocated_bytes;
798 case KIND_FREED_OBJECTS:
799 return stats->freed_objects;
800 case KIND_FREED_BYTES:
801 return stats->freed_bytes;
802 case KIND_GC_INVOCATIONS:
803 return stats->gc_for_alloc_count;
804 case KIND_CLASS_INIT_COUNT:
805 return stats->class_init_count;
806 case KIND_CLASS_INIT_TIME:
807 // Convert ns to us, reduce to 32 bits.
Elliott Hughes398f64b2012-03-26 18:05:48 -0700808 return static_cast<int>(stats->class_init_time_ns / 1000);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700809 case KIND_EXT_ALLOCATED_OBJECTS:
810 case KIND_EXT_ALLOCATED_BYTES:
811 case KIND_EXT_FREED_OBJECTS:
812 case KIND_EXT_FREED_BYTES:
813 return 0; // backward compatibility
814 default:
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700815 LOG(FATAL) << "Unknown statistic " << kind;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700816 return -1; // unreachable
817 }
818}
819
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700820void Runtime::BlockSignals() {
Elliott Hughes457005c2012-04-16 13:54:25 -0700821 SignalSet signals;
822 signals.Add(SIGPIPE);
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700823 // SIGQUIT is used to dump the runtime's state (including stack traces).
Elliott Hughes457005c2012-04-16 13:54:25 -0700824 signals.Add(SIGQUIT);
Elliott Hughes08795042012-04-03 14:48:52 -0700825 // SIGUSR1 is used to initiate a GC.
Elliott Hughes457005c2012-04-16 13:54:25 -0700826 signals.Add(SIGUSR1);
827 signals.Block();
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700828}
829
Elliott Hughes462c9442012-03-23 18:47:50 -0700830void Runtime::AttachCurrentThread(const char* thread_name, bool as_daemon, Object* thread_group) {
831 Thread::Attach(thread_name, as_daemon, thread_group);
Elliott Hughes22869a92012-03-27 14:08:24 -0700832 if (thread_name == NULL) {
833 LOG(WARNING) << *Thread::Current() << " attached without supplying a name";
834 }
Carl Shapiro61e019d2011-07-14 16:53:09 -0700835}
836
Elliott Hughesd92bec42011-09-02 17:04:36 -0700837void Runtime::DetachCurrentThread() {
Brian Carlstrom4d571432012-05-16 00:21:41 -0700838 Thread* self = Thread::Current();
839 if (self == NULL) {
840 LOG(FATAL) << "attempting to detach thread that is not attached";
841 }
842 if (self->GetTopOfStack().GetSP() != NULL) {
Elliott Hughes22869a92012-03-27 14:08:24 -0700843 LOG(FATAL) << *Thread::Current() << " attempting to detach while still running code";
844 }
Elliott Hughes02b48d12011-09-07 17:15:51 -0700845 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700846}
847
Brian Carlstrome24fa612011-09-29 00:53:55 -0700848void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700849 Dbg::VisitRoots(visitor, arg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700850 class_linker_->VisitRoots(visitor, arg);
851 intern_table_->VisitRoots(visitor, arg);
852 java_vm_->VisitRoots(visitor, arg);
853 thread_list_->VisitRoots(visitor, arg);
854 visitor(jni_stub_array_, arg);
855 visitor(abstract_method_error_stub_array_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700856 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
857 visitor(resolution_stub_array_[i], arg);
858 }
Ian Rogers19846512012-02-24 11:42:47 -0800859 visitor(resolution_method_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700860 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
861 visitor(callee_save_method_[i], arg);
862 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700863}
864
Ian Rogers169c9a72011-11-13 20:13:17 -0800865void Runtime::SetJniDlsymLookupStub(ByteArray* jni_stub_array) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700866 CHECK(jni_stub_array != NULL) << " jni_stub_array=" << jni_stub_array;
867 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array)
868 << "jni_stub_array_=" << jni_stub_array_ << " jni_stub_array=" << jni_stub_array;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700869 jni_stub_array_ = jni_stub_array;
870}
871
Brian Carlstrome24fa612011-09-29 00:53:55 -0700872void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) {
873 CHECK(abstract_method_error_stub_array != NULL);
874 CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array);
875 abstract_method_error_stub_array_ = abstract_method_error_stub_array;
876}
877
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700878void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700879 CHECK(resolution_stub_array != NULL);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700880 CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array);
881 resolution_stub_array_[type] = resolution_stub_array;
Ian Rogersad25ac52011-10-04 19:13:33 -0700882}
883
Ian Rogers19846512012-02-24 11:42:47 -0800884Method* Runtime::CreateResolutionMethod() {
885 Class* method_class = Method::GetMethodClass();
886 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
887 method->SetDeclaringClass(method_class);
888 // TODO: use a special method for resolution method saves
889 method->SetDexMethodIndex(DexFile::kDexNoIndex16);
890 ByteArray* unknown_resolution_stub = GetResolutionStubArray(kUnknownMethod);
891 CHECK(unknown_resolution_stub != NULL);
892 method->SetCode(unknown_resolution_stub->GetData());
893 return method.get();
894}
895
Brian Carlstromae826982011-11-09 01:33:42 -0800896Method* Runtime::CreateCalleeSaveMethod(InstructionSet instruction_set, CalleeSaveType type) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700897 Class* method_class = Method::GetMethodClass();
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700898 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
Ian Rogersff1ed472011-09-20 13:46:24 -0700899 method->SetDeclaringClass(method_class);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800900 // TODO: use a special method for callee saves
Ian Rogers19846512012-02-24 11:42:47 -0800901 method->SetDexMethodIndex(DexFile::kDexNoIndex16);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700902 method->SetCode(NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800903 if ((instruction_set == kThumb2) || (instruction_set == kArm)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700904 uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6) | (1 << art::arm::R7) |
905 (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11);
906 uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3);
907 uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9);
908 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills :0) |
909 (type == kSaveAll ? all_spills :0) | (1 << art::arm::LR);
910 uint32_t fp_all_spills = (1 << art::arm::S0) | (1 << art::arm::S1) | (1 << art::arm::S2) |
911 (1 << art::arm::S3) | (1 << art::arm::S4) | (1 << art::arm::S5) |
912 (1 << art::arm::S6) | (1 << art::arm::S7) | (1 << art::arm::S8) |
913 (1 << art::arm::S9) | (1 << art::arm::S10) | (1 << art::arm::S11) |
914 (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) |
915 (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) |
916 (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) |
917 (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) |
918 (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) |
919 (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
920 (1 << art::arm::S30) | (1 << art::arm::S31);
921 uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
922 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
923 __builtin_popcount(fp_spills) /* fprs */ +
924 1 /* Method* */) * kPointerSize, kStackAlignment);
Ian Rogers15fdb8c2011-09-25 15:45:07 -0700925 method->SetFrameSizeInBytes(frame_size);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700926 method->SetCoreSpillMask(core_spills);
927 method->SetFpSpillMask(fp_spills);
Brian Carlstromae826982011-11-09 01:33:42 -0800928 } else if (instruction_set == kX86) {
Ian Rogers7caad772012-03-30 01:07:54 -0700929 uint32_t ref_spills = (1 << art::x86::EBP) | (1 << art::x86::ESI) | (1 << art::x86::EDI);
930 uint32_t arg_spills = (1 << art::x86::ECX) | (1 << art::x86::EDX) | (1 << art::x86::EBX);
931 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills : 0) |
932 (1 << art::x86::kNumberOfCpuRegisters); // fake return address callee save
933 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
934 1 /* Method* */) * kPointerSize, kStackAlignment);
935 method->SetFrameSizeInBytes(frame_size);
936 method->SetCoreSpillMask(core_spills);
Ian Rogersff1ed472011-09-20 13:46:24 -0700937 method->SetFpSpillMask(0);
938 } else {
939 UNIMPLEMENTED(FATAL);
940 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700941 return method.get();
Ian Rogersff1ed472011-09-20 13:46:24 -0700942}
943
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700944void Runtime::SetCalleeSaveMethod(Method* method, CalleeSaveType type) {
945 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
946 callee_save_method_[type] = method;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700947}
948
jeffhao2692b572011-12-16 15:42:28 -0800949void Runtime::EnableMethodTracing(Trace* tracer) {
950 CHECK(!IsMethodTracingActive());
951 tracer_ = tracer;
952}
953
954void Runtime::DisableMethodTracing() {
955 CHECK(IsMethodTracingActive());
956 delete tracer_;
957 tracer_ = NULL;
958}
959
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800960const std::vector<const DexFile*>& Runtime::GetCompileTimeClassPath(const ClassLoader* class_loader) {
961 if (class_loader == NULL) {
962 return GetClassLinker()->GetBootClassPath();
963 }
964 CHECK(UseCompileTimeClassPath());
965 CompileTimeClassPaths::const_iterator it = compile_time_class_paths_.find(class_loader);
966 CHECK(it != compile_time_class_paths_.end());
967 return it->second;
968}
969
970void Runtime::SetCompileTimeClassPath(const ClassLoader* class_loader, std::vector<const DexFile*>& class_path) {
971 CHECK(!IsStarted());
972 use_compile_time_class_path_ = true;
Elliott Hughesa0e18062012-04-13 15:59:59 -0700973 compile_time_class_paths_.Put(class_loader, class_path);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800974}
975
Carl Shapiro1fb86202011-06-27 17:43:13 -0700976} // namespace art