blob: 3aa7e6e14a8e6fcab16d9e11823819ee0ddfa29a [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro1fb86202011-06-27 17:43:13 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "runtime.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070018
Brian Carlstromdbf05b72011-12-15 00:55:24 -080019#include <signal.h>
20
Elliott Hughesffe67362011-07-17 12:09:27 -070021#include <cstdio>
22#include <cstdlib>
Brian Carlstrom8a436592011-08-15 21:27:23 -070023#include <limits>
Carl Shapiro2ed144c2011-07-26 16:52:08 -070024#include <vector>
Elliott Hughesffe67362011-07-17 12:09:27 -070025
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070026#include "class_linker.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070027#include "class_loader.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070028#include "constants_arm.h"
29#include "constants_x86.h"
Elliott Hughes3bb81562011-10-21 18:52:59 -070030#include "debugger.h"
Elliott Hughes4d6850c2012-01-18 15:55:06 -080031#include "dex_verifier.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070032#include "heap.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070033#include "image.h"
Elliott Hughescf4c6c42011-09-01 15:16:42 -070034#include "intern_table.h"
Elliott Hughesc5f7c912011-08-18 14:00:42 -070035#include "jni_internal.h"
Elliott Hughes32d6e1e2011-10-11 14:47:44 -070036#include "monitor.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070037#include "oat_file.h"
Elliott Hughes726079d2011-10-07 18:43:44 -070038#include "ScopedLocalRef.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070039#include "signal_catcher.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070040#include "space.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070041#include "thread.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070042#include "thread_list.h"
jeffhaob5e81852012-03-12 11:15:45 -070043#include "trace.h"
Elliott Hughes3bb81562011-10-21 18:52:59 -070044#include "UniquePtr.h"
Carl Shapiro61e019d2011-07-14 16:53:09 -070045
Elliott Hughes90a33692011-08-30 13:27:07 -070046// TODO: this drags in cutil/log.h, which conflicts with our logging.h.
47#include "JniConstants.h"
48
Carl Shapiro1fb86202011-06-27 17:43:13 -070049namespace art {
50
Carl Shapiro2ed144c2011-07-26 16:52:08 -070051Runtime* Runtime::instance_ = NULL;
52
Elliott Hughesdcc24742011-09-07 14:02:44 -070053Runtime::Runtime()
Elliott Hughesd9c67be2012-02-02 19:54:06 -080054 : is_compiler_(false),
55 is_zygote_(false),
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -070056 default_stack_size_(Thread::kDefaultStackSize),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080057 heap_(NULL),
Elliott Hughesc33a32b2011-10-11 18:18:07 -070058 monitor_list_(NULL),
Elliott Hughesdcc24742011-09-07 14:02:44 -070059 thread_list_(NULL),
60 intern_table_(NULL),
61 class_linker_(NULL),
62 signal_catcher_(NULL),
63 java_vm_(NULL),
Brian Carlstrom16192862011-09-12 17:50:06 -070064 jni_stub_array_(NULL),
Brian Carlstrome24fa612011-09-29 00:53:55 -070065 abstract_method_error_stub_array_(NULL),
Ian Rogers19846512012-02-24 11:42:47 -080066 resolution_method_(NULL),
Ian Rogers7c3757f2012-02-15 17:18:53 -080067 system_class_loader_(NULL),
Elliott Hughes6b355752012-01-13 16:49:08 -080068 shutting_down_(false),
Elliott Hughesdcc24742011-09-07 14:02:44 -070069 started_(false),
70 vfprintf_(NULL),
71 exit_(NULL),
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070072 abort_(NULL),
jeffhao2692b572011-12-16 15:42:28 -080073 stats_enabled_(false),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080074 tracer_(NULL),
75 use_compile_time_class_path_(false) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -070076 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
77 resolution_stub_array_[i] = NULL;
78 }
79 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
80 callee_save_method_[i] = NULL;
81 }
Elliott Hughesdcc24742011-09-07 14:02:44 -070082}
83
Carl Shapiro61e019d2011-07-14 16:53:09 -070084Runtime::~Runtime() {
Elliott Hughes6b355752012-01-13 16:49:08 -080085 shutting_down_ = true;
86
jeffhaob5e81852012-03-12 11:15:45 -070087 if (IsMethodTracingActive()) {
88 Trace::Shutdown();
89 }
90
Elliott Hughesd1cc8362011-10-24 16:58:50 -070091 Dbg::StopJdwp();
92
Elliott Hughes5fe594f2011-09-08 12:33:17 -070093 // Make sure our internal threads are dead before we start tearing down things they're using.
94 delete signal_catcher_;
Elliott Hughes038a8062011-09-18 14:12:41 -070095 // TODO: GC thread.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070096
Elliott Hughes038a8062011-09-18 14:12:41 -070097 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
Elliott Hughes93e74e82011-09-13 11:07:03 -070098 delete thread_list_;
Elliott Hughesc33a32b2011-10-11 18:18:07 -070099 delete monitor_list_;
Elliott Hughes93e74e82011-09-13 11:07:03 -0700100
Carl Shapiro61e019d2011-07-14 16:53:09 -0700101 delete class_linker_;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800102 delete heap_;
Elliott Hughes4d6850c2012-01-18 15:55:06 -0800103 verifier::DexVerifier::DeleteGcMaps();
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700104 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700105 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700106 Thread::Shutdown();
Carl Shapiro4acf4642011-07-26 18:54:13 -0700107 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700108 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -0700109 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700110}
111
Elliott Hughescac6cc72011-11-03 20:31:21 -0700112static bool gAborting = false;
113
Elliott Hughese0918552011-10-28 17:18:29 -0700114struct AbortState {
115 void Dump(std::ostream& os) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700116 if (gAborting) {
117 os << "Runtime aborting --- recursively, so no thread-specific detail!\n";
118 return;
119 }
120 gAborting = true;
Elliott Hughese0918552011-10-28 17:18:29 -0700121 os << "Runtime aborting...\n";
Elliott Hughes6c7d2442012-02-01 18:40:47 -0800122 if (Runtime::Current() == NULL) {
123 os << "(Runtime does not yet exist!)\n";
124 return;
125 }
Elliott Hughese0918552011-10-28 17:18:29 -0700126 Thread* self = Thread::Current();
127 if (self == NULL) {
128 os << "(Aborting thread was not attached to runtime!)\n";
129 } else {
Elliott Hughes899e7892012-01-24 14:57:32 -0800130 self->Dump(os);
131 if (self->IsExceptionPending()) {
132 os << "Pending " << PrettyTypeOf(self->GetException()) << " on thread:\n"
133 << self->GetException()->Dump();
134 }
Elliott Hughese0918552011-10-28 17:18:29 -0700135 }
136 }
137};
138
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800139static Mutex& GetAbortLock() {
140 static Mutex abort_lock("abort lock");
141 return abort_lock;
142}
143
Elliott Hughesffe67362011-07-17 12:09:27 -0700144void Runtime::Abort(const char* file, int line) {
Elliott Hughes131aef82012-01-27 11:53:35 -0800145 // Ensure that we don't have multiple threads trying to abort at once,
146 // which would result in significantly worse diagnostics.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800147 MutexLock mu(GetAbortLock());
Elliott Hughes131aef82012-01-27 11:53:35 -0800148
Elliott Hughesffe67362011-07-17 12:09:27 -0700149 // Get any pending output out of the way.
150 fflush(NULL);
151
152 // Many people have difficulty distinguish aborts from crashes,
153 // so be explicit.
Elliott Hughese0918552011-10-28 17:18:29 -0700154 AbortState state;
Elliott Hughes9ee5f9c2012-02-03 18:33:16 -0800155 LOG(INTERNAL_FATAL) << Dumpable<AbortState>(state);
Elliott Hughesffe67362011-07-17 12:09:27 -0700156
Elliott Hughesffe67362011-07-17 12:09:27 -0700157 // Perform any platform-specific pre-abort actions.
158 PlatformAbort(file, line);
159
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700160 // use abort hook if we have one
161 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
162 Runtime::Current()->abort_();
163 // notreached
164 }
165
Elliott Hughesffe67362011-07-17 12:09:27 -0700166 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -0700167 // receive SIGABRT. debuggerd dumps the stack trace of the main
168 // thread, whether or not that was the thread that failed. By
169 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -0700170 // fault in the current thread, and get a useful log from debuggerd.
Elliott Hughes81ff3182012-03-23 20:35:56 -0700171 // We can also trivially tell the difference between a crash and
Elliott Hughesffe67362011-07-17 12:09:27 -0700172 // a deliberate abort by looking at the fault address.
173 *reinterpret_cast<char*>(0xdeadd00d) = 38;
174 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -0700175 // notreached
176}
177
Elliott Hughesbf86d042011-08-31 17:53:14 -0700178void Runtime::CallExitHook(jint status) {
179 if (exit_ != NULL) {
180 ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative);
181 exit_(status);
182 LOG(WARNING) << "Exit hook returned instead of exiting!";
183 }
184}
185
Brian Carlstrom8a436592011-08-15 21:27:23 -0700186// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
187// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
188// [gG] gigabytes.
189//
190// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700191// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
192// of 1024.
193//
194// The spec says the -Xmx and -Xms options must be multiples of 1024. It
195// doesn't say anything about -Xss.
196//
197// Returns 0 (a useless size) if "s" is malformed or specifies a low or
198// non-evenly-divisible value.
199//
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800200size_t ParseMemoryOption(const char* s, size_t div) {
Brian Carlstrom8a436592011-08-15 21:27:23 -0700201 // strtoul accepts a leading [+-], which we don't want,
202 // so make sure our string starts with a decimal digit.
203 if (isdigit(*s)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700204 char* s2;
205 size_t val = strtoul(s, &s2, 10);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700206 if (s2 != s) {
207 // s2 should be pointing just after the number.
208 // If this is the end of the string, the user
209 // has specified a number of bytes. Otherwise,
210 // there should be exactly one more character
211 // that specifies a multiplier.
212 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700213 // The remainder of the string is either a single multiplier
214 // character, or nothing to indicate that the value is in
215 // bytes.
216 char c = *s2++;
217 if (*s2 == '\0') {
218 size_t mul;
219 if (c == '\0') {
220 mul = 1;
221 } else if (c == 'k' || c == 'K') {
222 mul = KB;
223 } else if (c == 'm' || c == 'M') {
224 mul = MB;
225 } else if (c == 'g' || c == 'G') {
226 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700227 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700228 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700229 return 0;
230 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700231
232 if (val <= std::numeric_limits<size_t>::max() / mul) {
233 val *= mul;
234 } else {
235 // Clamp to a multiple of 1024.
236 val = std::numeric_limits<size_t>::max() & ~(1024-1);
237 }
238 } else {
239 // There's more than one character after the numeric part.
240 return 0;
241 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700242 }
243 // The man page says that a -Xm value must be a multiple of 1024.
244 if (val % div == 0) {
245 return val;
246 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700247 }
248 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700249 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700250}
251
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800252size_t ParseIntegerOrDie(const std::string& s) {
253 std::string::size_type colon = s.find(':');
254 if (colon == std::string::npos) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700255 LOG(FATAL) << "Missing integer: " << s;
256 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800257 const char* begin = &s[colon + 1];
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700258 char* end;
259 size_t result = strtoul(begin, &end, 10);
260 if (begin == end || *end != '\0') {
261 LOG(FATAL) << "Failed to parse integer in: " << s;
262 }
263 return result;
264}
265
Elliott Hughes0af55432011-08-17 18:37:28 -0700266void LoadJniLibrary(JavaVMExt* vm, const char* name) {
Elliott Hughes6cc332e2012-01-20 22:59:20 -0800267 std::string mapped_name(StringPrintf(OS_SHARED_LIB_FORMAT_STR, name));
Elliott Hughes75770752011-08-24 17:52:38 -0700268 std::string reason;
269 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700270 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
271 << reason;
272 }
273}
274
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700275Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700276 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800277 const char* boot_class_path_string = getenv("BOOTCLASSPATH");
278 if (boot_class_path_string != NULL) {
279 parsed->boot_class_path_string_ = boot_class_path_string;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700280 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800281 const char* class_path_string = getenv("CLASSPATH");
282 if (class_path_string != NULL) {
283 parsed->class_path_string_ = class_path_string;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700284 }
Elliott Hughes67d92002012-03-26 15:08:51 -0700285 // -Xcheck:jni is off by default for regular builds but on by default in debug builds.
286 parsed->check_jni_ = kIsDebugBuild;
Elliott Hughes85d15452011-09-16 17:33:01 -0700287
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700288 parsed->heap_initial_size_ = Heap::kInitialSize;
289 parsed->heap_maximum_size_ = Heap::kMaximumSize;
jeffhaoc1160702011-10-27 15:48:45 -0700290 parsed->heap_growth_limit_ = 0; // 0 means no growth limit
Brian Carlstromf734cf52011-08-17 16:28:14 -0700291 parsed->stack_size_ = Thread::kDefaultStackSize;
292
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800293 parsed->is_compiler_ = false;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700294 parsed->is_zygote_ = false;
295
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700296 parsed->jni_globals_max_ = 0;
Elliott Hughesfc861622011-10-17 17:57:47 -0700297 parsed->lock_profiling_threshold_ = 0;
298 parsed->hook_is_sensitive_thread_ = NULL;
299
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700300 parsed->hook_vfprintf_ = vfprintf;
301 parsed->hook_exit_ = exit;
302 parsed->hook_abort_ = abort;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700303
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800304// gLogVerbosity.class_linker = true; // TODO: don't check this in!
305// gLogVerbosity.compiler = true; // TODO: don't check this in!
306// gLogVerbosity.heap = true; // TODO: don't check this in!
307// gLogVerbosity.gc = true; // TODO: don't check this in!
308// gLogVerbosity.jdwp = true; // TODO: don't check this in!
309// gLogVerbosity.jni = true; // TODO: don't check this in!
310// gLogVerbosity.monitor = true; // TODO: don't check this in!
311// gLogVerbosity.startup = true; // TODO: don't check this in!
312// gLogVerbosity.third_party_jni = true; // TODO: don't check this in!
313// gLogVerbosity.threads = true; // TODO: don't check this in!
314
jeffhaob5e81852012-03-12 11:15:45 -0700315 parsed->method_trace_ = false;
316 parsed->method_trace_file_ = "/data/method-trace-file.bin";
317 parsed->method_trace_file_size_ = 10 * MB;
318
Brian Carlstrom8a436592011-08-15 21:27:23 -0700319 for (size_t i = 0; i < options.size(); ++i) {
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800320 const std::string option(options[i].first);
Brian Carlstrom0796af02011-10-12 14:31:45 -0700321 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700322 LOG(INFO) << "option[" << i << "]=" << option;
323 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800324 if (StartsWith(option, "-Xbootclasspath:")) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800325 parsed->boot_class_path_string_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700326 } else if (option == "-classpath" || option == "-cp") {
327 // TODO: support -Djava.class.path
328 i++;
329 if (i == options.size()) {
330 // TODO: usage
331 LOG(FATAL) << "Missing required class path value for " << option;
332 return NULL;
333 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800334 const StringPiece& value = options[i].first;
335 parsed->class_path_string_ = value.data();
336 } else if (option == "bootclasspath") {
337 parsed->boot_class_path_
338 = reinterpret_cast<const std::vector<const DexFile*>*>(options[i].second);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800339 } else if (StartsWith(option, "-Ximage:")) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800340 parsed->image_ = option.substr(strlen("-Ximage:")).data();
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800341 } else if (StartsWith(option, "-Xcheck:jni")) {
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700342 parsed->check_jni_ = true;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800343 } else if (StartsWith(option, "-Xrunjdwp:") || StartsWith(option, "-agentlib:jdwp=")) {
344 std::string tail(option.substr(option[1] == 'X' ? 10 : 15));
Elliott Hughes3bb81562011-10-21 18:52:59 -0700345 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
346 LOG(FATAL) << "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
347 << "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n";
348 return NULL;
349 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800350 } else if (StartsWith(option, "-Xms")) {
351 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).c_str(), 1024);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700352 if (size == 0) {
353 if (ignore_unrecognized) {
354 continue;
355 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700356 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700357 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700358 return NULL;
359 }
360 parsed->heap_initial_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800361 } else if (StartsWith(option, "-Xmx")) {
362 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).c_str(), 1024);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700363 if (size == 0) {
364 if (ignore_unrecognized) {
365 continue;
366 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700367 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700368 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700369 return NULL;
370 }
371 parsed->heap_maximum_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800372 } else if (StartsWith(option, "-XX:HeapGrowthLimit=")) {
373 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).c_str(), 1024);
jeffhaoc1160702011-10-27 15:48:45 -0700374 if (size == 0) {
375 if (ignore_unrecognized) {
376 continue;
377 }
378 // TODO: usage
379 LOG(FATAL) << "Failed to parse " << option;
380 return NULL;
381 }
382 parsed->heap_growth_limit_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800383 } else if (StartsWith(option, "-Xss")) {
384 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).c_str(), 1);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700385 if (size == 0) {
386 if (ignore_unrecognized) {
387 continue;
388 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700389 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700390 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700391 return NULL;
392 }
393 parsed->stack_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800394 } else if (StartsWith(option, "-D")) {
395 parsed->properties_.push_back(option.substr(strlen("-D")));
396 } else if (StartsWith(option, "-Xjnitrace:")) {
397 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:"));
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700398 } else if (option == "compiler") {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800399 parsed->is_compiler_ = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700400 } else if (option == "-Xzygote") {
401 parsed->is_zygote_ = true;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800402 } else if (StartsWith(option, "-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700403 std::vector<std::string> verbose_options;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800404 Split(option.substr(strlen("-verbose:")), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700405 for (size_t i = 0; i < verbose_options.size(); ++i) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800406 if (verbose_options[i] == "class") {
407 gLogVerbosity.class_linker = true;
408 } else if (verbose_options[i] == "compiler") {
409 gLogVerbosity.compiler = true;
410 } else if (verbose_options[i] == "heap") {
411 gLogVerbosity.heap = true;
412 } else if (verbose_options[i] == "gc") {
413 gLogVerbosity.gc = true;
414 } else if (verbose_options[i] == "jdwp") {
415 gLogVerbosity.jdwp = true;
416 } else if (verbose_options[i] == "jni") {
417 gLogVerbosity.jni = true;
418 } else if (verbose_options[i] == "monitor") {
419 gLogVerbosity.monitor = true;
420 } else if (verbose_options[i] == "startup") {
421 gLogVerbosity.startup = true;
422 } else if (verbose_options[i] == "third-party-jni") {
423 gLogVerbosity.third_party_jni = true;
424 } else if (verbose_options[i] == "threads") {
425 gLogVerbosity.threads = true;
426 } else {
427 LOG(WARNING) << "Ignoring unknown -verbose option: " << verbose_options[i];
428 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700429 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800430 } else if (StartsWith(option, "-Xjnigreflimit:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700431 parsed->jni_globals_max_ = ParseIntegerOrDie(option);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800432 } else if (StartsWith(option, "-Xlockprofthreshold:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700433 parsed->lock_profiling_threshold_ = ParseIntegerOrDie(option);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800434 } else if (StartsWith(option, "-Xstacktracefile:")) {
Elliott Hughes67d92002012-03-26 15:08:51 -0700435 if (kIsDebugBuild) {
436 // Ignore the zygote and always show stack traces in debug builds.
437 } else {
438 parsed->stack_trace_file_ = option.substr(strlen("-Xstacktracefile:"));
439 }
Elliott Hughesfc861622011-10-17 17:57:47 -0700440 } else if (option == "sensitiveThread") {
441 parsed->hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(options[i].second);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700442 } else if (option == "vfprintf") {
443 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
444 } else if (option == "exit") {
445 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
446 } else if (option == "abort") {
447 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700448 } else if (option == "host-prefix") {
449 parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second);
Elliott Hughesb3e66df2012-01-12 14:49:18 -0800450 } else if (option == "-Xgenregmap" || option == "-Xgc:precise") {
451 // We silently ignore these for backwards compatibility.
jeffhaob5e81852012-03-12 11:15:45 -0700452 } else if (option == "-Xmethod-trace") {
453 parsed->method_trace_ = true;
454 } else if (StartsWith(option, "-Xmethod-trace-file:")) {
455 parsed->method_trace_file_ = option.substr(strlen("-Xmethod-trace-file:"));
456 } else if (StartsWith(option, "-Xmethod-trace-file-size:")) {
457 parsed->method_trace_file_size_ = ParseIntegerOrDie(option);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700458 } else {
459 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700460 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700461 LOG(ERROR) << "Unrecognized option " << option;
462 // TODO: this should exit, but for now tolerate unknown options
463 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700464 }
465 }
466 }
467
Brian Carlstrom223f20f2012-02-04 23:06:55 -0800468 if (!parsed->is_compiler_ && parsed->image_.empty()) {
469 parsed->image_ += GetAndroidRoot();
470 parsed->image_ += "/framework/boot.art";
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700471 }
jeffhaoc1160702011-10-27 15:48:45 -0700472 if (parsed->heap_growth_limit_ == 0) {
473 parsed->heap_growth_limit_ = parsed->heap_maximum_size_;
474 }
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700475
Elliott Hughescac6cc72011-11-03 20:31:21 -0700476 LOG(INFO) << "Build type: "
Elliott Hughes67d92002012-03-26 15:08:51 -0700477 << (kIsDebugBuild ? "debug" : "optimized")
Elliott Hughescac6cc72011-11-03 20:31:21 -0700478 << "; CheckJNI: " << (parsed->check_jni_ ? "on" : "off");
Elliott Hughes85d15452011-09-16 17:33:01 -0700479
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700480 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700481}
482
483Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700484 // TODO: acquire a static mutex on Runtime to avoid racing.
485 if (Runtime::instance_ != NULL) {
486 return NULL;
487 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700488 instance_ = new Runtime;
489 if (!instance_->Init(options, ignore_unrecognized)) {
490 delete instance_;
491 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700492 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700493 return instance_;
494}
Elliott Hughes0af55432011-08-17 18:37:28 -0700495
Brian Carlstromaded5f72011-10-07 17:15:04 -0700496void CreateSystemClassLoader() {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800497 if (Runtime::Current()->UseCompileTimeClassPath()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700498 return;
499 }
500
501 Thread* self = Thread::Current();
502
503 // Must be in the kNative state for calling native methods.
504 CHECK_EQ(self->GetState(), Thread::kNative);
505
506 JNIEnv* env = self->GetJniEnv();
Brian Carlstromdf143242011-10-10 18:05:34 -0700507 ScopedLocalRef<jclass> ClassLoader_class(env, env->FindClass("java/lang/ClassLoader"));
508 CHECK(ClassLoader_class.get() != NULL);
509 jmethodID getSystemClassLoader = env->GetStaticMethodID(ClassLoader_class.get(),
510 "getSystemClassLoader",
511 "()Ljava/lang/ClassLoader;");
512 CHECK(getSystemClassLoader != NULL);
513 ScopedLocalRef<jobject> class_loader(env, env->CallStaticObjectMethod(ClassLoader_class.get(),
514 getSystemClassLoader));
515 CHECK(class_loader.get() != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700516
Brian Carlstromdf143242011-10-10 18:05:34 -0700517 Thread::Current()->SetClassLoaderOverride(Decode<ClassLoader*>(env, class_loader.get()));
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500518
519 ScopedLocalRef<jclass> Thread_class(env, env->FindClass("java/lang/Thread"));
520 CHECK(Thread_class.get() != NULL);
521 jfieldID contextClassLoader = env->GetFieldID(Thread_class.get(),
522 "contextClassLoader",
523 "Ljava/lang/ClassLoader;");
524 CHECK(contextClassLoader != NULL);
525 ScopedLocalRef<jobject> self_jobject(env, AddLocalReference<jobject>(env, self->GetPeer()));
526 env->SetObjectField(self_jobject.get(), contextClassLoader, class_loader.get());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700527}
528
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700529void Runtime::Start() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800530 VLOG(startup) << "Runtime::Start entering";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700531
532 CHECK(host_prefix_.empty()) << host_prefix_;
533
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700534 // Restore main thread state to kNative as expected by native code
535 Thread::Current()->SetState(Thread::kNative);
536
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700537 started_ = true;
538
Brian Carlstromae826982011-11-09 01:33:42 -0800539 // InitNativeMethods needs to be after started_ so that the classes
540 // it touches will have methods linked to the oat file if necessary.
541 InitNativeMethods();
542
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500543 Thread::FinishStartup();
544
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700545 if (!is_zygote_) {
546 DidForkFromZygote();
547 }
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700548
Elliott Hughes85d15452011-09-16 17:33:01 -0700549 StartDaemonThreads();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700550
Brian Carlstromaded5f72011-10-07 17:15:04 -0700551 CreateSystemClassLoader();
552
Elliott Hughes726079d2011-10-07 18:43:44 -0700553 Thread::Current()->GetJniEnv()->locals.AssertEmpty();
554
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800555 VLOG(startup) << "Runtime::Start exiting";
Elliott Hughes85d15452011-09-16 17:33:01 -0700556}
557
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700558void Runtime::DidForkFromZygote() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700559 is_zygote_ = false;
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700560
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700561 StartSignalCatcher();
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700562
563 // Start the JDWP thread. If the command-line debugger flags specified "suspend=y",
564 // this will pause the runtime, so we probably want this to come last.
565 Dbg::StartJdwp();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700566}
567
568void Runtime::StartSignalCatcher() {
569 if (!is_zygote_) {
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700570 signal_catcher_ = new SignalCatcher(stack_trace_file_);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700571 }
572}
573
Elliott Hughes85d15452011-09-16 17:33:01 -0700574void Runtime::StartDaemonThreads() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800575 VLOG(startup) << "Runtime::StartDaemonThreads entering";
Elliott Hughes85d15452011-09-16 17:33:01 -0700576
Elliott Hughes719b3232011-09-25 17:42:19 -0700577 Thread* self = Thread::Current();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700578
579 // Must be in the kNative state for calling native methods.
580 CHECK_EQ(self->GetState(), Thread::kNative);
581
Elliott Hughes719b3232011-09-25 17:42:19 -0700582 JNIEnv* env = self->GetJniEnv();
Elliott Hughes726079d2011-10-07 18:43:44 -0700583 ScopedLocalRef<jclass> c(env, env->FindClass("java/lang/Daemons"));
584 CHECK(c.get() != NULL);
585 jmethodID mid = env->GetStaticMethodID(c.get(), "start", "()V");
Elliott Hughes719b3232011-09-25 17:42:19 -0700586 CHECK(mid != NULL);
Elliott Hughes726079d2011-10-07 18:43:44 -0700587 env->CallStaticVoidMethod(c.get(), mid);
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800588 CHECK(!env->ExceptionCheck());
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700589
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800590 VLOG(startup) << "Runtime::StartDaemonThreads exiting";
Carl Shapiro61e019d2011-07-14 16:53:09 -0700591}
592
Elliott Hughes6b355752012-01-13 16:49:08 -0800593bool Runtime::IsShuttingDown() const {
594 return shutting_down_;
595}
596
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700597bool Runtime::IsStarted() const {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700598 return started_;
599}
600
Elliott Hughes0af55432011-08-17 18:37:28 -0700601bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700602 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700603
Elliott Hughes90a33692011-08-30 13:27:07 -0700604 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
605 if (options.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700606 LOG(ERROR) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700607 return false;
608 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800609 VLOG(startup) << "Runtime::Init -verbose:startup enabled";
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700610
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700611 SetJniGlobalsMax(options->jni_globals_max_);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800612 Monitor::Init(options->lock_profiling_threshold_, options->hook_is_sensitive_thread_);
Elliott Hughes32d6e1e2011-10-11 14:47:44 -0700613
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700614 host_prefix_ = options->host_prefix_;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800615 boot_class_path_string_ = options->boot_class_path_string_;
616 class_path_string_ = options->class_path_string_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700617 properties_ = options->properties_;
618
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800619 is_compiler_ = options->is_compiler_;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700620 is_zygote_ = options->is_zygote_;
621
Elliott Hughes0af55432011-08-17 18:37:28 -0700622 vfprintf_ = options->hook_vfprintf_;
623 exit_ = options->hook_exit_;
624 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700625
Elliott Hughesbe759c62011-09-08 19:38:21 -0700626 default_stack_size_ = options->stack_size_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700627 stack_trace_file_ = options->stack_trace_file_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700628
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700629 monitor_list_ = new MonitorList;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800630 thread_list_ = new ThreadList;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700631 intern_table_ = new InternTable;
632
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800633 verifier::DexVerifier::InitGcMaps();
634
635 heap_ = new Heap(options->heap_initial_size_,
636 options->heap_growth_limit_,
637 options->heap_maximum_size_,
638 options->image_);
jeffhao25045522012-03-13 19:34:37 -0700639 heap_->EnableObjectValidation();
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700640
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700641 BlockSignals();
642
Elliott Hughesa0957642011-09-02 14:27:33 -0700643 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700644
Elliott Hughesbe759c62011-09-08 19:38:21 -0700645 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700646
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700647 // ClassLinker needs an attached thread, but we can't fully attach a thread
Elliott Hughes462c9442012-03-23 18:47:50 -0700648 // without creating objects. We can't supply a thread group yet; it will be fixed later.
649 Thread::Attach("main", false, NULL);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700650
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700651 // Set us to runnable so tools using a runtime can allocate and GC by default
652 Thread::Current()->SetState(Thread::kRunnable);
653
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800654 CHECK_GE(GetHeap()->GetSpaces().size(), 1U);
655 if (GetHeap()->GetSpaces()[0]->IsImageSpace()) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800656 class_linker_ = ClassLinker::CreateFromImage(intern_table_);
657 } else {
658 CHECK(options->boot_class_path_ != NULL);
659 CHECK_NE(options->boot_class_path_->size(), 0U);
660 class_linker_ = ClassLinker::CreateFromCompiler(*options->boot_class_path_, intern_table_);
661 }
662 CHECK(class_linker_ != NULL);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700663
jeffhaob5e81852012-03-12 11:15:45 -0700664 method_trace_ = options->method_trace_;
665 method_trace_file_ = options->method_trace_file_;
666 method_trace_file_size_ = options->method_trace_file_size_;
667
668 if (options->method_trace_) {
669 Trace::Start(options->method_trace_file_.c_str(), -1, options->method_trace_file_size_, 0, false);
670 }
671
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800672 VLOG(startup) << "Runtime::Init exiting";
Carl Shapiro1fb86202011-06-27 17:43:13 -0700673 return true;
674}
675
Elliott Hughes038a8062011-09-18 14:12:41 -0700676void Runtime::InitNativeMethods() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800677 VLOG(startup) << "Runtime::InitNativeMethods entering";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700678 Thread* self = Thread::Current();
679 JNIEnv* env = self->GetJniEnv();
680
Elliott Hughes418d20f2011-09-22 14:00:39 -0700681 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Brian Carlstromaded5f72011-10-07 17:15:04 -0700682 CHECK_EQ(self->GetState(), Thread::kNative);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700683
Elliott Hughes418d20f2011-09-22 14:00:39 -0700684 // First set up JniConstants, which is used by both the runtime's built-in native
685 // methods and libcore.
Elliott Hughesfea966e2011-09-22 10:26:20 -0700686 JniConstants::init(env);
687
Elliott Hughes418d20f2011-09-22 14:00:39 -0700688 // Then set up the native methods provided by the runtime itself.
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700689 RegisterRuntimeNativeMethods(env);
690
Elliott Hughes418d20f2011-09-22 14:00:39 -0700691 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
692 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
693 // the library that implements System.loadLibrary!
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700694 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800695 VLOG(startup) << "Runtime::InitNativeMethods exiting";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700696}
697
698void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
699#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Ian Rogers5167c972012-02-03 10:41:20 -0800700 // Register Throwable first so that registration of other native methods can throw exceptions
701 REGISTER(register_java_lang_Throwable);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700702 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700703 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700704 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700705 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700706 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700707 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700708 REGISTER(register_java_lang_Object);
709 REGISTER(register_java_lang_Runtime);
710 REGISTER(register_java_lang_String);
711 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700712 REGISTER(register_java_lang_Thread);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700713 REGISTER(register_java_lang_VMClassLoader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700714 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700715 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700716 REGISTER(register_java_lang_reflect_Field);
717 REGISTER(register_java_lang_reflect_Method);
Jesse Wilson95caa792011-10-12 18:14:17 -0400718 REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700719 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700720 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700721 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700722 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700723#undef REGISTER
724}
725
Elliott Hughes8daa0922011-09-11 13:46:25 -0700726void Runtime::Dump(std::ostream& os) {
Elliott Hughese27955c2011-08-26 15:21:24 -0700727 // TODO: dump other runtime statistics?
Elliott Hughescac6cc72011-11-03 20:31:21 -0700728 GetClassLinker()->DumpForSigQuit(os);
729 GetInternTable()->DumpForSigQuit(os);
Elliott Hughes42ee1422011-09-06 12:33:32 -0700730 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700731
732 thread_list_->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700733}
734
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800735void Runtime::DumpLockHolders(std::ostream& os) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800736 pid_t heap_lock_owner = GetHeap()->GetLockOwner();
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800737 pid_t thread_list_lock_owner = GetThreadList()->GetLockOwner();
738 pid_t classes_lock_owner = GetClassLinker()->GetClassesLockOwner();
739 pid_t dex_lock_owner = GetClassLinker()->GetDexLockOwner();
740 if ((heap_lock_owner | thread_list_lock_owner | classes_lock_owner | dex_lock_owner) != 0) {
741 os << "Heap lock owner tid: " << heap_lock_owner << "\n"
742 << "ThreadList lock owner tid: " << thread_list_lock_owner << "\n"
743 << "ClassLinker classes lock owner tid: " << classes_lock_owner << "\n"
744 << "ClassLinker dex lock owner tid: " << dex_lock_owner << "\n";
745 }
746}
747
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700748void Runtime::SetStatsEnabled(bool new_state) {
749 if (new_state == true) {
750 GetStats()->Clear(~0);
751 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
752 Thread::Current()->GetStats()->Clear(~0);
753 }
754 stats_enabled_ = new_state;
755}
756
757void Runtime::ResetStats(int kinds) {
758 GetStats()->Clear(kinds & 0xffff);
759 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
760 Thread::Current()->GetStats()->Clear(kinds >> 16);
761}
762
763RuntimeStats* Runtime::GetStats() {
764 return &stats_;
765}
766
767int32_t Runtime::GetStat(int kind) {
768 RuntimeStats* stats;
769 if (kind < (1<<16)) {
770 stats = GetStats();
771 } else {
772 stats = Thread::Current()->GetStats();
773 kind >>= 16;
774 }
775 switch (kind) {
776 case KIND_ALLOCATED_OBJECTS:
777 return stats->allocated_objects;
778 case KIND_ALLOCATED_BYTES:
779 return stats->allocated_bytes;
780 case KIND_FREED_OBJECTS:
781 return stats->freed_objects;
782 case KIND_FREED_BYTES:
783 return stats->freed_bytes;
784 case KIND_GC_INVOCATIONS:
785 return stats->gc_for_alloc_count;
786 case KIND_CLASS_INIT_COUNT:
787 return stats->class_init_count;
788 case KIND_CLASS_INIT_TIME:
789 // Convert ns to us, reduce to 32 bits.
Elliott Hughes398f64b2012-03-26 18:05:48 -0700790 return static_cast<int>(stats->class_init_time_ns / 1000);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700791 case KIND_EXT_ALLOCATED_OBJECTS:
792 case KIND_EXT_ALLOCATED_BYTES:
793 case KIND_EXT_FREED_OBJECTS:
794 case KIND_EXT_FREED_BYTES:
795 return 0; // backward compatibility
796 default:
797 CHECK(false);
798 return -1; // unreachable
799 }
800}
801
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700802void Runtime::BlockSignals() {
803 sigset_t sigset;
804 if (sigemptyset(&sigset) == -1) {
805 PLOG(FATAL) << "sigemptyset failed";
806 }
807 if (sigaddset(&sigset, SIGPIPE) == -1) {
808 PLOG(ERROR) << "sigaddset SIGPIPE failed";
809 }
810 // SIGQUIT is used to dump the runtime's state (including stack traces).
811 if (sigaddset(&sigset, SIGQUIT) == -1) {
812 PLOG(ERROR) << "sigaddset SIGQUIT failed";
813 }
814 // SIGUSR1 is used to initiate a heap dump.
815 if (sigaddset(&sigset, SIGUSR1) == -1) {
816 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
817 }
818 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
819}
820
Elliott Hughes462c9442012-03-23 18:47:50 -0700821void Runtime::AttachCurrentThread(const char* thread_name, bool as_daemon, Object* thread_group) {
822 Thread::Attach(thread_name, as_daemon, thread_group);
Elliott Hughes22869a92012-03-27 14:08:24 -0700823 if (thread_name == NULL) {
824 LOG(WARNING) << *Thread::Current() << " attached without supplying a name";
825 }
Carl Shapiro61e019d2011-07-14 16:53:09 -0700826}
827
Elliott Hughesd92bec42011-09-02 17:04:36 -0700828void Runtime::DetachCurrentThread() {
Elliott Hughes22869a92012-03-27 14:08:24 -0700829 if (Thread::Current()->GetTopOfStack().GetSP() != NULL) {
830 LOG(FATAL) << *Thread::Current() << " attempting to detach while still running code";
831 }
Elliott Hughes02b48d12011-09-07 17:15:51 -0700832 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700833}
834
Brian Carlstrome24fa612011-09-29 00:53:55 -0700835void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700836 Dbg::VisitRoots(visitor, arg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700837 class_linker_->VisitRoots(visitor, arg);
838 intern_table_->VisitRoots(visitor, arg);
839 java_vm_->VisitRoots(visitor, arg);
840 thread_list_->VisitRoots(visitor, arg);
841 visitor(jni_stub_array_, arg);
842 visitor(abstract_method_error_stub_array_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700843 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
844 visitor(resolution_stub_array_[i], arg);
845 }
Ian Rogers19846512012-02-24 11:42:47 -0800846 visitor(resolution_method_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700847 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
848 visitor(callee_save_method_[i], arg);
849 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700850}
851
Ian Rogers169c9a72011-11-13 20:13:17 -0800852bool Runtime::HasJniDlsymLookupStub() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700853 return jni_stub_array_ != NULL;
854}
855
Ian Rogers169c9a72011-11-13 20:13:17 -0800856ByteArray* Runtime::GetJniDlsymLookupStub() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700857 CHECK(jni_stub_array_ != NULL);
858 return jni_stub_array_;
859}
860
Ian Rogers169c9a72011-11-13 20:13:17 -0800861void Runtime::SetJniDlsymLookupStub(ByteArray* jni_stub_array) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700862 CHECK(jni_stub_array != NULL) << " jni_stub_array=" << jni_stub_array;
863 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array)
864 << "jni_stub_array_=" << jni_stub_array_ << " jni_stub_array=" << jni_stub_array;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700865 jni_stub_array_ = jni_stub_array;
866}
867
868bool Runtime::HasAbstractMethodErrorStubArray() const {
869 return abstract_method_error_stub_array_ != NULL;
870}
871
872ByteArray* Runtime::GetAbstractMethodErrorStubArray() const {
873 CHECK(abstract_method_error_stub_array_ != NULL);
874 return abstract_method_error_stub_array_;
875}
876
877void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) {
878 CHECK(abstract_method_error_stub_array != NULL);
879 CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array);
880 abstract_method_error_stub_array_ = abstract_method_error_stub_array;
881}
882
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700883
884Runtime::TrampolineType Runtime::GetTrampolineType(Method* method) {
885 if (method == NULL) {
886 return Runtime::kUnknownMethod;
887 } else if (method->IsStatic()) {
888 return Runtime::kStaticMethod;
889 } else {
Ian Rogersfb6adba2012-03-04 21:51:51 -0800890 return Runtime::kUnknownMethod;
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700891 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700892}
893
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700894bool Runtime::HasResolutionStubArray(TrampolineType type) const {
895 return resolution_stub_array_[type] != NULL;
Ian Rogersad25ac52011-10-04 19:13:33 -0700896}
897
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700898ByteArray* Runtime::GetResolutionStubArray(TrampolineType type) const {
899 CHECK(HasResolutionStubArray(type));
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700900 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastTrampolineMethodType));
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700901 return resolution_stub_array_[type];
902}
903
904void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700905 CHECK(resolution_stub_array != NULL);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700906 CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array);
907 resolution_stub_array_[type] = resolution_stub_array;
Ian Rogersad25ac52011-10-04 19:13:33 -0700908}
909
Ian Rogers19846512012-02-24 11:42:47 -0800910Method* Runtime::CreateResolutionMethod() {
911 Class* method_class = Method::GetMethodClass();
912 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
913 method->SetDeclaringClass(method_class);
914 // TODO: use a special method for resolution method saves
915 method->SetDexMethodIndex(DexFile::kDexNoIndex16);
916 ByteArray* unknown_resolution_stub = GetResolutionStubArray(kUnknownMethod);
917 CHECK(unknown_resolution_stub != NULL);
918 method->SetCode(unknown_resolution_stub->GetData());
919 return method.get();
920}
921
922bool Runtime::HasResolutionMethod() const {
923 return resolution_method_ != NULL;
924}
925
926// Returns a special method that calls into a trampoline for runtime method resolution
927Method* Runtime::GetResolutionMethod() const {
928 CHECK(HasResolutionMethod());
929 return resolution_method_;
930}
931
932void Runtime::SetResolutionMethod(Method* method) {
933 resolution_method_ = method;
934}
935
Brian Carlstromae826982011-11-09 01:33:42 -0800936Method* Runtime::CreateCalleeSaveMethod(InstructionSet instruction_set, CalleeSaveType type) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700937 Class* method_class = Method::GetMethodClass();
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700938 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
Ian Rogersff1ed472011-09-20 13:46:24 -0700939 method->SetDeclaringClass(method_class);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800940 // TODO: use a special method for callee saves
Ian Rogers19846512012-02-24 11:42:47 -0800941 method->SetDexMethodIndex(DexFile::kDexNoIndex16);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700942 method->SetCode(NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800943 if ((instruction_set == kThumb2) || (instruction_set == kArm)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700944 uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6) | (1 << art::arm::R7) |
945 (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11);
946 uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3);
947 uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9);
948 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills :0) |
949 (type == kSaveAll ? all_spills :0) | (1 << art::arm::LR);
950 uint32_t fp_all_spills = (1 << art::arm::S0) | (1 << art::arm::S1) | (1 << art::arm::S2) |
951 (1 << art::arm::S3) | (1 << art::arm::S4) | (1 << art::arm::S5) |
952 (1 << art::arm::S6) | (1 << art::arm::S7) | (1 << art::arm::S8) |
953 (1 << art::arm::S9) | (1 << art::arm::S10) | (1 << art::arm::S11) |
954 (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) |
955 (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) |
956 (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) |
957 (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) |
958 (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) |
959 (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
960 (1 << art::arm::S30) | (1 << art::arm::S31);
961 uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
962 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
963 __builtin_popcount(fp_spills) /* fprs */ +
964 1 /* Method* */) * kPointerSize, kStackAlignment);
Ian Rogers15fdb8c2011-09-25 15:45:07 -0700965 method->SetFrameSizeInBytes(frame_size);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700966 method->SetCoreSpillMask(core_spills);
967 method->SetFpSpillMask(fp_spills);
Brian Carlstromae826982011-11-09 01:33:42 -0800968 } else if (instruction_set == kX86) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700969 method->SetFrameSizeInBytes(32);
Ian Rogersb41b33b2012-03-20 14:22:54 -0700970 method->SetCoreSpillMask((1 << art::x86::EBP) | (1 << art::x86::ESI) | (1 << art::x86::EDI));
Ian Rogersff1ed472011-09-20 13:46:24 -0700971 method->SetFpSpillMask(0);
972 } else {
973 UNIMPLEMENTED(FATAL);
974 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700975 return method.get();
Ian Rogersff1ed472011-09-20 13:46:24 -0700976}
977
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700978bool Runtime::HasCalleeSaveMethod(CalleeSaveType type) const {
979 return callee_save_method_[type] != NULL;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700980}
981
Brian Carlstrome24fa612011-09-29 00:53:55 -0700982// Returns a special method that describes all callee saves being spilled to the stack.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700983Method* Runtime::GetCalleeSaveMethod(CalleeSaveType type) const {
984 CHECK(HasCalleeSaveMethod(type));
985 return callee_save_method_[type];
Brian Carlstrome24fa612011-09-29 00:53:55 -0700986}
987
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700988void Runtime::SetCalleeSaveMethod(Method* method, CalleeSaveType type) {
989 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
990 callee_save_method_[type] = method;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700991}
992
jeffhao2692b572011-12-16 15:42:28 -0800993void Runtime::EnableMethodTracing(Trace* tracer) {
994 CHECK(!IsMethodTracingActive());
995 tracer_ = tracer;
996}
997
998void Runtime::DisableMethodTracing() {
999 CHECK(IsMethodTracingActive());
1000 delete tracer_;
1001 tracer_ = NULL;
1002}
1003
1004bool Runtime::IsMethodTracingActive() const {
1005 return (tracer_ != NULL);
1006}
1007
1008Trace* Runtime::GetTracer() const {
1009 CHECK(IsMethodTracingActive());
1010 return tracer_;
1011}
1012
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001013const std::vector<const DexFile*>& Runtime::GetCompileTimeClassPath(const ClassLoader* class_loader) {
1014 if (class_loader == NULL) {
1015 return GetClassLinker()->GetBootClassPath();
1016 }
1017 CHECK(UseCompileTimeClassPath());
1018 CompileTimeClassPaths::const_iterator it = compile_time_class_paths_.find(class_loader);
1019 CHECK(it != compile_time_class_paths_.end());
1020 return it->second;
1021}
1022
1023void Runtime::SetCompileTimeClassPath(const ClassLoader* class_loader, std::vector<const DexFile*>& class_path) {
1024 CHECK(!IsStarted());
1025 use_compile_time_class_path_ = true;
1026 compile_time_class_paths_[class_loader] = class_path;
1027}
1028
Carl Shapiro1fb86202011-06-27 17:43:13 -07001029} // namespace art