blob: 904d95914ea0cc42df21443a6e265ca4d394e382 [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) {
224 // TODO: OS_SHARED_LIB_FORMAT_STR
225 std::string mapped_name(StringPrintf("lib%s.so", name));
Elliott Hughes75770752011-08-24 17:52:38 -0700226 std::string reason;
227 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700228 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
229 << reason;
230 }
231}
232
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700233Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700234 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700235 bool compiler = false;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700236 const char* boot_class_path = getenv("BOOTCLASSPATH");
237 if (boot_class_path != NULL) {
Ian Rogers4f6b57a2012-01-17 16:23:27 -0800238 parsed->boot_class_path_ = boot_class_path;
239 } else {
240 parsed->boot_class_path_ = ".";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700241 }
242 const char* class_path = getenv("CLASSPATH");
243 if (class_path != NULL) {
Ian Rogers4f6b57a2012-01-17 16:23:27 -0800244 parsed->class_path_ = class_path;
245 } else {
246 parsed->class_path_ = ".";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700247 }
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700248#ifdef NDEBUG
Elliott Hughes5174fe62011-08-23 15:12:35 -0700249 // -Xcheck:jni is off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700250 parsed->check_jni_ = false;
251#else
252 // ...but on by default in debug builds.
253 parsed->check_jni_ = true;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700254#endif
Elliott Hughes85d15452011-09-16 17:33:01 -0700255
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700256 parsed->heap_initial_size_ = Heap::kInitialSize;
257 parsed->heap_maximum_size_ = Heap::kMaximumSize;
jeffhaoc1160702011-10-27 15:48:45 -0700258 parsed->heap_growth_limit_ = 0; // 0 means no growth limit
Brian Carlstromf734cf52011-08-17 16:28:14 -0700259 parsed->stack_size_ = Thread::kDefaultStackSize;
260
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700261 parsed->is_zygote_ = false;
262
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700263 parsed->jni_globals_max_ = 0;
Elliott Hughesfc861622011-10-17 17:57:47 -0700264 parsed->lock_profiling_threshold_ = 0;
265 parsed->hook_is_sensitive_thread_ = NULL;
266
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700267 parsed->hook_vfprintf_ = vfprintf;
268 parsed->hook_exit_ = exit;
269 parsed->hook_abort_ = abort;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700270
271 for (size_t i = 0; i < options.size(); ++i) {
272 const StringPiece& option = options[i].first;
Brian Carlstrom0796af02011-10-12 14:31:45 -0700273 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700274 LOG(INFO) << "option[" << i << "]=" << option;
275 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700276 if (option.starts_with("-Xbootclasspath:")) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700277 parsed->boot_class_path_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700278 } else if (option == "-classpath" || option == "-cp") {
279 // TODO: support -Djava.class.path
280 i++;
281 if (i == options.size()) {
282 // TODO: usage
283 LOG(FATAL) << "Missing required class path value for " << option;
284 return NULL;
285 }
286 const StringPiece& value = options[i].first;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700287 parsed->class_path_ = value.data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700288 } else if (option.starts_with("-Ximage:")) {
289 parsed->images_.push_back(option.substr(strlen("-Ximage:")).data());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700290 } else if (option.starts_with("-Xcheck:jni")) {
291 parsed->check_jni_ = true;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700292 } else if (option.starts_with("-Xrunjdwp:") || option.starts_with("-agentlib:jdwp=")) {
Elliott Hughes95572412011-12-13 18:14:20 -0800293 std::string tail(option.substr(option[1] == 'X' ? 10 : 15).ToString());
Elliott Hughes3bb81562011-10-21 18:52:59 -0700294 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
295 LOG(FATAL) << "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
296 << "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n";
297 return NULL;
298 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700299 } else if (option.starts_with("-Xms")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700300 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024);
301 if (size == 0) {
302 if (ignore_unrecognized) {
303 continue;
304 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700305 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700306 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700307 return NULL;
308 }
309 parsed->heap_initial_size_ = size;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700310 } else if (option.starts_with("-Xmx")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700311 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024);
312 if (size == 0) {
313 if (ignore_unrecognized) {
314 continue;
315 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700316 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700317 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700318 return NULL;
319 }
320 parsed->heap_maximum_size_ = size;
jeffhaoc1160702011-10-27 15:48:45 -0700321 } else if (option.starts_with("-XX:HeapGrowthLimit=")) {
322 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).data(), 1024);
323 if (size == 0) {
324 if (ignore_unrecognized) {
325 continue;
326 }
327 // TODO: usage
328 LOG(FATAL) << "Failed to parse " << option;
329 return NULL;
330 }
331 parsed->heap_growth_limit_ = size;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700332 } else if (option.starts_with("-Xss")) {
333 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1);
334 if (size == 0) {
335 if (ignore_unrecognized) {
336 continue;
337 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700338 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700339 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700340 return NULL;
341 }
342 parsed->stack_size_ = size;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700343 } else if (option.starts_with("-D")) {
344 parsed->properties_.push_back(option.substr(strlen("-D")).data());
Elliott Hughesa0957642011-09-02 14:27:33 -0700345 } else if (option.starts_with("-Xjnitrace:")) {
346 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:")).data();
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700347 } else if (option == "compiler") {
348 compiler = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700349 } else if (option == "-Xzygote") {
350 parsed->is_zygote_ = true;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700351 } else if (option.starts_with("-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700352 std::vector<std::string> verbose_options;
Elliott Hughes34023802011-08-30 12:06:17 -0700353 Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700354 for (size_t i = 0; i < verbose_options.size(); ++i) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800355 if (verbose_options[i] == "class") {
356 gLogVerbosity.class_linker = true;
357 } else if (verbose_options[i] == "compiler") {
358 gLogVerbosity.compiler = true;
359 } else if (verbose_options[i] == "heap") {
360 gLogVerbosity.heap = true;
361 } else if (verbose_options[i] == "gc") {
362 gLogVerbosity.gc = true;
363 } else if (verbose_options[i] == "jdwp") {
364 gLogVerbosity.jdwp = true;
365 } else if (verbose_options[i] == "jni") {
366 gLogVerbosity.jni = true;
367 } else if (verbose_options[i] == "monitor") {
368 gLogVerbosity.monitor = true;
369 } else if (verbose_options[i] == "startup") {
370 gLogVerbosity.startup = true;
371 } else if (verbose_options[i] == "third-party-jni") {
372 gLogVerbosity.third_party_jni = true;
373 } else if (verbose_options[i] == "threads") {
374 gLogVerbosity.threads = true;
375 } else {
376 LOG(WARNING) << "Ignoring unknown -verbose option: " << verbose_options[i];
377 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700378 }
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700379 } else if (option.starts_with("-Xjnigreflimit:")) {
380 parsed->jni_globals_max_ = ParseIntegerOrDie(option);
Elliott Hughesfc861622011-10-17 17:57:47 -0700381 } else if (option.starts_with("-Xlockprofthreshold:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700382 parsed->lock_profiling_threshold_ = ParseIntegerOrDie(option);
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700383 } else if (option.starts_with("-Xstacktracefile:")) {
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700384// always show stack traces in debug builds
385#ifdef NDEBUG
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700386 parsed->stack_trace_file_ = option.substr(strlen("-Xstacktracefile:")).data();
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700387#endif
Elliott Hughesfc861622011-10-17 17:57:47 -0700388 } else if (option == "sensitiveThread") {
389 parsed->hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(options[i].second);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700390 } else if (option == "vfprintf") {
391 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
392 } else if (option == "exit") {
393 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
394 } else if (option == "abort") {
395 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700396 } else if (option == "host-prefix") {
397 parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second);
Elliott Hughesb3e66df2012-01-12 14:49:18 -0800398 } else if (option == "-Xgenregmap" || option == "-Xgc:precise") {
399 // We silently ignore these for backwards compatibility.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700400 } else {
401 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700402 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700403 LOG(ERROR) << "Unrecognized option " << option;
404 // TODO: this should exit, but for now tolerate unknown options
405 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700406 }
407 }
408 }
409
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700410 if (!compiler && parsed->images_.empty()) {
Brian Carlstrom29e7ac72011-12-05 23:42:57 -0800411 parsed->images_.push_back("/system/framework/boot.art");
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700412 }
jeffhaoc1160702011-10-27 15:48:45 -0700413 if (parsed->heap_growth_limit_ == 0) {
414 parsed->heap_growth_limit_ = parsed->heap_maximum_size_;
415 }
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700416
Elliott Hughescac6cc72011-11-03 20:31:21 -0700417 LOG(INFO) << "Build type: "
418#ifndef NDEBUG
419 << "debug"
420#else
421 << "optimized"
422#endif
423 << "; CheckJNI: " << (parsed->check_jni_ ? "on" : "off");
Elliott Hughes85d15452011-09-16 17:33:01 -0700424
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700425 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700426}
427
428Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700429 // TODO: acquire a static mutex on Runtime to avoid racing.
430 if (Runtime::instance_ != NULL) {
431 return NULL;
432 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700433 instance_ = new Runtime;
434 if (!instance_->Init(options, ignore_unrecognized)) {
435 delete instance_;
436 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700437 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700438 return instance_;
439}
Elliott Hughes0af55432011-08-17 18:37:28 -0700440
Brian Carlstromaded5f72011-10-07 17:15:04 -0700441void CreateSystemClassLoader() {
442 if (ClassLoader::UseCompileTimeClassPath()) {
443 return;
444 }
445
446 Thread* self = Thread::Current();
447
448 // Must be in the kNative state for calling native methods.
449 CHECK_EQ(self->GetState(), Thread::kNative);
450
451 JNIEnv* env = self->GetJniEnv();
Brian Carlstromdf143242011-10-10 18:05:34 -0700452 ScopedLocalRef<jclass> ClassLoader_class(env, env->FindClass("java/lang/ClassLoader"));
453 CHECK(ClassLoader_class.get() != NULL);
454 jmethodID getSystemClassLoader = env->GetStaticMethodID(ClassLoader_class.get(),
455 "getSystemClassLoader",
456 "()Ljava/lang/ClassLoader;");
457 CHECK(getSystemClassLoader != NULL);
458 ScopedLocalRef<jobject> class_loader(env, env->CallStaticObjectMethod(ClassLoader_class.get(),
459 getSystemClassLoader));
460 CHECK(class_loader.get() != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700461
Brian Carlstromdf143242011-10-10 18:05:34 -0700462 Thread::Current()->SetClassLoaderOverride(Decode<ClassLoader*>(env, class_loader.get()));
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500463
464 ScopedLocalRef<jclass> Thread_class(env, env->FindClass("java/lang/Thread"));
465 CHECK(Thread_class.get() != NULL);
466 jfieldID contextClassLoader = env->GetFieldID(Thread_class.get(),
467 "contextClassLoader",
468 "Ljava/lang/ClassLoader;");
469 CHECK(contextClassLoader != NULL);
470 ScopedLocalRef<jobject> self_jobject(env, AddLocalReference<jobject>(env, self->GetPeer()));
471 env->SetObjectField(self_jobject.get(), contextClassLoader, class_loader.get());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700472}
473
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700474void Runtime::Start() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800475 VLOG(startup) << "Runtime::Start entering";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700476
477 CHECK(host_prefix_.empty()) << host_prefix_;
478
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700479 // Restore main thread state to kNative as expected by native code
480 Thread::Current()->SetState(Thread::kNative);
481
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700482 started_ = true;
483
Brian Carlstromae826982011-11-09 01:33:42 -0800484 // InitNativeMethods needs to be after started_ so that the classes
485 // it touches will have methods linked to the oat file if necessary.
486 InitNativeMethods();
487
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500488 Thread::FinishStartup();
489
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700490 if (!is_zygote_) {
491 DidForkFromZygote();
492 }
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700493
Elliott Hughes85d15452011-09-16 17:33:01 -0700494 StartDaemonThreads();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700495
Brian Carlstromaded5f72011-10-07 17:15:04 -0700496 CreateSystemClassLoader();
497
Elliott Hughes726079d2011-10-07 18:43:44 -0700498 Thread::Current()->GetJniEnv()->locals.AssertEmpty();
499
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800500 VLOG(startup) << "Runtime::Start exiting";
Elliott Hughes85d15452011-09-16 17:33:01 -0700501}
502
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700503void Runtime::DidForkFromZygote() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700504 is_zygote_ = false;
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700505
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700506 StartSignalCatcher();
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700507
508 // Start the JDWP thread. If the command-line debugger flags specified "suspend=y",
509 // this will pause the runtime, so we probably want this to come last.
510 Dbg::StartJdwp();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700511}
512
513void Runtime::StartSignalCatcher() {
514 if (!is_zygote_) {
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700515 signal_catcher_ = new SignalCatcher(stack_trace_file_);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700516 }
517}
518
Elliott Hughes85d15452011-09-16 17:33:01 -0700519void Runtime::StartDaemonThreads() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800520 VLOG(startup) << "Runtime::StartDaemonThreads entering";
Elliott Hughes85d15452011-09-16 17:33:01 -0700521
Elliott Hughes719b3232011-09-25 17:42:19 -0700522 Thread* self = Thread::Current();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700523
524 // Must be in the kNative state for calling native methods.
525 CHECK_EQ(self->GetState(), Thread::kNative);
526
Elliott Hughes719b3232011-09-25 17:42:19 -0700527 JNIEnv* env = self->GetJniEnv();
Elliott Hughes726079d2011-10-07 18:43:44 -0700528 ScopedLocalRef<jclass> c(env, env->FindClass("java/lang/Daemons"));
529 CHECK(c.get() != NULL);
530 jmethodID mid = env->GetStaticMethodID(c.get(), "start", "()V");
Elliott Hughes719b3232011-09-25 17:42:19 -0700531 CHECK(mid != NULL);
Elliott Hughes726079d2011-10-07 18:43:44 -0700532 env->CallStaticVoidMethod(c.get(), mid);
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700533
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800534 VLOG(startup) << "Runtime::StartDaemonThreads exiting";
Carl Shapiro61e019d2011-07-14 16:53:09 -0700535}
536
Elliott Hughes6b355752012-01-13 16:49:08 -0800537bool Runtime::IsShuttingDown() const {
538 return shutting_down_;
539}
540
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700541bool Runtime::IsStarted() const {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700542 return started_;
543}
544
Elliott Hughes0af55432011-08-17 18:37:28 -0700545bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700546 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700547
Elliott Hughes90a33692011-08-30 13:27:07 -0700548 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
549 if (options.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700550 LOG(ERROR) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700551 return false;
552 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800553 VLOG(startup) << "Runtime::Init -verbose:startup enabled";
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700554
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700555 SetJniGlobalsMax(options->jni_globals_max_);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800556 Monitor::Init(options->lock_profiling_threshold_, options->hook_is_sensitive_thread_);
Elliott Hughes32d6e1e2011-10-11 14:47:44 -0700557
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700558 host_prefix_ = options->host_prefix_;
559 boot_class_path_ = options->boot_class_path_;
560 class_path_ = options->class_path_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700561 properties_ = options->properties_;
562
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700563 is_zygote_ = options->is_zygote_;
564
Elliott Hughes0af55432011-08-17 18:37:28 -0700565 vfprintf_ = options->hook_vfprintf_;
566 exit_ = options->hook_exit_;
567 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700568
Elliott Hughesbe759c62011-09-08 19:38:21 -0700569 default_stack_size_ = options->stack_size_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700570 stack_trace_file_ = options->stack_trace_file_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700571
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700572 monitor_list_ = new MonitorList;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800573 thread_list_ = new ThreadList;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700574 intern_table_ = new InternTable;
575
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800576 Heap::Init(options->heap_initial_size_,
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700577 options->heap_maximum_size_,
jeffhaoc1160702011-10-27 15:48:45 -0700578 options->heap_growth_limit_,
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700579 options->images_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700580
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700581 BlockSignals();
582
Elliott Hughesa0957642011-09-02 14:27:33 -0700583 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700584
Elliott Hughesbe759c62011-09-08 19:38:21 -0700585 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700586
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700587 // ClassLinker needs an attached thread, but we can't fully attach a thread
588 // without creating objects.
589 Thread::Attach(this, "main", false);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700590
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700591 // Set us to runnable so tools using a runtime can allocate and GC by default
592 Thread::Current()->SetState(Thread::kRunnable);
593
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700594 CHECK_GE(Heap::GetSpaces().size(), 1U);
595 class_linker_ = ((Heap::GetSpaces()[0]->IsImageSpace())
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800596 ? ClassLinker::Create(intern_table_)
597 : ClassLinker::Create(options->boot_class_path_, intern_table_));
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700598
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800599 VLOG(startup) << "Runtime::Init exiting";
Carl Shapiro1fb86202011-06-27 17:43:13 -0700600 return true;
601}
602
Elliott Hughes038a8062011-09-18 14:12:41 -0700603void Runtime::InitNativeMethods() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800604 VLOG(startup) << "Runtime::InitNativeMethods entering";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700605 Thread* self = Thread::Current();
606 JNIEnv* env = self->GetJniEnv();
607
Elliott Hughes418d20f2011-09-22 14:00:39 -0700608 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Brian Carlstromaded5f72011-10-07 17:15:04 -0700609 CHECK_EQ(self->GetState(), Thread::kNative);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700610
Elliott Hughes418d20f2011-09-22 14:00:39 -0700611 // First set up JniConstants, which is used by both the runtime's built-in native
612 // methods and libcore.
Elliott Hughesfea966e2011-09-22 10:26:20 -0700613 JniConstants::init(env);
614
Elliott Hughes418d20f2011-09-22 14:00:39 -0700615 // Then set up the native methods provided by the runtime itself.
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700616 RegisterRuntimeNativeMethods(env);
617
Elliott Hughes418d20f2011-09-22 14:00:39 -0700618 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
619 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
620 // the library that implements System.loadLibrary!
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700621 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800622 VLOG(startup) << "Runtime::InitNativeMethods exiting";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700623}
624
625void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
626#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700627 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700628 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700629 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700630 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700631 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700632 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700633 REGISTER(register_java_lang_Object);
634 REGISTER(register_java_lang_Runtime);
635 REGISTER(register_java_lang_String);
636 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700637 REGISTER(register_java_lang_Thread);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700638 REGISTER(register_java_lang_Throwable);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700639 REGISTER(register_java_lang_VMClassLoader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700640 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700641 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700642 REGISTER(register_java_lang_reflect_Field);
643 REGISTER(register_java_lang_reflect_Method);
Jesse Wilson95caa792011-10-12 18:14:17 -0400644 REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700645 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700646 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700647 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700648 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700649#undef REGISTER
650}
651
Elliott Hughes8daa0922011-09-11 13:46:25 -0700652void Runtime::Dump(std::ostream& os) {
Elliott Hughese27955c2011-08-26 15:21:24 -0700653 // TODO: dump other runtime statistics?
Elliott Hughescac6cc72011-11-03 20:31:21 -0700654 GetClassLinker()->DumpForSigQuit(os);
655 GetInternTable()->DumpForSigQuit(os);
Elliott Hughes42ee1422011-09-06 12:33:32 -0700656 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700657
658 thread_list_->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700659}
660
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800661void Runtime::DumpLockHolders(std::ostream& os) {
662 pid_t heap_lock_owner = Heap::GetLockOwner();
663 pid_t thread_list_lock_owner = GetThreadList()->GetLockOwner();
664 pid_t classes_lock_owner = GetClassLinker()->GetClassesLockOwner();
665 pid_t dex_lock_owner = GetClassLinker()->GetDexLockOwner();
666 if ((heap_lock_owner | thread_list_lock_owner | classes_lock_owner | dex_lock_owner) != 0) {
667 os << "Heap lock owner tid: " << heap_lock_owner << "\n"
668 << "ThreadList lock owner tid: " << thread_list_lock_owner << "\n"
669 << "ClassLinker classes lock owner tid: " << classes_lock_owner << "\n"
670 << "ClassLinker dex lock owner tid: " << dex_lock_owner << "\n";
671 }
672}
673
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700674void Runtime::SetStatsEnabled(bool new_state) {
675 if (new_state == true) {
676 GetStats()->Clear(~0);
677 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
678 Thread::Current()->GetStats()->Clear(~0);
679 }
680 stats_enabled_ = new_state;
681}
682
683void Runtime::ResetStats(int kinds) {
684 GetStats()->Clear(kinds & 0xffff);
685 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
686 Thread::Current()->GetStats()->Clear(kinds >> 16);
687}
688
689RuntimeStats* Runtime::GetStats() {
690 return &stats_;
691}
692
693int32_t Runtime::GetStat(int kind) {
694 RuntimeStats* stats;
695 if (kind < (1<<16)) {
696 stats = GetStats();
697 } else {
698 stats = Thread::Current()->GetStats();
699 kind >>= 16;
700 }
701 switch (kind) {
702 case KIND_ALLOCATED_OBJECTS:
703 return stats->allocated_objects;
704 case KIND_ALLOCATED_BYTES:
705 return stats->allocated_bytes;
706 case KIND_FREED_OBJECTS:
707 return stats->freed_objects;
708 case KIND_FREED_BYTES:
709 return stats->freed_bytes;
710 case KIND_GC_INVOCATIONS:
711 return stats->gc_for_alloc_count;
712 case KIND_CLASS_INIT_COUNT:
713 return stats->class_init_count;
714 case KIND_CLASS_INIT_TIME:
715 // Convert ns to us, reduce to 32 bits.
716 return (int) (stats->class_init_time_ns / 1000);
717 case KIND_EXT_ALLOCATED_OBJECTS:
718 case KIND_EXT_ALLOCATED_BYTES:
719 case KIND_EXT_FREED_OBJECTS:
720 case KIND_EXT_FREED_BYTES:
721 return 0; // backward compatibility
722 default:
723 CHECK(false);
724 return -1; // unreachable
725 }
726}
727
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700728void Runtime::BlockSignals() {
729 sigset_t sigset;
730 if (sigemptyset(&sigset) == -1) {
731 PLOG(FATAL) << "sigemptyset failed";
732 }
733 if (sigaddset(&sigset, SIGPIPE) == -1) {
734 PLOG(ERROR) << "sigaddset SIGPIPE failed";
735 }
736 // SIGQUIT is used to dump the runtime's state (including stack traces).
737 if (sigaddset(&sigset, SIGQUIT) == -1) {
738 PLOG(ERROR) << "sigaddset SIGQUIT failed";
739 }
740 // SIGUSR1 is used to initiate a heap dump.
741 if (sigaddset(&sigset, SIGUSR1) == -1) {
742 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
743 }
744 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
745}
746
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700747void Runtime::AttachCurrentThread(const char* name, bool as_daemon) {
748 Thread::Attach(instance_, name, as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700749}
750
Elliott Hughesd92bec42011-09-02 17:04:36 -0700751void Runtime::DetachCurrentThread() {
Elliott Hughes038a8062011-09-18 14:12:41 -0700752 // TODO: check we're not calling DetachCurrentThread from a call stack that
753 // includes managed frames. (It's only valid if the stack is all-native.)
Elliott Hughes02b48d12011-09-07 17:15:51 -0700754 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700755}
756
Brian Carlstrome24fa612011-09-29 00:53:55 -0700757void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700758 Dbg::VisitRoots(visitor, arg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700759 class_linker_->VisitRoots(visitor, arg);
760 intern_table_->VisitRoots(visitor, arg);
761 java_vm_->VisitRoots(visitor, arg);
762 thread_list_->VisitRoots(visitor, arg);
763 visitor(jni_stub_array_, arg);
764 visitor(abstract_method_error_stub_array_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700765 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
766 visitor(resolution_stub_array_[i], arg);
767 }
768 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
769 visitor(callee_save_method_[i], arg);
770 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700771}
772
Ian Rogers169c9a72011-11-13 20:13:17 -0800773bool Runtime::HasJniDlsymLookupStub() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700774 return jni_stub_array_ != NULL;
775}
776
Ian Rogers169c9a72011-11-13 20:13:17 -0800777ByteArray* Runtime::GetJniDlsymLookupStub() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700778 CHECK(jni_stub_array_ != NULL);
779 return jni_stub_array_;
780}
781
Ian Rogers169c9a72011-11-13 20:13:17 -0800782void Runtime::SetJniDlsymLookupStub(ByteArray* jni_stub_array) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700783 CHECK(jni_stub_array != NULL) << " jni_stub_array=" << jni_stub_array;
784 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array)
785 << "jni_stub_array_=" << jni_stub_array_ << " jni_stub_array=" << jni_stub_array;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700786 jni_stub_array_ = jni_stub_array;
787}
788
789bool Runtime::HasAbstractMethodErrorStubArray() const {
790 return abstract_method_error_stub_array_ != NULL;
791}
792
793ByteArray* Runtime::GetAbstractMethodErrorStubArray() const {
794 CHECK(abstract_method_error_stub_array_ != NULL);
795 return abstract_method_error_stub_array_;
796}
797
798void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) {
799 CHECK(abstract_method_error_stub_array != NULL);
800 CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array);
801 abstract_method_error_stub_array_ = abstract_method_error_stub_array;
802}
803
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700804
805Runtime::TrampolineType Runtime::GetTrampolineType(Method* method) {
806 if (method == NULL) {
807 return Runtime::kUnknownMethod;
808 } else if (method->IsStatic()) {
809 return Runtime::kStaticMethod;
810 } else {
811 return Runtime::kInstanceMethod;
812 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700813}
814
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700815bool Runtime::HasResolutionStubArray(TrampolineType type) const {
816 return resolution_stub_array_[type] != NULL;
Ian Rogersad25ac52011-10-04 19:13:33 -0700817}
818
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700819ByteArray* Runtime::GetResolutionStubArray(TrampolineType type) const {
820 CHECK(HasResolutionStubArray(type));
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700821 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastTrampolineMethodType));
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700822 return resolution_stub_array_[type];
823}
824
825void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700826 CHECK(resolution_stub_array != NULL);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700827 CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array);
828 resolution_stub_array_[type] = resolution_stub_array;
Ian Rogersad25ac52011-10-04 19:13:33 -0700829}
830
Brian Carlstromae826982011-11-09 01:33:42 -0800831Method* Runtime::CreateCalleeSaveMethod(InstructionSet instruction_set, CalleeSaveType type) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700832 Class* method_class = Method::GetMethodClass();
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700833 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
Ian Rogersff1ed472011-09-20 13:46:24 -0700834 method->SetDeclaringClass(method_class);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800835 // TODO: use a special method for callee saves
836 method->SetMethodIndex(DexFile::kDexNoIndex16);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700837 method->SetCode(NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800838 if ((instruction_set == kThumb2) || (instruction_set == kArm)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700839 uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6) | (1 << art::arm::R7) |
840 (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11);
841 uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3);
842 uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9);
843 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills :0) |
844 (type == kSaveAll ? all_spills :0) | (1 << art::arm::LR);
845 uint32_t fp_all_spills = (1 << art::arm::S0) | (1 << art::arm::S1) | (1 << art::arm::S2) |
846 (1 << art::arm::S3) | (1 << art::arm::S4) | (1 << art::arm::S5) |
847 (1 << art::arm::S6) | (1 << art::arm::S7) | (1 << art::arm::S8) |
848 (1 << art::arm::S9) | (1 << art::arm::S10) | (1 << art::arm::S11) |
849 (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) |
850 (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) |
851 (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) |
852 (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) |
853 (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) |
854 (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
855 (1 << art::arm::S30) | (1 << art::arm::S31);
856 uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
857 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
858 __builtin_popcount(fp_spills) /* fprs */ +
859 1 /* Method* */) * kPointerSize, kStackAlignment);
Ian Rogers15fdb8c2011-09-25 15:45:07 -0700860 method->SetFrameSizeInBytes(frame_size);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700861 method->SetCoreSpillMask(core_spills);
862 method->SetFpSpillMask(fp_spills);
Brian Carlstromae826982011-11-09 01:33:42 -0800863 } else if (instruction_set == kX86) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700864 method->SetFrameSizeInBytes(32);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700865 method->SetCoreSpillMask((1 << art::x86::EBX) | (1 << art::x86::EBP) | (1 << art::x86::ESI) |
Ian Rogersff1ed472011-09-20 13:46:24 -0700866 (1 << art::x86::EDI));
867 method->SetFpSpillMask(0);
868 } else {
869 UNIMPLEMENTED(FATAL);
870 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700871 return method.get();
Ian Rogersff1ed472011-09-20 13:46:24 -0700872}
873
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700874bool Runtime::HasCalleeSaveMethod(CalleeSaveType type) const {
875 return callee_save_method_[type] != NULL;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700876}
877
Brian Carlstrome24fa612011-09-29 00:53:55 -0700878// Returns a special method that describes all callee saves being spilled to the stack.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700879Method* Runtime::GetCalleeSaveMethod(CalleeSaveType type) const {
880 CHECK(HasCalleeSaveMethod(type));
881 return callee_save_method_[type];
Brian Carlstrome24fa612011-09-29 00:53:55 -0700882}
883
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700884void Runtime::SetCalleeSaveMethod(Method* method, CalleeSaveType type) {
885 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
886 callee_save_method_[type] = method;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700887}
888
jeffhao2692b572011-12-16 15:42:28 -0800889void Runtime::EnableMethodTracing(Trace* tracer) {
890 CHECK(!IsMethodTracingActive());
891 tracer_ = tracer;
892}
893
894void Runtime::DisableMethodTracing() {
895 CHECK(IsMethodTracingActive());
896 delete tracer_;
897 tracer_ = NULL;
898}
899
900bool Runtime::IsMethodTracingActive() const {
901 return (tracer_ != NULL);
902}
903
904Trace* Runtime::GetTracer() const {
905 CHECK(IsMethodTracingActive());
906 return tracer_;
907}
908
Carl Shapiro1fb86202011-06-27 17:43:13 -0700909} // namespace art