blob: 985cdf6486040663dd62790dcee34cabdcb61960 [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());
253 } else if (option.starts_with("-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700254 std::vector<std::string> verbose_options;
Elliott Hughes34023802011-08-30 12:06:17 -0700255 Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700256 for (size_t i = 0; i < verbose_options.size(); ++i) {
257 parsed->verbose_.insert(verbose_options[i]);
258 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700259 } else if (option == "vfprintf") {
260 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
261 } else if (option == "exit") {
262 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
263 } else if (option == "abort") {
264 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700265 } else {
266 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700267 // TODO: print usage via vfprintf
Brian Carlstrom8a436592011-08-15 21:27:23 -0700268 LOG(FATAL) << "Unrecognized option " << option;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700269 return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700270 }
271 }
272 }
273
274 if (boot_class_path == NULL) {
275 boot_class_path = "";
276 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700277 if (parsed->boot_class_path_.size() == 0) {
278 CreateBootClassPath(boot_class_path, parsed->boot_class_path_);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700279 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700280 return parsed.release();
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700281}
282
Brian Carlstrom8a436592011-08-15 21:27:23 -0700283Runtime* Runtime::Create(const std::vector<const DexFile*>& boot_class_path) {
284 Runtime::Options options;
285 options.push_back(std::make_pair("bootclasspath", &boot_class_path));
286 return Runtime::Create(options, false);
287}
288
289Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700290 // TODO: acquire a static mutex on Runtime to avoid racing.
291 if (Runtime::instance_ != NULL) {
292 return NULL;
293 }
Elliott Hughes90a33692011-08-30 13:27:07 -0700294 UniquePtr<Runtime> runtime(new Runtime());
Brian Carlstrom8a436592011-08-15 21:27:23 -0700295 bool success = runtime->Init(options, ignore_unrecognized);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700296 if (!success) {
297 return NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700298 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700299 instance_ = runtime.release();
300
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700301 instance_->InitLibraries();
Elliott Hughese27955c2011-08-26 15:21:24 -0700302 instance_->signal_catcher_ = new SignalCatcher;
303
Elliott Hughes0af55432011-08-17 18:37:28 -0700304 return instance_;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700305}
306
Elliott Hughes0af55432011-08-17 18:37:28 -0700307bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700308 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700309
Elliott Hughes90a33692011-08-30 13:27:07 -0700310 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
311 if (options.get() == NULL) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700312 return false;
313 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700314 vfprintf_ = options->hook_vfprintf_;
315 exit_ = options->hook_exit_;
316 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700317
Brian Carlstromb765be02011-08-17 23:54:10 -0700318 stack_size_ = options->stack_size_;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700319 thread_list_ = ThreadList::Create();
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700320
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700321 intern_table_ = new InternTable;
322
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700323 if (!Heap::Init(options->heap_initial_size_,
324 options->heap_maximum_size_,
325 options->boot_image_)) {
326 return false;
327 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700328
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700329 BlockSignals();
330
Elliott Hughes0af55432011-08-17 18:37:28 -0700331 bool verbose_jni = options->verbose_.find("jni") != options->verbose_.end();
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700332 java_vm_ = new JavaVMExt(this, options->check_jni_, verbose_jni);
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700333
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700334 if (!Thread::Startup()) {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700335 return false;
336 }
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700337 Thread* current_thread = Thread::Attach(this);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700338 thread_list_->Register(current_thread);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700339
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700340 class_linker_ = ClassLinker::Create(options->boot_class_path_, intern_table_, Heap::GetBootSpace());
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700341
Carl Shapiro1fb86202011-06-27 17:43:13 -0700342 return true;
343}
344
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700345void Runtime::InitLibraries() {
346 Thread* self = Thread::Current();
347 JNIEnv* env = self->GetJniEnv();
348
349 // Must be in the kNative state for JNI-based method registration.
350 ScopedThreadStateChange tsc(self, Thread::kNative);
351
352 // First set up the native methods provided by the runtime itself.
353 RegisterRuntimeNativeMethods(env);
354
355 // Now set up libcore, which is just a JNI library with a JNI_OnLoad.
356 // Most JNI libraries can just use System.loadLibrary, but you can't
357 // if you're the library that implements System.loadLibrary!
358 JniConstants::init(env);
359 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
360}
361
362void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
363#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
364 //REGISTER(register_dalvik_bytecode_OpcodeInfo);
365 //REGISTER(register_dalvik_system_DexFile);
366 //REGISTER(register_dalvik_system_VMDebug);
367 //REGISTER(register_dalvik_system_VMRuntime);
368 //REGISTER(register_dalvik_system_VMStack);
369 //REGISTER(register_dalvik_system_Zygote);
370 //REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700371 REGISTER(register_java_lang_Object);
372 REGISTER(register_java_lang_Runtime);
373 REGISTER(register_java_lang_String);
374 REGISTER(register_java_lang_System);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700375 //REGISTER(register_java_lang_Thread);
376 //REGISTER(register_java_lang_Throwable);
377 //REGISTER(register_java_lang_VMClassLoader);
378 //REGISTER(register_java_lang_reflect_AccessibleObject);
379 //REGISTER(register_java_lang_reflect_Array);
380 //REGISTER(register_java_lang_reflect_Constructor);
381 //REGISTER(register_java_lang_reflect_Field);
382 //REGISTER(register_java_lang_reflect_Method);
383 //REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700384 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700385 //REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
386 //REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
387 //REGISTER(register_sun_misc_Unsafe);
388#undef REGISTER
389}
390
Elliott Hughese27955c2011-08-26 15:21:24 -0700391void Runtime::DumpStatistics(std::ostream& os) {
392 // TODO: dump other runtime statistics?
393 os << "Loaded classes: " << class_linker_->NumLoadedClasses() << "\n";
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700394 os << "Intern table size: " << GetInternTable()->Size() << "\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700395 // LOGV("VM stats: meth=%d ifld=%d sfld=%d linear=%d",
396 // gDvm.numDeclaredMethods,
397 // gDvm.numDeclaredInstFields,
398 // gDvm.numDeclaredStaticFields,
399 // gDvm.pBootLoaderAlloc->curOffset);
400 // LOGI("GC precise methods: %d", dvmPointerSetGetCount(gDvm.preciseMethods));
401}
402
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700403void Runtime::BlockSignals() {
404 sigset_t sigset;
405 if (sigemptyset(&sigset) == -1) {
406 PLOG(FATAL) << "sigemptyset failed";
407 }
408 if (sigaddset(&sigset, SIGPIPE) == -1) {
409 PLOG(ERROR) << "sigaddset SIGPIPE failed";
410 }
411 // SIGQUIT is used to dump the runtime's state (including stack traces).
412 if (sigaddset(&sigset, SIGQUIT) == -1) {
413 PLOG(ERROR) << "sigaddset SIGQUIT failed";
414 }
415 // SIGUSR1 is used to initiate a heap dump.
416 if (sigaddset(&sigset, SIGUSR1) == -1) {
417 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
418 }
419 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
420}
421
Elliott Hughes75770752011-08-24 17:52:38 -0700422bool Runtime::AttachCurrentThread(const char* name, JNIEnv** penv, bool as_daemon) {
423 if (as_daemon) {
424 UNIMPLEMENTED(WARNING) << "TODO: do something different for daemon threads";
425 }
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700426 return Thread::Attach(instance_) != NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700427}
428
Ian Rogersb033c752011-07-20 12:22:35 -0700429bool Runtime::DetachCurrentThread() {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700430 Thread* self = Thread::Current();
431 thread_list_->Unregister(self);
432 delete self;
Ian Rogersb033c752011-07-20 12:22:35 -0700433 return true;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700434}
435
Brian Carlstrom1f870082011-08-23 16:02:11 -0700436void Runtime::VisitRoots(Heap::RootVistor* root_visitor, void* arg) const {
437 GetClassLinker()->VisitRoots(root_visitor, arg);
438 UNIMPLEMENTED(WARNING) << "mark other roots including per thread state and thread stacks";
439}
440
Carl Shapiro1fb86202011-06-27 17:43:13 -0700441} // namespace art