blob: 61d93f8628552ecd8b20e4093c3e016c9facc020 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro1fb86202011-06-27 17:43:13 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "runtime.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070018
Brian Carlstromdbf05b72011-12-15 00:55:24 -080019#include <signal.h>
Elliott Hughesd06a6c72012-05-30 17:59:06 -070020#include <sys/syscall.h>
Brian Carlstromdbf05b72011-12-15 00:55:24 -080021
Elliott Hughesffe67362011-07-17 12:09:27 -070022#include <cstdio>
23#include <cstdlib>
Brian Carlstrom8a436592011-08-15 21:27:23 -070024#include <limits>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070025#include <vector>
Elliott Hughesffe67362011-07-17 12:09:27 -070026
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070027#include "class_linker.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070028#include "class_loader.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070029#include "constants_arm.h"
jeffhao7fbee072012-08-24 17:56:54 -070030#include "constants_mips.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070031#include "constants_x86.h"
Elliott Hughes3bb81562011-10-21 18:52:59 -070032#include "debugger.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070033#include "heap.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070034#include "image.h"
Elliott Hughescf4c6c42011-09-01 15:16:42 -070035#include "intern_table.h"
Elliott Hughesc5f7c912011-08-18 14:00:42 -070036#include "jni_internal.h"
Elliott Hughes32d6e1e2011-10-11 14:47:44 -070037#include "monitor.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070038#include "oat_file.h"
Elliott Hughes726079d2011-10-07 18:43:44 -070039#include "ScopedLocalRef.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070040#include "scoped_thread_state_change.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070041#include "signal_catcher.h"
Elliott Hughes457005c2012-04-16 13:54:25 -070042#include "signal_set.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070043#include "space.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070044#include "thread.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070045#include "thread_list.h"
jeffhaob5e81852012-03-12 11:15:45 -070046#include "trace.h"
Elliott Hughes3bb81562011-10-21 18:52:59 -070047#include "UniquePtr.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070048#include "verifier/method_verifier.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070049#include "well_known_classes.h"
Carl Shapiro61e019d2011-07-14 16:53:09 -070050
Logan Chien971bf3f2012-05-01 15:47:55 +080051#if defined(ART_USE_LLVM_COMPILER)
52#include "compiler_llvm/procedure_linkage_table.h"
53#endif
54
Elliott Hugheseac76672012-05-24 21:56:51 -070055#include "JniConstants.h" // Last to avoid LOG redefinition in ics-mr1-plus-art.
Elliott Hughes90a33692011-08-30 13:27:07 -070056
Carl Shapiro1fb86202011-06-27 17:43:13 -070057namespace art {
58
Carl Shapiro2ed144c2011-07-26 16:52:08 -070059Runtime* Runtime::instance_ = NULL;
60
Elliott Hughesdcc24742011-09-07 14:02:44 -070061Runtime::Runtime()
Elliott Hughesd9c67be2012-02-02 19:54:06 -080062 : is_compiler_(false),
63 is_zygote_(false),
Mathieu Chartier069387a2012-06-18 12:01:01 -070064 is_concurrent_gc_enabled_(true),
Elliott Hughes6cf23882012-06-15 15:42:07 -070065 default_stack_size_(0),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080066 heap_(NULL),
Elliott Hughesc33a32b2011-10-11 18:18:07 -070067 monitor_list_(NULL),
Elliott Hughesdcc24742011-09-07 14:02:44 -070068 thread_list_(NULL),
69 intern_table_(NULL),
70 class_linker_(NULL),
71 signal_catcher_(NULL),
72 java_vm_(NULL),
Elliott Hughes225f5a12012-06-11 11:23:48 -070073 pre_allocated_OutOfMemoryError_(NULL),
Brian Carlstrom16192862011-09-12 17:50:06 -070074 jni_stub_array_(NULL),
Brian Carlstrome24fa612011-09-29 00:53:55 -070075 abstract_method_error_stub_array_(NULL),
Ian Rogers19846512012-02-24 11:42:47 -080076 resolution_method_(NULL),
Ian Rogers7c3757f2012-02-15 17:18:53 -080077 system_class_loader_(NULL),
Ian Rogers120f1c72012-09-28 17:17:10 -070078 threads_being_born_(0),
79 shutdown_cond_(new ConditionVariable("Runtime shutdown")),
Elliott Hughes6b355752012-01-13 16:49:08 -080080 shutting_down_(false),
Ian Rogers120f1c72012-09-28 17:17:10 -070081 shutting_down_started_(false),
Elliott Hughesdcc24742011-09-07 14:02:44 -070082 started_(false),
Mathieu Chartier7664f5c2012-06-08 18:15:32 -070083 finished_starting_(false),
Elliott Hughesdcc24742011-09-07 14:02:44 -070084 vfprintf_(NULL),
85 exit_(NULL),
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070086 abort_(NULL),
jeffhao2692b572011-12-16 15:42:28 -080087 stats_enabled_(false),
Ian Rogers776ac1f2012-04-13 23:36:36 -070088 method_trace_(0),
89 method_trace_file_size_(0),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080090 tracer_(NULL),
Ian Rogers365c1022012-06-22 15:05:28 -070091 use_compile_time_class_path_(false),
92 main_thread_group_(NULL),
Logan Chien971bf3f2012-05-01 15:47:55 +080093 system_thread_group_(NULL)
94#if defined(ART_USE_LLVM_COMPILER)
95#if defined(__arm__)
96 , plt_(kArm)
97#elif defined(__mips__)
98 , plt_(kMips)
99#elif defined(__i386__)
100 , plt_(kX86)
101#endif
102#endif
103 {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700104 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
105 resolution_stub_array_[i] = NULL;
106 }
107 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
Elliott Hughes225f5a12012-06-11 11:23:48 -0700108 callee_save_methods_[i] = NULL;
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700109 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800110
111#if defined(ART_USE_LLVM_COMPILER)
112 CHECK(plt_.AllocateTable()) << "Failed to allocate PLT";
113#endif
Elliott Hughesdcc24742011-09-07 14:02:44 -0700114}
115
Carl Shapiro61e019d2011-07-14 16:53:09 -0700116Runtime::~Runtime() {
Ian Rogers120f1c72012-09-28 17:17:10 -0700117 Thread* self = Thread::Current();
118 {
119 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
120 shutting_down_started_ = true;
121 while (threads_being_born_ > 0) {
122 shutdown_cond_->Wait(self, *Locks::runtime_shutdown_lock_);
123 }
124 shutting_down_ = true;
125 }
Elliott Hughes6b355752012-01-13 16:49:08 -0800126
jeffhaob5e81852012-03-12 11:15:45 -0700127 if (IsMethodTracingActive()) {
128 Trace::Shutdown();
129 }
130
Mathieu Chartiera6399032012-06-11 18:49:50 -0700131 // Make sure to let the GC complete if it is running.
Ian Rogers120f1c72012-09-28 17:17:10 -0700132 heap_->WaitForConcurrentGcToComplete(self);
Mathieu Chartiera6399032012-06-11 18:49:50 -0700133
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700134 // Make sure our internal threads are dead before we start tearing down things they're using.
Elliott Hughese52e49b2012-04-02 16:05:44 -0700135 Dbg::StopJdwp();
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700136 delete signal_catcher_;
137
Elliott Hughes038a8062011-09-18 14:12:41 -0700138 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
Elliott Hughes93e74e82011-09-13 11:07:03 -0700139 delete thread_list_;
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700140 delete monitor_list_;
Elliott Hughes93e74e82011-09-13 11:07:03 -0700141
Carl Shapiro61e019d2011-07-14 16:53:09 -0700142 delete class_linker_;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800143 delete heap_;
Elliott Hughes0a1038b2012-06-14 16:24:17 -0700144 verifier::MethodVerifier::Shutdown();
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700145 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700146 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700147 Thread::Shutdown();
Elliott Hughes7c6169d2012-05-02 16:11:48 -0700148 QuasiAtomic::Shutdown();
149
Carl Shapiro4acf4642011-07-26 18:54:13 -0700150 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700151 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -0700152 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700153}
154
Elliott Hughescac6cc72011-11-03 20:31:21 -0700155static bool gAborting = false;
156
Elliott Hughese0918552011-10-28 17:18:29 -0700157struct AbortState {
158 void Dump(std::ostream& os) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700159 if (gAborting) {
160 os << "Runtime aborting --- recursively, so no thread-specific detail!\n";
161 return;
162 }
163 gAborting = true;
Elliott Hughese0918552011-10-28 17:18:29 -0700164 os << "Runtime aborting...\n";
Elliott Hughes6c7d2442012-02-01 18:40:47 -0800165 if (Runtime::Current() == NULL) {
166 os << "(Runtime does not yet exist!)\n";
167 return;
168 }
Elliott Hughese0918552011-10-28 17:18:29 -0700169 Thread* self = Thread::Current();
170 if (self == NULL) {
171 os << "(Aborting thread was not attached to runtime!)\n";
172 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700173 // TODO: we're aborting and the ScopedObjectAccess may attempt to acquire the mutator_lock_
174 // which may block indefinitely if there's a misbehaving thread holding it exclusively.
175 // The code below should be made robust to this.
176 ScopedObjectAccess soa(self);
Elliott Hughes899e7892012-01-24 14:57:32 -0800177 self->Dump(os);
178 if (self->IsExceptionPending()) {
179 os << "Pending " << PrettyTypeOf(self->GetException()) << " on thread:\n"
180 << self->GetException()->Dump();
181 }
Elliott Hughese0918552011-10-28 17:18:29 -0700182 }
183 }
184};
185
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700186void Runtime::Abort() {
Elliott Hughes131aef82012-01-27 11:53:35 -0800187 // Ensure that we don't have multiple threads trying to abort at once,
188 // which would result in significantly worse diagnostics.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700189 MutexLock mu(*Locks::abort_lock_);
Elliott Hughes131aef82012-01-27 11:53:35 -0800190
Elliott Hughesffe67362011-07-17 12:09:27 -0700191 // Get any pending output out of the way.
192 fflush(NULL);
193
194 // Many people have difficulty distinguish aborts from crashes,
195 // so be explicit.
Elliott Hughese0918552011-10-28 17:18:29 -0700196 AbortState state;
Elliott Hughes9ee5f9c2012-02-03 18:33:16 -0800197 LOG(INTERNAL_FATAL) << Dumpable<AbortState>(state);
Elliott Hughesffe67362011-07-17 12:09:27 -0700198
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700199 // Call the abort hook if we have one.
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700200 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700201 LOG(INTERNAL_FATAL) << "Calling abort hook...";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700202 Runtime::Current()->abort_();
203 // notreached
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700204 LOG(INTERNAL_FATAL) << "Unexpectedly returned from abort hook!";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700205 }
206
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700207#if defined(__BIONIC__)
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700208 // TODO: finish merging patches to fix abort(3) in bionic, then lose this!
209 // Bionic doesn't implement POSIX semantics for abort(3) in a multi-threaded
210 // process, so if we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -0700211 // receive SIGABRT. debuggerd dumps the stack trace of the main
212 // thread, whether or not that was the thread that failed. By
213 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -0700214 // fault in the current thread, and get a useful log from debuggerd.
Elliott Hughes81ff3182012-03-23 20:35:56 -0700215 // We can also trivially tell the difference between a crash and
Elliott Hughesffe67362011-07-17 12:09:27 -0700216 // a deliberate abort by looking at the fault address.
217 *reinterpret_cast<char*>(0xdeadd00d) = 38;
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700218#elif defined(__APPLE__)
219 // TODO: check that this actually gives good stack traces on the Mac!
220 pthread_kill(pthread_self(), SIGABRT);
221#else
222 // TODO: we ought to be able to use pthread_kill(3) here (or abort(3),
223 // which POSIX defines in terms of raise(3), which POSIX defines in terms
224 // of pthread_kill(3)). On Linux, though, libcorkscrew can't unwind through
225 // libpthread, which means the stacks we dump would be useless. Calling
226 // tgkill(2) directly avoids that.
227 syscall(__NR_tgkill, getpid(), GetTid(), SIGABRT);
228#endif
Elliott Hughesffe67362011-07-17 12:09:27 -0700229 // notreached
230}
231
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700232bool Runtime::PreZygoteFork() {
233 heap_->PreZygoteFork();
234 return true;
235}
236
Elliott Hughesbf86d042011-08-31 17:53:14 -0700237void Runtime::CallExitHook(jint status) {
238 if (exit_ != NULL) {
Elliott Hughes34e06962012-04-09 13:55:55 -0700239 ScopedThreadStateChange tsc(Thread::Current(), kNative);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700240 exit_(status);
241 LOG(WARNING) << "Exit hook returned instead of exiting!";
242 }
243}
244
Brian Carlstrom8a436592011-08-15 21:27:23 -0700245// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
246// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
247// [gG] gigabytes.
248//
249// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700250// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
251// of 1024.
252//
253// The spec says the -Xmx and -Xms options must be multiples of 1024. It
254// doesn't say anything about -Xss.
255//
256// Returns 0 (a useless size) if "s" is malformed or specifies a low or
257// non-evenly-divisible value.
258//
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800259size_t ParseMemoryOption(const char* s, size_t div) {
Brian Carlstrom8a436592011-08-15 21:27:23 -0700260 // strtoul accepts a leading [+-], which we don't want,
261 // so make sure our string starts with a decimal digit.
262 if (isdigit(*s)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700263 char* s2;
264 size_t val = strtoul(s, &s2, 10);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700265 if (s2 != s) {
266 // s2 should be pointing just after the number.
267 // If this is the end of the string, the user
268 // has specified a number of bytes. Otherwise,
269 // there should be exactly one more character
270 // that specifies a multiplier.
271 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700272 // The remainder of the string is either a single multiplier
273 // character, or nothing to indicate that the value is in
274 // bytes.
275 char c = *s2++;
276 if (*s2 == '\0') {
277 size_t mul;
278 if (c == '\0') {
279 mul = 1;
280 } else if (c == 'k' || c == 'K') {
281 mul = KB;
282 } else if (c == 'm' || c == 'M') {
283 mul = MB;
284 } else if (c == 'g' || c == 'G') {
285 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700286 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700287 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700288 return 0;
289 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700290
291 if (val <= std::numeric_limits<size_t>::max() / mul) {
292 val *= mul;
293 } else {
294 // Clamp to a multiple of 1024.
295 val = std::numeric_limits<size_t>::max() & ~(1024-1);
296 }
297 } else {
298 // There's more than one character after the numeric part.
299 return 0;
300 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700301 }
302 // The man page says that a -Xm value must be a multiple of 1024.
303 if (val % div == 0) {
304 return val;
305 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700306 }
307 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700308 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700309}
310
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800311size_t ParseIntegerOrDie(const std::string& s) {
312 std::string::size_type colon = s.find(':');
313 if (colon == std::string::npos) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700314 LOG(FATAL) << "Missing integer: " << s;
315 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800316 const char* begin = &s[colon + 1];
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700317 char* end;
318 size_t result = strtoul(begin, &end, 10);
319 if (begin == end || *end != '\0') {
320 LOG(FATAL) << "Failed to parse integer in: " << s;
321 }
322 return result;
323}
324
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700325Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700326 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800327 const char* boot_class_path_string = getenv("BOOTCLASSPATH");
328 if (boot_class_path_string != NULL) {
329 parsed->boot_class_path_string_ = boot_class_path_string;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700330 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800331 const char* class_path_string = getenv("CLASSPATH");
332 if (class_path_string != NULL) {
333 parsed->class_path_string_ = class_path_string;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700334 }
Elliott Hughes67d92002012-03-26 15:08:51 -0700335 // -Xcheck:jni is off by default for regular builds but on by default in debug builds.
336 parsed->check_jni_ = kIsDebugBuild;
Elliott Hughes85d15452011-09-16 17:33:01 -0700337
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700338 parsed->heap_initial_size_ = Heap::kInitialSize;
339 parsed->heap_maximum_size_ = Heap::kMaximumSize;
Elliott Hughes6cf23882012-06-15 15:42:07 -0700340 parsed->heap_growth_limit_ = 0; // 0 means no growth limit.
341 parsed->stack_size_ = 0; // 0 means default.
Brian Carlstromf734cf52011-08-17 16:28:14 -0700342
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800343 parsed->is_compiler_ = false;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700344 parsed->is_zygote_ = false;
Mathieu Chartier069387a2012-06-18 12:01:01 -0700345 parsed->is_concurrent_gc_enabled_ = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700346
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700347 parsed->jni_globals_max_ = 0;
Elliott Hughesfc861622011-10-17 17:57:47 -0700348 parsed->lock_profiling_threshold_ = 0;
349 parsed->hook_is_sensitive_thread_ = NULL;
350
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700351 parsed->hook_vfprintf_ = vfprintf;
352 parsed->hook_exit_ = exit;
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700353 parsed->hook_abort_ = NULL; // We don't call abort(3) by default; see Runtime::Abort.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700354
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800355// gLogVerbosity.class_linker = true; // TODO: don't check this in!
356// gLogVerbosity.compiler = true; // TODO: don't check this in!
357// gLogVerbosity.heap = true; // TODO: don't check this in!
358// gLogVerbosity.gc = true; // TODO: don't check this in!
359// gLogVerbosity.jdwp = true; // TODO: don't check this in!
360// gLogVerbosity.jni = true; // TODO: don't check this in!
361// gLogVerbosity.monitor = true; // TODO: don't check this in!
362// gLogVerbosity.startup = true; // TODO: don't check this in!
363// gLogVerbosity.third_party_jni = true; // TODO: don't check this in!
364// gLogVerbosity.threads = true; // TODO: don't check this in!
365
jeffhaob5e81852012-03-12 11:15:45 -0700366 parsed->method_trace_ = false;
367 parsed->method_trace_file_ = "/data/method-trace-file.bin";
368 parsed->method_trace_file_size_ = 10 * MB;
369
Brian Carlstrom8a436592011-08-15 21:27:23 -0700370 for (size_t i = 0; i < options.size(); ++i) {
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800371 const std::string option(options[i].first);
Brian Carlstrom0796af02011-10-12 14:31:45 -0700372 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700373 LOG(INFO) << "option[" << i << "]=" << option;
374 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800375 if (StartsWith(option, "-Xbootclasspath:")) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800376 parsed->boot_class_path_string_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700377 } else if (option == "-classpath" || option == "-cp") {
378 // TODO: support -Djava.class.path
379 i++;
380 if (i == options.size()) {
381 // TODO: usage
382 LOG(FATAL) << "Missing required class path value for " << option;
383 return NULL;
384 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800385 const StringPiece& value = options[i].first;
386 parsed->class_path_string_ = value.data();
387 } else if (option == "bootclasspath") {
388 parsed->boot_class_path_
389 = reinterpret_cast<const std::vector<const DexFile*>*>(options[i].second);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800390 } else if (StartsWith(option, "-Ximage:")) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800391 parsed->image_ = option.substr(strlen("-Ximage:")).data();
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800392 } else if (StartsWith(option, "-Xcheck:jni")) {
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700393 parsed->check_jni_ = true;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800394 } else if (StartsWith(option, "-Xrunjdwp:") || StartsWith(option, "-agentlib:jdwp=")) {
395 std::string tail(option.substr(option[1] == 'X' ? 10 : 15));
Elliott Hughes3bb81562011-10-21 18:52:59 -0700396 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
397 LOG(FATAL) << "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
398 << "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n";
399 return NULL;
400 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800401 } else if (StartsWith(option, "-Xms")) {
402 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).c_str(), 1024);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700403 if (size == 0) {
404 if (ignore_unrecognized) {
405 continue;
406 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700407 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700408 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700409 return NULL;
410 }
411 parsed->heap_initial_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800412 } else if (StartsWith(option, "-Xmx")) {
413 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).c_str(), 1024);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700414 if (size == 0) {
415 if (ignore_unrecognized) {
416 continue;
417 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700418 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700419 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700420 return NULL;
421 }
422 parsed->heap_maximum_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800423 } else if (StartsWith(option, "-XX:HeapGrowthLimit=")) {
424 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).c_str(), 1024);
jeffhaoc1160702011-10-27 15:48:45 -0700425 if (size == 0) {
426 if (ignore_unrecognized) {
427 continue;
428 }
429 // TODO: usage
430 LOG(FATAL) << "Failed to parse " << option;
431 return NULL;
432 }
433 parsed->heap_growth_limit_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800434 } else if (StartsWith(option, "-Xss")) {
435 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).c_str(), 1);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700436 if (size == 0) {
437 if (ignore_unrecognized) {
438 continue;
439 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700440 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700441 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700442 return NULL;
443 }
444 parsed->stack_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800445 } else if (StartsWith(option, "-D")) {
446 parsed->properties_.push_back(option.substr(strlen("-D")));
447 } else if (StartsWith(option, "-Xjnitrace:")) {
448 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:"));
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700449 } else if (option == "compiler") {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800450 parsed->is_compiler_ = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700451 } else if (option == "-Xzygote") {
452 parsed->is_zygote_ = true;
Mathieu Chartier069387a2012-06-18 12:01:01 -0700453 } else if (StartsWith(option, "-Xgc:")) {
454 std::vector<std::string> gc_options;
455 Split(option.substr(strlen("-Xgc:")), ',', gc_options);
456 for (size_t i = 0; i < gc_options.size(); ++i) {
457 if (gc_options[i] == "noconcurrent") {
458 parsed->is_concurrent_gc_enabled_ = false;
459 } else if (gc_options[i] == "concurrent") {
460 parsed->is_concurrent_gc_enabled_ = true;
461 } else {
462 LOG(WARNING) << "Ignoring unknown -Xgc option: " << gc_options[i];
463 }
464 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800465 } else if (StartsWith(option, "-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700466 std::vector<std::string> verbose_options;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800467 Split(option.substr(strlen("-verbose:")), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700468 for (size_t i = 0; i < verbose_options.size(); ++i) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800469 if (verbose_options[i] == "class") {
470 gLogVerbosity.class_linker = true;
471 } else if (verbose_options[i] == "compiler") {
472 gLogVerbosity.compiler = true;
473 } else if (verbose_options[i] == "heap") {
474 gLogVerbosity.heap = true;
475 } else if (verbose_options[i] == "gc") {
476 gLogVerbosity.gc = true;
477 } else if (verbose_options[i] == "jdwp") {
478 gLogVerbosity.jdwp = true;
479 } else if (verbose_options[i] == "jni") {
480 gLogVerbosity.jni = true;
481 } else if (verbose_options[i] == "monitor") {
482 gLogVerbosity.monitor = true;
483 } else if (verbose_options[i] == "startup") {
484 gLogVerbosity.startup = true;
485 } else if (verbose_options[i] == "third-party-jni") {
486 gLogVerbosity.third_party_jni = true;
487 } else if (verbose_options[i] == "threads") {
488 gLogVerbosity.threads = true;
489 } else {
490 LOG(WARNING) << "Ignoring unknown -verbose option: " << verbose_options[i];
491 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700492 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800493 } else if (StartsWith(option, "-Xjnigreflimit:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700494 parsed->jni_globals_max_ = ParseIntegerOrDie(option);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800495 } else if (StartsWith(option, "-Xlockprofthreshold:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700496 parsed->lock_profiling_threshold_ = ParseIntegerOrDie(option);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800497 } else if (StartsWith(option, "-Xstacktracefile:")) {
Elliott Hughes206c20d2012-06-28 11:05:37 -0700498 parsed->stack_trace_file_ = option.substr(strlen("-Xstacktracefile:"));
Elliott Hughesfc861622011-10-17 17:57:47 -0700499 } else if (option == "sensitiveThread") {
Ian Rogers1b09b092012-08-20 15:35:52 -0700500 parsed->hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(const_cast<void*>(options[i].second));
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700501 } else if (option == "vfprintf") {
Ian Rogers1b09b092012-08-20 15:35:52 -0700502 parsed->hook_vfprintf_ =
503 reinterpret_cast<int (*)(FILE *, const char*, va_list)>(const_cast<void*>(options[i].second));
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700504 } else if (option == "exit") {
Ian Rogers1b09b092012-08-20 15:35:52 -0700505 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(const_cast<void*>(options[i].second));
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700506 } else if (option == "abort") {
Ian Rogers1b09b092012-08-20 15:35:52 -0700507 parsed->hook_abort_ = reinterpret_cast<void(*)()>(const_cast<void*>(options[i].second));
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700508 } else if (option == "host-prefix") {
509 parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second);
Elliott Hughesb3e66df2012-01-12 14:49:18 -0800510 } else if (option == "-Xgenregmap" || option == "-Xgc:precise") {
511 // We silently ignore these for backwards compatibility.
jeffhaob5e81852012-03-12 11:15:45 -0700512 } else if (option == "-Xmethod-trace") {
513 parsed->method_trace_ = true;
514 } else if (StartsWith(option, "-Xmethod-trace-file:")) {
515 parsed->method_trace_file_ = option.substr(strlen("-Xmethod-trace-file:"));
516 } else if (StartsWith(option, "-Xmethod-trace-file-size:")) {
517 parsed->method_trace_file_size_ = ParseIntegerOrDie(option);
Elliott Hughese119a362012-05-22 17:37:06 -0700518 } else if (option == "-Xprofile:threadcpuclock") {
519 Trace::SetDefaultClockSource(kProfilerClockSourceThreadCpu);
520 } else if (option == "-Xprofile:wallclock") {
521 Trace::SetDefaultClockSource(kProfilerClockSourceWall);
522 } else if (option == "-Xprofile:dualclock") {
523 Trace::SetDefaultClockSource(kProfilerClockSourceDual);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700524 } else {
525 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700526 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700527 LOG(ERROR) << "Unrecognized option " << option;
528 // TODO: this should exit, but for now tolerate unknown options
529 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700530 }
531 }
532 }
533
Brian Carlstrom223f20f2012-02-04 23:06:55 -0800534 if (!parsed->is_compiler_ && parsed->image_.empty()) {
535 parsed->image_ += GetAndroidRoot();
536 parsed->image_ += "/framework/boot.art";
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700537 }
jeffhaoc1160702011-10-27 15:48:45 -0700538 if (parsed->heap_growth_limit_ == 0) {
539 parsed->heap_growth_limit_ = parsed->heap_maximum_size_;
540 }
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700541
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700542 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700543}
544
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700545bool Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700546 // TODO: acquire a static mutex on Runtime to avoid racing.
547 if (Runtime::instance_ != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700548 return false;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700549 }
Ian Rogersb726dcb2012-09-05 08:57:23 -0700550 Locks::Init();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700551 instance_ = new Runtime;
552 if (!instance_->Init(options, ignore_unrecognized)) {
553 delete instance_;
554 instance_ = NULL;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700555 return false;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700556 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700557 return true;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700558}
Elliott Hughes0af55432011-08-17 18:37:28 -0700559
Ian Rogers365c1022012-06-22 15:05:28 -0700560static void CreateSystemClassLoader() {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800561 if (Runtime::Current()->UseCompileTimeClassPath()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700562 return;
563 }
564
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700565 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700566
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700567 Class* class_loader_class = soa.Decode<Class*>(WellKnownClasses::java_lang_ClassLoader);
Ian Rogers365c1022012-06-22 15:05:28 -0700568 CHECK(Runtime::Current()->GetClassLinker()->EnsureInitialized(class_loader_class, true, true));
Brian Carlstromaded5f72011-10-07 17:15:04 -0700569
Mathieu Chartier66f19252012-09-18 08:57:04 -0700570 AbstractMethod* getSystemClassLoader = class_loader_class->FindDirectMethod("getSystemClassLoader", "()Ljava/lang/ClassLoader;");
Brian Carlstromdf143242011-10-10 18:05:34 -0700571 CHECK(getSystemClassLoader != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700572
Ian Rogers365c1022012-06-22 15:05:28 -0700573 ClassLoader* class_loader =
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700574 down_cast<ClassLoader*>(InvokeWithJValues(soa, NULL, getSystemClassLoader, NULL).GetL());
Ian Rogers365c1022012-06-22 15:05:28 -0700575 CHECK(class_loader != NULL);
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500576
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700577 soa.Self()->SetClassLoaderOverride(class_loader);
Ian Rogers365c1022012-06-22 15:05:28 -0700578
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700579 Class* thread_class = soa.Decode<Class*>(WellKnownClasses::java_lang_Thread);
Ian Rogers365c1022012-06-22 15:05:28 -0700580 CHECK(Runtime::Current()->GetClassLinker()->EnsureInitialized(thread_class, true, true));
581
582 Field* contextClassLoader = thread_class->FindDeclaredInstanceField("contextClassLoader",
583 "Ljava/lang/ClassLoader;");
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500584 CHECK(contextClassLoader != NULL);
Ian Rogers365c1022012-06-22 15:05:28 -0700585
Mathieu Chartierdbe6f462012-09-25 16:54:50 -0700586 contextClassLoader->SetObject(soa.Decode<Object*>(soa.Self()->GetPeer()), class_loader);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700587}
588
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700589void Runtime::Start() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800590 VLOG(startup) << "Runtime::Start entering";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700591
592 CHECK(host_prefix_.empty()) << host_prefix_;
593
Elliott Hughes225f5a12012-06-11 11:23:48 -0700594 // Relocate the OatFiles (ELF images).
Logan Chien0c717dd2012-03-28 18:31:07 +0800595 class_linker_->RelocateExecutable();
596
Elliott Hughes225f5a12012-06-11 11:23:48 -0700597 // Pre-allocate an OutOfMemoryError for the double-OOME case.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700598 Thread* self = Thread::Current();
Elliott Hughes225f5a12012-06-11 11:23:48 -0700599 self->ThrowNewException("Ljava/lang/OutOfMemoryError;",
600 "OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack available");
601 pre_allocated_OutOfMemoryError_ = self->GetException();
602 self->ClearException();
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700603
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700604 // Restore main thread state to kNative as expected by native code.
605 self->TransitionFromRunnableToSuspended(kNative);
606
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700607 started_ = true;
608
Brian Carlstromae826982011-11-09 01:33:42 -0800609 // InitNativeMethods needs to be after started_ so that the classes
610 // it touches will have methods linked to the oat file if necessary.
611 InitNativeMethods();
612
Ian Rogers365c1022012-06-22 15:05:28 -0700613 // Initialize well known thread group values that may be accessed threads while attaching.
614 InitThreadGroups(self);
615
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500616 Thread::FinishStartup();
617
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700618 if (!is_zygote_) {
619 DidForkFromZygote();
620 }
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700621
Elliott Hughes85d15452011-09-16 17:33:01 -0700622 StartDaemonThreads();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700623
Brian Carlstromaded5f72011-10-07 17:15:04 -0700624 CreateSystemClassLoader();
625
Elliott Hughes225f5a12012-06-11 11:23:48 -0700626 self->GetJniEnv()->locals.AssertEmpty();
Elliott Hughes726079d2011-10-07 18:43:44 -0700627
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800628 VLOG(startup) << "Runtime::Start exiting";
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700629
630 finished_starting_ = true;
Elliott Hughes85d15452011-09-16 17:33:01 -0700631}
632
Ian Rogers120f1c72012-09-28 17:17:10 -0700633void Runtime::EndThreadBirth() EXCLUSIVE_LOCKS_REQUIRED(Locks::runtime_shutdown_lock_) {
634 DCHECK_GT(threads_being_born_, 0U);
635 threads_being_born_--;
636 if (shutting_down_started_ && threads_being_born_ == 0) {
637 shutdown_cond_->Broadcast();
638 }
639}
640
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700641void Runtime::DidForkFromZygote() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700642 is_zygote_ = false;
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700643
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700644 StartSignalCatcher();
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700645
646 // Start the JDWP thread. If the command-line debugger flags specified "suspend=y",
647 // this will pause the runtime, so we probably want this to come last.
648 Dbg::StartJdwp();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700649}
650
651void Runtime::StartSignalCatcher() {
652 if (!is_zygote_) {
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700653 signal_catcher_ = new SignalCatcher(stack_trace_file_);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700654 }
655}
656
Elliott Hughes85d15452011-09-16 17:33:01 -0700657void Runtime::StartDaemonThreads() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800658 VLOG(startup) << "Runtime::StartDaemonThreads entering";
Elliott Hughes85d15452011-09-16 17:33:01 -0700659
Elliott Hughes719b3232011-09-25 17:42:19 -0700660 Thread* self = Thread::Current();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700661
662 // Must be in the kNative state for calling native methods.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700663 {
Ian Rogersb726dcb2012-09-05 08:57:23 -0700664 MutexLock mu(*Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700665 CHECK_EQ(self->GetState(), kNative);
666 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700667
Elliott Hughes719b3232011-09-25 17:42:19 -0700668 JNIEnv* env = self->GetJniEnv();
Elliott Hugheseac76672012-05-24 21:56:51 -0700669 env->CallStaticVoidMethod(WellKnownClasses::java_lang_Daemons, WellKnownClasses::java_lang_Daemons_start);
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800670 CHECK(!env->ExceptionCheck());
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700671
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800672 VLOG(startup) << "Runtime::StartDaemonThreads exiting";
Carl Shapiro61e019d2011-07-14 16:53:09 -0700673}
674
Elliott Hughes0af55432011-08-17 18:37:28 -0700675bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700676 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700677
Elliott Hughes90a33692011-08-30 13:27:07 -0700678 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
679 if (options.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700680 LOG(ERROR) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700681 return false;
682 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800683 VLOG(startup) << "Runtime::Init -verbose:startup enabled";
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700684
Elliott Hughes7c6169d2012-05-02 16:11:48 -0700685 QuasiAtomic::Startup();
686
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700687 SetJniGlobalsMax(options->jni_globals_max_);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800688 Monitor::Init(options->lock_profiling_threshold_, options->hook_is_sensitive_thread_);
Elliott Hughes32d6e1e2011-10-11 14:47:44 -0700689
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700690 host_prefix_ = options->host_prefix_;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800691 boot_class_path_string_ = options->boot_class_path_string_;
692 class_path_string_ = options->class_path_string_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700693 properties_ = options->properties_;
694
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800695 is_compiler_ = options->is_compiler_;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700696 is_zygote_ = options->is_zygote_;
Mathieu Chartier069387a2012-06-18 12:01:01 -0700697 is_concurrent_gc_enabled_ = options->is_concurrent_gc_enabled_;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700698
Elliott Hughes0af55432011-08-17 18:37:28 -0700699 vfprintf_ = options->hook_vfprintf_;
700 exit_ = options->hook_exit_;
701 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700702
Elliott Hughesbe759c62011-09-08 19:38:21 -0700703 default_stack_size_ = options->stack_size_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700704 stack_trace_file_ = options->stack_trace_file_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700705
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700706 monitor_list_ = new MonitorList;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800707 thread_list_ = new ThreadList;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700708 intern_table_ = new InternTable;
709
Elliott Hughes0a1038b2012-06-14 16:24:17 -0700710 verifier::MethodVerifier::Init();
Logan Chiendd361c92012-04-10 23:40:37 +0800711
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800712 heap_ = new Heap(options->heap_initial_size_,
713 options->heap_growth_limit_,
714 options->heap_maximum_size_,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700715 options->image_,
716 options->is_concurrent_gc_enabled_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700717
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700718 BlockSignals();
Elliott Hughes457005c2012-04-16 13:54:25 -0700719 InitPlatformSignalHandlers();
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700720
Elliott Hughesa0957642011-09-02 14:27:33 -0700721 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700722
Elliott Hughesbe759c62011-09-08 19:38:21 -0700723 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700724
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700725 // ClassLinker needs an attached thread, but we can't fully attach a thread
Elliott Hughes462c9442012-03-23 18:47:50 -0700726 // without creating objects. We can't supply a thread group yet; it will be fixed later.
Ian Rogers120f1c72012-09-28 17:17:10 -0700727 Thread* self = Thread::Attach("main", false, NULL);
728 CHECK(self != NULL);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700729
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700730 // Set us to runnable so tools using a runtime can allocate and GC by default
Ian Rogers120f1c72012-09-28 17:17:10 -0700731 self->TransitionFromSuspendedToRunnable();
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700732
Ian Rogers141d6222012-04-05 12:23:06 -0700733 // Now we're attached, we can take the heap lock and validate the heap.
734 GetHeap()->EnableObjectValidation();
735
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800736 CHECK_GE(GetHeap()->GetSpaces().size(), 1U);
737 if (GetHeap()->GetSpaces()[0]->IsImageSpace()) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800738 class_linker_ = ClassLinker::CreateFromImage(intern_table_);
739 } else {
740 CHECK(options->boot_class_path_ != NULL);
741 CHECK_NE(options->boot_class_path_->size(), 0U);
742 class_linker_ = ClassLinker::CreateFromCompiler(*options->boot_class_path_, intern_table_);
743 }
744 CHECK(class_linker_ != NULL);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700745
jeffhaob5e81852012-03-12 11:15:45 -0700746 method_trace_ = options->method_trace_;
747 method_trace_file_ = options->method_trace_file_;
748 method_trace_file_size_ = options->method_trace_file_size_;
749
750 if (options->method_trace_) {
751 Trace::Start(options->method_trace_file_.c_str(), -1, options->method_trace_file_size_, 0, false);
752 }
753
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800754 VLOG(startup) << "Runtime::Init exiting";
Carl Shapiro1fb86202011-06-27 17:43:13 -0700755 return true;
756}
757
Elliott Hughes038a8062011-09-18 14:12:41 -0700758void Runtime::InitNativeMethods() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800759 VLOG(startup) << "Runtime::InitNativeMethods entering";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700760 Thread* self = Thread::Current();
761 JNIEnv* env = self->GetJniEnv();
762
Elliott Hughes418d20f2011-09-22 14:00:39 -0700763 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700764 {
Ian Rogersb726dcb2012-09-05 08:57:23 -0700765 MutexLock mu(*Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700766 CHECK_EQ(self->GetState(), kNative);
767 }
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700768
Elliott Hughes418d20f2011-09-22 14:00:39 -0700769 // First set up JniConstants, which is used by both the runtime's built-in native
770 // methods and libcore.
Elliott Hughesfea966e2011-09-22 10:26:20 -0700771 JniConstants::init(env);
Elliott Hugheseac76672012-05-24 21:56:51 -0700772 WellKnownClasses::Init(env);
Elliott Hughesfea966e2011-09-22 10:26:20 -0700773
Elliott Hughes418d20f2011-09-22 14:00:39 -0700774 // Then set up the native methods provided by the runtime itself.
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700775 RegisterRuntimeNativeMethods(env);
776
Elliott Hughes418d20f2011-09-22 14:00:39 -0700777 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
778 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
779 // the library that implements System.loadLibrary!
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700780 {
781 std::string mapped_name(StringPrintf(OS_SHARED_LIB_FORMAT_STR, "javacore"));
782 std::string reason;
783 self->TransitionFromSuspendedToRunnable();
784 if (!instance_->java_vm_->LoadNativeLibrary(mapped_name, NULL, reason)) {
785 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": " << reason;
786 }
787 self->TransitionFromRunnableToSuspended(kNative);
788 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800789 VLOG(startup) << "Runtime::InitNativeMethods exiting";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700790}
791
Ian Rogers365c1022012-06-22 15:05:28 -0700792void Runtime::InitThreadGroups(Thread* self) {
793 JNIEnvExt* env = self->GetJniEnv();
794 ScopedJniEnvLocalRefState env_state(env);
795 main_thread_group_ =
796 env->NewGlobalRef(env->GetStaticObjectField(WellKnownClasses::java_lang_ThreadGroup,
797 WellKnownClasses::java_lang_ThreadGroup_mainThreadGroup));
Brian Carlstrom034f76b2012-08-01 15:51:58 -0700798 CHECK(main_thread_group_ != NULL || IsCompiler());
Ian Rogers365c1022012-06-22 15:05:28 -0700799 system_thread_group_ =
800 env->NewGlobalRef(env->GetStaticObjectField(WellKnownClasses::java_lang_ThreadGroup,
801 WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup));
Brian Carlstrom034f76b2012-08-01 15:51:58 -0700802 CHECK(system_thread_group_ != NULL || IsCompiler());
803}
804
805jobject Runtime::GetMainThreadGroup() const {
806 CHECK(main_thread_group_ != NULL || IsCompiler());
807 return main_thread_group_;
808}
809
810jobject Runtime::GetSystemThreadGroup() const {
811 CHECK(system_thread_group_ != NULL || IsCompiler());
812 return system_thread_group_;
Ian Rogers365c1022012-06-22 15:05:28 -0700813}
814
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700815void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
816#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Ian Rogers5167c972012-02-03 10:41:20 -0800817 // Register Throwable first so that registration of other native methods can throw exceptions
818 REGISTER(register_java_lang_Throwable);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700819 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700820 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700821 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700822 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700823 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700824 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700825 REGISTER(register_java_lang_Object);
826 REGISTER(register_java_lang_Runtime);
827 REGISTER(register_java_lang_String);
828 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700829 REGISTER(register_java_lang_Thread);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700830 REGISTER(register_java_lang_VMClassLoader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700831 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700832 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700833 REGISTER(register_java_lang_reflect_Field);
834 REGISTER(register_java_lang_reflect_Method);
Jesse Wilson95caa792011-10-12 18:14:17 -0400835 REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700836 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700837 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700838 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700839 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700840#undef REGISTER
841}
842
Elliott Hughesc967f782012-04-16 10:23:15 -0700843void Runtime::DumpForSigQuit(std::ostream& os) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700844 GetClassLinker()->DumpForSigQuit(os);
845 GetInternTable()->DumpForSigQuit(os);
Elliott Hughesae80b492012-04-24 10:43:17 -0700846 GetJavaVM()->DumpForSigQuit(os);
Elliott Hughesc967f782012-04-16 10:23:15 -0700847 GetHeap()->DumpForSigQuit(os);
Elliott Hughes42ee1422011-09-06 12:33:32 -0700848 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700849
Elliott Hughesc967f782012-04-16 10:23:15 -0700850 thread_list_->DumpForSigQuit(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700851}
852
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800853void Runtime::DumpLockHolders(std::ostream& os) {
Ian Rogersb726dcb2012-09-05 08:57:23 -0700854 uint64_t mutator_lock_owner = Locks::mutator_lock_->GetExclusiveOwnerTid();
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800855 pid_t thread_list_lock_owner = GetThreadList()->GetLockOwner();
856 pid_t classes_lock_owner = GetClassLinker()->GetClassesLockOwner();
857 pid_t dex_lock_owner = GetClassLinker()->GetDexLockOwner();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700858 if ((thread_list_lock_owner | classes_lock_owner | dex_lock_owner) != 0) {
859 os << "Mutator lock exclusive owner tid: " << mutator_lock_owner << "\n"
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800860 << "ThreadList lock owner tid: " << thread_list_lock_owner << "\n"
861 << "ClassLinker classes lock owner tid: " << classes_lock_owner << "\n"
862 << "ClassLinker dex lock owner tid: " << dex_lock_owner << "\n";
863 }
864}
865
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700866void Runtime::SetStatsEnabled(bool new_state) {
867 if (new_state == true) {
868 GetStats()->Clear(~0);
869 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
870 Thread::Current()->GetStats()->Clear(~0);
871 }
872 stats_enabled_ = new_state;
873}
874
875void Runtime::ResetStats(int kinds) {
876 GetStats()->Clear(kinds & 0xffff);
877 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
878 Thread::Current()->GetStats()->Clear(kinds >> 16);
879}
880
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700881int32_t Runtime::GetStat(int kind) {
882 RuntimeStats* stats;
883 if (kind < (1<<16)) {
884 stats = GetStats();
885 } else {
886 stats = Thread::Current()->GetStats();
887 kind >>= 16;
888 }
889 switch (kind) {
890 case KIND_ALLOCATED_OBJECTS:
891 return stats->allocated_objects;
892 case KIND_ALLOCATED_BYTES:
893 return stats->allocated_bytes;
894 case KIND_FREED_OBJECTS:
895 return stats->freed_objects;
896 case KIND_FREED_BYTES:
897 return stats->freed_bytes;
898 case KIND_GC_INVOCATIONS:
899 return stats->gc_for_alloc_count;
900 case KIND_CLASS_INIT_COUNT:
901 return stats->class_init_count;
902 case KIND_CLASS_INIT_TIME:
903 // Convert ns to us, reduce to 32 bits.
Elliott Hughes398f64b2012-03-26 18:05:48 -0700904 return static_cast<int>(stats->class_init_time_ns / 1000);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700905 case KIND_EXT_ALLOCATED_OBJECTS:
906 case KIND_EXT_ALLOCATED_BYTES:
907 case KIND_EXT_FREED_OBJECTS:
908 case KIND_EXT_FREED_BYTES:
909 return 0; // backward compatibility
910 default:
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700911 LOG(FATAL) << "Unknown statistic " << kind;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700912 return -1; // unreachable
913 }
914}
915
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700916void Runtime::BlockSignals() {
Elliott Hughes457005c2012-04-16 13:54:25 -0700917 SignalSet signals;
918 signals.Add(SIGPIPE);
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700919 // SIGQUIT is used to dump the runtime's state (including stack traces).
Elliott Hughes457005c2012-04-16 13:54:25 -0700920 signals.Add(SIGQUIT);
Elliott Hughes08795042012-04-03 14:48:52 -0700921 // SIGUSR1 is used to initiate a GC.
Elliott Hughes457005c2012-04-16 13:54:25 -0700922 signals.Add(SIGUSR1);
923 signals.Block();
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700924}
925
Ian Rogers120f1c72012-09-28 17:17:10 -0700926bool Runtime::AttachCurrentThread(const char* thread_name, bool as_daemon, jobject thread_group) {
927 bool success = Thread::Attach(thread_name, as_daemon, thread_group) != NULL;
Elliott Hughes22869a92012-03-27 14:08:24 -0700928 if (thread_name == NULL) {
929 LOG(WARNING) << *Thread::Current() << " attached without supplying a name";
930 }
Ian Rogers120f1c72012-09-28 17:17:10 -0700931 return success;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700932}
933
Elliott Hughesd92bec42011-09-02 17:04:36 -0700934void Runtime::DetachCurrentThread() {
Brian Carlstrom4d571432012-05-16 00:21:41 -0700935 Thread* self = Thread::Current();
936 if (self == NULL) {
937 LOG(FATAL) << "attempting to detach thread that is not attached";
938 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700939 if (self->HasManagedStack()) {
Elliott Hughes22869a92012-03-27 14:08:24 -0700940 LOG(FATAL) << *Thread::Current() << " attempting to detach while still running code";
941 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700942 thread_list_->Unregister(self);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700943}
944
Brian Carlstrome24fa612011-09-29 00:53:55 -0700945void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700946 Dbg::VisitRoots(visitor, arg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700947 class_linker_->VisitRoots(visitor, arg);
948 intern_table_->VisitRoots(visitor, arg);
949 java_vm_->VisitRoots(visitor, arg);
950 thread_list_->VisitRoots(visitor, arg);
Elliott Hughes225f5a12012-06-11 11:23:48 -0700951 if (pre_allocated_OutOfMemoryError_ != NULL) {
952 visitor(pre_allocated_OutOfMemoryError_, arg);
953 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700954 visitor(jni_stub_array_, arg);
955 visitor(abstract_method_error_stub_array_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700956 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
957 visitor(resolution_stub_array_[i], arg);
958 }
Ian Rogers19846512012-02-24 11:42:47 -0800959 visitor(resolution_method_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700960 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
Elliott Hughes225f5a12012-06-11 11:23:48 -0700961 visitor(callee_save_methods_[i], arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700962 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700963}
964
Ian Rogers169c9a72011-11-13 20:13:17 -0800965void Runtime::SetJniDlsymLookupStub(ByteArray* jni_stub_array) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700966 CHECK(jni_stub_array != NULL) << " jni_stub_array=" << jni_stub_array;
967 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array)
968 << "jni_stub_array_=" << jni_stub_array_ << " jni_stub_array=" << jni_stub_array;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700969 jni_stub_array_ = jni_stub_array;
970}
971
Brian Carlstrome24fa612011-09-29 00:53:55 -0700972void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) {
973 CHECK(abstract_method_error_stub_array != NULL);
974 CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array);
975 abstract_method_error_stub_array_ = abstract_method_error_stub_array;
976}
977
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700978void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700979 CHECK(resolution_stub_array != NULL);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700980 CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array);
981 resolution_stub_array_[type] = resolution_stub_array;
Ian Rogersad25ac52011-10-04 19:13:33 -0700982}
983
Mathieu Chartier66f19252012-09-18 08:57:04 -0700984AbstractMethod* Runtime::CreateResolutionMethod() {
985 Class* method_class = AbstractMethod::GetMethodClass();
986 SirtRef<AbstractMethod> method(down_cast<AbstractMethod*>(method_class->AllocObject()));
Ian Rogers19846512012-02-24 11:42:47 -0800987 method->SetDeclaringClass(method_class);
988 // TODO: use a special method for resolution method saves
989 method->SetDexMethodIndex(DexFile::kDexNoIndex16);
990 ByteArray* unknown_resolution_stub = GetResolutionStubArray(kUnknownMethod);
991 CHECK(unknown_resolution_stub != NULL);
992 method->SetCode(unknown_resolution_stub->GetData());
993 return method.get();
994}
995
Mathieu Chartier66f19252012-09-18 08:57:04 -0700996AbstractMethod* Runtime::CreateCalleeSaveMethod(InstructionSet instruction_set, CalleeSaveType type) {
997 Class* method_class = AbstractMethod::GetMethodClass();
998 SirtRef<AbstractMethod> method(down_cast<AbstractMethod*>(method_class->AllocObject()));
Ian Rogersff1ed472011-09-20 13:46:24 -0700999 method->SetDeclaringClass(method_class);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001000 // TODO: use a special method for callee saves
Ian Rogers19846512012-02-24 11:42:47 -08001001 method->SetDexMethodIndex(DexFile::kDexNoIndex16);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001002 method->SetCode(NULL);
Brian Carlstromae826982011-11-09 01:33:42 -08001003 if ((instruction_set == kThumb2) || (instruction_set == kArm)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001004 uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6) | (1 << art::arm::R7) |
1005 (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11);
1006 uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3);
1007 uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9);
1008 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills :0) |
1009 (type == kSaveAll ? all_spills :0) | (1 << art::arm::LR);
1010 uint32_t fp_all_spills = (1 << art::arm::S0) | (1 << art::arm::S1) | (1 << art::arm::S2) |
1011 (1 << art::arm::S3) | (1 << art::arm::S4) | (1 << art::arm::S5) |
1012 (1 << art::arm::S6) | (1 << art::arm::S7) | (1 << art::arm::S8) |
1013 (1 << art::arm::S9) | (1 << art::arm::S10) | (1 << art::arm::S11) |
1014 (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) |
1015 (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) |
1016 (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) |
1017 (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) |
1018 (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) |
1019 (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
1020 (1 << art::arm::S30) | (1 << art::arm::S31);
1021 uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
1022 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
1023 __builtin_popcount(fp_spills) /* fprs */ +
1024 1 /* Method* */) * kPointerSize, kStackAlignment);
Ian Rogers15fdb8c2011-09-25 15:45:07 -07001025 method->SetFrameSizeInBytes(frame_size);
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001026 method->SetCoreSpillMask(core_spills);
1027 method->SetFpSpillMask(fp_spills);
jeffhao7fbee072012-08-24 17:56:54 -07001028 } else if (instruction_set == kMips) {
1029 uint32_t ref_spills = (1 << art::mips::S2) | (1 << art::mips::S3) | (1 << art::mips::S4) |
1030 (1 << art::mips::S5) | (1 << art::mips::S6) | (1 << art::mips::S7) |
1031 (1 << art::mips::FP);
1032 uint32_t arg_spills = (1 << art::mips::A1) | (1 << art::mips::A2) | (1 << art::mips::A3);
jeffhao7fbee072012-08-24 17:56:54 -07001033 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills : 0) |
jeffhao07030602012-09-26 14:33:14 -07001034 (1 << art::mips::RA);
jeffhao7fbee072012-08-24 17:56:54 -07001035 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
jeffhao7fbee072012-08-24 17:56:54 -07001036 1 /* Method* */) * kPointerSize, kStackAlignment);
1037 method->SetFrameSizeInBytes(frame_size);
1038 method->SetCoreSpillMask(core_spills);
jeffhao07030602012-09-26 14:33:14 -07001039 method->SetFpSpillMask(0);
Brian Carlstromae826982011-11-09 01:33:42 -08001040 } else if (instruction_set == kX86) {
Ian Rogers7caad772012-03-30 01:07:54 -07001041 uint32_t ref_spills = (1 << art::x86::EBP) | (1 << art::x86::ESI) | (1 << art::x86::EDI);
1042 uint32_t arg_spills = (1 << art::x86::ECX) | (1 << art::x86::EDX) | (1 << art::x86::EBX);
1043 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills : 0) |
1044 (1 << art::x86::kNumberOfCpuRegisters); // fake return address callee save
1045 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
1046 1 /* Method* */) * kPointerSize, kStackAlignment);
1047 method->SetFrameSizeInBytes(frame_size);
1048 method->SetCoreSpillMask(core_spills);
Ian Rogersff1ed472011-09-20 13:46:24 -07001049 method->SetFpSpillMask(0);
1050 } else {
1051 UNIMPLEMENTED(FATAL);
1052 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001053 return method.get();
Ian Rogersff1ed472011-09-20 13:46:24 -07001054}
1055
Mathieu Chartier66f19252012-09-18 08:57:04 -07001056void Runtime::SetCalleeSaveMethod(AbstractMethod* method, CalleeSaveType type) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001057 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
Elliott Hughes225f5a12012-06-11 11:23:48 -07001058 callee_save_methods_[type] = method;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001059}
1060
jeffhao2692b572011-12-16 15:42:28 -08001061void Runtime::EnableMethodTracing(Trace* tracer) {
1062 CHECK(!IsMethodTracingActive());
1063 tracer_ = tracer;
1064}
1065
1066void Runtime::DisableMethodTracing() {
1067 CHECK(IsMethodTracingActive());
1068 delete tracer_;
1069 tracer_ = NULL;
1070}
1071
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001072const std::vector<const DexFile*>& Runtime::GetCompileTimeClassPath(jobject class_loader) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001073 if (class_loader == NULL) {
1074 return GetClassLinker()->GetBootClassPath();
1075 }
1076 CHECK(UseCompileTimeClassPath());
1077 CompileTimeClassPaths::const_iterator it = compile_time_class_paths_.find(class_loader);
1078 CHECK(it != compile_time_class_paths_.end());
1079 return it->second;
1080}
1081
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001082void Runtime::SetCompileTimeClassPath(jobject class_loader, std::vector<const DexFile*>& class_path) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001083 CHECK(!IsStarted());
1084 use_compile_time_class_path_ = true;
Elliott Hughesa0e18062012-04-13 15:59:59 -07001085 compile_time_class_paths_.Put(class_loader, class_path);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001086}
1087
Carl Shapiro1fb86202011-06-27 17:43:13 -07001088} // namespace art