blob: e6bd6e23bcc3394714a4485b924bfe509f9bff69 [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),
Elliott Hughesdcc24742011-09-07 14:02:44 -070034 started_(false),
35 vfprintf_(NULL),
36 exit_(NULL),
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070037 abort_(NULL),
38 stats_enabled_(false) {
Elliott Hughesdcc24742011-09-07 14:02:44 -070039}
40
Carl Shapiro61e019d2011-07-14 16:53:09 -070041Runtime::~Runtime() {
Elliott Hughes5fe594f2011-09-08 12:33:17 -070042 // Make sure our internal threads are dead before we start tearing down things they're using.
43 delete signal_catcher_;
Elliott Hughes038a8062011-09-18 14:12:41 -070044 // TODO: GC thread.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070045
Elliott Hughes038a8062011-09-18 14:12:41 -070046 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
Elliott Hughes93e74e82011-09-13 11:07:03 -070047 delete thread_list_;
48
Carl Shapiro61e019d2011-07-14 16:53:09 -070049 delete class_linker_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070050 Heap::Destroy();
Elliott Hughescf4c6c42011-09-01 15:16:42 -070051 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070052 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -070053 Thread::Shutdown();
Carl Shapiro4acf4642011-07-26 18:54:13 -070054 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070055 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -070056 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -070057}
58
Elliott Hughesffe67362011-07-17 12:09:27 -070059void Runtime::Abort(const char* file, int line) {
60 // Get any pending output out of the way.
61 fflush(NULL);
62
63 // Many people have difficulty distinguish aborts from crashes,
64 // so be explicit.
65 LogMessage(file, line, ERROR, -1).stream() << "Runtime aborting...";
66
Elliott Hughesffe67362011-07-17 12:09:27 -070067 // Perform any platform-specific pre-abort actions.
68 PlatformAbort(file, line);
69
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070070 // use abort hook if we have one
71 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
72 Runtime::Current()->abort_();
73 // notreached
74 }
75
Elliott Hughesffe67362011-07-17 12:09:27 -070076 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -070077 // receive SIGABRT. debuggerd dumps the stack trace of the main
78 // thread, whether or not that was the thread that failed. By
79 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -070080 // fault in the current thread, and get a useful log from debuggerd.
81 // We can also trivially tell the difference between a VM crash and
82 // a deliberate abort by looking at the fault address.
83 *reinterpret_cast<char*>(0xdeadd00d) = 38;
84 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -070085 // notreached
86}
87
Elliott Hughesbf86d042011-08-31 17:53:14 -070088void Runtime::CallExitHook(jint status) {
89 if (exit_ != NULL) {
90 ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative);
91 exit_(status);
92 LOG(WARNING) << "Exit hook returned instead of exiting!";
93 }
94}
95
Brian Carlstrom8a436592011-08-15 21:27:23 -070096// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
97// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
98// [gG] gigabytes.
99//
100// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700101// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
102// of 1024.
103//
104// The spec says the -Xmx and -Xms options must be multiples of 1024. It
105// doesn't say anything about -Xss.
106//
107// Returns 0 (a useless size) if "s" is malformed or specifies a low or
108// non-evenly-divisible value.
109//
110size_t ParseMemoryOption(const char *s, size_t div) {
111 // strtoul accepts a leading [+-], which we don't want,
112 // so make sure our string starts with a decimal digit.
113 if (isdigit(*s)) {
114 const char *s2;
115 size_t val = strtoul(s, (char **)&s2, 10);
116 if (s2 != s) {
117 // s2 should be pointing just after the number.
118 // If this is the end of the string, the user
119 // has specified a number of bytes. Otherwise,
120 // there should be exactly one more character
121 // that specifies a multiplier.
122 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700123 // The remainder of the string is either a single multiplier
124 // character, or nothing to indicate that the value is in
125 // bytes.
126 char c = *s2++;
127 if (*s2 == '\0') {
128 size_t mul;
129 if (c == '\0') {
130 mul = 1;
131 } else if (c == 'k' || c == 'K') {
132 mul = KB;
133 } else if (c == 'm' || c == 'M') {
134 mul = MB;
135 } else if (c == 'g' || c == 'G') {
136 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700137 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700138 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700139 return 0;
140 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700141
142 if (val <= std::numeric_limits<size_t>::max() / mul) {
143 val *= mul;
144 } else {
145 // Clamp to a multiple of 1024.
146 val = std::numeric_limits<size_t>::max() & ~(1024-1);
147 }
148 } else {
149 // There's more than one character after the numeric part.
150 return 0;
151 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700152 }
153 // The man page says that a -Xm value must be a multiple of 1024.
154 if (val % div == 0) {
155 return val;
156 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700157 }
158 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700159 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700160}
161
Elliott Hughes0af55432011-08-17 18:37:28 -0700162void LoadJniLibrary(JavaVMExt* vm, const char* name) {
163 // TODO: OS_SHARED_LIB_FORMAT_STR
164 std::string mapped_name(StringPrintf("lib%s.so", name));
Elliott Hughes75770752011-08-24 17:52:38 -0700165 std::string reason;
166 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700167 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
168 << reason;
169 }
170}
171
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700172void CreateClassPath(const std::string& class_path,
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700173 std::vector<const DexFile*>& class_path_vector) {
Carl Shapirofc322c72011-07-27 00:20:01 -0700174 std::vector<std::string> parsed;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700175 Split(class_path, ':', parsed);
Carl Shapirofc322c72011-07-27 00:20:01 -0700176 for (size_t i = 0; i < parsed.size(); ++i) {
Brian Carlstrom16192862011-09-12 17:50:06 -0700177 const DexFile* dex_file = DexFile::Open(parsed[i], "");
Carl Shapirofc322c72011-07-27 00:20:01 -0700178 if (dex_file != NULL) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700179 class_path_vector.push_back(dex_file);
Carl Shapirofc322c72011-07-27 00:20:01 -0700180 }
181 }
182}
183
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700184Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700185 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700186 parsed->boot_image_ = NULL;
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700187#ifdef NDEBUG
Elliott Hughes5174fe62011-08-23 15:12:35 -0700188 // -Xcheck:jni is off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700189 parsed->check_jni_ = false;
190#else
191 // ...but on by default in debug builds.
192 parsed->check_jni_ = true;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700193#endif
Elliott Hughes85d15452011-09-16 17:33:01 -0700194
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700195 parsed->heap_initial_size_ = Heap::kInitialSize;
196 parsed->heap_maximum_size_ = Heap::kMaximumSize;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700197 parsed->stack_size_ = Thread::kDefaultStackSize;
198
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700199 parsed->hook_vfprintf_ = vfprintf;
200 parsed->hook_exit_ = exit;
201 parsed->hook_abort_ = abort;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700202
203 for (size_t i = 0; i < options.size(); ++i) {
204 const StringPiece& option = options[i].first;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700205 if (option.starts_with("-Xbootclasspath:")) {
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700206 parsed->boot_class_path_string_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700207 } else if (option == "bootclasspath") {
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700208 UNIMPLEMENTED(WARNING) << "what should VMRuntime.getBootClassPath return here?";
Brian Carlstromf734cf52011-08-17 16:28:14 -0700209 const void* dex_vector = options[i].second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700210 const std::vector<const DexFile*>* v
211 = reinterpret_cast<const std::vector<const DexFile*>*>(dex_vector);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700212 if (v == NULL) {
213 if (ignore_unrecognized) {
214 continue;
215 }
216 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700217 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700218 return NULL;
219 }
220 parsed->boot_class_path_ = *v;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700221 } else if (option == "classpath") {
222 const void* dex_vector = options[i].second;
223 const std::vector<const DexFile*>* v
224 = reinterpret_cast<const std::vector<const DexFile*>*>(dex_vector);
225 if (v == NULL) {
226 if (ignore_unrecognized) {
227 continue;
228 }
229 // TODO: usage
230 LOG(FATAL) << "Failed to parse " << option;
231 return NULL;
232 }
233 parsed->class_path_ = *v;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700234 } else if (option == "-classpath" || option == "-cp") {
235 // TODO: support -Djava.class.path
236 i++;
237 if (i == options.size()) {
238 // TODO: usage
239 LOG(FATAL) << "Missing required class path value for " << option;
240 return NULL;
241 }
242 const StringPiece& value = options[i].first;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700243 parsed->class_path_string_ = value.data();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700244 } else if (option.starts_with("-Xbootimage:")) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700245 // TODO: remove when intern_addr_ is removed, just use -Ximage:
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700246 parsed->boot_image_ = option.substr(strlen("-Xbootimage:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700247 } else if (option.starts_with("-Ximage:")) {
248 parsed->images_.push_back(option.substr(strlen("-Ximage:")).data());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700249 } else if (option.starts_with("-Xcheck:jni")) {
250 parsed->check_jni_ = true;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700251 } else if (option.starts_with("-Xms")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700252 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024);
253 if (size == 0) {
254 if (ignore_unrecognized) {
255 continue;
256 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700257 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700258 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700259 return NULL;
260 }
261 parsed->heap_initial_size_ = size;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700262 } else if (option.starts_with("-Xmx")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700263 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024);
264 if (size == 0) {
265 if (ignore_unrecognized) {
266 continue;
267 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700268 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700269 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700270 return NULL;
271 }
272 parsed->heap_maximum_size_ = size;
273 } else if (option.starts_with("-Xss")) {
274 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1);
275 if (size == 0) {
276 if (ignore_unrecognized) {
277 continue;
278 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700279 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700280 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700281 return NULL;
282 }
283 parsed->stack_size_ = size;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700284 } else if (option.starts_with("-D")) {
285 parsed->properties_.push_back(option.substr(strlen("-D")).data());
Elliott Hughesa0957642011-09-02 14:27:33 -0700286 } else if (option.starts_with("-Xjnitrace:")) {
287 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:")).data();
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700288 } else if (option.starts_with("-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700289 std::vector<std::string> verbose_options;
Elliott Hughes34023802011-08-30 12:06:17 -0700290 Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700291 for (size_t i = 0; i < verbose_options.size(); ++i) {
292 parsed->verbose_.insert(verbose_options[i]);
293 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700294 } else if (option == "vfprintf") {
295 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
296 } else if (option == "exit") {
297 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
298 } else if (option == "abort") {
299 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700300 } else {
301 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700302 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700303 LOG(ERROR) << "Unrecognized option " << option;
304 // TODO: this should exit, but for now tolerate unknown options
305 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700306 }
307 }
308 }
309
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700310 // Consider it an error if both bootclasspath and -Xbootclasspath: are supplied.
Brian Carlstrom78128a62011-09-15 17:21:19 -0700311 // TODO: remove bootclasspath and classpath which are mostly just used by tests?
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700312 if (!parsed->boot_class_path_.empty() && !parsed->boot_class_path_string_.empty()) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700313 // TODO: usage
314 LOG(FATAL) << "bootclasspath and -Xbootclasspath: are mutually exclusive options.";
315 return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700316 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700317 if (!parsed->class_path_.empty() && !parsed->class_path_string_.empty()) {
318 // TODO: usage
319 LOG(FATAL) << "bootclasspath and -Xbootclasspath: are mutually exclusive options.";
320 return NULL;
321 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700322 if (parsed->boot_class_path_.empty()) {
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700323 if (parsed->boot_class_path_string_ == NULL) {
324 const char* BOOTCLASSPATH = getenv("BOOTCLASSPATH");
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700325 if (BOOTCLASSPATH != NULL) {
326 parsed->boot_class_path_string_ = BOOTCLASSPATH;
327 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700328 }
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700329 CreateClassPath(parsed->boot_class_path_string_, parsed->boot_class_path_);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700330 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700331
Brian Carlstrom78128a62011-09-15 17:21:19 -0700332 if (parsed->class_path_.empty()) {
333 if (parsed->class_path_string_ == NULL) {
334 const char* CLASSPATH = getenv("CLASSPATH");
335 if (CLASSPATH != NULL) {
336 parsed->class_path_string_ = CLASSPATH;
337 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700338 }
Brian Carlstrom78128a62011-09-15 17:21:19 -0700339 CreateClassPath(parsed->class_path_string_, parsed->class_path_);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700340 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700341
Elliott Hughes85d15452011-09-16 17:33:01 -0700342 LOG(INFO) << "CheckJNI is " << (parsed->check_jni_ ? "on" : "off");
343
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700344 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700345}
346
347Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700348 // TODO: acquire a static mutex on Runtime to avoid racing.
349 if (Runtime::instance_ != NULL) {
350 return NULL;
351 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700352 instance_ = new Runtime;
353 if (!instance_->Init(options, ignore_unrecognized)) {
354 delete instance_;
355 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700356 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700357 return instance_;
358}
Elliott Hughes0af55432011-08-17 18:37:28 -0700359
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700360void Runtime::Start() {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700361 started_ = true;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700362
Elliott Hughes038a8062011-09-18 14:12:41 -0700363 InitNativeMethods();
Elliott Hughes85d15452011-09-16 17:33:01 -0700364
Elliott Hughes038a8062011-09-18 14:12:41 -0700365 Thread::FinishStartup();
Elliott Hughes85d15452011-09-16 17:33:01 -0700366
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700367 RunImageClinits();
368
Elliott Hughes85d15452011-09-16 17:33:01 -0700369 StartDaemonThreads();
370}
371
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700372// initialize classes that have instances in the image but that have
373// <clinit> methods so they could not be initialized by the compiler.
374void Runtime::RunImageClinits() {
375 Class* Field_class = class_linker_->FindSystemClass("Ljava/lang/reflect/Field;");
376 CHECK(Field_class->FindDeclaredDirectMethod("<clinit>", "()V") != NULL);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700377 class_linker_->EnsureInitialized(Field_class, true);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700378 CHECK(!Thread::Current()->IsExceptionPending());
379}
380
Elliott Hughes85d15452011-09-16 17:33:01 -0700381void Runtime::StartDaemonThreads() {
382 signal_catcher_ = new SignalCatcher;
383
384 Class* c = class_linker_->FindSystemClass("Ljava/lang/Daemons;");
385 CHECK(c != NULL);
386 Method* m = c->FindDirectMethod("start", "()V");
387 CHECK(m != NULL);
Elliott Hughes038a8062011-09-18 14:12:41 -0700388 m->Invoke(Thread::Current(), NULL, NULL, NULL);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700389}
390
Elliott Hughesdcc24742011-09-07 14:02:44 -0700391bool Runtime::IsStarted() {
392 return started_;
393}
394
Elliott Hughes0af55432011-08-17 18:37:28 -0700395bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700396 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700397
Elliott Hughes90a33692011-08-30 13:27:07 -0700398 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
399 if (options.get() == NULL) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700400 LOG(WARNING) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700401 return false;
402 }
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700403
404 boot_class_path_ = options->boot_class_path_string_;
405 class_path_ = options->class_path_string_;
406 properties_ = options->properties_;
407
Elliott Hughes0af55432011-08-17 18:37:28 -0700408 vfprintf_ = options->hook_vfprintf_;
409 exit_ = options->hook_exit_;
410 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700411
Elliott Hughesbe759c62011-09-08 19:38:21 -0700412 default_stack_size_ = options->stack_size_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700413
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700414 thread_list_ = new ThreadList;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700415 intern_table_ = new InternTable;
416
Elliott Hughesbe759c62011-09-08 19:38:21 -0700417 Heap::Init(options->heap_initial_size_, options->heap_maximum_size_,
418 options->boot_image_, options->images_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700419
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700420 BlockSignals();
421
Elliott Hughesa0957642011-09-02 14:27:33 -0700422 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700423
Elliott Hughesbe759c62011-09-08 19:38:21 -0700424 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700425
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700426 // ClassLinker needs an attached thread, but we can't fully attach a thread
427 // without creating objects.
428 Thread::Attach(this, "main", false);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700429
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700430 class_linker_ = ClassLinker::Create(options->boot_class_path_,
431 options->class_path_,
432 intern_table_,
433 Heap::GetBootSpace());
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700434
Carl Shapiro1fb86202011-06-27 17:43:13 -0700435 return true;
436}
437
Elliott Hughes038a8062011-09-18 14:12:41 -0700438void Runtime::InitNativeMethods() {
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700439 Thread* self = Thread::Current();
440 JNIEnv* env = self->GetJniEnv();
441
442 // Must be in the kNative state for JNI-based method registration.
443 ScopedThreadStateChange tsc(self, Thread::kNative);
444
445 // First set up the native methods provided by the runtime itself.
446 RegisterRuntimeNativeMethods(env);
447
448 // Now set up libcore, which is just a JNI library with a JNI_OnLoad.
449 // Most JNI libraries can just use System.loadLibrary, but you can't
450 // if you're the library that implements System.loadLibrary!
451 JniConstants::init(env);
452 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
453}
454
455void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
456#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700457 //REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700458 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700459 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700460 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700461 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700462 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700463 REGISTER(register_java_lang_Object);
464 REGISTER(register_java_lang_Runtime);
465 REGISTER(register_java_lang_String);
466 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700467 REGISTER(register_java_lang_Thread);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700468 REGISTER(register_java_lang_Throwable);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700469 REGISTER(register_java_lang_VMClassLoader);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700470 //REGISTER(register_java_lang_reflect_AccessibleObject);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700471 REGISTER(register_java_lang_reflect_Array);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700472 //REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700473 REGISTER(register_java_lang_reflect_Field);
474 REGISTER(register_java_lang_reflect_Method);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700475 //REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700476 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700477 //REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
478 //REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700479 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700480#undef REGISTER
481}
482
Elliott Hughes8daa0922011-09-11 13:46:25 -0700483void Runtime::Dump(std::ostream& os) {
Elliott Hughese27955c2011-08-26 15:21:24 -0700484 // TODO: dump other runtime statistics?
485 os << "Loaded classes: " << class_linker_->NumLoadedClasses() << "\n";
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700486 os << "Intern table size: " << GetInternTable()->Size() << "\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700487 // LOGV("VM stats: meth=%d ifld=%d sfld=%d linear=%d",
488 // gDvm.numDeclaredMethods,
489 // gDvm.numDeclaredInstFields,
490 // gDvm.numDeclaredStaticFields,
491 // gDvm.pBootLoaderAlloc->curOffset);
492 // LOGI("GC precise methods: %d", dvmPointerSetGetCount(gDvm.preciseMethods));
Elliott Hughes42ee1422011-09-06 12:33:32 -0700493 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700494
495 thread_list_->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700496}
497
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700498void Runtime::SetStatsEnabled(bool new_state) {
499 if (new_state == true) {
500 GetStats()->Clear(~0);
501 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
502 Thread::Current()->GetStats()->Clear(~0);
503 }
504 stats_enabled_ = new_state;
505}
506
507void Runtime::ResetStats(int kinds) {
508 GetStats()->Clear(kinds & 0xffff);
509 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
510 Thread::Current()->GetStats()->Clear(kinds >> 16);
511}
512
513RuntimeStats* Runtime::GetStats() {
514 return &stats_;
515}
516
517int32_t Runtime::GetStat(int kind) {
518 RuntimeStats* stats;
519 if (kind < (1<<16)) {
520 stats = GetStats();
521 } else {
522 stats = Thread::Current()->GetStats();
523 kind >>= 16;
524 }
525 switch (kind) {
526 case KIND_ALLOCATED_OBJECTS:
527 return stats->allocated_objects;
528 case KIND_ALLOCATED_BYTES:
529 return stats->allocated_bytes;
530 case KIND_FREED_OBJECTS:
531 return stats->freed_objects;
532 case KIND_FREED_BYTES:
533 return stats->freed_bytes;
534 case KIND_GC_INVOCATIONS:
535 return stats->gc_for_alloc_count;
536 case KIND_CLASS_INIT_COUNT:
537 return stats->class_init_count;
538 case KIND_CLASS_INIT_TIME:
539 // Convert ns to us, reduce to 32 bits.
540 return (int) (stats->class_init_time_ns / 1000);
541 case KIND_EXT_ALLOCATED_OBJECTS:
542 case KIND_EXT_ALLOCATED_BYTES:
543 case KIND_EXT_FREED_OBJECTS:
544 case KIND_EXT_FREED_BYTES:
545 return 0; // backward compatibility
546 default:
547 CHECK(false);
548 return -1; // unreachable
549 }
550}
551
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700552void Runtime::BlockSignals() {
553 sigset_t sigset;
554 if (sigemptyset(&sigset) == -1) {
555 PLOG(FATAL) << "sigemptyset failed";
556 }
557 if (sigaddset(&sigset, SIGPIPE) == -1) {
558 PLOG(ERROR) << "sigaddset SIGPIPE failed";
559 }
560 // SIGQUIT is used to dump the runtime's state (including stack traces).
561 if (sigaddset(&sigset, SIGQUIT) == -1) {
562 PLOG(ERROR) << "sigaddset SIGQUIT failed";
563 }
564 // SIGUSR1 is used to initiate a heap dump.
565 if (sigaddset(&sigset, SIGUSR1) == -1) {
566 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
567 }
568 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
569}
570
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700571void Runtime::AttachCurrentThread(const char* name, bool as_daemon) {
572 Thread::Attach(instance_, name, as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700573}
574
Elliott Hughesd92bec42011-09-02 17:04:36 -0700575void Runtime::DetachCurrentThread() {
Elliott Hughes038a8062011-09-18 14:12:41 -0700576 // TODO: check we're not calling DetachCurrentThread from a call stack that
577 // includes managed frames. (It's only valid if the stack is all-native.)
Elliott Hughes02b48d12011-09-07 17:15:51 -0700578 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700579}
580
Elliott Hughes410c0c82011-09-01 17:58:25 -0700581void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
582 class_linker_->VisitRoots(visitor, arg);
583 intern_table_->VisitRoots(visitor, arg);
584 java_vm_->VisitRoots(visitor, arg);
585 thread_list_->VisitRoots(visitor, arg);
Brian Carlstrom16192862011-09-12 17:50:06 -0700586 visitor(jni_stub_array_, arg);
Elliott Hughes410c0c82011-09-01 17:58:25 -0700587
588 //(*visitor)(&gDvm.outOfMemoryObj, 0, ROOT_VM_INTERNAL, arg);
589 //(*visitor)(&gDvm.internalErrorObj, 0, ROOT_VM_INTERNAL, arg);
590 //(*visitor)(&gDvm.noClassDefFoundErrorObj, 0, ROOT_VM_INTERNAL, arg);
591 UNIMPLEMENTED(WARNING) << "some roots not marked";
Brian Carlstrom1f870082011-08-23 16:02:11 -0700592}
593
Carl Shapiro1fb86202011-06-27 17:43:13 -0700594} // namespace art