blob: 5230b774659e0d73fe41a1656445d6fba5aa9ee5 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro1fb86202011-06-27 17:43:13 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "runtime.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070018
Brian Carlstromdbf05b72011-12-15 00:55:24 -080019#include <signal.h>
Elliott Hughesd06a6c72012-05-30 17:59:06 -070020#include <sys/syscall.h>
Brian Carlstromdbf05b72011-12-15 00:55:24 -080021
Elliott Hughesffe67362011-07-17 12:09:27 -070022#include <cstdio>
23#include <cstdlib>
Brian Carlstrom8a436592011-08-15 21:27:23 -070024#include <limits>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070025#include <vector>
Elliott Hughesffe67362011-07-17 12:09:27 -070026
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070027#include "class_linker.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070028#include "class_loader.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070029#include "constants_arm.h"
30#include "constants_x86.h"
Elliott Hughes3bb81562011-10-21 18:52:59 -070031#include "debugger.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070032#include "heap.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070033#include "image.h"
Elliott Hughescf4c6c42011-09-01 15:16:42 -070034#include "intern_table.h"
Elliott Hughesc5f7c912011-08-18 14:00:42 -070035#include "jni_internal.h"
Elliott Hughes32d6e1e2011-10-11 14:47:44 -070036#include "monitor.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070037#include "oat_file.h"
Elliott Hughes726079d2011-10-07 18:43:44 -070038#include "ScopedLocalRef.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070039#include "scoped_thread_state_change.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070040#include "signal_catcher.h"
Elliott Hughes457005c2012-04-16 13:54:25 -070041#include "signal_set.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070042#include "space.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070043#include "thread.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070044#include "thread_list.h"
jeffhaob5e81852012-03-12 11:15:45 -070045#include "trace.h"
Elliott Hughes3bb81562011-10-21 18:52:59 -070046#include "UniquePtr.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070047#include "verifier/method_verifier.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070048#include "well_known_classes.h"
Carl Shapiro61e019d2011-07-14 16:53:09 -070049
Logan Chien971bf3f2012-05-01 15:47:55 +080050#if defined(ART_USE_LLVM_COMPILER)
51#include "compiler_llvm/procedure_linkage_table.h"
52#endif
53
Elliott Hugheseac76672012-05-24 21:56:51 -070054#include "JniConstants.h" // Last to avoid LOG redefinition in ics-mr1-plus-art.
Elliott Hughes90a33692011-08-30 13:27:07 -070055
Carl Shapiro1fb86202011-06-27 17:43:13 -070056namespace art {
57
Carl Shapiro2ed144c2011-07-26 16:52:08 -070058Runtime* Runtime::instance_ = NULL;
59
Elliott Hughesdcc24742011-09-07 14:02:44 -070060Runtime::Runtime()
Elliott Hughesd9c67be2012-02-02 19:54:06 -080061 : is_compiler_(false),
62 is_zygote_(false),
Mathieu Chartier069387a2012-06-18 12:01:01 -070063 is_concurrent_gc_enabled_(true),
Elliott Hughes6cf23882012-06-15 15:42:07 -070064 default_stack_size_(0),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080065 heap_(NULL),
Elliott Hughesc33a32b2011-10-11 18:18:07 -070066 monitor_list_(NULL),
Elliott Hughesdcc24742011-09-07 14:02:44 -070067 thread_list_(NULL),
68 intern_table_(NULL),
69 class_linker_(NULL),
70 signal_catcher_(NULL),
71 java_vm_(NULL),
Elliott Hughes225f5a12012-06-11 11:23:48 -070072 pre_allocated_OutOfMemoryError_(NULL),
Brian Carlstrom16192862011-09-12 17:50:06 -070073 jni_stub_array_(NULL),
Brian Carlstrome24fa612011-09-29 00:53:55 -070074 abstract_method_error_stub_array_(NULL),
Ian Rogers19846512012-02-24 11:42:47 -080075 resolution_method_(NULL),
Ian Rogers7c3757f2012-02-15 17:18:53 -080076 system_class_loader_(NULL),
Elliott Hughes6b355752012-01-13 16:49:08 -080077 shutting_down_(false),
Elliott Hughesdcc24742011-09-07 14:02:44 -070078 started_(false),
Mathieu Chartier7664f5c2012-06-08 18:15:32 -070079 finished_starting_(false),
Elliott Hughesdcc24742011-09-07 14:02:44 -070080 vfprintf_(NULL),
81 exit_(NULL),
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070082 abort_(NULL),
jeffhao2692b572011-12-16 15:42:28 -080083 stats_enabled_(false),
Ian Rogers776ac1f2012-04-13 23:36:36 -070084 method_trace_(0),
85 method_trace_file_size_(0),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080086 tracer_(NULL),
Ian Rogers365c1022012-06-22 15:05:28 -070087 use_compile_time_class_path_(false),
88 main_thread_group_(NULL),
Logan Chien971bf3f2012-05-01 15:47:55 +080089 system_thread_group_(NULL)
90#if defined(ART_USE_LLVM_COMPILER)
91#if defined(__arm__)
92 , plt_(kArm)
93#elif defined(__mips__)
94 , plt_(kMips)
95#elif defined(__i386__)
96 , plt_(kX86)
97#endif
98#endif
99 {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700100 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
101 resolution_stub_array_[i] = NULL;
102 }
103 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
Elliott Hughes225f5a12012-06-11 11:23:48 -0700104 callee_save_methods_[i] = NULL;
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700105 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800106
107#if defined(ART_USE_LLVM_COMPILER)
108 CHECK(plt_.AllocateTable()) << "Failed to allocate PLT";
109#endif
Elliott Hughesdcc24742011-09-07 14:02:44 -0700110}
111
Carl Shapiro61e019d2011-07-14 16:53:09 -0700112Runtime::~Runtime() {
Elliott Hughes6b355752012-01-13 16:49:08 -0800113 shutting_down_ = true;
114
jeffhaob5e81852012-03-12 11:15:45 -0700115 if (IsMethodTracingActive()) {
116 Trace::Shutdown();
117 }
118
Mathieu Chartiera6399032012-06-11 18:49:50 -0700119 // Make sure to let the GC complete if it is running.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700120 heap_->WaitForConcurrentGcToComplete();
Mathieu Chartiera6399032012-06-11 18:49:50 -0700121
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700122 // Make sure our internal threads are dead before we start tearing down things they're using.
Elliott Hughese52e49b2012-04-02 16:05:44 -0700123 Dbg::StopJdwp();
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700124 delete signal_catcher_;
125
Elliott Hughes038a8062011-09-18 14:12:41 -0700126 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
Elliott Hughes93e74e82011-09-13 11:07:03 -0700127 delete thread_list_;
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700128 delete monitor_list_;
Elliott Hughes93e74e82011-09-13 11:07:03 -0700129
Carl Shapiro61e019d2011-07-14 16:53:09 -0700130 delete class_linker_;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800131 delete heap_;
Elliott Hughes0a1038b2012-06-14 16:24:17 -0700132 verifier::MethodVerifier::Shutdown();
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700133 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700134 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700135 Thread::Shutdown();
Elliott Hughes7c6169d2012-05-02 16:11:48 -0700136 QuasiAtomic::Shutdown();
137
Carl Shapiro4acf4642011-07-26 18:54:13 -0700138 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700139 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -0700140 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700141}
142
Elliott Hughescac6cc72011-11-03 20:31:21 -0700143static bool gAborting = false;
144
Elliott Hughese0918552011-10-28 17:18:29 -0700145struct AbortState {
146 void Dump(std::ostream& os) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700147 if (gAborting) {
148 os << "Runtime aborting --- recursively, so no thread-specific detail!\n";
149 return;
150 }
151 gAborting = true;
Elliott Hughese0918552011-10-28 17:18:29 -0700152 os << "Runtime aborting...\n";
Elliott Hughes6c7d2442012-02-01 18:40:47 -0800153 if (Runtime::Current() == NULL) {
154 os << "(Runtime does not yet exist!)\n";
155 return;
156 }
Elliott Hughese0918552011-10-28 17:18:29 -0700157 Thread* self = Thread::Current();
158 if (self == NULL) {
159 os << "(Aborting thread was not attached to runtime!)\n";
160 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700161 // TODO: we're aborting and the ScopedObjectAccess may attempt to acquire the mutator_lock_
162 // which may block indefinitely if there's a misbehaving thread holding it exclusively.
163 // The code below should be made robust to this.
164 ScopedObjectAccess soa(self);
Elliott Hughes899e7892012-01-24 14:57:32 -0800165 self->Dump(os);
166 if (self->IsExceptionPending()) {
167 os << "Pending " << PrettyTypeOf(self->GetException()) << " on thread:\n"
168 << self->GetException()->Dump();
169 }
Elliott Hughese0918552011-10-28 17:18:29 -0700170 }
171 }
172};
173
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700174void Runtime::Abort() {
Elliott Hughes131aef82012-01-27 11:53:35 -0800175 // Ensure that we don't have multiple threads trying to abort at once,
176 // which would result in significantly worse diagnostics.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700177 MutexLock mu(*GlobalSynchronization::abort_lock_);
Elliott Hughes131aef82012-01-27 11:53:35 -0800178
Elliott Hughesffe67362011-07-17 12:09:27 -0700179 // Get any pending output out of the way.
180 fflush(NULL);
181
182 // Many people have difficulty distinguish aborts from crashes,
183 // so be explicit.
Elliott Hughese0918552011-10-28 17:18:29 -0700184 AbortState state;
Elliott Hughes9ee5f9c2012-02-03 18:33:16 -0800185 LOG(INTERNAL_FATAL) << Dumpable<AbortState>(state);
Elliott Hughesffe67362011-07-17 12:09:27 -0700186
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700187 // Call the abort hook if we have one.
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700188 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700189 LOG(INTERNAL_FATAL) << "Calling abort hook...";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700190 Runtime::Current()->abort_();
191 // notreached
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700192 LOG(INTERNAL_FATAL) << "Unexpectedly returned from abort hook!";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700193 }
194
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700195#if defined(__BIONIC__)
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700196 // TODO: finish merging patches to fix abort(3) in bionic, then lose this!
197 // Bionic doesn't implement POSIX semantics for abort(3) in a multi-threaded
198 // process, so if we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -0700199 // receive SIGABRT. debuggerd dumps the stack trace of the main
200 // thread, whether or not that was the thread that failed. By
201 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -0700202 // fault in the current thread, and get a useful log from debuggerd.
Elliott Hughes81ff3182012-03-23 20:35:56 -0700203 // We can also trivially tell the difference between a crash and
Elliott Hughesffe67362011-07-17 12:09:27 -0700204 // a deliberate abort by looking at the fault address.
205 *reinterpret_cast<char*>(0xdeadd00d) = 38;
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700206#elif defined(__APPLE__)
207 // TODO: check that this actually gives good stack traces on the Mac!
208 pthread_kill(pthread_self(), SIGABRT);
209#else
210 // TODO: we ought to be able to use pthread_kill(3) here (or abort(3),
211 // which POSIX defines in terms of raise(3), which POSIX defines in terms
212 // of pthread_kill(3)). On Linux, though, libcorkscrew can't unwind through
213 // libpthread, which means the stacks we dump would be useless. Calling
214 // tgkill(2) directly avoids that.
215 syscall(__NR_tgkill, getpid(), GetTid(), SIGABRT);
216#endif
Elliott Hughesffe67362011-07-17 12:09:27 -0700217 // notreached
218}
219
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700220bool Runtime::PreZygoteFork() {
221 heap_->PreZygoteFork();
222 return true;
223}
224
Elliott Hughesbf86d042011-08-31 17:53:14 -0700225void Runtime::CallExitHook(jint status) {
226 if (exit_ != NULL) {
Elliott Hughes34e06962012-04-09 13:55:55 -0700227 ScopedThreadStateChange tsc(Thread::Current(), kNative);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700228 exit_(status);
229 LOG(WARNING) << "Exit hook returned instead of exiting!";
230 }
231}
232
Brian Carlstrom8a436592011-08-15 21:27:23 -0700233// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
234// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
235// [gG] gigabytes.
236//
237// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700238// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
239// of 1024.
240//
241// The spec says the -Xmx and -Xms options must be multiples of 1024. It
242// doesn't say anything about -Xss.
243//
244// Returns 0 (a useless size) if "s" is malformed or specifies a low or
245// non-evenly-divisible value.
246//
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800247size_t ParseMemoryOption(const char* s, size_t div) {
Brian Carlstrom8a436592011-08-15 21:27:23 -0700248 // strtoul accepts a leading [+-], which we don't want,
249 // so make sure our string starts with a decimal digit.
250 if (isdigit(*s)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700251 char* s2;
252 size_t val = strtoul(s, &s2, 10);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700253 if (s2 != s) {
254 // s2 should be pointing just after the number.
255 // If this is the end of the string, the user
256 // has specified a number of bytes. Otherwise,
257 // there should be exactly one more character
258 // that specifies a multiplier.
259 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700260 // The remainder of the string is either a single multiplier
261 // character, or nothing to indicate that the value is in
262 // bytes.
263 char c = *s2++;
264 if (*s2 == '\0') {
265 size_t mul;
266 if (c == '\0') {
267 mul = 1;
268 } else if (c == 'k' || c == 'K') {
269 mul = KB;
270 } else if (c == 'm' || c == 'M') {
271 mul = MB;
272 } else if (c == 'g' || c == 'G') {
273 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700274 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700275 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700276 return 0;
277 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700278
279 if (val <= std::numeric_limits<size_t>::max() / mul) {
280 val *= mul;
281 } else {
282 // Clamp to a multiple of 1024.
283 val = std::numeric_limits<size_t>::max() & ~(1024-1);
284 }
285 } else {
286 // There's more than one character after the numeric part.
287 return 0;
288 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700289 }
290 // The man page says that a -Xm value must be a multiple of 1024.
291 if (val % div == 0) {
292 return val;
293 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700294 }
295 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700296 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700297}
298
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800299size_t ParseIntegerOrDie(const std::string& s) {
300 std::string::size_type colon = s.find(':');
301 if (colon == std::string::npos) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700302 LOG(FATAL) << "Missing integer: " << s;
303 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800304 const char* begin = &s[colon + 1];
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700305 char* end;
306 size_t result = strtoul(begin, &end, 10);
307 if (begin == end || *end != '\0') {
308 LOG(FATAL) << "Failed to parse integer in: " << s;
309 }
310 return result;
311}
312
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700313Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700314 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800315 const char* boot_class_path_string = getenv("BOOTCLASSPATH");
316 if (boot_class_path_string != NULL) {
317 parsed->boot_class_path_string_ = boot_class_path_string;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700318 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800319 const char* class_path_string = getenv("CLASSPATH");
320 if (class_path_string != NULL) {
321 parsed->class_path_string_ = class_path_string;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700322 }
Elliott Hughes67d92002012-03-26 15:08:51 -0700323 // -Xcheck:jni is off by default for regular builds but on by default in debug builds.
324 parsed->check_jni_ = kIsDebugBuild;
Elliott Hughes85d15452011-09-16 17:33:01 -0700325
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700326 parsed->heap_initial_size_ = Heap::kInitialSize;
327 parsed->heap_maximum_size_ = Heap::kMaximumSize;
Elliott Hughes6cf23882012-06-15 15:42:07 -0700328 parsed->heap_growth_limit_ = 0; // 0 means no growth limit.
329 parsed->stack_size_ = 0; // 0 means default.
Brian Carlstromf734cf52011-08-17 16:28:14 -0700330
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800331 parsed->is_compiler_ = false;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700332 parsed->is_zygote_ = false;
Mathieu Chartier069387a2012-06-18 12:01:01 -0700333 parsed->is_concurrent_gc_enabled_ = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700334
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700335 parsed->jni_globals_max_ = 0;
Elliott Hughesfc861622011-10-17 17:57:47 -0700336 parsed->lock_profiling_threshold_ = 0;
337 parsed->hook_is_sensitive_thread_ = NULL;
338
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700339 parsed->hook_vfprintf_ = vfprintf;
340 parsed->hook_exit_ = exit;
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700341 parsed->hook_abort_ = NULL; // We don't call abort(3) by default; see Runtime::Abort.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700342
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800343// gLogVerbosity.class_linker = true; // TODO: don't check this in!
344// gLogVerbosity.compiler = true; // TODO: don't check this in!
345// gLogVerbosity.heap = true; // TODO: don't check this in!
346// gLogVerbosity.gc = true; // TODO: don't check this in!
347// gLogVerbosity.jdwp = true; // TODO: don't check this in!
348// gLogVerbosity.jni = true; // TODO: don't check this in!
349// gLogVerbosity.monitor = true; // TODO: don't check this in!
350// gLogVerbosity.startup = true; // TODO: don't check this in!
351// gLogVerbosity.third_party_jni = true; // TODO: don't check this in!
352// gLogVerbosity.threads = true; // TODO: don't check this in!
353
jeffhaob5e81852012-03-12 11:15:45 -0700354 parsed->method_trace_ = false;
355 parsed->method_trace_file_ = "/data/method-trace-file.bin";
356 parsed->method_trace_file_size_ = 10 * MB;
357
Brian Carlstrom8a436592011-08-15 21:27:23 -0700358 for (size_t i = 0; i < options.size(); ++i) {
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800359 const std::string option(options[i].first);
Brian Carlstrom0796af02011-10-12 14:31:45 -0700360 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700361 LOG(INFO) << "option[" << i << "]=" << option;
362 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800363 if (StartsWith(option, "-Xbootclasspath:")) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800364 parsed->boot_class_path_string_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700365 } else if (option == "-classpath" || option == "-cp") {
366 // TODO: support -Djava.class.path
367 i++;
368 if (i == options.size()) {
369 // TODO: usage
370 LOG(FATAL) << "Missing required class path value for " << option;
371 return NULL;
372 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800373 const StringPiece& value = options[i].first;
374 parsed->class_path_string_ = value.data();
375 } else if (option == "bootclasspath") {
376 parsed->boot_class_path_
377 = reinterpret_cast<const std::vector<const DexFile*>*>(options[i].second);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800378 } else if (StartsWith(option, "-Ximage:")) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800379 parsed->image_ = option.substr(strlen("-Ximage:")).data();
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800380 } else if (StartsWith(option, "-Xcheck:jni")) {
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700381 parsed->check_jni_ = true;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800382 } else if (StartsWith(option, "-Xrunjdwp:") || StartsWith(option, "-agentlib:jdwp=")) {
383 std::string tail(option.substr(option[1] == 'X' ? 10 : 15));
Elliott Hughes3bb81562011-10-21 18:52:59 -0700384 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
385 LOG(FATAL) << "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
386 << "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n";
387 return NULL;
388 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800389 } else if (StartsWith(option, "-Xms")) {
390 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).c_str(), 1024);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700391 if (size == 0) {
392 if (ignore_unrecognized) {
393 continue;
394 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700395 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700396 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700397 return NULL;
398 }
399 parsed->heap_initial_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800400 } else if (StartsWith(option, "-Xmx")) {
401 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).c_str(), 1024);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700402 if (size == 0) {
403 if (ignore_unrecognized) {
404 continue;
405 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700406 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700407 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700408 return NULL;
409 }
410 parsed->heap_maximum_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800411 } else if (StartsWith(option, "-XX:HeapGrowthLimit=")) {
412 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).c_str(), 1024);
jeffhaoc1160702011-10-27 15:48:45 -0700413 if (size == 0) {
414 if (ignore_unrecognized) {
415 continue;
416 }
417 // TODO: usage
418 LOG(FATAL) << "Failed to parse " << option;
419 return NULL;
420 }
421 parsed->heap_growth_limit_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800422 } else if (StartsWith(option, "-Xss")) {
423 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).c_str(), 1);
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->stack_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800433 } else if (StartsWith(option, "-D")) {
434 parsed->properties_.push_back(option.substr(strlen("-D")));
435 } else if (StartsWith(option, "-Xjnitrace:")) {
436 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:"));
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700437 } else if (option == "compiler") {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800438 parsed->is_compiler_ = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700439 } else if (option == "-Xzygote") {
440 parsed->is_zygote_ = true;
Mathieu Chartier069387a2012-06-18 12:01:01 -0700441 } else if (StartsWith(option, "-Xgc:")) {
442 std::vector<std::string> gc_options;
443 Split(option.substr(strlen("-Xgc:")), ',', gc_options);
444 for (size_t i = 0; i < gc_options.size(); ++i) {
445 if (gc_options[i] == "noconcurrent") {
446 parsed->is_concurrent_gc_enabled_ = false;
447 } else if (gc_options[i] == "concurrent") {
448 parsed->is_concurrent_gc_enabled_ = true;
449 } else {
450 LOG(WARNING) << "Ignoring unknown -Xgc option: " << gc_options[i];
451 }
452 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800453 } else if (StartsWith(option, "-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700454 std::vector<std::string> verbose_options;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800455 Split(option.substr(strlen("-verbose:")), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700456 for (size_t i = 0; i < verbose_options.size(); ++i) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800457 if (verbose_options[i] == "class") {
458 gLogVerbosity.class_linker = true;
459 } else if (verbose_options[i] == "compiler") {
460 gLogVerbosity.compiler = true;
461 } else if (verbose_options[i] == "heap") {
462 gLogVerbosity.heap = true;
463 } else if (verbose_options[i] == "gc") {
464 gLogVerbosity.gc = true;
465 } else if (verbose_options[i] == "jdwp") {
466 gLogVerbosity.jdwp = true;
467 } else if (verbose_options[i] == "jni") {
468 gLogVerbosity.jni = true;
469 } else if (verbose_options[i] == "monitor") {
470 gLogVerbosity.monitor = true;
471 } else if (verbose_options[i] == "startup") {
472 gLogVerbosity.startup = true;
473 } else if (verbose_options[i] == "third-party-jni") {
474 gLogVerbosity.third_party_jni = true;
475 } else if (verbose_options[i] == "threads") {
476 gLogVerbosity.threads = true;
477 } else {
478 LOG(WARNING) << "Ignoring unknown -verbose option: " << verbose_options[i];
479 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700480 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800481 } else if (StartsWith(option, "-Xjnigreflimit:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700482 parsed->jni_globals_max_ = ParseIntegerOrDie(option);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800483 } else if (StartsWith(option, "-Xlockprofthreshold:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700484 parsed->lock_profiling_threshold_ = ParseIntegerOrDie(option);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800485 } else if (StartsWith(option, "-Xstacktracefile:")) {
Elliott Hughes206c20d2012-06-28 11:05:37 -0700486 parsed->stack_trace_file_ = option.substr(strlen("-Xstacktracefile:"));
Elliott Hughesfc861622011-10-17 17:57:47 -0700487 } else if (option == "sensitiveThread") {
488 parsed->hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(options[i].second);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700489 } else if (option == "vfprintf") {
490 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
491 } else if (option == "exit") {
492 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
493 } else if (option == "abort") {
494 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700495 } else if (option == "host-prefix") {
496 parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second);
Elliott Hughesb3e66df2012-01-12 14:49:18 -0800497 } else if (option == "-Xgenregmap" || option == "-Xgc:precise") {
498 // We silently ignore these for backwards compatibility.
jeffhaob5e81852012-03-12 11:15:45 -0700499 } else if (option == "-Xmethod-trace") {
500 parsed->method_trace_ = true;
501 } else if (StartsWith(option, "-Xmethod-trace-file:")) {
502 parsed->method_trace_file_ = option.substr(strlen("-Xmethod-trace-file:"));
503 } else if (StartsWith(option, "-Xmethod-trace-file-size:")) {
504 parsed->method_trace_file_size_ = ParseIntegerOrDie(option);
Elliott Hughese119a362012-05-22 17:37:06 -0700505 } else if (option == "-Xprofile:threadcpuclock") {
506 Trace::SetDefaultClockSource(kProfilerClockSourceThreadCpu);
507 } else if (option == "-Xprofile:wallclock") {
508 Trace::SetDefaultClockSource(kProfilerClockSourceWall);
509 } else if (option == "-Xprofile:dualclock") {
510 Trace::SetDefaultClockSource(kProfilerClockSourceDual);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700511 } else {
512 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700513 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700514 LOG(ERROR) << "Unrecognized option " << option;
515 // TODO: this should exit, but for now tolerate unknown options
516 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700517 }
518 }
519 }
520
Brian Carlstrom223f20f2012-02-04 23:06:55 -0800521 if (!parsed->is_compiler_ && parsed->image_.empty()) {
522 parsed->image_ += GetAndroidRoot();
523 parsed->image_ += "/framework/boot.art";
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700524 }
jeffhaoc1160702011-10-27 15:48:45 -0700525 if (parsed->heap_growth_limit_ == 0) {
526 parsed->heap_growth_limit_ = parsed->heap_maximum_size_;
527 }
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700528
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700529 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700530}
531
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700532bool Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700533 // TODO: acquire a static mutex on Runtime to avoid racing.
534 if (Runtime::instance_ != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700535 return false;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700536 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700537 GlobalSynchronization::Init();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700538 instance_ = new Runtime;
539 if (!instance_->Init(options, ignore_unrecognized)) {
540 delete instance_;
541 instance_ = NULL;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700542 return false;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700543 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700544 return true;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700545}
Elliott Hughes0af55432011-08-17 18:37:28 -0700546
Ian Rogers365c1022012-06-22 15:05:28 -0700547static void CreateSystemClassLoader() {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800548 if (Runtime::Current()->UseCompileTimeClassPath()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700549 return;
550 }
551
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700552 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700553
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700554 Class* class_loader_class = soa.Decode<Class*>(WellKnownClasses::java_lang_ClassLoader);
Ian Rogers365c1022012-06-22 15:05:28 -0700555 CHECK(Runtime::Current()->GetClassLinker()->EnsureInitialized(class_loader_class, true, true));
Brian Carlstromaded5f72011-10-07 17:15:04 -0700556
Ian Rogers365c1022012-06-22 15:05:28 -0700557 Method* getSystemClassLoader = class_loader_class->FindDirectMethod("getSystemClassLoader", "()Ljava/lang/ClassLoader;");
Brian Carlstromdf143242011-10-10 18:05:34 -0700558 CHECK(getSystemClassLoader != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700559
Ian Rogers365c1022012-06-22 15:05:28 -0700560 ClassLoader* class_loader =
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700561 down_cast<ClassLoader*>(InvokeWithJValues(soa, NULL, getSystemClassLoader, NULL).GetL());
Ian Rogers365c1022012-06-22 15:05:28 -0700562 CHECK(class_loader != NULL);
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500563
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700564 soa.Self()->SetClassLoaderOverride(class_loader);
Ian Rogers365c1022012-06-22 15:05:28 -0700565
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700566 Class* thread_class = soa.Decode<Class*>(WellKnownClasses::java_lang_Thread);
Ian Rogers365c1022012-06-22 15:05:28 -0700567 CHECK(Runtime::Current()->GetClassLinker()->EnsureInitialized(thread_class, true, true));
568
569 Field* contextClassLoader = thread_class->FindDeclaredInstanceField("contextClassLoader",
570 "Ljava/lang/ClassLoader;");
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500571 CHECK(contextClassLoader != NULL);
Ian Rogers365c1022012-06-22 15:05:28 -0700572
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700573 contextClassLoader->SetObject(soa.Self()->GetPeer(), class_loader);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700574}
575
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700576void Runtime::Start() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800577 VLOG(startup) << "Runtime::Start entering";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700578
579 CHECK(host_prefix_.empty()) << host_prefix_;
580
Elliott Hughes225f5a12012-06-11 11:23:48 -0700581 // Relocate the OatFiles (ELF images).
Logan Chien0c717dd2012-03-28 18:31:07 +0800582 class_linker_->RelocateExecutable();
583
Elliott Hughes225f5a12012-06-11 11:23:48 -0700584 // Pre-allocate an OutOfMemoryError for the double-OOME case.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700585 Thread* self = Thread::Current();
Elliott Hughes225f5a12012-06-11 11:23:48 -0700586 self->ThrowNewException("Ljava/lang/OutOfMemoryError;",
587 "OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack available");
588 pre_allocated_OutOfMemoryError_ = self->GetException();
589 self->ClearException();
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700590
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700591 // Restore main thread state to kNative as expected by native code.
592 self->TransitionFromRunnableToSuspended(kNative);
593
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700594 started_ = true;
595
Brian Carlstromae826982011-11-09 01:33:42 -0800596 // InitNativeMethods needs to be after started_ so that the classes
597 // it touches will have methods linked to the oat file if necessary.
598 InitNativeMethods();
599
Ian Rogers365c1022012-06-22 15:05:28 -0700600 // Initialize well known thread group values that may be accessed threads while attaching.
601 InitThreadGroups(self);
602
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500603 Thread::FinishStartup();
604
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700605 if (!is_zygote_) {
606 DidForkFromZygote();
607 }
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700608
Elliott Hughes85d15452011-09-16 17:33:01 -0700609 StartDaemonThreads();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700610
Brian Carlstromaded5f72011-10-07 17:15:04 -0700611 CreateSystemClassLoader();
612
Elliott Hughes225f5a12012-06-11 11:23:48 -0700613 self->GetJniEnv()->locals.AssertEmpty();
Elliott Hughes726079d2011-10-07 18:43:44 -0700614
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800615 VLOG(startup) << "Runtime::Start exiting";
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700616
617 finished_starting_ = true;
Elliott Hughes85d15452011-09-16 17:33:01 -0700618}
619
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700620void Runtime::DidForkFromZygote() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700621 is_zygote_ = false;
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700622
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700623 StartSignalCatcher();
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700624
625 // Start the JDWP thread. If the command-line debugger flags specified "suspend=y",
626 // this will pause the runtime, so we probably want this to come last.
627 Dbg::StartJdwp();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700628}
629
630void Runtime::StartSignalCatcher() {
631 if (!is_zygote_) {
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700632 signal_catcher_ = new SignalCatcher(stack_trace_file_);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700633 }
634}
635
Elliott Hughes85d15452011-09-16 17:33:01 -0700636void Runtime::StartDaemonThreads() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800637 VLOG(startup) << "Runtime::StartDaemonThreads entering";
Elliott Hughes85d15452011-09-16 17:33:01 -0700638
Elliott Hughes719b3232011-09-25 17:42:19 -0700639 Thread* self = Thread::Current();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700640
641 // Must be in the kNative state for calling native methods.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700642 {
643 MutexLock mu(*GlobalSynchronization::thread_suspend_count_lock_);
644 CHECK_EQ(self->GetState(), kNative);
645 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700646
Elliott Hughes719b3232011-09-25 17:42:19 -0700647 JNIEnv* env = self->GetJniEnv();
Elliott Hugheseac76672012-05-24 21:56:51 -0700648 env->CallStaticVoidMethod(WellKnownClasses::java_lang_Daemons, WellKnownClasses::java_lang_Daemons_start);
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800649 CHECK(!env->ExceptionCheck());
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700650
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800651 VLOG(startup) << "Runtime::StartDaemonThreads exiting";
Carl Shapiro61e019d2011-07-14 16:53:09 -0700652}
653
Elliott Hughes0af55432011-08-17 18:37:28 -0700654bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700655 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700656
Elliott Hughes90a33692011-08-30 13:27:07 -0700657 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
658 if (options.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700659 LOG(ERROR) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700660 return false;
661 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800662 VLOG(startup) << "Runtime::Init -verbose:startup enabled";
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700663
Elliott Hughes7c6169d2012-05-02 16:11:48 -0700664 QuasiAtomic::Startup();
665
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700666 SetJniGlobalsMax(options->jni_globals_max_);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800667 Monitor::Init(options->lock_profiling_threshold_, options->hook_is_sensitive_thread_);
Elliott Hughes32d6e1e2011-10-11 14:47:44 -0700668
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700669 host_prefix_ = options->host_prefix_;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800670 boot_class_path_string_ = options->boot_class_path_string_;
671 class_path_string_ = options->class_path_string_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700672 properties_ = options->properties_;
673
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800674 is_compiler_ = options->is_compiler_;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700675 is_zygote_ = options->is_zygote_;
Mathieu Chartier069387a2012-06-18 12:01:01 -0700676 is_concurrent_gc_enabled_ = options->is_concurrent_gc_enabled_;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700677
Elliott Hughes0af55432011-08-17 18:37:28 -0700678 vfprintf_ = options->hook_vfprintf_;
679 exit_ = options->hook_exit_;
680 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700681
Elliott Hughesbe759c62011-09-08 19:38:21 -0700682 default_stack_size_ = options->stack_size_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700683 stack_trace_file_ = options->stack_trace_file_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700684
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700685 monitor_list_ = new MonitorList;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800686 thread_list_ = new ThreadList;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700687 intern_table_ = new InternTable;
688
Elliott Hughes0a1038b2012-06-14 16:24:17 -0700689 verifier::MethodVerifier::Init();
Logan Chiendd361c92012-04-10 23:40:37 +0800690
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800691 heap_ = new Heap(options->heap_initial_size_,
692 options->heap_growth_limit_,
693 options->heap_maximum_size_,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700694 options->image_,
695 options->is_concurrent_gc_enabled_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700696
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700697 BlockSignals();
Elliott Hughes457005c2012-04-16 13:54:25 -0700698 InitPlatformSignalHandlers();
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700699
Elliott Hughesa0957642011-09-02 14:27:33 -0700700 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700701
Elliott Hughesbe759c62011-09-08 19:38:21 -0700702 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700703
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700704 // ClassLinker needs an attached thread, but we can't fully attach a thread
Elliott Hughes462c9442012-03-23 18:47:50 -0700705 // without creating objects. We can't supply a thread group yet; it will be fixed later.
706 Thread::Attach("main", false, NULL);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700707
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700708 // Set us to runnable so tools using a runtime can allocate and GC by default
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700709 Thread::Current()->TransitionFromSuspendedToRunnable();
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700710
Ian Rogers141d6222012-04-05 12:23:06 -0700711 // Now we're attached, we can take the heap lock and validate the heap.
712 GetHeap()->EnableObjectValidation();
713
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800714 CHECK_GE(GetHeap()->GetSpaces().size(), 1U);
715 if (GetHeap()->GetSpaces()[0]->IsImageSpace()) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800716 class_linker_ = ClassLinker::CreateFromImage(intern_table_);
717 } else {
718 CHECK(options->boot_class_path_ != NULL);
719 CHECK_NE(options->boot_class_path_->size(), 0U);
720 class_linker_ = ClassLinker::CreateFromCompiler(*options->boot_class_path_, intern_table_);
721 }
722 CHECK(class_linker_ != NULL);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700723
jeffhaob5e81852012-03-12 11:15:45 -0700724 method_trace_ = options->method_trace_;
725 method_trace_file_ = options->method_trace_file_;
726 method_trace_file_size_ = options->method_trace_file_size_;
727
728 if (options->method_trace_) {
729 Trace::Start(options->method_trace_file_.c_str(), -1, options->method_trace_file_size_, 0, false);
730 }
731
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800732 VLOG(startup) << "Runtime::Init exiting";
Carl Shapiro1fb86202011-06-27 17:43:13 -0700733 return true;
734}
735
Elliott Hughes038a8062011-09-18 14:12:41 -0700736void Runtime::InitNativeMethods() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800737 VLOG(startup) << "Runtime::InitNativeMethods entering";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700738 Thread* self = Thread::Current();
739 JNIEnv* env = self->GetJniEnv();
740
Elliott Hughes418d20f2011-09-22 14:00:39 -0700741 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700742 {
743 MutexLock mu(*GlobalSynchronization::thread_suspend_count_lock_);
744 CHECK_EQ(self->GetState(), kNative);
745 }
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700746
Elliott Hughes418d20f2011-09-22 14:00:39 -0700747 // First set up JniConstants, which is used by both the runtime's built-in native
748 // methods and libcore.
Elliott Hughesfea966e2011-09-22 10:26:20 -0700749 JniConstants::init(env);
Elliott Hugheseac76672012-05-24 21:56:51 -0700750 WellKnownClasses::Init(env);
Elliott Hughesfea966e2011-09-22 10:26:20 -0700751
Elliott Hughes418d20f2011-09-22 14:00:39 -0700752 // Then set up the native methods provided by the runtime itself.
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700753 RegisterRuntimeNativeMethods(env);
754
Elliott Hughes418d20f2011-09-22 14:00:39 -0700755 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
756 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
757 // the library that implements System.loadLibrary!
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700758 {
759 std::string mapped_name(StringPrintf(OS_SHARED_LIB_FORMAT_STR, "javacore"));
760 std::string reason;
761 self->TransitionFromSuspendedToRunnable();
762 if (!instance_->java_vm_->LoadNativeLibrary(mapped_name, NULL, reason)) {
763 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": " << reason;
764 }
765 self->TransitionFromRunnableToSuspended(kNative);
766 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800767 VLOG(startup) << "Runtime::InitNativeMethods exiting";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700768}
769
Ian Rogers365c1022012-06-22 15:05:28 -0700770void Runtime::InitThreadGroups(Thread* self) {
771 JNIEnvExt* env = self->GetJniEnv();
772 ScopedJniEnvLocalRefState env_state(env);
773 main_thread_group_ =
774 env->NewGlobalRef(env->GetStaticObjectField(WellKnownClasses::java_lang_ThreadGroup,
775 WellKnownClasses::java_lang_ThreadGroup_mainThreadGroup));
Brian Carlstrom034f76b2012-08-01 15:51:58 -0700776 CHECK(main_thread_group_ != NULL || IsCompiler());
Ian Rogers365c1022012-06-22 15:05:28 -0700777 system_thread_group_ =
778 env->NewGlobalRef(env->GetStaticObjectField(WellKnownClasses::java_lang_ThreadGroup,
779 WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup));
Brian Carlstrom034f76b2012-08-01 15:51:58 -0700780 CHECK(system_thread_group_ != NULL || IsCompiler());
781}
782
783jobject Runtime::GetMainThreadGroup() const {
784 CHECK(main_thread_group_ != NULL || IsCompiler());
785 return main_thread_group_;
786}
787
788jobject Runtime::GetSystemThreadGroup() const {
789 CHECK(system_thread_group_ != NULL || IsCompiler());
790 return system_thread_group_;
Ian Rogers365c1022012-06-22 15:05:28 -0700791}
792
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700793void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
794#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Ian Rogers5167c972012-02-03 10:41:20 -0800795 // Register Throwable first so that registration of other native methods can throw exceptions
796 REGISTER(register_java_lang_Throwable);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700797 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700798 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700799 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700800 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700801 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700802 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700803 REGISTER(register_java_lang_Object);
804 REGISTER(register_java_lang_Runtime);
805 REGISTER(register_java_lang_String);
806 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700807 REGISTER(register_java_lang_Thread);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700808 REGISTER(register_java_lang_VMClassLoader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700809 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700810 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700811 REGISTER(register_java_lang_reflect_Field);
812 REGISTER(register_java_lang_reflect_Method);
Jesse Wilson95caa792011-10-12 18:14:17 -0400813 REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700814 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700815 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700816 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700817 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700818#undef REGISTER
819}
820
Elliott Hughesc967f782012-04-16 10:23:15 -0700821void Runtime::DumpForSigQuit(std::ostream& os) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700822 GetClassLinker()->DumpForSigQuit(os);
823 GetInternTable()->DumpForSigQuit(os);
Elliott Hughesae80b492012-04-24 10:43:17 -0700824 GetJavaVM()->DumpForSigQuit(os);
Elliott Hughesc967f782012-04-16 10:23:15 -0700825 GetHeap()->DumpForSigQuit(os);
Elliott Hughes42ee1422011-09-06 12:33:32 -0700826 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700827
Elliott Hughesc967f782012-04-16 10:23:15 -0700828 thread_list_->DumpForSigQuit(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700829}
830
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800831void Runtime::DumpLockHolders(std::ostream& os) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700832 uint64_t mutator_lock_owner = GlobalSynchronization::mutator_lock_->GetExclusiveOwnerTid();
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800833 pid_t thread_list_lock_owner = GetThreadList()->GetLockOwner();
834 pid_t classes_lock_owner = GetClassLinker()->GetClassesLockOwner();
835 pid_t dex_lock_owner = GetClassLinker()->GetDexLockOwner();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700836 if ((thread_list_lock_owner | classes_lock_owner | dex_lock_owner) != 0) {
837 os << "Mutator lock exclusive owner tid: " << mutator_lock_owner << "\n"
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800838 << "ThreadList lock owner tid: " << thread_list_lock_owner << "\n"
839 << "ClassLinker classes lock owner tid: " << classes_lock_owner << "\n"
840 << "ClassLinker dex lock owner tid: " << dex_lock_owner << "\n";
841 }
842}
843
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700844void Runtime::SetStatsEnabled(bool new_state) {
845 if (new_state == true) {
846 GetStats()->Clear(~0);
847 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
848 Thread::Current()->GetStats()->Clear(~0);
849 }
850 stats_enabled_ = new_state;
851}
852
853void Runtime::ResetStats(int kinds) {
854 GetStats()->Clear(kinds & 0xffff);
855 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
856 Thread::Current()->GetStats()->Clear(kinds >> 16);
857}
858
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700859int32_t Runtime::GetStat(int kind) {
860 RuntimeStats* stats;
861 if (kind < (1<<16)) {
862 stats = GetStats();
863 } else {
864 stats = Thread::Current()->GetStats();
865 kind >>= 16;
866 }
867 switch (kind) {
868 case KIND_ALLOCATED_OBJECTS:
869 return stats->allocated_objects;
870 case KIND_ALLOCATED_BYTES:
871 return stats->allocated_bytes;
872 case KIND_FREED_OBJECTS:
873 return stats->freed_objects;
874 case KIND_FREED_BYTES:
875 return stats->freed_bytes;
876 case KIND_GC_INVOCATIONS:
877 return stats->gc_for_alloc_count;
878 case KIND_CLASS_INIT_COUNT:
879 return stats->class_init_count;
880 case KIND_CLASS_INIT_TIME:
881 // Convert ns to us, reduce to 32 bits.
Elliott Hughes398f64b2012-03-26 18:05:48 -0700882 return static_cast<int>(stats->class_init_time_ns / 1000);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700883 case KIND_EXT_ALLOCATED_OBJECTS:
884 case KIND_EXT_ALLOCATED_BYTES:
885 case KIND_EXT_FREED_OBJECTS:
886 case KIND_EXT_FREED_BYTES:
887 return 0; // backward compatibility
888 default:
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700889 LOG(FATAL) << "Unknown statistic " << kind;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700890 return -1; // unreachable
891 }
892}
893
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700894void Runtime::BlockSignals() {
Elliott Hughes457005c2012-04-16 13:54:25 -0700895 SignalSet signals;
896 signals.Add(SIGPIPE);
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700897 // SIGQUIT is used to dump the runtime's state (including stack traces).
Elliott Hughes457005c2012-04-16 13:54:25 -0700898 signals.Add(SIGQUIT);
Elliott Hughes08795042012-04-03 14:48:52 -0700899 // SIGUSR1 is used to initiate a GC.
Elliott Hughes457005c2012-04-16 13:54:25 -0700900 signals.Add(SIGUSR1);
901 signals.Block();
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700902}
903
Ian Rogers365c1022012-06-22 15:05:28 -0700904void Runtime::AttachCurrentThread(const char* thread_name, bool as_daemon, jobject thread_group) {
Elliott Hughes462c9442012-03-23 18:47:50 -0700905 Thread::Attach(thread_name, as_daemon, thread_group);
Elliott Hughes22869a92012-03-27 14:08:24 -0700906 if (thread_name == NULL) {
907 LOG(WARNING) << *Thread::Current() << " attached without supplying a name";
908 }
Carl Shapiro61e019d2011-07-14 16:53:09 -0700909}
910
Elliott Hughesd92bec42011-09-02 17:04:36 -0700911void Runtime::DetachCurrentThread() {
Brian Carlstrom4d571432012-05-16 00:21:41 -0700912 Thread* self = Thread::Current();
913 if (self == NULL) {
914 LOG(FATAL) << "attempting to detach thread that is not attached";
915 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700916 if (self->HasManagedStack()) {
Elliott Hughes22869a92012-03-27 14:08:24 -0700917 LOG(FATAL) << *Thread::Current() << " attempting to detach while still running code";
918 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700919 thread_list_->Unregister(self);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700920}
921
Brian Carlstrome24fa612011-09-29 00:53:55 -0700922void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700923 Dbg::VisitRoots(visitor, arg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700924 class_linker_->VisitRoots(visitor, arg);
925 intern_table_->VisitRoots(visitor, arg);
926 java_vm_->VisitRoots(visitor, arg);
927 thread_list_->VisitRoots(visitor, arg);
Elliott Hughes225f5a12012-06-11 11:23:48 -0700928 if (pre_allocated_OutOfMemoryError_ != NULL) {
929 visitor(pre_allocated_OutOfMemoryError_, arg);
930 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700931 visitor(jni_stub_array_, arg);
932 visitor(abstract_method_error_stub_array_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700933 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
934 visitor(resolution_stub_array_[i], arg);
935 }
Ian Rogers19846512012-02-24 11:42:47 -0800936 visitor(resolution_method_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700937 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
Elliott Hughes225f5a12012-06-11 11:23:48 -0700938 visitor(callee_save_methods_[i], arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700939 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700940}
941
Ian Rogers169c9a72011-11-13 20:13:17 -0800942void Runtime::SetJniDlsymLookupStub(ByteArray* jni_stub_array) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700943 CHECK(jni_stub_array != NULL) << " jni_stub_array=" << jni_stub_array;
944 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array)
945 << "jni_stub_array_=" << jni_stub_array_ << " jni_stub_array=" << jni_stub_array;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700946 jni_stub_array_ = jni_stub_array;
947}
948
Brian Carlstrome24fa612011-09-29 00:53:55 -0700949void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) {
950 CHECK(abstract_method_error_stub_array != NULL);
951 CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array);
952 abstract_method_error_stub_array_ = abstract_method_error_stub_array;
953}
954
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700955void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700956 CHECK(resolution_stub_array != NULL);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700957 CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array);
958 resolution_stub_array_[type] = resolution_stub_array;
Ian Rogersad25ac52011-10-04 19:13:33 -0700959}
960
Ian Rogers19846512012-02-24 11:42:47 -0800961Method* Runtime::CreateResolutionMethod() {
962 Class* method_class = Method::GetMethodClass();
963 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
964 method->SetDeclaringClass(method_class);
965 // TODO: use a special method for resolution method saves
966 method->SetDexMethodIndex(DexFile::kDexNoIndex16);
967 ByteArray* unknown_resolution_stub = GetResolutionStubArray(kUnknownMethod);
968 CHECK(unknown_resolution_stub != NULL);
969 method->SetCode(unknown_resolution_stub->GetData());
970 return method.get();
971}
972
Brian Carlstromae826982011-11-09 01:33:42 -0800973Method* Runtime::CreateCalleeSaveMethod(InstructionSet instruction_set, CalleeSaveType type) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700974 Class* method_class = Method::GetMethodClass();
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700975 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
Ian Rogersff1ed472011-09-20 13:46:24 -0700976 method->SetDeclaringClass(method_class);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800977 // TODO: use a special method for callee saves
Ian Rogers19846512012-02-24 11:42:47 -0800978 method->SetDexMethodIndex(DexFile::kDexNoIndex16);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700979 method->SetCode(NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800980 if ((instruction_set == kThumb2) || (instruction_set == kArm)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700981 uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6) | (1 << art::arm::R7) |
982 (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11);
983 uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3);
984 uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9);
985 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills :0) |
986 (type == kSaveAll ? all_spills :0) | (1 << art::arm::LR);
987 uint32_t fp_all_spills = (1 << art::arm::S0) | (1 << art::arm::S1) | (1 << art::arm::S2) |
988 (1 << art::arm::S3) | (1 << art::arm::S4) | (1 << art::arm::S5) |
989 (1 << art::arm::S6) | (1 << art::arm::S7) | (1 << art::arm::S8) |
990 (1 << art::arm::S9) | (1 << art::arm::S10) | (1 << art::arm::S11) |
991 (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) |
992 (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) |
993 (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) |
994 (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) |
995 (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) |
996 (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
997 (1 << art::arm::S30) | (1 << art::arm::S31);
998 uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
999 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
1000 __builtin_popcount(fp_spills) /* fprs */ +
1001 1 /* Method* */) * kPointerSize, kStackAlignment);
Ian Rogers15fdb8c2011-09-25 15:45:07 -07001002 method->SetFrameSizeInBytes(frame_size);
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001003 method->SetCoreSpillMask(core_spills);
1004 method->SetFpSpillMask(fp_spills);
Brian Carlstromae826982011-11-09 01:33:42 -08001005 } else if (instruction_set == kX86) {
Ian Rogers7caad772012-03-30 01:07:54 -07001006 uint32_t ref_spills = (1 << art::x86::EBP) | (1 << art::x86::ESI) | (1 << art::x86::EDI);
1007 uint32_t arg_spills = (1 << art::x86::ECX) | (1 << art::x86::EDX) | (1 << art::x86::EBX);
1008 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills : 0) |
1009 (1 << art::x86::kNumberOfCpuRegisters); // fake return address callee save
1010 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
1011 1 /* Method* */) * kPointerSize, kStackAlignment);
1012 method->SetFrameSizeInBytes(frame_size);
1013 method->SetCoreSpillMask(core_spills);
Ian Rogersff1ed472011-09-20 13:46:24 -07001014 method->SetFpSpillMask(0);
1015 } else {
1016 UNIMPLEMENTED(FATAL);
1017 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001018 return method.get();
Ian Rogersff1ed472011-09-20 13:46:24 -07001019}
1020
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001021void Runtime::SetCalleeSaveMethod(Method* method, CalleeSaveType type) {
1022 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
Elliott Hughes225f5a12012-06-11 11:23:48 -07001023 callee_save_methods_[type] = method;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001024}
1025
jeffhao2692b572011-12-16 15:42:28 -08001026void Runtime::EnableMethodTracing(Trace* tracer) {
1027 CHECK(!IsMethodTracingActive());
1028 tracer_ = tracer;
1029}
1030
1031void Runtime::DisableMethodTracing() {
1032 CHECK(IsMethodTracingActive());
1033 delete tracer_;
1034 tracer_ = NULL;
1035}
1036
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001037const std::vector<const DexFile*>& Runtime::GetCompileTimeClassPath(jobject class_loader) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001038 if (class_loader == NULL) {
1039 return GetClassLinker()->GetBootClassPath();
1040 }
1041 CHECK(UseCompileTimeClassPath());
1042 CompileTimeClassPaths::const_iterator it = compile_time_class_paths_.find(class_loader);
1043 CHECK(it != compile_time_class_paths_.end());
1044 return it->second;
1045}
1046
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001047void Runtime::SetCompileTimeClassPath(jobject class_loader, std::vector<const DexFile*>& class_path) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001048 CHECK(!IsStarted());
1049 use_compile_time_class_path_ = true;
Elliott Hughesa0e18062012-04-13 15:59:59 -07001050 compile_time_class_paths_.Put(class_loader, class_path);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001051}
1052
Carl Shapiro1fb86202011-06-27 17:43:13 -07001053} // namespace art