blob: c203ffd1c3bfd2904e0c9732383328c5a9c9fef3 [file] [log] [blame]
Carl Shapiro1fb86202011-06-27 17:43:13 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "runtime.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -07004
Brian Carlstromdbf05b72011-12-15 00:55:24 -08005#include <signal.h>
6
Elliott Hughesffe67362011-07-17 12:09:27 -07007#include <cstdio>
8#include <cstdlib>
Brian Carlstrom8a436592011-08-15 21:27:23 -07009#include <limits>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070010#include <vector>
Elliott Hughesffe67362011-07-17 12:09:27 -070011
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070012#include "class_linker.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070013#include "class_loader.h"
Elliott Hughes3bb81562011-10-21 18:52:59 -070014#include "debugger.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070015#include "heap.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070016#include "image.h"
Elliott Hughescf4c6c42011-09-01 15:16:42 -070017#include "intern_table.h"
Elliott Hughesc5f7c912011-08-18 14:00:42 -070018#include "jni_internal.h"
Elliott Hughes32d6e1e2011-10-11 14:47:44 -070019#include "monitor.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070020#include "oat_file.h"
Elliott Hughes726079d2011-10-07 18:43:44 -070021#include "ScopedLocalRef.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070022#include "signal_catcher.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070023#include "space.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070024#include "thread.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070025#include "thread_list.h"
Elliott Hughes3bb81562011-10-21 18:52:59 -070026#include "UniquePtr.h"
Carl Shapiro61e019d2011-07-14 16:53:09 -070027
Elliott Hughes90a33692011-08-30 13:27:07 -070028// TODO: this drags in cutil/log.h, which conflicts with our logging.h.
29#include "JniConstants.h"
30
Carl Shapiro1fb86202011-06-27 17:43:13 -070031namespace art {
32
Carl Shapiro2ed144c2011-07-26 16:52:08 -070033Runtime* Runtime::instance_ = NULL;
34
Elliott Hughesdcc24742011-09-07 14:02:44 -070035Runtime::Runtime()
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080036 : is_zygote_(false),
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -070037 default_stack_size_(Thread::kDefaultStackSize),
Elliott Hughesc33a32b2011-10-11 18:18:07 -070038 monitor_list_(NULL),
Elliott Hughesdcc24742011-09-07 14:02:44 -070039 thread_list_(NULL),
40 intern_table_(NULL),
41 class_linker_(NULL),
42 signal_catcher_(NULL),
43 java_vm_(NULL),
Brian Carlstrom16192862011-09-12 17:50:06 -070044 jni_stub_array_(NULL),
Brian Carlstrome24fa612011-09-29 00:53:55 -070045 abstract_method_error_stub_array_(NULL),
Elliott Hughes6b355752012-01-13 16:49:08 -080046 shutting_down_(false),
Elliott Hughesdcc24742011-09-07 14:02:44 -070047 started_(false),
48 vfprintf_(NULL),
49 exit_(NULL),
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070050 abort_(NULL),
jeffhao2692b572011-12-16 15:42:28 -080051 stats_enabled_(false),
52 tracer_(NULL) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -070053 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
54 resolution_stub_array_[i] = NULL;
55 }
56 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
57 callee_save_method_[i] = NULL;
58 }
Elliott Hughesdcc24742011-09-07 14:02:44 -070059}
60
Carl Shapiro61e019d2011-07-14 16:53:09 -070061Runtime::~Runtime() {
Elliott Hughes6b355752012-01-13 16:49:08 -080062 shutting_down_ = true;
63
Elliott Hughesd1cc8362011-10-24 16:58:50 -070064 Dbg::StopJdwp();
65
Elliott Hughes5fe594f2011-09-08 12:33:17 -070066 // Make sure our internal threads are dead before we start tearing down things they're using.
67 delete signal_catcher_;
Elliott Hughes038a8062011-09-18 14:12:41 -070068 // TODO: GC thread.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070069
Elliott Hughes038a8062011-09-18 14:12:41 -070070 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
Elliott Hughes93e74e82011-09-13 11:07:03 -070071 delete thread_list_;
Elliott Hughesc33a32b2011-10-11 18:18:07 -070072 delete monitor_list_;
Elliott Hughes93e74e82011-09-13 11:07:03 -070073
Carl Shapiro61e019d2011-07-14 16:53:09 -070074 delete class_linker_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070075 Heap::Destroy();
Elliott Hughescf4c6c42011-09-01 15:16:42 -070076 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070077 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -070078 Thread::Shutdown();
Carl Shapiro4acf4642011-07-26 18:54:13 -070079 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070080 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -070081 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -070082}
83
Elliott Hughescac6cc72011-11-03 20:31:21 -070084static bool gAborting = false;
85
Elliott Hughese0918552011-10-28 17:18:29 -070086struct AbortState {
87 void Dump(std::ostream& os) {
Elliott Hughescac6cc72011-11-03 20:31:21 -070088 if (gAborting) {
89 os << "Runtime aborting --- recursively, so no thread-specific detail!\n";
90 return;
91 }
92 gAborting = true;
Elliott Hughese0918552011-10-28 17:18:29 -070093 os << "Runtime aborting...\n";
94 Thread* self = Thread::Current();
95 if (self == NULL) {
96 os << "(Aborting thread was not attached to runtime!)\n";
97 } else {
98 self->Dump(os, true);
99 }
100 }
101};
102
Elliott Hughesffe67362011-07-17 12:09:27 -0700103void Runtime::Abort(const char* file, int line) {
104 // Get any pending output out of the way.
105 fflush(NULL);
106
107 // Many people have difficulty distinguish aborts from crashes,
108 // so be explicit.
Elliott Hughese0918552011-10-28 17:18:29 -0700109 AbortState state;
110 LOG(ERROR) << Dumpable<AbortState>(state);
Elliott Hughesffe67362011-07-17 12:09:27 -0700111
Elliott Hughesffe67362011-07-17 12:09:27 -0700112 // Perform any platform-specific pre-abort actions.
113 PlatformAbort(file, line);
114
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700115 // use abort hook if we have one
116 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
117 Runtime::Current()->abort_();
118 // notreached
119 }
120
Elliott Hughesffe67362011-07-17 12:09:27 -0700121 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -0700122 // receive SIGABRT. debuggerd dumps the stack trace of the main
123 // thread, whether or not that was the thread that failed. By
124 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -0700125 // fault in the current thread, and get a useful log from debuggerd.
126 // We can also trivially tell the difference between a VM crash and
127 // a deliberate abort by looking at the fault address.
128 *reinterpret_cast<char*>(0xdeadd00d) = 38;
129 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -0700130 // notreached
131}
132
Elliott Hughesbf86d042011-08-31 17:53:14 -0700133void Runtime::CallExitHook(jint status) {
134 if (exit_ != NULL) {
135 ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative);
136 exit_(status);
137 LOG(WARNING) << "Exit hook returned instead of exiting!";
138 }
139}
140
Brian Carlstrom8a436592011-08-15 21:27:23 -0700141// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
142// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
143// [gG] gigabytes.
144//
145// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700146// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
147// of 1024.
148//
149// The spec says the -Xmx and -Xms options must be multiples of 1024. It
150// doesn't say anything about -Xss.
151//
152// Returns 0 (a useless size) if "s" is malformed or specifies a low or
153// non-evenly-divisible value.
154//
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800155size_t ParseMemoryOption(const char* s, size_t div) {
Brian Carlstrom8a436592011-08-15 21:27:23 -0700156 // strtoul accepts a leading [+-], which we don't want,
157 // so make sure our string starts with a decimal digit.
158 if (isdigit(*s)) {
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800159 const char* s2;
160 size_t val = strtoul(s, (char**)&s2, 10);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700161 if (s2 != s) {
162 // s2 should be pointing just after the number.
163 // If this is the end of the string, the user
164 // has specified a number of bytes. Otherwise,
165 // there should be exactly one more character
166 // that specifies a multiplier.
167 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700168 // The remainder of the string is either a single multiplier
169 // character, or nothing to indicate that the value is in
170 // bytes.
171 char c = *s2++;
172 if (*s2 == '\0') {
173 size_t mul;
174 if (c == '\0') {
175 mul = 1;
176 } else if (c == 'k' || c == 'K') {
177 mul = KB;
178 } else if (c == 'm' || c == 'M') {
179 mul = MB;
180 } else if (c == 'g' || c == 'G') {
181 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700182 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700183 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700184 return 0;
185 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700186
187 if (val <= std::numeric_limits<size_t>::max() / mul) {
188 val *= mul;
189 } else {
190 // Clamp to a multiple of 1024.
191 val = std::numeric_limits<size_t>::max() & ~(1024-1);
192 }
193 } else {
194 // There's more than one character after the numeric part.
195 return 0;
196 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700197 }
198 // The man page says that a -Xm value must be a multiple of 1024.
199 if (val % div == 0) {
200 return val;
201 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700202 }
203 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700204 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700205}
206
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700207size_t ParseIntegerOrDie(const StringPiece& s) {
208 StringPiece::size_type colon = s.find(':');
209 if (colon == StringPiece::npos) {
210 LOG(FATAL) << "Missing integer: " << s;
211 }
212 const char* begin = &s.data()[colon + 1];
213 char* end;
214 size_t result = strtoul(begin, &end, 10);
215 if (begin == end || *end != '\0') {
216 LOG(FATAL) << "Failed to parse integer in: " << s;
217 }
218 return result;
219}
220
Elliott Hughes0af55432011-08-17 18:37:28 -0700221void LoadJniLibrary(JavaVMExt* vm, const char* name) {
222 // TODO: OS_SHARED_LIB_FORMAT_STR
223 std::string mapped_name(StringPrintf("lib%s.so", name));
Elliott Hughes75770752011-08-24 17:52:38 -0700224 std::string reason;
225 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700226 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
227 << reason;
228 }
229}
230
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700231Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700232 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700233 bool compiler = false;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700234 const char* boot_class_path = getenv("BOOTCLASSPATH");
235 if (boot_class_path != NULL) {
236 parsed->boot_class_path_ = getenv("BOOTCLASSPATH");
237 }
238 const char* class_path = getenv("CLASSPATH");
239 if (class_path != NULL) {
240 parsed->class_path_ = getenv("CLASSPATH");
241 }
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700242#ifdef NDEBUG
Elliott Hughes5174fe62011-08-23 15:12:35 -0700243 // -Xcheck:jni is off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700244 parsed->check_jni_ = false;
245#else
246 // ...but on by default in debug builds.
247 parsed->check_jni_ = true;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700248#endif
Elliott Hughes85d15452011-09-16 17:33:01 -0700249
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700250 parsed->heap_initial_size_ = Heap::kInitialSize;
251 parsed->heap_maximum_size_ = Heap::kMaximumSize;
jeffhaoc1160702011-10-27 15:48:45 -0700252 parsed->heap_growth_limit_ = 0; // 0 means no growth limit
Brian Carlstromf734cf52011-08-17 16:28:14 -0700253 parsed->stack_size_ = Thread::kDefaultStackSize;
254
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700255 parsed->is_zygote_ = false;
256
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700257 parsed->jni_globals_max_ = 0;
Elliott Hughesfc861622011-10-17 17:57:47 -0700258 parsed->lock_profiling_threshold_ = 0;
259 parsed->hook_is_sensitive_thread_ = NULL;
260
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700261 parsed->hook_vfprintf_ = vfprintf;
262 parsed->hook_exit_ = exit;
263 parsed->hook_abort_ = abort;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700264
265 for (size_t i = 0; i < options.size(); ++i) {
266 const StringPiece& option = options[i].first;
Brian Carlstrom0796af02011-10-12 14:31:45 -0700267 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700268 LOG(INFO) << "option[" << i << "]=" << option;
269 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700270 if (option.starts_with("-Xbootclasspath:")) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700271 parsed->boot_class_path_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700272 } else if (option == "-classpath" || option == "-cp") {
273 // TODO: support -Djava.class.path
274 i++;
275 if (i == options.size()) {
276 // TODO: usage
277 LOG(FATAL) << "Missing required class path value for " << option;
278 return NULL;
279 }
280 const StringPiece& value = options[i].first;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700281 parsed->class_path_ = value.data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700282 } else if (option.starts_with("-Ximage:")) {
283 parsed->images_.push_back(option.substr(strlen("-Ximage:")).data());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700284 } else if (option.starts_with("-Xcheck:jni")) {
285 parsed->check_jni_ = true;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700286 } else if (option.starts_with("-Xrunjdwp:") || option.starts_with("-agentlib:jdwp=")) {
Elliott Hughes95572412011-12-13 18:14:20 -0800287 std::string tail(option.substr(option[1] == 'X' ? 10 : 15).ToString());
Elliott Hughes3bb81562011-10-21 18:52:59 -0700288 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
289 LOG(FATAL) << "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
290 << "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n";
291 return NULL;
292 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700293 } else if (option.starts_with("-Xms")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700294 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).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_initial_size_ = size;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700304 } else if (option.starts_with("-Xmx")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700305 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024);
306 if (size == 0) {
307 if (ignore_unrecognized) {
308 continue;
309 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700310 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700311 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700312 return NULL;
313 }
314 parsed->heap_maximum_size_ = size;
jeffhaoc1160702011-10-27 15:48:45 -0700315 } else if (option.starts_with("-XX:HeapGrowthLimit=")) {
316 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).data(), 1024);
317 if (size == 0) {
318 if (ignore_unrecognized) {
319 continue;
320 }
321 // TODO: usage
322 LOG(FATAL) << "Failed to parse " << option;
323 return NULL;
324 }
325 parsed->heap_growth_limit_ = size;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700326 } else if (option.starts_with("-Xss")) {
327 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1);
328 if (size == 0) {
329 if (ignore_unrecognized) {
330 continue;
331 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700332 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700333 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700334 return NULL;
335 }
336 parsed->stack_size_ = size;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700337 } else if (option.starts_with("-D")) {
338 parsed->properties_.push_back(option.substr(strlen("-D")).data());
Elliott Hughesa0957642011-09-02 14:27:33 -0700339 } else if (option.starts_with("-Xjnitrace:")) {
340 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:")).data();
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700341 } else if (option == "compiler") {
342 compiler = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700343 } else if (option == "-Xzygote") {
344 parsed->is_zygote_ = true;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700345 } else if (option.starts_with("-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700346 std::vector<std::string> verbose_options;
Elliott Hughes34023802011-08-30 12:06:17 -0700347 Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700348 for (size_t i = 0; i < verbose_options.size(); ++i) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800349 if (verbose_options[i] == "class") {
350 gLogVerbosity.class_linker = true;
351 } else if (verbose_options[i] == "compiler") {
352 gLogVerbosity.compiler = true;
353 } else if (verbose_options[i] == "heap") {
354 gLogVerbosity.heap = true;
355 } else if (verbose_options[i] == "gc") {
356 gLogVerbosity.gc = true;
357 } else if (verbose_options[i] == "jdwp") {
358 gLogVerbosity.jdwp = true;
359 } else if (verbose_options[i] == "jni") {
360 gLogVerbosity.jni = true;
361 } else if (verbose_options[i] == "monitor") {
362 gLogVerbosity.monitor = true;
363 } else if (verbose_options[i] == "startup") {
364 gLogVerbosity.startup = true;
365 } else if (verbose_options[i] == "third-party-jni") {
366 gLogVerbosity.third_party_jni = true;
367 } else if (verbose_options[i] == "threads") {
368 gLogVerbosity.threads = true;
369 } else {
370 LOG(WARNING) << "Ignoring unknown -verbose option: " << verbose_options[i];
371 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700372 }
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700373 } else if (option.starts_with("-Xjnigreflimit:")) {
374 parsed->jni_globals_max_ = ParseIntegerOrDie(option);
Elliott Hughesfc861622011-10-17 17:57:47 -0700375 } else if (option.starts_with("-Xlockprofthreshold:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700376 parsed->lock_profiling_threshold_ = ParseIntegerOrDie(option);
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700377 } else if (option.starts_with("-Xstacktracefile:")) {
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700378// always show stack traces in debug builds
379#ifdef NDEBUG
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700380 parsed->stack_trace_file_ = option.substr(strlen("-Xstacktracefile:")).data();
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700381#endif
Elliott Hughesfc861622011-10-17 17:57:47 -0700382 } else if (option == "sensitiveThread") {
383 parsed->hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(options[i].second);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700384 } else if (option == "vfprintf") {
385 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
386 } else if (option == "exit") {
387 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
388 } else if (option == "abort") {
389 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700390 } else if (option == "host-prefix") {
391 parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second);
Elliott Hughesb3e66df2012-01-12 14:49:18 -0800392 } else if (option == "-Xgenregmap" || option == "-Xgc:precise") {
393 // We silently ignore these for backwards compatibility.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700394 } else {
395 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700396 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700397 LOG(ERROR) << "Unrecognized option " << option;
398 // TODO: this should exit, but for now tolerate unknown options
399 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700400 }
401 }
402 }
403
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700404 if (!compiler && parsed->images_.empty()) {
Brian Carlstrom29e7ac72011-12-05 23:42:57 -0800405 parsed->images_.push_back("/system/framework/boot.art");
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700406 }
jeffhaoc1160702011-10-27 15:48:45 -0700407 if (parsed->heap_growth_limit_ == 0) {
408 parsed->heap_growth_limit_ = parsed->heap_maximum_size_;
409 }
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700410
Elliott Hughescac6cc72011-11-03 20:31:21 -0700411 LOG(INFO) << "Build type: "
412#ifndef NDEBUG
413 << "debug"
414#else
415 << "optimized"
416#endif
417 << "; CheckJNI: " << (parsed->check_jni_ ? "on" : "off");
Elliott Hughes85d15452011-09-16 17:33:01 -0700418
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700419 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700420}
421
422Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700423 // TODO: acquire a static mutex on Runtime to avoid racing.
424 if (Runtime::instance_ != NULL) {
425 return NULL;
426 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700427 instance_ = new Runtime;
428 if (!instance_->Init(options, ignore_unrecognized)) {
429 delete instance_;
430 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700431 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700432 return instance_;
433}
Elliott Hughes0af55432011-08-17 18:37:28 -0700434
Brian Carlstromaded5f72011-10-07 17:15:04 -0700435void CreateSystemClassLoader() {
436 if (ClassLoader::UseCompileTimeClassPath()) {
437 return;
438 }
439
440 Thread* self = Thread::Current();
441
442 // Must be in the kNative state for calling native methods.
443 CHECK_EQ(self->GetState(), Thread::kNative);
444
445 JNIEnv* env = self->GetJniEnv();
Brian Carlstromdf143242011-10-10 18:05:34 -0700446 ScopedLocalRef<jclass> ClassLoader_class(env, env->FindClass("java/lang/ClassLoader"));
447 CHECK(ClassLoader_class.get() != NULL);
448 jmethodID getSystemClassLoader = env->GetStaticMethodID(ClassLoader_class.get(),
449 "getSystemClassLoader",
450 "()Ljava/lang/ClassLoader;");
451 CHECK(getSystemClassLoader != NULL);
452 ScopedLocalRef<jobject> class_loader(env, env->CallStaticObjectMethod(ClassLoader_class.get(),
453 getSystemClassLoader));
454 CHECK(class_loader.get() != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700455
Brian Carlstromdf143242011-10-10 18:05:34 -0700456 Thread::Current()->SetClassLoaderOverride(Decode<ClassLoader*>(env, class_loader.get()));
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500457
458 ScopedLocalRef<jclass> Thread_class(env, env->FindClass("java/lang/Thread"));
459 CHECK(Thread_class.get() != NULL);
460 jfieldID contextClassLoader = env->GetFieldID(Thread_class.get(),
461 "contextClassLoader",
462 "Ljava/lang/ClassLoader;");
463 CHECK(contextClassLoader != NULL);
464 ScopedLocalRef<jobject> self_jobject(env, AddLocalReference<jobject>(env, self->GetPeer()));
465 env->SetObjectField(self_jobject.get(), contextClassLoader, class_loader.get());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700466}
467
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700468void Runtime::Start() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800469 VLOG(startup) << "Runtime::Start entering";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700470
471 CHECK(host_prefix_.empty()) << host_prefix_;
472
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700473 // Restore main thread state to kNative as expected by native code
474 Thread::Current()->SetState(Thread::kNative);
475
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700476 started_ = true;
477
Brian Carlstromae826982011-11-09 01:33:42 -0800478 // InitNativeMethods needs to be after started_ so that the classes
479 // it touches will have methods linked to the oat file if necessary.
480 InitNativeMethods();
481
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500482 Thread::FinishStartup();
483
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700484 if (!is_zygote_) {
485 DidForkFromZygote();
486 }
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700487
Elliott Hughes85d15452011-09-16 17:33:01 -0700488 StartDaemonThreads();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700489
Brian Carlstromaded5f72011-10-07 17:15:04 -0700490 CreateSystemClassLoader();
491
Elliott Hughes726079d2011-10-07 18:43:44 -0700492 Thread::Current()->GetJniEnv()->locals.AssertEmpty();
493
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800494 VLOG(startup) << "Runtime::Start exiting";
Elliott Hughes85d15452011-09-16 17:33:01 -0700495}
496
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700497void Runtime::DidForkFromZygote() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700498 is_zygote_ = false;
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700499
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700500 StartSignalCatcher();
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700501
502 // Start the JDWP thread. If the command-line debugger flags specified "suspend=y",
503 // this will pause the runtime, so we probably want this to come last.
504 Dbg::StartJdwp();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700505}
506
507void Runtime::StartSignalCatcher() {
508 if (!is_zygote_) {
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700509 signal_catcher_ = new SignalCatcher(stack_trace_file_);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700510 }
511}
512
Elliott Hughes85d15452011-09-16 17:33:01 -0700513void Runtime::StartDaemonThreads() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800514 VLOG(startup) << "Runtime::StartDaemonThreads entering";
Elliott Hughes85d15452011-09-16 17:33:01 -0700515
Elliott Hughes719b3232011-09-25 17:42:19 -0700516 Thread* self = Thread::Current();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700517
518 // Must be in the kNative state for calling native methods.
519 CHECK_EQ(self->GetState(), Thread::kNative);
520
Elliott Hughes719b3232011-09-25 17:42:19 -0700521 JNIEnv* env = self->GetJniEnv();
Elliott Hughes726079d2011-10-07 18:43:44 -0700522 ScopedLocalRef<jclass> c(env, env->FindClass("java/lang/Daemons"));
523 CHECK(c.get() != NULL);
524 jmethodID mid = env->GetStaticMethodID(c.get(), "start", "()V");
Elliott Hughes719b3232011-09-25 17:42:19 -0700525 CHECK(mid != NULL);
Elliott Hughes726079d2011-10-07 18:43:44 -0700526 env->CallStaticVoidMethod(c.get(), mid);
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700527
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800528 VLOG(startup) << "Runtime::StartDaemonThreads exiting";
Carl Shapiro61e019d2011-07-14 16:53:09 -0700529}
530
Elliott Hughes6b355752012-01-13 16:49:08 -0800531bool Runtime::IsShuttingDown() const {
532 return shutting_down_;
533}
534
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700535bool Runtime::IsStarted() const {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700536 return started_;
537}
538
Elliott Hughes0af55432011-08-17 18:37:28 -0700539bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700540 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700541
Elliott Hughes90a33692011-08-30 13:27:07 -0700542 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
543 if (options.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700544 LOG(ERROR) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700545 return false;
546 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800547 VLOG(startup) << "Runtime::Init -verbose:startup enabled";
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700548
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700549 SetJniGlobalsMax(options->jni_globals_max_);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800550 Monitor::Init(options->lock_profiling_threshold_, options->hook_is_sensitive_thread_);
Elliott Hughes32d6e1e2011-10-11 14:47:44 -0700551
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700552 host_prefix_ = options->host_prefix_;
553 boot_class_path_ = options->boot_class_path_;
554 class_path_ = options->class_path_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700555 properties_ = options->properties_;
556
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700557 is_zygote_ = options->is_zygote_;
558
Elliott Hughes0af55432011-08-17 18:37:28 -0700559 vfprintf_ = options->hook_vfprintf_;
560 exit_ = options->hook_exit_;
561 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700562
Elliott Hughesbe759c62011-09-08 19:38:21 -0700563 default_stack_size_ = options->stack_size_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700564 stack_trace_file_ = options->stack_trace_file_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700565
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700566 monitor_list_ = new MonitorList;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800567 thread_list_ = new ThreadList;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700568 intern_table_ = new InternTable;
569
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800570 Heap::Init(options->heap_initial_size_,
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700571 options->heap_maximum_size_,
jeffhaoc1160702011-10-27 15:48:45 -0700572 options->heap_growth_limit_,
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700573 options->images_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700574
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700575 BlockSignals();
576
Elliott Hughesa0957642011-09-02 14:27:33 -0700577 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700578
Elliott Hughesbe759c62011-09-08 19:38:21 -0700579 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700580
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700581 // ClassLinker needs an attached thread, but we can't fully attach a thread
582 // without creating objects.
583 Thread::Attach(this, "main", false);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700584
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700585 // Set us to runnable so tools using a runtime can allocate and GC by default
586 Thread::Current()->SetState(Thread::kRunnable);
587
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700588 CHECK_GE(Heap::GetSpaces().size(), 1U);
589 class_linker_ = ((Heap::GetSpaces()[0]->IsImageSpace())
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800590 ? ClassLinker::Create(intern_table_)
591 : ClassLinker::Create(options->boot_class_path_, intern_table_));
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700592
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800593 VLOG(startup) << "Runtime::Init exiting";
Carl Shapiro1fb86202011-06-27 17:43:13 -0700594 return true;
595}
596
Elliott Hughes038a8062011-09-18 14:12:41 -0700597void Runtime::InitNativeMethods() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800598 VLOG(startup) << "Runtime::InitNativeMethods entering";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700599 Thread* self = Thread::Current();
600 JNIEnv* env = self->GetJniEnv();
601
Elliott Hughes418d20f2011-09-22 14:00:39 -0700602 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Brian Carlstromaded5f72011-10-07 17:15:04 -0700603 CHECK_EQ(self->GetState(), Thread::kNative);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700604
Elliott Hughes418d20f2011-09-22 14:00:39 -0700605 // First set up JniConstants, which is used by both the runtime's built-in native
606 // methods and libcore.
Elliott Hughesfea966e2011-09-22 10:26:20 -0700607 JniConstants::init(env);
608
Elliott Hughes418d20f2011-09-22 14:00:39 -0700609 // Then set up the native methods provided by the runtime itself.
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700610 RegisterRuntimeNativeMethods(env);
611
Elliott Hughes418d20f2011-09-22 14:00:39 -0700612 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
613 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
614 // the library that implements System.loadLibrary!
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700615 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800616 VLOG(startup) << "Runtime::InitNativeMethods exiting";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700617}
618
619void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
620#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700621 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700622 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700623 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700624 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700625 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700626 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700627 REGISTER(register_java_lang_Object);
628 REGISTER(register_java_lang_Runtime);
629 REGISTER(register_java_lang_String);
630 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700631 REGISTER(register_java_lang_Thread);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700632 REGISTER(register_java_lang_Throwable);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700633 REGISTER(register_java_lang_VMClassLoader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700634 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700635 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700636 REGISTER(register_java_lang_reflect_Field);
637 REGISTER(register_java_lang_reflect_Method);
Jesse Wilson95caa792011-10-12 18:14:17 -0400638 REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700639 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700640 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700641 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700642 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700643#undef REGISTER
644}
645
Elliott Hughes8daa0922011-09-11 13:46:25 -0700646void Runtime::Dump(std::ostream& os) {
Elliott Hughese27955c2011-08-26 15:21:24 -0700647 // TODO: dump other runtime statistics?
Elliott Hughescac6cc72011-11-03 20:31:21 -0700648 GetClassLinker()->DumpForSigQuit(os);
649 GetInternTable()->DumpForSigQuit(os);
Elliott Hughes42ee1422011-09-06 12:33:32 -0700650 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700651
652 thread_list_->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700653}
654
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800655void Runtime::DumpLockHolders(std::ostream& os) {
656 pid_t heap_lock_owner = Heap::GetLockOwner();
657 pid_t thread_list_lock_owner = GetThreadList()->GetLockOwner();
658 pid_t classes_lock_owner = GetClassLinker()->GetClassesLockOwner();
659 pid_t dex_lock_owner = GetClassLinker()->GetDexLockOwner();
660 if ((heap_lock_owner | thread_list_lock_owner | classes_lock_owner | dex_lock_owner) != 0) {
661 os << "Heap lock owner tid: " << heap_lock_owner << "\n"
662 << "ThreadList lock owner tid: " << thread_list_lock_owner << "\n"
663 << "ClassLinker classes lock owner tid: " << classes_lock_owner << "\n"
664 << "ClassLinker dex lock owner tid: " << dex_lock_owner << "\n";
665 }
666}
667
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700668void Runtime::SetStatsEnabled(bool new_state) {
669 if (new_state == true) {
670 GetStats()->Clear(~0);
671 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
672 Thread::Current()->GetStats()->Clear(~0);
673 }
674 stats_enabled_ = new_state;
675}
676
677void Runtime::ResetStats(int kinds) {
678 GetStats()->Clear(kinds & 0xffff);
679 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
680 Thread::Current()->GetStats()->Clear(kinds >> 16);
681}
682
683RuntimeStats* Runtime::GetStats() {
684 return &stats_;
685}
686
687int32_t Runtime::GetStat(int kind) {
688 RuntimeStats* stats;
689 if (kind < (1<<16)) {
690 stats = GetStats();
691 } else {
692 stats = Thread::Current()->GetStats();
693 kind >>= 16;
694 }
695 switch (kind) {
696 case KIND_ALLOCATED_OBJECTS:
697 return stats->allocated_objects;
698 case KIND_ALLOCATED_BYTES:
699 return stats->allocated_bytes;
700 case KIND_FREED_OBJECTS:
701 return stats->freed_objects;
702 case KIND_FREED_BYTES:
703 return stats->freed_bytes;
704 case KIND_GC_INVOCATIONS:
705 return stats->gc_for_alloc_count;
706 case KIND_CLASS_INIT_COUNT:
707 return stats->class_init_count;
708 case KIND_CLASS_INIT_TIME:
709 // Convert ns to us, reduce to 32 bits.
710 return (int) (stats->class_init_time_ns / 1000);
711 case KIND_EXT_ALLOCATED_OBJECTS:
712 case KIND_EXT_ALLOCATED_BYTES:
713 case KIND_EXT_FREED_OBJECTS:
714 case KIND_EXT_FREED_BYTES:
715 return 0; // backward compatibility
716 default:
717 CHECK(false);
718 return -1; // unreachable
719 }
720}
721
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700722void Runtime::BlockSignals() {
723 sigset_t sigset;
724 if (sigemptyset(&sigset) == -1) {
725 PLOG(FATAL) << "sigemptyset failed";
726 }
727 if (sigaddset(&sigset, SIGPIPE) == -1) {
728 PLOG(ERROR) << "sigaddset SIGPIPE failed";
729 }
730 // SIGQUIT is used to dump the runtime's state (including stack traces).
731 if (sigaddset(&sigset, SIGQUIT) == -1) {
732 PLOG(ERROR) << "sigaddset SIGQUIT failed";
733 }
734 // SIGUSR1 is used to initiate a heap dump.
735 if (sigaddset(&sigset, SIGUSR1) == -1) {
736 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
737 }
738 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
739}
740
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700741void Runtime::AttachCurrentThread(const char* name, bool as_daemon) {
742 Thread::Attach(instance_, name, as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700743}
744
Elliott Hughesd92bec42011-09-02 17:04:36 -0700745void Runtime::DetachCurrentThread() {
Elliott Hughes038a8062011-09-18 14:12:41 -0700746 // TODO: check we're not calling DetachCurrentThread from a call stack that
747 // includes managed frames. (It's only valid if the stack is all-native.)
Elliott Hughes02b48d12011-09-07 17:15:51 -0700748 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700749}
750
Brian Carlstrome24fa612011-09-29 00:53:55 -0700751void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700752 Dbg::VisitRoots(visitor, arg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700753 class_linker_->VisitRoots(visitor, arg);
754 intern_table_->VisitRoots(visitor, arg);
755 java_vm_->VisitRoots(visitor, arg);
756 thread_list_->VisitRoots(visitor, arg);
757 visitor(jni_stub_array_, arg);
758 visitor(abstract_method_error_stub_array_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700759 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
760 visitor(resolution_stub_array_[i], arg);
761 }
762 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
763 visitor(callee_save_method_[i], arg);
764 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700765}
766
Ian Rogers169c9a72011-11-13 20:13:17 -0800767bool Runtime::HasJniDlsymLookupStub() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700768 return jni_stub_array_ != NULL;
769}
770
Ian Rogers169c9a72011-11-13 20:13:17 -0800771ByteArray* Runtime::GetJniDlsymLookupStub() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700772 CHECK(jni_stub_array_ != NULL);
773 return jni_stub_array_;
774}
775
Ian Rogers169c9a72011-11-13 20:13:17 -0800776void Runtime::SetJniDlsymLookupStub(ByteArray* jni_stub_array) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700777 CHECK(jni_stub_array != NULL) << " jni_stub_array=" << jni_stub_array;
778 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array)
779 << "jni_stub_array_=" << jni_stub_array_ << " jni_stub_array=" << jni_stub_array;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700780 jni_stub_array_ = jni_stub_array;
781}
782
783bool Runtime::HasAbstractMethodErrorStubArray() const {
784 return abstract_method_error_stub_array_ != NULL;
785}
786
787ByteArray* Runtime::GetAbstractMethodErrorStubArray() const {
788 CHECK(abstract_method_error_stub_array_ != NULL);
789 return abstract_method_error_stub_array_;
790}
791
792void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) {
793 CHECK(abstract_method_error_stub_array != NULL);
794 CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array);
795 abstract_method_error_stub_array_ = abstract_method_error_stub_array;
796}
797
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700798
799Runtime::TrampolineType Runtime::GetTrampolineType(Method* method) {
800 if (method == NULL) {
801 return Runtime::kUnknownMethod;
802 } else if (method->IsStatic()) {
803 return Runtime::kStaticMethod;
804 } else {
805 return Runtime::kInstanceMethod;
806 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700807}
808
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700809bool Runtime::HasResolutionStubArray(TrampolineType type) const {
810 return resolution_stub_array_[type] != NULL;
Ian Rogersad25ac52011-10-04 19:13:33 -0700811}
812
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700813ByteArray* Runtime::GetResolutionStubArray(TrampolineType type) const {
814 CHECK(HasResolutionStubArray(type));
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700815 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastTrampolineMethodType));
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700816 return resolution_stub_array_[type];
817}
818
819void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700820 CHECK(resolution_stub_array != NULL);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700821 CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array);
822 resolution_stub_array_[type] = resolution_stub_array;
Ian Rogersad25ac52011-10-04 19:13:33 -0700823}
824
Brian Carlstromae826982011-11-09 01:33:42 -0800825Method* Runtime::CreateCalleeSaveMethod(InstructionSet instruction_set, CalleeSaveType type) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700826 Class* method_class = Method::GetMethodClass();
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700827 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
Ian Rogersff1ed472011-09-20 13:46:24 -0700828 method->SetDeclaringClass(method_class);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800829 // TODO: use a special method for callee saves
830 method->SetMethodIndex(DexFile::kDexNoIndex16);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700831 method->SetCode(NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800832 if ((instruction_set == kThumb2) || (instruction_set == kArm)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700833 uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6) | (1 << art::arm::R7) |
834 (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11);
835 uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3);
836 uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9);
837 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills :0) |
838 (type == kSaveAll ? all_spills :0) | (1 << art::arm::LR);
839 uint32_t fp_all_spills = (1 << art::arm::S0) | (1 << art::arm::S1) | (1 << art::arm::S2) |
840 (1 << art::arm::S3) | (1 << art::arm::S4) | (1 << art::arm::S5) |
841 (1 << art::arm::S6) | (1 << art::arm::S7) | (1 << art::arm::S8) |
842 (1 << art::arm::S9) | (1 << art::arm::S10) | (1 << art::arm::S11) |
843 (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) |
844 (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) |
845 (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) |
846 (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) |
847 (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) |
848 (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
849 (1 << art::arm::S30) | (1 << art::arm::S31);
850 uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
851 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
852 __builtin_popcount(fp_spills) /* fprs */ +
853 1 /* Method* */) * kPointerSize, kStackAlignment);
Ian Rogers15fdb8c2011-09-25 15:45:07 -0700854 method->SetFrameSizeInBytes(frame_size);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700855 method->SetCoreSpillMask(core_spills);
856 method->SetFpSpillMask(fp_spills);
Brian Carlstromae826982011-11-09 01:33:42 -0800857 } else if (instruction_set == kX86) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700858 method->SetFrameSizeInBytes(32);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700859 method->SetCoreSpillMask((1 << art::x86::EBX) | (1 << art::x86::EBP) | (1 << art::x86::ESI) |
Ian Rogersff1ed472011-09-20 13:46:24 -0700860 (1 << art::x86::EDI));
861 method->SetFpSpillMask(0);
862 } else {
863 UNIMPLEMENTED(FATAL);
864 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700865 return method.get();
Ian Rogersff1ed472011-09-20 13:46:24 -0700866}
867
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700868bool Runtime::HasCalleeSaveMethod(CalleeSaveType type) const {
869 return callee_save_method_[type] != NULL;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700870}
871
Brian Carlstrome24fa612011-09-29 00:53:55 -0700872// Returns a special method that describes all callee saves being spilled to the stack.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700873Method* Runtime::GetCalleeSaveMethod(CalleeSaveType type) const {
874 CHECK(HasCalleeSaveMethod(type));
875 return callee_save_method_[type];
Brian Carlstrome24fa612011-09-29 00:53:55 -0700876}
877
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700878void Runtime::SetCalleeSaveMethod(Method* method, CalleeSaveType type) {
879 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
880 callee_save_method_[type] = method;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700881}
882
jeffhao2692b572011-12-16 15:42:28 -0800883void Runtime::EnableMethodTracing(Trace* tracer) {
884 CHECK(!IsMethodTracingActive());
885 tracer_ = tracer;
886}
887
888void Runtime::DisableMethodTracing() {
889 CHECK(IsMethodTracingActive());
890 delete tracer_;
891 tracer_ = NULL;
892}
893
894bool Runtime::IsMethodTracingActive() const {
895 return (tracer_ != NULL);
896}
897
898Trace* Runtime::GetTracer() const {
899 CHECK(IsMethodTracingActive());
900 return tracer_;
901}
902
Carl Shapiro1fb86202011-06-27 17:43:13 -0700903} // namespace art