blob: 2b9a28d248b7d4d2615f4c5e6e8a24778f248eca [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"
jeffhao7fbee072012-08-24 17:56:54 -070030#include "constants_mips.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070031#include "constants_x86.h"
Elliott Hughes3bb81562011-10-21 18:52:59 -070032#include "debugger.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070033#include "heap.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070034#include "image.h"
Elliott Hughescf4c6c42011-09-01 15:16:42 -070035#include "intern_table.h"
Elliott Hughesc5f7c912011-08-18 14:00:42 -070036#include "jni_internal.h"
Elliott Hughes32d6e1e2011-10-11 14:47:44 -070037#include "monitor.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070038#include "oat_file.h"
Elliott Hughes726079d2011-10-07 18:43:44 -070039#include "ScopedLocalRef.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070040#include "scoped_thread_state_change.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070041#include "signal_catcher.h"
Elliott Hughes457005c2012-04-16 13:54:25 -070042#include "signal_set.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070043#include "space.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070044#include "thread.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070045#include "thread_list.h"
jeffhaob5e81852012-03-12 11:15:45 -070046#include "trace.h"
Elliott Hughes3bb81562011-10-21 18:52:59 -070047#include "UniquePtr.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070048#include "verifier/method_verifier.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070049#include "well_known_classes.h"
Carl Shapiro61e019d2011-07-14 16:53:09 -070050
Logan Chien971bf3f2012-05-01 15:47:55 +080051#if defined(ART_USE_LLVM_COMPILER)
52#include "compiler_llvm/procedure_linkage_table.h"
53#endif
54
Elliott Hugheseac76672012-05-24 21:56:51 -070055#include "JniConstants.h" // Last to avoid LOG redefinition in ics-mr1-plus-art.
Elliott Hughes90a33692011-08-30 13:27:07 -070056
Carl Shapiro1fb86202011-06-27 17:43:13 -070057namespace art {
58
Carl Shapiro2ed144c2011-07-26 16:52:08 -070059Runtime* Runtime::instance_ = NULL;
60
Elliott Hughesdcc24742011-09-07 14:02:44 -070061Runtime::Runtime()
Elliott Hughesd9c67be2012-02-02 19:54:06 -080062 : is_compiler_(false),
63 is_zygote_(false),
Mathieu Chartier069387a2012-06-18 12:01:01 -070064 is_concurrent_gc_enabled_(true),
Elliott Hughes6cf23882012-06-15 15:42:07 -070065 default_stack_size_(0),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080066 heap_(NULL),
Elliott Hughesc33a32b2011-10-11 18:18:07 -070067 monitor_list_(NULL),
Elliott Hughesdcc24742011-09-07 14:02:44 -070068 thread_list_(NULL),
69 intern_table_(NULL),
70 class_linker_(NULL),
71 signal_catcher_(NULL),
72 java_vm_(NULL),
Elliott Hughes225f5a12012-06-11 11:23:48 -070073 pre_allocated_OutOfMemoryError_(NULL),
Brian Carlstrom16192862011-09-12 17:50:06 -070074 jni_stub_array_(NULL),
Brian Carlstrome24fa612011-09-29 00:53:55 -070075 abstract_method_error_stub_array_(NULL),
Ian Rogers19846512012-02-24 11:42:47 -080076 resolution_method_(NULL),
Ian Rogers7c3757f2012-02-15 17:18:53 -080077 system_class_loader_(NULL),
Elliott Hughes6b355752012-01-13 16:49:08 -080078 shutting_down_(false),
Elliott Hughesdcc24742011-09-07 14:02:44 -070079 started_(false),
Mathieu Chartier7664f5c2012-06-08 18:15:32 -070080 finished_starting_(false),
Elliott Hughesdcc24742011-09-07 14:02:44 -070081 vfprintf_(NULL),
82 exit_(NULL),
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070083 abort_(NULL),
jeffhao2692b572011-12-16 15:42:28 -080084 stats_enabled_(false),
Ian Rogers776ac1f2012-04-13 23:36:36 -070085 method_trace_(0),
86 method_trace_file_size_(0),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080087 tracer_(NULL),
Ian Rogers365c1022012-06-22 15:05:28 -070088 use_compile_time_class_path_(false),
89 main_thread_group_(NULL),
Logan Chien971bf3f2012-05-01 15:47:55 +080090 system_thread_group_(NULL)
91#if defined(ART_USE_LLVM_COMPILER)
92#if defined(__arm__)
93 , plt_(kArm)
94#elif defined(__mips__)
95 , plt_(kMips)
96#elif defined(__i386__)
97 , plt_(kX86)
98#endif
99#endif
100 {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700101 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
102 resolution_stub_array_[i] = NULL;
103 }
104 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
Elliott Hughes225f5a12012-06-11 11:23:48 -0700105 callee_save_methods_[i] = NULL;
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700106 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800107
108#if defined(ART_USE_LLVM_COMPILER)
109 CHECK(plt_.AllocateTable()) << "Failed to allocate PLT";
110#endif
Elliott Hughesdcc24742011-09-07 14:02:44 -0700111}
112
Carl Shapiro61e019d2011-07-14 16:53:09 -0700113Runtime::~Runtime() {
Elliott Hughes6b355752012-01-13 16:49:08 -0800114 shutting_down_ = true;
115
jeffhaob5e81852012-03-12 11:15:45 -0700116 if (IsMethodTracingActive()) {
117 Trace::Shutdown();
118 }
119
Mathieu Chartiera6399032012-06-11 18:49:50 -0700120 // Make sure to let the GC complete if it is running.
Ian Rogers81d425b2012-09-27 16:03:43 -0700121 heap_->WaitForConcurrentGcToComplete(Thread::Current());
Mathieu Chartiera6399032012-06-11 18:49:50 -0700122
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700123 // Make sure our internal threads are dead before we start tearing down things they're using.
Elliott Hughese52e49b2012-04-02 16:05:44 -0700124 Dbg::StopJdwp();
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700125 delete signal_catcher_;
126
Elliott Hughes038a8062011-09-18 14:12:41 -0700127 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
Elliott Hughes93e74e82011-09-13 11:07:03 -0700128 delete thread_list_;
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700129 delete monitor_list_;
Elliott Hughes93e74e82011-09-13 11:07:03 -0700130
Carl Shapiro61e019d2011-07-14 16:53:09 -0700131 delete class_linker_;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800132 delete heap_;
Elliott Hughes0a1038b2012-06-14 16:24:17 -0700133 verifier::MethodVerifier::Shutdown();
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700134 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700135 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700136 Thread::Shutdown();
Elliott Hughes7c6169d2012-05-02 16:11:48 -0700137 QuasiAtomic::Shutdown();
138
Carl Shapiro4acf4642011-07-26 18:54:13 -0700139 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700140 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -0700141 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700142}
143
Elliott Hughescac6cc72011-11-03 20:31:21 -0700144static bool gAborting = false;
145
Elliott Hughese0918552011-10-28 17:18:29 -0700146struct AbortState {
147 void Dump(std::ostream& os) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700148 if (gAborting) {
149 os << "Runtime aborting --- recursively, so no thread-specific detail!\n";
150 return;
151 }
152 gAborting = true;
Elliott Hughese0918552011-10-28 17:18:29 -0700153 os << "Runtime aborting...\n";
Elliott Hughes6c7d2442012-02-01 18:40:47 -0800154 if (Runtime::Current() == NULL) {
155 os << "(Runtime does not yet exist!)\n";
156 return;
157 }
Elliott Hughese0918552011-10-28 17:18:29 -0700158 Thread* self = Thread::Current();
159 if (self == NULL) {
160 os << "(Aborting thread was not attached to runtime!)\n";
161 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700162 // TODO: we're aborting and the ScopedObjectAccess may attempt to acquire the mutator_lock_
163 // which may block indefinitely if there's a misbehaving thread holding it exclusively.
164 // The code below should be made robust to this.
165 ScopedObjectAccess soa(self);
Elliott Hughes899e7892012-01-24 14:57:32 -0800166 self->Dump(os);
167 if (self->IsExceptionPending()) {
168 os << "Pending " << PrettyTypeOf(self->GetException()) << " on thread:\n"
169 << self->GetException()->Dump();
170 }
Elliott Hughese0918552011-10-28 17:18:29 -0700171 }
172 }
173};
174
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700175void Runtime::Abort() {
Elliott Hughes131aef82012-01-27 11:53:35 -0800176 // Ensure that we don't have multiple threads trying to abort at once,
177 // which would result in significantly worse diagnostics.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700178 MutexLock mu(*Locks::abort_lock_);
Elliott Hughes131aef82012-01-27 11:53:35 -0800179
Elliott Hughesffe67362011-07-17 12:09:27 -0700180 // Get any pending output out of the way.
181 fflush(NULL);
182
183 // Many people have difficulty distinguish aborts from crashes,
184 // so be explicit.
Elliott Hughese0918552011-10-28 17:18:29 -0700185 AbortState state;
Elliott Hughes9ee5f9c2012-02-03 18:33:16 -0800186 LOG(INTERNAL_FATAL) << Dumpable<AbortState>(state);
Elliott Hughesffe67362011-07-17 12:09:27 -0700187
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700188 // Call the abort hook if we have one.
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700189 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700190 LOG(INTERNAL_FATAL) << "Calling abort hook...";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700191 Runtime::Current()->abort_();
192 // notreached
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700193 LOG(INTERNAL_FATAL) << "Unexpectedly returned from abort hook!";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700194 }
195
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700196#if defined(__BIONIC__)
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700197 // TODO: finish merging patches to fix abort(3) in bionic, then lose this!
198 // Bionic doesn't implement POSIX semantics for abort(3) in a multi-threaded
199 // process, so if we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -0700200 // receive SIGABRT. debuggerd dumps the stack trace of the main
201 // thread, whether or not that was the thread that failed. By
202 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -0700203 // fault in the current thread, and get a useful log from debuggerd.
Elliott Hughes81ff3182012-03-23 20:35:56 -0700204 // We can also trivially tell the difference between a crash and
Elliott Hughesffe67362011-07-17 12:09:27 -0700205 // a deliberate abort by looking at the fault address.
206 *reinterpret_cast<char*>(0xdeadd00d) = 38;
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700207#elif defined(__APPLE__)
208 // TODO: check that this actually gives good stack traces on the Mac!
209 pthread_kill(pthread_self(), SIGABRT);
210#else
211 // TODO: we ought to be able to use pthread_kill(3) here (or abort(3),
212 // which POSIX defines in terms of raise(3), which POSIX defines in terms
213 // of pthread_kill(3)). On Linux, though, libcorkscrew can't unwind through
214 // libpthread, which means the stacks we dump would be useless. Calling
215 // tgkill(2) directly avoids that.
216 syscall(__NR_tgkill, getpid(), GetTid(), SIGABRT);
217#endif
Elliott Hughesffe67362011-07-17 12:09:27 -0700218 // notreached
219}
220
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700221bool Runtime::PreZygoteFork() {
222 heap_->PreZygoteFork();
223 return true;
224}
225
Elliott Hughesbf86d042011-08-31 17:53:14 -0700226void Runtime::CallExitHook(jint status) {
227 if (exit_ != NULL) {
Elliott Hughes34e06962012-04-09 13:55:55 -0700228 ScopedThreadStateChange tsc(Thread::Current(), kNative);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700229 exit_(status);
230 LOG(WARNING) << "Exit hook returned instead of exiting!";
231 }
232}
233
Brian Carlstrom8a436592011-08-15 21:27:23 -0700234// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
235// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
236// [gG] gigabytes.
237//
238// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700239// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
240// of 1024.
241//
242// The spec says the -Xmx and -Xms options must be multiples of 1024. It
243// doesn't say anything about -Xss.
244//
245// Returns 0 (a useless size) if "s" is malformed or specifies a low or
246// non-evenly-divisible value.
247//
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800248size_t ParseMemoryOption(const char* s, size_t div) {
Brian Carlstrom8a436592011-08-15 21:27:23 -0700249 // strtoul accepts a leading [+-], which we don't want,
250 // so make sure our string starts with a decimal digit.
251 if (isdigit(*s)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700252 char* s2;
253 size_t val = strtoul(s, &s2, 10);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700254 if (s2 != s) {
255 // s2 should be pointing just after the number.
256 // If this is the end of the string, the user
257 // has specified a number of bytes. Otherwise,
258 // there should be exactly one more character
259 // that specifies a multiplier.
260 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700261 // The remainder of the string is either a single multiplier
262 // character, or nothing to indicate that the value is in
263 // bytes.
264 char c = *s2++;
265 if (*s2 == '\0') {
266 size_t mul;
267 if (c == '\0') {
268 mul = 1;
269 } else if (c == 'k' || c == 'K') {
270 mul = KB;
271 } else if (c == 'm' || c == 'M') {
272 mul = MB;
273 } else if (c == 'g' || c == 'G') {
274 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700275 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700276 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700277 return 0;
278 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700279
280 if (val <= std::numeric_limits<size_t>::max() / mul) {
281 val *= mul;
282 } else {
283 // Clamp to a multiple of 1024.
284 val = std::numeric_limits<size_t>::max() & ~(1024-1);
285 }
286 } else {
287 // There's more than one character after the numeric part.
288 return 0;
289 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700290 }
291 // The man page says that a -Xm value must be a multiple of 1024.
292 if (val % div == 0) {
293 return val;
294 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700295 }
296 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700297 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700298}
299
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800300size_t ParseIntegerOrDie(const std::string& s) {
301 std::string::size_type colon = s.find(':');
302 if (colon == std::string::npos) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700303 LOG(FATAL) << "Missing integer: " << s;
304 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800305 const char* begin = &s[colon + 1];
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700306 char* end;
307 size_t result = strtoul(begin, &end, 10);
308 if (begin == end || *end != '\0') {
309 LOG(FATAL) << "Failed to parse integer in: " << s;
310 }
311 return result;
312}
313
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700314Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700315 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800316 const char* boot_class_path_string = getenv("BOOTCLASSPATH");
317 if (boot_class_path_string != NULL) {
318 parsed->boot_class_path_string_ = boot_class_path_string;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700319 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800320 const char* class_path_string = getenv("CLASSPATH");
321 if (class_path_string != NULL) {
322 parsed->class_path_string_ = class_path_string;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700323 }
Elliott Hughes67d92002012-03-26 15:08:51 -0700324 // -Xcheck:jni is off by default for regular builds but on by default in debug builds.
325 parsed->check_jni_ = kIsDebugBuild;
Elliott Hughes85d15452011-09-16 17:33:01 -0700326
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700327 parsed->heap_initial_size_ = Heap::kInitialSize;
328 parsed->heap_maximum_size_ = Heap::kMaximumSize;
Elliott Hughes6cf23882012-06-15 15:42:07 -0700329 parsed->heap_growth_limit_ = 0; // 0 means no growth limit.
330 parsed->stack_size_ = 0; // 0 means default.
Brian Carlstromf734cf52011-08-17 16:28:14 -0700331
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800332 parsed->is_compiler_ = false;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700333 parsed->is_zygote_ = false;
Mathieu Chartier069387a2012-06-18 12:01:01 -0700334 parsed->is_concurrent_gc_enabled_ = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700335
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700336 parsed->jni_globals_max_ = 0;
Elliott Hughesfc861622011-10-17 17:57:47 -0700337 parsed->lock_profiling_threshold_ = 0;
338 parsed->hook_is_sensitive_thread_ = NULL;
339
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700340 parsed->hook_vfprintf_ = vfprintf;
341 parsed->hook_exit_ = exit;
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700342 parsed->hook_abort_ = NULL; // We don't call abort(3) by default; see Runtime::Abort.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700343
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800344// gLogVerbosity.class_linker = true; // TODO: don't check this in!
345// gLogVerbosity.compiler = true; // TODO: don't check this in!
346// gLogVerbosity.heap = true; // TODO: don't check this in!
347// gLogVerbosity.gc = true; // TODO: don't check this in!
348// gLogVerbosity.jdwp = true; // TODO: don't check this in!
349// gLogVerbosity.jni = true; // TODO: don't check this in!
350// gLogVerbosity.monitor = true; // TODO: don't check this in!
351// gLogVerbosity.startup = true; // TODO: don't check this in!
352// gLogVerbosity.third_party_jni = true; // TODO: don't check this in!
353// gLogVerbosity.threads = true; // TODO: don't check this in!
354
jeffhaob5e81852012-03-12 11:15:45 -0700355 parsed->method_trace_ = false;
356 parsed->method_trace_file_ = "/data/method-trace-file.bin";
357 parsed->method_trace_file_size_ = 10 * MB;
358
Brian Carlstrom8a436592011-08-15 21:27:23 -0700359 for (size_t i = 0; i < options.size(); ++i) {
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800360 const std::string option(options[i].first);
Brian Carlstrom0796af02011-10-12 14:31:45 -0700361 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700362 LOG(INFO) << "option[" << i << "]=" << option;
363 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800364 if (StartsWith(option, "-Xbootclasspath:")) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800365 parsed->boot_class_path_string_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700366 } else if (option == "-classpath" || option == "-cp") {
367 // TODO: support -Djava.class.path
368 i++;
369 if (i == options.size()) {
370 // TODO: usage
371 LOG(FATAL) << "Missing required class path value for " << option;
372 return NULL;
373 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800374 const StringPiece& value = options[i].first;
375 parsed->class_path_string_ = value.data();
376 } else if (option == "bootclasspath") {
377 parsed->boot_class_path_
378 = reinterpret_cast<const std::vector<const DexFile*>*>(options[i].second);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800379 } else if (StartsWith(option, "-Ximage:")) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800380 parsed->image_ = option.substr(strlen("-Ximage:")).data();
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800381 } else if (StartsWith(option, "-Xcheck:jni")) {
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700382 parsed->check_jni_ = true;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800383 } else if (StartsWith(option, "-Xrunjdwp:") || StartsWith(option, "-agentlib:jdwp=")) {
384 std::string tail(option.substr(option[1] == 'X' ? 10 : 15));
Elliott Hughes3bb81562011-10-21 18:52:59 -0700385 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
386 LOG(FATAL) << "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
387 << "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n";
388 return NULL;
389 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800390 } else if (StartsWith(option, "-Xms")) {
391 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).c_str(), 1024);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700392 if (size == 0) {
393 if (ignore_unrecognized) {
394 continue;
395 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700396 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700397 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700398 return NULL;
399 }
400 parsed->heap_initial_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800401 } else if (StartsWith(option, "-Xmx")) {
402 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).c_str(), 1024);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700403 if (size == 0) {
404 if (ignore_unrecognized) {
405 continue;
406 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700407 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700408 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700409 return NULL;
410 }
411 parsed->heap_maximum_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800412 } else if (StartsWith(option, "-XX:HeapGrowthLimit=")) {
413 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).c_str(), 1024);
jeffhaoc1160702011-10-27 15:48:45 -0700414 if (size == 0) {
415 if (ignore_unrecognized) {
416 continue;
417 }
418 // TODO: usage
419 LOG(FATAL) << "Failed to parse " << option;
420 return NULL;
421 }
422 parsed->heap_growth_limit_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800423 } else if (StartsWith(option, "-Xss")) {
424 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).c_str(), 1);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700425 if (size == 0) {
426 if (ignore_unrecognized) {
427 continue;
428 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700429 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700430 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700431 return NULL;
432 }
433 parsed->stack_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800434 } else if (StartsWith(option, "-D")) {
435 parsed->properties_.push_back(option.substr(strlen("-D")));
436 } else if (StartsWith(option, "-Xjnitrace:")) {
437 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:"));
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700438 } else if (option == "compiler") {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800439 parsed->is_compiler_ = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700440 } else if (option == "-Xzygote") {
441 parsed->is_zygote_ = true;
Mathieu Chartier069387a2012-06-18 12:01:01 -0700442 } else if (StartsWith(option, "-Xgc:")) {
443 std::vector<std::string> gc_options;
444 Split(option.substr(strlen("-Xgc:")), ',', gc_options);
445 for (size_t i = 0; i < gc_options.size(); ++i) {
446 if (gc_options[i] == "noconcurrent") {
447 parsed->is_concurrent_gc_enabled_ = false;
448 } else if (gc_options[i] == "concurrent") {
449 parsed->is_concurrent_gc_enabled_ = true;
450 } else {
451 LOG(WARNING) << "Ignoring unknown -Xgc option: " << gc_options[i];
452 }
453 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800454 } else if (StartsWith(option, "-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700455 std::vector<std::string> verbose_options;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800456 Split(option.substr(strlen("-verbose:")), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700457 for (size_t i = 0; i < verbose_options.size(); ++i) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800458 if (verbose_options[i] == "class") {
459 gLogVerbosity.class_linker = true;
460 } else if (verbose_options[i] == "compiler") {
461 gLogVerbosity.compiler = true;
462 } else if (verbose_options[i] == "heap") {
463 gLogVerbosity.heap = true;
464 } else if (verbose_options[i] == "gc") {
465 gLogVerbosity.gc = true;
466 } else if (verbose_options[i] == "jdwp") {
467 gLogVerbosity.jdwp = true;
468 } else if (verbose_options[i] == "jni") {
469 gLogVerbosity.jni = true;
470 } else if (verbose_options[i] == "monitor") {
471 gLogVerbosity.monitor = true;
472 } else if (verbose_options[i] == "startup") {
473 gLogVerbosity.startup = true;
474 } else if (verbose_options[i] == "third-party-jni") {
475 gLogVerbosity.third_party_jni = true;
476 } else if (verbose_options[i] == "threads") {
477 gLogVerbosity.threads = true;
478 } else {
479 LOG(WARNING) << "Ignoring unknown -verbose option: " << verbose_options[i];
480 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700481 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800482 } else if (StartsWith(option, "-Xjnigreflimit:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700483 parsed->jni_globals_max_ = ParseIntegerOrDie(option);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800484 } else if (StartsWith(option, "-Xlockprofthreshold:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700485 parsed->lock_profiling_threshold_ = ParseIntegerOrDie(option);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800486 } else if (StartsWith(option, "-Xstacktracefile:")) {
Elliott Hughes206c20d2012-06-28 11:05:37 -0700487 parsed->stack_trace_file_ = option.substr(strlen("-Xstacktracefile:"));
Elliott Hughesfc861622011-10-17 17:57:47 -0700488 } else if (option == "sensitiveThread") {
Ian Rogers1b09b092012-08-20 15:35:52 -0700489 parsed->hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(const_cast<void*>(options[i].second));
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700490 } else if (option == "vfprintf") {
Ian Rogers1b09b092012-08-20 15:35:52 -0700491 parsed->hook_vfprintf_ =
492 reinterpret_cast<int (*)(FILE *, const char*, va_list)>(const_cast<void*>(options[i].second));
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700493 } else if (option == "exit") {
Ian Rogers1b09b092012-08-20 15:35:52 -0700494 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(const_cast<void*>(options[i].second));
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700495 } else if (option == "abort") {
Ian Rogers1b09b092012-08-20 15:35:52 -0700496 parsed->hook_abort_ = reinterpret_cast<void(*)()>(const_cast<void*>(options[i].second));
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700497 } else if (option == "host-prefix") {
498 parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second);
Elliott Hughesb3e66df2012-01-12 14:49:18 -0800499 } else if (option == "-Xgenregmap" || option == "-Xgc:precise") {
500 // We silently ignore these for backwards compatibility.
jeffhaob5e81852012-03-12 11:15:45 -0700501 } else if (option == "-Xmethod-trace") {
502 parsed->method_trace_ = true;
503 } else if (StartsWith(option, "-Xmethod-trace-file:")) {
504 parsed->method_trace_file_ = option.substr(strlen("-Xmethod-trace-file:"));
505 } else if (StartsWith(option, "-Xmethod-trace-file-size:")) {
506 parsed->method_trace_file_size_ = ParseIntegerOrDie(option);
Elliott Hughese119a362012-05-22 17:37:06 -0700507 } else if (option == "-Xprofile:threadcpuclock") {
508 Trace::SetDefaultClockSource(kProfilerClockSourceThreadCpu);
509 } else if (option == "-Xprofile:wallclock") {
510 Trace::SetDefaultClockSource(kProfilerClockSourceWall);
511 } else if (option == "-Xprofile:dualclock") {
512 Trace::SetDefaultClockSource(kProfilerClockSourceDual);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700513 } else {
514 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700515 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700516 LOG(ERROR) << "Unrecognized option " << option;
517 // TODO: this should exit, but for now tolerate unknown options
518 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700519 }
520 }
521 }
522
Brian Carlstrom223f20f2012-02-04 23:06:55 -0800523 if (!parsed->is_compiler_ && parsed->image_.empty()) {
524 parsed->image_ += GetAndroidRoot();
525 parsed->image_ += "/framework/boot.art";
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700526 }
jeffhaoc1160702011-10-27 15:48:45 -0700527 if (parsed->heap_growth_limit_ == 0) {
528 parsed->heap_growth_limit_ = parsed->heap_maximum_size_;
529 }
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700530
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700531 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700532}
533
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700534bool Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700535 // TODO: acquire a static mutex on Runtime to avoid racing.
536 if (Runtime::instance_ != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700537 return false;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700538 }
Ian Rogersb726dcb2012-09-05 08:57:23 -0700539 Locks::Init();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700540 instance_ = new Runtime;
541 if (!instance_->Init(options, ignore_unrecognized)) {
542 delete instance_;
543 instance_ = NULL;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700544 return false;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700545 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700546 return true;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700547}
Elliott Hughes0af55432011-08-17 18:37:28 -0700548
Ian Rogers365c1022012-06-22 15:05:28 -0700549static void CreateSystemClassLoader() {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800550 if (Runtime::Current()->UseCompileTimeClassPath()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700551 return;
552 }
553
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700554 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700555
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700556 Class* class_loader_class = soa.Decode<Class*>(WellKnownClasses::java_lang_ClassLoader);
Ian Rogers365c1022012-06-22 15:05:28 -0700557 CHECK(Runtime::Current()->GetClassLinker()->EnsureInitialized(class_loader_class, true, true));
Brian Carlstromaded5f72011-10-07 17:15:04 -0700558
Mathieu Chartier66f19252012-09-18 08:57:04 -0700559 AbstractMethod* getSystemClassLoader = class_loader_class->FindDirectMethod("getSystemClassLoader", "()Ljava/lang/ClassLoader;");
Brian Carlstromdf143242011-10-10 18:05:34 -0700560 CHECK(getSystemClassLoader != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700561
Ian Rogers365c1022012-06-22 15:05:28 -0700562 ClassLoader* class_loader =
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700563 down_cast<ClassLoader*>(InvokeWithJValues(soa, NULL, getSystemClassLoader, NULL).GetL());
Ian Rogers365c1022012-06-22 15:05:28 -0700564 CHECK(class_loader != NULL);
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500565
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700566 soa.Self()->SetClassLoaderOverride(class_loader);
Ian Rogers365c1022012-06-22 15:05:28 -0700567
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700568 Class* thread_class = soa.Decode<Class*>(WellKnownClasses::java_lang_Thread);
Ian Rogers365c1022012-06-22 15:05:28 -0700569 CHECK(Runtime::Current()->GetClassLinker()->EnsureInitialized(thread_class, true, true));
570
571 Field* contextClassLoader = thread_class->FindDeclaredInstanceField("contextClassLoader",
572 "Ljava/lang/ClassLoader;");
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500573 CHECK(contextClassLoader != NULL);
Ian Rogers365c1022012-06-22 15:05:28 -0700574
Mathieu Chartierdbe6f462012-09-25 16:54:50 -0700575 contextClassLoader->SetObject(soa.Decode<Object*>(soa.Self()->GetPeer()), class_loader);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700576}
577
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700578void Runtime::Start() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800579 VLOG(startup) << "Runtime::Start entering";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700580
581 CHECK(host_prefix_.empty()) << host_prefix_;
582
Elliott Hughes225f5a12012-06-11 11:23:48 -0700583 // Relocate the OatFiles (ELF images).
Logan Chien0c717dd2012-03-28 18:31:07 +0800584 class_linker_->RelocateExecutable();
585
Elliott Hughes225f5a12012-06-11 11:23:48 -0700586 // Pre-allocate an OutOfMemoryError for the double-OOME case.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700587 Thread* self = Thread::Current();
Elliott Hughes225f5a12012-06-11 11:23:48 -0700588 self->ThrowNewException("Ljava/lang/OutOfMemoryError;",
589 "OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack available");
590 pre_allocated_OutOfMemoryError_ = self->GetException();
591 self->ClearException();
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700592
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700593 // Restore main thread state to kNative as expected by native code.
594 self->TransitionFromRunnableToSuspended(kNative);
595
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700596 started_ = true;
597
Brian Carlstromae826982011-11-09 01:33:42 -0800598 // InitNativeMethods needs to be after started_ so that the classes
599 // it touches will have methods linked to the oat file if necessary.
600 InitNativeMethods();
601
Ian Rogers365c1022012-06-22 15:05:28 -0700602 // Initialize well known thread group values that may be accessed threads while attaching.
603 InitThreadGroups(self);
604
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500605 Thread::FinishStartup();
606
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700607 if (!is_zygote_) {
608 DidForkFromZygote();
609 }
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700610
Elliott Hughes85d15452011-09-16 17:33:01 -0700611 StartDaemonThreads();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700612
Brian Carlstromaded5f72011-10-07 17:15:04 -0700613 CreateSystemClassLoader();
614
Elliott Hughes225f5a12012-06-11 11:23:48 -0700615 self->GetJniEnv()->locals.AssertEmpty();
Elliott Hughes726079d2011-10-07 18:43:44 -0700616
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800617 VLOG(startup) << "Runtime::Start exiting";
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700618
619 finished_starting_ = true;
Elliott Hughes85d15452011-09-16 17:33:01 -0700620}
621
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700622void Runtime::DidForkFromZygote() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700623 is_zygote_ = false;
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700624
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700625 StartSignalCatcher();
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700626
627 // Start the JDWP thread. If the command-line debugger flags specified "suspend=y",
628 // this will pause the runtime, so we probably want this to come last.
629 Dbg::StartJdwp();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700630}
631
632void Runtime::StartSignalCatcher() {
633 if (!is_zygote_) {
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700634 signal_catcher_ = new SignalCatcher(stack_trace_file_);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700635 }
636}
637
Elliott Hughes85d15452011-09-16 17:33:01 -0700638void Runtime::StartDaemonThreads() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800639 VLOG(startup) << "Runtime::StartDaemonThreads entering";
Elliott Hughes85d15452011-09-16 17:33:01 -0700640
Elliott Hughes719b3232011-09-25 17:42:19 -0700641 Thread* self = Thread::Current();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700642
643 // Must be in the kNative state for calling native methods.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700644 {
Ian Rogersb726dcb2012-09-05 08:57:23 -0700645 MutexLock mu(*Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700646 CHECK_EQ(self->GetState(), kNative);
647 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700648
Elliott Hughes719b3232011-09-25 17:42:19 -0700649 JNIEnv* env = self->GetJniEnv();
Elliott Hugheseac76672012-05-24 21:56:51 -0700650 env->CallStaticVoidMethod(WellKnownClasses::java_lang_Daemons, WellKnownClasses::java_lang_Daemons_start);
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800651 CHECK(!env->ExceptionCheck());
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700652
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800653 VLOG(startup) << "Runtime::StartDaemonThreads exiting";
Carl Shapiro61e019d2011-07-14 16:53:09 -0700654}
655
Elliott Hughes0af55432011-08-17 18:37:28 -0700656bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700657 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700658
Elliott Hughes90a33692011-08-30 13:27:07 -0700659 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
660 if (options.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700661 LOG(ERROR) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700662 return false;
663 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800664 VLOG(startup) << "Runtime::Init -verbose:startup enabled";
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700665
Elliott Hughes7c6169d2012-05-02 16:11:48 -0700666 QuasiAtomic::Startup();
667
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700668 SetJniGlobalsMax(options->jni_globals_max_);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800669 Monitor::Init(options->lock_profiling_threshold_, options->hook_is_sensitive_thread_);
Elliott Hughes32d6e1e2011-10-11 14:47:44 -0700670
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700671 host_prefix_ = options->host_prefix_;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800672 boot_class_path_string_ = options->boot_class_path_string_;
673 class_path_string_ = options->class_path_string_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700674 properties_ = options->properties_;
675
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800676 is_compiler_ = options->is_compiler_;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700677 is_zygote_ = options->is_zygote_;
Mathieu Chartier069387a2012-06-18 12:01:01 -0700678 is_concurrent_gc_enabled_ = options->is_concurrent_gc_enabled_;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700679
Elliott Hughes0af55432011-08-17 18:37:28 -0700680 vfprintf_ = options->hook_vfprintf_;
681 exit_ = options->hook_exit_;
682 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700683
Elliott Hughesbe759c62011-09-08 19:38:21 -0700684 default_stack_size_ = options->stack_size_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700685 stack_trace_file_ = options->stack_trace_file_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700686
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700687 monitor_list_ = new MonitorList;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800688 thread_list_ = new ThreadList;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700689 intern_table_ = new InternTable;
690
Elliott Hughes0a1038b2012-06-14 16:24:17 -0700691 verifier::MethodVerifier::Init();
Logan Chiendd361c92012-04-10 23:40:37 +0800692
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800693 heap_ = new Heap(options->heap_initial_size_,
694 options->heap_growth_limit_,
695 options->heap_maximum_size_,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700696 options->image_,
697 options->is_concurrent_gc_enabled_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700698
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700699 BlockSignals();
Elliott Hughes457005c2012-04-16 13:54:25 -0700700 InitPlatformSignalHandlers();
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700701
Elliott Hughesa0957642011-09-02 14:27:33 -0700702 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700703
Elliott Hughesbe759c62011-09-08 19:38:21 -0700704 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700705
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700706 // ClassLinker needs an attached thread, but we can't fully attach a thread
Elliott Hughes462c9442012-03-23 18:47:50 -0700707 // without creating objects. We can't supply a thread group yet; it will be fixed later.
708 Thread::Attach("main", false, NULL);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700709
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700710 // Set us to runnable so tools using a runtime can allocate and GC by default
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700711 Thread::Current()->TransitionFromSuspendedToRunnable();
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700712
Ian Rogers141d6222012-04-05 12:23:06 -0700713 // Now we're attached, we can take the heap lock and validate the heap.
714 GetHeap()->EnableObjectValidation();
715
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800716 CHECK_GE(GetHeap()->GetSpaces().size(), 1U);
717 if (GetHeap()->GetSpaces()[0]->IsImageSpace()) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800718 class_linker_ = ClassLinker::CreateFromImage(intern_table_);
719 } else {
720 CHECK(options->boot_class_path_ != NULL);
721 CHECK_NE(options->boot_class_path_->size(), 0U);
722 class_linker_ = ClassLinker::CreateFromCompiler(*options->boot_class_path_, intern_table_);
723 }
724 CHECK(class_linker_ != NULL);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700725
jeffhaob5e81852012-03-12 11:15:45 -0700726 method_trace_ = options->method_trace_;
727 method_trace_file_ = options->method_trace_file_;
728 method_trace_file_size_ = options->method_trace_file_size_;
729
730 if (options->method_trace_) {
731 Trace::Start(options->method_trace_file_.c_str(), -1, options->method_trace_file_size_, 0, false);
732 }
733
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800734 VLOG(startup) << "Runtime::Init exiting";
Carl Shapiro1fb86202011-06-27 17:43:13 -0700735 return true;
736}
737
Elliott Hughes038a8062011-09-18 14:12:41 -0700738void Runtime::InitNativeMethods() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800739 VLOG(startup) << "Runtime::InitNativeMethods entering";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700740 Thread* self = Thread::Current();
741 JNIEnv* env = self->GetJniEnv();
742
Elliott Hughes418d20f2011-09-22 14:00:39 -0700743 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700744 {
Ian Rogersb726dcb2012-09-05 08:57:23 -0700745 MutexLock mu(*Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700746 CHECK_EQ(self->GetState(), kNative);
747 }
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700748
Elliott Hughes418d20f2011-09-22 14:00:39 -0700749 // First set up JniConstants, which is used by both the runtime's built-in native
750 // methods and libcore.
Elliott Hughesfea966e2011-09-22 10:26:20 -0700751 JniConstants::init(env);
Elliott Hugheseac76672012-05-24 21:56:51 -0700752 WellKnownClasses::Init(env);
Elliott Hughesfea966e2011-09-22 10:26:20 -0700753
Elliott Hughes418d20f2011-09-22 14:00:39 -0700754 // Then set up the native methods provided by the runtime itself.
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700755 RegisterRuntimeNativeMethods(env);
756
Elliott Hughes418d20f2011-09-22 14:00:39 -0700757 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
758 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
759 // the library that implements System.loadLibrary!
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700760 {
761 std::string mapped_name(StringPrintf(OS_SHARED_LIB_FORMAT_STR, "javacore"));
762 std::string reason;
763 self->TransitionFromSuspendedToRunnable();
764 if (!instance_->java_vm_->LoadNativeLibrary(mapped_name, NULL, reason)) {
765 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": " << reason;
766 }
767 self->TransitionFromRunnableToSuspended(kNative);
768 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800769 VLOG(startup) << "Runtime::InitNativeMethods exiting";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700770}
771
Ian Rogers365c1022012-06-22 15:05:28 -0700772void Runtime::InitThreadGroups(Thread* self) {
773 JNIEnvExt* env = self->GetJniEnv();
774 ScopedJniEnvLocalRefState env_state(env);
775 main_thread_group_ =
776 env->NewGlobalRef(env->GetStaticObjectField(WellKnownClasses::java_lang_ThreadGroup,
777 WellKnownClasses::java_lang_ThreadGroup_mainThreadGroup));
Brian Carlstrom034f76b2012-08-01 15:51:58 -0700778 CHECK(main_thread_group_ != NULL || IsCompiler());
Ian Rogers365c1022012-06-22 15:05:28 -0700779 system_thread_group_ =
780 env->NewGlobalRef(env->GetStaticObjectField(WellKnownClasses::java_lang_ThreadGroup,
781 WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup));
Brian Carlstrom034f76b2012-08-01 15:51:58 -0700782 CHECK(system_thread_group_ != NULL || IsCompiler());
783}
784
785jobject Runtime::GetMainThreadGroup() const {
786 CHECK(main_thread_group_ != NULL || IsCompiler());
787 return main_thread_group_;
788}
789
790jobject Runtime::GetSystemThreadGroup() const {
791 CHECK(system_thread_group_ != NULL || IsCompiler());
792 return system_thread_group_;
Ian Rogers365c1022012-06-22 15:05:28 -0700793}
794
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700795void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
796#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Ian Rogers5167c972012-02-03 10:41:20 -0800797 // Register Throwable first so that registration of other native methods can throw exceptions
798 REGISTER(register_java_lang_Throwable);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700799 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700800 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700801 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700802 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700803 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700804 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700805 REGISTER(register_java_lang_Object);
806 REGISTER(register_java_lang_Runtime);
807 REGISTER(register_java_lang_String);
808 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700809 REGISTER(register_java_lang_Thread);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700810 REGISTER(register_java_lang_VMClassLoader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700811 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700812 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700813 REGISTER(register_java_lang_reflect_Field);
814 REGISTER(register_java_lang_reflect_Method);
Jesse Wilson95caa792011-10-12 18:14:17 -0400815 REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700816 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700817 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700818 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700819 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700820#undef REGISTER
821}
822
Elliott Hughesc967f782012-04-16 10:23:15 -0700823void Runtime::DumpForSigQuit(std::ostream& os) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700824 GetClassLinker()->DumpForSigQuit(os);
825 GetInternTable()->DumpForSigQuit(os);
Elliott Hughesae80b492012-04-24 10:43:17 -0700826 GetJavaVM()->DumpForSigQuit(os);
Elliott Hughesc967f782012-04-16 10:23:15 -0700827 GetHeap()->DumpForSigQuit(os);
Elliott Hughes42ee1422011-09-06 12:33:32 -0700828 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700829
Elliott Hughesc967f782012-04-16 10:23:15 -0700830 thread_list_->DumpForSigQuit(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700831}
832
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800833void Runtime::DumpLockHolders(std::ostream& os) {
Ian Rogersb726dcb2012-09-05 08:57:23 -0700834 uint64_t mutator_lock_owner = Locks::mutator_lock_->GetExclusiveOwnerTid();
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800835 pid_t thread_list_lock_owner = GetThreadList()->GetLockOwner();
836 pid_t classes_lock_owner = GetClassLinker()->GetClassesLockOwner();
837 pid_t dex_lock_owner = GetClassLinker()->GetDexLockOwner();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700838 if ((thread_list_lock_owner | classes_lock_owner | dex_lock_owner) != 0) {
839 os << "Mutator lock exclusive owner tid: " << mutator_lock_owner << "\n"
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800840 << "ThreadList lock owner tid: " << thread_list_lock_owner << "\n"
841 << "ClassLinker classes lock owner tid: " << classes_lock_owner << "\n"
842 << "ClassLinker dex lock owner tid: " << dex_lock_owner << "\n";
843 }
844}
845
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700846void Runtime::SetStatsEnabled(bool new_state) {
847 if (new_state == true) {
848 GetStats()->Clear(~0);
849 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
850 Thread::Current()->GetStats()->Clear(~0);
851 }
852 stats_enabled_ = new_state;
853}
854
855void Runtime::ResetStats(int kinds) {
856 GetStats()->Clear(kinds & 0xffff);
857 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
858 Thread::Current()->GetStats()->Clear(kinds >> 16);
859}
860
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700861int32_t Runtime::GetStat(int kind) {
862 RuntimeStats* stats;
863 if (kind < (1<<16)) {
864 stats = GetStats();
865 } else {
866 stats = Thread::Current()->GetStats();
867 kind >>= 16;
868 }
869 switch (kind) {
870 case KIND_ALLOCATED_OBJECTS:
871 return stats->allocated_objects;
872 case KIND_ALLOCATED_BYTES:
873 return stats->allocated_bytes;
874 case KIND_FREED_OBJECTS:
875 return stats->freed_objects;
876 case KIND_FREED_BYTES:
877 return stats->freed_bytes;
878 case KIND_GC_INVOCATIONS:
879 return stats->gc_for_alloc_count;
880 case KIND_CLASS_INIT_COUNT:
881 return stats->class_init_count;
882 case KIND_CLASS_INIT_TIME:
883 // Convert ns to us, reduce to 32 bits.
Elliott Hughes398f64b2012-03-26 18:05:48 -0700884 return static_cast<int>(stats->class_init_time_ns / 1000);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700885 case KIND_EXT_ALLOCATED_OBJECTS:
886 case KIND_EXT_ALLOCATED_BYTES:
887 case KIND_EXT_FREED_OBJECTS:
888 case KIND_EXT_FREED_BYTES:
889 return 0; // backward compatibility
890 default:
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700891 LOG(FATAL) << "Unknown statistic " << kind;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700892 return -1; // unreachable
893 }
894}
895
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700896void Runtime::BlockSignals() {
Elliott Hughes457005c2012-04-16 13:54:25 -0700897 SignalSet signals;
898 signals.Add(SIGPIPE);
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700899 // SIGQUIT is used to dump the runtime's state (including stack traces).
Elliott Hughes457005c2012-04-16 13:54:25 -0700900 signals.Add(SIGQUIT);
Elliott Hughes08795042012-04-03 14:48:52 -0700901 // SIGUSR1 is used to initiate a GC.
Elliott Hughes457005c2012-04-16 13:54:25 -0700902 signals.Add(SIGUSR1);
903 signals.Block();
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700904}
905
Ian Rogers365c1022012-06-22 15:05:28 -0700906void Runtime::AttachCurrentThread(const char* thread_name, bool as_daemon, jobject thread_group) {
Elliott Hughes462c9442012-03-23 18:47:50 -0700907 Thread::Attach(thread_name, as_daemon, thread_group);
Elliott Hughes22869a92012-03-27 14:08:24 -0700908 if (thread_name == NULL) {
909 LOG(WARNING) << *Thread::Current() << " attached without supplying a name";
910 }
Carl Shapiro61e019d2011-07-14 16:53:09 -0700911}
912
Elliott Hughesd92bec42011-09-02 17:04:36 -0700913void Runtime::DetachCurrentThread() {
Brian Carlstrom4d571432012-05-16 00:21:41 -0700914 Thread* self = Thread::Current();
915 if (self == NULL) {
916 LOG(FATAL) << "attempting to detach thread that is not attached";
917 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700918 if (self->HasManagedStack()) {
Elliott Hughes22869a92012-03-27 14:08:24 -0700919 LOG(FATAL) << *Thread::Current() << " attempting to detach while still running code";
920 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700921 thread_list_->Unregister(self);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700922}
923
Brian Carlstrome24fa612011-09-29 00:53:55 -0700924void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700925 Dbg::VisitRoots(visitor, arg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700926 class_linker_->VisitRoots(visitor, arg);
927 intern_table_->VisitRoots(visitor, arg);
928 java_vm_->VisitRoots(visitor, arg);
929 thread_list_->VisitRoots(visitor, arg);
Elliott Hughes225f5a12012-06-11 11:23:48 -0700930 if (pre_allocated_OutOfMemoryError_ != NULL) {
931 visitor(pre_allocated_OutOfMemoryError_, arg);
932 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700933 visitor(jni_stub_array_, arg);
934 visitor(abstract_method_error_stub_array_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700935 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
936 visitor(resolution_stub_array_[i], arg);
937 }
Ian Rogers19846512012-02-24 11:42:47 -0800938 visitor(resolution_method_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700939 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
Elliott Hughes225f5a12012-06-11 11:23:48 -0700940 visitor(callee_save_methods_[i], arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700941 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700942}
943
Ian Rogers169c9a72011-11-13 20:13:17 -0800944void Runtime::SetJniDlsymLookupStub(ByteArray* jni_stub_array) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700945 CHECK(jni_stub_array != NULL) << " jni_stub_array=" << jni_stub_array;
946 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array)
947 << "jni_stub_array_=" << jni_stub_array_ << " jni_stub_array=" << jni_stub_array;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700948 jni_stub_array_ = jni_stub_array;
949}
950
Brian Carlstrome24fa612011-09-29 00:53:55 -0700951void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) {
952 CHECK(abstract_method_error_stub_array != NULL);
953 CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array);
954 abstract_method_error_stub_array_ = abstract_method_error_stub_array;
955}
956
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700957void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700958 CHECK(resolution_stub_array != NULL);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700959 CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array);
960 resolution_stub_array_[type] = resolution_stub_array;
Ian Rogersad25ac52011-10-04 19:13:33 -0700961}
962
Mathieu Chartier66f19252012-09-18 08:57:04 -0700963AbstractMethod* Runtime::CreateResolutionMethod() {
964 Class* method_class = AbstractMethod::GetMethodClass();
965 SirtRef<AbstractMethod> method(down_cast<AbstractMethod*>(method_class->AllocObject()));
Ian Rogers19846512012-02-24 11:42:47 -0800966 method->SetDeclaringClass(method_class);
967 // TODO: use a special method for resolution method saves
968 method->SetDexMethodIndex(DexFile::kDexNoIndex16);
969 ByteArray* unknown_resolution_stub = GetResolutionStubArray(kUnknownMethod);
970 CHECK(unknown_resolution_stub != NULL);
971 method->SetCode(unknown_resolution_stub->GetData());
972 return method.get();
973}
974
Mathieu Chartier66f19252012-09-18 08:57:04 -0700975AbstractMethod* Runtime::CreateCalleeSaveMethod(InstructionSet instruction_set, CalleeSaveType type) {
976 Class* method_class = AbstractMethod::GetMethodClass();
977 SirtRef<AbstractMethod> method(down_cast<AbstractMethod*>(method_class->AllocObject()));
Ian Rogersff1ed472011-09-20 13:46:24 -0700978 method->SetDeclaringClass(method_class);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800979 // TODO: use a special method for callee saves
Ian Rogers19846512012-02-24 11:42:47 -0800980 method->SetDexMethodIndex(DexFile::kDexNoIndex16);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700981 method->SetCode(NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800982 if ((instruction_set == kThumb2) || (instruction_set == kArm)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700983 uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6) | (1 << art::arm::R7) |
984 (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11);
985 uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3);
986 uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9);
987 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills :0) |
988 (type == kSaveAll ? all_spills :0) | (1 << art::arm::LR);
989 uint32_t fp_all_spills = (1 << art::arm::S0) | (1 << art::arm::S1) | (1 << art::arm::S2) |
990 (1 << art::arm::S3) | (1 << art::arm::S4) | (1 << art::arm::S5) |
991 (1 << art::arm::S6) | (1 << art::arm::S7) | (1 << art::arm::S8) |
992 (1 << art::arm::S9) | (1 << art::arm::S10) | (1 << art::arm::S11) |
993 (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) |
994 (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) |
995 (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) |
996 (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) |
997 (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) |
998 (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
999 (1 << art::arm::S30) | (1 << art::arm::S31);
1000 uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
1001 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
1002 __builtin_popcount(fp_spills) /* fprs */ +
1003 1 /* Method* */) * kPointerSize, kStackAlignment);
Ian Rogers15fdb8c2011-09-25 15:45:07 -07001004 method->SetFrameSizeInBytes(frame_size);
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001005 method->SetCoreSpillMask(core_spills);
1006 method->SetFpSpillMask(fp_spills);
jeffhao7fbee072012-08-24 17:56:54 -07001007 } else if (instruction_set == kMips) {
1008 uint32_t ref_spills = (1 << art::mips::S2) | (1 << art::mips::S3) | (1 << art::mips::S4) |
1009 (1 << art::mips::S5) | (1 << art::mips::S6) | (1 << art::mips::S7) |
1010 (1 << art::mips::FP);
1011 uint32_t arg_spills = (1 << art::mips::A1) | (1 << art::mips::A2) | (1 << art::mips::A3);
jeffhao7fbee072012-08-24 17:56:54 -07001012 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills : 0) |
jeffhao07030602012-09-26 14:33:14 -07001013 (1 << art::mips::RA);
jeffhao7fbee072012-08-24 17:56:54 -07001014 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
jeffhao7fbee072012-08-24 17:56:54 -07001015 1 /* Method* */) * kPointerSize, kStackAlignment);
1016 method->SetFrameSizeInBytes(frame_size);
1017 method->SetCoreSpillMask(core_spills);
jeffhao07030602012-09-26 14:33:14 -07001018 method->SetFpSpillMask(0);
Brian Carlstromae826982011-11-09 01:33:42 -08001019 } else if (instruction_set == kX86) {
Ian Rogers7caad772012-03-30 01:07:54 -07001020 uint32_t ref_spills = (1 << art::x86::EBP) | (1 << art::x86::ESI) | (1 << art::x86::EDI);
1021 uint32_t arg_spills = (1 << art::x86::ECX) | (1 << art::x86::EDX) | (1 << art::x86::EBX);
1022 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills : 0) |
1023 (1 << art::x86::kNumberOfCpuRegisters); // fake return address callee save
1024 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
1025 1 /* Method* */) * kPointerSize, kStackAlignment);
1026 method->SetFrameSizeInBytes(frame_size);
1027 method->SetCoreSpillMask(core_spills);
Ian Rogersff1ed472011-09-20 13:46:24 -07001028 method->SetFpSpillMask(0);
1029 } else {
1030 UNIMPLEMENTED(FATAL);
1031 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001032 return method.get();
Ian Rogersff1ed472011-09-20 13:46:24 -07001033}
1034
Mathieu Chartier66f19252012-09-18 08:57:04 -07001035void Runtime::SetCalleeSaveMethod(AbstractMethod* method, CalleeSaveType type) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001036 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
Elliott Hughes225f5a12012-06-11 11:23:48 -07001037 callee_save_methods_[type] = method;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001038}
1039
jeffhao2692b572011-12-16 15:42:28 -08001040void Runtime::EnableMethodTracing(Trace* tracer) {
1041 CHECK(!IsMethodTracingActive());
1042 tracer_ = tracer;
1043}
1044
1045void Runtime::DisableMethodTracing() {
1046 CHECK(IsMethodTracingActive());
1047 delete tracer_;
1048 tracer_ = NULL;
1049}
1050
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001051const std::vector<const DexFile*>& Runtime::GetCompileTimeClassPath(jobject class_loader) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001052 if (class_loader == NULL) {
1053 return GetClassLinker()->GetBootClassPath();
1054 }
1055 CHECK(UseCompileTimeClassPath());
1056 CompileTimeClassPaths::const_iterator it = compile_time_class_paths_.find(class_loader);
1057 CHECK(it != compile_time_class_paths_.end());
1058 return it->second;
1059}
1060
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001061void Runtime::SetCompileTimeClassPath(jobject class_loader, std::vector<const DexFile*>& class_path) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001062 CHECK(!IsStarted());
1063 use_compile_time_class_path_ = true;
Elliott Hughesa0e18062012-04-13 15:59:59 -07001064 compile_time_class_paths_.Put(class_loader, class_path);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001065}
1066
Carl Shapiro1fb86202011-06-27 17:43:13 -07001067} // namespace art