blob: 11eb9bd97dd78b1bdeb443d89872b63fa8f900ca [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
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070010#include "class_linker.h"
11#include "heap.h"
Carl Shapirofc322c72011-07-27 00:20:01 -070012#include "scoped_ptr.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070013#include "thread.h"
Carl Shapiro61e019d2011-07-14 16:53:09 -070014
Carl Shapiro1fb86202011-06-27 17:43:13 -070015namespace art {
16
Carl Shapiro2ed144c2011-07-26 16:52:08 -070017Runtime* Runtime::instance_ = NULL;
18
Carl Shapiro61e019d2011-07-14 16:53:09 -070019Runtime::~Runtime() {
20 // TODO: use a smart pointer instead.
21 delete class_linker_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070022 Heap::Destroy();
Carl Shapiro61e019d2011-07-14 16:53:09 -070023 delete thread_list_;
Carl Shapiro4acf4642011-07-26 18:54:13 -070024 // TODO: acquire a static mutex on Runtime to avoid racing.
25 CHECK(instance_ == this);
26 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -070027}
28
Elliott Hughesffe67362011-07-17 12:09:27 -070029void Runtime::Abort(const char* file, int line) {
30 // Get any pending output out of the way.
31 fflush(NULL);
32
33 // Many people have difficulty distinguish aborts from crashes,
34 // so be explicit.
35 LogMessage(file, line, ERROR, -1).stream() << "Runtime aborting...";
36
Elliott Hughesffe67362011-07-17 12:09:27 -070037 // Perform any platform-specific pre-abort actions.
38 PlatformAbort(file, line);
39
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070040 // use abort hook if we have one
41 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
42 Runtime::Current()->abort_();
43 // notreached
44 }
45
Elliott Hughesffe67362011-07-17 12:09:27 -070046 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -070047 // receive SIGABRT. debuggerd dumps the stack trace of the main
48 // thread, whether or not that was the thread that failed. By
49 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -070050 // fault in the current thread, and get a useful log from debuggerd.
51 // We can also trivially tell the difference between a VM crash and
52 // a deliberate abort by looking at the fault address.
53 *reinterpret_cast<char*>(0xdeadd00d) = 38;
54 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -070055 // notreached
56}
57
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070058// Splits a C string using the given delimiter characters into a vector of
59// strings. Empty strings will be omitted.
60void Split(const char* str, const char* delim, std::vector<std::string>& vec) {
61 DCHECK(str != NULL);
62 DCHECK(delim != NULL);
63 scoped_ptr_malloc<char> tmp(strdup(str));
Carl Shapirofc322c72011-07-27 00:20:01 -070064 char* full = tmp.get();
65 char* p = full;
Carl Shapirod3c15752011-07-27 16:27:22 -070066 while (p != NULL) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070067 p = strpbrk(full, delim);
Carl Shapirofc322c72011-07-27 00:20:01 -070068 if (p != NULL) {
69 p[0] = '\0';
70 }
71 if (full[0] != '\0') {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070072 vec.push_back(std::string(full));
Carl Shapirofc322c72011-07-27 00:20:01 -070073 }
Carl Shapirod3c15752011-07-27 16:27:22 -070074 if (p != NULL) {
Carl Shapirofc322c72011-07-27 00:20:01 -070075 full = p + 1;
76 }
77 }
78}
79
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070080// Splits a colon delimited list of pathname elements into a vector of
81// strings. Empty strings will be omitted.
82void ParseClassPath(const char* class_path, std::vector<std::string>& vec) {
83 Split(class_path, ":", vec);
84}
Brian Carlstrom8a436592011-08-15 21:27:23 -070085
86// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
87// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
88// [gG] gigabytes.
89//
90// "s" should point just past the "-Xm?" part of the string.
91// "min" specifies the lowest acceptable value described by "s".
92// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
93// of 1024.
94//
95// The spec says the -Xmx and -Xms options must be multiples of 1024. It
96// doesn't say anything about -Xss.
97//
98// Returns 0 (a useless size) if "s" is malformed or specifies a low or
99// non-evenly-divisible value.
100//
101size_t ParseMemoryOption(const char *s, size_t div) {
102 // strtoul accepts a leading [+-], which we don't want,
103 // so make sure our string starts with a decimal digit.
104 if (isdigit(*s)) {
105 const char *s2;
106 size_t val = strtoul(s, (char **)&s2, 10);
107 if (s2 != s) {
108 // s2 should be pointing just after the number.
109 // If this is the end of the string, the user
110 // has specified a number of bytes. Otherwise,
111 // there should be exactly one more character
112 // that specifies a multiplier.
113 if (*s2 != '\0') {
114 // The remainder of the string is either a single multiplier
115 // character, or nothing to indicate that the value is in
116 // bytes.
117 char c = *s2++;
118 if (*s2 == '\0') {
119 size_t mul;
120 if (c == '\0') {
121 mul = 1;
122 } else if (c == 'k' || c == 'K') {
123 mul = 1024;
124 } else if (c == 'm' || c == 'M') {
125 mul = 1024 * 1024;
126 } else if (c == 'g' || c == 'G') {
127 mul = 1024 * 1024 * 1024;
128 } else {
129 // Unknown multiplier character.
130 return 0;
131 }
132
133 if (val <= std::numeric_limits<size_t>::max() / mul) {
134 val *= mul;
135 } else {
136 // Clamp to a multiple of 1024.
137 val = std::numeric_limits<size_t>::max() & ~(1024-1);
138 }
139 } else {
140 // There's more than one character after the numeric part.
141 return 0;
142 }
143 }
144 // The man page says that a -Xm value must be a multiple of 1024.
145 if (val % div == 0) {
146 return val;
147 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700148 }
149 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700150 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700151}
152
Elliott Hughes0af55432011-08-17 18:37:28 -0700153void LoadJniLibrary(JavaVMExt* vm, const char* name) {
154 // TODO: OS_SHARED_LIB_FORMAT_STR
155 std::string mapped_name(StringPrintf("lib%s.so", name));
156 char* reason = NULL;
157 if (!vm->LoadNativeLibrary(mapped_name, NULL, &reason)) {
158 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
159 << reason;
160 }
161}
162
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700163DexFile* Open(const std::string& filename) {
164 if (filename.size() < 4) {
165 LOG(WARNING) << "Ignoring short classpath entry '" << filename << "'";
166 return NULL;
167 }
168 std::string suffix(filename.substr(filename.size() - 4));
169 if (suffix == ".zip" || suffix == ".jar" || suffix == ".apk") {
170 return DexFile::OpenZip(filename);
171 } else {
172 return DexFile::OpenFile(filename);
173 }
174}
175
Brian Carlstrom8a436592011-08-15 21:27:23 -0700176void CreateBootClassPath(const char* boot_class_path_cstr,
177 std::vector<DexFile*>& boot_class_path_vector) {
178 CHECK(boot_class_path_cstr != NULL);
Carl Shapirofc322c72011-07-27 00:20:01 -0700179 std::vector<std::string> parsed;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700180 ParseClassPath(boot_class_path_cstr, parsed);
Carl Shapirofc322c72011-07-27 00:20:01 -0700181 for (size_t i = 0; i < parsed.size(); ++i) {
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700182 DexFile* dex_file = Open(parsed[i]);
Carl Shapirofc322c72011-07-27 00:20:01 -0700183 if (dex_file != NULL) {
Brian Carlstrom8a436592011-08-15 21:27:23 -0700184 boot_class_path_vector.push_back(dex_file);
Carl Shapirofc322c72011-07-27 00:20:01 -0700185 }
186 }
187}
188
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700189Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
190 scoped_ptr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstrom8a436592011-08-15 21:27:23 -0700191 const char* boot_class_path = getenv("BOOTCLASSPATH");
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700192 parsed->boot_image_ = NULL;
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700193#ifdef NDEBUG
Elliott Hughes0af55432011-08-17 18:37:28 -0700194 // -Xcheck:jni and -verbose:jni are off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700195 parsed->check_jni_ = false;
196#else
197 // ...but on by default in debug builds.
198 parsed->check_jni_ = true;
Elliott Hughes0af55432011-08-17 18:37:28 -0700199 parsed->verbose_.insert("jni");
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700200#endif
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700201 parsed->heap_initial_size_ = Heap::kInitialSize;
202 parsed->heap_maximum_size_ = Heap::kMaximumSize;
203 parsed->hook_vfprintf_ = vfprintf;
204 parsed->hook_exit_ = exit;
205 parsed->hook_abort_ = abort;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700206
207 for (size_t i = 0; i < options.size(); ++i) {
208 const StringPiece& option = options[i].first;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700209 if (option.starts_with("-Xbootclasspath:")) {
210 boot_class_path = option.substr(strlen("-Xbootclasspath:")).data();
211 } else if (option == "bootclasspath") {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700212 parsed->boot_class_path_ = *reinterpret_cast<const std::vector<DexFile*>*>(options[i].second);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700213 } else if (option.starts_with("-Xbootimage:")) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700214 parsed->boot_image_ = option.substr(strlen("-Xbootimage:")).data();
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700215 } else if (option.starts_with("-Xcheck:jni")) {
216 parsed->check_jni_ = true;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700217 } else if (option.starts_with("-Xms")) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700218 parsed->heap_initial_size_ = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700219 } else if (option.starts_with("-Xmx")) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700220 parsed->heap_maximum_size_ = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024);
221 } else if (option.starts_with("-D")) {
222 parsed->properties_.push_back(option.substr(strlen("-D")).data());
223 } else if (option.starts_with("-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700224 std::vector<std::string> verbose_options;
225 Split(option.substr(strlen("-verbose:")).data(), ",", verbose_options);
226 for (size_t i = 0; i < verbose_options.size(); ++i) {
227 parsed->verbose_.insert(verbose_options[i]);
228 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700229 } else if (option == "vfprintf") {
230 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
231 } else if (option == "exit") {
232 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
233 } else if (option == "abort") {
234 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700235 } else {
236 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700237 // TODO: print usage via vfprintf
Brian Carlstrom8a436592011-08-15 21:27:23 -0700238 LOG(FATAL) << "Unrecognized option " << option;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700239 return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700240 }
241 }
242 }
243
244 if (boot_class_path == NULL) {
245 boot_class_path = "";
246 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700247 if (parsed->boot_class_path_.size() == 0) {
248 CreateBootClassPath(boot_class_path, parsed->boot_class_path_);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700249 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700250 return parsed.release();
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700251}
252
Brian Carlstrom8a436592011-08-15 21:27:23 -0700253Runtime* Runtime::Create(const std::vector<const DexFile*>& boot_class_path) {
254 Runtime::Options options;
255 options.push_back(std::make_pair("bootclasspath", &boot_class_path));
256 return Runtime::Create(options, false);
257}
258
259Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700260 // TODO: acquire a static mutex on Runtime to avoid racing.
261 if (Runtime::instance_ != NULL) {
262 return NULL;
263 }
Carl Shapiro61e019d2011-07-14 16:53:09 -0700264 scoped_ptr<Runtime> runtime(new Runtime());
Brian Carlstrom8a436592011-08-15 21:27:23 -0700265 bool success = runtime->Init(options, ignore_unrecognized);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700266 if (!success) {
267 return NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700268 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700269 instance_ = runtime.release();
270
271 // Most JNI libraries can just use System.loadLibrary, but you can't
272 // if you're the library that implements System.loadLibrary!
273 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
274
275 return instance_;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700276}
277
Elliott Hughes0af55432011-08-17 18:37:28 -0700278bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700279 CHECK_EQ(kPageSize, sysconf(_SC_PAGE_SIZE));
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700280
Elliott Hughes0af55432011-08-17 18:37:28 -0700281 scoped_ptr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
282 if (options == NULL) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700283 return false;
284 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700285 vfprintf_ = options->hook_vfprintf_;
286 exit_ = options->hook_exit_;
287 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700288
Carl Shapiro61e019d2011-07-14 16:53:09 -0700289 thread_list_ = ThreadList::Create();
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700290
Elliott Hughes0af55432011-08-17 18:37:28 -0700291 Heap::Init(options->heap_initial_size_,
292 options->heap_maximum_size_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700293
Elliott Hughes0af55432011-08-17 18:37:28 -0700294 bool verbose_jni = options->verbose_.find("jni") != options->verbose_.end();
295 java_vm_.reset(new JavaVMExt(this, options->check_jni_, verbose_jni));
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700296
Carl Shapiro61e019d2011-07-14 16:53:09 -0700297 Thread::Init();
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700298 Thread* current_thread = Thread::Attach(this);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700299 thread_list_->Register(current_thread);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700300
Elliott Hughes0af55432011-08-17 18:37:28 -0700301 class_linker_ = ClassLinker::Create(options->boot_class_path_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700302
Carl Shapiro1fb86202011-06-27 17:43:13 -0700303 return true;
304}
305
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700306bool Runtime::AttachCurrentThread(const char* name, JNIEnv** penv) {
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700307 return Thread::Attach(instance_) != NULL;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700308}
309
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700310bool Runtime::AttachCurrentThreadAsDaemon(const char* name, JNIEnv** penv) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700311 // TODO: do something different for daemon threads.
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700312 return Thread::Attach(instance_) != NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700313}
314
Ian Rogersb033c752011-07-20 12:22:35 -0700315bool Runtime::DetachCurrentThread() {
Elliott Hughes53b61312011-08-12 18:28:20 -0700316 UNIMPLEMENTED(WARNING);
Ian Rogersb033c752011-07-20 12:22:35 -0700317 return true;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700318}
319
320} // namespace art