blob: 3fa31237f647658903d115e946e5caf04f505994 [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"
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070044#include "gc/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),
Ian Rogersc604d732012-10-14 16:09:54 -070080 shutdown_cond_(new ConditionVariable("Runtime shutdown", *Locks::runtime_shutdown_lock_)),
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() {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700118 heap_->DeleteThreadPool();
119
Ian Rogers120f1c72012-09-28 17:17:10 -0700120 Thread* self = Thread::Current();
121 {
122 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
123 shutting_down_started_ = true;
124 while (threads_being_born_ > 0) {
Ian Rogersc604d732012-10-14 16:09:54 -0700125 shutdown_cond_->Wait(self);
Ian Rogers120f1c72012-09-28 17:17:10 -0700126 }
127 shutting_down_ = true;
128 }
Elliott Hughes6b355752012-01-13 16:49:08 -0800129
jeffhaob5e81852012-03-12 11:15:45 -0700130 if (IsMethodTracingActive()) {
131 Trace::Shutdown();
132 }
133
Mathieu Chartiera6399032012-06-11 18:49:50 -0700134 // Make sure to let the GC complete if it is running.
Ian Rogers120f1c72012-09-28 17:17:10 -0700135 heap_->WaitForConcurrentGcToComplete(self);
Mathieu Chartiera6399032012-06-11 18:49:50 -0700136
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700137 // Make sure our internal threads are dead before we start tearing down things they're using.
Elliott Hughese52e49b2012-04-02 16:05:44 -0700138 Dbg::StopJdwp();
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700139 delete signal_catcher_;
140
Elliott Hughes038a8062011-09-18 14:12:41 -0700141 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
Elliott Hughes93e74e82011-09-13 11:07:03 -0700142 delete thread_list_;
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700143 delete monitor_list_;
Elliott Hughes93e74e82011-09-13 11:07:03 -0700144
Carl Shapiro61e019d2011-07-14 16:53:09 -0700145 delete class_linker_;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800146 delete heap_;
Elliott Hughes0a1038b2012-06-14 16:24:17 -0700147 verifier::MethodVerifier::Shutdown();
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700148 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700149 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700150 Thread::Shutdown();
Elliott Hughes7c6169d2012-05-02 16:11:48 -0700151 QuasiAtomic::Shutdown();
152
Carl Shapiro4acf4642011-07-26 18:54:13 -0700153 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700154 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -0700155 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700156}
157
Elliott Hughescac6cc72011-11-03 20:31:21 -0700158static bool gAborting = false;
159
Elliott Hughese0918552011-10-28 17:18:29 -0700160struct AbortState {
161 void Dump(std::ostream& os) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700162 if (gAborting) {
163 os << "Runtime aborting --- recursively, so no thread-specific detail!\n";
164 return;
165 }
166 gAborting = true;
Elliott Hughese0918552011-10-28 17:18:29 -0700167 os << "Runtime aborting...\n";
Elliott Hughes6c7d2442012-02-01 18:40:47 -0800168 if (Runtime::Current() == NULL) {
169 os << "(Runtime does not yet exist!)\n";
170 return;
171 }
Elliott Hughese0918552011-10-28 17:18:29 -0700172 Thread* self = Thread::Current();
173 if (self == NULL) {
174 os << "(Aborting thread was not attached to runtime!)\n";
175 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700176 // TODO: we're aborting and the ScopedObjectAccess may attempt to acquire the mutator_lock_
177 // which may block indefinitely if there's a misbehaving thread holding it exclusively.
178 // The code below should be made robust to this.
179 ScopedObjectAccess soa(self);
Ian Rogers2f7f9b62012-10-10 18:24:05 -0700180 os << "Aborting thread:\n";
Elliott Hughes899e7892012-01-24 14:57:32 -0800181 self->Dump(os);
182 if (self->IsExceptionPending()) {
183 os << "Pending " << PrettyTypeOf(self->GetException()) << " on thread:\n"
184 << self->GetException()->Dump();
185 }
Elliott Hughese0918552011-10-28 17:18:29 -0700186 }
Ian Rogers2f7f9b62012-10-10 18:24:05 -0700187 DumpAllThreads(os, self);
188 }
189
190 void DumpAllThreads(std::ostream& os, Thread* self) NO_THREAD_SAFETY_ANALYSIS {
191 bool tll_already_held = Locks::thread_list_lock_->IsExclusiveHeld(self);
192 bool ml_already_held = Locks::mutator_lock_->IsSharedHeld(self);
Ian Rogersfbd22912012-10-12 14:21:10 -0700193 if (!tll_already_held || !ml_already_held) {
194 os << "Dumping all threads without appropriate locks held:"
195 << (!tll_already_held ? " thread list lock" : "")
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700196 << (!ml_already_held ? " mutator lock" : "")
197 << "\n";
Ian Rogers2f7f9b62012-10-10 18:24:05 -0700198 }
Ian Rogersfbd22912012-10-12 14:21:10 -0700199 os << "All threads:\n";
200 Runtime::Current()->GetThreadList()->DumpLocked(os);
Elliott Hughese0918552011-10-28 17:18:29 -0700201 }
202};
203
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700204void Runtime::Abort() {
Elliott Hughes131aef82012-01-27 11:53:35 -0800205 // Ensure that we don't have multiple threads trying to abort at once,
206 // which would result in significantly worse diagnostics.
Ian Rogers50b35e22012-10-04 10:09:15 -0700207 MutexLock mu(Thread::Current(), *Locks::abort_lock_);
Elliott Hughes131aef82012-01-27 11:53:35 -0800208
Elliott Hughesffe67362011-07-17 12:09:27 -0700209 // Get any pending output out of the way.
210 fflush(NULL);
211
212 // Many people have difficulty distinguish aborts from crashes,
213 // so be explicit.
Elliott Hughese0918552011-10-28 17:18:29 -0700214 AbortState state;
Elliott Hughes9ee5f9c2012-02-03 18:33:16 -0800215 LOG(INTERNAL_FATAL) << Dumpable<AbortState>(state);
Elliott Hughesffe67362011-07-17 12:09:27 -0700216
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700217 // Call the abort hook if we have one.
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700218 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700219 LOG(INTERNAL_FATAL) << "Calling abort hook...";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700220 Runtime::Current()->abort_();
221 // notreached
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700222 LOG(INTERNAL_FATAL) << "Unexpectedly returned from abort hook!";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700223 }
224
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700225#if defined(__BIONIC__)
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700226 // TODO: finish merging patches to fix abort(3) in bionic, then lose this!
227 // Bionic doesn't implement POSIX semantics for abort(3) in a multi-threaded
228 // process, so if we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -0700229 // receive SIGABRT. debuggerd dumps the stack trace of the main
230 // thread, whether or not that was the thread that failed. By
231 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -0700232 // fault in the current thread, and get a useful log from debuggerd.
Elliott Hughes81ff3182012-03-23 20:35:56 -0700233 // We can also trivially tell the difference between a crash and
Elliott Hughesffe67362011-07-17 12:09:27 -0700234 // a deliberate abort by looking at the fault address.
235 *reinterpret_cast<char*>(0xdeadd00d) = 38;
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700236#elif defined(__APPLE__)
237 // TODO: check that this actually gives good stack traces on the Mac!
238 pthread_kill(pthread_self(), SIGABRT);
239#else
240 // TODO: we ought to be able to use pthread_kill(3) here (or abort(3),
241 // which POSIX defines in terms of raise(3), which POSIX defines in terms
242 // of pthread_kill(3)). On Linux, though, libcorkscrew can't unwind through
243 // libpthread, which means the stacks we dump would be useless. Calling
244 // tgkill(2) directly avoids that.
245 syscall(__NR_tgkill, getpid(), GetTid(), SIGABRT);
246#endif
Elliott Hughesffe67362011-07-17 12:09:27 -0700247 // notreached
248}
249
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700250bool Runtime::PreZygoteFork() {
251 heap_->PreZygoteFork();
252 return true;
253}
254
Elliott Hughesbf86d042011-08-31 17:53:14 -0700255void Runtime::CallExitHook(jint status) {
256 if (exit_ != NULL) {
Elliott Hughes34e06962012-04-09 13:55:55 -0700257 ScopedThreadStateChange tsc(Thread::Current(), kNative);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700258 exit_(status);
259 LOG(WARNING) << "Exit hook returned instead of exiting!";
260 }
261}
262
Brian Carlstrom8a436592011-08-15 21:27:23 -0700263// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
264// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
265// [gG] gigabytes.
266//
267// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700268// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
269// of 1024.
270//
271// The spec says the -Xmx and -Xms options must be multiples of 1024. It
272// doesn't say anything about -Xss.
273//
274// Returns 0 (a useless size) if "s" is malformed or specifies a low or
275// non-evenly-divisible value.
276//
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800277size_t ParseMemoryOption(const char* s, size_t div) {
Brian Carlstrom8a436592011-08-15 21:27:23 -0700278 // strtoul accepts a leading [+-], which we don't want,
279 // so make sure our string starts with a decimal digit.
280 if (isdigit(*s)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700281 char* s2;
282 size_t val = strtoul(s, &s2, 10);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700283 if (s2 != s) {
284 // s2 should be pointing just after the number.
285 // If this is the end of the string, the user
286 // has specified a number of bytes. Otherwise,
287 // there should be exactly one more character
288 // that specifies a multiplier.
289 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700290 // The remainder of the string is either a single multiplier
291 // character, or nothing to indicate that the value is in
292 // bytes.
293 char c = *s2++;
294 if (*s2 == '\0') {
295 size_t mul;
296 if (c == '\0') {
297 mul = 1;
298 } else if (c == 'k' || c == 'K') {
299 mul = KB;
300 } else if (c == 'm' || c == 'M') {
301 mul = MB;
302 } else if (c == 'g' || c == 'G') {
303 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700304 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700305 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700306 return 0;
307 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700308
309 if (val <= std::numeric_limits<size_t>::max() / mul) {
310 val *= mul;
311 } else {
312 // Clamp to a multiple of 1024.
313 val = std::numeric_limits<size_t>::max() & ~(1024-1);
314 }
315 } else {
316 // There's more than one character after the numeric part.
317 return 0;
318 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700319 }
320 // The man page says that a -Xm value must be a multiple of 1024.
321 if (val % div == 0) {
322 return val;
323 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700324 }
325 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700326 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700327}
328
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800329size_t ParseIntegerOrDie(const std::string& s) {
330 std::string::size_type colon = s.find(':');
331 if (colon == std::string::npos) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700332 LOG(FATAL) << "Missing integer: " << s;
333 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800334 const char* begin = &s[colon + 1];
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700335 char* end;
336 size_t result = strtoul(begin, &end, 10);
337 if (begin == end || *end != '\0') {
338 LOG(FATAL) << "Failed to parse integer in: " << s;
339 }
340 return result;
341}
342
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700343Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700344 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800345 const char* boot_class_path_string = getenv("BOOTCLASSPATH");
346 if (boot_class_path_string != NULL) {
347 parsed->boot_class_path_string_ = boot_class_path_string;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700348 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800349 const char* class_path_string = getenv("CLASSPATH");
350 if (class_path_string != NULL) {
351 parsed->class_path_string_ = class_path_string;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700352 }
Elliott Hughes67d92002012-03-26 15:08:51 -0700353 // -Xcheck:jni is off by default for regular builds but on by default in debug builds.
354 parsed->check_jni_ = kIsDebugBuild;
Elliott Hughes85d15452011-09-16 17:33:01 -0700355
Mathieu Chartier0051be62012-10-12 17:47:11 -0700356 parsed->heap_initial_size_ = Heap::kDefaultInitialSize;
357 parsed->heap_maximum_size_ = Heap::kDefaultMaximumSize;
358 parsed->heap_min_free_ = Heap::kDefaultMinFree;
359 parsed->heap_max_free_ = Heap::kDefaultMaxFree;
360 parsed->heap_target_utilization_ = Heap::kDefaultTargetUtilization;
Elliott Hughes6cf23882012-06-15 15:42:07 -0700361 parsed->heap_growth_limit_ = 0; // 0 means no growth limit.
362 parsed->stack_size_ = 0; // 0 means default.
Brian Carlstromf734cf52011-08-17 16:28:14 -0700363
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800364 parsed->is_compiler_ = false;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700365 parsed->is_zygote_ = false;
Mathieu Chartier069387a2012-06-18 12:01:01 -0700366 parsed->is_concurrent_gc_enabled_ = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700367
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700368 parsed->jni_globals_max_ = 0;
Elliott Hughesfc861622011-10-17 17:57:47 -0700369 parsed->lock_profiling_threshold_ = 0;
370 parsed->hook_is_sensitive_thread_ = NULL;
371
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700372 parsed->hook_vfprintf_ = vfprintf;
373 parsed->hook_exit_ = exit;
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700374 parsed->hook_abort_ = NULL; // We don't call abort(3) by default; see Runtime::Abort.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700375
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800376// gLogVerbosity.class_linker = true; // TODO: don't check this in!
377// gLogVerbosity.compiler = true; // TODO: don't check this in!
378// gLogVerbosity.heap = true; // TODO: don't check this in!
379// gLogVerbosity.gc = true; // TODO: don't check this in!
380// gLogVerbosity.jdwp = true; // TODO: don't check this in!
381// gLogVerbosity.jni = true; // TODO: don't check this in!
382// gLogVerbosity.monitor = true; // TODO: don't check this in!
383// gLogVerbosity.startup = true; // TODO: don't check this in!
384// gLogVerbosity.third_party_jni = true; // TODO: don't check this in!
385// gLogVerbosity.threads = true; // TODO: don't check this in!
386
jeffhaob5e81852012-03-12 11:15:45 -0700387 parsed->method_trace_ = false;
388 parsed->method_trace_file_ = "/data/method-trace-file.bin";
389 parsed->method_trace_file_size_ = 10 * MB;
390
Brian Carlstrom8a436592011-08-15 21:27:23 -0700391 for (size_t i = 0; i < options.size(); ++i) {
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800392 const std::string option(options[i].first);
Brian Carlstrom0796af02011-10-12 14:31:45 -0700393 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700394 LOG(INFO) << "option[" << i << "]=" << option;
395 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800396 if (StartsWith(option, "-Xbootclasspath:")) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800397 parsed->boot_class_path_string_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700398 } else if (option == "-classpath" || option == "-cp") {
399 // TODO: support -Djava.class.path
400 i++;
401 if (i == options.size()) {
402 // TODO: usage
403 LOG(FATAL) << "Missing required class path value for " << option;
404 return NULL;
405 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800406 const StringPiece& value = options[i].first;
407 parsed->class_path_string_ = value.data();
408 } else if (option == "bootclasspath") {
409 parsed->boot_class_path_
410 = reinterpret_cast<const std::vector<const DexFile*>*>(options[i].second);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800411 } else if (StartsWith(option, "-Ximage:")) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800412 parsed->image_ = option.substr(strlen("-Ximage:")).data();
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800413 } else if (StartsWith(option, "-Xcheck:jni")) {
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700414 parsed->check_jni_ = true;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800415 } else if (StartsWith(option, "-Xrunjdwp:") || StartsWith(option, "-agentlib:jdwp=")) {
416 std::string tail(option.substr(option[1] == 'X' ? 10 : 15));
Elliott Hughes3bb81562011-10-21 18:52:59 -0700417 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
418 LOG(FATAL) << "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
419 << "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n";
420 return NULL;
421 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800422 } else if (StartsWith(option, "-Xms")) {
423 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).c_str(), 1024);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700424 if (size == 0) {
425 if (ignore_unrecognized) {
426 continue;
427 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700428 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700429 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700430 return NULL;
431 }
432 parsed->heap_initial_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800433 } else if (StartsWith(option, "-Xmx")) {
434 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).c_str(), 1024);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700435 if (size == 0) {
436 if (ignore_unrecognized) {
437 continue;
438 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700439 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700440 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700441 return NULL;
442 }
443 parsed->heap_maximum_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800444 } else if (StartsWith(option, "-XX:HeapGrowthLimit=")) {
445 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).c_str(), 1024);
jeffhaoc1160702011-10-27 15:48:45 -0700446 if (size == 0) {
447 if (ignore_unrecognized) {
448 continue;
449 }
450 // TODO: usage
451 LOG(FATAL) << "Failed to parse " << option;
452 return NULL;
453 }
454 parsed->heap_growth_limit_ = size;
Mathieu Chartier0051be62012-10-12 17:47:11 -0700455 } else if (StartsWith(option, "-XX:HeapMinFree=")) {
456 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapMinFree=")).c_str(), 1024);
457 if (size == 0) {
458 if (ignore_unrecognized) {
459 continue;
460 }
461 // TODO: usage
462 LOG(FATAL) << "Failed to parse " << option;
463 return NULL;
464 }
465 parsed->heap_min_free_ = size;
466 } else if (StartsWith(option, "-XX:HeapMaxFree=")) {
467 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapMaxFree=")).c_str(), 1024);
468 if (size == 0) {
469 if (ignore_unrecognized) {
470 continue;
471 }
472 // TODO: usage
473 LOG(FATAL) << "Failed to parse " << option;
474 return NULL;
475 }
476 parsed->heap_max_free_ = size;
477 } else if (StartsWith(option, "-XX:HeapTargetUtilization=")) {
478 std::istringstream iss(option.substr(strlen("-XX:HeapTargetUtilization=")));
479 double value;
480 iss >> value;
481 // Ensure that we have a value, there was no cruft after it and it satisfies a sensible range.
482 const bool sane_val = iss.good() && (value >= 0.1) && (value <= 0.9);
483 if (!sane_val) {
484 if (ignore_unrecognized) {
485 continue;
486 }
487 LOG(FATAL) << "Invalid option '" << option << "'";
488 return NULL;
489 }
490 parsed->heap_target_utilization_ = value;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800491 } else if (StartsWith(option, "-Xss")) {
492 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).c_str(), 1);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700493 if (size == 0) {
494 if (ignore_unrecognized) {
495 continue;
496 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700497 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700498 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700499 return NULL;
500 }
501 parsed->stack_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800502 } else if (StartsWith(option, "-D")) {
503 parsed->properties_.push_back(option.substr(strlen("-D")));
504 } else if (StartsWith(option, "-Xjnitrace:")) {
505 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:"));
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700506 } else if (option == "compiler") {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800507 parsed->is_compiler_ = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700508 } else if (option == "-Xzygote") {
509 parsed->is_zygote_ = true;
Mathieu Chartier069387a2012-06-18 12:01:01 -0700510 } else if (StartsWith(option, "-Xgc:")) {
511 std::vector<std::string> gc_options;
512 Split(option.substr(strlen("-Xgc:")), ',', gc_options);
513 for (size_t i = 0; i < gc_options.size(); ++i) {
514 if (gc_options[i] == "noconcurrent") {
515 parsed->is_concurrent_gc_enabled_ = false;
516 } else if (gc_options[i] == "concurrent") {
517 parsed->is_concurrent_gc_enabled_ = true;
518 } else {
519 LOG(WARNING) << "Ignoring unknown -Xgc option: " << gc_options[i];
520 }
521 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800522 } else if (StartsWith(option, "-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700523 std::vector<std::string> verbose_options;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800524 Split(option.substr(strlen("-verbose:")), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700525 for (size_t i = 0; i < verbose_options.size(); ++i) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800526 if (verbose_options[i] == "class") {
527 gLogVerbosity.class_linker = true;
528 } else if (verbose_options[i] == "compiler") {
529 gLogVerbosity.compiler = true;
530 } else if (verbose_options[i] == "heap") {
531 gLogVerbosity.heap = true;
532 } else if (verbose_options[i] == "gc") {
533 gLogVerbosity.gc = true;
534 } else if (verbose_options[i] == "jdwp") {
535 gLogVerbosity.jdwp = true;
536 } else if (verbose_options[i] == "jni") {
537 gLogVerbosity.jni = true;
538 } else if (verbose_options[i] == "monitor") {
539 gLogVerbosity.monitor = true;
540 } else if (verbose_options[i] == "startup") {
541 gLogVerbosity.startup = true;
542 } else if (verbose_options[i] == "third-party-jni") {
543 gLogVerbosity.third_party_jni = true;
544 } else if (verbose_options[i] == "threads") {
545 gLogVerbosity.threads = true;
546 } else {
547 LOG(WARNING) << "Ignoring unknown -verbose option: " << verbose_options[i];
548 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700549 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800550 } else if (StartsWith(option, "-Xjnigreflimit:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700551 parsed->jni_globals_max_ = ParseIntegerOrDie(option);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800552 } else if (StartsWith(option, "-Xlockprofthreshold:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700553 parsed->lock_profiling_threshold_ = ParseIntegerOrDie(option);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800554 } else if (StartsWith(option, "-Xstacktracefile:")) {
Elliott Hughes206c20d2012-06-28 11:05:37 -0700555 parsed->stack_trace_file_ = option.substr(strlen("-Xstacktracefile:"));
Elliott Hughesfc861622011-10-17 17:57:47 -0700556 } else if (option == "sensitiveThread") {
Ian Rogers1b09b092012-08-20 15:35:52 -0700557 parsed->hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(const_cast<void*>(options[i].second));
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700558 } else if (option == "vfprintf") {
Ian Rogers1b09b092012-08-20 15:35:52 -0700559 parsed->hook_vfprintf_ =
560 reinterpret_cast<int (*)(FILE *, const char*, va_list)>(const_cast<void*>(options[i].second));
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700561 } else if (option == "exit") {
Ian Rogers1b09b092012-08-20 15:35:52 -0700562 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(const_cast<void*>(options[i].second));
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700563 } else if (option == "abort") {
Ian Rogers1b09b092012-08-20 15:35:52 -0700564 parsed->hook_abort_ = reinterpret_cast<void(*)()>(const_cast<void*>(options[i].second));
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700565 } else if (option == "host-prefix") {
566 parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second);
Elliott Hughesb3e66df2012-01-12 14:49:18 -0800567 } else if (option == "-Xgenregmap" || option == "-Xgc:precise") {
568 // We silently ignore these for backwards compatibility.
jeffhaob5e81852012-03-12 11:15:45 -0700569 } else if (option == "-Xmethod-trace") {
570 parsed->method_trace_ = true;
571 } else if (StartsWith(option, "-Xmethod-trace-file:")) {
572 parsed->method_trace_file_ = option.substr(strlen("-Xmethod-trace-file:"));
573 } else if (StartsWith(option, "-Xmethod-trace-file-size:")) {
574 parsed->method_trace_file_size_ = ParseIntegerOrDie(option);
Elliott Hughese119a362012-05-22 17:37:06 -0700575 } else if (option == "-Xprofile:threadcpuclock") {
576 Trace::SetDefaultClockSource(kProfilerClockSourceThreadCpu);
577 } else if (option == "-Xprofile:wallclock") {
578 Trace::SetDefaultClockSource(kProfilerClockSourceWall);
579 } else if (option == "-Xprofile:dualclock") {
580 Trace::SetDefaultClockSource(kProfilerClockSourceDual);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700581 } else {
582 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700583 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700584 LOG(ERROR) << "Unrecognized option " << option;
585 // TODO: this should exit, but for now tolerate unknown options
586 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700587 }
588 }
589 }
590
Brian Carlstrom223f20f2012-02-04 23:06:55 -0800591 if (!parsed->is_compiler_ && parsed->image_.empty()) {
592 parsed->image_ += GetAndroidRoot();
593 parsed->image_ += "/framework/boot.art";
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700594 }
jeffhaoc1160702011-10-27 15:48:45 -0700595 if (parsed->heap_growth_limit_ == 0) {
596 parsed->heap_growth_limit_ = parsed->heap_maximum_size_;
597 }
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700598
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700599 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700600}
601
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700602bool Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700603 // TODO: acquire a static mutex on Runtime to avoid racing.
604 if (Runtime::instance_ != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700605 return false;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700606 }
Ian Rogersb726dcb2012-09-05 08:57:23 -0700607 Locks::Init();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700608 instance_ = new Runtime;
609 if (!instance_->Init(options, ignore_unrecognized)) {
610 delete instance_;
611 instance_ = NULL;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700612 return false;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700613 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700614 return true;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700615}
Elliott Hughes0af55432011-08-17 18:37:28 -0700616
Ian Rogers365c1022012-06-22 15:05:28 -0700617static void CreateSystemClassLoader() {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800618 if (Runtime::Current()->UseCompileTimeClassPath()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700619 return;
620 }
621
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700622 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700623
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700624 Class* class_loader_class = soa.Decode<Class*>(WellKnownClasses::java_lang_ClassLoader);
Ian Rogers365c1022012-06-22 15:05:28 -0700625 CHECK(Runtime::Current()->GetClassLinker()->EnsureInitialized(class_loader_class, true, true));
Brian Carlstromaded5f72011-10-07 17:15:04 -0700626
Mathieu Chartier66f19252012-09-18 08:57:04 -0700627 AbstractMethod* getSystemClassLoader = class_loader_class->FindDirectMethod("getSystemClassLoader", "()Ljava/lang/ClassLoader;");
Brian Carlstromdf143242011-10-10 18:05:34 -0700628 CHECK(getSystemClassLoader != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700629
Ian Rogers365c1022012-06-22 15:05:28 -0700630 ClassLoader* class_loader =
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700631 down_cast<ClassLoader*>(InvokeWithJValues(soa, NULL, getSystemClassLoader, NULL).GetL());
Ian Rogers365c1022012-06-22 15:05:28 -0700632 CHECK(class_loader != NULL);
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500633
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700634 soa.Self()->SetClassLoaderOverride(class_loader);
Ian Rogers365c1022012-06-22 15:05:28 -0700635
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700636 Class* thread_class = soa.Decode<Class*>(WellKnownClasses::java_lang_Thread);
Ian Rogers365c1022012-06-22 15:05:28 -0700637 CHECK(Runtime::Current()->GetClassLinker()->EnsureInitialized(thread_class, true, true));
638
639 Field* contextClassLoader = thread_class->FindDeclaredInstanceField("contextClassLoader",
640 "Ljava/lang/ClassLoader;");
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500641 CHECK(contextClassLoader != NULL);
Ian Rogers365c1022012-06-22 15:05:28 -0700642
Mathieu Chartierdbe6f462012-09-25 16:54:50 -0700643 contextClassLoader->SetObject(soa.Decode<Object*>(soa.Self()->GetPeer()), class_loader);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700644}
645
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700646void Runtime::Start() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800647 VLOG(startup) << "Runtime::Start entering";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700648
649 CHECK(host_prefix_.empty()) << host_prefix_;
650
Elliott Hughes225f5a12012-06-11 11:23:48 -0700651 // Relocate the OatFiles (ELF images).
Logan Chien0c717dd2012-03-28 18:31:07 +0800652 class_linker_->RelocateExecutable();
653
Elliott Hughes225f5a12012-06-11 11:23:48 -0700654 // Pre-allocate an OutOfMemoryError for the double-OOME case.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700655 Thread* self = Thread::Current();
Elliott Hughes225f5a12012-06-11 11:23:48 -0700656 self->ThrowNewException("Ljava/lang/OutOfMemoryError;",
657 "OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack available");
658 pre_allocated_OutOfMemoryError_ = self->GetException();
659 self->ClearException();
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700660
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700661 // Restore main thread state to kNative as expected by native code.
662 self->TransitionFromRunnableToSuspended(kNative);
663
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700664 started_ = true;
665
Brian Carlstromae826982011-11-09 01:33:42 -0800666 // InitNativeMethods needs to be after started_ so that the classes
667 // it touches will have methods linked to the oat file if necessary.
668 InitNativeMethods();
669
Ian Rogers365c1022012-06-22 15:05:28 -0700670 // Initialize well known thread group values that may be accessed threads while attaching.
671 InitThreadGroups(self);
672
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500673 Thread::FinishStartup();
674
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700675 if (!is_zygote_) {
676 DidForkFromZygote();
677 }
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700678
Elliott Hughes85d15452011-09-16 17:33:01 -0700679 StartDaemonThreads();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700680
Brian Carlstromaded5f72011-10-07 17:15:04 -0700681 CreateSystemClassLoader();
682
Elliott Hughes225f5a12012-06-11 11:23:48 -0700683 self->GetJniEnv()->locals.AssertEmpty();
Elliott Hughes726079d2011-10-07 18:43:44 -0700684
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800685 VLOG(startup) << "Runtime::Start exiting";
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700686
687 finished_starting_ = true;
Elliott Hughes85d15452011-09-16 17:33:01 -0700688}
689
Ian Rogers120f1c72012-09-28 17:17:10 -0700690void Runtime::EndThreadBirth() EXCLUSIVE_LOCKS_REQUIRED(Locks::runtime_shutdown_lock_) {
691 DCHECK_GT(threads_being_born_, 0U);
692 threads_being_born_--;
693 if (shutting_down_started_ && threads_being_born_ == 0) {
Ian Rogersc604d732012-10-14 16:09:54 -0700694 shutdown_cond_->Broadcast(Thread::Current());
Ian Rogers120f1c72012-09-28 17:17:10 -0700695 }
696}
697
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700698void Runtime::DidForkFromZygote() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700699 is_zygote_ = false;
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700700
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700701 // Create the thread pool.
702 heap_->CreateThreadPool();
703
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700704 StartSignalCatcher();
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700705
706 // Start the JDWP thread. If the command-line debugger flags specified "suspend=y",
707 // this will pause the runtime, so we probably want this to come last.
708 Dbg::StartJdwp();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700709}
710
711void Runtime::StartSignalCatcher() {
712 if (!is_zygote_) {
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700713 signal_catcher_ = new SignalCatcher(stack_trace_file_);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700714 }
715}
716
Elliott Hughes85d15452011-09-16 17:33:01 -0700717void Runtime::StartDaemonThreads() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800718 VLOG(startup) << "Runtime::StartDaemonThreads entering";
Elliott Hughes85d15452011-09-16 17:33:01 -0700719
Elliott Hughes719b3232011-09-25 17:42:19 -0700720 Thread* self = Thread::Current();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700721
722 // Must be in the kNative state for calling native methods.
Ian Rogers50b35e22012-10-04 10:09:15 -0700723 CHECK_EQ(self->GetState(), kNative);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700724
Elliott Hughes719b3232011-09-25 17:42:19 -0700725 JNIEnv* env = self->GetJniEnv();
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700726 env->CallStaticVoidMethod(WellKnownClasses::java_lang_Daemons,
727 WellKnownClasses::java_lang_Daemons_start);
728 if (env->ExceptionCheck()) {
729 env->ExceptionDescribe();
730 LOG(FATAL) << "Error starting java.lang.Daemons";
731 }
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700732
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800733 VLOG(startup) << "Runtime::StartDaemonThreads exiting";
Carl Shapiro61e019d2011-07-14 16:53:09 -0700734}
735
Elliott Hughes0af55432011-08-17 18:37:28 -0700736bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700737 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700738
Elliott Hughes90a33692011-08-30 13:27:07 -0700739 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
740 if (options.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700741 LOG(ERROR) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700742 return false;
743 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800744 VLOG(startup) << "Runtime::Init -verbose:startup enabled";
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700745
Elliott Hughes7c6169d2012-05-02 16:11:48 -0700746 QuasiAtomic::Startup();
747
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700748 SetJniGlobalsMax(options->jni_globals_max_);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800749 Monitor::Init(options->lock_profiling_threshold_, options->hook_is_sensitive_thread_);
Elliott Hughes32d6e1e2011-10-11 14:47:44 -0700750
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700751 host_prefix_ = options->host_prefix_;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800752 boot_class_path_string_ = options->boot_class_path_string_;
753 class_path_string_ = options->class_path_string_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700754 properties_ = options->properties_;
755
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800756 is_compiler_ = options->is_compiler_;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700757 is_zygote_ = options->is_zygote_;
Mathieu Chartier069387a2012-06-18 12:01:01 -0700758 is_concurrent_gc_enabled_ = options->is_concurrent_gc_enabled_;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700759
Elliott Hughes0af55432011-08-17 18:37:28 -0700760 vfprintf_ = options->hook_vfprintf_;
761 exit_ = options->hook_exit_;
762 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700763
Elliott Hughesbe759c62011-09-08 19:38:21 -0700764 default_stack_size_ = options->stack_size_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700765 stack_trace_file_ = options->stack_trace_file_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700766
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700767 monitor_list_ = new MonitorList;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800768 thread_list_ = new ThreadList;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700769 intern_table_ = new InternTable;
770
Elliott Hughes0a1038b2012-06-14 16:24:17 -0700771 verifier::MethodVerifier::Init();
Logan Chiendd361c92012-04-10 23:40:37 +0800772
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800773 heap_ = new Heap(options->heap_initial_size_,
774 options->heap_growth_limit_,
Mathieu Chartier0051be62012-10-12 17:47:11 -0700775 options->heap_min_free_,
776 options->heap_max_free_,
777 options->heap_target_utilization_,
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800778 options->heap_maximum_size_,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700779 options->image_,
780 options->is_concurrent_gc_enabled_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700781
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700782 BlockSignals();
Elliott Hughes457005c2012-04-16 13:54:25 -0700783 InitPlatformSignalHandlers();
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700784
Elliott Hughesa0957642011-09-02 14:27:33 -0700785 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700786
Elliott Hughesbe759c62011-09-08 19:38:21 -0700787 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700788
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700789 // ClassLinker needs an attached thread, but we can't fully attach a thread
Elliott Hughes462c9442012-03-23 18:47:50 -0700790 // without creating objects. We can't supply a thread group yet; it will be fixed later.
Ian Rogers120f1c72012-09-28 17:17:10 -0700791 Thread* self = Thread::Attach("main", false, NULL);
792 CHECK(self != NULL);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700793
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700794 // Set us to runnable so tools using a runtime can allocate and GC by default
Ian Rogers120f1c72012-09-28 17:17:10 -0700795 self->TransitionFromSuspendedToRunnable();
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700796
Ian Rogers141d6222012-04-05 12:23:06 -0700797 // Now we're attached, we can take the heap lock and validate the heap.
798 GetHeap()->EnableObjectValidation();
799
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800800 CHECK_GE(GetHeap()->GetSpaces().size(), 1U);
801 if (GetHeap()->GetSpaces()[0]->IsImageSpace()) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800802 class_linker_ = ClassLinker::CreateFromImage(intern_table_);
803 } else {
804 CHECK(options->boot_class_path_ != NULL);
805 CHECK_NE(options->boot_class_path_->size(), 0U);
806 class_linker_ = ClassLinker::CreateFromCompiler(*options->boot_class_path_, intern_table_);
807 }
808 CHECK(class_linker_ != NULL);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700809
jeffhaob5e81852012-03-12 11:15:45 -0700810 method_trace_ = options->method_trace_;
811 method_trace_file_ = options->method_trace_file_;
812 method_trace_file_size_ = options->method_trace_file_size_;
813
814 if (options->method_trace_) {
815 Trace::Start(options->method_trace_file_.c_str(), -1, options->method_trace_file_size_, 0, false);
816 }
817
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800818 VLOG(startup) << "Runtime::Init exiting";
Carl Shapiro1fb86202011-06-27 17:43:13 -0700819 return true;
820}
821
Elliott Hughes038a8062011-09-18 14:12:41 -0700822void Runtime::InitNativeMethods() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800823 VLOG(startup) << "Runtime::InitNativeMethods entering";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700824 Thread* self = Thread::Current();
825 JNIEnv* env = self->GetJniEnv();
826
Elliott Hughes418d20f2011-09-22 14:00:39 -0700827 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Ian Rogers50b35e22012-10-04 10:09:15 -0700828 CHECK_EQ(self->GetState(), kNative);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700829
Elliott Hughes418d20f2011-09-22 14:00:39 -0700830 // First set up JniConstants, which is used by both the runtime's built-in native
831 // methods and libcore.
Elliott Hughesfea966e2011-09-22 10:26:20 -0700832 JniConstants::init(env);
Elliott Hugheseac76672012-05-24 21:56:51 -0700833 WellKnownClasses::Init(env);
Elliott Hughesfea966e2011-09-22 10:26:20 -0700834
Elliott Hughes418d20f2011-09-22 14:00:39 -0700835 // Then set up the native methods provided by the runtime itself.
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700836 RegisterRuntimeNativeMethods(env);
837
Elliott Hughes418d20f2011-09-22 14:00:39 -0700838 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
839 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
840 // the library that implements System.loadLibrary!
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700841 {
842 std::string mapped_name(StringPrintf(OS_SHARED_LIB_FORMAT_STR, "javacore"));
843 std::string reason;
844 self->TransitionFromSuspendedToRunnable();
845 if (!instance_->java_vm_->LoadNativeLibrary(mapped_name, NULL, reason)) {
846 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": " << reason;
847 }
848 self->TransitionFromRunnableToSuspended(kNative);
849 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800850 VLOG(startup) << "Runtime::InitNativeMethods exiting";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700851}
852
Ian Rogers365c1022012-06-22 15:05:28 -0700853void Runtime::InitThreadGroups(Thread* self) {
854 JNIEnvExt* env = self->GetJniEnv();
855 ScopedJniEnvLocalRefState env_state(env);
856 main_thread_group_ =
857 env->NewGlobalRef(env->GetStaticObjectField(WellKnownClasses::java_lang_ThreadGroup,
858 WellKnownClasses::java_lang_ThreadGroup_mainThreadGroup));
Brian Carlstrom034f76b2012-08-01 15:51:58 -0700859 CHECK(main_thread_group_ != NULL || IsCompiler());
Ian Rogers365c1022012-06-22 15:05:28 -0700860 system_thread_group_ =
861 env->NewGlobalRef(env->GetStaticObjectField(WellKnownClasses::java_lang_ThreadGroup,
862 WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup));
Brian Carlstrom034f76b2012-08-01 15:51:58 -0700863 CHECK(system_thread_group_ != NULL || IsCompiler());
864}
865
866jobject Runtime::GetMainThreadGroup() const {
867 CHECK(main_thread_group_ != NULL || IsCompiler());
868 return main_thread_group_;
869}
870
871jobject Runtime::GetSystemThreadGroup() const {
872 CHECK(system_thread_group_ != NULL || IsCompiler());
873 return system_thread_group_;
Ian Rogers365c1022012-06-22 15:05:28 -0700874}
875
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700876void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
877#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Ian Rogers5167c972012-02-03 10:41:20 -0800878 // Register Throwable first so that registration of other native methods can throw exceptions
879 REGISTER(register_java_lang_Throwable);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700880 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700881 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700882 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700883 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700884 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700885 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700886 REGISTER(register_java_lang_Object);
887 REGISTER(register_java_lang_Runtime);
888 REGISTER(register_java_lang_String);
889 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700890 REGISTER(register_java_lang_Thread);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700891 REGISTER(register_java_lang_VMClassLoader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700892 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700893 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700894 REGISTER(register_java_lang_reflect_Field);
895 REGISTER(register_java_lang_reflect_Method);
Jesse Wilson95caa792011-10-12 18:14:17 -0400896 REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700897 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700898 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700899 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700900 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700901#undef REGISTER
902}
903
Elliott Hughesc967f782012-04-16 10:23:15 -0700904void Runtime::DumpForSigQuit(std::ostream& os) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700905 GetClassLinker()->DumpForSigQuit(os);
906 GetInternTable()->DumpForSigQuit(os);
Elliott Hughesae80b492012-04-24 10:43:17 -0700907 GetJavaVM()->DumpForSigQuit(os);
Elliott Hughesc967f782012-04-16 10:23:15 -0700908 GetHeap()->DumpForSigQuit(os);
Elliott Hughes42ee1422011-09-06 12:33:32 -0700909 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700910
Elliott Hughesc967f782012-04-16 10:23:15 -0700911 thread_list_->DumpForSigQuit(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700912}
913
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800914void Runtime::DumpLockHolders(std::ostream& os) {
Ian Rogersb726dcb2012-09-05 08:57:23 -0700915 uint64_t mutator_lock_owner = Locks::mutator_lock_->GetExclusiveOwnerTid();
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800916 pid_t thread_list_lock_owner = GetThreadList()->GetLockOwner();
917 pid_t classes_lock_owner = GetClassLinker()->GetClassesLockOwner();
918 pid_t dex_lock_owner = GetClassLinker()->GetDexLockOwner();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700919 if ((thread_list_lock_owner | classes_lock_owner | dex_lock_owner) != 0) {
920 os << "Mutator lock exclusive owner tid: " << mutator_lock_owner << "\n"
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800921 << "ThreadList lock owner tid: " << thread_list_lock_owner << "\n"
922 << "ClassLinker classes lock owner tid: " << classes_lock_owner << "\n"
923 << "ClassLinker dex lock owner tid: " << dex_lock_owner << "\n";
924 }
925}
926
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700927void Runtime::SetStatsEnabled(bool new_state) {
928 if (new_state == true) {
929 GetStats()->Clear(~0);
930 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
931 Thread::Current()->GetStats()->Clear(~0);
932 }
933 stats_enabled_ = new_state;
934}
935
936void Runtime::ResetStats(int kinds) {
937 GetStats()->Clear(kinds & 0xffff);
938 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
939 Thread::Current()->GetStats()->Clear(kinds >> 16);
940}
941
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700942int32_t Runtime::GetStat(int kind) {
943 RuntimeStats* stats;
944 if (kind < (1<<16)) {
945 stats = GetStats();
946 } else {
947 stats = Thread::Current()->GetStats();
948 kind >>= 16;
949 }
950 switch (kind) {
951 case KIND_ALLOCATED_OBJECTS:
952 return stats->allocated_objects;
953 case KIND_ALLOCATED_BYTES:
954 return stats->allocated_bytes;
955 case KIND_FREED_OBJECTS:
956 return stats->freed_objects;
957 case KIND_FREED_BYTES:
958 return stats->freed_bytes;
959 case KIND_GC_INVOCATIONS:
960 return stats->gc_for_alloc_count;
961 case KIND_CLASS_INIT_COUNT:
962 return stats->class_init_count;
963 case KIND_CLASS_INIT_TIME:
964 // Convert ns to us, reduce to 32 bits.
Elliott Hughes398f64b2012-03-26 18:05:48 -0700965 return static_cast<int>(stats->class_init_time_ns / 1000);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700966 case KIND_EXT_ALLOCATED_OBJECTS:
967 case KIND_EXT_ALLOCATED_BYTES:
968 case KIND_EXT_FREED_OBJECTS:
969 case KIND_EXT_FREED_BYTES:
970 return 0; // backward compatibility
971 default:
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700972 LOG(FATAL) << "Unknown statistic " << kind;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700973 return -1; // unreachable
974 }
975}
976
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700977void Runtime::BlockSignals() {
Elliott Hughes457005c2012-04-16 13:54:25 -0700978 SignalSet signals;
979 signals.Add(SIGPIPE);
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700980 // SIGQUIT is used to dump the runtime's state (including stack traces).
Elliott Hughes457005c2012-04-16 13:54:25 -0700981 signals.Add(SIGQUIT);
Elliott Hughes08795042012-04-03 14:48:52 -0700982 // SIGUSR1 is used to initiate a GC.
Elliott Hughes457005c2012-04-16 13:54:25 -0700983 signals.Add(SIGUSR1);
984 signals.Block();
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700985}
986
Ian Rogers120f1c72012-09-28 17:17:10 -0700987bool Runtime::AttachCurrentThread(const char* thread_name, bool as_daemon, jobject thread_group) {
988 bool success = Thread::Attach(thread_name, as_daemon, thread_group) != NULL;
Elliott Hughes22869a92012-03-27 14:08:24 -0700989 if (thread_name == NULL) {
990 LOG(WARNING) << *Thread::Current() << " attached without supplying a name";
991 }
Ian Rogers120f1c72012-09-28 17:17:10 -0700992 return success;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700993}
994
Elliott Hughesd92bec42011-09-02 17:04:36 -0700995void Runtime::DetachCurrentThread() {
Brian Carlstrom4d571432012-05-16 00:21:41 -0700996 Thread* self = Thread::Current();
997 if (self == NULL) {
998 LOG(FATAL) << "attempting to detach thread that is not attached";
999 }
Ian Rogers0399dde2012-06-06 17:09:28 -07001000 if (self->HasManagedStack()) {
Elliott Hughes22869a92012-03-27 14:08:24 -07001001 LOG(FATAL) << *Thread::Current() << " attempting to detach while still running code";
1002 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001003 thread_list_->Unregister(self);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001004}
1005
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -07001006void Runtime::VisitConcurrentRoots(Heap::RootVisitor* visitor, void* arg) {
1007 if (intern_table_->IsDirty()) {
1008 intern_table_->VisitRoots(visitor, arg);
1009 }
1010 if (class_linker_->IsDirty()) {
1011 class_linker_->VisitRoots(visitor, arg);
1012 }
1013}
1014
Mathieu Chartier858f1c52012-10-17 17:45:55 -07001015void Runtime::VisitNonThreadRoots(Heap::RootVisitor* visitor, void* arg) {
Elliott Hughesbfe487b2011-10-26 15:48:55 -07001016 Dbg::VisitRoots(visitor, arg);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001017 java_vm_->VisitRoots(visitor, arg);
Elliott Hughes225f5a12012-06-11 11:23:48 -07001018 if (pre_allocated_OutOfMemoryError_ != NULL) {
1019 visitor(pre_allocated_OutOfMemoryError_, arg);
1020 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001021 visitor(jni_stub_array_, arg);
1022 visitor(abstract_method_error_stub_array_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001023 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
1024 visitor(resolution_stub_array_[i], arg);
1025 }
Ian Rogers19846512012-02-24 11:42:47 -08001026 visitor(resolution_method_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001027 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
Elliott Hughes225f5a12012-06-11 11:23:48 -07001028 visitor(callee_save_methods_[i], arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001029 }
Brian Carlstrome24fa612011-09-29 00:53:55 -07001030}
1031
Mathieu Chartier858f1c52012-10-17 17:45:55 -07001032void Runtime::VisitNonConcurrentRoots(Heap::RootVisitor* visitor, void* arg) {
1033 thread_list_->VisitRoots(visitor, arg);
1034 VisitNonThreadRoots(visitor, arg);
1035}
1036
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -07001037void Runtime::DirtyRoots() {
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001038 CHECK(intern_table_ != NULL);
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -07001039 intern_table_->Dirty();
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001040 CHECK(class_linker_ != NULL);
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -07001041 class_linker_->Dirty();
1042}
1043
1044void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
1045 VisitConcurrentRoots(visitor, arg);
1046 VisitNonConcurrentRoots(visitor, arg);
1047}
1048
Ian Rogers169c9a72011-11-13 20:13:17 -08001049void Runtime::SetJniDlsymLookupStub(ByteArray* jni_stub_array) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001050 CHECK(jni_stub_array != NULL) << " jni_stub_array=" << jni_stub_array;
1051 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array)
1052 << "jni_stub_array_=" << jni_stub_array_ << " jni_stub_array=" << jni_stub_array;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001053 jni_stub_array_ = jni_stub_array;
1054}
1055
Brian Carlstrome24fa612011-09-29 00:53:55 -07001056void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) {
1057 CHECK(abstract_method_error_stub_array != NULL);
1058 CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array);
1059 abstract_method_error_stub_array_ = abstract_method_error_stub_array;
1060}
1061
Ian Rogers1cb0a1d2011-10-06 15:24:35 -07001062void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -07001063 CHECK(resolution_stub_array != NULL);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -07001064 CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array);
1065 resolution_stub_array_[type] = resolution_stub_array;
Ian Rogersad25ac52011-10-04 19:13:33 -07001066}
1067
Mathieu Chartier66f19252012-09-18 08:57:04 -07001068AbstractMethod* Runtime::CreateResolutionMethod() {
1069 Class* method_class = AbstractMethod::GetMethodClass();
Ian Rogers50b35e22012-10-04 10:09:15 -07001070 Thread* self = Thread::Current();
1071 SirtRef<AbstractMethod> method(self, down_cast<AbstractMethod*>(method_class->AllocObject(self)));
Ian Rogers19846512012-02-24 11:42:47 -08001072 method->SetDeclaringClass(method_class);
1073 // TODO: use a special method for resolution method saves
1074 method->SetDexMethodIndex(DexFile::kDexNoIndex16);
1075 ByteArray* unknown_resolution_stub = GetResolutionStubArray(kUnknownMethod);
1076 CHECK(unknown_resolution_stub != NULL);
1077 method->SetCode(unknown_resolution_stub->GetData());
1078 return method.get();
1079}
1080
Ian Rogers1f539342012-10-03 21:09:42 -07001081AbstractMethod* Runtime::CreateCalleeSaveMethod(InstructionSet instruction_set,
1082 CalleeSaveType type) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001083 Class* method_class = AbstractMethod::GetMethodClass();
Ian Rogers50b35e22012-10-04 10:09:15 -07001084 Thread* self = Thread::Current();
1085 SirtRef<AbstractMethod> method(self, down_cast<AbstractMethod*>(method_class->AllocObject(self)));
Ian Rogersff1ed472011-09-20 13:46:24 -07001086 method->SetDeclaringClass(method_class);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001087 // TODO: use a special method for callee saves
Ian Rogers19846512012-02-24 11:42:47 -08001088 method->SetDexMethodIndex(DexFile::kDexNoIndex16);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001089 method->SetCode(NULL);
Brian Carlstromae826982011-11-09 01:33:42 -08001090 if ((instruction_set == kThumb2) || (instruction_set == kArm)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001091 uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6) | (1 << art::arm::R7) |
1092 (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11);
1093 uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3);
1094 uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9);
jeffhaofa147e22012-10-12 17:03:32 -07001095 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills : 0) |
1096 (type == kSaveAll ? all_spills : 0) | (1 << art::arm::LR);
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001097 uint32_t fp_all_spills = (1 << art::arm::S0) | (1 << art::arm::S1) | (1 << art::arm::S2) |
1098 (1 << art::arm::S3) | (1 << art::arm::S4) | (1 << art::arm::S5) |
1099 (1 << art::arm::S6) | (1 << art::arm::S7) | (1 << art::arm::S8) |
1100 (1 << art::arm::S9) | (1 << art::arm::S10) | (1 << art::arm::S11) |
1101 (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) |
1102 (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) |
1103 (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) |
1104 (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) |
1105 (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) |
1106 (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
1107 (1 << art::arm::S30) | (1 << art::arm::S31);
1108 uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
1109 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
1110 __builtin_popcount(fp_spills) /* fprs */ +
1111 1 /* Method* */) * kPointerSize, kStackAlignment);
Ian Rogers15fdb8c2011-09-25 15:45:07 -07001112 method->SetFrameSizeInBytes(frame_size);
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001113 method->SetCoreSpillMask(core_spills);
1114 method->SetFpSpillMask(fp_spills);
jeffhao7fbee072012-08-24 17:56:54 -07001115 } else if (instruction_set == kMips) {
1116 uint32_t ref_spills = (1 << art::mips::S2) | (1 << art::mips::S3) | (1 << art::mips::S4) |
1117 (1 << art::mips::S5) | (1 << art::mips::S6) | (1 << art::mips::S7) |
1118 (1 << art::mips::FP);
1119 uint32_t arg_spills = (1 << art::mips::A1) | (1 << art::mips::A2) | (1 << art::mips::A3);
jeffhaofa147e22012-10-12 17:03:32 -07001120 uint32_t all_spills = (1 << art::mips::S0) | (1 << art::mips::S1);
jeffhao7fbee072012-08-24 17:56:54 -07001121 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills : 0) |
jeffhaofa147e22012-10-12 17:03:32 -07001122 (type == kSaveAll ? all_spills : 0) | (1 << art::mips::RA);
jeffhao7fbee072012-08-24 17:56:54 -07001123 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
jeffhao4eb68ed2012-10-17 16:41:07 -07001124 (type == kRefsAndArgs ? 0 : 5) /* reserve arg space */ +
jeffhao7fbee072012-08-24 17:56:54 -07001125 1 /* Method* */) * kPointerSize, kStackAlignment);
1126 method->SetFrameSizeInBytes(frame_size);
1127 method->SetCoreSpillMask(core_spills);
jeffhao07030602012-09-26 14:33:14 -07001128 method->SetFpSpillMask(0);
Brian Carlstromae826982011-11-09 01:33:42 -08001129 } else if (instruction_set == kX86) {
Ian Rogers7caad772012-03-30 01:07:54 -07001130 uint32_t ref_spills = (1 << art::x86::EBP) | (1 << art::x86::ESI) | (1 << art::x86::EDI);
1131 uint32_t arg_spills = (1 << art::x86::ECX) | (1 << art::x86::EDX) | (1 << art::x86::EBX);
1132 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills : 0) |
1133 (1 << art::x86::kNumberOfCpuRegisters); // fake return address callee save
1134 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
1135 1 /* Method* */) * kPointerSize, kStackAlignment);
1136 method->SetFrameSizeInBytes(frame_size);
1137 method->SetCoreSpillMask(core_spills);
Ian Rogersff1ed472011-09-20 13:46:24 -07001138 method->SetFpSpillMask(0);
1139 } else {
1140 UNIMPLEMENTED(FATAL);
1141 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001142 return method.get();
Ian Rogersff1ed472011-09-20 13:46:24 -07001143}
1144
Mathieu Chartier66f19252012-09-18 08:57:04 -07001145void Runtime::SetCalleeSaveMethod(AbstractMethod* method, CalleeSaveType type) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001146 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
Elliott Hughes225f5a12012-06-11 11:23:48 -07001147 callee_save_methods_[type] = method;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001148}
1149
jeffhao2692b572011-12-16 15:42:28 -08001150void Runtime::EnableMethodTracing(Trace* tracer) {
1151 CHECK(!IsMethodTracingActive());
1152 tracer_ = tracer;
1153}
1154
1155void Runtime::DisableMethodTracing() {
1156 CHECK(IsMethodTracingActive());
1157 delete tracer_;
1158 tracer_ = NULL;
1159}
1160
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001161const std::vector<const DexFile*>& Runtime::GetCompileTimeClassPath(jobject class_loader) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001162 if (class_loader == NULL) {
1163 return GetClassLinker()->GetBootClassPath();
1164 }
1165 CHECK(UseCompileTimeClassPath());
1166 CompileTimeClassPaths::const_iterator it = compile_time_class_paths_.find(class_loader);
1167 CHECK(it != compile_time_class_paths_.end());
1168 return it->second;
1169}
1170
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001171void Runtime::SetCompileTimeClassPath(jobject class_loader, std::vector<const DexFile*>& class_path) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001172 CHECK(!IsStarted());
1173 use_compile_time_class_path_ = true;
Elliott Hughesa0e18062012-04-13 15:59:59 -07001174 compile_time_class_paths_.Put(class_loader, class_path);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001175}
1176
Carl Shapiro1fb86202011-06-27 17:43:13 -07001177} // namespace art