blob: 31b6539f4422ca44fb03e31cbed6f3e60bdce68f [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 Rogers7c3757f2012-02-15 17:18:53 -080064 system_class_loader_(NULL),
Elliott Hughes6b355752012-01-13 16:49:08 -080065 shutting_down_(false),
Elliott Hughesdcc24742011-09-07 14:02:44 -070066 started_(false),
67 vfprintf_(NULL),
68 exit_(NULL),
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070069 abort_(NULL),
jeffhao2692b572011-12-16 15:42:28 -080070 stats_enabled_(false),
71 tracer_(NULL) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -070072 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
73 resolution_stub_array_[i] = NULL;
74 }
75 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
76 callee_save_method_[i] = NULL;
77 }
Elliott Hughesdcc24742011-09-07 14:02:44 -070078}
79
Carl Shapiro61e019d2011-07-14 16:53:09 -070080Runtime::~Runtime() {
Elliott Hughes6b355752012-01-13 16:49:08 -080081 shutting_down_ = true;
82
Elliott Hughesd1cc8362011-10-24 16:58:50 -070083 Dbg::StopJdwp();
84
Elliott Hughes5fe594f2011-09-08 12:33:17 -070085 // Make sure our internal threads are dead before we start tearing down things they're using.
86 delete signal_catcher_;
Elliott Hughes038a8062011-09-18 14:12:41 -070087 // TODO: GC thread.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070088
Elliott Hughes038a8062011-09-18 14:12:41 -070089 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
Elliott Hughes93e74e82011-09-13 11:07:03 -070090 delete thread_list_;
Elliott Hughesc33a32b2011-10-11 18:18:07 -070091 delete monitor_list_;
Elliott Hughes93e74e82011-09-13 11:07:03 -070092
Carl Shapiro61e019d2011-07-14 16:53:09 -070093 delete class_linker_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070094 Heap::Destroy();
Elliott Hughes4d6850c2012-01-18 15:55:06 -080095 verifier::DexVerifier::DeleteGcMaps();
Elliott Hughescf4c6c42011-09-01 15:16:42 -070096 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070097 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -070098 Thread::Shutdown();
Carl Shapiro4acf4642011-07-26 18:54:13 -070099 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700100 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -0700101 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700102}
103
Elliott Hughescac6cc72011-11-03 20:31:21 -0700104static bool gAborting = false;
105
Elliott Hughese0918552011-10-28 17:18:29 -0700106struct AbortState {
107 void Dump(std::ostream& os) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700108 if (gAborting) {
109 os << "Runtime aborting --- recursively, so no thread-specific detail!\n";
110 return;
111 }
112 gAborting = true;
Elliott Hughese0918552011-10-28 17:18:29 -0700113 os << "Runtime aborting...\n";
Elliott Hughes6c7d2442012-02-01 18:40:47 -0800114 if (Runtime::Current() == NULL) {
115 os << "(Runtime does not yet exist!)\n";
116 return;
117 }
Elliott Hughese0918552011-10-28 17:18:29 -0700118 Thread* self = Thread::Current();
119 if (self == NULL) {
120 os << "(Aborting thread was not attached to runtime!)\n";
121 } else {
Elliott Hughes899e7892012-01-24 14:57:32 -0800122 self->Dump(os);
123 if (self->IsExceptionPending()) {
124 os << "Pending " << PrettyTypeOf(self->GetException()) << " on thread:\n"
125 << self->GetException()->Dump();
126 }
Elliott Hughese0918552011-10-28 17:18:29 -0700127 }
128 }
129};
130
Elliott Hughesffe67362011-07-17 12:09:27 -0700131void Runtime::Abort(const char* file, int line) {
Elliott Hughes131aef82012-01-27 11:53:35 -0800132 // Ensure that we don't have multiple threads trying to abort at once,
133 // which would result in significantly worse diagnostics.
134 MutexLock mu(abort_lock_);
135
Elliott Hughesffe67362011-07-17 12:09:27 -0700136 // Get any pending output out of the way.
137 fflush(NULL);
138
139 // Many people have difficulty distinguish aborts from crashes,
140 // so be explicit.
Elliott Hughese0918552011-10-28 17:18:29 -0700141 AbortState state;
Elliott Hughes9ee5f9c2012-02-03 18:33:16 -0800142 LOG(INTERNAL_FATAL) << Dumpable<AbortState>(state);
Elliott Hughesffe67362011-07-17 12:09:27 -0700143
Elliott Hughesffe67362011-07-17 12:09:27 -0700144 // Perform any platform-specific pre-abort actions.
145 PlatformAbort(file, line);
146
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700147 // use abort hook if we have one
148 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
149 Runtime::Current()->abort_();
150 // notreached
151 }
152
Elliott Hughesffe67362011-07-17 12:09:27 -0700153 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -0700154 // receive SIGABRT. debuggerd dumps the stack trace of the main
155 // thread, whether or not that was the thread that failed. By
156 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -0700157 // fault in the current thread, and get a useful log from debuggerd.
158 // We can also trivially tell the difference between a VM crash and
159 // a deliberate abort by looking at the fault address.
160 *reinterpret_cast<char*>(0xdeadd00d) = 38;
161 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -0700162 // notreached
163}
164
Elliott Hughesbf86d042011-08-31 17:53:14 -0700165void Runtime::CallExitHook(jint status) {
166 if (exit_ != NULL) {
167 ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative);
168 exit_(status);
169 LOG(WARNING) << "Exit hook returned instead of exiting!";
170 }
171}
172
Brian Carlstrom8a436592011-08-15 21:27:23 -0700173// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
174// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
175// [gG] gigabytes.
176//
177// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700178// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
179// of 1024.
180//
181// The spec says the -Xmx and -Xms options must be multiples of 1024. It
182// doesn't say anything about -Xss.
183//
184// Returns 0 (a useless size) if "s" is malformed or specifies a low or
185// non-evenly-divisible value.
186//
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800187size_t ParseMemoryOption(const char* s, size_t div) {
Brian Carlstrom8a436592011-08-15 21:27:23 -0700188 // strtoul accepts a leading [+-], which we don't want,
189 // so make sure our string starts with a decimal digit.
190 if (isdigit(*s)) {
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800191 const char* s2;
192 size_t val = strtoul(s, (char**)&s2, 10);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700193 if (s2 != s) {
194 // s2 should be pointing just after the number.
195 // If this is the end of the string, the user
196 // has specified a number of bytes. Otherwise,
197 // there should be exactly one more character
198 // that specifies a multiplier.
199 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700200 // The remainder of the string is either a single multiplier
201 // character, or nothing to indicate that the value is in
202 // bytes.
203 char c = *s2++;
204 if (*s2 == '\0') {
205 size_t mul;
206 if (c == '\0') {
207 mul = 1;
208 } else if (c == 'k' || c == 'K') {
209 mul = KB;
210 } else if (c == 'm' || c == 'M') {
211 mul = MB;
212 } else if (c == 'g' || c == 'G') {
213 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700214 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700215 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700216 return 0;
217 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700218
219 if (val <= std::numeric_limits<size_t>::max() / mul) {
220 val *= mul;
221 } else {
222 // Clamp to a multiple of 1024.
223 val = std::numeric_limits<size_t>::max() & ~(1024-1);
224 }
225 } else {
226 // There's more than one character after the numeric part.
227 return 0;
228 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700229 }
230 // The man page says that a -Xm value must be a multiple of 1024.
231 if (val % div == 0) {
232 return val;
233 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700234 }
235 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700236 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700237}
238
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800239size_t ParseIntegerOrDie(const std::string& s) {
240 std::string::size_type colon = s.find(':');
241 if (colon == std::string::npos) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700242 LOG(FATAL) << "Missing integer: " << s;
243 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800244 const char* begin = &s[colon + 1];
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700245 char* end;
246 size_t result = strtoul(begin, &end, 10);
247 if (begin == end || *end != '\0') {
248 LOG(FATAL) << "Failed to parse integer in: " << s;
249 }
250 return result;
251}
252
Elliott Hughes0af55432011-08-17 18:37:28 -0700253void LoadJniLibrary(JavaVMExt* vm, const char* name) {
Elliott Hughes6cc332e2012-01-20 22:59:20 -0800254 std::string mapped_name(StringPrintf(OS_SHARED_LIB_FORMAT_STR, name));
Elliott Hughes75770752011-08-24 17:52:38 -0700255 std::string reason;
256 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700257 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
258 << reason;
259 }
260}
261
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700262Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700263 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700264 const char* boot_class_path = getenv("BOOTCLASSPATH");
265 if (boot_class_path != NULL) {
Ian Rogers4f6b57a2012-01-17 16:23:27 -0800266 parsed->boot_class_path_ = boot_class_path;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700267 }
268 const char* class_path = getenv("CLASSPATH");
269 if (class_path != NULL) {
Ian Rogers4f6b57a2012-01-17 16:23:27 -0800270 parsed->class_path_ = class_path;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700271 }
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700272#ifdef NDEBUG
Elliott Hughes5174fe62011-08-23 15:12:35 -0700273 // -Xcheck:jni is off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700274 parsed->check_jni_ = false;
275#else
276 // ...but on by default in debug builds.
277 parsed->check_jni_ = true;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700278#endif
Elliott Hughes85d15452011-09-16 17:33:01 -0700279
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700280 parsed->heap_initial_size_ = Heap::kInitialSize;
281 parsed->heap_maximum_size_ = Heap::kMaximumSize;
jeffhaoc1160702011-10-27 15:48:45 -0700282 parsed->heap_growth_limit_ = 0; // 0 means no growth limit
Brian Carlstromf734cf52011-08-17 16:28:14 -0700283 parsed->stack_size_ = Thread::kDefaultStackSize;
284
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800285 parsed->is_compiler_ = false;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700286 parsed->is_zygote_ = false;
287
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700288 parsed->jni_globals_max_ = 0;
Elliott Hughesfc861622011-10-17 17:57:47 -0700289 parsed->lock_profiling_threshold_ = 0;
290 parsed->hook_is_sensitive_thread_ = NULL;
291
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700292 parsed->hook_vfprintf_ = vfprintf;
293 parsed->hook_exit_ = exit;
294 parsed->hook_abort_ = abort;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700295
296 for (size_t i = 0; i < options.size(); ++i) {
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800297 const std::string option(options[i].first);
Brian Carlstrom0796af02011-10-12 14:31:45 -0700298 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700299 LOG(INFO) << "option[" << i << "]=" << option;
300 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800301 if (StartsWith(option, "-Xbootclasspath:")) {
302 parsed->boot_class_path_ = option.substr(strlen("-Xbootclasspath:"));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700303 } else if (option == "-classpath" || option == "-cp") {
304 // TODO: support -Djava.class.path
305 i++;
306 if (i == options.size()) {
307 // TODO: usage
308 LOG(FATAL) << "Missing required class path value for " << option;
309 return NULL;
310 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800311 parsed->class_path_ = options[i].first;
312 } else if (StartsWith(option, "-Ximage:")) {
313 parsed->image_ = option.substr(strlen("-Ximage:"));
314 } else if (StartsWith(option, "-Xcheck:jni")) {
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700315 parsed->check_jni_ = true;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800316 } else if (StartsWith(option, "-Xrunjdwp:") || StartsWith(option, "-agentlib:jdwp=")) {
317 std::string tail(option.substr(option[1] == 'X' ? 10 : 15));
Elliott Hughes3bb81562011-10-21 18:52:59 -0700318 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
319 LOG(FATAL) << "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
320 << "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n";
321 return NULL;
322 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800323 } else if (StartsWith(option, "-Xms")) {
324 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).c_str(), 1024);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700325 if (size == 0) {
326 if (ignore_unrecognized) {
327 continue;
328 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700329 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700330 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700331 return NULL;
332 }
333 parsed->heap_initial_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800334 } else if (StartsWith(option, "-Xmx")) {
335 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).c_str(), 1024);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700336 if (size == 0) {
337 if (ignore_unrecognized) {
338 continue;
339 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700340 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700341 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700342 return NULL;
343 }
344 parsed->heap_maximum_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800345 } else if (StartsWith(option, "-XX:HeapGrowthLimit=")) {
346 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).c_str(), 1024);
jeffhaoc1160702011-10-27 15:48:45 -0700347 if (size == 0) {
348 if (ignore_unrecognized) {
349 continue;
350 }
351 // TODO: usage
352 LOG(FATAL) << "Failed to parse " << option;
353 return NULL;
354 }
355 parsed->heap_growth_limit_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800356 } else if (StartsWith(option, "-Xss")) {
357 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).c_str(), 1);
Brian Carlstromf734cf52011-08-17 16:28:14 -0700358 if (size == 0) {
359 if (ignore_unrecognized) {
360 continue;
361 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700362 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700363 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700364 return NULL;
365 }
366 parsed->stack_size_ = size;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800367 } else if (StartsWith(option, "-D")) {
368 parsed->properties_.push_back(option.substr(strlen("-D")));
369 } else if (StartsWith(option, "-Xjnitrace:")) {
370 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:"));
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700371 } else if (option == "compiler") {
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800372 parsed->is_compiler_ = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700373 } else if (option == "-Xzygote") {
374 parsed->is_zygote_ = true;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800375 } else if (StartsWith(option, "-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700376 std::vector<std::string> verbose_options;
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800377 Split(option.substr(strlen("-verbose:")), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700378 for (size_t i = 0; i < verbose_options.size(); ++i) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800379 if (verbose_options[i] == "class") {
380 gLogVerbosity.class_linker = true;
381 } else if (verbose_options[i] == "compiler") {
382 gLogVerbosity.compiler = true;
383 } else if (verbose_options[i] == "heap") {
384 gLogVerbosity.heap = true;
385 } else if (verbose_options[i] == "gc") {
386 gLogVerbosity.gc = true;
387 } else if (verbose_options[i] == "jdwp") {
388 gLogVerbosity.jdwp = true;
389 } else if (verbose_options[i] == "jni") {
390 gLogVerbosity.jni = true;
391 } else if (verbose_options[i] == "monitor") {
392 gLogVerbosity.monitor = true;
393 } else if (verbose_options[i] == "startup") {
394 gLogVerbosity.startup = true;
395 } else if (verbose_options[i] == "third-party-jni") {
396 gLogVerbosity.third_party_jni = true;
397 } else if (verbose_options[i] == "threads") {
398 gLogVerbosity.threads = true;
399 } else {
400 LOG(WARNING) << "Ignoring unknown -verbose option: " << verbose_options[i];
401 }
Elliott Hughes0af55432011-08-17 18:37:28 -0700402 }
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800403 } else if (StartsWith(option, "-Xjnigreflimit:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700404 parsed->jni_globals_max_ = ParseIntegerOrDie(option);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800405 } else if (StartsWith(option, "-Xlockprofthreshold:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700406 parsed->lock_profiling_threshold_ = ParseIntegerOrDie(option);
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800407 } else if (StartsWith(option, "-Xstacktracefile:")) {
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700408// always show stack traces in debug builds
409#ifdef NDEBUG
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800410 parsed->stack_trace_file_ = option.substr(strlen("-Xstacktracefile:"));
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700411#endif
Elliott Hughesfc861622011-10-17 17:57:47 -0700412 } else if (option == "sensitiveThread") {
413 parsed->hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(options[i].second);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700414 } else if (option == "vfprintf") {
415 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
416 } else if (option == "exit") {
417 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
418 } else if (option == "abort") {
419 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700420 } else if (option == "host-prefix") {
421 parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second);
Elliott Hughesb3e66df2012-01-12 14:49:18 -0800422 } else if (option == "-Xgenregmap" || option == "-Xgc:precise") {
423 // We silently ignore these for backwards compatibility.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700424 } else {
425 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700426 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700427 LOG(ERROR) << "Unrecognized option " << option;
428 // TODO: this should exit, but for now tolerate unknown options
429 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700430 }
431 }
432 }
433
Brian Carlstrom223f20f2012-02-04 23:06:55 -0800434 if (!parsed->is_compiler_ && parsed->image_.empty()) {
435 parsed->image_ += GetAndroidRoot();
436 parsed->image_ += "/framework/boot.art";
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700437 }
jeffhaoc1160702011-10-27 15:48:45 -0700438 if (parsed->heap_growth_limit_ == 0) {
439 parsed->heap_growth_limit_ = parsed->heap_maximum_size_;
440 }
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700441
Elliott Hughescac6cc72011-11-03 20:31:21 -0700442 LOG(INFO) << "Build type: "
443#ifndef NDEBUG
444 << "debug"
445#else
446 << "optimized"
447#endif
448 << "; CheckJNI: " << (parsed->check_jni_ ? "on" : "off");
Elliott Hughes85d15452011-09-16 17:33:01 -0700449
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700450 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700451}
452
453Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700454 // TODO: acquire a static mutex on Runtime to avoid racing.
455 if (Runtime::instance_ != NULL) {
456 return NULL;
457 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700458 instance_ = new Runtime;
459 if (!instance_->Init(options, ignore_unrecognized)) {
460 delete instance_;
461 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700462 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700463 return instance_;
464}
Elliott Hughes0af55432011-08-17 18:37:28 -0700465
Brian Carlstromaded5f72011-10-07 17:15:04 -0700466void CreateSystemClassLoader() {
467 if (ClassLoader::UseCompileTimeClassPath()) {
468 return;
469 }
470
471 Thread* self = Thread::Current();
472
473 // Must be in the kNative state for calling native methods.
474 CHECK_EQ(self->GetState(), Thread::kNative);
475
476 JNIEnv* env = self->GetJniEnv();
Brian Carlstromdf143242011-10-10 18:05:34 -0700477 ScopedLocalRef<jclass> ClassLoader_class(env, env->FindClass("java/lang/ClassLoader"));
478 CHECK(ClassLoader_class.get() != NULL);
479 jmethodID getSystemClassLoader = env->GetStaticMethodID(ClassLoader_class.get(),
480 "getSystemClassLoader",
481 "()Ljava/lang/ClassLoader;");
482 CHECK(getSystemClassLoader != NULL);
483 ScopedLocalRef<jobject> class_loader(env, env->CallStaticObjectMethod(ClassLoader_class.get(),
484 getSystemClassLoader));
485 CHECK(class_loader.get() != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700486
Brian Carlstromdf143242011-10-10 18:05:34 -0700487 Thread::Current()->SetClassLoaderOverride(Decode<ClassLoader*>(env, class_loader.get()));
Jesse Wilson1b2b2f22011-11-22 11:47:44 -0500488
489 ScopedLocalRef<jclass> Thread_class(env, env->FindClass("java/lang/Thread"));
490 CHECK(Thread_class.get() != NULL);
491 jfieldID contextClassLoader = env->GetFieldID(Thread_class.get(),
492 "contextClassLoader",
493 "Ljava/lang/ClassLoader;");
494 CHECK(contextClassLoader != NULL);
495 ScopedLocalRef<jobject> self_jobject(env, AddLocalReference<jobject>(env, self->GetPeer()));
496 env->SetObjectField(self_jobject.get(), contextClassLoader, class_loader.get());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700497}
498
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700499void Runtime::Start() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800500 VLOG(startup) << "Runtime::Start entering";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700501
502 CHECK(host_prefix_.empty()) << host_prefix_;
503
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700504 // Restore main thread state to kNative as expected by native code
505 Thread::Current()->SetState(Thread::kNative);
506
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700507 started_ = true;
508
Brian Carlstromae826982011-11-09 01:33:42 -0800509 // InitNativeMethods needs to be after started_ so that the classes
510 // it touches will have methods linked to the oat file if necessary.
511 InitNativeMethods();
512
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500513 Thread::FinishStartup();
514
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700515 if (!is_zygote_) {
516 DidForkFromZygote();
517 }
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700518
Elliott Hughes85d15452011-09-16 17:33:01 -0700519 StartDaemonThreads();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700520
Brian Carlstromaded5f72011-10-07 17:15:04 -0700521 CreateSystemClassLoader();
522
Elliott Hughes726079d2011-10-07 18:43:44 -0700523 Thread::Current()->GetJniEnv()->locals.AssertEmpty();
524
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800525 VLOG(startup) << "Runtime::Start exiting";
Elliott Hughes85d15452011-09-16 17:33:01 -0700526}
527
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700528void Runtime::DidForkFromZygote() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700529 is_zygote_ = false;
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700530
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700531 StartSignalCatcher();
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700532
533 // Start the JDWP thread. If the command-line debugger flags specified "suspend=y",
534 // this will pause the runtime, so we probably want this to come last.
535 Dbg::StartJdwp();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700536}
537
538void Runtime::StartSignalCatcher() {
539 if (!is_zygote_) {
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700540 signal_catcher_ = new SignalCatcher(stack_trace_file_);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700541 }
542}
543
Elliott Hughes85d15452011-09-16 17:33:01 -0700544void Runtime::StartDaemonThreads() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800545 VLOG(startup) << "Runtime::StartDaemonThreads entering";
Elliott Hughes85d15452011-09-16 17:33:01 -0700546
Elliott Hughes719b3232011-09-25 17:42:19 -0700547 Thread* self = Thread::Current();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700548
549 // Must be in the kNative state for calling native methods.
550 CHECK_EQ(self->GetState(), Thread::kNative);
551
Elliott Hughes719b3232011-09-25 17:42:19 -0700552 JNIEnv* env = self->GetJniEnv();
Elliott Hughes726079d2011-10-07 18:43:44 -0700553 ScopedLocalRef<jclass> c(env, env->FindClass("java/lang/Daemons"));
554 CHECK(c.get() != NULL);
555 jmethodID mid = env->GetStaticMethodID(c.get(), "start", "()V");
Elliott Hughes719b3232011-09-25 17:42:19 -0700556 CHECK(mid != NULL);
Elliott Hughes726079d2011-10-07 18:43:44 -0700557 env->CallStaticVoidMethod(c.get(), mid);
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800558 CHECK(!env->ExceptionCheck());
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700559
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800560 VLOG(startup) << "Runtime::StartDaemonThreads exiting";
Carl Shapiro61e019d2011-07-14 16:53:09 -0700561}
562
Elliott Hughes6b355752012-01-13 16:49:08 -0800563bool Runtime::IsShuttingDown() const {
564 return shutting_down_;
565}
566
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700567bool Runtime::IsStarted() const {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700568 return started_;
569}
570
Elliott Hughes0af55432011-08-17 18:37:28 -0700571bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700572 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700573
Elliott Hughes90a33692011-08-30 13:27:07 -0700574 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
575 if (options.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700576 LOG(ERROR) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700577 return false;
578 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800579 VLOG(startup) << "Runtime::Init -verbose:startup enabled";
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700580
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700581 SetJniGlobalsMax(options->jni_globals_max_);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800582 Monitor::Init(options->lock_profiling_threshold_, options->hook_is_sensitive_thread_);
Elliott Hughes32d6e1e2011-10-11 14:47:44 -0700583
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700584 host_prefix_ = options->host_prefix_;
585 boot_class_path_ = options->boot_class_path_;
586 class_path_ = options->class_path_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700587 properties_ = options->properties_;
588
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800589 is_compiler_ = options->is_compiler_;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700590 is_zygote_ = options->is_zygote_;
591
Elliott Hughes0af55432011-08-17 18:37:28 -0700592 vfprintf_ = options->hook_vfprintf_;
593 exit_ = options->hook_exit_;
594 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700595
Elliott Hughesbe759c62011-09-08 19:38:21 -0700596 default_stack_size_ = options->stack_size_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700597 stack_trace_file_ = options->stack_trace_file_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700598
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700599 monitor_list_ = new MonitorList;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800600 thread_list_ = new ThreadList;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700601 intern_table_ = new InternTable;
602
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800603 Heap::Init(options->heap_initial_size_,
jeffhaoc1160702011-10-27 15:48:45 -0700604 options->heap_growth_limit_,
Ian Rogers30fab402012-01-23 15:43:46 -0800605 options->heap_maximum_size_,
Brian Carlstrom223f20f2012-02-04 23:06:55 -0800606 options->image_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700607
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700608 BlockSignals();
609
Elliott Hughesa0957642011-09-02 14:27:33 -0700610 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700611
Elliott Hughesbe759c62011-09-08 19:38:21 -0700612 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700613
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700614 // ClassLinker needs an attached thread, but we can't fully attach a thread
615 // without creating objects.
616 Thread::Attach(this, "main", false);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700617
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700618 // Set us to runnable so tools using a runtime can allocate and GC by default
619 Thread::Current()->SetState(Thread::kRunnable);
620
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700621 CHECK_GE(Heap::GetSpaces().size(), 1U);
622 class_linker_ = ((Heap::GetSpaces()[0]->IsImageSpace())
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800623 ? ClassLinker::Create(intern_table_)
624 : ClassLinker::Create(options->boot_class_path_, intern_table_));
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700625
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800626 VLOG(startup) << "Runtime::Init exiting";
Carl Shapiro1fb86202011-06-27 17:43:13 -0700627 return true;
628}
629
Elliott Hughes038a8062011-09-18 14:12:41 -0700630void Runtime::InitNativeMethods() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800631 VLOG(startup) << "Runtime::InitNativeMethods entering";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700632 Thread* self = Thread::Current();
633 JNIEnv* env = self->GetJniEnv();
634
Elliott Hughes418d20f2011-09-22 14:00:39 -0700635 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Brian Carlstromaded5f72011-10-07 17:15:04 -0700636 CHECK_EQ(self->GetState(), Thread::kNative);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700637
Elliott Hughes418d20f2011-09-22 14:00:39 -0700638 // First set up JniConstants, which is used by both the runtime's built-in native
639 // methods and libcore.
Elliott Hughesfea966e2011-09-22 10:26:20 -0700640 JniConstants::init(env);
641
Elliott Hughes418d20f2011-09-22 14:00:39 -0700642 // Then set up the native methods provided by the runtime itself.
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700643 RegisterRuntimeNativeMethods(env);
644
Elliott Hughes418d20f2011-09-22 14:00:39 -0700645 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
646 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
647 // the library that implements System.loadLibrary!
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700648 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800649 VLOG(startup) << "Runtime::InitNativeMethods exiting";
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700650}
651
652void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
653#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Ian Rogers5167c972012-02-03 10:41:20 -0800654 // Register Throwable first so that registration of other native methods can throw exceptions
655 REGISTER(register_java_lang_Throwable);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700656 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700657 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700658 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700659 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700660 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700661 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700662 REGISTER(register_java_lang_Object);
663 REGISTER(register_java_lang_Runtime);
664 REGISTER(register_java_lang_String);
665 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700666 REGISTER(register_java_lang_Thread);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700667 REGISTER(register_java_lang_VMClassLoader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700668 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700669 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700670 REGISTER(register_java_lang_reflect_Field);
671 REGISTER(register_java_lang_reflect_Method);
Jesse Wilson95caa792011-10-12 18:14:17 -0400672 REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700673 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700674 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700675 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700676 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700677#undef REGISTER
678}
679
Elliott Hughes8daa0922011-09-11 13:46:25 -0700680void Runtime::Dump(std::ostream& os) {
Elliott Hughese27955c2011-08-26 15:21:24 -0700681 // TODO: dump other runtime statistics?
Elliott Hughescac6cc72011-11-03 20:31:21 -0700682 GetClassLinker()->DumpForSigQuit(os);
683 GetInternTable()->DumpForSigQuit(os);
Elliott Hughes42ee1422011-09-06 12:33:32 -0700684 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700685
686 thread_list_->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700687}
688
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800689void Runtime::DumpLockHolders(std::ostream& os) {
690 pid_t heap_lock_owner = Heap::GetLockOwner();
691 pid_t thread_list_lock_owner = GetThreadList()->GetLockOwner();
692 pid_t classes_lock_owner = GetClassLinker()->GetClassesLockOwner();
693 pid_t dex_lock_owner = GetClassLinker()->GetDexLockOwner();
694 if ((heap_lock_owner | thread_list_lock_owner | classes_lock_owner | dex_lock_owner) != 0) {
695 os << "Heap lock owner tid: " << heap_lock_owner << "\n"
696 << "ThreadList lock owner tid: " << thread_list_lock_owner << "\n"
697 << "ClassLinker classes lock owner tid: " << classes_lock_owner << "\n"
698 << "ClassLinker dex lock owner tid: " << dex_lock_owner << "\n";
699 }
700}
701
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700702void Runtime::SetStatsEnabled(bool new_state) {
703 if (new_state == true) {
704 GetStats()->Clear(~0);
705 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
706 Thread::Current()->GetStats()->Clear(~0);
707 }
708 stats_enabled_ = new_state;
709}
710
711void Runtime::ResetStats(int kinds) {
712 GetStats()->Clear(kinds & 0xffff);
713 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
714 Thread::Current()->GetStats()->Clear(kinds >> 16);
715}
716
717RuntimeStats* Runtime::GetStats() {
718 return &stats_;
719}
720
721int32_t Runtime::GetStat(int kind) {
722 RuntimeStats* stats;
723 if (kind < (1<<16)) {
724 stats = GetStats();
725 } else {
726 stats = Thread::Current()->GetStats();
727 kind >>= 16;
728 }
729 switch (kind) {
730 case KIND_ALLOCATED_OBJECTS:
731 return stats->allocated_objects;
732 case KIND_ALLOCATED_BYTES:
733 return stats->allocated_bytes;
734 case KIND_FREED_OBJECTS:
735 return stats->freed_objects;
736 case KIND_FREED_BYTES:
737 return stats->freed_bytes;
738 case KIND_GC_INVOCATIONS:
739 return stats->gc_for_alloc_count;
740 case KIND_CLASS_INIT_COUNT:
741 return stats->class_init_count;
742 case KIND_CLASS_INIT_TIME:
743 // Convert ns to us, reduce to 32 bits.
744 return (int) (stats->class_init_time_ns / 1000);
745 case KIND_EXT_ALLOCATED_OBJECTS:
746 case KIND_EXT_ALLOCATED_BYTES:
747 case KIND_EXT_FREED_OBJECTS:
748 case KIND_EXT_FREED_BYTES:
749 return 0; // backward compatibility
750 default:
751 CHECK(false);
752 return -1; // unreachable
753 }
754}
755
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700756void Runtime::BlockSignals() {
757 sigset_t sigset;
758 if (sigemptyset(&sigset) == -1) {
759 PLOG(FATAL) << "sigemptyset failed";
760 }
761 if (sigaddset(&sigset, SIGPIPE) == -1) {
762 PLOG(ERROR) << "sigaddset SIGPIPE failed";
763 }
764 // SIGQUIT is used to dump the runtime's state (including stack traces).
765 if (sigaddset(&sigset, SIGQUIT) == -1) {
766 PLOG(ERROR) << "sigaddset SIGQUIT failed";
767 }
768 // SIGUSR1 is used to initiate a heap dump.
769 if (sigaddset(&sigset, SIGUSR1) == -1) {
770 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
771 }
772 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
773}
774
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700775void Runtime::AttachCurrentThread(const char* name, bool as_daemon) {
776 Thread::Attach(instance_, name, as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700777}
778
Elliott Hughesd92bec42011-09-02 17:04:36 -0700779void Runtime::DetachCurrentThread() {
Elliott Hughes038a8062011-09-18 14:12:41 -0700780 // TODO: check we're not calling DetachCurrentThread from a call stack that
781 // includes managed frames. (It's only valid if the stack is all-native.)
Elliott Hughes02b48d12011-09-07 17:15:51 -0700782 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700783}
784
Brian Carlstrome24fa612011-09-29 00:53:55 -0700785void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700786 Dbg::VisitRoots(visitor, arg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700787 class_linker_->VisitRoots(visitor, arg);
788 intern_table_->VisitRoots(visitor, arg);
789 java_vm_->VisitRoots(visitor, arg);
790 thread_list_->VisitRoots(visitor, arg);
791 visitor(jni_stub_array_, arg);
792 visitor(abstract_method_error_stub_array_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700793 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
794 visitor(resolution_stub_array_[i], arg);
795 }
796 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
797 visitor(callee_save_method_[i], arg);
798 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700799}
800
Ian Rogers169c9a72011-11-13 20:13:17 -0800801bool Runtime::HasJniDlsymLookupStub() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700802 return jni_stub_array_ != NULL;
803}
804
Ian Rogers169c9a72011-11-13 20:13:17 -0800805ByteArray* Runtime::GetJniDlsymLookupStub() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700806 CHECK(jni_stub_array_ != NULL);
807 return jni_stub_array_;
808}
809
Ian Rogers169c9a72011-11-13 20:13:17 -0800810void Runtime::SetJniDlsymLookupStub(ByteArray* jni_stub_array) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700811 CHECK(jni_stub_array != NULL) << " jni_stub_array=" << jni_stub_array;
812 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array)
813 << "jni_stub_array_=" << jni_stub_array_ << " jni_stub_array=" << jni_stub_array;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700814 jni_stub_array_ = jni_stub_array;
815}
816
817bool Runtime::HasAbstractMethodErrorStubArray() const {
818 return abstract_method_error_stub_array_ != NULL;
819}
820
821ByteArray* Runtime::GetAbstractMethodErrorStubArray() const {
822 CHECK(abstract_method_error_stub_array_ != NULL);
823 return abstract_method_error_stub_array_;
824}
825
826void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) {
827 CHECK(abstract_method_error_stub_array != NULL);
828 CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array);
829 abstract_method_error_stub_array_ = abstract_method_error_stub_array;
830}
831
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700832
833Runtime::TrampolineType Runtime::GetTrampolineType(Method* method) {
834 if (method == NULL) {
835 return Runtime::kUnknownMethod;
836 } else if (method->IsStatic()) {
837 return Runtime::kStaticMethod;
838 } else {
839 return Runtime::kInstanceMethod;
840 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700841}
842
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700843bool Runtime::HasResolutionStubArray(TrampolineType type) const {
844 return resolution_stub_array_[type] != NULL;
Ian Rogersad25ac52011-10-04 19:13:33 -0700845}
846
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700847ByteArray* Runtime::GetResolutionStubArray(TrampolineType type) const {
848 CHECK(HasResolutionStubArray(type));
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700849 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastTrampolineMethodType));
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700850 return resolution_stub_array_[type];
851}
852
853void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700854 CHECK(resolution_stub_array != NULL);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700855 CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array);
856 resolution_stub_array_[type] = resolution_stub_array;
Ian Rogersad25ac52011-10-04 19:13:33 -0700857}
858
Brian Carlstromae826982011-11-09 01:33:42 -0800859Method* Runtime::CreateCalleeSaveMethod(InstructionSet instruction_set, CalleeSaveType type) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700860 Class* method_class = Method::GetMethodClass();
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700861 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
Ian Rogersff1ed472011-09-20 13:46:24 -0700862 method->SetDeclaringClass(method_class);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800863 // TODO: use a special method for callee saves
864 method->SetMethodIndex(DexFile::kDexNoIndex16);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700865 method->SetCode(NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800866 if ((instruction_set == kThumb2) || (instruction_set == kArm)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700867 uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6) | (1 << art::arm::R7) |
868 (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11);
869 uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3);
870 uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9);
871 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills :0) |
872 (type == kSaveAll ? all_spills :0) | (1 << art::arm::LR);
873 uint32_t fp_all_spills = (1 << art::arm::S0) | (1 << art::arm::S1) | (1 << art::arm::S2) |
874 (1 << art::arm::S3) | (1 << art::arm::S4) | (1 << art::arm::S5) |
875 (1 << art::arm::S6) | (1 << art::arm::S7) | (1 << art::arm::S8) |
876 (1 << art::arm::S9) | (1 << art::arm::S10) | (1 << art::arm::S11) |
877 (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) |
878 (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) |
879 (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) |
880 (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) |
881 (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) |
882 (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
883 (1 << art::arm::S30) | (1 << art::arm::S31);
884 uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
885 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
886 __builtin_popcount(fp_spills) /* fprs */ +
887 1 /* Method* */) * kPointerSize, kStackAlignment);
Ian Rogers15fdb8c2011-09-25 15:45:07 -0700888 method->SetFrameSizeInBytes(frame_size);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700889 method->SetCoreSpillMask(core_spills);
890 method->SetFpSpillMask(fp_spills);
Brian Carlstromae826982011-11-09 01:33:42 -0800891 } else if (instruction_set == kX86) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700892 method->SetFrameSizeInBytes(32);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700893 method->SetCoreSpillMask((1 << art::x86::EBX) | (1 << art::x86::EBP) | (1 << art::x86::ESI) |
Ian Rogersff1ed472011-09-20 13:46:24 -0700894 (1 << art::x86::EDI));
895 method->SetFpSpillMask(0);
896 } else {
897 UNIMPLEMENTED(FATAL);
898 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700899 return method.get();
Ian Rogersff1ed472011-09-20 13:46:24 -0700900}
901
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700902bool Runtime::HasCalleeSaveMethod(CalleeSaveType type) const {
903 return callee_save_method_[type] != NULL;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700904}
905
Brian Carlstrome24fa612011-09-29 00:53:55 -0700906// Returns a special method that describes all callee saves being spilled to the stack.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700907Method* Runtime::GetCalleeSaveMethod(CalleeSaveType type) const {
908 CHECK(HasCalleeSaveMethod(type));
909 return callee_save_method_[type];
Brian Carlstrome24fa612011-09-29 00:53:55 -0700910}
911
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700912void Runtime::SetCalleeSaveMethod(Method* method, CalleeSaveType type) {
913 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
914 callee_save_method_[type] = method;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700915}
916
jeffhao2692b572011-12-16 15:42:28 -0800917void Runtime::EnableMethodTracing(Trace* tracer) {
918 CHECK(!IsMethodTracingActive());
919 tracer_ = tracer;
920}
921
922void Runtime::DisableMethodTracing() {
923 CHECK(IsMethodTracingActive());
924 delete tracer_;
925 tracer_ = NULL;
926}
927
928bool Runtime::IsMethodTracingActive() const {
929 return (tracer_ != NULL);
930}
931
932Trace* Runtime::GetTracer() const {
933 CHECK(IsMethodTracingActive());
934 return tracer_;
935}
936
Carl Shapiro1fb86202011-06-27 17:43:13 -0700937} // namespace art