blob: d07a6e5f3393c84d7efd3ed466f6c2e32e643927 [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>
20
Elliott Hughesffe67362011-07-17 12:09:27 -070021#include <cstdio>
22#include <cstdlib>
Brian Carlstrom8a436592011-08-15 21:27:23 -070023#include <limits>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070024#include <vector>
Elliott Hughesffe67362011-07-17 12:09:27 -070025
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070026#include "class_linker.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070027#include "class_loader.h"
Elliott Hughes3bb81562011-10-21 18:52:59 -070028#include "debugger.h"
Elliott Hughes4d6850c2012-01-18 15:55:06 -080029#include "dex_verifier.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070030#include "heap.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070031#include "image.h"
Elliott Hughescf4c6c42011-09-01 15:16:42 -070032#include "intern_table.h"
Elliott Hughesc5f7c912011-08-18 14:00:42 -070033#include "jni_internal.h"
Elliott Hughes32d6e1e2011-10-11 14:47:44 -070034#include "monitor.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070035#include "oat_file.h"
Elliott Hughes726079d2011-10-07 18:43:44 -070036#include "ScopedLocalRef.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070037#include "signal_catcher.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070038#include "space.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070039#include "thread.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070040#include "thread_list.h"
jeffhaob5e81852012-03-12 11:15:45 -070041#include "trace.h"
Elliott Hughes3bb81562011-10-21 18:52:59 -070042#include "UniquePtr.h"
Carl Shapiro61e019d2011-07-14 16:53:09 -070043
Elliott Hughes90a33692011-08-30 13:27:07 -070044// TODO: this drags in cutil/log.h, which conflicts with our logging.h.
45#include "JniConstants.h"
46
Carl Shapiro1fb86202011-06-27 17:43:13 -070047namespace art {
48
Carl Shapiro2ed144c2011-07-26 16:52:08 -070049Runtime* Runtime::instance_ = NULL;
50
Elliott Hughesdcc24742011-09-07 14:02:44 -070051Runtime::Runtime()
Elliott Hughesd9c67be2012-02-02 19:54:06 -080052 : is_compiler_(false),
53 is_zygote_(false),
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -070054 default_stack_size_(Thread::kDefaultStackSize),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080055 heap_(NULL),
Elliott Hughesc33a32b2011-10-11 18:18:07 -070056 monitor_list_(NULL),
Elliott Hughesdcc24742011-09-07 14:02:44 -070057 thread_list_(NULL),
58 intern_table_(NULL),
59 class_linker_(NULL),
60 signal_catcher_(NULL),
61 java_vm_(NULL),
Brian Carlstrom16192862011-09-12 17:50:06 -070062 jni_stub_array_(NULL),
Brian Carlstrome24fa612011-09-29 00:53:55 -070063 abstract_method_error_stub_array_(NULL),
Ian Rogers19846512012-02-24 11:42:47 -080064 resolution_method_(NULL),
Ian Rogers7c3757f2012-02-15 17:18:53 -080065 system_class_loader_(NULL),
Elliott Hughes6b355752012-01-13 16:49:08 -080066 shutting_down_(false),
Elliott Hughesdcc24742011-09-07 14:02:44 -070067 started_(false),
68 vfprintf_(NULL),
69 exit_(NULL),
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070070 abort_(NULL),
jeffhao2692b572011-12-16 15:42:28 -080071 stats_enabled_(false),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080072 tracer_(NULL),
73 use_compile_time_class_path_(false) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -070074 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
75 resolution_stub_array_[i] = NULL;
76 }
77 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
78 callee_save_method_[i] = NULL;
79 }
Elliott Hughesdcc24742011-09-07 14:02:44 -070080}
81
Carl Shapiro61e019d2011-07-14 16:53:09 -070082Runtime::~Runtime() {
Elliott Hughes6b355752012-01-13 16:49:08 -080083 shutting_down_ = true;
84
jeffhaob5e81852012-03-12 11:15:45 -070085 if (IsMethodTracingActive()) {
86 Trace::Shutdown();
87 }
88
Elliott Hughesd1cc8362011-10-24 16:58:50 -070089 Dbg::StopJdwp();
90
Elliott Hughes5fe594f2011-09-08 12:33:17 -070091 // Make sure our internal threads are dead before we start tearing down things they're using.
92 delete signal_catcher_;
Elliott Hughes038a8062011-09-18 14:12:41 -070093 // TODO: GC thread.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070094
Elliott Hughes038a8062011-09-18 14:12:41 -070095 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
Elliott Hughes93e74e82011-09-13 11:07:03 -070096 delete thread_list_;
Elliott Hughesc33a32b2011-10-11 18:18:07 -070097 delete monitor_list_;
Elliott Hughes93e74e82011-09-13 11:07:03 -070098
Carl Shapiro61e019d2011-07-14 16:53:09 -070099 delete class_linker_;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800100 delete heap_;
Elliott Hughes4d6850c2012-01-18 15:55:06 -0800101 verifier::DexVerifier::DeleteGcMaps();
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700102 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700103 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700104 Thread::Shutdown();
Carl Shapiro4acf4642011-07-26 18:54:13 -0700105 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700106 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -0700107 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700108}
109
Elliott Hughescac6cc72011-11-03 20:31:21 -0700110static bool gAborting = false;
111
Elliott Hughese0918552011-10-28 17:18:29 -0700112struct AbortState {
113 void Dump(std::ostream& os) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700114 if (gAborting) {
115 os << "Runtime aborting --- recursively, so no thread-specific detail!\n";
116 return;
117 }
118 gAborting = true;
Elliott Hughese0918552011-10-28 17:18:29 -0700119 os << "Runtime aborting...\n";
Elliott Hughes6c7d2442012-02-01 18:40:47 -0800120 if (Runtime::Current() == NULL) {
121 os << "(Runtime does not yet exist!)\n";
122 return;
123 }
Elliott Hughese0918552011-10-28 17:18:29 -0700124 Thread* self = Thread::Current();
125 if (self == NULL) {
126 os << "(Aborting thread was not attached to runtime!)\n";
127 } else {
Elliott Hughes899e7892012-01-24 14:57:32 -0800128 self->Dump(os);
129 if (self->IsExceptionPending()) {
130 os << "Pending " << PrettyTypeOf(self->GetException()) << " on thread:\n"
131 << self->GetException()->Dump();
132 }
Elliott Hughese0918552011-10-28 17:18:29 -0700133 }
134 }
135};
136
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800137static Mutex& GetAbortLock() {
138 static Mutex abort_lock("abort lock");
139 return abort_lock;
140}
141
Elliott Hughesffe67362011-07-17 12:09:27 -0700142void Runtime::Abort(const char* file, int line) {
Elliott Hughes131aef82012-01-27 11:53:35 -0800143 // Ensure that we don't have multiple threads trying to abort at once,
144 // which would result in significantly worse diagnostics.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800145 MutexLock mu(GetAbortLock());
Elliott Hughes131aef82012-01-27 11:53:35 -0800146
Elliott Hughesffe67362011-07-17 12:09:27 -0700147 // Get any pending output out of the way.
148 fflush(NULL);
149
150 // Many people have difficulty distinguish aborts from crashes,
151 // so be explicit.
Elliott Hughese0918552011-10-28 17:18:29 -0700152 AbortState state;
Elliott Hughes9ee5f9c2012-02-03 18:33:16 -0800153 LOG(INTERNAL_FATAL) << Dumpable<AbortState>(state);
Elliott Hughesffe67362011-07-17 12:09:27 -0700154
Elliott Hughesffe67362011-07-17 12:09:27 -0700155 // Perform any platform-specific pre-abort actions.
156 PlatformAbort(file, line);
157
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700158 // use abort hook if we have one
159 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
160 Runtime::Current()->abort_();
161 // notreached
162 }
163
Elliott Hughesffe67362011-07-17 12:09:27 -0700164 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -0700165 // receive SIGABRT. debuggerd dumps the stack trace of the main
166 // thread, whether or not that was the thread that failed. By
167 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -0700168 // fault in the current thread, and get a useful log from debuggerd.
Elliott Hughes81ff3182012-03-23 20:35:56 -0700169 // We can also trivially tell the difference between a crash and
Elliott Hughesffe67362011-07-17 12:09:27 -0700170 // a deliberate abort by looking at the fault address.
171 *reinterpret_cast<char*>(0xdeadd00d) = 38;
172 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -0700173 // notreached
174}
175
Elliott Hughesbf86d042011-08-31 17:53:14 -0700176void Runtime::CallExitHook(jint status) {
177 if (exit_ != NULL) {
178 ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative);
179 exit_(status);
180 LOG(WARNING) << "Exit hook returned instead of exiting!";
181 }
182}
183
Brian Carlstrom8a436592011-08-15 21:27:23 -0700184// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
185// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
186// [gG] gigabytes.
187//
188// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700189// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
190// of 1024.
191//
192// The spec says the -Xmx and -Xms options must be multiples of 1024. It
193// doesn't say anything about -Xss.
194//
195// Returns 0 (a useless size) if "s" is malformed or specifies a low or
196// non-evenly-divisible value.
197//
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800198size_t ParseMemoryOption(const char* s, size_t div) {
Brian Carlstrom8a436592011-08-15 21:27:23 -0700199 // strtoul accepts a leading [+-], which we don't want,
200 // so make sure our string starts with a decimal digit.
201 if (isdigit(*s)) {
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800202 const char* s2;
203 size_t val = strtoul(s, (char**)&s2, 10);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700204 if (s2 != s) {
205 // s2 should be pointing just after the number.
206 // If this is the end of the string, the user
207 // has specified a number of bytes. Otherwise,
208 // there should be exactly one more character
209 // that specifies a multiplier.
210 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700211 // The remainder of the string is either a single multiplier
212 // character, or nothing to indicate that the value is in
213 // bytes.
214 char c = *s2++;
215 if (*s2 == '\0') {
216 size_t mul;
217 if (c == '\0') {
218 mul = 1;
219 } else if (c == 'k' || c == 'K') {
220 mul = KB;
221 } else if (c == 'm' || c == 'M') {
222 mul = MB;
223 } else if (c == 'g' || c == 'G') {
224 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700225 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700226 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700227 return 0;
228 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700229
230 if (val <= std::numeric_limits<size_t>::max() / mul) {
231 val *= mul;
232 } else {
233 // Clamp to a multiple of 1024.
234 val = std::numeric_limits<size_t>::max() & ~(1024-1);
235 }
236 } else {
237 // There's more than one character after the numeric part.
238 return 0;
239 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700240 }
241 // The man page says that a -Xm value must be a multiple of 1024.
242 if (val % div == 0) {
243 return val;
244 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700245 }
246 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700247 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700248}
249
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800250size_t ParseIntegerOrDie(const std::string& s) {
251 std::string::size_type colon = s.find(':');
252 if (colon == std::string::npos) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700253 LOG(FATAL) << "Missing integer: " << s;
254 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800255 const char* begin = &s[colon + 1];
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700256 char* end;
257 size_t result = strtoul(begin, &end, 10);
258 if (begin == end || *end != '\0') {
259 LOG(FATAL) << "Failed to parse integer in: " << s;
260 }
261 return result;
262}
263
Elliott Hughes0af55432011-08-17 18:37:28 -0700264void LoadJniLibrary(JavaVMExt* vm, const char* name) {
Elliott Hughes6cc332e2012-01-20 22:59:20 -0800265 std::string mapped_name(StringPrintf(OS_SHARED_LIB_FORMAT_STR, name));
Elliott Hughes75770752011-08-24 17:52:38 -0700266 std::string reason;
267 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700268 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
269 << reason;
270 }
271}
272
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700273Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700274 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800275 const char* boot_class_path_string = getenv("BOOTCLASSPATH");
276 if (boot_class_path_string != NULL) {
277 parsed->boot_class_path_string_ = boot_class_path_string;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700278 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800279 const char* class_path_string = getenv("CLASSPATH");
280 if (class_path_string != NULL) {
281 parsed->class_path_string_ = class_path_string;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700282 }
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700283#ifdef NDEBUG
Elliott Hughes5174fe62011-08-23 15:12:35 -0700284 // -Xcheck:jni is off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700285 parsed->check_jni_ = false;
286#else
287 // ...but on by default in debug builds.
288 parsed->check_jni_ = true;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700289#endif
Elliott Hughes85d15452011-09-16 17:33:01 -0700290
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700291 parsed->heap_initial_size_ = Heap::kInitialSize;
292 parsed->heap_maximum_size_ = Heap::kMaximumSize;
jeffhaoc1160702011-10-27 15:48:45 -0700293 parsed->heap_growth_limit_ = 0; // 0 means no growth limit
Brian Carlstromf734cf52011-08-17 16:28:14 -0700294 parsed->stack_size_ = Thread::kDefaultStackSize;
295
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800296 parsed->is_compiler_ = false;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700297 parsed->is_zygote_ = false;
298
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700299 parsed->jni_globals_max_ = 0;
Elliott Hughesfc861622011-10-17 17:57:47 -0700300 parsed->lock_profiling_threshold_ = 0;
301 parsed->hook_is_sensitive_thread_ = NULL;
302
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700303 parsed->hook_vfprintf_ = vfprintf;
304 parsed->hook_exit_ = exit;
305 parsed->hook_abort_ = abort;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700306
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800307// gLogVerbosity.class_linker = true; // TODO: don't check this in!
308// gLogVerbosity.compiler = true; // TODO: don't check this in!
309// gLogVerbosity.heap = true; // TODO: don't check this in!
310// gLogVerbosity.gc = true; // TODO: don't check this in!
311// gLogVerbosity.jdwp = true; // TODO: don't check this in!
312// gLogVerbosity.jni = true; // TODO: don't check this in!
313// gLogVerbosity.monitor = true; // TODO: don't check this in!
314// gLogVerbosity.startup = true; // TODO: don't check this in!
315// gLogVerbosity.third_party_jni = true; // TODO: don't check this in!
316// gLogVerbosity.threads = true; // TODO: don't check this in!
317
jeffhaob5e81852012-03-12 11:15:45 -0700318 parsed->method_trace_ = false;
319 parsed->method_trace_file_ = "/data/method-trace-file.bin";
320 parsed->method_trace_file_size_ = 10 * MB;
321
Brian Carlstrom8a436592011-08-15 21:27:23 -0700322 for (size_t i = 0; i < options.size(); ++i) {
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800323 const std::string option(options[i].first);
Brian Carlstrom0796af02011-10-12 14:31:45 -0700324 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700325 LOG(INFO) << "option[" << i << "]=" << option;
326 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800327 if (StartsWith(option, "-Xbootclasspath:")) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800328 parsed->boot_class_path_string_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700329 } else if (option == "-classpath" || option == "-cp") {
330 // TODO: support -Djava.class.path
331 i++;
332 if (i == options.size()) {
333 // TODO: usage
334 LOG(FATAL) << "Missing required class path value for " << option;
335 return NULL;
336 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800337 const StringPiece& value = options[i].first;
338 parsed->class_path_string_ = value.data();
339 } else if (option == "bootclasspath") {
340 parsed->boot_class_path_
341 = reinterpret_cast<const std::vector<const DexFile*>*>(options[i].second);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800342 } else if (StartsWith(option, "-Ximage:")) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800343 parsed->image_ = option.substr(strlen("-Ximage:")).data();
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800344 } else if (StartsWith(option, "-Xcheck:jni")) {
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700345 parsed->check_jni_ = true;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800346 } else if (StartsWith(option, "-Xrunjdwp:") || StartsWith(option, "-agentlib:jdwp=")) {
347 std::string tail(option.substr(option[1] == 'X' ? 10 : 15));
Elliott Hughes3bb81562011-10-21 18:52:59 -0700348 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
349 LOG(FATAL) << "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
350 << "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n";
351 return NULL;
352 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800353 } else if (StartsWith(option, "-Xms")) {
354 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).c_str(), 1024);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700355 if (size == 0) {
356 if (ignore_unrecognized) {
357 continue;
358 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700359 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700360 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700361 return NULL;
362 }
363 parsed->heap_initial_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800364 } else if (StartsWith(option, "-Xmx")) {
365 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).c_str(), 1024);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700366 if (size == 0) {
367 if (ignore_unrecognized) {
368 continue;
369 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700370 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700371 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700372 return NULL;
373 }
374 parsed->heap_maximum_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800375 } else if (StartsWith(option, "-XX:HeapGrowthLimit=")) {
376 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).c_str(), 1024);
jeffhaoc1160702011-10-27 15:48:45 -0700377 if (size == 0) {
378 if (ignore_unrecognized) {
379 continue;
380 }
381 // TODO: usage
382 LOG(FATAL) << "Failed to parse " << option;
383 return NULL;
384 }
385 parsed->heap_growth_limit_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800386 } else if (StartsWith(option, "-Xss")) {
387 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).c_str(), 1);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700388 if (size == 0) {
389 if (ignore_unrecognized) {
390 continue;
391 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700392 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700393 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700394 return NULL;
395 }
396 parsed->stack_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800397 } else if (StartsWith(option, "-D")) {
398 parsed->properties_.push_back(option.substr(strlen("-D")));
399 } else if (StartsWith(option, "-Xjnitrace:")) {
400 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:"));
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700401 } else if (option == "compiler") {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800402 parsed->is_compiler_ = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700403 } else if (option == "-Xzygote") {
404 parsed->is_zygote_ = true;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800405 } else if (StartsWith(option, "-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700406 std::vector<std::string> verbose_options;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800407 Split(option.substr(strlen("-verbose:")), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700408 for (size_t i = 0; i < verbose_options.size(); ++i) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800409 if (verbose_options[i] == "class") {
410 gLogVerbosity.class_linker = true;
411 } else if (verbose_options[i] == "compiler") {
412 gLogVerbosity.compiler = true;
413 } else if (verbose_options[i] == "heap") {
414 gLogVerbosity.heap = true;
415 } else if (verbose_options[i] == "gc") {
416 gLogVerbosity.gc = true;
417 } else if (verbose_options[i] == "jdwp") {
418 gLogVerbosity.jdwp = true;
419 } else if (verbose_options[i] == "jni") {
420 gLogVerbosity.jni = true;
421 } else if (verbose_options[i] == "monitor") {
422 gLogVerbosity.monitor = true;
423 } else if (verbose_options[i] == "startup") {
424 gLogVerbosity.startup = true;
425 } else if (verbose_options[i] == "third-party-jni") {
426 gLogVerbosity.third_party_jni = true;
427 } else if (verbose_options[i] == "threads") {
428 gLogVerbosity.threads = true;
429 } else {
430 LOG(WARNING) << "Ignoring unknown -verbose option: " << verbose_options[i];
431 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700432 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800433 } else if (StartsWith(option, "-Xjnigreflimit:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700434 parsed->jni_globals_max_ = ParseIntegerOrDie(option);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800435 } else if (StartsWith(option, "-Xlockprofthreshold:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700436 parsed->lock_profiling_threshold_ = ParseIntegerOrDie(option);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800437 } else if (StartsWith(option, "-Xstacktracefile:")) {
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700438// always show stack traces in debug builds
439#ifdef NDEBUG
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800440 parsed->stack_trace_file_ = option.substr(strlen("-Xstacktracefile:"));
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700441#endif
Elliott Hughesfc861622011-10-17 17:57:47 -0700442 } else if (option == "sensitiveThread") {
443 parsed->hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(options[i].second);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700444 } else if (option == "vfprintf") {
445 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
446 } else if (option == "exit") {
447 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
448 } else if (option == "abort") {
449 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700450 } else if (option == "host-prefix") {
451 parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second);
Elliott Hughesb3e66df2012-01-12 14:49:18 -0800452 } else if (option == "-Xgenregmap" || option == "-Xgc:precise") {
453 // We silently ignore these for backwards compatibility.
jeffhaob5e81852012-03-12 11:15:45 -0700454 } else if (option == "-Xmethod-trace") {
455 parsed->method_trace_ = true;
456 } else if (StartsWith(option, "-Xmethod-trace-file:")) {
457 parsed->method_trace_file_ = option.substr(strlen("-Xmethod-trace-file:"));
458 } else if (StartsWith(option, "-Xmethod-trace-file-size:")) {
459 parsed->method_trace_file_size_ = ParseIntegerOrDie(option);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700460 } else {
461 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700462 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700463 LOG(ERROR) << "Unrecognized option " << option;
464 // TODO: this should exit, but for now tolerate unknown options
465 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700466 }
467 }
468 }
469
Brian Carlstrom223f20f2012-02-04 23:06:55 -0800470 if (!parsed->is_compiler_ && parsed->image_.empty()) {
471 parsed->image_ += GetAndroidRoot();
472 parsed->image_ += "/framework/boot.art";
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700473 }
jeffhaoc1160702011-10-27 15:48:45 -0700474 if (parsed->heap_growth_limit_ == 0) {
475 parsed->heap_growth_limit_ = parsed->heap_maximum_size_;
476 }
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700477
Elliott Hughescac6cc72011-11-03 20:31:21 -0700478 LOG(INFO) << "Build type: "
479#ifndef NDEBUG
480 << "debug"
481#else
482 << "optimized"
483#endif
484 << "; CheckJNI: " << (parsed->check_jni_ ? "on" : "off");
Elliott Hughes85d15452011-09-16 17:33:01 -0700485
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700486 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700487}
488
489Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700490 // TODO: acquire a static mutex on Runtime to avoid racing.
491 if (Runtime::instance_ != NULL) {
492 return NULL;
493 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700494 instance_ = new Runtime;
495 if (!instance_->Init(options, ignore_unrecognized)) {
496 delete instance_;
497 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700498 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700499 return instance_;
500}
Elliott Hughes0af55432011-08-17 18:37:28 -0700501
Brian Carlstromaded5f72011-10-07 17:15:04 -0700502void CreateSystemClassLoader() {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800503 if (Runtime::Current()->UseCompileTimeClassPath()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700504 return;
505 }
506
507 Thread* self = Thread::Current();
508
509 // Must be in the kNative state for calling native methods.
510 CHECK_EQ(self->GetState(), Thread::kNative);
511
512 JNIEnv* env = self->GetJniEnv();
Brian Carlstromdf143242011-10-10 18:05:34 -0700513 ScopedLocalRef<jclass> ClassLoader_class(env, env->FindClass("java/lang/ClassLoader"));
514 CHECK(ClassLoader_class.get() != NULL);
515 jmethodID getSystemClassLoader = env->GetStaticMethodID(ClassLoader_class.get(),
516 "getSystemClassLoader",
517 "()Ljava/lang/ClassLoader;");
518 CHECK(getSystemClassLoader != NULL);
519 ScopedLocalRef<jobject> class_loader(env, env->CallStaticObjectMethod(ClassLoader_class.get(),
520 getSystemClassLoader));
521 CHECK(class_loader.get() != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700522
Brian Carlstromdf143242011-10-10 18:05:34 -0700523 Thread::Current()->SetClassLoaderOverride(Decode<ClassLoader*>(env, class_loader.get()));
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500524
525 ScopedLocalRef<jclass> Thread_class(env, env->FindClass("java/lang/Thread"));
526 CHECK(Thread_class.get() != NULL);
527 jfieldID contextClassLoader = env->GetFieldID(Thread_class.get(),
528 "contextClassLoader",
529 "Ljava/lang/ClassLoader;");
530 CHECK(contextClassLoader != NULL);
531 ScopedLocalRef<jobject> self_jobject(env, AddLocalReference<jobject>(env, self->GetPeer()));
532 env->SetObjectField(self_jobject.get(), contextClassLoader, class_loader.get());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700533}
534
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700535void Runtime::Start() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800536 VLOG(startup) << "Runtime::Start entering";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700537
538 CHECK(host_prefix_.empty()) << host_prefix_;
539
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700540 // Restore main thread state to kNative as expected by native code
541 Thread::Current()->SetState(Thread::kNative);
542
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700543 started_ = true;
544
Brian Carlstromae826982011-11-09 01:33:42 -0800545 // InitNativeMethods needs to be after started_ so that the classes
546 // it touches will have methods linked to the oat file if necessary.
547 InitNativeMethods();
548
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500549 Thread::FinishStartup();
550
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700551 if (!is_zygote_) {
552 DidForkFromZygote();
553 }
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700554
Elliott Hughes85d15452011-09-16 17:33:01 -0700555 StartDaemonThreads();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700556
Brian Carlstromaded5f72011-10-07 17:15:04 -0700557 CreateSystemClassLoader();
558
Elliott Hughes726079d2011-10-07 18:43:44 -0700559 Thread::Current()->GetJniEnv()->locals.AssertEmpty();
560
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800561 VLOG(startup) << "Runtime::Start exiting";
Elliott Hughes85d15452011-09-16 17:33:01 -0700562}
563
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700564void Runtime::DidForkFromZygote() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700565 is_zygote_ = false;
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700566
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700567 StartSignalCatcher();
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700568
569 // Start the JDWP thread. If the command-line debugger flags specified "suspend=y",
570 // this will pause the runtime, so we probably want this to come last.
571 Dbg::StartJdwp();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700572}
573
574void Runtime::StartSignalCatcher() {
575 if (!is_zygote_) {
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700576 signal_catcher_ = new SignalCatcher(stack_trace_file_);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700577 }
578}
579
Elliott Hughes85d15452011-09-16 17:33:01 -0700580void Runtime::StartDaemonThreads() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800581 VLOG(startup) << "Runtime::StartDaemonThreads entering";
Elliott Hughes85d15452011-09-16 17:33:01 -0700582
Elliott Hughes719b3232011-09-25 17:42:19 -0700583 Thread* self = Thread::Current();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700584
585 // Must be in the kNative state for calling native methods.
586 CHECK_EQ(self->GetState(), Thread::kNative);
587
Elliott Hughes719b3232011-09-25 17:42:19 -0700588 JNIEnv* env = self->GetJniEnv();
Elliott Hughes726079d2011-10-07 18:43:44 -0700589 ScopedLocalRef<jclass> c(env, env->FindClass("java/lang/Daemons"));
590 CHECK(c.get() != NULL);
591 jmethodID mid = env->GetStaticMethodID(c.get(), "start", "()V");
Elliott Hughes719b3232011-09-25 17:42:19 -0700592 CHECK(mid != NULL);
Elliott Hughes726079d2011-10-07 18:43:44 -0700593 env->CallStaticVoidMethod(c.get(), mid);
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800594 CHECK(!env->ExceptionCheck());
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700595
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800596 VLOG(startup) << "Runtime::StartDaemonThreads exiting";
Carl Shapiro61e019d2011-07-14 16:53:09 -0700597}
598
Elliott Hughes6b355752012-01-13 16:49:08 -0800599bool Runtime::IsShuttingDown() const {
600 return shutting_down_;
601}
602
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700603bool Runtime::IsStarted() const {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700604 return started_;
605}
606
Elliott Hughes0af55432011-08-17 18:37:28 -0700607bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700608 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700609
Elliott Hughes90a33692011-08-30 13:27:07 -0700610 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
611 if (options.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700612 LOG(ERROR) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700613 return false;
614 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800615 VLOG(startup) << "Runtime::Init -verbose:startup enabled";
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700616
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700617 SetJniGlobalsMax(options->jni_globals_max_);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800618 Monitor::Init(options->lock_profiling_threshold_, options->hook_is_sensitive_thread_);
Elliott Hughes32d6e1e2011-10-11 14:47:44 -0700619
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700620 host_prefix_ = options->host_prefix_;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800621 boot_class_path_string_ = options->boot_class_path_string_;
622 class_path_string_ = options->class_path_string_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700623 properties_ = options->properties_;
624
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800625 is_compiler_ = options->is_compiler_;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700626 is_zygote_ = options->is_zygote_;
627
Elliott Hughes0af55432011-08-17 18:37:28 -0700628 vfprintf_ = options->hook_vfprintf_;
629 exit_ = options->hook_exit_;
630 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700631
Elliott Hughesbe759c62011-09-08 19:38:21 -0700632 default_stack_size_ = options->stack_size_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700633 stack_trace_file_ = options->stack_trace_file_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700634
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700635 monitor_list_ = new MonitorList;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800636 thread_list_ = new ThreadList;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700637 intern_table_ = new InternTable;
638
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800639 verifier::DexVerifier::InitGcMaps();
640
641 heap_ = new Heap(options->heap_initial_size_,
642 options->heap_growth_limit_,
643 options->heap_maximum_size_,
644 options->image_);
jeffhao25045522012-03-13 19:34:37 -0700645 heap_->EnableObjectValidation();
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700646
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700647 BlockSignals();
648
Elliott Hughesa0957642011-09-02 14:27:33 -0700649 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700650
Elliott Hughesbe759c62011-09-08 19:38:21 -0700651 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700652
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700653 // ClassLinker needs an attached thread, but we can't fully attach a thread
654 // without creating objects.
655 Thread::Attach(this, "main", false);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700656
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700657 // Set us to runnable so tools using a runtime can allocate and GC by default
658 Thread::Current()->SetState(Thread::kRunnable);
659
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800660 CHECK_GE(GetHeap()->GetSpaces().size(), 1U);
661 if (GetHeap()->GetSpaces()[0]->IsImageSpace()) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800662 class_linker_ = ClassLinker::CreateFromImage(intern_table_);
663 } else {
664 CHECK(options->boot_class_path_ != NULL);
665 CHECK_NE(options->boot_class_path_->size(), 0U);
666 class_linker_ = ClassLinker::CreateFromCompiler(*options->boot_class_path_, intern_table_);
667 }
668 CHECK(class_linker_ != NULL);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700669
jeffhaob5e81852012-03-12 11:15:45 -0700670 method_trace_ = options->method_trace_;
671 method_trace_file_ = options->method_trace_file_;
672 method_trace_file_size_ = options->method_trace_file_size_;
673
674 if (options->method_trace_) {
675 Trace::Start(options->method_trace_file_.c_str(), -1, options->method_trace_file_size_, 0, false);
676 }
677
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800678 VLOG(startup) << "Runtime::Init exiting";
Carl Shapiro1fb86202011-06-27 17:43:13 -0700679 return true;
680}
681
Elliott Hughes038a8062011-09-18 14:12:41 -0700682void Runtime::InitNativeMethods() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800683 VLOG(startup) << "Runtime::InitNativeMethods entering";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700684 Thread* self = Thread::Current();
685 JNIEnv* env = self->GetJniEnv();
686
Elliott Hughes418d20f2011-09-22 14:00:39 -0700687 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Brian Carlstromaded5f72011-10-07 17:15:04 -0700688 CHECK_EQ(self->GetState(), Thread::kNative);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700689
Elliott Hughes418d20f2011-09-22 14:00:39 -0700690 // First set up JniConstants, which is used by both the runtime's built-in native
691 // methods and libcore.
Elliott Hughesfea966e2011-09-22 10:26:20 -0700692 JniConstants::init(env);
693
Elliott Hughes418d20f2011-09-22 14:00:39 -0700694 // Then set up the native methods provided by the runtime itself.
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700695 RegisterRuntimeNativeMethods(env);
696
Elliott Hughes418d20f2011-09-22 14:00:39 -0700697 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
698 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
699 // the library that implements System.loadLibrary!
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700700 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800701 VLOG(startup) << "Runtime::InitNativeMethods exiting";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700702}
703
704void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
705#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Ian Rogers5167c972012-02-03 10:41:20 -0800706 // Register Throwable first so that registration of other native methods can throw exceptions
707 REGISTER(register_java_lang_Throwable);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700708 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700709 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700710 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700711 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700712 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700713 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700714 REGISTER(register_java_lang_Object);
715 REGISTER(register_java_lang_Runtime);
716 REGISTER(register_java_lang_String);
717 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700718 REGISTER(register_java_lang_Thread);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700719 REGISTER(register_java_lang_VMClassLoader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700720 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700721 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700722 REGISTER(register_java_lang_reflect_Field);
723 REGISTER(register_java_lang_reflect_Method);
Jesse Wilson95caa792011-10-12 18:14:17 -0400724 REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700725 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700726 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700727 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700728 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700729#undef REGISTER
730}
731
Elliott Hughes8daa0922011-09-11 13:46:25 -0700732void Runtime::Dump(std::ostream& os) {
Elliott Hughese27955c2011-08-26 15:21:24 -0700733 // TODO: dump other runtime statistics?
Elliott Hughescac6cc72011-11-03 20:31:21 -0700734 GetClassLinker()->DumpForSigQuit(os);
735 GetInternTable()->DumpForSigQuit(os);
Elliott Hughes42ee1422011-09-06 12:33:32 -0700736 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700737
738 thread_list_->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700739}
740
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800741void Runtime::DumpLockHolders(std::ostream& os) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800742 pid_t heap_lock_owner = GetHeap()->GetLockOwner();
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800743 pid_t thread_list_lock_owner = GetThreadList()->GetLockOwner();
744 pid_t classes_lock_owner = GetClassLinker()->GetClassesLockOwner();
745 pid_t dex_lock_owner = GetClassLinker()->GetDexLockOwner();
746 if ((heap_lock_owner | thread_list_lock_owner | classes_lock_owner | dex_lock_owner) != 0) {
747 os << "Heap lock owner tid: " << heap_lock_owner << "\n"
748 << "ThreadList lock owner tid: " << thread_list_lock_owner << "\n"
749 << "ClassLinker classes lock owner tid: " << classes_lock_owner << "\n"
750 << "ClassLinker dex lock owner tid: " << dex_lock_owner << "\n";
751 }
752}
753
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700754void Runtime::SetStatsEnabled(bool new_state) {
755 if (new_state == true) {
756 GetStats()->Clear(~0);
757 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
758 Thread::Current()->GetStats()->Clear(~0);
759 }
760 stats_enabled_ = new_state;
761}
762
763void Runtime::ResetStats(int kinds) {
764 GetStats()->Clear(kinds & 0xffff);
765 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
766 Thread::Current()->GetStats()->Clear(kinds >> 16);
767}
768
769RuntimeStats* Runtime::GetStats() {
770 return &stats_;
771}
772
773int32_t Runtime::GetStat(int kind) {
774 RuntimeStats* stats;
775 if (kind < (1<<16)) {
776 stats = GetStats();
777 } else {
778 stats = Thread::Current()->GetStats();
779 kind >>= 16;
780 }
781 switch (kind) {
782 case KIND_ALLOCATED_OBJECTS:
783 return stats->allocated_objects;
784 case KIND_ALLOCATED_BYTES:
785 return stats->allocated_bytes;
786 case KIND_FREED_OBJECTS:
787 return stats->freed_objects;
788 case KIND_FREED_BYTES:
789 return stats->freed_bytes;
790 case KIND_GC_INVOCATIONS:
791 return stats->gc_for_alloc_count;
792 case KIND_CLASS_INIT_COUNT:
793 return stats->class_init_count;
794 case KIND_CLASS_INIT_TIME:
795 // Convert ns to us, reduce to 32 bits.
796 return (int) (stats->class_init_time_ns / 1000);
797 case KIND_EXT_ALLOCATED_OBJECTS:
798 case KIND_EXT_ALLOCATED_BYTES:
799 case KIND_EXT_FREED_OBJECTS:
800 case KIND_EXT_FREED_BYTES:
801 return 0; // backward compatibility
802 default:
803 CHECK(false);
804 return -1; // unreachable
805 }
806}
807
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700808void Runtime::BlockSignals() {
809 sigset_t sigset;
810 if (sigemptyset(&sigset) == -1) {
811 PLOG(FATAL) << "sigemptyset failed";
812 }
813 if (sigaddset(&sigset, SIGPIPE) == -1) {
814 PLOG(ERROR) << "sigaddset SIGPIPE failed";
815 }
816 // SIGQUIT is used to dump the runtime's state (including stack traces).
817 if (sigaddset(&sigset, SIGQUIT) == -1) {
818 PLOG(ERROR) << "sigaddset SIGQUIT failed";
819 }
820 // SIGUSR1 is used to initiate a heap dump.
821 if (sigaddset(&sigset, SIGUSR1) == -1) {
822 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
823 }
824 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
825}
826
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700827void Runtime::AttachCurrentThread(const char* name, bool as_daemon) {
828 Thread::Attach(instance_, name, as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700829}
830
Elliott Hughesd92bec42011-09-02 17:04:36 -0700831void Runtime::DetachCurrentThread() {
Elliott Hughes038a8062011-09-18 14:12:41 -0700832 // TODO: check we're not calling DetachCurrentThread from a call stack that
833 // includes managed frames. (It's only valid if the stack is all-native.)
Elliott Hughes02b48d12011-09-07 17:15:51 -0700834 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700835}
836
Brian Carlstrome24fa612011-09-29 00:53:55 -0700837void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700838 Dbg::VisitRoots(visitor, arg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700839 class_linker_->VisitRoots(visitor, arg);
840 intern_table_->VisitRoots(visitor, arg);
841 java_vm_->VisitRoots(visitor, arg);
842 thread_list_->VisitRoots(visitor, arg);
843 visitor(jni_stub_array_, arg);
844 visitor(abstract_method_error_stub_array_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700845 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
846 visitor(resolution_stub_array_[i], arg);
847 }
Ian Rogers19846512012-02-24 11:42:47 -0800848 visitor(resolution_method_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700849 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
850 visitor(callee_save_method_[i], arg);
851 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700852}
853
Ian Rogers169c9a72011-11-13 20:13:17 -0800854bool Runtime::HasJniDlsymLookupStub() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700855 return jni_stub_array_ != NULL;
856}
857
Ian Rogers169c9a72011-11-13 20:13:17 -0800858ByteArray* Runtime::GetJniDlsymLookupStub() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700859 CHECK(jni_stub_array_ != NULL);
860 return jni_stub_array_;
861}
862
Ian Rogers169c9a72011-11-13 20:13:17 -0800863void Runtime::SetJniDlsymLookupStub(ByteArray* jni_stub_array) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700864 CHECK(jni_stub_array != NULL) << " jni_stub_array=" << jni_stub_array;
865 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array)
866 << "jni_stub_array_=" << jni_stub_array_ << " jni_stub_array=" << jni_stub_array;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700867 jni_stub_array_ = jni_stub_array;
868}
869
870bool Runtime::HasAbstractMethodErrorStubArray() const {
871 return abstract_method_error_stub_array_ != NULL;
872}
873
874ByteArray* Runtime::GetAbstractMethodErrorStubArray() const {
875 CHECK(abstract_method_error_stub_array_ != NULL);
876 return abstract_method_error_stub_array_;
877}
878
879void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) {
880 CHECK(abstract_method_error_stub_array != NULL);
881 CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array);
882 abstract_method_error_stub_array_ = abstract_method_error_stub_array;
883}
884
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700885
886Runtime::TrampolineType Runtime::GetTrampolineType(Method* method) {
887 if (method == NULL) {
888 return Runtime::kUnknownMethod;
889 } else if (method->IsStatic()) {
890 return Runtime::kStaticMethod;
891 } else {
Ian Rogersfb6adba2012-03-04 21:51:51 -0800892 return Runtime::kUnknownMethod;
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700893 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700894}
895
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700896bool Runtime::HasResolutionStubArray(TrampolineType type) const {
897 return resolution_stub_array_[type] != NULL;
Ian Rogersad25ac52011-10-04 19:13:33 -0700898}
899
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700900ByteArray* Runtime::GetResolutionStubArray(TrampolineType type) const {
901 CHECK(HasResolutionStubArray(type));
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700902 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastTrampolineMethodType));
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700903 return resolution_stub_array_[type];
904}
905
906void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700907 CHECK(resolution_stub_array != NULL);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700908 CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array);
909 resolution_stub_array_[type] = resolution_stub_array;
Ian Rogersad25ac52011-10-04 19:13:33 -0700910}
911
Ian Rogers19846512012-02-24 11:42:47 -0800912Method* Runtime::CreateResolutionMethod() {
913 Class* method_class = Method::GetMethodClass();
914 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
915 method->SetDeclaringClass(method_class);
916 // TODO: use a special method for resolution method saves
917 method->SetDexMethodIndex(DexFile::kDexNoIndex16);
918 ByteArray* unknown_resolution_stub = GetResolutionStubArray(kUnknownMethod);
919 CHECK(unknown_resolution_stub != NULL);
920 method->SetCode(unknown_resolution_stub->GetData());
921 return method.get();
922}
923
924bool Runtime::HasResolutionMethod() const {
925 return resolution_method_ != NULL;
926}
927
928// Returns a special method that calls into a trampoline for runtime method resolution
929Method* Runtime::GetResolutionMethod() const {
930 CHECK(HasResolutionMethod());
931 return resolution_method_;
932}
933
934void Runtime::SetResolutionMethod(Method* method) {
935 resolution_method_ = method;
936}
937
Brian Carlstromae826982011-11-09 01:33:42 -0800938Method* Runtime::CreateCalleeSaveMethod(InstructionSet instruction_set, CalleeSaveType type) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700939 Class* method_class = Method::GetMethodClass();
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700940 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
Ian Rogersff1ed472011-09-20 13:46:24 -0700941 method->SetDeclaringClass(method_class);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800942 // TODO: use a special method for callee saves
Ian Rogers19846512012-02-24 11:42:47 -0800943 method->SetDexMethodIndex(DexFile::kDexNoIndex16);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700944 method->SetCode(NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800945 if ((instruction_set == kThumb2) || (instruction_set == kArm)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700946 uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6) | (1 << art::arm::R7) |
947 (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11);
948 uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3);
949 uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9);
950 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills :0) |
951 (type == kSaveAll ? all_spills :0) | (1 << art::arm::LR);
952 uint32_t fp_all_spills = (1 << art::arm::S0) | (1 << art::arm::S1) | (1 << art::arm::S2) |
953 (1 << art::arm::S3) | (1 << art::arm::S4) | (1 << art::arm::S5) |
954 (1 << art::arm::S6) | (1 << art::arm::S7) | (1 << art::arm::S8) |
955 (1 << art::arm::S9) | (1 << art::arm::S10) | (1 << art::arm::S11) |
956 (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) |
957 (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) |
958 (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) |
959 (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) |
960 (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) |
961 (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
962 (1 << art::arm::S30) | (1 << art::arm::S31);
963 uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
964 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
965 __builtin_popcount(fp_spills) /* fprs */ +
966 1 /* Method* */) * kPointerSize, kStackAlignment);
Ian Rogers15fdb8c2011-09-25 15:45:07 -0700967 method->SetFrameSizeInBytes(frame_size);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700968 method->SetCoreSpillMask(core_spills);
969 method->SetFpSpillMask(fp_spills);
Brian Carlstromae826982011-11-09 01:33:42 -0800970 } else if (instruction_set == kX86) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700971 method->SetFrameSizeInBytes(32);
Ian Rogersb41b33b2012-03-20 14:22:54 -0700972 method->SetCoreSpillMask((1 << art::x86::EBP) | (1 << art::x86::ESI) | (1 << art::x86::EDI));
Ian Rogersff1ed472011-09-20 13:46:24 -0700973 method->SetFpSpillMask(0);
974 } else {
975 UNIMPLEMENTED(FATAL);
976 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700977 return method.get();
Ian Rogersff1ed472011-09-20 13:46:24 -0700978}
979
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700980bool Runtime::HasCalleeSaveMethod(CalleeSaveType type) const {
981 return callee_save_method_[type] != NULL;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700982}
983
Brian Carlstrome24fa612011-09-29 00:53:55 -0700984// Returns a special method that describes all callee saves being spilled to the stack.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700985Method* Runtime::GetCalleeSaveMethod(CalleeSaveType type) const {
986 CHECK(HasCalleeSaveMethod(type));
987 return callee_save_method_[type];
Brian Carlstrome24fa612011-09-29 00:53:55 -0700988}
989
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700990void Runtime::SetCalleeSaveMethod(Method* method, CalleeSaveType type) {
991 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
992 callee_save_method_[type] = method;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700993}
994
jeffhao2692b572011-12-16 15:42:28 -0800995void Runtime::EnableMethodTracing(Trace* tracer) {
996 CHECK(!IsMethodTracingActive());
997 tracer_ = tracer;
998}
999
1000void Runtime::DisableMethodTracing() {
1001 CHECK(IsMethodTracingActive());
1002 delete tracer_;
1003 tracer_ = NULL;
1004}
1005
1006bool Runtime::IsMethodTracingActive() const {
1007 return (tracer_ != NULL);
1008}
1009
1010Trace* Runtime::GetTracer() const {
1011 CHECK(IsMethodTracingActive());
1012 return tracer_;
1013}
1014
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001015const std::vector<const DexFile*>& Runtime::GetCompileTimeClassPath(const ClassLoader* class_loader) {
1016 if (class_loader == NULL) {
1017 return GetClassLinker()->GetBootClassPath();
1018 }
1019 CHECK(UseCompileTimeClassPath());
1020 CompileTimeClassPaths::const_iterator it = compile_time_class_paths_.find(class_loader);
1021 CHECK(it != compile_time_class_paths_.end());
1022 return it->second;
1023}
1024
1025void Runtime::SetCompileTimeClassPath(const ClassLoader* class_loader, std::vector<const DexFile*>& class_path) {
1026 CHECK(!IsStarted());
1027 use_compile_time_class_path_ = true;
1028 compile_time_class_paths_[class_loader] = class_path;
1029}
1030
Carl Shapiro1fb86202011-06-27 17:43:13 -07001031} // namespace art