blob: 014ce6b475567fcbdf1c28e366375b4a8d1395e0 [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>
TDYa127b92bcab2012-04-08 00:09:51 -070025#include <fstream>
Elliott Hughesffe67362011-07-17 12:09:27 -070026
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070027#include "class_linker.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070028#include "class_loader.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070029#include "constants_arm.h"
30#include "constants_x86.h"
Elliott Hughes3bb81562011-10-21 18:52:59 -070031#include "debugger.h"
Elliott Hughes4d6850c2012-01-18 15:55:06 -080032#include "dex_verifier.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070033#include "heap.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070034#include "image.h"
Elliott Hughescf4c6c42011-09-01 15:16:42 -070035#include "intern_table.h"
Elliott Hughesc5f7c912011-08-18 14:00:42 -070036#include "jni_internal.h"
Elliott Hughes32d6e1e2011-10-11 14:47:44 -070037#include "monitor.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070038#include "oat_file.h"
Elliott Hughes726079d2011-10-07 18:43:44 -070039#include "ScopedLocalRef.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070040#include "signal_catcher.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070041#include "space.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070042#include "thread.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070043#include "thread_list.h"
jeffhaob5e81852012-03-12 11:15:45 -070044#include "trace.h"
Elliott Hughes3bb81562011-10-21 18:52:59 -070045#include "UniquePtr.h"
Carl Shapiro61e019d2011-07-14 16:53:09 -070046
Elliott Hughes90a33692011-08-30 13:27:07 -070047// TODO: this drags in cutil/log.h, which conflicts with our logging.h.
48#include "JniConstants.h"
49
Carl Shapiro1fb86202011-06-27 17:43:13 -070050namespace art {
51
Carl Shapiro2ed144c2011-07-26 16:52:08 -070052Runtime* Runtime::instance_ = NULL;
53
Elliott Hughesdcc24742011-09-07 14:02:44 -070054Runtime::Runtime()
Elliott Hughesd9c67be2012-02-02 19:54:06 -080055 : is_compiler_(false),
56 is_zygote_(false),
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -070057 default_stack_size_(Thread::kDefaultStackSize),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080058 heap_(NULL),
Elliott Hughesc33a32b2011-10-11 18:18:07 -070059 monitor_list_(NULL),
Elliott Hughesdcc24742011-09-07 14:02:44 -070060 thread_list_(NULL),
61 intern_table_(NULL),
62 class_linker_(NULL),
63 signal_catcher_(NULL),
64 java_vm_(NULL),
Brian Carlstrom16192862011-09-12 17:50:06 -070065 jni_stub_array_(NULL),
Brian Carlstrome24fa612011-09-29 00:53:55 -070066 abstract_method_error_stub_array_(NULL),
Ian Rogers19846512012-02-24 11:42:47 -080067 resolution_method_(NULL),
Ian Rogers7c3757f2012-02-15 17:18:53 -080068 system_class_loader_(NULL),
Elliott Hughes6b355752012-01-13 16:49:08 -080069 shutting_down_(false),
Elliott Hughesdcc24742011-09-07 14:02:44 -070070 started_(false),
71 vfprintf_(NULL),
72 exit_(NULL),
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070073 abort_(NULL),
jeffhao2692b572011-12-16 15:42:28 -080074 stats_enabled_(false),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080075 tracer_(NULL),
76 use_compile_time_class_path_(false) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -070077 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
78 resolution_stub_array_[i] = NULL;
79 }
80 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
81 callee_save_method_[i] = NULL;
82 }
Elliott Hughesdcc24742011-09-07 14:02:44 -070083}
84
Carl Shapiro61e019d2011-07-14 16:53:09 -070085Runtime::~Runtime() {
Elliott Hughes6b355752012-01-13 16:49:08 -080086 shutting_down_ = true;
87
jeffhaob5e81852012-03-12 11:15:45 -070088 if (IsMethodTracingActive()) {
89 Trace::Shutdown();
90 }
91
Elliott Hughes5fe594f2011-09-08 12:33:17 -070092 // Make sure our internal threads are dead before we start tearing down things they're using.
Elliott Hughese52e49b2012-04-02 16:05:44 -070093 Dbg::StopJdwp();
Elliott Hughes5fe594f2011-09-08 12:33:17 -070094 delete signal_catcher_;
95
Elliott Hughes038a8062011-09-18 14:12:41 -070096 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
Elliott Hughes93e74e82011-09-13 11:07:03 -070097 delete thread_list_;
Elliott Hughesc33a32b2011-10-11 18:18:07 -070098 delete monitor_list_;
Elliott Hughes93e74e82011-09-13 11:07:03 -070099
Carl Shapiro61e019d2011-07-14 16:53:09 -0700100 delete class_linker_;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800101 delete heap_;
Elliott Hughes4d6850c2012-01-18 15:55:06 -0800102 verifier::DexVerifier::DeleteGcMaps();
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700103 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700104 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700105 Thread::Shutdown();
Carl Shapiro4acf4642011-07-26 18:54:13 -0700106 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700107 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -0700108 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700109}
110
Elliott Hughescac6cc72011-11-03 20:31:21 -0700111static bool gAborting = false;
112
Elliott Hughese0918552011-10-28 17:18:29 -0700113struct AbortState {
114 void Dump(std::ostream& os) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700115 if (gAborting) {
116 os << "Runtime aborting --- recursively, so no thread-specific detail!\n";
117 return;
118 }
119 gAborting = true;
Elliott Hughese0918552011-10-28 17:18:29 -0700120 os << "Runtime aborting...\n";
Elliott Hughes6c7d2442012-02-01 18:40:47 -0800121 if (Runtime::Current() == NULL) {
122 os << "(Runtime does not yet exist!)\n";
123 return;
124 }
Elliott Hughese0918552011-10-28 17:18:29 -0700125 Thread* self = Thread::Current();
126 if (self == NULL) {
127 os << "(Aborting thread was not attached to runtime!)\n";
128 } else {
Elliott Hughes899e7892012-01-24 14:57:32 -0800129 self->Dump(os);
130 if (self->IsExceptionPending()) {
131 os << "Pending " << PrettyTypeOf(self->GetException()) << " on thread:\n"
132 << self->GetException()->Dump();
133 }
Elliott Hughese0918552011-10-28 17:18:29 -0700134 }
135 }
136};
137
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800138static Mutex& GetAbortLock() {
139 static Mutex abort_lock("abort lock");
140 return abort_lock;
141}
142
Elliott Hughesffe67362011-07-17 12:09:27 -0700143void Runtime::Abort(const char* file, int line) {
Elliott Hughes131aef82012-01-27 11:53:35 -0800144 // Ensure that we don't have multiple threads trying to abort at once,
145 // which would result in significantly worse diagnostics.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800146 MutexLock mu(GetAbortLock());
Elliott Hughes131aef82012-01-27 11:53:35 -0800147
Elliott Hughesffe67362011-07-17 12:09:27 -0700148 // Get any pending output out of the way.
149 fflush(NULL);
150
151 // Many people have difficulty distinguish aborts from crashes,
152 // so be explicit.
Elliott Hughese0918552011-10-28 17:18:29 -0700153 AbortState state;
Elliott Hughes9ee5f9c2012-02-03 18:33:16 -0800154 LOG(INTERNAL_FATAL) << Dumpable<AbortState>(state);
Elliott Hughesffe67362011-07-17 12:09:27 -0700155
Elliott Hughesffe67362011-07-17 12:09:27 -0700156 // Perform any platform-specific pre-abort actions.
157 PlatformAbort(file, line);
158
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700159 // use abort hook if we have one
160 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
161 Runtime::Current()->abort_();
162 // notreached
163 }
164
Elliott Hughesffe67362011-07-17 12:09:27 -0700165 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -0700166 // receive SIGABRT. debuggerd dumps the stack trace of the main
167 // thread, whether or not that was the thread that failed. By
168 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -0700169 // fault in the current thread, and get a useful log from debuggerd.
Elliott Hughes81ff3182012-03-23 20:35:56 -0700170 // We can also trivially tell the difference between a crash and
Elliott Hughesffe67362011-07-17 12:09:27 -0700171 // a deliberate abort by looking at the fault address.
172 *reinterpret_cast<char*>(0xdeadd00d) = 38;
173 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -0700174 // notreached
175}
176
Elliott Hughesbf86d042011-08-31 17:53:14 -0700177void Runtime::CallExitHook(jint status) {
178 if (exit_ != NULL) {
Elliott Hughes34e06962012-04-09 13:55:55 -0700179 ScopedThreadStateChange tsc(Thread::Current(), kNative);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700180 exit_(status);
181 LOG(WARNING) << "Exit hook returned instead of exiting!";
182 }
183}
184
Brian Carlstrom8a436592011-08-15 21:27:23 -0700185// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
186// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
187// [gG] gigabytes.
188//
189// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700190// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
191// of 1024.
192//
193// The spec says the -Xmx and -Xms options must be multiples of 1024. It
194// doesn't say anything about -Xss.
195//
196// Returns 0 (a useless size) if "s" is malformed or specifies a low or
197// non-evenly-divisible value.
198//
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800199size_t ParseMemoryOption(const char* s, size_t div) {
Brian Carlstrom8a436592011-08-15 21:27:23 -0700200 // strtoul accepts a leading [+-], which we don't want,
201 // so make sure our string starts with a decimal digit.
202 if (isdigit(*s)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700203 char* s2;
204 size_t val = strtoul(s, &s2, 10);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700205 if (s2 != s) {
206 // s2 should be pointing just after the number.
207 // If this is the end of the string, the user
208 // has specified a number of bytes. Otherwise,
209 // there should be exactly one more character
210 // that specifies a multiplier.
211 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700212 // The remainder of the string is either a single multiplier
213 // character, or nothing to indicate that the value is in
214 // bytes.
215 char c = *s2++;
216 if (*s2 == '\0') {
217 size_t mul;
218 if (c == '\0') {
219 mul = 1;
220 } else if (c == 'k' || c == 'K') {
221 mul = KB;
222 } else if (c == 'm' || c == 'M') {
223 mul = MB;
224 } else if (c == 'g' || c == 'G') {
225 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700226 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700227 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700228 return 0;
229 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700230
231 if (val <= std::numeric_limits<size_t>::max() / mul) {
232 val *= mul;
233 } else {
234 // Clamp to a multiple of 1024.
235 val = std::numeric_limits<size_t>::max() & ~(1024-1);
236 }
237 } else {
238 // There's more than one character after the numeric part.
239 return 0;
240 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700241 }
242 // The man page says that a -Xm value must be a multiple of 1024.
243 if (val % div == 0) {
244 return val;
245 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700246 }
247 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700248 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700249}
250
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800251size_t ParseIntegerOrDie(const std::string& s) {
252 std::string::size_type colon = s.find(':');
253 if (colon == std::string::npos) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700254 LOG(FATAL) << "Missing integer: " << s;
255 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800256 const char* begin = &s[colon + 1];
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700257 char* end;
258 size_t result = strtoul(begin, &end, 10);
259 if (begin == end || *end != '\0') {
260 LOG(FATAL) << "Failed to parse integer in: " << s;
261 }
262 return result;
263}
264
Elliott Hughes0af55432011-08-17 18:37:28 -0700265void LoadJniLibrary(JavaVMExt* vm, const char* name) {
Elliott Hughes6cc332e2012-01-20 22:59:20 -0800266 std::string mapped_name(StringPrintf(OS_SHARED_LIB_FORMAT_STR, name));
Elliott Hughes75770752011-08-24 17:52:38 -0700267 std::string reason;
268 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700269 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
270 << reason;
271 }
272}
273
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700274Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700275 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800276 const char* boot_class_path_string = getenv("BOOTCLASSPATH");
277 if (boot_class_path_string != NULL) {
278 parsed->boot_class_path_string_ = boot_class_path_string;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700279 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800280 const char* class_path_string = getenv("CLASSPATH");
281 if (class_path_string != NULL) {
282 parsed->class_path_string_ = class_path_string;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700283 }
Elliott Hughes67d92002012-03-26 15:08:51 -0700284 // -Xcheck:jni is off by default for regular builds but on by default in debug builds.
285 parsed->check_jni_ = kIsDebugBuild;
Elliott Hughes85d15452011-09-16 17:33:01 -0700286
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700287 parsed->heap_initial_size_ = Heap::kInitialSize;
288 parsed->heap_maximum_size_ = Heap::kMaximumSize;
jeffhaoc1160702011-10-27 15:48:45 -0700289 parsed->heap_growth_limit_ = 0; // 0 means no growth limit
Brian Carlstromf734cf52011-08-17 16:28:14 -0700290 parsed->stack_size_ = Thread::kDefaultStackSize;
291
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800292 parsed->is_compiler_ = false;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700293 parsed->is_zygote_ = false;
294
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700295 parsed->jni_globals_max_ = 0;
Elliott Hughesfc861622011-10-17 17:57:47 -0700296 parsed->lock_profiling_threshold_ = 0;
297 parsed->hook_is_sensitive_thread_ = NULL;
298
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700299 parsed->hook_vfprintf_ = vfprintf;
300 parsed->hook_exit_ = exit;
301 parsed->hook_abort_ = abort;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700302
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800303// gLogVerbosity.class_linker = true; // TODO: don't check this in!
304// gLogVerbosity.compiler = true; // TODO: don't check this in!
305// gLogVerbosity.heap = true; // TODO: don't check this in!
306// gLogVerbosity.gc = true; // TODO: don't check this in!
307// gLogVerbosity.jdwp = true; // TODO: don't check this in!
308// gLogVerbosity.jni = true; // TODO: don't check this in!
309// gLogVerbosity.monitor = true; // TODO: don't check this in!
310// gLogVerbosity.startup = true; // TODO: don't check this in!
311// gLogVerbosity.third_party_jni = true; // TODO: don't check this in!
312// gLogVerbosity.threads = true; // TODO: don't check this in!
313
jeffhaob5e81852012-03-12 11:15:45 -0700314 parsed->method_trace_ = false;
315 parsed->method_trace_file_ = "/data/method-trace-file.bin";
316 parsed->method_trace_file_size_ = 10 * MB;
317
Brian Carlstrom8a436592011-08-15 21:27:23 -0700318 for (size_t i = 0; i < options.size(); ++i) {
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800319 const std::string option(options[i].first);
Brian Carlstrom0796af02011-10-12 14:31:45 -0700320 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700321 LOG(INFO) << "option[" << i << "]=" << option;
322 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800323 if (StartsWith(option, "-Xbootclasspath:")) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800324 parsed->boot_class_path_string_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700325 } else if (option == "-classpath" || option == "-cp") {
326 // TODO: support -Djava.class.path
327 i++;
328 if (i == options.size()) {
329 // TODO: usage
330 LOG(FATAL) << "Missing required class path value for " << option;
331 return NULL;
332 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800333 const StringPiece& value = options[i].first;
334 parsed->class_path_string_ = value.data();
335 } else if (option == "bootclasspath") {
336 parsed->boot_class_path_
337 = reinterpret_cast<const std::vector<const DexFile*>*>(options[i].second);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800338 } else if (StartsWith(option, "-Ximage:")) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800339 parsed->image_ = option.substr(strlen("-Ximage:")).data();
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800340 } else if (StartsWith(option, "-Xcheck:jni")) {
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700341 parsed->check_jni_ = true;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800342 } else if (StartsWith(option, "-Xrunjdwp:") || StartsWith(option, "-agentlib:jdwp=")) {
343 std::string tail(option.substr(option[1] == 'X' ? 10 : 15));
Elliott Hughes3bb81562011-10-21 18:52:59 -0700344 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
345 LOG(FATAL) << "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
346 << "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n";
347 return NULL;
348 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800349 } else if (StartsWith(option, "-Xms")) {
350 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).c_str(), 1024);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700351 if (size == 0) {
352 if (ignore_unrecognized) {
353 continue;
354 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700355 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700356 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700357 return NULL;
358 }
359 parsed->heap_initial_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800360 } else if (StartsWith(option, "-Xmx")) {
361 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).c_str(), 1024);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700362 if (size == 0) {
363 if (ignore_unrecognized) {
364 continue;
365 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700366 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700367 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700368 return NULL;
369 }
370 parsed->heap_maximum_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800371 } else if (StartsWith(option, "-XX:HeapGrowthLimit=")) {
372 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).c_str(), 1024);
jeffhaoc1160702011-10-27 15:48:45 -0700373 if (size == 0) {
374 if (ignore_unrecognized) {
375 continue;
376 }
377 // TODO: usage
378 LOG(FATAL) << "Failed to parse " << option;
379 return NULL;
380 }
381 parsed->heap_growth_limit_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800382 } else if (StartsWith(option, "-Xss")) {
383 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).c_str(), 1);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700384 if (size == 0) {
385 if (ignore_unrecognized) {
386 continue;
387 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700388 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700389 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700390 return NULL;
391 }
392 parsed->stack_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800393 } else if (StartsWith(option, "-D")) {
394 parsed->properties_.push_back(option.substr(strlen("-D")));
395 } else if (StartsWith(option, "-Xjnitrace:")) {
396 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:"));
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700397 } else if (option == "compiler") {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800398 parsed->is_compiler_ = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700399 } else if (option == "-Xzygote") {
400 parsed->is_zygote_ = true;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800401 } else if (StartsWith(option, "-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700402 std::vector<std::string> verbose_options;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800403 Split(option.substr(strlen("-verbose:")), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700404 for (size_t i = 0; i < verbose_options.size(); ++i) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800405 if (verbose_options[i] == "class") {
406 gLogVerbosity.class_linker = true;
407 } else if (verbose_options[i] == "compiler") {
408 gLogVerbosity.compiler = true;
409 } else if (verbose_options[i] == "heap") {
410 gLogVerbosity.heap = true;
411 } else if (verbose_options[i] == "gc") {
412 gLogVerbosity.gc = true;
413 } else if (verbose_options[i] == "jdwp") {
414 gLogVerbosity.jdwp = true;
415 } else if (verbose_options[i] == "jni") {
416 gLogVerbosity.jni = true;
417 } else if (verbose_options[i] == "monitor") {
418 gLogVerbosity.monitor = true;
419 } else if (verbose_options[i] == "startup") {
420 gLogVerbosity.startup = true;
421 } else if (verbose_options[i] == "third-party-jni") {
422 gLogVerbosity.third_party_jni = true;
423 } else if (verbose_options[i] == "threads") {
424 gLogVerbosity.threads = true;
TDYa127b92bcab2012-04-08 00:09:51 -0700425 } else if (StartsWith(verbose_options[i], "log-to=")) {
426 std::string log_file_name(verbose_options[i].substr(strlen("log-to=")));
427 std::ofstream* log_file = new std::ofstream(log_file_name.c_str());
428 if (log_file->is_open() && log_file->good()) {
429 gLogVerbosity.SetLoggingStream(log_file);
430 } else {
431 LOG(ERROR) << "Fail to open log file: \"" << log_file_name << "\","
432 << " use default logging stream.";
433 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800434 } else {
435 LOG(WARNING) << "Ignoring unknown -verbose option: " << verbose_options[i];
436 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700437 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800438 } else if (StartsWith(option, "-Xjnigreflimit:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700439 parsed->jni_globals_max_ = ParseIntegerOrDie(option);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800440 } else if (StartsWith(option, "-Xlockprofthreshold:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700441 parsed->lock_profiling_threshold_ = ParseIntegerOrDie(option);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800442 } else if (StartsWith(option, "-Xstacktracefile:")) {
Elliott Hughes67d92002012-03-26 15:08:51 -0700443 if (kIsDebugBuild) {
444 // Ignore the zygote and always show stack traces in debug builds.
445 } else {
446 parsed->stack_trace_file_ = option.substr(strlen("-Xstacktracefile:"));
447 }
Elliott Hughesfc861622011-10-17 17:57:47 -0700448 } else if (option == "sensitiveThread") {
449 parsed->hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(options[i].second);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700450 } else if (option == "vfprintf") {
451 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
452 } else if (option == "exit") {
453 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
454 } else if (option == "abort") {
455 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700456 } else if (option == "host-prefix") {
457 parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second);
Elliott Hughesb3e66df2012-01-12 14:49:18 -0800458 } else if (option == "-Xgenregmap" || option == "-Xgc:precise") {
459 // We silently ignore these for backwards compatibility.
jeffhaob5e81852012-03-12 11:15:45 -0700460 } else if (option == "-Xmethod-trace") {
461 parsed->method_trace_ = true;
462 } else if (StartsWith(option, "-Xmethod-trace-file:")) {
463 parsed->method_trace_file_ = option.substr(strlen("-Xmethod-trace-file:"));
464 } else if (StartsWith(option, "-Xmethod-trace-file-size:")) {
465 parsed->method_trace_file_size_ = ParseIntegerOrDie(option);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700466 } else {
467 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700468 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700469 LOG(ERROR) << "Unrecognized option " << option;
470 // TODO: this should exit, but for now tolerate unknown options
471 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700472 }
473 }
474 }
475
Brian Carlstrom223f20f2012-02-04 23:06:55 -0800476 if (!parsed->is_compiler_ && parsed->image_.empty()) {
477 parsed->image_ += GetAndroidRoot();
478 parsed->image_ += "/framework/boot.art";
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700479 }
jeffhaoc1160702011-10-27 15:48:45 -0700480 if (parsed->heap_growth_limit_ == 0) {
481 parsed->heap_growth_limit_ = parsed->heap_maximum_size_;
482 }
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700483
Elliott Hughescac6cc72011-11-03 20:31:21 -0700484 LOG(INFO) << "Build type: "
Elliott Hughes67d92002012-03-26 15:08:51 -0700485 << (kIsDebugBuild ? "debug" : "optimized")
Elliott Hughescac6cc72011-11-03 20:31:21 -0700486 << "; CheckJNI: " << (parsed->check_jni_ ? "on" : "off");
Elliott Hughes85d15452011-09-16 17:33:01 -0700487
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700488 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700489}
490
491Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700492 // TODO: acquire a static mutex on Runtime to avoid racing.
493 if (Runtime::instance_ != NULL) {
494 return NULL;
495 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700496 instance_ = new Runtime;
497 if (!instance_->Init(options, ignore_unrecognized)) {
498 delete instance_;
499 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700500 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700501 return instance_;
502}
Elliott Hughes0af55432011-08-17 18:37:28 -0700503
Brian Carlstromaded5f72011-10-07 17:15:04 -0700504void CreateSystemClassLoader() {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800505 if (Runtime::Current()->UseCompileTimeClassPath()) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700506 return;
507 }
508
509 Thread* self = Thread::Current();
510
511 // Must be in the kNative state for calling native methods.
Elliott Hughes34e06962012-04-09 13:55:55 -0700512 CHECK_EQ(self->GetState(), kNative);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700513
514 JNIEnv* env = self->GetJniEnv();
Brian Carlstromdf143242011-10-10 18:05:34 -0700515 ScopedLocalRef<jclass> ClassLoader_class(env, env->FindClass("java/lang/ClassLoader"));
516 CHECK(ClassLoader_class.get() != NULL);
517 jmethodID getSystemClassLoader = env->GetStaticMethodID(ClassLoader_class.get(),
518 "getSystemClassLoader",
519 "()Ljava/lang/ClassLoader;");
520 CHECK(getSystemClassLoader != NULL);
521 ScopedLocalRef<jobject> class_loader(env, env->CallStaticObjectMethod(ClassLoader_class.get(),
522 getSystemClassLoader));
523 CHECK(class_loader.get() != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700524
Brian Carlstromdf143242011-10-10 18:05:34 -0700525 Thread::Current()->SetClassLoaderOverride(Decode<ClassLoader*>(env, class_loader.get()));
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500526
527 ScopedLocalRef<jclass> Thread_class(env, env->FindClass("java/lang/Thread"));
528 CHECK(Thread_class.get() != NULL);
529 jfieldID contextClassLoader = env->GetFieldID(Thread_class.get(),
530 "contextClassLoader",
531 "Ljava/lang/ClassLoader;");
532 CHECK(contextClassLoader != NULL);
533 ScopedLocalRef<jobject> self_jobject(env, AddLocalReference<jobject>(env, self->GetPeer()));
534 env->SetObjectField(self_jobject.get(), contextClassLoader, class_loader.get());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700535}
536
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700537void Runtime::Start() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800538 VLOG(startup) << "Runtime::Start entering";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700539
540 CHECK(host_prefix_.empty()) << host_prefix_;
541
Logan Chien0c717dd2012-03-28 18:31:07 +0800542 // Relocate the OatFiles (ELF images)
543 class_linker_->RelocateExecutable();
544
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700545 // Restore main thread state to kNative as expected by native code
Elliott Hughes34e06962012-04-09 13:55:55 -0700546 Thread::Current()->SetState(kNative);
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700547
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700548 started_ = true;
549
Brian Carlstromae826982011-11-09 01:33:42 -0800550 // InitNativeMethods needs to be after started_ so that the classes
551 // it touches will have methods linked to the oat file if necessary.
552 InitNativeMethods();
553
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500554 Thread::FinishStartup();
555
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700556 if (!is_zygote_) {
557 DidForkFromZygote();
558 }
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700559
Elliott Hughes85d15452011-09-16 17:33:01 -0700560 StartDaemonThreads();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700561
Brian Carlstromaded5f72011-10-07 17:15:04 -0700562 CreateSystemClassLoader();
563
Elliott Hughes726079d2011-10-07 18:43:44 -0700564 Thread::Current()->GetJniEnv()->locals.AssertEmpty();
565
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800566 VLOG(startup) << "Runtime::Start exiting";
Elliott Hughes85d15452011-09-16 17:33:01 -0700567}
568
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700569void Runtime::DidForkFromZygote() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700570 is_zygote_ = false;
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700571
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700572 StartSignalCatcher();
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700573
574 // Start the JDWP thread. If the command-line debugger flags specified "suspend=y",
575 // this will pause the runtime, so we probably want this to come last.
576 Dbg::StartJdwp();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700577}
578
579void Runtime::StartSignalCatcher() {
580 if (!is_zygote_) {
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700581 signal_catcher_ = new SignalCatcher(stack_trace_file_);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700582 }
583}
584
Elliott Hughes85d15452011-09-16 17:33:01 -0700585void Runtime::StartDaemonThreads() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800586 VLOG(startup) << "Runtime::StartDaemonThreads entering";
Elliott Hughes85d15452011-09-16 17:33:01 -0700587
Elliott Hughes719b3232011-09-25 17:42:19 -0700588 Thread* self = Thread::Current();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700589
590 // Must be in the kNative state for calling native methods.
Elliott Hughes34e06962012-04-09 13:55:55 -0700591 CHECK_EQ(self->GetState(), kNative);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700592
Elliott Hughes719b3232011-09-25 17:42:19 -0700593 JNIEnv* env = self->GetJniEnv();
Elliott Hughes726079d2011-10-07 18:43:44 -0700594 ScopedLocalRef<jclass> c(env, env->FindClass("java/lang/Daemons"));
595 CHECK(c.get() != NULL);
596 jmethodID mid = env->GetStaticMethodID(c.get(), "start", "()V");
Elliott Hughes719b3232011-09-25 17:42:19 -0700597 CHECK(mid != NULL);
Elliott Hughes726079d2011-10-07 18:43:44 -0700598 env->CallStaticVoidMethod(c.get(), mid);
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800599 CHECK(!env->ExceptionCheck());
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700600
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800601 VLOG(startup) << "Runtime::StartDaemonThreads exiting";
Carl Shapiro61e019d2011-07-14 16:53:09 -0700602}
603
Elliott Hughes6b355752012-01-13 16:49:08 -0800604bool Runtime::IsShuttingDown() const {
605 return shutting_down_;
606}
607
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700608bool Runtime::IsStarted() const {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700609 return started_;
610}
611
Elliott Hughes0af55432011-08-17 18:37:28 -0700612bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700613 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700614
Elliott Hughes90a33692011-08-30 13:27:07 -0700615 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
616 if (options.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700617 LOG(ERROR) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700618 return false;
619 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800620 VLOG(startup) << "Runtime::Init -verbose:startup enabled";
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700621
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700622 SetJniGlobalsMax(options->jni_globals_max_);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800623 Monitor::Init(options->lock_profiling_threshold_, options->hook_is_sensitive_thread_);
Elliott Hughes32d6e1e2011-10-11 14:47:44 -0700624
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700625 host_prefix_ = options->host_prefix_;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800626 boot_class_path_string_ = options->boot_class_path_string_;
627 class_path_string_ = options->class_path_string_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700628 properties_ = options->properties_;
629
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800630 is_compiler_ = options->is_compiler_;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700631 is_zygote_ = options->is_zygote_;
632
Elliott Hughes0af55432011-08-17 18:37:28 -0700633 vfprintf_ = options->hook_vfprintf_;
634 exit_ = options->hook_exit_;
635 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700636
Elliott Hughesbe759c62011-09-08 19:38:21 -0700637 default_stack_size_ = options->stack_size_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700638 stack_trace_file_ = options->stack_trace_file_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700639
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700640 monitor_list_ = new MonitorList;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800641 thread_list_ = new ThreadList;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700642 intern_table_ = new InternTable;
643
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800644 verifier::DexVerifier::InitGcMaps();
645
646 heap_ = new Heap(options->heap_initial_size_,
647 options->heap_growth_limit_,
648 options->heap_maximum_size_,
649 options->image_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700650
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700651 BlockSignals();
652
Elliott Hughesa0957642011-09-02 14:27:33 -0700653 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700654
Elliott Hughesbe759c62011-09-08 19:38:21 -0700655 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700656
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700657 // ClassLinker needs an attached thread, but we can't fully attach a thread
Elliott Hughes462c9442012-03-23 18:47:50 -0700658 // without creating objects. We can't supply a thread group yet; it will be fixed later.
659 Thread::Attach("main", false, NULL);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700660
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700661 // Set us to runnable so tools using a runtime can allocate and GC by default
Elliott Hughes34e06962012-04-09 13:55:55 -0700662 Thread::Current()->SetState(kRunnable);
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700663
Ian Rogers141d6222012-04-05 12:23:06 -0700664 // Now we're attached, we can take the heap lock and validate the heap.
665 GetHeap()->EnableObjectValidation();
666
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800667 CHECK_GE(GetHeap()->GetSpaces().size(), 1U);
668 if (GetHeap()->GetSpaces()[0]->IsImageSpace()) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800669 class_linker_ = ClassLinker::CreateFromImage(intern_table_);
670 } else {
671 CHECK(options->boot_class_path_ != NULL);
672 CHECK_NE(options->boot_class_path_->size(), 0U);
673 class_linker_ = ClassLinker::CreateFromCompiler(*options->boot_class_path_, intern_table_);
674 }
675 CHECK(class_linker_ != NULL);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700676
jeffhaob5e81852012-03-12 11:15:45 -0700677 method_trace_ = options->method_trace_;
678 method_trace_file_ = options->method_trace_file_;
679 method_trace_file_size_ = options->method_trace_file_size_;
680
681 if (options->method_trace_) {
682 Trace::Start(options->method_trace_file_.c_str(), -1, options->method_trace_file_size_, 0, false);
683 }
684
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800685 VLOG(startup) << "Runtime::Init exiting";
Carl Shapiro1fb86202011-06-27 17:43:13 -0700686 return true;
687}
688
Elliott Hughes038a8062011-09-18 14:12:41 -0700689void Runtime::InitNativeMethods() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800690 VLOG(startup) << "Runtime::InitNativeMethods entering";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700691 Thread* self = Thread::Current();
692 JNIEnv* env = self->GetJniEnv();
693
Elliott Hughes418d20f2011-09-22 14:00:39 -0700694 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Elliott Hughes34e06962012-04-09 13:55:55 -0700695 CHECK_EQ(self->GetState(), kNative);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700696
Elliott Hughes418d20f2011-09-22 14:00:39 -0700697 // First set up JniConstants, which is used by both the runtime's built-in native
698 // methods and libcore.
Elliott Hughesfea966e2011-09-22 10:26:20 -0700699 JniConstants::init(env);
700
Elliott Hughes418d20f2011-09-22 14:00:39 -0700701 // Then set up the native methods provided by the runtime itself.
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700702 RegisterRuntimeNativeMethods(env);
703
Elliott Hughes418d20f2011-09-22 14:00:39 -0700704 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
705 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
706 // the library that implements System.loadLibrary!
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700707 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800708 VLOG(startup) << "Runtime::InitNativeMethods exiting";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700709}
710
711void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
712#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Ian Rogers5167c972012-02-03 10:41:20 -0800713 // Register Throwable first so that registration of other native methods can throw exceptions
714 REGISTER(register_java_lang_Throwable);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700715 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700716 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700717 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700718 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700719 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700720 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700721 REGISTER(register_java_lang_Object);
722 REGISTER(register_java_lang_Runtime);
723 REGISTER(register_java_lang_String);
724 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700725 REGISTER(register_java_lang_Thread);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700726 REGISTER(register_java_lang_VMClassLoader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700727 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700728 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700729 REGISTER(register_java_lang_reflect_Field);
730 REGISTER(register_java_lang_reflect_Method);
Jesse Wilson95caa792011-10-12 18:14:17 -0400731 REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700732 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700733 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700734 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700735 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700736#undef REGISTER
737}
738
Elliott Hughes8daa0922011-09-11 13:46:25 -0700739void Runtime::Dump(std::ostream& os) {
Elliott Hughese27955c2011-08-26 15:21:24 -0700740 // TODO: dump other runtime statistics?
Elliott Hughescac6cc72011-11-03 20:31:21 -0700741 GetClassLinker()->DumpForSigQuit(os);
742 GetInternTable()->DumpForSigQuit(os);
Elliott Hughes42ee1422011-09-06 12:33:32 -0700743 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700744
745 thread_list_->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700746}
747
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800748void Runtime::DumpLockHolders(std::ostream& os) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800749 pid_t heap_lock_owner = GetHeap()->GetLockOwner();
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800750 pid_t thread_list_lock_owner = GetThreadList()->GetLockOwner();
751 pid_t classes_lock_owner = GetClassLinker()->GetClassesLockOwner();
752 pid_t dex_lock_owner = GetClassLinker()->GetDexLockOwner();
753 if ((heap_lock_owner | thread_list_lock_owner | classes_lock_owner | dex_lock_owner) != 0) {
754 os << "Heap lock owner tid: " << heap_lock_owner << "\n"
755 << "ThreadList lock owner tid: " << thread_list_lock_owner << "\n"
756 << "ClassLinker classes lock owner tid: " << classes_lock_owner << "\n"
757 << "ClassLinker dex lock owner tid: " << dex_lock_owner << "\n";
758 }
759}
760
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700761void Runtime::SetStatsEnabled(bool new_state) {
762 if (new_state == true) {
763 GetStats()->Clear(~0);
764 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
765 Thread::Current()->GetStats()->Clear(~0);
766 }
767 stats_enabled_ = new_state;
768}
769
770void Runtime::ResetStats(int kinds) {
771 GetStats()->Clear(kinds & 0xffff);
772 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
773 Thread::Current()->GetStats()->Clear(kinds >> 16);
774}
775
776RuntimeStats* Runtime::GetStats() {
777 return &stats_;
778}
779
780int32_t Runtime::GetStat(int kind) {
781 RuntimeStats* stats;
782 if (kind < (1<<16)) {
783 stats = GetStats();
784 } else {
785 stats = Thread::Current()->GetStats();
786 kind >>= 16;
787 }
788 switch (kind) {
789 case KIND_ALLOCATED_OBJECTS:
790 return stats->allocated_objects;
791 case KIND_ALLOCATED_BYTES:
792 return stats->allocated_bytes;
793 case KIND_FREED_OBJECTS:
794 return stats->freed_objects;
795 case KIND_FREED_BYTES:
796 return stats->freed_bytes;
797 case KIND_GC_INVOCATIONS:
798 return stats->gc_for_alloc_count;
799 case KIND_CLASS_INIT_COUNT:
800 return stats->class_init_count;
801 case KIND_CLASS_INIT_TIME:
802 // Convert ns to us, reduce to 32 bits.
Elliott Hughes398f64b2012-03-26 18:05:48 -0700803 return static_cast<int>(stats->class_init_time_ns / 1000);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700804 case KIND_EXT_ALLOCATED_OBJECTS:
805 case KIND_EXT_ALLOCATED_BYTES:
806 case KIND_EXT_FREED_OBJECTS:
807 case KIND_EXT_FREED_BYTES:
808 return 0; // backward compatibility
809 default:
810 CHECK(false);
811 return -1; // unreachable
812 }
813}
814
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700815void Runtime::BlockSignals() {
816 sigset_t sigset;
817 if (sigemptyset(&sigset) == -1) {
818 PLOG(FATAL) << "sigemptyset failed";
819 }
820 if (sigaddset(&sigset, SIGPIPE) == -1) {
821 PLOG(ERROR) << "sigaddset SIGPIPE failed";
822 }
823 // SIGQUIT is used to dump the runtime's state (including stack traces).
824 if (sigaddset(&sigset, SIGQUIT) == -1) {
825 PLOG(ERROR) << "sigaddset SIGQUIT failed";
826 }
Elliott Hughes08795042012-04-03 14:48:52 -0700827 // SIGUSR1 is used to initiate a GC.
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700828 if (sigaddset(&sigset, SIGUSR1) == -1) {
829 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
830 }
831 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
832}
833
Elliott Hughes462c9442012-03-23 18:47:50 -0700834void Runtime::AttachCurrentThread(const char* thread_name, bool as_daemon, Object* thread_group) {
835 Thread::Attach(thread_name, as_daemon, thread_group);
Elliott Hughes22869a92012-03-27 14:08:24 -0700836 if (thread_name == NULL) {
837 LOG(WARNING) << *Thread::Current() << " attached without supplying a name";
838 }
Carl Shapiro61e019d2011-07-14 16:53:09 -0700839}
840
Elliott Hughesd92bec42011-09-02 17:04:36 -0700841void Runtime::DetachCurrentThread() {
Elliott Hughes22869a92012-03-27 14:08:24 -0700842 if (Thread::Current()->GetTopOfStack().GetSP() != NULL) {
843 LOG(FATAL) << *Thread::Current() << " attempting to detach while still running code";
844 }
Elliott Hughes02b48d12011-09-07 17:15:51 -0700845 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700846}
847
Brian Carlstrome24fa612011-09-29 00:53:55 -0700848void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700849 Dbg::VisitRoots(visitor, arg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700850 class_linker_->VisitRoots(visitor, arg);
851 intern_table_->VisitRoots(visitor, arg);
852 java_vm_->VisitRoots(visitor, arg);
853 thread_list_->VisitRoots(visitor, arg);
854 visitor(jni_stub_array_, arg);
855 visitor(abstract_method_error_stub_array_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700856 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
857 visitor(resolution_stub_array_[i], arg);
858 }
Ian Rogers19846512012-02-24 11:42:47 -0800859 visitor(resolution_method_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700860 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
861 visitor(callee_save_method_[i], arg);
862 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700863}
864
Ian Rogers169c9a72011-11-13 20:13:17 -0800865bool Runtime::HasJniDlsymLookupStub() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700866 return jni_stub_array_ != NULL;
867}
868
Ian Rogers169c9a72011-11-13 20:13:17 -0800869ByteArray* Runtime::GetJniDlsymLookupStub() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700870 CHECK(jni_stub_array_ != NULL);
871 return jni_stub_array_;
872}
873
Ian Rogers169c9a72011-11-13 20:13:17 -0800874void Runtime::SetJniDlsymLookupStub(ByteArray* jni_stub_array) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700875 CHECK(jni_stub_array != NULL) << " jni_stub_array=" << jni_stub_array;
876 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array)
877 << "jni_stub_array_=" << jni_stub_array_ << " jni_stub_array=" << jni_stub_array;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700878 jni_stub_array_ = jni_stub_array;
879}
880
881bool Runtime::HasAbstractMethodErrorStubArray() const {
882 return abstract_method_error_stub_array_ != NULL;
883}
884
885ByteArray* Runtime::GetAbstractMethodErrorStubArray() const {
886 CHECK(abstract_method_error_stub_array_ != NULL);
887 return abstract_method_error_stub_array_;
888}
889
890void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) {
891 CHECK(abstract_method_error_stub_array != NULL);
892 CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array);
893 abstract_method_error_stub_array_ = abstract_method_error_stub_array;
894}
895
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700896
897Runtime::TrampolineType Runtime::GetTrampolineType(Method* method) {
898 if (method == NULL) {
899 return Runtime::kUnknownMethod;
900 } else if (method->IsStatic()) {
901 return Runtime::kStaticMethod;
902 } else {
Ian Rogersfb6adba2012-03-04 21:51:51 -0800903 return Runtime::kUnknownMethod;
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700904 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700905}
906
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700907bool Runtime::HasResolutionStubArray(TrampolineType type) const {
908 return resolution_stub_array_[type] != NULL;
Ian Rogersad25ac52011-10-04 19:13:33 -0700909}
910
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700911ByteArray* Runtime::GetResolutionStubArray(TrampolineType type) const {
912 CHECK(HasResolutionStubArray(type));
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700913 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastTrampolineMethodType));
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700914 return resolution_stub_array_[type];
915}
916
917void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700918 CHECK(resolution_stub_array != NULL);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700919 CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array);
920 resolution_stub_array_[type] = resolution_stub_array;
Ian Rogersad25ac52011-10-04 19:13:33 -0700921}
922
Ian Rogers19846512012-02-24 11:42:47 -0800923Method* Runtime::CreateResolutionMethod() {
924 Class* method_class = Method::GetMethodClass();
925 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
926 method->SetDeclaringClass(method_class);
927 // TODO: use a special method for resolution method saves
928 method->SetDexMethodIndex(DexFile::kDexNoIndex16);
929 ByteArray* unknown_resolution_stub = GetResolutionStubArray(kUnknownMethod);
930 CHECK(unknown_resolution_stub != NULL);
931 method->SetCode(unknown_resolution_stub->GetData());
932 return method.get();
933}
934
935bool Runtime::HasResolutionMethod() const {
936 return resolution_method_ != NULL;
937}
938
939// Returns a special method that calls into a trampoline for runtime method resolution
940Method* Runtime::GetResolutionMethod() const {
941 CHECK(HasResolutionMethod());
942 return resolution_method_;
943}
944
945void Runtime::SetResolutionMethod(Method* method) {
946 resolution_method_ = method;
947}
948
Brian Carlstromae826982011-11-09 01:33:42 -0800949Method* Runtime::CreateCalleeSaveMethod(InstructionSet instruction_set, CalleeSaveType type) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700950 Class* method_class = Method::GetMethodClass();
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700951 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
Ian Rogersff1ed472011-09-20 13:46:24 -0700952 method->SetDeclaringClass(method_class);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800953 // TODO: use a special method for callee saves
Ian Rogers19846512012-02-24 11:42:47 -0800954 method->SetDexMethodIndex(DexFile::kDexNoIndex16);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700955 method->SetCode(NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800956 if ((instruction_set == kThumb2) || (instruction_set == kArm)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700957 uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6) | (1 << art::arm::R7) |
958 (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11);
959 uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3);
960 uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9);
961 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills :0) |
962 (type == kSaveAll ? all_spills :0) | (1 << art::arm::LR);
963 uint32_t fp_all_spills = (1 << art::arm::S0) | (1 << art::arm::S1) | (1 << art::arm::S2) |
964 (1 << art::arm::S3) | (1 << art::arm::S4) | (1 << art::arm::S5) |
965 (1 << art::arm::S6) | (1 << art::arm::S7) | (1 << art::arm::S8) |
966 (1 << art::arm::S9) | (1 << art::arm::S10) | (1 << art::arm::S11) |
967 (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) |
968 (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) |
969 (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) |
970 (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) |
971 (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) |
972 (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
973 (1 << art::arm::S30) | (1 << art::arm::S31);
974 uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
975 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
976 __builtin_popcount(fp_spills) /* fprs */ +
977 1 /* Method* */) * kPointerSize, kStackAlignment);
Ian Rogers15fdb8c2011-09-25 15:45:07 -0700978 method->SetFrameSizeInBytes(frame_size);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700979 method->SetCoreSpillMask(core_spills);
980 method->SetFpSpillMask(fp_spills);
Brian Carlstromae826982011-11-09 01:33:42 -0800981 } else if (instruction_set == kX86) {
Ian Rogers7caad772012-03-30 01:07:54 -0700982 uint32_t ref_spills = (1 << art::x86::EBP) | (1 << art::x86::ESI) | (1 << art::x86::EDI);
983 uint32_t arg_spills = (1 << art::x86::ECX) | (1 << art::x86::EDX) | (1 << art::x86::EBX);
984 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills : 0) |
985 (1 << art::x86::kNumberOfCpuRegisters); // fake return address callee save
986 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
987 1 /* Method* */) * kPointerSize, kStackAlignment);
988 method->SetFrameSizeInBytes(frame_size);
989 method->SetCoreSpillMask(core_spills);
Ian Rogersff1ed472011-09-20 13:46:24 -0700990 method->SetFpSpillMask(0);
991 } else {
992 UNIMPLEMENTED(FATAL);
993 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700994 return method.get();
Ian Rogersff1ed472011-09-20 13:46:24 -0700995}
996
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700997bool Runtime::HasCalleeSaveMethod(CalleeSaveType type) const {
998 return callee_save_method_[type] != NULL;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700999}
1000
Brian Carlstrome24fa612011-09-29 00:53:55 -07001001// Returns a special method that describes all callee saves being spilled to the stack.
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001002Method* Runtime::GetCalleeSaveMethod(CalleeSaveType type) const {
1003 CHECK(HasCalleeSaveMethod(type));
1004 return callee_save_method_[type];
Brian Carlstrome24fa612011-09-29 00:53:55 -07001005}
1006
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001007void Runtime::SetCalleeSaveMethod(Method* method, CalleeSaveType type) {
1008 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
1009 callee_save_method_[type] = method;
Brian Carlstrome24fa612011-09-29 00:53:55 -07001010}
1011
jeffhao2692b572011-12-16 15:42:28 -08001012void Runtime::EnableMethodTracing(Trace* tracer) {
1013 CHECK(!IsMethodTracingActive());
1014 tracer_ = tracer;
1015}
1016
1017void Runtime::DisableMethodTracing() {
1018 CHECK(IsMethodTracingActive());
1019 delete tracer_;
1020 tracer_ = NULL;
1021}
1022
1023bool Runtime::IsMethodTracingActive() const {
1024 return (tracer_ != NULL);
1025}
1026
1027Trace* Runtime::GetTracer() const {
1028 CHECK(IsMethodTracingActive());
1029 return tracer_;
1030}
1031
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001032const std::vector<const DexFile*>& Runtime::GetCompileTimeClassPath(const ClassLoader* class_loader) {
1033 if (class_loader == NULL) {
1034 return GetClassLinker()->GetBootClassPath();
1035 }
1036 CHECK(UseCompileTimeClassPath());
1037 CompileTimeClassPaths::const_iterator it = compile_time_class_paths_.find(class_loader);
1038 CHECK(it != compile_time_class_paths_.end());
1039 return it->second;
1040}
1041
1042void Runtime::SetCompileTimeClassPath(const ClassLoader* class_loader, std::vector<const DexFile*>& class_path) {
1043 CHECK(!IsStarted());
1044 use_compile_time_class_path_ = true;
1045 compile_time_class_paths_[class_loader] = class_path;
1046}
1047
Carl Shapiro1fb86202011-06-27 17:43:13 -07001048} // namespace art