blob: 01abc4402af596446cf0971441cf1abe7c921c42 [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 Hughesc5f7c912011-08-18 14:00:42 -070013#include "jni_internal.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070014#include "signal_catcher.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070015#include "thread.h"
Carl Shapiro61e019d2011-07-14 16:53:09 -070016
Elliott Hughes90a33692011-08-30 13:27:07 -070017// TODO: this drags in cutil/log.h, which conflicts with our logging.h.
18#include "JniConstants.h"
19
Carl Shapiro1fb86202011-06-27 17:43:13 -070020namespace art {
21
Carl Shapiro2ed144c2011-07-26 16:52:08 -070022Runtime* Runtime::instance_ = NULL;
23
Carl Shapiro61e019d2011-07-14 16:53:09 -070024Runtime::~Runtime() {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070025 // TODO: use smart pointers instead. (we'll need the pimpl idiom.)
Carl Shapiro61e019d2011-07-14 16:53:09 -070026 delete class_linker_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070027 Heap::Destroy();
Elliott Hughese27955c2011-08-26 15:21:24 -070028 delete signal_catcher_;
Carl Shapiro61e019d2011-07-14 16:53:09 -070029 delete thread_list_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070030 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -070031 Thread::Shutdown();
Carl Shapiro4acf4642011-07-26 18:54:13 -070032 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070033 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -070034 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -070035}
36
Elliott Hughesffe67362011-07-17 12:09:27 -070037void Runtime::Abort(const char* file, int line) {
38 // Get any pending output out of the way.
39 fflush(NULL);
40
41 // Many people have difficulty distinguish aborts from crashes,
42 // so be explicit.
43 LogMessage(file, line, ERROR, -1).stream() << "Runtime aborting...";
44
Elliott Hughesffe67362011-07-17 12:09:27 -070045 // Perform any platform-specific pre-abort actions.
46 PlatformAbort(file, line);
47
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070048 // use abort hook if we have one
49 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
50 Runtime::Current()->abort_();
51 // notreached
52 }
53
Elliott Hughesffe67362011-07-17 12:09:27 -070054 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -070055 // receive SIGABRT. debuggerd dumps the stack trace of the main
56 // thread, whether or not that was the thread that failed. By
57 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -070058 // fault in the current thread, and get a useful log from debuggerd.
59 // We can also trivially tell the difference between a VM crash and
60 // a deliberate abort by looking at the fault address.
61 *reinterpret_cast<char*>(0xdeadd00d) = 38;
62 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -070063 // notreached
64}
65
Elliott Hughesbf86d042011-08-31 17:53:14 -070066void Runtime::CallExitHook(jint status) {
67 if (exit_ != NULL) {
68 ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative);
69 exit_(status);
70 LOG(WARNING) << "Exit hook returned instead of exiting!";
71 }
72}
73
Brian Carlstrom8a436592011-08-15 21:27:23 -070074// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
75// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
76// [gG] gigabytes.
77//
78// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -070079// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
80// of 1024.
81//
82// The spec says the -Xmx and -Xms options must be multiples of 1024. It
83// doesn't say anything about -Xss.
84//
85// Returns 0 (a useless size) if "s" is malformed or specifies a low or
86// non-evenly-divisible value.
87//
88size_t ParseMemoryOption(const char *s, size_t div) {
89 // strtoul accepts a leading [+-], which we don't want,
90 // so make sure our string starts with a decimal digit.
91 if (isdigit(*s)) {
92 const char *s2;
93 size_t val = strtoul(s, (char **)&s2, 10);
94 if (s2 != s) {
95 // s2 should be pointing just after the number.
96 // If this is the end of the string, the user
97 // has specified a number of bytes. Otherwise,
98 // there should be exactly one more character
99 // that specifies a multiplier.
100 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700101 // The remainder of the string is either a single multiplier
102 // character, or nothing to indicate that the value is in
103 // bytes.
104 char c = *s2++;
105 if (*s2 == '\0') {
106 size_t mul;
107 if (c == '\0') {
108 mul = 1;
109 } else if (c == 'k' || c == 'K') {
110 mul = KB;
111 } else if (c == 'm' || c == 'M') {
112 mul = MB;
113 } else if (c == 'g' || c == 'G') {
114 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700115 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700116 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700117 return 0;
118 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700119
120 if (val <= std::numeric_limits<size_t>::max() / mul) {
121 val *= mul;
122 } else {
123 // Clamp to a multiple of 1024.
124 val = std::numeric_limits<size_t>::max() & ~(1024-1);
125 }
126 } else {
127 // There's more than one character after the numeric part.
128 return 0;
129 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700130 }
131 // The man page says that a -Xm value must be a multiple of 1024.
132 if (val % div == 0) {
133 return val;
134 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700135 }
136 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700137 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700138}
139
Elliott Hughes0af55432011-08-17 18:37:28 -0700140void LoadJniLibrary(JavaVMExt* vm, const char* name) {
141 // TODO: OS_SHARED_LIB_FORMAT_STR
142 std::string mapped_name(StringPrintf("lib%s.so", name));
Elliott Hughes75770752011-08-24 17:52:38 -0700143 std::string reason;
144 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700145 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
146 << reason;
147 }
148}
149
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700150const DexFile* Open(const std::string& filename) {
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700151 if (filename.size() < 4) {
152 LOG(WARNING) << "Ignoring short classpath entry '" << filename << "'";
153 return NULL;
154 }
155 std::string suffix(filename.substr(filename.size() - 4));
156 if (suffix == ".zip" || suffix == ".jar" || suffix == ".apk") {
157 return DexFile::OpenZip(filename);
158 } else {
159 return DexFile::OpenFile(filename);
160 }
161}
162
Brian Carlstrom8a436592011-08-15 21:27:23 -0700163void CreateBootClassPath(const char* boot_class_path_cstr,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700164 std::vector<const DexFile*>& boot_class_path_vector) {
Brian Carlstrom8a436592011-08-15 21:27:23 -0700165 CHECK(boot_class_path_cstr != NULL);
Carl Shapirofc322c72011-07-27 00:20:01 -0700166 std::vector<std::string> parsed;
Elliott Hughes34023802011-08-30 12:06:17 -0700167 Split(boot_class_path_cstr, ':', parsed);
Carl Shapirofc322c72011-07-27 00:20:01 -0700168 for (size_t i = 0; i < parsed.size(); ++i) {
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700169 const DexFile* dex_file = Open(parsed[i]);
Carl Shapirofc322c72011-07-27 00:20:01 -0700170 if (dex_file != NULL) {
Brian Carlstrom8a436592011-08-15 21:27:23 -0700171 boot_class_path_vector.push_back(dex_file);
Carl Shapirofc322c72011-07-27 00:20:01 -0700172 }
173 }
174}
175
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700176Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700177 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstrom8a436592011-08-15 21:27:23 -0700178 const char* boot_class_path = getenv("BOOTCLASSPATH");
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700179 parsed->boot_image_ = NULL;
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700180#ifdef NDEBUG
Elliott Hughes5174fe62011-08-23 15:12:35 -0700181 // -Xcheck:jni is off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700182 parsed->check_jni_ = false;
183#else
184 // ...but on by default in debug builds.
185 parsed->check_jni_ = true;
186#endif
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700187 parsed->heap_initial_size_ = Heap::kInitialSize;
188 parsed->heap_maximum_size_ = Heap::kMaximumSize;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700189 parsed->stack_size_ = Thread::kDefaultStackSize;
190
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700191 parsed->hook_vfprintf_ = vfprintf;
192 parsed->hook_exit_ = exit;
193 parsed->hook_abort_ = abort;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700194
195 for (size_t i = 0; i < options.size(); ++i) {
196 const StringPiece& option = options[i].first;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700197 if (option.starts_with("-Xbootclasspath:")) {
198 boot_class_path = option.substr(strlen("-Xbootclasspath:")).data();
199 } else if (option == "bootclasspath") {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700200 const void* dex_vector = options[i].second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700201 const std::vector<const DexFile*>* v
202 = reinterpret_cast<const std::vector<const DexFile*>*>(dex_vector);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700203 if (v == NULL) {
204 if (ignore_unrecognized) {
205 continue;
206 }
207 // TODO: usage
208 LOG(FATAL) << "Could not parse " << option;
209 return NULL;
210 }
211 parsed->boot_class_path_ = *v;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700212 } else if (option.starts_with("-Xbootimage:")) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700213 parsed->boot_image_ = option.substr(strlen("-Xbootimage:")).data();
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700214 } else if (option.starts_with("-Xcheck:jni")) {
215 parsed->check_jni_ = true;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700216 } else if (option.starts_with("-Xms")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700217 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024);
218 if (size == 0) {
219 if (ignore_unrecognized) {
220 continue;
221 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700222 // TODO: usage
Brian Carlstromf734cf52011-08-17 16:28:14 -0700223 LOG(FATAL) << "Could not parse " << option;
224 return NULL;
225 }
226 parsed->heap_initial_size_ = size;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700227 } else if (option.starts_with("-Xmx")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700228 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024);
229 if (size == 0) {
230 if (ignore_unrecognized) {
231 continue;
232 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700233 // TODO: usage
Brian Carlstromf734cf52011-08-17 16:28:14 -0700234 LOG(FATAL) << "Could not parse " << option;
235 return NULL;
236 }
237 parsed->heap_maximum_size_ = size;
238 } else if (option.starts_with("-Xss")) {
239 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1);
240 if (size == 0) {
241 if (ignore_unrecognized) {
242 continue;
243 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700244 // TODO: usage
Brian Carlstromf734cf52011-08-17 16:28:14 -0700245 LOG(FATAL) << "Could not parse " << option;
246 return NULL;
247 }
248 parsed->stack_size_ = size;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700249 } else if (option.starts_with("-D")) {
250 parsed->properties_.push_back(option.substr(strlen("-D")).data());
251 } else if (option.starts_with("-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700252 std::vector<std::string> verbose_options;
Elliott Hughes34023802011-08-30 12:06:17 -0700253 Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700254 for (size_t i = 0; i < verbose_options.size(); ++i) {
255 parsed->verbose_.insert(verbose_options[i]);
256 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700257 } else if (option == "vfprintf") {
258 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
259 } else if (option == "exit") {
260 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
261 } else if (option == "abort") {
262 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700263 } else {
264 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700265 // TODO: print usage via vfprintf
Brian Carlstrom8a436592011-08-15 21:27:23 -0700266 LOG(FATAL) << "Unrecognized option " << option;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700267 return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700268 }
269 }
270 }
271
272 if (boot_class_path == NULL) {
273 boot_class_path = "";
274 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700275 if (parsed->boot_class_path_.size() == 0) {
276 CreateBootClassPath(boot_class_path, parsed->boot_class_path_);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700277 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700278 return parsed.release();
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700279}
280
Brian Carlstrom8a436592011-08-15 21:27:23 -0700281Runtime* Runtime::Create(const std::vector<const DexFile*>& boot_class_path) {
282 Runtime::Options options;
283 options.push_back(std::make_pair("bootclasspath", &boot_class_path));
284 return Runtime::Create(options, false);
285}
286
287Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700288 // TODO: acquire a static mutex on Runtime to avoid racing.
289 if (Runtime::instance_ != NULL) {
290 return NULL;
291 }
Elliott Hughes90a33692011-08-30 13:27:07 -0700292 UniquePtr<Runtime> runtime(new Runtime());
Brian Carlstrom8a436592011-08-15 21:27:23 -0700293 bool success = runtime->Init(options, ignore_unrecognized);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700294 if (!success) {
295 return NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700296 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700297 instance_ = runtime.release();
298
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700299 instance_->InitLibraries();
Elliott Hughese27955c2011-08-26 15:21:24 -0700300 instance_->signal_catcher_ = new SignalCatcher;
301
Elliott Hughes0af55432011-08-17 18:37:28 -0700302 return instance_;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700303}
304
Elliott Hughes0af55432011-08-17 18:37:28 -0700305bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700306 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700307
Elliott Hughes90a33692011-08-30 13:27:07 -0700308 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
309 if (options.get() == NULL) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700310 return false;
311 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700312 vfprintf_ = options->hook_vfprintf_;
313 exit_ = options->hook_exit_;
314 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700315
Brian Carlstromb765be02011-08-17 23:54:10 -0700316 stack_size_ = options->stack_size_;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700317 thread_list_ = ThreadList::Create();
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700318
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700319 if (!Heap::Init(options->heap_initial_size_,
320 options->heap_maximum_size_,
321 options->boot_image_)) {
322 return false;
323 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700324
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700325 BlockSignals();
326
Elliott Hughes0af55432011-08-17 18:37:28 -0700327 bool verbose_jni = options->verbose_.find("jni") != options->verbose_.end();
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700328 java_vm_ = new JavaVMExt(this, options->check_jni_, verbose_jni);
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700329
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700330 if (!Thread::Startup()) {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700331 return false;
332 }
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700333 Thread* current_thread = Thread::Attach(this);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700334 thread_list_->Register(current_thread);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700335
Brian Carlstroma663ea52011-08-19 23:33:41 -0700336 class_linker_ = ClassLinker::Create(options->boot_class_path_, Heap::GetBootSpace());
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700337
Carl Shapiro1fb86202011-06-27 17:43:13 -0700338 return true;
339}
340
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700341void Runtime::InitLibraries() {
342 Thread* self = Thread::Current();
343 JNIEnv* env = self->GetJniEnv();
344
345 // Must be in the kNative state for JNI-based method registration.
346 ScopedThreadStateChange tsc(self, Thread::kNative);
347
348 // First set up the native methods provided by the runtime itself.
349 RegisterRuntimeNativeMethods(env);
350
351 // Now set up libcore, which is just a JNI library with a JNI_OnLoad.
352 // Most JNI libraries can just use System.loadLibrary, but you can't
353 // if you're the library that implements System.loadLibrary!
354 JniConstants::init(env);
355 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
356}
357
358void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
359#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
360 //REGISTER(register_dalvik_bytecode_OpcodeInfo);
361 //REGISTER(register_dalvik_system_DexFile);
362 //REGISTER(register_dalvik_system_VMDebug);
363 //REGISTER(register_dalvik_system_VMRuntime);
364 //REGISTER(register_dalvik_system_VMStack);
365 //REGISTER(register_dalvik_system_Zygote);
366 //REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700367 REGISTER(register_java_lang_Object);
368 REGISTER(register_java_lang_Runtime);
369 REGISTER(register_java_lang_String);
370 REGISTER(register_java_lang_System);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700371 //REGISTER(register_java_lang_Thread);
372 //REGISTER(register_java_lang_Throwable);
373 //REGISTER(register_java_lang_VMClassLoader);
374 //REGISTER(register_java_lang_reflect_AccessibleObject);
375 //REGISTER(register_java_lang_reflect_Array);
376 //REGISTER(register_java_lang_reflect_Constructor);
377 //REGISTER(register_java_lang_reflect_Field);
378 //REGISTER(register_java_lang_reflect_Method);
379 //REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700380 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700381 //REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
382 //REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
383 //REGISTER(register_sun_misc_Unsafe);
384#undef REGISTER
385}
386
Elliott Hughese27955c2011-08-26 15:21:24 -0700387void Runtime::DumpStatistics(std::ostream& os) {
388 // TODO: dump other runtime statistics?
389 os << "Loaded classes: " << class_linker_->NumLoadedClasses() << "\n";
390 os << "Intern table size: " << class_linker_->GetInternTable().Size() << "\n";
391 // LOGV("VM stats: meth=%d ifld=%d sfld=%d linear=%d",
392 // gDvm.numDeclaredMethods,
393 // gDvm.numDeclaredInstFields,
394 // gDvm.numDeclaredStaticFields,
395 // gDvm.pBootLoaderAlloc->curOffset);
396 // LOGI("GC precise methods: %d", dvmPointerSetGetCount(gDvm.preciseMethods));
397}
398
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700399void Runtime::BlockSignals() {
400 sigset_t sigset;
401 if (sigemptyset(&sigset) == -1) {
402 PLOG(FATAL) << "sigemptyset failed";
403 }
404 if (sigaddset(&sigset, SIGPIPE) == -1) {
405 PLOG(ERROR) << "sigaddset SIGPIPE failed";
406 }
407 // SIGQUIT is used to dump the runtime's state (including stack traces).
408 if (sigaddset(&sigset, SIGQUIT) == -1) {
409 PLOG(ERROR) << "sigaddset SIGQUIT failed";
410 }
411 // SIGUSR1 is used to initiate a heap dump.
412 if (sigaddset(&sigset, SIGUSR1) == -1) {
413 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
414 }
415 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
416}
417
Elliott Hughes75770752011-08-24 17:52:38 -0700418bool Runtime::AttachCurrentThread(const char* name, JNIEnv** penv, bool as_daemon) {
419 if (as_daemon) {
420 UNIMPLEMENTED(WARNING) << "TODO: do something different for daemon threads";
421 }
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700422 return Thread::Attach(instance_) != NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700423}
424
Ian Rogersb033c752011-07-20 12:22:35 -0700425bool Runtime::DetachCurrentThread() {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700426 Thread* self = Thread::Current();
427 thread_list_->Unregister(self);
428 delete self;
Ian Rogersb033c752011-07-20 12:22:35 -0700429 return true;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700430}
431
Brian Carlstrom1f870082011-08-23 16:02:11 -0700432void Runtime::VisitRoots(Heap::RootVistor* root_visitor, void* arg) const {
433 GetClassLinker()->VisitRoots(root_visitor, arg);
434 UNIMPLEMENTED(WARNING) << "mark other roots including per thread state and thread stacks";
435}
436
Carl Shapiro1fb86202011-06-27 17:43:13 -0700437} // namespace art