blob: e9d5677aab8580db7bc374bfc6789c0ebfb24844 [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 Hughesdcc24742011-09-07 14:02:44 -070046 started_(false),
47 vfprintf_(NULL),
48 exit_(NULL),
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070049 abort_(NULL),
jeffhao2692b572011-12-16 15:42:28 -080050 stats_enabled_(false),
51 tracer_(NULL) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -070052 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
53 resolution_stub_array_[i] = NULL;
54 }
55 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
56 callee_save_method_[i] = NULL;
57 }
Elliott Hughesdcc24742011-09-07 14:02:44 -070058}
59
Carl Shapiro61e019d2011-07-14 16:53:09 -070060Runtime::~Runtime() {
Elliott Hughesd1cc8362011-10-24 16:58:50 -070061 Dbg::StopJdwp();
62
Elliott Hughes5fe594f2011-09-08 12:33:17 -070063 // Make sure our internal threads are dead before we start tearing down things they're using.
64 delete signal_catcher_;
Elliott Hughes038a8062011-09-18 14:12:41 -070065 // TODO: GC thread.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070066
Elliott Hughes038a8062011-09-18 14:12:41 -070067 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
Elliott Hughes93e74e82011-09-13 11:07:03 -070068 delete thread_list_;
Elliott Hughesc33a32b2011-10-11 18:18:07 -070069 delete monitor_list_;
Elliott Hughes93e74e82011-09-13 11:07:03 -070070
Carl Shapiro61e019d2011-07-14 16:53:09 -070071 delete class_linker_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070072 Heap::Destroy();
Elliott Hughescf4c6c42011-09-01 15:16:42 -070073 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070074 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -070075 Thread::Shutdown();
Carl Shapiro4acf4642011-07-26 18:54:13 -070076 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070077 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -070078 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -070079}
80
Elliott Hughescac6cc72011-11-03 20:31:21 -070081static bool gAborting = false;
82
Elliott Hughese0918552011-10-28 17:18:29 -070083struct AbortState {
84 void Dump(std::ostream& os) {
Elliott Hughescac6cc72011-11-03 20:31:21 -070085 if (gAborting) {
86 os << "Runtime aborting --- recursively, so no thread-specific detail!\n";
87 return;
88 }
89 gAborting = true;
Elliott Hughese0918552011-10-28 17:18:29 -070090 os << "Runtime aborting...\n";
91 Thread* self = Thread::Current();
92 if (self == NULL) {
93 os << "(Aborting thread was not attached to runtime!)\n";
94 } else {
95 self->Dump(os, true);
96 }
97 }
98};
99
Elliott Hughesffe67362011-07-17 12:09:27 -0700100void Runtime::Abort(const char* file, int line) {
101 // Get any pending output out of the way.
102 fflush(NULL);
103
104 // Many people have difficulty distinguish aborts from crashes,
105 // so be explicit.
Elliott Hughese0918552011-10-28 17:18:29 -0700106 AbortState state;
107 LOG(ERROR) << Dumpable<AbortState>(state);
Elliott Hughesffe67362011-07-17 12:09:27 -0700108
Elliott Hughesffe67362011-07-17 12:09:27 -0700109 // Perform any platform-specific pre-abort actions.
110 PlatformAbort(file, line);
111
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700112 // use abort hook if we have one
113 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
114 Runtime::Current()->abort_();
115 // notreached
116 }
117
Elliott Hughesffe67362011-07-17 12:09:27 -0700118 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -0700119 // receive SIGABRT. debuggerd dumps the stack trace of the main
120 // thread, whether or not that was the thread that failed. By
121 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -0700122 // fault in the current thread, and get a useful log from debuggerd.
123 // We can also trivially tell the difference between a VM crash and
124 // a deliberate abort by looking at the fault address.
125 *reinterpret_cast<char*>(0xdeadd00d) = 38;
126 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -0700127 // notreached
128}
129
Elliott Hughesbf86d042011-08-31 17:53:14 -0700130void Runtime::CallExitHook(jint status) {
131 if (exit_ != NULL) {
132 ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative);
133 exit_(status);
134 LOG(WARNING) << "Exit hook returned instead of exiting!";
135 }
136}
137
Brian Carlstrom8a436592011-08-15 21:27:23 -0700138// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
139// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
140// [gG] gigabytes.
141//
142// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700143// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
144// of 1024.
145//
146// The spec says the -Xmx and -Xms options must be multiples of 1024. It
147// doesn't say anything about -Xss.
148//
149// Returns 0 (a useless size) if "s" is malformed or specifies a low or
150// non-evenly-divisible value.
151//
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800152size_t ParseMemoryOption(const char* s, size_t div) {
Brian Carlstrom8a436592011-08-15 21:27:23 -0700153 // strtoul accepts a leading [+-], which we don't want,
154 // so make sure our string starts with a decimal digit.
155 if (isdigit(*s)) {
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800156 const char* s2;
157 size_t val = strtoul(s, (char**)&s2, 10);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700158 if (s2 != s) {
159 // s2 should be pointing just after the number.
160 // If this is the end of the string, the user
161 // has specified a number of bytes. Otherwise,
162 // there should be exactly one more character
163 // that specifies a multiplier.
164 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700165 // The remainder of the string is either a single multiplier
166 // character, or nothing to indicate that the value is in
167 // bytes.
168 char c = *s2++;
169 if (*s2 == '\0') {
170 size_t mul;
171 if (c == '\0') {
172 mul = 1;
173 } else if (c == 'k' || c == 'K') {
174 mul = KB;
175 } else if (c == 'm' || c == 'M') {
176 mul = MB;
177 } else if (c == 'g' || c == 'G') {
178 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700179 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700180 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700181 return 0;
182 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700183
184 if (val <= std::numeric_limits<size_t>::max() / mul) {
185 val *= mul;
186 } else {
187 // Clamp to a multiple of 1024.
188 val = std::numeric_limits<size_t>::max() & ~(1024-1);
189 }
190 } else {
191 // There's more than one character after the numeric part.
192 return 0;
193 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700194 }
195 // The man page says that a -Xm value must be a multiple of 1024.
196 if (val % div == 0) {
197 return val;
198 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700199 }
200 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700201 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700202}
203
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700204size_t ParseIntegerOrDie(const StringPiece& s) {
205 StringPiece::size_type colon = s.find(':');
206 if (colon == StringPiece::npos) {
207 LOG(FATAL) << "Missing integer: " << s;
208 }
209 const char* begin = &s.data()[colon + 1];
210 char* end;
211 size_t result = strtoul(begin, &end, 10);
212 if (begin == end || *end != '\0') {
213 LOG(FATAL) << "Failed to parse integer in: " << s;
214 }
215 return result;
216}
217
Elliott Hughes0af55432011-08-17 18:37:28 -0700218void LoadJniLibrary(JavaVMExt* vm, const char* name) {
219 // TODO: OS_SHARED_LIB_FORMAT_STR
220 std::string mapped_name(StringPrintf("lib%s.so", name));
Elliott Hughes75770752011-08-24 17:52:38 -0700221 std::string reason;
222 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700223 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
224 << reason;
225 }
226}
227
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700228Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700229 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700230 bool compiler = false;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700231 const char* boot_class_path = getenv("BOOTCLASSPATH");
232 if (boot_class_path != NULL) {
233 parsed->boot_class_path_ = getenv("BOOTCLASSPATH");
234 }
235 const char* class_path = getenv("CLASSPATH");
236 if (class_path != NULL) {
237 parsed->class_path_ = getenv("CLASSPATH");
238 }
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700239#ifdef NDEBUG
Elliott Hughes5174fe62011-08-23 15:12:35 -0700240 // -Xcheck:jni is off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700241 parsed->check_jni_ = false;
242#else
243 // ...but on by default in debug builds.
244 parsed->check_jni_ = true;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700245#endif
Elliott Hughes85d15452011-09-16 17:33:01 -0700246
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700247 parsed->heap_initial_size_ = Heap::kInitialSize;
248 parsed->heap_maximum_size_ = Heap::kMaximumSize;
jeffhaoc1160702011-10-27 15:48:45 -0700249 parsed->heap_growth_limit_ = 0; // 0 means no growth limit
Brian Carlstromf734cf52011-08-17 16:28:14 -0700250 parsed->stack_size_ = Thread::kDefaultStackSize;
251
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700252 parsed->is_zygote_ = false;
253
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700254 parsed->jni_globals_max_ = 0;
Elliott Hughesfc861622011-10-17 17:57:47 -0700255 parsed->lock_profiling_threshold_ = 0;
256 parsed->hook_is_sensitive_thread_ = NULL;
257
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700258 parsed->hook_vfprintf_ = vfprintf;
259 parsed->hook_exit_ = exit;
260 parsed->hook_abort_ = abort;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700261
262 for (size_t i = 0; i < options.size(); ++i) {
263 const StringPiece& option = options[i].first;
Brian Carlstrom0796af02011-10-12 14:31:45 -0700264 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700265 LOG(INFO) << "option[" << i << "]=" << option;
266 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700267 if (option.starts_with("-Xbootclasspath:")) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700268 parsed->boot_class_path_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700269 } else if (option == "-classpath" || option == "-cp") {
270 // TODO: support -Djava.class.path
271 i++;
272 if (i == options.size()) {
273 // TODO: usage
274 LOG(FATAL) << "Missing required class path value for " << option;
275 return NULL;
276 }
277 const StringPiece& value = options[i].first;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700278 parsed->class_path_ = value.data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700279 } else if (option.starts_with("-Ximage:")) {
280 parsed->images_.push_back(option.substr(strlen("-Ximage:")).data());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700281 } else if (option.starts_with("-Xcheck:jni")) {
282 parsed->check_jni_ = true;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700283 } else if (option.starts_with("-Xrunjdwp:") || option.starts_with("-agentlib:jdwp=")) {
Elliott Hughes95572412011-12-13 18:14:20 -0800284 std::string tail(option.substr(option[1] == 'X' ? 10 : 15).ToString());
Elliott Hughes3bb81562011-10-21 18:52:59 -0700285 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
286 LOG(FATAL) << "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
287 << "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n";
288 return NULL;
289 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700290 } else if (option.starts_with("-Xms")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700291 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024);
292 if (size == 0) {
293 if (ignore_unrecognized) {
294 continue;
295 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700296 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700297 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700298 return NULL;
299 }
300 parsed->heap_initial_size_ = size;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700301 } else if (option.starts_with("-Xmx")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700302 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024);
303 if (size == 0) {
304 if (ignore_unrecognized) {
305 continue;
306 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700307 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700308 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700309 return NULL;
310 }
311 parsed->heap_maximum_size_ = size;
jeffhaoc1160702011-10-27 15:48:45 -0700312 } else if (option.starts_with("-XX:HeapGrowthLimit=")) {
313 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).data(), 1024);
314 if (size == 0) {
315 if (ignore_unrecognized) {
316 continue;
317 }
318 // TODO: usage
319 LOG(FATAL) << "Failed to parse " << option;
320 return NULL;
321 }
322 parsed->heap_growth_limit_ = size;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700323 } else if (option.starts_with("-Xss")) {
324 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1);
325 if (size == 0) {
326 if (ignore_unrecognized) {
327 continue;
328 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700329 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700330 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700331 return NULL;
332 }
333 parsed->stack_size_ = size;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700334 } else if (option.starts_with("-D")) {
335 parsed->properties_.push_back(option.substr(strlen("-D")).data());
Elliott Hughesa0957642011-09-02 14:27:33 -0700336 } else if (option.starts_with("-Xjnitrace:")) {
337 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:")).data();
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700338 } else if (option == "compiler") {
339 compiler = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700340 } else if (option == "-Xzygote") {
341 parsed->is_zygote_ = true;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700342 } else if (option.starts_with("-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700343 std::vector<std::string> verbose_options;
Elliott Hughes34023802011-08-30 12:06:17 -0700344 Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700345 for (size_t i = 0; i < verbose_options.size(); ++i) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800346 if (verbose_options[i] == "class") {
347 gLogVerbosity.class_linker = true;
348 } else if (verbose_options[i] == "compiler") {
349 gLogVerbosity.compiler = true;
350 } else if (verbose_options[i] == "heap") {
351 gLogVerbosity.heap = true;
352 } else if (verbose_options[i] == "gc") {
353 gLogVerbosity.gc = true;
354 } else if (verbose_options[i] == "jdwp") {
355 gLogVerbosity.jdwp = true;
356 } else if (verbose_options[i] == "jni") {
357 gLogVerbosity.jni = true;
358 } else if (verbose_options[i] == "monitor") {
359 gLogVerbosity.monitor = true;
360 } else if (verbose_options[i] == "startup") {
361 gLogVerbosity.startup = true;
362 } else if (verbose_options[i] == "third-party-jni") {
363 gLogVerbosity.third_party_jni = true;
364 } else if (verbose_options[i] == "threads") {
365 gLogVerbosity.threads = true;
366 } else {
367 LOG(WARNING) << "Ignoring unknown -verbose option: " << verbose_options[i];
368 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700369 }
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700370 } else if (option.starts_with("-Xjnigreflimit:")) {
371 parsed->jni_globals_max_ = ParseIntegerOrDie(option);
Elliott Hughesfc861622011-10-17 17:57:47 -0700372 } else if (option.starts_with("-Xlockprofthreshold:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700373 parsed->lock_profiling_threshold_ = ParseIntegerOrDie(option);
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700374 } else if (option.starts_with("-Xstacktracefile:")) {
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700375// always show stack traces in debug builds
376#ifdef NDEBUG
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700377 parsed->stack_trace_file_ = option.substr(strlen("-Xstacktracefile:")).data();
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700378#endif
Elliott Hughesfc861622011-10-17 17:57:47 -0700379 } else if (option == "sensitiveThread") {
380 parsed->hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(options[i].second);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700381 } else if (option == "vfprintf") {
382 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
383 } else if (option == "exit") {
384 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
385 } else if (option == "abort") {
386 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700387 } else if (option == "host-prefix") {
388 parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second);
Elliott Hughesb3e66df2012-01-12 14:49:18 -0800389 } else if (option == "-Xgenregmap" || option == "-Xgc:precise") {
390 // We silently ignore these for backwards compatibility.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700391 } else {
392 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700393 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700394 LOG(ERROR) << "Unrecognized option " << option;
395 // TODO: this should exit, but for now tolerate unknown options
396 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700397 }
398 }
399 }
400
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700401 if (!compiler && parsed->images_.empty()) {
Brian Carlstrom29e7ac72011-12-05 23:42:57 -0800402 parsed->images_.push_back("/system/framework/boot.art");
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700403 }
jeffhaoc1160702011-10-27 15:48:45 -0700404 if (parsed->heap_growth_limit_ == 0) {
405 parsed->heap_growth_limit_ = parsed->heap_maximum_size_;
406 }
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700407
Elliott Hughescac6cc72011-11-03 20:31:21 -0700408 LOG(INFO) << "Build type: "
409#ifndef NDEBUG
410 << "debug"
411#else
412 << "optimized"
413#endif
414 << "; CheckJNI: " << (parsed->check_jni_ ? "on" : "off");
Elliott Hughes85d15452011-09-16 17:33:01 -0700415
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700416 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700417}
418
419Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700420 // TODO: acquire a static mutex on Runtime to avoid racing.
421 if (Runtime::instance_ != NULL) {
422 return NULL;
423 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700424 instance_ = new Runtime;
425 if (!instance_->Init(options, ignore_unrecognized)) {
426 delete instance_;
427 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700428 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700429 return instance_;
430}
Elliott Hughes0af55432011-08-17 18:37:28 -0700431
Brian Carlstromaded5f72011-10-07 17:15:04 -0700432void CreateSystemClassLoader() {
433 if (ClassLoader::UseCompileTimeClassPath()) {
434 return;
435 }
436
437 Thread* self = Thread::Current();
438
439 // Must be in the kNative state for calling native methods.
440 CHECK_EQ(self->GetState(), Thread::kNative);
441
442 JNIEnv* env = self->GetJniEnv();
Brian Carlstromdf143242011-10-10 18:05:34 -0700443 ScopedLocalRef<jclass> ClassLoader_class(env, env->FindClass("java/lang/ClassLoader"));
444 CHECK(ClassLoader_class.get() != NULL);
445 jmethodID getSystemClassLoader = env->GetStaticMethodID(ClassLoader_class.get(),
446 "getSystemClassLoader",
447 "()Ljava/lang/ClassLoader;");
448 CHECK(getSystemClassLoader != NULL);
449 ScopedLocalRef<jobject> class_loader(env, env->CallStaticObjectMethod(ClassLoader_class.get(),
450 getSystemClassLoader));
451 CHECK(class_loader.get() != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700452
Brian Carlstromdf143242011-10-10 18:05:34 -0700453 Thread::Current()->SetClassLoaderOverride(Decode<ClassLoader*>(env, class_loader.get()));
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500454
455 ScopedLocalRef<jclass> Thread_class(env, env->FindClass("java/lang/Thread"));
456 CHECK(Thread_class.get() != NULL);
457 jfieldID contextClassLoader = env->GetFieldID(Thread_class.get(),
458 "contextClassLoader",
459 "Ljava/lang/ClassLoader;");
460 CHECK(contextClassLoader != NULL);
461 ScopedLocalRef<jobject> self_jobject(env, AddLocalReference<jobject>(env, self->GetPeer()));
462 env->SetObjectField(self_jobject.get(), contextClassLoader, class_loader.get());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700463}
464
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700465void Runtime::Start() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800466 VLOG(startup) << "Runtime::Start entering";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700467
468 CHECK(host_prefix_.empty()) << host_prefix_;
469
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700470 // Restore main thread state to kNative as expected by native code
471 Thread::Current()->SetState(Thread::kNative);
472
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700473 started_ = true;
474
Brian Carlstromae826982011-11-09 01:33:42 -0800475 // InitNativeMethods needs to be after started_ so that the classes
476 // it touches will have methods linked to the oat file if necessary.
477 InitNativeMethods();
478
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500479 Thread::FinishStartup();
480
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700481 if (!is_zygote_) {
482 DidForkFromZygote();
483 }
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700484
Elliott Hughes85d15452011-09-16 17:33:01 -0700485 StartDaemonThreads();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700486
Brian Carlstromaded5f72011-10-07 17:15:04 -0700487 CreateSystemClassLoader();
488
Elliott Hughes726079d2011-10-07 18:43:44 -0700489 Thread::Current()->GetJniEnv()->locals.AssertEmpty();
490
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800491 VLOG(startup) << "Runtime::Start exiting";
Elliott Hughes85d15452011-09-16 17:33:01 -0700492}
493
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700494void Runtime::DidForkFromZygote() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700495 is_zygote_ = false;
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700496
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700497 StartSignalCatcher();
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700498
499 // Start the JDWP thread. If the command-line debugger flags specified "suspend=y",
500 // this will pause the runtime, so we probably want this to come last.
501 Dbg::StartJdwp();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700502}
503
504void Runtime::StartSignalCatcher() {
505 if (!is_zygote_) {
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700506 signal_catcher_ = new SignalCatcher(stack_trace_file_);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700507 }
508}
509
Elliott Hughes85d15452011-09-16 17:33:01 -0700510void Runtime::StartDaemonThreads() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800511 VLOG(startup) << "Runtime::StartDaemonThreads entering";
Elliott Hughes85d15452011-09-16 17:33:01 -0700512
Elliott Hughes719b3232011-09-25 17:42:19 -0700513 Thread* self = Thread::Current();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700514
515 // Must be in the kNative state for calling native methods.
516 CHECK_EQ(self->GetState(), Thread::kNative);
517
Elliott Hughes719b3232011-09-25 17:42:19 -0700518 JNIEnv* env = self->GetJniEnv();
Elliott Hughes726079d2011-10-07 18:43:44 -0700519 ScopedLocalRef<jclass> c(env, env->FindClass("java/lang/Daemons"));
520 CHECK(c.get() != NULL);
521 jmethodID mid = env->GetStaticMethodID(c.get(), "start", "()V");
Elliott Hughes719b3232011-09-25 17:42:19 -0700522 CHECK(mid != NULL);
Elliott Hughes726079d2011-10-07 18:43:44 -0700523 env->CallStaticVoidMethod(c.get(), mid);
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700524
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800525 VLOG(startup) << "Runtime::StartDaemonThreads exiting";
Carl Shapiro61e019d2011-07-14 16:53:09 -0700526}
527
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700528bool Runtime::IsStarted() const {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700529 return started_;
530}
531
Elliott Hughes0af55432011-08-17 18:37:28 -0700532bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700533 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700534
Elliott Hughes90a33692011-08-30 13:27:07 -0700535 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
536 if (options.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700537 LOG(ERROR) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700538 return false;
539 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800540 VLOG(startup) << "Runtime::Init -verbose:startup enabled";
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700541
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700542 SetJniGlobalsMax(options->jni_globals_max_);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800543 Monitor::Init(options->lock_profiling_threshold_, options->hook_is_sensitive_thread_);
Elliott Hughes32d6e1e2011-10-11 14:47:44 -0700544
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700545 host_prefix_ = options->host_prefix_;
546 boot_class_path_ = options->boot_class_path_;
547 class_path_ = options->class_path_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700548 properties_ = options->properties_;
549
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700550 is_zygote_ = options->is_zygote_;
551
Elliott Hughes0af55432011-08-17 18:37:28 -0700552 vfprintf_ = options->hook_vfprintf_;
553 exit_ = options->hook_exit_;
554 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700555
Elliott Hughesbe759c62011-09-08 19:38:21 -0700556 default_stack_size_ = options->stack_size_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700557 stack_trace_file_ = options->stack_trace_file_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700558
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700559 monitor_list_ = new MonitorList;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800560 thread_list_ = new ThreadList;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700561 intern_table_ = new InternTable;
562
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800563 Heap::Init(options->heap_initial_size_,
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700564 options->heap_maximum_size_,
jeffhaoc1160702011-10-27 15:48:45 -0700565 options->heap_growth_limit_,
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700566 options->images_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700567
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700568 BlockSignals();
569
Elliott Hughesa0957642011-09-02 14:27:33 -0700570 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700571
Elliott Hughesbe759c62011-09-08 19:38:21 -0700572 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700573
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700574 // ClassLinker needs an attached thread, but we can't fully attach a thread
575 // without creating objects.
576 Thread::Attach(this, "main", false);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700577
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700578 // Set us to runnable so tools using a runtime can allocate and GC by default
579 Thread::Current()->SetState(Thread::kRunnable);
580
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700581 CHECK_GE(Heap::GetSpaces().size(), 1U);
582 class_linker_ = ((Heap::GetSpaces()[0]->IsImageSpace())
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800583 ? ClassLinker::Create(intern_table_)
584 : ClassLinker::Create(options->boot_class_path_, intern_table_));
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700585
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800586 VLOG(startup) << "Runtime::Init exiting";
Carl Shapiro1fb86202011-06-27 17:43:13 -0700587 return true;
588}
589
Elliott Hughes038a8062011-09-18 14:12:41 -0700590void Runtime::InitNativeMethods() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800591 VLOG(startup) << "Runtime::InitNativeMethods entering";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700592 Thread* self = Thread::Current();
593 JNIEnv* env = self->GetJniEnv();
594
Elliott Hughes418d20f2011-09-22 14:00:39 -0700595 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Brian Carlstromaded5f72011-10-07 17:15:04 -0700596 CHECK_EQ(self->GetState(), Thread::kNative);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700597
Elliott Hughes418d20f2011-09-22 14:00:39 -0700598 // First set up JniConstants, which is used by both the runtime's built-in native
599 // methods and libcore.
Elliott Hughesfea966e2011-09-22 10:26:20 -0700600 JniConstants::init(env);
601
Elliott Hughes418d20f2011-09-22 14:00:39 -0700602 // Then set up the native methods provided by the runtime itself.
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700603 RegisterRuntimeNativeMethods(env);
604
Elliott Hughes418d20f2011-09-22 14:00:39 -0700605 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
606 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
607 // the library that implements System.loadLibrary!
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700608 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800609 VLOG(startup) << "Runtime::InitNativeMethods exiting";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700610}
611
612void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
613#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700614 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700615 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700616 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700617 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700618 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700619 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700620 REGISTER(register_java_lang_Object);
621 REGISTER(register_java_lang_Runtime);
622 REGISTER(register_java_lang_String);
623 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700624 REGISTER(register_java_lang_Thread);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700625 REGISTER(register_java_lang_Throwable);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700626 REGISTER(register_java_lang_VMClassLoader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700627 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700628 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700629 REGISTER(register_java_lang_reflect_Field);
630 REGISTER(register_java_lang_reflect_Method);
Jesse Wilson95caa792011-10-12 18:14:17 -0400631 REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700632 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700633 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700634 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700635 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700636#undef REGISTER
637}
638
Elliott Hughes8daa0922011-09-11 13:46:25 -0700639void Runtime::Dump(std::ostream& os) {
Elliott Hughese27955c2011-08-26 15:21:24 -0700640 // TODO: dump other runtime statistics?
Elliott Hughescac6cc72011-11-03 20:31:21 -0700641 GetClassLinker()->DumpForSigQuit(os);
642 GetInternTable()->DumpForSigQuit(os);
Elliott Hughes42ee1422011-09-06 12:33:32 -0700643 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700644
645 thread_list_->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700646}
647
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800648void Runtime::DumpLockHolders(std::ostream& os) {
649 pid_t heap_lock_owner = Heap::GetLockOwner();
650 pid_t thread_list_lock_owner = GetThreadList()->GetLockOwner();
651 pid_t classes_lock_owner = GetClassLinker()->GetClassesLockOwner();
652 pid_t dex_lock_owner = GetClassLinker()->GetDexLockOwner();
653 if ((heap_lock_owner | thread_list_lock_owner | classes_lock_owner | dex_lock_owner) != 0) {
654 os << "Heap lock owner tid: " << heap_lock_owner << "\n"
655 << "ThreadList lock owner tid: " << thread_list_lock_owner << "\n"
656 << "ClassLinker classes lock owner tid: " << classes_lock_owner << "\n"
657 << "ClassLinker dex lock owner tid: " << dex_lock_owner << "\n";
658 }
659}
660
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700661void Runtime::SetStatsEnabled(bool new_state) {
662 if (new_state == true) {
663 GetStats()->Clear(~0);
664 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
665 Thread::Current()->GetStats()->Clear(~0);
666 }
667 stats_enabled_ = new_state;
668}
669
670void Runtime::ResetStats(int kinds) {
671 GetStats()->Clear(kinds & 0xffff);
672 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
673 Thread::Current()->GetStats()->Clear(kinds >> 16);
674}
675
676RuntimeStats* Runtime::GetStats() {
677 return &stats_;
678}
679
680int32_t Runtime::GetStat(int kind) {
681 RuntimeStats* stats;
682 if (kind < (1<<16)) {
683 stats = GetStats();
684 } else {
685 stats = Thread::Current()->GetStats();
686 kind >>= 16;
687 }
688 switch (kind) {
689 case KIND_ALLOCATED_OBJECTS:
690 return stats->allocated_objects;
691 case KIND_ALLOCATED_BYTES:
692 return stats->allocated_bytes;
693 case KIND_FREED_OBJECTS:
694 return stats->freed_objects;
695 case KIND_FREED_BYTES:
696 return stats->freed_bytes;
697 case KIND_GC_INVOCATIONS:
698 return stats->gc_for_alloc_count;
699 case KIND_CLASS_INIT_COUNT:
700 return stats->class_init_count;
701 case KIND_CLASS_INIT_TIME:
702 // Convert ns to us, reduce to 32 bits.
703 return (int) (stats->class_init_time_ns / 1000);
704 case KIND_EXT_ALLOCATED_OBJECTS:
705 case KIND_EXT_ALLOCATED_BYTES:
706 case KIND_EXT_FREED_OBJECTS:
707 case KIND_EXT_FREED_BYTES:
708 return 0; // backward compatibility
709 default:
710 CHECK(false);
711 return -1; // unreachable
712 }
713}
714
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700715void Runtime::BlockSignals() {
716 sigset_t sigset;
717 if (sigemptyset(&sigset) == -1) {
718 PLOG(FATAL) << "sigemptyset failed";
719 }
720 if (sigaddset(&sigset, SIGPIPE) == -1) {
721 PLOG(ERROR) << "sigaddset SIGPIPE failed";
722 }
723 // SIGQUIT is used to dump the runtime's state (including stack traces).
724 if (sigaddset(&sigset, SIGQUIT) == -1) {
725 PLOG(ERROR) << "sigaddset SIGQUIT failed";
726 }
727 // SIGUSR1 is used to initiate a heap dump.
728 if (sigaddset(&sigset, SIGUSR1) == -1) {
729 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
730 }
731 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
732}
733
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700734void Runtime::AttachCurrentThread(const char* name, bool as_daemon) {
735 Thread::Attach(instance_, name, as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700736}
737
Elliott Hughesd92bec42011-09-02 17:04:36 -0700738void Runtime::DetachCurrentThread() {
Elliott Hughes038a8062011-09-18 14:12:41 -0700739 // TODO: check we're not calling DetachCurrentThread from a call stack that
740 // includes managed frames. (It's only valid if the stack is all-native.)
Elliott Hughes02b48d12011-09-07 17:15:51 -0700741 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700742}
743
Brian Carlstrome24fa612011-09-29 00:53:55 -0700744void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700745 Dbg::VisitRoots(visitor, arg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700746 class_linker_->VisitRoots(visitor, arg);
747 intern_table_->VisitRoots(visitor, arg);
748 java_vm_->VisitRoots(visitor, arg);
749 thread_list_->VisitRoots(visitor, arg);
750 visitor(jni_stub_array_, arg);
751 visitor(abstract_method_error_stub_array_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700752 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
753 visitor(resolution_stub_array_[i], arg);
754 }
755 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
756 visitor(callee_save_method_[i], arg);
757 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700758}
759
Ian Rogers169c9a72011-11-13 20:13:17 -0800760bool Runtime::HasJniDlsymLookupStub() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700761 return jni_stub_array_ != NULL;
762}
763
Ian Rogers169c9a72011-11-13 20:13:17 -0800764ByteArray* Runtime::GetJniDlsymLookupStub() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700765 CHECK(jni_stub_array_ != NULL);
766 return jni_stub_array_;
767}
768
Ian Rogers169c9a72011-11-13 20:13:17 -0800769void Runtime::SetJniDlsymLookupStub(ByteArray* jni_stub_array) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700770 CHECK(jni_stub_array != NULL) << " jni_stub_array=" << jni_stub_array;
771 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array)
772 << "jni_stub_array_=" << jni_stub_array_ << " jni_stub_array=" << jni_stub_array;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700773 jni_stub_array_ = jni_stub_array;
774}
775
776bool Runtime::HasAbstractMethodErrorStubArray() const {
777 return abstract_method_error_stub_array_ != NULL;
778}
779
780ByteArray* Runtime::GetAbstractMethodErrorStubArray() const {
781 CHECK(abstract_method_error_stub_array_ != NULL);
782 return abstract_method_error_stub_array_;
783}
784
785void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) {
786 CHECK(abstract_method_error_stub_array != NULL);
787 CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array);
788 abstract_method_error_stub_array_ = abstract_method_error_stub_array;
789}
790
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700791
792Runtime::TrampolineType Runtime::GetTrampolineType(Method* method) {
793 if (method == NULL) {
794 return Runtime::kUnknownMethod;
795 } else if (method->IsStatic()) {
796 return Runtime::kStaticMethod;
797 } else {
798 return Runtime::kInstanceMethod;
799 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700800}
801
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700802bool Runtime::HasResolutionStubArray(TrampolineType type) const {
803 return resolution_stub_array_[type] != NULL;
Ian Rogersad25ac52011-10-04 19:13:33 -0700804}
805
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700806ByteArray* Runtime::GetResolutionStubArray(TrampolineType type) const {
807 CHECK(HasResolutionStubArray(type));
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700808 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastTrampolineMethodType));
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700809 return resolution_stub_array_[type];
810}
811
812void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700813 CHECK(resolution_stub_array != NULL);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700814 CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array);
815 resolution_stub_array_[type] = resolution_stub_array;
Ian Rogersad25ac52011-10-04 19:13:33 -0700816}
817
Brian Carlstromae826982011-11-09 01:33:42 -0800818Method* Runtime::CreateCalleeSaveMethod(InstructionSet instruction_set, CalleeSaveType type) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700819 Class* method_class = Method::GetMethodClass();
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700820 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
Ian Rogersff1ed472011-09-20 13:46:24 -0700821 method->SetDeclaringClass(method_class);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800822 // TODO: use a special method for callee saves
823 method->SetMethodIndex(DexFile::kDexNoIndex16);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700824 method->SetCode(NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800825 if ((instruction_set == kThumb2) || (instruction_set == kArm)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700826 uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6) | (1 << art::arm::R7) |
827 (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11);
828 uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3);
829 uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9);
830 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills :0) |
831 (type == kSaveAll ? all_spills :0) | (1 << art::arm::LR);
832 uint32_t fp_all_spills = (1 << art::arm::S0) | (1 << art::arm::S1) | (1 << art::arm::S2) |
833 (1 << art::arm::S3) | (1 << art::arm::S4) | (1 << art::arm::S5) |
834 (1 << art::arm::S6) | (1 << art::arm::S7) | (1 << art::arm::S8) |
835 (1 << art::arm::S9) | (1 << art::arm::S10) | (1 << art::arm::S11) |
836 (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) |
837 (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) |
838 (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) |
839 (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) |
840 (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) |
841 (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
842 (1 << art::arm::S30) | (1 << art::arm::S31);
843 uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
844 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
845 __builtin_popcount(fp_spills) /* fprs */ +
846 1 /* Method* */) * kPointerSize, kStackAlignment);
Ian Rogers15fdb8c2011-09-25 15:45:07 -0700847 method->SetFrameSizeInBytes(frame_size);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700848 method->SetCoreSpillMask(core_spills);
849 method->SetFpSpillMask(fp_spills);
Brian Carlstromae826982011-11-09 01:33:42 -0800850 } else if (instruction_set == kX86) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700851 method->SetFrameSizeInBytes(32);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700852 method->SetCoreSpillMask((1 << art::x86::EBX) | (1 << art::x86::EBP) | (1 << art::x86::ESI) |
Ian Rogersff1ed472011-09-20 13:46:24 -0700853 (1 << art::x86::EDI));
854 method->SetFpSpillMask(0);
855 } else {
856 UNIMPLEMENTED(FATAL);
857 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700858 return method.get();
Ian Rogersff1ed472011-09-20 13:46:24 -0700859}
860
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700861bool Runtime::HasCalleeSaveMethod(CalleeSaveType type) const {
862 return callee_save_method_[type] != NULL;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700863}
864
Brian Carlstrome24fa612011-09-29 00:53:55 -0700865// Returns a special method that describes all callee saves being spilled to the stack.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700866Method* Runtime::GetCalleeSaveMethod(CalleeSaveType type) const {
867 CHECK(HasCalleeSaveMethod(type));
868 return callee_save_method_[type];
Brian Carlstrome24fa612011-09-29 00:53:55 -0700869}
870
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700871void Runtime::SetCalleeSaveMethod(Method* method, CalleeSaveType type) {
872 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
873 callee_save_method_[type] = method;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700874}
875
jeffhao2692b572011-12-16 15:42:28 -0800876void Runtime::EnableMethodTracing(Trace* tracer) {
877 CHECK(!IsMethodTracingActive());
878 tracer_ = tracer;
879}
880
881void Runtime::DisableMethodTracing() {
882 CHECK(IsMethodTracingActive());
883 delete tracer_;
884 tracer_ = NULL;
885}
886
887bool Runtime::IsMethodTracingActive() const {
888 return (tracer_ != NULL);
889}
890
891Trace* Runtime::GetTracer() const {
892 CHECK(IsMethodTracingActive());
893 return tracer_;
894}
895
Carl Shapiro1fb86202011-06-27 17:43:13 -0700896} // namespace art