blob: 54d7ec60f1564dcb5978fece8c20741b281ae8a5 [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 Hughese0918552011-10-28 17:18:29 -070079struct AbortState {
80 void Dump(std::ostream& os) {
81 os << "Runtime aborting...\n";
82 Thread* self = Thread::Current();
83 if (self == NULL) {
84 os << "(Aborting thread was not attached to runtime!)\n";
85 } else {
86 self->Dump(os, true);
87 }
88 }
89};
90
Elliott Hughesffe67362011-07-17 12:09:27 -070091void Runtime::Abort(const char* file, int line) {
92 // Get any pending output out of the way.
93 fflush(NULL);
94
95 // Many people have difficulty distinguish aborts from crashes,
96 // so be explicit.
Elliott Hughese0918552011-10-28 17:18:29 -070097 AbortState state;
98 LOG(ERROR) << Dumpable<AbortState>(state);
Elliott Hughesffe67362011-07-17 12:09:27 -070099
Elliott Hughesffe67362011-07-17 12:09:27 -0700100 // Perform any platform-specific pre-abort actions.
101 PlatformAbort(file, line);
102
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700103 // use abort hook if we have one
104 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
105 Runtime::Current()->abort_();
106 // notreached
107 }
108
Elliott Hughesffe67362011-07-17 12:09:27 -0700109 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -0700110 // receive SIGABRT. debuggerd dumps the stack trace of the main
111 // thread, whether or not that was the thread that failed. By
112 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -0700113 // fault in the current thread, and get a useful log from debuggerd.
114 // We can also trivially tell the difference between a VM crash and
115 // a deliberate abort by looking at the fault address.
116 *reinterpret_cast<char*>(0xdeadd00d) = 38;
117 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -0700118 // notreached
119}
120
Elliott Hughesbf86d042011-08-31 17:53:14 -0700121void Runtime::CallExitHook(jint status) {
122 if (exit_ != NULL) {
123 ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative);
124 exit_(status);
125 LOG(WARNING) << "Exit hook returned instead of exiting!";
126 }
127}
128
Brian Carlstrom8a436592011-08-15 21:27:23 -0700129// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
130// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
131// [gG] gigabytes.
132//
133// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700134// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
135// of 1024.
136//
137// The spec says the -Xmx and -Xms options must be multiples of 1024. It
138// doesn't say anything about -Xss.
139//
140// Returns 0 (a useless size) if "s" is malformed or specifies a low or
141// non-evenly-divisible value.
142//
143size_t ParseMemoryOption(const char *s, size_t div) {
144 // strtoul accepts a leading [+-], which we don't want,
145 // so make sure our string starts with a decimal digit.
146 if (isdigit(*s)) {
147 const char *s2;
148 size_t val = strtoul(s, (char **)&s2, 10);
149 if (s2 != s) {
150 // s2 should be pointing just after the number.
151 // If this is the end of the string, the user
152 // has specified a number of bytes. Otherwise,
153 // there should be exactly one more character
154 // that specifies a multiplier.
155 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700156 // The remainder of the string is either a single multiplier
157 // character, or nothing to indicate that the value is in
158 // bytes.
159 char c = *s2++;
160 if (*s2 == '\0') {
161 size_t mul;
162 if (c == '\0') {
163 mul = 1;
164 } else if (c == 'k' || c == 'K') {
165 mul = KB;
166 } else if (c == 'm' || c == 'M') {
167 mul = MB;
168 } else if (c == 'g' || c == 'G') {
169 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700170 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700171 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700172 return 0;
173 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700174
175 if (val <= std::numeric_limits<size_t>::max() / mul) {
176 val *= mul;
177 } else {
178 // Clamp to a multiple of 1024.
179 val = std::numeric_limits<size_t>::max() & ~(1024-1);
180 }
181 } else {
182 // There's more than one character after the numeric part.
183 return 0;
184 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700185 }
186 // The man page says that a -Xm value must be a multiple of 1024.
187 if (val % div == 0) {
188 return val;
189 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700190 }
191 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700192 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700193}
194
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700195size_t ParseIntegerOrDie(const StringPiece& s) {
196 StringPiece::size_type colon = s.find(':');
197 if (colon == StringPiece::npos) {
198 LOG(FATAL) << "Missing integer: " << s;
199 }
200 const char* begin = &s.data()[colon + 1];
201 char* end;
202 size_t result = strtoul(begin, &end, 10);
203 if (begin == end || *end != '\0') {
204 LOG(FATAL) << "Failed to parse integer in: " << s;
205 }
206 return result;
207}
208
Elliott Hughes0af55432011-08-17 18:37:28 -0700209void LoadJniLibrary(JavaVMExt* vm, const char* name) {
210 // TODO: OS_SHARED_LIB_FORMAT_STR
211 std::string mapped_name(StringPrintf("lib%s.so", name));
Elliott Hughes75770752011-08-24 17:52:38 -0700212 std::string reason;
213 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700214 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
215 << reason;
216 }
217}
218
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700219Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700220 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700221 bool compiler = false;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700222 const char* boot_class_path = getenv("BOOTCLASSPATH");
223 if (boot_class_path != NULL) {
224 parsed->boot_class_path_ = getenv("BOOTCLASSPATH");
225 }
226 const char* class_path = getenv("CLASSPATH");
227 if (class_path != NULL) {
228 parsed->class_path_ = getenv("CLASSPATH");
229 }
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700230#ifdef NDEBUG
Elliott Hughes5174fe62011-08-23 15:12:35 -0700231 // -Xcheck:jni is off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700232 parsed->check_jni_ = false;
233#else
234 // ...but on by default in debug builds.
235 parsed->check_jni_ = true;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700236#endif
Elliott Hughes85d15452011-09-16 17:33:01 -0700237
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700238 parsed->heap_initial_size_ = Heap::kInitialSize;
239 parsed->heap_maximum_size_ = Heap::kMaximumSize;
jeffhaoc1160702011-10-27 15:48:45 -0700240 parsed->heap_growth_limit_ = 0; // 0 means no growth limit
Brian Carlstromf734cf52011-08-17 16:28:14 -0700241 parsed->stack_size_ = Thread::kDefaultStackSize;
242
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700243 parsed->is_zygote_ = false;
244
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700245 parsed->jni_globals_max_ = 0;
Elliott Hughesfc861622011-10-17 17:57:47 -0700246 parsed->lock_profiling_threshold_ = 0;
247 parsed->hook_is_sensitive_thread_ = NULL;
248
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700249 parsed->hook_vfprintf_ = vfprintf;
250 parsed->hook_exit_ = exit;
251 parsed->hook_abort_ = abort;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700252
253 for (size_t i = 0; i < options.size(); ++i) {
254 const StringPiece& option = options[i].first;
Brian Carlstrom0796af02011-10-12 14:31:45 -0700255 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700256 LOG(INFO) << "option[" << i << "]=" << option;
257 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700258 if (option.starts_with("-Xbootclasspath:")) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700259 parsed->boot_class_path_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700260 } else if (option == "-classpath" || option == "-cp") {
261 // TODO: support -Djava.class.path
262 i++;
263 if (i == options.size()) {
264 // TODO: usage
265 LOG(FATAL) << "Missing required class path value for " << option;
266 return NULL;
267 }
268 const StringPiece& value = options[i].first;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700269 parsed->class_path_ = value.data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700270 } else if (option.starts_with("-Ximage:")) {
271 parsed->images_.push_back(option.substr(strlen("-Ximage:")).data());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700272 } else if (option.starts_with("-Xcheck:jni")) {
273 parsed->check_jni_ = true;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700274 } else if (option.starts_with("-Xrunjdwp:") || option.starts_with("-agentlib:jdwp=")) {
275 std::string tail = option.substr(option[1] == 'X' ? 10 : 15).ToString();
276 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
277 LOG(FATAL) << "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
278 << "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n";
279 return NULL;
280 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700281 } else if (option.starts_with("-Xms")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700282 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024);
283 if (size == 0) {
284 if (ignore_unrecognized) {
285 continue;
286 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700287 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700288 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700289 return NULL;
290 }
291 parsed->heap_initial_size_ = size;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700292 } else if (option.starts_with("-Xmx")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700293 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024);
294 if (size == 0) {
295 if (ignore_unrecognized) {
296 continue;
297 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700298 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700299 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700300 return NULL;
301 }
302 parsed->heap_maximum_size_ = size;
jeffhaoc1160702011-10-27 15:48:45 -0700303 } else if (option.starts_with("-XX:HeapGrowthLimit=")) {
304 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).data(), 1024);
305 if (size == 0) {
306 if (ignore_unrecognized) {
307 continue;
308 }
309 // TODO: usage
310 LOG(FATAL) << "Failed to parse " << option;
311 return NULL;
312 }
313 parsed->heap_growth_limit_ = size;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700314 } else if (option.starts_with("-Xss")) {
315 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1);
316 if (size == 0) {
317 if (ignore_unrecognized) {
318 continue;
319 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700320 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700321 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700322 return NULL;
323 }
324 parsed->stack_size_ = size;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700325 } else if (option.starts_with("-D")) {
326 parsed->properties_.push_back(option.substr(strlen("-D")).data());
Elliott Hughesa0957642011-09-02 14:27:33 -0700327 } else if (option.starts_with("-Xjnitrace:")) {
328 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:")).data();
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700329 } else if (option == "compiler") {
330 compiler = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700331 } else if (option == "-Xzygote") {
332 parsed->is_zygote_ = true;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700333 } else if (option.starts_with("-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700334 std::vector<std::string> verbose_options;
Elliott Hughes34023802011-08-30 12:06:17 -0700335 Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700336 for (size_t i = 0; i < verbose_options.size(); ++i) {
337 parsed->verbose_.insert(verbose_options[i]);
338 }
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700339 } else if (option.starts_with("-Xjnigreflimit:")) {
340 parsed->jni_globals_max_ = ParseIntegerOrDie(option);
Elliott Hughesfc861622011-10-17 17:57:47 -0700341 } else if (option.starts_with("-Xlockprofthreshold:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700342 parsed->lock_profiling_threshold_ = ParseIntegerOrDie(option);
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700343 } else if (option.starts_with("-Xstacktracefile:")) {
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700344// always show stack traces in debug builds
345#ifdef NDEBUG
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700346 parsed->stack_trace_file_ = option.substr(strlen("-Xstacktracefile:")).data();
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700347#endif
Elliott Hughesfc861622011-10-17 17:57:47 -0700348 } else if (option == "sensitiveThread") {
349 parsed->hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(options[i].second);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700350 } else if (option == "vfprintf") {
351 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
352 } else if (option == "exit") {
353 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
354 } else if (option == "abort") {
355 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700356 } else if (option == "host-prefix") {
357 parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700358 } else {
359 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700360 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700361 LOG(ERROR) << "Unrecognized option " << option;
362 // TODO: this should exit, but for now tolerate unknown options
363 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700364 }
365 }
366 }
367
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700368 if (!compiler && parsed->images_.empty()) {
369 parsed->images_.push_back("/data/art-cache/boot.art");
370 }
jeffhaoc1160702011-10-27 15:48:45 -0700371 if (parsed->heap_growth_limit_ == 0) {
372 parsed->heap_growth_limit_ = parsed->heap_maximum_size_;
373 }
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700374
Elliott Hughes85d15452011-09-16 17:33:01 -0700375 LOG(INFO) << "CheckJNI is " << (parsed->check_jni_ ? "on" : "off");
376
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700377 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700378}
379
380Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700381 // TODO: acquire a static mutex on Runtime to avoid racing.
382 if (Runtime::instance_ != NULL) {
383 return NULL;
384 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700385 instance_ = new Runtime;
386 if (!instance_->Init(options, ignore_unrecognized)) {
387 delete instance_;
388 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700389 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700390 return instance_;
391}
Elliott Hughes0af55432011-08-17 18:37:28 -0700392
Brian Carlstromaded5f72011-10-07 17:15:04 -0700393void CreateSystemClassLoader() {
394 if (ClassLoader::UseCompileTimeClassPath()) {
395 return;
396 }
397
398 Thread* self = Thread::Current();
399
400 // Must be in the kNative state for calling native methods.
401 CHECK_EQ(self->GetState(), Thread::kNative);
402
403 JNIEnv* env = self->GetJniEnv();
Brian Carlstromdf143242011-10-10 18:05:34 -0700404 ScopedLocalRef<jclass> ClassLoader_class(env, env->FindClass("java/lang/ClassLoader"));
405 CHECK(ClassLoader_class.get() != NULL);
406 jmethodID getSystemClassLoader = env->GetStaticMethodID(ClassLoader_class.get(),
407 "getSystemClassLoader",
408 "()Ljava/lang/ClassLoader;");
409 CHECK(getSystemClassLoader != NULL);
410 ScopedLocalRef<jobject> class_loader(env, env->CallStaticObjectMethod(ClassLoader_class.get(),
411 getSystemClassLoader));
412 CHECK(class_loader.get() != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700413
Brian Carlstromdf143242011-10-10 18:05:34 -0700414 Thread::Current()->SetClassLoaderOverride(Decode<ClassLoader*>(env, class_loader.get()));
Brian Carlstromaded5f72011-10-07 17:15:04 -0700415}
416
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700417void Runtime::Start() {
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700418 if (IsVerboseStartup()) {
419 LOG(INFO) << "Runtime::Start entering";
420 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700421
422 CHECK(host_prefix_.empty()) << host_prefix_;
423
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700424 // Restore main thread state to kNative as expected by native code
425 Thread::Current()->SetState(Thread::kNative);
426
Elliott Hughes038a8062011-09-18 14:12:41 -0700427 InitNativeMethods();
Elliott Hughes85d15452011-09-16 17:33:01 -0700428
Elliott Hughes038a8062011-09-18 14:12:41 -0700429 Thread::FinishStartup();
Elliott Hughes85d15452011-09-16 17:33:01 -0700430
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700431 class_linker_->RunRootClinits();
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700432
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700433 // Class::AllocObject asserts that all objects allocated better be
434 // initialized after Runtime::IsStarted is true, so this needs to
435 // come after ClassLinker::RunRootClinits.
436 started_ = true;
437
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700438 if (!is_zygote_) {
439 DidForkFromZygote();
440 }
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700441
Elliott Hughes85d15452011-09-16 17:33:01 -0700442 StartDaemonThreads();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700443
Brian Carlstromaded5f72011-10-07 17:15:04 -0700444 CreateSystemClassLoader();
445
Elliott Hughes726079d2011-10-07 18:43:44 -0700446 Thread::Current()->GetJniEnv()->locals.AssertEmpty();
447
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700448 if (IsVerboseStartup()) {
449 LOG(INFO) << "Runtime::Start exiting";
450 }
Elliott Hughes85d15452011-09-16 17:33:01 -0700451}
452
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700453void Runtime::DidForkFromZygote() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700454 is_zygote_ = false;
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700455
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700456 StartSignalCatcher();
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700457
458 // Start the JDWP thread. If the command-line debugger flags specified "suspend=y",
459 // this will pause the runtime, so we probably want this to come last.
460 Dbg::StartJdwp();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700461}
462
463void Runtime::StartSignalCatcher() {
464 if (!is_zygote_) {
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700465 signal_catcher_ = new SignalCatcher(stack_trace_file_);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700466 }
467}
468
Elliott Hughes85d15452011-09-16 17:33:01 -0700469void Runtime::StartDaemonThreads() {
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700470 if (IsVerboseStartup()) {
471 LOG(INFO) << "Runtime::StartDaemonThreads entering";
472 }
Elliott Hughes85d15452011-09-16 17:33:01 -0700473
Elliott Hughes719b3232011-09-25 17:42:19 -0700474 Thread* self = Thread::Current();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700475
476 // Must be in the kNative state for calling native methods.
477 CHECK_EQ(self->GetState(), Thread::kNative);
478
Elliott Hughes719b3232011-09-25 17:42:19 -0700479 JNIEnv* env = self->GetJniEnv();
Elliott Hughes726079d2011-10-07 18:43:44 -0700480 ScopedLocalRef<jclass> c(env, env->FindClass("java/lang/Daemons"));
481 CHECK(c.get() != NULL);
482 jmethodID mid = env->GetStaticMethodID(c.get(), "start", "()V");
Elliott Hughes719b3232011-09-25 17:42:19 -0700483 CHECK(mid != NULL);
Elliott Hughes726079d2011-10-07 18:43:44 -0700484 env->CallStaticVoidMethod(c.get(), mid);
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700485
486 if (IsVerboseStartup()) {
487 LOG(INFO) << "Runtime::StartDaemonThreads exiting";
488 }
Carl Shapiro61e019d2011-07-14 16:53:09 -0700489}
490
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700491bool Runtime::IsStarted() const {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700492 return started_;
493}
494
Elliott Hughes0af55432011-08-17 18:37:28 -0700495bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700496 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700497
Elliott Hughes90a33692011-08-30 13:27:07 -0700498 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
499 if (options.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700500 LOG(ERROR) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700501 return false;
502 }
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700503 verbose_startup_ = options->IsVerbose("startup");
504 if (IsVerboseStartup()) {
505 LOG(INFO) << "Runtime::Init -verbose:startup enabled";
506 }
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700507
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700508 SetJniGlobalsMax(options->jni_globals_max_);
Elliott Hughesfc861622011-10-17 17:57:47 -0700509 Monitor::Init(options->IsVerbose("monitor"), options->lock_profiling_threshold_, options->hook_is_sensitive_thread_);
Elliott Hughes32d6e1e2011-10-11 14:47:44 -0700510
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700511 host_prefix_ = options->host_prefix_;
512 boot_class_path_ = options->boot_class_path_;
513 class_path_ = options->class_path_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700514 properties_ = options->properties_;
515
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700516 is_zygote_ = options->is_zygote_;
517
Elliott Hughes0af55432011-08-17 18:37:28 -0700518 vfprintf_ = options->hook_vfprintf_;
519 exit_ = options->hook_exit_;
520 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700521
Elliott Hughesbe759c62011-09-08 19:38:21 -0700522 default_stack_size_ = options->stack_size_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700523 stack_trace_file_ = options->stack_trace_file_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700524
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700525 monitor_list_ = new MonitorList;
Elliott Hughes14357e82011-09-26 10:42:15 -0700526 thread_list_ = new ThreadList(options->IsVerbose("thread"));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700527 intern_table_ = new InternTable;
528
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700529 Heap::Init(options->IsVerbose("heap"),
530 options->IsVerbose("gc"),
531 options->heap_initial_size_,
532 options->heap_maximum_size_,
jeffhaoc1160702011-10-27 15:48:45 -0700533 options->heap_growth_limit_,
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700534 options->images_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700535
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700536 BlockSignals();
537
Elliott Hughesa0957642011-09-02 14:27:33 -0700538 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700539
Elliott Hughesbe759c62011-09-08 19:38:21 -0700540 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700541
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700542 // ClassLinker needs an attached thread, but we can't fully attach a thread
543 // without creating objects.
544 Thread::Attach(this, "main", false);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700545
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700546 // Set us to runnable so tools using a runtime can allocate and GC by default
547 Thread::Current()->SetState(Thread::kRunnable);
548
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700549 CHECK_GE(Heap::GetSpaces().size(), 1U);
550 class_linker_ = ((Heap::GetSpaces()[0]->IsImageSpace())
551 ? ClassLinker::Create(intern_table_)
552 : ClassLinker::Create(options->boot_class_path_, intern_table_));
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700553
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700554 if (IsVerboseStartup()) {
555 LOG(INFO) << "Runtime::Init exiting";
556 }
Carl Shapiro1fb86202011-06-27 17:43:13 -0700557 return true;
558}
559
Elliott Hughes038a8062011-09-18 14:12:41 -0700560void Runtime::InitNativeMethods() {
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700561 if (IsVerboseStartup()) {
562 LOG(INFO) << "Runtime::InitNativeMethods entering";
563 }
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700564 Thread* self = Thread::Current();
565 JNIEnv* env = self->GetJniEnv();
566
Elliott Hughes418d20f2011-09-22 14:00:39 -0700567 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Brian Carlstromaded5f72011-10-07 17:15:04 -0700568 CHECK_EQ(self->GetState(), Thread::kNative);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700569
Elliott Hughes418d20f2011-09-22 14:00:39 -0700570 // First set up JniConstants, which is used by both the runtime's built-in native
571 // methods and libcore.
Elliott Hughesfea966e2011-09-22 10:26:20 -0700572 JniConstants::init(env);
573
Elliott Hughes418d20f2011-09-22 14:00:39 -0700574 // Then set up the native methods provided by the runtime itself.
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700575 RegisterRuntimeNativeMethods(env);
576
Elliott Hughes418d20f2011-09-22 14:00:39 -0700577 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
578 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
579 // the library that implements System.loadLibrary!
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700580 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700581 if (IsVerboseStartup()) {
582 LOG(INFO) << "Runtime::InitNativeMethods exiting";
583 }
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700584}
585
586void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
587#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700588 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700589 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700590 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700591 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700592 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700593 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700594 REGISTER(register_java_lang_Object);
595 REGISTER(register_java_lang_Runtime);
596 REGISTER(register_java_lang_String);
597 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700598 REGISTER(register_java_lang_Thread);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700599 REGISTER(register_java_lang_Throwable);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700600 REGISTER(register_java_lang_VMClassLoader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700601 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700602 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700603 REGISTER(register_java_lang_reflect_Field);
604 REGISTER(register_java_lang_reflect_Method);
Jesse Wilson95caa792011-10-12 18:14:17 -0400605 REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700606 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700607 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700608 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700609 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700610#undef REGISTER
611}
612
Elliott Hughes8daa0922011-09-11 13:46:25 -0700613void Runtime::Dump(std::ostream& os) {
Elliott Hughese27955c2011-08-26 15:21:24 -0700614 // TODO: dump other runtime statistics?
615 os << "Loaded classes: " << class_linker_->NumLoadedClasses() << "\n";
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700616 os << "Intern table size: " << GetInternTable()->Size() << "\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700617 // LOGV("VM stats: meth=%d ifld=%d sfld=%d linear=%d",
618 // gDvm.numDeclaredMethods,
619 // gDvm.numDeclaredInstFields,
620 // gDvm.numDeclaredStaticFields,
621 // gDvm.pBootLoaderAlloc->curOffset);
622 // LOGI("GC precise methods: %d", dvmPointerSetGetCount(gDvm.preciseMethods));
Elliott Hughes42ee1422011-09-06 12:33:32 -0700623 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700624
625 thread_list_->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700626}
627
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700628void Runtime::SetStatsEnabled(bool new_state) {
629 if (new_state == true) {
630 GetStats()->Clear(~0);
631 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
632 Thread::Current()->GetStats()->Clear(~0);
633 }
634 stats_enabled_ = new_state;
635}
636
637void Runtime::ResetStats(int kinds) {
638 GetStats()->Clear(kinds & 0xffff);
639 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
640 Thread::Current()->GetStats()->Clear(kinds >> 16);
641}
642
643RuntimeStats* Runtime::GetStats() {
644 return &stats_;
645}
646
647int32_t Runtime::GetStat(int kind) {
648 RuntimeStats* stats;
649 if (kind < (1<<16)) {
650 stats = GetStats();
651 } else {
652 stats = Thread::Current()->GetStats();
653 kind >>= 16;
654 }
655 switch (kind) {
656 case KIND_ALLOCATED_OBJECTS:
657 return stats->allocated_objects;
658 case KIND_ALLOCATED_BYTES:
659 return stats->allocated_bytes;
660 case KIND_FREED_OBJECTS:
661 return stats->freed_objects;
662 case KIND_FREED_BYTES:
663 return stats->freed_bytes;
664 case KIND_GC_INVOCATIONS:
665 return stats->gc_for_alloc_count;
666 case KIND_CLASS_INIT_COUNT:
667 return stats->class_init_count;
668 case KIND_CLASS_INIT_TIME:
669 // Convert ns to us, reduce to 32 bits.
670 return (int) (stats->class_init_time_ns / 1000);
671 case KIND_EXT_ALLOCATED_OBJECTS:
672 case KIND_EXT_ALLOCATED_BYTES:
673 case KIND_EXT_FREED_OBJECTS:
674 case KIND_EXT_FREED_BYTES:
675 return 0; // backward compatibility
676 default:
677 CHECK(false);
678 return -1; // unreachable
679 }
680}
681
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700682void Runtime::BlockSignals() {
683 sigset_t sigset;
684 if (sigemptyset(&sigset) == -1) {
685 PLOG(FATAL) << "sigemptyset failed";
686 }
687 if (sigaddset(&sigset, SIGPIPE) == -1) {
688 PLOG(ERROR) << "sigaddset SIGPIPE failed";
689 }
690 // SIGQUIT is used to dump the runtime's state (including stack traces).
691 if (sigaddset(&sigset, SIGQUIT) == -1) {
692 PLOG(ERROR) << "sigaddset SIGQUIT failed";
693 }
694 // SIGUSR1 is used to initiate a heap dump.
695 if (sigaddset(&sigset, SIGUSR1) == -1) {
696 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
697 }
698 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
699}
700
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700701void Runtime::AttachCurrentThread(const char* name, bool as_daemon) {
702 Thread::Attach(instance_, name, as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700703}
704
Elliott Hughesd92bec42011-09-02 17:04:36 -0700705void Runtime::DetachCurrentThread() {
Elliott Hughes038a8062011-09-18 14:12:41 -0700706 // TODO: check we're not calling DetachCurrentThread from a call stack that
707 // includes managed frames. (It's only valid if the stack is all-native.)
Elliott Hughes02b48d12011-09-07 17:15:51 -0700708 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700709}
710
Brian Carlstrome24fa612011-09-29 00:53:55 -0700711void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700712 Dbg::VisitRoots(visitor, arg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700713 class_linker_->VisitRoots(visitor, arg);
714 intern_table_->VisitRoots(visitor, arg);
715 java_vm_->VisitRoots(visitor, arg);
716 thread_list_->VisitRoots(visitor, arg);
717 visitor(jni_stub_array_, arg);
718 visitor(abstract_method_error_stub_array_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700719 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
720 visitor(resolution_stub_array_[i], arg);
721 }
722 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
723 visitor(callee_save_method_[i], arg);
724 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700725}
726
727bool Runtime::HasJniStubArray() const {
728 return jni_stub_array_ != NULL;
729}
730
731ByteArray* Runtime::GetJniStubArray() const {
732 CHECK(jni_stub_array_ != NULL);
733 return jni_stub_array_;
734}
735
736void Runtime::SetJniStubArray(ByteArray* jni_stub_array) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700737 CHECK(jni_stub_array != NULL) << " jni_stub_array=" << jni_stub_array;
738 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array)
739 << "jni_stub_array_=" << jni_stub_array_ << " jni_stub_array=" << jni_stub_array;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700740 jni_stub_array_ = jni_stub_array;
741}
742
743bool Runtime::HasAbstractMethodErrorStubArray() const {
744 return abstract_method_error_stub_array_ != NULL;
745}
746
747ByteArray* Runtime::GetAbstractMethodErrorStubArray() const {
748 CHECK(abstract_method_error_stub_array_ != NULL);
749 return abstract_method_error_stub_array_;
750}
751
752void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) {
753 CHECK(abstract_method_error_stub_array != NULL);
754 CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array);
755 abstract_method_error_stub_array_ = abstract_method_error_stub_array;
756}
757
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700758
759Runtime::TrampolineType Runtime::GetTrampolineType(Method* method) {
760 if (method == NULL) {
761 return Runtime::kUnknownMethod;
762 } else if (method->IsStatic()) {
763 return Runtime::kStaticMethod;
764 } else {
765 return Runtime::kInstanceMethod;
766 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700767}
768
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700769bool Runtime::HasResolutionStubArray(TrampolineType type) const {
770 return resolution_stub_array_[type] != NULL;
Ian Rogersad25ac52011-10-04 19:13:33 -0700771}
772
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700773ByteArray* Runtime::GetResolutionStubArray(TrampolineType type) const {
774 CHECK(HasResolutionStubArray(type));
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700775 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastTrampolineMethodType));
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700776 return resolution_stub_array_[type];
777}
778
779void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700780 CHECK(resolution_stub_array != NULL);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700781 CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array);
782 resolution_stub_array_[type] = resolution_stub_array;
Ian Rogersad25ac52011-10-04 19:13:33 -0700783}
784
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700785Method* Runtime::CreateCalleeSaveMethod(InstructionSet insns, CalleeSaveType type) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700786 Class* method_class = Method::GetMethodClass();
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700787 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
Ian Rogersff1ed472011-09-20 13:46:24 -0700788 method->SetDeclaringClass(method_class);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700789 const char* name;
790 if (type == kSaveAll) {
791 name = "$$$callee_save_method$$$";
792 } else if (type == kRefsOnly) {
793 name = "$$$refs_only_callee_save_method$$$";
794 } else {
795 DCHECK(type == kRefsAndArgs);
796 name = "$$$refs_and_args_callee_save_method$$$";
797 }
798 method->SetName(intern_table_->InternStrong(name));
Elliott Hughes30646832011-10-13 16:59:46 -0700799 CHECK(method->GetName() != NULL);
Ian Rogersff1ed472011-09-20 13:46:24 -0700800 method->SetSignature(intern_table_->InternStrong("()V"));
Elliott Hughes30646832011-10-13 16:59:46 -0700801 CHECK(method->GetSignature() != NULL);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700802 method->SetCode(NULL);
Ian Rogersff1ed472011-09-20 13:46:24 -0700803 if ((insns == kThumb2) || (insns == kArm)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700804 uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6) | (1 << art::arm::R7) |
805 (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11);
806 uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3);
807 uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9);
808 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills :0) |
809 (type == kSaveAll ? all_spills :0) | (1 << art::arm::LR);
810 uint32_t fp_all_spills = (1 << art::arm::S0) | (1 << art::arm::S1) | (1 << art::arm::S2) |
811 (1 << art::arm::S3) | (1 << art::arm::S4) | (1 << art::arm::S5) |
812 (1 << art::arm::S6) | (1 << art::arm::S7) | (1 << art::arm::S8) |
813 (1 << art::arm::S9) | (1 << art::arm::S10) | (1 << art::arm::S11) |
814 (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) |
815 (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) |
816 (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) |
817 (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) |
818 (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) |
819 (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
820 (1 << art::arm::S30) | (1 << art::arm::S31);
821 uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
822 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
823 __builtin_popcount(fp_spills) /* fprs */ +
824 1 /* Method* */) * kPointerSize, kStackAlignment);
Ian Rogers15fdb8c2011-09-25 15:45:07 -0700825 method->SetFrameSizeInBytes(frame_size);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700826 method->SetCoreSpillMask(core_spills);
827 method->SetFpSpillMask(fp_spills);
Ian Rogersff1ed472011-09-20 13:46:24 -0700828 } else if (insns == kX86) {
829 method->SetFrameSizeInBytes(32);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700830 method->SetCoreSpillMask((1 << art::x86::EBX) | (1 << art::x86::EBP) | (1 << art::x86::ESI) |
Ian Rogersff1ed472011-09-20 13:46:24 -0700831 (1 << art::x86::EDI));
832 method->SetFpSpillMask(0);
833 } else {
834 UNIMPLEMENTED(FATAL);
835 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700836 return method.get();
Ian Rogersff1ed472011-09-20 13:46:24 -0700837}
838
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700839bool Runtime::HasCalleeSaveMethod(CalleeSaveType type) const {
840 return callee_save_method_[type] != NULL;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700841}
842
Brian Carlstrome24fa612011-09-29 00:53:55 -0700843// Returns a special method that describes all callee saves being spilled to the stack.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700844Method* Runtime::GetCalleeSaveMethod(CalleeSaveType type) const {
845 CHECK(HasCalleeSaveMethod(type));
846 return callee_save_method_[type];
Brian Carlstrome24fa612011-09-29 00:53:55 -0700847}
848
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700849void Runtime::SetCalleeSaveMethod(Method* method, CalleeSaveType type) {
850 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
851 callee_save_method_[type] = method;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700852}
853
Carl Shapiro1fb86202011-06-27 17:43:13 -0700854} // namespace art