blob: b4c25d39ae415ee339a98ca0c466ea251cbd3bc8 [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
Brian Carlstromaded5f72011-10-07 17:15:04 -070010#include "ScopedLocalRef.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070011#include "UniquePtr.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070012#include "class_linker.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070013#include "class_loader.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070014#include "heap.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070015#include "image.h"
Elliott Hughescf4c6c42011-09-01 15:16:42 -070016#include "intern_table.h"
Elliott Hughesc5f7c912011-08-18 14:00:42 -070017#include "jni_internal.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070018#include "oat_file.h"
Elliott Hughes726079d2011-10-07 18:43:44 -070019#include "ScopedLocalRef.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070020#include "signal_catcher.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070021#include "space.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070022#include "thread.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070023#include "thread_list.h"
Carl Shapiro61e019d2011-07-14 16:53:09 -070024
Elliott Hughes90a33692011-08-30 13:27:07 -070025// TODO: this drags in cutil/log.h, which conflicts with our logging.h.
26#include "JniConstants.h"
27
Carl Shapiro1fb86202011-06-27 17:43:13 -070028namespace art {
29
Carl Shapiro2ed144c2011-07-26 16:52:08 -070030Runtime* Runtime::instance_ = NULL;
31
Elliott Hughesdcc24742011-09-07 14:02:44 -070032Runtime::Runtime()
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -070033 : verbose_startup_(false),
34 default_stack_size_(Thread::kDefaultStackSize),
Elliott Hughesdcc24742011-09-07 14:02:44 -070035 thread_list_(NULL),
36 intern_table_(NULL),
37 class_linker_(NULL),
38 signal_catcher_(NULL),
39 java_vm_(NULL),
Brian Carlstrom16192862011-09-12 17:50:06 -070040 jni_stub_array_(NULL),
Brian Carlstrome24fa612011-09-29 00:53:55 -070041 abstract_method_error_stub_array_(NULL),
Elliott Hughesdcc24742011-09-07 14:02:44 -070042 started_(false),
43 vfprintf_(NULL),
44 exit_(NULL),
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070045 abort_(NULL),
46 stats_enabled_(false) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -070047 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
48 resolution_stub_array_[i] = NULL;
49 }
50 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
51 callee_save_method_[i] = NULL;
52 }
Elliott Hughesdcc24742011-09-07 14:02:44 -070053}
54
Carl Shapiro61e019d2011-07-14 16:53:09 -070055Runtime::~Runtime() {
Elliott Hughes5fe594f2011-09-08 12:33:17 -070056 // Make sure our internal threads are dead before we start tearing down things they're using.
57 delete signal_catcher_;
Elliott Hughes038a8062011-09-18 14:12:41 -070058 // TODO: GC thread.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070059
Elliott Hughes038a8062011-09-18 14:12:41 -070060 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
Elliott Hughes93e74e82011-09-13 11:07:03 -070061 delete thread_list_;
62
Carl Shapiro61e019d2011-07-14 16:53:09 -070063 delete class_linker_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070064 Heap::Destroy();
Elliott Hughescf4c6c42011-09-01 15:16:42 -070065 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070066 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -070067 Thread::Shutdown();
Carl Shapiro4acf4642011-07-26 18:54:13 -070068 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070069 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -070070 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -070071}
72
Elliott Hughesffe67362011-07-17 12:09:27 -070073void Runtime::Abort(const char* file, int line) {
74 // Get any pending output out of the way.
75 fflush(NULL);
76
77 // Many people have difficulty distinguish aborts from crashes,
78 // so be explicit.
79 LogMessage(file, line, ERROR, -1).stream() << "Runtime aborting...";
80
Elliott Hughesffe67362011-07-17 12:09:27 -070081 // Perform any platform-specific pre-abort actions.
82 PlatformAbort(file, line);
83
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070084 // use abort hook if we have one
85 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
86 Runtime::Current()->abort_();
87 // notreached
88 }
89
Elliott Hughesffe67362011-07-17 12:09:27 -070090 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -070091 // receive SIGABRT. debuggerd dumps the stack trace of the main
92 // thread, whether or not that was the thread that failed. By
93 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -070094 // fault in the current thread, and get a useful log from debuggerd.
95 // We can also trivially tell the difference between a VM crash and
96 // a deliberate abort by looking at the fault address.
97 *reinterpret_cast<char*>(0xdeadd00d) = 38;
98 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -070099 // notreached
100}
101
Elliott Hughesbf86d042011-08-31 17:53:14 -0700102void Runtime::CallExitHook(jint status) {
103 if (exit_ != NULL) {
104 ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative);
105 exit_(status);
106 LOG(WARNING) << "Exit hook returned instead of exiting!";
107 }
108}
109
Brian Carlstrom8a436592011-08-15 21:27:23 -0700110// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
111// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
112// [gG] gigabytes.
113//
114// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700115// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
116// of 1024.
117//
118// The spec says the -Xmx and -Xms options must be multiples of 1024. It
119// doesn't say anything about -Xss.
120//
121// Returns 0 (a useless size) if "s" is malformed or specifies a low or
122// non-evenly-divisible value.
123//
124size_t ParseMemoryOption(const char *s, size_t div) {
125 // strtoul accepts a leading [+-], which we don't want,
126 // so make sure our string starts with a decimal digit.
127 if (isdigit(*s)) {
128 const char *s2;
129 size_t val = strtoul(s, (char **)&s2, 10);
130 if (s2 != s) {
131 // s2 should be pointing just after the number.
132 // If this is the end of the string, the user
133 // has specified a number of bytes. Otherwise,
134 // there should be exactly one more character
135 // that specifies a multiplier.
136 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700137 // The remainder of the string is either a single multiplier
138 // character, or nothing to indicate that the value is in
139 // bytes.
140 char c = *s2++;
141 if (*s2 == '\0') {
142 size_t mul;
143 if (c == '\0') {
144 mul = 1;
145 } else if (c == 'k' || c == 'K') {
146 mul = KB;
147 } else if (c == 'm' || c == 'M') {
148 mul = MB;
149 } else if (c == 'g' || c == 'G') {
150 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700151 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700152 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700153 return 0;
154 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700155
156 if (val <= std::numeric_limits<size_t>::max() / mul) {
157 val *= mul;
158 } else {
159 // Clamp to a multiple of 1024.
160 val = std::numeric_limits<size_t>::max() & ~(1024-1);
161 }
162 } else {
163 // There's more than one character after the numeric part.
164 return 0;
165 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700166 }
167 // The man page says that a -Xm value must be a multiple of 1024.
168 if (val % div == 0) {
169 return val;
170 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700171 }
172 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700173 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700174}
175
Elliott Hughes0af55432011-08-17 18:37:28 -0700176void LoadJniLibrary(JavaVMExt* vm, const char* name) {
177 // TODO: OS_SHARED_LIB_FORMAT_STR
178 std::string mapped_name(StringPrintf("lib%s.so", name));
Elliott Hughes75770752011-08-24 17:52:38 -0700179 std::string reason;
180 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700181 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
182 << reason;
183 }
184}
185
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700186Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700187 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700188 const char* boot_class_path = getenv("BOOTCLASSPATH");
189 if (boot_class_path != NULL) {
190 parsed->boot_class_path_ = getenv("BOOTCLASSPATH");
191 }
192 const char* class_path = getenv("CLASSPATH");
193 if (class_path != NULL) {
194 parsed->class_path_ = getenv("CLASSPATH");
195 }
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700196#ifdef NDEBUG
Elliott Hughes5174fe62011-08-23 15:12:35 -0700197 // -Xcheck:jni is off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700198 parsed->check_jni_ = false;
199#else
200 // ...but on by default in debug builds.
201 parsed->check_jni_ = true;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700202#endif
Elliott Hughes85d15452011-09-16 17:33:01 -0700203
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700204 parsed->heap_initial_size_ = Heap::kInitialSize;
205 parsed->heap_maximum_size_ = Heap::kMaximumSize;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700206 parsed->stack_size_ = Thread::kDefaultStackSize;
207
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700208 parsed->hook_vfprintf_ = vfprintf;
209 parsed->hook_exit_ = exit;
210 parsed->hook_abort_ = abort;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700211
212 for (size_t i = 0; i < options.size(); ++i) {
213 const StringPiece& option = options[i].first;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700214 if (false) {
215 LOG(INFO) << "option[" << i << "]=" << option;
216 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700217 if (option.starts_with("-Xbootclasspath:")) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700218 parsed->boot_class_path_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700219 } else if (option == "-classpath" || option == "-cp") {
220 // TODO: support -Djava.class.path
221 i++;
222 if (i == options.size()) {
223 // TODO: usage
224 LOG(FATAL) << "Missing required class path value for " << option;
225 return NULL;
226 }
227 const StringPiece& value = options[i].first;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700228 parsed->class_path_ = value.data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700229 } else if (option.starts_with("-Ximage:")) {
230 parsed->images_.push_back(option.substr(strlen("-Ximage:")).data());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700231 } else if (option.starts_with("-Xcheck:jni")) {
232 parsed->check_jni_ = true;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700233 } else if (option.starts_with("-Xms")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700234 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024);
235 if (size == 0) {
236 if (ignore_unrecognized) {
237 continue;
238 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700239 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700240 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700241 return NULL;
242 }
243 parsed->heap_initial_size_ = size;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700244 } else if (option.starts_with("-Xmx")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700245 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024);
246 if (size == 0) {
247 if (ignore_unrecognized) {
248 continue;
249 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700250 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700251 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700252 return NULL;
253 }
254 parsed->heap_maximum_size_ = size;
255 } else if (option.starts_with("-Xss")) {
256 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1);
257 if (size == 0) {
258 if (ignore_unrecognized) {
259 continue;
260 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700261 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700262 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700263 return NULL;
264 }
265 parsed->stack_size_ = size;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700266 } else if (option.starts_with("-D")) {
267 parsed->properties_.push_back(option.substr(strlen("-D")).data());
Elliott Hughesa0957642011-09-02 14:27:33 -0700268 } else if (option.starts_with("-Xjnitrace:")) {
269 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:")).data();
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700270 } else if (option.starts_with("-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700271 std::vector<std::string> verbose_options;
Elliott Hughes34023802011-08-30 12:06:17 -0700272 Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700273 for (size_t i = 0; i < verbose_options.size(); ++i) {
274 parsed->verbose_.insert(verbose_options[i]);
275 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700276 } else if (option == "vfprintf") {
277 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
278 } else if (option == "exit") {
279 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
280 } else if (option == "abort") {
281 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700282 } else if (option == "host-prefix") {
283 parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700284 } else {
285 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700286 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700287 LOG(ERROR) << "Unrecognized option " << option;
288 // TODO: this should exit, but for now tolerate unknown options
289 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700290 }
291 }
292 }
293
Elliott Hughes85d15452011-09-16 17:33:01 -0700294 LOG(INFO) << "CheckJNI is " << (parsed->check_jni_ ? "on" : "off");
295
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700296 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700297}
298
299Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700300 // TODO: acquire a static mutex on Runtime to avoid racing.
301 if (Runtime::instance_ != NULL) {
302 return NULL;
303 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700304 instance_ = new Runtime;
305 if (!instance_->Init(options, ignore_unrecognized)) {
306 delete instance_;
307 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700308 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700309 return instance_;
310}
Elliott Hughes0af55432011-08-17 18:37:28 -0700311
Brian Carlstromaded5f72011-10-07 17:15:04 -0700312void CreateSystemClassLoader() {
313 if (ClassLoader::UseCompileTimeClassPath()) {
314 return;
315 }
316
317 Thread* self = Thread::Current();
318
319 // Must be in the kNative state for calling native methods.
320 CHECK_EQ(self->GetState(), Thread::kNative);
321
322 JNIEnv* env = self->GetJniEnv();
Brian Carlstromdf143242011-10-10 18:05:34 -0700323 ScopedLocalRef<jclass> ClassLoader_class(env, env->FindClass("java/lang/ClassLoader"));
324 CHECK(ClassLoader_class.get() != NULL);
325 jmethodID getSystemClassLoader = env->GetStaticMethodID(ClassLoader_class.get(),
326 "getSystemClassLoader",
327 "()Ljava/lang/ClassLoader;");
328 CHECK(getSystemClassLoader != NULL);
329 ScopedLocalRef<jobject> class_loader(env, env->CallStaticObjectMethod(ClassLoader_class.get(),
330 getSystemClassLoader));
331 CHECK(class_loader.get() != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700332
Brian Carlstromdf143242011-10-10 18:05:34 -0700333 Thread::Current()->SetClassLoaderOverride(Decode<ClassLoader*>(env, class_loader.get()));
Brian Carlstromaded5f72011-10-07 17:15:04 -0700334}
335
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700336void Runtime::Start() {
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700337 if (IsVerboseStartup()) {
338 LOG(INFO) << "Runtime::Start entering";
339 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700340
341 CHECK(host_prefix_.empty()) << host_prefix_;
342
Elliott Hughes038a8062011-09-18 14:12:41 -0700343 InitNativeMethods();
Elliott Hughes85d15452011-09-16 17:33:01 -0700344
Elliott Hughes038a8062011-09-18 14:12:41 -0700345 Thread::FinishStartup();
Elliott Hughes85d15452011-09-16 17:33:01 -0700346
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700347 class_linker_->RunRootClinits();
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700348
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700349 // Class::AllocObject asserts that all objects allocated better be
350 // initialized after Runtime::IsStarted is true, so this needs to
351 // come after ClassLinker::RunRootClinits.
352 started_ = true;
353
Elliott Hughes85d15452011-09-16 17:33:01 -0700354 StartDaemonThreads();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700355
Brian Carlstromaded5f72011-10-07 17:15:04 -0700356 CreateSystemClassLoader();
357
Elliott Hughes726079d2011-10-07 18:43:44 -0700358 Thread::Current()->GetJniEnv()->locals.AssertEmpty();
359
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700360 if (IsVerboseStartup()) {
361 LOG(INFO) << "Runtime::Start exiting";
362 }
Elliott Hughes85d15452011-09-16 17:33:01 -0700363}
364
365void Runtime::StartDaemonThreads() {
366 signal_catcher_ = new SignalCatcher;
367
Elliott Hughes719b3232011-09-25 17:42:19 -0700368 Thread* self = Thread::Current();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700369
370 // Must be in the kNative state for calling native methods.
371 CHECK_EQ(self->GetState(), Thread::kNative);
372
Elliott Hughes719b3232011-09-25 17:42:19 -0700373 JNIEnv* env = self->GetJniEnv();
Elliott Hughes726079d2011-10-07 18:43:44 -0700374 ScopedLocalRef<jclass> c(env, env->FindClass("java/lang/Daemons"));
375 CHECK(c.get() != NULL);
376 jmethodID mid = env->GetStaticMethodID(c.get(), "start", "()V");
Elliott Hughes719b3232011-09-25 17:42:19 -0700377 CHECK(mid != NULL);
Elliott Hughes726079d2011-10-07 18:43:44 -0700378 env->CallStaticVoidMethod(c.get(), mid);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700379}
380
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700381bool Runtime::IsStarted() const {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700382 return started_;
383}
384
Elliott Hughes0af55432011-08-17 18:37:28 -0700385bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700386 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700387
Elliott Hughes90a33692011-08-30 13:27:07 -0700388 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
389 if (options.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700390 LOG(ERROR) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700391 return false;
392 }
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700393 verbose_startup_ = options->IsVerbose("startup");
394 if (IsVerboseStartup()) {
395 LOG(INFO) << "Runtime::Init -verbose:startup enabled";
396 }
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700397
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700398 host_prefix_ = options->host_prefix_;
399 boot_class_path_ = options->boot_class_path_;
400 class_path_ = options->class_path_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700401 properties_ = options->properties_;
402
Elliott Hughes0af55432011-08-17 18:37:28 -0700403 vfprintf_ = options->hook_vfprintf_;
404 exit_ = options->hook_exit_;
405 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700406
Elliott Hughesbe759c62011-09-08 19:38:21 -0700407 default_stack_size_ = options->stack_size_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700408
Elliott Hughes14357e82011-09-26 10:42:15 -0700409 thread_list_ = new ThreadList(options->IsVerbose("thread"));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700410 intern_table_ = new InternTable;
411
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700412 Heap::Init(options->heap_initial_size_, options->heap_maximum_size_, 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 Carlstrom58ae9412011-10-04 00:56:06 -0700424 CHECK_GE(Heap::GetSpaces().size(), 1U);
425 class_linker_ = ((Heap::GetSpaces()[0]->IsImageSpace())
426 ? ClassLinker::Create(intern_table_)
427 : ClassLinker::Create(options->boot_class_path_, intern_table_));
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700428
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700429 if (IsVerboseStartup()) {
430 LOG(INFO) << "Runtime::Init exiting";
431 }
Carl Shapiro1fb86202011-06-27 17:43:13 -0700432 return true;
433}
434
Elliott Hughes038a8062011-09-18 14:12:41 -0700435void Runtime::InitNativeMethods() {
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700436 if (IsVerboseStartup()) {
437 LOG(INFO) << "Runtime::InitNativeMethods entering";
438 }
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700439 Thread* self = Thread::Current();
440 JNIEnv* env = self->GetJniEnv();
441
Elliott Hughes418d20f2011-09-22 14:00:39 -0700442 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Brian Carlstromaded5f72011-10-07 17:15:04 -0700443 CHECK_EQ(self->GetState(), Thread::kNative);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700444
Elliott Hughes418d20f2011-09-22 14:00:39 -0700445 // First set up JniConstants, which is used by both the runtime's built-in native
446 // methods and libcore.
Elliott Hughesfea966e2011-09-22 10:26:20 -0700447 JniConstants::init(env);
448
Elliott Hughes418d20f2011-09-22 14:00:39 -0700449 // Then set up the native methods provided by the runtime itself.
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700450 RegisterRuntimeNativeMethods(env);
451
Elliott Hughes418d20f2011-09-22 14:00:39 -0700452 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
453 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
454 // the library that implements System.loadLibrary!
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700455 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700456 if (IsVerboseStartup()) {
457 LOG(INFO) << "Runtime::InitNativeMethods exiting";
458 }
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700459}
460
461void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
462#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700463 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700464 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700465 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700466 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700467 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700468 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700469 REGISTER(register_java_lang_Object);
470 REGISTER(register_java_lang_Runtime);
471 REGISTER(register_java_lang_String);
472 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700473 REGISTER(register_java_lang_Thread);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700474 REGISTER(register_java_lang_Throwable);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700475 REGISTER(register_java_lang_VMClassLoader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700476 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700477 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700478 REGISTER(register_java_lang_reflect_Field);
479 REGISTER(register_java_lang_reflect_Method);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700480 //REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700481 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700482 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700483 //REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700484 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700485#undef REGISTER
486}
487
Elliott Hughes8daa0922011-09-11 13:46:25 -0700488void Runtime::Dump(std::ostream& os) {
Elliott Hughese27955c2011-08-26 15:21:24 -0700489 // TODO: dump other runtime statistics?
490 os << "Loaded classes: " << class_linker_->NumLoadedClasses() << "\n";
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700491 os << "Intern table size: " << GetInternTable()->Size() << "\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700492 // LOGV("VM stats: meth=%d ifld=%d sfld=%d linear=%d",
493 // gDvm.numDeclaredMethods,
494 // gDvm.numDeclaredInstFields,
495 // gDvm.numDeclaredStaticFields,
496 // gDvm.pBootLoaderAlloc->curOffset);
497 // LOGI("GC precise methods: %d", dvmPointerSetGetCount(gDvm.preciseMethods));
Elliott Hughes42ee1422011-09-06 12:33:32 -0700498 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700499
500 thread_list_->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700501}
502
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700503void Runtime::SetStatsEnabled(bool new_state) {
504 if (new_state == true) {
505 GetStats()->Clear(~0);
506 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
507 Thread::Current()->GetStats()->Clear(~0);
508 }
509 stats_enabled_ = new_state;
510}
511
512void Runtime::ResetStats(int kinds) {
513 GetStats()->Clear(kinds & 0xffff);
514 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
515 Thread::Current()->GetStats()->Clear(kinds >> 16);
516}
517
518RuntimeStats* Runtime::GetStats() {
519 return &stats_;
520}
521
522int32_t Runtime::GetStat(int kind) {
523 RuntimeStats* stats;
524 if (kind < (1<<16)) {
525 stats = GetStats();
526 } else {
527 stats = Thread::Current()->GetStats();
528 kind >>= 16;
529 }
530 switch (kind) {
531 case KIND_ALLOCATED_OBJECTS:
532 return stats->allocated_objects;
533 case KIND_ALLOCATED_BYTES:
534 return stats->allocated_bytes;
535 case KIND_FREED_OBJECTS:
536 return stats->freed_objects;
537 case KIND_FREED_BYTES:
538 return stats->freed_bytes;
539 case KIND_GC_INVOCATIONS:
540 return stats->gc_for_alloc_count;
541 case KIND_CLASS_INIT_COUNT:
542 return stats->class_init_count;
543 case KIND_CLASS_INIT_TIME:
544 // Convert ns to us, reduce to 32 bits.
545 return (int) (stats->class_init_time_ns / 1000);
546 case KIND_EXT_ALLOCATED_OBJECTS:
547 case KIND_EXT_ALLOCATED_BYTES:
548 case KIND_EXT_FREED_OBJECTS:
549 case KIND_EXT_FREED_BYTES:
550 return 0; // backward compatibility
551 default:
552 CHECK(false);
553 return -1; // unreachable
554 }
555}
556
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700557void Runtime::BlockSignals() {
558 sigset_t sigset;
559 if (sigemptyset(&sigset) == -1) {
560 PLOG(FATAL) << "sigemptyset failed";
561 }
562 if (sigaddset(&sigset, SIGPIPE) == -1) {
563 PLOG(ERROR) << "sigaddset SIGPIPE failed";
564 }
565 // SIGQUIT is used to dump the runtime's state (including stack traces).
566 if (sigaddset(&sigset, SIGQUIT) == -1) {
567 PLOG(ERROR) << "sigaddset SIGQUIT failed";
568 }
569 // SIGUSR1 is used to initiate a heap dump.
570 if (sigaddset(&sigset, SIGUSR1) == -1) {
571 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
572 }
573 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
574}
575
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700576void Runtime::AttachCurrentThread(const char* name, bool as_daemon) {
577 Thread::Attach(instance_, name, as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700578}
579
Elliott Hughesd92bec42011-09-02 17:04:36 -0700580void Runtime::DetachCurrentThread() {
Elliott Hughes038a8062011-09-18 14:12:41 -0700581 // TODO: check we're not calling DetachCurrentThread from a call stack that
582 // includes managed frames. (It's only valid if the stack is all-native.)
Elliott Hughes02b48d12011-09-07 17:15:51 -0700583 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700584}
585
Brian Carlstrome24fa612011-09-29 00:53:55 -0700586void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
587 class_linker_->VisitRoots(visitor, arg);
588 intern_table_->VisitRoots(visitor, arg);
589 java_vm_->VisitRoots(visitor, arg);
590 thread_list_->VisitRoots(visitor, arg);
591 visitor(jni_stub_array_, arg);
592 visitor(abstract_method_error_stub_array_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700593 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
594 visitor(resolution_stub_array_[i], arg);
595 }
596 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
597 visitor(callee_save_method_[i], arg);
598 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700599
600 //(*visitor)(&gDvm.outOfMemoryObj, 0, ROOT_VM_INTERNAL, arg);
601 //(*visitor)(&gDvm.internalErrorObj, 0, ROOT_VM_INTERNAL, arg);
602 //(*visitor)(&gDvm.noClassDefFoundErrorObj, 0, ROOT_VM_INTERNAL, arg);
603 UNIMPLEMENTED(WARNING) << "some roots not marked";
604}
605
606bool Runtime::HasJniStubArray() const {
607 return jni_stub_array_ != NULL;
608}
609
610ByteArray* Runtime::GetJniStubArray() const {
611 CHECK(jni_stub_array_ != NULL);
612 return jni_stub_array_;
613}
614
615void Runtime::SetJniStubArray(ByteArray* jni_stub_array) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700616 CHECK(jni_stub_array != NULL) << " jni_stub_array=" << jni_stub_array;
617 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array)
618 << "jni_stub_array_=" << jni_stub_array_ << " jni_stub_array=" << jni_stub_array;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700619 jni_stub_array_ = jni_stub_array;
620}
621
622bool Runtime::HasAbstractMethodErrorStubArray() const {
623 return abstract_method_error_stub_array_ != NULL;
624}
625
626ByteArray* Runtime::GetAbstractMethodErrorStubArray() const {
627 CHECK(abstract_method_error_stub_array_ != NULL);
628 return abstract_method_error_stub_array_;
629}
630
631void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) {
632 CHECK(abstract_method_error_stub_array != NULL);
633 CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array);
634 abstract_method_error_stub_array_ = abstract_method_error_stub_array;
635}
636
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700637
638Runtime::TrampolineType Runtime::GetTrampolineType(Method* method) {
639 if (method == NULL) {
640 return Runtime::kUnknownMethod;
641 } else if (method->IsStatic()) {
642 return Runtime::kStaticMethod;
643 } else {
644 return Runtime::kInstanceMethod;
645 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700646}
647
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700648bool Runtime::HasResolutionStubArray(TrampolineType type) const {
649 return resolution_stub_array_[type] != NULL;
Ian Rogersad25ac52011-10-04 19:13:33 -0700650}
651
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700652ByteArray* Runtime::GetResolutionStubArray(TrampolineType type) const {
653 CHECK(HasResolutionStubArray(type));
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700654 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastTrampolineMethodType));
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700655 return resolution_stub_array_[type];
656}
657
658void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700659 CHECK(resolution_stub_array != NULL);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700660 CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array);
661 resolution_stub_array_[type] = resolution_stub_array;
Ian Rogersad25ac52011-10-04 19:13:33 -0700662}
663
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700664Method* Runtime::CreateCalleeSaveMethod(InstructionSet insns, CalleeSaveType type) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700665 Class* method_class = Method::GetMethodClass();
666 Method* method = down_cast<Method*>(method_class->AllocObject());
667 method->SetDeclaringClass(method_class);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700668 const char* name;
669 if (type == kSaveAll) {
670 name = "$$$callee_save_method$$$";
671 } else if (type == kRefsOnly) {
672 name = "$$$refs_only_callee_save_method$$$";
673 } else {
674 DCHECK(type == kRefsAndArgs);
675 name = "$$$refs_and_args_callee_save_method$$$";
676 }
677 method->SetName(intern_table_->InternStrong(name));
Ian Rogersff1ed472011-09-20 13:46:24 -0700678 method->SetSignature(intern_table_->InternStrong("()V"));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700679 method->SetCode(NULL);
Ian Rogersff1ed472011-09-20 13:46:24 -0700680 if ((insns == kThumb2) || (insns == kArm)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700681 uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6) | (1 << art::arm::R7) |
682 (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11);
683 uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3);
684 uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9);
685 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills :0) |
686 (type == kSaveAll ? all_spills :0) | (1 << art::arm::LR);
687 uint32_t fp_all_spills = (1 << art::arm::S0) | (1 << art::arm::S1) | (1 << art::arm::S2) |
688 (1 << art::arm::S3) | (1 << art::arm::S4) | (1 << art::arm::S5) |
689 (1 << art::arm::S6) | (1 << art::arm::S7) | (1 << art::arm::S8) |
690 (1 << art::arm::S9) | (1 << art::arm::S10) | (1 << art::arm::S11) |
691 (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) |
692 (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) |
693 (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) |
694 (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) |
695 (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) |
696 (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
697 (1 << art::arm::S30) | (1 << art::arm::S31);
698 uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
699 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
700 __builtin_popcount(fp_spills) /* fprs */ +
701 1 /* Method* */) * kPointerSize, kStackAlignment);
Ian Rogers15fdb8c2011-09-25 15:45:07 -0700702 method->SetFrameSizeInBytes(frame_size);
703 method->SetReturnPcOffsetInBytes(frame_size - kPointerSize);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700704 method->SetCoreSpillMask(core_spills);
705 method->SetFpSpillMask(fp_spills);
Ian Rogersff1ed472011-09-20 13:46:24 -0700706 } else if (insns == kX86) {
707 method->SetFrameSizeInBytes(32);
708 method->SetReturnPcOffsetInBytes(28);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700709 method->SetCoreSpillMask((1 << art::x86::EBX) | (1 << art::x86::EBP) | (1 << art::x86::ESI) |
Ian Rogersff1ed472011-09-20 13:46:24 -0700710 (1 << art::x86::EDI));
711 method->SetFpSpillMask(0);
712 } else {
713 UNIMPLEMENTED(FATAL);
714 }
715 return method;
716}
717
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700718bool Runtime::HasCalleeSaveMethod(CalleeSaveType type) const {
719 return callee_save_method_[type] != NULL;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700720}
721
Brian Carlstrome24fa612011-09-29 00:53:55 -0700722// Returns a special method that describes all callee saves being spilled to the stack.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700723Method* Runtime::GetCalleeSaveMethod(CalleeSaveType type) const {
724 CHECK(HasCalleeSaveMethod(type));
725 return callee_save_method_[type];
Brian Carlstrome24fa612011-09-29 00:53:55 -0700726}
727
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700728void Runtime::SetCalleeSaveMethod(Method* method, CalleeSaveType type) {
729 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
730 callee_save_method_[type] = method;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700731}
732
Carl Shapiro1fb86202011-06-27 17:43:13 -0700733} // namespace art