blob: 22f43297202fc0d28d97e8734dbb3e31a7d4d8e3 [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 Hughesd9c67be2012-02-02 19:54:06 -080053 : is_compiler_(false),
54 is_zygote_(false),
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -070055 default_stack_size_(Thread::kDefaultStackSize),
Elliott Hughesc33a32b2011-10-11 18:18:07 -070056 monitor_list_(NULL),
Elliott Hughesdcc24742011-09-07 14:02:44 -070057 thread_list_(NULL),
58 intern_table_(NULL),
59 class_linker_(NULL),
60 signal_catcher_(NULL),
61 java_vm_(NULL),
Brian Carlstrom16192862011-09-12 17:50:06 -070062 jni_stub_array_(NULL),
Brian Carlstrome24fa612011-09-29 00:53:55 -070063 abstract_method_error_stub_array_(NULL),
Ian Rogers19846512012-02-24 11:42:47 -080064 resolution_method_(NULL),
Ian Rogers7c3757f2012-02-15 17:18:53 -080065 system_class_loader_(NULL),
Elliott Hughes6b355752012-01-13 16:49:08 -080066 shutting_down_(false),
Elliott Hughesdcc24742011-09-07 14:02:44 -070067 started_(false),
68 vfprintf_(NULL),
69 exit_(NULL),
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070070 abort_(NULL),
jeffhao2692b572011-12-16 15:42:28 -080071 stats_enabled_(false),
72 tracer_(NULL) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -070073 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
74 resolution_stub_array_[i] = NULL;
75 }
76 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
77 callee_save_method_[i] = NULL;
78 }
Elliott Hughesdcc24742011-09-07 14:02:44 -070079}
80
Carl Shapiro61e019d2011-07-14 16:53:09 -070081Runtime::~Runtime() {
Elliott Hughes6b355752012-01-13 16:49:08 -080082 shutting_down_ = true;
83
Elliott Hughesd1cc8362011-10-24 16:58:50 -070084 Dbg::StopJdwp();
85
Elliott Hughes5fe594f2011-09-08 12:33:17 -070086 // Make sure our internal threads are dead before we start tearing down things they're using.
87 delete signal_catcher_;
Elliott Hughes038a8062011-09-18 14:12:41 -070088 // TODO: GC thread.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070089
Elliott Hughes038a8062011-09-18 14:12:41 -070090 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
Elliott Hughes93e74e82011-09-13 11:07:03 -070091 delete thread_list_;
Elliott Hughesc33a32b2011-10-11 18:18:07 -070092 delete monitor_list_;
Elliott Hughes93e74e82011-09-13 11:07:03 -070093
Carl Shapiro61e019d2011-07-14 16:53:09 -070094 delete class_linker_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070095 Heap::Destroy();
Elliott Hughes4d6850c2012-01-18 15:55:06 -080096 verifier::DexVerifier::DeleteGcMaps();
Elliott Hughescf4c6c42011-09-01 15:16:42 -070097 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070098 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -070099 Thread::Shutdown();
Carl Shapiro4acf4642011-07-26 18:54:13 -0700100 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700101 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -0700102 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700103}
104
Elliott Hughescac6cc72011-11-03 20:31:21 -0700105static bool gAborting = false;
106
Elliott Hughese0918552011-10-28 17:18:29 -0700107struct AbortState {
108 void Dump(std::ostream& os) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700109 if (gAborting) {
110 os << "Runtime aborting --- recursively, so no thread-specific detail!\n";
111 return;
112 }
113 gAborting = true;
Elliott Hughese0918552011-10-28 17:18:29 -0700114 os << "Runtime aborting...\n";
Elliott Hughes6c7d2442012-02-01 18:40:47 -0800115 if (Runtime::Current() == NULL) {
116 os << "(Runtime does not yet exist!)\n";
117 return;
118 }
Elliott Hughese0918552011-10-28 17:18:29 -0700119 Thread* self = Thread::Current();
120 if (self == NULL) {
121 os << "(Aborting thread was not attached to runtime!)\n";
122 } else {
Elliott Hughes899e7892012-01-24 14:57:32 -0800123 self->Dump(os);
124 if (self->IsExceptionPending()) {
125 os << "Pending " << PrettyTypeOf(self->GetException()) << " on thread:\n"
126 << self->GetException()->Dump();
127 }
Elliott Hughese0918552011-10-28 17:18:29 -0700128 }
129 }
130};
131
Elliott Hughesffe67362011-07-17 12:09:27 -0700132void Runtime::Abort(const char* file, int line) {
Elliott Hughes131aef82012-01-27 11:53:35 -0800133 // Ensure that we don't have multiple threads trying to abort at once,
134 // which would result in significantly worse diagnostics.
135 MutexLock mu(abort_lock_);
136
Elliott Hughesffe67362011-07-17 12:09:27 -0700137 // Get any pending output out of the way.
138 fflush(NULL);
139
140 // Many people have difficulty distinguish aborts from crashes,
141 // so be explicit.
Elliott Hughese0918552011-10-28 17:18:29 -0700142 AbortState state;
Elliott Hughes9ee5f9c2012-02-03 18:33:16 -0800143 LOG(INTERNAL_FATAL) << Dumpable<AbortState>(state);
Elliott Hughesffe67362011-07-17 12:09:27 -0700144
Elliott Hughesffe67362011-07-17 12:09:27 -0700145 // Perform any platform-specific pre-abort actions.
146 PlatformAbort(file, line);
147
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700148 // use abort hook if we have one
149 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
150 Runtime::Current()->abort_();
151 // notreached
152 }
153
Elliott Hughesffe67362011-07-17 12:09:27 -0700154 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -0700155 // receive SIGABRT. debuggerd dumps the stack trace of the main
156 // thread, whether or not that was the thread that failed. By
157 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -0700158 // fault in the current thread, and get a useful log from debuggerd.
159 // We can also trivially tell the difference between a VM crash and
160 // a deliberate abort by looking at the fault address.
161 *reinterpret_cast<char*>(0xdeadd00d) = 38;
162 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -0700163 // notreached
164}
165
Elliott Hughesbf86d042011-08-31 17:53:14 -0700166void Runtime::CallExitHook(jint status) {
167 if (exit_ != NULL) {
168 ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative);
169 exit_(status);
170 LOG(WARNING) << "Exit hook returned instead of exiting!";
171 }
172}
173
Brian Carlstrom8a436592011-08-15 21:27:23 -0700174// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
175// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
176// [gG] gigabytes.
177//
178// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700179// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
180// of 1024.
181//
182// The spec says the -Xmx and -Xms options must be multiples of 1024. It
183// doesn't say anything about -Xss.
184//
185// Returns 0 (a useless size) if "s" is malformed or specifies a low or
186// non-evenly-divisible value.
187//
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800188size_t ParseMemoryOption(const char* s, size_t div) {
Brian Carlstrom8a436592011-08-15 21:27:23 -0700189 // strtoul accepts a leading [+-], which we don't want,
190 // so make sure our string starts with a decimal digit.
191 if (isdigit(*s)) {
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800192 const char* s2;
193 size_t val = strtoul(s, (char**)&s2, 10);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700194 if (s2 != s) {
195 // s2 should be pointing just after the number.
196 // If this is the end of the string, the user
197 // has specified a number of bytes. Otherwise,
198 // there should be exactly one more character
199 // that specifies a multiplier.
200 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700201 // The remainder of the string is either a single multiplier
202 // character, or nothing to indicate that the value is in
203 // bytes.
204 char c = *s2++;
205 if (*s2 == '\0') {
206 size_t mul;
207 if (c == '\0') {
208 mul = 1;
209 } else if (c == 'k' || c == 'K') {
210 mul = KB;
211 } else if (c == 'm' || c == 'M') {
212 mul = MB;
213 } else if (c == 'g' || c == 'G') {
214 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700215 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700216 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700217 return 0;
218 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700219
220 if (val <= std::numeric_limits<size_t>::max() / mul) {
221 val *= mul;
222 } else {
223 // Clamp to a multiple of 1024.
224 val = std::numeric_limits<size_t>::max() & ~(1024-1);
225 }
226 } else {
227 // There's more than one character after the numeric part.
228 return 0;
229 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700230 }
231 // The man page says that a -Xm value must be a multiple of 1024.
232 if (val % div == 0) {
233 return val;
234 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700235 }
236 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700237 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700238}
239
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800240size_t ParseIntegerOrDie(const std::string& s) {
241 std::string::size_type colon = s.find(':');
242 if (colon == std::string::npos) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700243 LOG(FATAL) << "Missing integer: " << s;
244 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800245 const char* begin = &s[colon + 1];
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700246 char* end;
247 size_t result = strtoul(begin, &end, 10);
248 if (begin == end || *end != '\0') {
249 LOG(FATAL) << "Failed to parse integer in: " << s;
250 }
251 return result;
252}
253
Elliott Hughes0af55432011-08-17 18:37:28 -0700254void LoadJniLibrary(JavaVMExt* vm, const char* name) {
Elliott Hughes6cc332e2012-01-20 22:59:20 -0800255 std::string mapped_name(StringPrintf(OS_SHARED_LIB_FORMAT_STR, name));
Elliott Hughes75770752011-08-24 17:52:38 -0700256 std::string reason;
257 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700258 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
259 << reason;
260 }
261}
262
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700263Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700264 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstroma004aa92012-02-08 18:05:09 -0800265 const char* boot_class_path_string = getenv("BOOTCLASSPATH");
266 if (boot_class_path_string != NULL) {
267 parsed->boot_class_path_string_ = boot_class_path_string;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700268 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800269 const char* class_path_string = getenv("CLASSPATH");
270 if (class_path_string != NULL) {
271 parsed->class_path_string_ = class_path_string;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700272 }
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700273#ifdef NDEBUG
Elliott Hughes5174fe62011-08-23 15:12:35 -0700274 // -Xcheck:jni is off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700275 parsed->check_jni_ = false;
276#else
277 // ...but on by default in debug builds.
278 parsed->check_jni_ = true;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700279#endif
Elliott Hughes85d15452011-09-16 17:33:01 -0700280
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700281 parsed->heap_initial_size_ = Heap::kInitialSize;
282 parsed->heap_maximum_size_ = Heap::kMaximumSize;
jeffhaoc1160702011-10-27 15:48:45 -0700283 parsed->heap_growth_limit_ = 0; // 0 means no growth limit
Brian Carlstromf734cf52011-08-17 16:28:14 -0700284 parsed->stack_size_ = Thread::kDefaultStackSize;
285
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800286 parsed->is_compiler_ = false;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700287 parsed->is_zygote_ = false;
288
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700289 parsed->jni_globals_max_ = 0;
Elliott Hughesfc861622011-10-17 17:57:47 -0700290 parsed->lock_profiling_threshold_ = 0;
291 parsed->hook_is_sensitive_thread_ = NULL;
292
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700293 parsed->hook_vfprintf_ = vfprintf;
294 parsed->hook_exit_ = exit;
295 parsed->hook_abort_ = abort;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700296
297 for (size_t i = 0; i < options.size(); ++i) {
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800298 const std::string option(options[i].first);
Brian Carlstrom0796af02011-10-12 14:31:45 -0700299 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700300 LOG(INFO) << "option[" << i << "]=" << option;
301 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800302 if (StartsWith(option, "-Xbootclasspath:")) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800303 parsed->boot_class_path_string_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700304 } else if (option == "-classpath" || option == "-cp") {
305 // TODO: support -Djava.class.path
306 i++;
307 if (i == options.size()) {
308 // TODO: usage
309 LOG(FATAL) << "Missing required class path value for " << option;
310 return NULL;
311 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800312 const StringPiece& value = options[i].first;
313 parsed->class_path_string_ = value.data();
314 } else if (option == "bootclasspath") {
315 parsed->boot_class_path_
316 = reinterpret_cast<const std::vector<const DexFile*>*>(options[i].second);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800317 } else if (StartsWith(option, "-Ximage:")) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800318 parsed->image_ = option.substr(strlen("-Ximage:")).data();
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800319 } else if (StartsWith(option, "-Xcheck:jni")) {
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700320 parsed->check_jni_ = true;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800321 } else if (StartsWith(option, "-Xrunjdwp:") || StartsWith(option, "-agentlib:jdwp=")) {
322 std::string tail(option.substr(option[1] == 'X' ? 10 : 15));
Elliott Hughes3bb81562011-10-21 18:52:59 -0700323 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
324 LOG(FATAL) << "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
325 << "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n";
326 return NULL;
327 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800328 } else if (StartsWith(option, "-Xms")) {
329 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).c_str(), 1024);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700330 if (size == 0) {
331 if (ignore_unrecognized) {
332 continue;
333 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700334 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700335 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700336 return NULL;
337 }
338 parsed->heap_initial_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800339 } else if (StartsWith(option, "-Xmx")) {
340 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).c_str(), 1024);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700341 if (size == 0) {
342 if (ignore_unrecognized) {
343 continue;
344 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700345 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700346 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700347 return NULL;
348 }
349 parsed->heap_maximum_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800350 } else if (StartsWith(option, "-XX:HeapGrowthLimit=")) {
351 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).c_str(), 1024);
jeffhaoc1160702011-10-27 15:48:45 -0700352 if (size == 0) {
353 if (ignore_unrecognized) {
354 continue;
355 }
356 // TODO: usage
357 LOG(FATAL) << "Failed to parse " << option;
358 return NULL;
359 }
360 parsed->heap_growth_limit_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800361 } else if (StartsWith(option, "-Xss")) {
362 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).c_str(), 1);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700363 if (size == 0) {
364 if (ignore_unrecognized) {
365 continue;
366 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700367 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700368 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700369 return NULL;
370 }
371 parsed->stack_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800372 } else if (StartsWith(option, "-D")) {
373 parsed->properties_.push_back(option.substr(strlen("-D")));
374 } else if (StartsWith(option, "-Xjnitrace:")) {
375 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:"));
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700376 } else if (option == "compiler") {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800377 parsed->is_compiler_ = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700378 } else if (option == "-Xzygote") {
379 parsed->is_zygote_ = true;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800380 } else if (StartsWith(option, "-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700381 std::vector<std::string> verbose_options;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800382 Split(option.substr(strlen("-verbose:")), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700383 for (size_t i = 0; i < verbose_options.size(); ++i) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800384 if (verbose_options[i] == "class") {
385 gLogVerbosity.class_linker = true;
386 } else if (verbose_options[i] == "compiler") {
387 gLogVerbosity.compiler = true;
388 } else if (verbose_options[i] == "heap") {
389 gLogVerbosity.heap = true;
390 } else if (verbose_options[i] == "gc") {
391 gLogVerbosity.gc = true;
392 } else if (verbose_options[i] == "jdwp") {
393 gLogVerbosity.jdwp = true;
394 } else if (verbose_options[i] == "jni") {
395 gLogVerbosity.jni = true;
396 } else if (verbose_options[i] == "monitor") {
397 gLogVerbosity.monitor = true;
398 } else if (verbose_options[i] == "startup") {
399 gLogVerbosity.startup = true;
400 } else if (verbose_options[i] == "third-party-jni") {
401 gLogVerbosity.third_party_jni = true;
402 } else if (verbose_options[i] == "threads") {
403 gLogVerbosity.threads = true;
404 } else {
405 LOG(WARNING) << "Ignoring unknown -verbose option: " << verbose_options[i];
406 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700407 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800408 } else if (StartsWith(option, "-Xjnigreflimit:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700409 parsed->jni_globals_max_ = ParseIntegerOrDie(option);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800410 } else if (StartsWith(option, "-Xlockprofthreshold:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700411 parsed->lock_profiling_threshold_ = ParseIntegerOrDie(option);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800412 } else if (StartsWith(option, "-Xstacktracefile:")) {
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700413// always show stack traces in debug builds
414#ifdef NDEBUG
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800415 parsed->stack_trace_file_ = option.substr(strlen("-Xstacktracefile:"));
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700416#endif
Elliott Hughesfc861622011-10-17 17:57:47 -0700417 } else if (option == "sensitiveThread") {
418 parsed->hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(options[i].second);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700419 } else if (option == "vfprintf") {
420 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
421 } else if (option == "exit") {
422 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
423 } else if (option == "abort") {
424 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700425 } else if (option == "host-prefix") {
426 parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second);
Elliott Hughesb3e66df2012-01-12 14:49:18 -0800427 } else if (option == "-Xgenregmap" || option == "-Xgc:precise") {
428 // We silently ignore these for backwards compatibility.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700429 } else {
430 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700431 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700432 LOG(ERROR) << "Unrecognized option " << option;
433 // TODO: this should exit, but for now tolerate unknown options
434 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700435 }
436 }
437 }
438
Brian Carlstrom223f20f2012-02-04 23:06:55 -0800439 if (!parsed->is_compiler_ && parsed->image_.empty()) {
440 parsed->image_ += GetAndroidRoot();
441 parsed->image_ += "/framework/boot.art";
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700442 }
jeffhaoc1160702011-10-27 15:48:45 -0700443 if (parsed->heap_growth_limit_ == 0) {
444 parsed->heap_growth_limit_ = parsed->heap_maximum_size_;
445 }
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700446
Elliott Hughescac6cc72011-11-03 20:31:21 -0700447 LOG(INFO) << "Build type: "
448#ifndef NDEBUG
449 << "debug"
450#else
451 << "optimized"
452#endif
453 << "; CheckJNI: " << (parsed->check_jni_ ? "on" : "off");
Elliott Hughes85d15452011-09-16 17:33:01 -0700454
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700455 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700456}
457
458Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700459 // TODO: acquire a static mutex on Runtime to avoid racing.
460 if (Runtime::instance_ != NULL) {
461 return NULL;
462 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700463 instance_ = new Runtime;
464 if (!instance_->Init(options, ignore_unrecognized)) {
465 delete instance_;
466 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700467 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700468 return instance_;
469}
Elliott Hughes0af55432011-08-17 18:37:28 -0700470
Brian Carlstromaded5f72011-10-07 17:15:04 -0700471void CreateSystemClassLoader() {
472 if (ClassLoader::UseCompileTimeClassPath()) {
473 return;
474 }
475
476 Thread* self = Thread::Current();
477
478 // Must be in the kNative state for calling native methods.
479 CHECK_EQ(self->GetState(), Thread::kNative);
480
481 JNIEnv* env = self->GetJniEnv();
Brian Carlstromdf143242011-10-10 18:05:34 -0700482 ScopedLocalRef<jclass> ClassLoader_class(env, env->FindClass("java/lang/ClassLoader"));
483 CHECK(ClassLoader_class.get() != NULL);
484 jmethodID getSystemClassLoader = env->GetStaticMethodID(ClassLoader_class.get(),
485 "getSystemClassLoader",
486 "()Ljava/lang/ClassLoader;");
487 CHECK(getSystemClassLoader != NULL);
488 ScopedLocalRef<jobject> class_loader(env, env->CallStaticObjectMethod(ClassLoader_class.get(),
489 getSystemClassLoader));
490 CHECK(class_loader.get() != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700491
Brian Carlstromdf143242011-10-10 18:05:34 -0700492 Thread::Current()->SetClassLoaderOverride(Decode<ClassLoader*>(env, class_loader.get()));
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500493
494 ScopedLocalRef<jclass> Thread_class(env, env->FindClass("java/lang/Thread"));
495 CHECK(Thread_class.get() != NULL);
496 jfieldID contextClassLoader = env->GetFieldID(Thread_class.get(),
497 "contextClassLoader",
498 "Ljava/lang/ClassLoader;");
499 CHECK(contextClassLoader != NULL);
500 ScopedLocalRef<jobject> self_jobject(env, AddLocalReference<jobject>(env, self->GetPeer()));
501 env->SetObjectField(self_jobject.get(), contextClassLoader, class_loader.get());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700502}
503
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700504void Runtime::Start() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800505 VLOG(startup) << "Runtime::Start entering";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700506
507 CHECK(host_prefix_.empty()) << host_prefix_;
508
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700509 // Restore main thread state to kNative as expected by native code
510 Thread::Current()->SetState(Thread::kNative);
511
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700512 started_ = true;
513
Brian Carlstromae826982011-11-09 01:33:42 -0800514 // InitNativeMethods needs to be after started_ so that the classes
515 // it touches will have methods linked to the oat file if necessary.
516 InitNativeMethods();
517
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500518 Thread::FinishStartup();
519
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700520 if (!is_zygote_) {
521 DidForkFromZygote();
522 }
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700523
Elliott Hughes85d15452011-09-16 17:33:01 -0700524 StartDaemonThreads();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700525
Brian Carlstromaded5f72011-10-07 17:15:04 -0700526 CreateSystemClassLoader();
527
Elliott Hughes726079d2011-10-07 18:43:44 -0700528 Thread::Current()->GetJniEnv()->locals.AssertEmpty();
529
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800530 VLOG(startup) << "Runtime::Start exiting";
Elliott Hughes85d15452011-09-16 17:33:01 -0700531}
532
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700533void Runtime::DidForkFromZygote() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700534 is_zygote_ = false;
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700535
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700536 StartSignalCatcher();
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700537
538 // Start the JDWP thread. If the command-line debugger flags specified "suspend=y",
539 // this will pause the runtime, so we probably want this to come last.
540 Dbg::StartJdwp();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700541}
542
543void Runtime::StartSignalCatcher() {
544 if (!is_zygote_) {
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700545 signal_catcher_ = new SignalCatcher(stack_trace_file_);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700546 }
547}
548
Elliott Hughes85d15452011-09-16 17:33:01 -0700549void Runtime::StartDaemonThreads() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800550 VLOG(startup) << "Runtime::StartDaemonThreads entering";
Elliott Hughes85d15452011-09-16 17:33:01 -0700551
Elliott Hughes719b3232011-09-25 17:42:19 -0700552 Thread* self = Thread::Current();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700553
554 // Must be in the kNative state for calling native methods.
555 CHECK_EQ(self->GetState(), Thread::kNative);
556
Elliott Hughes719b3232011-09-25 17:42:19 -0700557 JNIEnv* env = self->GetJniEnv();
Elliott Hughes726079d2011-10-07 18:43:44 -0700558 ScopedLocalRef<jclass> c(env, env->FindClass("java/lang/Daemons"));
559 CHECK(c.get() != NULL);
560 jmethodID mid = env->GetStaticMethodID(c.get(), "start", "()V");
Elliott Hughes719b3232011-09-25 17:42:19 -0700561 CHECK(mid != NULL);
Elliott Hughes726079d2011-10-07 18:43:44 -0700562 env->CallStaticVoidMethod(c.get(), mid);
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800563 CHECK(!env->ExceptionCheck());
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700564
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800565 VLOG(startup) << "Runtime::StartDaemonThreads exiting";
Carl Shapiro61e019d2011-07-14 16:53:09 -0700566}
567
Elliott Hughes6b355752012-01-13 16:49:08 -0800568bool Runtime::IsShuttingDown() const {
569 return shutting_down_;
570}
571
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700572bool Runtime::IsStarted() const {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700573 return started_;
574}
575
Elliott Hughes0af55432011-08-17 18:37:28 -0700576bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700577 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700578
Elliott Hughes90a33692011-08-30 13:27:07 -0700579 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
580 if (options.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700581 LOG(ERROR) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700582 return false;
583 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800584 VLOG(startup) << "Runtime::Init -verbose:startup enabled";
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700585
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700586 SetJniGlobalsMax(options->jni_globals_max_);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800587 Monitor::Init(options->lock_profiling_threshold_, options->hook_is_sensitive_thread_);
Elliott Hughes32d6e1e2011-10-11 14:47:44 -0700588
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700589 host_prefix_ = options->host_prefix_;
Brian Carlstroma004aa92012-02-08 18:05:09 -0800590 boot_class_path_string_ = options->boot_class_path_string_;
591 class_path_string_ = options->class_path_string_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700592 properties_ = options->properties_;
593
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800594 is_compiler_ = options->is_compiler_;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700595 is_zygote_ = options->is_zygote_;
596
Elliott Hughes0af55432011-08-17 18:37:28 -0700597 vfprintf_ = options->hook_vfprintf_;
598 exit_ = options->hook_exit_;
599 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700600
Elliott Hughesbe759c62011-09-08 19:38:21 -0700601 default_stack_size_ = options->stack_size_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700602 stack_trace_file_ = options->stack_trace_file_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700603
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700604 monitor_list_ = new MonitorList;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800605 thread_list_ = new ThreadList;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700606 intern_table_ = new InternTable;
607
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800608 Heap::Init(options->heap_initial_size_,
jeffhaoc1160702011-10-27 15:48:45 -0700609 options->heap_growth_limit_,
Ian Rogers30fab402012-01-23 15:43:46 -0800610 options->heap_maximum_size_,
Brian Carlstrom223f20f2012-02-04 23:06:55 -0800611 options->image_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700612
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700613 BlockSignals();
614
Elliott Hughesa0957642011-09-02 14:27:33 -0700615 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700616
Elliott Hughesbe759c62011-09-08 19:38:21 -0700617 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700618
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700619 // ClassLinker needs an attached thread, but we can't fully attach a thread
620 // without creating objects.
621 Thread::Attach(this, "main", false);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700622
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700623 // Set us to runnable so tools using a runtime can allocate and GC by default
624 Thread::Current()->SetState(Thread::kRunnable);
625
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700626 CHECK_GE(Heap::GetSpaces().size(), 1U);
Brian Carlstroma004aa92012-02-08 18:05:09 -0800627 if (Heap::GetSpaces()[0]->IsImageSpace()) {
628 class_linker_ = ClassLinker::CreateFromImage(intern_table_);
629 } else {
630 CHECK(options->boot_class_path_ != NULL);
631 CHECK_NE(options->boot_class_path_->size(), 0U);
632 class_linker_ = ClassLinker::CreateFromCompiler(*options->boot_class_path_, intern_table_);
633 }
634 CHECK(class_linker_ != NULL);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700635
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800636 VLOG(startup) << "Runtime::Init exiting";
Carl Shapiro1fb86202011-06-27 17:43:13 -0700637 return true;
638}
639
Elliott Hughes038a8062011-09-18 14:12:41 -0700640void Runtime::InitNativeMethods() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800641 VLOG(startup) << "Runtime::InitNativeMethods entering";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700642 Thread* self = Thread::Current();
643 JNIEnv* env = self->GetJniEnv();
644
Elliott Hughes418d20f2011-09-22 14:00:39 -0700645 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Brian Carlstromaded5f72011-10-07 17:15:04 -0700646 CHECK_EQ(self->GetState(), Thread::kNative);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700647
Elliott Hughes418d20f2011-09-22 14:00:39 -0700648 // First set up JniConstants, which is used by both the runtime's built-in native
649 // methods and libcore.
Elliott Hughesfea966e2011-09-22 10:26:20 -0700650 JniConstants::init(env);
651
Elliott Hughes418d20f2011-09-22 14:00:39 -0700652 // Then set up the native methods provided by the runtime itself.
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700653 RegisterRuntimeNativeMethods(env);
654
Elliott Hughes418d20f2011-09-22 14:00:39 -0700655 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
656 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
657 // the library that implements System.loadLibrary!
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700658 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800659 VLOG(startup) << "Runtime::InitNativeMethods exiting";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700660}
661
662void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
663#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Ian Rogers5167c972012-02-03 10:41:20 -0800664 // Register Throwable first so that registration of other native methods can throw exceptions
665 REGISTER(register_java_lang_Throwable);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700666 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700667 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700668 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700669 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700670 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700671 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700672 REGISTER(register_java_lang_Object);
673 REGISTER(register_java_lang_Runtime);
674 REGISTER(register_java_lang_String);
675 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700676 REGISTER(register_java_lang_Thread);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700677 REGISTER(register_java_lang_VMClassLoader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700678 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700679 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700680 REGISTER(register_java_lang_reflect_Field);
681 REGISTER(register_java_lang_reflect_Method);
Jesse Wilson95caa792011-10-12 18:14:17 -0400682 REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700683 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700684 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700685 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700686 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700687#undef REGISTER
688}
689
Elliott Hughes8daa0922011-09-11 13:46:25 -0700690void Runtime::Dump(std::ostream& os) {
Elliott Hughese27955c2011-08-26 15:21:24 -0700691 // TODO: dump other runtime statistics?
Elliott Hughescac6cc72011-11-03 20:31:21 -0700692 GetClassLinker()->DumpForSigQuit(os);
693 GetInternTable()->DumpForSigQuit(os);
Elliott Hughes42ee1422011-09-06 12:33:32 -0700694 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700695
696 thread_list_->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700697}
698
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800699void Runtime::DumpLockHolders(std::ostream& os) {
700 pid_t heap_lock_owner = Heap::GetLockOwner();
701 pid_t thread_list_lock_owner = GetThreadList()->GetLockOwner();
702 pid_t classes_lock_owner = GetClassLinker()->GetClassesLockOwner();
703 pid_t dex_lock_owner = GetClassLinker()->GetDexLockOwner();
704 if ((heap_lock_owner | thread_list_lock_owner | classes_lock_owner | dex_lock_owner) != 0) {
705 os << "Heap lock owner tid: " << heap_lock_owner << "\n"
706 << "ThreadList lock owner tid: " << thread_list_lock_owner << "\n"
707 << "ClassLinker classes lock owner tid: " << classes_lock_owner << "\n"
708 << "ClassLinker dex lock owner tid: " << dex_lock_owner << "\n";
709 }
710}
711
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700712void Runtime::SetStatsEnabled(bool new_state) {
713 if (new_state == true) {
714 GetStats()->Clear(~0);
715 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
716 Thread::Current()->GetStats()->Clear(~0);
717 }
718 stats_enabled_ = new_state;
719}
720
721void Runtime::ResetStats(int kinds) {
722 GetStats()->Clear(kinds & 0xffff);
723 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
724 Thread::Current()->GetStats()->Clear(kinds >> 16);
725}
726
727RuntimeStats* Runtime::GetStats() {
728 return &stats_;
729}
730
731int32_t Runtime::GetStat(int kind) {
732 RuntimeStats* stats;
733 if (kind < (1<<16)) {
734 stats = GetStats();
735 } else {
736 stats = Thread::Current()->GetStats();
737 kind >>= 16;
738 }
739 switch (kind) {
740 case KIND_ALLOCATED_OBJECTS:
741 return stats->allocated_objects;
742 case KIND_ALLOCATED_BYTES:
743 return stats->allocated_bytes;
744 case KIND_FREED_OBJECTS:
745 return stats->freed_objects;
746 case KIND_FREED_BYTES:
747 return stats->freed_bytes;
748 case KIND_GC_INVOCATIONS:
749 return stats->gc_for_alloc_count;
750 case KIND_CLASS_INIT_COUNT:
751 return stats->class_init_count;
752 case KIND_CLASS_INIT_TIME:
753 // Convert ns to us, reduce to 32 bits.
754 return (int) (stats->class_init_time_ns / 1000);
755 case KIND_EXT_ALLOCATED_OBJECTS:
756 case KIND_EXT_ALLOCATED_BYTES:
757 case KIND_EXT_FREED_OBJECTS:
758 case KIND_EXT_FREED_BYTES:
759 return 0; // backward compatibility
760 default:
761 CHECK(false);
762 return -1; // unreachable
763 }
764}
765
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700766void Runtime::BlockSignals() {
767 sigset_t sigset;
768 if (sigemptyset(&sigset) == -1) {
769 PLOG(FATAL) << "sigemptyset failed";
770 }
771 if (sigaddset(&sigset, SIGPIPE) == -1) {
772 PLOG(ERROR) << "sigaddset SIGPIPE failed";
773 }
774 // SIGQUIT is used to dump the runtime's state (including stack traces).
775 if (sigaddset(&sigset, SIGQUIT) == -1) {
776 PLOG(ERROR) << "sigaddset SIGQUIT failed";
777 }
778 // SIGUSR1 is used to initiate a heap dump.
779 if (sigaddset(&sigset, SIGUSR1) == -1) {
780 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
781 }
782 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
783}
784
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700785void Runtime::AttachCurrentThread(const char* name, bool as_daemon) {
786 Thread::Attach(instance_, name, as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700787}
788
Elliott Hughesd92bec42011-09-02 17:04:36 -0700789void Runtime::DetachCurrentThread() {
Elliott Hughes038a8062011-09-18 14:12:41 -0700790 // TODO: check we're not calling DetachCurrentThread from a call stack that
791 // includes managed frames. (It's only valid if the stack is all-native.)
Elliott Hughes02b48d12011-09-07 17:15:51 -0700792 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700793}
794
Brian Carlstrome24fa612011-09-29 00:53:55 -0700795void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700796 Dbg::VisitRoots(visitor, arg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700797 class_linker_->VisitRoots(visitor, arg);
798 intern_table_->VisitRoots(visitor, arg);
799 java_vm_->VisitRoots(visitor, arg);
800 thread_list_->VisitRoots(visitor, arg);
801 visitor(jni_stub_array_, arg);
802 visitor(abstract_method_error_stub_array_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700803 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
804 visitor(resolution_stub_array_[i], arg);
805 }
Ian Rogers19846512012-02-24 11:42:47 -0800806 visitor(resolution_method_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700807 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
808 visitor(callee_save_method_[i], arg);
809 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700810}
811
Ian Rogers169c9a72011-11-13 20:13:17 -0800812bool Runtime::HasJniDlsymLookupStub() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700813 return jni_stub_array_ != NULL;
814}
815
Ian Rogers169c9a72011-11-13 20:13:17 -0800816ByteArray* Runtime::GetJniDlsymLookupStub() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700817 CHECK(jni_stub_array_ != NULL);
818 return jni_stub_array_;
819}
820
Ian Rogers169c9a72011-11-13 20:13:17 -0800821void Runtime::SetJniDlsymLookupStub(ByteArray* jni_stub_array) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700822 CHECK(jni_stub_array != NULL) << " jni_stub_array=" << jni_stub_array;
823 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array)
824 << "jni_stub_array_=" << jni_stub_array_ << " jni_stub_array=" << jni_stub_array;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700825 jni_stub_array_ = jni_stub_array;
826}
827
828bool Runtime::HasAbstractMethodErrorStubArray() const {
829 return abstract_method_error_stub_array_ != NULL;
830}
831
832ByteArray* Runtime::GetAbstractMethodErrorStubArray() const {
833 CHECK(abstract_method_error_stub_array_ != NULL);
834 return abstract_method_error_stub_array_;
835}
836
837void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) {
838 CHECK(abstract_method_error_stub_array != NULL);
839 CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array);
840 abstract_method_error_stub_array_ = abstract_method_error_stub_array;
841}
842
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700843
844Runtime::TrampolineType Runtime::GetTrampolineType(Method* method) {
845 if (method == NULL) {
846 return Runtime::kUnknownMethod;
847 } else if (method->IsStatic()) {
848 return Runtime::kStaticMethod;
849 } else {
Ian Rogersfb6adba2012-03-04 21:51:51 -0800850 return Runtime::kUnknownMethod;
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700851 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700852}
853
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700854bool Runtime::HasResolutionStubArray(TrampolineType type) const {
855 return resolution_stub_array_[type] != NULL;
Ian Rogersad25ac52011-10-04 19:13:33 -0700856}
857
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700858ByteArray* Runtime::GetResolutionStubArray(TrampolineType type) const {
859 CHECK(HasResolutionStubArray(type));
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700860 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastTrampolineMethodType));
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700861 return resolution_stub_array_[type];
862}
863
864void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700865 CHECK(resolution_stub_array != NULL);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700866 CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array);
867 resolution_stub_array_[type] = resolution_stub_array;
Ian Rogersad25ac52011-10-04 19:13:33 -0700868}
869
Ian Rogers19846512012-02-24 11:42:47 -0800870Method* Runtime::CreateResolutionMethod() {
871 Class* method_class = Method::GetMethodClass();
872 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
873 method->SetDeclaringClass(method_class);
874 // TODO: use a special method for resolution method saves
875 method->SetDexMethodIndex(DexFile::kDexNoIndex16);
876 ByteArray* unknown_resolution_stub = GetResolutionStubArray(kUnknownMethod);
877 CHECK(unknown_resolution_stub != NULL);
878 method->SetCode(unknown_resolution_stub->GetData());
879 return method.get();
880}
881
882bool Runtime::HasResolutionMethod() const {
883 return resolution_method_ != NULL;
884}
885
886// Returns a special method that calls into a trampoline for runtime method resolution
887Method* Runtime::GetResolutionMethod() const {
888 CHECK(HasResolutionMethod());
889 return resolution_method_;
890}
891
892void Runtime::SetResolutionMethod(Method* method) {
893 resolution_method_ = method;
894}
895
Brian Carlstromae826982011-11-09 01:33:42 -0800896Method* Runtime::CreateCalleeSaveMethod(InstructionSet instruction_set, CalleeSaveType type) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700897 Class* method_class = Method::GetMethodClass();
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700898 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
Ian Rogersff1ed472011-09-20 13:46:24 -0700899 method->SetDeclaringClass(method_class);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800900 // TODO: use a special method for callee saves
Ian Rogers19846512012-02-24 11:42:47 -0800901 method->SetDexMethodIndex(DexFile::kDexNoIndex16);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700902 method->SetCode(NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800903 if ((instruction_set == kThumb2) || (instruction_set == kArm)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700904 uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6) | (1 << art::arm::R7) |
905 (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11);
906 uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3);
907 uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9);
908 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills :0) |
909 (type == kSaveAll ? all_spills :0) | (1 << art::arm::LR);
910 uint32_t fp_all_spills = (1 << art::arm::S0) | (1 << art::arm::S1) | (1 << art::arm::S2) |
911 (1 << art::arm::S3) | (1 << art::arm::S4) | (1 << art::arm::S5) |
912 (1 << art::arm::S6) | (1 << art::arm::S7) | (1 << art::arm::S8) |
913 (1 << art::arm::S9) | (1 << art::arm::S10) | (1 << art::arm::S11) |
914 (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) |
915 (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) |
916 (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) |
917 (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) |
918 (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) |
919 (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
920 (1 << art::arm::S30) | (1 << art::arm::S31);
921 uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
922 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
923 __builtin_popcount(fp_spills) /* fprs */ +
924 1 /* Method* */) * kPointerSize, kStackAlignment);
Ian Rogers15fdb8c2011-09-25 15:45:07 -0700925 method->SetFrameSizeInBytes(frame_size);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700926 method->SetCoreSpillMask(core_spills);
927 method->SetFpSpillMask(fp_spills);
Brian Carlstromae826982011-11-09 01:33:42 -0800928 } else if (instruction_set == kX86) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700929 method->SetFrameSizeInBytes(32);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700930 method->SetCoreSpillMask((1 << art::x86::EBX) | (1 << art::x86::EBP) | (1 << art::x86::ESI) |
Ian Rogersff1ed472011-09-20 13:46:24 -0700931 (1 << art::x86::EDI));
932 method->SetFpSpillMask(0);
933 } else {
934 UNIMPLEMENTED(FATAL);
935 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700936 return method.get();
Ian Rogersff1ed472011-09-20 13:46:24 -0700937}
938
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700939bool Runtime::HasCalleeSaveMethod(CalleeSaveType type) const {
940 return callee_save_method_[type] != NULL;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700941}
942
Brian Carlstrome24fa612011-09-29 00:53:55 -0700943// Returns a special method that describes all callee saves being spilled to the stack.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700944Method* Runtime::GetCalleeSaveMethod(CalleeSaveType type) const {
945 CHECK(HasCalleeSaveMethod(type));
946 return callee_save_method_[type];
Brian Carlstrome24fa612011-09-29 00:53:55 -0700947}
948
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700949void Runtime::SetCalleeSaveMethod(Method* method, CalleeSaveType type) {
950 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
951 callee_save_method_[type] = method;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700952}
953
jeffhao2692b572011-12-16 15:42:28 -0800954void Runtime::EnableMethodTracing(Trace* tracer) {
955 CHECK(!IsMethodTracingActive());
956 tracer_ = tracer;
957}
958
959void Runtime::DisableMethodTracing() {
960 CHECK(IsMethodTracingActive());
961 delete tracer_;
962 tracer_ = NULL;
963}
964
965bool Runtime::IsMethodTracingActive() const {
966 return (tracer_ != NULL);
967}
968
969Trace* Runtime::GetTracer() const {
970 CHECK(IsMethodTracingActive());
971 return tracer_;
972}
973
Carl Shapiro1fb86202011-06-27 17:43:13 -0700974} // namespace art