blob: c6081ff1525f31ed4853003c01270ade269d253d [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"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070015#include "heap.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070016#include "image.h"
Elliott Hughescf4c6c42011-09-01 15:16:42 -070017#include "intern_table.h"
Elliott Hughesc5f7c912011-08-18 14:00:42 -070018#include "jni_internal.h"
Elliott Hughes32d6e1e2011-10-11 14:47:44 -070019#include "monitor.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070020#include "oat_file.h"
Elliott Hughes726079d2011-10-07 18:43:44 -070021#include "ScopedLocalRef.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070022#include "signal_catcher.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070023#include "space.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070024#include "thread.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070025#include "thread_list.h"
Elliott Hughes3bb81562011-10-21 18:52:59 -070026#include "UniquePtr.h"
Carl Shapiro61e019d2011-07-14 16:53:09 -070027
Elliott Hughes90a33692011-08-30 13:27:07 -070028// TODO: this drags in cutil/log.h, which conflicts with our logging.h.
29#include "JniConstants.h"
30
Carl Shapiro1fb86202011-06-27 17:43:13 -070031namespace art {
32
Carl Shapiro2ed144c2011-07-26 16:52:08 -070033Runtime* Runtime::instance_ = NULL;
34
Elliott Hughesdcc24742011-09-07 14:02:44 -070035Runtime::Runtime()
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080036 : is_zygote_(false),
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -070037 default_stack_size_(Thread::kDefaultStackSize),
Elliott Hughesc33a32b2011-10-11 18:18:07 -070038 monitor_list_(NULL),
Elliott Hughesdcc24742011-09-07 14:02:44 -070039 thread_list_(NULL),
40 intern_table_(NULL),
41 class_linker_(NULL),
42 signal_catcher_(NULL),
43 java_vm_(NULL),
Brian Carlstrom16192862011-09-12 17:50:06 -070044 jni_stub_array_(NULL),
Brian Carlstrome24fa612011-09-29 00:53:55 -070045 abstract_method_error_stub_array_(NULL),
Elliott Hughes6b355752012-01-13 16:49:08 -080046 shutting_down_(false),
Elliott Hughesdcc24742011-09-07 14:02:44 -070047 started_(false),
48 vfprintf_(NULL),
49 exit_(NULL),
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070050 abort_(NULL),
jeffhao2692b572011-12-16 15:42:28 -080051 stats_enabled_(false),
52 tracer_(NULL) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -070053 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
54 resolution_stub_array_[i] = NULL;
55 }
56 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
57 callee_save_method_[i] = NULL;
58 }
Elliott Hughesdcc24742011-09-07 14:02:44 -070059}
60
Carl Shapiro61e019d2011-07-14 16:53:09 -070061Runtime::~Runtime() {
Elliott Hughes6b355752012-01-13 16:49:08 -080062 shutting_down_ = true;
63
Elliott Hughesd1cc8362011-10-24 16:58:50 -070064 Dbg::StopJdwp();
65
Elliott Hughes5fe594f2011-09-08 12:33:17 -070066 // Make sure our internal threads are dead before we start tearing down things they're using.
67 delete signal_catcher_;
Elliott Hughes038a8062011-09-18 14:12:41 -070068 // TODO: GC thread.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070069
Elliott Hughes038a8062011-09-18 14:12:41 -070070 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
Elliott Hughes93e74e82011-09-13 11:07:03 -070071 delete thread_list_;
Elliott Hughesc33a32b2011-10-11 18:18:07 -070072 delete monitor_list_;
Elliott Hughes93e74e82011-09-13 11:07:03 -070073
Carl Shapiro61e019d2011-07-14 16:53:09 -070074 delete class_linker_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070075 Heap::Destroy();
Elliott Hughescf4c6c42011-09-01 15:16:42 -070076 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070077 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -070078 Thread::Shutdown();
Carl Shapiro4acf4642011-07-26 18:54:13 -070079 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070080 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -070081 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -070082}
83
Elliott Hughescac6cc72011-11-03 20:31:21 -070084static bool gAborting = false;
85
Elliott Hughese0918552011-10-28 17:18:29 -070086struct AbortState {
87 void Dump(std::ostream& os) {
Elliott Hughescac6cc72011-11-03 20:31:21 -070088 if (gAborting) {
89 os << "Runtime aborting --- recursively, so no thread-specific detail!\n";
90 return;
91 }
92 gAborting = true;
Elliott Hughese0918552011-10-28 17:18:29 -070093 os << "Runtime aborting...\n";
94 Thread* self = Thread::Current();
95 if (self == NULL) {
96 os << "(Aborting thread was not attached to runtime!)\n";
97 } else {
98 self->Dump(os, true);
99 }
100 }
101};
102
Elliott Hughesffe67362011-07-17 12:09:27 -0700103void Runtime::Abort(const char* file, int line) {
104 // Get any pending output out of the way.
105 fflush(NULL);
106
107 // Many people have difficulty distinguish aborts from crashes,
108 // so be explicit.
Elliott Hughese0918552011-10-28 17:18:29 -0700109 AbortState state;
110 LOG(ERROR) << Dumpable<AbortState>(state);
Elliott Hughesffe67362011-07-17 12:09:27 -0700111
Elliott Hughesffe67362011-07-17 12:09:27 -0700112 // Perform any platform-specific pre-abort actions.
113 PlatformAbort(file, line);
114
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700115 // use abort hook if we have one
116 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
117 Runtime::Current()->abort_();
118 // notreached
119 }
120
Elliott Hughesffe67362011-07-17 12:09:27 -0700121 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -0700122 // receive SIGABRT. debuggerd dumps the stack trace of the main
123 // thread, whether or not that was the thread that failed. By
124 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -0700125 // fault in the current thread, and get a useful log from debuggerd.
126 // We can also trivially tell the difference between a VM crash and
127 // a deliberate abort by looking at the fault address.
128 *reinterpret_cast<char*>(0xdeadd00d) = 38;
129 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -0700130 // notreached
131}
132
Elliott Hughesbf86d042011-08-31 17:53:14 -0700133void Runtime::CallExitHook(jint status) {
134 if (exit_ != NULL) {
135 ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative);
136 exit_(status);
137 LOG(WARNING) << "Exit hook returned instead of exiting!";
138 }
139}
140
Brian Carlstrom8a436592011-08-15 21:27:23 -0700141// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
142// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
143// [gG] gigabytes.
144//
145// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700146// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
147// of 1024.
148//
149// The spec says the -Xmx and -Xms options must be multiples of 1024. It
150// doesn't say anything about -Xss.
151//
152// Returns 0 (a useless size) if "s" is malformed or specifies a low or
153// non-evenly-divisible value.
154//
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800155size_t ParseMemoryOption(const char* s, size_t div) {
Brian Carlstrom8a436592011-08-15 21:27:23 -0700156 // strtoul accepts a leading [+-], which we don't want,
157 // so make sure our string starts with a decimal digit.
158 if (isdigit(*s)) {
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800159 const char* s2;
160 size_t val = strtoul(s, (char**)&s2, 10);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700161 if (s2 != s) {
162 // s2 should be pointing just after the number.
163 // If this is the end of the string, the user
164 // has specified a number of bytes. Otherwise,
165 // there should be exactly one more character
166 // that specifies a multiplier.
167 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700168 // The remainder of the string is either a single multiplier
169 // character, or nothing to indicate that the value is in
170 // bytes.
171 char c = *s2++;
172 if (*s2 == '\0') {
173 size_t mul;
174 if (c == '\0') {
175 mul = 1;
176 } else if (c == 'k' || c == 'K') {
177 mul = KB;
178 } else if (c == 'm' || c == 'M') {
179 mul = MB;
180 } else if (c == 'g' || c == 'G') {
181 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700182 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700183 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700184 return 0;
185 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700186
187 if (val <= std::numeric_limits<size_t>::max() / mul) {
188 val *= mul;
189 } else {
190 // Clamp to a multiple of 1024.
191 val = std::numeric_limits<size_t>::max() & ~(1024-1);
192 }
193 } else {
194 // There's more than one character after the numeric part.
195 return 0;
196 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700197 }
198 // The man page says that a -Xm value must be a multiple of 1024.
199 if (val % div == 0) {
200 return val;
201 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700202 }
203 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700204 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700205}
206
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700207size_t ParseIntegerOrDie(const StringPiece& s) {
208 StringPiece::size_type colon = s.find(':');
209 if (colon == StringPiece::npos) {
210 LOG(FATAL) << "Missing integer: " << s;
211 }
212 const char* begin = &s.data()[colon + 1];
213 char* end;
214 size_t result = strtoul(begin, &end, 10);
215 if (begin == end || *end != '\0') {
216 LOG(FATAL) << "Failed to parse integer in: " << s;
217 }
218 return result;
219}
220
Elliott Hughes0af55432011-08-17 18:37:28 -0700221void LoadJniLibrary(JavaVMExt* vm, const char* name) {
222 // TODO: OS_SHARED_LIB_FORMAT_STR
223 std::string mapped_name(StringPrintf("lib%s.so", name));
Elliott Hughes75770752011-08-24 17:52:38 -0700224 std::string reason;
225 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700226 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
227 << reason;
228 }
229}
230
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700231Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700232 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700233 bool compiler = false;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700234 const char* boot_class_path = getenv("BOOTCLASSPATH");
235 if (boot_class_path != NULL) {
Ian Rogers4f6b57a2012-01-17 16:23:27 -0800236 parsed->boot_class_path_ = boot_class_path;
237 } else {
238 parsed->boot_class_path_ = ".";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700239 }
240 const char* class_path = getenv("CLASSPATH");
241 if (class_path != NULL) {
Ian Rogers4f6b57a2012-01-17 16:23:27 -0800242 parsed->class_path_ = class_path;
243 } else {
244 parsed->class_path_ = ".";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700245 }
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700246#ifdef NDEBUG
Elliott Hughes5174fe62011-08-23 15:12:35 -0700247 // -Xcheck:jni is off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700248 parsed->check_jni_ = false;
249#else
250 // ...but on by default in debug builds.
251 parsed->check_jni_ = true;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700252#endif
Elliott Hughes85d15452011-09-16 17:33:01 -0700253
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700254 parsed->heap_initial_size_ = Heap::kInitialSize;
255 parsed->heap_maximum_size_ = Heap::kMaximumSize;
jeffhaoc1160702011-10-27 15:48:45 -0700256 parsed->heap_growth_limit_ = 0; // 0 means no growth limit
Brian Carlstromf734cf52011-08-17 16:28:14 -0700257 parsed->stack_size_ = Thread::kDefaultStackSize;
258
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700259 parsed->is_zygote_ = false;
260
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700261 parsed->jni_globals_max_ = 0;
Elliott Hughesfc861622011-10-17 17:57:47 -0700262 parsed->lock_profiling_threshold_ = 0;
263 parsed->hook_is_sensitive_thread_ = NULL;
264
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700265 parsed->hook_vfprintf_ = vfprintf;
266 parsed->hook_exit_ = exit;
267 parsed->hook_abort_ = abort;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700268
269 for (size_t i = 0; i < options.size(); ++i) {
270 const StringPiece& option = options[i].first;
Brian Carlstrom0796af02011-10-12 14:31:45 -0700271 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700272 LOG(INFO) << "option[" << i << "]=" << option;
273 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700274 if (option.starts_with("-Xbootclasspath:")) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700275 parsed->boot_class_path_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700276 } else if (option == "-classpath" || option == "-cp") {
277 // TODO: support -Djava.class.path
278 i++;
279 if (i == options.size()) {
280 // TODO: usage
281 LOG(FATAL) << "Missing required class path value for " << option;
282 return NULL;
283 }
284 const StringPiece& value = options[i].first;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700285 parsed->class_path_ = value.data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700286 } else if (option.starts_with("-Ximage:")) {
287 parsed->images_.push_back(option.substr(strlen("-Ximage:")).data());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700288 } else if (option.starts_with("-Xcheck:jni")) {
289 parsed->check_jni_ = true;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700290 } else if (option.starts_with("-Xrunjdwp:") || option.starts_with("-agentlib:jdwp=")) {
Elliott Hughes95572412011-12-13 18:14:20 -0800291 std::string tail(option.substr(option[1] == 'X' ? 10 : 15).ToString());
Elliott Hughes3bb81562011-10-21 18:52:59 -0700292 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
293 LOG(FATAL) << "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
294 << "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n";
295 return NULL;
296 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700297 } else if (option.starts_with("-Xms")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700298 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024);
299 if (size == 0) {
300 if (ignore_unrecognized) {
301 continue;
302 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700303 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700304 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700305 return NULL;
306 }
307 parsed->heap_initial_size_ = size;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700308 } else if (option.starts_with("-Xmx")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700309 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).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_maximum_size_ = size;
jeffhaoc1160702011-10-27 15:48:45 -0700319 } else if (option.starts_with("-XX:HeapGrowthLimit=")) {
320 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).data(), 1024);
321 if (size == 0) {
322 if (ignore_unrecognized) {
323 continue;
324 }
325 // TODO: usage
326 LOG(FATAL) << "Failed to parse " << option;
327 return NULL;
328 }
329 parsed->heap_growth_limit_ = size;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700330 } else if (option.starts_with("-Xss")) {
331 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1);
332 if (size == 0) {
333 if (ignore_unrecognized) {
334 continue;
335 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700336 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700337 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700338 return NULL;
339 }
340 parsed->stack_size_ = size;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700341 } else if (option.starts_with("-D")) {
342 parsed->properties_.push_back(option.substr(strlen("-D")).data());
Elliott Hughesa0957642011-09-02 14:27:33 -0700343 } else if (option.starts_with("-Xjnitrace:")) {
344 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:")).data();
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700345 } else if (option == "compiler") {
346 compiler = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700347 } else if (option == "-Xzygote") {
348 parsed->is_zygote_ = true;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700349 } else if (option.starts_with("-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700350 std::vector<std::string> verbose_options;
Elliott Hughes34023802011-08-30 12:06:17 -0700351 Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700352 for (size_t i = 0; i < verbose_options.size(); ++i) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800353 if (verbose_options[i] == "class") {
354 gLogVerbosity.class_linker = true;
355 } else if (verbose_options[i] == "compiler") {
356 gLogVerbosity.compiler = true;
357 } else if (verbose_options[i] == "heap") {
358 gLogVerbosity.heap = true;
359 } else if (verbose_options[i] == "gc") {
360 gLogVerbosity.gc = true;
361 } else if (verbose_options[i] == "jdwp") {
362 gLogVerbosity.jdwp = true;
363 } else if (verbose_options[i] == "jni") {
364 gLogVerbosity.jni = true;
365 } else if (verbose_options[i] == "monitor") {
366 gLogVerbosity.monitor = true;
367 } else if (verbose_options[i] == "startup") {
368 gLogVerbosity.startup = true;
369 } else if (verbose_options[i] == "third-party-jni") {
370 gLogVerbosity.third_party_jni = true;
371 } else if (verbose_options[i] == "threads") {
372 gLogVerbosity.threads = true;
373 } else {
374 LOG(WARNING) << "Ignoring unknown -verbose option: " << verbose_options[i];
375 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700376 }
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700377 } else if (option.starts_with("-Xjnigreflimit:")) {
378 parsed->jni_globals_max_ = ParseIntegerOrDie(option);
Elliott Hughesfc861622011-10-17 17:57:47 -0700379 } else if (option.starts_with("-Xlockprofthreshold:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700380 parsed->lock_profiling_threshold_ = ParseIntegerOrDie(option);
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700381 } else if (option.starts_with("-Xstacktracefile:")) {
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700382// always show stack traces in debug builds
383#ifdef NDEBUG
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700384 parsed->stack_trace_file_ = option.substr(strlen("-Xstacktracefile:")).data();
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700385#endif
Elliott Hughesfc861622011-10-17 17:57:47 -0700386 } else if (option == "sensitiveThread") {
387 parsed->hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(options[i].second);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700388 } else if (option == "vfprintf") {
389 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
390 } else if (option == "exit") {
391 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
392 } else if (option == "abort") {
393 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700394 } else if (option == "host-prefix") {
395 parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second);
Elliott Hughesb3e66df2012-01-12 14:49:18 -0800396 } else if (option == "-Xgenregmap" || option == "-Xgc:precise") {
397 // We silently ignore these for backwards compatibility.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700398 } else {
399 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700400 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700401 LOG(ERROR) << "Unrecognized option " << option;
402 // TODO: this should exit, but for now tolerate unknown options
403 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700404 }
405 }
406 }
407
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700408 if (!compiler && parsed->images_.empty()) {
Brian Carlstrom29e7ac72011-12-05 23:42:57 -0800409 parsed->images_.push_back("/system/framework/boot.art");
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700410 }
jeffhaoc1160702011-10-27 15:48:45 -0700411 if (parsed->heap_growth_limit_ == 0) {
412 parsed->heap_growth_limit_ = parsed->heap_maximum_size_;
413 }
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700414
Elliott Hughescac6cc72011-11-03 20:31:21 -0700415 LOG(INFO) << "Build type: "
416#ifndef NDEBUG
417 << "debug"
418#else
419 << "optimized"
420#endif
421 << "; CheckJNI: " << (parsed->check_jni_ ? "on" : "off");
Elliott Hughes85d15452011-09-16 17:33:01 -0700422
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700423 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700424}
425
426Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700427 // TODO: acquire a static mutex on Runtime to avoid racing.
428 if (Runtime::instance_ != NULL) {
429 return NULL;
430 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700431 instance_ = new Runtime;
432 if (!instance_->Init(options, ignore_unrecognized)) {
433 delete instance_;
434 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700435 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700436 return instance_;
437}
Elliott Hughes0af55432011-08-17 18:37:28 -0700438
Brian Carlstromaded5f72011-10-07 17:15:04 -0700439void CreateSystemClassLoader() {
440 if (ClassLoader::UseCompileTimeClassPath()) {
441 return;
442 }
443
444 Thread* self = Thread::Current();
445
446 // Must be in the kNative state for calling native methods.
447 CHECK_EQ(self->GetState(), Thread::kNative);
448
449 JNIEnv* env = self->GetJniEnv();
Brian Carlstromdf143242011-10-10 18:05:34 -0700450 ScopedLocalRef<jclass> ClassLoader_class(env, env->FindClass("java/lang/ClassLoader"));
451 CHECK(ClassLoader_class.get() != NULL);
452 jmethodID getSystemClassLoader = env->GetStaticMethodID(ClassLoader_class.get(),
453 "getSystemClassLoader",
454 "()Ljava/lang/ClassLoader;");
455 CHECK(getSystemClassLoader != NULL);
456 ScopedLocalRef<jobject> class_loader(env, env->CallStaticObjectMethod(ClassLoader_class.get(),
457 getSystemClassLoader));
458 CHECK(class_loader.get() != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700459
Brian Carlstromdf143242011-10-10 18:05:34 -0700460 Thread::Current()->SetClassLoaderOverride(Decode<ClassLoader*>(env, class_loader.get()));
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500461
462 ScopedLocalRef<jclass> Thread_class(env, env->FindClass("java/lang/Thread"));
463 CHECK(Thread_class.get() != NULL);
464 jfieldID contextClassLoader = env->GetFieldID(Thread_class.get(),
465 "contextClassLoader",
466 "Ljava/lang/ClassLoader;");
467 CHECK(contextClassLoader != NULL);
468 ScopedLocalRef<jobject> self_jobject(env, AddLocalReference<jobject>(env, self->GetPeer()));
469 env->SetObjectField(self_jobject.get(), contextClassLoader, class_loader.get());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700470}
471
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700472void Runtime::Start() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800473 VLOG(startup) << "Runtime::Start entering";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700474
475 CHECK(host_prefix_.empty()) << host_prefix_;
476
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700477 // Restore main thread state to kNative as expected by native code
478 Thread::Current()->SetState(Thread::kNative);
479
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700480 started_ = true;
481
Brian Carlstromae826982011-11-09 01:33:42 -0800482 // InitNativeMethods needs to be after started_ so that the classes
483 // it touches will have methods linked to the oat file if necessary.
484 InitNativeMethods();
485
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500486 Thread::FinishStartup();
487
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700488 if (!is_zygote_) {
489 DidForkFromZygote();
490 }
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700491
Elliott Hughes85d15452011-09-16 17:33:01 -0700492 StartDaemonThreads();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700493
Brian Carlstromaded5f72011-10-07 17:15:04 -0700494 CreateSystemClassLoader();
495
Elliott Hughes726079d2011-10-07 18:43:44 -0700496 Thread::Current()->GetJniEnv()->locals.AssertEmpty();
497
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800498 VLOG(startup) << "Runtime::Start exiting";
Elliott Hughes85d15452011-09-16 17:33:01 -0700499}
500
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700501void Runtime::DidForkFromZygote() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700502 is_zygote_ = false;
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700503
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700504 StartSignalCatcher();
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700505
506 // Start the JDWP thread. If the command-line debugger flags specified "suspend=y",
507 // this will pause the runtime, so we probably want this to come last.
508 Dbg::StartJdwp();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700509}
510
511void Runtime::StartSignalCatcher() {
512 if (!is_zygote_) {
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700513 signal_catcher_ = new SignalCatcher(stack_trace_file_);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700514 }
515}
516
Elliott Hughes85d15452011-09-16 17:33:01 -0700517void Runtime::StartDaemonThreads() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800518 VLOG(startup) << "Runtime::StartDaemonThreads entering";
Elliott Hughes85d15452011-09-16 17:33:01 -0700519
Elliott Hughes719b3232011-09-25 17:42:19 -0700520 Thread* self = Thread::Current();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700521
522 // Must be in the kNative state for calling native methods.
523 CHECK_EQ(self->GetState(), Thread::kNative);
524
Elliott Hughes719b3232011-09-25 17:42:19 -0700525 JNIEnv* env = self->GetJniEnv();
Elliott Hughes726079d2011-10-07 18:43:44 -0700526 ScopedLocalRef<jclass> c(env, env->FindClass("java/lang/Daemons"));
527 CHECK(c.get() != NULL);
528 jmethodID mid = env->GetStaticMethodID(c.get(), "start", "()V");
Elliott Hughes719b3232011-09-25 17:42:19 -0700529 CHECK(mid != NULL);
Elliott Hughes726079d2011-10-07 18:43:44 -0700530 env->CallStaticVoidMethod(c.get(), mid);
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700531
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800532 VLOG(startup) << "Runtime::StartDaemonThreads exiting";
Carl Shapiro61e019d2011-07-14 16:53:09 -0700533}
534
Elliott Hughes6b355752012-01-13 16:49:08 -0800535bool Runtime::IsShuttingDown() const {
536 return shutting_down_;
537}
538
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700539bool Runtime::IsStarted() const {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700540 return started_;
541}
542
Elliott Hughes0af55432011-08-17 18:37:28 -0700543bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700544 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700545
Elliott Hughes90a33692011-08-30 13:27:07 -0700546 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
547 if (options.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700548 LOG(ERROR) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700549 return false;
550 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800551 VLOG(startup) << "Runtime::Init -verbose:startup enabled";
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700552
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700553 SetJniGlobalsMax(options->jni_globals_max_);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800554 Monitor::Init(options->lock_profiling_threshold_, options->hook_is_sensitive_thread_);
Elliott Hughes32d6e1e2011-10-11 14:47:44 -0700555
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700556 host_prefix_ = options->host_prefix_;
557 boot_class_path_ = options->boot_class_path_;
558 class_path_ = options->class_path_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700559 properties_ = options->properties_;
560
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700561 is_zygote_ = options->is_zygote_;
562
Elliott Hughes0af55432011-08-17 18:37:28 -0700563 vfprintf_ = options->hook_vfprintf_;
564 exit_ = options->hook_exit_;
565 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700566
Elliott Hughesbe759c62011-09-08 19:38:21 -0700567 default_stack_size_ = options->stack_size_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700568 stack_trace_file_ = options->stack_trace_file_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700569
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700570 monitor_list_ = new MonitorList;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800571 thread_list_ = new ThreadList;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700572 intern_table_ = new InternTable;
573
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800574 Heap::Init(options->heap_initial_size_,
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700575 options->heap_maximum_size_,
jeffhaoc1160702011-10-27 15:48:45 -0700576 options->heap_growth_limit_,
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700577 options->images_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700578
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700579 BlockSignals();
580
Elliott Hughesa0957642011-09-02 14:27:33 -0700581 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700582
Elliott Hughesbe759c62011-09-08 19:38:21 -0700583 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700584
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700585 // ClassLinker needs an attached thread, but we can't fully attach a thread
586 // without creating objects.
587 Thread::Attach(this, "main", false);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700588
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700589 // Set us to runnable so tools using a runtime can allocate and GC by default
590 Thread::Current()->SetState(Thread::kRunnable);
591
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700592 CHECK_GE(Heap::GetSpaces().size(), 1U);
593 class_linker_ = ((Heap::GetSpaces()[0]->IsImageSpace())
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800594 ? ClassLinker::Create(intern_table_)
595 : ClassLinker::Create(options->boot_class_path_, intern_table_));
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700596
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800597 VLOG(startup) << "Runtime::Init exiting";
Carl Shapiro1fb86202011-06-27 17:43:13 -0700598 return true;
599}
600
Elliott Hughes038a8062011-09-18 14:12:41 -0700601void Runtime::InitNativeMethods() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800602 VLOG(startup) << "Runtime::InitNativeMethods entering";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700603 Thread* self = Thread::Current();
604 JNIEnv* env = self->GetJniEnv();
605
Elliott Hughes418d20f2011-09-22 14:00:39 -0700606 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Brian Carlstromaded5f72011-10-07 17:15:04 -0700607 CHECK_EQ(self->GetState(), Thread::kNative);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700608
Elliott Hughes418d20f2011-09-22 14:00:39 -0700609 // First set up JniConstants, which is used by both the runtime's built-in native
610 // methods and libcore.
Elliott Hughesfea966e2011-09-22 10:26:20 -0700611 JniConstants::init(env);
612
Elliott Hughes418d20f2011-09-22 14:00:39 -0700613 // Then set up the native methods provided by the runtime itself.
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700614 RegisterRuntimeNativeMethods(env);
615
Elliott Hughes418d20f2011-09-22 14:00:39 -0700616 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
617 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
618 // the library that implements System.loadLibrary!
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700619 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800620 VLOG(startup) << "Runtime::InitNativeMethods exiting";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700621}
622
623void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
624#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700625 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700626 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700627 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700628 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700629 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700630 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700631 REGISTER(register_java_lang_Object);
632 REGISTER(register_java_lang_Runtime);
633 REGISTER(register_java_lang_String);
634 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700635 REGISTER(register_java_lang_Thread);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700636 REGISTER(register_java_lang_Throwable);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700637 REGISTER(register_java_lang_VMClassLoader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700638 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700639 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700640 REGISTER(register_java_lang_reflect_Field);
641 REGISTER(register_java_lang_reflect_Method);
Jesse Wilson95caa792011-10-12 18:14:17 -0400642 REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700643 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700644 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700645 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700646 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700647#undef REGISTER
648}
649
Elliott Hughes8daa0922011-09-11 13:46:25 -0700650void Runtime::Dump(std::ostream& os) {
Elliott Hughese27955c2011-08-26 15:21:24 -0700651 // TODO: dump other runtime statistics?
Elliott Hughescac6cc72011-11-03 20:31:21 -0700652 GetClassLinker()->DumpForSigQuit(os);
653 GetInternTable()->DumpForSigQuit(os);
Elliott Hughes42ee1422011-09-06 12:33:32 -0700654 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700655
656 thread_list_->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700657}
658
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800659void Runtime::DumpLockHolders(std::ostream& os) {
660 pid_t heap_lock_owner = Heap::GetLockOwner();
661 pid_t thread_list_lock_owner = GetThreadList()->GetLockOwner();
662 pid_t classes_lock_owner = GetClassLinker()->GetClassesLockOwner();
663 pid_t dex_lock_owner = GetClassLinker()->GetDexLockOwner();
664 if ((heap_lock_owner | thread_list_lock_owner | classes_lock_owner | dex_lock_owner) != 0) {
665 os << "Heap lock owner tid: " << heap_lock_owner << "\n"
666 << "ThreadList lock owner tid: " << thread_list_lock_owner << "\n"
667 << "ClassLinker classes lock owner tid: " << classes_lock_owner << "\n"
668 << "ClassLinker dex lock owner tid: " << dex_lock_owner << "\n";
669 }
670}
671
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700672void Runtime::SetStatsEnabled(bool new_state) {
673 if (new_state == true) {
674 GetStats()->Clear(~0);
675 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
676 Thread::Current()->GetStats()->Clear(~0);
677 }
678 stats_enabled_ = new_state;
679}
680
681void Runtime::ResetStats(int kinds) {
682 GetStats()->Clear(kinds & 0xffff);
683 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
684 Thread::Current()->GetStats()->Clear(kinds >> 16);
685}
686
687RuntimeStats* Runtime::GetStats() {
688 return &stats_;
689}
690
691int32_t Runtime::GetStat(int kind) {
692 RuntimeStats* stats;
693 if (kind < (1<<16)) {
694 stats = GetStats();
695 } else {
696 stats = Thread::Current()->GetStats();
697 kind >>= 16;
698 }
699 switch (kind) {
700 case KIND_ALLOCATED_OBJECTS:
701 return stats->allocated_objects;
702 case KIND_ALLOCATED_BYTES:
703 return stats->allocated_bytes;
704 case KIND_FREED_OBJECTS:
705 return stats->freed_objects;
706 case KIND_FREED_BYTES:
707 return stats->freed_bytes;
708 case KIND_GC_INVOCATIONS:
709 return stats->gc_for_alloc_count;
710 case KIND_CLASS_INIT_COUNT:
711 return stats->class_init_count;
712 case KIND_CLASS_INIT_TIME:
713 // Convert ns to us, reduce to 32 bits.
714 return (int) (stats->class_init_time_ns / 1000);
715 case KIND_EXT_ALLOCATED_OBJECTS:
716 case KIND_EXT_ALLOCATED_BYTES:
717 case KIND_EXT_FREED_OBJECTS:
718 case KIND_EXT_FREED_BYTES:
719 return 0; // backward compatibility
720 default:
721 CHECK(false);
722 return -1; // unreachable
723 }
724}
725
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700726void Runtime::BlockSignals() {
727 sigset_t sigset;
728 if (sigemptyset(&sigset) == -1) {
729 PLOG(FATAL) << "sigemptyset failed";
730 }
731 if (sigaddset(&sigset, SIGPIPE) == -1) {
732 PLOG(ERROR) << "sigaddset SIGPIPE failed";
733 }
734 // SIGQUIT is used to dump the runtime's state (including stack traces).
735 if (sigaddset(&sigset, SIGQUIT) == -1) {
736 PLOG(ERROR) << "sigaddset SIGQUIT failed";
737 }
738 // SIGUSR1 is used to initiate a heap dump.
739 if (sigaddset(&sigset, SIGUSR1) == -1) {
740 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
741 }
742 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
743}
744
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700745void Runtime::AttachCurrentThread(const char* name, bool as_daemon) {
746 Thread::Attach(instance_, name, as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700747}
748
Elliott Hughesd92bec42011-09-02 17:04:36 -0700749void Runtime::DetachCurrentThread() {
Elliott Hughes038a8062011-09-18 14:12:41 -0700750 // TODO: check we're not calling DetachCurrentThread from a call stack that
751 // includes managed frames. (It's only valid if the stack is all-native.)
Elliott Hughes02b48d12011-09-07 17:15:51 -0700752 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700753}
754
Brian Carlstrome24fa612011-09-29 00:53:55 -0700755void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700756 Dbg::VisitRoots(visitor, arg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700757 class_linker_->VisitRoots(visitor, arg);
758 intern_table_->VisitRoots(visitor, arg);
759 java_vm_->VisitRoots(visitor, arg);
760 thread_list_->VisitRoots(visitor, arg);
761 visitor(jni_stub_array_, arg);
762 visitor(abstract_method_error_stub_array_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700763 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
764 visitor(resolution_stub_array_[i], arg);
765 }
766 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
767 visitor(callee_save_method_[i], arg);
768 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700769}
770
Ian Rogers169c9a72011-11-13 20:13:17 -0800771bool Runtime::HasJniDlsymLookupStub() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700772 return jni_stub_array_ != NULL;
773}
774
Ian Rogers169c9a72011-11-13 20:13:17 -0800775ByteArray* Runtime::GetJniDlsymLookupStub() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700776 CHECK(jni_stub_array_ != NULL);
777 return jni_stub_array_;
778}
779
Ian Rogers169c9a72011-11-13 20:13:17 -0800780void Runtime::SetJniDlsymLookupStub(ByteArray* jni_stub_array) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700781 CHECK(jni_stub_array != NULL) << " jni_stub_array=" << jni_stub_array;
782 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array)
783 << "jni_stub_array_=" << jni_stub_array_ << " jni_stub_array=" << jni_stub_array;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700784 jni_stub_array_ = jni_stub_array;
785}
786
787bool Runtime::HasAbstractMethodErrorStubArray() const {
788 return abstract_method_error_stub_array_ != NULL;
789}
790
791ByteArray* Runtime::GetAbstractMethodErrorStubArray() const {
792 CHECK(abstract_method_error_stub_array_ != NULL);
793 return abstract_method_error_stub_array_;
794}
795
796void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) {
797 CHECK(abstract_method_error_stub_array != NULL);
798 CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array);
799 abstract_method_error_stub_array_ = abstract_method_error_stub_array;
800}
801
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700802
803Runtime::TrampolineType Runtime::GetTrampolineType(Method* method) {
804 if (method == NULL) {
805 return Runtime::kUnknownMethod;
806 } else if (method->IsStatic()) {
807 return Runtime::kStaticMethod;
808 } else {
809 return Runtime::kInstanceMethod;
810 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700811}
812
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700813bool Runtime::HasResolutionStubArray(TrampolineType type) const {
814 return resolution_stub_array_[type] != NULL;
Ian Rogersad25ac52011-10-04 19:13:33 -0700815}
816
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700817ByteArray* Runtime::GetResolutionStubArray(TrampolineType type) const {
818 CHECK(HasResolutionStubArray(type));
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700819 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastTrampolineMethodType));
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700820 return resolution_stub_array_[type];
821}
822
823void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700824 CHECK(resolution_stub_array != NULL);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700825 CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array);
826 resolution_stub_array_[type] = resolution_stub_array;
Ian Rogersad25ac52011-10-04 19:13:33 -0700827}
828
Brian Carlstromae826982011-11-09 01:33:42 -0800829Method* Runtime::CreateCalleeSaveMethod(InstructionSet instruction_set, CalleeSaveType type) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700830 Class* method_class = Method::GetMethodClass();
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700831 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
Ian Rogersff1ed472011-09-20 13:46:24 -0700832 method->SetDeclaringClass(method_class);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800833 // TODO: use a special method for callee saves
834 method->SetMethodIndex(DexFile::kDexNoIndex16);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700835 method->SetCode(NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800836 if ((instruction_set == kThumb2) || (instruction_set == kArm)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700837 uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6) | (1 << art::arm::R7) |
838 (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11);
839 uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3);
840 uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9);
841 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills :0) |
842 (type == kSaveAll ? all_spills :0) | (1 << art::arm::LR);
843 uint32_t fp_all_spills = (1 << art::arm::S0) | (1 << art::arm::S1) | (1 << art::arm::S2) |
844 (1 << art::arm::S3) | (1 << art::arm::S4) | (1 << art::arm::S5) |
845 (1 << art::arm::S6) | (1 << art::arm::S7) | (1 << art::arm::S8) |
846 (1 << art::arm::S9) | (1 << art::arm::S10) | (1 << art::arm::S11) |
847 (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) |
848 (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) |
849 (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) |
850 (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) |
851 (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) |
852 (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
853 (1 << art::arm::S30) | (1 << art::arm::S31);
854 uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
855 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
856 __builtin_popcount(fp_spills) /* fprs */ +
857 1 /* Method* */) * kPointerSize, kStackAlignment);
Ian Rogers15fdb8c2011-09-25 15:45:07 -0700858 method->SetFrameSizeInBytes(frame_size);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700859 method->SetCoreSpillMask(core_spills);
860 method->SetFpSpillMask(fp_spills);
Brian Carlstromae826982011-11-09 01:33:42 -0800861 } else if (instruction_set == kX86) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700862 method->SetFrameSizeInBytes(32);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700863 method->SetCoreSpillMask((1 << art::x86::EBX) | (1 << art::x86::EBP) | (1 << art::x86::ESI) |
Ian Rogersff1ed472011-09-20 13:46:24 -0700864 (1 << art::x86::EDI));
865 method->SetFpSpillMask(0);
866 } else {
867 UNIMPLEMENTED(FATAL);
868 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700869 return method.get();
Ian Rogersff1ed472011-09-20 13:46:24 -0700870}
871
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700872bool Runtime::HasCalleeSaveMethod(CalleeSaveType type) const {
873 return callee_save_method_[type] != NULL;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700874}
875
Brian Carlstrome24fa612011-09-29 00:53:55 -0700876// Returns a special method that describes all callee saves being spilled to the stack.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700877Method* Runtime::GetCalleeSaveMethod(CalleeSaveType type) const {
878 CHECK(HasCalleeSaveMethod(type));
879 return callee_save_method_[type];
Brian Carlstrome24fa612011-09-29 00:53:55 -0700880}
881
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700882void Runtime::SetCalleeSaveMethod(Method* method, CalleeSaveType type) {
883 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
884 callee_save_method_[type] = method;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700885}
886
jeffhao2692b572011-12-16 15:42:28 -0800887void Runtime::EnableMethodTracing(Trace* tracer) {
888 CHECK(!IsMethodTracingActive());
889 tracer_ = tracer;
890}
891
892void Runtime::DisableMethodTracing() {
893 CHECK(IsMethodTracingActive());
894 delete tracer_;
895 tracer_ = NULL;
896}
897
898bool Runtime::IsMethodTracingActive() const {
899 return (tracer_ != NULL);
900}
901
902Trace* Runtime::GetTracer() const {
903 CHECK(IsMethodTracingActive());
904 return tracer_;
905}
906
Carl Shapiro1fb86202011-06-27 17:43:13 -0700907} // namespace art