blob: 0d48aea742dfd86cdc83e8d7ee052cf12852449c [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),
33 started_(false),
34 vfprintf_(NULL),
35 exit_(NULL),
36 abort_(NULL) {
37}
38
Carl Shapiro61e019d2011-07-14 16:53:09 -070039Runtime::~Runtime() {
Elliott Hughes5fe594f2011-09-08 12:33:17 -070040 // Make sure our internal threads are dead before we start tearing down things they're using.
41 delete signal_catcher_;
42
Elliott Hughes93e74e82011-09-13 11:07:03 -070043 // Make sure all other threads have terminated too.
44 delete thread_list_;
45
Carl Shapiro61e019d2011-07-14 16:53:09 -070046 delete class_linker_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070047 Heap::Destroy();
Elliott Hughescf4c6c42011-09-01 15:16:42 -070048 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070049 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -070050 Thread::Shutdown();
Carl Shapiro4acf4642011-07-26 18:54:13 -070051 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070052 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -070053 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -070054}
55
Elliott Hughesffe67362011-07-17 12:09:27 -070056void Runtime::Abort(const char* file, int line) {
57 // Get any pending output out of the way.
58 fflush(NULL);
59
60 // Many people have difficulty distinguish aborts from crashes,
61 // so be explicit.
62 LogMessage(file, line, ERROR, -1).stream() << "Runtime aborting...";
63
Elliott Hughesffe67362011-07-17 12:09:27 -070064 // Perform any platform-specific pre-abort actions.
65 PlatformAbort(file, line);
66
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070067 // use abort hook if we have one
68 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
69 Runtime::Current()->abort_();
70 // notreached
71 }
72
Elliott Hughesffe67362011-07-17 12:09:27 -070073 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -070074 // receive SIGABRT. debuggerd dumps the stack trace of the main
75 // thread, whether or not that was the thread that failed. By
76 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -070077 // fault in the current thread, and get a useful log from debuggerd.
78 // We can also trivially tell the difference between a VM crash and
79 // a deliberate abort by looking at the fault address.
80 *reinterpret_cast<char*>(0xdeadd00d) = 38;
81 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -070082 // notreached
83}
84
Elliott Hughesbf86d042011-08-31 17:53:14 -070085void Runtime::CallExitHook(jint status) {
86 if (exit_ != NULL) {
87 ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative);
88 exit_(status);
89 LOG(WARNING) << "Exit hook returned instead of exiting!";
90 }
91}
92
Brian Carlstrom8a436592011-08-15 21:27:23 -070093// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
94// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
95// [gG] gigabytes.
96//
97// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -070098// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
99// of 1024.
100//
101// The spec says the -Xmx and -Xms options must be multiples of 1024. It
102// doesn't say anything about -Xss.
103//
104// Returns 0 (a useless size) if "s" is malformed or specifies a low or
105// non-evenly-divisible value.
106//
107size_t ParseMemoryOption(const char *s, size_t div) {
108 // strtoul accepts a leading [+-], which we don't want,
109 // so make sure our string starts with a decimal digit.
110 if (isdigit(*s)) {
111 const char *s2;
112 size_t val = strtoul(s, (char **)&s2, 10);
113 if (s2 != s) {
114 // s2 should be pointing just after the number.
115 // If this is the end of the string, the user
116 // has specified a number of bytes. Otherwise,
117 // there should be exactly one more character
118 // that specifies a multiplier.
119 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700120 // The remainder of the string is either a single multiplier
121 // character, or nothing to indicate that the value is in
122 // bytes.
123 char c = *s2++;
124 if (*s2 == '\0') {
125 size_t mul;
126 if (c == '\0') {
127 mul = 1;
128 } else if (c == 'k' || c == 'K') {
129 mul = KB;
130 } else if (c == 'm' || c == 'M') {
131 mul = MB;
132 } else if (c == 'g' || c == 'G') {
133 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700134 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700135 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700136 return 0;
137 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700138
139 if (val <= std::numeric_limits<size_t>::max() / mul) {
140 val *= mul;
141 } else {
142 // Clamp to a multiple of 1024.
143 val = std::numeric_limits<size_t>::max() & ~(1024-1);
144 }
145 } else {
146 // There's more than one character after the numeric part.
147 return 0;
148 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700149 }
150 // The man page says that a -Xm value must be a multiple of 1024.
151 if (val % div == 0) {
152 return val;
153 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700154 }
155 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700156 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700157}
158
Elliott Hughes0af55432011-08-17 18:37:28 -0700159void LoadJniLibrary(JavaVMExt* vm, const char* name) {
160 // TODO: OS_SHARED_LIB_FORMAT_STR
161 std::string mapped_name(StringPrintf("lib%s.so", name));
Elliott Hughes75770752011-08-24 17:52:38 -0700162 std::string reason;
163 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700164 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
165 << reason;
166 }
167}
168
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700169void CreateClassPath(const std::string& class_path,
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700170 std::vector<const DexFile*>& class_path_vector) {
Carl Shapirofc322c72011-07-27 00:20:01 -0700171 std::vector<std::string> parsed;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700172 Split(class_path, ':', parsed);
Carl Shapirofc322c72011-07-27 00:20:01 -0700173 for (size_t i = 0; i < parsed.size(); ++i) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700174 const DexFile* dex_file = DexFile::Open(parsed[i]);
Carl Shapirofc322c72011-07-27 00:20:01 -0700175 if (dex_file != NULL) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700176 class_path_vector.push_back(dex_file);
Carl Shapirofc322c72011-07-27 00:20:01 -0700177 }
178 }
179}
180
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700181Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700182 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700183 parsed->boot_image_ = NULL;
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700184#ifdef NDEBUG
Elliott Hughes5174fe62011-08-23 15:12:35 -0700185 // -Xcheck:jni is off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700186 parsed->check_jni_ = false;
187#else
188 // ...but on by default in debug builds.
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700189#if 0 // TODO: disabled for oatexec until the shorty's used by check_jni are managed heap allocated.
190 // Instead we turn on -Xcheck_jni in common_test.
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700191 parsed->check_jni_ = true;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700192#else
193 parsed->check_jni_ = false;
194#endif
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700195#endif
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 Carlstrom69b15fb2011-09-03 12:25:21 -0700222 } else if (option == "-classpath" || option == "-cp") {
223 // TODO: support -Djava.class.path
224 i++;
225 if (i == options.size()) {
226 // TODO: usage
227 LOG(FATAL) << "Missing required class path value for " << option;
228 return NULL;
229 }
230 const StringPiece& value = options[i].first;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700231 parsed->class_path_string_ = value.data();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700232 } else if (option.starts_with("-Xbootimage:")) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700233 // TODO: remove when intern_addr_ is removed, just use -Ximage:
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700234 parsed->boot_image_ = option.substr(strlen("-Xbootimage:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700235 } else if (option.starts_with("-Ximage:")) {
236 parsed->images_.push_back(option.substr(strlen("-Ximage:")).data());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700237 } else if (option.starts_with("-Xcheck:jni")) {
238 parsed->check_jni_ = true;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700239 } else if (option.starts_with("-Xms")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700240 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024);
241 if (size == 0) {
242 if (ignore_unrecognized) {
243 continue;
244 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700245 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700246 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700247 return NULL;
248 }
249 parsed->heap_initial_size_ = size;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700250 } else if (option.starts_with("-Xmx")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700251 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).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_maximum_size_ = size;
261 } else if (option.starts_with("-Xss")) {
262 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1);
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->stack_size_ = size;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700272 } else if (option.starts_with("-D")) {
273 parsed->properties_.push_back(option.substr(strlen("-D")).data());
Elliott Hughesa0957642011-09-02 14:27:33 -0700274 } else if (option.starts_with("-Xjnitrace:")) {
275 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:")).data();
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700276 } else if (option.starts_with("-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700277 std::vector<std::string> verbose_options;
Elliott Hughes34023802011-08-30 12:06:17 -0700278 Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700279 for (size_t i = 0; i < verbose_options.size(); ++i) {
280 parsed->verbose_.insert(verbose_options[i]);
281 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700282 } else if (option == "vfprintf") {
283 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
284 } else if (option == "exit") {
285 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
286 } else if (option == "abort") {
287 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700288 } else {
289 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700290 // TODO: print usage via vfprintf
Brian Carlstrom8a436592011-08-15 21:27:23 -0700291 LOG(FATAL) << "Unrecognized option " << option;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700292 return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700293 }
294 }
295 }
296
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700297 // Consider it an error if both bootclasspath and -Xbootclasspath: are supplied.
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700298 // TODO: remove bootclasspath which is only mostly just used by tests?
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700299 if (!parsed->boot_class_path_.empty() && !parsed->boot_class_path_string_.empty()) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700300 // TODO: usage
301 LOG(FATAL) << "bootclasspath and -Xbootclasspath: are mutually exclusive options.";
302 return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700303 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700304 if (parsed->boot_class_path_.empty()) {
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700305 if (parsed->boot_class_path_string_ == NULL) {
306 const char* BOOTCLASSPATH = getenv("BOOTCLASSPATH");
307 parsed->boot_class_path_string_ = BOOTCLASSPATH;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700308 }
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700309 CreateClassPath(parsed->boot_class_path_string_, parsed->boot_class_path_);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700310 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700311
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700312 if (parsed->class_path_string_ == NULL) {
313 const char* CLASSPATH = getenv("CLASSPATH");
314 if (CLASSPATH != NULL) {
315 parsed->class_path_string_ = CLASSPATH;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700316 }
317 }
318 CHECK_EQ(parsed->class_path_.size(), 0U);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700319 CreateClassPath(parsed->class_path_string_, parsed->class_path_);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700320
321 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700322}
323
324Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700325 // TODO: acquire a static mutex on Runtime to avoid racing.
326 if (Runtime::instance_ != NULL) {
327 return NULL;
328 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700329 instance_ = new Runtime;
330 if (!instance_->Init(options, ignore_unrecognized)) {
331 delete instance_;
332 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700333 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700334 return instance_;
335}
Elliott Hughes0af55432011-08-17 18:37:28 -0700336
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700337void Runtime::Start() {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700338 started_ = true;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700339
340 // Finish attaching the main thread.
341 Thread* main_thread = Thread::Current();
342 main_thread->CreatePeer("main", false);
343
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700344 instance_->InitLibraries();
Elliott Hughese27955c2011-08-26 15:21:24 -0700345 instance_->signal_catcher_ = new SignalCatcher;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700346}
347
Elliott Hughesdcc24742011-09-07 14:02:44 -0700348bool Runtime::IsStarted() {
349 return started_;
350}
351
Elliott Hughes0af55432011-08-17 18:37:28 -0700352bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700353 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700354
Elliott Hughes90a33692011-08-30 13:27:07 -0700355 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
356 if (options.get() == NULL) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700357 LOG(WARNING) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700358 return false;
359 }
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700360
361 boot_class_path_ = options->boot_class_path_string_;
362 class_path_ = options->class_path_string_;
363 properties_ = options->properties_;
364
Elliott Hughes0af55432011-08-17 18:37:28 -0700365 vfprintf_ = options->hook_vfprintf_;
366 exit_ = options->hook_exit_;
367 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700368
Elliott Hughesbe759c62011-09-08 19:38:21 -0700369 default_stack_size_ = options->stack_size_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700370
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700371 thread_list_ = new ThreadList;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700372 intern_table_ = new InternTable;
373
Elliott Hughesbe759c62011-09-08 19:38:21 -0700374 Heap::Init(options->heap_initial_size_, options->heap_maximum_size_,
375 options->boot_image_, options->images_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700376
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700377 BlockSignals();
378
Elliott Hughesa0957642011-09-02 14:27:33 -0700379 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700380
Elliott Hughesbe759c62011-09-08 19:38:21 -0700381 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700382
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700383 // ClassLinker needs an attached thread, but we can't fully attach a thread
384 // without creating objects.
385 Thread::Attach(this, "main", false);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700386
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700387 class_linker_ = ClassLinker::Create(options->boot_class_path_,
388 options->class_path_,
389 intern_table_,
390 Heap::GetBootSpace());
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700391
Carl Shapiro1fb86202011-06-27 17:43:13 -0700392 return true;
393}
394
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700395void Runtime::InitLibraries() {
396 Thread* self = Thread::Current();
397 JNIEnv* env = self->GetJniEnv();
398
399 // Must be in the kNative state for JNI-based method registration.
400 ScopedThreadStateChange tsc(self, Thread::kNative);
401
402 // First set up the native methods provided by the runtime itself.
403 RegisterRuntimeNativeMethods(env);
404
405 // Now set up libcore, which is just a JNI library with a JNI_OnLoad.
406 // Most JNI libraries can just use System.loadLibrary, but you can't
407 // if you're the library that implements System.loadLibrary!
408 JniConstants::init(env);
409 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
410}
411
412void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
413#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700414 //REGISTER(register_dalvik_system_DexFile);
415 //REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700416 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700417 REGISTER(register_dalvik_system_VMStack);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700418 //REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700419 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700420 REGISTER(register_java_lang_Object);
421 REGISTER(register_java_lang_Runtime);
422 REGISTER(register_java_lang_String);
423 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700424 REGISTER(register_java_lang_Thread);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700425 REGISTER(register_java_lang_Throwable);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700426 //REGISTER(register_java_lang_VMClassLoader);
427 //REGISTER(register_java_lang_reflect_AccessibleObject);
428 //REGISTER(register_java_lang_reflect_Array);
429 //REGISTER(register_java_lang_reflect_Constructor);
430 //REGISTER(register_java_lang_reflect_Field);
431 //REGISTER(register_java_lang_reflect_Method);
432 //REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700433 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700434 //REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
435 //REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700436 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700437#undef REGISTER
438}
439
Elliott Hughes8daa0922011-09-11 13:46:25 -0700440void Runtime::Dump(std::ostream& os) {
Elliott Hughese27955c2011-08-26 15:21:24 -0700441 // TODO: dump other runtime statistics?
442 os << "Loaded classes: " << class_linker_->NumLoadedClasses() << "\n";
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700443 os << "Intern table size: " << GetInternTable()->Size() << "\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700444 // LOGV("VM stats: meth=%d ifld=%d sfld=%d linear=%d",
445 // gDvm.numDeclaredMethods,
446 // gDvm.numDeclaredInstFields,
447 // gDvm.numDeclaredStaticFields,
448 // gDvm.pBootLoaderAlloc->curOffset);
449 // LOGI("GC precise methods: %d", dvmPointerSetGetCount(gDvm.preciseMethods));
Elliott Hughes42ee1422011-09-06 12:33:32 -0700450 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700451
452 thread_list_->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700453}
454
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700455void Runtime::BlockSignals() {
456 sigset_t sigset;
457 if (sigemptyset(&sigset) == -1) {
458 PLOG(FATAL) << "sigemptyset failed";
459 }
460 if (sigaddset(&sigset, SIGPIPE) == -1) {
461 PLOG(ERROR) << "sigaddset SIGPIPE failed";
462 }
463 // SIGQUIT is used to dump the runtime's state (including stack traces).
464 if (sigaddset(&sigset, SIGQUIT) == -1) {
465 PLOG(ERROR) << "sigaddset SIGQUIT failed";
466 }
467 // SIGUSR1 is used to initiate a heap dump.
468 if (sigaddset(&sigset, SIGUSR1) == -1) {
469 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
470 }
471 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
472}
473
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700474void Runtime::AttachCurrentThread(const char* name, bool as_daemon) {
475 Thread::Attach(instance_, name, as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700476}
477
Elliott Hughesd92bec42011-09-02 17:04:36 -0700478void Runtime::DetachCurrentThread() {
Elliott Hughes02b48d12011-09-07 17:15:51 -0700479 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700480}
481
Elliott Hughes410c0c82011-09-01 17:58:25 -0700482void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
483 class_linker_->VisitRoots(visitor, arg);
484 intern_table_->VisitRoots(visitor, arg);
485 java_vm_->VisitRoots(visitor, arg);
486 thread_list_->VisitRoots(visitor, arg);
487
488 //(*visitor)(&gDvm.outOfMemoryObj, 0, ROOT_VM_INTERNAL, arg);
489 //(*visitor)(&gDvm.internalErrorObj, 0, ROOT_VM_INTERNAL, arg);
490 //(*visitor)(&gDvm.noClassDefFoundErrorObj, 0, ROOT_VM_INTERNAL, arg);
491 UNIMPLEMENTED(WARNING) << "some roots not marked";
Brian Carlstrom1f870082011-08-23 16:02:11 -0700492}
493
Carl Shapiro1fb86202011-06-27 17:43:13 -0700494} // namespace art