blob: e46cf88fd7ceae84394e8dc7e6510c47cf60a7f0 [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),
Elliott Hughesdcc24742011-09-07 14:02:44 -070039 started_(false),
40 vfprintf_(NULL),
41 exit_(NULL),
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070042 abort_(NULL),
43 stats_enabled_(false) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -070044 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
45 resolution_stub_array_[i] = NULL;
46 }
47 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
48 callee_save_method_[i] = NULL;
49 }
Elliott Hughesdcc24742011-09-07 14:02:44 -070050}
51
Carl Shapiro61e019d2011-07-14 16:53:09 -070052Runtime::~Runtime() {
Elliott Hughes5fe594f2011-09-08 12:33:17 -070053 // Make sure our internal threads are dead before we start tearing down things they're using.
54 delete signal_catcher_;
Elliott Hughes038a8062011-09-18 14:12:41 -070055 // TODO: GC thread.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070056
Elliott Hughes038a8062011-09-18 14:12:41 -070057 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
Elliott Hughes93e74e82011-09-13 11:07:03 -070058 delete thread_list_;
59
Carl Shapiro61e019d2011-07-14 16:53:09 -070060 delete class_linker_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070061 Heap::Destroy();
Elliott Hughescf4c6c42011-09-01 15:16:42 -070062 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070063 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -070064 Thread::Shutdown();
Carl Shapiro4acf4642011-07-26 18:54:13 -070065 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070066 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -070067 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -070068}
69
Elliott Hughesffe67362011-07-17 12:09:27 -070070void Runtime::Abort(const char* file, int line) {
71 // Get any pending output out of the way.
72 fflush(NULL);
73
74 // Many people have difficulty distinguish aborts from crashes,
75 // so be explicit.
76 LogMessage(file, line, ERROR, -1).stream() << "Runtime aborting...";
77
Elliott Hughesffe67362011-07-17 12:09:27 -070078 // Perform any platform-specific pre-abort actions.
79 PlatformAbort(file, line);
80
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070081 // use abort hook if we have one
82 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
83 Runtime::Current()->abort_();
84 // notreached
85 }
86
Elliott Hughesffe67362011-07-17 12:09:27 -070087 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -070088 // receive SIGABRT. debuggerd dumps the stack trace of the main
89 // thread, whether or not that was the thread that failed. By
90 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -070091 // fault in the current thread, and get a useful log from debuggerd.
92 // We can also trivially tell the difference between a VM crash and
93 // a deliberate abort by looking at the fault address.
94 *reinterpret_cast<char*>(0xdeadd00d) = 38;
95 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -070096 // notreached
97}
98
Elliott Hughesbf86d042011-08-31 17:53:14 -070099void Runtime::CallExitHook(jint status) {
100 if (exit_ != NULL) {
101 ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative);
102 exit_(status);
103 LOG(WARNING) << "Exit hook returned instead of exiting!";
104 }
105}
106
Brian Carlstrom8a436592011-08-15 21:27:23 -0700107// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
108// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
109// [gG] gigabytes.
110//
111// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700112// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
113// of 1024.
114//
115// The spec says the -Xmx and -Xms options must be multiples of 1024. It
116// doesn't say anything about -Xss.
117//
118// Returns 0 (a useless size) if "s" is malformed or specifies a low or
119// non-evenly-divisible value.
120//
121size_t ParseMemoryOption(const char *s, size_t div) {
122 // strtoul accepts a leading [+-], which we don't want,
123 // so make sure our string starts with a decimal digit.
124 if (isdigit(*s)) {
125 const char *s2;
126 size_t val = strtoul(s, (char **)&s2, 10);
127 if (s2 != s) {
128 // s2 should be pointing just after the number.
129 // If this is the end of the string, the user
130 // has specified a number of bytes. Otherwise,
131 // there should be exactly one more character
132 // that specifies a multiplier.
133 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700134 // The remainder of the string is either a single multiplier
135 // character, or nothing to indicate that the value is in
136 // bytes.
137 char c = *s2++;
138 if (*s2 == '\0') {
139 size_t mul;
140 if (c == '\0') {
141 mul = 1;
142 } else if (c == 'k' || c == 'K') {
143 mul = KB;
144 } else if (c == 'm' || c == 'M') {
145 mul = MB;
146 } else if (c == 'g' || c == 'G') {
147 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700148 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700149 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700150 return 0;
151 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700152
153 if (val <= std::numeric_limits<size_t>::max() / mul) {
154 val *= mul;
155 } else {
156 // Clamp to a multiple of 1024.
157 val = std::numeric_limits<size_t>::max() & ~(1024-1);
158 }
159 } else {
160 // There's more than one character after the numeric part.
161 return 0;
162 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700163 }
164 // The man page says that a -Xm value must be a multiple of 1024.
165 if (val % div == 0) {
166 return val;
167 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700168 }
169 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700170 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700171}
172
Elliott Hughes0af55432011-08-17 18:37:28 -0700173void LoadJniLibrary(JavaVMExt* vm, const char* name) {
174 // TODO: OS_SHARED_LIB_FORMAT_STR
175 std::string mapped_name(StringPrintf("lib%s.so", name));
Elliott Hughes75770752011-08-24 17:52:38 -0700176 std::string reason;
177 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700178 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
179 << reason;
180 }
181}
182
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700183Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700184 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700185 const char* boot_class_path = getenv("BOOTCLASSPATH");
186 if (boot_class_path != NULL) {
187 parsed->boot_class_path_ = getenv("BOOTCLASSPATH");
188 }
189 const char* class_path = getenv("CLASSPATH");
190 if (class_path != NULL) {
191 parsed->class_path_ = getenv("CLASSPATH");
192 }
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700193#ifdef NDEBUG
Elliott Hughes5174fe62011-08-23 15:12:35 -0700194 // -Xcheck:jni is off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700195 parsed->check_jni_ = false;
196#else
197 // ...but on by default in debug builds.
198 parsed->check_jni_ = true;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700199#endif
Elliott Hughes85d15452011-09-16 17:33:01 -0700200
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700201 parsed->heap_initial_size_ = Heap::kInitialSize;
202 parsed->heap_maximum_size_ = Heap::kMaximumSize;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700203 parsed->stack_size_ = Thread::kDefaultStackSize;
204
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700205 parsed->hook_vfprintf_ = vfprintf;
206 parsed->hook_exit_ = exit;
207 parsed->hook_abort_ = abort;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700208
209 for (size_t i = 0; i < options.size(); ++i) {
210 const StringPiece& option = options[i].first;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700211 if (false) {
212 LOG(INFO) << "option[" << i << "]=" << option;
213 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700214 if (option.starts_with("-Xbootclasspath:")) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700215 parsed->boot_class_path_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700216 } else if (option == "-classpath" || option == "-cp") {
217 // TODO: support -Djava.class.path
218 i++;
219 if (i == options.size()) {
220 // TODO: usage
221 LOG(FATAL) << "Missing required class path value for " << option;
222 return NULL;
223 }
224 const StringPiece& value = options[i].first;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700225 parsed->class_path_ = value.data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700226 } else if (option.starts_with("-Ximage:")) {
227 parsed->images_.push_back(option.substr(strlen("-Ximage:")).data());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700228 } else if (option.starts_with("-Xcheck:jni")) {
229 parsed->check_jni_ = true;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700230 } else if (option.starts_with("-Xms")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700231 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024);
232 if (size == 0) {
233 if (ignore_unrecognized) {
234 continue;
235 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700236 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700237 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700238 return NULL;
239 }
240 parsed->heap_initial_size_ = size;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700241 } else if (option.starts_with("-Xmx")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700242 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024);
243 if (size == 0) {
244 if (ignore_unrecognized) {
245 continue;
246 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700247 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700248 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700249 return NULL;
250 }
251 parsed->heap_maximum_size_ = size;
252 } else if (option.starts_with("-Xss")) {
253 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1);
254 if (size == 0) {
255 if (ignore_unrecognized) {
256 continue;
257 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700258 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700259 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700260 return NULL;
261 }
262 parsed->stack_size_ = size;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700263 } else if (option.starts_with("-D")) {
264 parsed->properties_.push_back(option.substr(strlen("-D")).data());
Elliott Hughesa0957642011-09-02 14:27:33 -0700265 } else if (option.starts_with("-Xjnitrace:")) {
266 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:")).data();
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700267 } else if (option.starts_with("-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700268 std::vector<std::string> verbose_options;
Elliott Hughes34023802011-08-30 12:06:17 -0700269 Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700270 for (size_t i = 0; i < verbose_options.size(); ++i) {
271 parsed->verbose_.insert(verbose_options[i]);
272 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700273 } else if (option == "vfprintf") {
274 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
275 } else if (option == "exit") {
276 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
277 } else if (option == "abort") {
278 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700279 } else if (option == "host-prefix") {
280 parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700281 } else {
282 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700283 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700284 LOG(ERROR) << "Unrecognized option " << option;
285 // TODO: this should exit, but for now tolerate unknown options
286 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700287 }
288 }
289 }
290
Elliott Hughes85d15452011-09-16 17:33:01 -0700291 LOG(INFO) << "CheckJNI is " << (parsed->check_jni_ ? "on" : "off");
292
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700293 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700294}
295
296Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700297 // TODO: acquire a static mutex on Runtime to avoid racing.
298 if (Runtime::instance_ != NULL) {
299 return NULL;
300 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700301 instance_ = new Runtime;
302 if (!instance_->Init(options, ignore_unrecognized)) {
303 delete instance_;
304 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700305 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700306 return instance_;
307}
Elliott Hughes0af55432011-08-17 18:37:28 -0700308
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700309void Runtime::Start() {
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700310 if (IsVerboseStartup()) {
311 LOG(INFO) << "Runtime::Start entering";
312 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700313
314 CHECK(host_prefix_.empty()) << host_prefix_;
315
Elliott Hughes038a8062011-09-18 14:12:41 -0700316 InitNativeMethods();
Elliott Hughes85d15452011-09-16 17:33:01 -0700317
Elliott Hughes038a8062011-09-18 14:12:41 -0700318 Thread::FinishStartup();
Elliott Hughes85d15452011-09-16 17:33:01 -0700319
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700320 class_linker_->RunRootClinits();
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700321
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700322 // Class::AllocObject asserts that all objects allocated better be
323 // initialized after Runtime::IsStarted is true, so this needs to
324 // come after ClassLinker::RunRootClinits.
325 started_ = true;
326
Elliott Hughes85d15452011-09-16 17:33:01 -0700327 StartDaemonThreads();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700328
329 if (IsVerboseStartup()) {
330 LOG(INFO) << "Runtime::Start exiting";
331 }
Elliott Hughes85d15452011-09-16 17:33:01 -0700332}
333
334void Runtime::StartDaemonThreads() {
335 signal_catcher_ = new SignalCatcher;
336
Elliott Hughes719b3232011-09-25 17:42:19 -0700337 Thread* self = Thread::Current();
338 JNIEnv* env = self->GetJniEnv();
339 jclass c = env->FindClass("java/lang/Daemons");
Elliott Hughes85d15452011-09-16 17:33:01 -0700340 CHECK(c != NULL);
Elliott Hughes719b3232011-09-25 17:42:19 -0700341 jmethodID mid = env->GetStaticMethodID(c, "start", "()V");
342 CHECK(mid != NULL);
343 env->CallStaticVoidMethod(c, mid);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700344}
345
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700346bool Runtime::IsStarted() const {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700347 return started_;
348}
349
Elliott Hughes0af55432011-08-17 18:37:28 -0700350bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700351 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700352
Elliott Hughes90a33692011-08-30 13:27:07 -0700353 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
354 if (options.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700355 LOG(ERROR) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700356 return false;
357 }
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700358 verbose_startup_ = options->IsVerbose("startup");
359 if (IsVerboseStartup()) {
360 LOG(INFO) << "Runtime::Init -verbose:startup enabled";
361 }
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700362
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700363 host_prefix_ = options->host_prefix_;
364 boot_class_path_ = options->boot_class_path_;
365 class_path_ = options->class_path_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700366 properties_ = options->properties_;
367
Elliott Hughes0af55432011-08-17 18:37:28 -0700368 vfprintf_ = options->hook_vfprintf_;
369 exit_ = options->hook_exit_;
370 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700371
Elliott Hughesbe759c62011-09-08 19:38:21 -0700372 default_stack_size_ = options->stack_size_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700373
Elliott Hughes14357e82011-09-26 10:42:15 -0700374 thread_list_ = new ThreadList(options->IsVerbose("thread"));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700375 intern_table_ = new InternTable;
376
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700377 Heap::Init(options->heap_initial_size_, options->heap_maximum_size_, options->images_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700378
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700379 BlockSignals();
380
Elliott Hughesa0957642011-09-02 14:27:33 -0700381 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700382
Elliott Hughesbe759c62011-09-08 19:38:21 -0700383 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700384
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700385 // ClassLinker needs an attached thread, but we can't fully attach a thread
386 // without creating objects.
387 Thread::Attach(this, "main", false);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700388
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700389 CHECK_GE(Heap::GetSpaces().size(), 1U);
390 class_linker_ = ((Heap::GetSpaces()[0]->IsImageSpace())
391 ? ClassLinker::Create(intern_table_)
392 : ClassLinker::Create(options->boot_class_path_, intern_table_));
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700393
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700394 if (IsVerboseStartup()) {
395 LOG(INFO) << "Runtime::Init exiting";
396 }
Carl Shapiro1fb86202011-06-27 17:43:13 -0700397 return true;
398}
399
Elliott Hughes038a8062011-09-18 14:12:41 -0700400void Runtime::InitNativeMethods() {
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700401 if (IsVerboseStartup()) {
402 LOG(INFO) << "Runtime::InitNativeMethods entering";
403 }
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700404 Thread* self = Thread::Current();
405 JNIEnv* env = self->GetJniEnv();
406
Elliott Hughes418d20f2011-09-22 14:00:39 -0700407 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700408 ScopedThreadStateChange tsc(self, Thread::kNative);
409
Elliott Hughes418d20f2011-09-22 14:00:39 -0700410 // First set up JniConstants, which is used by both the runtime's built-in native
411 // methods and libcore.
Elliott Hughesfea966e2011-09-22 10:26:20 -0700412 JniConstants::init(env);
413
Elliott Hughes418d20f2011-09-22 14:00:39 -0700414 // Then set up the native methods provided by the runtime itself.
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700415 RegisterRuntimeNativeMethods(env);
416
Elliott Hughes418d20f2011-09-22 14:00:39 -0700417 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
418 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
419 // the library that implements System.loadLibrary!
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700420 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700421 if (IsVerboseStartup()) {
422 LOG(INFO) << "Runtime::InitNativeMethods exiting";
423 }
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700424}
425
426void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
427#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700428 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700429 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700430 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700431 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700432 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700433 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700434 REGISTER(register_java_lang_Object);
435 REGISTER(register_java_lang_Runtime);
436 REGISTER(register_java_lang_String);
437 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700438 REGISTER(register_java_lang_Thread);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700439 REGISTER(register_java_lang_Throwable);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700440 REGISTER(register_java_lang_VMClassLoader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700441 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700442 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700443 REGISTER(register_java_lang_reflect_Field);
444 REGISTER(register_java_lang_reflect_Method);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700445 //REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700446 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700447 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700448 //REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700449 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700450#undef REGISTER
451}
452
Elliott Hughes8daa0922011-09-11 13:46:25 -0700453void Runtime::Dump(std::ostream& os) {
Elliott Hughese27955c2011-08-26 15:21:24 -0700454 // TODO: dump other runtime statistics?
455 os << "Loaded classes: " << class_linker_->NumLoadedClasses() << "\n";
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700456 os << "Intern table size: " << GetInternTable()->Size() << "\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700457 // LOGV("VM stats: meth=%d ifld=%d sfld=%d linear=%d",
458 // gDvm.numDeclaredMethods,
459 // gDvm.numDeclaredInstFields,
460 // gDvm.numDeclaredStaticFields,
461 // gDvm.pBootLoaderAlloc->curOffset);
462 // LOGI("GC precise methods: %d", dvmPointerSetGetCount(gDvm.preciseMethods));
Elliott Hughes42ee1422011-09-06 12:33:32 -0700463 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700464
465 thread_list_->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700466}
467
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700468void Runtime::SetStatsEnabled(bool new_state) {
469 if (new_state == true) {
470 GetStats()->Clear(~0);
471 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
472 Thread::Current()->GetStats()->Clear(~0);
473 }
474 stats_enabled_ = new_state;
475}
476
477void Runtime::ResetStats(int kinds) {
478 GetStats()->Clear(kinds & 0xffff);
479 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
480 Thread::Current()->GetStats()->Clear(kinds >> 16);
481}
482
483RuntimeStats* Runtime::GetStats() {
484 return &stats_;
485}
486
487int32_t Runtime::GetStat(int kind) {
488 RuntimeStats* stats;
489 if (kind < (1<<16)) {
490 stats = GetStats();
491 } else {
492 stats = Thread::Current()->GetStats();
493 kind >>= 16;
494 }
495 switch (kind) {
496 case KIND_ALLOCATED_OBJECTS:
497 return stats->allocated_objects;
498 case KIND_ALLOCATED_BYTES:
499 return stats->allocated_bytes;
500 case KIND_FREED_OBJECTS:
501 return stats->freed_objects;
502 case KIND_FREED_BYTES:
503 return stats->freed_bytes;
504 case KIND_GC_INVOCATIONS:
505 return stats->gc_for_alloc_count;
506 case KIND_CLASS_INIT_COUNT:
507 return stats->class_init_count;
508 case KIND_CLASS_INIT_TIME:
509 // Convert ns to us, reduce to 32 bits.
510 return (int) (stats->class_init_time_ns / 1000);
511 case KIND_EXT_ALLOCATED_OBJECTS:
512 case KIND_EXT_ALLOCATED_BYTES:
513 case KIND_EXT_FREED_OBJECTS:
514 case KIND_EXT_FREED_BYTES:
515 return 0; // backward compatibility
516 default:
517 CHECK(false);
518 return -1; // unreachable
519 }
520}
521
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700522void Runtime::BlockSignals() {
523 sigset_t sigset;
524 if (sigemptyset(&sigset) == -1) {
525 PLOG(FATAL) << "sigemptyset failed";
526 }
527 if (sigaddset(&sigset, SIGPIPE) == -1) {
528 PLOG(ERROR) << "sigaddset SIGPIPE failed";
529 }
530 // SIGQUIT is used to dump the runtime's state (including stack traces).
531 if (sigaddset(&sigset, SIGQUIT) == -1) {
532 PLOG(ERROR) << "sigaddset SIGQUIT failed";
533 }
534 // SIGUSR1 is used to initiate a heap dump.
535 if (sigaddset(&sigset, SIGUSR1) == -1) {
536 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
537 }
538 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
539}
540
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700541void Runtime::AttachCurrentThread(const char* name, bool as_daemon) {
542 Thread::Attach(instance_, name, as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700543}
544
Elliott Hughesd92bec42011-09-02 17:04:36 -0700545void Runtime::DetachCurrentThread() {
Elliott Hughes038a8062011-09-18 14:12:41 -0700546 // TODO: check we're not calling DetachCurrentThread from a call stack that
547 // includes managed frames. (It's only valid if the stack is all-native.)
Elliott Hughes02b48d12011-09-07 17:15:51 -0700548 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700549}
550
Brian Carlstrome24fa612011-09-29 00:53:55 -0700551void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
552 class_linker_->VisitRoots(visitor, arg);
553 intern_table_->VisitRoots(visitor, arg);
554 java_vm_->VisitRoots(visitor, arg);
555 thread_list_->VisitRoots(visitor, arg);
556 visitor(jni_stub_array_, arg);
557 visitor(abstract_method_error_stub_array_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700558 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
559 visitor(resolution_stub_array_[i], arg);
560 }
561 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
562 visitor(callee_save_method_[i], arg);
563 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700564
565 //(*visitor)(&gDvm.outOfMemoryObj, 0, ROOT_VM_INTERNAL, arg);
566 //(*visitor)(&gDvm.internalErrorObj, 0, ROOT_VM_INTERNAL, arg);
567 //(*visitor)(&gDvm.noClassDefFoundErrorObj, 0, ROOT_VM_INTERNAL, arg);
568 UNIMPLEMENTED(WARNING) << "some roots not marked";
569}
570
571bool Runtime::HasJniStubArray() const {
572 return jni_stub_array_ != NULL;
573}
574
575ByteArray* Runtime::GetJniStubArray() const {
576 CHECK(jni_stub_array_ != NULL);
577 return jni_stub_array_;
578}
579
580void Runtime::SetJniStubArray(ByteArray* jni_stub_array) {
581 CHECK(jni_stub_array != NULL);
582 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array);
583 jni_stub_array_ = jni_stub_array;
584}
585
586bool Runtime::HasAbstractMethodErrorStubArray() const {
587 return abstract_method_error_stub_array_ != NULL;
588}
589
590ByteArray* Runtime::GetAbstractMethodErrorStubArray() const {
591 CHECK(abstract_method_error_stub_array_ != NULL);
592 return abstract_method_error_stub_array_;
593}
594
595void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) {
596 CHECK(abstract_method_error_stub_array != NULL);
597 CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array);
598 abstract_method_error_stub_array_ = abstract_method_error_stub_array;
599}
600
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700601
602Runtime::TrampolineType Runtime::GetTrampolineType(Method* method) {
603 if (method == NULL) {
604 return Runtime::kUnknownMethod;
605 } else if (method->IsStatic()) {
606 return Runtime::kStaticMethod;
607 } else {
608 return Runtime::kInstanceMethod;
609 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700610}
611
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700612bool Runtime::HasResolutionStubArray(TrampolineType type) const {
613 return resolution_stub_array_[type] != NULL;
Ian Rogersad25ac52011-10-04 19:13:33 -0700614}
615
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700616ByteArray* Runtime::GetResolutionStubArray(TrampolineType type) const {
617 CHECK(HasResolutionStubArray(type));
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700618 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastTrampolineMethodType));
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700619 return resolution_stub_array_[type];
620}
621
622void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700623 CHECK(resolution_stub_array != NULL);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700624 CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array);
625 resolution_stub_array_[type] = resolution_stub_array;
Ian Rogersad25ac52011-10-04 19:13:33 -0700626}
627
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700628Method* Runtime::CreateCalleeSaveMethod(InstructionSet insns, CalleeSaveType type) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700629 Class* method_class = Method::GetMethodClass();
630 Method* method = down_cast<Method*>(method_class->AllocObject());
631 method->SetDeclaringClass(method_class);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700632 const char* name;
633 if (type == kSaveAll) {
634 name = "$$$callee_save_method$$$";
635 } else if (type == kRefsOnly) {
636 name = "$$$refs_only_callee_save_method$$$";
637 } else {
638 DCHECK(type == kRefsAndArgs);
639 name = "$$$refs_and_args_callee_save_method$$$";
640 }
641 method->SetName(intern_table_->InternStrong(name));
Ian Rogersff1ed472011-09-20 13:46:24 -0700642 method->SetSignature(intern_table_->InternStrong("()V"));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700643 method->SetCode(NULL);
Ian Rogersff1ed472011-09-20 13:46:24 -0700644 if ((insns == kThumb2) || (insns == kArm)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700645 uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6) | (1 << art::arm::R7) |
646 (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11);
647 uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3);
648 uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9);
649 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills :0) |
650 (type == kSaveAll ? all_spills :0) | (1 << art::arm::LR);
651 uint32_t fp_all_spills = (1 << art::arm::S0) | (1 << art::arm::S1) | (1 << art::arm::S2) |
652 (1 << art::arm::S3) | (1 << art::arm::S4) | (1 << art::arm::S5) |
653 (1 << art::arm::S6) | (1 << art::arm::S7) | (1 << art::arm::S8) |
654 (1 << art::arm::S9) | (1 << art::arm::S10) | (1 << art::arm::S11) |
655 (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) |
656 (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) |
657 (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) |
658 (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) |
659 (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) |
660 (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
661 (1 << art::arm::S30) | (1 << art::arm::S31);
662 uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
663 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
664 __builtin_popcount(fp_spills) /* fprs */ +
665 1 /* Method* */) * kPointerSize, kStackAlignment);
Ian Rogers15fdb8c2011-09-25 15:45:07 -0700666 method->SetFrameSizeInBytes(frame_size);
667 method->SetReturnPcOffsetInBytes(frame_size - kPointerSize);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700668 method->SetCoreSpillMask(core_spills);
669 method->SetFpSpillMask(fp_spills);
Ian Rogersff1ed472011-09-20 13:46:24 -0700670 } else if (insns == kX86) {
671 method->SetFrameSizeInBytes(32);
672 method->SetReturnPcOffsetInBytes(28);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700673 method->SetCoreSpillMask((1 << art::x86::EBX) | (1 << art::x86::EBP) | (1 << art::x86::ESI) |
Ian Rogersff1ed472011-09-20 13:46:24 -0700674 (1 << art::x86::EDI));
675 method->SetFpSpillMask(0);
676 } else {
677 UNIMPLEMENTED(FATAL);
678 }
679 return method;
680}
681
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700682bool Runtime::HasCalleeSaveMethod(CalleeSaveType type) const {
683 return callee_save_method_[type] != NULL;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700684}
685
Brian Carlstrome24fa612011-09-29 00:53:55 -0700686// Returns a special method that describes all callee saves being spilled to the stack.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700687Method* Runtime::GetCalleeSaveMethod(CalleeSaveType type) const {
688 CHECK(HasCalleeSaveMethod(type));
689 return callee_save_method_[type];
Brian Carlstrome24fa612011-09-29 00:53:55 -0700690}
691
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700692void Runtime::SetCalleeSaveMethod(Method* method, CalleeSaveType type) {
693 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
694 callee_save_method_[type] = method;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700695}
696
Carl Shapiro1fb86202011-06-27 17:43:13 -0700697} // namespace art