blob: 79f75c76b1fc551b93e65b010455c7a6f898cf83 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro1fb86202011-06-27 17:43:13 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "runtime.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070018
Brian Carlstromdbf05b72011-12-15 00:55:24 -080019#include <signal.h>
Elliott Hughesd06a6c72012-05-30 17:59:06 -070020#include <sys/syscall.h>
Brian Carlstromdbf05b72011-12-15 00:55:24 -080021
Elliott Hughesffe67362011-07-17 12:09:27 -070022#include <cstdio>
23#include <cstdlib>
Brian Carlstrom8a436592011-08-15 21:27:23 -070024#include <limits>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070025#include <vector>
Elliott Hughesffe67362011-07-17 12:09:27 -070026
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070027#include "class_linker.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070028#include "class_loader.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070029#include "constants_arm.h"
30#include "constants_x86.h"
Elliott Hughes3bb81562011-10-21 18:52:59 -070031#include "debugger.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070032#include "heap.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070033#include "image.h"
Elliott Hughescf4c6c42011-09-01 15:16:42 -070034#include "intern_table.h"
Elliott Hughesc5f7c912011-08-18 14:00:42 -070035#include "jni_internal.h"
Elliott Hughes32d6e1e2011-10-11 14:47:44 -070036#include "monitor.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070037#include "oat_file.h"
Mathieu Chartiera6399032012-06-11 18:49:50 -070038#include "scoped_heap_lock.h"
Ian Rogers365c1022012-06-22 15:05:28 -070039#include "scoped_jni_thread_state.h"
Elliott Hughes726079d2011-10-07 18:43:44 -070040#include "ScopedLocalRef.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070041#include "signal_catcher.h"
Elliott Hughes457005c2012-04-16 13:54:25 -070042#include "signal_set.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070043#include "space.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070044#include "thread.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070045#include "thread_list.h"
jeffhaob5e81852012-03-12 11:15:45 -070046#include "trace.h"
Elliott Hughes3bb81562011-10-21 18:52:59 -070047#include "UniquePtr.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070048#include "verifier/method_verifier.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070049#include "well_known_classes.h"
Carl Shapiro61e019d2011-07-14 16:53:09 -070050
Logan Chien971bf3f2012-05-01 15:47:55 +080051#if defined(ART_USE_LLVM_COMPILER)
52#include "compiler_llvm/procedure_linkage_table.h"
53#endif
54
Elliott Hugheseac76672012-05-24 21:56:51 -070055#include "JniConstants.h" // Last to avoid LOG redefinition in ics-mr1-plus-art.
Elliott Hughes90a33692011-08-30 13:27:07 -070056
Carl Shapiro1fb86202011-06-27 17:43:13 -070057namespace art {
58
Carl Shapiro2ed144c2011-07-26 16:52:08 -070059Runtime* Runtime::instance_ = NULL;
60
Elliott Hughesdcc24742011-09-07 14:02:44 -070061Runtime::Runtime()
Elliott Hughesd9c67be2012-02-02 19:54:06 -080062 : is_compiler_(false),
63 is_zygote_(false),
Mathieu Chartier069387a2012-06-18 12:01:01 -070064 is_concurrent_gc_enabled_(true),
Elliott Hughes6cf23882012-06-15 15:42:07 -070065 default_stack_size_(0),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080066 heap_(NULL),
Elliott Hughesc33a32b2011-10-11 18:18:07 -070067 monitor_list_(NULL),
Elliott Hughesdcc24742011-09-07 14:02:44 -070068 thread_list_(NULL),
69 intern_table_(NULL),
70 class_linker_(NULL),
71 signal_catcher_(NULL),
72 java_vm_(NULL),
Elliott Hughes225f5a12012-06-11 11:23:48 -070073 pre_allocated_OutOfMemoryError_(NULL),
Brian Carlstrom16192862011-09-12 17:50:06 -070074 jni_stub_array_(NULL),
Brian Carlstrome24fa612011-09-29 00:53:55 -070075 abstract_method_error_stub_array_(NULL),
Ian Rogers19846512012-02-24 11:42:47 -080076 resolution_method_(NULL),
Ian Rogers7c3757f2012-02-15 17:18:53 -080077 system_class_loader_(NULL),
Elliott Hughes6b355752012-01-13 16:49:08 -080078 shutting_down_(false),
Elliott Hughesdcc24742011-09-07 14:02:44 -070079 started_(false),
Mathieu Chartier7664f5c2012-06-08 18:15:32 -070080 finished_starting_(false),
Elliott Hughesdcc24742011-09-07 14:02:44 -070081 vfprintf_(NULL),
82 exit_(NULL),
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070083 abort_(NULL),
jeffhao2692b572011-12-16 15:42:28 -080084 stats_enabled_(false),
Ian Rogers776ac1f2012-04-13 23:36:36 -070085 method_trace_(0),
86 method_trace_file_size_(0),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080087 tracer_(NULL),
Ian Rogers365c1022012-06-22 15:05:28 -070088 use_compile_time_class_path_(false),
89 main_thread_group_(NULL),
Logan Chien971bf3f2012-05-01 15:47:55 +080090 system_thread_group_(NULL)
91#if defined(ART_USE_LLVM_COMPILER)
92#if defined(__arm__)
93 , plt_(kArm)
94#elif defined(__mips__)
95 , plt_(kMips)
96#elif defined(__i386__)
97 , plt_(kX86)
98#endif
99#endif
100 {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700101 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
102 resolution_stub_array_[i] = NULL;
103 }
104 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
Elliott Hughes225f5a12012-06-11 11:23:48 -0700105 callee_save_methods_[i] = NULL;
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700106 }
Logan Chien971bf3f2012-05-01 15:47:55 +0800107
108#if defined(ART_USE_LLVM_COMPILER)
109 CHECK(plt_.AllocateTable()) << "Failed to allocate PLT";
110#endif
Elliott Hughesdcc24742011-09-07 14:02:44 -0700111}
112
Carl Shapiro61e019d2011-07-14 16:53:09 -0700113Runtime::~Runtime() {
Elliott Hughes6b355752012-01-13 16:49:08 -0800114 shutting_down_ = true;
115
jeffhaob5e81852012-03-12 11:15:45 -0700116 if (IsMethodTracingActive()) {
117 Trace::Shutdown();
118 }
119
Mathieu Chartiera6399032012-06-11 18:49:50 -0700120 // Make sure to let the GC complete if it is running.
121 {
122 ScopedHeapLock heap_lock;
123 heap_->WaitForConcurrentGcToComplete();
124 }
125
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700126 // Make sure our internal threads are dead before we start tearing down things they're using.
Elliott Hughese52e49b2012-04-02 16:05:44 -0700127 Dbg::StopJdwp();
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700128 delete signal_catcher_;
129
Elliott Hughes038a8062011-09-18 14:12:41 -0700130 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
Elliott Hughes93e74e82011-09-13 11:07:03 -0700131 delete thread_list_;
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700132 delete monitor_list_;
Elliott Hughes93e74e82011-09-13 11:07:03 -0700133
Carl Shapiro61e019d2011-07-14 16:53:09 -0700134 delete class_linker_;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800135 delete heap_;
Elliott Hughes0a1038b2012-06-14 16:24:17 -0700136 verifier::MethodVerifier::Shutdown();
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700137 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700138 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700139 Thread::Shutdown();
Elliott Hughes7c6169d2012-05-02 16:11:48 -0700140 QuasiAtomic::Shutdown();
141
Carl Shapiro4acf4642011-07-26 18:54:13 -0700142 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700143 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -0700144 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700145}
146
Elliott Hughescac6cc72011-11-03 20:31:21 -0700147static bool gAborting = false;
148
Elliott Hughese0918552011-10-28 17:18:29 -0700149struct AbortState {
150 void Dump(std::ostream& os) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700151 if (gAborting) {
152 os << "Runtime aborting --- recursively, so no thread-specific detail!\n";
153 return;
154 }
155 gAborting = true;
Elliott Hughese0918552011-10-28 17:18:29 -0700156 os << "Runtime aborting...\n";
Elliott Hughes6c7d2442012-02-01 18:40:47 -0800157 if (Runtime::Current() == NULL) {
158 os << "(Runtime does not yet exist!)\n";
159 return;
160 }
Elliott Hughese0918552011-10-28 17:18:29 -0700161 Thread* self = Thread::Current();
162 if (self == NULL) {
163 os << "(Aborting thread was not attached to runtime!)\n";
164 } else {
Elliott Hughes899e7892012-01-24 14:57:32 -0800165 self->Dump(os);
166 if (self->IsExceptionPending()) {
167 os << "Pending " << PrettyTypeOf(self->GetException()) << " on thread:\n"
168 << self->GetException()->Dump();
169 }
Elliott Hughese0918552011-10-28 17:18:29 -0700170 }
171 }
172};
173
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800174static Mutex& GetAbortLock() {
175 static Mutex abort_lock("abort lock");
176 return abort_lock;
177}
178
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700179void Runtime::Abort() {
Elliott Hughes131aef82012-01-27 11:53:35 -0800180 // Ensure that we don't have multiple threads trying to abort at once,
181 // which would result in significantly worse diagnostics.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800182 MutexLock mu(GetAbortLock());
Elliott Hughes131aef82012-01-27 11:53:35 -0800183
Elliott Hughesffe67362011-07-17 12:09:27 -0700184 // Get any pending output out of the way.
185 fflush(NULL);
186
187 // Many people have difficulty distinguish aborts from crashes,
188 // so be explicit.
Elliott Hughese0918552011-10-28 17:18:29 -0700189 AbortState state;
Elliott Hughes9ee5f9c2012-02-03 18:33:16 -0800190 LOG(INTERNAL_FATAL) << Dumpable<AbortState>(state);
Elliott Hughesffe67362011-07-17 12:09:27 -0700191
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700192 // Call the abort hook if we have one.
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700193 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700194 LOG(INTERNAL_FATAL) << "Calling abort hook...";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700195 Runtime::Current()->abort_();
196 // notreached
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700197 LOG(INTERNAL_FATAL) << "Unexpectedly returned from abort hook!";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700198 }
199
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700200#if defined(__BIONIC__)
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700201 // TODO: finish merging patches to fix abort(3) in bionic, then lose this!
202 // Bionic doesn't implement POSIX semantics for abort(3) in a multi-threaded
203 // process, so if we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -0700204 // receive SIGABRT. debuggerd dumps the stack trace of the main
205 // thread, whether or not that was the thread that failed. By
206 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -0700207 // fault in the current thread, and get a useful log from debuggerd.
Elliott Hughes81ff3182012-03-23 20:35:56 -0700208 // We can also trivially tell the difference between a crash and
Elliott Hughesffe67362011-07-17 12:09:27 -0700209 // a deliberate abort by looking at the fault address.
210 *reinterpret_cast<char*>(0xdeadd00d) = 38;
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700211#elif defined(__APPLE__)
212 // TODO: check that this actually gives good stack traces on the Mac!
213 pthread_kill(pthread_self(), SIGABRT);
214#else
215 // TODO: we ought to be able to use pthread_kill(3) here (or abort(3),
216 // which POSIX defines in terms of raise(3), which POSIX defines in terms
217 // of pthread_kill(3)). On Linux, though, libcorkscrew can't unwind through
218 // libpthread, which means the stacks we dump would be useless. Calling
219 // tgkill(2) directly avoids that.
220 syscall(__NR_tgkill, getpid(), GetTid(), SIGABRT);
221#endif
Elliott Hughesffe67362011-07-17 12:09:27 -0700222 // notreached
223}
224
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
Elliott Hughes0af55432011-08-17 18:37:28 -0700313void LoadJniLibrary(JavaVMExt* vm, const char* name) {
Elliott Hughes6cc332e2012-01-20 22:59:20 -0800314 std::string mapped_name(StringPrintf(OS_SHARED_LIB_FORMAT_STR, name));
Elliott Hughes75770752011-08-24 17:52:38 -0700315 std::string reason;
316 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700317 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
318 << reason;
319 }
320}
321
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700322Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700323 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800324 const char* boot_class_path_string = getenv("BOOTCLASSPATH");
325 if (boot_class_path_string != NULL) {
326 parsed->boot_class_path_string_ = boot_class_path_string;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700327 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800328 const char* class_path_string = getenv("CLASSPATH");
329 if (class_path_string != NULL) {
330 parsed->class_path_string_ = class_path_string;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700331 }
Elliott Hughes67d92002012-03-26 15:08:51 -0700332 // -Xcheck:jni is off by default for regular builds but on by default in debug builds.
333 parsed->check_jni_ = kIsDebugBuild;
Elliott Hughes85d15452011-09-16 17:33:01 -0700334
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700335 parsed->heap_initial_size_ = Heap::kInitialSize;
336 parsed->heap_maximum_size_ = Heap::kMaximumSize;
Elliott Hughes6cf23882012-06-15 15:42:07 -0700337 parsed->heap_growth_limit_ = 0; // 0 means no growth limit.
338 parsed->stack_size_ = 0; // 0 means default.
Brian Carlstromf734cf52011-08-17 16:28:14 -0700339
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800340 parsed->is_compiler_ = false;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700341 parsed->is_zygote_ = false;
Mathieu Chartier069387a2012-06-18 12:01:01 -0700342 parsed->is_concurrent_gc_enabled_ = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700343
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700344 parsed->jni_globals_max_ = 0;
Elliott Hughesfc861622011-10-17 17:57:47 -0700345 parsed->lock_profiling_threshold_ = 0;
346 parsed->hook_is_sensitive_thread_ = NULL;
347
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700348 parsed->hook_vfprintf_ = vfprintf;
349 parsed->hook_exit_ = exit;
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700350 parsed->hook_abort_ = NULL; // We don't call abort(3) by default; see Runtime::Abort.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700351
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800352// gLogVerbosity.class_linker = true; // TODO: don't check this in!
353// gLogVerbosity.compiler = true; // TODO: don't check this in!
354// gLogVerbosity.heap = true; // TODO: don't check this in!
355// gLogVerbosity.gc = true; // TODO: don't check this in!
356// gLogVerbosity.jdwp = true; // TODO: don't check this in!
357// gLogVerbosity.jni = true; // TODO: don't check this in!
358// gLogVerbosity.monitor = true; // TODO: don't check this in!
359// gLogVerbosity.startup = true; // TODO: don't check this in!
360// gLogVerbosity.third_party_jni = true; // TODO: don't check this in!
361// gLogVerbosity.threads = true; // TODO: don't check this in!
362
jeffhaob5e81852012-03-12 11:15:45 -0700363 parsed->method_trace_ = false;
364 parsed->method_trace_file_ = "/data/method-trace-file.bin";
365 parsed->method_trace_file_size_ = 10 * MB;
366
Brian Carlstrom8a436592011-08-15 21:27:23 -0700367 for (size_t i = 0; i < options.size(); ++i) {
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800368 const std::string option(options[i].first);
Brian Carlstrom0796af02011-10-12 14:31:45 -0700369 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700370 LOG(INFO) << "option[" << i << "]=" << option;
371 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800372 if (StartsWith(option, "-Xbootclasspath:")) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800373 parsed->boot_class_path_string_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700374 } else if (option == "-classpath" || option == "-cp") {
375 // TODO: support -Djava.class.path
376 i++;
377 if (i == options.size()) {
378 // TODO: usage
379 LOG(FATAL) << "Missing required class path value for " << option;
380 return NULL;
381 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800382 const StringPiece& value = options[i].first;
383 parsed->class_path_string_ = value.data();
384 } else if (option == "bootclasspath") {
385 parsed->boot_class_path_
386 = reinterpret_cast<const std::vector<const DexFile*>*>(options[i].second);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800387 } else if (StartsWith(option, "-Ximage:")) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800388 parsed->image_ = option.substr(strlen("-Ximage:")).data();
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800389 } else if (StartsWith(option, "-Xcheck:jni")) {
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700390 parsed->check_jni_ = true;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800391 } else if (StartsWith(option, "-Xrunjdwp:") || StartsWith(option, "-agentlib:jdwp=")) {
392 std::string tail(option.substr(option[1] == 'X' ? 10 : 15));
Elliott Hughes3bb81562011-10-21 18:52:59 -0700393 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
394 LOG(FATAL) << "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
395 << "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n";
396 return NULL;
397 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800398 } else if (StartsWith(option, "-Xms")) {
399 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).c_str(), 1024);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700400 if (size == 0) {
401 if (ignore_unrecognized) {
402 continue;
403 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700404 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700405 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700406 return NULL;
407 }
408 parsed->heap_initial_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800409 } else if (StartsWith(option, "-Xmx")) {
410 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).c_str(), 1024);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700411 if (size == 0) {
412 if (ignore_unrecognized) {
413 continue;
414 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700415 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700416 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700417 return NULL;
418 }
419 parsed->heap_maximum_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800420 } else if (StartsWith(option, "-XX:HeapGrowthLimit=")) {
421 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).c_str(), 1024);
jeffhaoc1160702011-10-27 15:48:45 -0700422 if (size == 0) {
423 if (ignore_unrecognized) {
424 continue;
425 }
426 // TODO: usage
427 LOG(FATAL) << "Failed to parse " << option;
428 return NULL;
429 }
430 parsed->heap_growth_limit_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800431 } else if (StartsWith(option, "-Xss")) {
432 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).c_str(), 1);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700433 if (size == 0) {
434 if (ignore_unrecognized) {
435 continue;
436 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700437 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700438 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700439 return NULL;
440 }
441 parsed->stack_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800442 } else if (StartsWith(option, "-D")) {
443 parsed->properties_.push_back(option.substr(strlen("-D")));
444 } else if (StartsWith(option, "-Xjnitrace:")) {
445 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:"));
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700446 } else if (option == "compiler") {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800447 parsed->is_compiler_ = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700448 } else if (option == "-Xzygote") {
449 parsed->is_zygote_ = true;
Mathieu Chartier069387a2012-06-18 12:01:01 -0700450 } else if (StartsWith(option, "-Xgc:")) {
451 std::vector<std::string> gc_options;
452 Split(option.substr(strlen("-Xgc:")), ',', gc_options);
453 for (size_t i = 0; i < gc_options.size(); ++i) {
454 if (gc_options[i] == "noconcurrent") {
455 parsed->is_concurrent_gc_enabled_ = false;
456 } else if (gc_options[i] == "concurrent") {
457 parsed->is_concurrent_gc_enabled_ = true;
458 } else {
459 LOG(WARNING) << "Ignoring unknown -Xgc option: " << gc_options[i];
460 }
461 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800462 } else if (StartsWith(option, "-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700463 std::vector<std::string> verbose_options;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800464 Split(option.substr(strlen("-verbose:")), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700465 for (size_t i = 0; i < verbose_options.size(); ++i) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800466 if (verbose_options[i] == "class") {
467 gLogVerbosity.class_linker = true;
468 } else if (verbose_options[i] == "compiler") {
469 gLogVerbosity.compiler = true;
470 } else if (verbose_options[i] == "heap") {
471 gLogVerbosity.heap = true;
472 } else if (verbose_options[i] == "gc") {
473 gLogVerbosity.gc = true;
474 } else if (verbose_options[i] == "jdwp") {
475 gLogVerbosity.jdwp = true;
476 } else if (verbose_options[i] == "jni") {
477 gLogVerbosity.jni = true;
478 } else if (verbose_options[i] == "monitor") {
479 gLogVerbosity.monitor = true;
480 } else if (verbose_options[i] == "startup") {
481 gLogVerbosity.startup = true;
482 } else if (verbose_options[i] == "third-party-jni") {
483 gLogVerbosity.third_party_jni = true;
484 } else if (verbose_options[i] == "threads") {
485 gLogVerbosity.threads = true;
486 } else {
487 LOG(WARNING) << "Ignoring unknown -verbose option: " << verbose_options[i];
488 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700489 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800490 } else if (StartsWith(option, "-Xjnigreflimit:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700491 parsed->jni_globals_max_ = ParseIntegerOrDie(option);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800492 } else if (StartsWith(option, "-Xlockprofthreshold:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700493 parsed->lock_profiling_threshold_ = ParseIntegerOrDie(option);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800494 } else if (StartsWith(option, "-Xstacktracefile:")) {
Elliott Hughes206c20d2012-06-28 11:05:37 -0700495 parsed->stack_trace_file_ = option.substr(strlen("-Xstacktracefile:"));
Elliott Hughesfc861622011-10-17 17:57:47 -0700496 } else if (option == "sensitiveThread") {
497 parsed->hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(options[i].second);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700498 } else if (option == "vfprintf") {
499 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
500 } else if (option == "exit") {
501 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
502 } else if (option == "abort") {
503 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700504 } else if (option == "host-prefix") {
505 parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second);
Elliott Hughesb3e66df2012-01-12 14:49:18 -0800506 } else if (option == "-Xgenregmap" || option == "-Xgc:precise") {
507 // We silently ignore these for backwards compatibility.
jeffhaob5e81852012-03-12 11:15:45 -0700508 } else if (option == "-Xmethod-trace") {
509 parsed->method_trace_ = true;
510 } else if (StartsWith(option, "-Xmethod-trace-file:")) {
511 parsed->method_trace_file_ = option.substr(strlen("-Xmethod-trace-file:"));
512 } else if (StartsWith(option, "-Xmethod-trace-file-size:")) {
513 parsed->method_trace_file_size_ = ParseIntegerOrDie(option);
Elliott Hughese119a362012-05-22 17:37:06 -0700514 } else if (option == "-Xprofile:threadcpuclock") {
515 Trace::SetDefaultClockSource(kProfilerClockSourceThreadCpu);
516 } else if (option == "-Xprofile:wallclock") {
517 Trace::SetDefaultClockSource(kProfilerClockSourceWall);
518 } else if (option == "-Xprofile:dualclock") {
519 Trace::SetDefaultClockSource(kProfilerClockSourceDual);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700520 } else {
521 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700522 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700523 LOG(ERROR) << "Unrecognized option " << option;
524 // TODO: this should exit, but for now tolerate unknown options
525 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700526 }
527 }
528 }
529
Brian Carlstrom223f20f2012-02-04 23:06:55 -0800530 if (!parsed->is_compiler_ && parsed->image_.empty()) {
531 parsed->image_ += GetAndroidRoot();
532 parsed->image_ += "/framework/boot.art";
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700533 }
jeffhaoc1160702011-10-27 15:48:45 -0700534 if (parsed->heap_growth_limit_ == 0) {
535 parsed->heap_growth_limit_ = parsed->heap_maximum_size_;
536 }
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700537
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700538 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700539}
540
541Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700542 // TODO: acquire a static mutex on Runtime to avoid racing.
543 if (Runtime::instance_ != NULL) {
544 return NULL;
545 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700546 instance_ = new Runtime;
547 if (!instance_->Init(options, ignore_unrecognized)) {
548 delete instance_;
549 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700550 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700551 return instance_;
552}
Elliott Hughes0af55432011-08-17 18:37:28 -0700553
Ian Rogers365c1022012-06-22 15:05:28 -0700554static void CreateSystemClassLoader() {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800555 if (Runtime::Current()->UseCompileTimeClassPath()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700556 return;
557 }
558
Ian Rogers365c1022012-06-22 15:05:28 -0700559 ScopedJniThreadState ts(Thread::Current());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700560
Ian Rogers365c1022012-06-22 15:05:28 -0700561 Class* class_loader_class = ts.Decode<Class*>(WellKnownClasses::java_lang_ClassLoader);
562 CHECK(Runtime::Current()->GetClassLinker()->EnsureInitialized(class_loader_class, true, true));
Brian Carlstromaded5f72011-10-07 17:15:04 -0700563
Ian Rogers365c1022012-06-22 15:05:28 -0700564 Method* getSystemClassLoader = class_loader_class->FindDirectMethod("getSystemClassLoader", "()Ljava/lang/ClassLoader;");
Brian Carlstromdf143242011-10-10 18:05:34 -0700565 CHECK(getSystemClassLoader != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700566
Ian Rogers365c1022012-06-22 15:05:28 -0700567 ClassLoader* class_loader =
568 down_cast<ClassLoader*>(InvokeWithJValues(ts, NULL, getSystemClassLoader, NULL).GetL());
569 CHECK(class_loader != NULL);
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500570
Ian Rogers365c1022012-06-22 15:05:28 -0700571 ts.Self()->SetClassLoaderOverride(class_loader);
572
573 Class* thread_class = ts.Decode<Class*>(WellKnownClasses::java_lang_Thread);
574 CHECK(Runtime::Current()->GetClassLinker()->EnsureInitialized(thread_class, true, true));
575
576 Field* contextClassLoader = thread_class->FindDeclaredInstanceField("contextClassLoader",
577 "Ljava/lang/ClassLoader;");
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500578 CHECK(contextClassLoader != NULL);
Ian Rogers365c1022012-06-22 15:05:28 -0700579
580 contextClassLoader->SetObject(ts.Self()->GetPeer(), class_loader);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700581}
582
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700583void Runtime::Start() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800584 VLOG(startup) << "Runtime::Start entering";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700585
586 CHECK(host_prefix_.empty()) << host_prefix_;
587
Elliott Hughes225f5a12012-06-11 11:23:48 -0700588 // Relocate the OatFiles (ELF images).
Logan Chien0c717dd2012-03-28 18:31:07 +0800589 class_linker_->RelocateExecutable();
590
Elliott Hughes225f5a12012-06-11 11:23:48 -0700591 // Restore main thread state to kNative as expected by native code.
592 Thread* self = Thread::Current();
593 self->SetState(kNative);
594
595 // Pre-allocate an OutOfMemoryError for the double-OOME case.
596 self->ThrowNewException("Ljava/lang/OutOfMemoryError;",
597 "OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack available");
598 pre_allocated_OutOfMemoryError_ = self->GetException();
599 self->ClearException();
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700600
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700601 started_ = true;
602
Brian Carlstromae826982011-11-09 01:33:42 -0800603 // InitNativeMethods needs to be after started_ so that the classes
604 // it touches will have methods linked to the oat file if necessary.
605 InitNativeMethods();
606
Ian Rogers365c1022012-06-22 15:05:28 -0700607 // Initialize well known thread group values that may be accessed threads while attaching.
608 InitThreadGroups(self);
609
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500610 Thread::FinishStartup();
611
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700612 if (!is_zygote_) {
613 DidForkFromZygote();
614 }
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700615
Elliott Hughes85d15452011-09-16 17:33:01 -0700616 StartDaemonThreads();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700617
Brian Carlstromaded5f72011-10-07 17:15:04 -0700618 CreateSystemClassLoader();
619
Elliott Hughes225f5a12012-06-11 11:23:48 -0700620 self->GetJniEnv()->locals.AssertEmpty();
Elliott Hughes726079d2011-10-07 18:43:44 -0700621
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800622 VLOG(startup) << "Runtime::Start exiting";
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700623
624 finished_starting_ = true;
Elliott Hughes85d15452011-09-16 17:33:01 -0700625}
626
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700627void Runtime::DidForkFromZygote() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700628 is_zygote_ = false;
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700629
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700630 StartSignalCatcher();
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700631
632 // Start the JDWP thread. If the command-line debugger flags specified "suspend=y",
633 // this will pause the runtime, so we probably want this to come last.
634 Dbg::StartJdwp();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700635}
636
637void Runtime::StartSignalCatcher() {
638 if (!is_zygote_) {
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700639 signal_catcher_ = new SignalCatcher(stack_trace_file_);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700640 }
641}
642
Elliott Hughes85d15452011-09-16 17:33:01 -0700643void Runtime::StartDaemonThreads() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800644 VLOG(startup) << "Runtime::StartDaemonThreads entering";
Elliott Hughes85d15452011-09-16 17:33:01 -0700645
Elliott Hughes719b3232011-09-25 17:42:19 -0700646 Thread* self = Thread::Current();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700647
648 // Must be in the kNative state for calling native methods.
Elliott Hughes34e06962012-04-09 13:55:55 -0700649 CHECK_EQ(self->GetState(), kNative);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700650
Elliott Hughes719b3232011-09-25 17:42:19 -0700651 JNIEnv* env = self->GetJniEnv();
Elliott Hugheseac76672012-05-24 21:56:51 -0700652 env->CallStaticVoidMethod(WellKnownClasses::java_lang_Daemons, WellKnownClasses::java_lang_Daemons_start);
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800653 CHECK(!env->ExceptionCheck());
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700654
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800655 VLOG(startup) << "Runtime::StartDaemonThreads exiting";
Carl Shapiro61e019d2011-07-14 16:53:09 -0700656}
657
Elliott Hughes0af55432011-08-17 18:37:28 -0700658bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700659 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700660
Elliott Hughes90a33692011-08-30 13:27:07 -0700661 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
662 if (options.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700663 LOG(ERROR) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700664 return false;
665 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800666 VLOG(startup) << "Runtime::Init -verbose:startup enabled";
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700667
Elliott Hughes7c6169d2012-05-02 16:11:48 -0700668 QuasiAtomic::Startup();
669
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700670 SetJniGlobalsMax(options->jni_globals_max_);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800671 Monitor::Init(options->lock_profiling_threshold_, options->hook_is_sensitive_thread_);
Elliott Hughes32d6e1e2011-10-11 14:47:44 -0700672
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700673 host_prefix_ = options->host_prefix_;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800674 boot_class_path_string_ = options->boot_class_path_string_;
675 class_path_string_ = options->class_path_string_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700676 properties_ = options->properties_;
677
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800678 is_compiler_ = options->is_compiler_;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700679 is_zygote_ = options->is_zygote_;
Mathieu Chartier069387a2012-06-18 12:01:01 -0700680 is_concurrent_gc_enabled_ = options->is_concurrent_gc_enabled_;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700681
Elliott Hughes0af55432011-08-17 18:37:28 -0700682 vfprintf_ = options->hook_vfprintf_;
683 exit_ = options->hook_exit_;
684 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700685
Elliott Hughesbe759c62011-09-08 19:38:21 -0700686 default_stack_size_ = options->stack_size_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700687 stack_trace_file_ = options->stack_trace_file_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700688
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700689 monitor_list_ = new MonitorList;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800690 thread_list_ = new ThreadList;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700691 intern_table_ = new InternTable;
692
Elliott Hughes0a1038b2012-06-14 16:24:17 -0700693 verifier::MethodVerifier::Init();
Logan Chiendd361c92012-04-10 23:40:37 +0800694
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800695 heap_ = new Heap(options->heap_initial_size_,
696 options->heap_growth_limit_,
697 options->heap_maximum_size_,
698 options->image_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700699
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700700 BlockSignals();
Elliott Hughes457005c2012-04-16 13:54:25 -0700701 InitPlatformSignalHandlers();
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700702
Elliott Hughesa0957642011-09-02 14:27:33 -0700703 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700704
Elliott Hughesbe759c62011-09-08 19:38:21 -0700705 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700706
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700707 // ClassLinker needs an attached thread, but we can't fully attach a thread
Elliott Hughes462c9442012-03-23 18:47:50 -0700708 // without creating objects. We can't supply a thread group yet; it will be fixed later.
709 Thread::Attach("main", false, NULL);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700710
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700711 // Set us to runnable so tools using a runtime can allocate and GC by default
Elliott Hughes34e06962012-04-09 13:55:55 -0700712 Thread::Current()->SetState(kRunnable);
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700713
Ian Rogers141d6222012-04-05 12:23:06 -0700714 // Now we're attached, we can take the heap lock and validate the heap.
715 GetHeap()->EnableObjectValidation();
716
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800717 CHECK_GE(GetHeap()->GetSpaces().size(), 1U);
718 if (GetHeap()->GetSpaces()[0]->IsImageSpace()) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800719 class_linker_ = ClassLinker::CreateFromImage(intern_table_);
720 } else {
721 CHECK(options->boot_class_path_ != NULL);
722 CHECK_NE(options->boot_class_path_->size(), 0U);
723 class_linker_ = ClassLinker::CreateFromCompiler(*options->boot_class_path_, intern_table_);
724 }
725 CHECK(class_linker_ != NULL);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700726
jeffhaob5e81852012-03-12 11:15:45 -0700727 method_trace_ = options->method_trace_;
728 method_trace_file_ = options->method_trace_file_;
729 method_trace_file_size_ = options->method_trace_file_size_;
730
731 if (options->method_trace_) {
732 Trace::Start(options->method_trace_file_.c_str(), -1, options->method_trace_file_size_, 0, false);
733 }
734
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800735 VLOG(startup) << "Runtime::Init exiting";
Carl Shapiro1fb86202011-06-27 17:43:13 -0700736 return true;
737}
738
Elliott Hughes038a8062011-09-18 14:12:41 -0700739void Runtime::InitNativeMethods() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800740 VLOG(startup) << "Runtime::InitNativeMethods entering";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700741 Thread* self = Thread::Current();
742 JNIEnv* env = self->GetJniEnv();
743
Elliott Hughes418d20f2011-09-22 14:00:39 -0700744 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Elliott Hughes34e06962012-04-09 13:55:55 -0700745 CHECK_EQ(self->GetState(), kNative);
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!
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700758 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800759 VLOG(startup) << "Runtime::InitNativeMethods exiting";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700760}
761
Ian Rogers365c1022012-06-22 15:05:28 -0700762void Runtime::InitThreadGroups(Thread* self) {
763 JNIEnvExt* env = self->GetJniEnv();
764 ScopedJniEnvLocalRefState env_state(env);
765 main_thread_group_ =
766 env->NewGlobalRef(env->GetStaticObjectField(WellKnownClasses::java_lang_ThreadGroup,
767 WellKnownClasses::java_lang_ThreadGroup_mainThreadGroup));
768 system_thread_group_ =
769 env->NewGlobalRef(env->GetStaticObjectField(WellKnownClasses::java_lang_ThreadGroup,
770 WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup));
771}
772
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700773void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
774#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Ian Rogers5167c972012-02-03 10:41:20 -0800775 // Register Throwable first so that registration of other native methods can throw exceptions
776 REGISTER(register_java_lang_Throwable);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700777 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700778 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700779 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700780 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700781 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700782 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700783 REGISTER(register_java_lang_Object);
784 REGISTER(register_java_lang_Runtime);
785 REGISTER(register_java_lang_String);
786 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700787 REGISTER(register_java_lang_Thread);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700788 REGISTER(register_java_lang_VMClassLoader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700789 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700790 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700791 REGISTER(register_java_lang_reflect_Field);
792 REGISTER(register_java_lang_reflect_Method);
Jesse Wilson95caa792011-10-12 18:14:17 -0400793 REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700794 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700795 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700796 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700797 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700798#undef REGISTER
799}
800
Elliott Hughesc967f782012-04-16 10:23:15 -0700801void Runtime::DumpForSigQuit(std::ostream& os) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700802 GetClassLinker()->DumpForSigQuit(os);
803 GetInternTable()->DumpForSigQuit(os);
Elliott Hughesae80b492012-04-24 10:43:17 -0700804 GetJavaVM()->DumpForSigQuit(os);
Elliott Hughesc967f782012-04-16 10:23:15 -0700805 GetHeap()->DumpForSigQuit(os);
Elliott Hughes42ee1422011-09-06 12:33:32 -0700806 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700807
Elliott Hughesc967f782012-04-16 10:23:15 -0700808 thread_list_->DumpForSigQuit(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700809}
810
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800811void Runtime::DumpLockHolders(std::ostream& os) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800812 pid_t heap_lock_owner = GetHeap()->GetLockOwner();
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800813 pid_t thread_list_lock_owner = GetThreadList()->GetLockOwner();
814 pid_t classes_lock_owner = GetClassLinker()->GetClassesLockOwner();
815 pid_t dex_lock_owner = GetClassLinker()->GetDexLockOwner();
816 if ((heap_lock_owner | thread_list_lock_owner | classes_lock_owner | dex_lock_owner) != 0) {
817 os << "Heap lock owner tid: " << heap_lock_owner << "\n"
818 << "ThreadList lock owner tid: " << thread_list_lock_owner << "\n"
819 << "ClassLinker classes lock owner tid: " << classes_lock_owner << "\n"
820 << "ClassLinker dex lock owner tid: " << dex_lock_owner << "\n";
821 }
822}
823
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700824void Runtime::SetStatsEnabled(bool new_state) {
825 if (new_state == true) {
826 GetStats()->Clear(~0);
827 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
828 Thread::Current()->GetStats()->Clear(~0);
829 }
830 stats_enabled_ = new_state;
831}
832
833void Runtime::ResetStats(int kinds) {
834 GetStats()->Clear(kinds & 0xffff);
835 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
836 Thread::Current()->GetStats()->Clear(kinds >> 16);
837}
838
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700839int32_t Runtime::GetStat(int kind) {
840 RuntimeStats* stats;
841 if (kind < (1<<16)) {
842 stats = GetStats();
843 } else {
844 stats = Thread::Current()->GetStats();
845 kind >>= 16;
846 }
847 switch (kind) {
848 case KIND_ALLOCATED_OBJECTS:
849 return stats->allocated_objects;
850 case KIND_ALLOCATED_BYTES:
851 return stats->allocated_bytes;
852 case KIND_FREED_OBJECTS:
853 return stats->freed_objects;
854 case KIND_FREED_BYTES:
855 return stats->freed_bytes;
856 case KIND_GC_INVOCATIONS:
857 return stats->gc_for_alloc_count;
858 case KIND_CLASS_INIT_COUNT:
859 return stats->class_init_count;
860 case KIND_CLASS_INIT_TIME:
861 // Convert ns to us, reduce to 32 bits.
Elliott Hughes398f64b2012-03-26 18:05:48 -0700862 return static_cast<int>(stats->class_init_time_ns / 1000);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700863 case KIND_EXT_ALLOCATED_OBJECTS:
864 case KIND_EXT_ALLOCATED_BYTES:
865 case KIND_EXT_FREED_OBJECTS:
866 case KIND_EXT_FREED_BYTES:
867 return 0; // backward compatibility
868 default:
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700869 LOG(FATAL) << "Unknown statistic " << kind;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700870 return -1; // unreachable
871 }
872}
873
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700874void Runtime::BlockSignals() {
Elliott Hughes457005c2012-04-16 13:54:25 -0700875 SignalSet signals;
876 signals.Add(SIGPIPE);
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700877 // SIGQUIT is used to dump the runtime's state (including stack traces).
Elliott Hughes457005c2012-04-16 13:54:25 -0700878 signals.Add(SIGQUIT);
Elliott Hughes08795042012-04-03 14:48:52 -0700879 // SIGUSR1 is used to initiate a GC.
Elliott Hughes457005c2012-04-16 13:54:25 -0700880 signals.Add(SIGUSR1);
881 signals.Block();
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700882}
883
Ian Rogers365c1022012-06-22 15:05:28 -0700884void Runtime::AttachCurrentThread(const char* thread_name, bool as_daemon, jobject thread_group) {
Elliott Hughes462c9442012-03-23 18:47:50 -0700885 Thread::Attach(thread_name, as_daemon, thread_group);
Elliott Hughes22869a92012-03-27 14:08:24 -0700886 if (thread_name == NULL) {
887 LOG(WARNING) << *Thread::Current() << " attached without supplying a name";
888 }
Carl Shapiro61e019d2011-07-14 16:53:09 -0700889}
890
Elliott Hughesd92bec42011-09-02 17:04:36 -0700891void Runtime::DetachCurrentThread() {
Brian Carlstrom4d571432012-05-16 00:21:41 -0700892 Thread* self = Thread::Current();
893 if (self == NULL) {
894 LOG(FATAL) << "attempting to detach thread that is not attached";
895 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700896 if (self->HasManagedStack()) {
Elliott Hughes22869a92012-03-27 14:08:24 -0700897 LOG(FATAL) << *Thread::Current() << " attempting to detach while still running code";
898 }
Elliott Hughes02b48d12011-09-07 17:15:51 -0700899 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700900}
901
Brian Carlstrome24fa612011-09-29 00:53:55 -0700902void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700903 Dbg::VisitRoots(visitor, arg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700904 class_linker_->VisitRoots(visitor, arg);
905 intern_table_->VisitRoots(visitor, arg);
906 java_vm_->VisitRoots(visitor, arg);
907 thread_list_->VisitRoots(visitor, arg);
Elliott Hughes225f5a12012-06-11 11:23:48 -0700908 if (pre_allocated_OutOfMemoryError_ != NULL) {
909 visitor(pre_allocated_OutOfMemoryError_, arg);
910 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700911 visitor(jni_stub_array_, arg);
912 visitor(abstract_method_error_stub_array_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700913 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
914 visitor(resolution_stub_array_[i], arg);
915 }
Ian Rogers19846512012-02-24 11:42:47 -0800916 visitor(resolution_method_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700917 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
Elliott Hughes225f5a12012-06-11 11:23:48 -0700918 visitor(callee_save_methods_[i], arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700919 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700920}
921
Ian Rogers169c9a72011-11-13 20:13:17 -0800922void Runtime::SetJniDlsymLookupStub(ByteArray* jni_stub_array) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700923 CHECK(jni_stub_array != NULL) << " jni_stub_array=" << jni_stub_array;
924 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array)
925 << "jni_stub_array_=" << jni_stub_array_ << " jni_stub_array=" << jni_stub_array;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700926 jni_stub_array_ = jni_stub_array;
927}
928
Brian Carlstrome24fa612011-09-29 00:53:55 -0700929void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) {
930 CHECK(abstract_method_error_stub_array != NULL);
931 CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array);
932 abstract_method_error_stub_array_ = abstract_method_error_stub_array;
933}
934
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700935void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700936 CHECK(resolution_stub_array != NULL);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700937 CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array);
938 resolution_stub_array_[type] = resolution_stub_array;
Ian Rogersad25ac52011-10-04 19:13:33 -0700939}
940
Ian Rogers19846512012-02-24 11:42:47 -0800941Method* Runtime::CreateResolutionMethod() {
942 Class* method_class = Method::GetMethodClass();
943 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
944 method->SetDeclaringClass(method_class);
945 // TODO: use a special method for resolution method saves
946 method->SetDexMethodIndex(DexFile::kDexNoIndex16);
947 ByteArray* unknown_resolution_stub = GetResolutionStubArray(kUnknownMethod);
948 CHECK(unknown_resolution_stub != NULL);
949 method->SetCode(unknown_resolution_stub->GetData());
950 return method.get();
951}
952
Brian Carlstromae826982011-11-09 01:33:42 -0800953Method* Runtime::CreateCalleeSaveMethod(InstructionSet instruction_set, CalleeSaveType type) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700954 Class* method_class = Method::GetMethodClass();
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700955 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
Ian Rogersff1ed472011-09-20 13:46:24 -0700956 method->SetDeclaringClass(method_class);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800957 // TODO: use a special method for callee saves
Ian Rogers19846512012-02-24 11:42:47 -0800958 method->SetDexMethodIndex(DexFile::kDexNoIndex16);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700959 method->SetCode(NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800960 if ((instruction_set == kThumb2) || (instruction_set == kArm)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700961 uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6) | (1 << art::arm::R7) |
962 (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11);
963 uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3);
964 uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9);
965 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills :0) |
966 (type == kSaveAll ? all_spills :0) | (1 << art::arm::LR);
967 uint32_t fp_all_spills = (1 << art::arm::S0) | (1 << art::arm::S1) | (1 << art::arm::S2) |
968 (1 << art::arm::S3) | (1 << art::arm::S4) | (1 << art::arm::S5) |
969 (1 << art::arm::S6) | (1 << art::arm::S7) | (1 << art::arm::S8) |
970 (1 << art::arm::S9) | (1 << art::arm::S10) | (1 << art::arm::S11) |
971 (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) |
972 (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) |
973 (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) |
974 (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) |
975 (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) |
976 (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
977 (1 << art::arm::S30) | (1 << art::arm::S31);
978 uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
979 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
980 __builtin_popcount(fp_spills) /* fprs */ +
981 1 /* Method* */) * kPointerSize, kStackAlignment);
Ian Rogers15fdb8c2011-09-25 15:45:07 -0700982 method->SetFrameSizeInBytes(frame_size);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700983 method->SetCoreSpillMask(core_spills);
984 method->SetFpSpillMask(fp_spills);
Brian Carlstromae826982011-11-09 01:33:42 -0800985 } else if (instruction_set == kX86) {
Ian Rogers7caad772012-03-30 01:07:54 -0700986 uint32_t ref_spills = (1 << art::x86::EBP) | (1 << art::x86::ESI) | (1 << art::x86::EDI);
987 uint32_t arg_spills = (1 << art::x86::ECX) | (1 << art::x86::EDX) | (1 << art::x86::EBX);
988 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills : 0) |
989 (1 << art::x86::kNumberOfCpuRegisters); // fake return address callee save
990 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
991 1 /* Method* */) * kPointerSize, kStackAlignment);
992 method->SetFrameSizeInBytes(frame_size);
993 method->SetCoreSpillMask(core_spills);
Ian Rogersff1ed472011-09-20 13:46:24 -0700994 method->SetFpSpillMask(0);
995 } else {
996 UNIMPLEMENTED(FATAL);
997 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700998 return method.get();
Ian Rogersff1ed472011-09-20 13:46:24 -0700999}
1000
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001001void Runtime::SetCalleeSaveMethod(Method* method, CalleeSaveType type) {
1002 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
Elliott Hughes225f5a12012-06-11 11:23:48 -07001003 callee_save_methods_[type] = method;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001004}
1005
jeffhao2692b572011-12-16 15:42:28 -08001006void Runtime::EnableMethodTracing(Trace* tracer) {
1007 CHECK(!IsMethodTracingActive());
1008 tracer_ = tracer;
1009}
1010
1011void Runtime::DisableMethodTracing() {
1012 CHECK(IsMethodTracingActive());
1013 delete tracer_;
1014 tracer_ = NULL;
1015}
1016
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001017const std::vector<const DexFile*>& Runtime::GetCompileTimeClassPath(const ClassLoader* class_loader) {
1018 if (class_loader == NULL) {
1019 return GetClassLinker()->GetBootClassPath();
1020 }
1021 CHECK(UseCompileTimeClassPath());
1022 CompileTimeClassPaths::const_iterator it = compile_time_class_paths_.find(class_loader);
1023 CHECK(it != compile_time_class_paths_.end());
1024 return it->second;
1025}
1026
1027void Runtime::SetCompileTimeClassPath(const ClassLoader* class_loader, std::vector<const DexFile*>& class_path) {
1028 CHECK(!IsStarted());
1029 use_compile_time_class_path_ = true;
Elliott Hughesa0e18062012-04-13 15:59:59 -07001030 compile_time_class_paths_.Put(class_loader, class_path);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001031}
1032
Carl Shapiro1fb86202011-06-27 17:43:13 -07001033} // namespace art