blob: 2891e56f0b69611035c340d029c845d54fdb0c0e [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 Carlstrom9f30b382011-08-28 22:41:38 -0700152const DexFile* Open(const std::string& filename) {
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700153 if (filename.size() < 4) {
154 LOG(WARNING) << "Ignoring short classpath entry '" << filename << "'";
155 return NULL;
156 }
157 std::string suffix(filename.substr(filename.size() - 4));
158 if (suffix == ".zip" || suffix == ".jar" || suffix == ".apk") {
159 return DexFile::OpenZip(filename);
160 } else {
161 return DexFile::OpenFile(filename);
162 }
163}
164
Brian Carlstrom8a436592011-08-15 21:27:23 -0700165void CreateBootClassPath(const char* boot_class_path_cstr,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700166 std::vector<const DexFile*>& boot_class_path_vector) {
Brian Carlstrom8a436592011-08-15 21:27:23 -0700167 CHECK(boot_class_path_cstr != NULL);
Carl Shapirofc322c72011-07-27 00:20:01 -0700168 std::vector<std::string> parsed;
Elliott Hughes34023802011-08-30 12:06:17 -0700169 Split(boot_class_path_cstr, ':', parsed);
Carl Shapirofc322c72011-07-27 00:20:01 -0700170 for (size_t i = 0; i < parsed.size(); ++i) {
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700171 const DexFile* dex_file = Open(parsed[i]);
Carl Shapirofc322c72011-07-27 00:20:01 -0700172 if (dex_file != NULL) {
Brian Carlstrom8a436592011-08-15 21:27:23 -0700173 boot_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 Carlstrom8a436592011-08-15 21:27:23 -0700180 const char* boot_class_path = getenv("BOOTCLASSPATH");
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700181 parsed->boot_image_ = NULL;
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700182#ifdef NDEBUG
Elliott Hughes5174fe62011-08-23 15:12:35 -0700183 // -Xcheck:jni is off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700184 parsed->check_jni_ = false;
185#else
186 // ...but on by default in debug builds.
187 parsed->check_jni_ = true;
188#endif
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700189 parsed->heap_initial_size_ = Heap::kInitialSize;
190 parsed->heap_maximum_size_ = Heap::kMaximumSize;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700191 parsed->stack_size_ = Thread::kDefaultStackSize;
192
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700193 parsed->hook_vfprintf_ = vfprintf;
194 parsed->hook_exit_ = exit;
195 parsed->hook_abort_ = abort;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700196
197 for (size_t i = 0; i < options.size(); ++i) {
198 const StringPiece& option = options[i].first;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700199 if (option.starts_with("-Xbootclasspath:")) {
200 boot_class_path = option.substr(strlen("-Xbootclasspath:")).data();
201 } else if (option == "bootclasspath") {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700202 const void* dex_vector = options[i].second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700203 const std::vector<const DexFile*>* v
204 = reinterpret_cast<const std::vector<const DexFile*>*>(dex_vector);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700205 if (v == NULL) {
206 if (ignore_unrecognized) {
207 continue;
208 }
209 // TODO: usage
210 LOG(FATAL) << "Could not parse " << option;
211 return NULL;
212 }
213 parsed->boot_class_path_ = *v;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700214 } else if (option.starts_with("-Xbootimage:")) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700215 parsed->boot_image_ = option.substr(strlen("-Xbootimage:")).data();
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700216 } else if (option.starts_with("-Xcheck:jni")) {
217 parsed->check_jni_ = true;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700218 } else if (option.starts_with("-Xms")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700219 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024);
220 if (size == 0) {
221 if (ignore_unrecognized) {
222 continue;
223 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700224 // TODO: usage
Brian Carlstromf734cf52011-08-17 16:28:14 -0700225 LOG(FATAL) << "Could not parse " << option;
226 return NULL;
227 }
228 parsed->heap_initial_size_ = size;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700229 } else if (option.starts_with("-Xmx")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700230 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024);
231 if (size == 0) {
232 if (ignore_unrecognized) {
233 continue;
234 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700235 // TODO: usage
Brian Carlstromf734cf52011-08-17 16:28:14 -0700236 LOG(FATAL) << "Could not parse " << option;
237 return NULL;
238 }
239 parsed->heap_maximum_size_ = size;
240 } else if (option.starts_with("-Xss")) {
241 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1);
242 if (size == 0) {
243 if (ignore_unrecognized) {
244 continue;
245 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700246 // TODO: usage
Brian Carlstromf734cf52011-08-17 16:28:14 -0700247 LOG(FATAL) << "Could not parse " << option;
248 return NULL;
249 }
250 parsed->stack_size_ = size;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700251 } else if (option.starts_with("-D")) {
252 parsed->properties_.push_back(option.substr(strlen("-D")).data());
Elliott Hughesa0957642011-09-02 14:27:33 -0700253 } else if (option.starts_with("-Xjnitrace:")) {
254 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:")).data();
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700255 } else if (option.starts_with("-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700256 std::vector<std::string> verbose_options;
Elliott Hughes34023802011-08-30 12:06:17 -0700257 Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700258 for (size_t i = 0; i < verbose_options.size(); ++i) {
259 parsed->verbose_.insert(verbose_options[i]);
260 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700261 } else if (option == "vfprintf") {
262 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
263 } else if (option == "exit") {
264 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
265 } else if (option == "abort") {
266 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700267 } else {
268 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700269 // TODO: print usage via vfprintf
Brian Carlstrom8a436592011-08-15 21:27:23 -0700270 LOG(FATAL) << "Unrecognized option " << option;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700271 return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700272 }
273 }
274 }
275
276 if (boot_class_path == NULL) {
277 boot_class_path = "";
278 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700279 if (parsed->boot_class_path_.size() == 0) {
280 CreateBootClassPath(boot_class_path, parsed->boot_class_path_);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700281 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700282 return parsed.release();
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700283}
284
Brian Carlstrom8a436592011-08-15 21:27:23 -0700285Runtime* Runtime::Create(const std::vector<const DexFile*>& boot_class_path) {
286 Runtime::Options options;
287 options.push_back(std::make_pair("bootclasspath", &boot_class_path));
288 return Runtime::Create(options, false);
289}
290
291Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700292 // TODO: acquire a static mutex on Runtime to avoid racing.
293 if (Runtime::instance_ != NULL) {
294 return NULL;
295 }
Elliott Hughes90a33692011-08-30 13:27:07 -0700296 UniquePtr<Runtime> runtime(new Runtime());
Brian Carlstrom8a436592011-08-15 21:27:23 -0700297 bool success = runtime->Init(options, ignore_unrecognized);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700298 if (!success) {
299 return NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700300 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700301 instance_ = runtime.release();
302
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700303 instance_->InitLibraries();
Elliott Hughese27955c2011-08-26 15:21:24 -0700304 instance_->signal_catcher_ = new SignalCatcher;
305
Elliott Hughes0af55432011-08-17 18:37:28 -0700306 return instance_;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700307}
308
Elliott Hughes0af55432011-08-17 18:37:28 -0700309bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700310 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700311
Elliott Hughes90a33692011-08-30 13:27:07 -0700312 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
313 if (options.get() == NULL) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700314 return false;
315 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700316 vfprintf_ = options->hook_vfprintf_;
317 exit_ = options->hook_exit_;
318 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700319
Brian Carlstromb765be02011-08-17 23:54:10 -0700320 stack_size_ = options->stack_size_;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700321 thread_list_ = ThreadList::Create();
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700322
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700323 intern_table_ = new InternTable;
324
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700325 if (!Heap::Init(options->heap_initial_size_,
326 options->heap_maximum_size_,
327 options->boot_image_)) {
328 return false;
329 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700330
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700331 BlockSignals();
332
Elliott Hughesa0957642011-09-02 14:27:33 -0700333 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700334
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700335 if (!Thread::Startup()) {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700336 return false;
337 }
Elliott Hughesd92bec42011-09-02 17:04:36 -0700338
339 thread_list_->Register(Thread::Attach(this));
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700340
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700341 class_linker_ = ClassLinker::Create(options->boot_class_path_, intern_table_, Heap::GetBootSpace());
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700342
Carl Shapiro1fb86202011-06-27 17:43:13 -0700343 return true;
344}
345
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700346void Runtime::InitLibraries() {
347 Thread* self = Thread::Current();
348 JNIEnv* env = self->GetJniEnv();
349
350 // Must be in the kNative state for JNI-based method registration.
351 ScopedThreadStateChange tsc(self, Thread::kNative);
352
353 // First set up the native methods provided by the runtime itself.
354 RegisterRuntimeNativeMethods(env);
355
356 // Now set up libcore, which is just a JNI library with a JNI_OnLoad.
357 // Most JNI libraries can just use System.loadLibrary, but you can't
358 // if you're the library that implements System.loadLibrary!
359 JniConstants::init(env);
360 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
361}
362
363void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
364#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
365 //REGISTER(register_dalvik_bytecode_OpcodeInfo);
366 //REGISTER(register_dalvik_system_DexFile);
367 //REGISTER(register_dalvik_system_VMDebug);
368 //REGISTER(register_dalvik_system_VMRuntime);
369 //REGISTER(register_dalvik_system_VMStack);
370 //REGISTER(register_dalvik_system_Zygote);
371 //REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700372 REGISTER(register_java_lang_Object);
373 REGISTER(register_java_lang_Runtime);
374 REGISTER(register_java_lang_String);
375 REGISTER(register_java_lang_System);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700376 //REGISTER(register_java_lang_Thread);
377 //REGISTER(register_java_lang_Throwable);
378 //REGISTER(register_java_lang_VMClassLoader);
379 //REGISTER(register_java_lang_reflect_AccessibleObject);
380 //REGISTER(register_java_lang_reflect_Array);
381 //REGISTER(register_java_lang_reflect_Constructor);
382 //REGISTER(register_java_lang_reflect_Field);
383 //REGISTER(register_java_lang_reflect_Method);
384 //REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700385 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700386 //REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
387 //REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
388 //REGISTER(register_sun_misc_Unsafe);
389#undef REGISTER
390}
391
Elliott Hughese27955c2011-08-26 15:21:24 -0700392void Runtime::DumpStatistics(std::ostream& os) {
393 // TODO: dump other runtime statistics?
394 os << "Loaded classes: " << class_linker_->NumLoadedClasses() << "\n";
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700395 os << "Intern table size: " << GetInternTable()->Size() << "\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700396 // LOGV("VM stats: meth=%d ifld=%d sfld=%d linear=%d",
397 // gDvm.numDeclaredMethods,
398 // gDvm.numDeclaredInstFields,
399 // gDvm.numDeclaredStaticFields,
400 // gDvm.pBootLoaderAlloc->curOffset);
401 // LOGI("GC precise methods: %d", dvmPointerSetGetCount(gDvm.preciseMethods));
Elliott Hughes42ee1422011-09-06 12:33:32 -0700402 os << "\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700403}
404
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700405void Runtime::BlockSignals() {
406 sigset_t sigset;
407 if (sigemptyset(&sigset) == -1) {
408 PLOG(FATAL) << "sigemptyset failed";
409 }
410 if (sigaddset(&sigset, SIGPIPE) == -1) {
411 PLOG(ERROR) << "sigaddset SIGPIPE failed";
412 }
413 // SIGQUIT is used to dump the runtime's state (including stack traces).
414 if (sigaddset(&sigset, SIGQUIT) == -1) {
415 PLOG(ERROR) << "sigaddset SIGQUIT failed";
416 }
417 // SIGUSR1 is used to initiate a heap dump.
418 if (sigaddset(&sigset, SIGUSR1) == -1) {
419 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
420 }
421 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
422}
423
Elliott Hughesd92bec42011-09-02 17:04:36 -0700424void Runtime::AttachCurrentThread(const char* name, JNIEnv** penv, bool as_daemon) {
Elliott Hughes75770752011-08-24 17:52:38 -0700425 if (as_daemon) {
426 UNIMPLEMENTED(WARNING) << "TODO: do something different for daemon threads";
427 }
Elliott Hughesd92bec42011-09-02 17:04:36 -0700428 Thread* t = Thread::Attach(instance_);
429 thread_list_->Register(t);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700430}
431
Elliott Hughesd92bec42011-09-02 17:04:36 -0700432void Runtime::DetachCurrentThread() {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700433 Thread* self = Thread::Current();
434 thread_list_->Unregister(self);
435 delete self;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700436}
437
Elliott Hughes410c0c82011-09-01 17:58:25 -0700438void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
439 class_linker_->VisitRoots(visitor, arg);
440 intern_table_->VisitRoots(visitor, arg);
441 java_vm_->VisitRoots(visitor, arg);
442 thread_list_->VisitRoots(visitor, arg);
443
444 //(*visitor)(&gDvm.outOfMemoryObj, 0, ROOT_VM_INTERNAL, arg);
445 //(*visitor)(&gDvm.internalErrorObj, 0, ROOT_VM_INTERNAL, arg);
446 //(*visitor)(&gDvm.noClassDefFoundErrorObj, 0, ROOT_VM_INTERNAL, arg);
447 UNIMPLEMENTED(WARNING) << "some roots not marked";
Brian Carlstrom1f870082011-08-23 16:02:11 -0700448}
449
Carl Shapiro1fb86202011-06-27 17:43:13 -0700450} // namespace art