blob: 2c2c5e27505b21d243a34ef285cfd7f6c9b40f62 [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
Carl Shapiro61e019d2011-07-14 16:53:09 -070025Runtime::~Runtime() {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070026 // TODO: use smart pointers instead. (we'll need the pimpl idiom.)
Carl Shapiro61e019d2011-07-14 16:53:09 -070027 delete class_linker_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070028 Heap::Destroy();
Elliott Hughese27955c2011-08-26 15:21:24 -070029 delete signal_catcher_;
Carl Shapiro61e019d2011-07-14 16:53:09 -070030 delete thread_list_;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070031 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070032 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -070033 Thread::Shutdown();
Carl Shapiro4acf4642011-07-26 18:54:13 -070034 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070035 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -070036 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -070037}
38
Elliott Hughesffe67362011-07-17 12:09:27 -070039void Runtime::Abort(const char* file, int line) {
40 // Get any pending output out of the way.
41 fflush(NULL);
42
43 // Many people have difficulty distinguish aborts from crashes,
44 // so be explicit.
45 LogMessage(file, line, ERROR, -1).stream() << "Runtime aborting...";
46
Elliott Hughesffe67362011-07-17 12:09:27 -070047 // Perform any platform-specific pre-abort actions.
48 PlatformAbort(file, line);
49
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070050 // use abort hook if we have one
51 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
52 Runtime::Current()->abort_();
53 // notreached
54 }
55
Elliott Hughesffe67362011-07-17 12:09:27 -070056 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -070057 // receive SIGABRT. debuggerd dumps the stack trace of the main
58 // thread, whether or not that was the thread that failed. By
59 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -070060 // fault in the current thread, and get a useful log from debuggerd.
61 // We can also trivially tell the difference between a VM crash and
62 // a deliberate abort by looking at the fault address.
63 *reinterpret_cast<char*>(0xdeadd00d) = 38;
64 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -070065 // notreached
66}
67
Elliott Hughesbf86d042011-08-31 17:53:14 -070068void Runtime::CallExitHook(jint status) {
69 if (exit_ != NULL) {
70 ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative);
71 exit_(status);
72 LOG(WARNING) << "Exit hook returned instead of exiting!";
73 }
74}
75
Brian Carlstrom8a436592011-08-15 21:27:23 -070076// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
77// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
78// [gG] gigabytes.
79//
80// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -070081// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
82// of 1024.
83//
84// The spec says the -Xmx and -Xms options must be multiples of 1024. It
85// doesn't say anything about -Xss.
86//
87// Returns 0 (a useless size) if "s" is malformed or specifies a low or
88// non-evenly-divisible value.
89//
90size_t ParseMemoryOption(const char *s, size_t div) {
91 // strtoul accepts a leading [+-], which we don't want,
92 // so make sure our string starts with a decimal digit.
93 if (isdigit(*s)) {
94 const char *s2;
95 size_t val = strtoul(s, (char **)&s2, 10);
96 if (s2 != s) {
97 // s2 should be pointing just after the number.
98 // If this is the end of the string, the user
99 // has specified a number of bytes. Otherwise,
100 // there should be exactly one more character
101 // that specifies a multiplier.
102 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700103 // The remainder of the string is either a single multiplier
104 // character, or nothing to indicate that the value is in
105 // bytes.
106 char c = *s2++;
107 if (*s2 == '\0') {
108 size_t mul;
109 if (c == '\0') {
110 mul = 1;
111 } else if (c == 'k' || c == 'K') {
112 mul = KB;
113 } else if (c == 'm' || c == 'M') {
114 mul = MB;
115 } else if (c == 'g' || c == 'G') {
116 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700117 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700118 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700119 return 0;
120 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700121
122 if (val <= std::numeric_limits<size_t>::max() / mul) {
123 val *= mul;
124 } else {
125 // Clamp to a multiple of 1024.
126 val = std::numeric_limits<size_t>::max() & ~(1024-1);
127 }
128 } else {
129 // There's more than one character after the numeric part.
130 return 0;
131 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700132 }
133 // The man page says that a -Xm value must be a multiple of 1024.
134 if (val % div == 0) {
135 return val;
136 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700137 }
138 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700139 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700140}
141
Elliott Hughes0af55432011-08-17 18:37:28 -0700142void LoadJniLibrary(JavaVMExt* vm, const char* name) {
143 // TODO: OS_SHARED_LIB_FORMAT_STR
144 std::string mapped_name(StringPrintf("lib%s.so", name));
Elliott Hughes75770752011-08-24 17:52:38 -0700145 std::string reason;
146 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700147 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
148 << reason;
149 }
150}
151
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700152void CreateClassPath(const char* class_path_cstr,
153 std::vector<const DexFile*>& class_path_vector) {
154 CHECK(class_path_cstr != NULL);
Carl Shapirofc322c72011-07-27 00:20:01 -0700155 std::vector<std::string> parsed;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700156 Split(class_path_cstr, ':', parsed);
Carl Shapirofc322c72011-07-27 00:20:01 -0700157 for (size_t i = 0; i < parsed.size(); ++i) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700158 const DexFile* dex_file = DexFile::Open(parsed[i]);
Carl Shapirofc322c72011-07-27 00:20:01 -0700159 if (dex_file != NULL) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700160 class_path_vector.push_back(dex_file);
Carl Shapirofc322c72011-07-27 00:20:01 -0700161 }
162 }
163}
164
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700165Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700166 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700167 const char* boot_class_path = NULL;
168 const char* class_path = NULL;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700169 parsed->boot_image_ = NULL;
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700170#ifdef NDEBUG
Elliott Hughes5174fe62011-08-23 15:12:35 -0700171 // -Xcheck:jni is off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700172 parsed->check_jni_ = false;
173#else
174 // ...but on by default in debug builds.
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700175#if 0 // TODO: disabled for oatexec until the shorty's used by check_jni are managed heap allocated.
176 // Instead we turn on -Xcheck_jni in common_test.
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700177 parsed->check_jni_ = true;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700178#else
179 parsed->check_jni_ = false;
180#endif
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700181#endif
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700182 parsed->heap_initial_size_ = Heap::kInitialSize;
183 parsed->heap_maximum_size_ = Heap::kMaximumSize;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700184 parsed->stack_size_ = Thread::kDefaultStackSize;
185
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700186 parsed->hook_vfprintf_ = vfprintf;
187 parsed->hook_exit_ = exit;
188 parsed->hook_abort_ = abort;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700189
190 for (size_t i = 0; i < options.size(); ++i) {
191 const StringPiece& option = options[i].first;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700192 if (option.starts_with("-Xbootclasspath:")) {
193 boot_class_path = option.substr(strlen("-Xbootclasspath:")).data();
194 } else if (option == "bootclasspath") {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700195 const void* dex_vector = options[i].second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700196 const std::vector<const DexFile*>* v
197 = reinterpret_cast<const std::vector<const DexFile*>*>(dex_vector);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700198 if (v == NULL) {
199 if (ignore_unrecognized) {
200 continue;
201 }
202 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700203 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700204 return NULL;
205 }
206 parsed->boot_class_path_ = *v;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700207 } else if (option == "-classpath" || option == "-cp") {
208 // TODO: support -Djava.class.path
209 i++;
210 if (i == options.size()) {
211 // TODO: usage
212 LOG(FATAL) << "Missing required class path value for " << option;
213 return NULL;
214 }
215 const StringPiece& value = options[i].first;
216 class_path = value.data();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700217 } else if (option.starts_with("-Xbootimage:")) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700218 // TODO: remove when intern_addr_ is removed, just use -Ximage:
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700219 parsed->boot_image_ = option.substr(strlen("-Xbootimage:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700220 } else if (option.starts_with("-Ximage:")) {
221 parsed->images_.push_back(option.substr(strlen("-Ximage:")).data());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700222 } else if (option.starts_with("-Xcheck:jni")) {
223 parsed->check_jni_ = true;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700224 } else if (option.starts_with("-Xms")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700225 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024);
226 if (size == 0) {
227 if (ignore_unrecognized) {
228 continue;
229 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700230 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700231 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700232 return NULL;
233 }
234 parsed->heap_initial_size_ = size;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700235 } else if (option.starts_with("-Xmx")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700236 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024);
237 if (size == 0) {
238 if (ignore_unrecognized) {
239 continue;
240 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700241 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700242 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700243 return NULL;
244 }
245 parsed->heap_maximum_size_ = size;
246 } else if (option.starts_with("-Xss")) {
247 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1);
248 if (size == 0) {
249 if (ignore_unrecognized) {
250 continue;
251 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700252 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700253 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700254 return NULL;
255 }
256 parsed->stack_size_ = size;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700257 } else if (option.starts_with("-D")) {
258 parsed->properties_.push_back(option.substr(strlen("-D")).data());
Elliott Hughesa0957642011-09-02 14:27:33 -0700259 } else if (option.starts_with("-Xjnitrace:")) {
260 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:")).data();
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700261 } else if (option.starts_with("-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700262 std::vector<std::string> verbose_options;
Elliott Hughes34023802011-08-30 12:06:17 -0700263 Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700264 for (size_t i = 0; i < verbose_options.size(); ++i) {
265 parsed->verbose_.insert(verbose_options[i]);
266 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700267 } else if (option == "vfprintf") {
268 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
269 } else if (option == "exit") {
270 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
271 } else if (option == "abort") {
272 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700273 } else {
274 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700275 // TODO: print usage via vfprintf
Brian Carlstrom8a436592011-08-15 21:27:23 -0700276 LOG(FATAL) << "Unrecognized option " << option;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700277 return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700278 }
279 }
280 }
281
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700282 // consider it an error if both bootclasspath and -Xbootclasspath: are supplied.
283 // TODO: remove bootclasspath which is only mostly just used by tests?
284 if (!parsed->boot_class_path_.empty() && boot_class_path != NULL) {
285 // TODO: usage
286 LOG(FATAL) << "bootclasspath and -Xbootclasspath: are mutually exclusive options.";
287 return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700288 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700289 if (parsed->boot_class_path_.empty()) {
290 if (boot_class_path == NULL) {
291 boot_class_path = getenv("BOOTCLASSPATH");
292 if (boot_class_path == NULL) {
293 boot_class_path = "";
294 }
295 }
296 CreateClassPath(boot_class_path, parsed->boot_class_path_);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700297 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700298
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700299 if (class_path == NULL) {
300 class_path = getenv("CLASSPATH");
301 if (class_path == NULL) {
302 class_path = "";
303 }
304 }
305 CHECK_EQ(parsed->class_path_.size(), 0U);
306 CreateClassPath(class_path, parsed->class_path_);
307
308 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700309}
310
311Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700312 // TODO: acquire a static mutex on Runtime to avoid racing.
313 if (Runtime::instance_ != NULL) {
314 return NULL;
315 }
Elliott Hughes90a33692011-08-30 13:27:07 -0700316 UniquePtr<Runtime> runtime(new Runtime());
Brian Carlstrom8a436592011-08-15 21:27:23 -0700317 bool success = runtime->Init(options, ignore_unrecognized);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700318 if (!success) {
319 return NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700320 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700321 instance_ = runtime.release();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700322 return instance_;
323}
Elliott Hughes0af55432011-08-17 18:37:28 -0700324
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700325void Runtime::Start() {
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700326 instance_->InitLibraries();
Elliott Hughese27955c2011-08-26 15:21:24 -0700327 instance_->signal_catcher_ = new SignalCatcher;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700328}
329
Elliott Hughes0af55432011-08-17 18:37:28 -0700330bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700331 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700332
Elliott Hughes90a33692011-08-30 13:27:07 -0700333 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
334 if (options.get() == NULL) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700335 LOG(WARNING) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700336 return false;
337 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700338 vfprintf_ = options->hook_vfprintf_;
339 exit_ = options->hook_exit_;
340 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700341
Brian Carlstromb765be02011-08-17 23:54:10 -0700342 stack_size_ = options->stack_size_;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700343 thread_list_ = ThreadList::Create();
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700344
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700345 intern_table_ = new InternTable;
346
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700347 if (!Heap::Init(options->heap_initial_size_,
348 options->heap_maximum_size_,
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700349 options->boot_image_,
350 options->images_)) {
351 LOG(WARNING) << "Failed to create heap";
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700352 return false;
353 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700354
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700355 BlockSignals();
356
Elliott Hughesa0957642011-09-02 14:27:33 -0700357 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700358
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700359 if (!Thread::Startup()) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700360 LOG(WARNING) << "Failed to startup threads";
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700361 return false;
362 }
Elliott Hughesd92bec42011-09-02 17:04:36 -0700363
364 thread_list_->Register(Thread::Attach(this));
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700365
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700366 class_linker_ = ClassLinker::Create(options->boot_class_path_,
367 options->class_path_,
368 intern_table_,
369 Heap::GetBootSpace());
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700370
Carl Shapiro1fb86202011-06-27 17:43:13 -0700371 return true;
372}
373
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700374void Runtime::InitLibraries() {
375 Thread* self = Thread::Current();
376 JNIEnv* env = self->GetJniEnv();
377
378 // Must be in the kNative state for JNI-based method registration.
379 ScopedThreadStateChange tsc(self, Thread::kNative);
380
381 // First set up the native methods provided by the runtime itself.
382 RegisterRuntimeNativeMethods(env);
383
384 // Now set up libcore, which is just a JNI library with a JNI_OnLoad.
385 // Most JNI libraries can just use System.loadLibrary, but you can't
386 // if you're the library that implements System.loadLibrary!
387 JniConstants::init(env);
388 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
389}
390
391void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
392#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
393 //REGISTER(register_dalvik_bytecode_OpcodeInfo);
394 //REGISTER(register_dalvik_system_DexFile);
395 //REGISTER(register_dalvik_system_VMDebug);
396 //REGISTER(register_dalvik_system_VMRuntime);
397 //REGISTER(register_dalvik_system_VMStack);
398 //REGISTER(register_dalvik_system_Zygote);
399 //REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700400 REGISTER(register_java_lang_Object);
401 REGISTER(register_java_lang_Runtime);
402 REGISTER(register_java_lang_String);
403 REGISTER(register_java_lang_System);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700404 //REGISTER(register_java_lang_Thread);
405 //REGISTER(register_java_lang_Throwable);
406 //REGISTER(register_java_lang_VMClassLoader);
407 //REGISTER(register_java_lang_reflect_AccessibleObject);
408 //REGISTER(register_java_lang_reflect_Array);
409 //REGISTER(register_java_lang_reflect_Constructor);
410 //REGISTER(register_java_lang_reflect_Field);
411 //REGISTER(register_java_lang_reflect_Method);
412 //REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700413 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700414 //REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
415 //REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
416 //REGISTER(register_sun_misc_Unsafe);
417#undef REGISTER
418}
419
Elliott Hughese27955c2011-08-26 15:21:24 -0700420void Runtime::DumpStatistics(std::ostream& os) {
421 // TODO: dump other runtime statistics?
422 os << "Loaded classes: " << class_linker_->NumLoadedClasses() << "\n";
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700423 os << "Intern table size: " << GetInternTable()->Size() << "\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700424 // LOGV("VM stats: meth=%d ifld=%d sfld=%d linear=%d",
425 // gDvm.numDeclaredMethods,
426 // gDvm.numDeclaredInstFields,
427 // gDvm.numDeclaredStaticFields,
428 // gDvm.pBootLoaderAlloc->curOffset);
429 // LOGI("GC precise methods: %d", dvmPointerSetGetCount(gDvm.preciseMethods));
Elliott Hughes42ee1422011-09-06 12:33:32 -0700430 os << "\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700431}
432
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700433void Runtime::BlockSignals() {
434 sigset_t sigset;
435 if (sigemptyset(&sigset) == -1) {
436 PLOG(FATAL) << "sigemptyset failed";
437 }
438 if (sigaddset(&sigset, SIGPIPE) == -1) {
439 PLOG(ERROR) << "sigaddset SIGPIPE failed";
440 }
441 // SIGQUIT is used to dump the runtime's state (including stack traces).
442 if (sigaddset(&sigset, SIGQUIT) == -1) {
443 PLOG(ERROR) << "sigaddset SIGQUIT failed";
444 }
445 // SIGUSR1 is used to initiate a heap dump.
446 if (sigaddset(&sigset, SIGUSR1) == -1) {
447 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
448 }
449 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
450}
451
Elliott Hughesd92bec42011-09-02 17:04:36 -0700452void Runtime::AttachCurrentThread(const char* name, JNIEnv** penv, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700453 if (as_daemon) {
454 UNIMPLEMENTED(WARNING) << "TODO: do something different for daemon threads";
455 }
Elliott Hughesd92bec42011-09-02 17:04:36 -0700456 Thread* t = Thread::Attach(instance_);
457 thread_list_->Register(t);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700458}
459
Elliott Hughesd92bec42011-09-02 17:04:36 -0700460void Runtime::DetachCurrentThread() {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700461 Thread* self = Thread::Current();
462 thread_list_->Unregister(self);
463 delete self;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700464}
465
Elliott Hughes410c0c82011-09-01 17:58:25 -0700466void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
467 class_linker_->VisitRoots(visitor, arg);
468 intern_table_->VisitRoots(visitor, arg);
469 java_vm_->VisitRoots(visitor, arg);
470 thread_list_->VisitRoots(visitor, arg);
471
472 //(*visitor)(&gDvm.outOfMemoryObj, 0, ROOT_VM_INTERNAL, arg);
473 //(*visitor)(&gDvm.internalErrorObj, 0, ROOT_VM_INTERNAL, arg);
474 //(*visitor)(&gDvm.noClassDefFoundErrorObj, 0, ROOT_VM_INTERNAL, arg);
475 UNIMPLEMENTED(WARNING) << "some roots not marked";
Brian Carlstrom1f870082011-08-23 16:02:11 -0700476}
477
Carl Shapiro1fb86202011-06-27 17:43:13 -0700478} // namespace art