blob: 6a454a0bcd5d682b5630a2e535730b9130886f6e [file] [log] [blame]
Carl Shapiro1fb86202011-06-27 17:43:13 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "runtime.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -07004
Brian Carlstromdbf05b72011-12-15 00:55:24 -08005#include <signal.h>
6
Elliott Hughesffe67362011-07-17 12:09:27 -07007#include <cstdio>
8#include <cstdlib>
Brian Carlstrom8a436592011-08-15 21:27:23 -07009#include <limits>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070010#include <vector>
Elliott Hughesffe67362011-07-17 12:09:27 -070011
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070012#include "class_linker.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070013#include "class_loader.h"
Elliott Hughes3bb81562011-10-21 18:52:59 -070014#include "debugger.h"
Elliott Hughes4d6850c2012-01-18 15:55:06 -080015#include "dex_verifier.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070016#include "heap.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070017#include "image.h"
Elliott Hughescf4c6c42011-09-01 15:16:42 -070018#include "intern_table.h"
Elliott Hughesc5f7c912011-08-18 14:00:42 -070019#include "jni_internal.h"
Elliott Hughes32d6e1e2011-10-11 14:47:44 -070020#include "monitor.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070021#include "oat_file.h"
Elliott Hughes726079d2011-10-07 18:43:44 -070022#include "ScopedLocalRef.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070023#include "signal_catcher.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070024#include "space.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070025#include "thread.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070026#include "thread_list.h"
Elliott Hughes3bb81562011-10-21 18:52:59 -070027#include "UniquePtr.h"
Carl Shapiro61e019d2011-07-14 16:53:09 -070028
Elliott Hughes90a33692011-08-30 13:27:07 -070029// TODO: this drags in cutil/log.h, which conflicts with our logging.h.
30#include "JniConstants.h"
31
Carl Shapiro1fb86202011-06-27 17:43:13 -070032namespace art {
33
Carl Shapiro2ed144c2011-07-26 16:52:08 -070034Runtime* Runtime::instance_ = NULL;
35
Elliott Hughes131aef82012-01-27 11:53:35 -080036Mutex Runtime::abort_lock_("abort lock");
37
Elliott Hughesdcc24742011-09-07 14:02:44 -070038Runtime::Runtime()
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080039 : is_zygote_(false),
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -070040 default_stack_size_(Thread::kDefaultStackSize),
Elliott Hughesc33a32b2011-10-11 18:18:07 -070041 monitor_list_(NULL),
Elliott Hughesdcc24742011-09-07 14:02:44 -070042 thread_list_(NULL),
43 intern_table_(NULL),
44 class_linker_(NULL),
45 signal_catcher_(NULL),
46 java_vm_(NULL),
Brian Carlstrom16192862011-09-12 17:50:06 -070047 jni_stub_array_(NULL),
Brian Carlstrome24fa612011-09-29 00:53:55 -070048 abstract_method_error_stub_array_(NULL),
Elliott Hughes6b355752012-01-13 16:49:08 -080049 shutting_down_(false),
Elliott Hughesdcc24742011-09-07 14:02:44 -070050 started_(false),
51 vfprintf_(NULL),
52 exit_(NULL),
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070053 abort_(NULL),
jeffhao2692b572011-12-16 15:42:28 -080054 stats_enabled_(false),
55 tracer_(NULL) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -070056 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
57 resolution_stub_array_[i] = NULL;
58 }
59 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
60 callee_save_method_[i] = NULL;
61 }
Elliott Hughesdcc24742011-09-07 14:02:44 -070062}
63
Carl Shapiro61e019d2011-07-14 16:53:09 -070064Runtime::~Runtime() {
Elliott Hughes6b355752012-01-13 16:49:08 -080065 shutting_down_ = true;
66
Elliott Hughesd1cc8362011-10-24 16:58:50 -070067 Dbg::StopJdwp();
68
Elliott Hughes5fe594f2011-09-08 12:33:17 -070069 // Make sure our internal threads are dead before we start tearing down things they're using.
70 delete signal_catcher_;
Elliott Hughes038a8062011-09-18 14:12:41 -070071 // TODO: GC thread.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070072
Elliott Hughes038a8062011-09-18 14:12:41 -070073 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
Elliott Hughes93e74e82011-09-13 11:07:03 -070074 delete thread_list_;
Elliott Hughesc33a32b2011-10-11 18:18:07 -070075 delete monitor_list_;
Elliott Hughes93e74e82011-09-13 11:07:03 -070076
Carl Shapiro61e019d2011-07-14 16:53:09 -070077 delete class_linker_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070078 Heap::Destroy();
Elliott Hughes4d6850c2012-01-18 15:55:06 -080079 verifier::DexVerifier::DeleteGcMaps();
Elliott Hughescf4c6c42011-09-01 15:16:42 -070080 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070081 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -070082 Thread::Shutdown();
Carl Shapiro4acf4642011-07-26 18:54:13 -070083 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070084 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -070085 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -070086}
87
Elliott Hughescac6cc72011-11-03 20:31:21 -070088static bool gAborting = false;
89
Elliott Hughese0918552011-10-28 17:18:29 -070090struct AbortState {
91 void Dump(std::ostream& os) {
Elliott Hughescac6cc72011-11-03 20:31:21 -070092 if (gAborting) {
93 os << "Runtime aborting --- recursively, so no thread-specific detail!\n";
94 return;
95 }
96 gAborting = true;
Elliott Hughese0918552011-10-28 17:18:29 -070097 os << "Runtime aborting...\n";
98 Thread* self = Thread::Current();
99 if (self == NULL) {
100 os << "(Aborting thread was not attached to runtime!)\n";
101 } else {
Elliott Hughes899e7892012-01-24 14:57:32 -0800102 self->Dump(os);
103 if (self->IsExceptionPending()) {
104 os << "Pending " << PrettyTypeOf(self->GetException()) << " on thread:\n"
105 << self->GetException()->Dump();
106 }
Elliott Hughese0918552011-10-28 17:18:29 -0700107 }
108 }
109};
110
Elliott Hughesffe67362011-07-17 12:09:27 -0700111void Runtime::Abort(const char* file, int line) {
Elliott Hughes131aef82012-01-27 11:53:35 -0800112 // Ensure that we don't have multiple threads trying to abort at once,
113 // which would result in significantly worse diagnostics.
114 MutexLock mu(abort_lock_);
115
Elliott Hughesffe67362011-07-17 12:09:27 -0700116 // Get any pending output out of the way.
117 fflush(NULL);
118
119 // Many people have difficulty distinguish aborts from crashes,
120 // so be explicit.
Elliott Hughese0918552011-10-28 17:18:29 -0700121 AbortState state;
122 LOG(ERROR) << Dumpable<AbortState>(state);
Elliott Hughesffe67362011-07-17 12:09:27 -0700123
Elliott Hughesffe67362011-07-17 12:09:27 -0700124 // Perform any platform-specific pre-abort actions.
125 PlatformAbort(file, line);
126
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700127 // use abort hook if we have one
128 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
129 Runtime::Current()->abort_();
130 // notreached
131 }
132
Elliott Hughesffe67362011-07-17 12:09:27 -0700133 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -0700134 // receive SIGABRT. debuggerd dumps the stack trace of the main
135 // thread, whether or not that was the thread that failed. By
136 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -0700137 // fault in the current thread, and get a useful log from debuggerd.
138 // We can also trivially tell the difference between a VM crash and
139 // a deliberate abort by looking at the fault address.
140 *reinterpret_cast<char*>(0xdeadd00d) = 38;
141 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -0700142 // notreached
143}
144
Elliott Hughesbf86d042011-08-31 17:53:14 -0700145void Runtime::CallExitHook(jint status) {
146 if (exit_ != NULL) {
147 ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative);
148 exit_(status);
149 LOG(WARNING) << "Exit hook returned instead of exiting!";
150 }
151}
152
Brian Carlstrom8a436592011-08-15 21:27:23 -0700153// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
154// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
155// [gG] gigabytes.
156//
157// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700158// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
159// of 1024.
160//
161// The spec says the -Xmx and -Xms options must be multiples of 1024. It
162// doesn't say anything about -Xss.
163//
164// Returns 0 (a useless size) if "s" is malformed or specifies a low or
165// non-evenly-divisible value.
166//
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800167size_t ParseMemoryOption(const char* s, size_t div) {
Brian Carlstrom8a436592011-08-15 21:27:23 -0700168 // strtoul accepts a leading [+-], which we don't want,
169 // so make sure our string starts with a decimal digit.
170 if (isdigit(*s)) {
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800171 const char* s2;
172 size_t val = strtoul(s, (char**)&s2, 10);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700173 if (s2 != s) {
174 // s2 should be pointing just after the number.
175 // If this is the end of the string, the user
176 // has specified a number of bytes. Otherwise,
177 // there should be exactly one more character
178 // that specifies a multiplier.
179 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700180 // The remainder of the string is either a single multiplier
181 // character, or nothing to indicate that the value is in
182 // bytes.
183 char c = *s2++;
184 if (*s2 == '\0') {
185 size_t mul;
186 if (c == '\0') {
187 mul = 1;
188 } else if (c == 'k' || c == 'K') {
189 mul = KB;
190 } else if (c == 'm' || c == 'M') {
191 mul = MB;
192 } else if (c == 'g' || c == 'G') {
193 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700194 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700195 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700196 return 0;
197 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700198
199 if (val <= std::numeric_limits<size_t>::max() / mul) {
200 val *= mul;
201 } else {
202 // Clamp to a multiple of 1024.
203 val = std::numeric_limits<size_t>::max() & ~(1024-1);
204 }
205 } else {
206 // There's more than one character after the numeric part.
207 return 0;
208 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700209 }
210 // The man page says that a -Xm value must be a multiple of 1024.
211 if (val % div == 0) {
212 return val;
213 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700214 }
215 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700216 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700217}
218
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700219size_t ParseIntegerOrDie(const StringPiece& s) {
220 StringPiece::size_type colon = s.find(':');
221 if (colon == StringPiece::npos) {
222 LOG(FATAL) << "Missing integer: " << s;
223 }
224 const char* begin = &s.data()[colon + 1];
225 char* end;
226 size_t result = strtoul(begin, &end, 10);
227 if (begin == end || *end != '\0') {
228 LOG(FATAL) << "Failed to parse integer in: " << s;
229 }
230 return result;
231}
232
Elliott Hughes0af55432011-08-17 18:37:28 -0700233void LoadJniLibrary(JavaVMExt* vm, const char* name) {
Elliott Hughes6cc332e2012-01-20 22:59:20 -0800234 std::string mapped_name(StringPrintf(OS_SHARED_LIB_FORMAT_STR, name));
Elliott Hughes75770752011-08-24 17:52:38 -0700235 std::string reason;
236 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700237 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
238 << reason;
239 }
240}
241
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700242Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700243 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700244 bool compiler = false;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700245 const char* boot_class_path = getenv("BOOTCLASSPATH");
246 if (boot_class_path != NULL) {
Ian Rogers4f6b57a2012-01-17 16:23:27 -0800247 parsed->boot_class_path_ = boot_class_path;
248 } else {
249 parsed->boot_class_path_ = ".";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700250 }
251 const char* class_path = getenv("CLASSPATH");
252 if (class_path != NULL) {
Ian Rogers4f6b57a2012-01-17 16:23:27 -0800253 parsed->class_path_ = class_path;
254 } else {
255 parsed->class_path_ = ".";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700256 }
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700257#ifdef NDEBUG
Elliott Hughes5174fe62011-08-23 15:12:35 -0700258 // -Xcheck:jni is off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700259 parsed->check_jni_ = false;
260#else
261 // ...but on by default in debug builds.
262 parsed->check_jni_ = true;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700263#endif
Elliott Hughes85d15452011-09-16 17:33:01 -0700264
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700265 parsed->heap_initial_size_ = Heap::kInitialSize;
266 parsed->heap_maximum_size_ = Heap::kMaximumSize;
jeffhaoc1160702011-10-27 15:48:45 -0700267 parsed->heap_growth_limit_ = 0; // 0 means no growth limit
Brian Carlstromf734cf52011-08-17 16:28:14 -0700268 parsed->stack_size_ = Thread::kDefaultStackSize;
269
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700270 parsed->is_zygote_ = false;
271
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700272 parsed->jni_globals_max_ = 0;
Elliott Hughesfc861622011-10-17 17:57:47 -0700273 parsed->lock_profiling_threshold_ = 0;
274 parsed->hook_is_sensitive_thread_ = NULL;
275
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700276 parsed->hook_vfprintf_ = vfprintf;
277 parsed->hook_exit_ = exit;
278 parsed->hook_abort_ = abort;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700279
280 for (size_t i = 0; i < options.size(); ++i) {
281 const StringPiece& option = options[i].first;
Brian Carlstrom0796af02011-10-12 14:31:45 -0700282 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700283 LOG(INFO) << "option[" << i << "]=" << option;
284 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700285 if (option.starts_with("-Xbootclasspath:")) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700286 parsed->boot_class_path_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700287 } else if (option == "-classpath" || option == "-cp") {
288 // TODO: support -Djava.class.path
289 i++;
290 if (i == options.size()) {
291 // TODO: usage
292 LOG(FATAL) << "Missing required class path value for " << option;
293 return NULL;
294 }
295 const StringPiece& value = options[i].first;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700296 parsed->class_path_ = value.data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700297 } else if (option.starts_with("-Ximage:")) {
298 parsed->images_.push_back(option.substr(strlen("-Ximage:")).data());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700299 } else if (option.starts_with("-Xcheck:jni")) {
300 parsed->check_jni_ = true;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700301 } else if (option.starts_with("-Xrunjdwp:") || option.starts_with("-agentlib:jdwp=")) {
Elliott Hughes95572412011-12-13 18:14:20 -0800302 std::string tail(option.substr(option[1] == 'X' ? 10 : 15).ToString());
Elliott Hughes3bb81562011-10-21 18:52:59 -0700303 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
304 LOG(FATAL) << "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
305 << "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n";
306 return NULL;
307 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700308 } else if (option.starts_with("-Xms")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700309 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024);
310 if (size == 0) {
311 if (ignore_unrecognized) {
312 continue;
313 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700314 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700315 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700316 return NULL;
317 }
318 parsed->heap_initial_size_ = size;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700319 } else if (option.starts_with("-Xmx")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700320 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024);
321 if (size == 0) {
322 if (ignore_unrecognized) {
323 continue;
324 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700325 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700326 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700327 return NULL;
328 }
329 parsed->heap_maximum_size_ = size;
jeffhaoc1160702011-10-27 15:48:45 -0700330 } else if (option.starts_with("-XX:HeapGrowthLimit=")) {
331 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).data(), 1024);
332 if (size == 0) {
333 if (ignore_unrecognized) {
334 continue;
335 }
336 // TODO: usage
337 LOG(FATAL) << "Failed to parse " << option;
338 return NULL;
339 }
340 parsed->heap_growth_limit_ = size;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700341 } else if (option.starts_with("-Xss")) {
342 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1);
343 if (size == 0) {
344 if (ignore_unrecognized) {
345 continue;
346 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700347 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700348 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700349 return NULL;
350 }
351 parsed->stack_size_ = size;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700352 } else if (option.starts_with("-D")) {
353 parsed->properties_.push_back(option.substr(strlen("-D")).data());
Elliott Hughesa0957642011-09-02 14:27:33 -0700354 } else if (option.starts_with("-Xjnitrace:")) {
355 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:")).data();
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700356 } else if (option == "compiler") {
357 compiler = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700358 } else if (option == "-Xzygote") {
359 parsed->is_zygote_ = true;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700360 } else if (option.starts_with("-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700361 std::vector<std::string> verbose_options;
Elliott Hughes34023802011-08-30 12:06:17 -0700362 Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700363 for (size_t i = 0; i < verbose_options.size(); ++i) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800364 if (verbose_options[i] == "class") {
365 gLogVerbosity.class_linker = true;
366 } else if (verbose_options[i] == "compiler") {
367 gLogVerbosity.compiler = true;
368 } else if (verbose_options[i] == "heap") {
369 gLogVerbosity.heap = true;
370 } else if (verbose_options[i] == "gc") {
371 gLogVerbosity.gc = true;
372 } else if (verbose_options[i] == "jdwp") {
373 gLogVerbosity.jdwp = true;
374 } else if (verbose_options[i] == "jni") {
375 gLogVerbosity.jni = true;
376 } else if (verbose_options[i] == "monitor") {
377 gLogVerbosity.monitor = true;
378 } else if (verbose_options[i] == "startup") {
379 gLogVerbosity.startup = true;
380 } else if (verbose_options[i] == "third-party-jni") {
381 gLogVerbosity.third_party_jni = true;
382 } else if (verbose_options[i] == "threads") {
383 gLogVerbosity.threads = true;
384 } else {
385 LOG(WARNING) << "Ignoring unknown -verbose option: " << verbose_options[i];
386 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700387 }
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700388 } else if (option.starts_with("-Xjnigreflimit:")) {
389 parsed->jni_globals_max_ = ParseIntegerOrDie(option);
Elliott Hughesfc861622011-10-17 17:57:47 -0700390 } else if (option.starts_with("-Xlockprofthreshold:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700391 parsed->lock_profiling_threshold_ = ParseIntegerOrDie(option);
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700392 } else if (option.starts_with("-Xstacktracefile:")) {
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700393// always show stack traces in debug builds
394#ifdef NDEBUG
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700395 parsed->stack_trace_file_ = option.substr(strlen("-Xstacktracefile:")).data();
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700396#endif
Elliott Hughesfc861622011-10-17 17:57:47 -0700397 } else if (option == "sensitiveThread") {
398 parsed->hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(options[i].second);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700399 } else if (option == "vfprintf") {
400 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
401 } else if (option == "exit") {
402 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
403 } else if (option == "abort") {
404 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700405 } else if (option == "host-prefix") {
406 parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second);
Elliott Hughesb3e66df2012-01-12 14:49:18 -0800407 } else if (option == "-Xgenregmap" || option == "-Xgc:precise") {
408 // We silently ignore these for backwards compatibility.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700409 } else {
410 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700411 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700412 LOG(ERROR) << "Unrecognized option " << option;
413 // TODO: this should exit, but for now tolerate unknown options
414 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700415 }
416 }
417 }
418
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700419 if (!compiler && parsed->images_.empty()) {
Brian Carlstrom29e7ac72011-12-05 23:42:57 -0800420 parsed->images_.push_back("/system/framework/boot.art");
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700421 }
jeffhaoc1160702011-10-27 15:48:45 -0700422 if (parsed->heap_growth_limit_ == 0) {
423 parsed->heap_growth_limit_ = parsed->heap_maximum_size_;
424 }
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700425
Elliott Hughescac6cc72011-11-03 20:31:21 -0700426 LOG(INFO) << "Build type: "
427#ifndef NDEBUG
428 << "debug"
429#else
430 << "optimized"
431#endif
432 << "; CheckJNI: " << (parsed->check_jni_ ? "on" : "off");
Elliott Hughes85d15452011-09-16 17:33:01 -0700433
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700434 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700435}
436
437Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700438 // TODO: acquire a static mutex on Runtime to avoid racing.
439 if (Runtime::instance_ != NULL) {
440 return NULL;
441 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700442 instance_ = new Runtime;
443 if (!instance_->Init(options, ignore_unrecognized)) {
444 delete instance_;
445 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700446 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700447 return instance_;
448}
Elliott Hughes0af55432011-08-17 18:37:28 -0700449
Brian Carlstromaded5f72011-10-07 17:15:04 -0700450void CreateSystemClassLoader() {
451 if (ClassLoader::UseCompileTimeClassPath()) {
452 return;
453 }
454
455 Thread* self = Thread::Current();
456
457 // Must be in the kNative state for calling native methods.
458 CHECK_EQ(self->GetState(), Thread::kNative);
459
460 JNIEnv* env = self->GetJniEnv();
Brian Carlstromdf143242011-10-10 18:05:34 -0700461 ScopedLocalRef<jclass> ClassLoader_class(env, env->FindClass("java/lang/ClassLoader"));
462 CHECK(ClassLoader_class.get() != NULL);
463 jmethodID getSystemClassLoader = env->GetStaticMethodID(ClassLoader_class.get(),
464 "getSystemClassLoader",
465 "()Ljava/lang/ClassLoader;");
466 CHECK(getSystemClassLoader != NULL);
467 ScopedLocalRef<jobject> class_loader(env, env->CallStaticObjectMethod(ClassLoader_class.get(),
468 getSystemClassLoader));
469 CHECK(class_loader.get() != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700470
Brian Carlstromdf143242011-10-10 18:05:34 -0700471 Thread::Current()->SetClassLoaderOverride(Decode<ClassLoader*>(env, class_loader.get()));
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500472
473 ScopedLocalRef<jclass> Thread_class(env, env->FindClass("java/lang/Thread"));
474 CHECK(Thread_class.get() != NULL);
475 jfieldID contextClassLoader = env->GetFieldID(Thread_class.get(),
476 "contextClassLoader",
477 "Ljava/lang/ClassLoader;");
478 CHECK(contextClassLoader != NULL);
479 ScopedLocalRef<jobject> self_jobject(env, AddLocalReference<jobject>(env, self->GetPeer()));
480 env->SetObjectField(self_jobject.get(), contextClassLoader, class_loader.get());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700481}
482
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700483void Runtime::Start() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800484 VLOG(startup) << "Runtime::Start entering";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700485
486 CHECK(host_prefix_.empty()) << host_prefix_;
487
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700488 // Restore main thread state to kNative as expected by native code
489 Thread::Current()->SetState(Thread::kNative);
490
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700491 started_ = true;
492
Brian Carlstromae826982011-11-09 01:33:42 -0800493 // InitNativeMethods needs to be after started_ so that the classes
494 // it touches will have methods linked to the oat file if necessary.
495 InitNativeMethods();
496
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500497 Thread::FinishStartup();
498
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700499 if (!is_zygote_) {
500 DidForkFromZygote();
501 }
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700502
Elliott Hughes85d15452011-09-16 17:33:01 -0700503 StartDaemonThreads();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700504
Brian Carlstromaded5f72011-10-07 17:15:04 -0700505 CreateSystemClassLoader();
506
Elliott Hughes726079d2011-10-07 18:43:44 -0700507 Thread::Current()->GetJniEnv()->locals.AssertEmpty();
508
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800509 VLOG(startup) << "Runtime::Start exiting";
Elliott Hughes85d15452011-09-16 17:33:01 -0700510}
511
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700512void Runtime::DidForkFromZygote() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700513 is_zygote_ = false;
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700514
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700515 StartSignalCatcher();
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700516
517 // Start the JDWP thread. If the command-line debugger flags specified "suspend=y",
518 // this will pause the runtime, so we probably want this to come last.
519 Dbg::StartJdwp();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700520}
521
522void Runtime::StartSignalCatcher() {
523 if (!is_zygote_) {
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700524 signal_catcher_ = new SignalCatcher(stack_trace_file_);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700525 }
526}
527
Elliott Hughes85d15452011-09-16 17:33:01 -0700528void Runtime::StartDaemonThreads() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800529 VLOG(startup) << "Runtime::StartDaemonThreads entering";
Elliott Hughes85d15452011-09-16 17:33:01 -0700530
Elliott Hughes719b3232011-09-25 17:42:19 -0700531 Thread* self = Thread::Current();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700532
533 // Must be in the kNative state for calling native methods.
534 CHECK_EQ(self->GetState(), Thread::kNative);
535
Elliott Hughes719b3232011-09-25 17:42:19 -0700536 JNIEnv* env = self->GetJniEnv();
Elliott Hughes726079d2011-10-07 18:43:44 -0700537 ScopedLocalRef<jclass> c(env, env->FindClass("java/lang/Daemons"));
538 CHECK(c.get() != NULL);
539 jmethodID mid = env->GetStaticMethodID(c.get(), "start", "()V");
Elliott Hughes719b3232011-09-25 17:42:19 -0700540 CHECK(mid != NULL);
Elliott Hughes726079d2011-10-07 18:43:44 -0700541 env->CallStaticVoidMethod(c.get(), mid);
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700542
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800543 VLOG(startup) << "Runtime::StartDaemonThreads exiting";
Carl Shapiro61e019d2011-07-14 16:53:09 -0700544}
545
Elliott Hughes6b355752012-01-13 16:49:08 -0800546bool Runtime::IsShuttingDown() const {
547 return shutting_down_;
548}
549
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700550bool Runtime::IsStarted() const {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700551 return started_;
552}
553
Elliott Hughes0af55432011-08-17 18:37:28 -0700554bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700555 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700556
Elliott Hughes90a33692011-08-30 13:27:07 -0700557 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
558 if (options.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700559 LOG(ERROR) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700560 return false;
561 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800562 VLOG(startup) << "Runtime::Init -verbose:startup enabled";
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700563
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700564 SetJniGlobalsMax(options->jni_globals_max_);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800565 Monitor::Init(options->lock_profiling_threshold_, options->hook_is_sensitive_thread_);
Elliott Hughes32d6e1e2011-10-11 14:47:44 -0700566
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700567 host_prefix_ = options->host_prefix_;
568 boot_class_path_ = options->boot_class_path_;
569 class_path_ = options->class_path_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700570 properties_ = options->properties_;
571
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700572 is_zygote_ = options->is_zygote_;
573
Elliott Hughes0af55432011-08-17 18:37:28 -0700574 vfprintf_ = options->hook_vfprintf_;
575 exit_ = options->hook_exit_;
576 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700577
Elliott Hughesbe759c62011-09-08 19:38:21 -0700578 default_stack_size_ = options->stack_size_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700579 stack_trace_file_ = options->stack_trace_file_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700580
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700581 monitor_list_ = new MonitorList;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800582 thread_list_ = new ThreadList;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700583 intern_table_ = new InternTable;
584
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800585 Heap::Init(options->heap_initial_size_,
jeffhaoc1160702011-10-27 15:48:45 -0700586 options->heap_growth_limit_,
Ian Rogers30fab402012-01-23 15:43:46 -0800587 options->heap_maximum_size_,
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700588 options->images_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700589
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700590 BlockSignals();
591
Elliott Hughesa0957642011-09-02 14:27:33 -0700592 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700593
Elliott Hughesbe759c62011-09-08 19:38:21 -0700594 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700595
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700596 // ClassLinker needs an attached thread, but we can't fully attach a thread
597 // without creating objects.
598 Thread::Attach(this, "main", false);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700599
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700600 // Set us to runnable so tools using a runtime can allocate and GC by default
601 Thread::Current()->SetState(Thread::kRunnable);
602
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700603 CHECK_GE(Heap::GetSpaces().size(), 1U);
604 class_linker_ = ((Heap::GetSpaces()[0]->IsImageSpace())
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800605 ? ClassLinker::Create(intern_table_)
606 : ClassLinker::Create(options->boot_class_path_, intern_table_));
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700607
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800608 VLOG(startup) << "Runtime::Init exiting";
Carl Shapiro1fb86202011-06-27 17:43:13 -0700609 return true;
610}
611
Elliott Hughes038a8062011-09-18 14:12:41 -0700612void Runtime::InitNativeMethods() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800613 VLOG(startup) << "Runtime::InitNativeMethods entering";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700614 Thread* self = Thread::Current();
615 JNIEnv* env = self->GetJniEnv();
616
Elliott Hughes418d20f2011-09-22 14:00:39 -0700617 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Brian Carlstromaded5f72011-10-07 17:15:04 -0700618 CHECK_EQ(self->GetState(), Thread::kNative);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700619
Elliott Hughes418d20f2011-09-22 14:00:39 -0700620 // First set up JniConstants, which is used by both the runtime's built-in native
621 // methods and libcore.
Elliott Hughesfea966e2011-09-22 10:26:20 -0700622 JniConstants::init(env);
623
Elliott Hughes418d20f2011-09-22 14:00:39 -0700624 // Then set up the native methods provided by the runtime itself.
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700625 RegisterRuntimeNativeMethods(env);
626
Elliott Hughes418d20f2011-09-22 14:00:39 -0700627 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
628 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
629 // the library that implements System.loadLibrary!
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700630 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800631 VLOG(startup) << "Runtime::InitNativeMethods exiting";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700632}
633
634void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
635#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700636 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700637 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700638 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700639 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700640 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700641 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700642 REGISTER(register_java_lang_Object);
643 REGISTER(register_java_lang_Runtime);
644 REGISTER(register_java_lang_String);
645 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700646 REGISTER(register_java_lang_Thread);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700647 REGISTER(register_java_lang_Throwable);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700648 REGISTER(register_java_lang_VMClassLoader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700649 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700650 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700651 REGISTER(register_java_lang_reflect_Field);
652 REGISTER(register_java_lang_reflect_Method);
Jesse Wilson95caa792011-10-12 18:14:17 -0400653 REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700654 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700655 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700656 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700657 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700658#undef REGISTER
659}
660
Elliott Hughes8daa0922011-09-11 13:46:25 -0700661void Runtime::Dump(std::ostream& os) {
Elliott Hughese27955c2011-08-26 15:21:24 -0700662 // TODO: dump other runtime statistics?
Elliott Hughescac6cc72011-11-03 20:31:21 -0700663 GetClassLinker()->DumpForSigQuit(os);
664 GetInternTable()->DumpForSigQuit(os);
Elliott Hughes42ee1422011-09-06 12:33:32 -0700665 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700666
667 thread_list_->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700668}
669
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800670void Runtime::DumpLockHolders(std::ostream& os) {
671 pid_t heap_lock_owner = Heap::GetLockOwner();
672 pid_t thread_list_lock_owner = GetThreadList()->GetLockOwner();
673 pid_t classes_lock_owner = GetClassLinker()->GetClassesLockOwner();
674 pid_t dex_lock_owner = GetClassLinker()->GetDexLockOwner();
675 if ((heap_lock_owner | thread_list_lock_owner | classes_lock_owner | dex_lock_owner) != 0) {
676 os << "Heap lock owner tid: " << heap_lock_owner << "\n"
677 << "ThreadList lock owner tid: " << thread_list_lock_owner << "\n"
678 << "ClassLinker classes lock owner tid: " << classes_lock_owner << "\n"
679 << "ClassLinker dex lock owner tid: " << dex_lock_owner << "\n";
680 }
681}
682
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700683void Runtime::SetStatsEnabled(bool new_state) {
684 if (new_state == true) {
685 GetStats()->Clear(~0);
686 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
687 Thread::Current()->GetStats()->Clear(~0);
688 }
689 stats_enabled_ = new_state;
690}
691
692void Runtime::ResetStats(int kinds) {
693 GetStats()->Clear(kinds & 0xffff);
694 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
695 Thread::Current()->GetStats()->Clear(kinds >> 16);
696}
697
698RuntimeStats* Runtime::GetStats() {
699 return &stats_;
700}
701
702int32_t Runtime::GetStat(int kind) {
703 RuntimeStats* stats;
704 if (kind < (1<<16)) {
705 stats = GetStats();
706 } else {
707 stats = Thread::Current()->GetStats();
708 kind >>= 16;
709 }
710 switch (kind) {
711 case KIND_ALLOCATED_OBJECTS:
712 return stats->allocated_objects;
713 case KIND_ALLOCATED_BYTES:
714 return stats->allocated_bytes;
715 case KIND_FREED_OBJECTS:
716 return stats->freed_objects;
717 case KIND_FREED_BYTES:
718 return stats->freed_bytes;
719 case KIND_GC_INVOCATIONS:
720 return stats->gc_for_alloc_count;
721 case KIND_CLASS_INIT_COUNT:
722 return stats->class_init_count;
723 case KIND_CLASS_INIT_TIME:
724 // Convert ns to us, reduce to 32 bits.
725 return (int) (stats->class_init_time_ns / 1000);
726 case KIND_EXT_ALLOCATED_OBJECTS:
727 case KIND_EXT_ALLOCATED_BYTES:
728 case KIND_EXT_FREED_OBJECTS:
729 case KIND_EXT_FREED_BYTES:
730 return 0; // backward compatibility
731 default:
732 CHECK(false);
733 return -1; // unreachable
734 }
735}
736
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700737void Runtime::BlockSignals() {
738 sigset_t sigset;
739 if (sigemptyset(&sigset) == -1) {
740 PLOG(FATAL) << "sigemptyset failed";
741 }
742 if (sigaddset(&sigset, SIGPIPE) == -1) {
743 PLOG(ERROR) << "sigaddset SIGPIPE failed";
744 }
745 // SIGQUIT is used to dump the runtime's state (including stack traces).
746 if (sigaddset(&sigset, SIGQUIT) == -1) {
747 PLOG(ERROR) << "sigaddset SIGQUIT failed";
748 }
749 // SIGUSR1 is used to initiate a heap dump.
750 if (sigaddset(&sigset, SIGUSR1) == -1) {
751 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
752 }
753 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
754}
755
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700756void Runtime::AttachCurrentThread(const char* name, bool as_daemon) {
757 Thread::Attach(instance_, name, as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700758}
759
Elliott Hughesd92bec42011-09-02 17:04:36 -0700760void Runtime::DetachCurrentThread() {
Elliott Hughes038a8062011-09-18 14:12:41 -0700761 // TODO: check we're not calling DetachCurrentThread from a call stack that
762 // includes managed frames. (It's only valid if the stack is all-native.)
Elliott Hughes02b48d12011-09-07 17:15:51 -0700763 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700764}
765
Brian Carlstrome24fa612011-09-29 00:53:55 -0700766void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700767 Dbg::VisitRoots(visitor, arg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700768 class_linker_->VisitRoots(visitor, arg);
769 intern_table_->VisitRoots(visitor, arg);
770 java_vm_->VisitRoots(visitor, arg);
771 thread_list_->VisitRoots(visitor, arg);
772 visitor(jni_stub_array_, arg);
773 visitor(abstract_method_error_stub_array_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700774 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
775 visitor(resolution_stub_array_[i], arg);
776 }
777 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
778 visitor(callee_save_method_[i], arg);
779 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700780}
781
Ian Rogers169c9a72011-11-13 20:13:17 -0800782bool Runtime::HasJniDlsymLookupStub() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700783 return jni_stub_array_ != NULL;
784}
785
Ian Rogers169c9a72011-11-13 20:13:17 -0800786ByteArray* Runtime::GetJniDlsymLookupStub() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700787 CHECK(jni_stub_array_ != NULL);
788 return jni_stub_array_;
789}
790
Ian Rogers169c9a72011-11-13 20:13:17 -0800791void Runtime::SetJniDlsymLookupStub(ByteArray* jni_stub_array) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700792 CHECK(jni_stub_array != NULL) << " jni_stub_array=" << jni_stub_array;
793 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array)
794 << "jni_stub_array_=" << jni_stub_array_ << " jni_stub_array=" << jni_stub_array;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700795 jni_stub_array_ = jni_stub_array;
796}
797
798bool Runtime::HasAbstractMethodErrorStubArray() const {
799 return abstract_method_error_stub_array_ != NULL;
800}
801
802ByteArray* Runtime::GetAbstractMethodErrorStubArray() const {
803 CHECK(abstract_method_error_stub_array_ != NULL);
804 return abstract_method_error_stub_array_;
805}
806
807void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) {
808 CHECK(abstract_method_error_stub_array != NULL);
809 CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array);
810 abstract_method_error_stub_array_ = abstract_method_error_stub_array;
811}
812
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700813
814Runtime::TrampolineType Runtime::GetTrampolineType(Method* method) {
815 if (method == NULL) {
816 return Runtime::kUnknownMethod;
817 } else if (method->IsStatic()) {
818 return Runtime::kStaticMethod;
819 } else {
820 return Runtime::kInstanceMethod;
821 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700822}
823
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700824bool Runtime::HasResolutionStubArray(TrampolineType type) const {
825 return resolution_stub_array_[type] != NULL;
Ian Rogersad25ac52011-10-04 19:13:33 -0700826}
827
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700828ByteArray* Runtime::GetResolutionStubArray(TrampolineType type) const {
829 CHECK(HasResolutionStubArray(type));
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700830 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastTrampolineMethodType));
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700831 return resolution_stub_array_[type];
832}
833
834void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700835 CHECK(resolution_stub_array != NULL);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700836 CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array);
837 resolution_stub_array_[type] = resolution_stub_array;
Ian Rogersad25ac52011-10-04 19:13:33 -0700838}
839
Brian Carlstromae826982011-11-09 01:33:42 -0800840Method* Runtime::CreateCalleeSaveMethod(InstructionSet instruction_set, CalleeSaveType type) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700841 Class* method_class = Method::GetMethodClass();
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700842 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
Ian Rogersff1ed472011-09-20 13:46:24 -0700843 method->SetDeclaringClass(method_class);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800844 // TODO: use a special method for callee saves
845 method->SetMethodIndex(DexFile::kDexNoIndex16);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700846 method->SetCode(NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800847 if ((instruction_set == kThumb2) || (instruction_set == kArm)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700848 uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6) | (1 << art::arm::R7) |
849 (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11);
850 uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3);
851 uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9);
852 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills :0) |
853 (type == kSaveAll ? all_spills :0) | (1 << art::arm::LR);
854 uint32_t fp_all_spills = (1 << art::arm::S0) | (1 << art::arm::S1) | (1 << art::arm::S2) |
855 (1 << art::arm::S3) | (1 << art::arm::S4) | (1 << art::arm::S5) |
856 (1 << art::arm::S6) | (1 << art::arm::S7) | (1 << art::arm::S8) |
857 (1 << art::arm::S9) | (1 << art::arm::S10) | (1 << art::arm::S11) |
858 (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) |
859 (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) |
860 (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) |
861 (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) |
862 (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) |
863 (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
864 (1 << art::arm::S30) | (1 << art::arm::S31);
865 uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
866 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
867 __builtin_popcount(fp_spills) /* fprs */ +
868 1 /* Method* */) * kPointerSize, kStackAlignment);
Ian Rogers15fdb8c2011-09-25 15:45:07 -0700869 method->SetFrameSizeInBytes(frame_size);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700870 method->SetCoreSpillMask(core_spills);
871 method->SetFpSpillMask(fp_spills);
Brian Carlstromae826982011-11-09 01:33:42 -0800872 } else if (instruction_set == kX86) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700873 method->SetFrameSizeInBytes(32);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700874 method->SetCoreSpillMask((1 << art::x86::EBX) | (1 << art::x86::EBP) | (1 << art::x86::ESI) |
Ian Rogersff1ed472011-09-20 13:46:24 -0700875 (1 << art::x86::EDI));
876 method->SetFpSpillMask(0);
877 } else {
878 UNIMPLEMENTED(FATAL);
879 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700880 return method.get();
Ian Rogersff1ed472011-09-20 13:46:24 -0700881}
882
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700883bool Runtime::HasCalleeSaveMethod(CalleeSaveType type) const {
884 return callee_save_method_[type] != NULL;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700885}
886
Brian Carlstrome24fa612011-09-29 00:53:55 -0700887// Returns a special method that describes all callee saves being spilled to the stack.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700888Method* Runtime::GetCalleeSaveMethod(CalleeSaveType type) const {
889 CHECK(HasCalleeSaveMethod(type));
890 return callee_save_method_[type];
Brian Carlstrome24fa612011-09-29 00:53:55 -0700891}
892
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700893void Runtime::SetCalleeSaveMethod(Method* method, CalleeSaveType type) {
894 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
895 callee_save_method_[type] = method;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700896}
897
jeffhao2692b572011-12-16 15:42:28 -0800898void Runtime::EnableMethodTracing(Trace* tracer) {
899 CHECK(!IsMethodTracingActive());
900 tracer_ = tracer;
901}
902
903void Runtime::DisableMethodTracing() {
904 CHECK(IsMethodTracingActive());
905 delete tracer_;
906 tracer_ = NULL;
907}
908
909bool Runtime::IsMethodTracingActive() const {
910 return (tracer_ != NULL);
911}
912
913Trace* Runtime::GetTracer() const {
914 CHECK(IsMethodTracingActive());
915 return tracer_;
916}
917
Carl Shapiro1fb86202011-06-27 17:43:13 -0700918} // namespace art