blob: dda8f621e187b0cc817a5c78a535da1296a077ee [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),
Elliott Hughes225f5a12012-06-11 11:23:48 -070066 pre_allocated_OutOfMemoryError_(NULL),
Brian Carlstrom16192862011-09-12 17:50:06 -070067 jni_stub_array_(NULL),
Brian Carlstrome24fa612011-09-29 00:53:55 -070068 abstract_method_error_stub_array_(NULL),
Ian Rogers19846512012-02-24 11:42:47 -080069 resolution_method_(NULL),
Ian Rogers7c3757f2012-02-15 17:18:53 -080070 system_class_loader_(NULL),
Elliott Hughes6b355752012-01-13 16:49:08 -080071 shutting_down_(false),
Elliott Hughesdcc24742011-09-07 14:02:44 -070072 started_(false),
Mathieu Chartier7664f5c2012-06-08 18:15:32 -070073 finished_starting_(false),
Elliott Hughesdcc24742011-09-07 14:02:44 -070074 vfprintf_(NULL),
75 exit_(NULL),
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070076 abort_(NULL),
jeffhao2692b572011-12-16 15:42:28 -080077 stats_enabled_(false),
Ian Rogers776ac1f2012-04-13 23:36:36 -070078 method_trace_(0),
79 method_trace_file_size_(0),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080080 tracer_(NULL),
81 use_compile_time_class_path_(false) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -070082 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
83 resolution_stub_array_[i] = NULL;
84 }
85 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
Elliott Hughes225f5a12012-06-11 11:23:48 -070086 callee_save_methods_[i] = NULL;
Ian Rogers4f0d07c2011-10-06 23:38:47 -070087 }
Elliott Hughesdcc24742011-09-07 14:02:44 -070088}
89
Carl Shapiro61e019d2011-07-14 16:53:09 -070090Runtime::~Runtime() {
Elliott Hughes6b355752012-01-13 16:49:08 -080091 shutting_down_ = true;
92
jeffhaob5e81852012-03-12 11:15:45 -070093 if (IsMethodTracingActive()) {
94 Trace::Shutdown();
95 }
96
Elliott Hughes5fe594f2011-09-08 12:33:17 -070097 // Make sure our internal threads are dead before we start tearing down things they're using.
Elliott Hughese52e49b2012-04-02 16:05:44 -070098 Dbg::StopJdwp();
Elliott Hughes5fe594f2011-09-08 12:33:17 -070099 delete signal_catcher_;
100
Elliott Hughes038a8062011-09-18 14:12:41 -0700101 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
Elliott Hughes93e74e82011-09-13 11:07:03 -0700102 delete thread_list_;
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700103 delete monitor_list_;
Elliott Hughes93e74e82011-09-13 11:07:03 -0700104
Carl Shapiro61e019d2011-07-14 16:53:09 -0700105 delete class_linker_;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800106 delete heap_;
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -0700107#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
Ian Rogers776ac1f2012-04-13 23:36:36 -0700108 verifier::MethodVerifier::DeleteInferredRegCategoryMaps();
Logan Chiendd361c92012-04-10 23:40:37 +0800109#endif
Ian Rogers776ac1f2012-04-13 23:36:36 -0700110 verifier::MethodVerifier::DeleteGcMaps();
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700111 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700112 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700113 Thread::Shutdown();
Elliott Hughes7c6169d2012-05-02 16:11:48 -0700114 QuasiAtomic::Shutdown();
115
Carl Shapiro4acf4642011-07-26 18:54:13 -0700116 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700117 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -0700118 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700119}
120
Elliott Hughescac6cc72011-11-03 20:31:21 -0700121static bool gAborting = false;
122
Elliott Hughese0918552011-10-28 17:18:29 -0700123struct AbortState {
124 void Dump(std::ostream& os) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700125 if (gAborting) {
126 os << "Runtime aborting --- recursively, so no thread-specific detail!\n";
127 return;
128 }
129 gAborting = true;
Elliott Hughese0918552011-10-28 17:18:29 -0700130 os << "Runtime aborting...\n";
Elliott Hughes6c7d2442012-02-01 18:40:47 -0800131 if (Runtime::Current() == NULL) {
132 os << "(Runtime does not yet exist!)\n";
133 return;
134 }
Elliott Hughese0918552011-10-28 17:18:29 -0700135 Thread* self = Thread::Current();
136 if (self == NULL) {
137 os << "(Aborting thread was not attached to runtime!)\n";
138 } else {
Elliott Hughes899e7892012-01-24 14:57:32 -0800139 self->Dump(os);
140 if (self->IsExceptionPending()) {
141 os << "Pending " << PrettyTypeOf(self->GetException()) << " on thread:\n"
142 << self->GetException()->Dump();
143 }
Elliott Hughese0918552011-10-28 17:18:29 -0700144 }
145 }
146};
147
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800148static Mutex& GetAbortLock() {
149 static Mutex abort_lock("abort lock");
150 return abort_lock;
151}
152
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700153void Runtime::Abort() {
Elliott Hughes131aef82012-01-27 11:53:35 -0800154 // Ensure that we don't have multiple threads trying to abort at once,
155 // which would result in significantly worse diagnostics.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800156 MutexLock mu(GetAbortLock());
Elliott Hughes131aef82012-01-27 11:53:35 -0800157
Elliott Hughesffe67362011-07-17 12:09:27 -0700158 // Get any pending output out of the way.
159 fflush(NULL);
160
161 // Many people have difficulty distinguish aborts from crashes,
162 // so be explicit.
Elliott Hughese0918552011-10-28 17:18:29 -0700163 AbortState state;
Elliott Hughes9ee5f9c2012-02-03 18:33:16 -0800164 LOG(INTERNAL_FATAL) << Dumpable<AbortState>(state);
Elliott Hughesffe67362011-07-17 12:09:27 -0700165
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700166 // Call the abort hook if we have one.
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700167 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700168 LOG(INTERNAL_FATAL) << "Calling abort hook...";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700169 Runtime::Current()->abort_();
170 // notreached
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700171 LOG(INTERNAL_FATAL) << "Unexpectedly returned from abort hook!";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700172 }
173
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700174#if defined(__BIONIC__)
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700175 // TODO: finish merging patches to fix abort(3) in bionic, then lose this!
176 // Bionic doesn't implement POSIX semantics for abort(3) in a multi-threaded
177 // process, so if we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -0700178 // receive SIGABRT. debuggerd dumps the stack trace of the main
179 // thread, whether or not that was the thread that failed. By
180 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -0700181 // fault in the current thread, and get a useful log from debuggerd.
Elliott Hughes81ff3182012-03-23 20:35:56 -0700182 // We can also trivially tell the difference between a crash and
Elliott Hughesffe67362011-07-17 12:09:27 -0700183 // a deliberate abort by looking at the fault address.
184 *reinterpret_cast<char*>(0xdeadd00d) = 38;
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700185#elif defined(__APPLE__)
186 // TODO: check that this actually gives good stack traces on the Mac!
187 pthread_kill(pthread_self(), SIGABRT);
188#else
189 // TODO: we ought to be able to use pthread_kill(3) here (or abort(3),
190 // which POSIX defines in terms of raise(3), which POSIX defines in terms
191 // of pthread_kill(3)). On Linux, though, libcorkscrew can't unwind through
192 // libpthread, which means the stacks we dump would be useless. Calling
193 // tgkill(2) directly avoids that.
194 syscall(__NR_tgkill, getpid(), GetTid(), SIGABRT);
195#endif
Elliott Hughesffe67362011-07-17 12:09:27 -0700196 // notreached
197}
198
Elliott Hughesbf86d042011-08-31 17:53:14 -0700199void Runtime::CallExitHook(jint status) {
200 if (exit_ != NULL) {
Elliott Hughes34e06962012-04-09 13:55:55 -0700201 ScopedThreadStateChange tsc(Thread::Current(), kNative);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700202 exit_(status);
203 LOG(WARNING) << "Exit hook returned instead of exiting!";
204 }
205}
206
Brian Carlstrom8a436592011-08-15 21:27:23 -0700207// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
208// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
209// [gG] gigabytes.
210//
211// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700212// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
213// of 1024.
214//
215// The spec says the -Xmx and -Xms options must be multiples of 1024. It
216// doesn't say anything about -Xss.
217//
218// Returns 0 (a useless size) if "s" is malformed or specifies a low or
219// non-evenly-divisible value.
220//
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800221size_t ParseMemoryOption(const char* s, size_t div) {
Brian Carlstrom8a436592011-08-15 21:27:23 -0700222 // strtoul accepts a leading [+-], which we don't want,
223 // so make sure our string starts with a decimal digit.
224 if (isdigit(*s)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700225 char* s2;
226 size_t val = strtoul(s, &s2, 10);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700227 if (s2 != s) {
228 // s2 should be pointing just after the number.
229 // If this is the end of the string, the user
230 // has specified a number of bytes. Otherwise,
231 // there should be exactly one more character
232 // that specifies a multiplier.
233 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700234 // The remainder of the string is either a single multiplier
235 // character, or nothing to indicate that the value is in
236 // bytes.
237 char c = *s2++;
238 if (*s2 == '\0') {
239 size_t mul;
240 if (c == '\0') {
241 mul = 1;
242 } else if (c == 'k' || c == 'K') {
243 mul = KB;
244 } else if (c == 'm' || c == 'M') {
245 mul = MB;
246 } else if (c == 'g' || c == 'G') {
247 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700248 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700249 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700250 return 0;
251 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700252
253 if (val <= std::numeric_limits<size_t>::max() / mul) {
254 val *= mul;
255 } else {
256 // Clamp to a multiple of 1024.
257 val = std::numeric_limits<size_t>::max() & ~(1024-1);
258 }
259 } else {
260 // There's more than one character after the numeric part.
261 return 0;
262 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700263 }
264 // The man page says that a -Xm value must be a multiple of 1024.
265 if (val % div == 0) {
266 return val;
267 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700268 }
269 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700270 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700271}
272
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800273size_t ParseIntegerOrDie(const std::string& s) {
274 std::string::size_type colon = s.find(':');
275 if (colon == std::string::npos) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700276 LOG(FATAL) << "Missing integer: " << s;
277 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800278 const char* begin = &s[colon + 1];
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700279 char* end;
280 size_t result = strtoul(begin, &end, 10);
281 if (begin == end || *end != '\0') {
282 LOG(FATAL) << "Failed to parse integer in: " << s;
283 }
284 return result;
285}
286
Elliott Hughes0af55432011-08-17 18:37:28 -0700287void LoadJniLibrary(JavaVMExt* vm, const char* name) {
Elliott Hughes6cc332e2012-01-20 22:59:20 -0800288 std::string mapped_name(StringPrintf(OS_SHARED_LIB_FORMAT_STR, name));
Elliott Hughes75770752011-08-24 17:52:38 -0700289 std::string reason;
290 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700291 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
292 << reason;
293 }
294}
295
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700296Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700297 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800298 const char* boot_class_path_string = getenv("BOOTCLASSPATH");
299 if (boot_class_path_string != NULL) {
300 parsed->boot_class_path_string_ = boot_class_path_string;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700301 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800302 const char* class_path_string = getenv("CLASSPATH");
303 if (class_path_string != NULL) {
304 parsed->class_path_string_ = class_path_string;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700305 }
Elliott Hughes67d92002012-03-26 15:08:51 -0700306 // -Xcheck:jni is off by default for regular builds but on by default in debug builds.
307 parsed->check_jni_ = kIsDebugBuild;
Elliott Hughes85d15452011-09-16 17:33:01 -0700308
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700309 parsed->heap_initial_size_ = Heap::kInitialSize;
310 parsed->heap_maximum_size_ = Heap::kMaximumSize;
jeffhaoc1160702011-10-27 15:48:45 -0700311 parsed->heap_growth_limit_ = 0; // 0 means no growth limit
Brian Carlstromf734cf52011-08-17 16:28:14 -0700312 parsed->stack_size_ = Thread::kDefaultStackSize;
313
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800314 parsed->is_compiler_ = false;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700315 parsed->is_zygote_ = false;
316
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700317 parsed->jni_globals_max_ = 0;
Elliott Hughesfc861622011-10-17 17:57:47 -0700318 parsed->lock_profiling_threshold_ = 0;
319 parsed->hook_is_sensitive_thread_ = NULL;
320
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700321 parsed->hook_vfprintf_ = vfprintf;
322 parsed->hook_exit_ = exit;
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700323 parsed->hook_abort_ = NULL; // We don't call abort(3) by default; see Runtime::Abort.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700324
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800325// gLogVerbosity.class_linker = true; // TODO: don't check this in!
326// gLogVerbosity.compiler = true; // TODO: don't check this in!
327// gLogVerbosity.heap = true; // TODO: don't check this in!
328// gLogVerbosity.gc = true; // TODO: don't check this in!
329// gLogVerbosity.jdwp = true; // TODO: don't check this in!
330// gLogVerbosity.jni = true; // TODO: don't check this in!
331// gLogVerbosity.monitor = true; // TODO: don't check this in!
332// gLogVerbosity.startup = true; // TODO: don't check this in!
333// gLogVerbosity.third_party_jni = true; // TODO: don't check this in!
334// gLogVerbosity.threads = true; // TODO: don't check this in!
335
jeffhaob5e81852012-03-12 11:15:45 -0700336 parsed->method_trace_ = false;
337 parsed->method_trace_file_ = "/data/method-trace-file.bin";
338 parsed->method_trace_file_size_ = 10 * MB;
339
Brian Carlstrom8a436592011-08-15 21:27:23 -0700340 for (size_t i = 0; i < options.size(); ++i) {
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800341 const std::string option(options[i].first);
Brian Carlstrom0796af02011-10-12 14:31:45 -0700342 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700343 LOG(INFO) << "option[" << i << "]=" << option;
344 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800345 if (StartsWith(option, "-Xbootclasspath:")) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800346 parsed->boot_class_path_string_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700347 } else if (option == "-classpath" || option == "-cp") {
348 // TODO: support -Djava.class.path
349 i++;
350 if (i == options.size()) {
351 // TODO: usage
352 LOG(FATAL) << "Missing required class path value for " << option;
353 return NULL;
354 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800355 const StringPiece& value = options[i].first;
356 parsed->class_path_string_ = value.data();
357 } else if (option == "bootclasspath") {
358 parsed->boot_class_path_
359 = reinterpret_cast<const std::vector<const DexFile*>*>(options[i].second);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800360 } else if (StartsWith(option, "-Ximage:")) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800361 parsed->image_ = option.substr(strlen("-Ximage:")).data();
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800362 } else if (StartsWith(option, "-Xcheck:jni")) {
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700363 parsed->check_jni_ = true;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800364 } else if (StartsWith(option, "-Xrunjdwp:") || StartsWith(option, "-agentlib:jdwp=")) {
365 std::string tail(option.substr(option[1] == 'X' ? 10 : 15));
Elliott Hughes3bb81562011-10-21 18:52:59 -0700366 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
367 LOG(FATAL) << "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
368 << "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n";
369 return NULL;
370 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800371 } else if (StartsWith(option, "-Xms")) {
372 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).c_str(), 1024);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700373 if (size == 0) {
374 if (ignore_unrecognized) {
375 continue;
376 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700377 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700378 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700379 return NULL;
380 }
381 parsed->heap_initial_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800382 } else if (StartsWith(option, "-Xmx")) {
383 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).c_str(), 1024);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700384 if (size == 0) {
385 if (ignore_unrecognized) {
386 continue;
387 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700388 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700389 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700390 return NULL;
391 }
392 parsed->heap_maximum_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800393 } else if (StartsWith(option, "-XX:HeapGrowthLimit=")) {
394 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).c_str(), 1024);
jeffhaoc1160702011-10-27 15:48:45 -0700395 if (size == 0) {
396 if (ignore_unrecognized) {
397 continue;
398 }
399 // TODO: usage
400 LOG(FATAL) << "Failed to parse " << option;
401 return NULL;
402 }
403 parsed->heap_growth_limit_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800404 } else if (StartsWith(option, "-Xss")) {
405 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).c_str(), 1);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700406 if (size == 0) {
407 if (ignore_unrecognized) {
408 continue;
409 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700410 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700411 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700412 return NULL;
413 }
414 parsed->stack_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800415 } else if (StartsWith(option, "-D")) {
416 parsed->properties_.push_back(option.substr(strlen("-D")));
417 } else if (StartsWith(option, "-Xjnitrace:")) {
418 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:"));
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700419 } else if (option == "compiler") {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800420 parsed->is_compiler_ = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700421 } else if (option == "-Xzygote") {
422 parsed->is_zygote_ = true;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800423 } else if (StartsWith(option, "-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700424 std::vector<std::string> verbose_options;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800425 Split(option.substr(strlen("-verbose:")), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700426 for (size_t i = 0; i < verbose_options.size(); ++i) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800427 if (verbose_options[i] == "class") {
428 gLogVerbosity.class_linker = true;
429 } else if (verbose_options[i] == "compiler") {
430 gLogVerbosity.compiler = true;
431 } else if (verbose_options[i] == "heap") {
432 gLogVerbosity.heap = true;
433 } else if (verbose_options[i] == "gc") {
434 gLogVerbosity.gc = true;
435 } else if (verbose_options[i] == "jdwp") {
436 gLogVerbosity.jdwp = true;
437 } else if (verbose_options[i] == "jni") {
438 gLogVerbosity.jni = true;
439 } else if (verbose_options[i] == "monitor") {
440 gLogVerbosity.monitor = true;
441 } else if (verbose_options[i] == "startup") {
442 gLogVerbosity.startup = true;
443 } else if (verbose_options[i] == "third-party-jni") {
444 gLogVerbosity.third_party_jni = true;
445 } else if (verbose_options[i] == "threads") {
446 gLogVerbosity.threads = true;
447 } else {
448 LOG(WARNING) << "Ignoring unknown -verbose option: " << verbose_options[i];
449 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700450 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800451 } else if (StartsWith(option, "-Xjnigreflimit:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700452 parsed->jni_globals_max_ = ParseIntegerOrDie(option);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800453 } else if (StartsWith(option, "-Xlockprofthreshold:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700454 parsed->lock_profiling_threshold_ = ParseIntegerOrDie(option);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800455 } else if (StartsWith(option, "-Xstacktracefile:")) {
Elliott Hughes67d92002012-03-26 15:08:51 -0700456 if (kIsDebugBuild) {
457 // Ignore the zygote and always show stack traces in debug builds.
458 } else {
459 parsed->stack_trace_file_ = option.substr(strlen("-Xstacktracefile:"));
460 }
Elliott Hughesfc861622011-10-17 17:57:47 -0700461 } else if (option == "sensitiveThread") {
462 parsed->hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(options[i].second);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700463 } else if (option == "vfprintf") {
464 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
465 } else if (option == "exit") {
466 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
467 } else if (option == "abort") {
468 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700469 } else if (option == "host-prefix") {
470 parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second);
Elliott Hughesb3e66df2012-01-12 14:49:18 -0800471 } else if (option == "-Xgenregmap" || option == "-Xgc:precise") {
472 // We silently ignore these for backwards compatibility.
jeffhaob5e81852012-03-12 11:15:45 -0700473 } else if (option == "-Xmethod-trace") {
474 parsed->method_trace_ = true;
475 } else if (StartsWith(option, "-Xmethod-trace-file:")) {
476 parsed->method_trace_file_ = option.substr(strlen("-Xmethod-trace-file:"));
477 } else if (StartsWith(option, "-Xmethod-trace-file-size:")) {
478 parsed->method_trace_file_size_ = ParseIntegerOrDie(option);
Elliott Hughese119a362012-05-22 17:37:06 -0700479 } else if (option == "-Xprofile:threadcpuclock") {
480 Trace::SetDefaultClockSource(kProfilerClockSourceThreadCpu);
481 } else if (option == "-Xprofile:wallclock") {
482 Trace::SetDefaultClockSource(kProfilerClockSourceWall);
483 } else if (option == "-Xprofile:dualclock") {
484 Trace::SetDefaultClockSource(kProfilerClockSourceDual);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700485 } else {
486 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700487 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700488 LOG(ERROR) << "Unrecognized option " << option;
489 // TODO: this should exit, but for now tolerate unknown options
490 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700491 }
492 }
493 }
494
Brian Carlstrom223f20f2012-02-04 23:06:55 -0800495 if (!parsed->is_compiler_ && parsed->image_.empty()) {
496 parsed->image_ += GetAndroidRoot();
497 parsed->image_ += "/framework/boot.art";
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700498 }
jeffhaoc1160702011-10-27 15:48:45 -0700499 if (parsed->heap_growth_limit_ == 0) {
500 parsed->heap_growth_limit_ = parsed->heap_maximum_size_;
501 }
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700502
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700503 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700504}
505
506Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700507 // TODO: acquire a static mutex on Runtime to avoid racing.
508 if (Runtime::instance_ != NULL) {
509 return NULL;
510 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700511 instance_ = new Runtime;
512 if (!instance_->Init(options, ignore_unrecognized)) {
513 delete instance_;
514 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700515 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700516 return instance_;
517}
Elliott Hughes0af55432011-08-17 18:37:28 -0700518
Brian Carlstromaded5f72011-10-07 17:15:04 -0700519void CreateSystemClassLoader() {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800520 if (Runtime::Current()->UseCompileTimeClassPath()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700521 return;
522 }
523
524 Thread* self = Thread::Current();
525
526 // Must be in the kNative state for calling native methods.
Elliott Hughes34e06962012-04-09 13:55:55 -0700527 CHECK_EQ(self->GetState(), kNative);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700528
529 JNIEnv* env = self->GetJniEnv();
Elliott Hugheseac76672012-05-24 21:56:51 -0700530 jmethodID getSystemClassLoader = env->GetStaticMethodID(WellKnownClasses::java_lang_ClassLoader,
Brian Carlstromdf143242011-10-10 18:05:34 -0700531 "getSystemClassLoader",
532 "()Ljava/lang/ClassLoader;");
533 CHECK(getSystemClassLoader != NULL);
Elliott Hugheseac76672012-05-24 21:56:51 -0700534 ScopedLocalRef<jobject> class_loader(env, env->CallStaticObjectMethod(WellKnownClasses::java_lang_ClassLoader,
Brian Carlstromdf143242011-10-10 18:05:34 -0700535 getSystemClassLoader));
536 CHECK(class_loader.get() != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700537
Brian Carlstromdf143242011-10-10 18:05:34 -0700538 Thread::Current()->SetClassLoaderOverride(Decode<ClassLoader*>(env, class_loader.get()));
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500539
Elliott Hugheseac76672012-05-24 21:56:51 -0700540 jfieldID contextClassLoader = env->GetFieldID(WellKnownClasses::java_lang_Thread,
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500541 "contextClassLoader",
542 "Ljava/lang/ClassLoader;");
543 CHECK(contextClassLoader != NULL);
544 ScopedLocalRef<jobject> self_jobject(env, AddLocalReference<jobject>(env, self->GetPeer()));
545 env->SetObjectField(self_jobject.get(), contextClassLoader, class_loader.get());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700546}
547
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700548void Runtime::Start() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800549 VLOG(startup) << "Runtime::Start entering";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700550
551 CHECK(host_prefix_.empty()) << host_prefix_;
552
Elliott Hughes225f5a12012-06-11 11:23:48 -0700553 // Relocate the OatFiles (ELF images).
Logan Chien0c717dd2012-03-28 18:31:07 +0800554 class_linker_->RelocateExecutable();
555
Elliott Hughes225f5a12012-06-11 11:23:48 -0700556 // Restore main thread state to kNative as expected by native code.
557 Thread* self = Thread::Current();
558 self->SetState(kNative);
559
560 // Pre-allocate an OutOfMemoryError for the double-OOME case.
561 self->ThrowNewException("Ljava/lang/OutOfMemoryError;",
562 "OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack available");
563 pre_allocated_OutOfMemoryError_ = self->GetException();
564 self->ClearException();
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700565
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700566 started_ = true;
567
Brian Carlstromae826982011-11-09 01:33:42 -0800568 // InitNativeMethods needs to be after started_ so that the classes
569 // it touches will have methods linked to the oat file if necessary.
570 InitNativeMethods();
571
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500572 Thread::FinishStartup();
573
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700574 if (!is_zygote_) {
575 DidForkFromZygote();
576 }
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700577
Elliott Hughes85d15452011-09-16 17:33:01 -0700578 StartDaemonThreads();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700579
Brian Carlstromaded5f72011-10-07 17:15:04 -0700580 CreateSystemClassLoader();
581
Elliott Hughes225f5a12012-06-11 11:23:48 -0700582 self->GetJniEnv()->locals.AssertEmpty();
Elliott Hughes726079d2011-10-07 18:43:44 -0700583
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800584 VLOG(startup) << "Runtime::Start exiting";
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700585
586 finished_starting_ = true;
Elliott Hughes85d15452011-09-16 17:33:01 -0700587}
588
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700589void Runtime::DidForkFromZygote() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700590 is_zygote_ = false;
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700591
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700592 StartSignalCatcher();
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700593
594 // Start the JDWP thread. If the command-line debugger flags specified "suspend=y",
595 // this will pause the runtime, so we probably want this to come last.
596 Dbg::StartJdwp();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700597}
598
599void Runtime::StartSignalCatcher() {
600 if (!is_zygote_) {
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700601 signal_catcher_ = new SignalCatcher(stack_trace_file_);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700602 }
603}
604
Elliott Hughes85d15452011-09-16 17:33:01 -0700605void Runtime::StartDaemonThreads() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800606 VLOG(startup) << "Runtime::StartDaemonThreads entering";
Elliott Hughes85d15452011-09-16 17:33:01 -0700607
Elliott Hughes719b3232011-09-25 17:42:19 -0700608 Thread* self = Thread::Current();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700609
610 // Must be in the kNative state for calling native methods.
Elliott Hughes34e06962012-04-09 13:55:55 -0700611 CHECK_EQ(self->GetState(), kNative);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700612
Elliott Hughes719b3232011-09-25 17:42:19 -0700613 JNIEnv* env = self->GetJniEnv();
Elliott Hugheseac76672012-05-24 21:56:51 -0700614 env->CallStaticVoidMethod(WellKnownClasses::java_lang_Daemons, WellKnownClasses::java_lang_Daemons_start);
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800615 CHECK(!env->ExceptionCheck());
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700616
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800617 VLOG(startup) << "Runtime::StartDaemonThreads exiting";
Carl Shapiro61e019d2011-07-14 16:53:09 -0700618}
619
Elliott Hughes0af55432011-08-17 18:37:28 -0700620bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700621 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700622
Elliott Hughes90a33692011-08-30 13:27:07 -0700623 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
624 if (options.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700625 LOG(ERROR) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700626 return false;
627 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800628 VLOG(startup) << "Runtime::Init -verbose:startup enabled";
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700629
Elliott Hughes7c6169d2012-05-02 16:11:48 -0700630 QuasiAtomic::Startup();
631
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700632 SetJniGlobalsMax(options->jni_globals_max_);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800633 Monitor::Init(options->lock_profiling_threshold_, options->hook_is_sensitive_thread_);
Elliott Hughes32d6e1e2011-10-11 14:47:44 -0700634
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700635 host_prefix_ = options->host_prefix_;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800636 boot_class_path_string_ = options->boot_class_path_string_;
637 class_path_string_ = options->class_path_string_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700638 properties_ = options->properties_;
639
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800640 is_compiler_ = options->is_compiler_;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700641 is_zygote_ = options->is_zygote_;
642
Elliott Hughes0af55432011-08-17 18:37:28 -0700643 vfprintf_ = options->hook_vfprintf_;
644 exit_ = options->hook_exit_;
645 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700646
Elliott Hughesbe759c62011-09-08 19:38:21 -0700647 default_stack_size_ = options->stack_size_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700648 stack_trace_file_ = options->stack_trace_file_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700649
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700650 monitor_list_ = new MonitorList;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800651 thread_list_ = new ThreadList;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700652 intern_table_ = new InternTable;
653
Ian Rogers776ac1f2012-04-13 23:36:36 -0700654 verifier::MethodVerifier::InitGcMaps();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800655
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -0700656#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
Ian Rogers776ac1f2012-04-13 23:36:36 -0700657 verifier::MethodVerifier::InitInferredRegCategoryMaps();
Logan Chiendd361c92012-04-10 23:40:37 +0800658#endif
659
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800660 heap_ = new Heap(options->heap_initial_size_,
661 options->heap_growth_limit_,
662 options->heap_maximum_size_,
663 options->image_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700664
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700665 BlockSignals();
Elliott Hughes457005c2012-04-16 13:54:25 -0700666 InitPlatformSignalHandlers();
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700667
Elliott Hughesa0957642011-09-02 14:27:33 -0700668 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700669
Elliott Hughesbe759c62011-09-08 19:38:21 -0700670 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700671
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700672 // ClassLinker needs an attached thread, but we can't fully attach a thread
Elliott Hughes462c9442012-03-23 18:47:50 -0700673 // without creating objects. We can't supply a thread group yet; it will be fixed later.
674 Thread::Attach("main", false, NULL);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700675
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700676 // Set us to runnable so tools using a runtime can allocate and GC by default
Elliott Hughes34e06962012-04-09 13:55:55 -0700677 Thread::Current()->SetState(kRunnable);
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700678
Ian Rogers141d6222012-04-05 12:23:06 -0700679 // Now we're attached, we can take the heap lock and validate the heap.
680 GetHeap()->EnableObjectValidation();
681
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800682 CHECK_GE(GetHeap()->GetSpaces().size(), 1U);
683 if (GetHeap()->GetSpaces()[0]->IsImageSpace()) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800684 class_linker_ = ClassLinker::CreateFromImage(intern_table_);
685 } else {
686 CHECK(options->boot_class_path_ != NULL);
687 CHECK_NE(options->boot_class_path_->size(), 0U);
688 class_linker_ = ClassLinker::CreateFromCompiler(*options->boot_class_path_, intern_table_);
689 }
690 CHECK(class_linker_ != NULL);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700691
jeffhaob5e81852012-03-12 11:15:45 -0700692 method_trace_ = options->method_trace_;
693 method_trace_file_ = options->method_trace_file_;
694 method_trace_file_size_ = options->method_trace_file_size_;
695
696 if (options->method_trace_) {
697 Trace::Start(options->method_trace_file_.c_str(), -1, options->method_trace_file_size_, 0, false);
698 }
699
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800700 VLOG(startup) << "Runtime::Init exiting";
Carl Shapiro1fb86202011-06-27 17:43:13 -0700701 return true;
702}
703
Elliott Hughes038a8062011-09-18 14:12:41 -0700704void Runtime::InitNativeMethods() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800705 VLOG(startup) << "Runtime::InitNativeMethods entering";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700706 Thread* self = Thread::Current();
707 JNIEnv* env = self->GetJniEnv();
708
Elliott Hughes418d20f2011-09-22 14:00:39 -0700709 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Elliott Hughes34e06962012-04-09 13:55:55 -0700710 CHECK_EQ(self->GetState(), kNative);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700711
Elliott Hughes418d20f2011-09-22 14:00:39 -0700712 // First set up JniConstants, which is used by both the runtime's built-in native
713 // methods and libcore.
Elliott Hughesfea966e2011-09-22 10:26:20 -0700714 JniConstants::init(env);
Elliott Hugheseac76672012-05-24 21:56:51 -0700715 WellKnownClasses::Init(env);
Elliott Hughesfea966e2011-09-22 10:26:20 -0700716
Elliott Hughes418d20f2011-09-22 14:00:39 -0700717 // Then set up the native methods provided by the runtime itself.
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700718 RegisterRuntimeNativeMethods(env);
719
Elliott Hughes418d20f2011-09-22 14:00:39 -0700720 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
721 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
722 // the library that implements System.loadLibrary!
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700723 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800724 VLOG(startup) << "Runtime::InitNativeMethods exiting";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700725}
726
727void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
728#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Ian Rogers5167c972012-02-03 10:41:20 -0800729 // Register Throwable first so that registration of other native methods can throw exceptions
730 REGISTER(register_java_lang_Throwable);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700731 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700732 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700733 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700734 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700735 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700736 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700737 REGISTER(register_java_lang_Object);
738 REGISTER(register_java_lang_Runtime);
739 REGISTER(register_java_lang_String);
740 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700741 REGISTER(register_java_lang_Thread);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700742 REGISTER(register_java_lang_VMClassLoader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700743 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700744 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700745 REGISTER(register_java_lang_reflect_Field);
746 REGISTER(register_java_lang_reflect_Method);
Jesse Wilson95caa792011-10-12 18:14:17 -0400747 REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700748 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700749 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700750 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700751 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700752#undef REGISTER
753}
754
Elliott Hughesc967f782012-04-16 10:23:15 -0700755void Runtime::DumpForSigQuit(std::ostream& os) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700756 GetClassLinker()->DumpForSigQuit(os);
757 GetInternTable()->DumpForSigQuit(os);
Elliott Hughesae80b492012-04-24 10:43:17 -0700758 GetJavaVM()->DumpForSigQuit(os);
Elliott Hughesc967f782012-04-16 10:23:15 -0700759 GetHeap()->DumpForSigQuit(os);
Elliott Hughes42ee1422011-09-06 12:33:32 -0700760 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700761
Elliott Hughesc967f782012-04-16 10:23:15 -0700762 thread_list_->DumpForSigQuit(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700763}
764
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800765void Runtime::DumpLockHolders(std::ostream& os) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800766 pid_t heap_lock_owner = GetHeap()->GetLockOwner();
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800767 pid_t thread_list_lock_owner = GetThreadList()->GetLockOwner();
768 pid_t classes_lock_owner = GetClassLinker()->GetClassesLockOwner();
769 pid_t dex_lock_owner = GetClassLinker()->GetDexLockOwner();
770 if ((heap_lock_owner | thread_list_lock_owner | classes_lock_owner | dex_lock_owner) != 0) {
771 os << "Heap lock owner tid: " << heap_lock_owner << "\n"
772 << "ThreadList lock owner tid: " << thread_list_lock_owner << "\n"
773 << "ClassLinker classes lock owner tid: " << classes_lock_owner << "\n"
774 << "ClassLinker dex lock owner tid: " << dex_lock_owner << "\n";
775 }
776}
777
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700778void Runtime::SetStatsEnabled(bool new_state) {
779 if (new_state == true) {
780 GetStats()->Clear(~0);
781 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
782 Thread::Current()->GetStats()->Clear(~0);
783 }
784 stats_enabled_ = new_state;
785}
786
787void Runtime::ResetStats(int kinds) {
788 GetStats()->Clear(kinds & 0xffff);
789 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
790 Thread::Current()->GetStats()->Clear(kinds >> 16);
791}
792
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700793int32_t Runtime::GetStat(int kind) {
794 RuntimeStats* stats;
795 if (kind < (1<<16)) {
796 stats = GetStats();
797 } else {
798 stats = Thread::Current()->GetStats();
799 kind >>= 16;
800 }
801 switch (kind) {
802 case KIND_ALLOCATED_OBJECTS:
803 return stats->allocated_objects;
804 case KIND_ALLOCATED_BYTES:
805 return stats->allocated_bytes;
806 case KIND_FREED_OBJECTS:
807 return stats->freed_objects;
808 case KIND_FREED_BYTES:
809 return stats->freed_bytes;
810 case KIND_GC_INVOCATIONS:
811 return stats->gc_for_alloc_count;
812 case KIND_CLASS_INIT_COUNT:
813 return stats->class_init_count;
814 case KIND_CLASS_INIT_TIME:
815 // Convert ns to us, reduce to 32 bits.
Elliott Hughes398f64b2012-03-26 18:05:48 -0700816 return static_cast<int>(stats->class_init_time_ns / 1000);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700817 case KIND_EXT_ALLOCATED_OBJECTS:
818 case KIND_EXT_ALLOCATED_BYTES:
819 case KIND_EXT_FREED_OBJECTS:
820 case KIND_EXT_FREED_BYTES:
821 return 0; // backward compatibility
822 default:
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700823 LOG(FATAL) << "Unknown statistic " << kind;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700824 return -1; // unreachable
825 }
826}
827
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700828void Runtime::BlockSignals() {
Elliott Hughes457005c2012-04-16 13:54:25 -0700829 SignalSet signals;
830 signals.Add(SIGPIPE);
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700831 // SIGQUIT is used to dump the runtime's state (including stack traces).
Elliott Hughes457005c2012-04-16 13:54:25 -0700832 signals.Add(SIGQUIT);
Elliott Hughes08795042012-04-03 14:48:52 -0700833 // SIGUSR1 is used to initiate a GC.
Elliott Hughes457005c2012-04-16 13:54:25 -0700834 signals.Add(SIGUSR1);
835 signals.Block();
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700836}
837
Elliott Hughes462c9442012-03-23 18:47:50 -0700838void Runtime::AttachCurrentThread(const char* thread_name, bool as_daemon, Object* thread_group) {
839 Thread::Attach(thread_name, as_daemon, thread_group);
Elliott Hughes22869a92012-03-27 14:08:24 -0700840 if (thread_name == NULL) {
841 LOG(WARNING) << *Thread::Current() << " attached without supplying a name";
842 }
Carl Shapiro61e019d2011-07-14 16:53:09 -0700843}
844
Elliott Hughesd92bec42011-09-02 17:04:36 -0700845void Runtime::DetachCurrentThread() {
Brian Carlstrom4d571432012-05-16 00:21:41 -0700846 Thread* self = Thread::Current();
847 if (self == NULL) {
848 LOG(FATAL) << "attempting to detach thread that is not attached";
849 }
850 if (self->GetTopOfStack().GetSP() != NULL) {
Elliott Hughes22869a92012-03-27 14:08:24 -0700851 LOG(FATAL) << *Thread::Current() << " attempting to detach while still running code";
852 }
Elliott Hughes02b48d12011-09-07 17:15:51 -0700853 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700854}
855
Brian Carlstrome24fa612011-09-29 00:53:55 -0700856void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700857 Dbg::VisitRoots(visitor, arg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700858 class_linker_->VisitRoots(visitor, arg);
859 intern_table_->VisitRoots(visitor, arg);
860 java_vm_->VisitRoots(visitor, arg);
861 thread_list_->VisitRoots(visitor, arg);
Elliott Hughes225f5a12012-06-11 11:23:48 -0700862 if (pre_allocated_OutOfMemoryError_ != NULL) {
863 visitor(pre_allocated_OutOfMemoryError_, arg);
864 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700865 visitor(jni_stub_array_, arg);
866 visitor(abstract_method_error_stub_array_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700867 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
868 visitor(resolution_stub_array_[i], arg);
869 }
Ian Rogers19846512012-02-24 11:42:47 -0800870 visitor(resolution_method_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700871 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
Elliott Hughes225f5a12012-06-11 11:23:48 -0700872 visitor(callee_save_methods_[i], arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700873 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700874}
875
Ian Rogers169c9a72011-11-13 20:13:17 -0800876void Runtime::SetJniDlsymLookupStub(ByteArray* jni_stub_array) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700877 CHECK(jni_stub_array != NULL) << " jni_stub_array=" << jni_stub_array;
878 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array)
879 << "jni_stub_array_=" << jni_stub_array_ << " jni_stub_array=" << jni_stub_array;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700880 jni_stub_array_ = jni_stub_array;
881}
882
Brian Carlstrome24fa612011-09-29 00:53:55 -0700883void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) {
884 CHECK(abstract_method_error_stub_array != NULL);
885 CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array);
886 abstract_method_error_stub_array_ = abstract_method_error_stub_array;
887}
888
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700889void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700890 CHECK(resolution_stub_array != NULL);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700891 CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array);
892 resolution_stub_array_[type] = resolution_stub_array;
Ian Rogersad25ac52011-10-04 19:13:33 -0700893}
894
Ian Rogers19846512012-02-24 11:42:47 -0800895Method* Runtime::CreateResolutionMethod() {
896 Class* method_class = Method::GetMethodClass();
897 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
898 method->SetDeclaringClass(method_class);
899 // TODO: use a special method for resolution method saves
900 method->SetDexMethodIndex(DexFile::kDexNoIndex16);
901 ByteArray* unknown_resolution_stub = GetResolutionStubArray(kUnknownMethod);
902 CHECK(unknown_resolution_stub != NULL);
903 method->SetCode(unknown_resolution_stub->GetData());
904 return method.get();
905}
906
Brian Carlstromae826982011-11-09 01:33:42 -0800907Method* Runtime::CreateCalleeSaveMethod(InstructionSet instruction_set, CalleeSaveType type) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700908 Class* method_class = Method::GetMethodClass();
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700909 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
Ian Rogersff1ed472011-09-20 13:46:24 -0700910 method->SetDeclaringClass(method_class);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800911 // TODO: use a special method for callee saves
Ian Rogers19846512012-02-24 11:42:47 -0800912 method->SetDexMethodIndex(DexFile::kDexNoIndex16);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700913 method->SetCode(NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800914 if ((instruction_set == kThumb2) || (instruction_set == kArm)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700915 uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6) | (1 << art::arm::R7) |
916 (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11);
917 uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3);
918 uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9);
919 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills :0) |
920 (type == kSaveAll ? all_spills :0) | (1 << art::arm::LR);
921 uint32_t fp_all_spills = (1 << art::arm::S0) | (1 << art::arm::S1) | (1 << art::arm::S2) |
922 (1 << art::arm::S3) | (1 << art::arm::S4) | (1 << art::arm::S5) |
923 (1 << art::arm::S6) | (1 << art::arm::S7) | (1 << art::arm::S8) |
924 (1 << art::arm::S9) | (1 << art::arm::S10) | (1 << art::arm::S11) |
925 (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) |
926 (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) |
927 (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) |
928 (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) |
929 (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) |
930 (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
931 (1 << art::arm::S30) | (1 << art::arm::S31);
932 uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
933 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
934 __builtin_popcount(fp_spills) /* fprs */ +
935 1 /* Method* */) * kPointerSize, kStackAlignment);
Ian Rogers15fdb8c2011-09-25 15:45:07 -0700936 method->SetFrameSizeInBytes(frame_size);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700937 method->SetCoreSpillMask(core_spills);
938 method->SetFpSpillMask(fp_spills);
Brian Carlstromae826982011-11-09 01:33:42 -0800939 } else if (instruction_set == kX86) {
Ian Rogers7caad772012-03-30 01:07:54 -0700940 uint32_t ref_spills = (1 << art::x86::EBP) | (1 << art::x86::ESI) | (1 << art::x86::EDI);
941 uint32_t arg_spills = (1 << art::x86::ECX) | (1 << art::x86::EDX) | (1 << art::x86::EBX);
942 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills : 0) |
943 (1 << art::x86::kNumberOfCpuRegisters); // fake return address callee save
944 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
945 1 /* Method* */) * kPointerSize, kStackAlignment);
946 method->SetFrameSizeInBytes(frame_size);
947 method->SetCoreSpillMask(core_spills);
Ian Rogersff1ed472011-09-20 13:46:24 -0700948 method->SetFpSpillMask(0);
949 } else {
950 UNIMPLEMENTED(FATAL);
951 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700952 return method.get();
Ian Rogersff1ed472011-09-20 13:46:24 -0700953}
954
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700955void Runtime::SetCalleeSaveMethod(Method* method, CalleeSaveType type) {
956 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
Elliott Hughes225f5a12012-06-11 11:23:48 -0700957 callee_save_methods_[type] = method;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700958}
959
jeffhao2692b572011-12-16 15:42:28 -0800960void Runtime::EnableMethodTracing(Trace* tracer) {
961 CHECK(!IsMethodTracingActive());
962 tracer_ = tracer;
963}
964
965void Runtime::DisableMethodTracing() {
966 CHECK(IsMethodTracingActive());
967 delete tracer_;
968 tracer_ = NULL;
969}
970
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800971const std::vector<const DexFile*>& Runtime::GetCompileTimeClassPath(const ClassLoader* class_loader) {
972 if (class_loader == NULL) {
973 return GetClassLinker()->GetBootClassPath();
974 }
975 CHECK(UseCompileTimeClassPath());
976 CompileTimeClassPaths::const_iterator it = compile_time_class_paths_.find(class_loader);
977 CHECK(it != compile_time_class_paths_.end());
978 return it->second;
979}
980
981void Runtime::SetCompileTimeClassPath(const ClassLoader* class_loader, std::vector<const DexFile*>& class_path) {
982 CHECK(!IsStarted());
983 use_compile_time_class_path_ = true;
Elliott Hughesa0e18062012-04-13 15:59:59 -0700984 compile_time_class_paths_.Put(class_loader, class_path);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800985}
986
Carl Shapiro1fb86202011-06-27 17:43:13 -0700987} // namespace art