blob: e9b7fdc806a83808b1a21bf35ca23ceb1ca0ebd5 [file] [log] [blame]
Carl Shapiro1fb86202011-06-27 17:43:13 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "runtime.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -07004
Elliott Hughesffe67362011-07-17 12:09:27 -07005#include <cstdio>
6#include <cstdlib>
Brian Carlstrom8a436592011-08-15 21:27:23 -07007#include <limits>
Carl Shapiro2ed144c2011-07-26 16:52:08 -07008#include <vector>
Elliott Hughesffe67362011-07-17 12:09:27 -07009
Elliott Hughes90a33692011-08-30 13:27:07 -070010#include "UniquePtr.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070011#include "class_linker.h"
12#include "heap.h"
Elliott Hughescf4c6c42011-09-01 15:16:42 -070013#include "intern_table.h"
Elliott Hughesc5f7c912011-08-18 14:00:42 -070014#include "jni_internal.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070015#include "signal_catcher.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070016#include "thread.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070017#include "thread_list.h"
Carl Shapiro61e019d2011-07-14 16:53:09 -070018
Elliott Hughes90a33692011-08-30 13:27:07 -070019// TODO: this drags in cutil/log.h, which conflicts with our logging.h.
20#include "JniConstants.h"
21
Carl Shapiro1fb86202011-06-27 17:43:13 -070022namespace art {
23
Carl Shapiro2ed144c2011-07-26 16:52:08 -070024Runtime* Runtime::instance_ = NULL;
25
Elliott Hughesdcc24742011-09-07 14:02:44 -070026Runtime::Runtime()
Elliott Hughesbe759c62011-09-08 19:38:21 -070027 : default_stack_size_(Thread::kDefaultStackSize),
Elliott Hughesdcc24742011-09-07 14:02:44 -070028 thread_list_(NULL),
29 intern_table_(NULL),
30 class_linker_(NULL),
31 signal_catcher_(NULL),
32 java_vm_(NULL),
Brian Carlstrom16192862011-09-12 17:50:06 -070033 jni_stub_array_(NULL),
Ian Rogersff1ed472011-09-20 13:46:24 -070034 callee_save_method_(NULL),
Elliott Hughesdcc24742011-09-07 14:02:44 -070035 started_(false),
36 vfprintf_(NULL),
37 exit_(NULL),
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070038 abort_(NULL),
39 stats_enabled_(false) {
Elliott Hughesdcc24742011-09-07 14:02:44 -070040}
41
Carl Shapiro61e019d2011-07-14 16:53:09 -070042Runtime::~Runtime() {
Elliott Hughes5fe594f2011-09-08 12:33:17 -070043 // Make sure our internal threads are dead before we start tearing down things they're using.
44 delete signal_catcher_;
Elliott Hughes038a8062011-09-18 14:12:41 -070045 // TODO: GC thread.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070046
Elliott Hughes038a8062011-09-18 14:12:41 -070047 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
Elliott Hughes93e74e82011-09-13 11:07:03 -070048 delete thread_list_;
49
Carl Shapiro61e019d2011-07-14 16:53:09 -070050 delete class_linker_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070051 Heap::Destroy();
Elliott Hughescf4c6c42011-09-01 15:16:42 -070052 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070053 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -070054 Thread::Shutdown();
Carl Shapiro4acf4642011-07-26 18:54:13 -070055 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070056 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -070057 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -070058}
59
Elliott Hughesffe67362011-07-17 12:09:27 -070060void Runtime::Abort(const char* file, int line) {
61 // Get any pending output out of the way.
62 fflush(NULL);
63
64 // Many people have difficulty distinguish aborts from crashes,
65 // so be explicit.
66 LogMessage(file, line, ERROR, -1).stream() << "Runtime aborting...";
67
Elliott Hughesffe67362011-07-17 12:09:27 -070068 // Perform any platform-specific pre-abort actions.
69 PlatformAbort(file, line);
70
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070071 // use abort hook if we have one
72 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
73 Runtime::Current()->abort_();
74 // notreached
75 }
76
Elliott Hughesffe67362011-07-17 12:09:27 -070077 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -070078 // receive SIGABRT. debuggerd dumps the stack trace of the main
79 // thread, whether or not that was the thread that failed. By
80 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -070081 // fault in the current thread, and get a useful log from debuggerd.
82 // We can also trivially tell the difference between a VM crash and
83 // a deliberate abort by looking at the fault address.
84 *reinterpret_cast<char*>(0xdeadd00d) = 38;
85 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -070086 // notreached
87}
88
Elliott Hughesbf86d042011-08-31 17:53:14 -070089void Runtime::CallExitHook(jint status) {
90 if (exit_ != NULL) {
91 ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative);
92 exit_(status);
93 LOG(WARNING) << "Exit hook returned instead of exiting!";
94 }
95}
96
Brian Carlstrom8a436592011-08-15 21:27:23 -070097// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
98// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
99// [gG] gigabytes.
100//
101// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700102// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
103// of 1024.
104//
105// The spec says the -Xmx and -Xms options must be multiples of 1024. It
106// doesn't say anything about -Xss.
107//
108// Returns 0 (a useless size) if "s" is malformed or specifies a low or
109// non-evenly-divisible value.
110//
111size_t ParseMemoryOption(const char *s, size_t div) {
112 // strtoul accepts a leading [+-], which we don't want,
113 // so make sure our string starts with a decimal digit.
114 if (isdigit(*s)) {
115 const char *s2;
116 size_t val = strtoul(s, (char **)&s2, 10);
117 if (s2 != s) {
118 // s2 should be pointing just after the number.
119 // If this is the end of the string, the user
120 // has specified a number of bytes. Otherwise,
121 // there should be exactly one more character
122 // that specifies a multiplier.
123 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700124 // The remainder of the string is either a single multiplier
125 // character, or nothing to indicate that the value is in
126 // bytes.
127 char c = *s2++;
128 if (*s2 == '\0') {
129 size_t mul;
130 if (c == '\0') {
131 mul = 1;
132 } else if (c == 'k' || c == 'K') {
133 mul = KB;
134 } else if (c == 'm' || c == 'M') {
135 mul = MB;
136 } else if (c == 'g' || c == 'G') {
137 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700138 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700139 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700140 return 0;
141 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700142
143 if (val <= std::numeric_limits<size_t>::max() / mul) {
144 val *= mul;
145 } else {
146 // Clamp to a multiple of 1024.
147 val = std::numeric_limits<size_t>::max() & ~(1024-1);
148 }
149 } else {
150 // There's more than one character after the numeric part.
151 return 0;
152 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700153 }
154 // The man page says that a -Xm value must be a multiple of 1024.
155 if (val % div == 0) {
156 return val;
157 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700158 }
159 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700160 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700161}
162
Elliott Hughes0af55432011-08-17 18:37:28 -0700163void LoadJniLibrary(JavaVMExt* vm, const char* name) {
164 // TODO: OS_SHARED_LIB_FORMAT_STR
165 std::string mapped_name(StringPrintf("lib%s.so", name));
Elliott Hughes75770752011-08-24 17:52:38 -0700166 std::string reason;
167 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700168 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
169 << reason;
170 }
171}
172
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700173void CreateClassPath(const std::string& class_path,
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700174 std::vector<const DexFile*>& class_path_vector) {
Carl Shapirofc322c72011-07-27 00:20:01 -0700175 std::vector<std::string> parsed;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700176 Split(class_path, ':', parsed);
Carl Shapirofc322c72011-07-27 00:20:01 -0700177 for (size_t i = 0; i < parsed.size(); ++i) {
Brian Carlstrom16192862011-09-12 17:50:06 -0700178 const DexFile* dex_file = DexFile::Open(parsed[i], "");
Carl Shapirofc322c72011-07-27 00:20:01 -0700179 if (dex_file != NULL) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700180 class_path_vector.push_back(dex_file);
Carl Shapirofc322c72011-07-27 00:20:01 -0700181 }
182 }
183}
184
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700185Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700186 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700187 parsed->boot_image_ = NULL;
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700188#ifdef NDEBUG
Elliott Hughes5174fe62011-08-23 15:12:35 -0700189 // -Xcheck:jni is off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700190 parsed->check_jni_ = false;
191#else
192 // ...but on by default in debug builds.
193 parsed->check_jni_ = true;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700194#endif
Elliott Hughes85d15452011-09-16 17:33:01 -0700195
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700196 parsed->heap_initial_size_ = Heap::kInitialSize;
197 parsed->heap_maximum_size_ = Heap::kMaximumSize;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700198 parsed->stack_size_ = Thread::kDefaultStackSize;
199
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700200 parsed->hook_vfprintf_ = vfprintf;
201 parsed->hook_exit_ = exit;
202 parsed->hook_abort_ = abort;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700203
204 for (size_t i = 0; i < options.size(); ++i) {
205 const StringPiece& option = options[i].first;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700206 if (option.starts_with("-Xbootclasspath:")) {
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700207 parsed->boot_class_path_string_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700208 } else if (option == "bootclasspath") {
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700209 UNIMPLEMENTED(WARNING) << "what should VMRuntime.getBootClassPath return here?";
Brian Carlstromf734cf52011-08-17 16:28:14 -0700210 const void* dex_vector = options[i].second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700211 const std::vector<const DexFile*>* v
212 = reinterpret_cast<const std::vector<const DexFile*>*>(dex_vector);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700213 if (v == NULL) {
214 if (ignore_unrecognized) {
215 continue;
216 }
217 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700218 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700219 return NULL;
220 }
221 parsed->boot_class_path_ = *v;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700222 } else if (option == "classpath") {
223 const void* dex_vector = options[i].second;
224 const std::vector<const DexFile*>* v
225 = reinterpret_cast<const std::vector<const DexFile*>*>(dex_vector);
226 if (v == NULL) {
227 if (ignore_unrecognized) {
228 continue;
229 }
230 // TODO: usage
231 LOG(FATAL) << "Failed to parse " << option;
232 return NULL;
233 }
234 parsed->class_path_ = *v;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700235 } else if (option == "-classpath" || option == "-cp") {
236 // TODO: support -Djava.class.path
237 i++;
238 if (i == options.size()) {
239 // TODO: usage
240 LOG(FATAL) << "Missing required class path value for " << option;
241 return NULL;
242 }
243 const StringPiece& value = options[i].first;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700244 parsed->class_path_string_ = value.data();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700245 } else if (option.starts_with("-Xbootimage:")) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700246 // TODO: remove when intern_addr_ is removed, just use -Ximage:
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700247 parsed->boot_image_ = option.substr(strlen("-Xbootimage:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700248 } else if (option.starts_with("-Ximage:")) {
249 parsed->images_.push_back(option.substr(strlen("-Ximage:")).data());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700250 } else if (option.starts_with("-Xcheck:jni")) {
251 parsed->check_jni_ = true;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700252 } else if (option.starts_with("-Xms")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700253 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024);
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->heap_initial_size_ = size;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700263 } else if (option.starts_with("-Xmx")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700264 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024);
265 if (size == 0) {
266 if (ignore_unrecognized) {
267 continue;
268 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700269 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700270 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700271 return NULL;
272 }
273 parsed->heap_maximum_size_ = size;
274 } else if (option.starts_with("-Xss")) {
275 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1);
276 if (size == 0) {
277 if (ignore_unrecognized) {
278 continue;
279 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700280 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700281 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700282 return NULL;
283 }
284 parsed->stack_size_ = size;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700285 } else if (option.starts_with("-D")) {
286 parsed->properties_.push_back(option.substr(strlen("-D")).data());
Elliott Hughesa0957642011-09-02 14:27:33 -0700287 } else if (option.starts_with("-Xjnitrace:")) {
288 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:")).data();
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700289 } else if (option.starts_with("-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700290 std::vector<std::string> verbose_options;
Elliott Hughes34023802011-08-30 12:06:17 -0700291 Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700292 for (size_t i = 0; i < verbose_options.size(); ++i) {
293 parsed->verbose_.insert(verbose_options[i]);
294 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700295 } else if (option == "vfprintf") {
296 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
297 } else if (option == "exit") {
298 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
299 } else if (option == "abort") {
300 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700301 } else {
302 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700303 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700304 LOG(ERROR) << "Unrecognized option " << option;
305 // TODO: this should exit, but for now tolerate unknown options
306 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700307 }
308 }
309 }
310
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700311 // Consider it an error if both bootclasspath and -Xbootclasspath: are supplied.
Brian Carlstrom78128a62011-09-15 17:21:19 -0700312 // TODO: remove bootclasspath and classpath which are mostly just used by tests?
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700313 if (!parsed->boot_class_path_.empty() && !parsed->boot_class_path_string_.empty()) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700314 // TODO: usage
315 LOG(FATAL) << "bootclasspath and -Xbootclasspath: are mutually exclusive options.";
316 return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700317 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700318 if (!parsed->class_path_.empty() && !parsed->class_path_string_.empty()) {
319 // TODO: usage
320 LOG(FATAL) << "bootclasspath and -Xbootclasspath: are mutually exclusive options.";
321 return NULL;
322 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700323 if (parsed->boot_class_path_.empty()) {
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700324 if (parsed->boot_class_path_string_ == NULL) {
325 const char* BOOTCLASSPATH = getenv("BOOTCLASSPATH");
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700326 if (BOOTCLASSPATH != NULL) {
327 parsed->boot_class_path_string_ = BOOTCLASSPATH;
328 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700329 }
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700330 CreateClassPath(parsed->boot_class_path_string_, parsed->boot_class_path_);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700331 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700332
Brian Carlstrom78128a62011-09-15 17:21:19 -0700333 if (parsed->class_path_.empty()) {
334 if (parsed->class_path_string_ == NULL) {
335 const char* CLASSPATH = getenv("CLASSPATH");
336 if (CLASSPATH != NULL) {
337 parsed->class_path_string_ = CLASSPATH;
338 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700339 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700340 CreateClassPath(parsed->class_path_string_, parsed->class_path_);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700341 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700342
Elliott Hughes85d15452011-09-16 17:33:01 -0700343 LOG(INFO) << "CheckJNI is " << (parsed->check_jni_ ? "on" : "off");
344
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700345 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700346}
347
348Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700349 // TODO: acquire a static mutex on Runtime to avoid racing.
350 if (Runtime::instance_ != NULL) {
351 return NULL;
352 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700353 instance_ = new Runtime;
354 if (!instance_->Init(options, ignore_unrecognized)) {
355 delete instance_;
356 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700357 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700358 return instance_;
359}
Elliott Hughes0af55432011-08-17 18:37:28 -0700360
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700361void Runtime::Start() {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700362 started_ = true;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700363
Elliott Hughes038a8062011-09-18 14:12:41 -0700364 InitNativeMethods();
Elliott Hughes85d15452011-09-16 17:33:01 -0700365
Elliott Hughes038a8062011-09-18 14:12:41 -0700366 Thread::FinishStartup();
Elliott Hughes85d15452011-09-16 17:33:01 -0700367
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700368 RunImageClinits();
369
Elliott Hughes85d15452011-09-16 17:33:01 -0700370 StartDaemonThreads();
371}
372
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700373// initialize classes that have instances in the image but that have
374// <clinit> methods so they could not be initialized by the compiler.
375void Runtime::RunImageClinits() {
376 Class* Field_class = class_linker_->FindSystemClass("Ljava/lang/reflect/Field;");
377 CHECK(Field_class->FindDeclaredDirectMethod("<clinit>", "()V") != NULL);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700378 class_linker_->EnsureInitialized(Field_class, true);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700379 CHECK(!Thread::Current()->IsExceptionPending());
380}
381
Elliott Hughes85d15452011-09-16 17:33:01 -0700382void Runtime::StartDaemonThreads() {
383 signal_catcher_ = new SignalCatcher;
384
385 Class* c = class_linker_->FindSystemClass("Ljava/lang/Daemons;");
386 CHECK(c != NULL);
387 Method* m = c->FindDirectMethod("start", "()V");
388 CHECK(m != NULL);
Elliott Hughes038a8062011-09-18 14:12:41 -0700389 m->Invoke(Thread::Current(), NULL, NULL, NULL);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700390}
391
Elliott Hughesdcc24742011-09-07 14:02:44 -0700392bool Runtime::IsStarted() {
393 return started_;
394}
395
Elliott Hughes0af55432011-08-17 18:37:28 -0700396bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700397 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700398
Elliott Hughes90a33692011-08-30 13:27:07 -0700399 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
400 if (options.get() == NULL) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700401 LOG(WARNING) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700402 return false;
403 }
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700404
405 boot_class_path_ = options->boot_class_path_string_;
406 class_path_ = options->class_path_string_;
407 properties_ = options->properties_;
408
Elliott Hughes0af55432011-08-17 18:37:28 -0700409 vfprintf_ = options->hook_vfprintf_;
410 exit_ = options->hook_exit_;
411 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700412
Elliott Hughesbe759c62011-09-08 19:38:21 -0700413 default_stack_size_ = options->stack_size_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700414
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700415 thread_list_ = new ThreadList;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700416 intern_table_ = new InternTable;
417
Elliott Hughesbe759c62011-09-08 19:38:21 -0700418 Heap::Init(options->heap_initial_size_, options->heap_maximum_size_,
419 options->boot_image_, options->images_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700420
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700421 BlockSignals();
422
Elliott Hughesa0957642011-09-02 14:27:33 -0700423 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700424
Elliott Hughesbe759c62011-09-08 19:38:21 -0700425 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700426
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700427 // ClassLinker needs an attached thread, but we can't fully attach a thread
428 // without creating objects.
429 Thread::Attach(this, "main", false);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700430
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700431 class_linker_ = ClassLinker::Create(options->boot_class_path_,
432 options->class_path_,
433 intern_table_,
434 Heap::GetBootSpace());
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700435
Carl Shapiro1fb86202011-06-27 17:43:13 -0700436 return true;
437}
438
Elliott Hughes038a8062011-09-18 14:12:41 -0700439void Runtime::InitNativeMethods() {
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700440 Thread* self = Thread::Current();
441 JNIEnv* env = self->GetJniEnv();
442
443 // Must be in the kNative state for JNI-based method registration.
444 ScopedThreadStateChange tsc(self, Thread::kNative);
445
Elliott Hughesfea966e2011-09-22 10:26:20 -0700446 JniConstants::init(env);
447
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700448 // First set up the native methods provided by the runtime itself.
449 RegisterRuntimeNativeMethods(env);
450
451 // Now set up libcore, which is just a JNI library with a JNI_OnLoad.
452 // Most JNI libraries can just use System.loadLibrary, but you can't
453 // if you're the library that implements System.loadLibrary!
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700454 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
455}
456
457void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
458#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700459 //REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700460 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700461 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700462 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700463 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700464 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700465 REGISTER(register_java_lang_Object);
466 REGISTER(register_java_lang_Runtime);
467 REGISTER(register_java_lang_String);
468 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700469 REGISTER(register_java_lang_Thread);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700470 REGISTER(register_java_lang_Throwable);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700471 REGISTER(register_java_lang_VMClassLoader);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700472 //REGISTER(register_java_lang_reflect_AccessibleObject);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700473 REGISTER(register_java_lang_reflect_Array);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700474 //REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700475 REGISTER(register_java_lang_reflect_Field);
476 REGISTER(register_java_lang_reflect_Method);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700477 //REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700478 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700479 //REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
480 //REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700481 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700482#undef REGISTER
483}
484
Elliott Hughes8daa0922011-09-11 13:46:25 -0700485void Runtime::Dump(std::ostream& os) {
Elliott Hughese27955c2011-08-26 15:21:24 -0700486 // TODO: dump other runtime statistics?
487 os << "Loaded classes: " << class_linker_->NumLoadedClasses() << "\n";
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700488 os << "Intern table size: " << GetInternTable()->Size() << "\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700489 // LOGV("VM stats: meth=%d ifld=%d sfld=%d linear=%d",
490 // gDvm.numDeclaredMethods,
491 // gDvm.numDeclaredInstFields,
492 // gDvm.numDeclaredStaticFields,
493 // gDvm.pBootLoaderAlloc->curOffset);
494 // LOGI("GC precise methods: %d", dvmPointerSetGetCount(gDvm.preciseMethods));
Elliott Hughes42ee1422011-09-06 12:33:32 -0700495 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700496
497 thread_list_->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700498}
499
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700500void Runtime::SetStatsEnabled(bool new_state) {
501 if (new_state == true) {
502 GetStats()->Clear(~0);
503 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
504 Thread::Current()->GetStats()->Clear(~0);
505 }
506 stats_enabled_ = new_state;
507}
508
509void Runtime::ResetStats(int kinds) {
510 GetStats()->Clear(kinds & 0xffff);
511 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
512 Thread::Current()->GetStats()->Clear(kinds >> 16);
513}
514
515RuntimeStats* Runtime::GetStats() {
516 return &stats_;
517}
518
519int32_t Runtime::GetStat(int kind) {
520 RuntimeStats* stats;
521 if (kind < (1<<16)) {
522 stats = GetStats();
523 } else {
524 stats = Thread::Current()->GetStats();
525 kind >>= 16;
526 }
527 switch (kind) {
528 case KIND_ALLOCATED_OBJECTS:
529 return stats->allocated_objects;
530 case KIND_ALLOCATED_BYTES:
531 return stats->allocated_bytes;
532 case KIND_FREED_OBJECTS:
533 return stats->freed_objects;
534 case KIND_FREED_BYTES:
535 return stats->freed_bytes;
536 case KIND_GC_INVOCATIONS:
537 return stats->gc_for_alloc_count;
538 case KIND_CLASS_INIT_COUNT:
539 return stats->class_init_count;
540 case KIND_CLASS_INIT_TIME:
541 // Convert ns to us, reduce to 32 bits.
542 return (int) (stats->class_init_time_ns / 1000);
543 case KIND_EXT_ALLOCATED_OBJECTS:
544 case KIND_EXT_ALLOCATED_BYTES:
545 case KIND_EXT_FREED_OBJECTS:
546 case KIND_EXT_FREED_BYTES:
547 return 0; // backward compatibility
548 default:
549 CHECK(false);
550 return -1; // unreachable
551 }
552}
553
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700554void Runtime::BlockSignals() {
555 sigset_t sigset;
556 if (sigemptyset(&sigset) == -1) {
557 PLOG(FATAL) << "sigemptyset failed";
558 }
559 if (sigaddset(&sigset, SIGPIPE) == -1) {
560 PLOG(ERROR) << "sigaddset SIGPIPE failed";
561 }
562 // SIGQUIT is used to dump the runtime's state (including stack traces).
563 if (sigaddset(&sigset, SIGQUIT) == -1) {
564 PLOG(ERROR) << "sigaddset SIGQUIT failed";
565 }
566 // SIGUSR1 is used to initiate a heap dump.
567 if (sigaddset(&sigset, SIGUSR1) == -1) {
568 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
569 }
570 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
571}
572
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700573void Runtime::AttachCurrentThread(const char* name, bool as_daemon) {
574 Thread::Attach(instance_, name, as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700575}
576
Elliott Hughesd92bec42011-09-02 17:04:36 -0700577void Runtime::DetachCurrentThread() {
Elliott Hughes038a8062011-09-18 14:12:41 -0700578 // TODO: check we're not calling DetachCurrentThread from a call stack that
579 // includes managed frames. (It's only valid if the stack is all-native.)
Elliott Hughes02b48d12011-09-07 17:15:51 -0700580 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700581}
582
Ian Rogersff1ed472011-09-20 13:46:24 -0700583Method* Runtime::CreateCalleeSaveMethod(InstructionSet insns) {
584 Class* method_class = Method::GetMethodClass();
585 Method* method = down_cast<Method*>(method_class->AllocObject());
586 method->SetDeclaringClass(method_class);
587 method->SetName(intern_table_->InternStrong("$$$callee_save_method$$$"));
588 method->SetSignature(intern_table_->InternStrong("()V"));
589 method->SetCode(NULL, insns, NULL);
590 if ((insns == kThumb2) || (insns == kArm)) {
591 method->SetFrameSizeInBytes(64);
592 method->SetReturnPcOffsetInBytes(60);
593 method->SetCoreSpillMask((1 << art::arm::R1) |
594 (1 << art::arm::R2) |
595 (1 << art::arm::R3) |
596 (1 << art::arm::R4) |
597 (1 << art::arm::R5) |
598 (1 << art::arm::R6) |
599 (1 << art::arm::R7) |
600 (1 << art::arm::R8) |
601 (1 << art::arm::R9) |
602 (1 << art::arm::R10) |
603 (1 << art::arm::R11) |
604 (1 << art::arm::LR));
605 method->SetFpSpillMask(0);
606 } else if (insns == kX86) {
607 method->SetFrameSizeInBytes(32);
608 method->SetReturnPcOffsetInBytes(28);
609 method->SetCoreSpillMask((1 << art::x86::EBX) |
610 (1 << art::x86::EBP) |
611 (1 << art::x86::ESI) |
612 (1 << art::x86::EDI));
613 method->SetFpSpillMask(0);
614 } else {
615 UNIMPLEMENTED(FATAL);
616 }
617 return method;
618}
619
Elliott Hughes410c0c82011-09-01 17:58:25 -0700620void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
621 class_linker_->VisitRoots(visitor, arg);
622 intern_table_->VisitRoots(visitor, arg);
623 java_vm_->VisitRoots(visitor, arg);
624 thread_list_->VisitRoots(visitor, arg);
Brian Carlstrom16192862011-09-12 17:50:06 -0700625 visitor(jni_stub_array_, arg);
Ian Rogersff1ed472011-09-20 13:46:24 -0700626 visitor(callee_save_method_, arg);
Elliott Hughes410c0c82011-09-01 17:58:25 -0700627
628 //(*visitor)(&gDvm.outOfMemoryObj, 0, ROOT_VM_INTERNAL, arg);
629 //(*visitor)(&gDvm.internalErrorObj, 0, ROOT_VM_INTERNAL, arg);
630 //(*visitor)(&gDvm.noClassDefFoundErrorObj, 0, ROOT_VM_INTERNAL, arg);
631 UNIMPLEMENTED(WARNING) << "some roots not marked";
Brian Carlstrom1f870082011-08-23 16:02:11 -0700632}
633
Carl Shapiro1fb86202011-06-27 17:43:13 -0700634} // namespace art