blob: a00ebcd6205077c760e2390906e94e5f1c694dd1 [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),
50 stats_enabled_(false) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -070051 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
52 resolution_stub_array_[i] = NULL;
53 }
54 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
55 callee_save_method_[i] = NULL;
56 }
Elliott Hughesdcc24742011-09-07 14:02:44 -070057}
58
Carl Shapiro61e019d2011-07-14 16:53:09 -070059Runtime::~Runtime() {
Elliott Hughesd1cc8362011-10-24 16:58:50 -070060 Dbg::StopJdwp();
61
Elliott Hughes5fe594f2011-09-08 12:33:17 -070062 // Make sure our internal threads are dead before we start tearing down things they're using.
63 delete signal_catcher_;
Elliott Hughes038a8062011-09-18 14:12:41 -070064 // TODO: GC thread.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070065
Elliott Hughes038a8062011-09-18 14:12:41 -070066 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
Elliott Hughes93e74e82011-09-13 11:07:03 -070067 delete thread_list_;
Elliott Hughesc33a32b2011-10-11 18:18:07 -070068 delete monitor_list_;
Elliott Hughes93e74e82011-09-13 11:07:03 -070069
Carl Shapiro61e019d2011-07-14 16:53:09 -070070 delete class_linker_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070071 Heap::Destroy();
Elliott Hughescf4c6c42011-09-01 15:16:42 -070072 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070073 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -070074 Thread::Shutdown();
Carl Shapiro4acf4642011-07-26 18:54:13 -070075 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070076 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -070077 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -070078}
79
Elliott Hughescac6cc72011-11-03 20:31:21 -070080static bool gAborting = false;
81
Elliott Hughese0918552011-10-28 17:18:29 -070082struct AbortState {
83 void Dump(std::ostream& os) {
Elliott Hughescac6cc72011-11-03 20:31:21 -070084 if (gAborting) {
85 os << "Runtime aborting --- recursively, so no thread-specific detail!\n";
86 return;
87 }
88 gAborting = true;
Elliott Hughese0918552011-10-28 17:18:29 -070089 os << "Runtime aborting...\n";
90 Thread* self = Thread::Current();
91 if (self == NULL) {
92 os << "(Aborting thread was not attached to runtime!)\n";
93 } else {
94 self->Dump(os, true);
95 }
96 }
97};
98
Elliott Hughesffe67362011-07-17 12:09:27 -070099void Runtime::Abort(const char* file, int line) {
100 // Get any pending output out of the way.
101 fflush(NULL);
102
103 // Many people have difficulty distinguish aborts from crashes,
104 // so be explicit.
Elliott Hughese0918552011-10-28 17:18:29 -0700105 AbortState state;
106 LOG(ERROR) << Dumpable<AbortState>(state);
Elliott Hughesffe67362011-07-17 12:09:27 -0700107
Elliott Hughesffe67362011-07-17 12:09:27 -0700108 // Perform any platform-specific pre-abort actions.
109 PlatformAbort(file, line);
110
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700111 // use abort hook if we have one
112 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
113 Runtime::Current()->abort_();
114 // notreached
115 }
116
Elliott Hughesffe67362011-07-17 12:09:27 -0700117 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -0700118 // receive SIGABRT. debuggerd dumps the stack trace of the main
119 // thread, whether or not that was the thread that failed. By
120 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -0700121 // fault in the current thread, and get a useful log from debuggerd.
122 // We can also trivially tell the difference between a VM crash and
123 // a deliberate abort by looking at the fault address.
124 *reinterpret_cast<char*>(0xdeadd00d) = 38;
125 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -0700126 // notreached
127}
128
Elliott Hughesbf86d042011-08-31 17:53:14 -0700129void Runtime::CallExitHook(jint status) {
130 if (exit_ != NULL) {
131 ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative);
132 exit_(status);
133 LOG(WARNING) << "Exit hook returned instead of exiting!";
134 }
135}
136
Brian Carlstrom8a436592011-08-15 21:27:23 -0700137// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
138// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
139// [gG] gigabytes.
140//
141// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700142// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
143// of 1024.
144//
145// The spec says the -Xmx and -Xms options must be multiples of 1024. It
146// doesn't say anything about -Xss.
147//
148// Returns 0 (a useless size) if "s" is malformed or specifies a low or
149// non-evenly-divisible value.
150//
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800151size_t ParseMemoryOption(const char* s, size_t div) {
Brian Carlstrom8a436592011-08-15 21:27:23 -0700152 // strtoul accepts a leading [+-], which we don't want,
153 // so make sure our string starts with a decimal digit.
154 if (isdigit(*s)) {
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800155 const char* s2;
156 size_t val = strtoul(s, (char**)&s2, 10);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700157 if (s2 != s) {
158 // s2 should be pointing just after the number.
159 // If this is the end of the string, the user
160 // has specified a number of bytes. Otherwise,
161 // there should be exactly one more character
162 // that specifies a multiplier.
163 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700164 // The remainder of the string is either a single multiplier
165 // character, or nothing to indicate that the value is in
166 // bytes.
167 char c = *s2++;
168 if (*s2 == '\0') {
169 size_t mul;
170 if (c == '\0') {
171 mul = 1;
172 } else if (c == 'k' || c == 'K') {
173 mul = KB;
174 } else if (c == 'm' || c == 'M') {
175 mul = MB;
176 } else if (c == 'g' || c == 'G') {
177 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700178 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700179 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700180 return 0;
181 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700182
183 if (val <= std::numeric_limits<size_t>::max() / mul) {
184 val *= mul;
185 } else {
186 // Clamp to a multiple of 1024.
187 val = std::numeric_limits<size_t>::max() & ~(1024-1);
188 }
189 } else {
190 // There's more than one character after the numeric part.
191 return 0;
192 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700193 }
194 // The man page says that a -Xm value must be a multiple of 1024.
195 if (val % div == 0) {
196 return val;
197 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700198 }
199 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700200 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700201}
202
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700203size_t ParseIntegerOrDie(const StringPiece& s) {
204 StringPiece::size_type colon = s.find(':');
205 if (colon == StringPiece::npos) {
206 LOG(FATAL) << "Missing integer: " << s;
207 }
208 const char* begin = &s.data()[colon + 1];
209 char* end;
210 size_t result = strtoul(begin, &end, 10);
211 if (begin == end || *end != '\0') {
212 LOG(FATAL) << "Failed to parse integer in: " << s;
213 }
214 return result;
215}
216
Elliott Hughes0af55432011-08-17 18:37:28 -0700217void LoadJniLibrary(JavaVMExt* vm, const char* name) {
218 // TODO: OS_SHARED_LIB_FORMAT_STR
219 std::string mapped_name(StringPrintf("lib%s.so", name));
Elliott Hughes75770752011-08-24 17:52:38 -0700220 std::string reason;
221 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700222 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
223 << reason;
224 }
225}
226
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700227Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700228 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700229 bool compiler = false;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700230 const char* boot_class_path = getenv("BOOTCLASSPATH");
231 if (boot_class_path != NULL) {
232 parsed->boot_class_path_ = getenv("BOOTCLASSPATH");
233 }
234 const char* class_path = getenv("CLASSPATH");
235 if (class_path != NULL) {
236 parsed->class_path_ = getenv("CLASSPATH");
237 }
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700238#ifdef NDEBUG
Elliott Hughes5174fe62011-08-23 15:12:35 -0700239 // -Xcheck:jni is off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700240 parsed->check_jni_ = false;
241#else
242 // ...but on by default in debug builds.
243 parsed->check_jni_ = true;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700244#endif
Elliott Hughes85d15452011-09-16 17:33:01 -0700245
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700246 parsed->heap_initial_size_ = Heap::kInitialSize;
247 parsed->heap_maximum_size_ = Heap::kMaximumSize;
jeffhaoc1160702011-10-27 15:48:45 -0700248 parsed->heap_growth_limit_ = 0; // 0 means no growth limit
Brian Carlstromf734cf52011-08-17 16:28:14 -0700249 parsed->stack_size_ = Thread::kDefaultStackSize;
250
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700251 parsed->is_zygote_ = false;
252
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700253 parsed->jni_globals_max_ = 0;
Elliott Hughesfc861622011-10-17 17:57:47 -0700254 parsed->lock_profiling_threshold_ = 0;
255 parsed->hook_is_sensitive_thread_ = NULL;
256
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700257 parsed->hook_vfprintf_ = vfprintf;
258 parsed->hook_exit_ = exit;
259 parsed->hook_abort_ = abort;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700260
261 for (size_t i = 0; i < options.size(); ++i) {
262 const StringPiece& option = options[i].first;
Brian Carlstrom0796af02011-10-12 14:31:45 -0700263 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700264 LOG(INFO) << "option[" << i << "]=" << option;
265 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700266 if (option.starts_with("-Xbootclasspath:")) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700267 parsed->boot_class_path_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700268 } else if (option == "-classpath" || option == "-cp") {
269 // TODO: support -Djava.class.path
270 i++;
271 if (i == options.size()) {
272 // TODO: usage
273 LOG(FATAL) << "Missing required class path value for " << option;
274 return NULL;
275 }
276 const StringPiece& value = options[i].first;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700277 parsed->class_path_ = value.data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700278 } else if (option.starts_with("-Ximage:")) {
279 parsed->images_.push_back(option.substr(strlen("-Ximage:")).data());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700280 } else if (option.starts_with("-Xcheck:jni")) {
281 parsed->check_jni_ = true;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700282 } else if (option.starts_with("-Xrunjdwp:") || option.starts_with("-agentlib:jdwp=")) {
Elliott Hughes95572412011-12-13 18:14:20 -0800283 std::string tail(option.substr(option[1] == 'X' ? 10 : 15).ToString());
Elliott Hughes3bb81562011-10-21 18:52:59 -0700284 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
285 LOG(FATAL) << "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
286 << "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n";
287 return NULL;
288 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700289 } else if (option.starts_with("-Xms")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700290 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024);
291 if (size == 0) {
292 if (ignore_unrecognized) {
293 continue;
294 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700295 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700296 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700297 return NULL;
298 }
299 parsed->heap_initial_size_ = size;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700300 } else if (option.starts_with("-Xmx")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700301 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024);
302 if (size == 0) {
303 if (ignore_unrecognized) {
304 continue;
305 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700306 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700307 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700308 return NULL;
309 }
310 parsed->heap_maximum_size_ = size;
jeffhaoc1160702011-10-27 15:48:45 -0700311 } else if (option.starts_with("-XX:HeapGrowthLimit=")) {
312 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).data(), 1024);
313 if (size == 0) {
314 if (ignore_unrecognized) {
315 continue;
316 }
317 // TODO: usage
318 LOG(FATAL) << "Failed to parse " << option;
319 return NULL;
320 }
321 parsed->heap_growth_limit_ = size;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700322 } else if (option.starts_with("-Xss")) {
323 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1);
324 if (size == 0) {
325 if (ignore_unrecognized) {
326 continue;
327 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700328 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700329 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700330 return NULL;
331 }
332 parsed->stack_size_ = size;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700333 } else if (option.starts_with("-D")) {
334 parsed->properties_.push_back(option.substr(strlen("-D")).data());
Elliott Hughesa0957642011-09-02 14:27:33 -0700335 } else if (option.starts_with("-Xjnitrace:")) {
336 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:")).data();
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700337 } else if (option == "compiler") {
338 compiler = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700339 } else if (option == "-Xzygote") {
340 parsed->is_zygote_ = true;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700341 } else if (option.starts_with("-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700342 std::vector<std::string> verbose_options;
Elliott Hughes34023802011-08-30 12:06:17 -0700343 Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700344 for (size_t i = 0; i < verbose_options.size(); ++i) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800345 if (verbose_options[i] == "class") {
346 gLogVerbosity.class_linker = true;
347 } else if (verbose_options[i] == "compiler") {
348 gLogVerbosity.compiler = true;
349 } else if (verbose_options[i] == "heap") {
350 gLogVerbosity.heap = true;
351 } else if (verbose_options[i] == "gc") {
352 gLogVerbosity.gc = true;
353 } else if (verbose_options[i] == "jdwp") {
354 gLogVerbosity.jdwp = true;
355 } else if (verbose_options[i] == "jni") {
356 gLogVerbosity.jni = true;
357 } else if (verbose_options[i] == "monitor") {
358 gLogVerbosity.monitor = true;
359 } else if (verbose_options[i] == "startup") {
360 gLogVerbosity.startup = true;
361 } else if (verbose_options[i] == "third-party-jni") {
362 gLogVerbosity.third_party_jni = true;
363 } else if (verbose_options[i] == "threads") {
364 gLogVerbosity.threads = true;
365 } else {
366 LOG(WARNING) << "Ignoring unknown -verbose option: " << verbose_options[i];
367 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700368 }
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700369 } else if (option.starts_with("-Xjnigreflimit:")) {
370 parsed->jni_globals_max_ = ParseIntegerOrDie(option);
Elliott Hughesfc861622011-10-17 17:57:47 -0700371 } else if (option.starts_with("-Xlockprofthreshold:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700372 parsed->lock_profiling_threshold_ = ParseIntegerOrDie(option);
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700373 } else if (option.starts_with("-Xstacktracefile:")) {
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700374// always show stack traces in debug builds
375#ifdef NDEBUG
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700376 parsed->stack_trace_file_ = option.substr(strlen("-Xstacktracefile:")).data();
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700377#endif
Elliott Hughesfc861622011-10-17 17:57:47 -0700378 } else if (option == "sensitiveThread") {
379 parsed->hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(options[i].second);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700380 } else if (option == "vfprintf") {
381 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
382 } else if (option == "exit") {
383 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
384 } else if (option == "abort") {
385 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700386 } else if (option == "host-prefix") {
387 parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700388 } else {
389 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700390 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700391 LOG(ERROR) << "Unrecognized option " << option;
392 // TODO: this should exit, but for now tolerate unknown options
393 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700394 }
395 }
396 }
397
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700398 if (!compiler && parsed->images_.empty()) {
Brian Carlstrom29e7ac72011-12-05 23:42:57 -0800399 parsed->images_.push_back("/system/framework/boot.art");
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700400 }
jeffhaoc1160702011-10-27 15:48:45 -0700401 if (parsed->heap_growth_limit_ == 0) {
402 parsed->heap_growth_limit_ = parsed->heap_maximum_size_;
403 }
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700404
Elliott Hughescac6cc72011-11-03 20:31:21 -0700405 LOG(INFO) << "Build type: "
406#ifndef NDEBUG
407 << "debug"
408#else
409 << "optimized"
410#endif
411 << "; CheckJNI: " << (parsed->check_jni_ ? "on" : "off");
Elliott Hughes85d15452011-09-16 17:33:01 -0700412
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700413 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700414}
415
416Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700417 // TODO: acquire a static mutex on Runtime to avoid racing.
418 if (Runtime::instance_ != NULL) {
419 return NULL;
420 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700421 instance_ = new Runtime;
422 if (!instance_->Init(options, ignore_unrecognized)) {
423 delete instance_;
424 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700425 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700426 return instance_;
427}
Elliott Hughes0af55432011-08-17 18:37:28 -0700428
Brian Carlstromaded5f72011-10-07 17:15:04 -0700429void CreateSystemClassLoader() {
430 if (ClassLoader::UseCompileTimeClassPath()) {
431 return;
432 }
433
434 Thread* self = Thread::Current();
435
436 // Must be in the kNative state for calling native methods.
437 CHECK_EQ(self->GetState(), Thread::kNative);
438
439 JNIEnv* env = self->GetJniEnv();
Brian Carlstromdf143242011-10-10 18:05:34 -0700440 ScopedLocalRef<jclass> ClassLoader_class(env, env->FindClass("java/lang/ClassLoader"));
441 CHECK(ClassLoader_class.get() != NULL);
442 jmethodID getSystemClassLoader = env->GetStaticMethodID(ClassLoader_class.get(),
443 "getSystemClassLoader",
444 "()Ljava/lang/ClassLoader;");
445 CHECK(getSystemClassLoader != NULL);
446 ScopedLocalRef<jobject> class_loader(env, env->CallStaticObjectMethod(ClassLoader_class.get(),
447 getSystemClassLoader));
448 CHECK(class_loader.get() != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700449
Brian Carlstromdf143242011-10-10 18:05:34 -0700450 Thread::Current()->SetClassLoaderOverride(Decode<ClassLoader*>(env, class_loader.get()));
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500451
452 ScopedLocalRef<jclass> Thread_class(env, env->FindClass("java/lang/Thread"));
453 CHECK(Thread_class.get() != NULL);
454 jfieldID contextClassLoader = env->GetFieldID(Thread_class.get(),
455 "contextClassLoader",
456 "Ljava/lang/ClassLoader;");
457 CHECK(contextClassLoader != NULL);
458 ScopedLocalRef<jobject> self_jobject(env, AddLocalReference<jobject>(env, self->GetPeer()));
459 env->SetObjectField(self_jobject.get(), contextClassLoader, class_loader.get());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700460}
461
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700462void Runtime::Start() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800463 VLOG(startup) << "Runtime::Start entering";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700464
465 CHECK(host_prefix_.empty()) << host_prefix_;
466
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700467 // Restore main thread state to kNative as expected by native code
468 Thread::Current()->SetState(Thread::kNative);
469
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700470 started_ = true;
471
Brian Carlstromae826982011-11-09 01:33:42 -0800472 // InitNativeMethods needs to be after started_ so that the classes
473 // it touches will have methods linked to the oat file if necessary.
474 InitNativeMethods();
475
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500476 Thread::FinishStartup();
477
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700478 if (!is_zygote_) {
479 DidForkFromZygote();
480 }
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700481
Elliott Hughes85d15452011-09-16 17:33:01 -0700482 StartDaemonThreads();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700483
Brian Carlstromaded5f72011-10-07 17:15:04 -0700484 CreateSystemClassLoader();
485
Elliott Hughes726079d2011-10-07 18:43:44 -0700486 Thread::Current()->GetJniEnv()->locals.AssertEmpty();
487
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800488 VLOG(startup) << "Runtime::Start exiting";
Elliott Hughes85d15452011-09-16 17:33:01 -0700489}
490
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700491void Runtime::DidForkFromZygote() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700492 is_zygote_ = false;
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700493
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700494 StartSignalCatcher();
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700495
496 // Start the JDWP thread. If the command-line debugger flags specified "suspend=y",
497 // this will pause the runtime, so we probably want this to come last.
498 Dbg::StartJdwp();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700499}
500
501void Runtime::StartSignalCatcher() {
502 if (!is_zygote_) {
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700503 signal_catcher_ = new SignalCatcher(stack_trace_file_);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700504 }
505}
506
Elliott Hughes85d15452011-09-16 17:33:01 -0700507void Runtime::StartDaemonThreads() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800508 VLOG(startup) << "Runtime::StartDaemonThreads entering";
Elliott Hughes85d15452011-09-16 17:33:01 -0700509
Elliott Hughes719b3232011-09-25 17:42:19 -0700510 Thread* self = Thread::Current();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700511
512 // Must be in the kNative state for calling native methods.
513 CHECK_EQ(self->GetState(), Thread::kNative);
514
Elliott Hughes719b3232011-09-25 17:42:19 -0700515 JNIEnv* env = self->GetJniEnv();
Elliott Hughes726079d2011-10-07 18:43:44 -0700516 ScopedLocalRef<jclass> c(env, env->FindClass("java/lang/Daemons"));
517 CHECK(c.get() != NULL);
518 jmethodID mid = env->GetStaticMethodID(c.get(), "start", "()V");
Elliott Hughes719b3232011-09-25 17:42:19 -0700519 CHECK(mid != NULL);
Elliott Hughes726079d2011-10-07 18:43:44 -0700520 env->CallStaticVoidMethod(c.get(), mid);
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700521
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800522 VLOG(startup) << "Runtime::StartDaemonThreads exiting";
Carl Shapiro61e019d2011-07-14 16:53:09 -0700523}
524
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700525bool Runtime::IsStarted() const {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700526 return started_;
527}
528
Elliott Hughes0af55432011-08-17 18:37:28 -0700529bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700530 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700531
Elliott Hughes90a33692011-08-30 13:27:07 -0700532 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
533 if (options.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700534 LOG(ERROR) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700535 return false;
536 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800537 VLOG(startup) << "Runtime::Init -verbose:startup enabled";
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700538
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700539 SetJniGlobalsMax(options->jni_globals_max_);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800540 Monitor::Init(options->lock_profiling_threshold_, options->hook_is_sensitive_thread_);
Elliott Hughes32d6e1e2011-10-11 14:47:44 -0700541
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700542 host_prefix_ = options->host_prefix_;
543 boot_class_path_ = options->boot_class_path_;
544 class_path_ = options->class_path_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700545 properties_ = options->properties_;
546
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700547 is_zygote_ = options->is_zygote_;
548
Elliott Hughes0af55432011-08-17 18:37:28 -0700549 vfprintf_ = options->hook_vfprintf_;
550 exit_ = options->hook_exit_;
551 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700552
Elliott Hughesbe759c62011-09-08 19:38:21 -0700553 default_stack_size_ = options->stack_size_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700554 stack_trace_file_ = options->stack_trace_file_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700555
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700556 monitor_list_ = new MonitorList;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800557 thread_list_ = new ThreadList;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700558 intern_table_ = new InternTable;
559
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800560 Heap::Init(options->heap_initial_size_,
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700561 options->heap_maximum_size_,
jeffhaoc1160702011-10-27 15:48:45 -0700562 options->heap_growth_limit_,
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700563 options->images_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700564
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700565 BlockSignals();
566
Elliott Hughesa0957642011-09-02 14:27:33 -0700567 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700568
Elliott Hughesbe759c62011-09-08 19:38:21 -0700569 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700570
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700571 // ClassLinker needs an attached thread, but we can't fully attach a thread
572 // without creating objects.
573 Thread::Attach(this, "main", false);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700574
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700575 // Set us to runnable so tools using a runtime can allocate and GC by default
576 Thread::Current()->SetState(Thread::kRunnable);
577
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700578 CHECK_GE(Heap::GetSpaces().size(), 1U);
579 class_linker_ = ((Heap::GetSpaces()[0]->IsImageSpace())
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800580 ? ClassLinker::Create(intern_table_)
581 : ClassLinker::Create(options->boot_class_path_, intern_table_));
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700582
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800583 VLOG(startup) << "Runtime::Init exiting";
Carl Shapiro1fb86202011-06-27 17:43:13 -0700584 return true;
585}
586
Elliott Hughes038a8062011-09-18 14:12:41 -0700587void Runtime::InitNativeMethods() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800588 VLOG(startup) << "Runtime::InitNativeMethods entering";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700589 Thread* self = Thread::Current();
590 JNIEnv* env = self->GetJniEnv();
591
Elliott Hughes418d20f2011-09-22 14:00:39 -0700592 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Brian Carlstromaded5f72011-10-07 17:15:04 -0700593 CHECK_EQ(self->GetState(), Thread::kNative);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700594
Elliott Hughes418d20f2011-09-22 14:00:39 -0700595 // First set up JniConstants, which is used by both the runtime's built-in native
596 // methods and libcore.
Elliott Hughesfea966e2011-09-22 10:26:20 -0700597 JniConstants::init(env);
598
Elliott Hughes418d20f2011-09-22 14:00:39 -0700599 // Then set up the native methods provided by the runtime itself.
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700600 RegisterRuntimeNativeMethods(env);
601
Elliott Hughes418d20f2011-09-22 14:00:39 -0700602 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
603 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
604 // the library that implements System.loadLibrary!
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700605 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800606 VLOG(startup) << "Runtime::InitNativeMethods exiting";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700607}
608
609void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
610#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700611 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700612 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700613 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700614 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700615 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700616 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700617 REGISTER(register_java_lang_Object);
618 REGISTER(register_java_lang_Runtime);
619 REGISTER(register_java_lang_String);
620 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700621 REGISTER(register_java_lang_Thread);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700622 REGISTER(register_java_lang_Throwable);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700623 REGISTER(register_java_lang_VMClassLoader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700624 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700625 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700626 REGISTER(register_java_lang_reflect_Field);
627 REGISTER(register_java_lang_reflect_Method);
Jesse Wilson95caa792011-10-12 18:14:17 -0400628 REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700629 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700630 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700631 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700632 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700633#undef REGISTER
634}
635
Elliott Hughes8daa0922011-09-11 13:46:25 -0700636void Runtime::Dump(std::ostream& os) {
Elliott Hughese27955c2011-08-26 15:21:24 -0700637 // TODO: dump other runtime statistics?
Elliott Hughescac6cc72011-11-03 20:31:21 -0700638 GetClassLinker()->DumpForSigQuit(os);
639 GetInternTable()->DumpForSigQuit(os);
Elliott Hughes42ee1422011-09-06 12:33:32 -0700640 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700641
642 thread_list_->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700643}
644
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800645void Runtime::DumpLockHolders(std::ostream& os) {
646 pid_t heap_lock_owner = Heap::GetLockOwner();
647 pid_t thread_list_lock_owner = GetThreadList()->GetLockOwner();
648 pid_t classes_lock_owner = GetClassLinker()->GetClassesLockOwner();
649 pid_t dex_lock_owner = GetClassLinker()->GetDexLockOwner();
650 if ((heap_lock_owner | thread_list_lock_owner | classes_lock_owner | dex_lock_owner) != 0) {
651 os << "Heap lock owner tid: " << heap_lock_owner << "\n"
652 << "ThreadList lock owner tid: " << thread_list_lock_owner << "\n"
653 << "ClassLinker classes lock owner tid: " << classes_lock_owner << "\n"
654 << "ClassLinker dex lock owner tid: " << dex_lock_owner << "\n";
655 }
656}
657
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700658void Runtime::SetStatsEnabled(bool new_state) {
659 if (new_state == true) {
660 GetStats()->Clear(~0);
661 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
662 Thread::Current()->GetStats()->Clear(~0);
663 }
664 stats_enabled_ = new_state;
665}
666
667void Runtime::ResetStats(int kinds) {
668 GetStats()->Clear(kinds & 0xffff);
669 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
670 Thread::Current()->GetStats()->Clear(kinds >> 16);
671}
672
673RuntimeStats* Runtime::GetStats() {
674 return &stats_;
675}
676
677int32_t Runtime::GetStat(int kind) {
678 RuntimeStats* stats;
679 if (kind < (1<<16)) {
680 stats = GetStats();
681 } else {
682 stats = Thread::Current()->GetStats();
683 kind >>= 16;
684 }
685 switch (kind) {
686 case KIND_ALLOCATED_OBJECTS:
687 return stats->allocated_objects;
688 case KIND_ALLOCATED_BYTES:
689 return stats->allocated_bytes;
690 case KIND_FREED_OBJECTS:
691 return stats->freed_objects;
692 case KIND_FREED_BYTES:
693 return stats->freed_bytes;
694 case KIND_GC_INVOCATIONS:
695 return stats->gc_for_alloc_count;
696 case KIND_CLASS_INIT_COUNT:
697 return stats->class_init_count;
698 case KIND_CLASS_INIT_TIME:
699 // Convert ns to us, reduce to 32 bits.
700 return (int) (stats->class_init_time_ns / 1000);
701 case KIND_EXT_ALLOCATED_OBJECTS:
702 case KIND_EXT_ALLOCATED_BYTES:
703 case KIND_EXT_FREED_OBJECTS:
704 case KIND_EXT_FREED_BYTES:
705 return 0; // backward compatibility
706 default:
707 CHECK(false);
708 return -1; // unreachable
709 }
710}
711
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700712void Runtime::BlockSignals() {
713 sigset_t sigset;
714 if (sigemptyset(&sigset) == -1) {
715 PLOG(FATAL) << "sigemptyset failed";
716 }
717 if (sigaddset(&sigset, SIGPIPE) == -1) {
718 PLOG(ERROR) << "sigaddset SIGPIPE failed";
719 }
720 // SIGQUIT is used to dump the runtime's state (including stack traces).
721 if (sigaddset(&sigset, SIGQUIT) == -1) {
722 PLOG(ERROR) << "sigaddset SIGQUIT failed";
723 }
724 // SIGUSR1 is used to initiate a heap dump.
725 if (sigaddset(&sigset, SIGUSR1) == -1) {
726 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
727 }
728 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
729}
730
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700731void Runtime::AttachCurrentThread(const char* name, bool as_daemon) {
732 Thread::Attach(instance_, name, as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700733}
734
Elliott Hughesd92bec42011-09-02 17:04:36 -0700735void Runtime::DetachCurrentThread() {
Elliott Hughes038a8062011-09-18 14:12:41 -0700736 // TODO: check we're not calling DetachCurrentThread from a call stack that
737 // includes managed frames. (It's only valid if the stack is all-native.)
Elliott Hughes02b48d12011-09-07 17:15:51 -0700738 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700739}
740
Brian Carlstrome24fa612011-09-29 00:53:55 -0700741void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700742 Dbg::VisitRoots(visitor, arg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700743 class_linker_->VisitRoots(visitor, arg);
744 intern_table_->VisitRoots(visitor, arg);
745 java_vm_->VisitRoots(visitor, arg);
746 thread_list_->VisitRoots(visitor, arg);
747 visitor(jni_stub_array_, arg);
748 visitor(abstract_method_error_stub_array_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700749 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
750 visitor(resolution_stub_array_[i], arg);
751 }
752 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
753 visitor(callee_save_method_[i], arg);
754 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700755}
756
Ian Rogers169c9a72011-11-13 20:13:17 -0800757bool Runtime::HasJniDlsymLookupStub() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700758 return jni_stub_array_ != NULL;
759}
760
Ian Rogers169c9a72011-11-13 20:13:17 -0800761ByteArray* Runtime::GetJniDlsymLookupStub() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700762 CHECK(jni_stub_array_ != NULL);
763 return jni_stub_array_;
764}
765
Ian Rogers169c9a72011-11-13 20:13:17 -0800766void Runtime::SetJniDlsymLookupStub(ByteArray* jni_stub_array) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700767 CHECK(jni_stub_array != NULL) << " jni_stub_array=" << jni_stub_array;
768 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array)
769 << "jni_stub_array_=" << jni_stub_array_ << " jni_stub_array=" << jni_stub_array;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700770 jni_stub_array_ = jni_stub_array;
771}
772
773bool Runtime::HasAbstractMethodErrorStubArray() const {
774 return abstract_method_error_stub_array_ != NULL;
775}
776
777ByteArray* Runtime::GetAbstractMethodErrorStubArray() const {
778 CHECK(abstract_method_error_stub_array_ != NULL);
779 return abstract_method_error_stub_array_;
780}
781
782void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) {
783 CHECK(abstract_method_error_stub_array != NULL);
784 CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array);
785 abstract_method_error_stub_array_ = abstract_method_error_stub_array;
786}
787
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700788
789Runtime::TrampolineType Runtime::GetTrampolineType(Method* method) {
790 if (method == NULL) {
791 return Runtime::kUnknownMethod;
792 } else if (method->IsStatic()) {
793 return Runtime::kStaticMethod;
794 } else {
795 return Runtime::kInstanceMethod;
796 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700797}
798
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700799bool Runtime::HasResolutionStubArray(TrampolineType type) const {
800 return resolution_stub_array_[type] != NULL;
Ian Rogersad25ac52011-10-04 19:13:33 -0700801}
802
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700803ByteArray* Runtime::GetResolutionStubArray(TrampolineType type) const {
804 CHECK(HasResolutionStubArray(type));
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700805 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastTrampolineMethodType));
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700806 return resolution_stub_array_[type];
807}
808
809void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700810 CHECK(resolution_stub_array != NULL);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700811 CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array);
812 resolution_stub_array_[type] = resolution_stub_array;
Ian Rogersad25ac52011-10-04 19:13:33 -0700813}
814
Brian Carlstromae826982011-11-09 01:33:42 -0800815Method* Runtime::CreateCalleeSaveMethod(InstructionSet instruction_set, CalleeSaveType type) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700816 Class* method_class = Method::GetMethodClass();
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700817 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
Ian Rogersff1ed472011-09-20 13:46:24 -0700818 method->SetDeclaringClass(method_class);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800819 // TODO: use a special method for callee saves
820 method->SetMethodIndex(DexFile::kDexNoIndex16);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700821 method->SetCode(NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800822 if ((instruction_set == kThumb2) || (instruction_set == kArm)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700823 uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6) | (1 << art::arm::R7) |
824 (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11);
825 uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3);
826 uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9);
827 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills :0) |
828 (type == kSaveAll ? all_spills :0) | (1 << art::arm::LR);
829 uint32_t fp_all_spills = (1 << art::arm::S0) | (1 << art::arm::S1) | (1 << art::arm::S2) |
830 (1 << art::arm::S3) | (1 << art::arm::S4) | (1 << art::arm::S5) |
831 (1 << art::arm::S6) | (1 << art::arm::S7) | (1 << art::arm::S8) |
832 (1 << art::arm::S9) | (1 << art::arm::S10) | (1 << art::arm::S11) |
833 (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) |
834 (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) |
835 (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) |
836 (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) |
837 (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) |
838 (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
839 (1 << art::arm::S30) | (1 << art::arm::S31);
840 uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
841 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
842 __builtin_popcount(fp_spills) /* fprs */ +
843 1 /* Method* */) * kPointerSize, kStackAlignment);
Ian Rogers15fdb8c2011-09-25 15:45:07 -0700844 method->SetFrameSizeInBytes(frame_size);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700845 method->SetCoreSpillMask(core_spills);
846 method->SetFpSpillMask(fp_spills);
Brian Carlstromae826982011-11-09 01:33:42 -0800847 } else if (instruction_set == kX86) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700848 method->SetFrameSizeInBytes(32);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700849 method->SetCoreSpillMask((1 << art::x86::EBX) | (1 << art::x86::EBP) | (1 << art::x86::ESI) |
Ian Rogersff1ed472011-09-20 13:46:24 -0700850 (1 << art::x86::EDI));
851 method->SetFpSpillMask(0);
852 } else {
853 UNIMPLEMENTED(FATAL);
854 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700855 return method.get();
Ian Rogersff1ed472011-09-20 13:46:24 -0700856}
857
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700858bool Runtime::HasCalleeSaveMethod(CalleeSaveType type) const {
859 return callee_save_method_[type] != NULL;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700860}
861
Brian Carlstrome24fa612011-09-29 00:53:55 -0700862// Returns a special method that describes all callee saves being spilled to the stack.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700863Method* Runtime::GetCalleeSaveMethod(CalleeSaveType type) const {
864 CHECK(HasCalleeSaveMethod(type));
865 return callee_save_method_[type];
Brian Carlstrome24fa612011-09-29 00:53:55 -0700866}
867
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700868void Runtime::SetCalleeSaveMethod(Method* method, CalleeSaveType type) {
869 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
870 callee_save_method_[type] = method;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700871}
872
Carl Shapiro1fb86202011-06-27 17:43:13 -0700873} // namespace art