blob: 454a38ceafd9be03cf165804e734ee9a6e6f0a80 [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),
33 started_(false),
34 vfprintf_(NULL),
35 exit_(NULL),
36 abort_(NULL) {
37}
38
Carl Shapiro61e019d2011-07-14 16:53:09 -070039Runtime::~Runtime() {
Elliott Hughes5fe594f2011-09-08 12:33:17 -070040 // Make sure our internal threads are dead before we start tearing down things they're using.
41 delete signal_catcher_;
42
Carl Shapiro61e019d2011-07-14 16:53:09 -070043 delete class_linker_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070044 Heap::Destroy();
Carl Shapiro61e019d2011-07-14 16:53:09 -070045 delete thread_list_;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070046 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070047 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -070048 Thread::Shutdown();
Carl Shapiro4acf4642011-07-26 18:54:13 -070049 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070050 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -070051 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -070052}
53
Elliott Hughesffe67362011-07-17 12:09:27 -070054void Runtime::Abort(const char* file, int line) {
55 // Get any pending output out of the way.
56 fflush(NULL);
57
58 // Many people have difficulty distinguish aborts from crashes,
59 // so be explicit.
60 LogMessage(file, line, ERROR, -1).stream() << "Runtime aborting...";
61
Elliott Hughesffe67362011-07-17 12:09:27 -070062 // Perform any platform-specific pre-abort actions.
63 PlatformAbort(file, line);
64
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070065 // use abort hook if we have one
66 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
67 Runtime::Current()->abort_();
68 // notreached
69 }
70
Elliott Hughesffe67362011-07-17 12:09:27 -070071 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -070072 // receive SIGABRT. debuggerd dumps the stack trace of the main
73 // thread, whether or not that was the thread that failed. By
74 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -070075 // fault in the current thread, and get a useful log from debuggerd.
76 // We can also trivially tell the difference between a VM crash and
77 // a deliberate abort by looking at the fault address.
78 *reinterpret_cast<char*>(0xdeadd00d) = 38;
79 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -070080 // notreached
81}
82
Elliott Hughesbf86d042011-08-31 17:53:14 -070083void Runtime::CallExitHook(jint status) {
84 if (exit_ != NULL) {
85 ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative);
86 exit_(status);
87 LOG(WARNING) << "Exit hook returned instead of exiting!";
88 }
89}
90
Brian Carlstrom8a436592011-08-15 21:27:23 -070091// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
92// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
93// [gG] gigabytes.
94//
95// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -070096// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
97// of 1024.
98//
99// The spec says the -Xmx and -Xms options must be multiples of 1024. It
100// doesn't say anything about -Xss.
101//
102// Returns 0 (a useless size) if "s" is malformed or specifies a low or
103// non-evenly-divisible value.
104//
105size_t ParseMemoryOption(const char *s, size_t div) {
106 // strtoul accepts a leading [+-], which we don't want,
107 // so make sure our string starts with a decimal digit.
108 if (isdigit(*s)) {
109 const char *s2;
110 size_t val = strtoul(s, (char **)&s2, 10);
111 if (s2 != s) {
112 // s2 should be pointing just after the number.
113 // If this is the end of the string, the user
114 // has specified a number of bytes. Otherwise,
115 // there should be exactly one more character
116 // that specifies a multiplier.
117 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700118 // The remainder of the string is either a single multiplier
119 // character, or nothing to indicate that the value is in
120 // bytes.
121 char c = *s2++;
122 if (*s2 == '\0') {
123 size_t mul;
124 if (c == '\0') {
125 mul = 1;
126 } else if (c == 'k' || c == 'K') {
127 mul = KB;
128 } else if (c == 'm' || c == 'M') {
129 mul = MB;
130 } else if (c == 'g' || c == 'G') {
131 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700132 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700133 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700134 return 0;
135 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700136
137 if (val <= std::numeric_limits<size_t>::max() / mul) {
138 val *= mul;
139 } else {
140 // Clamp to a multiple of 1024.
141 val = std::numeric_limits<size_t>::max() & ~(1024-1);
142 }
143 } else {
144 // There's more than one character after the numeric part.
145 return 0;
146 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700147 }
148 // The man page says that a -Xm value must be a multiple of 1024.
149 if (val % div == 0) {
150 return val;
151 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700152 }
153 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700154 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700155}
156
Elliott Hughes0af55432011-08-17 18:37:28 -0700157void LoadJniLibrary(JavaVMExt* vm, const char* name) {
158 // TODO: OS_SHARED_LIB_FORMAT_STR
159 std::string mapped_name(StringPrintf("lib%s.so", name));
Elliott Hughes75770752011-08-24 17:52:38 -0700160 std::string reason;
161 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700162 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
163 << reason;
164 }
165}
166
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700167void CreateClassPath(const char* class_path_cstr,
168 std::vector<const DexFile*>& class_path_vector) {
169 CHECK(class_path_cstr != NULL);
Carl Shapirofc322c72011-07-27 00:20:01 -0700170 std::vector<std::string> parsed;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700171 Split(class_path_cstr, ':', parsed);
Carl Shapirofc322c72011-07-27 00:20:01 -0700172 for (size_t i = 0; i < parsed.size(); ++i) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700173 const DexFile* dex_file = DexFile::Open(parsed[i]);
Carl Shapirofc322c72011-07-27 00:20:01 -0700174 if (dex_file != NULL) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700175 class_path_vector.push_back(dex_file);
Carl Shapirofc322c72011-07-27 00:20:01 -0700176 }
177 }
178}
179
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700180Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700181 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700182 const char* boot_class_path = NULL;
183 const char* class_path = NULL;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700184 parsed->boot_image_ = NULL;
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700185#ifdef NDEBUG
Elliott Hughes5174fe62011-08-23 15:12:35 -0700186 // -Xcheck:jni is off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700187 parsed->check_jni_ = false;
188#else
189 // ...but on by default in debug builds.
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700190#if 0 // TODO: disabled for oatexec until the shorty's used by check_jni are managed heap allocated.
191 // Instead we turn on -Xcheck_jni in common_test.
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700192 parsed->check_jni_ = true;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700193#else
194 parsed->check_jni_ = false;
195#endif
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700196#endif
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:")) {
208 boot_class_path = option.substr(strlen("-Xbootclasspath:")).data();
209 } else if (option == "bootclasspath") {
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 Carlstrom69b15fb2011-09-03 12:25:21 -0700222 } else if (option == "-classpath" || option == "-cp") {
223 // TODO: support -Djava.class.path
224 i++;
225 if (i == options.size()) {
226 // TODO: usage
227 LOG(FATAL) << "Missing required class path value for " << option;
228 return NULL;
229 }
230 const StringPiece& value = options[i].first;
231 class_path = value.data();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700232 } else if (option.starts_with("-Xbootimage:")) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700233 // TODO: remove when intern_addr_ is removed, just use -Ximage:
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700234 parsed->boot_image_ = option.substr(strlen("-Xbootimage:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700235 } else if (option.starts_with("-Ximage:")) {
236 parsed->images_.push_back(option.substr(strlen("-Ximage:")).data());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700237 } else if (option.starts_with("-Xcheck:jni")) {
238 parsed->check_jni_ = true;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700239 } else if (option.starts_with("-Xms")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700240 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024);
241 if (size == 0) {
242 if (ignore_unrecognized) {
243 continue;
244 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700245 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700246 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700247 return NULL;
248 }
249 parsed->heap_initial_size_ = size;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700250 } else if (option.starts_with("-Xmx")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700251 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024);
252 if (size == 0) {
253 if (ignore_unrecognized) {
254 continue;
255 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700256 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700257 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700258 return NULL;
259 }
260 parsed->heap_maximum_size_ = size;
261 } else if (option.starts_with("-Xss")) {
262 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1);
263 if (size == 0) {
264 if (ignore_unrecognized) {
265 continue;
266 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700267 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700268 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700269 return NULL;
270 }
271 parsed->stack_size_ = size;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700272 } else if (option.starts_with("-D")) {
273 parsed->properties_.push_back(option.substr(strlen("-D")).data());
Elliott Hughesa0957642011-09-02 14:27:33 -0700274 } else if (option.starts_with("-Xjnitrace:")) {
275 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:")).data();
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700276 } else if (option.starts_with("-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700277 std::vector<std::string> verbose_options;
Elliott Hughes34023802011-08-30 12:06:17 -0700278 Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700279 for (size_t i = 0; i < verbose_options.size(); ++i) {
280 parsed->verbose_.insert(verbose_options[i]);
281 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700282 } else if (option == "vfprintf") {
283 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
284 } else if (option == "exit") {
285 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
286 } else if (option == "abort") {
287 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700288 } else {
289 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700290 // TODO: print usage via vfprintf
Brian Carlstrom8a436592011-08-15 21:27:23 -0700291 LOG(FATAL) << "Unrecognized option " << option;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700292 return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700293 }
294 }
295 }
296
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700297 // consider it an error if both bootclasspath and -Xbootclasspath: are supplied.
298 // TODO: remove bootclasspath which is only mostly just used by tests?
299 if (!parsed->boot_class_path_.empty() && boot_class_path != NULL) {
300 // TODO: usage
301 LOG(FATAL) << "bootclasspath and -Xbootclasspath: are mutually exclusive options.";
302 return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700303 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700304 if (parsed->boot_class_path_.empty()) {
305 if (boot_class_path == NULL) {
306 boot_class_path = getenv("BOOTCLASSPATH");
307 if (boot_class_path == NULL) {
308 boot_class_path = "";
309 }
310 }
311 CreateClassPath(boot_class_path, parsed->boot_class_path_);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700312 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700313
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700314 if (class_path == NULL) {
315 class_path = getenv("CLASSPATH");
316 if (class_path == NULL) {
317 class_path = "";
318 }
319 }
320 CHECK_EQ(parsed->class_path_.size(), 0U);
321 CreateClassPath(class_path, parsed->class_path_);
322
323 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700324}
325
326Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700327 // TODO: acquire a static mutex on Runtime to avoid racing.
328 if (Runtime::instance_ != NULL) {
329 return NULL;
330 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700331 instance_ = new Runtime;
332 if (!instance_->Init(options, ignore_unrecognized)) {
333 delete instance_;
334 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700335 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700336 return instance_;
337}
Elliott Hughes0af55432011-08-17 18:37:28 -0700338
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700339void Runtime::Start() {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700340 started_ = true;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700341
342 // Finish attaching the main thread.
343 Thread* main_thread = Thread::Current();
344 main_thread->CreatePeer("main", false);
345
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700346 instance_->InitLibraries();
Elliott Hughese27955c2011-08-26 15:21:24 -0700347 instance_->signal_catcher_ = new SignalCatcher;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700348}
349
Elliott Hughesdcc24742011-09-07 14:02:44 -0700350bool Runtime::IsStarted() {
351 return started_;
352}
353
Elliott Hughes0af55432011-08-17 18:37:28 -0700354bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700355 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700356
Elliott Hughes90a33692011-08-30 13:27:07 -0700357 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
358 if (options.get() == NULL) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700359 LOG(WARNING) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700360 return false;
361 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700362 vfprintf_ = options->hook_vfprintf_;
363 exit_ = options->hook_exit_;
364 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700365
Elliott Hughesbe759c62011-09-08 19:38:21 -0700366 default_stack_size_ = options->stack_size_;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700367 thread_list_ = new ThreadList;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700368
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700369 intern_table_ = new InternTable;
370
Elliott Hughesbe759c62011-09-08 19:38:21 -0700371 Heap::Init(options->heap_initial_size_, options->heap_maximum_size_,
372 options->boot_image_, options->images_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700373
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700374 BlockSignals();
375
Elliott Hughesa0957642011-09-02 14:27:33 -0700376 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700377
Elliott Hughesbe759c62011-09-08 19:38:21 -0700378 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700379
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700380 // ClassLinker needs an attached thread, but we can't fully attach a thread
381 // without creating objects.
382 Thread::Attach(this, "main", false);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700383
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700384 class_linker_ = ClassLinker::Create(options->boot_class_path_,
385 options->class_path_,
386 intern_table_,
387 Heap::GetBootSpace());
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700388
Carl Shapiro1fb86202011-06-27 17:43:13 -0700389 return true;
390}
391
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700392void Runtime::InitLibraries() {
393 Thread* self = Thread::Current();
394 JNIEnv* env = self->GetJniEnv();
395
396 // Must be in the kNative state for JNI-based method registration.
397 ScopedThreadStateChange tsc(self, Thread::kNative);
398
399 // First set up the native methods provided by the runtime itself.
400 RegisterRuntimeNativeMethods(env);
401
402 // Now set up libcore, which is just a JNI library with a JNI_OnLoad.
403 // Most JNI libraries can just use System.loadLibrary, but you can't
404 // if you're the library that implements System.loadLibrary!
405 JniConstants::init(env);
406 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
407}
408
409void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
410#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700411 //REGISTER(register_dalvik_system_DexFile);
412 //REGISTER(register_dalvik_system_VMDebug);
413 //REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700414 REGISTER(register_dalvik_system_VMStack);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700415 //REGISTER(register_dalvik_system_Zygote);
416 //REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700417 REGISTER(register_java_lang_Object);
418 REGISTER(register_java_lang_Runtime);
419 REGISTER(register_java_lang_String);
420 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700421 REGISTER(register_java_lang_Thread);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700422 REGISTER(register_java_lang_Throwable);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700423 //REGISTER(register_java_lang_VMClassLoader);
424 //REGISTER(register_java_lang_reflect_AccessibleObject);
425 //REGISTER(register_java_lang_reflect_Array);
426 //REGISTER(register_java_lang_reflect_Constructor);
427 //REGISTER(register_java_lang_reflect_Field);
428 //REGISTER(register_java_lang_reflect_Method);
429 //REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700430 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700431 //REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
432 //REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
433 //REGISTER(register_sun_misc_Unsafe);
434#undef REGISTER
435}
436
Elliott Hughes8daa0922011-09-11 13:46:25 -0700437void Runtime::Dump(std::ostream& os) {
Elliott Hughese27955c2011-08-26 15:21:24 -0700438 // TODO: dump other runtime statistics?
439 os << "Loaded classes: " << class_linker_->NumLoadedClasses() << "\n";
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700440 os << "Intern table size: " << GetInternTable()->Size() << "\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700441 // LOGV("VM stats: meth=%d ifld=%d sfld=%d linear=%d",
442 // gDvm.numDeclaredMethods,
443 // gDvm.numDeclaredInstFields,
444 // gDvm.numDeclaredStaticFields,
445 // gDvm.pBootLoaderAlloc->curOffset);
446 // LOGI("GC precise methods: %d", dvmPointerSetGetCount(gDvm.preciseMethods));
Elliott Hughes42ee1422011-09-06 12:33:32 -0700447 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700448
449 thread_list_->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700450}
451
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700452void Runtime::BlockSignals() {
453 sigset_t sigset;
454 if (sigemptyset(&sigset) == -1) {
455 PLOG(FATAL) << "sigemptyset failed";
456 }
457 if (sigaddset(&sigset, SIGPIPE) == -1) {
458 PLOG(ERROR) << "sigaddset SIGPIPE failed";
459 }
460 // SIGQUIT is used to dump the runtime's state (including stack traces).
461 if (sigaddset(&sigset, SIGQUIT) == -1) {
462 PLOG(ERROR) << "sigaddset SIGQUIT failed";
463 }
464 // SIGUSR1 is used to initiate a heap dump.
465 if (sigaddset(&sigset, SIGUSR1) == -1) {
466 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
467 }
468 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
469}
470
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700471void Runtime::AttachCurrentThread(const char* name, bool as_daemon) {
472 Thread::Attach(instance_, name, as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700473}
474
Elliott Hughesd92bec42011-09-02 17:04:36 -0700475void Runtime::DetachCurrentThread() {
Elliott Hughes02b48d12011-09-07 17:15:51 -0700476 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700477}
478
Elliott Hughes410c0c82011-09-01 17:58:25 -0700479void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
480 class_linker_->VisitRoots(visitor, arg);
481 intern_table_->VisitRoots(visitor, arg);
482 java_vm_->VisitRoots(visitor, arg);
483 thread_list_->VisitRoots(visitor, arg);
484
485 //(*visitor)(&gDvm.outOfMemoryObj, 0, ROOT_VM_INTERNAL, arg);
486 //(*visitor)(&gDvm.internalErrorObj, 0, ROOT_VM_INTERNAL, arg);
487 //(*visitor)(&gDvm.noClassDefFoundErrorObj, 0, ROOT_VM_INTERNAL, arg);
488 UNIMPLEMENTED(WARNING) << "some roots not marked";
Brian Carlstrom1f870082011-08-23 16:02:11 -0700489}
490
Carl Shapiro1fb86202011-06-27 17:43:13 -0700491} // namespace art