blob: 1304787d1a970cc63393f1457aeaefceb4b4fbd3 [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
Elliott Hughesffe67362011-07-17 12:09:27 -07005#include <cstdio>
6#include <cstdlib>
Brian Carlstrom8a436592011-08-15 21:27:23 -07007#include <limits>
Carl Shapiro2ed144c2011-07-26 16:52:08 -07008#include <vector>
Elliott Hughesffe67362011-07-17 12:09:27 -07009
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070010#include "class_linker.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070011#include "class_loader.h"
Elliott Hughes3bb81562011-10-21 18:52:59 -070012#include "debugger.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070013#include "heap.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070014#include "image.h"
Elliott Hughescf4c6c42011-09-01 15:16:42 -070015#include "intern_table.h"
Elliott Hughesc5f7c912011-08-18 14:00:42 -070016#include "jni_internal.h"
Elliott Hughes32d6e1e2011-10-11 14:47:44 -070017#include "monitor.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070018#include "oat_file.h"
Elliott Hughes726079d2011-10-07 18:43:44 -070019#include "ScopedLocalRef.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070020#include "signal_catcher.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070021#include "space.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070022#include "thread.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070023#include "thread_list.h"
Elliott Hughes3bb81562011-10-21 18:52:59 -070024#include "UniquePtr.h"
Carl Shapiro61e019d2011-07-14 16:53:09 -070025
Elliott Hughes90a33692011-08-30 13:27:07 -070026// TODO: this drags in cutil/log.h, which conflicts with our logging.h.
27#include "JniConstants.h"
28
Carl Shapiro1fb86202011-06-27 17:43:13 -070029namespace art {
30
Carl Shapiro2ed144c2011-07-26 16:52:08 -070031Runtime* Runtime::instance_ = NULL;
32
Elliott Hughesdcc24742011-09-07 14:02:44 -070033Runtime::Runtime()
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080034 : is_zygote_(false),
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -070035 default_stack_size_(Thread::kDefaultStackSize),
Elliott Hughesc33a32b2011-10-11 18:18:07 -070036 monitor_list_(NULL),
Elliott Hughesdcc24742011-09-07 14:02:44 -070037 thread_list_(NULL),
38 intern_table_(NULL),
39 class_linker_(NULL),
40 signal_catcher_(NULL),
41 java_vm_(NULL),
Brian Carlstrom16192862011-09-12 17:50:06 -070042 jni_stub_array_(NULL),
Brian Carlstrome24fa612011-09-29 00:53:55 -070043 abstract_method_error_stub_array_(NULL),
Elliott Hughesdcc24742011-09-07 14:02:44 -070044 started_(false),
45 vfprintf_(NULL),
46 exit_(NULL),
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070047 abort_(NULL),
48 stats_enabled_(false) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -070049 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
50 resolution_stub_array_[i] = NULL;
51 }
52 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
53 callee_save_method_[i] = NULL;
54 }
Elliott Hughesdcc24742011-09-07 14:02:44 -070055}
56
Carl Shapiro61e019d2011-07-14 16:53:09 -070057Runtime::~Runtime() {
Elliott Hughesd1cc8362011-10-24 16:58:50 -070058 Dbg::StopJdwp();
59
Elliott Hughes5fe594f2011-09-08 12:33:17 -070060 // Make sure our internal threads are dead before we start tearing down things they're using.
61 delete signal_catcher_;
Elliott Hughes038a8062011-09-18 14:12:41 -070062 // TODO: GC thread.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070063
Elliott Hughes038a8062011-09-18 14:12:41 -070064 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
Elliott Hughes93e74e82011-09-13 11:07:03 -070065 delete thread_list_;
Elliott Hughesc33a32b2011-10-11 18:18:07 -070066 delete monitor_list_;
Elliott Hughes93e74e82011-09-13 11:07:03 -070067
Carl Shapiro61e019d2011-07-14 16:53:09 -070068 delete class_linker_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070069 Heap::Destroy();
Elliott Hughescf4c6c42011-09-01 15:16:42 -070070 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070071 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -070072 Thread::Shutdown();
Carl Shapiro4acf4642011-07-26 18:54:13 -070073 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070074 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -070075 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -070076}
77
Elliott Hughescac6cc72011-11-03 20:31:21 -070078static bool gAborting = false;
79
Elliott Hughese0918552011-10-28 17:18:29 -070080struct AbortState {
81 void Dump(std::ostream& os) {
Elliott Hughescac6cc72011-11-03 20:31:21 -070082 if (gAborting) {
83 os << "Runtime aborting --- recursively, so no thread-specific detail!\n";
84 return;
85 }
86 gAborting = true;
Elliott Hughese0918552011-10-28 17:18:29 -070087 os << "Runtime aborting...\n";
88 Thread* self = Thread::Current();
89 if (self == NULL) {
90 os << "(Aborting thread was not attached to runtime!)\n";
91 } else {
92 self->Dump(os, true);
93 }
94 }
95};
96
Elliott Hughesffe67362011-07-17 12:09:27 -070097void Runtime::Abort(const char* file, int line) {
98 // Get any pending output out of the way.
99 fflush(NULL);
100
101 // Many people have difficulty distinguish aborts from crashes,
102 // so be explicit.
Elliott Hughese0918552011-10-28 17:18:29 -0700103 AbortState state;
104 LOG(ERROR) << Dumpable<AbortState>(state);
Elliott Hughesffe67362011-07-17 12:09:27 -0700105
Elliott Hughesffe67362011-07-17 12:09:27 -0700106 // Perform any platform-specific pre-abort actions.
107 PlatformAbort(file, line);
108
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700109 // use abort hook if we have one
110 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
111 Runtime::Current()->abort_();
112 // notreached
113 }
114
Elliott Hughesffe67362011-07-17 12:09:27 -0700115 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -0700116 // receive SIGABRT. debuggerd dumps the stack trace of the main
117 // thread, whether or not that was the thread that failed. By
118 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -0700119 // fault in the current thread, and get a useful log from debuggerd.
120 // We can also trivially tell the difference between a VM crash and
121 // a deliberate abort by looking at the fault address.
122 *reinterpret_cast<char*>(0xdeadd00d) = 38;
123 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -0700124 // notreached
125}
126
Elliott Hughesbf86d042011-08-31 17:53:14 -0700127void Runtime::CallExitHook(jint status) {
128 if (exit_ != NULL) {
129 ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative);
130 exit_(status);
131 LOG(WARNING) << "Exit hook returned instead of exiting!";
132 }
133}
134
Brian Carlstrom8a436592011-08-15 21:27:23 -0700135// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
136// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
137// [gG] gigabytes.
138//
139// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700140// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
141// of 1024.
142//
143// The spec says the -Xmx and -Xms options must be multiples of 1024. It
144// doesn't say anything about -Xss.
145//
146// Returns 0 (a useless size) if "s" is malformed or specifies a low or
147// non-evenly-divisible value.
148//
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800149size_t ParseMemoryOption(const char* s, size_t div) {
Brian Carlstrom8a436592011-08-15 21:27:23 -0700150 // strtoul accepts a leading [+-], which we don't want,
151 // so make sure our string starts with a decimal digit.
152 if (isdigit(*s)) {
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800153 const char* s2;
154 size_t val = strtoul(s, (char**)&s2, 10);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700155 if (s2 != s) {
156 // s2 should be pointing just after the number.
157 // If this is the end of the string, the user
158 // has specified a number of bytes. Otherwise,
159 // there should be exactly one more character
160 // that specifies a multiplier.
161 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700162 // The remainder of the string is either a single multiplier
163 // character, or nothing to indicate that the value is in
164 // bytes.
165 char c = *s2++;
166 if (*s2 == '\0') {
167 size_t mul;
168 if (c == '\0') {
169 mul = 1;
170 } else if (c == 'k' || c == 'K') {
171 mul = KB;
172 } else if (c == 'm' || c == 'M') {
173 mul = MB;
174 } else if (c == 'g' || c == 'G') {
175 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700176 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700177 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700178 return 0;
179 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700180
181 if (val <= std::numeric_limits<size_t>::max() / mul) {
182 val *= mul;
183 } else {
184 // Clamp to a multiple of 1024.
185 val = std::numeric_limits<size_t>::max() & ~(1024-1);
186 }
187 } else {
188 // There's more than one character after the numeric part.
189 return 0;
190 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700191 }
192 // The man page says that a -Xm value must be a multiple of 1024.
193 if (val % div == 0) {
194 return val;
195 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700196 }
197 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700198 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700199}
200
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700201size_t ParseIntegerOrDie(const StringPiece& s) {
202 StringPiece::size_type colon = s.find(':');
203 if (colon == StringPiece::npos) {
204 LOG(FATAL) << "Missing integer: " << s;
205 }
206 const char* begin = &s.data()[colon + 1];
207 char* end;
208 size_t result = strtoul(begin, &end, 10);
209 if (begin == end || *end != '\0') {
210 LOG(FATAL) << "Failed to parse integer in: " << s;
211 }
212 return result;
213}
214
Elliott Hughes0af55432011-08-17 18:37:28 -0700215void LoadJniLibrary(JavaVMExt* vm, const char* name) {
216 // TODO: OS_SHARED_LIB_FORMAT_STR
217 std::string mapped_name(StringPrintf("lib%s.so", name));
Elliott Hughes75770752011-08-24 17:52:38 -0700218 std::string reason;
219 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700220 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
221 << reason;
222 }
223}
224
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700225Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700226 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700227 bool compiler = false;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700228 const char* boot_class_path = getenv("BOOTCLASSPATH");
229 if (boot_class_path != NULL) {
230 parsed->boot_class_path_ = getenv("BOOTCLASSPATH");
231 }
232 const char* class_path = getenv("CLASSPATH");
233 if (class_path != NULL) {
234 parsed->class_path_ = getenv("CLASSPATH");
235 }
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700236#ifdef NDEBUG
Elliott Hughes5174fe62011-08-23 15:12:35 -0700237 // -Xcheck:jni is off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700238 parsed->check_jni_ = false;
239#else
240 // ...but on by default in debug builds.
241 parsed->check_jni_ = true;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700242#endif
Elliott Hughes85d15452011-09-16 17:33:01 -0700243
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700244 parsed->heap_initial_size_ = Heap::kInitialSize;
245 parsed->heap_maximum_size_ = Heap::kMaximumSize;
jeffhaoc1160702011-10-27 15:48:45 -0700246 parsed->heap_growth_limit_ = 0; // 0 means no growth limit
Brian Carlstromf734cf52011-08-17 16:28:14 -0700247 parsed->stack_size_ = Thread::kDefaultStackSize;
248
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700249 parsed->is_zygote_ = false;
250
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700251 parsed->jni_globals_max_ = 0;
Elliott Hughesfc861622011-10-17 17:57:47 -0700252 parsed->lock_profiling_threshold_ = 0;
253 parsed->hook_is_sensitive_thread_ = NULL;
254
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700255 parsed->hook_vfprintf_ = vfprintf;
256 parsed->hook_exit_ = exit;
257 parsed->hook_abort_ = abort;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700258
259 for (size_t i = 0; i < options.size(); ++i) {
260 const StringPiece& option = options[i].first;
Brian Carlstrom0796af02011-10-12 14:31:45 -0700261 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700262 LOG(INFO) << "option[" << i << "]=" << option;
263 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700264 if (option.starts_with("-Xbootclasspath:")) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700265 parsed->boot_class_path_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700266 } else if (option == "-classpath" || option == "-cp") {
267 // TODO: support -Djava.class.path
268 i++;
269 if (i == options.size()) {
270 // TODO: usage
271 LOG(FATAL) << "Missing required class path value for " << option;
272 return NULL;
273 }
274 const StringPiece& value = options[i].first;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700275 parsed->class_path_ = value.data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700276 } else if (option.starts_with("-Ximage:")) {
277 parsed->images_.push_back(option.substr(strlen("-Ximage:")).data());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700278 } else if (option.starts_with("-Xcheck:jni")) {
279 parsed->check_jni_ = true;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700280 } else if (option.starts_with("-Xrunjdwp:") || option.starts_with("-agentlib:jdwp=")) {
281 std::string tail = option.substr(option[1] == 'X' ? 10 : 15).ToString();
282 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
283 LOG(FATAL) << "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
284 << "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n";
285 return NULL;
286 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700287 } else if (option.starts_with("-Xms")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700288 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024);
289 if (size == 0) {
290 if (ignore_unrecognized) {
291 continue;
292 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700293 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700294 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700295 return NULL;
296 }
297 parsed->heap_initial_size_ = size;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700298 } else if (option.starts_with("-Xmx")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700299 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024);
300 if (size == 0) {
301 if (ignore_unrecognized) {
302 continue;
303 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700304 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700305 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700306 return NULL;
307 }
308 parsed->heap_maximum_size_ = size;
jeffhaoc1160702011-10-27 15:48:45 -0700309 } else if (option.starts_with("-XX:HeapGrowthLimit=")) {
310 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).data(), 1024);
311 if (size == 0) {
312 if (ignore_unrecognized) {
313 continue;
314 }
315 // TODO: usage
316 LOG(FATAL) << "Failed to parse " << option;
317 return NULL;
318 }
319 parsed->heap_growth_limit_ = size;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700320 } else if (option.starts_with("-Xss")) {
321 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1);
322 if (size == 0) {
323 if (ignore_unrecognized) {
324 continue;
325 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700326 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700327 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700328 return NULL;
329 }
330 parsed->stack_size_ = size;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700331 } else if (option.starts_with("-D")) {
332 parsed->properties_.push_back(option.substr(strlen("-D")).data());
Elliott Hughesa0957642011-09-02 14:27:33 -0700333 } else if (option.starts_with("-Xjnitrace:")) {
334 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:")).data();
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700335 } else if (option == "compiler") {
336 compiler = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700337 } else if (option == "-Xzygote") {
338 parsed->is_zygote_ = true;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700339 } else if (option.starts_with("-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700340 std::vector<std::string> verbose_options;
Elliott Hughes34023802011-08-30 12:06:17 -0700341 Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700342 for (size_t i = 0; i < verbose_options.size(); ++i) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800343 if (verbose_options[i] == "class") {
344 gLogVerbosity.class_linker = true;
345 } else if (verbose_options[i] == "compiler") {
346 gLogVerbosity.compiler = true;
347 } else if (verbose_options[i] == "heap") {
348 gLogVerbosity.heap = true;
349 } else if (verbose_options[i] == "gc") {
350 gLogVerbosity.gc = true;
351 } else if (verbose_options[i] == "jdwp") {
352 gLogVerbosity.jdwp = true;
353 } else if (verbose_options[i] == "jni") {
354 gLogVerbosity.jni = true;
355 } else if (verbose_options[i] == "monitor") {
356 gLogVerbosity.monitor = true;
357 } else if (verbose_options[i] == "startup") {
358 gLogVerbosity.startup = true;
359 } else if (verbose_options[i] == "third-party-jni") {
360 gLogVerbosity.third_party_jni = true;
361 } else if (verbose_options[i] == "threads") {
362 gLogVerbosity.threads = true;
363 } else {
364 LOG(WARNING) << "Ignoring unknown -verbose option: " << verbose_options[i];
365 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700366 }
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700367 } else if (option.starts_with("-Xjnigreflimit:")) {
368 parsed->jni_globals_max_ = ParseIntegerOrDie(option);
Elliott Hughesfc861622011-10-17 17:57:47 -0700369 } else if (option.starts_with("-Xlockprofthreshold:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700370 parsed->lock_profiling_threshold_ = ParseIntegerOrDie(option);
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700371 } else if (option.starts_with("-Xstacktracefile:")) {
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700372// always show stack traces in debug builds
373#ifdef NDEBUG
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700374 parsed->stack_trace_file_ = option.substr(strlen("-Xstacktracefile:")).data();
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700375#endif
Elliott Hughesfc861622011-10-17 17:57:47 -0700376 } else if (option == "sensitiveThread") {
377 parsed->hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(options[i].second);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700378 } else if (option == "vfprintf") {
379 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
380 } else if (option == "exit") {
381 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
382 } else if (option == "abort") {
383 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700384 } else if (option == "host-prefix") {
385 parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700386 } else {
387 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700388 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700389 LOG(ERROR) << "Unrecognized option " << option;
390 // TODO: this should exit, but for now tolerate unknown options
391 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700392 }
393 }
394 }
395
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700396 if (!compiler && parsed->images_.empty()) {
397 parsed->images_.push_back("/data/art-cache/boot.art");
398 }
jeffhaoc1160702011-10-27 15:48:45 -0700399 if (parsed->heap_growth_limit_ == 0) {
400 parsed->heap_growth_limit_ = parsed->heap_maximum_size_;
401 }
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700402
Elliott Hughescac6cc72011-11-03 20:31:21 -0700403 LOG(INFO) << "Build type: "
404#ifndef NDEBUG
405 << "debug"
406#else
407 << "optimized"
408#endif
409 << "; CheckJNI: " << (parsed->check_jni_ ? "on" : "off");
Elliott Hughes85d15452011-09-16 17:33:01 -0700410
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700411 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700412}
413
414Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700415 // TODO: acquire a static mutex on Runtime to avoid racing.
416 if (Runtime::instance_ != NULL) {
417 return NULL;
418 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700419 instance_ = new Runtime;
420 if (!instance_->Init(options, ignore_unrecognized)) {
421 delete instance_;
422 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700423 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700424 return instance_;
425}
Elliott Hughes0af55432011-08-17 18:37:28 -0700426
Brian Carlstromaded5f72011-10-07 17:15:04 -0700427void CreateSystemClassLoader() {
428 if (ClassLoader::UseCompileTimeClassPath()) {
429 return;
430 }
431
432 Thread* self = Thread::Current();
433
434 // Must be in the kNative state for calling native methods.
435 CHECK_EQ(self->GetState(), Thread::kNative);
436
437 JNIEnv* env = self->GetJniEnv();
Brian Carlstromdf143242011-10-10 18:05:34 -0700438 ScopedLocalRef<jclass> ClassLoader_class(env, env->FindClass("java/lang/ClassLoader"));
439 CHECK(ClassLoader_class.get() != NULL);
440 jmethodID getSystemClassLoader = env->GetStaticMethodID(ClassLoader_class.get(),
441 "getSystemClassLoader",
442 "()Ljava/lang/ClassLoader;");
443 CHECK(getSystemClassLoader != NULL);
444 ScopedLocalRef<jobject> class_loader(env, env->CallStaticObjectMethod(ClassLoader_class.get(),
445 getSystemClassLoader));
446 CHECK(class_loader.get() != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700447
Brian Carlstromdf143242011-10-10 18:05:34 -0700448 Thread::Current()->SetClassLoaderOverride(Decode<ClassLoader*>(env, class_loader.get()));
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500449
450 ScopedLocalRef<jclass> Thread_class(env, env->FindClass("java/lang/Thread"));
451 CHECK(Thread_class.get() != NULL);
452 jfieldID contextClassLoader = env->GetFieldID(Thread_class.get(),
453 "contextClassLoader",
454 "Ljava/lang/ClassLoader;");
455 CHECK(contextClassLoader != NULL);
456 ScopedLocalRef<jobject> self_jobject(env, AddLocalReference<jobject>(env, self->GetPeer()));
457 env->SetObjectField(self_jobject.get(), contextClassLoader, class_loader.get());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700458}
459
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700460void Runtime::Start() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800461 VLOG(startup) << "Runtime::Start entering";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700462
463 CHECK(host_prefix_.empty()) << host_prefix_;
464
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700465 // Restore main thread state to kNative as expected by native code
466 Thread::Current()->SetState(Thread::kNative);
467
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700468 started_ = true;
469
Brian Carlstromae826982011-11-09 01:33:42 -0800470 // InitNativeMethods needs to be after started_ so that the classes
471 // it touches will have methods linked to the oat file if necessary.
472 InitNativeMethods();
473
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500474 Thread::FinishStartup();
475
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700476 if (!is_zygote_) {
477 DidForkFromZygote();
478 }
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700479
Elliott Hughes85d15452011-09-16 17:33:01 -0700480 StartDaemonThreads();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700481
Brian Carlstromaded5f72011-10-07 17:15:04 -0700482 CreateSystemClassLoader();
483
Elliott Hughes726079d2011-10-07 18:43:44 -0700484 Thread::Current()->GetJniEnv()->locals.AssertEmpty();
485
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800486 VLOG(startup) << "Runtime::Start exiting";
Elliott Hughes85d15452011-09-16 17:33:01 -0700487}
488
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700489void Runtime::DidForkFromZygote() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700490 is_zygote_ = false;
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700491
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700492 StartSignalCatcher();
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700493
494 // Start the JDWP thread. If the command-line debugger flags specified "suspend=y",
495 // this will pause the runtime, so we probably want this to come last.
496 Dbg::StartJdwp();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700497}
498
499void Runtime::StartSignalCatcher() {
500 if (!is_zygote_) {
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700501 signal_catcher_ = new SignalCatcher(stack_trace_file_);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700502 }
503}
504
Elliott Hughes85d15452011-09-16 17:33:01 -0700505void Runtime::StartDaemonThreads() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800506 VLOG(startup) << "Runtime::StartDaemonThreads entering";
Elliott Hughes85d15452011-09-16 17:33:01 -0700507
Elliott Hughes719b3232011-09-25 17:42:19 -0700508 Thread* self = Thread::Current();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700509
510 // Must be in the kNative state for calling native methods.
511 CHECK_EQ(self->GetState(), Thread::kNative);
512
Elliott Hughes719b3232011-09-25 17:42:19 -0700513 JNIEnv* env = self->GetJniEnv();
Elliott Hughes726079d2011-10-07 18:43:44 -0700514 ScopedLocalRef<jclass> c(env, env->FindClass("java/lang/Daemons"));
515 CHECK(c.get() != NULL);
516 jmethodID mid = env->GetStaticMethodID(c.get(), "start", "()V");
Elliott Hughes719b3232011-09-25 17:42:19 -0700517 CHECK(mid != NULL);
Elliott Hughes726079d2011-10-07 18:43:44 -0700518 env->CallStaticVoidMethod(c.get(), mid);
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700519
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800520 VLOG(startup) << "Runtime::StartDaemonThreads exiting";
Carl Shapiro61e019d2011-07-14 16:53:09 -0700521}
522
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700523bool Runtime::IsStarted() const {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700524 return started_;
525}
526
Elliott Hughes0af55432011-08-17 18:37:28 -0700527bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700528 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700529
Elliott Hughes90a33692011-08-30 13:27:07 -0700530 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
531 if (options.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700532 LOG(ERROR) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700533 return false;
534 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800535 VLOG(startup) << "Runtime::Init -verbose:startup enabled";
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700536
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700537 SetJniGlobalsMax(options->jni_globals_max_);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800538 Monitor::Init(options->lock_profiling_threshold_, options->hook_is_sensitive_thread_);
Elliott Hughes32d6e1e2011-10-11 14:47:44 -0700539
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700540 host_prefix_ = options->host_prefix_;
541 boot_class_path_ = options->boot_class_path_;
542 class_path_ = options->class_path_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700543 properties_ = options->properties_;
544
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700545 is_zygote_ = options->is_zygote_;
546
Elliott Hughes0af55432011-08-17 18:37:28 -0700547 vfprintf_ = options->hook_vfprintf_;
548 exit_ = options->hook_exit_;
549 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700550
Elliott Hughesbe759c62011-09-08 19:38:21 -0700551 default_stack_size_ = options->stack_size_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700552 stack_trace_file_ = options->stack_trace_file_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700553
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700554 monitor_list_ = new MonitorList;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800555 thread_list_ = new ThreadList;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700556 intern_table_ = new InternTable;
557
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800558 Heap::Init(options->heap_initial_size_,
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700559 options->heap_maximum_size_,
jeffhaoc1160702011-10-27 15:48:45 -0700560 options->heap_growth_limit_,
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700561 options->images_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700562
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700563 BlockSignals();
564
Elliott Hughesa0957642011-09-02 14:27:33 -0700565 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700566
Elliott Hughesbe759c62011-09-08 19:38:21 -0700567 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700568
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700569 // ClassLinker needs an attached thread, but we can't fully attach a thread
570 // without creating objects.
571 Thread::Attach(this, "main", false);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700572
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700573 // Set us to runnable so tools using a runtime can allocate and GC by default
574 Thread::Current()->SetState(Thread::kRunnable);
575
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700576 CHECK_GE(Heap::GetSpaces().size(), 1U);
577 class_linker_ = ((Heap::GetSpaces()[0]->IsImageSpace())
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800578 ? ClassLinker::Create(intern_table_)
579 : ClassLinker::Create(options->boot_class_path_, intern_table_));
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700580
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800581 VLOG(startup) << "Runtime::Init exiting";
Carl Shapiro1fb86202011-06-27 17:43:13 -0700582 return true;
583}
584
Elliott Hughes038a8062011-09-18 14:12:41 -0700585void Runtime::InitNativeMethods() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800586 VLOG(startup) << "Runtime::InitNativeMethods entering";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700587 Thread* self = Thread::Current();
588 JNIEnv* env = self->GetJniEnv();
589
Elliott Hughes418d20f2011-09-22 14:00:39 -0700590 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Brian Carlstromaded5f72011-10-07 17:15:04 -0700591 CHECK_EQ(self->GetState(), Thread::kNative);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700592
Elliott Hughes418d20f2011-09-22 14:00:39 -0700593 // First set up JniConstants, which is used by both the runtime's built-in native
594 // methods and libcore.
Elliott Hughesfea966e2011-09-22 10:26:20 -0700595 JniConstants::init(env);
596
Elliott Hughes418d20f2011-09-22 14:00:39 -0700597 // Then set up the native methods provided by the runtime itself.
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700598 RegisterRuntimeNativeMethods(env);
599
Elliott Hughes418d20f2011-09-22 14:00:39 -0700600 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
601 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
602 // the library that implements System.loadLibrary!
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700603 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800604 VLOG(startup) << "Runtime::InitNativeMethods exiting";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700605}
606
607void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
608#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700609 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700610 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700611 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700612 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700613 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700614 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700615 REGISTER(register_java_lang_Object);
616 REGISTER(register_java_lang_Runtime);
617 REGISTER(register_java_lang_String);
618 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700619 REGISTER(register_java_lang_Thread);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700620 REGISTER(register_java_lang_Throwable);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700621 REGISTER(register_java_lang_VMClassLoader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700622 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700623 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700624 REGISTER(register_java_lang_reflect_Field);
625 REGISTER(register_java_lang_reflect_Method);
Jesse Wilson95caa792011-10-12 18:14:17 -0400626 REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700627 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700628 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700629 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700630 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700631#undef REGISTER
632}
633
Elliott Hughes8daa0922011-09-11 13:46:25 -0700634void Runtime::Dump(std::ostream& os) {
Elliott Hughese27955c2011-08-26 15:21:24 -0700635 // TODO: dump other runtime statistics?
Elliott Hughescac6cc72011-11-03 20:31:21 -0700636 GetClassLinker()->DumpForSigQuit(os);
637 GetInternTable()->DumpForSigQuit(os);
Elliott Hughes42ee1422011-09-06 12:33:32 -0700638 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700639
640 thread_list_->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700641}
642
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800643void Runtime::DumpLockHolders(std::ostream& os) {
644 pid_t heap_lock_owner = Heap::GetLockOwner();
645 pid_t thread_list_lock_owner = GetThreadList()->GetLockOwner();
646 pid_t classes_lock_owner = GetClassLinker()->GetClassesLockOwner();
647 pid_t dex_lock_owner = GetClassLinker()->GetDexLockOwner();
648 if ((heap_lock_owner | thread_list_lock_owner | classes_lock_owner | dex_lock_owner) != 0) {
649 os << "Heap lock owner tid: " << heap_lock_owner << "\n"
650 << "ThreadList lock owner tid: " << thread_list_lock_owner << "\n"
651 << "ClassLinker classes lock owner tid: " << classes_lock_owner << "\n"
652 << "ClassLinker dex lock owner tid: " << dex_lock_owner << "\n";
653 }
654}
655
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700656void Runtime::SetStatsEnabled(bool new_state) {
657 if (new_state == true) {
658 GetStats()->Clear(~0);
659 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
660 Thread::Current()->GetStats()->Clear(~0);
661 }
662 stats_enabled_ = new_state;
663}
664
665void Runtime::ResetStats(int kinds) {
666 GetStats()->Clear(kinds & 0xffff);
667 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
668 Thread::Current()->GetStats()->Clear(kinds >> 16);
669}
670
671RuntimeStats* Runtime::GetStats() {
672 return &stats_;
673}
674
675int32_t Runtime::GetStat(int kind) {
676 RuntimeStats* stats;
677 if (kind < (1<<16)) {
678 stats = GetStats();
679 } else {
680 stats = Thread::Current()->GetStats();
681 kind >>= 16;
682 }
683 switch (kind) {
684 case KIND_ALLOCATED_OBJECTS:
685 return stats->allocated_objects;
686 case KIND_ALLOCATED_BYTES:
687 return stats->allocated_bytes;
688 case KIND_FREED_OBJECTS:
689 return stats->freed_objects;
690 case KIND_FREED_BYTES:
691 return stats->freed_bytes;
692 case KIND_GC_INVOCATIONS:
693 return stats->gc_for_alloc_count;
694 case KIND_CLASS_INIT_COUNT:
695 return stats->class_init_count;
696 case KIND_CLASS_INIT_TIME:
697 // Convert ns to us, reduce to 32 bits.
698 return (int) (stats->class_init_time_ns / 1000);
699 case KIND_EXT_ALLOCATED_OBJECTS:
700 case KIND_EXT_ALLOCATED_BYTES:
701 case KIND_EXT_FREED_OBJECTS:
702 case KIND_EXT_FREED_BYTES:
703 return 0; // backward compatibility
704 default:
705 CHECK(false);
706 return -1; // unreachable
707 }
708}
709
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700710void Runtime::BlockSignals() {
711 sigset_t sigset;
712 if (sigemptyset(&sigset) == -1) {
713 PLOG(FATAL) << "sigemptyset failed";
714 }
715 if (sigaddset(&sigset, SIGPIPE) == -1) {
716 PLOG(ERROR) << "sigaddset SIGPIPE failed";
717 }
718 // SIGQUIT is used to dump the runtime's state (including stack traces).
719 if (sigaddset(&sigset, SIGQUIT) == -1) {
720 PLOG(ERROR) << "sigaddset SIGQUIT failed";
721 }
722 // SIGUSR1 is used to initiate a heap dump.
723 if (sigaddset(&sigset, SIGUSR1) == -1) {
724 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
725 }
726 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
727}
728
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700729void Runtime::AttachCurrentThread(const char* name, bool as_daemon) {
730 Thread::Attach(instance_, name, as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700731}
732
Elliott Hughesd92bec42011-09-02 17:04:36 -0700733void Runtime::DetachCurrentThread() {
Elliott Hughes038a8062011-09-18 14:12:41 -0700734 // TODO: check we're not calling DetachCurrentThread from a call stack that
735 // includes managed frames. (It's only valid if the stack is all-native.)
Elliott Hughes02b48d12011-09-07 17:15:51 -0700736 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700737}
738
Brian Carlstrome24fa612011-09-29 00:53:55 -0700739void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700740 Dbg::VisitRoots(visitor, arg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700741 class_linker_->VisitRoots(visitor, arg);
742 intern_table_->VisitRoots(visitor, arg);
743 java_vm_->VisitRoots(visitor, arg);
744 thread_list_->VisitRoots(visitor, arg);
745 visitor(jni_stub_array_, arg);
746 visitor(abstract_method_error_stub_array_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700747 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
748 visitor(resolution_stub_array_[i], arg);
749 }
750 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
751 visitor(callee_save_method_[i], arg);
752 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700753}
754
Ian Rogers169c9a72011-11-13 20:13:17 -0800755bool Runtime::HasJniDlsymLookupStub() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700756 return jni_stub_array_ != NULL;
757}
758
Ian Rogers169c9a72011-11-13 20:13:17 -0800759ByteArray* Runtime::GetJniDlsymLookupStub() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700760 CHECK(jni_stub_array_ != NULL);
761 return jni_stub_array_;
762}
763
Ian Rogers169c9a72011-11-13 20:13:17 -0800764void Runtime::SetJniDlsymLookupStub(ByteArray* jni_stub_array) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700765 CHECK(jni_stub_array != NULL) << " jni_stub_array=" << jni_stub_array;
766 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array)
767 << "jni_stub_array_=" << jni_stub_array_ << " jni_stub_array=" << jni_stub_array;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700768 jni_stub_array_ = jni_stub_array;
769}
770
771bool Runtime::HasAbstractMethodErrorStubArray() const {
772 return abstract_method_error_stub_array_ != NULL;
773}
774
775ByteArray* Runtime::GetAbstractMethodErrorStubArray() const {
776 CHECK(abstract_method_error_stub_array_ != NULL);
777 return abstract_method_error_stub_array_;
778}
779
780void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) {
781 CHECK(abstract_method_error_stub_array != NULL);
782 CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array);
783 abstract_method_error_stub_array_ = abstract_method_error_stub_array;
784}
785
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700786
787Runtime::TrampolineType Runtime::GetTrampolineType(Method* method) {
788 if (method == NULL) {
789 return Runtime::kUnknownMethod;
790 } else if (method->IsStatic()) {
791 return Runtime::kStaticMethod;
792 } else {
793 return Runtime::kInstanceMethod;
794 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700795}
796
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700797bool Runtime::HasResolutionStubArray(TrampolineType type) const {
798 return resolution_stub_array_[type] != NULL;
Ian Rogersad25ac52011-10-04 19:13:33 -0700799}
800
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700801ByteArray* Runtime::GetResolutionStubArray(TrampolineType type) const {
802 CHECK(HasResolutionStubArray(type));
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700803 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastTrampolineMethodType));
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700804 return resolution_stub_array_[type];
805}
806
807void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700808 CHECK(resolution_stub_array != NULL);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700809 CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array);
810 resolution_stub_array_[type] = resolution_stub_array;
Ian Rogersad25ac52011-10-04 19:13:33 -0700811}
812
Brian Carlstromae826982011-11-09 01:33:42 -0800813Method* Runtime::CreateCalleeSaveMethod(InstructionSet instruction_set, CalleeSaveType type) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700814 Class* method_class = Method::GetMethodClass();
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700815 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
Ian Rogersff1ed472011-09-20 13:46:24 -0700816 method->SetDeclaringClass(method_class);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800817 // TODO: use a special method for callee saves
818 method->SetMethodIndex(DexFile::kDexNoIndex16);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700819 method->SetCode(NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800820 if ((instruction_set == kThumb2) || (instruction_set == kArm)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700821 uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6) | (1 << art::arm::R7) |
822 (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11);
823 uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3);
824 uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9);
825 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills :0) |
826 (type == kSaveAll ? all_spills :0) | (1 << art::arm::LR);
827 uint32_t fp_all_spills = (1 << art::arm::S0) | (1 << art::arm::S1) | (1 << art::arm::S2) |
828 (1 << art::arm::S3) | (1 << art::arm::S4) | (1 << art::arm::S5) |
829 (1 << art::arm::S6) | (1 << art::arm::S7) | (1 << art::arm::S8) |
830 (1 << art::arm::S9) | (1 << art::arm::S10) | (1 << art::arm::S11) |
831 (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) |
832 (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) |
833 (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) |
834 (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) |
835 (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) |
836 (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
837 (1 << art::arm::S30) | (1 << art::arm::S31);
838 uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
839 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
840 __builtin_popcount(fp_spills) /* fprs */ +
841 1 /* Method* */) * kPointerSize, kStackAlignment);
Ian Rogers15fdb8c2011-09-25 15:45:07 -0700842 method->SetFrameSizeInBytes(frame_size);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700843 method->SetCoreSpillMask(core_spills);
844 method->SetFpSpillMask(fp_spills);
Brian Carlstromae826982011-11-09 01:33:42 -0800845 } else if (instruction_set == kX86) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700846 method->SetFrameSizeInBytes(32);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700847 method->SetCoreSpillMask((1 << art::x86::EBX) | (1 << art::x86::EBP) | (1 << art::x86::ESI) |
Ian Rogersff1ed472011-09-20 13:46:24 -0700848 (1 << art::x86::EDI));
849 method->SetFpSpillMask(0);
850 } else {
851 UNIMPLEMENTED(FATAL);
852 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700853 return method.get();
Ian Rogersff1ed472011-09-20 13:46:24 -0700854}
855
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700856bool Runtime::HasCalleeSaveMethod(CalleeSaveType type) const {
857 return callee_save_method_[type] != NULL;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700858}
859
Brian Carlstrome24fa612011-09-29 00:53:55 -0700860// Returns a special method that describes all callee saves being spilled to the stack.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700861Method* Runtime::GetCalleeSaveMethod(CalleeSaveType type) const {
862 CHECK(HasCalleeSaveMethod(type));
863 return callee_save_method_[type];
Brian Carlstrome24fa612011-09-29 00:53:55 -0700864}
865
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700866void Runtime::SetCalleeSaveMethod(Method* method, CalleeSaveType type) {
867 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
868 callee_save_method_[type] = method;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700869}
870
Carl Shapiro1fb86202011-06-27 17:43:13 -0700871} // namespace art