blob: 9c923fb9137f722e4302562885167ca9f68f7f5d [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()
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -070034 : verbose_startup_(false),
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -070035 is_zygote_(false),
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -070036 default_stack_size_(Thread::kDefaultStackSize),
Elliott Hughesc33a32b2011-10-11 18:18:07 -070037 monitor_list_(NULL),
Elliott Hughesdcc24742011-09-07 14:02:44 -070038 thread_list_(NULL),
39 intern_table_(NULL),
40 class_linker_(NULL),
41 signal_catcher_(NULL),
42 java_vm_(NULL),
Brian Carlstrom16192862011-09-12 17:50:06 -070043 jni_stub_array_(NULL),
Brian Carlstrome24fa612011-09-29 00:53:55 -070044 abstract_method_error_stub_array_(NULL),
Elliott Hughesdcc24742011-09-07 14:02:44 -070045 started_(false),
46 vfprintf_(NULL),
47 exit_(NULL),
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070048 abort_(NULL),
49 stats_enabled_(false) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -070050 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
51 resolution_stub_array_[i] = NULL;
52 }
53 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
54 callee_save_method_[i] = NULL;
55 }
Elliott Hughesdcc24742011-09-07 14:02:44 -070056}
57
Carl Shapiro61e019d2011-07-14 16:53:09 -070058Runtime::~Runtime() {
Elliott Hughesd1cc8362011-10-24 16:58:50 -070059 Dbg::StopJdwp();
60
Elliott Hughes5fe594f2011-09-08 12:33:17 -070061 // Make sure our internal threads are dead before we start tearing down things they're using.
62 delete signal_catcher_;
Elliott Hughes038a8062011-09-18 14:12:41 -070063 // TODO: GC thread.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070064
Elliott Hughes038a8062011-09-18 14:12:41 -070065 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
Elliott Hughes93e74e82011-09-13 11:07:03 -070066 delete thread_list_;
Elliott Hughesc33a32b2011-10-11 18:18:07 -070067 delete monitor_list_;
Elliott Hughes93e74e82011-09-13 11:07:03 -070068
Carl Shapiro61e019d2011-07-14 16:53:09 -070069 delete class_linker_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070070 Heap::Destroy();
Elliott Hughescf4c6c42011-09-01 15:16:42 -070071 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070072 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -070073 Thread::Shutdown();
Carl Shapiro4acf4642011-07-26 18:54:13 -070074 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070075 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -070076 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -070077}
78
Elliott Hughesffe67362011-07-17 12:09:27 -070079void Runtime::Abort(const char* file, int line) {
80 // Get any pending output out of the way.
81 fflush(NULL);
82
83 // Many people have difficulty distinguish aborts from crashes,
84 // so be explicit.
Ian Rogers9074b992011-10-26 17:41:55 -070085 {
86 LogMessage log(file, line, ERROR, -1);
87 log.stream() << "Runtime aborting..." << std::endl;
88 // Add Java stack trace if possible
89 Thread* thread = Thread::Current();
90 if (thread != NULL) {
91 log.stream() << "Java stack trace of aborting thread:" << std::endl;
92 thread->DumpStack(log.stream());
93 if (thread->IsExceptionPending()) {
94 Throwable* e = thread->GetException();
95 log.stream() << "Pending exception on thread: " << PrettyTypeOf(e) << std::endl;
96 log.stream() << e->Dump();
97 }
98 }
99 }
Elliott Hughesffe67362011-07-17 12:09:27 -0700100
Elliott Hughesffe67362011-07-17 12:09:27 -0700101 // Perform any platform-specific pre-abort actions.
102 PlatformAbort(file, line);
103
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700104 // use abort hook if we have one
105 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
106 Runtime::Current()->abort_();
107 // notreached
108 }
109
Elliott Hughesffe67362011-07-17 12:09:27 -0700110 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -0700111 // receive SIGABRT. debuggerd dumps the stack trace of the main
112 // thread, whether or not that was the thread that failed. By
113 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -0700114 // fault in the current thread, and get a useful log from debuggerd.
115 // We can also trivially tell the difference between a VM crash and
116 // a deliberate abort by looking at the fault address.
117 *reinterpret_cast<char*>(0xdeadd00d) = 38;
118 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -0700119 // notreached
120}
121
Elliott Hughesbf86d042011-08-31 17:53:14 -0700122void Runtime::CallExitHook(jint status) {
123 if (exit_ != NULL) {
124 ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative);
125 exit_(status);
126 LOG(WARNING) << "Exit hook returned instead of exiting!";
127 }
128}
129
Brian Carlstrom8a436592011-08-15 21:27:23 -0700130// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
131// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
132// [gG] gigabytes.
133//
134// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700135// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
136// of 1024.
137//
138// The spec says the -Xmx and -Xms options must be multiples of 1024. It
139// doesn't say anything about -Xss.
140//
141// Returns 0 (a useless size) if "s" is malformed or specifies a low or
142// non-evenly-divisible value.
143//
144size_t ParseMemoryOption(const char *s, size_t div) {
145 // strtoul accepts a leading [+-], which we don't want,
146 // so make sure our string starts with a decimal digit.
147 if (isdigit(*s)) {
148 const char *s2;
149 size_t val = strtoul(s, (char **)&s2, 10);
150 if (s2 != s) {
151 // s2 should be pointing just after the number.
152 // If this is the end of the string, the user
153 // has specified a number of bytes. Otherwise,
154 // there should be exactly one more character
155 // that specifies a multiplier.
156 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700157 // The remainder of the string is either a single multiplier
158 // character, or nothing to indicate that the value is in
159 // bytes.
160 char c = *s2++;
161 if (*s2 == '\0') {
162 size_t mul;
163 if (c == '\0') {
164 mul = 1;
165 } else if (c == 'k' || c == 'K') {
166 mul = KB;
167 } else if (c == 'm' || c == 'M') {
168 mul = MB;
169 } else if (c == 'g' || c == 'G') {
170 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700171 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700172 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700173 return 0;
174 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700175
176 if (val <= std::numeric_limits<size_t>::max() / mul) {
177 val *= mul;
178 } else {
179 // Clamp to a multiple of 1024.
180 val = std::numeric_limits<size_t>::max() & ~(1024-1);
181 }
182 } else {
183 // There's more than one character after the numeric part.
184 return 0;
185 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700186 }
187 // The man page says that a -Xm value must be a multiple of 1024.
188 if (val % div == 0) {
189 return val;
190 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700191 }
192 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700193 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700194}
195
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700196size_t ParseIntegerOrDie(const StringPiece& s) {
197 StringPiece::size_type colon = s.find(':');
198 if (colon == StringPiece::npos) {
199 LOG(FATAL) << "Missing integer: " << s;
200 }
201 const char* begin = &s.data()[colon + 1];
202 char* end;
203 size_t result = strtoul(begin, &end, 10);
204 if (begin == end || *end != '\0') {
205 LOG(FATAL) << "Failed to parse integer in: " << s;
206 }
207 return result;
208}
209
Elliott Hughes0af55432011-08-17 18:37:28 -0700210void LoadJniLibrary(JavaVMExt* vm, const char* name) {
211 // TODO: OS_SHARED_LIB_FORMAT_STR
212 std::string mapped_name(StringPrintf("lib%s.so", name));
Elliott Hughes75770752011-08-24 17:52:38 -0700213 std::string reason;
214 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700215 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
216 << reason;
217 }
218}
219
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700220Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700221 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700222 bool compiler = false;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700223 const char* boot_class_path = getenv("BOOTCLASSPATH");
224 if (boot_class_path != NULL) {
225 parsed->boot_class_path_ = getenv("BOOTCLASSPATH");
226 }
227 const char* class_path = getenv("CLASSPATH");
228 if (class_path != NULL) {
229 parsed->class_path_ = getenv("CLASSPATH");
230 }
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700231#ifdef NDEBUG
Elliott Hughes5174fe62011-08-23 15:12:35 -0700232 // -Xcheck:jni is off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700233 parsed->check_jni_ = false;
234#else
235 // ...but on by default in debug builds.
236 parsed->check_jni_ = true;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700237#endif
Elliott Hughes85d15452011-09-16 17:33:01 -0700238
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700239 parsed->heap_initial_size_ = Heap::kInitialSize;
240 parsed->heap_maximum_size_ = Heap::kMaximumSize;
jeffhaoc1160702011-10-27 15:48:45 -0700241 parsed->heap_growth_limit_ = 0; // 0 means no growth limit
Brian Carlstromf734cf52011-08-17 16:28:14 -0700242 parsed->stack_size_ = Thread::kDefaultStackSize;
243
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700244 parsed->is_zygote_ = false;
245
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700246 parsed->jni_globals_max_ = 0;
Elliott Hughesfc861622011-10-17 17:57:47 -0700247 parsed->lock_profiling_threshold_ = 0;
248 parsed->hook_is_sensitive_thread_ = NULL;
249
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700250 parsed->hook_vfprintf_ = vfprintf;
251 parsed->hook_exit_ = exit;
252 parsed->hook_abort_ = abort;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700253
254 for (size_t i = 0; i < options.size(); ++i) {
255 const StringPiece& option = options[i].first;
Brian Carlstrom0796af02011-10-12 14:31:45 -0700256 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700257 LOG(INFO) << "option[" << i << "]=" << option;
258 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700259 if (option.starts_with("-Xbootclasspath:")) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700260 parsed->boot_class_path_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700261 } else if (option == "-classpath" || option == "-cp") {
262 // TODO: support -Djava.class.path
263 i++;
264 if (i == options.size()) {
265 // TODO: usage
266 LOG(FATAL) << "Missing required class path value for " << option;
267 return NULL;
268 }
269 const StringPiece& value = options[i].first;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700270 parsed->class_path_ = value.data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700271 } else if (option.starts_with("-Ximage:")) {
272 parsed->images_.push_back(option.substr(strlen("-Ximage:")).data());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700273 } else if (option.starts_with("-Xcheck:jni")) {
274 parsed->check_jni_ = true;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700275 } else if (option.starts_with("-Xrunjdwp:") || option.starts_with("-agentlib:jdwp=")) {
276 std::string tail = option.substr(option[1] == 'X' ? 10 : 15).ToString();
277 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
278 LOG(FATAL) << "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
279 << "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n";
280 return NULL;
281 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700282 } else if (option.starts_with("-Xms")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700283 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024);
284 if (size == 0) {
285 if (ignore_unrecognized) {
286 continue;
287 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700288 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700289 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700290 return NULL;
291 }
292 parsed->heap_initial_size_ = size;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700293 } else if (option.starts_with("-Xmx")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700294 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024);
295 if (size == 0) {
296 if (ignore_unrecognized) {
297 continue;
298 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700299 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700300 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700301 return NULL;
302 }
303 parsed->heap_maximum_size_ = size;
jeffhaoc1160702011-10-27 15:48:45 -0700304 } else if (option.starts_with("-XX:HeapGrowthLimit=")) {
305 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).data(), 1024);
306 if (size == 0) {
307 if (ignore_unrecognized) {
308 continue;
309 }
310 // TODO: usage
311 LOG(FATAL) << "Failed to parse " << option;
312 return NULL;
313 }
314 parsed->heap_growth_limit_ = size;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700315 } else if (option.starts_with("-Xss")) {
316 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1);
317 if (size == 0) {
318 if (ignore_unrecognized) {
319 continue;
320 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700321 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700322 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700323 return NULL;
324 }
325 parsed->stack_size_ = size;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700326 } else if (option.starts_with("-D")) {
327 parsed->properties_.push_back(option.substr(strlen("-D")).data());
Elliott Hughesa0957642011-09-02 14:27:33 -0700328 } else if (option.starts_with("-Xjnitrace:")) {
329 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:")).data();
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700330 } else if (option == "compiler") {
331 compiler = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700332 } else if (option == "-Xzygote") {
333 parsed->is_zygote_ = true;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700334 } else if (option.starts_with("-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700335 std::vector<std::string> verbose_options;
Elliott Hughes34023802011-08-30 12:06:17 -0700336 Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700337 for (size_t i = 0; i < verbose_options.size(); ++i) {
338 parsed->verbose_.insert(verbose_options[i]);
339 }
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700340 } else if (option.starts_with("-Xjnigreflimit:")) {
341 parsed->jni_globals_max_ = ParseIntegerOrDie(option);
Elliott Hughesfc861622011-10-17 17:57:47 -0700342 } else if (option.starts_with("-Xlockprofthreshold:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700343 parsed->lock_profiling_threshold_ = ParseIntegerOrDie(option);
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700344 } else if (option.starts_with("-Xstacktracefile:")) {
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700345// always show stack traces in debug builds
346#ifdef NDEBUG
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700347 parsed->stack_trace_file_ = option.substr(strlen("-Xstacktracefile:")).data();
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700348#endif
Elliott Hughesfc861622011-10-17 17:57:47 -0700349 } else if (option == "sensitiveThread") {
350 parsed->hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(options[i].second);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700351 } else if (option == "vfprintf") {
352 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
353 } else if (option == "exit") {
354 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
355 } else if (option == "abort") {
356 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700357 } else if (option == "host-prefix") {
358 parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700359 } else {
360 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700361 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700362 LOG(ERROR) << "Unrecognized option " << option;
363 // TODO: this should exit, but for now tolerate unknown options
364 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700365 }
366 }
367 }
368
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700369 if (!compiler && parsed->images_.empty()) {
370 parsed->images_.push_back("/data/art-cache/boot.art");
371 }
jeffhaoc1160702011-10-27 15:48:45 -0700372 if (parsed->heap_growth_limit_ == 0) {
373 parsed->heap_growth_limit_ = parsed->heap_maximum_size_;
374 }
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700375
Elliott Hughes85d15452011-09-16 17:33:01 -0700376 LOG(INFO) << "CheckJNI is " << (parsed->check_jni_ ? "on" : "off");
377
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700378 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700379}
380
381Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700382 // TODO: acquire a static mutex on Runtime to avoid racing.
383 if (Runtime::instance_ != NULL) {
384 return NULL;
385 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700386 instance_ = new Runtime;
387 if (!instance_->Init(options, ignore_unrecognized)) {
388 delete instance_;
389 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700390 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700391 return instance_;
392}
Elliott Hughes0af55432011-08-17 18:37:28 -0700393
Brian Carlstromaded5f72011-10-07 17:15:04 -0700394void CreateSystemClassLoader() {
395 if (ClassLoader::UseCompileTimeClassPath()) {
396 return;
397 }
398
399 Thread* self = Thread::Current();
400
401 // Must be in the kNative state for calling native methods.
402 CHECK_EQ(self->GetState(), Thread::kNative);
403
404 JNIEnv* env = self->GetJniEnv();
Brian Carlstromdf143242011-10-10 18:05:34 -0700405 ScopedLocalRef<jclass> ClassLoader_class(env, env->FindClass("java/lang/ClassLoader"));
406 CHECK(ClassLoader_class.get() != NULL);
407 jmethodID getSystemClassLoader = env->GetStaticMethodID(ClassLoader_class.get(),
408 "getSystemClassLoader",
409 "()Ljava/lang/ClassLoader;");
410 CHECK(getSystemClassLoader != NULL);
411 ScopedLocalRef<jobject> class_loader(env, env->CallStaticObjectMethod(ClassLoader_class.get(),
412 getSystemClassLoader));
413 CHECK(class_loader.get() != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700414
Brian Carlstromdf143242011-10-10 18:05:34 -0700415 Thread::Current()->SetClassLoaderOverride(Decode<ClassLoader*>(env, class_loader.get()));
Brian Carlstromaded5f72011-10-07 17:15:04 -0700416}
417
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700418void Runtime::Start() {
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700419 if (IsVerboseStartup()) {
420 LOG(INFO) << "Runtime::Start entering";
421 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700422
423 CHECK(host_prefix_.empty()) << host_prefix_;
424
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700425 // Restore main thread state to kNative as expected by native code
426 Thread::Current()->SetState(Thread::kNative);
427
Elliott Hughes038a8062011-09-18 14:12:41 -0700428 InitNativeMethods();
Elliott Hughes85d15452011-09-16 17:33:01 -0700429
Elliott Hughes038a8062011-09-18 14:12:41 -0700430 Thread::FinishStartup();
Elliott Hughes85d15452011-09-16 17:33:01 -0700431
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700432 class_linker_->RunRootClinits();
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700433
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700434 // Class::AllocObject asserts that all objects allocated better be
435 // initialized after Runtime::IsStarted is true, so this needs to
436 // come after ClassLinker::RunRootClinits.
437 started_ = true;
438
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700439 if (!is_zygote_) {
440 DidForkFromZygote();
441 }
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700442
Elliott Hughes85d15452011-09-16 17:33:01 -0700443 StartDaemonThreads();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700444
Brian Carlstromaded5f72011-10-07 17:15:04 -0700445 CreateSystemClassLoader();
446
Elliott Hughes726079d2011-10-07 18:43:44 -0700447 Thread::Current()->GetJniEnv()->locals.AssertEmpty();
448
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700449 if (IsVerboseStartup()) {
450 LOG(INFO) << "Runtime::Start exiting";
451 }
Elliott Hughes85d15452011-09-16 17:33:01 -0700452}
453
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700454void Runtime::DidForkFromZygote() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700455 is_zygote_ = false;
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700456
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700457 StartSignalCatcher();
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700458
459 // Start the JDWP thread. If the command-line debugger flags specified "suspend=y",
460 // this will pause the runtime, so we probably want this to come last.
461 Dbg::StartJdwp();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700462}
463
464void Runtime::StartSignalCatcher() {
465 if (!is_zygote_) {
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700466 signal_catcher_ = new SignalCatcher(stack_trace_file_);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700467 }
468}
469
Elliott Hughes85d15452011-09-16 17:33:01 -0700470void Runtime::StartDaemonThreads() {
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700471 if (IsVerboseStartup()) {
472 LOG(INFO) << "Runtime::StartDaemonThreads entering";
473 }
Elliott Hughes85d15452011-09-16 17:33:01 -0700474
Elliott Hughes719b3232011-09-25 17:42:19 -0700475 Thread* self = Thread::Current();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700476
477 // Must be in the kNative state for calling native methods.
478 CHECK_EQ(self->GetState(), Thread::kNative);
479
Elliott Hughes719b3232011-09-25 17:42:19 -0700480 JNIEnv* env = self->GetJniEnv();
Elliott Hughes726079d2011-10-07 18:43:44 -0700481 ScopedLocalRef<jclass> c(env, env->FindClass("java/lang/Daemons"));
482 CHECK(c.get() != NULL);
483 jmethodID mid = env->GetStaticMethodID(c.get(), "start", "()V");
Elliott Hughes719b3232011-09-25 17:42:19 -0700484 CHECK(mid != NULL);
Elliott Hughes726079d2011-10-07 18:43:44 -0700485 env->CallStaticVoidMethod(c.get(), mid);
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700486
487 if (IsVerboseStartup()) {
488 LOG(INFO) << "Runtime::StartDaemonThreads exiting";
489 }
Carl Shapiro61e019d2011-07-14 16:53:09 -0700490}
491
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700492bool Runtime::IsStarted() const {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700493 return started_;
494}
495
Elliott Hughes0af55432011-08-17 18:37:28 -0700496bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700497 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700498
Elliott Hughes90a33692011-08-30 13:27:07 -0700499 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
500 if (options.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700501 LOG(ERROR) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700502 return false;
503 }
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700504 verbose_startup_ = options->IsVerbose("startup");
505 if (IsVerboseStartup()) {
506 LOG(INFO) << "Runtime::Init -verbose:startup enabled";
507 }
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700508
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700509 SetJniGlobalsMax(options->jni_globals_max_);
Elliott Hughesfc861622011-10-17 17:57:47 -0700510 Monitor::Init(options->IsVerbose("monitor"), options->lock_profiling_threshold_, options->hook_is_sensitive_thread_);
Elliott Hughes32d6e1e2011-10-11 14:47:44 -0700511
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700512 host_prefix_ = options->host_prefix_;
513 boot_class_path_ = options->boot_class_path_;
514 class_path_ = options->class_path_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700515 properties_ = options->properties_;
516
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700517 is_zygote_ = options->is_zygote_;
518
Elliott Hughes0af55432011-08-17 18:37:28 -0700519 vfprintf_ = options->hook_vfprintf_;
520 exit_ = options->hook_exit_;
521 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700522
Elliott Hughesbe759c62011-09-08 19:38:21 -0700523 default_stack_size_ = options->stack_size_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700524 stack_trace_file_ = options->stack_trace_file_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700525
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700526 monitor_list_ = new MonitorList;
Elliott Hughes14357e82011-09-26 10:42:15 -0700527 thread_list_ = new ThreadList(options->IsVerbose("thread"));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700528 intern_table_ = new InternTable;
529
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700530 Heap::Init(options->IsVerbose("heap"),
531 options->IsVerbose("gc"),
532 options->heap_initial_size_,
533 options->heap_maximum_size_,
jeffhaoc1160702011-10-27 15:48:45 -0700534 options->heap_growth_limit_,
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700535 options->images_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700536
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700537 BlockSignals();
538
Elliott Hughesa0957642011-09-02 14:27:33 -0700539 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700540
Elliott Hughesbe759c62011-09-08 19:38:21 -0700541 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700542
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700543 // ClassLinker needs an attached thread, but we can't fully attach a thread
544 // without creating objects.
545 Thread::Attach(this, "main", false);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700546
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700547 // Set us to runnable so tools using a runtime can allocate and GC by default
548 Thread::Current()->SetState(Thread::kRunnable);
549
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700550 CHECK_GE(Heap::GetSpaces().size(), 1U);
551 class_linker_ = ((Heap::GetSpaces()[0]->IsImageSpace())
552 ? ClassLinker::Create(intern_table_)
553 : ClassLinker::Create(options->boot_class_path_, intern_table_));
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700554
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700555 if (IsVerboseStartup()) {
556 LOG(INFO) << "Runtime::Init exiting";
557 }
Carl Shapiro1fb86202011-06-27 17:43:13 -0700558 return true;
559}
560
Elliott Hughes038a8062011-09-18 14:12:41 -0700561void Runtime::InitNativeMethods() {
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700562 if (IsVerboseStartup()) {
563 LOG(INFO) << "Runtime::InitNativeMethods entering";
564 }
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700565 Thread* self = Thread::Current();
566 JNIEnv* env = self->GetJniEnv();
567
Elliott Hughes418d20f2011-09-22 14:00:39 -0700568 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Brian Carlstromaded5f72011-10-07 17:15:04 -0700569 CHECK_EQ(self->GetState(), Thread::kNative);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700570
Elliott Hughes418d20f2011-09-22 14:00:39 -0700571 // First set up JniConstants, which is used by both the runtime's built-in native
572 // methods and libcore.
Elliott Hughesfea966e2011-09-22 10:26:20 -0700573 JniConstants::init(env);
574
Elliott Hughes418d20f2011-09-22 14:00:39 -0700575 // Then set up the native methods provided by the runtime itself.
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700576 RegisterRuntimeNativeMethods(env);
577
Elliott Hughes418d20f2011-09-22 14:00:39 -0700578 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
579 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
580 // the library that implements System.loadLibrary!
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700581 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700582 if (IsVerboseStartup()) {
583 LOG(INFO) << "Runtime::InitNativeMethods exiting";
584 }
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700585}
586
587void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
588#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700589 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700590 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700591 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700592 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700593 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700594 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700595 REGISTER(register_java_lang_Object);
596 REGISTER(register_java_lang_Runtime);
597 REGISTER(register_java_lang_String);
598 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700599 REGISTER(register_java_lang_Thread);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700600 REGISTER(register_java_lang_Throwable);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700601 REGISTER(register_java_lang_VMClassLoader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700602 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700603 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700604 REGISTER(register_java_lang_reflect_Field);
605 REGISTER(register_java_lang_reflect_Method);
Jesse Wilson95caa792011-10-12 18:14:17 -0400606 REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700607 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700608 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700609 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700610 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700611#undef REGISTER
612}
613
Elliott Hughes8daa0922011-09-11 13:46:25 -0700614void Runtime::Dump(std::ostream& os) {
Elliott Hughese27955c2011-08-26 15:21:24 -0700615 // TODO: dump other runtime statistics?
616 os << "Loaded classes: " << class_linker_->NumLoadedClasses() << "\n";
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700617 os << "Intern table size: " << GetInternTable()->Size() << "\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700618 // LOGV("VM stats: meth=%d ifld=%d sfld=%d linear=%d",
619 // gDvm.numDeclaredMethods,
620 // gDvm.numDeclaredInstFields,
621 // gDvm.numDeclaredStaticFields,
622 // gDvm.pBootLoaderAlloc->curOffset);
623 // LOGI("GC precise methods: %d", dvmPointerSetGetCount(gDvm.preciseMethods));
Elliott Hughes42ee1422011-09-06 12:33:32 -0700624 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700625
626 thread_list_->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700627}
628
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700629void Runtime::SetStatsEnabled(bool new_state) {
630 if (new_state == true) {
631 GetStats()->Clear(~0);
632 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
633 Thread::Current()->GetStats()->Clear(~0);
634 }
635 stats_enabled_ = new_state;
636}
637
638void Runtime::ResetStats(int kinds) {
639 GetStats()->Clear(kinds & 0xffff);
640 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
641 Thread::Current()->GetStats()->Clear(kinds >> 16);
642}
643
644RuntimeStats* Runtime::GetStats() {
645 return &stats_;
646}
647
648int32_t Runtime::GetStat(int kind) {
649 RuntimeStats* stats;
650 if (kind < (1<<16)) {
651 stats = GetStats();
652 } else {
653 stats = Thread::Current()->GetStats();
654 kind >>= 16;
655 }
656 switch (kind) {
657 case KIND_ALLOCATED_OBJECTS:
658 return stats->allocated_objects;
659 case KIND_ALLOCATED_BYTES:
660 return stats->allocated_bytes;
661 case KIND_FREED_OBJECTS:
662 return stats->freed_objects;
663 case KIND_FREED_BYTES:
664 return stats->freed_bytes;
665 case KIND_GC_INVOCATIONS:
666 return stats->gc_for_alloc_count;
667 case KIND_CLASS_INIT_COUNT:
668 return stats->class_init_count;
669 case KIND_CLASS_INIT_TIME:
670 // Convert ns to us, reduce to 32 bits.
671 return (int) (stats->class_init_time_ns / 1000);
672 case KIND_EXT_ALLOCATED_OBJECTS:
673 case KIND_EXT_ALLOCATED_BYTES:
674 case KIND_EXT_FREED_OBJECTS:
675 case KIND_EXT_FREED_BYTES:
676 return 0; // backward compatibility
677 default:
678 CHECK(false);
679 return -1; // unreachable
680 }
681}
682
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700683void Runtime::BlockSignals() {
684 sigset_t sigset;
685 if (sigemptyset(&sigset) == -1) {
686 PLOG(FATAL) << "sigemptyset failed";
687 }
688 if (sigaddset(&sigset, SIGPIPE) == -1) {
689 PLOG(ERROR) << "sigaddset SIGPIPE failed";
690 }
691 // SIGQUIT is used to dump the runtime's state (including stack traces).
692 if (sigaddset(&sigset, SIGQUIT) == -1) {
693 PLOG(ERROR) << "sigaddset SIGQUIT failed";
694 }
695 // SIGUSR1 is used to initiate a heap dump.
696 if (sigaddset(&sigset, SIGUSR1) == -1) {
697 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
698 }
699 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
700}
701
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700702void Runtime::AttachCurrentThread(const char* name, bool as_daemon) {
703 Thread::Attach(instance_, name, as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700704}
705
Elliott Hughesd92bec42011-09-02 17:04:36 -0700706void Runtime::DetachCurrentThread() {
Elliott Hughes038a8062011-09-18 14:12:41 -0700707 // TODO: check we're not calling DetachCurrentThread from a call stack that
708 // includes managed frames. (It's only valid if the stack is all-native.)
Elliott Hughes02b48d12011-09-07 17:15:51 -0700709 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700710}
711
Brian Carlstrome24fa612011-09-29 00:53:55 -0700712void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700713 Dbg::VisitRoots(visitor, arg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700714 class_linker_->VisitRoots(visitor, arg);
715 intern_table_->VisitRoots(visitor, arg);
716 java_vm_->VisitRoots(visitor, arg);
717 thread_list_->VisitRoots(visitor, arg);
718 visitor(jni_stub_array_, arg);
719 visitor(abstract_method_error_stub_array_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700720 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
721 visitor(resolution_stub_array_[i], arg);
722 }
723 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
724 visitor(callee_save_method_[i], arg);
725 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700726}
727
728bool Runtime::HasJniStubArray() const {
729 return jni_stub_array_ != NULL;
730}
731
732ByteArray* Runtime::GetJniStubArray() const {
733 CHECK(jni_stub_array_ != NULL);
734 return jni_stub_array_;
735}
736
737void Runtime::SetJniStubArray(ByteArray* jni_stub_array) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700738 CHECK(jni_stub_array != NULL) << " jni_stub_array=" << jni_stub_array;
739 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array)
740 << "jni_stub_array_=" << jni_stub_array_ << " jni_stub_array=" << jni_stub_array;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700741 jni_stub_array_ = jni_stub_array;
742}
743
744bool Runtime::HasAbstractMethodErrorStubArray() const {
745 return abstract_method_error_stub_array_ != NULL;
746}
747
748ByteArray* Runtime::GetAbstractMethodErrorStubArray() const {
749 CHECK(abstract_method_error_stub_array_ != NULL);
750 return abstract_method_error_stub_array_;
751}
752
753void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) {
754 CHECK(abstract_method_error_stub_array != NULL);
755 CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array);
756 abstract_method_error_stub_array_ = abstract_method_error_stub_array;
757}
758
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700759
760Runtime::TrampolineType Runtime::GetTrampolineType(Method* method) {
761 if (method == NULL) {
762 return Runtime::kUnknownMethod;
763 } else if (method->IsStatic()) {
764 return Runtime::kStaticMethod;
765 } else {
766 return Runtime::kInstanceMethod;
767 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700768}
769
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700770bool Runtime::HasResolutionStubArray(TrampolineType type) const {
771 return resolution_stub_array_[type] != NULL;
Ian Rogersad25ac52011-10-04 19:13:33 -0700772}
773
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700774ByteArray* Runtime::GetResolutionStubArray(TrampolineType type) const {
775 CHECK(HasResolutionStubArray(type));
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700776 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastTrampolineMethodType));
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700777 return resolution_stub_array_[type];
778}
779
780void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700781 CHECK(resolution_stub_array != NULL);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700782 CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array);
783 resolution_stub_array_[type] = resolution_stub_array;
Ian Rogersad25ac52011-10-04 19:13:33 -0700784}
785
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700786Method* Runtime::CreateCalleeSaveMethod(InstructionSet insns, CalleeSaveType type) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700787 Class* method_class = Method::GetMethodClass();
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700788 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
Ian Rogersff1ed472011-09-20 13:46:24 -0700789 method->SetDeclaringClass(method_class);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700790 const char* name;
791 if (type == kSaveAll) {
792 name = "$$$callee_save_method$$$";
793 } else if (type == kRefsOnly) {
794 name = "$$$refs_only_callee_save_method$$$";
795 } else {
796 DCHECK(type == kRefsAndArgs);
797 name = "$$$refs_and_args_callee_save_method$$$";
798 }
799 method->SetName(intern_table_->InternStrong(name));
Elliott Hughes30646832011-10-13 16:59:46 -0700800 CHECK(method->GetName() != NULL);
Ian Rogersff1ed472011-09-20 13:46:24 -0700801 method->SetSignature(intern_table_->InternStrong("()V"));
Elliott Hughes30646832011-10-13 16:59:46 -0700802 CHECK(method->GetSignature() != NULL);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700803 method->SetCode(NULL);
Ian Rogersff1ed472011-09-20 13:46:24 -0700804 if ((insns == kThumb2) || (insns == kArm)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700805 uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6) | (1 << art::arm::R7) |
806 (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11);
807 uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3);
808 uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9);
809 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills :0) |
810 (type == kSaveAll ? all_spills :0) | (1 << art::arm::LR);
811 uint32_t fp_all_spills = (1 << art::arm::S0) | (1 << art::arm::S1) | (1 << art::arm::S2) |
812 (1 << art::arm::S3) | (1 << art::arm::S4) | (1 << art::arm::S5) |
813 (1 << art::arm::S6) | (1 << art::arm::S7) | (1 << art::arm::S8) |
814 (1 << art::arm::S9) | (1 << art::arm::S10) | (1 << art::arm::S11) |
815 (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) |
816 (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) |
817 (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) |
818 (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) |
819 (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) |
820 (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
821 (1 << art::arm::S30) | (1 << art::arm::S31);
822 uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
823 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
824 __builtin_popcount(fp_spills) /* fprs */ +
825 1 /* Method* */) * kPointerSize, kStackAlignment);
Ian Rogers15fdb8c2011-09-25 15:45:07 -0700826 method->SetFrameSizeInBytes(frame_size);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700827 method->SetCoreSpillMask(core_spills);
828 method->SetFpSpillMask(fp_spills);
Ian Rogersff1ed472011-09-20 13:46:24 -0700829 } else if (insns == kX86) {
830 method->SetFrameSizeInBytes(32);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700831 method->SetCoreSpillMask((1 << art::x86::EBX) | (1 << art::x86::EBP) | (1 << art::x86::ESI) |
Ian Rogersff1ed472011-09-20 13:46:24 -0700832 (1 << art::x86::EDI));
833 method->SetFpSpillMask(0);
834 } else {
835 UNIMPLEMENTED(FATAL);
836 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700837 return method.get();
Ian Rogersff1ed472011-09-20 13:46:24 -0700838}
839
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700840bool Runtime::HasCalleeSaveMethod(CalleeSaveType type) const {
841 return callee_save_method_[type] != NULL;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700842}
843
Brian Carlstrome24fa612011-09-29 00:53:55 -0700844// Returns a special method that describes all callee saves being spilled to the stack.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700845Method* Runtime::GetCalleeSaveMethod(CalleeSaveType type) const {
846 CHECK(HasCalleeSaveMethod(type));
847 return callee_save_method_[type];
Brian Carlstrome24fa612011-09-29 00:53:55 -0700848}
849
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700850void Runtime::SetCalleeSaveMethod(Method* method, CalleeSaveType type) {
851 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
852 callee_save_method_[type] = method;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700853}
854
Carl Shapiro1fb86202011-06-27 17:43:13 -0700855} // namespace art