blob: 6cb874f51a516349ae696d07778a08b9a8b74376 [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";
112 Thread* self = Thread::Current();
113 if (self == NULL) {
114 os << "(Aborting thread was not attached to runtime!)\n";
115 } else {
Elliott Hughes899e7892012-01-24 14:57:32 -0800116 self->Dump(os);
117 if (self->IsExceptionPending()) {
118 os << "Pending " << PrettyTypeOf(self->GetException()) << " on thread:\n"
119 << self->GetException()->Dump();
120 }
Elliott Hughese0918552011-10-28 17:18:29 -0700121 }
122 }
123};
124
Elliott Hughesffe67362011-07-17 12:09:27 -0700125void Runtime::Abort(const char* file, int line) {
Elliott Hughes131aef82012-01-27 11:53:35 -0800126 // Ensure that we don't have multiple threads trying to abort at once,
127 // which would result in significantly worse diagnostics.
128 MutexLock mu(abort_lock_);
129
Elliott Hughesffe67362011-07-17 12:09:27 -0700130 // Get any pending output out of the way.
131 fflush(NULL);
132
133 // Many people have difficulty distinguish aborts from crashes,
134 // so be explicit.
Elliott Hughese0918552011-10-28 17:18:29 -0700135 AbortState state;
136 LOG(ERROR) << Dumpable<AbortState>(state);
Elliott Hughesffe67362011-07-17 12:09:27 -0700137
Elliott Hughesffe67362011-07-17 12:09:27 -0700138 // Perform any platform-specific pre-abort actions.
139 PlatformAbort(file, line);
140
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700141 // use abort hook if we have one
142 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
143 Runtime::Current()->abort_();
144 // notreached
145 }
146
Elliott Hughesffe67362011-07-17 12:09:27 -0700147 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -0700148 // receive SIGABRT. debuggerd dumps the stack trace of the main
149 // thread, whether or not that was the thread that failed. By
150 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -0700151 // fault in the current thread, and get a useful log from debuggerd.
152 // We can also trivially tell the difference between a VM crash and
153 // a deliberate abort by looking at the fault address.
154 *reinterpret_cast<char*>(0xdeadd00d) = 38;
155 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -0700156 // notreached
157}
158
Elliott Hughesbf86d042011-08-31 17:53:14 -0700159void Runtime::CallExitHook(jint status) {
160 if (exit_ != NULL) {
161 ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative);
162 exit_(status);
163 LOG(WARNING) << "Exit hook returned instead of exiting!";
164 }
165}
166
Brian Carlstrom8a436592011-08-15 21:27:23 -0700167// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
168// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
169// [gG] gigabytes.
170//
171// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700172// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
173// of 1024.
174//
175// The spec says the -Xmx and -Xms options must be multiples of 1024. It
176// doesn't say anything about -Xss.
177//
178// Returns 0 (a useless size) if "s" is malformed or specifies a low or
179// non-evenly-divisible value.
180//
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800181size_t ParseMemoryOption(const char* s, size_t div) {
Brian Carlstrom8a436592011-08-15 21:27:23 -0700182 // strtoul accepts a leading [+-], which we don't want,
183 // so make sure our string starts with a decimal digit.
184 if (isdigit(*s)) {
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800185 const char* s2;
186 size_t val = strtoul(s, (char**)&s2, 10);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700187 if (s2 != s) {
188 // s2 should be pointing just after the number.
189 // If this is the end of the string, the user
190 // has specified a number of bytes. Otherwise,
191 // there should be exactly one more character
192 // that specifies a multiplier.
193 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700194 // The remainder of the string is either a single multiplier
195 // character, or nothing to indicate that the value is in
196 // bytes.
197 char c = *s2++;
198 if (*s2 == '\0') {
199 size_t mul;
200 if (c == '\0') {
201 mul = 1;
202 } else if (c == 'k' || c == 'K') {
203 mul = KB;
204 } else if (c == 'm' || c == 'M') {
205 mul = MB;
206 } else if (c == 'g' || c == 'G') {
207 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700208 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700209 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700210 return 0;
211 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700212
213 if (val <= std::numeric_limits<size_t>::max() / mul) {
214 val *= mul;
215 } else {
216 // Clamp to a multiple of 1024.
217 val = std::numeric_limits<size_t>::max() & ~(1024-1);
218 }
219 } else {
220 // There's more than one character after the numeric part.
221 return 0;
222 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700223 }
224 // The man page says that a -Xm value must be a multiple of 1024.
225 if (val % div == 0) {
226 return val;
227 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700228 }
229 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700230 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700231}
232
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700233size_t ParseIntegerOrDie(const StringPiece& s) {
234 StringPiece::size_type colon = s.find(':');
235 if (colon == StringPiece::npos) {
236 LOG(FATAL) << "Missing integer: " << s;
237 }
238 const char* begin = &s.data()[colon + 1];
239 char* end;
240 size_t result = strtoul(begin, &end, 10);
241 if (begin == end || *end != '\0') {
242 LOG(FATAL) << "Failed to parse integer in: " << s;
243 }
244 return result;
245}
246
Elliott Hughes0af55432011-08-17 18:37:28 -0700247void LoadJniLibrary(JavaVMExt* vm, const char* name) {
Elliott Hughes6cc332e2012-01-20 22:59:20 -0800248 std::string mapped_name(StringPrintf(OS_SHARED_LIB_FORMAT_STR, name));
Elliott Hughes75770752011-08-24 17:52:38 -0700249 std::string reason;
250 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700251 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
252 << reason;
253 }
254}
255
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700256Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700257 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700258 bool compiler = false;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700259 const char* boot_class_path = getenv("BOOTCLASSPATH");
260 if (boot_class_path != NULL) {
Ian Rogers4f6b57a2012-01-17 16:23:27 -0800261 parsed->boot_class_path_ = boot_class_path;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700262 }
263 const char* class_path = getenv("CLASSPATH");
264 if (class_path != NULL) {
Ian Rogers4f6b57a2012-01-17 16:23:27 -0800265 parsed->class_path_ = class_path;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700266 }
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700267#ifdef NDEBUG
Elliott Hughes5174fe62011-08-23 15:12:35 -0700268 // -Xcheck:jni is off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700269 parsed->check_jni_ = false;
270#else
271 // ...but on by default in debug builds.
272 parsed->check_jni_ = true;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700273#endif
Elliott Hughes85d15452011-09-16 17:33:01 -0700274
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700275 parsed->heap_initial_size_ = Heap::kInitialSize;
276 parsed->heap_maximum_size_ = Heap::kMaximumSize;
jeffhaoc1160702011-10-27 15:48:45 -0700277 parsed->heap_growth_limit_ = 0; // 0 means no growth limit
Brian Carlstromf734cf52011-08-17 16:28:14 -0700278 parsed->stack_size_ = Thread::kDefaultStackSize;
279
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700280 parsed->is_zygote_ = false;
281
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700282 parsed->jni_globals_max_ = 0;
Elliott Hughesfc861622011-10-17 17:57:47 -0700283 parsed->lock_profiling_threshold_ = 0;
284 parsed->hook_is_sensitive_thread_ = NULL;
285
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700286 parsed->hook_vfprintf_ = vfprintf;
287 parsed->hook_exit_ = exit;
288 parsed->hook_abort_ = abort;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700289
290 for (size_t i = 0; i < options.size(); ++i) {
291 const StringPiece& option = options[i].first;
Brian Carlstrom0796af02011-10-12 14:31:45 -0700292 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700293 LOG(INFO) << "option[" << i << "]=" << option;
294 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700295 if (option.starts_with("-Xbootclasspath:")) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700296 parsed->boot_class_path_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700297 } else if (option == "-classpath" || option == "-cp") {
298 // TODO: support -Djava.class.path
299 i++;
300 if (i == options.size()) {
301 // TODO: usage
302 LOG(FATAL) << "Missing required class path value for " << option;
303 return NULL;
304 }
305 const StringPiece& value = options[i].first;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700306 parsed->class_path_ = value.data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700307 } else if (option.starts_with("-Ximage:")) {
308 parsed->images_.push_back(option.substr(strlen("-Ximage:")).data());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700309 } else if (option.starts_with("-Xcheck:jni")) {
310 parsed->check_jni_ = true;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700311 } else if (option.starts_with("-Xrunjdwp:") || option.starts_with("-agentlib:jdwp=")) {
Elliott Hughes95572412011-12-13 18:14:20 -0800312 std::string tail(option.substr(option[1] == 'X' ? 10 : 15).ToString());
Elliott Hughes3bb81562011-10-21 18:52:59 -0700313 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
314 LOG(FATAL) << "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
315 << "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n";
316 return NULL;
317 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700318 } else if (option.starts_with("-Xms")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700319 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024);
320 if (size == 0) {
321 if (ignore_unrecognized) {
322 continue;
323 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700324 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700325 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700326 return NULL;
327 }
328 parsed->heap_initial_size_ = size;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700329 } else if (option.starts_with("-Xmx")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700330 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024);
331 if (size == 0) {
332 if (ignore_unrecognized) {
333 continue;
334 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700335 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700336 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700337 return NULL;
338 }
339 parsed->heap_maximum_size_ = size;
jeffhaoc1160702011-10-27 15:48:45 -0700340 } else if (option.starts_with("-XX:HeapGrowthLimit=")) {
341 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).data(), 1024);
342 if (size == 0) {
343 if (ignore_unrecognized) {
344 continue;
345 }
346 // TODO: usage
347 LOG(FATAL) << "Failed to parse " << option;
348 return NULL;
349 }
350 parsed->heap_growth_limit_ = size;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700351 } else if (option.starts_with("-Xss")) {
352 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1);
353 if (size == 0) {
354 if (ignore_unrecognized) {
355 continue;
356 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700357 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700358 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700359 return NULL;
360 }
361 parsed->stack_size_ = size;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700362 } else if (option.starts_with("-D")) {
363 parsed->properties_.push_back(option.substr(strlen("-D")).data());
Elliott Hughesa0957642011-09-02 14:27:33 -0700364 } else if (option.starts_with("-Xjnitrace:")) {
365 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:")).data();
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700366 } else if (option == "compiler") {
367 compiler = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700368 } else if (option == "-Xzygote") {
369 parsed->is_zygote_ = true;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700370 } else if (option.starts_with("-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700371 std::vector<std::string> verbose_options;
Elliott Hughes34023802011-08-30 12:06:17 -0700372 Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700373 for (size_t i = 0; i < verbose_options.size(); ++i) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800374 if (verbose_options[i] == "class") {
375 gLogVerbosity.class_linker = true;
376 } else if (verbose_options[i] == "compiler") {
377 gLogVerbosity.compiler = true;
378 } else if (verbose_options[i] == "heap") {
379 gLogVerbosity.heap = true;
380 } else if (verbose_options[i] == "gc") {
381 gLogVerbosity.gc = true;
382 } else if (verbose_options[i] == "jdwp") {
383 gLogVerbosity.jdwp = true;
384 } else if (verbose_options[i] == "jni") {
385 gLogVerbosity.jni = true;
386 } else if (verbose_options[i] == "monitor") {
387 gLogVerbosity.monitor = true;
388 } else if (verbose_options[i] == "startup") {
389 gLogVerbosity.startup = true;
390 } else if (verbose_options[i] == "third-party-jni") {
391 gLogVerbosity.third_party_jni = true;
392 } else if (verbose_options[i] == "threads") {
393 gLogVerbosity.threads = true;
394 } else {
395 LOG(WARNING) << "Ignoring unknown -verbose option: " << verbose_options[i];
396 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700397 }
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700398 } else if (option.starts_with("-Xjnigreflimit:")) {
399 parsed->jni_globals_max_ = ParseIntegerOrDie(option);
Elliott Hughesfc861622011-10-17 17:57:47 -0700400 } else if (option.starts_with("-Xlockprofthreshold:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700401 parsed->lock_profiling_threshold_ = ParseIntegerOrDie(option);
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700402 } else if (option.starts_with("-Xstacktracefile:")) {
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700403// always show stack traces in debug builds
404#ifdef NDEBUG
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700405 parsed->stack_trace_file_ = option.substr(strlen("-Xstacktracefile:")).data();
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700406#endif
Elliott Hughesfc861622011-10-17 17:57:47 -0700407 } else if (option == "sensitiveThread") {
408 parsed->hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(options[i].second);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700409 } else if (option == "vfprintf") {
410 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
411 } else if (option == "exit") {
412 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
413 } else if (option == "abort") {
414 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700415 } else if (option == "host-prefix") {
416 parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second);
Elliott Hughesb3e66df2012-01-12 14:49:18 -0800417 } else if (option == "-Xgenregmap" || option == "-Xgc:precise") {
418 // We silently ignore these for backwards compatibility.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700419 } else {
420 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700421 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700422 LOG(ERROR) << "Unrecognized option " << option;
423 // TODO: this should exit, but for now tolerate unknown options
424 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700425 }
426 }
427 }
428
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700429 if (!compiler && parsed->images_.empty()) {
Brian Carlstrom29e7ac72011-12-05 23:42:57 -0800430 parsed->images_.push_back("/system/framework/boot.art");
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700431 }
jeffhaoc1160702011-10-27 15:48:45 -0700432 if (parsed->heap_growth_limit_ == 0) {
433 parsed->heap_growth_limit_ = parsed->heap_maximum_size_;
434 }
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700435
Elliott Hughescac6cc72011-11-03 20:31:21 -0700436 LOG(INFO) << "Build type: "
437#ifndef NDEBUG
438 << "debug"
439#else
440 << "optimized"
441#endif
442 << "; CheckJNI: " << (parsed->check_jni_ ? "on" : "off");
Elliott Hughes85d15452011-09-16 17:33:01 -0700443
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700444 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700445}
446
447Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700448 // TODO: acquire a static mutex on Runtime to avoid racing.
449 if (Runtime::instance_ != NULL) {
450 return NULL;
451 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700452 instance_ = new Runtime;
453 if (!instance_->Init(options, ignore_unrecognized)) {
454 delete instance_;
455 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700456 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700457 return instance_;
458}
Elliott Hughes0af55432011-08-17 18:37:28 -0700459
Brian Carlstromaded5f72011-10-07 17:15:04 -0700460void CreateSystemClassLoader() {
461 if (ClassLoader::UseCompileTimeClassPath()) {
462 return;
463 }
464
465 Thread* self = Thread::Current();
466
467 // Must be in the kNative state for calling native methods.
468 CHECK_EQ(self->GetState(), Thread::kNative);
469
470 JNIEnv* env = self->GetJniEnv();
Brian Carlstromdf143242011-10-10 18:05:34 -0700471 ScopedLocalRef<jclass> ClassLoader_class(env, env->FindClass("java/lang/ClassLoader"));
472 CHECK(ClassLoader_class.get() != NULL);
473 jmethodID getSystemClassLoader = env->GetStaticMethodID(ClassLoader_class.get(),
474 "getSystemClassLoader",
475 "()Ljava/lang/ClassLoader;");
476 CHECK(getSystemClassLoader != NULL);
477 ScopedLocalRef<jobject> class_loader(env, env->CallStaticObjectMethod(ClassLoader_class.get(),
478 getSystemClassLoader));
479 CHECK(class_loader.get() != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700480
Brian Carlstromdf143242011-10-10 18:05:34 -0700481 Thread::Current()->SetClassLoaderOverride(Decode<ClassLoader*>(env, class_loader.get()));
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500482
483 ScopedLocalRef<jclass> Thread_class(env, env->FindClass("java/lang/Thread"));
484 CHECK(Thread_class.get() != NULL);
485 jfieldID contextClassLoader = env->GetFieldID(Thread_class.get(),
486 "contextClassLoader",
487 "Ljava/lang/ClassLoader;");
488 CHECK(contextClassLoader != NULL);
489 ScopedLocalRef<jobject> self_jobject(env, AddLocalReference<jobject>(env, self->GetPeer()));
490 env->SetObjectField(self_jobject.get(), contextClassLoader, class_loader.get());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700491}
492
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700493void Runtime::Start() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800494 VLOG(startup) << "Runtime::Start entering";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700495
496 CHECK(host_prefix_.empty()) << host_prefix_;
497
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700498 // Restore main thread state to kNative as expected by native code
499 Thread::Current()->SetState(Thread::kNative);
500
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700501 started_ = true;
502
Brian Carlstromae826982011-11-09 01:33:42 -0800503 // InitNativeMethods needs to be after started_ so that the classes
504 // it touches will have methods linked to the oat file if necessary.
505 InitNativeMethods();
506
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500507 Thread::FinishStartup();
508
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700509 if (!is_zygote_) {
510 DidForkFromZygote();
511 }
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700512
Elliott Hughes85d15452011-09-16 17:33:01 -0700513 StartDaemonThreads();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700514
Brian Carlstromaded5f72011-10-07 17:15:04 -0700515 CreateSystemClassLoader();
516
Elliott Hughes726079d2011-10-07 18:43:44 -0700517 Thread::Current()->GetJniEnv()->locals.AssertEmpty();
518
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800519 VLOG(startup) << "Runtime::Start exiting";
Elliott Hughes85d15452011-09-16 17:33:01 -0700520}
521
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700522void Runtime::DidForkFromZygote() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700523 is_zygote_ = false;
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700524
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700525 StartSignalCatcher();
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700526
527 // Start the JDWP thread. If the command-line debugger flags specified "suspend=y",
528 // this will pause the runtime, so we probably want this to come last.
529 Dbg::StartJdwp();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700530}
531
532void Runtime::StartSignalCatcher() {
533 if (!is_zygote_) {
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700534 signal_catcher_ = new SignalCatcher(stack_trace_file_);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700535 }
536}
537
Elliott Hughes85d15452011-09-16 17:33:01 -0700538void Runtime::StartDaemonThreads() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800539 VLOG(startup) << "Runtime::StartDaemonThreads entering";
Elliott Hughes85d15452011-09-16 17:33:01 -0700540
Elliott Hughes719b3232011-09-25 17:42:19 -0700541 Thread* self = Thread::Current();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700542
543 // Must be in the kNative state for calling native methods.
544 CHECK_EQ(self->GetState(), Thread::kNative);
545
Elliott Hughes719b3232011-09-25 17:42:19 -0700546 JNIEnv* env = self->GetJniEnv();
Elliott Hughes726079d2011-10-07 18:43:44 -0700547 ScopedLocalRef<jclass> c(env, env->FindClass("java/lang/Daemons"));
548 CHECK(c.get() != NULL);
549 jmethodID mid = env->GetStaticMethodID(c.get(), "start", "()V");
Elliott Hughes719b3232011-09-25 17:42:19 -0700550 CHECK(mid != NULL);
Elliott Hughes726079d2011-10-07 18:43:44 -0700551 env->CallStaticVoidMethod(c.get(), mid);
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700552
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800553 VLOG(startup) << "Runtime::StartDaemonThreads exiting";
Carl Shapiro61e019d2011-07-14 16:53:09 -0700554}
555
Elliott Hughes6b355752012-01-13 16:49:08 -0800556bool Runtime::IsShuttingDown() const {
557 return shutting_down_;
558}
559
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700560bool Runtime::IsStarted() const {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700561 return started_;
562}
563
Elliott Hughes0af55432011-08-17 18:37:28 -0700564bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700565 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700566
Elliott Hughes90a33692011-08-30 13:27:07 -0700567 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
568 if (options.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700569 LOG(ERROR) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700570 return false;
571 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800572 VLOG(startup) << "Runtime::Init -verbose:startup enabled";
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700573
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700574 SetJniGlobalsMax(options->jni_globals_max_);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800575 Monitor::Init(options->lock_profiling_threshold_, options->hook_is_sensitive_thread_);
Elliott Hughes32d6e1e2011-10-11 14:47:44 -0700576
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700577 host_prefix_ = options->host_prefix_;
578 boot_class_path_ = options->boot_class_path_;
579 class_path_ = options->class_path_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700580 properties_ = options->properties_;
581
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700582 is_zygote_ = options->is_zygote_;
583
Elliott Hughes0af55432011-08-17 18:37:28 -0700584 vfprintf_ = options->hook_vfprintf_;
585 exit_ = options->hook_exit_;
586 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700587
Elliott Hughesbe759c62011-09-08 19:38:21 -0700588 default_stack_size_ = options->stack_size_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700589 stack_trace_file_ = options->stack_trace_file_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700590
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700591 monitor_list_ = new MonitorList;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800592 thread_list_ = new ThreadList;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700593 intern_table_ = new InternTable;
594
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800595 Heap::Init(options->heap_initial_size_,
jeffhaoc1160702011-10-27 15:48:45 -0700596 options->heap_growth_limit_,
Ian Rogers30fab402012-01-23 15:43:46 -0800597 options->heap_maximum_size_,
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700598 options->images_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700599
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700600 BlockSignals();
601
Elliott Hughesa0957642011-09-02 14:27:33 -0700602 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700603
Elliott Hughesbe759c62011-09-08 19:38:21 -0700604 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700605
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700606 // ClassLinker needs an attached thread, but we can't fully attach a thread
607 // without creating objects.
608 Thread::Attach(this, "main", false);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700609
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700610 // Set us to runnable so tools using a runtime can allocate and GC by default
611 Thread::Current()->SetState(Thread::kRunnable);
612
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700613 CHECK_GE(Heap::GetSpaces().size(), 1U);
614 class_linker_ = ((Heap::GetSpaces()[0]->IsImageSpace())
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800615 ? ClassLinker::Create(intern_table_)
616 : ClassLinker::Create(options->boot_class_path_, intern_table_));
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700617
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800618 VLOG(startup) << "Runtime::Init exiting";
Carl Shapiro1fb86202011-06-27 17:43:13 -0700619 return true;
620}
621
Elliott Hughes038a8062011-09-18 14:12:41 -0700622void Runtime::InitNativeMethods() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800623 VLOG(startup) << "Runtime::InitNativeMethods entering";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700624 Thread* self = Thread::Current();
625 JNIEnv* env = self->GetJniEnv();
626
Elliott Hughes418d20f2011-09-22 14:00:39 -0700627 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Brian Carlstromaded5f72011-10-07 17:15:04 -0700628 CHECK_EQ(self->GetState(), Thread::kNative);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700629
Elliott Hughes418d20f2011-09-22 14:00:39 -0700630 // First set up JniConstants, which is used by both the runtime's built-in native
631 // methods and libcore.
Elliott Hughesfea966e2011-09-22 10:26:20 -0700632 JniConstants::init(env);
633
Elliott Hughes418d20f2011-09-22 14:00:39 -0700634 // Then set up the native methods provided by the runtime itself.
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700635 RegisterRuntimeNativeMethods(env);
636
Elliott Hughes418d20f2011-09-22 14:00:39 -0700637 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
638 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
639 // the library that implements System.loadLibrary!
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700640 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800641 VLOG(startup) << "Runtime::InitNativeMethods exiting";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700642}
643
644void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
645#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700646 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700647 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700648 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700649 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700650 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700651 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700652 REGISTER(register_java_lang_Object);
653 REGISTER(register_java_lang_Runtime);
654 REGISTER(register_java_lang_String);
655 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700656 REGISTER(register_java_lang_Thread);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700657 REGISTER(register_java_lang_Throwable);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700658 REGISTER(register_java_lang_VMClassLoader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700659 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700660 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700661 REGISTER(register_java_lang_reflect_Field);
662 REGISTER(register_java_lang_reflect_Method);
Jesse Wilson95caa792011-10-12 18:14:17 -0400663 REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700664 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700665 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700666 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700667 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700668#undef REGISTER
669}
670
Elliott Hughes8daa0922011-09-11 13:46:25 -0700671void Runtime::Dump(std::ostream& os) {
Elliott Hughese27955c2011-08-26 15:21:24 -0700672 // TODO: dump other runtime statistics?
Elliott Hughescac6cc72011-11-03 20:31:21 -0700673 GetClassLinker()->DumpForSigQuit(os);
674 GetInternTable()->DumpForSigQuit(os);
Elliott Hughes42ee1422011-09-06 12:33:32 -0700675 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700676
677 thread_list_->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700678}
679
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800680void Runtime::DumpLockHolders(std::ostream& os) {
681 pid_t heap_lock_owner = Heap::GetLockOwner();
682 pid_t thread_list_lock_owner = GetThreadList()->GetLockOwner();
683 pid_t classes_lock_owner = GetClassLinker()->GetClassesLockOwner();
684 pid_t dex_lock_owner = GetClassLinker()->GetDexLockOwner();
685 if ((heap_lock_owner | thread_list_lock_owner | classes_lock_owner | dex_lock_owner) != 0) {
686 os << "Heap lock owner tid: " << heap_lock_owner << "\n"
687 << "ThreadList lock owner tid: " << thread_list_lock_owner << "\n"
688 << "ClassLinker classes lock owner tid: " << classes_lock_owner << "\n"
689 << "ClassLinker dex lock owner tid: " << dex_lock_owner << "\n";
690 }
691}
692
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700693void Runtime::SetStatsEnabled(bool new_state) {
694 if (new_state == true) {
695 GetStats()->Clear(~0);
696 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
697 Thread::Current()->GetStats()->Clear(~0);
698 }
699 stats_enabled_ = new_state;
700}
701
702void Runtime::ResetStats(int kinds) {
703 GetStats()->Clear(kinds & 0xffff);
704 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
705 Thread::Current()->GetStats()->Clear(kinds >> 16);
706}
707
708RuntimeStats* Runtime::GetStats() {
709 return &stats_;
710}
711
712int32_t Runtime::GetStat(int kind) {
713 RuntimeStats* stats;
714 if (kind < (1<<16)) {
715 stats = GetStats();
716 } else {
717 stats = Thread::Current()->GetStats();
718 kind >>= 16;
719 }
720 switch (kind) {
721 case KIND_ALLOCATED_OBJECTS:
722 return stats->allocated_objects;
723 case KIND_ALLOCATED_BYTES:
724 return stats->allocated_bytes;
725 case KIND_FREED_OBJECTS:
726 return stats->freed_objects;
727 case KIND_FREED_BYTES:
728 return stats->freed_bytes;
729 case KIND_GC_INVOCATIONS:
730 return stats->gc_for_alloc_count;
731 case KIND_CLASS_INIT_COUNT:
732 return stats->class_init_count;
733 case KIND_CLASS_INIT_TIME:
734 // Convert ns to us, reduce to 32 bits.
735 return (int) (stats->class_init_time_ns / 1000);
736 case KIND_EXT_ALLOCATED_OBJECTS:
737 case KIND_EXT_ALLOCATED_BYTES:
738 case KIND_EXT_FREED_OBJECTS:
739 case KIND_EXT_FREED_BYTES:
740 return 0; // backward compatibility
741 default:
742 CHECK(false);
743 return -1; // unreachable
744 }
745}
746
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700747void Runtime::BlockSignals() {
748 sigset_t sigset;
749 if (sigemptyset(&sigset) == -1) {
750 PLOG(FATAL) << "sigemptyset failed";
751 }
752 if (sigaddset(&sigset, SIGPIPE) == -1) {
753 PLOG(ERROR) << "sigaddset SIGPIPE failed";
754 }
755 // SIGQUIT is used to dump the runtime's state (including stack traces).
756 if (sigaddset(&sigset, SIGQUIT) == -1) {
757 PLOG(ERROR) << "sigaddset SIGQUIT failed";
758 }
759 // SIGUSR1 is used to initiate a heap dump.
760 if (sigaddset(&sigset, SIGUSR1) == -1) {
761 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
762 }
763 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
764}
765
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700766void Runtime::AttachCurrentThread(const char* name, bool as_daemon) {
767 Thread::Attach(instance_, name, as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700768}
769
Elliott Hughesd92bec42011-09-02 17:04:36 -0700770void Runtime::DetachCurrentThread() {
Elliott Hughes038a8062011-09-18 14:12:41 -0700771 // TODO: check we're not calling DetachCurrentThread from a call stack that
772 // includes managed frames. (It's only valid if the stack is all-native.)
Elliott Hughes02b48d12011-09-07 17:15:51 -0700773 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700774}
775
Brian Carlstrome24fa612011-09-29 00:53:55 -0700776void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700777 Dbg::VisitRoots(visitor, arg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700778 class_linker_->VisitRoots(visitor, arg);
779 intern_table_->VisitRoots(visitor, arg);
780 java_vm_->VisitRoots(visitor, arg);
781 thread_list_->VisitRoots(visitor, arg);
782 visitor(jni_stub_array_, arg);
783 visitor(abstract_method_error_stub_array_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700784 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
785 visitor(resolution_stub_array_[i], arg);
786 }
787 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
788 visitor(callee_save_method_[i], arg);
789 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700790}
791
Ian Rogers169c9a72011-11-13 20:13:17 -0800792bool Runtime::HasJniDlsymLookupStub() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700793 return jni_stub_array_ != NULL;
794}
795
Ian Rogers169c9a72011-11-13 20:13:17 -0800796ByteArray* Runtime::GetJniDlsymLookupStub() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700797 CHECK(jni_stub_array_ != NULL);
798 return jni_stub_array_;
799}
800
Ian Rogers169c9a72011-11-13 20:13:17 -0800801void Runtime::SetJniDlsymLookupStub(ByteArray* jni_stub_array) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700802 CHECK(jni_stub_array != NULL) << " jni_stub_array=" << jni_stub_array;
803 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array)
804 << "jni_stub_array_=" << jni_stub_array_ << " jni_stub_array=" << jni_stub_array;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700805 jni_stub_array_ = jni_stub_array;
806}
807
808bool Runtime::HasAbstractMethodErrorStubArray() const {
809 return abstract_method_error_stub_array_ != NULL;
810}
811
812ByteArray* Runtime::GetAbstractMethodErrorStubArray() const {
813 CHECK(abstract_method_error_stub_array_ != NULL);
814 return abstract_method_error_stub_array_;
815}
816
817void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) {
818 CHECK(abstract_method_error_stub_array != NULL);
819 CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array);
820 abstract_method_error_stub_array_ = abstract_method_error_stub_array;
821}
822
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700823
824Runtime::TrampolineType Runtime::GetTrampolineType(Method* method) {
825 if (method == NULL) {
826 return Runtime::kUnknownMethod;
827 } else if (method->IsStatic()) {
828 return Runtime::kStaticMethod;
829 } else {
830 return Runtime::kInstanceMethod;
831 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700832}
833
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700834bool Runtime::HasResolutionStubArray(TrampolineType type) const {
835 return resolution_stub_array_[type] != NULL;
Ian Rogersad25ac52011-10-04 19:13:33 -0700836}
837
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700838ByteArray* Runtime::GetResolutionStubArray(TrampolineType type) const {
839 CHECK(HasResolutionStubArray(type));
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700840 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastTrampolineMethodType));
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700841 return resolution_stub_array_[type];
842}
843
844void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700845 CHECK(resolution_stub_array != NULL);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700846 CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array);
847 resolution_stub_array_[type] = resolution_stub_array;
Ian Rogersad25ac52011-10-04 19:13:33 -0700848}
849
Brian Carlstromae826982011-11-09 01:33:42 -0800850Method* Runtime::CreateCalleeSaveMethod(InstructionSet instruction_set, CalleeSaveType type) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700851 Class* method_class = Method::GetMethodClass();
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700852 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
Ian Rogersff1ed472011-09-20 13:46:24 -0700853 method->SetDeclaringClass(method_class);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800854 // TODO: use a special method for callee saves
855 method->SetMethodIndex(DexFile::kDexNoIndex16);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700856 method->SetCode(NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800857 if ((instruction_set == kThumb2) || (instruction_set == kArm)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700858 uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6) | (1 << art::arm::R7) |
859 (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11);
860 uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3);
861 uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9);
862 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills :0) |
863 (type == kSaveAll ? all_spills :0) | (1 << art::arm::LR);
864 uint32_t fp_all_spills = (1 << art::arm::S0) | (1 << art::arm::S1) | (1 << art::arm::S2) |
865 (1 << art::arm::S3) | (1 << art::arm::S4) | (1 << art::arm::S5) |
866 (1 << art::arm::S6) | (1 << art::arm::S7) | (1 << art::arm::S8) |
867 (1 << art::arm::S9) | (1 << art::arm::S10) | (1 << art::arm::S11) |
868 (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) |
869 (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) |
870 (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) |
871 (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) |
872 (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) |
873 (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
874 (1 << art::arm::S30) | (1 << art::arm::S31);
875 uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
876 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
877 __builtin_popcount(fp_spills) /* fprs */ +
878 1 /* Method* */) * kPointerSize, kStackAlignment);
Ian Rogers15fdb8c2011-09-25 15:45:07 -0700879 method->SetFrameSizeInBytes(frame_size);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700880 method->SetCoreSpillMask(core_spills);
881 method->SetFpSpillMask(fp_spills);
Brian Carlstromae826982011-11-09 01:33:42 -0800882 } else if (instruction_set == kX86) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700883 method->SetFrameSizeInBytes(32);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700884 method->SetCoreSpillMask((1 << art::x86::EBX) | (1 << art::x86::EBP) | (1 << art::x86::ESI) |
Ian Rogersff1ed472011-09-20 13:46:24 -0700885 (1 << art::x86::EDI));
886 method->SetFpSpillMask(0);
887 } else {
888 UNIMPLEMENTED(FATAL);
889 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700890 return method.get();
Ian Rogersff1ed472011-09-20 13:46:24 -0700891}
892
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700893bool Runtime::HasCalleeSaveMethod(CalleeSaveType type) const {
894 return callee_save_method_[type] != NULL;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700895}
896
Brian Carlstrome24fa612011-09-29 00:53:55 -0700897// Returns a special method that describes all callee saves being spilled to the stack.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700898Method* Runtime::GetCalleeSaveMethod(CalleeSaveType type) const {
899 CHECK(HasCalleeSaveMethod(type));
900 return callee_save_method_[type];
Brian Carlstrome24fa612011-09-29 00:53:55 -0700901}
902
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700903void Runtime::SetCalleeSaveMethod(Method* method, CalleeSaveType type) {
904 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
905 callee_save_method_[type] = method;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700906}
907
jeffhao2692b572011-12-16 15:42:28 -0800908void Runtime::EnableMethodTracing(Trace* tracer) {
909 CHECK(!IsMethodTracingActive());
910 tracer_ = tracer;
911}
912
913void Runtime::DisableMethodTracing() {
914 CHECK(IsMethodTracingActive());
915 delete tracer_;
916 tracer_ = NULL;
917}
918
919bool Runtime::IsMethodTracingActive() const {
920 return (tracer_ != NULL);
921}
922
923Trace* Runtime::GetTracer() const {
924 CHECK(IsMethodTracingActive());
925 return tracer_;
926}
927
Carl Shapiro1fb86202011-06-27 17:43:13 -0700928} // namespace art