blob: a94a93a04757342aab042ac731b21191edf230c5 [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>
20
Elliott Hughesffe67362011-07-17 12:09:27 -070021#include <cstdio>
22#include <cstdlib>
Brian Carlstrom8a436592011-08-15 21:27:23 -070023#include <limits>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070024#include <vector>
Elliott Hughesffe67362011-07-17 12:09:27 -070025
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070026#include "class_linker.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070027#include "class_loader.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070028#include "constants_arm.h"
29#include "constants_x86.h"
Elliott Hughes3bb81562011-10-21 18:52:59 -070030#include "debugger.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070031#include "heap.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070032#include "image.h"
Elliott Hughescf4c6c42011-09-01 15:16:42 -070033#include "intern_table.h"
Elliott Hughesc5f7c912011-08-18 14:00:42 -070034#include "jni_internal.h"
Elliott Hughes32d6e1e2011-10-11 14:47:44 -070035#include "monitor.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070036#include "oat_file.h"
Elliott Hughes726079d2011-10-07 18:43:44 -070037#include "ScopedLocalRef.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070038#include "signal_catcher.h"
Elliott Hughes457005c2012-04-16 13:54:25 -070039#include "signal_set.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070040#include "space.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070041#include "thread.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070042#include "thread_list.h"
jeffhaob5e81852012-03-12 11:15:45 -070043#include "trace.h"
Elliott Hughes3bb81562011-10-21 18:52:59 -070044#include "UniquePtr.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070045#include "verifier/method_verifier.h"
Carl Shapiro61e019d2011-07-14 16:53:09 -070046
Elliott Hughes90a33692011-08-30 13:27:07 -070047// TODO: this drags in cutil/log.h, which conflicts with our logging.h.
48#include "JniConstants.h"
49
Carl Shapiro1fb86202011-06-27 17:43:13 -070050namespace art {
51
Carl Shapiro2ed144c2011-07-26 16:52:08 -070052Runtime* Runtime::instance_ = NULL;
53
Elliott Hughesdcc24742011-09-07 14:02:44 -070054Runtime::Runtime()
Elliott Hughesd9c67be2012-02-02 19:54:06 -080055 : is_compiler_(false),
56 is_zygote_(false),
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -070057 default_stack_size_(Thread::kDefaultStackSize),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080058 heap_(NULL),
Elliott Hughesc33a32b2011-10-11 18:18:07 -070059 monitor_list_(NULL),
Elliott Hughesdcc24742011-09-07 14:02:44 -070060 thread_list_(NULL),
61 intern_table_(NULL),
62 class_linker_(NULL),
63 signal_catcher_(NULL),
64 java_vm_(NULL),
Brian Carlstrom16192862011-09-12 17:50:06 -070065 jni_stub_array_(NULL),
Brian Carlstrome24fa612011-09-29 00:53:55 -070066 abstract_method_error_stub_array_(NULL),
Ian Rogers19846512012-02-24 11:42:47 -080067 resolution_method_(NULL),
Ian Rogers7c3757f2012-02-15 17:18:53 -080068 system_class_loader_(NULL),
Elliott Hughes6b355752012-01-13 16:49:08 -080069 shutting_down_(false),
Elliott Hughesdcc24742011-09-07 14:02:44 -070070 started_(false),
71 vfprintf_(NULL),
72 exit_(NULL),
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070073 abort_(NULL),
jeffhao2692b572011-12-16 15:42:28 -080074 stats_enabled_(false),
Ian Rogers776ac1f2012-04-13 23:36:36 -070075 method_trace_(0),
76 method_trace_file_size_(0),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080077 tracer_(NULL),
78 use_compile_time_class_path_(false) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -070079 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
80 resolution_stub_array_[i] = NULL;
81 }
82 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
83 callee_save_method_[i] = NULL;
84 }
Elliott Hughesdcc24742011-09-07 14:02:44 -070085}
86
Carl Shapiro61e019d2011-07-14 16:53:09 -070087Runtime::~Runtime() {
Elliott Hughes6b355752012-01-13 16:49:08 -080088 shutting_down_ = true;
89
jeffhaob5e81852012-03-12 11:15:45 -070090 if (IsMethodTracingActive()) {
91 Trace::Shutdown();
92 }
93
Elliott Hughes5fe594f2011-09-08 12:33:17 -070094 // Make sure our internal threads are dead before we start tearing down things they're using.
Elliott Hughese52e49b2012-04-02 16:05:44 -070095 Dbg::StopJdwp();
Elliott Hughes5fe594f2011-09-08 12:33:17 -070096 delete signal_catcher_;
97
Elliott Hughes038a8062011-09-18 14:12:41 -070098 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
Elliott Hughes93e74e82011-09-13 11:07:03 -070099 delete thread_list_;
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700100 delete monitor_list_;
Elliott Hughes93e74e82011-09-13 11:07:03 -0700101
Carl Shapiro61e019d2011-07-14 16:53:09 -0700102 delete class_linker_;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800103 delete heap_;
Logan Chiendd361c92012-04-10 23:40:37 +0800104#if defined(ART_USE_LLVM_COMPILER)
Ian Rogers776ac1f2012-04-13 23:36:36 -0700105 verifier::MethodVerifier::DeleteInferredRegCategoryMaps();
Logan Chiendd361c92012-04-10 23:40:37 +0800106#endif
Ian Rogers776ac1f2012-04-13 23:36:36 -0700107 verifier::MethodVerifier::DeleteGcMaps();
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700108 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700109 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700110 Thread::Shutdown();
Carl Shapiro4acf4642011-07-26 18:54:13 -0700111 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700112 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -0700113 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700114}
115
Elliott Hughescac6cc72011-11-03 20:31:21 -0700116static bool gAborting = false;
117
Elliott Hughese0918552011-10-28 17:18:29 -0700118struct AbortState {
119 void Dump(std::ostream& os) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700120 if (gAborting) {
121 os << "Runtime aborting --- recursively, so no thread-specific detail!\n";
122 return;
123 }
124 gAborting = true;
Elliott Hughese0918552011-10-28 17:18:29 -0700125 os << "Runtime aborting...\n";
Elliott Hughes6c7d2442012-02-01 18:40:47 -0800126 if (Runtime::Current() == NULL) {
127 os << "(Runtime does not yet exist!)\n";
128 return;
129 }
Elliott Hughese0918552011-10-28 17:18:29 -0700130 Thread* self = Thread::Current();
131 if (self == NULL) {
132 os << "(Aborting thread was not attached to runtime!)\n";
133 } else {
Elliott Hughes899e7892012-01-24 14:57:32 -0800134 self->Dump(os);
135 if (self->IsExceptionPending()) {
136 os << "Pending " << PrettyTypeOf(self->GetException()) << " on thread:\n"
137 << self->GetException()->Dump();
138 }
Elliott Hughese0918552011-10-28 17:18:29 -0700139 }
140 }
141};
142
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800143static Mutex& GetAbortLock() {
144 static Mutex abort_lock("abort lock");
145 return abort_lock;
146}
147
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700148void Runtime::Abort() {
Elliott Hughes131aef82012-01-27 11:53:35 -0800149 // Ensure that we don't have multiple threads trying to abort at once,
150 // which would result in significantly worse diagnostics.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800151 MutexLock mu(GetAbortLock());
Elliott Hughes131aef82012-01-27 11:53:35 -0800152
Elliott Hughesffe67362011-07-17 12:09:27 -0700153 // Get any pending output out of the way.
154 fflush(NULL);
155
156 // Many people have difficulty distinguish aborts from crashes,
157 // so be explicit.
Elliott Hughese0918552011-10-28 17:18:29 -0700158 AbortState state;
Elliott Hughes9ee5f9c2012-02-03 18:33:16 -0800159 LOG(INTERNAL_FATAL) << Dumpable<AbortState>(state);
Elliott Hughesffe67362011-07-17 12:09:27 -0700160
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700161 // Call the abort hook if we have one.
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700162 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700163 LOG(INTERNAL_FATAL) << "Calling abort hook...";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700164 Runtime::Current()->abort_();
165 // notreached
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700166 LOG(INTERNAL_FATAL) << "Unexpectedly returned from abort hook!";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700167 }
168
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700169 // TODO: finish merging patches to fix abort(3) in bionic, then lose this!
170 // Bionic doesn't implement POSIX semantics for abort(3) in a multi-threaded
171 // process, so if we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -0700172 // receive SIGABRT. debuggerd dumps the stack trace of the main
173 // thread, whether or not that was the thread that failed. By
174 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -0700175 // fault in the current thread, and get a useful log from debuggerd.
Elliott Hughes81ff3182012-03-23 20:35:56 -0700176 // We can also trivially tell the difference between a crash and
Elliott Hughesffe67362011-07-17 12:09:27 -0700177 // a deliberate abort by looking at the fault address.
178 *reinterpret_cast<char*>(0xdeadd00d) = 38;
179 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -0700180 // notreached
181}
182
Elliott Hughesbf86d042011-08-31 17:53:14 -0700183void Runtime::CallExitHook(jint status) {
184 if (exit_ != NULL) {
Elliott Hughes34e06962012-04-09 13:55:55 -0700185 ScopedThreadStateChange tsc(Thread::Current(), kNative);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700186 exit_(status);
187 LOG(WARNING) << "Exit hook returned instead of exiting!";
188 }
189}
190
Brian Carlstrom8a436592011-08-15 21:27:23 -0700191// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
192// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
193// [gG] gigabytes.
194//
195// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700196// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
197// of 1024.
198//
199// The spec says the -Xmx and -Xms options must be multiples of 1024. It
200// doesn't say anything about -Xss.
201//
202// Returns 0 (a useless size) if "s" is malformed or specifies a low or
203// non-evenly-divisible value.
204//
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800205size_t ParseMemoryOption(const char* s, size_t div) {
Brian Carlstrom8a436592011-08-15 21:27:23 -0700206 // strtoul accepts a leading [+-], which we don't want,
207 // so make sure our string starts with a decimal digit.
208 if (isdigit(*s)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700209 char* s2;
210 size_t val = strtoul(s, &s2, 10);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700211 if (s2 != s) {
212 // s2 should be pointing just after the number.
213 // If this is the end of the string, the user
214 // has specified a number of bytes. Otherwise,
215 // there should be exactly one more character
216 // that specifies a multiplier.
217 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700218 // The remainder of the string is either a single multiplier
219 // character, or nothing to indicate that the value is in
220 // bytes.
221 char c = *s2++;
222 if (*s2 == '\0') {
223 size_t mul;
224 if (c == '\0') {
225 mul = 1;
226 } else if (c == 'k' || c == 'K') {
227 mul = KB;
228 } else if (c == 'm' || c == 'M') {
229 mul = MB;
230 } else if (c == 'g' || c == 'G') {
231 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700232 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700233 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700234 return 0;
235 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700236
237 if (val <= std::numeric_limits<size_t>::max() / mul) {
238 val *= mul;
239 } else {
240 // Clamp to a multiple of 1024.
241 val = std::numeric_limits<size_t>::max() & ~(1024-1);
242 }
243 } else {
244 // There's more than one character after the numeric part.
245 return 0;
246 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700247 }
248 // The man page says that a -Xm value must be a multiple of 1024.
249 if (val % div == 0) {
250 return val;
251 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700252 }
253 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700254 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700255}
256
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800257size_t ParseIntegerOrDie(const std::string& s) {
258 std::string::size_type colon = s.find(':');
259 if (colon == std::string::npos) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700260 LOG(FATAL) << "Missing integer: " << s;
261 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800262 const char* begin = &s[colon + 1];
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700263 char* end;
264 size_t result = strtoul(begin, &end, 10);
265 if (begin == end || *end != '\0') {
266 LOG(FATAL) << "Failed to parse integer in: " << s;
267 }
268 return result;
269}
270
Elliott Hughes0af55432011-08-17 18:37:28 -0700271void LoadJniLibrary(JavaVMExt* vm, const char* name) {
Elliott Hughes6cc332e2012-01-20 22:59:20 -0800272 std::string mapped_name(StringPrintf(OS_SHARED_LIB_FORMAT_STR, name));
Elliott Hughes75770752011-08-24 17:52:38 -0700273 std::string reason;
274 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700275 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
276 << reason;
277 }
278}
279
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700280Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700281 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800282 const char* boot_class_path_string = getenv("BOOTCLASSPATH");
283 if (boot_class_path_string != NULL) {
284 parsed->boot_class_path_string_ = boot_class_path_string;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700285 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800286 const char* class_path_string = getenv("CLASSPATH");
287 if (class_path_string != NULL) {
288 parsed->class_path_string_ = class_path_string;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700289 }
Elliott Hughes67d92002012-03-26 15:08:51 -0700290 // -Xcheck:jni is off by default for regular builds but on by default in debug builds.
291 parsed->check_jni_ = kIsDebugBuild;
Elliott Hughes85d15452011-09-16 17:33:01 -0700292
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700293 parsed->heap_initial_size_ = Heap::kInitialSize;
294 parsed->heap_maximum_size_ = Heap::kMaximumSize;
jeffhaoc1160702011-10-27 15:48:45 -0700295 parsed->heap_growth_limit_ = 0; // 0 means no growth limit
Brian Carlstromf734cf52011-08-17 16:28:14 -0700296 parsed->stack_size_ = Thread::kDefaultStackSize;
297
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800298 parsed->is_compiler_ = false;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700299 parsed->is_zygote_ = false;
300
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700301 parsed->jni_globals_max_ = 0;
Elliott Hughesfc861622011-10-17 17:57:47 -0700302 parsed->lock_profiling_threshold_ = 0;
303 parsed->hook_is_sensitive_thread_ = NULL;
304
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700305 parsed->hook_vfprintf_ = vfprintf;
306 parsed->hook_exit_ = exit;
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700307 parsed->hook_abort_ = NULL; // We don't call abort(3) by default; see Runtime::Abort.
308#if defined(__APPLE__)
309 parsed->hook_abort_ = abort; // On the Mac, abort(3) gives better results; see Runtime::InitPlatformSignalHandlers.
310#endif
Brian Carlstrom8a436592011-08-15 21:27:23 -0700311
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800312// gLogVerbosity.class_linker = true; // TODO: don't check this in!
313// gLogVerbosity.compiler = true; // TODO: don't check this in!
314// gLogVerbosity.heap = true; // TODO: don't check this in!
315// gLogVerbosity.gc = true; // TODO: don't check this in!
316// gLogVerbosity.jdwp = true; // TODO: don't check this in!
317// gLogVerbosity.jni = true; // TODO: don't check this in!
318// gLogVerbosity.monitor = true; // TODO: don't check this in!
319// gLogVerbosity.startup = true; // TODO: don't check this in!
320// gLogVerbosity.third_party_jni = true; // TODO: don't check this in!
321// gLogVerbosity.threads = true; // TODO: don't check this in!
322
jeffhaob5e81852012-03-12 11:15:45 -0700323 parsed->method_trace_ = false;
324 parsed->method_trace_file_ = "/data/method-trace-file.bin";
325 parsed->method_trace_file_size_ = 10 * MB;
326
Brian Carlstrom8a436592011-08-15 21:27:23 -0700327 for (size_t i = 0; i < options.size(); ++i) {
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800328 const std::string option(options[i].first);
Brian Carlstrom0796af02011-10-12 14:31:45 -0700329 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700330 LOG(INFO) << "option[" << i << "]=" << option;
331 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800332 if (StartsWith(option, "-Xbootclasspath:")) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800333 parsed->boot_class_path_string_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700334 } else if (option == "-classpath" || option == "-cp") {
335 // TODO: support -Djava.class.path
336 i++;
337 if (i == options.size()) {
338 // TODO: usage
339 LOG(FATAL) << "Missing required class path value for " << option;
340 return NULL;
341 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800342 const StringPiece& value = options[i].first;
343 parsed->class_path_string_ = value.data();
344 } else if (option == "bootclasspath") {
345 parsed->boot_class_path_
346 = reinterpret_cast<const std::vector<const DexFile*>*>(options[i].second);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800347 } else if (StartsWith(option, "-Ximage:")) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800348 parsed->image_ = option.substr(strlen("-Ximage:")).data();
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800349 } else if (StartsWith(option, "-Xcheck:jni")) {
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700350 parsed->check_jni_ = true;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800351 } else if (StartsWith(option, "-Xrunjdwp:") || StartsWith(option, "-agentlib:jdwp=")) {
352 std::string tail(option.substr(option[1] == 'X' ? 10 : 15));
Elliott Hughes3bb81562011-10-21 18:52:59 -0700353 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
354 LOG(FATAL) << "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
355 << "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n";
356 return NULL;
357 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800358 } else if (StartsWith(option, "-Xms")) {
359 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).c_str(), 1024);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700360 if (size == 0) {
361 if (ignore_unrecognized) {
362 continue;
363 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700364 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700365 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700366 return NULL;
367 }
368 parsed->heap_initial_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800369 } else if (StartsWith(option, "-Xmx")) {
370 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).c_str(), 1024);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700371 if (size == 0) {
372 if (ignore_unrecognized) {
373 continue;
374 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700375 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700376 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700377 return NULL;
378 }
379 parsed->heap_maximum_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800380 } else if (StartsWith(option, "-XX:HeapGrowthLimit=")) {
381 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).c_str(), 1024);
jeffhaoc1160702011-10-27 15:48:45 -0700382 if (size == 0) {
383 if (ignore_unrecognized) {
384 continue;
385 }
386 // TODO: usage
387 LOG(FATAL) << "Failed to parse " << option;
388 return NULL;
389 }
390 parsed->heap_growth_limit_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800391 } else if (StartsWith(option, "-Xss")) {
392 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).c_str(), 1);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700393 if (size == 0) {
394 if (ignore_unrecognized) {
395 continue;
396 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700397 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700398 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700399 return NULL;
400 }
401 parsed->stack_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800402 } else if (StartsWith(option, "-D")) {
403 parsed->properties_.push_back(option.substr(strlen("-D")));
404 } else if (StartsWith(option, "-Xjnitrace:")) {
405 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:"));
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700406 } else if (option == "compiler") {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800407 parsed->is_compiler_ = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700408 } else if (option == "-Xzygote") {
409 parsed->is_zygote_ = true;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800410 } else if (StartsWith(option, "-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700411 std::vector<std::string> verbose_options;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800412 Split(option.substr(strlen("-verbose:")), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700413 for (size_t i = 0; i < verbose_options.size(); ++i) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800414 if (verbose_options[i] == "class") {
415 gLogVerbosity.class_linker = true;
416 } else if (verbose_options[i] == "compiler") {
417 gLogVerbosity.compiler = true;
418 } else if (verbose_options[i] == "heap") {
419 gLogVerbosity.heap = true;
420 } else if (verbose_options[i] == "gc") {
421 gLogVerbosity.gc = true;
422 } else if (verbose_options[i] == "jdwp") {
423 gLogVerbosity.jdwp = true;
424 } else if (verbose_options[i] == "jni") {
425 gLogVerbosity.jni = true;
426 } else if (verbose_options[i] == "monitor") {
427 gLogVerbosity.monitor = true;
428 } else if (verbose_options[i] == "startup") {
429 gLogVerbosity.startup = true;
430 } else if (verbose_options[i] == "third-party-jni") {
431 gLogVerbosity.third_party_jni = true;
432 } else if (verbose_options[i] == "threads") {
433 gLogVerbosity.threads = true;
434 } else {
435 LOG(WARNING) << "Ignoring unknown -verbose option: " << verbose_options[i];
436 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700437 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800438 } else if (StartsWith(option, "-Xjnigreflimit:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700439 parsed->jni_globals_max_ = ParseIntegerOrDie(option);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800440 } else if (StartsWith(option, "-Xlockprofthreshold:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700441 parsed->lock_profiling_threshold_ = ParseIntegerOrDie(option);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800442 } else if (StartsWith(option, "-Xstacktracefile:")) {
Elliott Hughes67d92002012-03-26 15:08:51 -0700443 if (kIsDebugBuild) {
444 // Ignore the zygote and always show stack traces in debug builds.
445 } else {
446 parsed->stack_trace_file_ = option.substr(strlen("-Xstacktracefile:"));
447 }
Elliott Hughesfc861622011-10-17 17:57:47 -0700448 } else if (option == "sensitiveThread") {
449 parsed->hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(options[i].second);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700450 } else if (option == "vfprintf") {
451 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
452 } else if (option == "exit") {
453 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
454 } else if (option == "abort") {
455 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700456 } else if (option == "host-prefix") {
457 parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second);
Elliott Hughesb3e66df2012-01-12 14:49:18 -0800458 } else if (option == "-Xgenregmap" || option == "-Xgc:precise") {
459 // We silently ignore these for backwards compatibility.
jeffhaob5e81852012-03-12 11:15:45 -0700460 } else if (option == "-Xmethod-trace") {
461 parsed->method_trace_ = true;
462 } else if (StartsWith(option, "-Xmethod-trace-file:")) {
463 parsed->method_trace_file_ = option.substr(strlen("-Xmethod-trace-file:"));
464 } else if (StartsWith(option, "-Xmethod-trace-file-size:")) {
465 parsed->method_trace_file_size_ = ParseIntegerOrDie(option);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700466 } else {
467 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700468 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700469 LOG(ERROR) << "Unrecognized option " << option;
470 // TODO: this should exit, but for now tolerate unknown options
471 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700472 }
473 }
474 }
475
Brian Carlstrom223f20f2012-02-04 23:06:55 -0800476 if (!parsed->is_compiler_ && parsed->image_.empty()) {
477 parsed->image_ += GetAndroidRoot();
478 parsed->image_ += "/framework/boot.art";
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700479 }
jeffhaoc1160702011-10-27 15:48:45 -0700480 if (parsed->heap_growth_limit_ == 0) {
481 parsed->heap_growth_limit_ = parsed->heap_maximum_size_;
482 }
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700483
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700484 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700485}
486
487Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700488 // TODO: acquire a static mutex on Runtime to avoid racing.
489 if (Runtime::instance_ != NULL) {
490 return NULL;
491 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700492 instance_ = new Runtime;
493 if (!instance_->Init(options, ignore_unrecognized)) {
494 delete instance_;
495 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700496 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700497 return instance_;
498}
Elliott Hughes0af55432011-08-17 18:37:28 -0700499
Brian Carlstromaded5f72011-10-07 17:15:04 -0700500void CreateSystemClassLoader() {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800501 if (Runtime::Current()->UseCompileTimeClassPath()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700502 return;
503 }
504
505 Thread* self = Thread::Current();
506
507 // Must be in the kNative state for calling native methods.
Elliott Hughes34e06962012-04-09 13:55:55 -0700508 CHECK_EQ(self->GetState(), kNative);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700509
510 JNIEnv* env = self->GetJniEnv();
Brian Carlstromdf143242011-10-10 18:05:34 -0700511 ScopedLocalRef<jclass> ClassLoader_class(env, env->FindClass("java/lang/ClassLoader"));
512 CHECK(ClassLoader_class.get() != NULL);
513 jmethodID getSystemClassLoader = env->GetStaticMethodID(ClassLoader_class.get(),
514 "getSystemClassLoader",
515 "()Ljava/lang/ClassLoader;");
516 CHECK(getSystemClassLoader != NULL);
517 ScopedLocalRef<jobject> class_loader(env, env->CallStaticObjectMethod(ClassLoader_class.get(),
518 getSystemClassLoader));
519 CHECK(class_loader.get() != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700520
Brian Carlstromdf143242011-10-10 18:05:34 -0700521 Thread::Current()->SetClassLoaderOverride(Decode<ClassLoader*>(env, class_loader.get()));
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500522
523 ScopedLocalRef<jclass> Thread_class(env, env->FindClass("java/lang/Thread"));
524 CHECK(Thread_class.get() != NULL);
525 jfieldID contextClassLoader = env->GetFieldID(Thread_class.get(),
526 "contextClassLoader",
527 "Ljava/lang/ClassLoader;");
528 CHECK(contextClassLoader != NULL);
529 ScopedLocalRef<jobject> self_jobject(env, AddLocalReference<jobject>(env, self->GetPeer()));
530 env->SetObjectField(self_jobject.get(), contextClassLoader, class_loader.get());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700531}
532
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700533void Runtime::Start() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800534 VLOG(startup) << "Runtime::Start entering";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700535
536 CHECK(host_prefix_.empty()) << host_prefix_;
537
Logan Chien0c717dd2012-03-28 18:31:07 +0800538 // Relocate the OatFiles (ELF images)
539 class_linker_->RelocateExecutable();
540
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700541 // Restore main thread state to kNative as expected by native code
Elliott Hughes34e06962012-04-09 13:55:55 -0700542 Thread::Current()->SetState(kNative);
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700543
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700544 started_ = true;
545
Brian Carlstromae826982011-11-09 01:33:42 -0800546 // InitNativeMethods needs to be after started_ so that the classes
547 // it touches will have methods linked to the oat file if necessary.
548 InitNativeMethods();
549
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500550 Thread::FinishStartup();
551
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700552 if (!is_zygote_) {
553 DidForkFromZygote();
554 }
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700555
Elliott Hughes85d15452011-09-16 17:33:01 -0700556 StartDaemonThreads();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700557
Brian Carlstromaded5f72011-10-07 17:15:04 -0700558 CreateSystemClassLoader();
559
Elliott Hughes726079d2011-10-07 18:43:44 -0700560 Thread::Current()->GetJniEnv()->locals.AssertEmpty();
561
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800562 VLOG(startup) << "Runtime::Start exiting";
Elliott Hughes85d15452011-09-16 17:33:01 -0700563}
564
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700565void Runtime::DidForkFromZygote() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700566 is_zygote_ = false;
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700567
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700568 StartSignalCatcher();
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700569
570 // Start the JDWP thread. If the command-line debugger flags specified "suspend=y",
571 // this will pause the runtime, so we probably want this to come last.
572 Dbg::StartJdwp();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700573}
574
575void Runtime::StartSignalCatcher() {
576 if (!is_zygote_) {
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700577 signal_catcher_ = new SignalCatcher(stack_trace_file_);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700578 }
579}
580
Elliott Hughes85d15452011-09-16 17:33:01 -0700581void Runtime::StartDaemonThreads() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800582 VLOG(startup) << "Runtime::StartDaemonThreads entering";
Elliott Hughes85d15452011-09-16 17:33:01 -0700583
Elliott Hughes719b3232011-09-25 17:42:19 -0700584 Thread* self = Thread::Current();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700585
586 // Must be in the kNative state for calling native methods.
Elliott Hughes34e06962012-04-09 13:55:55 -0700587 CHECK_EQ(self->GetState(), kNative);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700588
Elliott Hughes719b3232011-09-25 17:42:19 -0700589 JNIEnv* env = self->GetJniEnv();
Elliott Hughes726079d2011-10-07 18:43:44 -0700590 ScopedLocalRef<jclass> c(env, env->FindClass("java/lang/Daemons"));
591 CHECK(c.get() != NULL);
592 jmethodID mid = env->GetStaticMethodID(c.get(), "start", "()V");
Elliott Hughes719b3232011-09-25 17:42:19 -0700593 CHECK(mid != NULL);
Elliott Hughes726079d2011-10-07 18:43:44 -0700594 env->CallStaticVoidMethod(c.get(), mid);
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800595 CHECK(!env->ExceptionCheck());
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700596
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800597 VLOG(startup) << "Runtime::StartDaemonThreads exiting";
Carl Shapiro61e019d2011-07-14 16:53:09 -0700598}
599
Elliott Hughes6b355752012-01-13 16:49:08 -0800600bool Runtime::IsShuttingDown() const {
601 return shutting_down_;
602}
603
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700604bool Runtime::IsStarted() const {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700605 return started_;
606}
607
Elliott Hughes0af55432011-08-17 18:37:28 -0700608bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700609 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700610
Elliott Hughes90a33692011-08-30 13:27:07 -0700611 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
612 if (options.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700613 LOG(ERROR) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700614 return false;
615 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800616 VLOG(startup) << "Runtime::Init -verbose:startup enabled";
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700617
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700618 SetJniGlobalsMax(options->jni_globals_max_);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800619 Monitor::Init(options->lock_profiling_threshold_, options->hook_is_sensitive_thread_);
Elliott Hughes32d6e1e2011-10-11 14:47:44 -0700620
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700621 host_prefix_ = options->host_prefix_;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800622 boot_class_path_string_ = options->boot_class_path_string_;
623 class_path_string_ = options->class_path_string_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700624 properties_ = options->properties_;
625
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800626 is_compiler_ = options->is_compiler_;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700627 is_zygote_ = options->is_zygote_;
628
Elliott Hughes0af55432011-08-17 18:37:28 -0700629 vfprintf_ = options->hook_vfprintf_;
630 exit_ = options->hook_exit_;
631 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700632
Elliott Hughesbe759c62011-09-08 19:38:21 -0700633 default_stack_size_ = options->stack_size_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700634 stack_trace_file_ = options->stack_trace_file_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700635
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700636 monitor_list_ = new MonitorList;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800637 thread_list_ = new ThreadList;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700638 intern_table_ = new InternTable;
639
Ian Rogers776ac1f2012-04-13 23:36:36 -0700640 verifier::MethodVerifier::InitGcMaps();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800641
Logan Chiendd361c92012-04-10 23:40:37 +0800642#if defined(ART_USE_LLVM_COMPILER)
Ian Rogers776ac1f2012-04-13 23:36:36 -0700643 verifier::MethodVerifier::InitInferredRegCategoryMaps();
Logan Chiendd361c92012-04-10 23:40:37 +0800644#endif
645
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800646 heap_ = new Heap(options->heap_initial_size_,
647 options->heap_growth_limit_,
648 options->heap_maximum_size_,
649 options->image_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700650
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700651 BlockSignals();
Elliott Hughes457005c2012-04-16 13:54:25 -0700652 InitPlatformSignalHandlers();
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700653
Elliott Hughesa0957642011-09-02 14:27:33 -0700654 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700655
Elliott Hughesbe759c62011-09-08 19:38:21 -0700656 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700657
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700658 // ClassLinker needs an attached thread, but we can't fully attach a thread
Elliott Hughes462c9442012-03-23 18:47:50 -0700659 // without creating objects. We can't supply a thread group yet; it will be fixed later.
660 Thread::Attach("main", false, NULL);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700661
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700662 // Set us to runnable so tools using a runtime can allocate and GC by default
Elliott Hughes34e06962012-04-09 13:55:55 -0700663 Thread::Current()->SetState(kRunnable);
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700664
Ian Rogers141d6222012-04-05 12:23:06 -0700665 // Now we're attached, we can take the heap lock and validate the heap.
666 GetHeap()->EnableObjectValidation();
667
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800668 CHECK_GE(GetHeap()->GetSpaces().size(), 1U);
669 if (GetHeap()->GetSpaces()[0]->IsImageSpace()) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800670 class_linker_ = ClassLinker::CreateFromImage(intern_table_);
671 } else {
672 CHECK(options->boot_class_path_ != NULL);
673 CHECK_NE(options->boot_class_path_->size(), 0U);
674 class_linker_ = ClassLinker::CreateFromCompiler(*options->boot_class_path_, intern_table_);
675 }
676 CHECK(class_linker_ != NULL);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700677
jeffhaob5e81852012-03-12 11:15:45 -0700678 method_trace_ = options->method_trace_;
679 method_trace_file_ = options->method_trace_file_;
680 method_trace_file_size_ = options->method_trace_file_size_;
681
682 if (options->method_trace_) {
683 Trace::Start(options->method_trace_file_.c_str(), -1, options->method_trace_file_size_, 0, false);
684 }
685
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800686 VLOG(startup) << "Runtime::Init exiting";
Carl Shapiro1fb86202011-06-27 17:43:13 -0700687 return true;
688}
689
Elliott Hughes038a8062011-09-18 14:12:41 -0700690void Runtime::InitNativeMethods() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800691 VLOG(startup) << "Runtime::InitNativeMethods entering";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700692 Thread* self = Thread::Current();
693 JNIEnv* env = self->GetJniEnv();
694
Elliott Hughes418d20f2011-09-22 14:00:39 -0700695 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Elliott Hughes34e06962012-04-09 13:55:55 -0700696 CHECK_EQ(self->GetState(), kNative);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700697
Elliott Hughes418d20f2011-09-22 14:00:39 -0700698 // First set up JniConstants, which is used by both the runtime's built-in native
699 // methods and libcore.
Elliott Hughesfea966e2011-09-22 10:26:20 -0700700 JniConstants::init(env);
701
Elliott Hughes418d20f2011-09-22 14:00:39 -0700702 // Then set up the native methods provided by the runtime itself.
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700703 RegisterRuntimeNativeMethods(env);
704
Elliott Hughes418d20f2011-09-22 14:00:39 -0700705 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
706 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
707 // the library that implements System.loadLibrary!
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700708 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800709 VLOG(startup) << "Runtime::InitNativeMethods exiting";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700710}
711
712void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
713#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Ian Rogers5167c972012-02-03 10:41:20 -0800714 // Register Throwable first so that registration of other native methods can throw exceptions
715 REGISTER(register_java_lang_Throwable);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700716 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700717 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700718 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700719 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700720 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700721 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700722 REGISTER(register_java_lang_Object);
723 REGISTER(register_java_lang_Runtime);
724 REGISTER(register_java_lang_String);
725 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700726 REGISTER(register_java_lang_Thread);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700727 REGISTER(register_java_lang_VMClassLoader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700728 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700729 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700730 REGISTER(register_java_lang_reflect_Field);
731 REGISTER(register_java_lang_reflect_Method);
Jesse Wilson95caa792011-10-12 18:14:17 -0400732 REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700733 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700734 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700735 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700736 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700737#undef REGISTER
738}
739
Elliott Hughesc967f782012-04-16 10:23:15 -0700740void Runtime::DumpForSigQuit(std::ostream& os) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700741 GetClassLinker()->DumpForSigQuit(os);
742 GetInternTable()->DumpForSigQuit(os);
Elliott Hughesae80b492012-04-24 10:43:17 -0700743 GetJavaVM()->DumpForSigQuit(os);
Elliott Hughesc967f782012-04-16 10:23:15 -0700744 GetHeap()->DumpForSigQuit(os);
Elliott Hughes42ee1422011-09-06 12:33:32 -0700745 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700746
Elliott Hughesc967f782012-04-16 10:23:15 -0700747 thread_list_->DumpForSigQuit(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700748}
749
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800750void Runtime::DumpLockHolders(std::ostream& os) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800751 pid_t heap_lock_owner = GetHeap()->GetLockOwner();
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800752 pid_t thread_list_lock_owner = GetThreadList()->GetLockOwner();
753 pid_t classes_lock_owner = GetClassLinker()->GetClassesLockOwner();
754 pid_t dex_lock_owner = GetClassLinker()->GetDexLockOwner();
755 if ((heap_lock_owner | thread_list_lock_owner | classes_lock_owner | dex_lock_owner) != 0) {
756 os << "Heap lock owner tid: " << heap_lock_owner << "\n"
757 << "ThreadList lock owner tid: " << thread_list_lock_owner << "\n"
758 << "ClassLinker classes lock owner tid: " << classes_lock_owner << "\n"
759 << "ClassLinker dex lock owner tid: " << dex_lock_owner << "\n";
760 }
761}
762
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700763void Runtime::SetStatsEnabled(bool new_state) {
764 if (new_state == true) {
765 GetStats()->Clear(~0);
766 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
767 Thread::Current()->GetStats()->Clear(~0);
768 }
769 stats_enabled_ = new_state;
770}
771
772void Runtime::ResetStats(int kinds) {
773 GetStats()->Clear(kinds & 0xffff);
774 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
775 Thread::Current()->GetStats()->Clear(kinds >> 16);
776}
777
778RuntimeStats* Runtime::GetStats() {
779 return &stats_;
780}
781
782int32_t Runtime::GetStat(int kind) {
783 RuntimeStats* stats;
784 if (kind < (1<<16)) {
785 stats = GetStats();
786 } else {
787 stats = Thread::Current()->GetStats();
788 kind >>= 16;
789 }
790 switch (kind) {
791 case KIND_ALLOCATED_OBJECTS:
792 return stats->allocated_objects;
793 case KIND_ALLOCATED_BYTES:
794 return stats->allocated_bytes;
795 case KIND_FREED_OBJECTS:
796 return stats->freed_objects;
797 case KIND_FREED_BYTES:
798 return stats->freed_bytes;
799 case KIND_GC_INVOCATIONS:
800 return stats->gc_for_alloc_count;
801 case KIND_CLASS_INIT_COUNT:
802 return stats->class_init_count;
803 case KIND_CLASS_INIT_TIME:
804 // Convert ns to us, reduce to 32 bits.
Elliott Hughes398f64b2012-03-26 18:05:48 -0700805 return static_cast<int>(stats->class_init_time_ns / 1000);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700806 case KIND_EXT_ALLOCATED_OBJECTS:
807 case KIND_EXT_ALLOCATED_BYTES:
808 case KIND_EXT_FREED_OBJECTS:
809 case KIND_EXT_FREED_BYTES:
810 return 0; // backward compatibility
811 default:
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700812 LOG(FATAL) << "Unknown statistic " << kind;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700813 return -1; // unreachable
814 }
815}
816
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700817void Runtime::BlockSignals() {
Elliott Hughes457005c2012-04-16 13:54:25 -0700818 SignalSet signals;
819 signals.Add(SIGPIPE);
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700820 // SIGQUIT is used to dump the runtime's state (including stack traces).
Elliott Hughes457005c2012-04-16 13:54:25 -0700821 signals.Add(SIGQUIT);
Elliott Hughes08795042012-04-03 14:48:52 -0700822 // SIGUSR1 is used to initiate a GC.
Elliott Hughes457005c2012-04-16 13:54:25 -0700823 signals.Add(SIGUSR1);
824 signals.Block();
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700825}
826
Elliott Hughes462c9442012-03-23 18:47:50 -0700827void Runtime::AttachCurrentThread(const char* thread_name, bool as_daemon, Object* thread_group) {
828 Thread::Attach(thread_name, as_daemon, thread_group);
Elliott Hughes22869a92012-03-27 14:08:24 -0700829 if (thread_name == NULL) {
830 LOG(WARNING) << *Thread::Current() << " attached without supplying a name";
831 }
Carl Shapiro61e019d2011-07-14 16:53:09 -0700832}
833
Elliott Hughesd92bec42011-09-02 17:04:36 -0700834void Runtime::DetachCurrentThread() {
Elliott Hughes22869a92012-03-27 14:08:24 -0700835 if (Thread::Current()->GetTopOfStack().GetSP() != NULL) {
836 LOG(FATAL) << *Thread::Current() << " attempting to detach while still running code";
837 }
Elliott Hughes02b48d12011-09-07 17:15:51 -0700838 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700839}
840
Brian Carlstrome24fa612011-09-29 00:53:55 -0700841void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700842 Dbg::VisitRoots(visitor, arg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700843 class_linker_->VisitRoots(visitor, arg);
844 intern_table_->VisitRoots(visitor, arg);
845 java_vm_->VisitRoots(visitor, arg);
846 thread_list_->VisitRoots(visitor, arg);
847 visitor(jni_stub_array_, arg);
848 visitor(abstract_method_error_stub_array_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700849 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
850 visitor(resolution_stub_array_[i], arg);
851 }
Ian Rogers19846512012-02-24 11:42:47 -0800852 visitor(resolution_method_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700853 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
854 visitor(callee_save_method_[i], arg);
855 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700856}
857
Ian Rogers169c9a72011-11-13 20:13:17 -0800858bool Runtime::HasJniDlsymLookupStub() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700859 return jni_stub_array_ != NULL;
860}
861
Ian Rogers169c9a72011-11-13 20:13:17 -0800862ByteArray* Runtime::GetJniDlsymLookupStub() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700863 CHECK(jni_stub_array_ != NULL);
864 return jni_stub_array_;
865}
866
Ian Rogers169c9a72011-11-13 20:13:17 -0800867void Runtime::SetJniDlsymLookupStub(ByteArray* jni_stub_array) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700868 CHECK(jni_stub_array != NULL) << " jni_stub_array=" << jni_stub_array;
869 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array)
870 << "jni_stub_array_=" << jni_stub_array_ << " jni_stub_array=" << jni_stub_array;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700871 jni_stub_array_ = jni_stub_array;
872}
873
874bool Runtime::HasAbstractMethodErrorStubArray() const {
875 return abstract_method_error_stub_array_ != NULL;
876}
877
878ByteArray* Runtime::GetAbstractMethodErrorStubArray() const {
879 CHECK(abstract_method_error_stub_array_ != NULL);
880 return abstract_method_error_stub_array_;
881}
882
883void 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 -0700889
890Runtime::TrampolineType Runtime::GetTrampolineType(Method* method) {
891 if (method == NULL) {
892 return Runtime::kUnknownMethod;
893 } else if (method->IsStatic()) {
894 return Runtime::kStaticMethod;
895 } else {
Ian Rogersfb6adba2012-03-04 21:51:51 -0800896 return Runtime::kUnknownMethod;
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700897 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700898}
899
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700900bool Runtime::HasResolutionStubArray(TrampolineType type) const {
901 return resolution_stub_array_[type] != NULL;
Ian Rogersad25ac52011-10-04 19:13:33 -0700902}
903
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700904ByteArray* Runtime::GetResolutionStubArray(TrampolineType type) const {
905 CHECK(HasResolutionStubArray(type));
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700906 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastTrampolineMethodType));
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700907 return resolution_stub_array_[type];
908}
909
910void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700911 CHECK(resolution_stub_array != NULL);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700912 CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array);
913 resolution_stub_array_[type] = resolution_stub_array;
Ian Rogersad25ac52011-10-04 19:13:33 -0700914}
915
Ian Rogers19846512012-02-24 11:42:47 -0800916Method* Runtime::CreateResolutionMethod() {
917 Class* method_class = Method::GetMethodClass();
918 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
919 method->SetDeclaringClass(method_class);
920 // TODO: use a special method for resolution method saves
921 method->SetDexMethodIndex(DexFile::kDexNoIndex16);
922 ByteArray* unknown_resolution_stub = GetResolutionStubArray(kUnknownMethod);
923 CHECK(unknown_resolution_stub != NULL);
924 method->SetCode(unknown_resolution_stub->GetData());
925 return method.get();
926}
927
928bool Runtime::HasResolutionMethod() const {
929 return resolution_method_ != NULL;
930}
931
932// Returns a special method that calls into a trampoline for runtime method resolution
933Method* Runtime::GetResolutionMethod() const {
934 CHECK(HasResolutionMethod());
935 return resolution_method_;
936}
937
938void Runtime::SetResolutionMethod(Method* method) {
939 resolution_method_ = method;
940}
941
Brian Carlstromae826982011-11-09 01:33:42 -0800942Method* Runtime::CreateCalleeSaveMethod(InstructionSet instruction_set, CalleeSaveType type) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700943 Class* method_class = Method::GetMethodClass();
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700944 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
Ian Rogersff1ed472011-09-20 13:46:24 -0700945 method->SetDeclaringClass(method_class);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800946 // TODO: use a special method for callee saves
Ian Rogers19846512012-02-24 11:42:47 -0800947 method->SetDexMethodIndex(DexFile::kDexNoIndex16);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700948 method->SetCode(NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800949 if ((instruction_set == kThumb2) || (instruction_set == kArm)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700950 uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6) | (1 << art::arm::R7) |
951 (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11);
952 uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3);
953 uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9);
954 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills :0) |
955 (type == kSaveAll ? all_spills :0) | (1 << art::arm::LR);
956 uint32_t fp_all_spills = (1 << art::arm::S0) | (1 << art::arm::S1) | (1 << art::arm::S2) |
957 (1 << art::arm::S3) | (1 << art::arm::S4) | (1 << art::arm::S5) |
958 (1 << art::arm::S6) | (1 << art::arm::S7) | (1 << art::arm::S8) |
959 (1 << art::arm::S9) | (1 << art::arm::S10) | (1 << art::arm::S11) |
960 (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) |
961 (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) |
962 (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) |
963 (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) |
964 (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) |
965 (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
966 (1 << art::arm::S30) | (1 << art::arm::S31);
967 uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
968 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
969 __builtin_popcount(fp_spills) /* fprs */ +
970 1 /* Method* */) * kPointerSize, kStackAlignment);
Ian Rogers15fdb8c2011-09-25 15:45:07 -0700971 method->SetFrameSizeInBytes(frame_size);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700972 method->SetCoreSpillMask(core_spills);
973 method->SetFpSpillMask(fp_spills);
Brian Carlstromae826982011-11-09 01:33:42 -0800974 } else if (instruction_set == kX86) {
Ian Rogers7caad772012-03-30 01:07:54 -0700975 uint32_t ref_spills = (1 << art::x86::EBP) | (1 << art::x86::ESI) | (1 << art::x86::EDI);
976 uint32_t arg_spills = (1 << art::x86::ECX) | (1 << art::x86::EDX) | (1 << art::x86::EBX);
977 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills : 0) |
978 (1 << art::x86::kNumberOfCpuRegisters); // fake return address callee save
979 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
980 1 /* Method* */) * kPointerSize, kStackAlignment);
981 method->SetFrameSizeInBytes(frame_size);
982 method->SetCoreSpillMask(core_spills);
Ian Rogersff1ed472011-09-20 13:46:24 -0700983 method->SetFpSpillMask(0);
984 } else {
985 UNIMPLEMENTED(FATAL);
986 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700987 return method.get();
Ian Rogersff1ed472011-09-20 13:46:24 -0700988}
989
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700990bool Runtime::HasCalleeSaveMethod(CalleeSaveType type) const {
991 return callee_save_method_[type] != NULL;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700992}
993
Brian Carlstrome24fa612011-09-29 00:53:55 -0700994// Returns a special method that describes all callee saves being spilled to the stack.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700995Method* Runtime::GetCalleeSaveMethod(CalleeSaveType type) const {
996 CHECK(HasCalleeSaveMethod(type));
997 return callee_save_method_[type];
Brian Carlstrome24fa612011-09-29 00:53:55 -0700998}
999
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001000void Runtime::SetCalleeSaveMethod(Method* method, CalleeSaveType type) {
1001 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
1002 callee_save_method_[type] = method;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001003}
1004
jeffhao2692b572011-12-16 15:42:28 -08001005void Runtime::EnableMethodTracing(Trace* tracer) {
1006 CHECK(!IsMethodTracingActive());
1007 tracer_ = tracer;
1008}
1009
1010void Runtime::DisableMethodTracing() {
1011 CHECK(IsMethodTracingActive());
1012 delete tracer_;
1013 tracer_ = NULL;
1014}
1015
1016bool Runtime::IsMethodTracingActive() const {
1017 return (tracer_ != NULL);
1018}
1019
1020Trace* Runtime::GetTracer() const {
1021 CHECK(IsMethodTracingActive());
1022 return tracer_;
1023}
1024
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001025const std::vector<const DexFile*>& Runtime::GetCompileTimeClassPath(const ClassLoader* class_loader) {
1026 if (class_loader == NULL) {
1027 return GetClassLinker()->GetBootClassPath();
1028 }
1029 CHECK(UseCompileTimeClassPath());
1030 CompileTimeClassPaths::const_iterator it = compile_time_class_paths_.find(class_loader);
1031 CHECK(it != compile_time_class_paths_.end());
1032 return it->second;
1033}
1034
1035void Runtime::SetCompileTimeClassPath(const ClassLoader* class_loader, std::vector<const DexFile*>& class_path) {
1036 CHECK(!IsStarted());
1037 use_compile_time_class_path_ = true;
Elliott Hughesa0e18062012-04-13 15:59:59 -07001038 compile_time_class_paths_.Put(class_loader, class_path);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001039}
1040
Carl Shapiro1fb86202011-06-27 17:43:13 -07001041} // namespace art