blob: 5d2689db94728f9e938694b312cca2a385a4d60d [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"
Brian Carlstrome24fa612011-09-29 00:53:55 -070013#include "image.h"
Elliott Hughescf4c6c42011-09-01 15:16:42 -070014#include "intern_table.h"
Elliott Hughesc5f7c912011-08-18 14:00:42 -070015#include "jni_internal.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070016#include "oat_file.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070017#include "signal_catcher.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070018#include "space.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070019#include "thread.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070020#include "thread_list.h"
Carl Shapiro61e019d2011-07-14 16:53:09 -070021
Elliott Hughes90a33692011-08-30 13:27:07 -070022// TODO: this drags in cutil/log.h, which conflicts with our logging.h.
23#include "JniConstants.h"
24
Carl Shapiro1fb86202011-06-27 17:43:13 -070025namespace art {
26
Carl Shapiro2ed144c2011-07-26 16:52:08 -070027Runtime* Runtime::instance_ = NULL;
28
Elliott Hughesdcc24742011-09-07 14:02:44 -070029Runtime::Runtime()
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -070030 : verbose_startup_(false),
31 default_stack_size_(Thread::kDefaultStackSize),
Elliott Hughesdcc24742011-09-07 14:02:44 -070032 thread_list_(NULL),
33 intern_table_(NULL),
34 class_linker_(NULL),
35 signal_catcher_(NULL),
36 java_vm_(NULL),
Brian Carlstrom16192862011-09-12 17:50:06 -070037 jni_stub_array_(NULL),
Brian Carlstrome24fa612011-09-29 00:53:55 -070038 abstract_method_error_stub_array_(NULL),
Ian Rogersff1ed472011-09-20 13:46:24 -070039 callee_save_method_(NULL),
Elliott Hughesdcc24742011-09-07 14:02:44 -070040 started_(false),
41 vfprintf_(NULL),
42 exit_(NULL),
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070043 abort_(NULL),
44 stats_enabled_(false) {
Ian Rogersad25ac52011-10-04 19:13:33 -070045 resolution_stub_array_[0] = NULL;
46 resolution_stub_array_[1] = NULL;
Elliott Hughesdcc24742011-09-07 14:02:44 -070047}
48
Carl Shapiro61e019d2011-07-14 16:53:09 -070049Runtime::~Runtime() {
Elliott Hughes5fe594f2011-09-08 12:33:17 -070050 // Make sure our internal threads are dead before we start tearing down things they're using.
51 delete signal_catcher_;
Elliott Hughes038a8062011-09-18 14:12:41 -070052 // TODO: GC thread.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070053
Elliott Hughes038a8062011-09-18 14:12:41 -070054 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
Elliott Hughes93e74e82011-09-13 11:07:03 -070055 delete thread_list_;
56
Carl Shapiro61e019d2011-07-14 16:53:09 -070057 delete class_linker_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070058 Heap::Destroy();
Elliott Hughescf4c6c42011-09-01 15:16:42 -070059 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070060 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -070061 Thread::Shutdown();
Carl Shapiro4acf4642011-07-26 18:54:13 -070062 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070063 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -070064 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -070065}
66
Elliott Hughesffe67362011-07-17 12:09:27 -070067void Runtime::Abort(const char* file, int line) {
68 // Get any pending output out of the way.
69 fflush(NULL);
70
71 // Many people have difficulty distinguish aborts from crashes,
72 // so be explicit.
73 LogMessage(file, line, ERROR, -1).stream() << "Runtime aborting...";
74
Elliott Hughesffe67362011-07-17 12:09:27 -070075 // Perform any platform-specific pre-abort actions.
76 PlatformAbort(file, line);
77
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070078 // use abort hook if we have one
79 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
80 Runtime::Current()->abort_();
81 // notreached
82 }
83
Elliott Hughesffe67362011-07-17 12:09:27 -070084 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -070085 // receive SIGABRT. debuggerd dumps the stack trace of the main
86 // thread, whether or not that was the thread that failed. By
87 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -070088 // fault in the current thread, and get a useful log from debuggerd.
89 // We can also trivially tell the difference between a VM crash and
90 // a deliberate abort by looking at the fault address.
91 *reinterpret_cast<char*>(0xdeadd00d) = 38;
92 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -070093 // notreached
94}
95
Elliott Hughesbf86d042011-08-31 17:53:14 -070096void Runtime::CallExitHook(jint status) {
97 if (exit_ != NULL) {
98 ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative);
99 exit_(status);
100 LOG(WARNING) << "Exit hook returned instead of exiting!";
101 }
102}
103
Brian Carlstrom8a436592011-08-15 21:27:23 -0700104// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
105// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
106// [gG] gigabytes.
107//
108// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700109// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
110// of 1024.
111//
112// The spec says the -Xmx and -Xms options must be multiples of 1024. It
113// doesn't say anything about -Xss.
114//
115// Returns 0 (a useless size) if "s" is malformed or specifies a low or
116// non-evenly-divisible value.
117//
118size_t ParseMemoryOption(const char *s, size_t div) {
119 // strtoul accepts a leading [+-], which we don't want,
120 // so make sure our string starts with a decimal digit.
121 if (isdigit(*s)) {
122 const char *s2;
123 size_t val = strtoul(s, (char **)&s2, 10);
124 if (s2 != s) {
125 // s2 should be pointing just after the number.
126 // If this is the end of the string, the user
127 // has specified a number of bytes. Otherwise,
128 // there should be exactly one more character
129 // that specifies a multiplier.
130 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700131 // The remainder of the string is either a single multiplier
132 // character, or nothing to indicate that the value is in
133 // bytes.
134 char c = *s2++;
135 if (*s2 == '\0') {
136 size_t mul;
137 if (c == '\0') {
138 mul = 1;
139 } else if (c == 'k' || c == 'K') {
140 mul = KB;
141 } else if (c == 'm' || c == 'M') {
142 mul = MB;
143 } else if (c == 'g' || c == 'G') {
144 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700145 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700146 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700147 return 0;
148 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700149
150 if (val <= std::numeric_limits<size_t>::max() / mul) {
151 val *= mul;
152 } else {
153 // Clamp to a multiple of 1024.
154 val = std::numeric_limits<size_t>::max() & ~(1024-1);
155 }
156 } else {
157 // There's more than one character after the numeric part.
158 return 0;
159 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700160 }
161 // The man page says that a -Xm value must be a multiple of 1024.
162 if (val % div == 0) {
163 return val;
164 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700165 }
166 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700167 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700168}
169
Elliott Hughes0af55432011-08-17 18:37:28 -0700170void LoadJniLibrary(JavaVMExt* vm, const char* name) {
171 // TODO: OS_SHARED_LIB_FORMAT_STR
172 std::string mapped_name(StringPrintf("lib%s.so", name));
Elliott Hughes75770752011-08-24 17:52:38 -0700173 std::string reason;
174 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700175 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
176 << reason;
177 }
178}
179
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700180Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700181 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700182 const char* boot_class_path = getenv("BOOTCLASSPATH");
183 if (boot_class_path != NULL) {
184 parsed->boot_class_path_ = getenv("BOOTCLASSPATH");
185 }
186 const char* class_path = getenv("CLASSPATH");
187 if (class_path != NULL) {
188 parsed->class_path_ = getenv("CLASSPATH");
189 }
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700190#ifdef NDEBUG
Elliott Hughes5174fe62011-08-23 15:12:35 -0700191 // -Xcheck:jni is off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700192 parsed->check_jni_ = false;
193#else
194 // ...but on by default in debug builds.
195 parsed->check_jni_ = true;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700196#endif
Elliott Hughes85d15452011-09-16 17:33:01 -0700197
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700198 parsed->heap_initial_size_ = Heap::kInitialSize;
199 parsed->heap_maximum_size_ = Heap::kMaximumSize;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700200 parsed->stack_size_ = Thread::kDefaultStackSize;
201
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700202 parsed->hook_vfprintf_ = vfprintf;
203 parsed->hook_exit_ = exit;
204 parsed->hook_abort_ = abort;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700205
206 for (size_t i = 0; i < options.size(); ++i) {
207 const StringPiece& option = options[i].first;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700208 if (false) {
209 LOG(INFO) << "option[" << i << "]=" << option;
210 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700211 if (option.starts_with("-Xbootclasspath:")) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700212 parsed->boot_class_path_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700213 } else if (option == "-classpath" || option == "-cp") {
214 // TODO: support -Djava.class.path
215 i++;
216 if (i == options.size()) {
217 // TODO: usage
218 LOG(FATAL) << "Missing required class path value for " << option;
219 return NULL;
220 }
221 const StringPiece& value = options[i].first;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700222 parsed->class_path_ = value.data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700223 } else if (option.starts_with("-Ximage:")) {
224 parsed->images_.push_back(option.substr(strlen("-Ximage:")).data());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700225 } else if (option.starts_with("-Xcheck:jni")) {
226 parsed->check_jni_ = true;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700227 } else if (option.starts_with("-Xms")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700228 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024);
229 if (size == 0) {
230 if (ignore_unrecognized) {
231 continue;
232 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700233 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700234 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700235 return NULL;
236 }
237 parsed->heap_initial_size_ = size;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700238 } else if (option.starts_with("-Xmx")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700239 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024);
240 if (size == 0) {
241 if (ignore_unrecognized) {
242 continue;
243 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700244 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700245 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700246 return NULL;
247 }
248 parsed->heap_maximum_size_ = size;
249 } else if (option.starts_with("-Xss")) {
250 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1);
251 if (size == 0) {
252 if (ignore_unrecognized) {
253 continue;
254 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700255 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700256 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700257 return NULL;
258 }
259 parsed->stack_size_ = size;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700260 } else if (option.starts_with("-D")) {
261 parsed->properties_.push_back(option.substr(strlen("-D")).data());
Elliott Hughesa0957642011-09-02 14:27:33 -0700262 } else if (option.starts_with("-Xjnitrace:")) {
263 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:")).data();
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700264 } else if (option.starts_with("-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700265 std::vector<std::string> verbose_options;
Elliott Hughes34023802011-08-30 12:06:17 -0700266 Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700267 for (size_t i = 0; i < verbose_options.size(); ++i) {
268 parsed->verbose_.insert(verbose_options[i]);
269 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700270 } else if (option == "vfprintf") {
271 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
272 } else if (option == "exit") {
273 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
274 } else if (option == "abort") {
275 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700276 } else if (option == "host-prefix") {
277 parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700278 } else {
279 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700280 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700281 LOG(ERROR) << "Unrecognized option " << option;
282 // TODO: this should exit, but for now tolerate unknown options
283 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700284 }
285 }
286 }
287
Elliott Hughes85d15452011-09-16 17:33:01 -0700288 LOG(INFO) << "CheckJNI is " << (parsed->check_jni_ ? "on" : "off");
289
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700290 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700291}
292
293Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700294 // TODO: acquire a static mutex on Runtime to avoid racing.
295 if (Runtime::instance_ != NULL) {
296 return NULL;
297 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700298 instance_ = new Runtime;
299 if (!instance_->Init(options, ignore_unrecognized)) {
300 delete instance_;
301 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700302 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700303 return instance_;
304}
Elliott Hughes0af55432011-08-17 18:37:28 -0700305
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700306void Runtime::Start() {
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700307 if (IsVerboseStartup()) {
308 LOG(INFO) << "Runtime::Start entering";
309 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700310
311 CHECK(host_prefix_.empty()) << host_prefix_;
312
Elliott Hughes038a8062011-09-18 14:12:41 -0700313 InitNativeMethods();
Elliott Hughes85d15452011-09-16 17:33:01 -0700314
Elliott Hughes038a8062011-09-18 14:12:41 -0700315 Thread::FinishStartup();
Elliott Hughes85d15452011-09-16 17:33:01 -0700316
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700317 class_linker_->RunRootClinits();
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700318
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700319 // Class::AllocObject asserts that all objects allocated better be
320 // initialized after Runtime::IsStarted is true, so this needs to
321 // come after ClassLinker::RunRootClinits.
322 started_ = true;
323
Elliott Hughes85d15452011-09-16 17:33:01 -0700324 StartDaemonThreads();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700325
326 if (IsVerboseStartup()) {
327 LOG(INFO) << "Runtime::Start exiting";
328 }
Elliott Hughes85d15452011-09-16 17:33:01 -0700329}
330
331void Runtime::StartDaemonThreads() {
332 signal_catcher_ = new SignalCatcher;
333
Elliott Hughes719b3232011-09-25 17:42:19 -0700334 Thread* self = Thread::Current();
335 JNIEnv* env = self->GetJniEnv();
336 jclass c = env->FindClass("java/lang/Daemons");
Elliott Hughes85d15452011-09-16 17:33:01 -0700337 CHECK(c != NULL);
Elliott Hughes719b3232011-09-25 17:42:19 -0700338 jmethodID mid = env->GetStaticMethodID(c, "start", "()V");
339 CHECK(mid != NULL);
340 env->CallStaticVoidMethod(c, mid);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700341}
342
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700343bool Runtime::IsStarted() const {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700344 return started_;
345}
346
Elliott Hughes0af55432011-08-17 18:37:28 -0700347bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700348 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700349
Elliott Hughes90a33692011-08-30 13:27:07 -0700350 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
351 if (options.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700352 LOG(ERROR) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700353 return false;
354 }
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700355 verbose_startup_ = options->IsVerbose("startup");
356 if (IsVerboseStartup()) {
357 LOG(INFO) << "Runtime::Init -verbose:startup enabled";
358 }
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700359
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700360 host_prefix_ = options->host_prefix_;
361 boot_class_path_ = options->boot_class_path_;
362 class_path_ = options->class_path_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700363 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 Hughes14357e82011-09-26 10:42:15 -0700371 thread_list_ = new ThreadList(options->IsVerbose("thread"));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700372 intern_table_ = new InternTable;
373
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700374 Heap::Init(options->heap_initial_size_, options->heap_maximum_size_, options->images_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700375
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700376 BlockSignals();
377
Elliott Hughesa0957642011-09-02 14:27:33 -0700378 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700379
Elliott Hughesbe759c62011-09-08 19:38:21 -0700380 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700381
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700382 // ClassLinker needs an attached thread, but we can't fully attach a thread
383 // without creating objects.
384 Thread::Attach(this, "main", false);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700385
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700386 CHECK_GE(Heap::GetSpaces().size(), 1U);
387 class_linker_ = ((Heap::GetSpaces()[0]->IsImageSpace())
388 ? ClassLinker::Create(intern_table_)
389 : ClassLinker::Create(options->boot_class_path_, intern_table_));
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700390
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700391 if (IsVerboseStartup()) {
392 LOG(INFO) << "Runtime::Init exiting";
393 }
Carl Shapiro1fb86202011-06-27 17:43:13 -0700394 return true;
395}
396
Elliott Hughes038a8062011-09-18 14:12:41 -0700397void Runtime::InitNativeMethods() {
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700398 if (IsVerboseStartup()) {
399 LOG(INFO) << "Runtime::InitNativeMethods entering";
400 }
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700401 Thread* self = Thread::Current();
402 JNIEnv* env = self->GetJniEnv();
403
Elliott Hughes418d20f2011-09-22 14:00:39 -0700404 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700405 ScopedThreadStateChange tsc(self, Thread::kNative);
406
Elliott Hughes418d20f2011-09-22 14:00:39 -0700407 // First set up JniConstants, which is used by both the runtime's built-in native
408 // methods and libcore.
Elliott Hughesfea966e2011-09-22 10:26:20 -0700409 JniConstants::init(env);
410
Elliott Hughes418d20f2011-09-22 14:00:39 -0700411 // Then set up the native methods provided by the runtime itself.
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700412 RegisterRuntimeNativeMethods(env);
413
Elliott Hughes418d20f2011-09-22 14:00:39 -0700414 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
415 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
416 // the library that implements System.loadLibrary!
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700417 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700418 if (IsVerboseStartup()) {
419 LOG(INFO) << "Runtime::InitNativeMethods exiting";
420 }
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700421}
422
423void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
424#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700425 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700426 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700427 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700428 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700429 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700430 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700431 REGISTER(register_java_lang_Object);
432 REGISTER(register_java_lang_Runtime);
433 REGISTER(register_java_lang_String);
434 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700435 REGISTER(register_java_lang_Thread);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700436 REGISTER(register_java_lang_Throwable);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700437 REGISTER(register_java_lang_VMClassLoader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700438 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700439 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700440 REGISTER(register_java_lang_reflect_Field);
441 REGISTER(register_java_lang_reflect_Method);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700442 //REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700443 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700444 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700445 //REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700446 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700447#undef REGISTER
448}
449
Elliott Hughes8daa0922011-09-11 13:46:25 -0700450void Runtime::Dump(std::ostream& os) {
Elliott Hughese27955c2011-08-26 15:21:24 -0700451 // TODO: dump other runtime statistics?
452 os << "Loaded classes: " << class_linker_->NumLoadedClasses() << "\n";
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700453 os << "Intern table size: " << GetInternTable()->Size() << "\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700454 // LOGV("VM stats: meth=%d ifld=%d sfld=%d linear=%d",
455 // gDvm.numDeclaredMethods,
456 // gDvm.numDeclaredInstFields,
457 // gDvm.numDeclaredStaticFields,
458 // gDvm.pBootLoaderAlloc->curOffset);
459 // LOGI("GC precise methods: %d", dvmPointerSetGetCount(gDvm.preciseMethods));
Elliott Hughes42ee1422011-09-06 12:33:32 -0700460 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700461
462 thread_list_->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700463}
464
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700465void Runtime::SetStatsEnabled(bool new_state) {
466 if (new_state == true) {
467 GetStats()->Clear(~0);
468 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
469 Thread::Current()->GetStats()->Clear(~0);
470 }
471 stats_enabled_ = new_state;
472}
473
474void Runtime::ResetStats(int kinds) {
475 GetStats()->Clear(kinds & 0xffff);
476 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
477 Thread::Current()->GetStats()->Clear(kinds >> 16);
478}
479
480RuntimeStats* Runtime::GetStats() {
481 return &stats_;
482}
483
484int32_t Runtime::GetStat(int kind) {
485 RuntimeStats* stats;
486 if (kind < (1<<16)) {
487 stats = GetStats();
488 } else {
489 stats = Thread::Current()->GetStats();
490 kind >>= 16;
491 }
492 switch (kind) {
493 case KIND_ALLOCATED_OBJECTS:
494 return stats->allocated_objects;
495 case KIND_ALLOCATED_BYTES:
496 return stats->allocated_bytes;
497 case KIND_FREED_OBJECTS:
498 return stats->freed_objects;
499 case KIND_FREED_BYTES:
500 return stats->freed_bytes;
501 case KIND_GC_INVOCATIONS:
502 return stats->gc_for_alloc_count;
503 case KIND_CLASS_INIT_COUNT:
504 return stats->class_init_count;
505 case KIND_CLASS_INIT_TIME:
506 // Convert ns to us, reduce to 32 bits.
507 return (int) (stats->class_init_time_ns / 1000);
508 case KIND_EXT_ALLOCATED_OBJECTS:
509 case KIND_EXT_ALLOCATED_BYTES:
510 case KIND_EXT_FREED_OBJECTS:
511 case KIND_EXT_FREED_BYTES:
512 return 0; // backward compatibility
513 default:
514 CHECK(false);
515 return -1; // unreachable
516 }
517}
518
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700519void Runtime::BlockSignals() {
520 sigset_t sigset;
521 if (sigemptyset(&sigset) == -1) {
522 PLOG(FATAL) << "sigemptyset failed";
523 }
524 if (sigaddset(&sigset, SIGPIPE) == -1) {
525 PLOG(ERROR) << "sigaddset SIGPIPE failed";
526 }
527 // SIGQUIT is used to dump the runtime's state (including stack traces).
528 if (sigaddset(&sigset, SIGQUIT) == -1) {
529 PLOG(ERROR) << "sigaddset SIGQUIT failed";
530 }
531 // SIGUSR1 is used to initiate a heap dump.
532 if (sigaddset(&sigset, SIGUSR1) == -1) {
533 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
534 }
535 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
536}
537
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700538void Runtime::AttachCurrentThread(const char* name, bool as_daemon) {
539 Thread::Attach(instance_, name, as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700540}
541
Elliott Hughesd92bec42011-09-02 17:04:36 -0700542void Runtime::DetachCurrentThread() {
Elliott Hughes038a8062011-09-18 14:12:41 -0700543 // TODO: check we're not calling DetachCurrentThread from a call stack that
544 // includes managed frames. (It's only valid if the stack is all-native.)
Elliott Hughes02b48d12011-09-07 17:15:51 -0700545 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700546}
547
Brian Carlstrome24fa612011-09-29 00:53:55 -0700548void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
549 class_linker_->VisitRoots(visitor, arg);
550 intern_table_->VisitRoots(visitor, arg);
551 java_vm_->VisitRoots(visitor, arg);
552 thread_list_->VisitRoots(visitor, arg);
553 visitor(jni_stub_array_, arg);
554 visitor(abstract_method_error_stub_array_, arg);
Ian Rogersad25ac52011-10-04 19:13:33 -0700555 visitor(resolution_stub_array_[0], arg);
556 visitor(resolution_stub_array_[1], arg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700557 visitor(callee_save_method_, arg);
558
559 //(*visitor)(&gDvm.outOfMemoryObj, 0, ROOT_VM_INTERNAL, arg);
560 //(*visitor)(&gDvm.internalErrorObj, 0, ROOT_VM_INTERNAL, arg);
561 //(*visitor)(&gDvm.noClassDefFoundErrorObj, 0, ROOT_VM_INTERNAL, arg);
562 UNIMPLEMENTED(WARNING) << "some roots not marked";
563}
564
565bool Runtime::HasJniStubArray() const {
566 return jni_stub_array_ != NULL;
567}
568
569ByteArray* Runtime::GetJniStubArray() const {
570 CHECK(jni_stub_array_ != NULL);
571 return jni_stub_array_;
572}
573
574void Runtime::SetJniStubArray(ByteArray* jni_stub_array) {
575 CHECK(jni_stub_array != NULL);
576 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array);
577 jni_stub_array_ = jni_stub_array;
578}
579
580bool Runtime::HasAbstractMethodErrorStubArray() const {
581 return abstract_method_error_stub_array_ != NULL;
582}
583
584ByteArray* Runtime::GetAbstractMethodErrorStubArray() const {
585 CHECK(abstract_method_error_stub_array_ != NULL);
586 return abstract_method_error_stub_array_;
587}
588
589void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) {
590 CHECK(abstract_method_error_stub_array != NULL);
591 CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array);
592 abstract_method_error_stub_array_ = abstract_method_error_stub_array;
593}
594
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700595
596Runtime::TrampolineType Runtime::GetTrampolineType(Method* method) {
597 if (method == NULL) {
598 return Runtime::kUnknownMethod;
599 } else if (method->IsStatic()) {
600 return Runtime::kStaticMethod;
601 } else {
602 return Runtime::kInstanceMethod;
603 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700604}
605
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700606bool Runtime::HasResolutionStubArray(TrampolineType type) const {
607 return resolution_stub_array_[type] != NULL;
Ian Rogersad25ac52011-10-04 19:13:33 -0700608}
609
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700610ByteArray* Runtime::GetResolutionStubArray(TrampolineType type) const {
611 CHECK(HasResolutionStubArray(type));
612 return resolution_stub_array_[type];
613}
614
615void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700616 CHECK(resolution_stub_array != NULL);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700617 CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array);
618 resolution_stub_array_[type] = resolution_stub_array;
Ian Rogersad25ac52011-10-04 19:13:33 -0700619}
620
Ian Rogersff1ed472011-09-20 13:46:24 -0700621Method* Runtime::CreateCalleeSaveMethod(InstructionSet insns) {
622 Class* method_class = Method::GetMethodClass();
623 Method* method = down_cast<Method*>(method_class->AllocObject());
624 method->SetDeclaringClass(method_class);
625 method->SetName(intern_table_->InternStrong("$$$callee_save_method$$$"));
626 method->SetSignature(intern_table_->InternStrong("()V"));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700627 method->SetCodeArray(NULL, insns);
Ian Rogersff1ed472011-09-20 13:46:24 -0700628 if ((insns == kThumb2) || (insns == kArm)) {
Ian Rogers15fdb8c2011-09-25 15:45:07 -0700629 size_t frame_size = (12 /* gprs */ + 32 /* fprs */ + 4 /* data */) * kPointerSize;
630 method->SetFrameSizeInBytes(frame_size);
631 method->SetReturnPcOffsetInBytes(frame_size - kPointerSize);
Ian Rogersff1ed472011-09-20 13:46:24 -0700632 method->SetCoreSpillMask((1 << art::arm::R1) |
633 (1 << art::arm::R2) |
634 (1 << art::arm::R3) |
635 (1 << art::arm::R4) |
636 (1 << art::arm::R5) |
637 (1 << art::arm::R6) |
638 (1 << art::arm::R7) |
639 (1 << art::arm::R8) |
640 (1 << art::arm::R9) |
641 (1 << art::arm::R10) |
642 (1 << art::arm::R11) |
643 (1 << art::arm::LR));
Ian Rogers15fdb8c2011-09-25 15:45:07 -0700644 method->SetFpSpillMask((1 << art::arm::S0) |
645 (1 << art::arm::S1) |
646 (1 << art::arm::S2) |
647 (1 << art::arm::S3) |
648 (1 << art::arm::S4) |
649 (1 << art::arm::S5) |
650 (1 << art::arm::S6) |
651 (1 << art::arm::S7) |
652 (1 << art::arm::S8) |
653 (1 << art::arm::S9) |
654 (1 << art::arm::S10) |
655 (1 << art::arm::S11) |
656 (1 << art::arm::S12) |
657 (1 << art::arm::S13) |
658 (1 << art::arm::S14) |
659 (1 << art::arm::S15) |
660 (1 << art::arm::S16) |
661 (1 << art::arm::S17) |
662 (1 << art::arm::S18) |
663 (1 << art::arm::S19) |
664 (1 << art::arm::S20) |
665 (1 << art::arm::S21) |
666 (1 << art::arm::S22) |
667 (1 << art::arm::S23) |
668 (1 << art::arm::S24) |
669 (1 << art::arm::S25) |
670 (1 << art::arm::S26) |
671 (1 << art::arm::S27) |
672 (1 << art::arm::S28) |
673 (1 << art::arm::S29) |
674 (1 << art::arm::S30) |
675 (1 << art::arm::S31));
Ian Rogersff1ed472011-09-20 13:46:24 -0700676 } else if (insns == kX86) {
677 method->SetFrameSizeInBytes(32);
678 method->SetReturnPcOffsetInBytes(28);
679 method->SetCoreSpillMask((1 << art::x86::EBX) |
680 (1 << art::x86::EBP) |
681 (1 << art::x86::ESI) |
682 (1 << art::x86::EDI));
683 method->SetFpSpillMask(0);
684 } else {
685 UNIMPLEMENTED(FATAL);
686 }
687 return method;
688}
689
Brian Carlstrome24fa612011-09-29 00:53:55 -0700690bool Runtime::HasCalleeSaveMethod() const {
691 return callee_save_method_ != NULL;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700692}
693
Brian Carlstrome24fa612011-09-29 00:53:55 -0700694// Returns a special method that describes all callee saves being spilled to the stack.
695Method* Runtime::GetCalleeSaveMethod() const {
696 CHECK(callee_save_method_ != NULL);
697 return callee_save_method_;
698}
699
700void Runtime::SetCalleeSaveMethod(Method* method) {
701 callee_save_method_ = method;
702}
703
704
Carl Shapiro1fb86202011-06-27 17:43:13 -0700705} // namespace art