blob: a6977b2a4c5ff2e75025acf85afedb5630718ee0 [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()
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -070027 : verbose_startup_(false),
28 default_stack_size_(Thread::kDefaultStackSize),
Elliott Hughesdcc24742011-09-07 14:02:44 -070029 thread_list_(NULL),
30 intern_table_(NULL),
31 class_linker_(NULL),
32 signal_catcher_(NULL),
33 java_vm_(NULL),
Brian Carlstrom16192862011-09-12 17:50:06 -070034 jni_stub_array_(NULL),
Ian Rogersff1ed472011-09-20 13:46:24 -070035 callee_save_method_(NULL),
Elliott Hughesdcc24742011-09-07 14:02:44 -070036 started_(false),
37 vfprintf_(NULL),
38 exit_(NULL),
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070039 abort_(NULL),
40 stats_enabled_(false) {
Elliott Hughesdcc24742011-09-07 14:02:44 -070041}
42
Carl Shapiro61e019d2011-07-14 16:53:09 -070043Runtime::~Runtime() {
Elliott Hughes5fe594f2011-09-08 12:33:17 -070044 // Make sure our internal threads are dead before we start tearing down things they're using.
45 delete signal_catcher_;
Elliott Hughes038a8062011-09-18 14:12:41 -070046 // TODO: GC thread.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070047
Elliott Hughes038a8062011-09-18 14:12:41 -070048 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
Elliott Hughes93e74e82011-09-13 11:07:03 -070049 delete thread_list_;
50
Carl Shapiro61e019d2011-07-14 16:53:09 -070051 delete class_linker_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070052 Heap::Destroy();
Elliott Hughescf4c6c42011-09-01 15:16:42 -070053 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070054 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -070055 Thread::Shutdown();
Carl Shapiro4acf4642011-07-26 18:54:13 -070056 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070057 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -070058 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -070059}
60
Elliott Hughesffe67362011-07-17 12:09:27 -070061void Runtime::Abort(const char* file, int line) {
62 // Get any pending output out of the way.
63 fflush(NULL);
64
65 // Many people have difficulty distinguish aborts from crashes,
66 // so be explicit.
67 LogMessage(file, line, ERROR, -1).stream() << "Runtime aborting...";
68
Elliott Hughesffe67362011-07-17 12:09:27 -070069 // Perform any platform-specific pre-abort actions.
70 PlatformAbort(file, line);
71
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070072 // use abort hook if we have one
73 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
74 Runtime::Current()->abort_();
75 // notreached
76 }
77
Elliott Hughesffe67362011-07-17 12:09:27 -070078 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -070079 // receive SIGABRT. debuggerd dumps the stack trace of the main
80 // thread, whether or not that was the thread that failed. By
81 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -070082 // fault in the current thread, and get a useful log from debuggerd.
83 // We can also trivially tell the difference between a VM crash and
84 // a deliberate abort by looking at the fault address.
85 *reinterpret_cast<char*>(0xdeadd00d) = 38;
86 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -070087 // notreached
88}
89
Elliott Hughesbf86d042011-08-31 17:53:14 -070090void Runtime::CallExitHook(jint status) {
91 if (exit_ != NULL) {
92 ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative);
93 exit_(status);
94 LOG(WARNING) << "Exit hook returned instead of exiting!";
95 }
96}
97
Brian Carlstrom8a436592011-08-15 21:27:23 -070098// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
99// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
100// [gG] gigabytes.
101//
102// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700103// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
104// of 1024.
105//
106// The spec says the -Xmx and -Xms options must be multiples of 1024. It
107// doesn't say anything about -Xss.
108//
109// Returns 0 (a useless size) if "s" is malformed or specifies a low or
110// non-evenly-divisible value.
111//
112size_t ParseMemoryOption(const char *s, size_t div) {
113 // strtoul accepts a leading [+-], which we don't want,
114 // so make sure our string starts with a decimal digit.
115 if (isdigit(*s)) {
116 const char *s2;
117 size_t val = strtoul(s, (char **)&s2, 10);
118 if (s2 != s) {
119 // s2 should be pointing just after the number.
120 // If this is the end of the string, the user
121 // has specified a number of bytes. Otherwise,
122 // there should be exactly one more character
123 // that specifies a multiplier.
124 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700125 // The remainder of the string is either a single multiplier
126 // character, or nothing to indicate that the value is in
127 // bytes.
128 char c = *s2++;
129 if (*s2 == '\0') {
130 size_t mul;
131 if (c == '\0') {
132 mul = 1;
133 } else if (c == 'k' || c == 'K') {
134 mul = KB;
135 } else if (c == 'm' || c == 'M') {
136 mul = MB;
137 } else if (c == 'g' || c == 'G') {
138 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700139 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700140 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700141 return 0;
142 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700143
144 if (val <= std::numeric_limits<size_t>::max() / mul) {
145 val *= mul;
146 } else {
147 // Clamp to a multiple of 1024.
148 val = std::numeric_limits<size_t>::max() & ~(1024-1);
149 }
150 } else {
151 // There's more than one character after the numeric part.
152 return 0;
153 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700154 }
155 // The man page says that a -Xm value must be a multiple of 1024.
156 if (val % div == 0) {
157 return val;
158 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700159 }
160 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700161 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700162}
163
Elliott Hughes0af55432011-08-17 18:37:28 -0700164void LoadJniLibrary(JavaVMExt* vm, const char* name) {
165 // TODO: OS_SHARED_LIB_FORMAT_STR
166 std::string mapped_name(StringPrintf("lib%s.so", name));
Elliott Hughes75770752011-08-24 17:52:38 -0700167 std::string reason;
168 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700169 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
170 << reason;
171 }
172}
173
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700174void CreateClassPath(const std::string& class_path,
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700175 std::vector<const DexFile*>& class_path_vector) {
Carl Shapirofc322c72011-07-27 00:20:01 -0700176 std::vector<std::string> parsed;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700177 Split(class_path, ':', parsed);
Carl Shapirofc322c72011-07-27 00:20:01 -0700178 for (size_t i = 0; i < parsed.size(); ++i) {
Brian Carlstrom16192862011-09-12 17:50:06 -0700179 const DexFile* dex_file = DexFile::Open(parsed[i], "");
Carl Shapirofc322c72011-07-27 00:20:01 -0700180 if (dex_file != NULL) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700181 class_path_vector.push_back(dex_file);
Carl Shapirofc322c72011-07-27 00:20:01 -0700182 }
183 }
184}
185
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700186Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700187 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700188 parsed->boot_image_ = NULL;
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700189#ifdef NDEBUG
Elliott Hughes5174fe62011-08-23 15:12:35 -0700190 // -Xcheck:jni is off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700191 parsed->check_jni_ = false;
192#else
193 // ...but on by default in debug builds.
194 parsed->check_jni_ = true;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700195#endif
Elliott Hughes85d15452011-09-16 17:33:01 -0700196
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700197 parsed->heap_initial_size_ = Heap::kInitialSize;
198 parsed->heap_maximum_size_ = Heap::kMaximumSize;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700199 parsed->stack_size_ = Thread::kDefaultStackSize;
200
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700201 parsed->hook_vfprintf_ = vfprintf;
202 parsed->hook_exit_ = exit;
203 parsed->hook_abort_ = abort;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700204
205 for (size_t i = 0; i < options.size(); ++i) {
206 const StringPiece& option = options[i].first;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700207 if (option.starts_with("-Xbootclasspath:")) {
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700208 parsed->boot_class_path_string_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700209 } else if (option == "bootclasspath") {
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700210 UNIMPLEMENTED(WARNING) << "what should VMRuntime.getBootClassPath return here?";
Brian Carlstromf734cf52011-08-17 16:28:14 -0700211 const void* dex_vector = options[i].second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700212 const std::vector<const DexFile*>* v
213 = reinterpret_cast<const std::vector<const DexFile*>*>(dex_vector);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700214 if (v == NULL) {
215 if (ignore_unrecognized) {
216 continue;
217 }
218 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700219 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700220 return NULL;
221 }
222 parsed->boot_class_path_ = *v;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700223 } else if (option == "classpath") {
224 const void* dex_vector = options[i].second;
225 const std::vector<const DexFile*>* v
226 = reinterpret_cast<const std::vector<const DexFile*>*>(dex_vector);
227 if (v == NULL) {
228 if (ignore_unrecognized) {
229 continue;
230 }
231 // TODO: usage
232 LOG(FATAL) << "Failed to parse " << option;
233 return NULL;
234 }
235 parsed->class_path_ = *v;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700236 } else if (option == "-classpath" || option == "-cp") {
237 // TODO: support -Djava.class.path
238 i++;
239 if (i == options.size()) {
240 // TODO: usage
241 LOG(FATAL) << "Missing required class path value for " << option;
242 return NULL;
243 }
244 const StringPiece& value = options[i].first;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700245 parsed->class_path_string_ = value.data();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700246 } else if (option.starts_with("-Xbootimage:")) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700247 // TODO: remove when intern_addr_ is removed, just use -Ximage:
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700248 parsed->boot_image_ = option.substr(strlen("-Xbootimage:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700249 } else if (option.starts_with("-Ximage:")) {
250 parsed->images_.push_back(option.substr(strlen("-Ximage:")).data());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700251 } else if (option.starts_with("-Xcheck:jni")) {
252 parsed->check_jni_ = true;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700253 } else if (option.starts_with("-Xms")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700254 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024);
255 if (size == 0) {
256 if (ignore_unrecognized) {
257 continue;
258 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700259 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700260 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700261 return NULL;
262 }
263 parsed->heap_initial_size_ = size;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700264 } else if (option.starts_with("-Xmx")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700265 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024);
266 if (size == 0) {
267 if (ignore_unrecognized) {
268 continue;
269 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700270 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700271 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700272 return NULL;
273 }
274 parsed->heap_maximum_size_ = size;
275 } else if (option.starts_with("-Xss")) {
276 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1);
277 if (size == 0) {
278 if (ignore_unrecognized) {
279 continue;
280 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700281 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700282 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700283 return NULL;
284 }
285 parsed->stack_size_ = size;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700286 } else if (option.starts_with("-D")) {
287 parsed->properties_.push_back(option.substr(strlen("-D")).data());
Elliott Hughesa0957642011-09-02 14:27:33 -0700288 } else if (option.starts_with("-Xjnitrace:")) {
289 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:")).data();
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700290 } else if (option.starts_with("-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700291 std::vector<std::string> verbose_options;
Elliott Hughes34023802011-08-30 12:06:17 -0700292 Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700293 for (size_t i = 0; i < verbose_options.size(); ++i) {
294 parsed->verbose_.insert(verbose_options[i]);
295 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700296 } else if (option == "vfprintf") {
297 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
298 } else if (option == "exit") {
299 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
300 } else if (option == "abort") {
301 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700302 } else {
303 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700304 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700305 LOG(ERROR) << "Unrecognized option " << option;
306 // TODO: this should exit, but for now tolerate unknown options
307 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700308 }
309 }
310 }
311
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700312 // Consider it an error if both bootclasspath and -Xbootclasspath: are supplied.
Brian Carlstrom78128a62011-09-15 17:21:19 -0700313 // TODO: remove bootclasspath and classpath which are mostly just used by tests?
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700314 if (!parsed->boot_class_path_.empty() && !parsed->boot_class_path_string_.empty()) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700315 // TODO: usage
316 LOG(FATAL) << "bootclasspath and -Xbootclasspath: are mutually exclusive options.";
317 return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700318 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700319 if (!parsed->class_path_.empty() && !parsed->class_path_string_.empty()) {
320 // TODO: usage
321 LOG(FATAL) << "bootclasspath and -Xbootclasspath: are mutually exclusive options.";
322 return NULL;
323 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700324 if (parsed->boot_class_path_.empty()) {
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700325 if (parsed->boot_class_path_string_ == NULL) {
326 const char* BOOTCLASSPATH = getenv("BOOTCLASSPATH");
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700327 if (BOOTCLASSPATH != NULL) {
328 parsed->boot_class_path_string_ = BOOTCLASSPATH;
329 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700330 }
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700331 CreateClassPath(parsed->boot_class_path_string_, parsed->boot_class_path_);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700332 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700333
Brian Carlstrom78128a62011-09-15 17:21:19 -0700334 if (parsed->class_path_.empty()) {
335 if (parsed->class_path_string_ == NULL) {
336 const char* CLASSPATH = getenv("CLASSPATH");
337 if (CLASSPATH != NULL) {
338 parsed->class_path_string_ = CLASSPATH;
339 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700340 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700341 CreateClassPath(parsed->class_path_string_, parsed->class_path_);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700342 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700343
Elliott Hughes85d15452011-09-16 17:33:01 -0700344 LOG(INFO) << "CheckJNI is " << (parsed->check_jni_ ? "on" : "off");
345
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700346 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700347}
348
349Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700350 // TODO: acquire a static mutex on Runtime to avoid racing.
351 if (Runtime::instance_ != NULL) {
352 return NULL;
353 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700354 instance_ = new Runtime;
355 if (!instance_->Init(options, ignore_unrecognized)) {
356 delete instance_;
357 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700358 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700359 return instance_;
360}
Elliott Hughes0af55432011-08-17 18:37:28 -0700361
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700362void Runtime::Start() {
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700363 if (IsVerboseStartup()) {
364 LOG(INFO) << "Runtime::Start entering";
365 }
Elliott Hughes038a8062011-09-18 14:12:41 -0700366 InitNativeMethods();
Elliott Hughes85d15452011-09-16 17:33:01 -0700367
Elliott Hughes038a8062011-09-18 14:12:41 -0700368 Thread::FinishStartup();
Elliott Hughes85d15452011-09-16 17:33:01 -0700369
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700370 class_linker_->RunRootClinits();
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700371
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700372 // Class::AllocObject asserts that all objects allocated better be
373 // initialized after Runtime::IsStarted is true, so this needs to
374 // come after ClassLinker::RunRootClinits.
375 started_ = true;
376
Elliott Hughes85d15452011-09-16 17:33:01 -0700377 StartDaemonThreads();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700378
379 if (IsVerboseStartup()) {
380 LOG(INFO) << "Runtime::Start exiting";
381 }
Elliott Hughes85d15452011-09-16 17:33:01 -0700382}
383
384void Runtime::StartDaemonThreads() {
385 signal_catcher_ = new SignalCatcher;
386
Elliott Hughes719b3232011-09-25 17:42:19 -0700387 Thread* self = Thread::Current();
388 JNIEnv* env = self->GetJniEnv();
389 jclass c = env->FindClass("java/lang/Daemons");
Elliott Hughes85d15452011-09-16 17:33:01 -0700390 CHECK(c != NULL);
Elliott Hughes719b3232011-09-25 17:42:19 -0700391 jmethodID mid = env->GetStaticMethodID(c, "start", "()V");
392 CHECK(mid != NULL);
393 env->CallStaticVoidMethod(c, mid);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700394}
395
Elliott Hughesdcc24742011-09-07 14:02:44 -0700396bool Runtime::IsStarted() {
397 return started_;
398}
399
Elliott Hughes0af55432011-08-17 18:37:28 -0700400bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700401 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700402
Elliott Hughes90a33692011-08-30 13:27:07 -0700403 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
404 if (options.get() == NULL) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700405 LOG(WARNING) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700406 return false;
407 }
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700408 verbose_startup_ = options->IsVerbose("startup");
409 if (IsVerboseStartup()) {
410 LOG(INFO) << "Runtime::Init -verbose:startup enabled";
411 }
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700412
413 boot_class_path_ = options->boot_class_path_string_;
414 class_path_ = options->class_path_string_;
415 properties_ = options->properties_;
416
Elliott Hughes0af55432011-08-17 18:37:28 -0700417 vfprintf_ = options->hook_vfprintf_;
418 exit_ = options->hook_exit_;
419 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700420
Elliott Hughesbe759c62011-09-08 19:38:21 -0700421 default_stack_size_ = options->stack_size_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700422
Elliott Hughes14357e82011-09-26 10:42:15 -0700423 thread_list_ = new ThreadList(options->IsVerbose("thread"));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700424 intern_table_ = new InternTable;
425
Elliott Hughesbe759c62011-09-08 19:38:21 -0700426 Heap::Init(options->heap_initial_size_, options->heap_maximum_size_,
427 options->boot_image_, options->images_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700428
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700429 BlockSignals();
430
Elliott Hughesa0957642011-09-02 14:27:33 -0700431 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700432
Elliott Hughesbe759c62011-09-08 19:38:21 -0700433 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700434
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700435 // ClassLinker needs an attached thread, but we can't fully attach a thread
436 // without creating objects.
437 Thread::Attach(this, "main", false);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700438
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700439 class_linker_ = ClassLinker::Create(options->boot_class_path_,
440 options->class_path_,
441 intern_table_,
442 Heap::GetBootSpace());
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700443
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700444 if (IsVerboseStartup()) {
445 LOG(INFO) << "Runtime::Init exiting";
446 }
Carl Shapiro1fb86202011-06-27 17:43:13 -0700447 return true;
448}
449
Elliott Hughes038a8062011-09-18 14:12:41 -0700450void Runtime::InitNativeMethods() {
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700451 if (IsVerboseStartup()) {
452 LOG(INFO) << "Runtime::InitNativeMethods entering";
453 }
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700454 Thread* self = Thread::Current();
455 JNIEnv* env = self->GetJniEnv();
456
Elliott Hughes418d20f2011-09-22 14:00:39 -0700457 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700458 ScopedThreadStateChange tsc(self, Thread::kNative);
459
Elliott Hughes418d20f2011-09-22 14:00:39 -0700460 // First set up JniConstants, which is used by both the runtime's built-in native
461 // methods and libcore.
Elliott Hughesfea966e2011-09-22 10:26:20 -0700462 JniConstants::init(env);
463
Elliott Hughes418d20f2011-09-22 14:00:39 -0700464 // Then set up the native methods provided by the runtime itself.
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700465 RegisterRuntimeNativeMethods(env);
466
Elliott Hughes418d20f2011-09-22 14:00:39 -0700467 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
468 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
469 // the library that implements System.loadLibrary!
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700470 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700471 if (IsVerboseStartup()) {
472 LOG(INFO) << "Runtime::InitNativeMethods exiting";
473 }
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700474}
475
476void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
477#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700478 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700479 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700480 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700481 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700482 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700483 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700484 REGISTER(register_java_lang_Object);
485 REGISTER(register_java_lang_Runtime);
486 REGISTER(register_java_lang_String);
487 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700488 REGISTER(register_java_lang_Thread);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700489 REGISTER(register_java_lang_Throwable);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700490 REGISTER(register_java_lang_VMClassLoader);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700491 //REGISTER(register_java_lang_reflect_AccessibleObject);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700492 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700493 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700494 REGISTER(register_java_lang_reflect_Field);
495 REGISTER(register_java_lang_reflect_Method);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700496 //REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700497 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700498 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700499 //REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700500 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700501#undef REGISTER
502}
503
Elliott Hughes8daa0922011-09-11 13:46:25 -0700504void Runtime::Dump(std::ostream& os) {
Elliott Hughese27955c2011-08-26 15:21:24 -0700505 // TODO: dump other runtime statistics?
506 os << "Loaded classes: " << class_linker_->NumLoadedClasses() << "\n";
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700507 os << "Intern table size: " << GetInternTable()->Size() << "\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700508 // LOGV("VM stats: meth=%d ifld=%d sfld=%d linear=%d",
509 // gDvm.numDeclaredMethods,
510 // gDvm.numDeclaredInstFields,
511 // gDvm.numDeclaredStaticFields,
512 // gDvm.pBootLoaderAlloc->curOffset);
513 // LOGI("GC precise methods: %d", dvmPointerSetGetCount(gDvm.preciseMethods));
Elliott Hughes42ee1422011-09-06 12:33:32 -0700514 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700515
516 thread_list_->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700517}
518
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700519void Runtime::SetStatsEnabled(bool new_state) {
520 if (new_state == true) {
521 GetStats()->Clear(~0);
522 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
523 Thread::Current()->GetStats()->Clear(~0);
524 }
525 stats_enabled_ = new_state;
526}
527
528void Runtime::ResetStats(int kinds) {
529 GetStats()->Clear(kinds & 0xffff);
530 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
531 Thread::Current()->GetStats()->Clear(kinds >> 16);
532}
533
534RuntimeStats* Runtime::GetStats() {
535 return &stats_;
536}
537
538int32_t Runtime::GetStat(int kind) {
539 RuntimeStats* stats;
540 if (kind < (1<<16)) {
541 stats = GetStats();
542 } else {
543 stats = Thread::Current()->GetStats();
544 kind >>= 16;
545 }
546 switch (kind) {
547 case KIND_ALLOCATED_OBJECTS:
548 return stats->allocated_objects;
549 case KIND_ALLOCATED_BYTES:
550 return stats->allocated_bytes;
551 case KIND_FREED_OBJECTS:
552 return stats->freed_objects;
553 case KIND_FREED_BYTES:
554 return stats->freed_bytes;
555 case KIND_GC_INVOCATIONS:
556 return stats->gc_for_alloc_count;
557 case KIND_CLASS_INIT_COUNT:
558 return stats->class_init_count;
559 case KIND_CLASS_INIT_TIME:
560 // Convert ns to us, reduce to 32 bits.
561 return (int) (stats->class_init_time_ns / 1000);
562 case KIND_EXT_ALLOCATED_OBJECTS:
563 case KIND_EXT_ALLOCATED_BYTES:
564 case KIND_EXT_FREED_OBJECTS:
565 case KIND_EXT_FREED_BYTES:
566 return 0; // backward compatibility
567 default:
568 CHECK(false);
569 return -1; // unreachable
570 }
571}
572
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700573void Runtime::BlockSignals() {
574 sigset_t sigset;
575 if (sigemptyset(&sigset) == -1) {
576 PLOG(FATAL) << "sigemptyset failed";
577 }
578 if (sigaddset(&sigset, SIGPIPE) == -1) {
579 PLOG(ERROR) << "sigaddset SIGPIPE failed";
580 }
581 // SIGQUIT is used to dump the runtime's state (including stack traces).
582 if (sigaddset(&sigset, SIGQUIT) == -1) {
583 PLOG(ERROR) << "sigaddset SIGQUIT failed";
584 }
585 // SIGUSR1 is used to initiate a heap dump.
586 if (sigaddset(&sigset, SIGUSR1) == -1) {
587 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
588 }
589 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
590}
591
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700592void Runtime::AttachCurrentThread(const char* name, bool as_daemon) {
593 Thread::Attach(instance_, name, as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700594}
595
Elliott Hughesd92bec42011-09-02 17:04:36 -0700596void Runtime::DetachCurrentThread() {
Elliott Hughes038a8062011-09-18 14:12:41 -0700597 // TODO: check we're not calling DetachCurrentThread from a call stack that
598 // includes managed frames. (It's only valid if the stack is all-native.)
Elliott Hughes02b48d12011-09-07 17:15:51 -0700599 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700600}
601
Ian Rogersff1ed472011-09-20 13:46:24 -0700602Method* Runtime::CreateCalleeSaveMethod(InstructionSet insns) {
603 Class* method_class = Method::GetMethodClass();
604 Method* method = down_cast<Method*>(method_class->AllocObject());
605 method->SetDeclaringClass(method_class);
606 method->SetName(intern_table_->InternStrong("$$$callee_save_method$$$"));
607 method->SetSignature(intern_table_->InternStrong("()V"));
608 method->SetCode(NULL, insns, NULL);
609 if ((insns == kThumb2) || (insns == kArm)) {
Ian Rogers15fdb8c2011-09-25 15:45:07 -0700610 size_t frame_size = (12 /* gprs */ + 32 /* fprs */ + 4 /* data */) * kPointerSize;
611 method->SetFrameSizeInBytes(frame_size);
612 method->SetReturnPcOffsetInBytes(frame_size - kPointerSize);
Ian Rogersff1ed472011-09-20 13:46:24 -0700613 method->SetCoreSpillMask((1 << art::arm::R1) |
614 (1 << art::arm::R2) |
615 (1 << art::arm::R3) |
616 (1 << art::arm::R4) |
617 (1 << art::arm::R5) |
618 (1 << art::arm::R6) |
619 (1 << art::arm::R7) |
620 (1 << art::arm::R8) |
621 (1 << art::arm::R9) |
622 (1 << art::arm::R10) |
623 (1 << art::arm::R11) |
624 (1 << art::arm::LR));
Ian Rogers15fdb8c2011-09-25 15:45:07 -0700625 method->SetFpSpillMask((1 << art::arm::S0) |
626 (1 << art::arm::S1) |
627 (1 << art::arm::S2) |
628 (1 << art::arm::S3) |
629 (1 << art::arm::S4) |
630 (1 << art::arm::S5) |
631 (1 << art::arm::S6) |
632 (1 << art::arm::S7) |
633 (1 << art::arm::S8) |
634 (1 << art::arm::S9) |
635 (1 << art::arm::S10) |
636 (1 << art::arm::S11) |
637 (1 << art::arm::S12) |
638 (1 << art::arm::S13) |
639 (1 << art::arm::S14) |
640 (1 << art::arm::S15) |
641 (1 << art::arm::S16) |
642 (1 << art::arm::S17) |
643 (1 << art::arm::S18) |
644 (1 << art::arm::S19) |
645 (1 << art::arm::S20) |
646 (1 << art::arm::S21) |
647 (1 << art::arm::S22) |
648 (1 << art::arm::S23) |
649 (1 << art::arm::S24) |
650 (1 << art::arm::S25) |
651 (1 << art::arm::S26) |
652 (1 << art::arm::S27) |
653 (1 << art::arm::S28) |
654 (1 << art::arm::S29) |
655 (1 << art::arm::S30) |
656 (1 << art::arm::S31));
Ian Rogersff1ed472011-09-20 13:46:24 -0700657 } else if (insns == kX86) {
658 method->SetFrameSizeInBytes(32);
659 method->SetReturnPcOffsetInBytes(28);
660 method->SetCoreSpillMask((1 << art::x86::EBX) |
661 (1 << art::x86::EBP) |
662 (1 << art::x86::ESI) |
663 (1 << art::x86::EDI));
664 method->SetFpSpillMask(0);
665 } else {
666 UNIMPLEMENTED(FATAL);
667 }
668 return method;
669}
670
Elliott Hughes410c0c82011-09-01 17:58:25 -0700671void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
672 class_linker_->VisitRoots(visitor, arg);
673 intern_table_->VisitRoots(visitor, arg);
674 java_vm_->VisitRoots(visitor, arg);
675 thread_list_->VisitRoots(visitor, arg);
Brian Carlstrom16192862011-09-12 17:50:06 -0700676 visitor(jni_stub_array_, arg);
Ian Rogersff1ed472011-09-20 13:46:24 -0700677 visitor(callee_save_method_, arg);
Elliott Hughes410c0c82011-09-01 17:58:25 -0700678
679 //(*visitor)(&gDvm.outOfMemoryObj, 0, ROOT_VM_INTERNAL, arg);
680 //(*visitor)(&gDvm.internalErrorObj, 0, ROOT_VM_INTERNAL, arg);
681 //(*visitor)(&gDvm.noClassDefFoundErrorObj, 0, ROOT_VM_INTERNAL, arg);
682 UNIMPLEMENTED(WARNING) << "some roots not marked";
Brian Carlstrom1f870082011-08-23 16:02:11 -0700683}
684
Carl Shapiro1fb86202011-06-27 17:43:13 -0700685} // namespace art