blob: acb42e9392a85127ca10603a735e60aff3354ada [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
Elliott Hughes90a33692011-08-30 13:27:07 -070010#include "UniquePtr.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070011#include "class_linker.h"
12#include "heap.h"
Elliott Hughescf4c6c42011-09-01 15:16:42 -070013#include "intern_table.h"
Elliott Hughesc5f7c912011-08-18 14:00:42 -070014#include "jni_internal.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070015#include "signal_catcher.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070016#include "thread.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070017#include "thread_list.h"
Carl Shapiro61e019d2011-07-14 16:53:09 -070018
Elliott Hughes90a33692011-08-30 13:27:07 -070019// TODO: this drags in cutil/log.h, which conflicts with our logging.h.
20#include "JniConstants.h"
21
Carl Shapiro1fb86202011-06-27 17:43:13 -070022namespace art {
23
Carl Shapiro2ed144c2011-07-26 16:52:08 -070024Runtime* Runtime::instance_ = NULL;
25
Elliott Hughesdcc24742011-09-07 14:02:44 -070026Runtime::Runtime()
Elliott Hughesbe759c62011-09-08 19:38:21 -070027 : default_stack_size_(Thread::kDefaultStackSize),
Elliott Hughesdcc24742011-09-07 14:02:44 -070028 thread_list_(NULL),
29 intern_table_(NULL),
30 class_linker_(NULL),
31 signal_catcher_(NULL),
32 java_vm_(NULL),
Brian Carlstrom16192862011-09-12 17:50:06 -070033 jni_stub_array_(NULL),
Ian Rogersff1ed472011-09-20 13:46:24 -070034 callee_save_method_(NULL),
Elliott Hughesdcc24742011-09-07 14:02:44 -070035 started_(false),
36 vfprintf_(NULL),
37 exit_(NULL),
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070038 abort_(NULL),
39 stats_enabled_(false) {
Elliott Hughesdcc24742011-09-07 14:02:44 -070040}
41
Carl Shapiro61e019d2011-07-14 16:53:09 -070042Runtime::~Runtime() {
Elliott Hughes5fe594f2011-09-08 12:33:17 -070043 // Make sure our internal threads are dead before we start tearing down things they're using.
44 delete signal_catcher_;
Elliott Hughes038a8062011-09-18 14:12:41 -070045 // TODO: GC thread.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070046
Elliott Hughes038a8062011-09-18 14:12:41 -070047 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
Elliott Hughes93e74e82011-09-13 11:07:03 -070048 delete thread_list_;
49
Carl Shapiro61e019d2011-07-14 16:53:09 -070050 delete class_linker_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070051 Heap::Destroy();
Elliott Hughescf4c6c42011-09-01 15:16:42 -070052 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070053 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -070054 Thread::Shutdown();
Carl Shapiro4acf4642011-07-26 18:54:13 -070055 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070056 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -070057 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -070058}
59
Elliott Hughesffe67362011-07-17 12:09:27 -070060void Runtime::Abort(const char* file, int line) {
61 // Get any pending output out of the way.
62 fflush(NULL);
63
64 // Many people have difficulty distinguish aborts from crashes,
65 // so be explicit.
66 LogMessage(file, line, ERROR, -1).stream() << "Runtime aborting...";
67
Elliott Hughesffe67362011-07-17 12:09:27 -070068 // Perform any platform-specific pre-abort actions.
69 PlatformAbort(file, line);
70
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070071 // use abort hook if we have one
72 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
73 Runtime::Current()->abort_();
74 // notreached
75 }
76
Elliott Hughesffe67362011-07-17 12:09:27 -070077 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -070078 // receive SIGABRT. debuggerd dumps the stack trace of the main
79 // thread, whether or not that was the thread that failed. By
80 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -070081 // fault in the current thread, and get a useful log from debuggerd.
82 // We can also trivially tell the difference between a VM crash and
83 // a deliberate abort by looking at the fault address.
84 *reinterpret_cast<char*>(0xdeadd00d) = 38;
85 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -070086 // notreached
87}
88
Elliott Hughesbf86d042011-08-31 17:53:14 -070089void Runtime::CallExitHook(jint status) {
90 if (exit_ != NULL) {
91 ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative);
92 exit_(status);
93 LOG(WARNING) << "Exit hook returned instead of exiting!";
94 }
95}
96
Brian Carlstrom8a436592011-08-15 21:27:23 -070097// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
98// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
99// [gG] gigabytes.
100//
101// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700102// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
103// of 1024.
104//
105// The spec says the -Xmx and -Xms options must be multiples of 1024. It
106// doesn't say anything about -Xss.
107//
108// Returns 0 (a useless size) if "s" is malformed or specifies a low or
109// non-evenly-divisible value.
110//
111size_t ParseMemoryOption(const char *s, size_t div) {
112 // strtoul accepts a leading [+-], which we don't want,
113 // so make sure our string starts with a decimal digit.
114 if (isdigit(*s)) {
115 const char *s2;
116 size_t val = strtoul(s, (char **)&s2, 10);
117 if (s2 != s) {
118 // s2 should be pointing just after the number.
119 // If this is the end of the string, the user
120 // has specified a number of bytes. Otherwise,
121 // there should be exactly one more character
122 // that specifies a multiplier.
123 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700124 // The remainder of the string is either a single multiplier
125 // character, or nothing to indicate that the value is in
126 // bytes.
127 char c = *s2++;
128 if (*s2 == '\0') {
129 size_t mul;
130 if (c == '\0') {
131 mul = 1;
132 } else if (c == 'k' || c == 'K') {
133 mul = KB;
134 } else if (c == 'm' || c == 'M') {
135 mul = MB;
136 } else if (c == 'g' || c == 'G') {
137 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700138 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700139 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700140 return 0;
141 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700142
143 if (val <= std::numeric_limits<size_t>::max() / mul) {
144 val *= mul;
145 } else {
146 // Clamp to a multiple of 1024.
147 val = std::numeric_limits<size_t>::max() & ~(1024-1);
148 }
149 } else {
150 // There's more than one character after the numeric part.
151 return 0;
152 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700153 }
154 // The man page says that a -Xm value must be a multiple of 1024.
155 if (val % div == 0) {
156 return val;
157 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700158 }
159 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700160 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700161}
162
Elliott Hughes0af55432011-08-17 18:37:28 -0700163void LoadJniLibrary(JavaVMExt* vm, const char* name) {
164 // TODO: OS_SHARED_LIB_FORMAT_STR
165 std::string mapped_name(StringPrintf("lib%s.so", name));
Elliott Hughes75770752011-08-24 17:52:38 -0700166 std::string reason;
167 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700168 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
169 << reason;
170 }
171}
172
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700173void CreateClassPath(const std::string& class_path,
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700174 std::vector<const DexFile*>& class_path_vector) {
Carl Shapirofc322c72011-07-27 00:20:01 -0700175 std::vector<std::string> parsed;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700176 Split(class_path, ':', parsed);
Carl Shapirofc322c72011-07-27 00:20:01 -0700177 for (size_t i = 0; i < parsed.size(); ++i) {
Brian Carlstrom16192862011-09-12 17:50:06 -0700178 const DexFile* dex_file = DexFile::Open(parsed[i], "");
Carl Shapirofc322c72011-07-27 00:20:01 -0700179 if (dex_file != NULL) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700180 class_path_vector.push_back(dex_file);
Carl Shapirofc322c72011-07-27 00:20:01 -0700181 }
182 }
183}
184
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700185Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700186 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700187 parsed->boot_image_ = NULL;
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700188#ifdef NDEBUG
Elliott Hughes5174fe62011-08-23 15:12:35 -0700189 // -Xcheck:jni is off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700190 parsed->check_jni_ = false;
191#else
192 // ...but on by default in debug builds.
193 parsed->check_jni_ = true;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700194#endif
Elliott Hughes85d15452011-09-16 17:33:01 -0700195
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700196 parsed->heap_initial_size_ = Heap::kInitialSize;
197 parsed->heap_maximum_size_ = Heap::kMaximumSize;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700198 parsed->stack_size_ = Thread::kDefaultStackSize;
199
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700200 parsed->hook_vfprintf_ = vfprintf;
201 parsed->hook_exit_ = exit;
202 parsed->hook_abort_ = abort;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700203
204 for (size_t i = 0; i < options.size(); ++i) {
205 const StringPiece& option = options[i].first;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700206 if (option.starts_with("-Xbootclasspath:")) {
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700207 parsed->boot_class_path_string_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700208 } else if (option == "bootclasspath") {
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700209 UNIMPLEMENTED(WARNING) << "what should VMRuntime.getBootClassPath return here?";
Brian Carlstromf734cf52011-08-17 16:28:14 -0700210 const void* dex_vector = options[i].second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700211 const std::vector<const DexFile*>* v
212 = reinterpret_cast<const std::vector<const DexFile*>*>(dex_vector);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700213 if (v == NULL) {
214 if (ignore_unrecognized) {
215 continue;
216 }
217 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700218 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700219 return NULL;
220 }
221 parsed->boot_class_path_ = *v;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700222 } else if (option == "classpath") {
223 const void* dex_vector = options[i].second;
224 const std::vector<const DexFile*>* v
225 = reinterpret_cast<const std::vector<const DexFile*>*>(dex_vector);
226 if (v == NULL) {
227 if (ignore_unrecognized) {
228 continue;
229 }
230 // TODO: usage
231 LOG(FATAL) << "Failed to parse " << option;
232 return NULL;
233 }
234 parsed->class_path_ = *v;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700235 } else if (option == "-classpath" || option == "-cp") {
236 // TODO: support -Djava.class.path
237 i++;
238 if (i == options.size()) {
239 // TODO: usage
240 LOG(FATAL) << "Missing required class path value for " << option;
241 return NULL;
242 }
243 const StringPiece& value = options[i].first;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700244 parsed->class_path_string_ = value.data();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700245 } else if (option.starts_with("-Xbootimage:")) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700246 // TODO: remove when intern_addr_ is removed, just use -Ximage:
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700247 parsed->boot_image_ = option.substr(strlen("-Xbootimage:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700248 } else if (option.starts_with("-Ximage:")) {
249 parsed->images_.push_back(option.substr(strlen("-Ximage:")).data());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700250 } else if (option.starts_with("-Xcheck:jni")) {
251 parsed->check_jni_ = true;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700252 } else if (option.starts_with("-Xms")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700253 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024);
254 if (size == 0) {
255 if (ignore_unrecognized) {
256 continue;
257 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700258 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700259 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700260 return NULL;
261 }
262 parsed->heap_initial_size_ = size;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700263 } else if (option.starts_with("-Xmx")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700264 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024);
265 if (size == 0) {
266 if (ignore_unrecognized) {
267 continue;
268 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700269 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700270 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700271 return NULL;
272 }
273 parsed->heap_maximum_size_ = size;
274 } else if (option.starts_with("-Xss")) {
275 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1);
276 if (size == 0) {
277 if (ignore_unrecognized) {
278 continue;
279 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700280 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700281 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700282 return NULL;
283 }
284 parsed->stack_size_ = size;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700285 } else if (option.starts_with("-D")) {
286 parsed->properties_.push_back(option.substr(strlen("-D")).data());
Elliott Hughesa0957642011-09-02 14:27:33 -0700287 } else if (option.starts_with("-Xjnitrace:")) {
288 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:")).data();
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700289 } else if (option.starts_with("-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700290 std::vector<std::string> verbose_options;
Elliott Hughes34023802011-08-30 12:06:17 -0700291 Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700292 for (size_t i = 0; i < verbose_options.size(); ++i) {
293 parsed->verbose_.insert(verbose_options[i]);
294 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700295 } else if (option == "vfprintf") {
296 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
297 } else if (option == "exit") {
298 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
299 } else if (option == "abort") {
300 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700301 } else {
302 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700303 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700304 LOG(ERROR) << "Unrecognized option " << option;
305 // TODO: this should exit, but for now tolerate unknown options
306 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700307 }
308 }
309 }
310
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700311 // Consider it an error if both bootclasspath and -Xbootclasspath: are supplied.
Brian Carlstrom78128a62011-09-15 17:21:19 -0700312 // TODO: remove bootclasspath and classpath which are mostly just used by tests?
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700313 if (!parsed->boot_class_path_.empty() && !parsed->boot_class_path_string_.empty()) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700314 // TODO: usage
315 LOG(FATAL) << "bootclasspath and -Xbootclasspath: are mutually exclusive options.";
316 return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700317 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700318 if (!parsed->class_path_.empty() && !parsed->class_path_string_.empty()) {
319 // TODO: usage
320 LOG(FATAL) << "bootclasspath and -Xbootclasspath: are mutually exclusive options.";
321 return NULL;
322 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700323 if (parsed->boot_class_path_.empty()) {
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700324 if (parsed->boot_class_path_string_ == NULL) {
325 const char* BOOTCLASSPATH = getenv("BOOTCLASSPATH");
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700326 if (BOOTCLASSPATH != NULL) {
327 parsed->boot_class_path_string_ = BOOTCLASSPATH;
328 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700329 }
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700330 CreateClassPath(parsed->boot_class_path_string_, parsed->boot_class_path_);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700331 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700332
Brian Carlstrom78128a62011-09-15 17:21:19 -0700333 if (parsed->class_path_.empty()) {
334 if (parsed->class_path_string_ == NULL) {
335 const char* CLASSPATH = getenv("CLASSPATH");
336 if (CLASSPATH != NULL) {
337 parsed->class_path_string_ = CLASSPATH;
338 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700339 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700340 CreateClassPath(parsed->class_path_string_, parsed->class_path_);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700341 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700342
Elliott Hughes85d15452011-09-16 17:33:01 -0700343 LOG(INFO) << "CheckJNI is " << (parsed->check_jni_ ? "on" : "off");
344
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700345 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700346}
347
348Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700349 // TODO: acquire a static mutex on Runtime to avoid racing.
350 if (Runtime::instance_ != NULL) {
351 return NULL;
352 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700353 instance_ = new Runtime;
354 if (!instance_->Init(options, ignore_unrecognized)) {
355 delete instance_;
356 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700357 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700358 return instance_;
359}
Elliott Hughes0af55432011-08-17 18:37:28 -0700360
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700361void Runtime::Start() {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700362 started_ = true;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700363
Elliott Hughes038a8062011-09-18 14:12:41 -0700364 InitNativeMethods();
Elliott Hughes85d15452011-09-16 17:33:01 -0700365
Elliott Hughes038a8062011-09-18 14:12:41 -0700366 Thread::FinishStartup();
Elliott Hughes85d15452011-09-16 17:33:01 -0700367
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700368 class_linker_->RunRootClinits();
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700369
Elliott Hughes85d15452011-09-16 17:33:01 -0700370 StartDaemonThreads();
371}
372
373void Runtime::StartDaemonThreads() {
374 signal_catcher_ = new SignalCatcher;
375
Elliott Hughes719b3232011-09-25 17:42:19 -0700376 Thread* self = Thread::Current();
377 JNIEnv* env = self->GetJniEnv();
378 jclass c = env->FindClass("java/lang/Daemons");
Elliott Hughes85d15452011-09-16 17:33:01 -0700379 CHECK(c != NULL);
Elliott Hughes719b3232011-09-25 17:42:19 -0700380 jmethodID mid = env->GetStaticMethodID(c, "start", "()V");
381 CHECK(mid != NULL);
382 env->CallStaticVoidMethod(c, mid);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700383}
384
Elliott Hughesdcc24742011-09-07 14:02:44 -0700385bool Runtime::IsStarted() {
386 return started_;
387}
388
Elliott Hughes0af55432011-08-17 18:37:28 -0700389bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700390 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700391
Elliott Hughes90a33692011-08-30 13:27:07 -0700392 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
393 if (options.get() == NULL) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700394 LOG(WARNING) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700395 return false;
396 }
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700397
398 boot_class_path_ = options->boot_class_path_string_;
399 class_path_ = options->class_path_string_;
400 properties_ = options->properties_;
401
Elliott Hughes0af55432011-08-17 18:37:28 -0700402 vfprintf_ = options->hook_vfprintf_;
403 exit_ = options->hook_exit_;
404 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700405
Elliott Hughesbe759c62011-09-08 19:38:21 -0700406 default_stack_size_ = options->stack_size_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700407
Elliott Hughes14357e82011-09-26 10:42:15 -0700408 thread_list_ = new ThreadList(options->IsVerbose("thread"));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700409 intern_table_ = new InternTable;
410
Elliott Hughesbe759c62011-09-08 19:38:21 -0700411 Heap::Init(options->heap_initial_size_, options->heap_maximum_size_,
412 options->boot_image_, options->images_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700413
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700414 BlockSignals();
415
Elliott Hughesa0957642011-09-02 14:27:33 -0700416 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700417
Elliott Hughesbe759c62011-09-08 19:38:21 -0700418 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700419
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700420 // ClassLinker needs an attached thread, but we can't fully attach a thread
421 // without creating objects.
422 Thread::Attach(this, "main", false);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700423
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700424 class_linker_ = ClassLinker::Create(options->boot_class_path_,
425 options->class_path_,
426 intern_table_,
427 Heap::GetBootSpace());
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700428
Carl Shapiro1fb86202011-06-27 17:43:13 -0700429 return true;
430}
431
Elliott Hughes038a8062011-09-18 14:12:41 -0700432void Runtime::InitNativeMethods() {
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700433 Thread* self = Thread::Current();
434 JNIEnv* env = self->GetJniEnv();
435
Elliott Hughes418d20f2011-09-22 14:00:39 -0700436 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700437 ScopedThreadStateChange tsc(self, Thread::kNative);
438
Elliott Hughes418d20f2011-09-22 14:00:39 -0700439 // First set up JniConstants, which is used by both the runtime's built-in native
440 // methods and libcore.
Elliott Hughesfea966e2011-09-22 10:26:20 -0700441 JniConstants::init(env);
442
Elliott Hughes418d20f2011-09-22 14:00:39 -0700443 // Then set up the native methods provided by the runtime itself.
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700444 RegisterRuntimeNativeMethods(env);
445
Elliott Hughes418d20f2011-09-22 14:00:39 -0700446 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
447 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
448 // the library that implements System.loadLibrary!
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700449 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
450}
451
452void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
453#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700454 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700455 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700456 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700457 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700458 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700459 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700460 REGISTER(register_java_lang_Object);
461 REGISTER(register_java_lang_Runtime);
462 REGISTER(register_java_lang_String);
463 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700464 REGISTER(register_java_lang_Thread);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700465 REGISTER(register_java_lang_Throwable);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700466 REGISTER(register_java_lang_VMClassLoader);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700467 //REGISTER(register_java_lang_reflect_AccessibleObject);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700468 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700469 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700470 REGISTER(register_java_lang_reflect_Field);
471 REGISTER(register_java_lang_reflect_Method);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700472 //REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700473 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700474 //REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
475 //REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700476 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700477#undef REGISTER
478}
479
Elliott Hughes8daa0922011-09-11 13:46:25 -0700480void Runtime::Dump(std::ostream& os) {
Elliott Hughese27955c2011-08-26 15:21:24 -0700481 // TODO: dump other runtime statistics?
482 os << "Loaded classes: " << class_linker_->NumLoadedClasses() << "\n";
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700483 os << "Intern table size: " << GetInternTable()->Size() << "\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700484 // LOGV("VM stats: meth=%d ifld=%d sfld=%d linear=%d",
485 // gDvm.numDeclaredMethods,
486 // gDvm.numDeclaredInstFields,
487 // gDvm.numDeclaredStaticFields,
488 // gDvm.pBootLoaderAlloc->curOffset);
489 // LOGI("GC precise methods: %d", dvmPointerSetGetCount(gDvm.preciseMethods));
Elliott Hughes42ee1422011-09-06 12:33:32 -0700490 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700491
492 thread_list_->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700493}
494
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700495void Runtime::SetStatsEnabled(bool new_state) {
496 if (new_state == true) {
497 GetStats()->Clear(~0);
498 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
499 Thread::Current()->GetStats()->Clear(~0);
500 }
501 stats_enabled_ = new_state;
502}
503
504void Runtime::ResetStats(int kinds) {
505 GetStats()->Clear(kinds & 0xffff);
506 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
507 Thread::Current()->GetStats()->Clear(kinds >> 16);
508}
509
510RuntimeStats* Runtime::GetStats() {
511 return &stats_;
512}
513
514int32_t Runtime::GetStat(int kind) {
515 RuntimeStats* stats;
516 if (kind < (1<<16)) {
517 stats = GetStats();
518 } else {
519 stats = Thread::Current()->GetStats();
520 kind >>= 16;
521 }
522 switch (kind) {
523 case KIND_ALLOCATED_OBJECTS:
524 return stats->allocated_objects;
525 case KIND_ALLOCATED_BYTES:
526 return stats->allocated_bytes;
527 case KIND_FREED_OBJECTS:
528 return stats->freed_objects;
529 case KIND_FREED_BYTES:
530 return stats->freed_bytes;
531 case KIND_GC_INVOCATIONS:
532 return stats->gc_for_alloc_count;
533 case KIND_CLASS_INIT_COUNT:
534 return stats->class_init_count;
535 case KIND_CLASS_INIT_TIME:
536 // Convert ns to us, reduce to 32 bits.
537 return (int) (stats->class_init_time_ns / 1000);
538 case KIND_EXT_ALLOCATED_OBJECTS:
539 case KIND_EXT_ALLOCATED_BYTES:
540 case KIND_EXT_FREED_OBJECTS:
541 case KIND_EXT_FREED_BYTES:
542 return 0; // backward compatibility
543 default:
544 CHECK(false);
545 return -1; // unreachable
546 }
547}
548
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700549void Runtime::BlockSignals() {
550 sigset_t sigset;
551 if (sigemptyset(&sigset) == -1) {
552 PLOG(FATAL) << "sigemptyset failed";
553 }
554 if (sigaddset(&sigset, SIGPIPE) == -1) {
555 PLOG(ERROR) << "sigaddset SIGPIPE failed";
556 }
557 // SIGQUIT is used to dump the runtime's state (including stack traces).
558 if (sigaddset(&sigset, SIGQUIT) == -1) {
559 PLOG(ERROR) << "sigaddset SIGQUIT failed";
560 }
561 // SIGUSR1 is used to initiate a heap dump.
562 if (sigaddset(&sigset, SIGUSR1) == -1) {
563 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
564 }
565 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
566}
567
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700568void Runtime::AttachCurrentThread(const char* name, bool as_daemon) {
569 Thread::Attach(instance_, name, as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700570}
571
Elliott Hughesd92bec42011-09-02 17:04:36 -0700572void Runtime::DetachCurrentThread() {
Elliott Hughes038a8062011-09-18 14:12:41 -0700573 // TODO: check we're not calling DetachCurrentThread from a call stack that
574 // includes managed frames. (It's only valid if the stack is all-native.)
Elliott Hughes02b48d12011-09-07 17:15:51 -0700575 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700576}
577
Ian Rogersff1ed472011-09-20 13:46:24 -0700578Method* Runtime::CreateCalleeSaveMethod(InstructionSet insns) {
579 Class* method_class = Method::GetMethodClass();
580 Method* method = down_cast<Method*>(method_class->AllocObject());
581 method->SetDeclaringClass(method_class);
582 method->SetName(intern_table_->InternStrong("$$$callee_save_method$$$"));
583 method->SetSignature(intern_table_->InternStrong("()V"));
584 method->SetCode(NULL, insns, NULL);
585 if ((insns == kThumb2) || (insns == kArm)) {
Ian Rogers15fdb8c2011-09-25 15:45:07 -0700586 size_t frame_size = (12 /* gprs */ + 32 /* fprs */ + 4 /* data */) * kPointerSize;
587 method->SetFrameSizeInBytes(frame_size);
588 method->SetReturnPcOffsetInBytes(frame_size - kPointerSize);
Ian Rogersff1ed472011-09-20 13:46:24 -0700589 method->SetCoreSpillMask((1 << art::arm::R1) |
590 (1 << art::arm::R2) |
591 (1 << art::arm::R3) |
592 (1 << art::arm::R4) |
593 (1 << art::arm::R5) |
594 (1 << art::arm::R6) |
595 (1 << art::arm::R7) |
596 (1 << art::arm::R8) |
597 (1 << art::arm::R9) |
598 (1 << art::arm::R10) |
599 (1 << art::arm::R11) |
600 (1 << art::arm::LR));
Ian Rogers15fdb8c2011-09-25 15:45:07 -0700601 method->SetFpSpillMask((1 << art::arm::S0) |
602 (1 << art::arm::S1) |
603 (1 << art::arm::S2) |
604 (1 << art::arm::S3) |
605 (1 << art::arm::S4) |
606 (1 << art::arm::S5) |
607 (1 << art::arm::S6) |
608 (1 << art::arm::S7) |
609 (1 << art::arm::S8) |
610 (1 << art::arm::S9) |
611 (1 << art::arm::S10) |
612 (1 << art::arm::S11) |
613 (1 << art::arm::S12) |
614 (1 << art::arm::S13) |
615 (1 << art::arm::S14) |
616 (1 << art::arm::S15) |
617 (1 << art::arm::S16) |
618 (1 << art::arm::S17) |
619 (1 << art::arm::S18) |
620 (1 << art::arm::S19) |
621 (1 << art::arm::S20) |
622 (1 << art::arm::S21) |
623 (1 << art::arm::S22) |
624 (1 << art::arm::S23) |
625 (1 << art::arm::S24) |
626 (1 << art::arm::S25) |
627 (1 << art::arm::S26) |
628 (1 << art::arm::S27) |
629 (1 << art::arm::S28) |
630 (1 << art::arm::S29) |
631 (1 << art::arm::S30) |
632 (1 << art::arm::S31));
Ian Rogersff1ed472011-09-20 13:46:24 -0700633 } else if (insns == kX86) {
634 method->SetFrameSizeInBytes(32);
635 method->SetReturnPcOffsetInBytes(28);
636 method->SetCoreSpillMask((1 << art::x86::EBX) |
637 (1 << art::x86::EBP) |
638 (1 << art::x86::ESI) |
639 (1 << art::x86::EDI));
640 method->SetFpSpillMask(0);
641 } else {
642 UNIMPLEMENTED(FATAL);
643 }
644 return method;
645}
646
Elliott Hughes410c0c82011-09-01 17:58:25 -0700647void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
648 class_linker_->VisitRoots(visitor, arg);
649 intern_table_->VisitRoots(visitor, arg);
650 java_vm_->VisitRoots(visitor, arg);
651 thread_list_->VisitRoots(visitor, arg);
Brian Carlstrom16192862011-09-12 17:50:06 -0700652 visitor(jni_stub_array_, arg);
Ian Rogersff1ed472011-09-20 13:46:24 -0700653 visitor(callee_save_method_, arg);
Elliott Hughes410c0c82011-09-01 17:58:25 -0700654
655 //(*visitor)(&gDvm.outOfMemoryObj, 0, ROOT_VM_INTERNAL, arg);
656 //(*visitor)(&gDvm.internalErrorObj, 0, ROOT_VM_INTERNAL, arg);
657 //(*visitor)(&gDvm.noClassDefFoundErrorObj, 0, ROOT_VM_INTERNAL, arg);
658 UNIMPLEMENTED(WARNING) << "some roots not marked";
Brian Carlstrom1f870082011-08-23 16:02:11 -0700659}
660
Carl Shapiro1fb86202011-06-27 17:43:13 -0700661} // namespace art