blob: 2acf2ece1745c1e4182e37981c3cf6e5c4fc5e1e [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"
Carl Shapiro61e019d2011-07-14 16:53:09 -070017
Elliott Hughes90a33692011-08-30 13:27:07 -070018// TODO: this drags in cutil/log.h, which conflicts with our logging.h.
19#include "JniConstants.h"
20
Carl Shapiro1fb86202011-06-27 17:43:13 -070021namespace art {
22
Carl Shapiro2ed144c2011-07-26 16:52:08 -070023Runtime* Runtime::instance_ = NULL;
24
Elliott Hughesdcc24742011-09-07 14:02:44 -070025Runtime::Runtime()
26 : stack_size_(0),
27 thread_list_(NULL),
28 intern_table_(NULL),
29 class_linker_(NULL),
30 signal_catcher_(NULL),
31 java_vm_(NULL),
32 started_(false),
33 vfprintf_(NULL),
34 exit_(NULL),
35 abort_(NULL) {
36}
37
Carl Shapiro61e019d2011-07-14 16:53:09 -070038Runtime::~Runtime() {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070039 // TODO: use smart pointers instead. (we'll need the pimpl idiom.)
Carl Shapiro61e019d2011-07-14 16:53:09 -070040 delete class_linker_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070041 Heap::Destroy();
Elliott Hughese27955c2011-08-26 15:21:24 -070042 delete signal_catcher_;
Carl Shapiro61e019d2011-07-14 16:53:09 -070043 delete thread_list_;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070044 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070045 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -070046 Thread::Shutdown();
Carl Shapiro4acf4642011-07-26 18:54:13 -070047 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070048 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -070049 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -070050}
51
Elliott Hughesffe67362011-07-17 12:09:27 -070052void Runtime::Abort(const char* file, int line) {
53 // Get any pending output out of the way.
54 fflush(NULL);
55
56 // Many people have difficulty distinguish aborts from crashes,
57 // so be explicit.
58 LogMessage(file, line, ERROR, -1).stream() << "Runtime aborting...";
59
Elliott Hughesffe67362011-07-17 12:09:27 -070060 // Perform any platform-specific pre-abort actions.
61 PlatformAbort(file, line);
62
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070063 // use abort hook if we have one
64 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
65 Runtime::Current()->abort_();
66 // notreached
67 }
68
Elliott Hughesffe67362011-07-17 12:09:27 -070069 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -070070 // receive SIGABRT. debuggerd dumps the stack trace of the main
71 // thread, whether or not that was the thread that failed. By
72 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -070073 // fault in the current thread, and get a useful log from debuggerd.
74 // We can also trivially tell the difference between a VM crash and
75 // a deliberate abort by looking at the fault address.
76 *reinterpret_cast<char*>(0xdeadd00d) = 38;
77 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -070078 // notreached
79}
80
Elliott Hughesbf86d042011-08-31 17:53:14 -070081void Runtime::CallExitHook(jint status) {
82 if (exit_ != NULL) {
83 ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative);
84 exit_(status);
85 LOG(WARNING) << "Exit hook returned instead of exiting!";
86 }
87}
88
Brian Carlstrom8a436592011-08-15 21:27:23 -070089// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
90// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
91// [gG] gigabytes.
92//
93// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -070094// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
95// of 1024.
96//
97// The spec says the -Xmx and -Xms options must be multiples of 1024. It
98// doesn't say anything about -Xss.
99//
100// Returns 0 (a useless size) if "s" is malformed or specifies a low or
101// non-evenly-divisible value.
102//
103size_t ParseMemoryOption(const char *s, size_t div) {
104 // strtoul accepts a leading [+-], which we don't want,
105 // so make sure our string starts with a decimal digit.
106 if (isdigit(*s)) {
107 const char *s2;
108 size_t val = strtoul(s, (char **)&s2, 10);
109 if (s2 != s) {
110 // s2 should be pointing just after the number.
111 // If this is the end of the string, the user
112 // has specified a number of bytes. Otherwise,
113 // there should be exactly one more character
114 // that specifies a multiplier.
115 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700116 // The remainder of the string is either a single multiplier
117 // character, or nothing to indicate that the value is in
118 // bytes.
119 char c = *s2++;
120 if (*s2 == '\0') {
121 size_t mul;
122 if (c == '\0') {
123 mul = 1;
124 } else if (c == 'k' || c == 'K') {
125 mul = KB;
126 } else if (c == 'm' || c == 'M') {
127 mul = MB;
128 } else if (c == 'g' || c == 'G') {
129 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700130 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700131 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700132 return 0;
133 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700134
135 if (val <= std::numeric_limits<size_t>::max() / mul) {
136 val *= mul;
137 } else {
138 // Clamp to a multiple of 1024.
139 val = std::numeric_limits<size_t>::max() & ~(1024-1);
140 }
141 } else {
142 // There's more than one character after the numeric part.
143 return 0;
144 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700145 }
146 // The man page says that a -Xm value must be a multiple of 1024.
147 if (val % div == 0) {
148 return val;
149 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700150 }
151 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700152 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700153}
154
Elliott Hughes0af55432011-08-17 18:37:28 -0700155void LoadJniLibrary(JavaVMExt* vm, const char* name) {
156 // TODO: OS_SHARED_LIB_FORMAT_STR
157 std::string mapped_name(StringPrintf("lib%s.so", name));
Elliott Hughes75770752011-08-24 17:52:38 -0700158 std::string reason;
159 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700160 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
161 << reason;
162 }
163}
164
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700165void CreateClassPath(const char* class_path_cstr,
166 std::vector<const DexFile*>& class_path_vector) {
167 CHECK(class_path_cstr != NULL);
Carl Shapirofc322c72011-07-27 00:20:01 -0700168 std::vector<std::string> parsed;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700169 Split(class_path_cstr, ':', parsed);
Carl Shapirofc322c72011-07-27 00:20:01 -0700170 for (size_t i = 0; i < parsed.size(); ++i) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700171 const DexFile* dex_file = DexFile::Open(parsed[i]);
Carl Shapirofc322c72011-07-27 00:20:01 -0700172 if (dex_file != NULL) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700173 class_path_vector.push_back(dex_file);
Carl Shapirofc322c72011-07-27 00:20:01 -0700174 }
175 }
176}
177
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700178Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700179 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700180 const char* boot_class_path = NULL;
181 const char* class_path = NULL;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700182 parsed->boot_image_ = NULL;
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700183#ifdef NDEBUG
Elliott Hughes5174fe62011-08-23 15:12:35 -0700184 // -Xcheck:jni is off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700185 parsed->check_jni_ = false;
186#else
187 // ...but on by default in debug builds.
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700188#if 0 // TODO: disabled for oatexec until the shorty's used by check_jni are managed heap allocated.
189 // Instead we turn on -Xcheck_jni in common_test.
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700190 parsed->check_jni_ = true;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700191#else
192 parsed->check_jni_ = false;
193#endif
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700194#endif
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:")) {
206 boot_class_path = option.substr(strlen("-Xbootclasspath:")).data();
207 } else if (option == "bootclasspath") {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700208 const void* dex_vector = options[i].second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700209 const std::vector<const DexFile*>* v
210 = reinterpret_cast<const std::vector<const DexFile*>*>(dex_vector);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700211 if (v == NULL) {
212 if (ignore_unrecognized) {
213 continue;
214 }
215 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700216 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700217 return NULL;
218 }
219 parsed->boot_class_path_ = *v;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700220 } else if (option == "-classpath" || option == "-cp") {
221 // TODO: support -Djava.class.path
222 i++;
223 if (i == options.size()) {
224 // TODO: usage
225 LOG(FATAL) << "Missing required class path value for " << option;
226 return NULL;
227 }
228 const StringPiece& value = options[i].first;
229 class_path = value.data();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700230 } else if (option.starts_with("-Xbootimage:")) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700231 // TODO: remove when intern_addr_ is removed, just use -Ximage:
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700232 parsed->boot_image_ = option.substr(strlen("-Xbootimage:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700233 } else if (option.starts_with("-Ximage:")) {
234 parsed->images_.push_back(option.substr(strlen("-Ximage:")).data());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700235 } else if (option.starts_with("-Xcheck:jni")) {
236 parsed->check_jni_ = true;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700237 } else if (option.starts_with("-Xms")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700238 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024);
239 if (size == 0) {
240 if (ignore_unrecognized) {
241 continue;
242 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700243 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700244 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700245 return NULL;
246 }
247 parsed->heap_initial_size_ = size;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700248 } else if (option.starts_with("-Xmx")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700249 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024);
250 if (size == 0) {
251 if (ignore_unrecognized) {
252 continue;
253 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700254 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700255 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700256 return NULL;
257 }
258 parsed->heap_maximum_size_ = size;
259 } else if (option.starts_with("-Xss")) {
260 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1);
261 if (size == 0) {
262 if (ignore_unrecognized) {
263 continue;
264 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700265 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700266 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700267 return NULL;
268 }
269 parsed->stack_size_ = size;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700270 } else if (option.starts_with("-D")) {
271 parsed->properties_.push_back(option.substr(strlen("-D")).data());
Elliott Hughesa0957642011-09-02 14:27:33 -0700272 } else if (option.starts_with("-Xjnitrace:")) {
273 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:")).data();
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700274 } else if (option.starts_with("-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700275 std::vector<std::string> verbose_options;
Elliott Hughes34023802011-08-30 12:06:17 -0700276 Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700277 for (size_t i = 0; i < verbose_options.size(); ++i) {
278 parsed->verbose_.insert(verbose_options[i]);
279 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700280 } else if (option == "vfprintf") {
281 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
282 } else if (option == "exit") {
283 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
284 } else if (option == "abort") {
285 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700286 } else {
287 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700288 // TODO: print usage via vfprintf
Brian Carlstrom8a436592011-08-15 21:27:23 -0700289 LOG(FATAL) << "Unrecognized option " << option;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700290 return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700291 }
292 }
293 }
294
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700295 // consider it an error if both bootclasspath and -Xbootclasspath: are supplied.
296 // TODO: remove bootclasspath which is only mostly just used by tests?
297 if (!parsed->boot_class_path_.empty() && boot_class_path != NULL) {
298 // TODO: usage
299 LOG(FATAL) << "bootclasspath and -Xbootclasspath: are mutually exclusive options.";
300 return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700301 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700302 if (parsed->boot_class_path_.empty()) {
303 if (boot_class_path == NULL) {
304 boot_class_path = getenv("BOOTCLASSPATH");
305 if (boot_class_path == NULL) {
306 boot_class_path = "";
307 }
308 }
309 CreateClassPath(boot_class_path, parsed->boot_class_path_);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700310 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700311
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700312 if (class_path == NULL) {
313 class_path = getenv("CLASSPATH");
314 if (class_path == NULL) {
315 class_path = "";
316 }
317 }
318 CHECK_EQ(parsed->class_path_.size(), 0U);
319 CreateClassPath(class_path, parsed->class_path_);
320
321 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700322}
323
324Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700325 // TODO: acquire a static mutex on Runtime to avoid racing.
326 if (Runtime::instance_ != NULL) {
327 return NULL;
328 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700329 instance_ = new Runtime;
330 if (!instance_->Init(options, ignore_unrecognized)) {
331 delete instance_;
332 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700333 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700334 return instance_;
335}
Elliott Hughes0af55432011-08-17 18:37:28 -0700336
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700337void Runtime::Start() {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700338 started_ = true;
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700339 instance_->InitLibraries();
Elliott Hughese27955c2011-08-26 15:21:24 -0700340 instance_->signal_catcher_ = new SignalCatcher;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700341}
342
Elliott Hughesdcc24742011-09-07 14:02:44 -0700343bool Runtime::IsStarted() {
344 return started_;
345}
346
Elliott Hughes0af55432011-08-17 18:37:28 -0700347bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700348 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700349
Elliott Hughes90a33692011-08-30 13:27:07 -0700350 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
351 if (options.get() == NULL) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700352 LOG(WARNING) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700353 return false;
354 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700355 vfprintf_ = options->hook_vfprintf_;
356 exit_ = options->hook_exit_;
357 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700358
Brian Carlstromb765be02011-08-17 23:54:10 -0700359 stack_size_ = options->stack_size_;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700360 thread_list_ = ThreadList::Create();
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700361
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700362 intern_table_ = new InternTable;
363
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700364 if (!Heap::Init(options->heap_initial_size_,
365 options->heap_maximum_size_,
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700366 options->boot_image_,
367 options->images_)) {
368 LOG(WARNING) << "Failed to create heap";
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700369 return false;
370 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700371
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700372 BlockSignals();
373
Elliott Hughesa0957642011-09-02 14:27:33 -0700374 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700375
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700376 if (!Thread::Startup()) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700377 LOG(WARNING) << "Failed to startup threads";
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700378 return false;
379 }
Elliott Hughesd92bec42011-09-02 17:04:36 -0700380
Elliott Hughesdcc24742011-09-07 14:02:44 -0700381 thread_list_->Register(Thread::Attach(this, "main", false));
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700382
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700383 class_linker_ = ClassLinker::Create(options->boot_class_path_,
384 options->class_path_,
385 intern_table_,
386 Heap::GetBootSpace());
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700387
Carl Shapiro1fb86202011-06-27 17:43:13 -0700388 return true;
389}
390
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700391void Runtime::InitLibraries() {
392 Thread* self = Thread::Current();
393 JNIEnv* env = self->GetJniEnv();
394
395 // Must be in the kNative state for JNI-based method registration.
396 ScopedThreadStateChange tsc(self, Thread::kNative);
397
398 // First set up the native methods provided by the runtime itself.
399 RegisterRuntimeNativeMethods(env);
400
401 // Now set up libcore, which is just a JNI library with a JNI_OnLoad.
402 // Most JNI libraries can just use System.loadLibrary, but you can't
403 // if you're the library that implements System.loadLibrary!
404 JniConstants::init(env);
405 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
406}
407
408void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
409#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
410 //REGISTER(register_dalvik_bytecode_OpcodeInfo);
411 //REGISTER(register_dalvik_system_DexFile);
412 //REGISTER(register_dalvik_system_VMDebug);
413 //REGISTER(register_dalvik_system_VMRuntime);
414 //REGISTER(register_dalvik_system_VMStack);
415 //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 Hughesad7c2a32011-08-31 11:58:10 -0700421 //REGISTER(register_java_lang_Thread);
422 //REGISTER(register_java_lang_Throwable);
423 //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 Hughese27955c2011-08-26 15:21:24 -0700437void Runtime::DumpStatistics(std::ostream& os) {
438 // 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 Hughese27955c2011-08-26 15:21:24 -0700448}
449
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700450void Runtime::BlockSignals() {
451 sigset_t sigset;
452 if (sigemptyset(&sigset) == -1) {
453 PLOG(FATAL) << "sigemptyset failed";
454 }
455 if (sigaddset(&sigset, SIGPIPE) == -1) {
456 PLOG(ERROR) << "sigaddset SIGPIPE failed";
457 }
458 // SIGQUIT is used to dump the runtime's state (including stack traces).
459 if (sigaddset(&sigset, SIGQUIT) == -1) {
460 PLOG(ERROR) << "sigaddset SIGQUIT failed";
461 }
462 // SIGUSR1 is used to initiate a heap dump.
463 if (sigaddset(&sigset, SIGUSR1) == -1) {
464 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
465 }
466 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
467}
468
Elliott Hughesd92bec42011-09-02 17:04:36 -0700469void Runtime::AttachCurrentThread(const char* name, JNIEnv** penv, bool as_daemon) {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700470 Thread* t = Thread::Attach(instance_, name, as_daemon);
Elliott Hughesd92bec42011-09-02 17:04:36 -0700471 thread_list_->Register(t);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700472}
473
Elliott Hughesd92bec42011-09-02 17:04:36 -0700474void Runtime::DetachCurrentThread() {
Elliott Hughes02b48d12011-09-07 17:15:51 -0700475 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700476}
477
Elliott Hughes410c0c82011-09-01 17:58:25 -0700478void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
479 class_linker_->VisitRoots(visitor, arg);
480 intern_table_->VisitRoots(visitor, arg);
481 java_vm_->VisitRoots(visitor, arg);
482 thread_list_->VisitRoots(visitor, arg);
483
484 //(*visitor)(&gDvm.outOfMemoryObj, 0, ROOT_VM_INTERNAL, arg);
485 //(*visitor)(&gDvm.internalErrorObj, 0, ROOT_VM_INTERNAL, arg);
486 //(*visitor)(&gDvm.noClassDefFoundErrorObj, 0, ROOT_VM_INTERNAL, arg);
487 UNIMPLEMENTED(WARNING) << "some roots not marked";
Brian Carlstrom1f870082011-08-23 16:02:11 -0700488}
489
Carl Shapiro1fb86202011-06-27 17:43:13 -0700490} // namespace art