blob: a874464aa5889bebe1a7af41ae30323f2d890270 [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"
Brian Carlstrome24fa612011-09-29 00:53:55 -070013#include "image.h"
Elliott Hughescf4c6c42011-09-01 15:16:42 -070014#include "intern_table.h"
Elliott Hughesc5f7c912011-08-18 14:00:42 -070015#include "jni_internal.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070016#include "oat_file.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070017#include "signal_catcher.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070018#include "space.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070019#include "thread.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070020#include "thread_list.h"
Carl Shapiro61e019d2011-07-14 16:53:09 -070021
Elliott Hughes90a33692011-08-30 13:27:07 -070022// TODO: this drags in cutil/log.h, which conflicts with our logging.h.
23#include "JniConstants.h"
24
Carl Shapiro1fb86202011-06-27 17:43:13 -070025namespace art {
26
Carl Shapiro2ed144c2011-07-26 16:52:08 -070027Runtime* Runtime::instance_ = NULL;
28
Elliott Hughesdcc24742011-09-07 14:02:44 -070029Runtime::Runtime()
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -070030 : verbose_startup_(false),
31 default_stack_size_(Thread::kDefaultStackSize),
Elliott Hughesdcc24742011-09-07 14:02:44 -070032 thread_list_(NULL),
33 intern_table_(NULL),
34 class_linker_(NULL),
35 signal_catcher_(NULL),
36 java_vm_(NULL),
Brian Carlstrom16192862011-09-12 17:50:06 -070037 jni_stub_array_(NULL),
Brian Carlstrome24fa612011-09-29 00:53:55 -070038 abstract_method_error_stub_array_(NULL),
Ian Rogersff1ed472011-09-20 13:46:24 -070039 callee_save_method_(NULL),
Elliott Hughesdcc24742011-09-07 14:02:44 -070040 started_(false),
41 vfprintf_(NULL),
42 exit_(NULL),
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070043 abort_(NULL),
44 stats_enabled_(false) {
Elliott Hughesdcc24742011-09-07 14:02:44 -070045}
46
Carl Shapiro61e019d2011-07-14 16:53:09 -070047Runtime::~Runtime() {
Elliott Hughes5fe594f2011-09-08 12:33:17 -070048 // Make sure our internal threads are dead before we start tearing down things they're using.
49 delete signal_catcher_;
Elliott Hughes038a8062011-09-18 14:12:41 -070050 // TODO: GC thread.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070051
Elliott Hughes038a8062011-09-18 14:12:41 -070052 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
Elliott Hughes93e74e82011-09-13 11:07:03 -070053 delete thread_list_;
54
Carl Shapiro61e019d2011-07-14 16:53:09 -070055 delete class_linker_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070056 Heap::Destroy();
Elliott Hughescf4c6c42011-09-01 15:16:42 -070057 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070058 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -070059 Thread::Shutdown();
Carl Shapiro4acf4642011-07-26 18:54:13 -070060 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070061 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -070062 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -070063}
64
Elliott Hughesffe67362011-07-17 12:09:27 -070065void Runtime::Abort(const char* file, int line) {
66 // Get any pending output out of the way.
67 fflush(NULL);
68
69 // Many people have difficulty distinguish aborts from crashes,
70 // so be explicit.
71 LogMessage(file, line, ERROR, -1).stream() << "Runtime aborting...";
72
Elliott Hughesffe67362011-07-17 12:09:27 -070073 // Perform any platform-specific pre-abort actions.
74 PlatformAbort(file, line);
75
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070076 // use abort hook if we have one
77 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
78 Runtime::Current()->abort_();
79 // notreached
80 }
81
Elliott Hughesffe67362011-07-17 12:09:27 -070082 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -070083 // receive SIGABRT. debuggerd dumps the stack trace of the main
84 // thread, whether or not that was the thread that failed. By
85 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -070086 // fault in the current thread, and get a useful log from debuggerd.
87 // We can also trivially tell the difference between a VM crash and
88 // a deliberate abort by looking at the fault address.
89 *reinterpret_cast<char*>(0xdeadd00d) = 38;
90 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -070091 // notreached
92}
93
Elliott Hughesbf86d042011-08-31 17:53:14 -070094void Runtime::CallExitHook(jint status) {
95 if (exit_ != NULL) {
96 ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative);
97 exit_(status);
98 LOG(WARNING) << "Exit hook returned instead of exiting!";
99 }
100}
101
Brian Carlstrom8a436592011-08-15 21:27:23 -0700102// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
103// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
104// [gG] gigabytes.
105//
106// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700107// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
108// of 1024.
109//
110// The spec says the -Xmx and -Xms options must be multiples of 1024. It
111// doesn't say anything about -Xss.
112//
113// Returns 0 (a useless size) if "s" is malformed or specifies a low or
114// non-evenly-divisible value.
115//
116size_t ParseMemoryOption(const char *s, size_t div) {
117 // strtoul accepts a leading [+-], which we don't want,
118 // so make sure our string starts with a decimal digit.
119 if (isdigit(*s)) {
120 const char *s2;
121 size_t val = strtoul(s, (char **)&s2, 10);
122 if (s2 != s) {
123 // s2 should be pointing just after the number.
124 // If this is the end of the string, the user
125 // has specified a number of bytes. Otherwise,
126 // there should be exactly one more character
127 // that specifies a multiplier.
128 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700129 // The remainder of the string is either a single multiplier
130 // character, or nothing to indicate that the value is in
131 // bytes.
132 char c = *s2++;
133 if (*s2 == '\0') {
134 size_t mul;
135 if (c == '\0') {
136 mul = 1;
137 } else if (c == 'k' || c == 'K') {
138 mul = KB;
139 } else if (c == 'm' || c == 'M') {
140 mul = MB;
141 } else if (c == 'g' || c == 'G') {
142 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700143 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700144 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700145 return 0;
146 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700147
148 if (val <= std::numeric_limits<size_t>::max() / mul) {
149 val *= mul;
150 } else {
151 // Clamp to a multiple of 1024.
152 val = std::numeric_limits<size_t>::max() & ~(1024-1);
153 }
154 } else {
155 // There's more than one character after the numeric part.
156 return 0;
157 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700158 }
159 // The man page says that a -Xm value must be a multiple of 1024.
160 if (val % div == 0) {
161 return val;
162 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700163 }
164 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700165 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700166}
167
Elliott Hughes0af55432011-08-17 18:37:28 -0700168void LoadJniLibrary(JavaVMExt* vm, const char* name) {
169 // TODO: OS_SHARED_LIB_FORMAT_STR
170 std::string mapped_name(StringPrintf("lib%s.so", name));
Elliott Hughes75770752011-08-24 17:52:38 -0700171 std::string reason;
172 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700173 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
174 << reason;
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 Carlstrom58ae9412011-10-04 00:56:06 -0700180 const char* boot_class_path = getenv("BOOTCLASSPATH");
181 if (boot_class_path != NULL) {
182 parsed->boot_class_path_ = getenv("BOOTCLASSPATH");
183 }
184 const char* class_path = getenv("CLASSPATH");
185 if (class_path != NULL) {
186 parsed->class_path_ = getenv("CLASSPATH");
187 }
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700188#ifdef NDEBUG
Elliott Hughes5174fe62011-08-23 15:12:35 -0700189 // -Xcheck:jni is off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700190 parsed->check_jni_ = false;
191#else
192 // ...but on by default in debug builds.
193 parsed->check_jni_ = true;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700194#endif
Elliott Hughes85d15452011-09-16 17:33:01 -0700195
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 Carlstrom58ae9412011-10-04 00:56:06 -0700206 if (false) {
207 LOG(INFO) << "option[" << i << "]=" << option;
208 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700209 if (option.starts_with("-Xbootclasspath:")) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700210 parsed->boot_class_path_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700211 } else if (option == "-classpath" || option == "-cp") {
212 // TODO: support -Djava.class.path
213 i++;
214 if (i == options.size()) {
215 // TODO: usage
216 LOG(FATAL) << "Missing required class path value for " << option;
217 return NULL;
218 }
219 const StringPiece& value = options[i].first;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700220 parsed->class_path_ = value.data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700221 } else if (option.starts_with("-Ximage:")) {
222 parsed->images_.push_back(option.substr(strlen("-Ximage:")).data());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700223 } else if (option.starts_with("-Xcheck:jni")) {
224 parsed->check_jni_ = true;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700225 } else if (option.starts_with("-Xms")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700226 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024);
227 if (size == 0) {
228 if (ignore_unrecognized) {
229 continue;
230 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700231 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700232 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700233 return NULL;
234 }
235 parsed->heap_initial_size_ = size;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700236 } else if (option.starts_with("-Xmx")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700237 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024);
238 if (size == 0) {
239 if (ignore_unrecognized) {
240 continue;
241 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700242 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700243 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700244 return NULL;
245 }
246 parsed->heap_maximum_size_ = size;
247 } else if (option.starts_with("-Xss")) {
248 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1);
249 if (size == 0) {
250 if (ignore_unrecognized) {
251 continue;
252 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700253 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700254 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700255 return NULL;
256 }
257 parsed->stack_size_ = size;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700258 } else if (option.starts_with("-D")) {
259 parsed->properties_.push_back(option.substr(strlen("-D")).data());
Elliott Hughesa0957642011-09-02 14:27:33 -0700260 } else if (option.starts_with("-Xjnitrace:")) {
261 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:")).data();
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700262 } else if (option.starts_with("-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700263 std::vector<std::string> verbose_options;
Elliott Hughes34023802011-08-30 12:06:17 -0700264 Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700265 for (size_t i = 0; i < verbose_options.size(); ++i) {
266 parsed->verbose_.insert(verbose_options[i]);
267 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700268 } else if (option == "vfprintf") {
269 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
270 } else if (option == "exit") {
271 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
272 } else if (option == "abort") {
273 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700274 } else if (option == "host-prefix") {
275 parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700276 } else {
277 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700278 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700279 LOG(ERROR) << "Unrecognized option " << option;
280 // TODO: this should exit, but for now tolerate unknown options
281 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700282 }
283 }
284 }
285
Elliott Hughes85d15452011-09-16 17:33:01 -0700286 LOG(INFO) << "CheckJNI is " << (parsed->check_jni_ ? "on" : "off");
287
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700288 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700289}
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 Hughesdcc24742011-09-07 14:02:44 -0700296 instance_ = new Runtime;
297 if (!instance_->Init(options, ignore_unrecognized)) {
298 delete instance_;
299 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700300 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700301 return instance_;
302}
Elliott Hughes0af55432011-08-17 18:37:28 -0700303
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700304void Runtime::Start() {
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700305 if (IsVerboseStartup()) {
306 LOG(INFO) << "Runtime::Start entering";
307 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700308
309 CHECK(host_prefix_.empty()) << host_prefix_;
310
Elliott Hughes038a8062011-09-18 14:12:41 -0700311 InitNativeMethods();
Elliott Hughes85d15452011-09-16 17:33:01 -0700312
Elliott Hughes038a8062011-09-18 14:12:41 -0700313 Thread::FinishStartup();
Elliott Hughes85d15452011-09-16 17:33:01 -0700314
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700315 class_linker_->RunRootClinits();
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700316
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700317 // Class::AllocObject asserts that all objects allocated better be
318 // initialized after Runtime::IsStarted is true, so this needs to
319 // come after ClassLinker::RunRootClinits.
320 started_ = true;
321
Elliott Hughes85d15452011-09-16 17:33:01 -0700322 StartDaemonThreads();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700323
324 if (IsVerboseStartup()) {
325 LOG(INFO) << "Runtime::Start exiting";
326 }
Elliott Hughes85d15452011-09-16 17:33:01 -0700327}
328
329void Runtime::StartDaemonThreads() {
330 signal_catcher_ = new SignalCatcher;
331
Elliott Hughes719b3232011-09-25 17:42:19 -0700332 Thread* self = Thread::Current();
333 JNIEnv* env = self->GetJniEnv();
334 jclass c = env->FindClass("java/lang/Daemons");
Elliott Hughes85d15452011-09-16 17:33:01 -0700335 CHECK(c != NULL);
Elliott Hughes719b3232011-09-25 17:42:19 -0700336 jmethodID mid = env->GetStaticMethodID(c, "start", "()V");
337 CHECK(mid != NULL);
338 env->CallStaticVoidMethod(c, mid);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700339}
340
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700341bool Runtime::IsStarted() const {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700342 return started_;
343}
344
Elliott Hughes0af55432011-08-17 18:37:28 -0700345bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700346 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700347
Elliott Hughes90a33692011-08-30 13:27:07 -0700348 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
349 if (options.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700350 LOG(ERROR) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700351 return false;
352 }
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700353 verbose_startup_ = options->IsVerbose("startup");
354 if (IsVerboseStartup()) {
355 LOG(INFO) << "Runtime::Init -verbose:startup enabled";
356 }
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700357
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700358 host_prefix_ = options->host_prefix_;
359 boot_class_path_ = options->boot_class_path_;
360 class_path_ = options->class_path_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700361 properties_ = options->properties_;
362
Elliott Hughes0af55432011-08-17 18:37:28 -0700363 vfprintf_ = options->hook_vfprintf_;
364 exit_ = options->hook_exit_;
365 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700366
Elliott Hughesbe759c62011-09-08 19:38:21 -0700367 default_stack_size_ = options->stack_size_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700368
Elliott Hughes14357e82011-09-26 10:42:15 -0700369 thread_list_ = new ThreadList(options->IsVerbose("thread"));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700370 intern_table_ = new InternTable;
371
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700372 Heap::Init(options->heap_initial_size_, options->heap_maximum_size_, options->images_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700373
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700374 BlockSignals();
375
Elliott Hughesa0957642011-09-02 14:27:33 -0700376 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700377
Elliott Hughesbe759c62011-09-08 19:38:21 -0700378 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700379
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700380 // ClassLinker needs an attached thread, but we can't fully attach a thread
381 // without creating objects.
382 Thread::Attach(this, "main", false);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700383
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700384 CHECK_GE(Heap::GetSpaces().size(), 1U);
385 class_linker_ = ((Heap::GetSpaces()[0]->IsImageSpace())
386 ? ClassLinker::Create(intern_table_)
387 : ClassLinker::Create(options->boot_class_path_, intern_table_));
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700388
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700389 if (IsVerboseStartup()) {
390 LOG(INFO) << "Runtime::Init exiting";
391 }
Carl Shapiro1fb86202011-06-27 17:43:13 -0700392 return true;
393}
394
Elliott Hughes038a8062011-09-18 14:12:41 -0700395void Runtime::InitNativeMethods() {
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700396 if (IsVerboseStartup()) {
397 LOG(INFO) << "Runtime::InitNativeMethods entering";
398 }
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700399 Thread* self = Thread::Current();
400 JNIEnv* env = self->GetJniEnv();
401
Elliott Hughes418d20f2011-09-22 14:00:39 -0700402 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700403 ScopedThreadStateChange tsc(self, Thread::kNative);
404
Elliott Hughes418d20f2011-09-22 14:00:39 -0700405 // First set up JniConstants, which is used by both the runtime's built-in native
406 // methods and libcore.
Elliott Hughesfea966e2011-09-22 10:26:20 -0700407 JniConstants::init(env);
408
Elliott Hughes418d20f2011-09-22 14:00:39 -0700409 // Then set up the native methods provided by the runtime itself.
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700410 RegisterRuntimeNativeMethods(env);
411
Elliott Hughes418d20f2011-09-22 14:00:39 -0700412 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
413 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
414 // the library that implements System.loadLibrary!
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700415 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700416 if (IsVerboseStartup()) {
417 LOG(INFO) << "Runtime::InitNativeMethods exiting";
418 }
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700419}
420
421void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
422#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700423 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700424 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700425 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700426 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700427 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700428 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700429 REGISTER(register_java_lang_Object);
430 REGISTER(register_java_lang_Runtime);
431 REGISTER(register_java_lang_String);
432 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700433 REGISTER(register_java_lang_Thread);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700434 REGISTER(register_java_lang_Throwable);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700435 REGISTER(register_java_lang_VMClassLoader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700436 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700437 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700438 REGISTER(register_java_lang_reflect_Field);
439 REGISTER(register_java_lang_reflect_Method);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700440 //REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700441 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700442 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700443 //REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700444 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700445#undef REGISTER
446}
447
Elliott Hughes8daa0922011-09-11 13:46:25 -0700448void Runtime::Dump(std::ostream& os) {
Elliott Hughese27955c2011-08-26 15:21:24 -0700449 // TODO: dump other runtime statistics?
450 os << "Loaded classes: " << class_linker_->NumLoadedClasses() << "\n";
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700451 os << "Intern table size: " << GetInternTable()->Size() << "\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700452 // LOGV("VM stats: meth=%d ifld=%d sfld=%d linear=%d",
453 // gDvm.numDeclaredMethods,
454 // gDvm.numDeclaredInstFields,
455 // gDvm.numDeclaredStaticFields,
456 // gDvm.pBootLoaderAlloc->curOffset);
457 // LOGI("GC precise methods: %d", dvmPointerSetGetCount(gDvm.preciseMethods));
Elliott Hughes42ee1422011-09-06 12:33:32 -0700458 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700459
460 thread_list_->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700461}
462
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700463void Runtime::SetStatsEnabled(bool new_state) {
464 if (new_state == true) {
465 GetStats()->Clear(~0);
466 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
467 Thread::Current()->GetStats()->Clear(~0);
468 }
469 stats_enabled_ = new_state;
470}
471
472void Runtime::ResetStats(int kinds) {
473 GetStats()->Clear(kinds & 0xffff);
474 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
475 Thread::Current()->GetStats()->Clear(kinds >> 16);
476}
477
478RuntimeStats* Runtime::GetStats() {
479 return &stats_;
480}
481
482int32_t Runtime::GetStat(int kind) {
483 RuntimeStats* stats;
484 if (kind < (1<<16)) {
485 stats = GetStats();
486 } else {
487 stats = Thread::Current()->GetStats();
488 kind >>= 16;
489 }
490 switch (kind) {
491 case KIND_ALLOCATED_OBJECTS:
492 return stats->allocated_objects;
493 case KIND_ALLOCATED_BYTES:
494 return stats->allocated_bytes;
495 case KIND_FREED_OBJECTS:
496 return stats->freed_objects;
497 case KIND_FREED_BYTES:
498 return stats->freed_bytes;
499 case KIND_GC_INVOCATIONS:
500 return stats->gc_for_alloc_count;
501 case KIND_CLASS_INIT_COUNT:
502 return stats->class_init_count;
503 case KIND_CLASS_INIT_TIME:
504 // Convert ns to us, reduce to 32 bits.
505 return (int) (stats->class_init_time_ns / 1000);
506 case KIND_EXT_ALLOCATED_OBJECTS:
507 case KIND_EXT_ALLOCATED_BYTES:
508 case KIND_EXT_FREED_OBJECTS:
509 case KIND_EXT_FREED_BYTES:
510 return 0; // backward compatibility
511 default:
512 CHECK(false);
513 return -1; // unreachable
514 }
515}
516
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700517void Runtime::BlockSignals() {
518 sigset_t sigset;
519 if (sigemptyset(&sigset) == -1) {
520 PLOG(FATAL) << "sigemptyset failed";
521 }
522 if (sigaddset(&sigset, SIGPIPE) == -1) {
523 PLOG(ERROR) << "sigaddset SIGPIPE failed";
524 }
525 // SIGQUIT is used to dump the runtime's state (including stack traces).
526 if (sigaddset(&sigset, SIGQUIT) == -1) {
527 PLOG(ERROR) << "sigaddset SIGQUIT failed";
528 }
529 // SIGUSR1 is used to initiate a heap dump.
530 if (sigaddset(&sigset, SIGUSR1) == -1) {
531 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
532 }
533 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
534}
535
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700536void Runtime::AttachCurrentThread(const char* name, bool as_daemon) {
537 Thread::Attach(instance_, name, as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700538}
539
Elliott Hughesd92bec42011-09-02 17:04:36 -0700540void Runtime::DetachCurrentThread() {
Elliott Hughes038a8062011-09-18 14:12:41 -0700541 // TODO: check we're not calling DetachCurrentThread from a call stack that
542 // includes managed frames. (It's only valid if the stack is all-native.)
Elliott Hughes02b48d12011-09-07 17:15:51 -0700543 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700544}
545
Brian Carlstrome24fa612011-09-29 00:53:55 -0700546void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
547 class_linker_->VisitRoots(visitor, arg);
548 intern_table_->VisitRoots(visitor, arg);
549 java_vm_->VisitRoots(visitor, arg);
550 thread_list_->VisitRoots(visitor, arg);
551 visitor(jni_stub_array_, arg);
552 visitor(abstract_method_error_stub_array_, arg);
553 visitor(callee_save_method_, arg);
554
555 //(*visitor)(&gDvm.outOfMemoryObj, 0, ROOT_VM_INTERNAL, arg);
556 //(*visitor)(&gDvm.internalErrorObj, 0, ROOT_VM_INTERNAL, arg);
557 //(*visitor)(&gDvm.noClassDefFoundErrorObj, 0, ROOT_VM_INTERNAL, arg);
558 UNIMPLEMENTED(WARNING) << "some roots not marked";
559}
560
561bool Runtime::HasJniStubArray() const {
562 return jni_stub_array_ != NULL;
563}
564
565ByteArray* Runtime::GetJniStubArray() const {
566 CHECK(jni_stub_array_ != NULL);
567 return jni_stub_array_;
568}
569
570void Runtime::SetJniStubArray(ByteArray* jni_stub_array) {
571 CHECK(jni_stub_array != NULL);
572 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array);
573 jni_stub_array_ = jni_stub_array;
574}
575
576bool Runtime::HasAbstractMethodErrorStubArray() const {
577 return abstract_method_error_stub_array_ != NULL;
578}
579
580ByteArray* Runtime::GetAbstractMethodErrorStubArray() const {
581 CHECK(abstract_method_error_stub_array_ != NULL);
582 return abstract_method_error_stub_array_;
583}
584
585void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) {
586 CHECK(abstract_method_error_stub_array != NULL);
587 CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array);
588 abstract_method_error_stub_array_ = abstract_method_error_stub_array;
589}
590
Ian Rogersff1ed472011-09-20 13:46:24 -0700591Method* Runtime::CreateCalleeSaveMethod(InstructionSet insns) {
592 Class* method_class = Method::GetMethodClass();
593 Method* method = down_cast<Method*>(method_class->AllocObject());
594 method->SetDeclaringClass(method_class);
595 method->SetName(intern_table_->InternStrong("$$$callee_save_method$$$"));
596 method->SetSignature(intern_table_->InternStrong("()V"));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700597 method->SetCodeArray(NULL, insns);
Ian Rogersff1ed472011-09-20 13:46:24 -0700598 if ((insns == kThumb2) || (insns == kArm)) {
Ian Rogers15fdb8c2011-09-25 15:45:07 -0700599 size_t frame_size = (12 /* gprs */ + 32 /* fprs */ + 4 /* data */) * kPointerSize;
600 method->SetFrameSizeInBytes(frame_size);
601 method->SetReturnPcOffsetInBytes(frame_size - kPointerSize);
Ian Rogersff1ed472011-09-20 13:46:24 -0700602 method->SetCoreSpillMask((1 << art::arm::R1) |
603 (1 << art::arm::R2) |
604 (1 << art::arm::R3) |
605 (1 << art::arm::R4) |
606 (1 << art::arm::R5) |
607 (1 << art::arm::R6) |
608 (1 << art::arm::R7) |
609 (1 << art::arm::R8) |
610 (1 << art::arm::R9) |
611 (1 << art::arm::R10) |
612 (1 << art::arm::R11) |
613 (1 << art::arm::LR));
Ian Rogers15fdb8c2011-09-25 15:45:07 -0700614 method->SetFpSpillMask((1 << art::arm::S0) |
615 (1 << art::arm::S1) |
616 (1 << art::arm::S2) |
617 (1 << art::arm::S3) |
618 (1 << art::arm::S4) |
619 (1 << art::arm::S5) |
620 (1 << art::arm::S6) |
621 (1 << art::arm::S7) |
622 (1 << art::arm::S8) |
623 (1 << art::arm::S9) |
624 (1 << art::arm::S10) |
625 (1 << art::arm::S11) |
626 (1 << art::arm::S12) |
627 (1 << art::arm::S13) |
628 (1 << art::arm::S14) |
629 (1 << art::arm::S15) |
630 (1 << art::arm::S16) |
631 (1 << art::arm::S17) |
632 (1 << art::arm::S18) |
633 (1 << art::arm::S19) |
634 (1 << art::arm::S20) |
635 (1 << art::arm::S21) |
636 (1 << art::arm::S22) |
637 (1 << art::arm::S23) |
638 (1 << art::arm::S24) |
639 (1 << art::arm::S25) |
640 (1 << art::arm::S26) |
641 (1 << art::arm::S27) |
642 (1 << art::arm::S28) |
643 (1 << art::arm::S29) |
644 (1 << art::arm::S30) |
645 (1 << art::arm::S31));
Ian Rogersff1ed472011-09-20 13:46:24 -0700646 } else if (insns == kX86) {
647 method->SetFrameSizeInBytes(32);
648 method->SetReturnPcOffsetInBytes(28);
649 method->SetCoreSpillMask((1 << art::x86::EBX) |
650 (1 << art::x86::EBP) |
651 (1 << art::x86::ESI) |
652 (1 << art::x86::EDI));
653 method->SetFpSpillMask(0);
654 } else {
655 UNIMPLEMENTED(FATAL);
656 }
657 return method;
658}
659
Brian Carlstrome24fa612011-09-29 00:53:55 -0700660bool Runtime::HasCalleeSaveMethod() const {
661 return callee_save_method_ != NULL;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700662}
663
Brian Carlstrome24fa612011-09-29 00:53:55 -0700664// Returns a special method that describes all callee saves being spilled to the stack.
665Method* Runtime::GetCalleeSaveMethod() const {
666 CHECK(callee_save_method_ != NULL);
667 return callee_save_method_;
668}
669
670void Runtime::SetCalleeSaveMethod(Method* method) {
671 callee_save_method_ = method;
672}
673
674
Carl Shapiro1fb86202011-06-27 17:43:13 -0700675} // namespace art