blob: 6447ede6a01fed62c906d7a5830001eb88e134bf [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro1fb86202011-06-27 17:43:13 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "runtime.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070018
Brian Carlstromdbf05b72011-12-15 00:55:24 -080019#include <signal.h>
Elliott Hughesd06a6c72012-05-30 17:59:06 -070020#include <sys/syscall.h>
Brian Carlstromdbf05b72011-12-15 00:55:24 -080021
Elliott Hughesffe67362011-07-17 12:09:27 -070022#include <cstdio>
23#include <cstdlib>
Brian Carlstrom8a436592011-08-15 21:27:23 -070024#include <limits>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070025#include <vector>
Elliott Hughesffe67362011-07-17 12:09:27 -070026
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070027#include "class_linker.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070028#include "class_loader.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070029#include "constants_arm.h"
30#include "constants_x86.h"
Elliott Hughes3bb81562011-10-21 18:52:59 -070031#include "debugger.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070032#include "heap.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070033#include "image.h"
Elliott Hughescf4c6c42011-09-01 15:16:42 -070034#include "intern_table.h"
Elliott Hughesc5f7c912011-08-18 14:00:42 -070035#include "jni_internal.h"
Elliott Hughes32d6e1e2011-10-11 14:47:44 -070036#include "monitor.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070037#include "oat_file.h"
Mathieu Chartiera6399032012-06-11 18:49:50 -070038#include "scoped_heap_lock.h"
Ian Rogers365c1022012-06-22 15:05:28 -070039#include "scoped_jni_thread_state.h"
Elliott Hughes726079d2011-10-07 18:43:44 -070040#include "ScopedLocalRef.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.
121 {
122 ScopedHeapLock heap_lock;
123 heap_->WaitForConcurrentGcToComplete();
124 }
125
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700126 // Make sure our internal threads are dead before we start tearing down things they're using.
Elliott Hughese52e49b2012-04-02 16:05:44 -0700127 Dbg::StopJdwp();
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700128 delete signal_catcher_;
129
Elliott Hughes038a8062011-09-18 14:12:41 -0700130 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
Elliott Hughes93e74e82011-09-13 11:07:03 -0700131 delete thread_list_;
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700132 delete monitor_list_;
Elliott Hughes93e74e82011-09-13 11:07:03 -0700133
Carl Shapiro61e019d2011-07-14 16:53:09 -0700134 delete class_linker_;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800135 delete heap_;
Elliott Hughes0a1038b2012-06-14 16:24:17 -0700136 verifier::MethodVerifier::Shutdown();
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700137 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700138 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700139 Thread::Shutdown();
Elliott Hughes7c6169d2012-05-02 16:11:48 -0700140 QuasiAtomic::Shutdown();
141
Carl Shapiro4acf4642011-07-26 18:54:13 -0700142 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700143 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -0700144 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700145}
146
Elliott Hughescac6cc72011-11-03 20:31:21 -0700147static bool gAborting = false;
148
Elliott Hughese0918552011-10-28 17:18:29 -0700149struct AbortState {
150 void Dump(std::ostream& os) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700151 if (gAborting) {
152 os << "Runtime aborting --- recursively, so no thread-specific detail!\n";
153 return;
154 }
155 gAborting = true;
Elliott Hughese0918552011-10-28 17:18:29 -0700156 os << "Runtime aborting...\n";
Elliott Hughes6c7d2442012-02-01 18:40:47 -0800157 if (Runtime::Current() == NULL) {
158 os << "(Runtime does not yet exist!)\n";
159 return;
160 }
Elliott Hughese0918552011-10-28 17:18:29 -0700161 Thread* self = Thread::Current();
162 if (self == NULL) {
163 os << "(Aborting thread was not attached to runtime!)\n";
164 } else {
Elliott Hughes899e7892012-01-24 14:57:32 -0800165 self->Dump(os);
166 if (self->IsExceptionPending()) {
167 os << "Pending " << PrettyTypeOf(self->GetException()) << " on thread:\n"
168 << self->GetException()->Dump();
169 }
Elliott Hughese0918552011-10-28 17:18:29 -0700170 }
171 }
172};
173
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800174static Mutex& GetAbortLock() {
175 static Mutex abort_lock("abort lock");
176 return abort_lock;
177}
178
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700179void Runtime::Abort() {
Elliott Hughes131aef82012-01-27 11:53:35 -0800180 // Ensure that we don't have multiple threads trying to abort at once,
181 // which would result in significantly worse diagnostics.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800182 MutexLock mu(GetAbortLock());
Elliott Hughes131aef82012-01-27 11:53:35 -0800183
Elliott Hughesffe67362011-07-17 12:09:27 -0700184 // Get any pending output out of the way.
185 fflush(NULL);
186
187 // Many people have difficulty distinguish aborts from crashes,
188 // so be explicit.
Elliott Hughese0918552011-10-28 17:18:29 -0700189 AbortState state;
Elliott Hughes9ee5f9c2012-02-03 18:33:16 -0800190 LOG(INTERNAL_FATAL) << Dumpable<AbortState>(state);
Elliott Hughesffe67362011-07-17 12:09:27 -0700191
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700192 // Call the abort hook if we have one.
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700193 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700194 LOG(INTERNAL_FATAL) << "Calling abort hook...";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700195 Runtime::Current()->abort_();
196 // notreached
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700197 LOG(INTERNAL_FATAL) << "Unexpectedly returned from abort hook!";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700198 }
199
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700200#if defined(__BIONIC__)
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700201 // TODO: finish merging patches to fix abort(3) in bionic, then lose this!
202 // Bionic doesn't implement POSIX semantics for abort(3) in a multi-threaded
203 // process, so if we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -0700204 // receive SIGABRT. debuggerd dumps the stack trace of the main
205 // thread, whether or not that was the thread that failed. By
206 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -0700207 // fault in the current thread, and get a useful log from debuggerd.
Elliott Hughes81ff3182012-03-23 20:35:56 -0700208 // We can also trivially tell the difference between a crash and
Elliott Hughesffe67362011-07-17 12:09:27 -0700209 // a deliberate abort by looking at the fault address.
210 *reinterpret_cast<char*>(0xdeadd00d) = 38;
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700211#elif defined(__APPLE__)
212 // TODO: check that this actually gives good stack traces on the Mac!
213 pthread_kill(pthread_self(), SIGABRT);
214#else
215 // TODO: we ought to be able to use pthread_kill(3) here (or abort(3),
216 // which POSIX defines in terms of raise(3), which POSIX defines in terms
217 // of pthread_kill(3)). On Linux, though, libcorkscrew can't unwind through
218 // libpthread, which means the stacks we dump would be useless. Calling
219 // tgkill(2) directly avoids that.
220 syscall(__NR_tgkill, getpid(), GetTid(), SIGABRT);
221#endif
Elliott Hughesffe67362011-07-17 12:09:27 -0700222 // notreached
223}
224
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700225bool Runtime::PreZygoteFork() {
226 heap_->PreZygoteFork();
227 return true;
228}
229
Elliott Hughesbf86d042011-08-31 17:53:14 -0700230void Runtime::CallExitHook(jint status) {
231 if (exit_ != NULL) {
Elliott Hughes34e06962012-04-09 13:55:55 -0700232 ScopedThreadStateChange tsc(Thread::Current(), kNative);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700233 exit_(status);
234 LOG(WARNING) << "Exit hook returned instead of exiting!";
235 }
236}
237
Brian Carlstrom8a436592011-08-15 21:27:23 -0700238// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
239// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
240// [gG] gigabytes.
241//
242// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700243// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
244// of 1024.
245//
246// The spec says the -Xmx and -Xms options must be multiples of 1024. It
247// doesn't say anything about -Xss.
248//
249// Returns 0 (a useless size) if "s" is malformed or specifies a low or
250// non-evenly-divisible value.
251//
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800252size_t ParseMemoryOption(const char* s, size_t div) {
Brian Carlstrom8a436592011-08-15 21:27:23 -0700253 // strtoul accepts a leading [+-], which we don't want,
254 // so make sure our string starts with a decimal digit.
255 if (isdigit(*s)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700256 char* s2;
257 size_t val = strtoul(s, &s2, 10);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700258 if (s2 != s) {
259 // s2 should be pointing just after the number.
260 // If this is the end of the string, the user
261 // has specified a number of bytes. Otherwise,
262 // there should be exactly one more character
263 // that specifies a multiplier.
264 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700265 // The remainder of the string is either a single multiplier
266 // character, or nothing to indicate that the value is in
267 // bytes.
268 char c = *s2++;
269 if (*s2 == '\0') {
270 size_t mul;
271 if (c == '\0') {
272 mul = 1;
273 } else if (c == 'k' || c == 'K') {
274 mul = KB;
275 } else if (c == 'm' || c == 'M') {
276 mul = MB;
277 } else if (c == 'g' || c == 'G') {
278 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700279 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700280 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700281 return 0;
282 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700283
284 if (val <= std::numeric_limits<size_t>::max() / mul) {
285 val *= mul;
286 } else {
287 // Clamp to a multiple of 1024.
288 val = std::numeric_limits<size_t>::max() & ~(1024-1);
289 }
290 } else {
291 // There's more than one character after the numeric part.
292 return 0;
293 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700294 }
295 // The man page says that a -Xm value must be a multiple of 1024.
296 if (val % div == 0) {
297 return val;
298 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700299 }
300 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700301 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700302}
303
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800304size_t ParseIntegerOrDie(const std::string& s) {
305 std::string::size_type colon = s.find(':');
306 if (colon == std::string::npos) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700307 LOG(FATAL) << "Missing integer: " << s;
308 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800309 const char* begin = &s[colon + 1];
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700310 char* end;
311 size_t result = strtoul(begin, &end, 10);
312 if (begin == end || *end != '\0') {
313 LOG(FATAL) << "Failed to parse integer in: " << s;
314 }
315 return result;
316}
317
Elliott Hughes0af55432011-08-17 18:37:28 -0700318void LoadJniLibrary(JavaVMExt* vm, const char* name) {
Elliott Hughes6cc332e2012-01-20 22:59:20 -0800319 std::string mapped_name(StringPrintf(OS_SHARED_LIB_FORMAT_STR, name));
Elliott Hughes75770752011-08-24 17:52:38 -0700320 std::string reason;
321 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700322 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
323 << reason;
324 }
325}
326
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700327Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700328 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800329 const char* boot_class_path_string = getenv("BOOTCLASSPATH");
330 if (boot_class_path_string != NULL) {
331 parsed->boot_class_path_string_ = boot_class_path_string;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700332 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800333 const char* class_path_string = getenv("CLASSPATH");
334 if (class_path_string != NULL) {
335 parsed->class_path_string_ = class_path_string;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700336 }
Elliott Hughes67d92002012-03-26 15:08:51 -0700337 // -Xcheck:jni is off by default for regular builds but on by default in debug builds.
338 parsed->check_jni_ = kIsDebugBuild;
Elliott Hughes85d15452011-09-16 17:33:01 -0700339
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700340 parsed->heap_initial_size_ = Heap::kInitialSize;
341 parsed->heap_maximum_size_ = Heap::kMaximumSize;
Elliott Hughes6cf23882012-06-15 15:42:07 -0700342 parsed->heap_growth_limit_ = 0; // 0 means no growth limit.
343 parsed->stack_size_ = 0; // 0 means default.
Brian Carlstromf734cf52011-08-17 16:28:14 -0700344
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800345 parsed->is_compiler_ = false;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700346 parsed->is_zygote_ = false;
Mathieu Chartier069387a2012-06-18 12:01:01 -0700347 parsed->is_concurrent_gc_enabled_ = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700348
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700349 parsed->jni_globals_max_ = 0;
Elliott Hughesfc861622011-10-17 17:57:47 -0700350 parsed->lock_profiling_threshold_ = 0;
351 parsed->hook_is_sensitive_thread_ = NULL;
352
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700353 parsed->hook_vfprintf_ = vfprintf;
354 parsed->hook_exit_ = exit;
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700355 parsed->hook_abort_ = NULL; // We don't call abort(3) by default; see Runtime::Abort.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700356
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800357// gLogVerbosity.class_linker = true; // TODO: don't check this in!
358// gLogVerbosity.compiler = true; // TODO: don't check this in!
359// gLogVerbosity.heap = true; // TODO: don't check this in!
360// gLogVerbosity.gc = true; // TODO: don't check this in!
361// gLogVerbosity.jdwp = true; // TODO: don't check this in!
362// gLogVerbosity.jni = true; // TODO: don't check this in!
363// gLogVerbosity.monitor = true; // TODO: don't check this in!
364// gLogVerbosity.startup = true; // TODO: don't check this in!
365// gLogVerbosity.third_party_jni = true; // TODO: don't check this in!
366// gLogVerbosity.threads = true; // TODO: don't check this in!
367
jeffhaob5e81852012-03-12 11:15:45 -0700368 parsed->method_trace_ = false;
369 parsed->method_trace_file_ = "/data/method-trace-file.bin";
370 parsed->method_trace_file_size_ = 10 * MB;
371
Brian Carlstrom8a436592011-08-15 21:27:23 -0700372 for (size_t i = 0; i < options.size(); ++i) {
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800373 const std::string option(options[i].first);
Brian Carlstrom0796af02011-10-12 14:31:45 -0700374 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700375 LOG(INFO) << "option[" << i << "]=" << option;
376 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800377 if (StartsWith(option, "-Xbootclasspath:")) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800378 parsed->boot_class_path_string_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700379 } else if (option == "-classpath" || option == "-cp") {
380 // TODO: support -Djava.class.path
381 i++;
382 if (i == options.size()) {
383 // TODO: usage
384 LOG(FATAL) << "Missing required class path value for " << option;
385 return NULL;
386 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800387 const StringPiece& value = options[i].first;
388 parsed->class_path_string_ = value.data();
389 } else if (option == "bootclasspath") {
390 parsed->boot_class_path_
391 = reinterpret_cast<const std::vector<const DexFile*>*>(options[i].second);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800392 } else if (StartsWith(option, "-Ximage:")) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800393 parsed->image_ = option.substr(strlen("-Ximage:")).data();
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800394 } else if (StartsWith(option, "-Xcheck:jni")) {
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700395 parsed->check_jni_ = true;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800396 } else if (StartsWith(option, "-Xrunjdwp:") || StartsWith(option, "-agentlib:jdwp=")) {
397 std::string tail(option.substr(option[1] == 'X' ? 10 : 15));
Elliott Hughes3bb81562011-10-21 18:52:59 -0700398 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
399 LOG(FATAL) << "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
400 << "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n";
401 return NULL;
402 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800403 } else if (StartsWith(option, "-Xms")) {
404 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).c_str(), 1024);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700405 if (size == 0) {
406 if (ignore_unrecognized) {
407 continue;
408 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700409 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700410 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700411 return NULL;
412 }
413 parsed->heap_initial_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800414 } else if (StartsWith(option, "-Xmx")) {
415 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).c_str(), 1024);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700416 if (size == 0) {
417 if (ignore_unrecognized) {
418 continue;
419 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700420 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700421 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700422 return NULL;
423 }
424 parsed->heap_maximum_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800425 } else if (StartsWith(option, "-XX:HeapGrowthLimit=")) {
426 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).c_str(), 1024);
jeffhaoc1160702011-10-27 15:48:45 -0700427 if (size == 0) {
428 if (ignore_unrecognized) {
429 continue;
430 }
431 // TODO: usage
432 LOG(FATAL) << "Failed to parse " << option;
433 return NULL;
434 }
435 parsed->heap_growth_limit_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800436 } else if (StartsWith(option, "-Xss")) {
437 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).c_str(), 1);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700438 if (size == 0) {
439 if (ignore_unrecognized) {
440 continue;
441 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700442 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700443 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700444 return NULL;
445 }
446 parsed->stack_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800447 } else if (StartsWith(option, "-D")) {
448 parsed->properties_.push_back(option.substr(strlen("-D")));
449 } else if (StartsWith(option, "-Xjnitrace:")) {
450 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:"));
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700451 } else if (option == "compiler") {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800452 parsed->is_compiler_ = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700453 } else if (option == "-Xzygote") {
454 parsed->is_zygote_ = true;
Mathieu Chartier069387a2012-06-18 12:01:01 -0700455 } else if (StartsWith(option, "-Xgc:")) {
456 std::vector<std::string> gc_options;
457 Split(option.substr(strlen("-Xgc:")), ',', gc_options);
458 for (size_t i = 0; i < gc_options.size(); ++i) {
459 if (gc_options[i] == "noconcurrent") {
460 parsed->is_concurrent_gc_enabled_ = false;
461 } else if (gc_options[i] == "concurrent") {
462 parsed->is_concurrent_gc_enabled_ = true;
463 } else {
464 LOG(WARNING) << "Ignoring unknown -Xgc option: " << gc_options[i];
465 }
466 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800467 } else if (StartsWith(option, "-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700468 std::vector<std::string> verbose_options;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800469 Split(option.substr(strlen("-verbose:")), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700470 for (size_t i = 0; i < verbose_options.size(); ++i) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800471 if (verbose_options[i] == "class") {
472 gLogVerbosity.class_linker = true;
473 } else if (verbose_options[i] == "compiler") {
474 gLogVerbosity.compiler = true;
475 } else if (verbose_options[i] == "heap") {
476 gLogVerbosity.heap = true;
477 } else if (verbose_options[i] == "gc") {
478 gLogVerbosity.gc = true;
479 } else if (verbose_options[i] == "jdwp") {
480 gLogVerbosity.jdwp = true;
481 } else if (verbose_options[i] == "jni") {
482 gLogVerbosity.jni = true;
483 } else if (verbose_options[i] == "monitor") {
484 gLogVerbosity.monitor = true;
485 } else if (verbose_options[i] == "startup") {
486 gLogVerbosity.startup = true;
487 } else if (verbose_options[i] == "third-party-jni") {
488 gLogVerbosity.third_party_jni = true;
489 } else if (verbose_options[i] == "threads") {
490 gLogVerbosity.threads = true;
491 } else {
492 LOG(WARNING) << "Ignoring unknown -verbose option: " << verbose_options[i];
493 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700494 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800495 } else if (StartsWith(option, "-Xjnigreflimit:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700496 parsed->jni_globals_max_ = ParseIntegerOrDie(option);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800497 } else if (StartsWith(option, "-Xlockprofthreshold:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700498 parsed->lock_profiling_threshold_ = ParseIntegerOrDie(option);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800499 } else if (StartsWith(option, "-Xstacktracefile:")) {
Elliott Hughes206c20d2012-06-28 11:05:37 -0700500 parsed->stack_trace_file_ = option.substr(strlen("-Xstacktracefile:"));
Elliott Hughesfc861622011-10-17 17:57:47 -0700501 } else if (option == "sensitiveThread") {
502 parsed->hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(options[i].second);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700503 } else if (option == "vfprintf") {
504 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
505 } else if (option == "exit") {
506 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
507 } else if (option == "abort") {
508 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700509 } else if (option == "host-prefix") {
510 parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second);
Elliott Hughesb3e66df2012-01-12 14:49:18 -0800511 } else if (option == "-Xgenregmap" || option == "-Xgc:precise") {
512 // We silently ignore these for backwards compatibility.
jeffhaob5e81852012-03-12 11:15:45 -0700513 } else if (option == "-Xmethod-trace") {
514 parsed->method_trace_ = true;
515 } else if (StartsWith(option, "-Xmethod-trace-file:")) {
516 parsed->method_trace_file_ = option.substr(strlen("-Xmethod-trace-file:"));
517 } else if (StartsWith(option, "-Xmethod-trace-file-size:")) {
518 parsed->method_trace_file_size_ = ParseIntegerOrDie(option);
Elliott Hughese119a362012-05-22 17:37:06 -0700519 } else if (option == "-Xprofile:threadcpuclock") {
520 Trace::SetDefaultClockSource(kProfilerClockSourceThreadCpu);
521 } else if (option == "-Xprofile:wallclock") {
522 Trace::SetDefaultClockSource(kProfilerClockSourceWall);
523 } else if (option == "-Xprofile:dualclock") {
524 Trace::SetDefaultClockSource(kProfilerClockSourceDual);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700525 } else {
526 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700527 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700528 LOG(ERROR) << "Unrecognized option " << option;
529 // TODO: this should exit, but for now tolerate unknown options
530 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700531 }
532 }
533 }
534
Brian Carlstrom223f20f2012-02-04 23:06:55 -0800535 if (!parsed->is_compiler_ && parsed->image_.empty()) {
536 parsed->image_ += GetAndroidRoot();
537 parsed->image_ += "/framework/boot.art";
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700538 }
jeffhaoc1160702011-10-27 15:48:45 -0700539 if (parsed->heap_growth_limit_ == 0) {
540 parsed->heap_growth_limit_ = parsed->heap_maximum_size_;
541 }
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700542
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700543 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700544}
545
546Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700547 // TODO: acquire a static mutex on Runtime to avoid racing.
548 if (Runtime::instance_ != NULL) {
549 return NULL;
550 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700551 instance_ = new Runtime;
552 if (!instance_->Init(options, ignore_unrecognized)) {
553 delete instance_;
554 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700555 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700556 return instance_;
557}
Elliott Hughes0af55432011-08-17 18:37:28 -0700558
Ian Rogers365c1022012-06-22 15:05:28 -0700559static void CreateSystemClassLoader() {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800560 if (Runtime::Current()->UseCompileTimeClassPath()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700561 return;
562 }
563
Ian Rogers365c1022012-06-22 15:05:28 -0700564 ScopedJniThreadState ts(Thread::Current());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700565
Ian Rogers365c1022012-06-22 15:05:28 -0700566 Class* class_loader_class = ts.Decode<Class*>(WellKnownClasses::java_lang_ClassLoader);
567 CHECK(Runtime::Current()->GetClassLinker()->EnsureInitialized(class_loader_class, true, true));
Brian Carlstromaded5f72011-10-07 17:15:04 -0700568
Ian Rogers365c1022012-06-22 15:05:28 -0700569 Method* getSystemClassLoader = class_loader_class->FindDirectMethod("getSystemClassLoader", "()Ljava/lang/ClassLoader;");
Brian Carlstromdf143242011-10-10 18:05:34 -0700570 CHECK(getSystemClassLoader != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700571
Ian Rogers365c1022012-06-22 15:05:28 -0700572 ClassLoader* class_loader =
573 down_cast<ClassLoader*>(InvokeWithJValues(ts, NULL, getSystemClassLoader, NULL).GetL());
574 CHECK(class_loader != NULL);
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500575
Ian Rogers365c1022012-06-22 15:05:28 -0700576 ts.Self()->SetClassLoaderOverride(class_loader);
577
578 Class* thread_class = ts.Decode<Class*>(WellKnownClasses::java_lang_Thread);
579 CHECK(Runtime::Current()->GetClassLinker()->EnsureInitialized(thread_class, true, true));
580
581 Field* contextClassLoader = thread_class->FindDeclaredInstanceField("contextClassLoader",
582 "Ljava/lang/ClassLoader;");
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500583 CHECK(contextClassLoader != NULL);
Ian Rogers365c1022012-06-22 15:05:28 -0700584
585 contextClassLoader->SetObject(ts.Self()->GetPeer(), class_loader);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700586}
587
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700588void Runtime::Start() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800589 VLOG(startup) << "Runtime::Start entering";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700590
591 CHECK(host_prefix_.empty()) << host_prefix_;
592
Elliott Hughes225f5a12012-06-11 11:23:48 -0700593 // Relocate the OatFiles (ELF images).
Logan Chien0c717dd2012-03-28 18:31:07 +0800594 class_linker_->RelocateExecutable();
595
Elliott Hughes225f5a12012-06-11 11:23:48 -0700596 // Restore main thread state to kNative as expected by native code.
597 Thread* self = Thread::Current();
598 self->SetState(kNative);
599
600 // Pre-allocate an OutOfMemoryError for the double-OOME case.
601 self->ThrowNewException("Ljava/lang/OutOfMemoryError;",
602 "OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack available");
603 pre_allocated_OutOfMemoryError_ = self->GetException();
604 self->ClearException();
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700605
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700606 started_ = true;
607
Brian Carlstromae826982011-11-09 01:33:42 -0800608 // InitNativeMethods needs to be after started_ so that the classes
609 // it touches will have methods linked to the oat file if necessary.
610 InitNativeMethods();
611
Ian Rogers365c1022012-06-22 15:05:28 -0700612 // Initialize well known thread group values that may be accessed threads while attaching.
613 InitThreadGroups(self);
614
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500615 Thread::FinishStartup();
616
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700617 if (!is_zygote_) {
618 DidForkFromZygote();
619 }
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700620
Elliott Hughes85d15452011-09-16 17:33:01 -0700621 StartDaemonThreads();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700622
Brian Carlstromaded5f72011-10-07 17:15:04 -0700623 CreateSystemClassLoader();
624
Elliott Hughes225f5a12012-06-11 11:23:48 -0700625 self->GetJniEnv()->locals.AssertEmpty();
Elliott Hughes726079d2011-10-07 18:43:44 -0700626
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800627 VLOG(startup) << "Runtime::Start exiting";
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700628
629 finished_starting_ = true;
Elliott Hughes85d15452011-09-16 17:33:01 -0700630}
631
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700632void Runtime::DidForkFromZygote() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700633 is_zygote_ = false;
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700634
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700635 StartSignalCatcher();
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700636
637 // Start the JDWP thread. If the command-line debugger flags specified "suspend=y",
638 // this will pause the runtime, so we probably want this to come last.
639 Dbg::StartJdwp();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700640}
641
642void Runtime::StartSignalCatcher() {
643 if (!is_zygote_) {
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700644 signal_catcher_ = new SignalCatcher(stack_trace_file_);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700645 }
646}
647
Elliott Hughes85d15452011-09-16 17:33:01 -0700648void Runtime::StartDaemonThreads() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800649 VLOG(startup) << "Runtime::StartDaemonThreads entering";
Elliott Hughes85d15452011-09-16 17:33:01 -0700650
Elliott Hughes719b3232011-09-25 17:42:19 -0700651 Thread* self = Thread::Current();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700652
653 // Must be in the kNative state for calling native methods.
Elliott Hughes34e06962012-04-09 13:55:55 -0700654 CHECK_EQ(self->GetState(), kNative);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700655
Elliott Hughes719b3232011-09-25 17:42:19 -0700656 JNIEnv* env = self->GetJniEnv();
Elliott Hugheseac76672012-05-24 21:56:51 -0700657 env->CallStaticVoidMethod(WellKnownClasses::java_lang_Daemons, WellKnownClasses::java_lang_Daemons_start);
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800658 CHECK(!env->ExceptionCheck());
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700659
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800660 VLOG(startup) << "Runtime::StartDaemonThreads exiting";
Carl Shapiro61e019d2011-07-14 16:53:09 -0700661}
662
Elliott Hughes0af55432011-08-17 18:37:28 -0700663bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700664 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700665
Elliott Hughes90a33692011-08-30 13:27:07 -0700666 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
667 if (options.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700668 LOG(ERROR) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700669 return false;
670 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800671 VLOG(startup) << "Runtime::Init -verbose:startup enabled";
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700672
Elliott Hughes7c6169d2012-05-02 16:11:48 -0700673 QuasiAtomic::Startup();
674
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700675 SetJniGlobalsMax(options->jni_globals_max_);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800676 Monitor::Init(options->lock_profiling_threshold_, options->hook_is_sensitive_thread_);
Elliott Hughes32d6e1e2011-10-11 14:47:44 -0700677
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700678 host_prefix_ = options->host_prefix_;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800679 boot_class_path_string_ = options->boot_class_path_string_;
680 class_path_string_ = options->class_path_string_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700681 properties_ = options->properties_;
682
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800683 is_compiler_ = options->is_compiler_;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700684 is_zygote_ = options->is_zygote_;
Mathieu Chartier069387a2012-06-18 12:01:01 -0700685 is_concurrent_gc_enabled_ = options->is_concurrent_gc_enabled_;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700686
Elliott Hughes0af55432011-08-17 18:37:28 -0700687 vfprintf_ = options->hook_vfprintf_;
688 exit_ = options->hook_exit_;
689 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700690
Elliott Hughesbe759c62011-09-08 19:38:21 -0700691 default_stack_size_ = options->stack_size_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700692 stack_trace_file_ = options->stack_trace_file_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700693
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700694 monitor_list_ = new MonitorList;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800695 thread_list_ = new ThreadList;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700696 intern_table_ = new InternTable;
697
Elliott Hughes0a1038b2012-06-14 16:24:17 -0700698 verifier::MethodVerifier::Init();
Logan Chiendd361c92012-04-10 23:40:37 +0800699
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800700 heap_ = new Heap(options->heap_initial_size_,
701 options->heap_growth_limit_,
702 options->heap_maximum_size_,
703 options->image_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700704
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700705 BlockSignals();
Elliott Hughes457005c2012-04-16 13:54:25 -0700706 InitPlatformSignalHandlers();
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700707
Elliott Hughesa0957642011-09-02 14:27:33 -0700708 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700709
Elliott Hughesbe759c62011-09-08 19:38:21 -0700710 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700711
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700712 // ClassLinker needs an attached thread, but we can't fully attach a thread
Elliott Hughes462c9442012-03-23 18:47:50 -0700713 // without creating objects. We can't supply a thread group yet; it will be fixed later.
714 Thread::Attach("main", false, NULL);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700715
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700716 // Set us to runnable so tools using a runtime can allocate and GC by default
Elliott Hughes34e06962012-04-09 13:55:55 -0700717 Thread::Current()->SetState(kRunnable);
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700718
Ian Rogers141d6222012-04-05 12:23:06 -0700719 // Now we're attached, we can take the heap lock and validate the heap.
720 GetHeap()->EnableObjectValidation();
721
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800722 CHECK_GE(GetHeap()->GetSpaces().size(), 1U);
723 if (GetHeap()->GetSpaces()[0]->IsImageSpace()) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800724 class_linker_ = ClassLinker::CreateFromImage(intern_table_);
725 } else {
726 CHECK(options->boot_class_path_ != NULL);
727 CHECK_NE(options->boot_class_path_->size(), 0U);
728 class_linker_ = ClassLinker::CreateFromCompiler(*options->boot_class_path_, intern_table_);
729 }
730 CHECK(class_linker_ != NULL);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700731
jeffhaob5e81852012-03-12 11:15:45 -0700732 method_trace_ = options->method_trace_;
733 method_trace_file_ = options->method_trace_file_;
734 method_trace_file_size_ = options->method_trace_file_size_;
735
736 if (options->method_trace_) {
737 Trace::Start(options->method_trace_file_.c_str(), -1, options->method_trace_file_size_, 0, false);
738 }
739
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800740 VLOG(startup) << "Runtime::Init exiting";
Carl Shapiro1fb86202011-06-27 17:43:13 -0700741 return true;
742}
743
Elliott Hughes038a8062011-09-18 14:12:41 -0700744void Runtime::InitNativeMethods() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800745 VLOG(startup) << "Runtime::InitNativeMethods entering";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700746 Thread* self = Thread::Current();
747 JNIEnv* env = self->GetJniEnv();
748
Elliott Hughes418d20f2011-09-22 14:00:39 -0700749 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Elliott Hughes34e06962012-04-09 13:55:55 -0700750 CHECK_EQ(self->GetState(), kNative);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700751
Elliott Hughes418d20f2011-09-22 14:00:39 -0700752 // First set up JniConstants, which is used by both the runtime's built-in native
753 // methods and libcore.
Elliott Hughesfea966e2011-09-22 10:26:20 -0700754 JniConstants::init(env);
Elliott Hugheseac76672012-05-24 21:56:51 -0700755 WellKnownClasses::Init(env);
Elliott Hughesfea966e2011-09-22 10:26:20 -0700756
Elliott Hughes418d20f2011-09-22 14:00:39 -0700757 // Then set up the native methods provided by the runtime itself.
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700758 RegisterRuntimeNativeMethods(env);
759
Elliott Hughes418d20f2011-09-22 14:00:39 -0700760 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
761 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
762 // the library that implements System.loadLibrary!
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700763 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800764 VLOG(startup) << "Runtime::InitNativeMethods exiting";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700765}
766
Ian Rogers365c1022012-06-22 15:05:28 -0700767void Runtime::InitThreadGroups(Thread* self) {
768 JNIEnvExt* env = self->GetJniEnv();
769 ScopedJniEnvLocalRefState env_state(env);
770 main_thread_group_ =
771 env->NewGlobalRef(env->GetStaticObjectField(WellKnownClasses::java_lang_ThreadGroup,
772 WellKnownClasses::java_lang_ThreadGroup_mainThreadGroup));
Brian Carlstrom034f76b2012-08-01 15:51:58 -0700773 CHECK(main_thread_group_ != NULL || IsCompiler());
Ian Rogers365c1022012-06-22 15:05:28 -0700774 system_thread_group_ =
775 env->NewGlobalRef(env->GetStaticObjectField(WellKnownClasses::java_lang_ThreadGroup,
776 WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup));
Brian Carlstrom034f76b2012-08-01 15:51:58 -0700777 CHECK(system_thread_group_ != NULL || IsCompiler());
778}
779
780jobject Runtime::GetMainThreadGroup() const {
781 CHECK(main_thread_group_ != NULL || IsCompiler());
782 return main_thread_group_;
783}
784
785jobject Runtime::GetSystemThreadGroup() const {
786 CHECK(system_thread_group_ != NULL || IsCompiler());
787 return system_thread_group_;
Ian Rogers365c1022012-06-22 15:05:28 -0700788}
789
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700790void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
791#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Ian Rogers5167c972012-02-03 10:41:20 -0800792 // Register Throwable first so that registration of other native methods can throw exceptions
793 REGISTER(register_java_lang_Throwable);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700794 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700795 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700796 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700797 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700798 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700799 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700800 REGISTER(register_java_lang_Object);
801 REGISTER(register_java_lang_Runtime);
802 REGISTER(register_java_lang_String);
803 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700804 REGISTER(register_java_lang_Thread);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700805 REGISTER(register_java_lang_VMClassLoader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700806 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700807 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700808 REGISTER(register_java_lang_reflect_Field);
809 REGISTER(register_java_lang_reflect_Method);
Jesse Wilson95caa792011-10-12 18:14:17 -0400810 REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700811 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700812 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700813 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700814 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700815#undef REGISTER
816}
817
Elliott Hughesc967f782012-04-16 10:23:15 -0700818void Runtime::DumpForSigQuit(std::ostream& os) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700819 GetClassLinker()->DumpForSigQuit(os);
820 GetInternTable()->DumpForSigQuit(os);
Elliott Hughesae80b492012-04-24 10:43:17 -0700821 GetJavaVM()->DumpForSigQuit(os);
Elliott Hughesc967f782012-04-16 10:23:15 -0700822 GetHeap()->DumpForSigQuit(os);
Elliott Hughes42ee1422011-09-06 12:33:32 -0700823 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700824
Elliott Hughesc967f782012-04-16 10:23:15 -0700825 thread_list_->DumpForSigQuit(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700826}
827
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800828void Runtime::DumpLockHolders(std::ostream& os) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800829 pid_t heap_lock_owner = GetHeap()->GetLockOwner();
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800830 pid_t thread_list_lock_owner = GetThreadList()->GetLockOwner();
831 pid_t classes_lock_owner = GetClassLinker()->GetClassesLockOwner();
832 pid_t dex_lock_owner = GetClassLinker()->GetDexLockOwner();
833 if ((heap_lock_owner | thread_list_lock_owner | classes_lock_owner | dex_lock_owner) != 0) {
834 os << "Heap lock owner tid: " << heap_lock_owner << "\n"
835 << "ThreadList lock owner tid: " << thread_list_lock_owner << "\n"
836 << "ClassLinker classes lock owner tid: " << classes_lock_owner << "\n"
837 << "ClassLinker dex lock owner tid: " << dex_lock_owner << "\n";
838 }
839}
840
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700841void Runtime::SetStatsEnabled(bool new_state) {
842 if (new_state == true) {
843 GetStats()->Clear(~0);
844 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
845 Thread::Current()->GetStats()->Clear(~0);
846 }
847 stats_enabled_ = new_state;
848}
849
850void Runtime::ResetStats(int kinds) {
851 GetStats()->Clear(kinds & 0xffff);
852 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
853 Thread::Current()->GetStats()->Clear(kinds >> 16);
854}
855
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700856int32_t Runtime::GetStat(int kind) {
857 RuntimeStats* stats;
858 if (kind < (1<<16)) {
859 stats = GetStats();
860 } else {
861 stats = Thread::Current()->GetStats();
862 kind >>= 16;
863 }
864 switch (kind) {
865 case KIND_ALLOCATED_OBJECTS:
866 return stats->allocated_objects;
867 case KIND_ALLOCATED_BYTES:
868 return stats->allocated_bytes;
869 case KIND_FREED_OBJECTS:
870 return stats->freed_objects;
871 case KIND_FREED_BYTES:
872 return stats->freed_bytes;
873 case KIND_GC_INVOCATIONS:
874 return stats->gc_for_alloc_count;
875 case KIND_CLASS_INIT_COUNT:
876 return stats->class_init_count;
877 case KIND_CLASS_INIT_TIME:
878 // Convert ns to us, reduce to 32 bits.
Elliott Hughes398f64b2012-03-26 18:05:48 -0700879 return static_cast<int>(stats->class_init_time_ns / 1000);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700880 case KIND_EXT_ALLOCATED_OBJECTS:
881 case KIND_EXT_ALLOCATED_BYTES:
882 case KIND_EXT_FREED_OBJECTS:
883 case KIND_EXT_FREED_BYTES:
884 return 0; // backward compatibility
885 default:
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700886 LOG(FATAL) << "Unknown statistic " << kind;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700887 return -1; // unreachable
888 }
889}
890
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700891void Runtime::BlockSignals() {
Elliott Hughes457005c2012-04-16 13:54:25 -0700892 SignalSet signals;
893 signals.Add(SIGPIPE);
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700894 // SIGQUIT is used to dump the runtime's state (including stack traces).
Elliott Hughes457005c2012-04-16 13:54:25 -0700895 signals.Add(SIGQUIT);
Elliott Hughes08795042012-04-03 14:48:52 -0700896 // SIGUSR1 is used to initiate a GC.
Elliott Hughes457005c2012-04-16 13:54:25 -0700897 signals.Add(SIGUSR1);
898 signals.Block();
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700899}
900
Ian Rogers365c1022012-06-22 15:05:28 -0700901void Runtime::AttachCurrentThread(const char* thread_name, bool as_daemon, jobject thread_group) {
Elliott Hughes462c9442012-03-23 18:47:50 -0700902 Thread::Attach(thread_name, as_daemon, thread_group);
Elliott Hughes22869a92012-03-27 14:08:24 -0700903 if (thread_name == NULL) {
904 LOG(WARNING) << *Thread::Current() << " attached without supplying a name";
905 }
Carl Shapiro61e019d2011-07-14 16:53:09 -0700906}
907
Elliott Hughesd92bec42011-09-02 17:04:36 -0700908void Runtime::DetachCurrentThread() {
Brian Carlstrom4d571432012-05-16 00:21:41 -0700909 Thread* self = Thread::Current();
910 if (self == NULL) {
911 LOG(FATAL) << "attempting to detach thread that is not attached";
912 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700913 if (self->HasManagedStack()) {
Elliott Hughes22869a92012-03-27 14:08:24 -0700914 LOG(FATAL) << *Thread::Current() << " attempting to detach while still running code";
915 }
Elliott Hughes02b48d12011-09-07 17:15:51 -0700916 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700917}
918
Brian Carlstrome24fa612011-09-29 00:53:55 -0700919void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700920 Dbg::VisitRoots(visitor, arg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700921 class_linker_->VisitRoots(visitor, arg);
922 intern_table_->VisitRoots(visitor, arg);
923 java_vm_->VisitRoots(visitor, arg);
924 thread_list_->VisitRoots(visitor, arg);
Elliott Hughes225f5a12012-06-11 11:23:48 -0700925 if (pre_allocated_OutOfMemoryError_ != NULL) {
926 visitor(pre_allocated_OutOfMemoryError_, arg);
927 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700928 visitor(jni_stub_array_, arg);
929 visitor(abstract_method_error_stub_array_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700930 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
931 visitor(resolution_stub_array_[i], arg);
932 }
Ian Rogers19846512012-02-24 11:42:47 -0800933 visitor(resolution_method_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700934 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
Elliott Hughes225f5a12012-06-11 11:23:48 -0700935 visitor(callee_save_methods_[i], arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700936 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700937}
938
Ian Rogers169c9a72011-11-13 20:13:17 -0800939void Runtime::SetJniDlsymLookupStub(ByteArray* jni_stub_array) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700940 CHECK(jni_stub_array != NULL) << " jni_stub_array=" << jni_stub_array;
941 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array)
942 << "jni_stub_array_=" << jni_stub_array_ << " jni_stub_array=" << jni_stub_array;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700943 jni_stub_array_ = jni_stub_array;
944}
945
Brian Carlstrome24fa612011-09-29 00:53:55 -0700946void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) {
947 CHECK(abstract_method_error_stub_array != NULL);
948 CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array);
949 abstract_method_error_stub_array_ = abstract_method_error_stub_array;
950}
951
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700952void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700953 CHECK(resolution_stub_array != NULL);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700954 CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array);
955 resolution_stub_array_[type] = resolution_stub_array;
Ian Rogersad25ac52011-10-04 19:13:33 -0700956}
957
Ian Rogers19846512012-02-24 11:42:47 -0800958Method* Runtime::CreateResolutionMethod() {
959 Class* method_class = Method::GetMethodClass();
960 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
961 method->SetDeclaringClass(method_class);
962 // TODO: use a special method for resolution method saves
963 method->SetDexMethodIndex(DexFile::kDexNoIndex16);
964 ByteArray* unknown_resolution_stub = GetResolutionStubArray(kUnknownMethod);
965 CHECK(unknown_resolution_stub != NULL);
966 method->SetCode(unknown_resolution_stub->GetData());
967 return method.get();
968}
969
Brian Carlstromae826982011-11-09 01:33:42 -0800970Method* Runtime::CreateCalleeSaveMethod(InstructionSet instruction_set, CalleeSaveType type) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700971 Class* method_class = Method::GetMethodClass();
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700972 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
Ian Rogersff1ed472011-09-20 13:46:24 -0700973 method->SetDeclaringClass(method_class);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800974 // TODO: use a special method for callee saves
Ian Rogers19846512012-02-24 11:42:47 -0800975 method->SetDexMethodIndex(DexFile::kDexNoIndex16);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700976 method->SetCode(NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800977 if ((instruction_set == kThumb2) || (instruction_set == kArm)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700978 uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6) | (1 << art::arm::R7) |
979 (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11);
980 uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3);
981 uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9);
982 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills :0) |
983 (type == kSaveAll ? all_spills :0) | (1 << art::arm::LR);
984 uint32_t fp_all_spills = (1 << art::arm::S0) | (1 << art::arm::S1) | (1 << art::arm::S2) |
985 (1 << art::arm::S3) | (1 << art::arm::S4) | (1 << art::arm::S5) |
986 (1 << art::arm::S6) | (1 << art::arm::S7) | (1 << art::arm::S8) |
987 (1 << art::arm::S9) | (1 << art::arm::S10) | (1 << art::arm::S11) |
988 (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) |
989 (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) |
990 (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) |
991 (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) |
992 (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) |
993 (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
994 (1 << art::arm::S30) | (1 << art::arm::S31);
995 uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
996 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
997 __builtin_popcount(fp_spills) /* fprs */ +
998 1 /* Method* */) * kPointerSize, kStackAlignment);
Ian Rogers15fdb8c2011-09-25 15:45:07 -0700999 method->SetFrameSizeInBytes(frame_size);
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001000 method->SetCoreSpillMask(core_spills);
1001 method->SetFpSpillMask(fp_spills);
Brian Carlstromae826982011-11-09 01:33:42 -08001002 } else if (instruction_set == kX86) {
Ian Rogers7caad772012-03-30 01:07:54 -07001003 uint32_t ref_spills = (1 << art::x86::EBP) | (1 << art::x86::ESI) | (1 << art::x86::EDI);
1004 uint32_t arg_spills = (1 << art::x86::ECX) | (1 << art::x86::EDX) | (1 << art::x86::EBX);
1005 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills : 0) |
1006 (1 << art::x86::kNumberOfCpuRegisters); // fake return address callee save
1007 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
1008 1 /* Method* */) * kPointerSize, kStackAlignment);
1009 method->SetFrameSizeInBytes(frame_size);
1010 method->SetCoreSpillMask(core_spills);
Ian Rogersff1ed472011-09-20 13:46:24 -07001011 method->SetFpSpillMask(0);
1012 } else {
1013 UNIMPLEMENTED(FATAL);
1014 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001015 return method.get();
Ian Rogersff1ed472011-09-20 13:46:24 -07001016}
1017
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001018void Runtime::SetCalleeSaveMethod(Method* method, CalleeSaveType type) {
1019 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
Elliott Hughes225f5a12012-06-11 11:23:48 -07001020 callee_save_methods_[type] = method;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001021}
1022
jeffhao2692b572011-12-16 15:42:28 -08001023void Runtime::EnableMethodTracing(Trace* tracer) {
1024 CHECK(!IsMethodTracingActive());
1025 tracer_ = tracer;
1026}
1027
1028void Runtime::DisableMethodTracing() {
1029 CHECK(IsMethodTracingActive());
1030 delete tracer_;
1031 tracer_ = NULL;
1032}
1033
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001034const std::vector<const DexFile*>& Runtime::GetCompileTimeClassPath(const ClassLoader* class_loader) {
1035 if (class_loader == NULL) {
1036 return GetClassLinker()->GetBootClassPath();
1037 }
1038 CHECK(UseCompileTimeClassPath());
1039 CompileTimeClassPaths::const_iterator it = compile_time_class_paths_.find(class_loader);
1040 CHECK(it != compile_time_class_paths_.end());
1041 return it->second;
1042}
1043
1044void Runtime::SetCompileTimeClassPath(const ClassLoader* class_loader, std::vector<const DexFile*>& class_path) {
1045 CHECK(!IsStarted());
1046 use_compile_time_class_path_ = true;
Elliott Hughesa0e18062012-04-13 15:59:59 -07001047 compile_time_class_paths_.Put(class_loader, class_path);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001048}
1049
Carl Shapiro1fb86202011-06-27 17:43:13 -07001050} // namespace art