blob: d10c351a45da1d4cf952cb7af09df8d2c0709524 [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"
Ian Rogers1f539342012-10-03 21:09:42 -070043#include "sirt_ref.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070044#include "space.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070045#include "thread.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070046#include "thread_list.h"
jeffhaob5e81852012-03-12 11:15:45 -070047#include "trace.h"
Elliott Hughes3bb81562011-10-21 18:52:59 -070048#include "UniquePtr.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070049#include "verifier/method_verifier.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070050#include "well_known_classes.h"
Carl Shapiro61e019d2011-07-14 16:53:09 -070051
Logan Chien971bf3f2012-05-01 15:47:55 +080052#if defined(ART_USE_LLVM_COMPILER)
53#include "compiler_llvm/procedure_linkage_table.h"
54#endif
55
Elliott Hugheseac76672012-05-24 21:56:51 -070056#include "JniConstants.h" // Last to avoid LOG redefinition in ics-mr1-plus-art.
Elliott Hughes90a33692011-08-30 13:27:07 -070057
Carl Shapiro1fb86202011-06-27 17:43:13 -070058namespace art {
59
Carl Shapiro2ed144c2011-07-26 16:52:08 -070060Runtime* Runtime::instance_ = NULL;
61
Elliott Hughesdcc24742011-09-07 14:02:44 -070062Runtime::Runtime()
Elliott Hughesd9c67be2012-02-02 19:54:06 -080063 : is_compiler_(false),
64 is_zygote_(false),
Mathieu Chartier069387a2012-06-18 12:01:01 -070065 is_concurrent_gc_enabled_(true),
Elliott Hughes6cf23882012-06-15 15:42:07 -070066 default_stack_size_(0),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080067 heap_(NULL),
Elliott Hughesc33a32b2011-10-11 18:18:07 -070068 monitor_list_(NULL),
Elliott Hughesdcc24742011-09-07 14:02:44 -070069 thread_list_(NULL),
70 intern_table_(NULL),
71 class_linker_(NULL),
72 signal_catcher_(NULL),
73 java_vm_(NULL),
Elliott Hughes225f5a12012-06-11 11:23:48 -070074 pre_allocated_OutOfMemoryError_(NULL),
Brian Carlstrom16192862011-09-12 17:50:06 -070075 jni_stub_array_(NULL),
Brian Carlstrome24fa612011-09-29 00:53:55 -070076 abstract_method_error_stub_array_(NULL),
Ian Rogers19846512012-02-24 11:42:47 -080077 resolution_method_(NULL),
Ian Rogers7c3757f2012-02-15 17:18:53 -080078 system_class_loader_(NULL),
Ian Rogers120f1c72012-09-28 17:17:10 -070079 threads_being_born_(0),
80 shutdown_cond_(new ConditionVariable("Runtime shutdown")),
Elliott Hughes6b355752012-01-13 16:49:08 -080081 shutting_down_(false),
Ian Rogers120f1c72012-09-28 17:17:10 -070082 shutting_down_started_(false),
Elliott Hughesdcc24742011-09-07 14:02:44 -070083 started_(false),
Mathieu Chartier7664f5c2012-06-08 18:15:32 -070084 finished_starting_(false),
Elliott Hughesdcc24742011-09-07 14:02:44 -070085 vfprintf_(NULL),
86 exit_(NULL),
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070087 abort_(NULL),
jeffhao2692b572011-12-16 15:42:28 -080088 stats_enabled_(false),
Ian Rogers776ac1f2012-04-13 23:36:36 -070089 method_trace_(0),
90 method_trace_file_size_(0),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080091 tracer_(NULL),
Ian Rogers365c1022012-06-22 15:05:28 -070092 use_compile_time_class_path_(false),
93 main_thread_group_(NULL),
Logan Chien971bf3f2012-05-01 15:47:55 +080094 system_thread_group_(NULL)
95#if defined(ART_USE_LLVM_COMPILER)
96#if defined(__arm__)
97 , plt_(kArm)
98#elif defined(__mips__)
99 , plt_(kMips)
100#elif defined(__i386__)
101 , plt_(kX86)
102#endif
103#endif
104 {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700105 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
106 resolution_stub_array_[i] = NULL;
107 }
108 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
Elliott Hughes225f5a12012-06-11 11:23:48 -0700109 callee_save_methods_[i] = NULL;
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700110 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800111
112#if defined(ART_USE_LLVM_COMPILER)
113 CHECK(plt_.AllocateTable()) << "Failed to allocate PLT";
114#endif
Elliott Hughesdcc24742011-09-07 14:02:44 -0700115}
116
Carl Shapiro61e019d2011-07-14 16:53:09 -0700117Runtime::~Runtime() {
Ian Rogers120f1c72012-09-28 17:17:10 -0700118 Thread* self = Thread::Current();
119 {
120 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
121 shutting_down_started_ = true;
122 while (threads_being_born_ > 0) {
123 shutdown_cond_->Wait(self, *Locks::runtime_shutdown_lock_);
124 }
125 shutting_down_ = true;
126 }
Elliott Hughes6b355752012-01-13 16:49:08 -0800127
jeffhaob5e81852012-03-12 11:15:45 -0700128 if (IsMethodTracingActive()) {
129 Trace::Shutdown();
130 }
131
Mathieu Chartiera6399032012-06-11 18:49:50 -0700132 // Make sure to let the GC complete if it is running.
Ian Rogers120f1c72012-09-28 17:17:10 -0700133 heap_->WaitForConcurrentGcToComplete(self);
Mathieu Chartiera6399032012-06-11 18:49:50 -0700134
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700135 // Make sure our internal threads are dead before we start tearing down things they're using.
Elliott Hughese52e49b2012-04-02 16:05:44 -0700136 Dbg::StopJdwp();
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700137 delete signal_catcher_;
138
Elliott Hughes038a8062011-09-18 14:12:41 -0700139 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
Elliott Hughes93e74e82011-09-13 11:07:03 -0700140 delete thread_list_;
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700141 delete monitor_list_;
Elliott Hughes93e74e82011-09-13 11:07:03 -0700142
Carl Shapiro61e019d2011-07-14 16:53:09 -0700143 delete class_linker_;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800144 delete heap_;
Elliott Hughes0a1038b2012-06-14 16:24:17 -0700145 verifier::MethodVerifier::Shutdown();
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700146 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700147 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700148 Thread::Shutdown();
Elliott Hughes7c6169d2012-05-02 16:11:48 -0700149 QuasiAtomic::Shutdown();
150
Carl Shapiro4acf4642011-07-26 18:54:13 -0700151 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700152 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -0700153 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700154}
155
Elliott Hughescac6cc72011-11-03 20:31:21 -0700156static bool gAborting = false;
157
Elliott Hughese0918552011-10-28 17:18:29 -0700158struct AbortState {
159 void Dump(std::ostream& os) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700160 if (gAborting) {
161 os << "Runtime aborting --- recursively, so no thread-specific detail!\n";
162 return;
163 }
164 gAborting = true;
Elliott Hughese0918552011-10-28 17:18:29 -0700165 os << "Runtime aborting...\n";
Elliott Hughes6c7d2442012-02-01 18:40:47 -0800166 if (Runtime::Current() == NULL) {
167 os << "(Runtime does not yet exist!)\n";
168 return;
169 }
Elliott Hughese0918552011-10-28 17:18:29 -0700170 Thread* self = Thread::Current();
171 if (self == NULL) {
172 os << "(Aborting thread was not attached to runtime!)\n";
173 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700174 // TODO: we're aborting and the ScopedObjectAccess may attempt to acquire the mutator_lock_
175 // which may block indefinitely if there's a misbehaving thread holding it exclusively.
176 // The code below should be made robust to this.
177 ScopedObjectAccess soa(self);
Elliott Hughes899e7892012-01-24 14:57:32 -0800178 self->Dump(os);
179 if (self->IsExceptionPending()) {
180 os << "Pending " << PrettyTypeOf(self->GetException()) << " on thread:\n"
181 << self->GetException()->Dump();
182 }
Elliott Hughese0918552011-10-28 17:18:29 -0700183 }
184 }
185};
186
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700187void Runtime::Abort() {
Elliott Hughes131aef82012-01-27 11:53:35 -0800188 // Ensure that we don't have multiple threads trying to abort at once,
189 // which would result in significantly worse diagnostics.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700190 MutexLock mu(*Locks::abort_lock_);
Elliott Hughes131aef82012-01-27 11:53:35 -0800191
Elliott Hughesffe67362011-07-17 12:09:27 -0700192 // Get any pending output out of the way.
193 fflush(NULL);
194
195 // Many people have difficulty distinguish aborts from crashes,
196 // so be explicit.
Elliott Hughese0918552011-10-28 17:18:29 -0700197 AbortState state;
Elliott Hughes9ee5f9c2012-02-03 18:33:16 -0800198 LOG(INTERNAL_FATAL) << Dumpable<AbortState>(state);
Elliott Hughesffe67362011-07-17 12:09:27 -0700199
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700200 // Call the abort hook if we have one.
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700201 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700202 LOG(INTERNAL_FATAL) << "Calling abort hook...";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700203 Runtime::Current()->abort_();
204 // notreached
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700205 LOG(INTERNAL_FATAL) << "Unexpectedly returned from abort hook!";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700206 }
207
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700208#if defined(__BIONIC__)
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700209 // TODO: finish merging patches to fix abort(3) in bionic, then lose this!
210 // Bionic doesn't implement POSIX semantics for abort(3) in a multi-threaded
211 // process, so if we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -0700212 // receive SIGABRT. debuggerd dumps the stack trace of the main
213 // thread, whether or not that was the thread that failed. By
214 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -0700215 // fault in the current thread, and get a useful log from debuggerd.
Elliott Hughes81ff3182012-03-23 20:35:56 -0700216 // We can also trivially tell the difference between a crash and
Elliott Hughesffe67362011-07-17 12:09:27 -0700217 // a deliberate abort by looking at the fault address.
218 *reinterpret_cast<char*>(0xdeadd00d) = 38;
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700219#elif defined(__APPLE__)
220 // TODO: check that this actually gives good stack traces on the Mac!
221 pthread_kill(pthread_self(), SIGABRT);
222#else
223 // TODO: we ought to be able to use pthread_kill(3) here (or abort(3),
224 // which POSIX defines in terms of raise(3), which POSIX defines in terms
225 // of pthread_kill(3)). On Linux, though, libcorkscrew can't unwind through
226 // libpthread, which means the stacks we dump would be useless. Calling
227 // tgkill(2) directly avoids that.
228 syscall(__NR_tgkill, getpid(), GetTid(), SIGABRT);
229#endif
Elliott Hughesffe67362011-07-17 12:09:27 -0700230 // notreached
231}
232
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700233bool Runtime::PreZygoteFork() {
234 heap_->PreZygoteFork();
235 return true;
236}
237
Elliott Hughesbf86d042011-08-31 17:53:14 -0700238void Runtime::CallExitHook(jint status) {
239 if (exit_ != NULL) {
Elliott Hughes34e06962012-04-09 13:55:55 -0700240 ScopedThreadStateChange tsc(Thread::Current(), kNative);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700241 exit_(status);
242 LOG(WARNING) << "Exit hook returned instead of exiting!";
243 }
244}
245
Brian Carlstrom8a436592011-08-15 21:27:23 -0700246// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
247// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
248// [gG] gigabytes.
249//
250// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700251// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
252// of 1024.
253//
254// The spec says the -Xmx and -Xms options must be multiples of 1024. It
255// doesn't say anything about -Xss.
256//
257// Returns 0 (a useless size) if "s" is malformed or specifies a low or
258// non-evenly-divisible value.
259//
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800260size_t ParseMemoryOption(const char* s, size_t div) {
Brian Carlstrom8a436592011-08-15 21:27:23 -0700261 // strtoul accepts a leading [+-], which we don't want,
262 // so make sure our string starts with a decimal digit.
263 if (isdigit(*s)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700264 char* s2;
265 size_t val = strtoul(s, &s2, 10);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700266 if (s2 != s) {
267 // s2 should be pointing just after the number.
268 // If this is the end of the string, the user
269 // has specified a number of bytes. Otherwise,
270 // there should be exactly one more character
271 // that specifies a multiplier.
272 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700273 // The remainder of the string is either a single multiplier
274 // character, or nothing to indicate that the value is in
275 // bytes.
276 char c = *s2++;
277 if (*s2 == '\0') {
278 size_t mul;
279 if (c == '\0') {
280 mul = 1;
281 } else if (c == 'k' || c == 'K') {
282 mul = KB;
283 } else if (c == 'm' || c == 'M') {
284 mul = MB;
285 } else if (c == 'g' || c == 'G') {
286 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700287 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700288 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700289 return 0;
290 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700291
292 if (val <= std::numeric_limits<size_t>::max() / mul) {
293 val *= mul;
294 } else {
295 // Clamp to a multiple of 1024.
296 val = std::numeric_limits<size_t>::max() & ~(1024-1);
297 }
298 } else {
299 // There's more than one character after the numeric part.
300 return 0;
301 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700302 }
303 // The man page says that a -Xm value must be a multiple of 1024.
304 if (val % div == 0) {
305 return val;
306 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700307 }
308 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700309 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700310}
311
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800312size_t ParseIntegerOrDie(const std::string& s) {
313 std::string::size_type colon = s.find(':');
314 if (colon == std::string::npos) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700315 LOG(FATAL) << "Missing integer: " << s;
316 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800317 const char* begin = &s[colon + 1];
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700318 char* end;
319 size_t result = strtoul(begin, &end, 10);
320 if (begin == end || *end != '\0') {
321 LOG(FATAL) << "Failed to parse integer in: " << s;
322 }
323 return result;
324}
325
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700326Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700327 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800328 const char* boot_class_path_string = getenv("BOOTCLASSPATH");
329 if (boot_class_path_string != NULL) {
330 parsed->boot_class_path_string_ = boot_class_path_string;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700331 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800332 const char* class_path_string = getenv("CLASSPATH");
333 if (class_path_string != NULL) {
334 parsed->class_path_string_ = class_path_string;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700335 }
Elliott Hughes67d92002012-03-26 15:08:51 -0700336 // -Xcheck:jni is off by default for regular builds but on by default in debug builds.
337 parsed->check_jni_ = kIsDebugBuild;
Elliott Hughes85d15452011-09-16 17:33:01 -0700338
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700339 parsed->heap_initial_size_ = Heap::kInitialSize;
340 parsed->heap_maximum_size_ = Heap::kMaximumSize;
Elliott Hughes6cf23882012-06-15 15:42:07 -0700341 parsed->heap_growth_limit_ = 0; // 0 means no growth limit.
342 parsed->stack_size_ = 0; // 0 means default.
Brian Carlstromf734cf52011-08-17 16:28:14 -0700343
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800344 parsed->is_compiler_ = false;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700345 parsed->is_zygote_ = false;
Mathieu Chartier069387a2012-06-18 12:01:01 -0700346 parsed->is_concurrent_gc_enabled_ = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700347
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700348 parsed->jni_globals_max_ = 0;
Elliott Hughesfc861622011-10-17 17:57:47 -0700349 parsed->lock_profiling_threshold_ = 0;
350 parsed->hook_is_sensitive_thread_ = NULL;
351
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700352 parsed->hook_vfprintf_ = vfprintf;
353 parsed->hook_exit_ = exit;
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700354 parsed->hook_abort_ = NULL; // We don't call abort(3) by default; see Runtime::Abort.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700355
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800356// gLogVerbosity.class_linker = true; // TODO: don't check this in!
357// gLogVerbosity.compiler = true; // TODO: don't check this in!
358// gLogVerbosity.heap = true; // TODO: don't check this in!
359// gLogVerbosity.gc = true; // TODO: don't check this in!
360// gLogVerbosity.jdwp = true; // TODO: don't check this in!
361// gLogVerbosity.jni = true; // TODO: don't check this in!
362// gLogVerbosity.monitor = true; // TODO: don't check this in!
363// gLogVerbosity.startup = true; // TODO: don't check this in!
364// gLogVerbosity.third_party_jni = true; // TODO: don't check this in!
365// gLogVerbosity.threads = true; // TODO: don't check this in!
366
jeffhaob5e81852012-03-12 11:15:45 -0700367 parsed->method_trace_ = false;
368 parsed->method_trace_file_ = "/data/method-trace-file.bin";
369 parsed->method_trace_file_size_ = 10 * MB;
370
Brian Carlstrom8a436592011-08-15 21:27:23 -0700371 for (size_t i = 0; i < options.size(); ++i) {
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800372 const std::string option(options[i].first);
Brian Carlstrom0796af02011-10-12 14:31:45 -0700373 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700374 LOG(INFO) << "option[" << i << "]=" << option;
375 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800376 if (StartsWith(option, "-Xbootclasspath:")) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800377 parsed->boot_class_path_string_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700378 } else if (option == "-classpath" || option == "-cp") {
379 // TODO: support -Djava.class.path
380 i++;
381 if (i == options.size()) {
382 // TODO: usage
383 LOG(FATAL) << "Missing required class path value for " << option;
384 return NULL;
385 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800386 const StringPiece& value = options[i].first;
387 parsed->class_path_string_ = value.data();
388 } else if (option == "bootclasspath") {
389 parsed->boot_class_path_
390 = reinterpret_cast<const std::vector<const DexFile*>*>(options[i].second);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800391 } else if (StartsWith(option, "-Ximage:")) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800392 parsed->image_ = option.substr(strlen("-Ximage:")).data();
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800393 } else if (StartsWith(option, "-Xcheck:jni")) {
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700394 parsed->check_jni_ = true;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800395 } else if (StartsWith(option, "-Xrunjdwp:") || StartsWith(option, "-agentlib:jdwp=")) {
396 std::string tail(option.substr(option[1] == 'X' ? 10 : 15));
Elliott Hughes3bb81562011-10-21 18:52:59 -0700397 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
398 LOG(FATAL) << "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
399 << "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n";
400 return NULL;
401 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800402 } else if (StartsWith(option, "-Xms")) {
403 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).c_str(), 1024);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700404 if (size == 0) {
405 if (ignore_unrecognized) {
406 continue;
407 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700408 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700409 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700410 return NULL;
411 }
412 parsed->heap_initial_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800413 } else if (StartsWith(option, "-Xmx")) {
414 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).c_str(), 1024);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700415 if (size == 0) {
416 if (ignore_unrecognized) {
417 continue;
418 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700419 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700420 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700421 return NULL;
422 }
423 parsed->heap_maximum_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800424 } else if (StartsWith(option, "-XX:HeapGrowthLimit=")) {
425 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).c_str(), 1024);
jeffhaoc1160702011-10-27 15:48:45 -0700426 if (size == 0) {
427 if (ignore_unrecognized) {
428 continue;
429 }
430 // TODO: usage
431 LOG(FATAL) << "Failed to parse " << option;
432 return NULL;
433 }
434 parsed->heap_growth_limit_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800435 } else if (StartsWith(option, "-Xss")) {
436 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).c_str(), 1);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700437 if (size == 0) {
438 if (ignore_unrecognized) {
439 continue;
440 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700441 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700442 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700443 return NULL;
444 }
445 parsed->stack_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800446 } else if (StartsWith(option, "-D")) {
447 parsed->properties_.push_back(option.substr(strlen("-D")));
448 } else if (StartsWith(option, "-Xjnitrace:")) {
449 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:"));
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700450 } else if (option == "compiler") {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800451 parsed->is_compiler_ = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700452 } else if (option == "-Xzygote") {
453 parsed->is_zygote_ = true;
Mathieu Chartier069387a2012-06-18 12:01:01 -0700454 } else if (StartsWith(option, "-Xgc:")) {
455 std::vector<std::string> gc_options;
456 Split(option.substr(strlen("-Xgc:")), ',', gc_options);
457 for (size_t i = 0; i < gc_options.size(); ++i) {
458 if (gc_options[i] == "noconcurrent") {
459 parsed->is_concurrent_gc_enabled_ = false;
460 } else if (gc_options[i] == "concurrent") {
461 parsed->is_concurrent_gc_enabled_ = true;
462 } else {
463 LOG(WARNING) << "Ignoring unknown -Xgc option: " << gc_options[i];
464 }
465 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800466 } else if (StartsWith(option, "-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700467 std::vector<std::string> verbose_options;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800468 Split(option.substr(strlen("-verbose:")), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700469 for (size_t i = 0; i < verbose_options.size(); ++i) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800470 if (verbose_options[i] == "class") {
471 gLogVerbosity.class_linker = true;
472 } else if (verbose_options[i] == "compiler") {
473 gLogVerbosity.compiler = true;
474 } else if (verbose_options[i] == "heap") {
475 gLogVerbosity.heap = true;
476 } else if (verbose_options[i] == "gc") {
477 gLogVerbosity.gc = true;
478 } else if (verbose_options[i] == "jdwp") {
479 gLogVerbosity.jdwp = true;
480 } else if (verbose_options[i] == "jni") {
481 gLogVerbosity.jni = true;
482 } else if (verbose_options[i] == "monitor") {
483 gLogVerbosity.monitor = true;
484 } else if (verbose_options[i] == "startup") {
485 gLogVerbosity.startup = true;
486 } else if (verbose_options[i] == "third-party-jni") {
487 gLogVerbosity.third_party_jni = true;
488 } else if (verbose_options[i] == "threads") {
489 gLogVerbosity.threads = true;
490 } else {
491 LOG(WARNING) << "Ignoring unknown -verbose option: " << verbose_options[i];
492 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700493 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800494 } else if (StartsWith(option, "-Xjnigreflimit:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700495 parsed->jni_globals_max_ = ParseIntegerOrDie(option);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800496 } else if (StartsWith(option, "-Xlockprofthreshold:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700497 parsed->lock_profiling_threshold_ = ParseIntegerOrDie(option);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800498 } else if (StartsWith(option, "-Xstacktracefile:")) {
Elliott Hughes206c20d2012-06-28 11:05:37 -0700499 parsed->stack_trace_file_ = option.substr(strlen("-Xstacktracefile:"));
Elliott Hughesfc861622011-10-17 17:57:47 -0700500 } else if (option == "sensitiveThread") {
Ian Rogers1b09b092012-08-20 15:35:52 -0700501 parsed->hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(const_cast<void*>(options[i].second));
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700502 } else if (option == "vfprintf") {
Ian Rogers1b09b092012-08-20 15:35:52 -0700503 parsed->hook_vfprintf_ =
504 reinterpret_cast<int (*)(FILE *, const char*, va_list)>(const_cast<void*>(options[i].second));
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700505 } else if (option == "exit") {
Ian Rogers1b09b092012-08-20 15:35:52 -0700506 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(const_cast<void*>(options[i].second));
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700507 } else if (option == "abort") {
Ian Rogers1b09b092012-08-20 15:35:52 -0700508 parsed->hook_abort_ = reinterpret_cast<void(*)()>(const_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
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700546bool 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) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700549 return false;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700550 }
Ian Rogersb726dcb2012-09-05 08:57:23 -0700551 Locks::Init();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700552 instance_ = new Runtime;
553 if (!instance_->Init(options, ignore_unrecognized)) {
554 delete instance_;
555 instance_ = NULL;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700556 return false;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700557 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700558 return true;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700559}
Elliott Hughes0af55432011-08-17 18:37:28 -0700560
Ian Rogers365c1022012-06-22 15:05:28 -0700561static void CreateSystemClassLoader() {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800562 if (Runtime::Current()->UseCompileTimeClassPath()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700563 return;
564 }
565
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700566 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700567
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700568 Class* class_loader_class = soa.Decode<Class*>(WellKnownClasses::java_lang_ClassLoader);
Ian Rogers365c1022012-06-22 15:05:28 -0700569 CHECK(Runtime::Current()->GetClassLinker()->EnsureInitialized(class_loader_class, true, true));
Brian Carlstromaded5f72011-10-07 17:15:04 -0700570
Mathieu Chartier66f19252012-09-18 08:57:04 -0700571 AbstractMethod* getSystemClassLoader = class_loader_class->FindDirectMethod("getSystemClassLoader", "()Ljava/lang/ClassLoader;");
Brian Carlstromdf143242011-10-10 18:05:34 -0700572 CHECK(getSystemClassLoader != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700573
Ian Rogers365c1022012-06-22 15:05:28 -0700574 ClassLoader* class_loader =
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700575 down_cast<ClassLoader*>(InvokeWithJValues(soa, NULL, getSystemClassLoader, NULL).GetL());
Ian Rogers365c1022012-06-22 15:05:28 -0700576 CHECK(class_loader != NULL);
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500577
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700578 soa.Self()->SetClassLoaderOverride(class_loader);
Ian Rogers365c1022012-06-22 15:05:28 -0700579
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700580 Class* thread_class = soa.Decode<Class*>(WellKnownClasses::java_lang_Thread);
Ian Rogers365c1022012-06-22 15:05:28 -0700581 CHECK(Runtime::Current()->GetClassLinker()->EnsureInitialized(thread_class, true, true));
582
583 Field* contextClassLoader = thread_class->FindDeclaredInstanceField("contextClassLoader",
584 "Ljava/lang/ClassLoader;");
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500585 CHECK(contextClassLoader != NULL);
Ian Rogers365c1022012-06-22 15:05:28 -0700586
Mathieu Chartierdbe6f462012-09-25 16:54:50 -0700587 contextClassLoader->SetObject(soa.Decode<Object*>(soa.Self()->GetPeer()), class_loader);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700588}
589
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700590void Runtime::Start() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800591 VLOG(startup) << "Runtime::Start entering";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700592
593 CHECK(host_prefix_.empty()) << host_prefix_;
594
Elliott Hughes225f5a12012-06-11 11:23:48 -0700595 // Relocate the OatFiles (ELF images).
Logan Chien0c717dd2012-03-28 18:31:07 +0800596 class_linker_->RelocateExecutable();
597
Elliott Hughes225f5a12012-06-11 11:23:48 -0700598 // Pre-allocate an OutOfMemoryError for the double-OOME case.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700599 Thread* self = Thread::Current();
Elliott Hughes225f5a12012-06-11 11:23:48 -0700600 self->ThrowNewException("Ljava/lang/OutOfMemoryError;",
601 "OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack available");
602 pre_allocated_OutOfMemoryError_ = self->GetException();
603 self->ClearException();
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700604
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700605 // Restore main thread state to kNative as expected by native code.
606 self->TransitionFromRunnableToSuspended(kNative);
607
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700608 started_ = true;
609
Brian Carlstromae826982011-11-09 01:33:42 -0800610 // InitNativeMethods needs to be after started_ so that the classes
611 // it touches will have methods linked to the oat file if necessary.
612 InitNativeMethods();
613
Ian Rogers365c1022012-06-22 15:05:28 -0700614 // Initialize well known thread group values that may be accessed threads while attaching.
615 InitThreadGroups(self);
616
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500617 Thread::FinishStartup();
618
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700619 if (!is_zygote_) {
620 DidForkFromZygote();
621 }
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700622
Elliott Hughes85d15452011-09-16 17:33:01 -0700623 StartDaemonThreads();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700624
Brian Carlstromaded5f72011-10-07 17:15:04 -0700625 CreateSystemClassLoader();
626
Elliott Hughes225f5a12012-06-11 11:23:48 -0700627 self->GetJniEnv()->locals.AssertEmpty();
Elliott Hughes726079d2011-10-07 18:43:44 -0700628
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800629 VLOG(startup) << "Runtime::Start exiting";
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700630
631 finished_starting_ = true;
Elliott Hughes85d15452011-09-16 17:33:01 -0700632}
633
Ian Rogers120f1c72012-09-28 17:17:10 -0700634void Runtime::EndThreadBirth() EXCLUSIVE_LOCKS_REQUIRED(Locks::runtime_shutdown_lock_) {
635 DCHECK_GT(threads_being_born_, 0U);
636 threads_being_born_--;
637 if (shutting_down_started_ && threads_being_born_ == 0) {
638 shutdown_cond_->Broadcast();
639 }
640}
641
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700642void Runtime::DidForkFromZygote() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700643 is_zygote_ = false;
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700644
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700645 StartSignalCatcher();
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700646
647 // Start the JDWP thread. If the command-line debugger flags specified "suspend=y",
648 // this will pause the runtime, so we probably want this to come last.
649 Dbg::StartJdwp();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700650}
651
652void Runtime::StartSignalCatcher() {
653 if (!is_zygote_) {
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700654 signal_catcher_ = new SignalCatcher(stack_trace_file_);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700655 }
656}
657
Elliott Hughes85d15452011-09-16 17:33:01 -0700658void Runtime::StartDaemonThreads() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800659 VLOG(startup) << "Runtime::StartDaemonThreads entering";
Elliott Hughes85d15452011-09-16 17:33:01 -0700660
Elliott Hughes719b3232011-09-25 17:42:19 -0700661 Thread* self = Thread::Current();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700662
663 // Must be in the kNative state for calling native methods.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700664 {
Ian Rogersb726dcb2012-09-05 08:57:23 -0700665 MutexLock mu(*Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700666 CHECK_EQ(self->GetState(), kNative);
667 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700668
Elliott Hughes719b3232011-09-25 17:42:19 -0700669 JNIEnv* env = self->GetJniEnv();
Elliott Hugheseac76672012-05-24 21:56:51 -0700670 env->CallStaticVoidMethod(WellKnownClasses::java_lang_Daemons, WellKnownClasses::java_lang_Daemons_start);
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800671 CHECK(!env->ExceptionCheck());
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700672
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800673 VLOG(startup) << "Runtime::StartDaemonThreads exiting";
Carl Shapiro61e019d2011-07-14 16:53:09 -0700674}
675
Elliott Hughes0af55432011-08-17 18:37:28 -0700676bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700677 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700678
Elliott Hughes90a33692011-08-30 13:27:07 -0700679 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
680 if (options.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700681 LOG(ERROR) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700682 return false;
683 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800684 VLOG(startup) << "Runtime::Init -verbose:startup enabled";
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700685
Elliott Hughes7c6169d2012-05-02 16:11:48 -0700686 QuasiAtomic::Startup();
687
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700688 SetJniGlobalsMax(options->jni_globals_max_);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800689 Monitor::Init(options->lock_profiling_threshold_, options->hook_is_sensitive_thread_);
Elliott Hughes32d6e1e2011-10-11 14:47:44 -0700690
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700691 host_prefix_ = options->host_prefix_;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800692 boot_class_path_string_ = options->boot_class_path_string_;
693 class_path_string_ = options->class_path_string_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700694 properties_ = options->properties_;
695
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800696 is_compiler_ = options->is_compiler_;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700697 is_zygote_ = options->is_zygote_;
Mathieu Chartier069387a2012-06-18 12:01:01 -0700698 is_concurrent_gc_enabled_ = options->is_concurrent_gc_enabled_;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700699
Elliott Hughes0af55432011-08-17 18:37:28 -0700700 vfprintf_ = options->hook_vfprintf_;
701 exit_ = options->hook_exit_;
702 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700703
Elliott Hughesbe759c62011-09-08 19:38:21 -0700704 default_stack_size_ = options->stack_size_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700705 stack_trace_file_ = options->stack_trace_file_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700706
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700707 monitor_list_ = new MonitorList;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800708 thread_list_ = new ThreadList;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700709 intern_table_ = new InternTable;
710
Elliott Hughes0a1038b2012-06-14 16:24:17 -0700711 verifier::MethodVerifier::Init();
Logan Chiendd361c92012-04-10 23:40:37 +0800712
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800713 heap_ = new Heap(options->heap_initial_size_,
714 options->heap_growth_limit_,
715 options->heap_maximum_size_,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700716 options->image_,
717 options->is_concurrent_gc_enabled_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700718
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700719 BlockSignals();
Elliott Hughes457005c2012-04-16 13:54:25 -0700720 InitPlatformSignalHandlers();
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700721
Elliott Hughesa0957642011-09-02 14:27:33 -0700722 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700723
Elliott Hughesbe759c62011-09-08 19:38:21 -0700724 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700725
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700726 // ClassLinker needs an attached thread, but we can't fully attach a thread
Elliott Hughes462c9442012-03-23 18:47:50 -0700727 // without creating objects. We can't supply a thread group yet; it will be fixed later.
Ian Rogers120f1c72012-09-28 17:17:10 -0700728 Thread* self = Thread::Attach("main", false, NULL);
729 CHECK(self != NULL);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700730
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700731 // Set us to runnable so tools using a runtime can allocate and GC by default
Ian Rogers120f1c72012-09-28 17:17:10 -0700732 self->TransitionFromSuspendedToRunnable();
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700733
Ian Rogers141d6222012-04-05 12:23:06 -0700734 // Now we're attached, we can take the heap lock and validate the heap.
735 GetHeap()->EnableObjectValidation();
736
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800737 CHECK_GE(GetHeap()->GetSpaces().size(), 1U);
738 if (GetHeap()->GetSpaces()[0]->IsImageSpace()) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800739 class_linker_ = ClassLinker::CreateFromImage(intern_table_);
740 } else {
741 CHECK(options->boot_class_path_ != NULL);
742 CHECK_NE(options->boot_class_path_->size(), 0U);
743 class_linker_ = ClassLinker::CreateFromCompiler(*options->boot_class_path_, intern_table_);
744 }
745 CHECK(class_linker_ != NULL);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700746
jeffhaob5e81852012-03-12 11:15:45 -0700747 method_trace_ = options->method_trace_;
748 method_trace_file_ = options->method_trace_file_;
749 method_trace_file_size_ = options->method_trace_file_size_;
750
751 if (options->method_trace_) {
752 Trace::Start(options->method_trace_file_.c_str(), -1, options->method_trace_file_size_, 0, false);
753 }
754
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800755 VLOG(startup) << "Runtime::Init exiting";
Carl Shapiro1fb86202011-06-27 17:43:13 -0700756 return true;
757}
758
Elliott Hughes038a8062011-09-18 14:12:41 -0700759void Runtime::InitNativeMethods() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800760 VLOG(startup) << "Runtime::InitNativeMethods entering";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700761 Thread* self = Thread::Current();
762 JNIEnv* env = self->GetJniEnv();
763
Elliott Hughes418d20f2011-09-22 14:00:39 -0700764 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700765 {
Ian Rogersb726dcb2012-09-05 08:57:23 -0700766 MutexLock mu(*Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700767 CHECK_EQ(self->GetState(), kNative);
768 }
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700769
Elliott Hughes418d20f2011-09-22 14:00:39 -0700770 // First set up JniConstants, which is used by both the runtime's built-in native
771 // methods and libcore.
Elliott Hughesfea966e2011-09-22 10:26:20 -0700772 JniConstants::init(env);
Elliott Hugheseac76672012-05-24 21:56:51 -0700773 WellKnownClasses::Init(env);
Elliott Hughesfea966e2011-09-22 10:26:20 -0700774
Elliott Hughes418d20f2011-09-22 14:00:39 -0700775 // Then set up the native methods provided by the runtime itself.
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700776 RegisterRuntimeNativeMethods(env);
777
Elliott Hughes418d20f2011-09-22 14:00:39 -0700778 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
779 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
780 // the library that implements System.loadLibrary!
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700781 {
782 std::string mapped_name(StringPrintf(OS_SHARED_LIB_FORMAT_STR, "javacore"));
783 std::string reason;
784 self->TransitionFromSuspendedToRunnable();
785 if (!instance_->java_vm_->LoadNativeLibrary(mapped_name, NULL, reason)) {
786 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": " << reason;
787 }
788 self->TransitionFromRunnableToSuspended(kNative);
789 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800790 VLOG(startup) << "Runtime::InitNativeMethods exiting";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700791}
792
Ian Rogers365c1022012-06-22 15:05:28 -0700793void Runtime::InitThreadGroups(Thread* self) {
794 JNIEnvExt* env = self->GetJniEnv();
795 ScopedJniEnvLocalRefState env_state(env);
796 main_thread_group_ =
797 env->NewGlobalRef(env->GetStaticObjectField(WellKnownClasses::java_lang_ThreadGroup,
798 WellKnownClasses::java_lang_ThreadGroup_mainThreadGroup));
Brian Carlstrom034f76b2012-08-01 15:51:58 -0700799 CHECK(main_thread_group_ != NULL || IsCompiler());
Ian Rogers365c1022012-06-22 15:05:28 -0700800 system_thread_group_ =
801 env->NewGlobalRef(env->GetStaticObjectField(WellKnownClasses::java_lang_ThreadGroup,
802 WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup));
Brian Carlstrom034f76b2012-08-01 15:51:58 -0700803 CHECK(system_thread_group_ != NULL || IsCompiler());
804}
805
806jobject Runtime::GetMainThreadGroup() const {
807 CHECK(main_thread_group_ != NULL || IsCompiler());
808 return main_thread_group_;
809}
810
811jobject Runtime::GetSystemThreadGroup() const {
812 CHECK(system_thread_group_ != NULL || IsCompiler());
813 return system_thread_group_;
Ian Rogers365c1022012-06-22 15:05:28 -0700814}
815
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700816void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
817#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Ian Rogers5167c972012-02-03 10:41:20 -0800818 // Register Throwable first so that registration of other native methods can throw exceptions
819 REGISTER(register_java_lang_Throwable);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700820 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700821 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700822 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700823 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700824 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700825 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700826 REGISTER(register_java_lang_Object);
827 REGISTER(register_java_lang_Runtime);
828 REGISTER(register_java_lang_String);
829 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700830 REGISTER(register_java_lang_Thread);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700831 REGISTER(register_java_lang_VMClassLoader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700832 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700833 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700834 REGISTER(register_java_lang_reflect_Field);
835 REGISTER(register_java_lang_reflect_Method);
Jesse Wilson95caa792011-10-12 18:14:17 -0400836 REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700837 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700838 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700839 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700840 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700841#undef REGISTER
842}
843
Elliott Hughesc967f782012-04-16 10:23:15 -0700844void Runtime::DumpForSigQuit(std::ostream& os) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700845 GetClassLinker()->DumpForSigQuit(os);
846 GetInternTable()->DumpForSigQuit(os);
Elliott Hughesae80b492012-04-24 10:43:17 -0700847 GetJavaVM()->DumpForSigQuit(os);
Elliott Hughesc967f782012-04-16 10:23:15 -0700848 GetHeap()->DumpForSigQuit(os);
Elliott Hughes42ee1422011-09-06 12:33:32 -0700849 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700850
Elliott Hughesc967f782012-04-16 10:23:15 -0700851 thread_list_->DumpForSigQuit(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700852}
853
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800854void Runtime::DumpLockHolders(std::ostream& os) {
Ian Rogersb726dcb2012-09-05 08:57:23 -0700855 uint64_t mutator_lock_owner = Locks::mutator_lock_->GetExclusiveOwnerTid();
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800856 pid_t thread_list_lock_owner = GetThreadList()->GetLockOwner();
857 pid_t classes_lock_owner = GetClassLinker()->GetClassesLockOwner();
858 pid_t dex_lock_owner = GetClassLinker()->GetDexLockOwner();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700859 if ((thread_list_lock_owner | classes_lock_owner | dex_lock_owner) != 0) {
860 os << "Mutator lock exclusive owner tid: " << mutator_lock_owner << "\n"
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800861 << "ThreadList lock owner tid: " << thread_list_lock_owner << "\n"
862 << "ClassLinker classes lock owner tid: " << classes_lock_owner << "\n"
863 << "ClassLinker dex lock owner tid: " << dex_lock_owner << "\n";
864 }
865}
866
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700867void Runtime::SetStatsEnabled(bool new_state) {
868 if (new_state == true) {
869 GetStats()->Clear(~0);
870 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
871 Thread::Current()->GetStats()->Clear(~0);
872 }
873 stats_enabled_ = new_state;
874}
875
876void Runtime::ResetStats(int kinds) {
877 GetStats()->Clear(kinds & 0xffff);
878 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
879 Thread::Current()->GetStats()->Clear(kinds >> 16);
880}
881
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700882int32_t Runtime::GetStat(int kind) {
883 RuntimeStats* stats;
884 if (kind < (1<<16)) {
885 stats = GetStats();
886 } else {
887 stats = Thread::Current()->GetStats();
888 kind >>= 16;
889 }
890 switch (kind) {
891 case KIND_ALLOCATED_OBJECTS:
892 return stats->allocated_objects;
893 case KIND_ALLOCATED_BYTES:
894 return stats->allocated_bytes;
895 case KIND_FREED_OBJECTS:
896 return stats->freed_objects;
897 case KIND_FREED_BYTES:
898 return stats->freed_bytes;
899 case KIND_GC_INVOCATIONS:
900 return stats->gc_for_alloc_count;
901 case KIND_CLASS_INIT_COUNT:
902 return stats->class_init_count;
903 case KIND_CLASS_INIT_TIME:
904 // Convert ns to us, reduce to 32 bits.
Elliott Hughes398f64b2012-03-26 18:05:48 -0700905 return static_cast<int>(stats->class_init_time_ns / 1000);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700906 case KIND_EXT_ALLOCATED_OBJECTS:
907 case KIND_EXT_ALLOCATED_BYTES:
908 case KIND_EXT_FREED_OBJECTS:
909 case KIND_EXT_FREED_BYTES:
910 return 0; // backward compatibility
911 default:
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700912 LOG(FATAL) << "Unknown statistic " << kind;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700913 return -1; // unreachable
914 }
915}
916
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700917void Runtime::BlockSignals() {
Elliott Hughes457005c2012-04-16 13:54:25 -0700918 SignalSet signals;
919 signals.Add(SIGPIPE);
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700920 // SIGQUIT is used to dump the runtime's state (including stack traces).
Elliott Hughes457005c2012-04-16 13:54:25 -0700921 signals.Add(SIGQUIT);
Elliott Hughes08795042012-04-03 14:48:52 -0700922 // SIGUSR1 is used to initiate a GC.
Elliott Hughes457005c2012-04-16 13:54:25 -0700923 signals.Add(SIGUSR1);
924 signals.Block();
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700925}
926
Ian Rogers120f1c72012-09-28 17:17:10 -0700927bool Runtime::AttachCurrentThread(const char* thread_name, bool as_daemon, jobject thread_group) {
928 bool success = Thread::Attach(thread_name, as_daemon, thread_group) != NULL;
Elliott Hughes22869a92012-03-27 14:08:24 -0700929 if (thread_name == NULL) {
930 LOG(WARNING) << *Thread::Current() << " attached without supplying a name";
931 }
Ian Rogers120f1c72012-09-28 17:17:10 -0700932 return success;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700933}
934
Elliott Hughesd92bec42011-09-02 17:04:36 -0700935void Runtime::DetachCurrentThread() {
Brian Carlstrom4d571432012-05-16 00:21:41 -0700936 Thread* self = Thread::Current();
937 if (self == NULL) {
938 LOG(FATAL) << "attempting to detach thread that is not attached";
939 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700940 if (self->HasManagedStack()) {
Elliott Hughes22869a92012-03-27 14:08:24 -0700941 LOG(FATAL) << *Thread::Current() << " attempting to detach while still running code";
942 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700943 thread_list_->Unregister(self);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700944}
945
Brian Carlstrome24fa612011-09-29 00:53:55 -0700946void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700947 Dbg::VisitRoots(visitor, arg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700948 class_linker_->VisitRoots(visitor, arg);
949 intern_table_->VisitRoots(visitor, arg);
950 java_vm_->VisitRoots(visitor, arg);
951 thread_list_->VisitRoots(visitor, arg);
Elliott Hughes225f5a12012-06-11 11:23:48 -0700952 if (pre_allocated_OutOfMemoryError_ != NULL) {
953 visitor(pre_allocated_OutOfMemoryError_, arg);
954 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700955 visitor(jni_stub_array_, arg);
956 visitor(abstract_method_error_stub_array_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700957 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
958 visitor(resolution_stub_array_[i], arg);
959 }
Ian Rogers19846512012-02-24 11:42:47 -0800960 visitor(resolution_method_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700961 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
Elliott Hughes225f5a12012-06-11 11:23:48 -0700962 visitor(callee_save_methods_[i], arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700963 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700964}
965
Ian Rogers169c9a72011-11-13 20:13:17 -0800966void Runtime::SetJniDlsymLookupStub(ByteArray* jni_stub_array) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700967 CHECK(jni_stub_array != NULL) << " jni_stub_array=" << jni_stub_array;
968 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array)
969 << "jni_stub_array_=" << jni_stub_array_ << " jni_stub_array=" << jni_stub_array;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700970 jni_stub_array_ = jni_stub_array;
971}
972
Brian Carlstrome24fa612011-09-29 00:53:55 -0700973void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) {
974 CHECK(abstract_method_error_stub_array != NULL);
975 CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array);
976 abstract_method_error_stub_array_ = abstract_method_error_stub_array;
977}
978
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700979void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700980 CHECK(resolution_stub_array != NULL);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700981 CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array);
982 resolution_stub_array_[type] = resolution_stub_array;
Ian Rogersad25ac52011-10-04 19:13:33 -0700983}
984
Mathieu Chartier66f19252012-09-18 08:57:04 -0700985AbstractMethod* Runtime::CreateResolutionMethod() {
986 Class* method_class = AbstractMethod::GetMethodClass();
Ian Rogers1f539342012-10-03 21:09:42 -0700987 SirtRef<AbstractMethod> method(Thread::Current(),
988 down_cast<AbstractMethod*>(method_class->AllocObject()));
Ian Rogers19846512012-02-24 11:42:47 -0800989 method->SetDeclaringClass(method_class);
990 // TODO: use a special method for resolution method saves
991 method->SetDexMethodIndex(DexFile::kDexNoIndex16);
992 ByteArray* unknown_resolution_stub = GetResolutionStubArray(kUnknownMethod);
993 CHECK(unknown_resolution_stub != NULL);
994 method->SetCode(unknown_resolution_stub->GetData());
995 return method.get();
996}
997
Ian Rogers1f539342012-10-03 21:09:42 -0700998AbstractMethod* Runtime::CreateCalleeSaveMethod(InstructionSet instruction_set,
999 CalleeSaveType type) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001000 Class* method_class = AbstractMethod::GetMethodClass();
Ian Rogers1f539342012-10-03 21:09:42 -07001001 SirtRef<AbstractMethod>
1002 method(Thread::Current(), down_cast<AbstractMethod*>(method_class->AllocObject()));
Ian Rogersff1ed472011-09-20 13:46:24 -07001003 method->SetDeclaringClass(method_class);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001004 // TODO: use a special method for callee saves
Ian Rogers19846512012-02-24 11:42:47 -08001005 method->SetDexMethodIndex(DexFile::kDexNoIndex16);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001006 method->SetCode(NULL);
Brian Carlstromae826982011-11-09 01:33:42 -08001007 if ((instruction_set == kThumb2) || (instruction_set == kArm)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001008 uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6) | (1 << art::arm::R7) |
1009 (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11);
1010 uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3);
1011 uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9);
1012 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills :0) |
1013 (type == kSaveAll ? all_spills :0) | (1 << art::arm::LR);
1014 uint32_t fp_all_spills = (1 << art::arm::S0) | (1 << art::arm::S1) | (1 << art::arm::S2) |
1015 (1 << art::arm::S3) | (1 << art::arm::S4) | (1 << art::arm::S5) |
1016 (1 << art::arm::S6) | (1 << art::arm::S7) | (1 << art::arm::S8) |
1017 (1 << art::arm::S9) | (1 << art::arm::S10) | (1 << art::arm::S11) |
1018 (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) |
1019 (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) |
1020 (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) |
1021 (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) |
1022 (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) |
1023 (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
1024 (1 << art::arm::S30) | (1 << art::arm::S31);
1025 uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
1026 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
1027 __builtin_popcount(fp_spills) /* fprs */ +
1028 1 /* Method* */) * kPointerSize, kStackAlignment);
Ian Rogers15fdb8c2011-09-25 15:45:07 -07001029 method->SetFrameSizeInBytes(frame_size);
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001030 method->SetCoreSpillMask(core_spills);
1031 method->SetFpSpillMask(fp_spills);
jeffhao7fbee072012-08-24 17:56:54 -07001032 } else if (instruction_set == kMips) {
1033 uint32_t ref_spills = (1 << art::mips::S2) | (1 << art::mips::S3) | (1 << art::mips::S4) |
1034 (1 << art::mips::S5) | (1 << art::mips::S6) | (1 << art::mips::S7) |
1035 (1 << art::mips::FP);
1036 uint32_t arg_spills = (1 << art::mips::A1) | (1 << art::mips::A2) | (1 << art::mips::A3);
jeffhao7fbee072012-08-24 17:56:54 -07001037 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills : 0) |
jeffhao07030602012-09-26 14:33:14 -07001038 (1 << art::mips::RA);
jeffhao7fbee072012-08-24 17:56:54 -07001039 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
jeffhao7fbee072012-08-24 17:56:54 -07001040 1 /* Method* */) * kPointerSize, kStackAlignment);
1041 method->SetFrameSizeInBytes(frame_size);
1042 method->SetCoreSpillMask(core_spills);
jeffhao07030602012-09-26 14:33:14 -07001043 method->SetFpSpillMask(0);
Brian Carlstromae826982011-11-09 01:33:42 -08001044 } else if (instruction_set == kX86) {
Ian Rogers7caad772012-03-30 01:07:54 -07001045 uint32_t ref_spills = (1 << art::x86::EBP) | (1 << art::x86::ESI) | (1 << art::x86::EDI);
1046 uint32_t arg_spills = (1 << art::x86::ECX) | (1 << art::x86::EDX) | (1 << art::x86::EBX);
1047 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills : 0) |
1048 (1 << art::x86::kNumberOfCpuRegisters); // fake return address callee save
1049 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
1050 1 /* Method* */) * kPointerSize, kStackAlignment);
1051 method->SetFrameSizeInBytes(frame_size);
1052 method->SetCoreSpillMask(core_spills);
Ian Rogersff1ed472011-09-20 13:46:24 -07001053 method->SetFpSpillMask(0);
1054 } else {
1055 UNIMPLEMENTED(FATAL);
1056 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001057 return method.get();
Ian Rogersff1ed472011-09-20 13:46:24 -07001058}
1059
Mathieu Chartier66f19252012-09-18 08:57:04 -07001060void Runtime::SetCalleeSaveMethod(AbstractMethod* method, CalleeSaveType type) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001061 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
Elliott Hughes225f5a12012-06-11 11:23:48 -07001062 callee_save_methods_[type] = method;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001063}
1064
jeffhao2692b572011-12-16 15:42:28 -08001065void Runtime::EnableMethodTracing(Trace* tracer) {
1066 CHECK(!IsMethodTracingActive());
1067 tracer_ = tracer;
1068}
1069
1070void Runtime::DisableMethodTracing() {
1071 CHECK(IsMethodTracingActive());
1072 delete tracer_;
1073 tracer_ = NULL;
1074}
1075
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001076const std::vector<const DexFile*>& Runtime::GetCompileTimeClassPath(jobject class_loader) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001077 if (class_loader == NULL) {
1078 return GetClassLinker()->GetBootClassPath();
1079 }
1080 CHECK(UseCompileTimeClassPath());
1081 CompileTimeClassPaths::const_iterator it = compile_time_class_paths_.find(class_loader);
1082 CHECK(it != compile_time_class_paths_.end());
1083 return it->second;
1084}
1085
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001086void Runtime::SetCompileTimeClassPath(jobject class_loader, std::vector<const DexFile*>& class_path) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001087 CHECK(!IsStarted());
1088 use_compile_time_class_path_ = true;
Elliott Hughesa0e18062012-04-13 15:59:59 -07001089 compile_time_class_paths_.Put(class_loader, class_path);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001090}
1091
Carl Shapiro1fb86202011-06-27 17:43:13 -07001092} // namespace art