blob: e1c3099b0fedf7e6e477befd35f50ca9d5adce27 [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"
Elliott Hughese27955c2011-08-26 15:21:24 -070039#include "signal_catcher.h"
Elliott Hughes457005c2012-04-16 13:54:25 -070040#include "signal_set.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070041#include "space.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070042#include "thread.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070043#include "thread_list.h"
jeffhaob5e81852012-03-12 11:15:45 -070044#include "trace.h"
Elliott Hughes3bb81562011-10-21 18:52:59 -070045#include "UniquePtr.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070046#include "verifier/method_verifier.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070047#include "well_known_classes.h"
Carl Shapiro61e019d2011-07-14 16:53:09 -070048
Elliott Hugheseac76672012-05-24 21:56:51 -070049#include "JniConstants.h" // Last to avoid LOG redefinition in ics-mr1-plus-art.
Elliott Hughes90a33692011-08-30 13:27:07 -070050
Carl Shapiro1fb86202011-06-27 17:43:13 -070051namespace art {
52
Carl Shapiro2ed144c2011-07-26 16:52:08 -070053Runtime* Runtime::instance_ = NULL;
54
Elliott Hughesdcc24742011-09-07 14:02:44 -070055Runtime::Runtime()
Elliott Hughesd9c67be2012-02-02 19:54:06 -080056 : is_compiler_(false),
57 is_zygote_(false),
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -070058 default_stack_size_(Thread::kDefaultStackSize),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080059 heap_(NULL),
Elliott Hughesc33a32b2011-10-11 18:18:07 -070060 monitor_list_(NULL),
Elliott Hughesdcc24742011-09-07 14:02:44 -070061 thread_list_(NULL),
62 intern_table_(NULL),
63 class_linker_(NULL),
64 signal_catcher_(NULL),
65 java_vm_(NULL),
Brian Carlstrom16192862011-09-12 17:50:06 -070066 jni_stub_array_(NULL),
Brian Carlstrome24fa612011-09-29 00:53:55 -070067 abstract_method_error_stub_array_(NULL),
Ian Rogers19846512012-02-24 11:42:47 -080068 resolution_method_(NULL),
Ian Rogers7c3757f2012-02-15 17:18:53 -080069 system_class_loader_(NULL),
Elliott Hughes6b355752012-01-13 16:49:08 -080070 shutting_down_(false),
Elliott Hughesdcc24742011-09-07 14:02:44 -070071 started_(false),
72 vfprintf_(NULL),
73 exit_(NULL),
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070074 abort_(NULL),
jeffhao2692b572011-12-16 15:42:28 -080075 stats_enabled_(false),
Ian Rogers776ac1f2012-04-13 23:36:36 -070076 method_trace_(0),
77 method_trace_file_size_(0),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080078 tracer_(NULL),
79 use_compile_time_class_path_(false) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -070080 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
81 resolution_stub_array_[i] = NULL;
82 }
83 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
84 callee_save_method_[i] = NULL;
85 }
Elliott Hughesdcc24742011-09-07 14:02:44 -070086}
87
Carl Shapiro61e019d2011-07-14 16:53:09 -070088Runtime::~Runtime() {
Elliott Hughes6b355752012-01-13 16:49:08 -080089 shutting_down_ = true;
90
jeffhaob5e81852012-03-12 11:15:45 -070091 if (IsMethodTracingActive()) {
92 Trace::Shutdown();
93 }
94
Elliott Hughes5fe594f2011-09-08 12:33:17 -070095 // Make sure our internal threads are dead before we start tearing down things they're using.
Elliott Hughese52e49b2012-04-02 16:05:44 -070096 Dbg::StopJdwp();
Elliott Hughes5fe594f2011-09-08 12:33:17 -070097 delete signal_catcher_;
98
Elliott Hughes038a8062011-09-18 14:12:41 -070099 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
Elliott Hughes93e74e82011-09-13 11:07:03 -0700100 delete thread_list_;
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700101 delete monitor_list_;
Elliott Hughes93e74e82011-09-13 11:07:03 -0700102
Carl Shapiro61e019d2011-07-14 16:53:09 -0700103 delete class_linker_;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800104 delete heap_;
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -0700105#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
Ian Rogers776ac1f2012-04-13 23:36:36 -0700106 verifier::MethodVerifier::DeleteInferredRegCategoryMaps();
Logan Chiendd361c92012-04-10 23:40:37 +0800107#endif
Ian Rogers776ac1f2012-04-13 23:36:36 -0700108 verifier::MethodVerifier::DeleteGcMaps();
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700109 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700110 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700111 Thread::Shutdown();
Elliott Hughes7c6169d2012-05-02 16:11:48 -0700112 QuasiAtomic::Shutdown();
113
Carl Shapiro4acf4642011-07-26 18:54:13 -0700114 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700115 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -0700116 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700117}
118
Elliott Hughescac6cc72011-11-03 20:31:21 -0700119static bool gAborting = false;
120
Elliott Hughese0918552011-10-28 17:18:29 -0700121struct AbortState {
122 void Dump(std::ostream& os) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700123 if (gAborting) {
124 os << "Runtime aborting --- recursively, so no thread-specific detail!\n";
125 return;
126 }
127 gAborting = true;
Elliott Hughese0918552011-10-28 17:18:29 -0700128 os << "Runtime aborting...\n";
Elliott Hughes6c7d2442012-02-01 18:40:47 -0800129 if (Runtime::Current() == NULL) {
130 os << "(Runtime does not yet exist!)\n";
131 return;
132 }
Elliott Hughese0918552011-10-28 17:18:29 -0700133 Thread* self = Thread::Current();
134 if (self == NULL) {
135 os << "(Aborting thread was not attached to runtime!)\n";
136 } else {
Elliott Hughes899e7892012-01-24 14:57:32 -0800137 self->Dump(os);
138 if (self->IsExceptionPending()) {
139 os << "Pending " << PrettyTypeOf(self->GetException()) << " on thread:\n"
140 << self->GetException()->Dump();
141 }
Elliott Hughese0918552011-10-28 17:18:29 -0700142 }
143 }
144};
145
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800146static Mutex& GetAbortLock() {
147 static Mutex abort_lock("abort lock");
148 return abort_lock;
149}
150
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700151void Runtime::Abort() {
Elliott Hughes131aef82012-01-27 11:53:35 -0800152 // Ensure that we don't have multiple threads trying to abort at once,
153 // which would result in significantly worse diagnostics.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800154 MutexLock mu(GetAbortLock());
Elliott Hughes131aef82012-01-27 11:53:35 -0800155
Elliott Hughesffe67362011-07-17 12:09:27 -0700156 // Get any pending output out of the way.
157 fflush(NULL);
158
159 // Many people have difficulty distinguish aborts from crashes,
160 // so be explicit.
Elliott Hughese0918552011-10-28 17:18:29 -0700161 AbortState state;
Elliott Hughes9ee5f9c2012-02-03 18:33:16 -0800162 LOG(INTERNAL_FATAL) << Dumpable<AbortState>(state);
Elliott Hughesffe67362011-07-17 12:09:27 -0700163
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700164 // Call the abort hook if we have one.
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700165 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700166 LOG(INTERNAL_FATAL) << "Calling abort hook...";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700167 Runtime::Current()->abort_();
168 // notreached
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700169 LOG(INTERNAL_FATAL) << "Unexpectedly returned from abort hook!";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700170 }
171
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700172#if defined(__BIONIC__)
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700173 // TODO: finish merging patches to fix abort(3) in bionic, then lose this!
174 // Bionic doesn't implement POSIX semantics for abort(3) in a multi-threaded
175 // process, so if we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -0700176 // receive SIGABRT. debuggerd dumps the stack trace of the main
177 // thread, whether or not that was the thread that failed. By
178 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -0700179 // fault in the current thread, and get a useful log from debuggerd.
Elliott Hughes81ff3182012-03-23 20:35:56 -0700180 // We can also trivially tell the difference between a crash and
Elliott Hughesffe67362011-07-17 12:09:27 -0700181 // a deliberate abort by looking at the fault address.
182 *reinterpret_cast<char*>(0xdeadd00d) = 38;
Elliott Hughesd06a6c72012-05-30 17:59:06 -0700183#elif defined(__APPLE__)
184 // TODO: check that this actually gives good stack traces on the Mac!
185 pthread_kill(pthread_self(), SIGABRT);
186#else
187 // TODO: we ought to be able to use pthread_kill(3) here (or abort(3),
188 // which POSIX defines in terms of raise(3), which POSIX defines in terms
189 // of pthread_kill(3)). On Linux, though, libcorkscrew can't unwind through
190 // libpthread, which means the stacks we dump would be useless. Calling
191 // tgkill(2) directly avoids that.
192 syscall(__NR_tgkill, getpid(), GetTid(), SIGABRT);
193#endif
Elliott Hughesffe67362011-07-17 12:09:27 -0700194 // notreached
195}
196
Elliott Hughesbf86d042011-08-31 17:53:14 -0700197void Runtime::CallExitHook(jint status) {
198 if (exit_ != NULL) {
Elliott Hughes34e06962012-04-09 13:55:55 -0700199 ScopedThreadStateChange tsc(Thread::Current(), kNative);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700200 exit_(status);
201 LOG(WARNING) << "Exit hook returned instead of exiting!";
202 }
203}
204
Brian Carlstrom8a436592011-08-15 21:27:23 -0700205// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
206// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
207// [gG] gigabytes.
208//
209// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700210// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
211// of 1024.
212//
213// The spec says the -Xmx and -Xms options must be multiples of 1024. It
214// doesn't say anything about -Xss.
215//
216// Returns 0 (a useless size) if "s" is malformed or specifies a low or
217// non-evenly-divisible value.
218//
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800219size_t ParseMemoryOption(const char* s, size_t div) {
Brian Carlstrom8a436592011-08-15 21:27:23 -0700220 // strtoul accepts a leading [+-], which we don't want,
221 // so make sure our string starts with a decimal digit.
222 if (isdigit(*s)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700223 char* s2;
224 size_t val = strtoul(s, &s2, 10);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700225 if (s2 != s) {
226 // s2 should be pointing just after the number.
227 // If this is the end of the string, the user
228 // has specified a number of bytes. Otherwise,
229 // there should be exactly one more character
230 // that specifies a multiplier.
231 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700232 // The remainder of the string is either a single multiplier
233 // character, or nothing to indicate that the value is in
234 // bytes.
235 char c = *s2++;
236 if (*s2 == '\0') {
237 size_t mul;
238 if (c == '\0') {
239 mul = 1;
240 } else if (c == 'k' || c == 'K') {
241 mul = KB;
242 } else if (c == 'm' || c == 'M') {
243 mul = MB;
244 } else if (c == 'g' || c == 'G') {
245 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700246 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700247 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700248 return 0;
249 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700250
251 if (val <= std::numeric_limits<size_t>::max() / mul) {
252 val *= mul;
253 } else {
254 // Clamp to a multiple of 1024.
255 val = std::numeric_limits<size_t>::max() & ~(1024-1);
256 }
257 } else {
258 // There's more than one character after the numeric part.
259 return 0;
260 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700261 }
262 // The man page says that a -Xm value must be a multiple of 1024.
263 if (val % div == 0) {
264 return val;
265 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700266 }
267 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700268 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700269}
270
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800271size_t ParseIntegerOrDie(const std::string& s) {
272 std::string::size_type colon = s.find(':');
273 if (colon == std::string::npos) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700274 LOG(FATAL) << "Missing integer: " << s;
275 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800276 const char* begin = &s[colon + 1];
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700277 char* end;
278 size_t result = strtoul(begin, &end, 10);
279 if (begin == end || *end != '\0') {
280 LOG(FATAL) << "Failed to parse integer in: " << s;
281 }
282 return result;
283}
284
Elliott Hughes0af55432011-08-17 18:37:28 -0700285void LoadJniLibrary(JavaVMExt* vm, const char* name) {
Elliott Hughes6cc332e2012-01-20 22:59:20 -0800286 std::string mapped_name(StringPrintf(OS_SHARED_LIB_FORMAT_STR, name));
Elliott Hughes75770752011-08-24 17:52:38 -0700287 std::string reason;
288 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700289 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
290 << reason;
291 }
292}
293
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700294Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700295 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800296 const char* boot_class_path_string = getenv("BOOTCLASSPATH");
297 if (boot_class_path_string != NULL) {
298 parsed->boot_class_path_string_ = boot_class_path_string;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700299 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800300 const char* class_path_string = getenv("CLASSPATH");
301 if (class_path_string != NULL) {
302 parsed->class_path_string_ = class_path_string;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700303 }
Elliott Hughes67d92002012-03-26 15:08:51 -0700304 // -Xcheck:jni is off by default for regular builds but on by default in debug builds.
305 parsed->check_jni_ = kIsDebugBuild;
Elliott Hughes85d15452011-09-16 17:33:01 -0700306
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700307 parsed->heap_initial_size_ = Heap::kInitialSize;
308 parsed->heap_maximum_size_ = Heap::kMaximumSize;
jeffhaoc1160702011-10-27 15:48:45 -0700309 parsed->heap_growth_limit_ = 0; // 0 means no growth limit
Brian Carlstromf734cf52011-08-17 16:28:14 -0700310 parsed->stack_size_ = Thread::kDefaultStackSize;
311
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800312 parsed->is_compiler_ = false;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700313 parsed->is_zygote_ = false;
314
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700315 parsed->jni_globals_max_ = 0;
Elliott Hughesfc861622011-10-17 17:57:47 -0700316 parsed->lock_profiling_threshold_ = 0;
317 parsed->hook_is_sensitive_thread_ = NULL;
318
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700319 parsed->hook_vfprintf_ = vfprintf;
320 parsed->hook_exit_ = exit;
Elliott Hughes6c1c69e2012-04-23 16:12:51 -0700321 parsed->hook_abort_ = NULL; // We don't call abort(3) by default; see Runtime::Abort.
322#if defined(__APPLE__)
323 parsed->hook_abort_ = abort; // On the Mac, abort(3) gives better results; see Runtime::InitPlatformSignalHandlers.
324#endif
Brian Carlstrom8a436592011-08-15 21:27:23 -0700325
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800326// gLogVerbosity.class_linker = true; // TODO: don't check this in!
327// gLogVerbosity.compiler = true; // TODO: don't check this in!
328// gLogVerbosity.heap = true; // TODO: don't check this in!
329// gLogVerbosity.gc = true; // TODO: don't check this in!
330// gLogVerbosity.jdwp = true; // TODO: don't check this in!
331// gLogVerbosity.jni = true; // TODO: don't check this in!
332// gLogVerbosity.monitor = true; // TODO: don't check this in!
333// gLogVerbosity.startup = true; // TODO: don't check this in!
334// gLogVerbosity.third_party_jni = true; // TODO: don't check this in!
335// gLogVerbosity.threads = true; // TODO: don't check this in!
336
jeffhaob5e81852012-03-12 11:15:45 -0700337 parsed->method_trace_ = false;
338 parsed->method_trace_file_ = "/data/method-trace-file.bin";
339 parsed->method_trace_file_size_ = 10 * MB;
340
Brian Carlstrom8a436592011-08-15 21:27:23 -0700341 for (size_t i = 0; i < options.size(); ++i) {
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800342 const std::string option(options[i].first);
Brian Carlstrom0796af02011-10-12 14:31:45 -0700343 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700344 LOG(INFO) << "option[" << i << "]=" << option;
345 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800346 if (StartsWith(option, "-Xbootclasspath:")) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800347 parsed->boot_class_path_string_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700348 } else if (option == "-classpath" || option == "-cp") {
349 // TODO: support -Djava.class.path
350 i++;
351 if (i == options.size()) {
352 // TODO: usage
353 LOG(FATAL) << "Missing required class path value for " << option;
354 return NULL;
355 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800356 const StringPiece& value = options[i].first;
357 parsed->class_path_string_ = value.data();
358 } else if (option == "bootclasspath") {
359 parsed->boot_class_path_
360 = reinterpret_cast<const std::vector<const DexFile*>*>(options[i].second);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800361 } else if (StartsWith(option, "-Ximage:")) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800362 parsed->image_ = option.substr(strlen("-Ximage:")).data();
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800363 } else if (StartsWith(option, "-Xcheck:jni")) {
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700364 parsed->check_jni_ = true;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800365 } else if (StartsWith(option, "-Xrunjdwp:") || StartsWith(option, "-agentlib:jdwp=")) {
366 std::string tail(option.substr(option[1] == 'X' ? 10 : 15));
Elliott Hughes3bb81562011-10-21 18:52:59 -0700367 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
368 LOG(FATAL) << "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
369 << "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n";
370 return NULL;
371 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800372 } else if (StartsWith(option, "-Xms")) {
373 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).c_str(), 1024);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700374 if (size == 0) {
375 if (ignore_unrecognized) {
376 continue;
377 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700378 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700379 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700380 return NULL;
381 }
382 parsed->heap_initial_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800383 } else if (StartsWith(option, "-Xmx")) {
384 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).c_str(), 1024);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700385 if (size == 0) {
386 if (ignore_unrecognized) {
387 continue;
388 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700389 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700390 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700391 return NULL;
392 }
393 parsed->heap_maximum_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800394 } else if (StartsWith(option, "-XX:HeapGrowthLimit=")) {
395 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).c_str(), 1024);
jeffhaoc1160702011-10-27 15:48:45 -0700396 if (size == 0) {
397 if (ignore_unrecognized) {
398 continue;
399 }
400 // TODO: usage
401 LOG(FATAL) << "Failed to parse " << option;
402 return NULL;
403 }
404 parsed->heap_growth_limit_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800405 } else if (StartsWith(option, "-Xss")) {
406 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).c_str(), 1);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700407 if (size == 0) {
408 if (ignore_unrecognized) {
409 continue;
410 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700411 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700412 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700413 return NULL;
414 }
415 parsed->stack_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800416 } else if (StartsWith(option, "-D")) {
417 parsed->properties_.push_back(option.substr(strlen("-D")));
418 } else if (StartsWith(option, "-Xjnitrace:")) {
419 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:"));
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700420 } else if (option == "compiler") {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800421 parsed->is_compiler_ = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700422 } else if (option == "-Xzygote") {
423 parsed->is_zygote_ = true;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800424 } else if (StartsWith(option, "-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700425 std::vector<std::string> verbose_options;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800426 Split(option.substr(strlen("-verbose:")), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700427 for (size_t i = 0; i < verbose_options.size(); ++i) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800428 if (verbose_options[i] == "class") {
429 gLogVerbosity.class_linker = true;
430 } else if (verbose_options[i] == "compiler") {
431 gLogVerbosity.compiler = true;
432 } else if (verbose_options[i] == "heap") {
433 gLogVerbosity.heap = true;
434 } else if (verbose_options[i] == "gc") {
435 gLogVerbosity.gc = true;
436 } else if (verbose_options[i] == "jdwp") {
437 gLogVerbosity.jdwp = true;
438 } else if (verbose_options[i] == "jni") {
439 gLogVerbosity.jni = true;
440 } else if (verbose_options[i] == "monitor") {
441 gLogVerbosity.monitor = true;
442 } else if (verbose_options[i] == "startup") {
443 gLogVerbosity.startup = true;
444 } else if (verbose_options[i] == "third-party-jni") {
445 gLogVerbosity.third_party_jni = true;
446 } else if (verbose_options[i] == "threads") {
447 gLogVerbosity.threads = true;
448 } else {
449 LOG(WARNING) << "Ignoring unknown -verbose option: " << verbose_options[i];
450 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700451 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800452 } else if (StartsWith(option, "-Xjnigreflimit:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700453 parsed->jni_globals_max_ = ParseIntegerOrDie(option);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800454 } else if (StartsWith(option, "-Xlockprofthreshold:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700455 parsed->lock_profiling_threshold_ = ParseIntegerOrDie(option);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800456 } else if (StartsWith(option, "-Xstacktracefile:")) {
Elliott Hughes67d92002012-03-26 15:08:51 -0700457 if (kIsDebugBuild) {
458 // Ignore the zygote and always show stack traces in debug builds.
459 } else {
460 parsed->stack_trace_file_ = option.substr(strlen("-Xstacktracefile:"));
461 }
Elliott Hughesfc861622011-10-17 17:57:47 -0700462 } else if (option == "sensitiveThread") {
463 parsed->hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(options[i].second);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700464 } else if (option == "vfprintf") {
465 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
466 } else if (option == "exit") {
467 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
468 } else if (option == "abort") {
469 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700470 } else if (option == "host-prefix") {
471 parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second);
Elliott Hughesb3e66df2012-01-12 14:49:18 -0800472 } else if (option == "-Xgenregmap" || option == "-Xgc:precise") {
473 // We silently ignore these for backwards compatibility.
jeffhaob5e81852012-03-12 11:15:45 -0700474 } else if (option == "-Xmethod-trace") {
475 parsed->method_trace_ = true;
476 } else if (StartsWith(option, "-Xmethod-trace-file:")) {
477 parsed->method_trace_file_ = option.substr(strlen("-Xmethod-trace-file:"));
478 } else if (StartsWith(option, "-Xmethod-trace-file-size:")) {
479 parsed->method_trace_file_size_ = ParseIntegerOrDie(option);
Elliott Hughese119a362012-05-22 17:37:06 -0700480 } else if (option == "-Xprofile:threadcpuclock") {
481 Trace::SetDefaultClockSource(kProfilerClockSourceThreadCpu);
482 } else if (option == "-Xprofile:wallclock") {
483 Trace::SetDefaultClockSource(kProfilerClockSourceWall);
484 } else if (option == "-Xprofile:dualclock") {
485 Trace::SetDefaultClockSource(kProfilerClockSourceDual);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700486 } else {
487 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700488 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700489 LOG(ERROR) << "Unrecognized option " << option;
490 // TODO: this should exit, but for now tolerate unknown options
491 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700492 }
493 }
494 }
495
Brian Carlstrom223f20f2012-02-04 23:06:55 -0800496 if (!parsed->is_compiler_ && parsed->image_.empty()) {
497 parsed->image_ += GetAndroidRoot();
498 parsed->image_ += "/framework/boot.art";
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700499 }
jeffhaoc1160702011-10-27 15:48:45 -0700500 if (parsed->heap_growth_limit_ == 0) {
501 parsed->heap_growth_limit_ = parsed->heap_maximum_size_;
502 }
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700503
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700504 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700505}
506
507Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700508 // TODO: acquire a static mutex on Runtime to avoid racing.
509 if (Runtime::instance_ != NULL) {
510 return NULL;
511 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700512 instance_ = new Runtime;
513 if (!instance_->Init(options, ignore_unrecognized)) {
514 delete instance_;
515 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700516 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700517 return instance_;
518}
Elliott Hughes0af55432011-08-17 18:37:28 -0700519
Brian Carlstromaded5f72011-10-07 17:15:04 -0700520void CreateSystemClassLoader() {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800521 if (Runtime::Current()->UseCompileTimeClassPath()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700522 return;
523 }
524
525 Thread* self = Thread::Current();
526
527 // Must be in the kNative state for calling native methods.
Elliott Hughes34e06962012-04-09 13:55:55 -0700528 CHECK_EQ(self->GetState(), kNative);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700529
530 JNIEnv* env = self->GetJniEnv();
Elliott Hugheseac76672012-05-24 21:56:51 -0700531 jmethodID getSystemClassLoader = env->GetStaticMethodID(WellKnownClasses::java_lang_ClassLoader,
Brian Carlstromdf143242011-10-10 18:05:34 -0700532 "getSystemClassLoader",
533 "()Ljava/lang/ClassLoader;");
534 CHECK(getSystemClassLoader != NULL);
Elliott Hugheseac76672012-05-24 21:56:51 -0700535 ScopedLocalRef<jobject> class_loader(env, env->CallStaticObjectMethod(WellKnownClasses::java_lang_ClassLoader,
Brian Carlstromdf143242011-10-10 18:05:34 -0700536 getSystemClassLoader));
537 CHECK(class_loader.get() != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700538
Brian Carlstromdf143242011-10-10 18:05:34 -0700539 Thread::Current()->SetClassLoaderOverride(Decode<ClassLoader*>(env, class_loader.get()));
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500540
Elliott Hugheseac76672012-05-24 21:56:51 -0700541 jfieldID contextClassLoader = env->GetFieldID(WellKnownClasses::java_lang_Thread,
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500542 "contextClassLoader",
543 "Ljava/lang/ClassLoader;");
544 CHECK(contextClassLoader != NULL);
545 ScopedLocalRef<jobject> self_jobject(env, AddLocalReference<jobject>(env, self->GetPeer()));
546 env->SetObjectField(self_jobject.get(), contextClassLoader, class_loader.get());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700547}
548
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700549void Runtime::Start() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800550 VLOG(startup) << "Runtime::Start entering";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700551
552 CHECK(host_prefix_.empty()) << host_prefix_;
553
Logan Chien0c717dd2012-03-28 18:31:07 +0800554 // Relocate the OatFiles (ELF images)
555 class_linker_->RelocateExecutable();
556
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700557 // Restore main thread state to kNative as expected by native code
Elliott Hughes34e06962012-04-09 13:55:55 -0700558 Thread::Current()->SetState(kNative);
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700559
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700560 started_ = true;
561
Brian Carlstromae826982011-11-09 01:33:42 -0800562 // InitNativeMethods needs to be after started_ so that the classes
563 // it touches will have methods linked to the oat file if necessary.
564 InitNativeMethods();
565
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500566 Thread::FinishStartup();
567
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700568 if (!is_zygote_) {
569 DidForkFromZygote();
570 }
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700571
Elliott Hughes85d15452011-09-16 17:33:01 -0700572 StartDaemonThreads();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700573
Brian Carlstromaded5f72011-10-07 17:15:04 -0700574 CreateSystemClassLoader();
575
Elliott Hughes726079d2011-10-07 18:43:44 -0700576 Thread::Current()->GetJniEnv()->locals.AssertEmpty();
577
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800578 VLOG(startup) << "Runtime::Start exiting";
Elliott Hughes85d15452011-09-16 17:33:01 -0700579}
580
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700581void Runtime::DidForkFromZygote() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700582 is_zygote_ = false;
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700583
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700584 StartSignalCatcher();
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700585
586 // Start the JDWP thread. If the command-line debugger flags specified "suspend=y",
587 // this will pause the runtime, so we probably want this to come last.
588 Dbg::StartJdwp();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700589}
590
591void Runtime::StartSignalCatcher() {
592 if (!is_zygote_) {
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700593 signal_catcher_ = new SignalCatcher(stack_trace_file_);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700594 }
595}
596
Elliott Hughes85d15452011-09-16 17:33:01 -0700597void Runtime::StartDaemonThreads() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800598 VLOG(startup) << "Runtime::StartDaemonThreads entering";
Elliott Hughes85d15452011-09-16 17:33:01 -0700599
Elliott Hughes719b3232011-09-25 17:42:19 -0700600 Thread* self = Thread::Current();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700601
602 // Must be in the kNative state for calling native methods.
Elliott Hughes34e06962012-04-09 13:55:55 -0700603 CHECK_EQ(self->GetState(), kNative);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700604
Elliott Hughes719b3232011-09-25 17:42:19 -0700605 JNIEnv* env = self->GetJniEnv();
Elliott Hugheseac76672012-05-24 21:56:51 -0700606 env->CallStaticVoidMethod(WellKnownClasses::java_lang_Daemons, WellKnownClasses::java_lang_Daemons_start);
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800607 CHECK(!env->ExceptionCheck());
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700608
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800609 VLOG(startup) << "Runtime::StartDaemonThreads exiting";
Carl Shapiro61e019d2011-07-14 16:53:09 -0700610}
611
Elliott Hughes6b355752012-01-13 16:49:08 -0800612bool Runtime::IsShuttingDown() const {
613 return shutting_down_;
614}
615
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700616bool Runtime::IsStarted() const {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700617 return started_;
618}
619
Elliott Hughes0af55432011-08-17 18:37:28 -0700620bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700621 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700622
Elliott Hughes90a33692011-08-30 13:27:07 -0700623 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
624 if (options.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700625 LOG(ERROR) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700626 return false;
627 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800628 VLOG(startup) << "Runtime::Init -verbose:startup enabled";
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700629
Elliott Hughes7c6169d2012-05-02 16:11:48 -0700630 QuasiAtomic::Startup();
631
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700632 SetJniGlobalsMax(options->jni_globals_max_);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800633 Monitor::Init(options->lock_profiling_threshold_, options->hook_is_sensitive_thread_);
Elliott Hughes32d6e1e2011-10-11 14:47:44 -0700634
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700635 host_prefix_ = options->host_prefix_;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800636 boot_class_path_string_ = options->boot_class_path_string_;
637 class_path_string_ = options->class_path_string_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700638 properties_ = options->properties_;
639
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800640 is_compiler_ = options->is_compiler_;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700641 is_zygote_ = options->is_zygote_;
642
Elliott Hughes0af55432011-08-17 18:37:28 -0700643 vfprintf_ = options->hook_vfprintf_;
644 exit_ = options->hook_exit_;
645 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700646
Elliott Hughesbe759c62011-09-08 19:38:21 -0700647 default_stack_size_ = options->stack_size_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700648 stack_trace_file_ = options->stack_trace_file_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700649
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700650 monitor_list_ = new MonitorList;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800651 thread_list_ = new ThreadList;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700652 intern_table_ = new InternTable;
653
Ian Rogers776ac1f2012-04-13 23:36:36 -0700654 verifier::MethodVerifier::InitGcMaps();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800655
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -0700656#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
Ian Rogers776ac1f2012-04-13 23:36:36 -0700657 verifier::MethodVerifier::InitInferredRegCategoryMaps();
Logan Chiendd361c92012-04-10 23:40:37 +0800658#endif
659
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800660 heap_ = new Heap(options->heap_initial_size_,
661 options->heap_growth_limit_,
662 options->heap_maximum_size_,
663 options->image_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700664
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700665 BlockSignals();
Elliott Hughes457005c2012-04-16 13:54:25 -0700666 InitPlatformSignalHandlers();
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700667
Elliott Hughesa0957642011-09-02 14:27:33 -0700668 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700669
Elliott Hughesbe759c62011-09-08 19:38:21 -0700670 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700671
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700672 // ClassLinker needs an attached thread, but we can't fully attach a thread
Elliott Hughes462c9442012-03-23 18:47:50 -0700673 // without creating objects. We can't supply a thread group yet; it will be fixed later.
674 Thread::Attach("main", false, NULL);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700675
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700676 // Set us to runnable so tools using a runtime can allocate and GC by default
Elliott Hughes34e06962012-04-09 13:55:55 -0700677 Thread::Current()->SetState(kRunnable);
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700678
Ian Rogers141d6222012-04-05 12:23:06 -0700679 // Now we're attached, we can take the heap lock and validate the heap.
680 GetHeap()->EnableObjectValidation();
681
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800682 CHECK_GE(GetHeap()->GetSpaces().size(), 1U);
683 if (GetHeap()->GetSpaces()[0]->IsImageSpace()) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800684 class_linker_ = ClassLinker::CreateFromImage(intern_table_);
685 } else {
686 CHECK(options->boot_class_path_ != NULL);
687 CHECK_NE(options->boot_class_path_->size(), 0U);
688 class_linker_ = ClassLinker::CreateFromCompiler(*options->boot_class_path_, intern_table_);
689 }
690 CHECK(class_linker_ != NULL);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700691
jeffhaob5e81852012-03-12 11:15:45 -0700692 method_trace_ = options->method_trace_;
693 method_trace_file_ = options->method_trace_file_;
694 method_trace_file_size_ = options->method_trace_file_size_;
695
696 if (options->method_trace_) {
697 Trace::Start(options->method_trace_file_.c_str(), -1, options->method_trace_file_size_, 0, false);
698 }
699
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800700 VLOG(startup) << "Runtime::Init exiting";
Carl Shapiro1fb86202011-06-27 17:43:13 -0700701 return true;
702}
703
Elliott Hughes038a8062011-09-18 14:12:41 -0700704void Runtime::InitNativeMethods() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800705 VLOG(startup) << "Runtime::InitNativeMethods entering";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700706 Thread* self = Thread::Current();
707 JNIEnv* env = self->GetJniEnv();
708
Elliott Hughes418d20f2011-09-22 14:00:39 -0700709 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Elliott Hughes34e06962012-04-09 13:55:55 -0700710 CHECK_EQ(self->GetState(), kNative);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700711
Elliott Hughes418d20f2011-09-22 14:00:39 -0700712 // First set up JniConstants, which is used by both the runtime's built-in native
713 // methods and libcore.
Elliott Hughesfea966e2011-09-22 10:26:20 -0700714 JniConstants::init(env);
Elliott Hugheseac76672012-05-24 21:56:51 -0700715 WellKnownClasses::Init(env);
Elliott Hughesfea966e2011-09-22 10:26:20 -0700716
Elliott Hughes418d20f2011-09-22 14:00:39 -0700717 // Then set up the native methods provided by the runtime itself.
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700718 RegisterRuntimeNativeMethods(env);
719
Elliott Hughes418d20f2011-09-22 14:00:39 -0700720 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
721 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
722 // the library that implements System.loadLibrary!
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700723 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800724 VLOG(startup) << "Runtime::InitNativeMethods exiting";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700725}
726
727void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
728#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Ian Rogers5167c972012-02-03 10:41:20 -0800729 // Register Throwable first so that registration of other native methods can throw exceptions
730 REGISTER(register_java_lang_Throwable);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700731 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700732 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700733 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700734 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700735 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700736 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700737 REGISTER(register_java_lang_Object);
738 REGISTER(register_java_lang_Runtime);
739 REGISTER(register_java_lang_String);
740 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700741 REGISTER(register_java_lang_Thread);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700742 REGISTER(register_java_lang_VMClassLoader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700743 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700744 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700745 REGISTER(register_java_lang_reflect_Field);
746 REGISTER(register_java_lang_reflect_Method);
Jesse Wilson95caa792011-10-12 18:14:17 -0400747 REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700748 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700749 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700750 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700751 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700752#undef REGISTER
753}
754
Elliott Hughesc967f782012-04-16 10:23:15 -0700755void Runtime::DumpForSigQuit(std::ostream& os) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700756 GetClassLinker()->DumpForSigQuit(os);
757 GetInternTable()->DumpForSigQuit(os);
Elliott Hughesae80b492012-04-24 10:43:17 -0700758 GetJavaVM()->DumpForSigQuit(os);
Elliott Hughesc967f782012-04-16 10:23:15 -0700759 GetHeap()->DumpForSigQuit(os);
Elliott Hughes42ee1422011-09-06 12:33:32 -0700760 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700761
Elliott Hughesc967f782012-04-16 10:23:15 -0700762 thread_list_->DumpForSigQuit(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700763}
764
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800765void Runtime::DumpLockHolders(std::ostream& os) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800766 pid_t heap_lock_owner = GetHeap()->GetLockOwner();
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800767 pid_t thread_list_lock_owner = GetThreadList()->GetLockOwner();
768 pid_t classes_lock_owner = GetClassLinker()->GetClassesLockOwner();
769 pid_t dex_lock_owner = GetClassLinker()->GetDexLockOwner();
770 if ((heap_lock_owner | thread_list_lock_owner | classes_lock_owner | dex_lock_owner) != 0) {
771 os << "Heap lock owner tid: " << heap_lock_owner << "\n"
772 << "ThreadList lock owner tid: " << thread_list_lock_owner << "\n"
773 << "ClassLinker classes lock owner tid: " << classes_lock_owner << "\n"
774 << "ClassLinker dex lock owner tid: " << dex_lock_owner << "\n";
775 }
776}
777
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700778void Runtime::SetStatsEnabled(bool new_state) {
779 if (new_state == true) {
780 GetStats()->Clear(~0);
781 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
782 Thread::Current()->GetStats()->Clear(~0);
783 }
784 stats_enabled_ = new_state;
785}
786
787void Runtime::ResetStats(int kinds) {
788 GetStats()->Clear(kinds & 0xffff);
789 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
790 Thread::Current()->GetStats()->Clear(kinds >> 16);
791}
792
793RuntimeStats* Runtime::GetStats() {
794 return &stats_;
795}
796
797int32_t Runtime::GetStat(int kind) {
798 RuntimeStats* stats;
799 if (kind < (1<<16)) {
800 stats = GetStats();
801 } else {
802 stats = Thread::Current()->GetStats();
803 kind >>= 16;
804 }
805 switch (kind) {
806 case KIND_ALLOCATED_OBJECTS:
807 return stats->allocated_objects;
808 case KIND_ALLOCATED_BYTES:
809 return stats->allocated_bytes;
810 case KIND_FREED_OBJECTS:
811 return stats->freed_objects;
812 case KIND_FREED_BYTES:
813 return stats->freed_bytes;
814 case KIND_GC_INVOCATIONS:
815 return stats->gc_for_alloc_count;
816 case KIND_CLASS_INIT_COUNT:
817 return stats->class_init_count;
818 case KIND_CLASS_INIT_TIME:
819 // Convert ns to us, reduce to 32 bits.
Elliott Hughes398f64b2012-03-26 18:05:48 -0700820 return static_cast<int>(stats->class_init_time_ns / 1000);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700821 case KIND_EXT_ALLOCATED_OBJECTS:
822 case KIND_EXT_ALLOCATED_BYTES:
823 case KIND_EXT_FREED_OBJECTS:
824 case KIND_EXT_FREED_BYTES:
825 return 0; // backward compatibility
826 default:
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700827 LOG(FATAL) << "Unknown statistic " << kind;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700828 return -1; // unreachable
829 }
830}
831
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700832void Runtime::BlockSignals() {
Elliott Hughes457005c2012-04-16 13:54:25 -0700833 SignalSet signals;
834 signals.Add(SIGPIPE);
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700835 // SIGQUIT is used to dump the runtime's state (including stack traces).
Elliott Hughes457005c2012-04-16 13:54:25 -0700836 signals.Add(SIGQUIT);
Elliott Hughes08795042012-04-03 14:48:52 -0700837 // SIGUSR1 is used to initiate a GC.
Elliott Hughes457005c2012-04-16 13:54:25 -0700838 signals.Add(SIGUSR1);
839 signals.Block();
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700840}
841
Elliott Hughes462c9442012-03-23 18:47:50 -0700842void Runtime::AttachCurrentThread(const char* thread_name, bool as_daemon, Object* thread_group) {
843 Thread::Attach(thread_name, as_daemon, thread_group);
Elliott Hughes22869a92012-03-27 14:08:24 -0700844 if (thread_name == NULL) {
845 LOG(WARNING) << *Thread::Current() << " attached without supplying a name";
846 }
Carl Shapiro61e019d2011-07-14 16:53:09 -0700847}
848
Elliott Hughesd92bec42011-09-02 17:04:36 -0700849void Runtime::DetachCurrentThread() {
Brian Carlstrom4d571432012-05-16 00:21:41 -0700850 Thread* self = Thread::Current();
851 if (self == NULL) {
852 LOG(FATAL) << "attempting to detach thread that is not attached";
853 }
854 if (self->GetTopOfStack().GetSP() != NULL) {
Elliott Hughes22869a92012-03-27 14:08:24 -0700855 LOG(FATAL) << *Thread::Current() << " attempting to detach while still running code";
856 }
Elliott Hughes02b48d12011-09-07 17:15:51 -0700857 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700858}
859
Brian Carlstrome24fa612011-09-29 00:53:55 -0700860void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700861 Dbg::VisitRoots(visitor, arg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700862 class_linker_->VisitRoots(visitor, arg);
863 intern_table_->VisitRoots(visitor, arg);
864 java_vm_->VisitRoots(visitor, arg);
865 thread_list_->VisitRoots(visitor, arg);
866 visitor(jni_stub_array_, arg);
867 visitor(abstract_method_error_stub_array_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700868 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
869 visitor(resolution_stub_array_[i], arg);
870 }
Ian Rogers19846512012-02-24 11:42:47 -0800871 visitor(resolution_method_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700872 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
873 visitor(callee_save_method_[i], arg);
874 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700875}
876
Ian Rogers169c9a72011-11-13 20:13:17 -0800877bool Runtime::HasJniDlsymLookupStub() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700878 return jni_stub_array_ != NULL;
879}
880
Ian Rogers169c9a72011-11-13 20:13:17 -0800881ByteArray* Runtime::GetJniDlsymLookupStub() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700882 CHECK(jni_stub_array_ != NULL);
883 return jni_stub_array_;
884}
885
Ian Rogers169c9a72011-11-13 20:13:17 -0800886void Runtime::SetJniDlsymLookupStub(ByteArray* jni_stub_array) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700887 CHECK(jni_stub_array != NULL) << " jni_stub_array=" << jni_stub_array;
888 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array)
889 << "jni_stub_array_=" << jni_stub_array_ << " jni_stub_array=" << jni_stub_array;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700890 jni_stub_array_ = jni_stub_array;
891}
892
893bool Runtime::HasAbstractMethodErrorStubArray() const {
894 return abstract_method_error_stub_array_ != NULL;
895}
896
897ByteArray* Runtime::GetAbstractMethodErrorStubArray() const {
898 CHECK(abstract_method_error_stub_array_ != NULL);
899 return abstract_method_error_stub_array_;
900}
901
902void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) {
903 CHECK(abstract_method_error_stub_array != NULL);
904 CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array);
905 abstract_method_error_stub_array_ = abstract_method_error_stub_array;
906}
907
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700908
909Runtime::TrampolineType Runtime::GetTrampolineType(Method* method) {
910 if (method == NULL) {
911 return Runtime::kUnknownMethod;
912 } else if (method->IsStatic()) {
913 return Runtime::kStaticMethod;
914 } else {
Ian Rogersfb6adba2012-03-04 21:51:51 -0800915 return Runtime::kUnknownMethod;
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700916 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700917}
918
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700919bool Runtime::HasResolutionStubArray(TrampolineType type) const {
920 return resolution_stub_array_[type] != NULL;
Ian Rogersad25ac52011-10-04 19:13:33 -0700921}
922
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700923ByteArray* Runtime::GetResolutionStubArray(TrampolineType type) const {
924 CHECK(HasResolutionStubArray(type));
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700925 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastTrampolineMethodType));
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700926 return resolution_stub_array_[type];
927}
928
929void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700930 CHECK(resolution_stub_array != NULL);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700931 CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array);
932 resolution_stub_array_[type] = resolution_stub_array;
Ian Rogersad25ac52011-10-04 19:13:33 -0700933}
934
Ian Rogers19846512012-02-24 11:42:47 -0800935Method* Runtime::CreateResolutionMethod() {
936 Class* method_class = Method::GetMethodClass();
937 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
938 method->SetDeclaringClass(method_class);
939 // TODO: use a special method for resolution method saves
940 method->SetDexMethodIndex(DexFile::kDexNoIndex16);
941 ByteArray* unknown_resolution_stub = GetResolutionStubArray(kUnknownMethod);
942 CHECK(unknown_resolution_stub != NULL);
943 method->SetCode(unknown_resolution_stub->GetData());
944 return method.get();
945}
946
947bool Runtime::HasResolutionMethod() const {
948 return resolution_method_ != NULL;
949}
950
951// Returns a special method that calls into a trampoline for runtime method resolution
952Method* Runtime::GetResolutionMethod() const {
953 CHECK(HasResolutionMethod());
954 return resolution_method_;
955}
956
957void Runtime::SetResolutionMethod(Method* method) {
958 resolution_method_ = method;
959}
960
Brian Carlstromae826982011-11-09 01:33:42 -0800961Method* Runtime::CreateCalleeSaveMethod(InstructionSet instruction_set, CalleeSaveType type) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700962 Class* method_class = Method::GetMethodClass();
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700963 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
Ian Rogersff1ed472011-09-20 13:46:24 -0700964 method->SetDeclaringClass(method_class);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800965 // TODO: use a special method for callee saves
Ian Rogers19846512012-02-24 11:42:47 -0800966 method->SetDexMethodIndex(DexFile::kDexNoIndex16);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700967 method->SetCode(NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800968 if ((instruction_set == kThumb2) || (instruction_set == kArm)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700969 uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6) | (1 << art::arm::R7) |
970 (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11);
971 uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3);
972 uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9);
973 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills :0) |
974 (type == kSaveAll ? all_spills :0) | (1 << art::arm::LR);
975 uint32_t fp_all_spills = (1 << art::arm::S0) | (1 << art::arm::S1) | (1 << art::arm::S2) |
976 (1 << art::arm::S3) | (1 << art::arm::S4) | (1 << art::arm::S5) |
977 (1 << art::arm::S6) | (1 << art::arm::S7) | (1 << art::arm::S8) |
978 (1 << art::arm::S9) | (1 << art::arm::S10) | (1 << art::arm::S11) |
979 (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) |
980 (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) |
981 (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) |
982 (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) |
983 (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) |
984 (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
985 (1 << art::arm::S30) | (1 << art::arm::S31);
986 uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
987 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
988 __builtin_popcount(fp_spills) /* fprs */ +
989 1 /* Method* */) * kPointerSize, kStackAlignment);
Ian Rogers15fdb8c2011-09-25 15:45:07 -0700990 method->SetFrameSizeInBytes(frame_size);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700991 method->SetCoreSpillMask(core_spills);
992 method->SetFpSpillMask(fp_spills);
Brian Carlstromae826982011-11-09 01:33:42 -0800993 } else if (instruction_set == kX86) {
Ian Rogers7caad772012-03-30 01:07:54 -0700994 uint32_t ref_spills = (1 << art::x86::EBP) | (1 << art::x86::ESI) | (1 << art::x86::EDI);
995 uint32_t arg_spills = (1 << art::x86::ECX) | (1 << art::x86::EDX) | (1 << art::x86::EBX);
996 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills : 0) |
997 (1 << art::x86::kNumberOfCpuRegisters); // fake return address callee save
998 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
999 1 /* Method* */) * kPointerSize, kStackAlignment);
1000 method->SetFrameSizeInBytes(frame_size);
1001 method->SetCoreSpillMask(core_spills);
Ian Rogersff1ed472011-09-20 13:46:24 -07001002 method->SetFpSpillMask(0);
1003 } else {
1004 UNIMPLEMENTED(FATAL);
1005 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001006 return method.get();
Ian Rogersff1ed472011-09-20 13:46:24 -07001007}
1008
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001009bool Runtime::HasCalleeSaveMethod(CalleeSaveType type) const {
1010 return callee_save_method_[type] != NULL;
Brian Carlstrom1f870082011-08-23 16:02:11 -07001011}
1012
Brian Carlstrome24fa612011-09-29 00:53:55 -07001013// Returns a special method that describes all callee saves being spilled to the stack.
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001014Method* Runtime::GetCalleeSaveMethod(CalleeSaveType type) const {
1015 CHECK(HasCalleeSaveMethod(type));
1016 return callee_save_method_[type];
Brian Carlstrome24fa612011-09-29 00:53:55 -07001017}
1018
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001019void Runtime::SetCalleeSaveMethod(Method* method, CalleeSaveType type) {
1020 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
1021 callee_save_method_[type] = method;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001022}
1023
jeffhao2692b572011-12-16 15:42:28 -08001024void Runtime::EnableMethodTracing(Trace* tracer) {
1025 CHECK(!IsMethodTracingActive());
1026 tracer_ = tracer;
1027}
1028
1029void Runtime::DisableMethodTracing() {
1030 CHECK(IsMethodTracingActive());
1031 delete tracer_;
1032 tracer_ = NULL;
1033}
1034
1035bool Runtime::IsMethodTracingActive() const {
1036 return (tracer_ != NULL);
1037}
1038
1039Trace* Runtime::GetTracer() const {
1040 CHECK(IsMethodTracingActive());
1041 return tracer_;
1042}
1043
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001044const std::vector<const DexFile*>& Runtime::GetCompileTimeClassPath(const ClassLoader* class_loader) {
1045 if (class_loader == NULL) {
1046 return GetClassLinker()->GetBootClassPath();
1047 }
1048 CHECK(UseCompileTimeClassPath());
1049 CompileTimeClassPaths::const_iterator it = compile_time_class_paths_.find(class_loader);
1050 CHECK(it != compile_time_class_paths_.end());
1051 return it->second;
1052}
1053
1054void Runtime::SetCompileTimeClassPath(const ClassLoader* class_loader, std::vector<const DexFile*>& class_path) {
1055 CHECK(!IsStarted());
1056 use_compile_time_class_path_ = true;
Elliott Hughesa0e18062012-04-13 15:59:59 -07001057 compile_time_class_paths_.Put(class_loader, class_path);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001058}
1059
Carl Shapiro1fb86202011-06-27 17:43:13 -07001060} // namespace art