blob: 60e875bbe2c69ea3c0e416ccd50c2dfec3122a21 [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 Hughes3bb81562011-10-21 18:52:59 -070028#include "debugger.h"
Elliott Hughes4d6850c2012-01-18 15:55:06 -080029#include "dex_verifier.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070030#include "heap.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070031#include "image.h"
Elliott Hughescf4c6c42011-09-01 15:16:42 -070032#include "intern_table.h"
Elliott Hughesc5f7c912011-08-18 14:00:42 -070033#include "jni_internal.h"
Elliott Hughes32d6e1e2011-10-11 14:47:44 -070034#include "monitor.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070035#include "oat_file.h"
Elliott Hughes726079d2011-10-07 18:43:44 -070036#include "ScopedLocalRef.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070037#include "signal_catcher.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070038#include "space.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070039#include "thread.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070040#include "thread_list.h"
Elliott Hughes3bb81562011-10-21 18:52:59 -070041#include "UniquePtr.h"
Carl Shapiro61e019d2011-07-14 16:53:09 -070042
Elliott Hughes90a33692011-08-30 13:27:07 -070043// TODO: this drags in cutil/log.h, which conflicts with our logging.h.
44#include "JniConstants.h"
45
Carl Shapiro1fb86202011-06-27 17:43:13 -070046namespace art {
47
Carl Shapiro2ed144c2011-07-26 16:52:08 -070048Runtime* Runtime::instance_ = NULL;
49
Elliott Hughes131aef82012-01-27 11:53:35 -080050Mutex Runtime::abort_lock_("abort lock");
51
Elliott Hughesdcc24742011-09-07 14:02:44 -070052Runtime::Runtime()
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080053 : is_zygote_(false),
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -070054 default_stack_size_(Thread::kDefaultStackSize),
Elliott Hughesc33a32b2011-10-11 18:18:07 -070055 monitor_list_(NULL),
Elliott Hughesdcc24742011-09-07 14:02:44 -070056 thread_list_(NULL),
57 intern_table_(NULL),
58 class_linker_(NULL),
59 signal_catcher_(NULL),
60 java_vm_(NULL),
Brian Carlstrom16192862011-09-12 17:50:06 -070061 jni_stub_array_(NULL),
Brian Carlstrome24fa612011-09-29 00:53:55 -070062 abstract_method_error_stub_array_(NULL),
Elliott Hughes6b355752012-01-13 16:49:08 -080063 shutting_down_(false),
Elliott Hughesdcc24742011-09-07 14:02:44 -070064 started_(false),
65 vfprintf_(NULL),
66 exit_(NULL),
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070067 abort_(NULL),
jeffhao2692b572011-12-16 15:42:28 -080068 stats_enabled_(false),
69 tracer_(NULL) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -070070 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
71 resolution_stub_array_[i] = NULL;
72 }
73 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
74 callee_save_method_[i] = NULL;
75 }
Elliott Hughesdcc24742011-09-07 14:02:44 -070076}
77
Carl Shapiro61e019d2011-07-14 16:53:09 -070078Runtime::~Runtime() {
Elliott Hughes6b355752012-01-13 16:49:08 -080079 shutting_down_ = true;
80
Elliott Hughesd1cc8362011-10-24 16:58:50 -070081 Dbg::StopJdwp();
82
Elliott Hughes5fe594f2011-09-08 12:33:17 -070083 // Make sure our internal threads are dead before we start tearing down things they're using.
84 delete signal_catcher_;
Elliott Hughes038a8062011-09-18 14:12:41 -070085 // TODO: GC thread.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070086
Elliott Hughes038a8062011-09-18 14:12:41 -070087 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
Elliott Hughes93e74e82011-09-13 11:07:03 -070088 delete thread_list_;
Elliott Hughesc33a32b2011-10-11 18:18:07 -070089 delete monitor_list_;
Elliott Hughes93e74e82011-09-13 11:07:03 -070090
Carl Shapiro61e019d2011-07-14 16:53:09 -070091 delete class_linker_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070092 Heap::Destroy();
Elliott Hughes4d6850c2012-01-18 15:55:06 -080093 verifier::DexVerifier::DeleteGcMaps();
Elliott Hughescf4c6c42011-09-01 15:16:42 -070094 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070095 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -070096 Thread::Shutdown();
Carl Shapiro4acf4642011-07-26 18:54:13 -070097 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070098 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -070099 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700100}
101
Elliott Hughescac6cc72011-11-03 20:31:21 -0700102static bool gAborting = false;
103
Elliott Hughese0918552011-10-28 17:18:29 -0700104struct AbortState {
105 void Dump(std::ostream& os) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700106 if (gAborting) {
107 os << "Runtime aborting --- recursively, so no thread-specific detail!\n";
108 return;
109 }
110 gAborting = true;
Elliott Hughese0918552011-10-28 17:18:29 -0700111 os << "Runtime aborting...\n";
Elliott Hughes6c7d2442012-02-01 18:40:47 -0800112 if (Runtime::Current() == NULL) {
113 os << "(Runtime does not yet exist!)\n";
114 return;
115 }
Elliott Hughese0918552011-10-28 17:18:29 -0700116 Thread* self = Thread::Current();
117 if (self == NULL) {
118 os << "(Aborting thread was not attached to runtime!)\n";
119 } else {
Elliott Hughes899e7892012-01-24 14:57:32 -0800120 self->Dump(os);
121 if (self->IsExceptionPending()) {
122 os << "Pending " << PrettyTypeOf(self->GetException()) << " on thread:\n"
123 << self->GetException()->Dump();
124 }
Elliott Hughese0918552011-10-28 17:18:29 -0700125 }
126 }
127};
128
Elliott Hughesffe67362011-07-17 12:09:27 -0700129void Runtime::Abort(const char* file, int line) {
Elliott Hughes131aef82012-01-27 11:53:35 -0800130 // Ensure that we don't have multiple threads trying to abort at once,
131 // which would result in significantly worse diagnostics.
132 MutexLock mu(abort_lock_);
133
Elliott Hughesffe67362011-07-17 12:09:27 -0700134 // Get any pending output out of the way.
135 fflush(NULL);
136
137 // Many people have difficulty distinguish aborts from crashes,
138 // so be explicit.
Elliott Hughese0918552011-10-28 17:18:29 -0700139 AbortState state;
140 LOG(ERROR) << Dumpable<AbortState>(state);
Elliott Hughesffe67362011-07-17 12:09:27 -0700141
Elliott Hughesffe67362011-07-17 12:09:27 -0700142 // Perform any platform-specific pre-abort actions.
143 PlatformAbort(file, line);
144
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700145 // use abort hook if we have one
146 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
147 Runtime::Current()->abort_();
148 // notreached
149 }
150
Elliott Hughesffe67362011-07-17 12:09:27 -0700151 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -0700152 // receive SIGABRT. debuggerd dumps the stack trace of the main
153 // thread, whether or not that was the thread that failed. By
154 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -0700155 // fault in the current thread, and get a useful log from debuggerd.
156 // We can also trivially tell the difference between a VM crash and
157 // a deliberate abort by looking at the fault address.
158 *reinterpret_cast<char*>(0xdeadd00d) = 38;
159 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -0700160 // notreached
161}
162
Elliott Hughesbf86d042011-08-31 17:53:14 -0700163void Runtime::CallExitHook(jint status) {
164 if (exit_ != NULL) {
165 ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative);
166 exit_(status);
167 LOG(WARNING) << "Exit hook returned instead of exiting!";
168 }
169}
170
Brian Carlstrom8a436592011-08-15 21:27:23 -0700171// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
172// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
173// [gG] gigabytes.
174//
175// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700176// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
177// of 1024.
178//
179// The spec says the -Xmx and -Xms options must be multiples of 1024. It
180// doesn't say anything about -Xss.
181//
182// Returns 0 (a useless size) if "s" is malformed or specifies a low or
183// non-evenly-divisible value.
184//
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800185size_t ParseMemoryOption(const char* s, size_t div) {
Brian Carlstrom8a436592011-08-15 21:27:23 -0700186 // strtoul accepts a leading [+-], which we don't want,
187 // so make sure our string starts with a decimal digit.
188 if (isdigit(*s)) {
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800189 const char* s2;
190 size_t val = strtoul(s, (char**)&s2, 10);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700191 if (s2 != s) {
192 // s2 should be pointing just after the number.
193 // If this is the end of the string, the user
194 // has specified a number of bytes. Otherwise,
195 // there should be exactly one more character
196 // that specifies a multiplier.
197 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700198 // The remainder of the string is either a single multiplier
199 // character, or nothing to indicate that the value is in
200 // bytes.
201 char c = *s2++;
202 if (*s2 == '\0') {
203 size_t mul;
204 if (c == '\0') {
205 mul = 1;
206 } else if (c == 'k' || c == 'K') {
207 mul = KB;
208 } else if (c == 'm' || c == 'M') {
209 mul = MB;
210 } else if (c == 'g' || c == 'G') {
211 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700212 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700213 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700214 return 0;
215 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700216
217 if (val <= std::numeric_limits<size_t>::max() / mul) {
218 val *= mul;
219 } else {
220 // Clamp to a multiple of 1024.
221 val = std::numeric_limits<size_t>::max() & ~(1024-1);
222 }
223 } else {
224 // There's more than one character after the numeric part.
225 return 0;
226 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700227 }
228 // The man page says that a -Xm value must be a multiple of 1024.
229 if (val % div == 0) {
230 return val;
231 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700232 }
233 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700234 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700235}
236
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700237size_t ParseIntegerOrDie(const StringPiece& s) {
238 StringPiece::size_type colon = s.find(':');
239 if (colon == StringPiece::npos) {
240 LOG(FATAL) << "Missing integer: " << s;
241 }
242 const char* begin = &s.data()[colon + 1];
243 char* end;
244 size_t result = strtoul(begin, &end, 10);
245 if (begin == end || *end != '\0') {
246 LOG(FATAL) << "Failed to parse integer in: " << s;
247 }
248 return result;
249}
250
Elliott Hughes0af55432011-08-17 18:37:28 -0700251void LoadJniLibrary(JavaVMExt* vm, const char* name) {
Elliott Hughes6cc332e2012-01-20 22:59:20 -0800252 std::string mapped_name(StringPrintf(OS_SHARED_LIB_FORMAT_STR, name));
Elliott Hughes75770752011-08-24 17:52:38 -0700253 std::string reason;
254 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700255 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
256 << reason;
257 }
258}
259
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700260Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700261 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700262 bool compiler = false;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700263 const char* boot_class_path = getenv("BOOTCLASSPATH");
264 if (boot_class_path != NULL) {
Ian Rogers4f6b57a2012-01-17 16:23:27 -0800265 parsed->boot_class_path_ = boot_class_path;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700266 }
267 const char* class_path = getenv("CLASSPATH");
268 if (class_path != NULL) {
Ian Rogers4f6b57a2012-01-17 16:23:27 -0800269 parsed->class_path_ = class_path;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700270 }
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700271#ifdef NDEBUG
Elliott Hughes5174fe62011-08-23 15:12:35 -0700272 // -Xcheck:jni is off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700273 parsed->check_jni_ = false;
274#else
275 // ...but on by default in debug builds.
276 parsed->check_jni_ = true;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700277#endif
Elliott Hughes85d15452011-09-16 17:33:01 -0700278
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700279 parsed->heap_initial_size_ = Heap::kInitialSize;
280 parsed->heap_maximum_size_ = Heap::kMaximumSize;
jeffhaoc1160702011-10-27 15:48:45 -0700281 parsed->heap_growth_limit_ = 0; // 0 means no growth limit
Brian Carlstromf734cf52011-08-17 16:28:14 -0700282 parsed->stack_size_ = Thread::kDefaultStackSize;
283
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700284 parsed->is_zygote_ = false;
285
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700286 parsed->jni_globals_max_ = 0;
Elliott Hughesfc861622011-10-17 17:57:47 -0700287 parsed->lock_profiling_threshold_ = 0;
288 parsed->hook_is_sensitive_thread_ = NULL;
289
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700290 parsed->hook_vfprintf_ = vfprintf;
291 parsed->hook_exit_ = exit;
292 parsed->hook_abort_ = abort;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700293
294 for (size_t i = 0; i < options.size(); ++i) {
295 const StringPiece& option = options[i].first;
Brian Carlstrom0796af02011-10-12 14:31:45 -0700296 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700297 LOG(INFO) << "option[" << i << "]=" << option;
298 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700299 if (option.starts_with("-Xbootclasspath:")) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700300 parsed->boot_class_path_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700301 } else if (option == "-classpath" || option == "-cp") {
302 // TODO: support -Djava.class.path
303 i++;
304 if (i == options.size()) {
305 // TODO: usage
306 LOG(FATAL) << "Missing required class path value for " << option;
307 return NULL;
308 }
309 const StringPiece& value = options[i].first;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700310 parsed->class_path_ = value.data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700311 } else if (option.starts_with("-Ximage:")) {
312 parsed->images_.push_back(option.substr(strlen("-Ximage:")).data());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700313 } else if (option.starts_with("-Xcheck:jni")) {
314 parsed->check_jni_ = true;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700315 } else if (option.starts_with("-Xrunjdwp:") || option.starts_with("-agentlib:jdwp=")) {
Elliott Hughes95572412011-12-13 18:14:20 -0800316 std::string tail(option.substr(option[1] == 'X' ? 10 : 15).ToString());
Elliott Hughes3bb81562011-10-21 18:52:59 -0700317 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
318 LOG(FATAL) << "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
319 << "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n";
320 return NULL;
321 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700322 } else if (option.starts_with("-Xms")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700323 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024);
324 if (size == 0) {
325 if (ignore_unrecognized) {
326 continue;
327 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700328 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700329 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700330 return NULL;
331 }
332 parsed->heap_initial_size_ = size;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700333 } else if (option.starts_with("-Xmx")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700334 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024);
335 if (size == 0) {
336 if (ignore_unrecognized) {
337 continue;
338 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700339 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700340 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700341 return NULL;
342 }
343 parsed->heap_maximum_size_ = size;
jeffhaoc1160702011-10-27 15:48:45 -0700344 } else if (option.starts_with("-XX:HeapGrowthLimit=")) {
345 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).data(), 1024);
346 if (size == 0) {
347 if (ignore_unrecognized) {
348 continue;
349 }
350 // TODO: usage
351 LOG(FATAL) << "Failed to parse " << option;
352 return NULL;
353 }
354 parsed->heap_growth_limit_ = size;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700355 } else if (option.starts_with("-Xss")) {
356 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1);
357 if (size == 0) {
358 if (ignore_unrecognized) {
359 continue;
360 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700361 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700362 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700363 return NULL;
364 }
365 parsed->stack_size_ = size;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700366 } else if (option.starts_with("-D")) {
367 parsed->properties_.push_back(option.substr(strlen("-D")).data());
Elliott Hughesa0957642011-09-02 14:27:33 -0700368 } else if (option.starts_with("-Xjnitrace:")) {
369 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:")).data();
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700370 } else if (option == "compiler") {
371 compiler = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700372 } else if (option == "-Xzygote") {
373 parsed->is_zygote_ = true;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700374 } else if (option.starts_with("-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700375 std::vector<std::string> verbose_options;
Elliott Hughes34023802011-08-30 12:06:17 -0700376 Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700377 for (size_t i = 0; i < verbose_options.size(); ++i) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800378 if (verbose_options[i] == "class") {
379 gLogVerbosity.class_linker = true;
380 } else if (verbose_options[i] == "compiler") {
381 gLogVerbosity.compiler = true;
382 } else if (verbose_options[i] == "heap") {
383 gLogVerbosity.heap = true;
384 } else if (verbose_options[i] == "gc") {
385 gLogVerbosity.gc = true;
386 } else if (verbose_options[i] == "jdwp") {
387 gLogVerbosity.jdwp = true;
388 } else if (verbose_options[i] == "jni") {
389 gLogVerbosity.jni = true;
390 } else if (verbose_options[i] == "monitor") {
391 gLogVerbosity.monitor = true;
392 } else if (verbose_options[i] == "startup") {
393 gLogVerbosity.startup = true;
394 } else if (verbose_options[i] == "third-party-jni") {
395 gLogVerbosity.third_party_jni = true;
396 } else if (verbose_options[i] == "threads") {
397 gLogVerbosity.threads = true;
398 } else {
399 LOG(WARNING) << "Ignoring unknown -verbose option: " << verbose_options[i];
400 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700401 }
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700402 } else if (option.starts_with("-Xjnigreflimit:")) {
403 parsed->jni_globals_max_ = ParseIntegerOrDie(option);
Elliott Hughesfc861622011-10-17 17:57:47 -0700404 } else if (option.starts_with("-Xlockprofthreshold:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700405 parsed->lock_profiling_threshold_ = ParseIntegerOrDie(option);
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700406 } else if (option.starts_with("-Xstacktracefile:")) {
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700407// always show stack traces in debug builds
408#ifdef NDEBUG
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700409 parsed->stack_trace_file_ = option.substr(strlen("-Xstacktracefile:")).data();
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700410#endif
Elliott Hughesfc861622011-10-17 17:57:47 -0700411 } else if (option == "sensitiveThread") {
412 parsed->hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(options[i].second);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700413 } else if (option == "vfprintf") {
414 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
415 } else if (option == "exit") {
416 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
417 } else if (option == "abort") {
418 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700419 } else if (option == "host-prefix") {
420 parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second);
Elliott Hughesb3e66df2012-01-12 14:49:18 -0800421 } else if (option == "-Xgenregmap" || option == "-Xgc:precise") {
422 // We silently ignore these for backwards compatibility.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700423 } else {
424 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700425 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700426 LOG(ERROR) << "Unrecognized option " << option;
427 // TODO: this should exit, but for now tolerate unknown options
428 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700429 }
430 }
431 }
432
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700433 if (!compiler && parsed->images_.empty()) {
Brian Carlstrom29e7ac72011-12-05 23:42:57 -0800434 parsed->images_.push_back("/system/framework/boot.art");
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700435 }
jeffhaoc1160702011-10-27 15:48:45 -0700436 if (parsed->heap_growth_limit_ == 0) {
437 parsed->heap_growth_limit_ = parsed->heap_maximum_size_;
438 }
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700439
Elliott Hughescac6cc72011-11-03 20:31:21 -0700440 LOG(INFO) << "Build type: "
441#ifndef NDEBUG
442 << "debug"
443#else
444 << "optimized"
445#endif
446 << "; CheckJNI: " << (parsed->check_jni_ ? "on" : "off");
Elliott Hughes85d15452011-09-16 17:33:01 -0700447
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700448 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700449}
450
451Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700452 // TODO: acquire a static mutex on Runtime to avoid racing.
453 if (Runtime::instance_ != NULL) {
454 return NULL;
455 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700456 instance_ = new Runtime;
457 if (!instance_->Init(options, ignore_unrecognized)) {
458 delete instance_;
459 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700460 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700461 return instance_;
462}
Elliott Hughes0af55432011-08-17 18:37:28 -0700463
Brian Carlstromaded5f72011-10-07 17:15:04 -0700464void CreateSystemClassLoader() {
465 if (ClassLoader::UseCompileTimeClassPath()) {
466 return;
467 }
468
469 Thread* self = Thread::Current();
470
471 // Must be in the kNative state for calling native methods.
472 CHECK_EQ(self->GetState(), Thread::kNative);
473
474 JNIEnv* env = self->GetJniEnv();
Brian Carlstromdf143242011-10-10 18:05:34 -0700475 ScopedLocalRef<jclass> ClassLoader_class(env, env->FindClass("java/lang/ClassLoader"));
476 CHECK(ClassLoader_class.get() != NULL);
477 jmethodID getSystemClassLoader = env->GetStaticMethodID(ClassLoader_class.get(),
478 "getSystemClassLoader",
479 "()Ljava/lang/ClassLoader;");
480 CHECK(getSystemClassLoader != NULL);
481 ScopedLocalRef<jobject> class_loader(env, env->CallStaticObjectMethod(ClassLoader_class.get(),
482 getSystemClassLoader));
483 CHECK(class_loader.get() != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700484
Brian Carlstromdf143242011-10-10 18:05:34 -0700485 Thread::Current()->SetClassLoaderOverride(Decode<ClassLoader*>(env, class_loader.get()));
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500486
487 ScopedLocalRef<jclass> Thread_class(env, env->FindClass("java/lang/Thread"));
488 CHECK(Thread_class.get() != NULL);
489 jfieldID contextClassLoader = env->GetFieldID(Thread_class.get(),
490 "contextClassLoader",
491 "Ljava/lang/ClassLoader;");
492 CHECK(contextClassLoader != NULL);
493 ScopedLocalRef<jobject> self_jobject(env, AddLocalReference<jobject>(env, self->GetPeer()));
494 env->SetObjectField(self_jobject.get(), contextClassLoader, class_loader.get());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700495}
496
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700497void Runtime::Start() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800498 VLOG(startup) << "Runtime::Start entering";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700499
500 CHECK(host_prefix_.empty()) << host_prefix_;
501
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700502 // Restore main thread state to kNative as expected by native code
503 Thread::Current()->SetState(Thread::kNative);
504
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700505 started_ = true;
506
Brian Carlstromae826982011-11-09 01:33:42 -0800507 // InitNativeMethods needs to be after started_ so that the classes
508 // it touches will have methods linked to the oat file if necessary.
509 InitNativeMethods();
510
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500511 Thread::FinishStartup();
512
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700513 if (!is_zygote_) {
514 DidForkFromZygote();
515 }
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700516
Elliott Hughes85d15452011-09-16 17:33:01 -0700517 StartDaemonThreads();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700518
Brian Carlstromaded5f72011-10-07 17:15:04 -0700519 CreateSystemClassLoader();
520
Elliott Hughes726079d2011-10-07 18:43:44 -0700521 Thread::Current()->GetJniEnv()->locals.AssertEmpty();
522
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800523 VLOG(startup) << "Runtime::Start exiting";
Elliott Hughes85d15452011-09-16 17:33:01 -0700524}
525
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700526void Runtime::DidForkFromZygote() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700527 is_zygote_ = false;
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700528
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700529 StartSignalCatcher();
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700530
531 // Start the JDWP thread. If the command-line debugger flags specified "suspend=y",
532 // this will pause the runtime, so we probably want this to come last.
533 Dbg::StartJdwp();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700534}
535
536void Runtime::StartSignalCatcher() {
537 if (!is_zygote_) {
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700538 signal_catcher_ = new SignalCatcher(stack_trace_file_);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700539 }
540}
541
Elliott Hughes85d15452011-09-16 17:33:01 -0700542void Runtime::StartDaemonThreads() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800543 VLOG(startup) << "Runtime::StartDaemonThreads entering";
Elliott Hughes85d15452011-09-16 17:33:01 -0700544
Elliott Hughes719b3232011-09-25 17:42:19 -0700545 Thread* self = Thread::Current();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700546
547 // Must be in the kNative state for calling native methods.
548 CHECK_EQ(self->GetState(), Thread::kNative);
549
Elliott Hughes719b3232011-09-25 17:42:19 -0700550 JNIEnv* env = self->GetJniEnv();
Elliott Hughes726079d2011-10-07 18:43:44 -0700551 ScopedLocalRef<jclass> c(env, env->FindClass("java/lang/Daemons"));
552 CHECK(c.get() != NULL);
553 jmethodID mid = env->GetStaticMethodID(c.get(), "start", "()V");
Elliott Hughes719b3232011-09-25 17:42:19 -0700554 CHECK(mid != NULL);
Elliott Hughes726079d2011-10-07 18:43:44 -0700555 env->CallStaticVoidMethod(c.get(), mid);
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800556 CHECK(!env->ExceptionCheck());
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700557
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800558 VLOG(startup) << "Runtime::StartDaemonThreads exiting";
Carl Shapiro61e019d2011-07-14 16:53:09 -0700559}
560
Elliott Hughes6b355752012-01-13 16:49:08 -0800561bool Runtime::IsShuttingDown() const {
562 return shutting_down_;
563}
564
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700565bool Runtime::IsStarted() const {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700566 return started_;
567}
568
Elliott Hughes0af55432011-08-17 18:37:28 -0700569bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700570 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700571
Elliott Hughes90a33692011-08-30 13:27:07 -0700572 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
573 if (options.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700574 LOG(ERROR) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700575 return false;
576 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800577 VLOG(startup) << "Runtime::Init -verbose:startup enabled";
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700578
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700579 SetJniGlobalsMax(options->jni_globals_max_);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800580 Monitor::Init(options->lock_profiling_threshold_, options->hook_is_sensitive_thread_);
Elliott Hughes32d6e1e2011-10-11 14:47:44 -0700581
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700582 host_prefix_ = options->host_prefix_;
583 boot_class_path_ = options->boot_class_path_;
584 class_path_ = options->class_path_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700585 properties_ = options->properties_;
586
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700587 is_zygote_ = options->is_zygote_;
588
Elliott Hughes0af55432011-08-17 18:37:28 -0700589 vfprintf_ = options->hook_vfprintf_;
590 exit_ = options->hook_exit_;
591 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700592
Elliott Hughesbe759c62011-09-08 19:38:21 -0700593 default_stack_size_ = options->stack_size_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700594 stack_trace_file_ = options->stack_trace_file_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700595
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700596 monitor_list_ = new MonitorList;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800597 thread_list_ = new ThreadList;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700598 intern_table_ = new InternTable;
599
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800600 Heap::Init(options->heap_initial_size_,
jeffhaoc1160702011-10-27 15:48:45 -0700601 options->heap_growth_limit_,
Ian Rogers30fab402012-01-23 15:43:46 -0800602 options->heap_maximum_size_,
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700603 options->images_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700604
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700605 BlockSignals();
606
Elliott Hughesa0957642011-09-02 14:27:33 -0700607 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700608
Elliott Hughesbe759c62011-09-08 19:38:21 -0700609 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700610
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700611 // ClassLinker needs an attached thread, but we can't fully attach a thread
612 // without creating objects.
613 Thread::Attach(this, "main", false);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700614
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700615 // Set us to runnable so tools using a runtime can allocate and GC by default
616 Thread::Current()->SetState(Thread::kRunnable);
617
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700618 CHECK_GE(Heap::GetSpaces().size(), 1U);
619 class_linker_ = ((Heap::GetSpaces()[0]->IsImageSpace())
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800620 ? ClassLinker::Create(intern_table_)
621 : ClassLinker::Create(options->boot_class_path_, intern_table_));
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700622
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800623 VLOG(startup) << "Runtime::Init exiting";
Carl Shapiro1fb86202011-06-27 17:43:13 -0700624 return true;
625}
626
Elliott Hughes038a8062011-09-18 14:12:41 -0700627void Runtime::InitNativeMethods() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800628 VLOG(startup) << "Runtime::InitNativeMethods entering";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700629 Thread* self = Thread::Current();
630 JNIEnv* env = self->GetJniEnv();
631
Elliott Hughes418d20f2011-09-22 14:00:39 -0700632 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Brian Carlstromaded5f72011-10-07 17:15:04 -0700633 CHECK_EQ(self->GetState(), Thread::kNative);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700634
Elliott Hughes418d20f2011-09-22 14:00:39 -0700635 // First set up JniConstants, which is used by both the runtime's built-in native
636 // methods and libcore.
Elliott Hughesfea966e2011-09-22 10:26:20 -0700637 JniConstants::init(env);
638
Elliott Hughes418d20f2011-09-22 14:00:39 -0700639 // Then set up the native methods provided by the runtime itself.
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700640 RegisterRuntimeNativeMethods(env);
641
Elliott Hughes418d20f2011-09-22 14:00:39 -0700642 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
643 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
644 // the library that implements System.loadLibrary!
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700645 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800646 VLOG(startup) << "Runtime::InitNativeMethods exiting";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700647}
648
649void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
650#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700651 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700652 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700653 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700654 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700655 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700656 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700657 REGISTER(register_java_lang_Object);
658 REGISTER(register_java_lang_Runtime);
659 REGISTER(register_java_lang_String);
660 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700661 REGISTER(register_java_lang_Thread);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700662 REGISTER(register_java_lang_Throwable);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700663 REGISTER(register_java_lang_VMClassLoader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700664 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700665 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700666 REGISTER(register_java_lang_reflect_Field);
667 REGISTER(register_java_lang_reflect_Method);
Jesse Wilson95caa792011-10-12 18:14:17 -0400668 REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700669 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700670 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700671 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700672 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700673#undef REGISTER
674}
675
Elliott Hughes8daa0922011-09-11 13:46:25 -0700676void Runtime::Dump(std::ostream& os) {
Elliott Hughese27955c2011-08-26 15:21:24 -0700677 // TODO: dump other runtime statistics?
Elliott Hughescac6cc72011-11-03 20:31:21 -0700678 GetClassLinker()->DumpForSigQuit(os);
679 GetInternTable()->DumpForSigQuit(os);
Elliott Hughes42ee1422011-09-06 12:33:32 -0700680 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700681
682 thread_list_->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700683}
684
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800685void Runtime::DumpLockHolders(std::ostream& os) {
686 pid_t heap_lock_owner = Heap::GetLockOwner();
687 pid_t thread_list_lock_owner = GetThreadList()->GetLockOwner();
688 pid_t classes_lock_owner = GetClassLinker()->GetClassesLockOwner();
689 pid_t dex_lock_owner = GetClassLinker()->GetDexLockOwner();
690 if ((heap_lock_owner | thread_list_lock_owner | classes_lock_owner | dex_lock_owner) != 0) {
691 os << "Heap lock owner tid: " << heap_lock_owner << "\n"
692 << "ThreadList lock owner tid: " << thread_list_lock_owner << "\n"
693 << "ClassLinker classes lock owner tid: " << classes_lock_owner << "\n"
694 << "ClassLinker dex lock owner tid: " << dex_lock_owner << "\n";
695 }
696}
697
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700698void Runtime::SetStatsEnabled(bool new_state) {
699 if (new_state == true) {
700 GetStats()->Clear(~0);
701 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
702 Thread::Current()->GetStats()->Clear(~0);
703 }
704 stats_enabled_ = new_state;
705}
706
707void Runtime::ResetStats(int kinds) {
708 GetStats()->Clear(kinds & 0xffff);
709 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
710 Thread::Current()->GetStats()->Clear(kinds >> 16);
711}
712
713RuntimeStats* Runtime::GetStats() {
714 return &stats_;
715}
716
717int32_t Runtime::GetStat(int kind) {
718 RuntimeStats* stats;
719 if (kind < (1<<16)) {
720 stats = GetStats();
721 } else {
722 stats = Thread::Current()->GetStats();
723 kind >>= 16;
724 }
725 switch (kind) {
726 case KIND_ALLOCATED_OBJECTS:
727 return stats->allocated_objects;
728 case KIND_ALLOCATED_BYTES:
729 return stats->allocated_bytes;
730 case KIND_FREED_OBJECTS:
731 return stats->freed_objects;
732 case KIND_FREED_BYTES:
733 return stats->freed_bytes;
734 case KIND_GC_INVOCATIONS:
735 return stats->gc_for_alloc_count;
736 case KIND_CLASS_INIT_COUNT:
737 return stats->class_init_count;
738 case KIND_CLASS_INIT_TIME:
739 // Convert ns to us, reduce to 32 bits.
740 return (int) (stats->class_init_time_ns / 1000);
741 case KIND_EXT_ALLOCATED_OBJECTS:
742 case KIND_EXT_ALLOCATED_BYTES:
743 case KIND_EXT_FREED_OBJECTS:
744 case KIND_EXT_FREED_BYTES:
745 return 0; // backward compatibility
746 default:
747 CHECK(false);
748 return -1; // unreachable
749 }
750}
751
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700752void Runtime::BlockSignals() {
753 sigset_t sigset;
754 if (sigemptyset(&sigset) == -1) {
755 PLOG(FATAL) << "sigemptyset failed";
756 }
757 if (sigaddset(&sigset, SIGPIPE) == -1) {
758 PLOG(ERROR) << "sigaddset SIGPIPE failed";
759 }
760 // SIGQUIT is used to dump the runtime's state (including stack traces).
761 if (sigaddset(&sigset, SIGQUIT) == -1) {
762 PLOG(ERROR) << "sigaddset SIGQUIT failed";
763 }
764 // SIGUSR1 is used to initiate a heap dump.
765 if (sigaddset(&sigset, SIGUSR1) == -1) {
766 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
767 }
768 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
769}
770
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700771void Runtime::AttachCurrentThread(const char* name, bool as_daemon) {
772 Thread::Attach(instance_, name, as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700773}
774
Elliott Hughesd92bec42011-09-02 17:04:36 -0700775void Runtime::DetachCurrentThread() {
Elliott Hughes038a8062011-09-18 14:12:41 -0700776 // TODO: check we're not calling DetachCurrentThread from a call stack that
777 // includes managed frames. (It's only valid if the stack is all-native.)
Elliott Hughes02b48d12011-09-07 17:15:51 -0700778 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700779}
780
Brian Carlstrome24fa612011-09-29 00:53:55 -0700781void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700782 Dbg::VisitRoots(visitor, arg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700783 class_linker_->VisitRoots(visitor, arg);
784 intern_table_->VisitRoots(visitor, arg);
785 java_vm_->VisitRoots(visitor, arg);
786 thread_list_->VisitRoots(visitor, arg);
787 visitor(jni_stub_array_, arg);
788 visitor(abstract_method_error_stub_array_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700789 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
790 visitor(resolution_stub_array_[i], arg);
791 }
792 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
793 visitor(callee_save_method_[i], arg);
794 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700795}
796
Ian Rogers169c9a72011-11-13 20:13:17 -0800797bool Runtime::HasJniDlsymLookupStub() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700798 return jni_stub_array_ != NULL;
799}
800
Ian Rogers169c9a72011-11-13 20:13:17 -0800801ByteArray* Runtime::GetJniDlsymLookupStub() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700802 CHECK(jni_stub_array_ != NULL);
803 return jni_stub_array_;
804}
805
Ian Rogers169c9a72011-11-13 20:13:17 -0800806void Runtime::SetJniDlsymLookupStub(ByteArray* jni_stub_array) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700807 CHECK(jni_stub_array != NULL) << " jni_stub_array=" << jni_stub_array;
808 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array)
809 << "jni_stub_array_=" << jni_stub_array_ << " jni_stub_array=" << jni_stub_array;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700810 jni_stub_array_ = jni_stub_array;
811}
812
813bool Runtime::HasAbstractMethodErrorStubArray() const {
814 return abstract_method_error_stub_array_ != NULL;
815}
816
817ByteArray* Runtime::GetAbstractMethodErrorStubArray() const {
818 CHECK(abstract_method_error_stub_array_ != NULL);
819 return abstract_method_error_stub_array_;
820}
821
822void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) {
823 CHECK(abstract_method_error_stub_array != NULL);
824 CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array);
825 abstract_method_error_stub_array_ = abstract_method_error_stub_array;
826}
827
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700828
829Runtime::TrampolineType Runtime::GetTrampolineType(Method* method) {
830 if (method == NULL) {
831 return Runtime::kUnknownMethod;
832 } else if (method->IsStatic()) {
833 return Runtime::kStaticMethod;
834 } else {
835 return Runtime::kInstanceMethod;
836 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700837}
838
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700839bool Runtime::HasResolutionStubArray(TrampolineType type) const {
840 return resolution_stub_array_[type] != NULL;
Ian Rogersad25ac52011-10-04 19:13:33 -0700841}
842
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700843ByteArray* Runtime::GetResolutionStubArray(TrampolineType type) const {
844 CHECK(HasResolutionStubArray(type));
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700845 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastTrampolineMethodType));
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700846 return resolution_stub_array_[type];
847}
848
849void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700850 CHECK(resolution_stub_array != NULL);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700851 CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array);
852 resolution_stub_array_[type] = resolution_stub_array;
Ian Rogersad25ac52011-10-04 19:13:33 -0700853}
854
Brian Carlstromae826982011-11-09 01:33:42 -0800855Method* Runtime::CreateCalleeSaveMethod(InstructionSet instruction_set, CalleeSaveType type) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700856 Class* method_class = Method::GetMethodClass();
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700857 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
Ian Rogersff1ed472011-09-20 13:46:24 -0700858 method->SetDeclaringClass(method_class);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800859 // TODO: use a special method for callee saves
860 method->SetMethodIndex(DexFile::kDexNoIndex16);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700861 method->SetCode(NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800862 if ((instruction_set == kThumb2) || (instruction_set == kArm)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700863 uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6) | (1 << art::arm::R7) |
864 (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11);
865 uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3);
866 uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9);
867 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills :0) |
868 (type == kSaveAll ? all_spills :0) | (1 << art::arm::LR);
869 uint32_t fp_all_spills = (1 << art::arm::S0) | (1 << art::arm::S1) | (1 << art::arm::S2) |
870 (1 << art::arm::S3) | (1 << art::arm::S4) | (1 << art::arm::S5) |
871 (1 << art::arm::S6) | (1 << art::arm::S7) | (1 << art::arm::S8) |
872 (1 << art::arm::S9) | (1 << art::arm::S10) | (1 << art::arm::S11) |
873 (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) |
874 (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) |
875 (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) |
876 (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) |
877 (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) |
878 (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
879 (1 << art::arm::S30) | (1 << art::arm::S31);
880 uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
881 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
882 __builtin_popcount(fp_spills) /* fprs */ +
883 1 /* Method* */) * kPointerSize, kStackAlignment);
Ian Rogers15fdb8c2011-09-25 15:45:07 -0700884 method->SetFrameSizeInBytes(frame_size);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700885 method->SetCoreSpillMask(core_spills);
886 method->SetFpSpillMask(fp_spills);
Brian Carlstromae826982011-11-09 01:33:42 -0800887 } else if (instruction_set == kX86) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700888 method->SetFrameSizeInBytes(32);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700889 method->SetCoreSpillMask((1 << art::x86::EBX) | (1 << art::x86::EBP) | (1 << art::x86::ESI) |
Ian Rogersff1ed472011-09-20 13:46:24 -0700890 (1 << art::x86::EDI));
891 method->SetFpSpillMask(0);
892 } else {
893 UNIMPLEMENTED(FATAL);
894 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700895 return method.get();
Ian Rogersff1ed472011-09-20 13:46:24 -0700896}
897
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700898bool Runtime::HasCalleeSaveMethod(CalleeSaveType type) const {
899 return callee_save_method_[type] != NULL;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700900}
901
Brian Carlstrome24fa612011-09-29 00:53:55 -0700902// Returns a special method that describes all callee saves being spilled to the stack.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700903Method* Runtime::GetCalleeSaveMethod(CalleeSaveType type) const {
904 CHECK(HasCalleeSaveMethod(type));
905 return callee_save_method_[type];
Brian Carlstrome24fa612011-09-29 00:53:55 -0700906}
907
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700908void Runtime::SetCalleeSaveMethod(Method* method, CalleeSaveType type) {
909 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
910 callee_save_method_[type] = method;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700911}
912
jeffhao2692b572011-12-16 15:42:28 -0800913void Runtime::EnableMethodTracing(Trace* tracer) {
914 CHECK(!IsMethodTracingActive());
915 tracer_ = tracer;
916}
917
918void Runtime::DisableMethodTracing() {
919 CHECK(IsMethodTracingActive());
920 delete tracer_;
921 tracer_ = NULL;
922}
923
924bool Runtime::IsMethodTracingActive() const {
925 return (tracer_ != NULL);
926}
927
928Trace* Runtime::GetTracer() const {
929 CHECK(IsMethodTracingActive());
930 return tracer_;
931}
932
Carl Shapiro1fb86202011-06-27 17:43:13 -0700933} // namespace art