blob: 656fdb8c69cb1f748561591df71190fb1a9d5be1 [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 Hughescac6cc72011-11-03 20:31:21 -070079static bool gAborting = false;
80
Elliott Hughese0918552011-10-28 17:18:29 -070081struct AbortState {
82 void Dump(std::ostream& os) {
Elliott Hughescac6cc72011-11-03 20:31:21 -070083 if (gAborting) {
84 os << "Runtime aborting --- recursively, so no thread-specific detail!\n";
85 return;
86 }
87 gAborting = true;
Elliott Hughese0918552011-10-28 17:18:29 -070088 os << "Runtime aborting...\n";
89 Thread* self = Thread::Current();
90 if (self == NULL) {
91 os << "(Aborting thread was not attached to runtime!)\n";
92 } else {
93 self->Dump(os, true);
94 }
95 }
96};
97
Elliott Hughesffe67362011-07-17 12:09:27 -070098void Runtime::Abort(const char* file, int line) {
99 // Get any pending output out of the way.
100 fflush(NULL);
101
102 // Many people have difficulty distinguish aborts from crashes,
103 // so be explicit.
Elliott Hughese0918552011-10-28 17:18:29 -0700104 AbortState state;
105 LOG(ERROR) << Dumpable<AbortState>(state);
Elliott Hughesffe67362011-07-17 12:09:27 -0700106
Elliott Hughesffe67362011-07-17 12:09:27 -0700107 // Perform any platform-specific pre-abort actions.
108 PlatformAbort(file, line);
109
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700110 // use abort hook if we have one
111 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
112 Runtime::Current()->abort_();
113 // notreached
114 }
115
Elliott Hughesffe67362011-07-17 12:09:27 -0700116 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -0700117 // receive SIGABRT. debuggerd dumps the stack trace of the main
118 // thread, whether or not that was the thread that failed. By
119 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -0700120 // fault in the current thread, and get a useful log from debuggerd.
121 // We can also trivially tell the difference between a VM crash and
122 // a deliberate abort by looking at the fault address.
123 *reinterpret_cast<char*>(0xdeadd00d) = 38;
124 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -0700125 // notreached
126}
127
Elliott Hughesbf86d042011-08-31 17:53:14 -0700128void Runtime::CallExitHook(jint status) {
129 if (exit_ != NULL) {
130 ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative);
131 exit_(status);
132 LOG(WARNING) << "Exit hook returned instead of exiting!";
133 }
134}
135
Brian Carlstrom8a436592011-08-15 21:27:23 -0700136// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
137// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
138// [gG] gigabytes.
139//
140// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700141// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
142// of 1024.
143//
144// The spec says the -Xmx and -Xms options must be multiples of 1024. It
145// doesn't say anything about -Xss.
146//
147// Returns 0 (a useless size) if "s" is malformed or specifies a low or
148// non-evenly-divisible value.
149//
150size_t ParseMemoryOption(const char *s, size_t div) {
151 // strtoul accepts a leading [+-], which we don't want,
152 // so make sure our string starts with a decimal digit.
153 if (isdigit(*s)) {
154 const char *s2;
155 size_t val = strtoul(s, (char **)&s2, 10);
156 if (s2 != s) {
157 // s2 should be pointing just after the number.
158 // If this is the end of the string, the user
159 // has specified a number of bytes. Otherwise,
160 // there should be exactly one more character
161 // that specifies a multiplier.
162 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700163 // The remainder of the string is either a single multiplier
164 // character, or nothing to indicate that the value is in
165 // bytes.
166 char c = *s2++;
167 if (*s2 == '\0') {
168 size_t mul;
169 if (c == '\0') {
170 mul = 1;
171 } else if (c == 'k' || c == 'K') {
172 mul = KB;
173 } else if (c == 'm' || c == 'M') {
174 mul = MB;
175 } else if (c == 'g' || c == 'G') {
176 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700177 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700178 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700179 return 0;
180 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700181
182 if (val <= std::numeric_limits<size_t>::max() / mul) {
183 val *= mul;
184 } else {
185 // Clamp to a multiple of 1024.
186 val = std::numeric_limits<size_t>::max() & ~(1024-1);
187 }
188 } else {
189 // There's more than one character after the numeric part.
190 return 0;
191 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700192 }
193 // The man page says that a -Xm value must be a multiple of 1024.
194 if (val % div == 0) {
195 return val;
196 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700197 }
198 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700199 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700200}
201
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700202size_t ParseIntegerOrDie(const StringPiece& s) {
203 StringPiece::size_type colon = s.find(':');
204 if (colon == StringPiece::npos) {
205 LOG(FATAL) << "Missing integer: " << s;
206 }
207 const char* begin = &s.data()[colon + 1];
208 char* end;
209 size_t result = strtoul(begin, &end, 10);
210 if (begin == end || *end != '\0') {
211 LOG(FATAL) << "Failed to parse integer in: " << s;
212 }
213 return result;
214}
215
Elliott Hughes0af55432011-08-17 18:37:28 -0700216void LoadJniLibrary(JavaVMExt* vm, const char* name) {
217 // TODO: OS_SHARED_LIB_FORMAT_STR
218 std::string mapped_name(StringPrintf("lib%s.so", name));
Elliott Hughes75770752011-08-24 17:52:38 -0700219 std::string reason;
220 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700221 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
222 << reason;
223 }
224}
225
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700226Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700227 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700228 bool compiler = false;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700229 const char* boot_class_path = getenv("BOOTCLASSPATH");
230 if (boot_class_path != NULL) {
231 parsed->boot_class_path_ = getenv("BOOTCLASSPATH");
232 }
233 const char* class_path = getenv("CLASSPATH");
234 if (class_path != NULL) {
235 parsed->class_path_ = getenv("CLASSPATH");
236 }
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700237#ifdef NDEBUG
Elliott Hughes5174fe62011-08-23 15:12:35 -0700238 // -Xcheck:jni is off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700239 parsed->check_jni_ = false;
240#else
241 // ...but on by default in debug builds.
242 parsed->check_jni_ = true;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700243#endif
Elliott Hughes85d15452011-09-16 17:33:01 -0700244
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700245 parsed->heap_initial_size_ = Heap::kInitialSize;
246 parsed->heap_maximum_size_ = Heap::kMaximumSize;
jeffhaoc1160702011-10-27 15:48:45 -0700247 parsed->heap_growth_limit_ = 0; // 0 means no growth limit
Brian Carlstromf734cf52011-08-17 16:28:14 -0700248 parsed->stack_size_ = Thread::kDefaultStackSize;
249
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700250 parsed->is_zygote_ = false;
251
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700252 parsed->jni_globals_max_ = 0;
Elliott Hughesfc861622011-10-17 17:57:47 -0700253 parsed->lock_profiling_threshold_ = 0;
254 parsed->hook_is_sensitive_thread_ = NULL;
255
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700256 parsed->hook_vfprintf_ = vfprintf;
257 parsed->hook_exit_ = exit;
258 parsed->hook_abort_ = abort;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700259
260 for (size_t i = 0; i < options.size(); ++i) {
261 const StringPiece& option = options[i].first;
Brian Carlstrom0796af02011-10-12 14:31:45 -0700262 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700263 LOG(INFO) << "option[" << i << "]=" << option;
264 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700265 if (option.starts_with("-Xbootclasspath:")) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700266 parsed->boot_class_path_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700267 } else if (option == "-classpath" || option == "-cp") {
268 // TODO: support -Djava.class.path
269 i++;
270 if (i == options.size()) {
271 // TODO: usage
272 LOG(FATAL) << "Missing required class path value for " << option;
273 return NULL;
274 }
275 const StringPiece& value = options[i].first;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700276 parsed->class_path_ = value.data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700277 } else if (option.starts_with("-Ximage:")) {
278 parsed->images_.push_back(option.substr(strlen("-Ximage:")).data());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700279 } else if (option.starts_with("-Xcheck:jni")) {
280 parsed->check_jni_ = true;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700281 } else if (option.starts_with("-Xrunjdwp:") || option.starts_with("-agentlib:jdwp=")) {
282 std::string tail = option.substr(option[1] == 'X' ? 10 : 15).ToString();
283 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
284 LOG(FATAL) << "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
285 << "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n";
286 return NULL;
287 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700288 } else if (option.starts_with("-Xms")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700289 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024);
290 if (size == 0) {
291 if (ignore_unrecognized) {
292 continue;
293 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700294 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700295 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700296 return NULL;
297 }
298 parsed->heap_initial_size_ = size;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700299 } else if (option.starts_with("-Xmx")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700300 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024);
301 if (size == 0) {
302 if (ignore_unrecognized) {
303 continue;
304 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700305 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700306 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700307 return NULL;
308 }
309 parsed->heap_maximum_size_ = size;
jeffhaoc1160702011-10-27 15:48:45 -0700310 } else if (option.starts_with("-XX:HeapGrowthLimit=")) {
311 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).data(), 1024);
312 if (size == 0) {
313 if (ignore_unrecognized) {
314 continue;
315 }
316 // TODO: usage
317 LOG(FATAL) << "Failed to parse " << option;
318 return NULL;
319 }
320 parsed->heap_growth_limit_ = size;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700321 } else if (option.starts_with("-Xss")) {
322 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1);
323 if (size == 0) {
324 if (ignore_unrecognized) {
325 continue;
326 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700327 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700328 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700329 return NULL;
330 }
331 parsed->stack_size_ = size;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700332 } else if (option.starts_with("-D")) {
333 parsed->properties_.push_back(option.substr(strlen("-D")).data());
Elliott Hughesa0957642011-09-02 14:27:33 -0700334 } else if (option.starts_with("-Xjnitrace:")) {
335 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:")).data();
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700336 } else if (option == "compiler") {
337 compiler = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700338 } else if (option == "-Xzygote") {
339 parsed->is_zygote_ = true;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700340 } else if (option.starts_with("-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700341 std::vector<std::string> verbose_options;
Elliott Hughes34023802011-08-30 12:06:17 -0700342 Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700343 for (size_t i = 0; i < verbose_options.size(); ++i) {
344 parsed->verbose_.insert(verbose_options[i]);
345 }
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700346 } else if (option.starts_with("-Xjnigreflimit:")) {
347 parsed->jni_globals_max_ = ParseIntegerOrDie(option);
Elliott Hughesfc861622011-10-17 17:57:47 -0700348 } else if (option.starts_with("-Xlockprofthreshold:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700349 parsed->lock_profiling_threshold_ = ParseIntegerOrDie(option);
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700350 } else if (option.starts_with("-Xstacktracefile:")) {
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700351// always show stack traces in debug builds
352#ifdef NDEBUG
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700353 parsed->stack_trace_file_ = option.substr(strlen("-Xstacktracefile:")).data();
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700354#endif
Elliott Hughesfc861622011-10-17 17:57:47 -0700355 } else if (option == "sensitiveThread") {
356 parsed->hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(options[i].second);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700357 } else if (option == "vfprintf") {
358 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
359 } else if (option == "exit") {
360 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
361 } else if (option == "abort") {
362 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700363 } else if (option == "host-prefix") {
364 parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700365 } else {
366 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700367 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700368 LOG(ERROR) << "Unrecognized option " << option;
369 // TODO: this should exit, but for now tolerate unknown options
370 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700371 }
372 }
373 }
374
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700375 if (!compiler && parsed->images_.empty()) {
376 parsed->images_.push_back("/data/art-cache/boot.art");
377 }
jeffhaoc1160702011-10-27 15:48:45 -0700378 if (parsed->heap_growth_limit_ == 0) {
379 parsed->heap_growth_limit_ = parsed->heap_maximum_size_;
380 }
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700381
Elliott Hughescac6cc72011-11-03 20:31:21 -0700382 LOG(INFO) << "Build type: "
383#ifndef NDEBUG
384 << "debug"
385#else
386 << "optimized"
387#endif
388 << "; CheckJNI: " << (parsed->check_jni_ ? "on" : "off");
Elliott Hughes85d15452011-09-16 17:33:01 -0700389
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700390 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700391}
392
393Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700394 // TODO: acquire a static mutex on Runtime to avoid racing.
395 if (Runtime::instance_ != NULL) {
396 return NULL;
397 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700398 instance_ = new Runtime;
399 if (!instance_->Init(options, ignore_unrecognized)) {
400 delete instance_;
401 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700402 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700403 return instance_;
404}
Elliott Hughes0af55432011-08-17 18:37:28 -0700405
Brian Carlstromaded5f72011-10-07 17:15:04 -0700406void CreateSystemClassLoader() {
407 if (ClassLoader::UseCompileTimeClassPath()) {
408 return;
409 }
410
411 Thread* self = Thread::Current();
412
413 // Must be in the kNative state for calling native methods.
414 CHECK_EQ(self->GetState(), Thread::kNative);
415
416 JNIEnv* env = self->GetJniEnv();
Brian Carlstromdf143242011-10-10 18:05:34 -0700417 ScopedLocalRef<jclass> ClassLoader_class(env, env->FindClass("java/lang/ClassLoader"));
418 CHECK(ClassLoader_class.get() != NULL);
419 jmethodID getSystemClassLoader = env->GetStaticMethodID(ClassLoader_class.get(),
420 "getSystemClassLoader",
421 "()Ljava/lang/ClassLoader;");
422 CHECK(getSystemClassLoader != NULL);
423 ScopedLocalRef<jobject> class_loader(env, env->CallStaticObjectMethod(ClassLoader_class.get(),
424 getSystemClassLoader));
425 CHECK(class_loader.get() != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700426
Brian Carlstromdf143242011-10-10 18:05:34 -0700427 Thread::Current()->SetClassLoaderOverride(Decode<ClassLoader*>(env, class_loader.get()));
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500428
429 ScopedLocalRef<jclass> Thread_class(env, env->FindClass("java/lang/Thread"));
430 CHECK(Thread_class.get() != NULL);
431 jfieldID contextClassLoader = env->GetFieldID(Thread_class.get(),
432 "contextClassLoader",
433 "Ljava/lang/ClassLoader;");
434 CHECK(contextClassLoader != NULL);
435 ScopedLocalRef<jobject> self_jobject(env, AddLocalReference<jobject>(env, self->GetPeer()));
436 env->SetObjectField(self_jobject.get(), contextClassLoader, class_loader.get());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700437}
438
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700439void Runtime::Start() {
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700440 if (IsVerboseStartup()) {
441 LOG(INFO) << "Runtime::Start entering";
442 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700443
444 CHECK(host_prefix_.empty()) << host_prefix_;
445
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700446 // Restore main thread state to kNative as expected by native code
447 Thread::Current()->SetState(Thread::kNative);
448
Elliott Hughes038a8062011-09-18 14:12:41 -0700449 InitNativeMethods();
Elliott Hughes85d15452011-09-16 17:33:01 -0700450
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700451 started_ = true;
452
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500453 Thread::FinishStartup();
454
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700455 if (!is_zygote_) {
456 DidForkFromZygote();
457 }
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700458
Elliott Hughes85d15452011-09-16 17:33:01 -0700459 StartDaemonThreads();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700460
Brian Carlstromaded5f72011-10-07 17:15:04 -0700461 CreateSystemClassLoader();
462
Elliott Hughes726079d2011-10-07 18:43:44 -0700463 Thread::Current()->GetJniEnv()->locals.AssertEmpty();
464
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700465 if (IsVerboseStartup()) {
466 LOG(INFO) << "Runtime::Start exiting";
467 }
Elliott Hughes85d15452011-09-16 17:33:01 -0700468}
469
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700470void Runtime::DidForkFromZygote() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700471 is_zygote_ = false;
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700472
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700473 StartSignalCatcher();
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700474
475 // Start the JDWP thread. If the command-line debugger flags specified "suspend=y",
476 // this will pause the runtime, so we probably want this to come last.
477 Dbg::StartJdwp();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700478}
479
480void Runtime::StartSignalCatcher() {
481 if (!is_zygote_) {
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700482 signal_catcher_ = new SignalCatcher(stack_trace_file_);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700483 }
484}
485
Elliott Hughes85d15452011-09-16 17:33:01 -0700486void Runtime::StartDaemonThreads() {
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700487 if (IsVerboseStartup()) {
488 LOG(INFO) << "Runtime::StartDaemonThreads entering";
489 }
Elliott Hughes85d15452011-09-16 17:33:01 -0700490
Elliott Hughes719b3232011-09-25 17:42:19 -0700491 Thread* self = Thread::Current();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700492
493 // Must be in the kNative state for calling native methods.
494 CHECK_EQ(self->GetState(), Thread::kNative);
495
Elliott Hughes719b3232011-09-25 17:42:19 -0700496 JNIEnv* env = self->GetJniEnv();
Elliott Hughes726079d2011-10-07 18:43:44 -0700497 ScopedLocalRef<jclass> c(env, env->FindClass("java/lang/Daemons"));
498 CHECK(c.get() != NULL);
499 jmethodID mid = env->GetStaticMethodID(c.get(), "start", "()V");
Elliott Hughes719b3232011-09-25 17:42:19 -0700500 CHECK(mid != NULL);
Elliott Hughes726079d2011-10-07 18:43:44 -0700501 env->CallStaticVoidMethod(c.get(), mid);
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700502
503 if (IsVerboseStartup()) {
504 LOG(INFO) << "Runtime::StartDaemonThreads exiting";
505 }
Carl Shapiro61e019d2011-07-14 16:53:09 -0700506}
507
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700508bool Runtime::IsStarted() const {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700509 return started_;
510}
511
Elliott Hughes0af55432011-08-17 18:37:28 -0700512bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700513 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700514
Elliott Hughes90a33692011-08-30 13:27:07 -0700515 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
516 if (options.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700517 LOG(ERROR) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700518 return false;
519 }
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700520 verbose_startup_ = options->IsVerbose("startup");
521 if (IsVerboseStartup()) {
522 LOG(INFO) << "Runtime::Init -verbose:startup enabled";
523 }
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700524
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700525 SetJniGlobalsMax(options->jni_globals_max_);
Elliott Hughesfc861622011-10-17 17:57:47 -0700526 Monitor::Init(options->IsVerbose("monitor"), options->lock_profiling_threshold_, options->hook_is_sensitive_thread_);
Elliott Hughes32d6e1e2011-10-11 14:47:44 -0700527
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700528 host_prefix_ = options->host_prefix_;
529 boot_class_path_ = options->boot_class_path_;
530 class_path_ = options->class_path_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700531 properties_ = options->properties_;
532
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700533 is_zygote_ = options->is_zygote_;
534
Elliott Hughes0af55432011-08-17 18:37:28 -0700535 vfprintf_ = options->hook_vfprintf_;
536 exit_ = options->hook_exit_;
537 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700538
Elliott Hughesbe759c62011-09-08 19:38:21 -0700539 default_stack_size_ = options->stack_size_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700540 stack_trace_file_ = options->stack_trace_file_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700541
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700542 monitor_list_ = new MonitorList;
Elliott Hughes14357e82011-09-26 10:42:15 -0700543 thread_list_ = new ThreadList(options->IsVerbose("thread"));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700544 intern_table_ = new InternTable;
545
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700546 Heap::Init(options->IsVerbose("heap"),
547 options->IsVerbose("gc"),
548 options->heap_initial_size_,
549 options->heap_maximum_size_,
jeffhaoc1160702011-10-27 15:48:45 -0700550 options->heap_growth_limit_,
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700551 options->images_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700552
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700553 BlockSignals();
554
Elliott Hughesa0957642011-09-02 14:27:33 -0700555 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700556
Elliott Hughesbe759c62011-09-08 19:38:21 -0700557 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700558
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700559 // ClassLinker needs an attached thread, but we can't fully attach a thread
560 // without creating objects.
561 Thread::Attach(this, "main", false);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700562
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700563 // Set us to runnable so tools using a runtime can allocate and GC by default
564 Thread::Current()->SetState(Thread::kRunnable);
565
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700566 CHECK_GE(Heap::GetSpaces().size(), 1U);
567 class_linker_ = ((Heap::GetSpaces()[0]->IsImageSpace())
568 ? ClassLinker::Create(intern_table_)
569 : ClassLinker::Create(options->boot_class_path_, intern_table_));
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700570
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700571 if (IsVerboseStartup()) {
572 LOG(INFO) << "Runtime::Init exiting";
573 }
Carl Shapiro1fb86202011-06-27 17:43:13 -0700574 return true;
575}
576
Elliott Hughes038a8062011-09-18 14:12:41 -0700577void Runtime::InitNativeMethods() {
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700578 if (IsVerboseStartup()) {
579 LOG(INFO) << "Runtime::InitNativeMethods entering";
580 }
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700581 Thread* self = Thread::Current();
582 JNIEnv* env = self->GetJniEnv();
583
Elliott Hughes418d20f2011-09-22 14:00:39 -0700584 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Brian Carlstromaded5f72011-10-07 17:15:04 -0700585 CHECK_EQ(self->GetState(), Thread::kNative);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700586
Elliott Hughes418d20f2011-09-22 14:00:39 -0700587 // First set up JniConstants, which is used by both the runtime's built-in native
588 // methods and libcore.
Elliott Hughesfea966e2011-09-22 10:26:20 -0700589 JniConstants::init(env);
590
Elliott Hughes418d20f2011-09-22 14:00:39 -0700591 // Then set up the native methods provided by the runtime itself.
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700592 RegisterRuntimeNativeMethods(env);
593
Elliott Hughes418d20f2011-09-22 14:00:39 -0700594 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
595 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
596 // the library that implements System.loadLibrary!
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700597 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700598 if (IsVerboseStartup()) {
599 LOG(INFO) << "Runtime::InitNativeMethods exiting";
600 }
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700601}
602
603void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
604#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700605 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700606 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700607 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700608 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700609 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700610 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700611 REGISTER(register_java_lang_Object);
612 REGISTER(register_java_lang_Runtime);
613 REGISTER(register_java_lang_String);
614 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700615 REGISTER(register_java_lang_Thread);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700616 REGISTER(register_java_lang_Throwable);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700617 REGISTER(register_java_lang_VMClassLoader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700618 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700619 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700620 REGISTER(register_java_lang_reflect_Field);
621 REGISTER(register_java_lang_reflect_Method);
Jesse Wilson95caa792011-10-12 18:14:17 -0400622 REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700623 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700624 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700625 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700626 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700627#undef REGISTER
628}
629
Elliott Hughes8daa0922011-09-11 13:46:25 -0700630void Runtime::Dump(std::ostream& os) {
Elliott Hughese27955c2011-08-26 15:21:24 -0700631 // TODO: dump other runtime statistics?
Elliott Hughescac6cc72011-11-03 20:31:21 -0700632 GetClassLinker()->DumpForSigQuit(os);
633 GetInternTable()->DumpForSigQuit(os);
Elliott Hughes42ee1422011-09-06 12:33:32 -0700634 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700635
636 thread_list_->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700637}
638
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700639void Runtime::SetStatsEnabled(bool new_state) {
640 if (new_state == true) {
641 GetStats()->Clear(~0);
642 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
643 Thread::Current()->GetStats()->Clear(~0);
644 }
645 stats_enabled_ = new_state;
646}
647
648void Runtime::ResetStats(int kinds) {
649 GetStats()->Clear(kinds & 0xffff);
650 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
651 Thread::Current()->GetStats()->Clear(kinds >> 16);
652}
653
654RuntimeStats* Runtime::GetStats() {
655 return &stats_;
656}
657
658int32_t Runtime::GetStat(int kind) {
659 RuntimeStats* stats;
660 if (kind < (1<<16)) {
661 stats = GetStats();
662 } else {
663 stats = Thread::Current()->GetStats();
664 kind >>= 16;
665 }
666 switch (kind) {
667 case KIND_ALLOCATED_OBJECTS:
668 return stats->allocated_objects;
669 case KIND_ALLOCATED_BYTES:
670 return stats->allocated_bytes;
671 case KIND_FREED_OBJECTS:
672 return stats->freed_objects;
673 case KIND_FREED_BYTES:
674 return stats->freed_bytes;
675 case KIND_GC_INVOCATIONS:
676 return stats->gc_for_alloc_count;
677 case KIND_CLASS_INIT_COUNT:
678 return stats->class_init_count;
679 case KIND_CLASS_INIT_TIME:
680 // Convert ns to us, reduce to 32 bits.
681 return (int) (stats->class_init_time_ns / 1000);
682 case KIND_EXT_ALLOCATED_OBJECTS:
683 case KIND_EXT_ALLOCATED_BYTES:
684 case KIND_EXT_FREED_OBJECTS:
685 case KIND_EXT_FREED_BYTES:
686 return 0; // backward compatibility
687 default:
688 CHECK(false);
689 return -1; // unreachable
690 }
691}
692
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700693void Runtime::BlockSignals() {
694 sigset_t sigset;
695 if (sigemptyset(&sigset) == -1) {
696 PLOG(FATAL) << "sigemptyset failed";
697 }
698 if (sigaddset(&sigset, SIGPIPE) == -1) {
699 PLOG(ERROR) << "sigaddset SIGPIPE failed";
700 }
701 // SIGQUIT is used to dump the runtime's state (including stack traces).
702 if (sigaddset(&sigset, SIGQUIT) == -1) {
703 PLOG(ERROR) << "sigaddset SIGQUIT failed";
704 }
705 // SIGUSR1 is used to initiate a heap dump.
706 if (sigaddset(&sigset, SIGUSR1) == -1) {
707 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
708 }
709 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
710}
711
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700712void Runtime::AttachCurrentThread(const char* name, bool as_daemon) {
713 Thread::Attach(instance_, name, as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700714}
715
Elliott Hughesd92bec42011-09-02 17:04:36 -0700716void Runtime::DetachCurrentThread() {
Elliott Hughes038a8062011-09-18 14:12:41 -0700717 // TODO: check we're not calling DetachCurrentThread from a call stack that
718 // includes managed frames. (It's only valid if the stack is all-native.)
Elliott Hughes02b48d12011-09-07 17:15:51 -0700719 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700720}
721
Brian Carlstrome24fa612011-09-29 00:53:55 -0700722void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700723 Dbg::VisitRoots(visitor, arg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700724 class_linker_->VisitRoots(visitor, arg);
725 intern_table_->VisitRoots(visitor, arg);
726 java_vm_->VisitRoots(visitor, arg);
727 thread_list_->VisitRoots(visitor, arg);
728 visitor(jni_stub_array_, arg);
729 visitor(abstract_method_error_stub_array_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700730 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
731 visitor(resolution_stub_array_[i], arg);
732 }
733 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
734 visitor(callee_save_method_[i], arg);
735 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700736}
737
Ian Rogers169c9a72011-11-13 20:13:17 -0800738bool Runtime::HasJniDlsymLookupStub() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700739 return jni_stub_array_ != NULL;
740}
741
Ian Rogers169c9a72011-11-13 20:13:17 -0800742ByteArray* Runtime::GetJniDlsymLookupStub() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700743 CHECK(jni_stub_array_ != NULL);
744 return jni_stub_array_;
745}
746
Ian Rogers169c9a72011-11-13 20:13:17 -0800747void Runtime::SetJniDlsymLookupStub(ByteArray* jni_stub_array) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700748 CHECK(jni_stub_array != NULL) << " jni_stub_array=" << jni_stub_array;
749 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array)
750 << "jni_stub_array_=" << jni_stub_array_ << " jni_stub_array=" << jni_stub_array;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700751 jni_stub_array_ = jni_stub_array;
752}
753
754bool Runtime::HasAbstractMethodErrorStubArray() const {
755 return abstract_method_error_stub_array_ != NULL;
756}
757
758ByteArray* Runtime::GetAbstractMethodErrorStubArray() const {
759 CHECK(abstract_method_error_stub_array_ != NULL);
760 return abstract_method_error_stub_array_;
761}
762
763void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) {
764 CHECK(abstract_method_error_stub_array != NULL);
765 CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array);
766 abstract_method_error_stub_array_ = abstract_method_error_stub_array;
767}
768
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700769
770Runtime::TrampolineType Runtime::GetTrampolineType(Method* method) {
771 if (method == NULL) {
772 return Runtime::kUnknownMethod;
773 } else if (method->IsStatic()) {
774 return Runtime::kStaticMethod;
775 } else {
776 return Runtime::kInstanceMethod;
777 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700778}
779
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700780bool Runtime::HasResolutionStubArray(TrampolineType type) const {
781 return resolution_stub_array_[type] != NULL;
Ian Rogersad25ac52011-10-04 19:13:33 -0700782}
783
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700784ByteArray* Runtime::GetResolutionStubArray(TrampolineType type) const {
785 CHECK(HasResolutionStubArray(type));
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700786 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastTrampolineMethodType));
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700787 return resolution_stub_array_[type];
788}
789
790void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700791 CHECK(resolution_stub_array != NULL);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700792 CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array);
793 resolution_stub_array_[type] = resolution_stub_array;
Ian Rogersad25ac52011-10-04 19:13:33 -0700794}
795
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700796Method* Runtime::CreateCalleeSaveMethod(InstructionSet insns, CalleeSaveType type) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700797 Class* method_class = Method::GetMethodClass();
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700798 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
Ian Rogersff1ed472011-09-20 13:46:24 -0700799 method->SetDeclaringClass(method_class);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700800 const char* name;
801 if (type == kSaveAll) {
802 name = "$$$callee_save_method$$$";
803 } else if (type == kRefsOnly) {
804 name = "$$$refs_only_callee_save_method$$$";
805 } else {
806 DCHECK(type == kRefsAndArgs);
807 name = "$$$refs_and_args_callee_save_method$$$";
808 }
809 method->SetName(intern_table_->InternStrong(name));
Elliott Hughes30646832011-10-13 16:59:46 -0700810 CHECK(method->GetName() != NULL);
Ian Rogersff1ed472011-09-20 13:46:24 -0700811 method->SetSignature(intern_table_->InternStrong("()V"));
Elliott Hughes30646832011-10-13 16:59:46 -0700812 CHECK(method->GetSignature() != NULL);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700813 method->SetCode(NULL);
Ian Rogersff1ed472011-09-20 13:46:24 -0700814 if ((insns == kThumb2) || (insns == kArm)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700815 uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6) | (1 << art::arm::R7) |
816 (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11);
817 uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3);
818 uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9);
819 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills :0) |
820 (type == kSaveAll ? all_spills :0) | (1 << art::arm::LR);
821 uint32_t fp_all_spills = (1 << art::arm::S0) | (1 << art::arm::S1) | (1 << art::arm::S2) |
822 (1 << art::arm::S3) | (1 << art::arm::S4) | (1 << art::arm::S5) |
823 (1 << art::arm::S6) | (1 << art::arm::S7) | (1 << art::arm::S8) |
824 (1 << art::arm::S9) | (1 << art::arm::S10) | (1 << art::arm::S11) |
825 (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) |
826 (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) |
827 (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) |
828 (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) |
829 (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) |
830 (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
831 (1 << art::arm::S30) | (1 << art::arm::S31);
832 uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
833 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
834 __builtin_popcount(fp_spills) /* fprs */ +
835 1 /* Method* */) * kPointerSize, kStackAlignment);
Ian Rogers15fdb8c2011-09-25 15:45:07 -0700836 method->SetFrameSizeInBytes(frame_size);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700837 method->SetCoreSpillMask(core_spills);
838 method->SetFpSpillMask(fp_spills);
Ian Rogersff1ed472011-09-20 13:46:24 -0700839 } else if (insns == kX86) {
840 method->SetFrameSizeInBytes(32);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700841 method->SetCoreSpillMask((1 << art::x86::EBX) | (1 << art::x86::EBP) | (1 << art::x86::ESI) |
Ian Rogersff1ed472011-09-20 13:46:24 -0700842 (1 << art::x86::EDI));
843 method->SetFpSpillMask(0);
844 } else {
845 UNIMPLEMENTED(FATAL);
846 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700847 return method.get();
Ian Rogersff1ed472011-09-20 13:46:24 -0700848}
849
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700850bool Runtime::HasCalleeSaveMethod(CalleeSaveType type) const {
851 return callee_save_method_[type] != NULL;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700852}
853
Brian Carlstrome24fa612011-09-29 00:53:55 -0700854// Returns a special method that describes all callee saves being spilled to the stack.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700855Method* Runtime::GetCalleeSaveMethod(CalleeSaveType type) const {
856 CHECK(HasCalleeSaveMethod(type));
857 return callee_save_method_[type];
Brian Carlstrome24fa612011-09-29 00:53:55 -0700858}
859
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700860void Runtime::SetCalleeSaveMethod(Method* method, CalleeSaveType type) {
861 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
862 callee_save_method_[type] = method;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700863}
864
Carl Shapiro1fb86202011-06-27 17:43:13 -0700865} // namespace art