blob: 114c538aed73052dcdbb0fd2b9296905c83db5f3 [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 Hughesdcc24742011-09-07 14:02:44 -070036Runtime::Runtime()
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080037 : is_zygote_(false),
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -070038 default_stack_size_(Thread::kDefaultStackSize),
Elliott Hughesc33a32b2011-10-11 18:18:07 -070039 monitor_list_(NULL),
Elliott Hughesdcc24742011-09-07 14:02:44 -070040 thread_list_(NULL),
41 intern_table_(NULL),
42 class_linker_(NULL),
43 signal_catcher_(NULL),
44 java_vm_(NULL),
Brian Carlstrom16192862011-09-12 17:50:06 -070045 jni_stub_array_(NULL),
Brian Carlstrome24fa612011-09-29 00:53:55 -070046 abstract_method_error_stub_array_(NULL),
Elliott Hughes6b355752012-01-13 16:49:08 -080047 shutting_down_(false),
Elliott Hughesdcc24742011-09-07 14:02:44 -070048 started_(false),
49 vfprintf_(NULL),
50 exit_(NULL),
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070051 abort_(NULL),
jeffhao2692b572011-12-16 15:42:28 -080052 stats_enabled_(false),
53 tracer_(NULL) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -070054 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
55 resolution_stub_array_[i] = NULL;
56 }
57 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
58 callee_save_method_[i] = NULL;
59 }
Elliott Hughesdcc24742011-09-07 14:02:44 -070060}
61
Carl Shapiro61e019d2011-07-14 16:53:09 -070062Runtime::~Runtime() {
Elliott Hughes6b355752012-01-13 16:49:08 -080063 shutting_down_ = true;
64
Elliott Hughesd1cc8362011-10-24 16:58:50 -070065 Dbg::StopJdwp();
66
Elliott Hughes5fe594f2011-09-08 12:33:17 -070067 // Make sure our internal threads are dead before we start tearing down things they're using.
68 delete signal_catcher_;
Elliott Hughes038a8062011-09-18 14:12:41 -070069 // TODO: GC thread.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070070
Elliott Hughes038a8062011-09-18 14:12:41 -070071 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
Elliott Hughes93e74e82011-09-13 11:07:03 -070072 delete thread_list_;
Elliott Hughesc33a32b2011-10-11 18:18:07 -070073 delete monitor_list_;
Elliott Hughes93e74e82011-09-13 11:07:03 -070074
Carl Shapiro61e019d2011-07-14 16:53:09 -070075 delete class_linker_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070076 Heap::Destroy();
Elliott Hughes4d6850c2012-01-18 15:55:06 -080077 verifier::DexVerifier::DeleteGcMaps();
Elliott Hughescf4c6c42011-09-01 15:16:42 -070078 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070079 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -070080 Thread::Shutdown();
Carl Shapiro4acf4642011-07-26 18:54:13 -070081 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070082 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -070083 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -070084}
85
Elliott Hughescac6cc72011-11-03 20:31:21 -070086static bool gAborting = false;
87
Elliott Hughese0918552011-10-28 17:18:29 -070088struct AbortState {
89 void Dump(std::ostream& os) {
Elliott Hughescac6cc72011-11-03 20:31:21 -070090 if (gAborting) {
91 os << "Runtime aborting --- recursively, so no thread-specific detail!\n";
92 return;
93 }
94 gAborting = true;
Elliott Hughese0918552011-10-28 17:18:29 -070095 os << "Runtime aborting...\n";
96 Thread* self = Thread::Current();
97 if (self == NULL) {
98 os << "(Aborting thread was not attached to runtime!)\n";
99 } else {
100 self->Dump(os, true);
101 }
102 }
103};
104
Elliott Hughesffe67362011-07-17 12:09:27 -0700105void Runtime::Abort(const char* file, int line) {
106 // Get any pending output out of the way.
107 fflush(NULL);
108
109 // Many people have difficulty distinguish aborts from crashes,
110 // so be explicit.
Elliott Hughese0918552011-10-28 17:18:29 -0700111 AbortState state;
112 LOG(ERROR) << Dumpable<AbortState>(state);
Elliott Hughesffe67362011-07-17 12:09:27 -0700113
Elliott Hughesffe67362011-07-17 12:09:27 -0700114 // Perform any platform-specific pre-abort actions.
115 PlatformAbort(file, line);
116
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700117 // use abort hook if we have one
118 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
119 Runtime::Current()->abort_();
120 // notreached
121 }
122
Elliott Hughesffe67362011-07-17 12:09:27 -0700123 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -0700124 // receive SIGABRT. debuggerd dumps the stack trace of the main
125 // thread, whether or not that was the thread that failed. By
126 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -0700127 // fault in the current thread, and get a useful log from debuggerd.
128 // We can also trivially tell the difference between a VM crash and
129 // a deliberate abort by looking at the fault address.
130 *reinterpret_cast<char*>(0xdeadd00d) = 38;
131 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -0700132 // notreached
133}
134
Elliott Hughesbf86d042011-08-31 17:53:14 -0700135void Runtime::CallExitHook(jint status) {
136 if (exit_ != NULL) {
137 ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative);
138 exit_(status);
139 LOG(WARNING) << "Exit hook returned instead of exiting!";
140 }
141}
142
Brian Carlstrom8a436592011-08-15 21:27:23 -0700143// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
144// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
145// [gG] gigabytes.
146//
147// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700148// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
149// of 1024.
150//
151// The spec says the -Xmx and -Xms options must be multiples of 1024. It
152// doesn't say anything about -Xss.
153//
154// Returns 0 (a useless size) if "s" is malformed or specifies a low or
155// non-evenly-divisible value.
156//
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800157size_t ParseMemoryOption(const char* s, size_t div) {
Brian Carlstrom8a436592011-08-15 21:27:23 -0700158 // strtoul accepts a leading [+-], which we don't want,
159 // so make sure our string starts with a decimal digit.
160 if (isdigit(*s)) {
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800161 const char* s2;
162 size_t val = strtoul(s, (char**)&s2, 10);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700163 if (s2 != s) {
164 // s2 should be pointing just after the number.
165 // If this is the end of the string, the user
166 // has specified a number of bytes. Otherwise,
167 // there should be exactly one more character
168 // that specifies a multiplier.
169 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700170 // The remainder of the string is either a single multiplier
171 // character, or nothing to indicate that the value is in
172 // bytes.
173 char c = *s2++;
174 if (*s2 == '\0') {
175 size_t mul;
176 if (c == '\0') {
177 mul = 1;
178 } else if (c == 'k' || c == 'K') {
179 mul = KB;
180 } else if (c == 'm' || c == 'M') {
181 mul = MB;
182 } else if (c == 'g' || c == 'G') {
183 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700184 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700185 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700186 return 0;
187 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700188
189 if (val <= std::numeric_limits<size_t>::max() / mul) {
190 val *= mul;
191 } else {
192 // Clamp to a multiple of 1024.
193 val = std::numeric_limits<size_t>::max() & ~(1024-1);
194 }
195 } else {
196 // There's more than one character after the numeric part.
197 return 0;
198 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700199 }
200 // The man page says that a -Xm value must be a multiple of 1024.
201 if (val % div == 0) {
202 return val;
203 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700204 }
205 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700206 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700207}
208
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700209size_t ParseIntegerOrDie(const StringPiece& s) {
210 StringPiece::size_type colon = s.find(':');
211 if (colon == StringPiece::npos) {
212 LOG(FATAL) << "Missing integer: " << s;
213 }
214 const char* begin = &s.data()[colon + 1];
215 char* end;
216 size_t result = strtoul(begin, &end, 10);
217 if (begin == end || *end != '\0') {
218 LOG(FATAL) << "Failed to parse integer in: " << s;
219 }
220 return result;
221}
222
Elliott Hughes0af55432011-08-17 18:37:28 -0700223void LoadJniLibrary(JavaVMExt* vm, const char* name) {
Elliott Hughes6cc332e2012-01-20 22:59:20 -0800224 std::string mapped_name(StringPrintf(OS_SHARED_LIB_FORMAT_STR, name));
Elliott Hughes75770752011-08-24 17:52:38 -0700225 std::string reason;
226 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700227 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
228 << reason;
229 }
230}
231
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700232Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700233 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700234 bool compiler = false;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700235 const char* boot_class_path = getenv("BOOTCLASSPATH");
236 if (boot_class_path != NULL) {
Ian Rogers4f6b57a2012-01-17 16:23:27 -0800237 parsed->boot_class_path_ = boot_class_path;
238 } else {
239 parsed->boot_class_path_ = ".";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700240 }
241 const char* class_path = getenv("CLASSPATH");
242 if (class_path != NULL) {
Ian Rogers4f6b57a2012-01-17 16:23:27 -0800243 parsed->class_path_ = class_path;
244 } else {
245 parsed->class_path_ = ".";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700246 }
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700247#ifdef NDEBUG
Elliott Hughes5174fe62011-08-23 15:12:35 -0700248 // -Xcheck:jni is off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700249 parsed->check_jni_ = false;
250#else
251 // ...but on by default in debug builds.
252 parsed->check_jni_ = true;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700253#endif
Elliott Hughes85d15452011-09-16 17:33:01 -0700254
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700255 parsed->heap_initial_size_ = Heap::kInitialSize;
256 parsed->heap_maximum_size_ = Heap::kMaximumSize;
jeffhaoc1160702011-10-27 15:48:45 -0700257 parsed->heap_growth_limit_ = 0; // 0 means no growth limit
Brian Carlstromf734cf52011-08-17 16:28:14 -0700258 parsed->stack_size_ = Thread::kDefaultStackSize;
259
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700260 parsed->is_zygote_ = false;
261
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700262 parsed->jni_globals_max_ = 0;
Elliott Hughesfc861622011-10-17 17:57:47 -0700263 parsed->lock_profiling_threshold_ = 0;
264 parsed->hook_is_sensitive_thread_ = NULL;
265
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700266 parsed->hook_vfprintf_ = vfprintf;
267 parsed->hook_exit_ = exit;
268 parsed->hook_abort_ = abort;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700269
270 for (size_t i = 0; i < options.size(); ++i) {
271 const StringPiece& option = options[i].first;
Brian Carlstrom0796af02011-10-12 14:31:45 -0700272 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700273 LOG(INFO) << "option[" << i << "]=" << option;
274 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700275 if (option.starts_with("-Xbootclasspath:")) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700276 parsed->boot_class_path_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700277 } else if (option == "-classpath" || option == "-cp") {
278 // TODO: support -Djava.class.path
279 i++;
280 if (i == options.size()) {
281 // TODO: usage
282 LOG(FATAL) << "Missing required class path value for " << option;
283 return NULL;
284 }
285 const StringPiece& value = options[i].first;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700286 parsed->class_path_ = value.data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700287 } else if (option.starts_with("-Ximage:")) {
288 parsed->images_.push_back(option.substr(strlen("-Ximage:")).data());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700289 } else if (option.starts_with("-Xcheck:jni")) {
290 parsed->check_jni_ = true;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700291 } else if (option.starts_with("-Xrunjdwp:") || option.starts_with("-agentlib:jdwp=")) {
Elliott Hughes95572412011-12-13 18:14:20 -0800292 std::string tail(option.substr(option[1] == 'X' ? 10 : 15).ToString());
Elliott Hughes3bb81562011-10-21 18:52:59 -0700293 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
294 LOG(FATAL) << "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
295 << "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n";
296 return NULL;
297 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700298 } else if (option.starts_with("-Xms")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700299 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024);
300 if (size == 0) {
301 if (ignore_unrecognized) {
302 continue;
303 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700304 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700305 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700306 return NULL;
307 }
308 parsed->heap_initial_size_ = size;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700309 } else if (option.starts_with("-Xmx")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700310 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024);
311 if (size == 0) {
312 if (ignore_unrecognized) {
313 continue;
314 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700315 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700316 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700317 return NULL;
318 }
319 parsed->heap_maximum_size_ = size;
jeffhaoc1160702011-10-27 15:48:45 -0700320 } else if (option.starts_with("-XX:HeapGrowthLimit=")) {
321 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).data(), 1024);
322 if (size == 0) {
323 if (ignore_unrecognized) {
324 continue;
325 }
326 // TODO: usage
327 LOG(FATAL) << "Failed to parse " << option;
328 return NULL;
329 }
330 parsed->heap_growth_limit_ = size;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700331 } else if (option.starts_with("-Xss")) {
332 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1);
333 if (size == 0) {
334 if (ignore_unrecognized) {
335 continue;
336 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700337 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700338 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700339 return NULL;
340 }
341 parsed->stack_size_ = size;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700342 } else if (option.starts_with("-D")) {
343 parsed->properties_.push_back(option.substr(strlen("-D")).data());
Elliott Hughesa0957642011-09-02 14:27:33 -0700344 } else if (option.starts_with("-Xjnitrace:")) {
345 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:")).data();
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700346 } else if (option == "compiler") {
347 compiler = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700348 } else if (option == "-Xzygote") {
349 parsed->is_zygote_ = true;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700350 } else if (option.starts_with("-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700351 std::vector<std::string> verbose_options;
Elliott Hughes34023802011-08-30 12:06:17 -0700352 Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700353 for (size_t i = 0; i < verbose_options.size(); ++i) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800354 if (verbose_options[i] == "class") {
355 gLogVerbosity.class_linker = true;
356 } else if (verbose_options[i] == "compiler") {
357 gLogVerbosity.compiler = true;
358 } else if (verbose_options[i] == "heap") {
359 gLogVerbosity.heap = true;
360 } else if (verbose_options[i] == "gc") {
361 gLogVerbosity.gc = true;
362 } else if (verbose_options[i] == "jdwp") {
363 gLogVerbosity.jdwp = true;
364 } else if (verbose_options[i] == "jni") {
365 gLogVerbosity.jni = true;
366 } else if (verbose_options[i] == "monitor") {
367 gLogVerbosity.monitor = true;
368 } else if (verbose_options[i] == "startup") {
369 gLogVerbosity.startup = true;
370 } else if (verbose_options[i] == "third-party-jni") {
371 gLogVerbosity.third_party_jni = true;
372 } else if (verbose_options[i] == "threads") {
373 gLogVerbosity.threads = true;
374 } else {
375 LOG(WARNING) << "Ignoring unknown -verbose option: " << verbose_options[i];
376 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700377 }
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700378 } else if (option.starts_with("-Xjnigreflimit:")) {
379 parsed->jni_globals_max_ = ParseIntegerOrDie(option);
Elliott Hughesfc861622011-10-17 17:57:47 -0700380 } else if (option.starts_with("-Xlockprofthreshold:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700381 parsed->lock_profiling_threshold_ = ParseIntegerOrDie(option);
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700382 } else if (option.starts_with("-Xstacktracefile:")) {
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700383// always show stack traces in debug builds
384#ifdef NDEBUG
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700385 parsed->stack_trace_file_ = option.substr(strlen("-Xstacktracefile:")).data();
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700386#endif
Elliott Hughesfc861622011-10-17 17:57:47 -0700387 } else if (option == "sensitiveThread") {
388 parsed->hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(options[i].second);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700389 } else if (option == "vfprintf") {
390 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
391 } else if (option == "exit") {
392 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
393 } else if (option == "abort") {
394 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700395 } else if (option == "host-prefix") {
396 parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second);
Elliott Hughesb3e66df2012-01-12 14:49:18 -0800397 } else if (option == "-Xgenregmap" || option == "-Xgc:precise") {
398 // We silently ignore these for backwards compatibility.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700399 } else {
400 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700401 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700402 LOG(ERROR) << "Unrecognized option " << option;
403 // TODO: this should exit, but for now tolerate unknown options
404 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700405 }
406 }
407 }
408
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700409 if (!compiler && parsed->images_.empty()) {
Brian Carlstrom29e7ac72011-12-05 23:42:57 -0800410 parsed->images_.push_back("/system/framework/boot.art");
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700411 }
jeffhaoc1160702011-10-27 15:48:45 -0700412 if (parsed->heap_growth_limit_ == 0) {
413 parsed->heap_growth_limit_ = parsed->heap_maximum_size_;
414 }
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700415
Elliott Hughescac6cc72011-11-03 20:31:21 -0700416 LOG(INFO) << "Build type: "
417#ifndef NDEBUG
418 << "debug"
419#else
420 << "optimized"
421#endif
422 << "; CheckJNI: " << (parsed->check_jni_ ? "on" : "off");
Elliott Hughes85d15452011-09-16 17:33:01 -0700423
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700424 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700425}
426
427Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700428 // TODO: acquire a static mutex on Runtime to avoid racing.
429 if (Runtime::instance_ != NULL) {
430 return NULL;
431 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700432 instance_ = new Runtime;
433 if (!instance_->Init(options, ignore_unrecognized)) {
434 delete instance_;
435 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700436 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700437 return instance_;
438}
Elliott Hughes0af55432011-08-17 18:37:28 -0700439
Brian Carlstromaded5f72011-10-07 17:15:04 -0700440void CreateSystemClassLoader() {
441 if (ClassLoader::UseCompileTimeClassPath()) {
442 return;
443 }
444
445 Thread* self = Thread::Current();
446
447 // Must be in the kNative state for calling native methods.
448 CHECK_EQ(self->GetState(), Thread::kNative);
449
450 JNIEnv* env = self->GetJniEnv();
Brian Carlstromdf143242011-10-10 18:05:34 -0700451 ScopedLocalRef<jclass> ClassLoader_class(env, env->FindClass("java/lang/ClassLoader"));
452 CHECK(ClassLoader_class.get() != NULL);
453 jmethodID getSystemClassLoader = env->GetStaticMethodID(ClassLoader_class.get(),
454 "getSystemClassLoader",
455 "()Ljava/lang/ClassLoader;");
456 CHECK(getSystemClassLoader != NULL);
457 ScopedLocalRef<jobject> class_loader(env, env->CallStaticObjectMethod(ClassLoader_class.get(),
458 getSystemClassLoader));
459 CHECK(class_loader.get() != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700460
Brian Carlstromdf143242011-10-10 18:05:34 -0700461 Thread::Current()->SetClassLoaderOverride(Decode<ClassLoader*>(env, class_loader.get()));
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500462
463 ScopedLocalRef<jclass> Thread_class(env, env->FindClass("java/lang/Thread"));
464 CHECK(Thread_class.get() != NULL);
465 jfieldID contextClassLoader = env->GetFieldID(Thread_class.get(),
466 "contextClassLoader",
467 "Ljava/lang/ClassLoader;");
468 CHECK(contextClassLoader != NULL);
469 ScopedLocalRef<jobject> self_jobject(env, AddLocalReference<jobject>(env, self->GetPeer()));
470 env->SetObjectField(self_jobject.get(), contextClassLoader, class_loader.get());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700471}
472
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700473void Runtime::Start() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800474 VLOG(startup) << "Runtime::Start entering";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700475
476 CHECK(host_prefix_.empty()) << host_prefix_;
477
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700478 // Restore main thread state to kNative as expected by native code
479 Thread::Current()->SetState(Thread::kNative);
480
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700481 started_ = true;
482
Brian Carlstromae826982011-11-09 01:33:42 -0800483 // InitNativeMethods needs to be after started_ so that the classes
484 // it touches will have methods linked to the oat file if necessary.
485 InitNativeMethods();
486
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500487 Thread::FinishStartup();
488
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700489 if (!is_zygote_) {
490 DidForkFromZygote();
491 }
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700492
Elliott Hughes85d15452011-09-16 17:33:01 -0700493 StartDaemonThreads();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700494
Brian Carlstromaded5f72011-10-07 17:15:04 -0700495 CreateSystemClassLoader();
496
Elliott Hughes726079d2011-10-07 18:43:44 -0700497 Thread::Current()->GetJniEnv()->locals.AssertEmpty();
498
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800499 VLOG(startup) << "Runtime::Start exiting";
Elliott Hughes85d15452011-09-16 17:33:01 -0700500}
501
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700502void Runtime::DidForkFromZygote() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700503 is_zygote_ = false;
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700504
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700505 StartSignalCatcher();
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700506
507 // Start the JDWP thread. If the command-line debugger flags specified "suspend=y",
508 // this will pause the runtime, so we probably want this to come last.
509 Dbg::StartJdwp();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700510}
511
512void Runtime::StartSignalCatcher() {
513 if (!is_zygote_) {
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700514 signal_catcher_ = new SignalCatcher(stack_trace_file_);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700515 }
516}
517
Elliott Hughes85d15452011-09-16 17:33:01 -0700518void Runtime::StartDaemonThreads() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800519 VLOG(startup) << "Runtime::StartDaemonThreads entering";
Elliott Hughes85d15452011-09-16 17:33:01 -0700520
Elliott Hughes719b3232011-09-25 17:42:19 -0700521 Thread* self = Thread::Current();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700522
523 // Must be in the kNative state for calling native methods.
524 CHECK_EQ(self->GetState(), Thread::kNative);
525
Elliott Hughes719b3232011-09-25 17:42:19 -0700526 JNIEnv* env = self->GetJniEnv();
Elliott Hughes726079d2011-10-07 18:43:44 -0700527 ScopedLocalRef<jclass> c(env, env->FindClass("java/lang/Daemons"));
528 CHECK(c.get() != NULL);
529 jmethodID mid = env->GetStaticMethodID(c.get(), "start", "()V");
Elliott Hughes719b3232011-09-25 17:42:19 -0700530 CHECK(mid != NULL);
Elliott Hughes726079d2011-10-07 18:43:44 -0700531 env->CallStaticVoidMethod(c.get(), mid);
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700532
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800533 VLOG(startup) << "Runtime::StartDaemonThreads exiting";
Carl Shapiro61e019d2011-07-14 16:53:09 -0700534}
535
Elliott Hughes6b355752012-01-13 16:49:08 -0800536bool Runtime::IsShuttingDown() const {
537 return shutting_down_;
538}
539
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700540bool Runtime::IsStarted() const {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700541 return started_;
542}
543
Elliott Hughes0af55432011-08-17 18:37:28 -0700544bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700545 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700546
Elliott Hughes90a33692011-08-30 13:27:07 -0700547 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
548 if (options.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700549 LOG(ERROR) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700550 return false;
551 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800552 VLOG(startup) << "Runtime::Init -verbose:startup enabled";
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700553
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700554 SetJniGlobalsMax(options->jni_globals_max_);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800555 Monitor::Init(options->lock_profiling_threshold_, options->hook_is_sensitive_thread_);
Elliott Hughes32d6e1e2011-10-11 14:47:44 -0700556
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700557 host_prefix_ = options->host_prefix_;
558 boot_class_path_ = options->boot_class_path_;
559 class_path_ = options->class_path_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700560 properties_ = options->properties_;
561
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700562 is_zygote_ = options->is_zygote_;
563
Elliott Hughes0af55432011-08-17 18:37:28 -0700564 vfprintf_ = options->hook_vfprintf_;
565 exit_ = options->hook_exit_;
566 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700567
Elliott Hughesbe759c62011-09-08 19:38:21 -0700568 default_stack_size_ = options->stack_size_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700569 stack_trace_file_ = options->stack_trace_file_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700570
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700571 monitor_list_ = new MonitorList;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800572 thread_list_ = new ThreadList;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700573 intern_table_ = new InternTable;
574
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800575 Heap::Init(options->heap_initial_size_,
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700576 options->heap_maximum_size_,
jeffhaoc1160702011-10-27 15:48:45 -0700577 options->heap_growth_limit_,
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700578 options->images_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700579
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700580 BlockSignals();
581
Elliott Hughesa0957642011-09-02 14:27:33 -0700582 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700583
Elliott Hughesbe759c62011-09-08 19:38:21 -0700584 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700585
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700586 // ClassLinker needs an attached thread, but we can't fully attach a thread
587 // without creating objects.
588 Thread::Attach(this, "main", false);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700589
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700590 // Set us to runnable so tools using a runtime can allocate and GC by default
591 Thread::Current()->SetState(Thread::kRunnable);
592
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700593 CHECK_GE(Heap::GetSpaces().size(), 1U);
594 class_linker_ = ((Heap::GetSpaces()[0]->IsImageSpace())
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800595 ? ClassLinker::Create(intern_table_)
596 : ClassLinker::Create(options->boot_class_path_, intern_table_));
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700597
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800598 VLOG(startup) << "Runtime::Init exiting";
Carl Shapiro1fb86202011-06-27 17:43:13 -0700599 return true;
600}
601
Elliott Hughes038a8062011-09-18 14:12:41 -0700602void Runtime::InitNativeMethods() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800603 VLOG(startup) << "Runtime::InitNativeMethods entering";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700604 Thread* self = Thread::Current();
605 JNIEnv* env = self->GetJniEnv();
606
Elliott Hughes418d20f2011-09-22 14:00:39 -0700607 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Brian Carlstromaded5f72011-10-07 17:15:04 -0700608 CHECK_EQ(self->GetState(), Thread::kNative);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700609
Elliott Hughes418d20f2011-09-22 14:00:39 -0700610 // First set up JniConstants, which is used by both the runtime's built-in native
611 // methods and libcore.
Elliott Hughesfea966e2011-09-22 10:26:20 -0700612 JniConstants::init(env);
613
Elliott Hughes418d20f2011-09-22 14:00:39 -0700614 // Then set up the native methods provided by the runtime itself.
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700615 RegisterRuntimeNativeMethods(env);
616
Elliott Hughes418d20f2011-09-22 14:00:39 -0700617 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
618 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
619 // the library that implements System.loadLibrary!
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700620 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800621 VLOG(startup) << "Runtime::InitNativeMethods exiting";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700622}
623
624void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
625#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700626 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700627 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700628 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700629 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700630 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700631 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700632 REGISTER(register_java_lang_Object);
633 REGISTER(register_java_lang_Runtime);
634 REGISTER(register_java_lang_String);
635 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700636 REGISTER(register_java_lang_Thread);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700637 REGISTER(register_java_lang_Throwable);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700638 REGISTER(register_java_lang_VMClassLoader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700639 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700640 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700641 REGISTER(register_java_lang_reflect_Field);
642 REGISTER(register_java_lang_reflect_Method);
Jesse Wilson95caa792011-10-12 18:14:17 -0400643 REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700644 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700645 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700646 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700647 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700648#undef REGISTER
649}
650
Elliott Hughes8daa0922011-09-11 13:46:25 -0700651void Runtime::Dump(std::ostream& os) {
Elliott Hughese27955c2011-08-26 15:21:24 -0700652 // TODO: dump other runtime statistics?
Elliott Hughescac6cc72011-11-03 20:31:21 -0700653 GetClassLinker()->DumpForSigQuit(os);
654 GetInternTable()->DumpForSigQuit(os);
Elliott Hughes42ee1422011-09-06 12:33:32 -0700655 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700656
657 thread_list_->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700658}
659
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800660void Runtime::DumpLockHolders(std::ostream& os) {
661 pid_t heap_lock_owner = Heap::GetLockOwner();
662 pid_t thread_list_lock_owner = GetThreadList()->GetLockOwner();
663 pid_t classes_lock_owner = GetClassLinker()->GetClassesLockOwner();
664 pid_t dex_lock_owner = GetClassLinker()->GetDexLockOwner();
665 if ((heap_lock_owner | thread_list_lock_owner | classes_lock_owner | dex_lock_owner) != 0) {
666 os << "Heap lock owner tid: " << heap_lock_owner << "\n"
667 << "ThreadList lock owner tid: " << thread_list_lock_owner << "\n"
668 << "ClassLinker classes lock owner tid: " << classes_lock_owner << "\n"
669 << "ClassLinker dex lock owner tid: " << dex_lock_owner << "\n";
670 }
671}
672
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700673void Runtime::SetStatsEnabled(bool new_state) {
674 if (new_state == true) {
675 GetStats()->Clear(~0);
676 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
677 Thread::Current()->GetStats()->Clear(~0);
678 }
679 stats_enabled_ = new_state;
680}
681
682void Runtime::ResetStats(int kinds) {
683 GetStats()->Clear(kinds & 0xffff);
684 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
685 Thread::Current()->GetStats()->Clear(kinds >> 16);
686}
687
688RuntimeStats* Runtime::GetStats() {
689 return &stats_;
690}
691
692int32_t Runtime::GetStat(int kind) {
693 RuntimeStats* stats;
694 if (kind < (1<<16)) {
695 stats = GetStats();
696 } else {
697 stats = Thread::Current()->GetStats();
698 kind >>= 16;
699 }
700 switch (kind) {
701 case KIND_ALLOCATED_OBJECTS:
702 return stats->allocated_objects;
703 case KIND_ALLOCATED_BYTES:
704 return stats->allocated_bytes;
705 case KIND_FREED_OBJECTS:
706 return stats->freed_objects;
707 case KIND_FREED_BYTES:
708 return stats->freed_bytes;
709 case KIND_GC_INVOCATIONS:
710 return stats->gc_for_alloc_count;
711 case KIND_CLASS_INIT_COUNT:
712 return stats->class_init_count;
713 case KIND_CLASS_INIT_TIME:
714 // Convert ns to us, reduce to 32 bits.
715 return (int) (stats->class_init_time_ns / 1000);
716 case KIND_EXT_ALLOCATED_OBJECTS:
717 case KIND_EXT_ALLOCATED_BYTES:
718 case KIND_EXT_FREED_OBJECTS:
719 case KIND_EXT_FREED_BYTES:
720 return 0; // backward compatibility
721 default:
722 CHECK(false);
723 return -1; // unreachable
724 }
725}
726
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700727void Runtime::BlockSignals() {
728 sigset_t sigset;
729 if (sigemptyset(&sigset) == -1) {
730 PLOG(FATAL) << "sigemptyset failed";
731 }
732 if (sigaddset(&sigset, SIGPIPE) == -1) {
733 PLOG(ERROR) << "sigaddset SIGPIPE failed";
734 }
735 // SIGQUIT is used to dump the runtime's state (including stack traces).
736 if (sigaddset(&sigset, SIGQUIT) == -1) {
737 PLOG(ERROR) << "sigaddset SIGQUIT failed";
738 }
739 // SIGUSR1 is used to initiate a heap dump.
740 if (sigaddset(&sigset, SIGUSR1) == -1) {
741 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
742 }
743 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
744}
745
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700746void Runtime::AttachCurrentThread(const char* name, bool as_daemon) {
747 Thread::Attach(instance_, name, as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700748}
749
Elliott Hughesd92bec42011-09-02 17:04:36 -0700750void Runtime::DetachCurrentThread() {
Elliott Hughes038a8062011-09-18 14:12:41 -0700751 // TODO: check we're not calling DetachCurrentThread from a call stack that
752 // includes managed frames. (It's only valid if the stack is all-native.)
Elliott Hughes02b48d12011-09-07 17:15:51 -0700753 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700754}
755
Brian Carlstrome24fa612011-09-29 00:53:55 -0700756void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700757 Dbg::VisitRoots(visitor, arg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700758 class_linker_->VisitRoots(visitor, arg);
759 intern_table_->VisitRoots(visitor, arg);
760 java_vm_->VisitRoots(visitor, arg);
761 thread_list_->VisitRoots(visitor, arg);
762 visitor(jni_stub_array_, arg);
763 visitor(abstract_method_error_stub_array_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700764 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
765 visitor(resolution_stub_array_[i], arg);
766 }
767 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
768 visitor(callee_save_method_[i], arg);
769 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700770}
771
Ian Rogers169c9a72011-11-13 20:13:17 -0800772bool Runtime::HasJniDlsymLookupStub() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700773 return jni_stub_array_ != NULL;
774}
775
Ian Rogers169c9a72011-11-13 20:13:17 -0800776ByteArray* Runtime::GetJniDlsymLookupStub() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700777 CHECK(jni_stub_array_ != NULL);
778 return jni_stub_array_;
779}
780
Ian Rogers169c9a72011-11-13 20:13:17 -0800781void Runtime::SetJniDlsymLookupStub(ByteArray* jni_stub_array) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700782 CHECK(jni_stub_array != NULL) << " jni_stub_array=" << jni_stub_array;
783 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array)
784 << "jni_stub_array_=" << jni_stub_array_ << " jni_stub_array=" << jni_stub_array;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700785 jni_stub_array_ = jni_stub_array;
786}
787
788bool Runtime::HasAbstractMethodErrorStubArray() const {
789 return abstract_method_error_stub_array_ != NULL;
790}
791
792ByteArray* Runtime::GetAbstractMethodErrorStubArray() const {
793 CHECK(abstract_method_error_stub_array_ != NULL);
794 return abstract_method_error_stub_array_;
795}
796
797void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) {
798 CHECK(abstract_method_error_stub_array != NULL);
799 CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array);
800 abstract_method_error_stub_array_ = abstract_method_error_stub_array;
801}
802
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700803
804Runtime::TrampolineType Runtime::GetTrampolineType(Method* method) {
805 if (method == NULL) {
806 return Runtime::kUnknownMethod;
807 } else if (method->IsStatic()) {
808 return Runtime::kStaticMethod;
809 } else {
810 return Runtime::kInstanceMethod;
811 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700812}
813
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700814bool Runtime::HasResolutionStubArray(TrampolineType type) const {
815 return resolution_stub_array_[type] != NULL;
Ian Rogersad25ac52011-10-04 19:13:33 -0700816}
817
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700818ByteArray* Runtime::GetResolutionStubArray(TrampolineType type) const {
819 CHECK(HasResolutionStubArray(type));
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700820 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastTrampolineMethodType));
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700821 return resolution_stub_array_[type];
822}
823
824void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700825 CHECK(resolution_stub_array != NULL);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700826 CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array);
827 resolution_stub_array_[type] = resolution_stub_array;
Ian Rogersad25ac52011-10-04 19:13:33 -0700828}
829
Brian Carlstromae826982011-11-09 01:33:42 -0800830Method* Runtime::CreateCalleeSaveMethod(InstructionSet instruction_set, CalleeSaveType type) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700831 Class* method_class = Method::GetMethodClass();
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700832 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
Ian Rogersff1ed472011-09-20 13:46:24 -0700833 method->SetDeclaringClass(method_class);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800834 // TODO: use a special method for callee saves
835 method->SetMethodIndex(DexFile::kDexNoIndex16);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700836 method->SetCode(NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800837 if ((instruction_set == kThumb2) || (instruction_set == kArm)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700838 uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6) | (1 << art::arm::R7) |
839 (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11);
840 uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3);
841 uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9);
842 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills :0) |
843 (type == kSaveAll ? all_spills :0) | (1 << art::arm::LR);
844 uint32_t fp_all_spills = (1 << art::arm::S0) | (1 << art::arm::S1) | (1 << art::arm::S2) |
845 (1 << art::arm::S3) | (1 << art::arm::S4) | (1 << art::arm::S5) |
846 (1 << art::arm::S6) | (1 << art::arm::S7) | (1 << art::arm::S8) |
847 (1 << art::arm::S9) | (1 << art::arm::S10) | (1 << art::arm::S11) |
848 (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) |
849 (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) |
850 (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) |
851 (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) |
852 (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) |
853 (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
854 (1 << art::arm::S30) | (1 << art::arm::S31);
855 uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
856 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
857 __builtin_popcount(fp_spills) /* fprs */ +
858 1 /* Method* */) * kPointerSize, kStackAlignment);
Ian Rogers15fdb8c2011-09-25 15:45:07 -0700859 method->SetFrameSizeInBytes(frame_size);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700860 method->SetCoreSpillMask(core_spills);
861 method->SetFpSpillMask(fp_spills);
Brian Carlstromae826982011-11-09 01:33:42 -0800862 } else if (instruction_set == kX86) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700863 method->SetFrameSizeInBytes(32);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700864 method->SetCoreSpillMask((1 << art::x86::EBX) | (1 << art::x86::EBP) | (1 << art::x86::ESI) |
Ian Rogersff1ed472011-09-20 13:46:24 -0700865 (1 << art::x86::EDI));
866 method->SetFpSpillMask(0);
867 } else {
868 UNIMPLEMENTED(FATAL);
869 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700870 return method.get();
Ian Rogersff1ed472011-09-20 13:46:24 -0700871}
872
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700873bool Runtime::HasCalleeSaveMethod(CalleeSaveType type) const {
874 return callee_save_method_[type] != NULL;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700875}
876
Brian Carlstrome24fa612011-09-29 00:53:55 -0700877// Returns a special method that describes all callee saves being spilled to the stack.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700878Method* Runtime::GetCalleeSaveMethod(CalleeSaveType type) const {
879 CHECK(HasCalleeSaveMethod(type));
880 return callee_save_method_[type];
Brian Carlstrome24fa612011-09-29 00:53:55 -0700881}
882
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700883void Runtime::SetCalleeSaveMethod(Method* method, CalleeSaveType type) {
884 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
885 callee_save_method_[type] = method;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700886}
887
jeffhao2692b572011-12-16 15:42:28 -0800888void Runtime::EnableMethodTracing(Trace* tracer) {
889 CHECK(!IsMethodTracingActive());
890 tracer_ = tracer;
891}
892
893void Runtime::DisableMethodTracing() {
894 CHECK(IsMethodTracingActive());
895 delete tracer_;
896 tracer_ = NULL;
897}
898
899bool Runtime::IsMethodTracingActive() const {
900 return (tracer_ != NULL);
901}
902
903Trace* Runtime::GetTracer() const {
904 CHECK(IsMethodTracingActive());
905 return tracer_;
906}
907
Carl Shapiro1fb86202011-06-27 17:43:13 -0700908} // namespace art