blob: 99f5e3a5d9b34607a096a5b59eab441cdd21721e [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_;
Elliott Hughes038a8062011-09-18 14:12:41 -070043 // TODO: GC thread.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070044
Elliott Hughes038a8062011-09-18 14:12:41 -070045 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
Elliott Hughes93e74e82011-09-13 11:07:03 -070046 delete thread_list_;
47
Carl Shapiro61e019d2011-07-14 16:53:09 -070048 delete class_linker_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070049 Heap::Destroy();
Elliott Hughescf4c6c42011-09-01 15:16:42 -070050 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070051 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -070052 Thread::Shutdown();
Carl Shapiro4acf4642011-07-26 18:54:13 -070053 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070054 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -070055 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -070056}
57
Elliott Hughesffe67362011-07-17 12:09:27 -070058void Runtime::Abort(const char* file, int line) {
59 // Get any pending output out of the way.
60 fflush(NULL);
61
62 // Many people have difficulty distinguish aborts from crashes,
63 // so be explicit.
64 LogMessage(file, line, ERROR, -1).stream() << "Runtime aborting...";
65
Elliott Hughesffe67362011-07-17 12:09:27 -070066 // Perform any platform-specific pre-abort actions.
67 PlatformAbort(file, line);
68
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070069 // use abort hook if we have one
70 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
71 Runtime::Current()->abort_();
72 // notreached
73 }
74
Elliott Hughesffe67362011-07-17 12:09:27 -070075 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -070076 // receive SIGABRT. debuggerd dumps the stack trace of the main
77 // thread, whether or not that was the thread that failed. By
78 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -070079 // fault in the current thread, and get a useful log from debuggerd.
80 // We can also trivially tell the difference between a VM crash and
81 // a deliberate abort by looking at the fault address.
82 *reinterpret_cast<char*>(0xdeadd00d) = 38;
83 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -070084 // notreached
85}
86
Elliott Hughesbf86d042011-08-31 17:53:14 -070087void Runtime::CallExitHook(jint status) {
88 if (exit_ != NULL) {
89 ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative);
90 exit_(status);
91 LOG(WARNING) << "Exit hook returned instead of exiting!";
92 }
93}
94
Brian Carlstrom8a436592011-08-15 21:27:23 -070095// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
96// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
97// [gG] gigabytes.
98//
99// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700100// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
101// of 1024.
102//
103// The spec says the -Xmx and -Xms options must be multiples of 1024. It
104// doesn't say anything about -Xss.
105//
106// Returns 0 (a useless size) if "s" is malformed or specifies a low or
107// non-evenly-divisible value.
108//
109size_t ParseMemoryOption(const char *s, size_t div) {
110 // strtoul accepts a leading [+-], which we don't want,
111 // so make sure our string starts with a decimal digit.
112 if (isdigit(*s)) {
113 const char *s2;
114 size_t val = strtoul(s, (char **)&s2, 10);
115 if (s2 != s) {
116 // s2 should be pointing just after the number.
117 // If this is the end of the string, the user
118 // has specified a number of bytes. Otherwise,
119 // there should be exactly one more character
120 // that specifies a multiplier.
121 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700122 // The remainder of the string is either a single multiplier
123 // character, or nothing to indicate that the value is in
124 // bytes.
125 char c = *s2++;
126 if (*s2 == '\0') {
127 size_t mul;
128 if (c == '\0') {
129 mul = 1;
130 } else if (c == 'k' || c == 'K') {
131 mul = KB;
132 } else if (c == 'm' || c == 'M') {
133 mul = MB;
134 } else if (c == 'g' || c == 'G') {
135 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700136 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700137 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700138 return 0;
139 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700140
141 if (val <= std::numeric_limits<size_t>::max() / mul) {
142 val *= mul;
143 } else {
144 // Clamp to a multiple of 1024.
145 val = std::numeric_limits<size_t>::max() & ~(1024-1);
146 }
147 } else {
148 // There's more than one character after the numeric part.
149 return 0;
150 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700151 }
152 // The man page says that a -Xm value must be a multiple of 1024.
153 if (val % div == 0) {
154 return val;
155 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700156 }
157 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700158 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700159}
160
Elliott Hughes0af55432011-08-17 18:37:28 -0700161void LoadJniLibrary(JavaVMExt* vm, const char* name) {
162 // TODO: OS_SHARED_LIB_FORMAT_STR
163 std::string mapped_name(StringPrintf("lib%s.so", name));
Elliott Hughes75770752011-08-24 17:52:38 -0700164 std::string reason;
165 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700166 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
167 << reason;
168 }
169}
170
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700171void CreateClassPath(const std::string& class_path,
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700172 std::vector<const DexFile*>& class_path_vector) {
Carl Shapirofc322c72011-07-27 00:20:01 -0700173 std::vector<std::string> parsed;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700174 Split(class_path, ':', parsed);
Carl Shapirofc322c72011-07-27 00:20:01 -0700175 for (size_t i = 0; i < parsed.size(); ++i) {
Brian Carlstrom16192862011-09-12 17:50:06 -0700176 const DexFile* dex_file = DexFile::Open(parsed[i], "");
Carl Shapirofc322c72011-07-27 00:20:01 -0700177 if (dex_file != NULL) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700178 class_path_vector.push_back(dex_file);
Carl Shapirofc322c72011-07-27 00:20:01 -0700179 }
180 }
181}
182
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700183Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700184 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700185 parsed->boot_image_ = NULL;
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700186#ifdef NDEBUG
Elliott Hughes5174fe62011-08-23 15:12:35 -0700187 // -Xcheck:jni is off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700188 parsed->check_jni_ = false;
189#else
190 // ...but on by default in debug builds.
191 parsed->check_jni_ = true;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700192#endif
Elliott Hughes85d15452011-09-16 17:33:01 -0700193
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700194 parsed->heap_initial_size_ = Heap::kInitialSize;
195 parsed->heap_maximum_size_ = Heap::kMaximumSize;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700196 parsed->stack_size_ = Thread::kDefaultStackSize;
197
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700198 parsed->hook_vfprintf_ = vfprintf;
199 parsed->hook_exit_ = exit;
200 parsed->hook_abort_ = abort;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700201
202 for (size_t i = 0; i < options.size(); ++i) {
203 const StringPiece& option = options[i].first;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700204 if (option.starts_with("-Xbootclasspath:")) {
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700205 parsed->boot_class_path_string_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700206 } else if (option == "bootclasspath") {
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700207 UNIMPLEMENTED(WARNING) << "what should VMRuntime.getBootClassPath return here?";
Brian Carlstromf734cf52011-08-17 16:28:14 -0700208 const void* dex_vector = options[i].second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700209 const std::vector<const DexFile*>* v
210 = reinterpret_cast<const std::vector<const DexFile*>*>(dex_vector);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700211 if (v == NULL) {
212 if (ignore_unrecognized) {
213 continue;
214 }
215 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700216 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700217 return NULL;
218 }
219 parsed->boot_class_path_ = *v;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700220 } else if (option == "classpath") {
221 const void* dex_vector = options[i].second;
222 const std::vector<const DexFile*>* v
223 = reinterpret_cast<const std::vector<const DexFile*>*>(dex_vector);
224 if (v == NULL) {
225 if (ignore_unrecognized) {
226 continue;
227 }
228 // TODO: usage
229 LOG(FATAL) << "Failed to parse " << option;
230 return NULL;
231 }
232 parsed->class_path_ = *v;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700233 } else if (option == "-classpath" || option == "-cp") {
234 // TODO: support -Djava.class.path
235 i++;
236 if (i == options.size()) {
237 // TODO: usage
238 LOG(FATAL) << "Missing required class path value for " << option;
239 return NULL;
240 }
241 const StringPiece& value = options[i].first;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700242 parsed->class_path_string_ = value.data();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700243 } else if (option.starts_with("-Xbootimage:")) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700244 // TODO: remove when intern_addr_ is removed, just use -Ximage:
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700245 parsed->boot_image_ = option.substr(strlen("-Xbootimage:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700246 } else if (option.starts_with("-Ximage:")) {
247 parsed->images_.push_back(option.substr(strlen("-Ximage:")).data());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700248 } else if (option.starts_with("-Xcheck:jni")) {
249 parsed->check_jni_ = true;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700250 } else if (option.starts_with("-Xms")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700251 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024);
252 if (size == 0) {
253 if (ignore_unrecognized) {
254 continue;
255 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700256 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700257 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700258 return NULL;
259 }
260 parsed->heap_initial_size_ = size;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700261 } else if (option.starts_with("-Xmx")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700262 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024);
263 if (size == 0) {
264 if (ignore_unrecognized) {
265 continue;
266 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700267 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700268 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700269 return NULL;
270 }
271 parsed->heap_maximum_size_ = size;
272 } else if (option.starts_with("-Xss")) {
273 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1);
274 if (size == 0) {
275 if (ignore_unrecognized) {
276 continue;
277 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700278 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700279 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700280 return NULL;
281 }
282 parsed->stack_size_ = size;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700283 } else if (option.starts_with("-D")) {
284 parsed->properties_.push_back(option.substr(strlen("-D")).data());
Elliott Hughesa0957642011-09-02 14:27:33 -0700285 } else if (option.starts_with("-Xjnitrace:")) {
286 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:")).data();
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700287 } else if (option.starts_with("-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700288 std::vector<std::string> verbose_options;
Elliott Hughes34023802011-08-30 12:06:17 -0700289 Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700290 for (size_t i = 0; i < verbose_options.size(); ++i) {
291 parsed->verbose_.insert(verbose_options[i]);
292 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700293 } else if (option == "vfprintf") {
294 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
295 } else if (option == "exit") {
296 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
297 } else if (option == "abort") {
298 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700299 } else {
300 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700301 // TODO: print usage via vfprintf
Brian Carlstrom8a436592011-08-15 21:27:23 -0700302 LOG(FATAL) << "Unrecognized option " << option;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700303 return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700304 }
305 }
306 }
307
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700308 // Consider it an error if both bootclasspath and -Xbootclasspath: are supplied.
Brian Carlstrom78128a62011-09-15 17:21:19 -0700309 // TODO: remove bootclasspath and classpath which are mostly just used by tests?
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700310 if (!parsed->boot_class_path_.empty() && !parsed->boot_class_path_string_.empty()) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700311 // TODO: usage
312 LOG(FATAL) << "bootclasspath and -Xbootclasspath: are mutually exclusive options.";
313 return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700314 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700315 if (!parsed->class_path_.empty() && !parsed->class_path_string_.empty()) {
316 // TODO: usage
317 LOG(FATAL) << "bootclasspath and -Xbootclasspath: are mutually exclusive options.";
318 return NULL;
319 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700320 if (parsed->boot_class_path_.empty()) {
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700321 if (parsed->boot_class_path_string_ == NULL) {
322 const char* BOOTCLASSPATH = getenv("BOOTCLASSPATH");
323 parsed->boot_class_path_string_ = BOOTCLASSPATH;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700324 }
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700325 CreateClassPath(parsed->boot_class_path_string_, parsed->boot_class_path_);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700326 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700327
Brian Carlstrom78128a62011-09-15 17:21:19 -0700328 if (parsed->class_path_.empty()) {
329 if (parsed->class_path_string_ == NULL) {
330 const char* CLASSPATH = getenv("CLASSPATH");
331 if (CLASSPATH != NULL) {
332 parsed->class_path_string_ = CLASSPATH;
333 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700334 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700335 CreateClassPath(parsed->class_path_string_, parsed->class_path_);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700336 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700337
Elliott Hughes85d15452011-09-16 17:33:01 -0700338 LOG(INFO) << "CheckJNI is " << (parsed->check_jni_ ? "on" : "off");
339
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700340 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700341}
342
343Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700344 // TODO: acquire a static mutex on Runtime to avoid racing.
345 if (Runtime::instance_ != NULL) {
346 return NULL;
347 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700348 instance_ = new Runtime;
349 if (!instance_->Init(options, ignore_unrecognized)) {
350 delete instance_;
351 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700352 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700353 return instance_;
354}
Elliott Hughes0af55432011-08-17 18:37:28 -0700355
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700356void Runtime::Start() {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700357 started_ = true;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700358
Elliott Hughes038a8062011-09-18 14:12:41 -0700359 InitNativeMethods();
Elliott Hughes85d15452011-09-16 17:33:01 -0700360
Elliott Hughes038a8062011-09-18 14:12:41 -0700361 Thread::FinishStartup();
Elliott Hughes85d15452011-09-16 17:33:01 -0700362
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700363 RunImageClinits();
364
Elliott Hughes85d15452011-09-16 17:33:01 -0700365 StartDaemonThreads();
366}
367
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700368// initialize classes that have instances in the image but that have
369// <clinit> methods so they could not be initialized by the compiler.
370void Runtime::RunImageClinits() {
371 Class* Field_class = class_linker_->FindSystemClass("Ljava/lang/reflect/Field;");
372 CHECK(Field_class->FindDeclaredDirectMethod("<clinit>", "()V") != NULL);
373 class_linker_->EnsureInitialized(Field_class);
374 CHECK(!Thread::Current()->IsExceptionPending());
375}
376
Elliott Hughes85d15452011-09-16 17:33:01 -0700377void Runtime::StartDaemonThreads() {
378 signal_catcher_ = new SignalCatcher;
379
380 Class* c = class_linker_->FindSystemClass("Ljava/lang/Daemons;");
381 CHECK(c != NULL);
382 Method* m = c->FindDirectMethod("start", "()V");
383 CHECK(m != NULL);
Elliott Hughes038a8062011-09-18 14:12:41 -0700384 m->Invoke(Thread::Current(), NULL, NULL, NULL);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700385}
386
Elliott Hughesdcc24742011-09-07 14:02:44 -0700387bool Runtime::IsStarted() {
388 return started_;
389}
390
Elliott Hughes0af55432011-08-17 18:37:28 -0700391bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700392 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700393
Elliott Hughes90a33692011-08-30 13:27:07 -0700394 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
395 if (options.get() == NULL) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700396 LOG(WARNING) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700397 return false;
398 }
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700399
400 boot_class_path_ = options->boot_class_path_string_;
401 class_path_ = options->class_path_string_;
402 properties_ = options->properties_;
403
Elliott Hughes0af55432011-08-17 18:37:28 -0700404 vfprintf_ = options->hook_vfprintf_;
405 exit_ = options->hook_exit_;
406 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700407
Elliott Hughesbe759c62011-09-08 19:38:21 -0700408 default_stack_size_ = options->stack_size_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700409
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700410 thread_list_ = new ThreadList;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700411 intern_table_ = new InternTable;
412
Elliott Hughesbe759c62011-09-08 19:38:21 -0700413 Heap::Init(options->heap_initial_size_, options->heap_maximum_size_,
414 options->boot_image_, options->images_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700415
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700416 BlockSignals();
417
Elliott Hughesa0957642011-09-02 14:27:33 -0700418 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700419
Elliott Hughesbe759c62011-09-08 19:38:21 -0700420 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700421
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700422 // ClassLinker needs an attached thread, but we can't fully attach a thread
423 // without creating objects.
424 Thread::Attach(this, "main", false);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700425
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700426 class_linker_ = ClassLinker::Create(options->boot_class_path_,
427 options->class_path_,
428 intern_table_,
429 Heap::GetBootSpace());
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700430
Carl Shapiro1fb86202011-06-27 17:43:13 -0700431 return true;
432}
433
Elliott Hughes038a8062011-09-18 14:12:41 -0700434void Runtime::InitNativeMethods() {
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700435 Thread* self = Thread::Current();
436 JNIEnv* env = self->GetJniEnv();
437
438 // Must be in the kNative state for JNI-based method registration.
439 ScopedThreadStateChange tsc(self, Thread::kNative);
440
441 // First set up the native methods provided by the runtime itself.
442 RegisterRuntimeNativeMethods(env);
443
444 // Now set up libcore, which is just a JNI library with a JNI_OnLoad.
445 // Most JNI libraries can just use System.loadLibrary, but you can't
446 // if you're the library that implements System.loadLibrary!
447 JniConstants::init(env);
448 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
449}
450
451void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
452#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700453 //REGISTER(register_dalvik_system_DexFile);
454 //REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700455 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700456 REGISTER(register_dalvik_system_VMStack);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700457 //REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700458 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700459 REGISTER(register_java_lang_Object);
460 REGISTER(register_java_lang_Runtime);
461 REGISTER(register_java_lang_String);
462 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700463 REGISTER(register_java_lang_Thread);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700464 REGISTER(register_java_lang_Throwable);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700465 //REGISTER(register_java_lang_VMClassLoader);
466 //REGISTER(register_java_lang_reflect_AccessibleObject);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700467 REGISTER(register_java_lang_reflect_Array);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700468 //REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700469 REGISTER(register_java_lang_reflect_Field);
470 REGISTER(register_java_lang_reflect_Method);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700471 //REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700472 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700473 //REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
474 //REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700475 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700476#undef REGISTER
477}
478
Elliott Hughes8daa0922011-09-11 13:46:25 -0700479void Runtime::Dump(std::ostream& os) {
Elliott Hughese27955c2011-08-26 15:21:24 -0700480 // TODO: dump other runtime statistics?
481 os << "Loaded classes: " << class_linker_->NumLoadedClasses() << "\n";
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700482 os << "Intern table size: " << GetInternTable()->Size() << "\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700483 // LOGV("VM stats: meth=%d ifld=%d sfld=%d linear=%d",
484 // gDvm.numDeclaredMethods,
485 // gDvm.numDeclaredInstFields,
486 // gDvm.numDeclaredStaticFields,
487 // gDvm.pBootLoaderAlloc->curOffset);
488 // LOGI("GC precise methods: %d", dvmPointerSetGetCount(gDvm.preciseMethods));
Elliott Hughes42ee1422011-09-06 12:33:32 -0700489 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700490
491 thread_list_->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700492}
493
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700494void Runtime::BlockSignals() {
495 sigset_t sigset;
496 if (sigemptyset(&sigset) == -1) {
497 PLOG(FATAL) << "sigemptyset failed";
498 }
499 if (sigaddset(&sigset, SIGPIPE) == -1) {
500 PLOG(ERROR) << "sigaddset SIGPIPE failed";
501 }
502 // SIGQUIT is used to dump the runtime's state (including stack traces).
503 if (sigaddset(&sigset, SIGQUIT) == -1) {
504 PLOG(ERROR) << "sigaddset SIGQUIT failed";
505 }
506 // SIGUSR1 is used to initiate a heap dump.
507 if (sigaddset(&sigset, SIGUSR1) == -1) {
508 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
509 }
510 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
511}
512
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700513void Runtime::AttachCurrentThread(const char* name, bool as_daemon) {
514 Thread::Attach(instance_, name, as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700515}
516
Elliott Hughesd92bec42011-09-02 17:04:36 -0700517void Runtime::DetachCurrentThread() {
Elliott Hughes038a8062011-09-18 14:12:41 -0700518 // TODO: check we're not calling DetachCurrentThread from a call stack that
519 // includes managed frames. (It's only valid if the stack is all-native.)
Elliott Hughes02b48d12011-09-07 17:15:51 -0700520 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700521}
522
Elliott Hughes410c0c82011-09-01 17:58:25 -0700523void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
524 class_linker_->VisitRoots(visitor, arg);
525 intern_table_->VisitRoots(visitor, arg);
526 java_vm_->VisitRoots(visitor, arg);
527 thread_list_->VisitRoots(visitor, arg);
Brian Carlstrom16192862011-09-12 17:50:06 -0700528 visitor(jni_stub_array_, arg);
Elliott Hughes410c0c82011-09-01 17:58:25 -0700529
530 //(*visitor)(&gDvm.outOfMemoryObj, 0, ROOT_VM_INTERNAL, arg);
531 //(*visitor)(&gDvm.internalErrorObj, 0, ROOT_VM_INTERNAL, arg);
532 //(*visitor)(&gDvm.noClassDefFoundErrorObj, 0, ROOT_VM_INTERNAL, arg);
533 UNIMPLEMENTED(WARNING) << "some roots not marked";
Brian Carlstrom1f870082011-08-23 16:02:11 -0700534}
535
Carl Shapiro1fb86202011-06-27 17:43:13 -0700536} // namespace art