blob: c7ae9c1d7b2bb1637e841b188a3da1b0fadd79f9 [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 Hughes5fe594f2011-09-08 12:33:17 -070039 // Make sure our internal threads are dead before we start tearing down things they're using.
40 delete signal_catcher_;
41
Carl Shapiro61e019d2011-07-14 16:53:09 -070042 delete class_linker_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070043 Heap::Destroy();
Carl Shapiro61e019d2011-07-14 16:53:09 -070044 delete thread_list_;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070045 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070046 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -070047 Thread::Shutdown();
Carl Shapiro4acf4642011-07-26 18:54:13 -070048 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070049 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -070050 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -070051}
52
Elliott Hughesffe67362011-07-17 12:09:27 -070053void Runtime::Abort(const char* file, int line) {
54 // Get any pending output out of the way.
55 fflush(NULL);
56
57 // Many people have difficulty distinguish aborts from crashes,
58 // so be explicit.
59 LogMessage(file, line, ERROR, -1).stream() << "Runtime aborting...";
60
Elliott Hughesffe67362011-07-17 12:09:27 -070061 // Perform any platform-specific pre-abort actions.
62 PlatformAbort(file, line);
63
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070064 // use abort hook if we have one
65 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
66 Runtime::Current()->abort_();
67 // notreached
68 }
69
Elliott Hughesffe67362011-07-17 12:09:27 -070070 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -070071 // receive SIGABRT. debuggerd dumps the stack trace of the main
72 // thread, whether or not that was the thread that failed. By
73 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -070074 // fault in the current thread, and get a useful log from debuggerd.
75 // We can also trivially tell the difference between a VM crash and
76 // a deliberate abort by looking at the fault address.
77 *reinterpret_cast<char*>(0xdeadd00d) = 38;
78 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -070079 // notreached
80}
81
Elliott Hughesbf86d042011-08-31 17:53:14 -070082void Runtime::CallExitHook(jint status) {
83 if (exit_ != NULL) {
84 ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative);
85 exit_(status);
86 LOG(WARNING) << "Exit hook returned instead of exiting!";
87 }
88}
89
Brian Carlstrom8a436592011-08-15 21:27:23 -070090// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
91// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
92// [gG] gigabytes.
93//
94// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -070095// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
96// of 1024.
97//
98// The spec says the -Xmx and -Xms options must be multiples of 1024. It
99// doesn't say anything about -Xss.
100//
101// Returns 0 (a useless size) if "s" is malformed or specifies a low or
102// non-evenly-divisible value.
103//
104size_t ParseMemoryOption(const char *s, size_t div) {
105 // strtoul accepts a leading [+-], which we don't want,
106 // so make sure our string starts with a decimal digit.
107 if (isdigit(*s)) {
108 const char *s2;
109 size_t val = strtoul(s, (char **)&s2, 10);
110 if (s2 != s) {
111 // s2 should be pointing just after the number.
112 // If this is the end of the string, the user
113 // has specified a number of bytes. Otherwise,
114 // there should be exactly one more character
115 // that specifies a multiplier.
116 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700117 // The remainder of the string is either a single multiplier
118 // character, or nothing to indicate that the value is in
119 // bytes.
120 char c = *s2++;
121 if (*s2 == '\0') {
122 size_t mul;
123 if (c == '\0') {
124 mul = 1;
125 } else if (c == 'k' || c == 'K') {
126 mul = KB;
127 } else if (c == 'm' || c == 'M') {
128 mul = MB;
129 } else if (c == 'g' || c == 'G') {
130 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700131 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700132 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700133 return 0;
134 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700135
136 if (val <= std::numeric_limits<size_t>::max() / mul) {
137 val *= mul;
138 } else {
139 // Clamp to a multiple of 1024.
140 val = std::numeric_limits<size_t>::max() & ~(1024-1);
141 }
142 } else {
143 // There's more than one character after the numeric part.
144 return 0;
145 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700146 }
147 // The man page says that a -Xm value must be a multiple of 1024.
148 if (val % div == 0) {
149 return val;
150 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700151 }
152 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700153 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700154}
155
Elliott Hughes0af55432011-08-17 18:37:28 -0700156void LoadJniLibrary(JavaVMExt* vm, const char* name) {
157 // TODO: OS_SHARED_LIB_FORMAT_STR
158 std::string mapped_name(StringPrintf("lib%s.so", name));
Elliott Hughes75770752011-08-24 17:52:38 -0700159 std::string reason;
160 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700161 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
162 << reason;
163 }
164}
165
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700166void CreateClassPath(const char* class_path_cstr,
167 std::vector<const DexFile*>& class_path_vector) {
168 CHECK(class_path_cstr != NULL);
Carl Shapirofc322c72011-07-27 00:20:01 -0700169 std::vector<std::string> parsed;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700170 Split(class_path_cstr, ':', parsed);
Carl Shapirofc322c72011-07-27 00:20:01 -0700171 for (size_t i = 0; i < parsed.size(); ++i) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700172 const DexFile* dex_file = DexFile::Open(parsed[i]);
Carl Shapirofc322c72011-07-27 00:20:01 -0700173 if (dex_file != NULL) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700174 class_path_vector.push_back(dex_file);
Carl Shapirofc322c72011-07-27 00:20:01 -0700175 }
176 }
177}
178
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700179Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700180 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700181 const char* boot_class_path = NULL;
182 const char* class_path = NULL;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700183 parsed->boot_image_ = NULL;
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700184#ifdef NDEBUG
Elliott Hughes5174fe62011-08-23 15:12:35 -0700185 // -Xcheck:jni is off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700186 parsed->check_jni_ = false;
187#else
188 // ...but on by default in debug builds.
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700189#if 0 // TODO: disabled for oatexec until the shorty's used by check_jni are managed heap allocated.
190 // Instead we turn on -Xcheck_jni in common_test.
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700191 parsed->check_jni_ = true;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700192#else
193 parsed->check_jni_ = false;
194#endif
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700195#endif
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700196 parsed->heap_initial_size_ = Heap::kInitialSize;
197 parsed->heap_maximum_size_ = Heap::kMaximumSize;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700198 parsed->stack_size_ = Thread::kDefaultStackSize;
199
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700200 parsed->hook_vfprintf_ = vfprintf;
201 parsed->hook_exit_ = exit;
202 parsed->hook_abort_ = abort;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700203
204 for (size_t i = 0; i < options.size(); ++i) {
205 const StringPiece& option = options[i].first;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700206 if (option.starts_with("-Xbootclasspath:")) {
207 boot_class_path = option.substr(strlen("-Xbootclasspath:")).data();
208 } else if (option == "bootclasspath") {
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 Carlstrom69b15fb2011-09-03 12:25:21 -0700221 } else if (option == "-classpath" || option == "-cp") {
222 // TODO: support -Djava.class.path
223 i++;
224 if (i == options.size()) {
225 // TODO: usage
226 LOG(FATAL) << "Missing required class path value for " << option;
227 return NULL;
228 }
229 const StringPiece& value = options[i].first;
230 class_path = value.data();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700231 } else if (option.starts_with("-Xbootimage:")) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700232 // TODO: remove when intern_addr_ is removed, just use -Ximage:
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700233 parsed->boot_image_ = option.substr(strlen("-Xbootimage:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700234 } else if (option.starts_with("-Ximage:")) {
235 parsed->images_.push_back(option.substr(strlen("-Ximage:")).data());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700236 } else if (option.starts_with("-Xcheck:jni")) {
237 parsed->check_jni_ = true;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700238 } else if (option.starts_with("-Xms")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700239 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024);
240 if (size == 0) {
241 if (ignore_unrecognized) {
242 continue;
243 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700244 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700245 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700246 return NULL;
247 }
248 parsed->heap_initial_size_ = size;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700249 } else if (option.starts_with("-Xmx")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700250 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024);
251 if (size == 0) {
252 if (ignore_unrecognized) {
253 continue;
254 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700255 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700256 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700257 return NULL;
258 }
259 parsed->heap_maximum_size_ = size;
260 } else if (option.starts_with("-Xss")) {
261 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1);
262 if (size == 0) {
263 if (ignore_unrecognized) {
264 continue;
265 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700266 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700267 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700268 return NULL;
269 }
270 parsed->stack_size_ = size;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700271 } else if (option.starts_with("-D")) {
272 parsed->properties_.push_back(option.substr(strlen("-D")).data());
Elliott Hughesa0957642011-09-02 14:27:33 -0700273 } else if (option.starts_with("-Xjnitrace:")) {
274 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:")).data();
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700275 } else if (option.starts_with("-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700276 std::vector<std::string> verbose_options;
Elliott Hughes34023802011-08-30 12:06:17 -0700277 Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700278 for (size_t i = 0; i < verbose_options.size(); ++i) {
279 parsed->verbose_.insert(verbose_options[i]);
280 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700281 } else if (option == "vfprintf") {
282 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
283 } else if (option == "exit") {
284 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
285 } else if (option == "abort") {
286 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700287 } else {
288 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700289 // TODO: print usage via vfprintf
Brian Carlstrom8a436592011-08-15 21:27:23 -0700290 LOG(FATAL) << "Unrecognized option " << option;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700291 return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700292 }
293 }
294 }
295
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700296 // consider it an error if both bootclasspath and -Xbootclasspath: are supplied.
297 // TODO: remove bootclasspath which is only mostly just used by tests?
298 if (!parsed->boot_class_path_.empty() && boot_class_path != NULL) {
299 // TODO: usage
300 LOG(FATAL) << "bootclasspath and -Xbootclasspath: are mutually exclusive options.";
301 return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700302 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700303 if (parsed->boot_class_path_.empty()) {
304 if (boot_class_path == NULL) {
305 boot_class_path = getenv("BOOTCLASSPATH");
306 if (boot_class_path == NULL) {
307 boot_class_path = "";
308 }
309 }
310 CreateClassPath(boot_class_path, parsed->boot_class_path_);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700311 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700312
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700313 if (class_path == NULL) {
314 class_path = getenv("CLASSPATH");
315 if (class_path == NULL) {
316 class_path = "";
317 }
318 }
319 CHECK_EQ(parsed->class_path_.size(), 0U);
320 CreateClassPath(class_path, parsed->class_path_);
321
322 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700323}
324
325Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700326 // TODO: acquire a static mutex on Runtime to avoid racing.
327 if (Runtime::instance_ != NULL) {
328 return NULL;
329 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700330 instance_ = new Runtime;
331 if (!instance_->Init(options, ignore_unrecognized)) {
332 delete instance_;
333 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700334 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700335 return instance_;
336}
Elliott Hughes0af55432011-08-17 18:37:28 -0700337
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700338void Runtime::Start() {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700339 started_ = true;
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700340
341 // Finish attaching the main thread.
342 Thread* main_thread = Thread::Current();
343 main_thread->CreatePeer("main", false);
344
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700345 instance_->InitLibraries();
Elliott Hughese27955c2011-08-26 15:21:24 -0700346 instance_->signal_catcher_ = new SignalCatcher;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700347}
348
Elliott Hughesdcc24742011-09-07 14:02:44 -0700349bool Runtime::IsStarted() {
350 return started_;
351}
352
Elliott Hughes0af55432011-08-17 18:37:28 -0700353bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700354 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700355
Elliott Hughes90a33692011-08-30 13:27:07 -0700356 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
357 if (options.get() == NULL) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700358 LOG(WARNING) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700359 return false;
360 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700361 vfprintf_ = options->hook_vfprintf_;
362 exit_ = options->hook_exit_;
363 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700364
Brian Carlstromb765be02011-08-17 23:54:10 -0700365 stack_size_ = options->stack_size_;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700366 thread_list_ = ThreadList::Create();
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700367
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700368 intern_table_ = new InternTable;
369
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700370 if (!Heap::Init(options->heap_initial_size_,
371 options->heap_maximum_size_,
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700372 options->boot_image_,
373 options->images_)) {
374 LOG(WARNING) << "Failed to create heap";
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700375 return false;
376 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700377
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700378 BlockSignals();
379
Elliott Hughesa0957642011-09-02 14:27:33 -0700380 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700381
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700382 if (!Thread::Startup()) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700383 LOG(WARNING) << "Failed to startup threads";
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700384 return false;
385 }
Elliott Hughesd92bec42011-09-02 17:04:36 -0700386
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700387 // ClassLinker needs an attached thread, but we can't fully attach a thread
388 // without creating objects.
389 Thread::Attach(this, "main", false);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700390
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700391 class_linker_ = ClassLinker::Create(options->boot_class_path_,
392 options->class_path_,
393 intern_table_,
394 Heap::GetBootSpace());
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700395
Carl Shapiro1fb86202011-06-27 17:43:13 -0700396 return true;
397}
398
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700399void Runtime::InitLibraries() {
400 Thread* self = Thread::Current();
401 JNIEnv* env = self->GetJniEnv();
402
403 // Must be in the kNative state for JNI-based method registration.
404 ScopedThreadStateChange tsc(self, Thread::kNative);
405
406 // First set up the native methods provided by the runtime itself.
407 RegisterRuntimeNativeMethods(env);
408
409 // Now set up libcore, which is just a JNI library with a JNI_OnLoad.
410 // Most JNI libraries can just use System.loadLibrary, but you can't
411 // if you're the library that implements System.loadLibrary!
412 JniConstants::init(env);
413 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
414}
415
416void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
417#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
418 //REGISTER(register_dalvik_bytecode_OpcodeInfo);
419 //REGISTER(register_dalvik_system_DexFile);
420 //REGISTER(register_dalvik_system_VMDebug);
421 //REGISTER(register_dalvik_system_VMRuntime);
422 //REGISTER(register_dalvik_system_VMStack);
423 //REGISTER(register_dalvik_system_Zygote);
424 //REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700425 REGISTER(register_java_lang_Object);
426 REGISTER(register_java_lang_Runtime);
427 REGISTER(register_java_lang_String);
428 REGISTER(register_java_lang_System);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700429 //REGISTER(register_java_lang_Thread);
430 //REGISTER(register_java_lang_Throwable);
431 //REGISTER(register_java_lang_VMClassLoader);
432 //REGISTER(register_java_lang_reflect_AccessibleObject);
433 //REGISTER(register_java_lang_reflect_Array);
434 //REGISTER(register_java_lang_reflect_Constructor);
435 //REGISTER(register_java_lang_reflect_Field);
436 //REGISTER(register_java_lang_reflect_Method);
437 //REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700438 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700439 //REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
440 //REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
441 //REGISTER(register_sun_misc_Unsafe);
442#undef REGISTER
443}
444
Elliott Hughese27955c2011-08-26 15:21:24 -0700445void Runtime::DumpStatistics(std::ostream& os) {
446 // TODO: dump other runtime statistics?
447 os << "Loaded classes: " << class_linker_->NumLoadedClasses() << "\n";
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700448 os << "Intern table size: " << GetInternTable()->Size() << "\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700449 // LOGV("VM stats: meth=%d ifld=%d sfld=%d linear=%d",
450 // gDvm.numDeclaredMethods,
451 // gDvm.numDeclaredInstFields,
452 // gDvm.numDeclaredStaticFields,
453 // gDvm.pBootLoaderAlloc->curOffset);
454 // LOGI("GC precise methods: %d", dvmPointerSetGetCount(gDvm.preciseMethods));
Elliott Hughes42ee1422011-09-06 12:33:32 -0700455 os << "\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700456}
457
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700458void Runtime::BlockSignals() {
459 sigset_t sigset;
460 if (sigemptyset(&sigset) == -1) {
461 PLOG(FATAL) << "sigemptyset failed";
462 }
463 if (sigaddset(&sigset, SIGPIPE) == -1) {
464 PLOG(ERROR) << "sigaddset SIGPIPE failed";
465 }
466 // SIGQUIT is used to dump the runtime's state (including stack traces).
467 if (sigaddset(&sigset, SIGQUIT) == -1) {
468 PLOG(ERROR) << "sigaddset SIGQUIT failed";
469 }
470 // SIGUSR1 is used to initiate a heap dump.
471 if (sigaddset(&sigset, SIGUSR1) == -1) {
472 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
473 }
474 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
475}
476
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700477void Runtime::AttachCurrentThread(const char* name, bool as_daemon) {
478 Thread::Attach(instance_, name, as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700479}
480
Elliott Hughesd92bec42011-09-02 17:04:36 -0700481void Runtime::DetachCurrentThread() {
Elliott Hughes02b48d12011-09-07 17:15:51 -0700482 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700483}
484
Elliott Hughes410c0c82011-09-01 17:58:25 -0700485void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
486 class_linker_->VisitRoots(visitor, arg);
487 intern_table_->VisitRoots(visitor, arg);
488 java_vm_->VisitRoots(visitor, arg);
489 thread_list_->VisitRoots(visitor, arg);
490
491 //(*visitor)(&gDvm.outOfMemoryObj, 0, ROOT_VM_INTERNAL, arg);
492 //(*visitor)(&gDvm.internalErrorObj, 0, ROOT_VM_INTERNAL, arg);
493 //(*visitor)(&gDvm.noClassDefFoundErrorObj, 0, ROOT_VM_INTERNAL, arg);
494 UNIMPLEMENTED(WARNING) << "some roots not marked";
Brian Carlstrom1f870082011-08-23 16:02:11 -0700495}
496
Carl Shapiro1fb86202011-06-27 17:43:13 -0700497} // namespace art