Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3 | #include "runtime.h" |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 4 | |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 5 | #include <cstdio> |
| 6 | #include <cstdlib> |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 7 | #include <limits> |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 8 | #include <vector> |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 9 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 10 | #include "class_linker.h" |
| 11 | #include "heap.h" |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 12 | #include "scoped_ptr.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 13 | #include "thread.h" |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 14 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 15 | namespace art { |
| 16 | |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 17 | Runtime* Runtime::instance_ = NULL; |
| 18 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 19 | Runtime::~Runtime() { |
| 20 | // TODO: use a smart pointer instead. |
| 21 | delete class_linker_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 22 | Heap::Destroy(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 23 | delete thread_list_; |
Carl Shapiro | 4acf464 | 2011-07-26 18:54:13 -0700 | [diff] [blame] | 24 | // TODO: acquire a static mutex on Runtime to avoid racing. |
| 25 | CHECK(instance_ == this); |
| 26 | instance_ = NULL; |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 27 | } |
| 28 | |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 29 | void 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 Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 37 | // Perform any platform-specific pre-abort actions. |
| 38 | PlatformAbort(file, line); |
| 39 | |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame^] | 40 | // 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 Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 46 | // If we call abort(3) on a device, all threads in the process |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 47 | // 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 Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 50 | // 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 Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 55 | // notreached |
| 56 | } |
| 57 | |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame^] | 58 | // Splits a C string using the given delimiter characters into a vector of |
| 59 | // strings. Empty strings will be omitted. |
| 60 | void 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 Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 64 | char* full = tmp.get(); |
| 65 | char* p = full; |
Carl Shapiro | d3c1575 | 2011-07-27 16:27:22 -0700 | [diff] [blame] | 66 | while (p != NULL) { |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame^] | 67 | p = strpbrk(full, delim); |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 68 | if (p != NULL) { |
| 69 | p[0] = '\0'; |
| 70 | } |
| 71 | if (full[0] != '\0') { |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame^] | 72 | vec.push_back(std::string(full)); |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 73 | } |
Carl Shapiro | d3c1575 | 2011-07-27 16:27:22 -0700 | [diff] [blame] | 74 | if (p != NULL) { |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 75 | full = p + 1; |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame^] | 80 | // Splits a colon delimited list of pathname elements into a vector of |
| 81 | // strings. Empty strings will be omitted. |
| 82 | void ParseClassPath(const char* class_path, std::vector<std::string>& vec) { |
| 83 | Split(class_path, ":", vec); |
| 84 | } |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 85 | |
| 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 | // |
| 101 | size_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 Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 148 | } |
| 149 | } |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 150 | return 0; |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Elliott Hughes | 40ef99e | 2011-08-11 17:44:34 -0700 | [diff] [blame] | 153 | DexFile* Open(const std::string& filename) { |
| 154 | if (filename.size() < 4) { |
| 155 | LOG(WARNING) << "Ignoring short classpath entry '" << filename << "'"; |
| 156 | return NULL; |
| 157 | } |
| 158 | std::string suffix(filename.substr(filename.size() - 4)); |
| 159 | if (suffix == ".zip" || suffix == ".jar" || suffix == ".apk") { |
| 160 | return DexFile::OpenZip(filename); |
| 161 | } else { |
| 162 | return DexFile::OpenFile(filename); |
| 163 | } |
| 164 | } |
| 165 | |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 166 | void CreateBootClassPath(const char* boot_class_path_cstr, |
| 167 | std::vector<DexFile*>& boot_class_path_vector) { |
| 168 | CHECK(boot_class_path_cstr != NULL); |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 169 | std::vector<std::string> parsed; |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame^] | 170 | ParseClassPath(boot_class_path_cstr, parsed); |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 171 | for (size_t i = 0; i < parsed.size(); ++i) { |
Elliott Hughes | 40ef99e | 2011-08-11 17:44:34 -0700 | [diff] [blame] | 172 | DexFile* dex_file = Open(parsed[i]); |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 173 | if (dex_file != NULL) { |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 174 | boot_class_path_vector.push_back(dex_file); |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame^] | 179 | Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) { |
| 180 | scoped_ptr<ParsedOptions> parsed(new ParsedOptions()); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 181 | const char* boot_class_path = getenv("BOOTCLASSPATH"); |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame^] | 182 | parsed->boot_image_ = NULL; |
| 183 | parsed->heap_initial_size_ = Heap::kInitialSize; |
| 184 | parsed->heap_maximum_size_ = Heap::kMaximumSize; |
| 185 | parsed->hook_vfprintf_ = vfprintf; |
| 186 | parsed->hook_exit_ = exit; |
| 187 | parsed->hook_abort_ = abort; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 188 | |
| 189 | for (size_t i = 0; i < options.size(); ++i) { |
| 190 | const StringPiece& option = options[i].first; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 191 | if (option.starts_with("-Xbootclasspath:")) { |
| 192 | boot_class_path = option.substr(strlen("-Xbootclasspath:")).data(); |
| 193 | } else if (option == "bootclasspath") { |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame^] | 194 | parsed->boot_class_path_ = *reinterpret_cast<const std::vector<DexFile*>*>(options[i].second); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 195 | } else if (option.starts_with("-Xbootimage:")) { |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame^] | 196 | parsed->boot_image_ = option.substr(strlen("-Xbootimage:")).data(); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 197 | } else if (option.starts_with("-Xms")) { |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame^] | 198 | parsed->heap_initial_size_ = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 199 | } else if (option.starts_with("-Xmx")) { |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame^] | 200 | parsed->heap_maximum_size_ = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024); |
| 201 | } else if (option.starts_with("-D")) { |
| 202 | parsed->properties_.push_back(option.substr(strlen("-D")).data()); |
| 203 | } else if (option.starts_with("-verbose:")) { |
| 204 | Split(option.substr(strlen("-verbose:")).data(), ",", parsed->verbose_); |
| 205 | } else if (option == "vfprintf") { |
| 206 | parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second); |
| 207 | } else if (option == "exit") { |
| 208 | parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second); |
| 209 | } else if (option == "abort") { |
| 210 | parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 211 | } else { |
| 212 | if (!ignore_unrecognized) { |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame^] | 213 | // TODO: print usage via vfprintf |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 214 | LOG(FATAL) << "Unrecognized option " << option; |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame^] | 215 | return NULL; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | if (boot_class_path == NULL) { |
| 221 | boot_class_path = ""; |
| 222 | } |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame^] | 223 | if (parsed->boot_class_path_.size() == 0) { |
| 224 | CreateBootClassPath(boot_class_path, parsed->boot_class_path_); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 225 | } |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame^] | 226 | return parsed.release(); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 229 | Runtime* Runtime::Create(const std::vector<const DexFile*>& boot_class_path) { |
| 230 | Runtime::Options options; |
| 231 | options.push_back(std::make_pair("bootclasspath", &boot_class_path)); |
| 232 | return Runtime::Create(options, false); |
| 233 | } |
| 234 | |
| 235 | Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 236 | // TODO: acquire a static mutex on Runtime to avoid racing. |
| 237 | if (Runtime::instance_ != NULL) { |
| 238 | return NULL; |
| 239 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 240 | scoped_ptr<Runtime> runtime(new Runtime()); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 241 | bool success = runtime->Init(options, ignore_unrecognized); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 242 | if (!success) { |
| 243 | return NULL; |
| 244 | } else { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 245 | return Runtime::instance_ = runtime.release(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 249 | bool Runtime::Init(const Options& options, bool ignore_unrecognized) { |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 250 | CHECK_EQ(kPageSize, sysconf(_SC_PAGE_SIZE)); |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame^] | 251 | |
| 252 | scoped_ptr<ParsedOptions> parsed_options(ParsedOptions::Create(options, ignore_unrecognized)); |
| 253 | if (parsed_options == NULL) { |
| 254 | return false; |
| 255 | } |
| 256 | vfprintf_ = parsed_options->hook_vfprintf_; |
| 257 | exit_ = parsed_options->hook_exit_; |
| 258 | abort_ = parsed_options->hook_abort_; |
| 259 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 260 | thread_list_ = ThreadList::Create(); |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame^] | 261 | |
| 262 | Heap::Init(parsed_options->heap_initial_size_, |
| 263 | parsed_options->heap_maximum_size_); |
| 264 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 265 | Thread::Init(); |
| 266 | Thread* current_thread = Thread::Attach(); |
| 267 | thread_list_->Register(current_thread); |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame^] | 268 | |
| 269 | class_linker_ = ClassLinker::Create(parsed_options->boot_class_path_); |
| 270 | |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 271 | java_vm_.reset(CreateJavaVM(this)); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 272 | return true; |
| 273 | } |
| 274 | |
Elliott Hughes | 40ef99e | 2011-08-11 17:44:34 -0700 | [diff] [blame] | 275 | bool Runtime::AttachCurrentThread(const char* name, JNIEnv** penv) { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 276 | return Thread::Attach() != NULL; |
| 277 | } |
| 278 | |
Elliott Hughes | 40ef99e | 2011-08-11 17:44:34 -0700 | [diff] [blame] | 279 | bool Runtime::AttachCurrentThreadAsDaemon(const char* name, JNIEnv** penv) { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 280 | // TODO: do something different for daemon threads. |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 281 | return Thread::Attach() != NULL; |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 284 | bool Runtime::DetachCurrentThread() { |
Elliott Hughes | 53b6131 | 2011-08-12 18:28:20 -0700 | [diff] [blame] | 285 | UNIMPLEMENTED(WARNING); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 286 | return true; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | } // namespace art |