blob: de94b3da7bd9bf5ec2e340a59ef5869951457bbc [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),
Elliott Hughesdcc24742011-09-07 14:02:44 -070034 started_(false),
35 vfprintf_(NULL),
36 exit_(NULL),
37 abort_(NULL) {
38}
39
Carl Shapiro61e019d2011-07-14 16:53:09 -070040Runtime::~Runtime() {
Elliott Hughes5fe594f2011-09-08 12:33:17 -070041 // Make sure our internal threads are dead before we start tearing down things they're using.
42 delete signal_catcher_;
43
Elliott Hughes93e74e82011-09-13 11:07:03 -070044 // Make sure all other threads have terminated too.
45 delete thread_list_;
46
Carl Shapiro61e019d2011-07-14 16:53:09 -070047 delete class_linker_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070048 Heap::Destroy();
Elliott Hughescf4c6c42011-09-01 15:16:42 -070049 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070050 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -070051 Thread::Shutdown();
Carl Shapiro4acf4642011-07-26 18:54:13 -070052 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070053 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -070054 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -070055}
56
Elliott Hughesffe67362011-07-17 12:09:27 -070057void Runtime::Abort(const char* file, int line) {
58 // Get any pending output out of the way.
59 fflush(NULL);
60
61 // Many people have difficulty distinguish aborts from crashes,
62 // so be explicit.
63 LogMessage(file, line, ERROR, -1).stream() << "Runtime aborting...";
64
Elliott Hughesffe67362011-07-17 12:09:27 -070065 // Perform any platform-specific pre-abort actions.
66 PlatformAbort(file, line);
67
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070068 // use abort hook if we have one
69 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
70 Runtime::Current()->abort_();
71 // notreached
72 }
73
Elliott Hughesffe67362011-07-17 12:09:27 -070074 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -070075 // receive SIGABRT. debuggerd dumps the stack trace of the main
76 // thread, whether or not that was the thread that failed. By
77 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -070078 // fault in the current thread, and get a useful log from debuggerd.
79 // We can also trivially tell the difference between a VM crash and
80 // a deliberate abort by looking at the fault address.
81 *reinterpret_cast<char*>(0xdeadd00d) = 38;
82 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -070083 // notreached
84}
85
Elliott Hughesbf86d042011-08-31 17:53:14 -070086void Runtime::CallExitHook(jint status) {
87 if (exit_ != NULL) {
88 ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative);
89 exit_(status);
90 LOG(WARNING) << "Exit hook returned instead of exiting!";
91 }
92}
93
Brian Carlstrom8a436592011-08-15 21:27:23 -070094// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
95// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
96// [gG] gigabytes.
97//
98// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -070099// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
100// of 1024.
101//
102// The spec says the -Xmx and -Xms options must be multiples of 1024. It
103// doesn't say anything about -Xss.
104//
105// Returns 0 (a useless size) if "s" is malformed or specifies a low or
106// non-evenly-divisible value.
107//
108size_t ParseMemoryOption(const char *s, size_t div) {
109 // strtoul accepts a leading [+-], which we don't want,
110 // so make sure our string starts with a decimal digit.
111 if (isdigit(*s)) {
112 const char *s2;
113 size_t val = strtoul(s, (char **)&s2, 10);
114 if (s2 != s) {
115 // s2 should be pointing just after the number.
116 // If this is the end of the string, the user
117 // has specified a number of bytes. Otherwise,
118 // there should be exactly one more character
119 // that specifies a multiplier.
120 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700121 // The remainder of the string is either a single multiplier
122 // character, or nothing to indicate that the value is in
123 // bytes.
124 char c = *s2++;
125 if (*s2 == '\0') {
126 size_t mul;
127 if (c == '\0') {
128 mul = 1;
129 } else if (c == 'k' || c == 'K') {
130 mul = KB;
131 } else if (c == 'm' || c == 'M') {
132 mul = MB;
133 } else if (c == 'g' || c == 'G') {
134 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700135 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700136 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700137 return 0;
138 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700139
140 if (val <= std::numeric_limits<size_t>::max() / mul) {
141 val *= mul;
142 } else {
143 // Clamp to a multiple of 1024.
144 val = std::numeric_limits<size_t>::max() & ~(1024-1);
145 }
146 } else {
147 // There's more than one character after the numeric part.
148 return 0;
149 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700150 }
151 // The man page says that a -Xm value must be a multiple of 1024.
152 if (val % div == 0) {
153 return val;
154 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700155 }
156 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700157 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700158}
159
Elliott Hughes0af55432011-08-17 18:37:28 -0700160void LoadJniLibrary(JavaVMExt* vm, const char* name) {
161 // TODO: OS_SHARED_LIB_FORMAT_STR
162 std::string mapped_name(StringPrintf("lib%s.so", name));
Elliott Hughes75770752011-08-24 17:52:38 -0700163 std::string reason;
164 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700165 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
166 << reason;
167 }
168}
169
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700170void CreateClassPath(const std::string& class_path,
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700171 std::vector<const DexFile*>& class_path_vector) {
Carl Shapirofc322c72011-07-27 00:20:01 -0700172 std::vector<std::string> parsed;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700173 Split(class_path, ':', parsed);
Carl Shapirofc322c72011-07-27 00:20:01 -0700174 for (size_t i = 0; i < parsed.size(); ++i) {
Brian Carlstrom16192862011-09-12 17:50:06 -0700175 const DexFile* dex_file = DexFile::Open(parsed[i], "");
Carl Shapirofc322c72011-07-27 00:20:01 -0700176 if (dex_file != NULL) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700177 class_path_vector.push_back(dex_file);
Carl Shapirofc322c72011-07-27 00:20:01 -0700178 }
179 }
180}
181
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700182Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700183 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700184 parsed->boot_image_ = NULL;
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700185#ifdef NDEBUG
Elliott Hughes5174fe62011-08-23 15:12:35 -0700186 // -Xcheck:jni is off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700187 parsed->check_jni_ = false;
188#else
189 // ...but on by default in debug builds.
190 parsed->check_jni_ = true;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700191#endif
Elliott Hughes85d15452011-09-16 17:33:01 -0700192
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700193 parsed->heap_initial_size_ = Heap::kInitialSize;
194 parsed->heap_maximum_size_ = Heap::kMaximumSize;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700195 parsed->stack_size_ = Thread::kDefaultStackSize;
196
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700197 parsed->hook_vfprintf_ = vfprintf;
198 parsed->hook_exit_ = exit;
199 parsed->hook_abort_ = abort;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700200
201 for (size_t i = 0; i < options.size(); ++i) {
202 const StringPiece& option = options[i].first;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700203 if (option.starts_with("-Xbootclasspath:")) {
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700204 parsed->boot_class_path_string_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700205 } else if (option == "bootclasspath") {
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700206 UNIMPLEMENTED(WARNING) << "what should VMRuntime.getBootClassPath return here?";
Brian Carlstromf734cf52011-08-17 16:28:14 -0700207 const void* dex_vector = options[i].second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700208 const std::vector<const DexFile*>* v
209 = reinterpret_cast<const std::vector<const DexFile*>*>(dex_vector);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700210 if (v == NULL) {
211 if (ignore_unrecognized) {
212 continue;
213 }
214 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700215 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700216 return NULL;
217 }
218 parsed->boot_class_path_ = *v;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700219 } else if (option == "classpath") {
220 const void* dex_vector = options[i].second;
221 const std::vector<const DexFile*>* v
222 = reinterpret_cast<const std::vector<const DexFile*>*>(dex_vector);
223 if (v == NULL) {
224 if (ignore_unrecognized) {
225 continue;
226 }
227 // TODO: usage
228 LOG(FATAL) << "Failed to parse " << option;
229 return NULL;
230 }
231 parsed->class_path_ = *v;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700232 } else if (option == "-classpath" || option == "-cp") {
233 // TODO: support -Djava.class.path
234 i++;
235 if (i == options.size()) {
236 // TODO: usage
237 LOG(FATAL) << "Missing required class path value for " << option;
238 return NULL;
239 }
240 const StringPiece& value = options[i].first;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700241 parsed->class_path_string_ = value.data();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700242 } else if (option.starts_with("-Xbootimage:")) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700243 // TODO: remove when intern_addr_ is removed, just use -Ximage:
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700244 parsed->boot_image_ = option.substr(strlen("-Xbootimage:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700245 } else if (option.starts_with("-Ximage:")) {
246 parsed->images_.push_back(option.substr(strlen("-Ximage:")).data());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700247 } else if (option.starts_with("-Xcheck:jni")) {
248 parsed->check_jni_ = true;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700249 } else if (option.starts_with("-Xms")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700250 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024);
251 if (size == 0) {
252 if (ignore_unrecognized) {
253 continue;
254 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700255 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700256 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700257 return NULL;
258 }
259 parsed->heap_initial_size_ = size;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700260 } else if (option.starts_with("-Xmx")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700261 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024);
262 if (size == 0) {
263 if (ignore_unrecognized) {
264 continue;
265 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700266 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700267 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700268 return NULL;
269 }
270 parsed->heap_maximum_size_ = size;
271 } else if (option.starts_with("-Xss")) {
272 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1);
273 if (size == 0) {
274 if (ignore_unrecognized) {
275 continue;
276 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700277 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700278 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700279 return NULL;
280 }
281 parsed->stack_size_ = size;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700282 } else if (option.starts_with("-D")) {
283 parsed->properties_.push_back(option.substr(strlen("-D")).data());
Elliott Hughesa0957642011-09-02 14:27:33 -0700284 } else if (option.starts_with("-Xjnitrace:")) {
285 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:")).data();
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700286 } else if (option.starts_with("-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700287 std::vector<std::string> verbose_options;
Elliott Hughes34023802011-08-30 12:06:17 -0700288 Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700289 for (size_t i = 0; i < verbose_options.size(); ++i) {
290 parsed->verbose_.insert(verbose_options[i]);
291 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700292 } else if (option == "vfprintf") {
293 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
294 } else if (option == "exit") {
295 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
296 } else if (option == "abort") {
297 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700298 } else {
299 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700300 // TODO: print usage via vfprintf
Brian Carlstrom8a436592011-08-15 21:27:23 -0700301 LOG(FATAL) << "Unrecognized option " << option;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700302 return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700303 }
304 }
305 }
306
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700307 // Consider it an error if both bootclasspath and -Xbootclasspath: are supplied.
Brian Carlstrom78128a62011-09-15 17:21:19 -0700308 // TODO: remove bootclasspath and classpath which are mostly just used by tests?
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700309 if (!parsed->boot_class_path_.empty() && !parsed->boot_class_path_string_.empty()) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700310 // TODO: usage
311 LOG(FATAL) << "bootclasspath and -Xbootclasspath: are mutually exclusive options.";
312 return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700313 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700314 if (!parsed->class_path_.empty() && !parsed->class_path_string_.empty()) {
315 // TODO: usage
316 LOG(FATAL) << "bootclasspath and -Xbootclasspath: are mutually exclusive options.";
317 return NULL;
318 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700319 if (parsed->boot_class_path_.empty()) {
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700320 if (parsed->boot_class_path_string_ == NULL) {
321 const char* BOOTCLASSPATH = getenv("BOOTCLASSPATH");
322 parsed->boot_class_path_string_ = BOOTCLASSPATH;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700323 }
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700324 CreateClassPath(parsed->boot_class_path_string_, parsed->boot_class_path_);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700325 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700326
Brian Carlstrom78128a62011-09-15 17:21:19 -0700327 if (parsed->class_path_.empty()) {
328 if (parsed->class_path_string_ == NULL) {
329 const char* CLASSPATH = getenv("CLASSPATH");
330 if (CLASSPATH != NULL) {
331 parsed->class_path_string_ = CLASSPATH;
332 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700333 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700334 CreateClassPath(parsed->class_path_string_, parsed->class_path_);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700335 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700336
Elliott Hughes85d15452011-09-16 17:33:01 -0700337 LOG(INFO) << "CheckJNI is " << (parsed->check_jni_ ? "on" : "off");
338
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700339 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700340}
341
342Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700343 // TODO: acquire a static mutex on Runtime to avoid racing.
344 if (Runtime::instance_ != NULL) {
345 return NULL;
346 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700347 instance_ = new Runtime;
348 if (!instance_->Init(options, ignore_unrecognized)) {
349 delete instance_;
350 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700351 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700352 return instance_;
353}
Elliott Hughes0af55432011-08-17 18:37:28 -0700354
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700355void Runtime::Start() {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700356 started_ = true;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700357
Elliott Hughes85d15452011-09-16 17:33:01 -0700358 // Initialize both the built-in and libcore native methods.
359 InitLibraries();
360
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700361 // Finish attaching the main thread.
Elliott Hughes85d15452011-09-16 17:33:01 -0700362 Thread::Current()->CreatePeer("main", false);
363
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700364 RunImageClinits();
365
Elliott Hughes85d15452011-09-16 17:33:01 -0700366 StartDaemonThreads();
367}
368
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700369// initialize classes that have instances in the image but that have
370// <clinit> methods so they could not be initialized by the compiler.
371void Runtime::RunImageClinits() {
372 Class* Field_class = class_linker_->FindSystemClass("Ljava/lang/reflect/Field;");
373 CHECK(Field_class->FindDeclaredDirectMethod("<clinit>", "()V") != NULL);
374 class_linker_->EnsureInitialized(Field_class);
375 CHECK(!Thread::Current()->IsExceptionPending());
376}
377
Elliott Hughes85d15452011-09-16 17:33:01 -0700378void Runtime::StartDaemonThreads() {
379 signal_catcher_ = new SignalCatcher;
380
381 Class* c = class_linker_->FindSystemClass("Ljava/lang/Daemons;");
382 CHECK(c != NULL);
383 Method* m = c->FindDirectMethod("start", "()V");
384 CHECK(m != NULL);
385// m->Invoke(Thread::Current(), NULL, NULL, NULL);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700386}
387
Elliott Hughesdcc24742011-09-07 14:02:44 -0700388bool Runtime::IsStarted() {
389 return started_;
390}
391
Elliott Hughes0af55432011-08-17 18:37:28 -0700392bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700393 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700394
Elliott Hughes90a33692011-08-30 13:27:07 -0700395 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
396 if (options.get() == NULL) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700397 LOG(WARNING) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700398 return false;
399 }
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700400
401 boot_class_path_ = options->boot_class_path_string_;
402 class_path_ = options->class_path_string_;
403 properties_ = options->properties_;
404
Elliott Hughes0af55432011-08-17 18:37:28 -0700405 vfprintf_ = options->hook_vfprintf_;
406 exit_ = options->hook_exit_;
407 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700408
Elliott Hughesbe759c62011-09-08 19:38:21 -0700409 default_stack_size_ = options->stack_size_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700410
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700411 thread_list_ = new ThreadList;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700412 intern_table_ = new InternTable;
413
Elliott Hughesbe759c62011-09-08 19:38:21 -0700414 Heap::Init(options->heap_initial_size_, options->heap_maximum_size_,
415 options->boot_image_, options->images_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700416
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700417 BlockSignals();
418
Elliott Hughesa0957642011-09-02 14:27:33 -0700419 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700420
Elliott Hughesbe759c62011-09-08 19:38:21 -0700421 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700422
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700423 // ClassLinker needs an attached thread, but we can't fully attach a thread
424 // without creating objects.
425 Thread::Attach(this, "main", false);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700426
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700427 class_linker_ = ClassLinker::Create(options->boot_class_path_,
428 options->class_path_,
429 intern_table_,
430 Heap::GetBootSpace());
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700431
Carl Shapiro1fb86202011-06-27 17:43:13 -0700432 return true;
433}
434
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700435void Runtime::InitLibraries() {
436 Thread* self = Thread::Current();
437 JNIEnv* env = self->GetJniEnv();
438
439 // Must be in the kNative state for JNI-based method registration.
440 ScopedThreadStateChange tsc(self, Thread::kNative);
441
442 // First set up the native methods provided by the runtime itself.
443 RegisterRuntimeNativeMethods(env);
444
445 // Now set up libcore, which is just a JNI library with a JNI_OnLoad.
446 // Most JNI libraries can just use System.loadLibrary, but you can't
447 // if you're the library that implements System.loadLibrary!
448 JniConstants::init(env);
449 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
450}
451
452void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
453#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700454 //REGISTER(register_dalvik_system_DexFile);
455 //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 Hughesad7c2a32011-08-31 11:58: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 Hughesad7c2a32011-08-31 11:58:10 -0700466 //REGISTER(register_java_lang_VMClassLoader);
467 //REGISTER(register_java_lang_reflect_AccessibleObject);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700468 REGISTER(register_java_lang_reflect_Array);
Elliott Hughesad7c2a32011-08-31 11:58:10 -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 Hughesc1674ed2011-08-25 18:09:09 -0700495void Runtime::BlockSignals() {
496 sigset_t sigset;
497 if (sigemptyset(&sigset) == -1) {
498 PLOG(FATAL) << "sigemptyset failed";
499 }
500 if (sigaddset(&sigset, SIGPIPE) == -1) {
501 PLOG(ERROR) << "sigaddset SIGPIPE failed";
502 }
503 // SIGQUIT is used to dump the runtime's state (including stack traces).
504 if (sigaddset(&sigset, SIGQUIT) == -1) {
505 PLOG(ERROR) << "sigaddset SIGQUIT failed";
506 }
507 // SIGUSR1 is used to initiate a heap dump.
508 if (sigaddset(&sigset, SIGUSR1) == -1) {
509 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
510 }
511 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
512}
513
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700514void Runtime::AttachCurrentThread(const char* name, bool as_daemon) {
515 Thread::Attach(instance_, name, as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700516}
517
Elliott Hughesd92bec42011-09-02 17:04:36 -0700518void Runtime::DetachCurrentThread() {
Elliott Hughes02b48d12011-09-07 17:15:51 -0700519 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700520}
521
Elliott Hughes410c0c82011-09-01 17:58:25 -0700522void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
523 class_linker_->VisitRoots(visitor, arg);
524 intern_table_->VisitRoots(visitor, arg);
525 java_vm_->VisitRoots(visitor, arg);
526 thread_list_->VisitRoots(visitor, arg);
Brian Carlstrom16192862011-09-12 17:50:06 -0700527 visitor(jni_stub_array_, arg);
Elliott Hughes410c0c82011-09-01 17:58:25 -0700528
529 //(*visitor)(&gDvm.outOfMemoryObj, 0, ROOT_VM_INTERNAL, arg);
530 //(*visitor)(&gDvm.internalErrorObj, 0, ROOT_VM_INTERNAL, arg);
531 //(*visitor)(&gDvm.noClassDefFoundErrorObj, 0, ROOT_VM_INTERNAL, arg);
532 UNIMPLEMENTED(WARNING) << "some roots not marked";
Brian Carlstrom1f870082011-08-23 16:02:11 -0700533}
534
Carl Shapiro1fb86202011-06-27 17:43:13 -0700535} // namespace art