runtime.cc revision 1984651929744dd603fd082e23eacd877b9bc177
12faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes/*
22faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Copyright (C) 2011 The Android Open Source Project
32faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
42faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
52faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * you may not use this file except in compliance with the License.
62faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * You may obtain a copy of the License at
72faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
82faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
92faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
102faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Unless required by applicable law or agreed to in writing, software
112faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
122faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * See the License for the specific language governing permissions and
142faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * limitations under the License.
152faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes */
166b6b5f0e67ce03f38223a525612955663bc1799bCarl Shapiro
17578bbdc684db8ed68e9fedbc678669d27fa68b6eBrian Carlstrom#include "runtime.h"
187b21670581d13db32f1384a3b2692bcfc8f57320Carl Shapiro
19dbf05b722af99ba2fd2f4c4fc7eb6c3e9880e5d1Brian Carlstrom#include <signal.h>
20dbf05b722af99ba2fd2f4c4fc7eb6c3e9880e5d1Brian Carlstrom
21ffe6736397d17457188727510f0a2953f69a383aElliott Hughes#include <cstdio>
22ffe6736397d17457188727510f0a2953f69a383aElliott Hughes#include <cstdlib>
238a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom#include <limits>
242ed144c2b49ae1da6c464d7a1be0062870530802Carl Shapiro#include <vector>
25ffe6736397d17457188727510f0a2953f69a383aElliott Hughes
26578bbdc684db8ed68e9fedbc678669d27fa68b6eBrian Carlstrom#include "class_linker.h"
27aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom#include "class_loader.h"
283bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes#include "debugger.h"
294d6850c47515fe2a4f9de923befb6287bb863c8cElliott Hughes#include "dex_verifier.h"
30578bbdc684db8ed68e9fedbc678669d27fa68b6eBrian Carlstrom#include "heap.h"
31e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom#include "image.h"
32cf4c6c41b0084dc4567ff709fb8ce9ebd72b26acElliott Hughes#include "intern_table.h"
33c5f7c91ab89055cffb573fff7e06dbdd860bccedElliott Hughes#include "jni_internal.h"
3432d6e1e5654433d7eadede89e1c770b2c839aee9Elliott Hughes#include "monitor.h"
35e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom#include "oat_file.h"
36726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes#include "ScopedLocalRef.h"
37e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes#include "signal_catcher.h"
38e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom#include "space.h"
39578bbdc684db8ed68e9fedbc678669d27fa68b6eBrian Carlstrom#include "thread.h"
408daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes#include "thread_list.h"
413bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes#include "UniquePtr.h"
4261e019d291583029c01b61b93bea750f2b663c37Carl Shapiro
4390a3369d3b6238f1a4c9b19ca68978dab1c39bc4Elliott Hughes// TODO: this drags in cutil/log.h, which conflicts with our logging.h.
4490a3369d3b6238f1a4c9b19ca68978dab1c39bc4Elliott Hughes#include "JniConstants.h"
4590a3369d3b6238f1a4c9b19ca68978dab1c39bc4Elliott Hughes
466b6b5f0e67ce03f38223a525612955663bc1799bCarl Shapironamespace art {
477b21670581d13db32f1384a3b2692bcfc8f57320Carl Shapiro
482ed144c2b49ae1da6c464d7a1be0062870530802Carl ShapiroRuntime* Runtime::instance_ = NULL;
492ed144c2b49ae1da6c464d7a1be0062870530802Carl Shapiro
50131aef8c94292cc530da2fd91ee98d1432352959Elliott HughesMutex Runtime::abort_lock_("abort lock");
51131aef8c94292cc530da2fd91ee98d1432352959Elliott Hughes
52dcc247493fd8fb243e335c3ec08e5e625896a47cElliott HughesRuntime::Runtime()
53d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes    : is_compiler_(false),
54d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes      is_zygote_(false),
550a5b14de5115f51e0ed2dd6c3fcc5b84bbce690eBrian Carlstrom      default_stack_size_(Thread::kDefaultStackSize),
56c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes      monitor_list_(NULL),
57dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes      thread_list_(NULL),
58dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes      intern_table_(NULL),
59dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes      class_linker_(NULL),
60dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes      signal_catcher_(NULL),
61dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes      java_vm_(NULL),
62161928613d3f097108319de60494fab1aab8d48aBrian Carlstrom      jni_stub_array_(NULL),
63e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom      abstract_method_error_stub_array_(NULL),
641984651929744dd603fd082e23eacd877b9bc177Ian Rogers      resolution_method_(NULL),
657c3757fb51450d0669883c82375040ece051e189Ian Rogers      system_class_loader_(NULL),
666b3557571e798b60df995f978fa01c0ca1980dfdElliott Hughes      shutting_down_(false),
67dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes      started_(false),
68dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes      vfprintf_(NULL),
69dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes      exit_(NULL),
709d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes      abort_(NULL),
712692b573a56cd63a3c8c8aa1636e3766b6d8c9c4jeffhao      stats_enabled_(false),
722692b573a56cd63a3c8c8aa1636e3766b6d8c9c4jeffhao      tracer_(NULL) {
734f0d07c783afef89703dce32c94440fc8621a29bIan Rogers  for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
744f0d07c783afef89703dce32c94440fc8621a29bIan Rogers    resolution_stub_array_[i] = NULL;
754f0d07c783afef89703dce32c94440fc8621a29bIan Rogers  }
764f0d07c783afef89703dce32c94440fc8621a29bIan Rogers  for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
774f0d07c783afef89703dce32c94440fc8621a29bIan Rogers    callee_save_method_[i] = NULL;
784f0d07c783afef89703dce32c94440fc8621a29bIan Rogers  }
79dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes}
80dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes
8161e019d291583029c01b61b93bea750f2b663c37Carl ShapiroRuntime::~Runtime() {
826b3557571e798b60df995f978fa01c0ca1980dfdElliott Hughes  shutting_down_ = true;
836b3557571e798b60df995f978fa01c0ca1980dfdElliott Hughes
84d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  Dbg::StopJdwp();
85d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes
865fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes  // Make sure our internal threads are dead before we start tearing down things they're using.
875fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes  delete signal_catcher_;
88038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes  // TODO: GC thread.
895fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes
90038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes  // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
9193e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes  delete thread_list_;
92c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes  delete monitor_list_;
9393e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes
9461e019d291583029c01b61b93bea750f2b663c37Carl Shapiro  delete class_linker_;
9569759eaa6fd4386f1e6d8748052ad221087b3476Carl Shapiro  Heap::Destroy();
964d6850c47515fe2a4f9de923befb6287bb863c8cElliott Hughes  verifier::DexVerifier::DeleteGcMaps();
97cf4c6c41b0084dc4567ff709fb8ce9ebd72b26acElliott Hughes  delete intern_table_;
98c5f7c91ab89055cffb573fff7e06dbdd860bccedElliott Hughes  delete java_vm_;
99c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes  Thread::Shutdown();
1004acf4646fbbd6c1893cec5bb700d3c7cd09e4a26Carl Shapiro  // TODO: acquire a static mutex on Runtime to avoid racing.
1014a289ed61242964b921434de7d375f46480472a1Brian Carlstrom  CHECK(instance_ == NULL || instance_ == this);
1024acf4646fbbd6c1893cec5bb700d3c7cd09e4a26Carl Shapiro  instance_ = NULL;
10361e019d291583029c01b61b93bea750f2b663c37Carl Shapiro}
10461e019d291583029c01b61b93bea750f2b663c37Carl Shapiro
105cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughesstatic bool gAborting = false;
106cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes
107e0918556e7551de638870dcad3f2023f94f85a50Elliott Hughesstruct AbortState {
108e0918556e7551de638870dcad3f2023f94f85a50Elliott Hughes  void Dump(std::ostream& os) {
109cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes    if (gAborting) {
110cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes      os << "Runtime aborting --- recursively, so no thread-specific detail!\n";
111cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes      return;
112cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes    }
113cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes    gAborting = true;
114e0918556e7551de638870dcad3f2023f94f85a50Elliott Hughes    os << "Runtime aborting...\n";
1156c7d244058b74cdd61533968dd6cddd7003d2671Elliott Hughes    if (Runtime::Current() == NULL) {
1166c7d244058b74cdd61533968dd6cddd7003d2671Elliott Hughes      os << "(Runtime does not yet exist!)\n";
1176c7d244058b74cdd61533968dd6cddd7003d2671Elliott Hughes      return;
1186c7d244058b74cdd61533968dd6cddd7003d2671Elliott Hughes    }
119e0918556e7551de638870dcad3f2023f94f85a50Elliott Hughes    Thread* self = Thread::Current();
120e0918556e7551de638870dcad3f2023f94f85a50Elliott Hughes    if (self == NULL) {
121e0918556e7551de638870dcad3f2023f94f85a50Elliott Hughes      os << "(Aborting thread was not attached to runtime!)\n";
122e0918556e7551de638870dcad3f2023f94f85a50Elliott Hughes    } else {
123899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes      self->Dump(os);
124899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes      if (self->IsExceptionPending()) {
125899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes        os << "Pending " << PrettyTypeOf(self->GetException()) << " on thread:\n"
126899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes           << self->GetException()->Dump();
127899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes      }
128e0918556e7551de638870dcad3f2023f94f85a50Elliott Hughes    }
129e0918556e7551de638870dcad3f2023f94f85a50Elliott Hughes  }
130e0918556e7551de638870dcad3f2023f94f85a50Elliott Hughes};
131e0918556e7551de638870dcad3f2023f94f85a50Elliott Hughes
132ffe6736397d17457188727510f0a2953f69a383aElliott Hughesvoid Runtime::Abort(const char* file, int line) {
133131aef8c94292cc530da2fd91ee98d1432352959Elliott Hughes  // Ensure that we don't have multiple threads trying to abort at once,
134131aef8c94292cc530da2fd91ee98d1432352959Elliott Hughes  // which would result in significantly worse diagnostics.
135131aef8c94292cc530da2fd91ee98d1432352959Elliott Hughes  MutexLock mu(abort_lock_);
136131aef8c94292cc530da2fd91ee98d1432352959Elliott Hughes
137ffe6736397d17457188727510f0a2953f69a383aElliott Hughes  // Get any pending output out of the way.
138ffe6736397d17457188727510f0a2953f69a383aElliott Hughes  fflush(NULL);
139ffe6736397d17457188727510f0a2953f69a383aElliott Hughes
140ffe6736397d17457188727510f0a2953f69a383aElliott Hughes  // Many people have difficulty distinguish aborts from crashes,
141ffe6736397d17457188727510f0a2953f69a383aElliott Hughes  // so be explicit.
142e0918556e7551de638870dcad3f2023f94f85a50Elliott Hughes  AbortState state;
1439ee5f9ccba015beb3ae376ed08b3363bd9df6422Elliott Hughes  LOG(INTERNAL_FATAL) << Dumpable<AbortState>(state);
144ffe6736397d17457188727510f0a2953f69a383aElliott Hughes
145ffe6736397d17457188727510f0a2953f69a383aElliott Hughes  // Perform any platform-specific pre-abort actions.
146ffe6736397d17457188727510f0a2953f69a383aElliott Hughes  PlatformAbort(file, line);
147ffe6736397d17457188727510f0a2953f69a383aElliott Hughes
1486ea095ad30bf13cc00b4fee7afbbe4731a349c83Brian Carlstrom  // use abort hook if we have one
1496ea095ad30bf13cc00b4fee7afbbe4731a349c83Brian Carlstrom  if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
1506ea095ad30bf13cc00b4fee7afbbe4731a349c83Brian Carlstrom    Runtime::Current()->abort_();
1516ea095ad30bf13cc00b4fee7afbbe4731a349c83Brian Carlstrom    // notreached
1526ea095ad30bf13cc00b4fee7afbbe4731a349c83Brian Carlstrom  }
1536ea095ad30bf13cc00b4fee7afbbe4731a349c83Brian Carlstrom
154ffe6736397d17457188727510f0a2953f69a383aElliott Hughes  // If we call abort(3) on a device, all threads in the process
15569759eaa6fd4386f1e6d8748052ad221087b3476Carl Shapiro  // receive SIGABRT.  debuggerd dumps the stack trace of the main
15669759eaa6fd4386f1e6d8748052ad221087b3476Carl Shapiro  // thread, whether or not that was the thread that failed.  By
15769759eaa6fd4386f1e6d8748052ad221087b3476Carl Shapiro  // stuffing a value into a bogus address, we cause a segmentation
158ffe6736397d17457188727510f0a2953f69a383aElliott Hughes  // fault in the current thread, and get a useful log from debuggerd.
159ffe6736397d17457188727510f0a2953f69a383aElliott Hughes  // We can also trivially tell the difference between a VM crash and
160ffe6736397d17457188727510f0a2953f69a383aElliott Hughes  // a deliberate abort by looking at the fault address.
161ffe6736397d17457188727510f0a2953f69a383aElliott Hughes  *reinterpret_cast<char*>(0xdeadd00d) = 38;
162ffe6736397d17457188727510f0a2953f69a383aElliott Hughes  abort();
163ffe6736397d17457188727510f0a2953f69a383aElliott Hughes  // notreached
164ffe6736397d17457188727510f0a2953f69a383aElliott Hughes}
165ffe6736397d17457188727510f0a2953f69a383aElliott Hughes
166bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughesvoid Runtime::CallExitHook(jint status) {
167bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  if (exit_ != NULL) {
168bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes    ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative);
169bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes    exit_(status);
170bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes    LOG(WARNING) << "Exit hook returned instead of exiting!";
171bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  }
172bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes}
173bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes
1748a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
1758a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom// memory sizes.  [kK] indicates kilobytes, [mM] megabytes, and
1768a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom// [gG] gigabytes.
1778a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom//
1788a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom// "s" should point just past the "-Xm?" part of the string.
1798a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
1808a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom// of 1024.
1818a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom//
1828a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom// The spec says the -Xmx and -Xms options must be multiples of 1024.  It
1838a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom// doesn't say anything about -Xss.
1848a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom//
1858a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom// Returns 0 (a useless size) if "s" is malformed or specifies a low or
1868a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom// non-evenly-divisible value.
1878a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom//
188c1f143de3d0370a17a4561eb83bf10a5d7908aa3Elliott Hughessize_t ParseMemoryOption(const char* s, size_t div) {
1898a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom  // strtoul accepts a leading [+-], which we don't want,
1908a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom  // so make sure our string starts with a decimal digit.
1918a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom  if (isdigit(*s)) {
192c1f143de3d0370a17a4561eb83bf10a5d7908aa3Elliott Hughes    const char* s2;
193c1f143de3d0370a17a4561eb83bf10a5d7908aa3Elliott Hughes    size_t val = strtoul(s, (char**)&s2, 10);
1948a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom    if (s2 != s) {
1958a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom      // s2 should be pointing just after the number.
1968a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom      // If this is the end of the string, the user
1978a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom      // has specified a number of bytes.  Otherwise,
1988a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom      // there should be exactly one more character
1998a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom      // that specifies a multiplier.
2008a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom      if (*s2 != '\0') {
201f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom        // The remainder of the string is either a single multiplier
202f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom        // character, or nothing to indicate that the value is in
203f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom        // bytes.
204f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom        char c = *s2++;
205f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom        if (*s2 == '\0') {
206f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom          size_t mul;
207f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom          if (c == '\0') {
208f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom            mul = 1;
209f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom          } else if (c == 'k' || c == 'K') {
210f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom            mul = KB;
211f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom          } else if (c == 'm' || c == 'M') {
212f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom            mul = MB;
213f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom          } else if (c == 'g' || c == 'G') {
214f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom            mul = GB;
2158a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom          } else {
216f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom            // Unknown multiplier character.
2178a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom            return 0;
2188a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom          }
219f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom
220f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom          if (val <= std::numeric_limits<size_t>::max() / mul) {
221f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom            val *= mul;
222f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom          } else {
223f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom            // Clamp to a multiple of 1024.
224f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom            val = std::numeric_limits<size_t>::max() & ~(1024-1);
225f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom          }
226f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom        } else {
227f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom          // There's more than one character after the numeric part.
228f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom          return 0;
229f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom        }
2308a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom      }
2318a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom      // The man page says that a -Xm value must be a multiple of 1024.
2328a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom      if (val % div == 0) {
2338a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom        return val;
2348a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom      }
235fc322c72d7a89a6f7a05c8bdc232d780aecd9232Carl Shapiro    }
236fc322c72d7a89a6f7a05c8bdc232d780aecd9232Carl Shapiro  }
2378a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom  return 0;
238fc322c72d7a89a6f7a05c8bdc232d780aecd9232Carl Shapiro}
239fc322c72d7a89a6f7a05c8bdc232d780aecd9232Carl Shapiro
240f1a5adc87760f938b01df26d906295063546b259Elliott Hughessize_t ParseIntegerOrDie(const std::string& s) {
241f1a5adc87760f938b01df26d906295063546b259Elliott Hughes  std::string::size_type colon = s.find(':');
242f1a5adc87760f938b01df26d906295063546b259Elliott Hughes  if (colon == std::string::npos) {
243bb1e8f0a07c12a8b0a2dd3cab6a1a7e825a54c6fElliott Hughes    LOG(FATAL) << "Missing integer: " << s;
244bb1e8f0a07c12a8b0a2dd3cab6a1a7e825a54c6fElliott Hughes  }
245f1a5adc87760f938b01df26d906295063546b259Elliott Hughes  const char* begin = &s[colon + 1];
246bb1e8f0a07c12a8b0a2dd3cab6a1a7e825a54c6fElliott Hughes  char* end;
247bb1e8f0a07c12a8b0a2dd3cab6a1a7e825a54c6fElliott Hughes  size_t result = strtoul(begin, &end, 10);
248bb1e8f0a07c12a8b0a2dd3cab6a1a7e825a54c6fElliott Hughes  if (begin == end || *end != '\0') {
249bb1e8f0a07c12a8b0a2dd3cab6a1a7e825a54c6fElliott Hughes    LOG(FATAL) << "Failed to parse integer in: " << s;
250bb1e8f0a07c12a8b0a2dd3cab6a1a7e825a54c6fElliott Hughes  }
251bb1e8f0a07c12a8b0a2dd3cab6a1a7e825a54c6fElliott Hughes  return result;
252bb1e8f0a07c12a8b0a2dd3cab6a1a7e825a54c6fElliott Hughes}
253bb1e8f0a07c12a8b0a2dd3cab6a1a7e825a54c6fElliott Hughes
2540af5543f8ea20c3e655b2d748a1b7dcf283792feElliott Hughesvoid LoadJniLibrary(JavaVMExt* vm, const char* name) {
2556cc332ef80edf56221676e33ea16bb4022130344Elliott Hughes  std::string mapped_name(StringPrintf(OS_SHARED_LIB_FORMAT_STR, name));
2567577075b147fd8fa37ca41e7a32d1124676776ceElliott Hughes  std::string reason;
2577577075b147fd8fa37ca41e7a32d1124676776ceElliott Hughes  if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
2580af5543f8ea20c3e655b2d748a1b7dcf283792feElliott Hughes    LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
2590af5543f8ea20c3e655b2d748a1b7dcf283792feElliott Hughes               << reason;
2600af5543f8ea20c3e655b2d748a1b7dcf283792feElliott Hughes  }
2610af5543f8ea20c3e655b2d748a1b7dcf283792feElliott Hughes}
2620af5543f8ea20c3e655b2d748a1b7dcf283792feElliott Hughes
2636ea095ad30bf13cc00b4fee7afbbe4731a349c83Brian CarlstromRuntime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
26490a3369d3b6238f1a4c9b19ca68978dab1c39bc4Elliott Hughes  UniquePtr<ParsedOptions> parsed(new ParsedOptions());
265a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  const char* boot_class_path_string = getenv("BOOTCLASSPATH");
266a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  if (boot_class_path_string != NULL) {
267a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom    parsed->boot_class_path_string_ = boot_class_path_string;
26858ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom  }
269a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  const char* class_path_string = getenv("CLASSPATH");
270a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  if (class_path_string != NULL) {
271a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom    parsed->class_path_string_ = class_path_string;
27258ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom  }
273515a5bc89282d6f910cae4d5852bb77124a47825Elliott Hughes#ifdef NDEBUG
2745174fe6e4e931c423e910366ff22ce0838567940Elliott Hughes  // -Xcheck:jni is off by default for regular builds...
275515a5bc89282d6f910cae4d5852bb77124a47825Elliott Hughes  parsed->check_jni_ = false;
276515a5bc89282d6f910cae4d5852bb77124a47825Elliott Hughes#else
277515a5bc89282d6f910cae4d5852bb77124a47825Elliott Hughes  // ...but on by default in debug builds.
278515a5bc89282d6f910cae4d5852bb77124a47825Elliott Hughes  parsed->check_jni_ = true;
279515a5bc89282d6f910cae4d5852bb77124a47825Elliott Hughes#endif
28085d1545e985ac689db4bad7849880e843707c862Elliott Hughes
2816ea095ad30bf13cc00b4fee7afbbe4731a349c83Brian Carlstrom  parsed->heap_initial_size_ = Heap::kInitialSize;
2826ea095ad30bf13cc00b4fee7afbbe4731a349c83Brian Carlstrom  parsed->heap_maximum_size_ = Heap::kMaximumSize;
283c11607024d2e7a52ca690b8339306cab040e6653jeffhao  parsed->heap_growth_limit_ = 0;  // 0 means no growth limit
284f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom  parsed->stack_size_ = Thread::kDefaultStackSize;
285f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom
286d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes  parsed->is_compiler_ = false;
2879ca7a1fdd0daa546b790adb6a24dbc13c22df76aElliott Hughes  parsed->is_zygote_ = false;
2889ca7a1fdd0daa546b790adb6a24dbc13c22df76aElliott Hughes
289bb1e8f0a07c12a8b0a2dd3cab6a1a7e825a54c6fElliott Hughes  parsed->jni_globals_max_ = 0;
290fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes  parsed->lock_profiling_threshold_ = 0;
291fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes  parsed->hook_is_sensitive_thread_ = NULL;
292fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes
2936ea095ad30bf13cc00b4fee7afbbe4731a349c83Brian Carlstrom  parsed->hook_vfprintf_ = vfprintf;
2946ea095ad30bf13cc00b4fee7afbbe4731a349c83Brian Carlstrom  parsed->hook_exit_ = exit;
2956ea095ad30bf13cc00b4fee7afbbe4731a349c83Brian Carlstrom  parsed->hook_abort_ = abort;
2968a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom
2978a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom  for (size_t i = 0; i < options.size(); ++i) {
298f1a5adc87760f938b01df26d906295063546b259Elliott Hughes    const std::string option(options[i].first);
2990796af03edc06d92bb8d631f1c0c23befdae2315Brian Carlstrom    if (true && options[0].first == "-Xzygote") {
30058ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom      LOG(INFO) << "option[" << i << "]=" << option;
30158ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom    }
302f1a5adc87760f938b01df26d906295063546b259Elliott Hughes    if (StartsWith(option, "-Xbootclasspath:")) {
303a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom      parsed->boot_class_path_string_ = option.substr(strlen("-Xbootclasspath:")).data();
30469b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom    } else if (option == "-classpath" || option == "-cp") {
30569b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom      // TODO: support -Djava.class.path
30669b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom      i++;
30769b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom      if (i == options.size()) {
30869b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom        // TODO: usage
30969b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom        LOG(FATAL) << "Missing required class path value for " << option;
31069b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom        return NULL;
31169b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom      }
312a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom      const StringPiece& value = options[i].first;
313a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom      parsed->class_path_string_ = value.data();
314a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom    } else if (option == "bootclasspath") {
315a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom      parsed->boot_class_path_
316a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom          = reinterpret_cast<const std::vector<const DexFile*>*>(options[i].second);
317f1a5adc87760f938b01df26d906295063546b259Elliott Hughes    } else if (StartsWith(option, "-Ximage:")) {
318a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom      parsed->image_ = option.substr(strlen("-Ximage:")).data();
319f1a5adc87760f938b01df26d906295063546b259Elliott Hughes    } else if (StartsWith(option, "-Xcheck:jni")) {
320515a5bc89282d6f910cae4d5852bb77124a47825Elliott Hughes      parsed->check_jni_ = true;
321f1a5adc87760f938b01df26d906295063546b259Elliott Hughes    } else if (StartsWith(option, "-Xrunjdwp:") || StartsWith(option, "-agentlib:jdwp=")) {
322f1a5adc87760f938b01df26d906295063546b259Elliott Hughes      std::string tail(option.substr(option[1] == 'X' ? 10 : 15));
3233bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
3243bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes        LOG(FATAL) << "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
3253bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes                   << "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n";
3263bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes        return NULL;
3273bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      }
328f1a5adc87760f938b01df26d906295063546b259Elliott Hughes    } else if (StartsWith(option, "-Xms")) {
329f1a5adc87760f938b01df26d906295063546b259Elliott Hughes      size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).c_str(), 1024);
330f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom      if (size == 0) {
331f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom        if (ignore_unrecognized) {
332f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom          continue;
333f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom        }
3344a289ed61242964b921434de7d375f46480472a1Brian Carlstrom        // TODO: usage
33569b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom        LOG(FATAL) << "Failed to parse " << option;
336f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom        return NULL;
337f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom      }
338f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom      parsed->heap_initial_size_ = size;
339f1a5adc87760f938b01df26d906295063546b259Elliott Hughes    } else if (StartsWith(option, "-Xmx")) {
340f1a5adc87760f938b01df26d906295063546b259Elliott Hughes      size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).c_str(), 1024);
341f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom      if (size == 0) {
342f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom        if (ignore_unrecognized) {
343f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom          continue;
344f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom        }
3454a289ed61242964b921434de7d375f46480472a1Brian Carlstrom        // TODO: usage
34669b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom        LOG(FATAL) << "Failed to parse " << option;
347f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom        return NULL;
348f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom      }
349f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom      parsed->heap_maximum_size_ = size;
350f1a5adc87760f938b01df26d906295063546b259Elliott Hughes    } else if (StartsWith(option, "-XX:HeapGrowthLimit=")) {
351f1a5adc87760f938b01df26d906295063546b259Elliott Hughes      size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).c_str(), 1024);
352c11607024d2e7a52ca690b8339306cab040e6653jeffhao      if (size == 0) {
353c11607024d2e7a52ca690b8339306cab040e6653jeffhao        if (ignore_unrecognized) {
354c11607024d2e7a52ca690b8339306cab040e6653jeffhao          continue;
355c11607024d2e7a52ca690b8339306cab040e6653jeffhao        }
356c11607024d2e7a52ca690b8339306cab040e6653jeffhao        // TODO: usage
357c11607024d2e7a52ca690b8339306cab040e6653jeffhao        LOG(FATAL) << "Failed to parse " << option;
358c11607024d2e7a52ca690b8339306cab040e6653jeffhao        return NULL;
359c11607024d2e7a52ca690b8339306cab040e6653jeffhao      }
360c11607024d2e7a52ca690b8339306cab040e6653jeffhao      parsed->heap_growth_limit_ = size;
361f1a5adc87760f938b01df26d906295063546b259Elliott Hughes    } else if (StartsWith(option, "-Xss")) {
362f1a5adc87760f938b01df26d906295063546b259Elliott Hughes      size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).c_str(), 1);
363f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom      if (size == 0) {
364f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom        if (ignore_unrecognized) {
365f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom          continue;
366f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom        }
3674a289ed61242964b921434de7d375f46480472a1Brian Carlstrom        // TODO: usage
36869b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom        LOG(FATAL) << "Failed to parse " << option;
369f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom        return NULL;
370f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom      }
371f734cf55d510976f4862b15e35fc86eae2a3daf8Brian Carlstrom      parsed->stack_size_ = size;
372f1a5adc87760f938b01df26d906295063546b259Elliott Hughes    } else if (StartsWith(option, "-D")) {
373f1a5adc87760f938b01df26d906295063546b259Elliott Hughes      parsed->properties_.push_back(option.substr(strlen("-D")));
374f1a5adc87760f938b01df26d906295063546b259Elliott Hughes    } else if (StartsWith(option, "-Xjnitrace:")) {
375f1a5adc87760f938b01df26d906295063546b259Elliott Hughes      parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:"));
3765de8fe5253ca8bd285cba0eb2e56930573ea4c7fBrian Carlstrom    } else if (option == "compiler") {
377d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes      parsed->is_compiler_ = true;
3789ca7a1fdd0daa546b790adb6a24dbc13c22df76aElliott Hughes    } else if (option == "-Xzygote") {
3799ca7a1fdd0daa546b790adb6a24dbc13c22df76aElliott Hughes      parsed->is_zygote_ = true;
380f1a5adc87760f938b01df26d906295063546b259Elliott Hughes    } else if (StartsWith(option, "-verbose:")) {
3810af5543f8ea20c3e655b2d748a1b7dcf283792feElliott Hughes      std::vector<std::string> verbose_options;
382f1a5adc87760f938b01df26d906295063546b259Elliott Hughes      Split(option.substr(strlen("-verbose:")), ',', verbose_options);
3830af5543f8ea20c3e655b2d748a1b7dcf283792feElliott Hughes      for (size_t i = 0; i < verbose_options.size(); ++i) {
3844dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes        if (verbose_options[i] == "class") {
3854dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes          gLogVerbosity.class_linker = true;
3864dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes        } else if (verbose_options[i] == "compiler") {
3874dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes          gLogVerbosity.compiler = true;
3884dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes        } else if (verbose_options[i] == "heap") {
3894dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes          gLogVerbosity.heap = true;
3904dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes        } else if (verbose_options[i] == "gc") {
3914dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes          gLogVerbosity.gc = true;
3924dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes        } else if (verbose_options[i] == "jdwp") {
3934dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes          gLogVerbosity.jdwp = true;
3944dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes        } else if (verbose_options[i] == "jni") {
3954dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes          gLogVerbosity.jni = true;
3964dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes        } else if (verbose_options[i] == "monitor") {
3974dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes          gLogVerbosity.monitor = true;
3984dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes        } else if (verbose_options[i] == "startup") {
3994dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes          gLogVerbosity.startup = true;
4004dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes        } else if (verbose_options[i] == "third-party-jni") {
4014dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes          gLogVerbosity.third_party_jni = true;
4024dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes        } else if (verbose_options[i] == "threads") {
4034dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes          gLogVerbosity.threads = true;
4044dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes        } else {
4054dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes          LOG(WARNING) << "Ignoring unknown -verbose option: " << verbose_options[i];
4064dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes        }
4070af5543f8ea20c3e655b2d748a1b7dcf283792feElliott Hughes      }
408f1a5adc87760f938b01df26d906295063546b259Elliott Hughes    } else if (StartsWith(option, "-Xjnigreflimit:")) {
409bb1e8f0a07c12a8b0a2dd3cab6a1a7e825a54c6fElliott Hughes      parsed->jni_globals_max_ = ParseIntegerOrDie(option);
410f1a5adc87760f938b01df26d906295063546b259Elliott Hughes    } else if (StartsWith(option, "-Xlockprofthreshold:")) {
411bb1e8f0a07c12a8b0a2dd3cab6a1a7e825a54c6fElliott Hughes      parsed->lock_profiling_threshold_ = ParseIntegerOrDie(option);
412f1a5adc87760f938b01df26d906295063546b259Elliott Hughes    } else if (StartsWith(option, "-Xstacktracefile:")) {
4137c6deaa3382f1f4fb0f591af206f03045c6e9004Brian Carlstrom// always show stack traces in debug builds
4147c6deaa3382f1f4fb0f591af206f03045c6e9004Brian Carlstrom#ifdef NDEBUG
415f1a5adc87760f938b01df26d906295063546b259Elliott Hughes      parsed->stack_trace_file_ = option.substr(strlen("-Xstacktracefile:"));
4167c6deaa3382f1f4fb0f591af206f03045c6e9004Brian Carlstrom#endif
417fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes    } else if (option == "sensitiveThread") {
418fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes      parsed->hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(options[i].second);
4196ea095ad30bf13cc00b4fee7afbbe4731a349c83Brian Carlstrom    } else if (option == "vfprintf") {
4206ea095ad30bf13cc00b4fee7afbbe4731a349c83Brian Carlstrom      parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
4216ea095ad30bf13cc00b4fee7afbbe4731a349c83Brian Carlstrom    } else if (option == "exit") {
4226ea095ad30bf13cc00b4fee7afbbe4731a349c83Brian Carlstrom      parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
4236ea095ad30bf13cc00b4fee7afbbe4731a349c83Brian Carlstrom    } else if (option == "abort") {
4246ea095ad30bf13cc00b4fee7afbbe4731a349c83Brian Carlstrom      parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
42558ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom    } else if (option == "host-prefix") {
42658ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom      parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second);
427b3e66dfcd069db8818cd902d787ff1d7bbca45f2Elliott Hughes    } else if (option == "-Xgenregmap" || option == "-Xgc:precise") {
428b3e66dfcd069db8818cd902d787ff1d7bbca45f2Elliott Hughes      // We silently ignore these for backwards compatibility.
4298a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom    } else {
4308a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom      if (!ignore_unrecognized) {
4316ea095ad30bf13cc00b4fee7afbbe4731a349c83Brian Carlstrom        // TODO: print usage via vfprintf
43227ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom        LOG(ERROR) << "Unrecognized option " << option;
43327ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom        // TODO: this should exit, but for now tolerate unknown options
43427ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom        //return NULL;
4358a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom      }
4368a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom    }
4378a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom  }
4388a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom
439223f20fa939c386c695977263780dea2195093dbBrian Carlstrom  if (!parsed->is_compiler_ && parsed->image_.empty()) {
440223f20fa939c386c695977263780dea2195093dbBrian Carlstrom    parsed->image_ += GetAndroidRoot();
441223f20fa939c386c695977263780dea2195093dbBrian Carlstrom    parsed->image_ += "/framework/boot.art";
4425de8fe5253ca8bd285cba0eb2e56930573ea4c7fBrian Carlstrom  }
443c11607024d2e7a52ca690b8339306cab040e6653jeffhao  if (parsed->heap_growth_limit_ == 0) {
444c11607024d2e7a52ca690b8339306cab040e6653jeffhao    parsed->heap_growth_limit_ = parsed->heap_maximum_size_;
445c11607024d2e7a52ca690b8339306cab040e6653jeffhao  }
4465de8fe5253ca8bd285cba0eb2e56930573ea4c7fBrian Carlstrom
447cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes  LOG(INFO) << "Build type: "
448cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes#ifndef NDEBUG
449cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes            << "debug"
450cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes#else
451cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes            << "optimized"
452cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes#endif
453cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes            << "; CheckJNI: " << (parsed->check_jni_ ? "on" : "off");
45485d1545e985ac689db4bad7849880e843707c862Elliott Hughes
45569b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  return parsed.release();
4568a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom}
4578a436595d36c1e4935984fcac249d7d877e00383Brian Carlstrom
4588a436595d36c1e4935984fcac249d7d877e00383Brian CarlstromRuntime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
4592ed144c2b49ae1da6c464d7a1be0062870530802Carl Shapiro  // TODO: acquire a static mutex on Runtime to avoid racing.
4602ed144c2b49ae1da6c464d7a1be0062870530802Carl Shapiro  if (Runtime::instance_ != NULL) {
4612ed144c2b49ae1da6c464d7a1be0062870530802Carl Shapiro    return NULL;
4622ed144c2b49ae1da6c464d7a1be0062870530802Carl Shapiro  }
463dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes  instance_ = new Runtime;
464dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes  if (!instance_->Init(options, ignore_unrecognized)) {
465dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes    delete instance_;
466dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes    instance_ = NULL;
46761e019d291583029c01b61b93bea750f2b663c37Carl Shapiro  }
46869b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom  return instance_;
46969b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstrom}
4700af5543f8ea20c3e655b2d748a1b7dcf283792feElliott Hughes
471aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstromvoid CreateSystemClassLoader() {
472aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  if (ClassLoader::UseCompileTimeClassPath()) {
473aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    return;
474aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
475aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
476aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  Thread* self = Thread::Current();
477aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
478aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  // Must be in the kNative state for calling native methods.
479aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  CHECK_EQ(self->GetState(), Thread::kNative);
480aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
481aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  JNIEnv* env = self->GetJniEnv();
482df143242f4beaad4cc9fbabebfc033b68c40964eBrian Carlstrom  ScopedLocalRef<jclass> ClassLoader_class(env, env->FindClass("java/lang/ClassLoader"));
483df143242f4beaad4cc9fbabebfc033b68c40964eBrian Carlstrom  CHECK(ClassLoader_class.get() != NULL);
484df143242f4beaad4cc9fbabebfc033b68c40964eBrian Carlstrom  jmethodID getSystemClassLoader = env->GetStaticMethodID(ClassLoader_class.get(),
485df143242f4beaad4cc9fbabebfc033b68c40964eBrian Carlstrom                                                          "getSystemClassLoader",
486df143242f4beaad4cc9fbabebfc033b68c40964eBrian Carlstrom                                                          "()Ljava/lang/ClassLoader;");
487df143242f4beaad4cc9fbabebfc033b68c40964eBrian Carlstrom  CHECK(getSystemClassLoader != NULL);
488df143242f4beaad4cc9fbabebfc033b68c40964eBrian Carlstrom  ScopedLocalRef<jobject> class_loader(env, env->CallStaticObjectMethod(ClassLoader_class.get(),
489df143242f4beaad4cc9fbabebfc033b68c40964eBrian Carlstrom                                                                        getSystemClassLoader));
490df143242f4beaad4cc9fbabebfc033b68c40964eBrian Carlstrom  CHECK(class_loader.get() != NULL);
491df143242f4beaad4cc9fbabebfc033b68c40964eBrian Carlstrom
492df143242f4beaad4cc9fbabebfc033b68c40964eBrian Carlstrom  Thread::Current()->SetClassLoaderOverride(Decode<ClassLoader*>(env, class_loader.get()));
4931b2b2f26c4202020d54cc7a5b9c342e6bca5b680Jesse Wilson
4941b2b2f26c4202020d54cc7a5b9c342e6bca5b680Jesse Wilson  ScopedLocalRef<jclass> Thread_class(env, env->FindClass("java/lang/Thread"));
4951b2b2f26c4202020d54cc7a5b9c342e6bca5b680Jesse Wilson  CHECK(Thread_class.get() != NULL);
4961b2b2f26c4202020d54cc7a5b9c342e6bca5b680Jesse Wilson  jfieldID contextClassLoader = env->GetFieldID(Thread_class.get(),
4971b2b2f26c4202020d54cc7a5b9c342e6bca5b680Jesse Wilson                                                "contextClassLoader",
4981b2b2f26c4202020d54cc7a5b9c342e6bca5b680Jesse Wilson                                                "Ljava/lang/ClassLoader;");
4991b2b2f26c4202020d54cc7a5b9c342e6bca5b680Jesse Wilson  CHECK(contextClassLoader != NULL);
5001b2b2f26c4202020d54cc7a5b9c342e6bca5b680Jesse Wilson  ScopedLocalRef<jobject> self_jobject(env, AddLocalReference<jobject>(env, self->GetPeer()));
5011b2b2f26c4202020d54cc7a5b9c342e6bca5b680Jesse Wilson  env->SetObjectField(self_jobject.get(), contextClassLoader, class_loader.get());
502aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom}
503aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
50469b15fb098162f19a4c20e6dccdcead04d9c77f0Brian Carlstromvoid Runtime::Start() {
5054dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  VLOG(startup) << "Runtime::Start entering";
50658ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom
50758ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom  CHECK(host_prefix_.empty()) << host_prefix_;
50858ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom
509f28bc5b6df6edb4b22aeca360ff102a95f3155e1Brian Carlstrom  // Restore main thread state to kNative as expected by native code
510f28bc5b6df6edb4b22aeca360ff102a95f3155e1Brian Carlstrom  Thread::Current()->SetState(Thread::kNative);
511f28bc5b6df6edb4b22aeca360ff102a95f3155e1Brian Carlstrom
5125d40f182181488eb39ccd19ffd306bb1fb9740c9Brian Carlstrom  started_ = true;
5135d40f182181488eb39ccd19ffd306bb1fb9740c9Brian Carlstrom
514ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  // InitNativeMethods needs to be after started_ so that the classes
515ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  // it touches will have methods linked to the oat file if necessary.
516ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  InitNativeMethods();
517ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom
5189a6bae896a2f003d7216603bf29771d105c10ca4Jesse Wilson  Thread::FinishStartup();
5199a6bae896a2f003d7216603bf29771d105c10ca4Jesse Wilson
520d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  if (!is_zygote_) {
521d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes    DidForkFromZygote();
522d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  }
5239ca7a1fdd0daa546b790adb6a24dbc13c22df76aElliott Hughes
52485d1545e985ac689db4bad7849880e843707c862Elliott Hughes  StartDaemonThreads();
5250a5b14de5115f51e0ed2dd6c3fcc5b84bbce690eBrian Carlstrom
526aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  CreateSystemClassLoader();
527aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
528726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes  Thread::Current()->GetJniEnv()->locals.AssertEmpty();
529726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes
5304dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  VLOG(startup) << "Runtime::Start exiting";
53185d1545e985ac689db4bad7849880e843707c862Elliott Hughes}
53285d1545e985ac689db4bad7849880e843707c862Elliott Hughes
533caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstromvoid Runtime::DidForkFromZygote() {
534caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom  is_zygote_ = false;
535d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes
536caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom  StartSignalCatcher();
537d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes
538d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // Start the JDWP thread. If the command-line debugger flags specified "suspend=y",
539d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // this will pause the runtime, so we probably want this to come last.
540d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  Dbg::StartJdwp();
541caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom}
542caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom
543caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstromvoid Runtime::StartSignalCatcher() {
544caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom  if (!is_zygote_) {
54594ce37a3919a0bdb8855a3d3264a50df1dbc41beElliott Hughes    signal_catcher_ = new SignalCatcher(stack_trace_file_);
546caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom  }
547caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom}
548caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom
54985d1545e985ac689db4bad7849880e843707c862Elliott Hughesvoid Runtime::StartDaemonThreads() {
5504dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  VLOG(startup) << "Runtime::StartDaemonThreads entering";
55185d1545e985ac689db4bad7849880e843707c862Elliott Hughes
552719b323e6a290a075239ac8f4b664e23fd3190f4Elliott Hughes  Thread* self = Thread::Current();
553aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
554aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  // Must be in the kNative state for calling native methods.
555aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  CHECK_EQ(self->GetState(), Thread::kNative);
556aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
557719b323e6a290a075239ac8f4b664e23fd3190f4Elliott Hughes  JNIEnv* env = self->GetJniEnv();
558726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes  ScopedLocalRef<jclass> c(env, env->FindClass("java/lang/Daemons"));
559726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes  CHECK(c.get() != NULL);
560726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes  jmethodID mid = env->GetStaticMethodID(c.get(), "start", "()V");
561719b323e6a290a075239ac8f4b664e23fd3190f4Elliott Hughes  CHECK(mid != NULL);
562726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes  env->CallStaticVoidMethod(c.get(), mid);
5638cf5bc0ab8742df107f0f4fc8ec0e991b6568f11Elliott Hughes  CHECK(!env->ExceptionCheck());
5649ca7a1fdd0daa546b790adb6a24dbc13c22df76aElliott Hughes
5654dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  VLOG(startup) << "Runtime::StartDaemonThreads exiting";
56661e019d291583029c01b61b93bea750f2b663c37Carl Shapiro}
56761e019d291583029c01b61b93bea750f2b663c37Carl Shapiro
5686b3557571e798b60df995f978fa01c0ca1980dfdElliott Hughesbool Runtime::IsShuttingDown() const {
5696b3557571e798b60df995f978fa01c0ca1980dfdElliott Hughes  return shutting_down_;
5706b3557571e798b60df995f978fa01c0ca1980dfdElliott Hughes}
5716b3557571e798b60df995f978fa01c0ca1980dfdElliott Hughes
57258ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrombool Runtime::IsStarted() const {
573dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes  return started_;
574dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes}
575dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes
5760af5543f8ea20c3e655b2d748a1b7dcf283792feElliott Hughesbool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
577c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes  CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
5786ea095ad30bf13cc00b4fee7afbbe4731a349c83Brian Carlstrom
57990a3369d3b6238f1a4c9b19ca68978dab1c39bc4Elliott Hughes  UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
58090a3369d3b6238f1a4c9b19ca68978dab1c39bc4Elliott Hughes  if (options.get() == NULL) {
581e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom    LOG(ERROR) << "Failed to parse options";
5826ea095ad30bf13cc00b4fee7afbbe4731a349c83Brian Carlstrom    return false;
5836ea095ad30bf13cc00b4fee7afbbe4731a349c83Brian Carlstrom  }
5844dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  VLOG(startup) << "Runtime::Init -verbose:startup enabled";
5857ede61eceed2f8da14a4fff05c57c748c160d57fElliott Hughes
586bb1e8f0a07c12a8b0a2dd3cab6a1a7e825a54c6fElliott Hughes  SetJniGlobalsMax(options->jni_globals_max_);
5874dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  Monitor::Init(options->lock_profiling_threshold_, options->hook_is_sensitive_thread_);
58832d6e1e5654433d7eadede89e1c770b2c839aee9Elliott Hughes
58958ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom  host_prefix_ = options->host_prefix_;
590a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  boot_class_path_string_ = options->boot_class_path_string_;
591a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  class_path_string_ = options->class_path_string_;
5927ede61eceed2f8da14a4fff05c57c748c160d57fElliott Hughes  properties_ = options->properties_;
5937ede61eceed2f8da14a4fff05c57c748c160d57fElliott Hughes
594d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes  is_compiler_ = options->is_compiler_;
5959ca7a1fdd0daa546b790adb6a24dbc13c22df76aElliott Hughes  is_zygote_ = options->is_zygote_;
5969ca7a1fdd0daa546b790adb6a24dbc13c22df76aElliott Hughes
5970af5543f8ea20c3e655b2d748a1b7dcf283792feElliott Hughes  vfprintf_ = options->hook_vfprintf_;
5980af5543f8ea20c3e655b2d748a1b7dcf283792feElliott Hughes  exit_ = options->hook_exit_;
5990af5543f8ea20c3e655b2d748a1b7dcf283792feElliott Hughes  abort_ = options->hook_abort_;
6006ea095ad30bf13cc00b4fee7afbbe4731a349c83Brian Carlstrom
601be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  default_stack_size_ = options->stack_size_;
60294ce37a3919a0bdb8855a3d3264a50df1dbc41beElliott Hughes  stack_trace_file_ = options->stack_trace_file_;
6036ea095ad30bf13cc00b4fee7afbbe4731a349c83Brian Carlstrom
604c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes  monitor_list_ = new MonitorList;
6054dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  thread_list_ = new ThreadList;
606cf4c6c41b0084dc4567ff709fb8ce9ebd72b26acElliott Hughes  intern_table_ = new InternTable;
607cf4c6c41b0084dc4567ff709fb8ce9ebd72b26acElliott Hughes
6084dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  Heap::Init(options->heap_initial_size_,
609c11607024d2e7a52ca690b8339306cab040e6653jeffhao             options->heap_growth_limit_,
61030fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers             options->heap_maximum_size_,
611223f20fa939c386c695977263780dea2195093dbBrian Carlstrom             options->image_);
6126ea095ad30bf13cc00b4fee7afbbe4731a349c83Brian Carlstrom
613c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes  BlockSignals();
614c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes
615a09576416788b916095739e43a16917e7948f3a4Elliott Hughes  java_vm_ = new JavaVMExt(this, options.get());
616515a5bc89282d6f910cae4d5852bb77124a47825Elliott Hughes
617be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes  Thread::Startup();
618d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes
6195fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes  // ClassLinker needs an attached thread, but we can't fully attach a thread
6205fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes  // without creating objects.
6215fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes  Thread::Attach(this, "main", false);
6226ea095ad30bf13cc00b4fee7afbbe4731a349c83Brian Carlstrom
623f28bc5b6df6edb4b22aeca360ff102a95f3155e1Brian Carlstrom  // Set us to runnable so tools using a runtime can allocate and GC by default
624f28bc5b6df6edb4b22aeca360ff102a95f3155e1Brian Carlstrom  Thread::Current()->SetState(Thread::kRunnable);
625f28bc5b6df6edb4b22aeca360ff102a95f3155e1Brian Carlstrom
62658ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom  CHECK_GE(Heap::GetSpaces().size(), 1U);
627a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  if (Heap::GetSpaces()[0]->IsImageSpace()) {
628a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom    class_linker_ = ClassLinker::CreateFromImage(intern_table_);
629a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  } else {
630a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom    CHECK(options->boot_class_path_ != NULL);
631a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom    CHECK_NE(options->boot_class_path_->size(), 0U);
632a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom    class_linker_ = ClassLinker::CreateFromCompiler(*options->boot_class_path_, intern_table_);
633a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  }
634a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom  CHECK(class_linker_ != NULL);
6356ea095ad30bf13cc00b4fee7afbbe4731a349c83Brian Carlstrom
6364dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  VLOG(startup) << "Runtime::Init exiting";
6377b21670581d13db32f1384a3b2692bcfc8f57320Carl Shapiro  return true;
6387b21670581d13db32f1384a3b2692bcfc8f57320Carl Shapiro}
6397b21670581d13db32f1384a3b2692bcfc8f57320Carl Shapiro
640038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesvoid Runtime::InitNativeMethods() {
6414dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  VLOG(startup) << "Runtime::InitNativeMethods entering";
642ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes  Thread* self = Thread::Current();
643ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes  JNIEnv* env = self->GetJniEnv();
644ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes
645418d20fc407052d4152157f61e7453359f902383Elliott Hughes  // Must be in the kNative state for calling native methods (JNI_OnLoad code).
646aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  CHECK_EQ(self->GetState(), Thread::kNative);
647ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes
648418d20fc407052d4152157f61e7453359f902383Elliott Hughes  // First set up JniConstants, which is used by both the runtime's built-in native
649418d20fc407052d4152157f61e7453359f902383Elliott Hughes  // methods and libcore.
650fea966e51e65f2040e1510a2e39d407baad29b2cElliott Hughes  JniConstants::init(env);
651fea966e51e65f2040e1510a2e39d407baad29b2cElliott Hughes
652418d20fc407052d4152157f61e7453359f902383Elliott Hughes  // Then set up the native methods provided by the runtime itself.
653ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes  RegisterRuntimeNativeMethods(env);
654ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes
655418d20fc407052d4152157f61e7453359f902383Elliott Hughes  // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
656418d20fc407052d4152157f61e7453359f902383Elliott Hughes  // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
657418d20fc407052d4152157f61e7453359f902383Elliott Hughes  // the library that implements System.loadLibrary!
658ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes  LoadJniLibrary(instance_->GetJavaVM(), "javacore");
6594dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  VLOG(startup) << "Runtime::InitNativeMethods exiting";
660ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes}
661ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes
662ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughesvoid Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
663ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
6645167c97a4f672ba821453418e3943988fabbfc43Ian Rogers  // Register Throwable first so that registration of other native methods can throw exceptions
6655167c97a4f672ba821453418e3943988fabbfc43Ian Rogers  REGISTER(register_java_lang_Throwable);
666f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom  REGISTER(register_dalvik_system_DexFile);
6679d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  REGISTER(register_dalvik_system_VMDebug);
6687ede61eceed2f8da14a4fff05c57c748c160d57fElliott Hughes  REGISTER(register_dalvik_system_VMRuntime);
6698daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  REGISTER(register_dalvik_system_VMStack);
67001158d7a57c8321370667a6045220237d16e0da8Elliott Hughes  REGISTER(register_dalvik_system_Zygote);
671d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes  REGISTER(register_java_lang_Class);
672bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  REGISTER(register_java_lang_Object);
673bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  REGISTER(register_java_lang_Runtime);
674bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  REGISTER(register_java_lang_String);
675bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  REGISTER(register_java_lang_System);
6768daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  REGISTER(register_java_lang_Thread);
67764bf5a33d55aa779ef452552a466943002d39e4fElliott Hughes  REGISTER(register_java_lang_VMClassLoader);
6785b8e4c810a97c9dc417142b8c6e07871ae15c797Brian Carlstrom  REGISTER(register_java_lang_reflect_Array);
6792a20cfd0b7fc81099f5de0da782ebcc1cb262792Elliott Hughes  REGISTER(register_java_lang_reflect_Constructor);
680f867b6f706818c886087f61b89d1e8f5fc4653cfBrian Carlstrom  REGISTER(register_java_lang_reflect_Field);
681f867b6f706818c886087f61b89d1e8f5fc4653cfBrian Carlstrom  REGISTER(register_java_lang_reflect_Method);
68295caa791e560da97363c0c0d22bfda4a7e7377c3Jesse Wilson  REGISTER(register_java_lang_reflect_Proxy);
683bf86d0438e9ef9c145ebcf16a2e74c4efaa2686aElliott Hughes  REGISTER(register_java_util_concurrent_atomic_AtomicLong);
684395520eaa47eca25b92e86188accf3095d60af49Brian Carlstrom  REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
685f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
6865ee7a8b56dee896245e0f4ae5215f8b7376c1787Elliott Hughes  REGISTER(register_sun_misc_Unsafe);
687ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes#undef REGISTER
688ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes}
689ad7c2a3b4daa2abd707375444f4b0db7d69a3838Elliott Hughes
6908daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughesvoid Runtime::Dump(std::ostream& os) {
691e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes  // TODO: dump other runtime statistics?
692cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes  GetClassLinker()->DumpForSigQuit(os);
693cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes  GetInternTable()->DumpForSigQuit(os);
69442ee14279065352a4b9a3e8028d02c567e847d05Elliott Hughes  os << "\n";
6958daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes
6968daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes  thread_list_->Dump(os);
697e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes}
698e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes
69921a5bf2dc8c9cb3fbe5a30a88d1149c328e3aacaElliott Hughesvoid Runtime::DumpLockHolders(std::ostream& os) {
70021a5bf2dc8c9cb3fbe5a30a88d1149c328e3aacaElliott Hughes  pid_t heap_lock_owner = Heap::GetLockOwner();
70121a5bf2dc8c9cb3fbe5a30a88d1149c328e3aacaElliott Hughes  pid_t thread_list_lock_owner = GetThreadList()->GetLockOwner();
70221a5bf2dc8c9cb3fbe5a30a88d1149c328e3aacaElliott Hughes  pid_t classes_lock_owner = GetClassLinker()->GetClassesLockOwner();
70321a5bf2dc8c9cb3fbe5a30a88d1149c328e3aacaElliott Hughes  pid_t dex_lock_owner = GetClassLinker()->GetDexLockOwner();
70421a5bf2dc8c9cb3fbe5a30a88d1149c328e3aacaElliott Hughes  if ((heap_lock_owner | thread_list_lock_owner | classes_lock_owner | dex_lock_owner) != 0) {
70521a5bf2dc8c9cb3fbe5a30a88d1149c328e3aacaElliott Hughes    os << "Heap lock owner tid: " << heap_lock_owner << "\n"
70621a5bf2dc8c9cb3fbe5a30a88d1149c328e3aacaElliott Hughes       << "ThreadList lock owner tid: " << thread_list_lock_owner << "\n"
70721a5bf2dc8c9cb3fbe5a30a88d1149c328e3aacaElliott Hughes       << "ClassLinker classes lock owner tid: " << classes_lock_owner << "\n"
70821a5bf2dc8c9cb3fbe5a30a88d1149c328e3aacaElliott Hughes       << "ClassLinker dex lock owner tid: " << dex_lock_owner << "\n";
70921a5bf2dc8c9cb3fbe5a30a88d1149c328e3aacaElliott Hughes  }
71021a5bf2dc8c9cb3fbe5a30a88d1149c328e3aacaElliott Hughes}
71121a5bf2dc8c9cb3fbe5a30a88d1149c328e3aacaElliott Hughes
7129d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughesvoid Runtime::SetStatsEnabled(bool new_state) {
7139d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  if (new_state == true) {
7149d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    GetStats()->Clear(~0);
7159d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    // TODO: wouldn't it make more sense to clear _all_ threads' stats?
7169d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    Thread::Current()->GetStats()->Clear(~0);
7179d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  }
7189d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  stats_enabled_ = new_state;
7199d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
7209d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
7219d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughesvoid Runtime::ResetStats(int kinds) {
7229d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  GetStats()->Clear(kinds & 0xffff);
7239d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  // TODO: wouldn't it make more sense to clear _all_ threads' stats?
7249d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  Thread::Current()->GetStats()->Clear(kinds >> 16);
7259d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
7269d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
7279d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott HughesRuntimeStats* Runtime::GetStats() {
7289d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  return &stats_;
7299d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
7309d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
7319d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughesint32_t Runtime::GetStat(int kind) {
7329d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  RuntimeStats* stats;
7339d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  if (kind < (1<<16)) {
7349d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    stats = GetStats();
7359d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  } else {
7369d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    stats = Thread::Current()->GetStats();
7379d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    kind >>= 16;
7389d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  }
7399d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  switch (kind) {
7409d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  case KIND_ALLOCATED_OBJECTS:
7419d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    return stats->allocated_objects;
7429d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  case KIND_ALLOCATED_BYTES:
7439d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    return stats->allocated_bytes;
7449d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  case KIND_FREED_OBJECTS:
7459d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    return stats->freed_objects;
7469d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  case KIND_FREED_BYTES:
7479d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    return stats->freed_bytes;
7489d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  case KIND_GC_INVOCATIONS:
7499d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    return stats->gc_for_alloc_count;
7509d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  case KIND_CLASS_INIT_COUNT:
7519d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    return stats->class_init_count;
7529d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  case KIND_CLASS_INIT_TIME:
7539d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    // Convert ns to us, reduce to 32 bits.
7549d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    return (int) (stats->class_init_time_ns / 1000);
7559d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  case KIND_EXT_ALLOCATED_OBJECTS:
7569d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  case KIND_EXT_ALLOCATED_BYTES:
7579d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  case KIND_EXT_FREED_OBJECTS:
7589d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  case KIND_EXT_FREED_BYTES:
7599d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    return 0;  // backward compatibility
7609d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  default:
7619d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    CHECK(false);
7629d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes    return -1; // unreachable
7639d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes  }
7649d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes}
7659d5ccec86d60c9ddd811836b9a2bc28d0b3d11feElliott Hughes
766c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughesvoid Runtime::BlockSignals() {
767c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes  sigset_t sigset;
768c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes  if (sigemptyset(&sigset) == -1) {
769c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes    PLOG(FATAL) << "sigemptyset failed";
770c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes  }
771c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes  if (sigaddset(&sigset, SIGPIPE) == -1) {
772c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes    PLOG(ERROR) << "sigaddset SIGPIPE failed";
773c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes  }
774c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes  // SIGQUIT is used to dump the runtime's state (including stack traces).
775c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes  if (sigaddset(&sigset, SIGQUIT) == -1) {
776c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes    PLOG(ERROR) << "sigaddset SIGQUIT failed";
777c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes  }
778c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes  // SIGUSR1 is used to initiate a heap dump.
779c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes  if (sigaddset(&sigset, SIGUSR1) == -1) {
780c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes    PLOG(ERROR) << "sigaddset SIGUSR1 failed";
781c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes  }
782c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes  CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
783c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes}
784c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes
7855fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughesvoid Runtime::AttachCurrentThread(const char* name, bool as_daemon) {
7865fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes  Thread::Attach(instance_, name, as_daemon);
78761e019d291583029c01b61b93bea750f2b663c37Carl Shapiro}
78861e019d291583029c01b61b93bea750f2b663c37Carl Shapiro
789d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughesvoid Runtime::DetachCurrentThread() {
790038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes  // TODO: check we're not calling DetachCurrentThread from a call stack that
791038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes  // includes managed frames. (It's only valid if the stack is all-native.)
79202b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes  thread_list_->Unregister();
7937b21670581d13db32f1384a3b2692bcfc8f57320Carl Shapiro}
7947b21670581d13db32f1384a3b2692bcfc8f57320Carl Shapiro
795e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstromvoid Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
796bfe487be25652c5456236661b9d9c3579d2296c1Elliott Hughes  Dbg::VisitRoots(visitor, arg);
797e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  class_linker_->VisitRoots(visitor, arg);
798e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  intern_table_->VisitRoots(visitor, arg);
799e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  java_vm_->VisitRoots(visitor, arg);
800e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  thread_list_->VisitRoots(visitor, arg);
801e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  visitor(jni_stub_array_, arg);
802e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  visitor(abstract_method_error_stub_array_, arg);
8034f0d07c783afef89703dce32c94440fc8621a29bIan Rogers  for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
8044f0d07c783afef89703dce32c94440fc8621a29bIan Rogers    visitor(resolution_stub_array_[i], arg);
8054f0d07c783afef89703dce32c94440fc8621a29bIan Rogers  }
8061984651929744dd603fd082e23eacd877b9bc177Ian Rogers  visitor(resolution_method_, arg);
8074f0d07c783afef89703dce32c94440fc8621a29bIan Rogers  for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
8084f0d07c783afef89703dce32c94440fc8621a29bIan Rogers    visitor(callee_save_method_[i], arg);
8094f0d07c783afef89703dce32c94440fc8621a29bIan Rogers  }
810e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom}
811e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
812169c9a7f46776b235d0a37d5e0ff27682deffe06Ian Rogersbool Runtime::HasJniDlsymLookupStub() const {
813e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  return jni_stub_array_ != NULL;
814e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom}
815e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
816169c9a7f46776b235d0a37d5e0ff27682deffe06Ian RogersByteArray* Runtime::GetJniDlsymLookupStub() const {
817e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  CHECK(jni_stub_array_ != NULL);
818e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  return jni_stub_array_;
819e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom}
820e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
821169c9a7f46776b235d0a37d5e0ff27682deffe06Ian Rogersvoid Runtime::SetJniDlsymLookupStub(ByteArray* jni_stub_array) {
822aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  CHECK(jni_stub_array != NULL)  << " jni_stub_array=" << jni_stub_array;
823aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array)
824aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom      << "jni_stub_array_=" << jni_stub_array_ << " jni_stub_array=" << jni_stub_array;
825e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  jni_stub_array_ = jni_stub_array;
826e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom}
827e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
828e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrombool Runtime::HasAbstractMethodErrorStubArray() const {
829e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  return abstract_method_error_stub_array_ != NULL;
830e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom}
831e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
832e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian CarlstromByteArray* Runtime::GetAbstractMethodErrorStubArray() const {
833e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  CHECK(abstract_method_error_stub_array_ != NULL);
834e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  return abstract_method_error_stub_array_;
835e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom}
836e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
837e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstromvoid Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) {
838e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  CHECK(abstract_method_error_stub_array != NULL);
839e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array);
840e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  abstract_method_error_stub_array_ = abstract_method_error_stub_array;
841e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom}
842e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
8431cb0a1dfc32531c79a968aeac26ccb5525862497Ian Rogers
8441cb0a1dfc32531c79a968aeac26ccb5525862497Ian RogersRuntime::TrampolineType Runtime::GetTrampolineType(Method* method) {
8451cb0a1dfc32531c79a968aeac26ccb5525862497Ian Rogers  if (method == NULL) {
8461cb0a1dfc32531c79a968aeac26ccb5525862497Ian Rogers    return Runtime::kUnknownMethod;
8471cb0a1dfc32531c79a968aeac26ccb5525862497Ian Rogers  } else if (method->IsStatic()) {
8481cb0a1dfc32531c79a968aeac26ccb5525862497Ian Rogers    return Runtime::kStaticMethod;
8491cb0a1dfc32531c79a968aeac26ccb5525862497Ian Rogers  } else {
8501cb0a1dfc32531c79a968aeac26ccb5525862497Ian Rogers    return Runtime::kInstanceMethod;
8511cb0a1dfc32531c79a968aeac26ccb5525862497Ian Rogers  }
8521cb0a1dfc32531c79a968aeac26ccb5525862497Ian Rogers}
8531cb0a1dfc32531c79a968aeac26ccb5525862497Ian Rogers
8541cb0a1dfc32531c79a968aeac26ccb5525862497Ian Rogersbool Runtime::HasResolutionStubArray(TrampolineType type) const {
8551cb0a1dfc32531c79a968aeac26ccb5525862497Ian Rogers  return resolution_stub_array_[type] != NULL;
856ad25ac568407ceb14334e8551dd1c4dd0fd6993cIan Rogers}
857ad25ac568407ceb14334e8551dd1c4dd0fd6993cIan Rogers
8581cb0a1dfc32531c79a968aeac26ccb5525862497Ian RogersByteArray* Runtime::GetResolutionStubArray(TrampolineType type) const {
8591cb0a1dfc32531c79a968aeac26ccb5525862497Ian Rogers  CHECK(HasResolutionStubArray(type));
8604f0d07c783afef89703dce32c94440fc8621a29bIan Rogers  DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastTrampolineMethodType));
8611cb0a1dfc32531c79a968aeac26ccb5525862497Ian Rogers  return resolution_stub_array_[type];
862ad25ac568407ceb14334e8551dd1c4dd0fd6993cIan Rogers}
863ad25ac568407ceb14334e8551dd1c4dd0fd6993cIan Rogers
8641cb0a1dfc32531c79a968aeac26ccb5525862497Ian Rogersvoid Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) {
865ad25ac568407ceb14334e8551dd1c4dd0fd6993cIan Rogers  CHECK(resolution_stub_array != NULL);
8661cb0a1dfc32531c79a968aeac26ccb5525862497Ian Rogers  CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array);
8671cb0a1dfc32531c79a968aeac26ccb5525862497Ian Rogers  resolution_stub_array_[type] = resolution_stub_array;
868ad25ac568407ceb14334e8551dd1c4dd0fd6993cIan Rogers}
869ad25ac568407ceb14334e8551dd1c4dd0fd6993cIan Rogers
8701984651929744dd603fd082e23eacd877b9bc177Ian RogersMethod* Runtime::CreateResolutionMethod() {
8711984651929744dd603fd082e23eacd877b9bc177Ian Rogers  Class* method_class = Method::GetMethodClass();
8721984651929744dd603fd082e23eacd877b9bc177Ian Rogers  SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
8731984651929744dd603fd082e23eacd877b9bc177Ian Rogers  method->SetDeclaringClass(method_class);
8741984651929744dd603fd082e23eacd877b9bc177Ian Rogers  // TODO: use a special method for resolution method saves
8751984651929744dd603fd082e23eacd877b9bc177Ian Rogers  method->SetDexMethodIndex(DexFile::kDexNoIndex16);
8761984651929744dd603fd082e23eacd877b9bc177Ian Rogers  ByteArray* unknown_resolution_stub = GetResolutionStubArray(kUnknownMethod);
8771984651929744dd603fd082e23eacd877b9bc177Ian Rogers  CHECK(unknown_resolution_stub != NULL);
8781984651929744dd603fd082e23eacd877b9bc177Ian Rogers  method->SetCode(unknown_resolution_stub->GetData());
8791984651929744dd603fd082e23eacd877b9bc177Ian Rogers  return method.get();
8801984651929744dd603fd082e23eacd877b9bc177Ian Rogers}
8811984651929744dd603fd082e23eacd877b9bc177Ian Rogers
8821984651929744dd603fd082e23eacd877b9bc177Ian Rogersbool Runtime::HasResolutionMethod() const {
8831984651929744dd603fd082e23eacd877b9bc177Ian Rogers  return resolution_method_ != NULL;
8841984651929744dd603fd082e23eacd877b9bc177Ian Rogers}
8851984651929744dd603fd082e23eacd877b9bc177Ian Rogers
8861984651929744dd603fd082e23eacd877b9bc177Ian Rogers// Returns a special method that calls into a trampoline for runtime method resolution
8871984651929744dd603fd082e23eacd877b9bc177Ian RogersMethod* Runtime::GetResolutionMethod() const {
8881984651929744dd603fd082e23eacd877b9bc177Ian Rogers  CHECK(HasResolutionMethod());
8891984651929744dd603fd082e23eacd877b9bc177Ian Rogers  return resolution_method_;
8901984651929744dd603fd082e23eacd877b9bc177Ian Rogers}
8911984651929744dd603fd082e23eacd877b9bc177Ian Rogers
8921984651929744dd603fd082e23eacd877b9bc177Ian Rogersvoid Runtime::SetResolutionMethod(Method* method) {
8931984651929744dd603fd082e23eacd877b9bc177Ian Rogers  resolution_method_ = method;
8941984651929744dd603fd082e23eacd877b9bc177Ian Rogers}
8951984651929744dd603fd082e23eacd877b9bc177Ian Rogers
896ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian CarlstromMethod* Runtime::CreateCalleeSaveMethod(InstructionSet instruction_set, CalleeSaveType type) {
897ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers  Class* method_class = Method::GetMethodClass();
89840381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom  SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
899ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers  method->SetDeclaringClass(method_class);
9006d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  // TODO: use a special method for callee saves
9011984651929744dd603fd082e23eacd877b9bc177Ian Rogers  method->SetDexMethodIndex(DexFile::kDexNoIndex16);
9023320cf46afd082398aa401b246e6f301cebdf64dBrian Carlstrom  method->SetCode(NULL);
903ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  if ((instruction_set == kThumb2) || (instruction_set == kArm)) {
9044f0d07c783afef89703dce32c94440fc8621a29bIan Rogers    uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6)  | (1 << art::arm::R7) |
9054f0d07c783afef89703dce32c94440fc8621a29bIan Rogers                          (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11);
9064f0d07c783afef89703dce32c94440fc8621a29bIan Rogers    uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3);
9074f0d07c783afef89703dce32c94440fc8621a29bIan Rogers    uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9);
9084f0d07c783afef89703dce32c94440fc8621a29bIan Rogers    uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills :0) |
9094f0d07c783afef89703dce32c94440fc8621a29bIan Rogers                           (type == kSaveAll ? all_spills :0) | (1 << art::arm::LR);
9104f0d07c783afef89703dce32c94440fc8621a29bIan Rogers    uint32_t fp_all_spills = (1 << art::arm::S0)  | (1 << art::arm::S1)  | (1 << art::arm::S2) |
9114f0d07c783afef89703dce32c94440fc8621a29bIan Rogers                             (1 << art::arm::S3)  | (1 << art::arm::S4)  | (1 << art::arm::S5) |
9124f0d07c783afef89703dce32c94440fc8621a29bIan Rogers                             (1 << art::arm::S6)  | (1 << art::arm::S7)  | (1 << art::arm::S8) |
9134f0d07c783afef89703dce32c94440fc8621a29bIan Rogers                             (1 << art::arm::S9)  | (1 << art::arm::S10) | (1 << art::arm::S11) |
9144f0d07c783afef89703dce32c94440fc8621a29bIan Rogers                             (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) |
9154f0d07c783afef89703dce32c94440fc8621a29bIan Rogers                             (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) |
9164f0d07c783afef89703dce32c94440fc8621a29bIan Rogers                             (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) |
9174f0d07c783afef89703dce32c94440fc8621a29bIan Rogers                             (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) |
9184f0d07c783afef89703dce32c94440fc8621a29bIan Rogers                             (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) |
9194f0d07c783afef89703dce32c94440fc8621a29bIan Rogers                             (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
9204f0d07c783afef89703dce32c94440fc8621a29bIan Rogers                             (1 << art::arm::S30) | (1 << art::arm::S31);
9214f0d07c783afef89703dce32c94440fc8621a29bIan Rogers    uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
9224f0d07c783afef89703dce32c94440fc8621a29bIan Rogers    size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
9234f0d07c783afef89703dce32c94440fc8621a29bIan Rogers                                 __builtin_popcount(fp_spills) /* fprs */ +
9244f0d07c783afef89703dce32c94440fc8621a29bIan Rogers                                 1 /* Method* */) * kPointerSize, kStackAlignment);
92515fdb8cfb5b2e3fc882113ec0648d492cebf852cIan Rogers    method->SetFrameSizeInBytes(frame_size);
9264f0d07c783afef89703dce32c94440fc8621a29bIan Rogers    method->SetCoreSpillMask(core_spills);
9274f0d07c783afef89703dce32c94440fc8621a29bIan Rogers    method->SetFpSpillMask(fp_spills);
928ae826983f7903bc0a6bbbe8426bf393fb2f6d747Brian Carlstrom  } else if (instruction_set == kX86) {
929ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers    method->SetFrameSizeInBytes(32);
9304f0d07c783afef89703dce32c94440fc8621a29bIan Rogers    method->SetCoreSpillMask((1 << art::x86::EBX) | (1 << art::x86::EBP) | (1 << art::x86::ESI) |
931ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers                             (1 << art::x86::EDI));
932ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers    method->SetFpSpillMask(0);
933ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers  } else {
934ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers    UNIMPLEMENTED(FATAL);
935ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers  }
93640381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom  return method.get();
937ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers}
938ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers
9394f0d07c783afef89703dce32c94440fc8621a29bIan Rogersbool Runtime::HasCalleeSaveMethod(CalleeSaveType type) const {
9404f0d07c783afef89703dce32c94440fc8621a29bIan Rogers  return callee_save_method_[type] != NULL;
941e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom}
942410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes
943e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom// Returns a special method that describes all callee saves being spilled to the stack.
9444f0d07c783afef89703dce32c94440fc8621a29bIan RogersMethod* Runtime::GetCalleeSaveMethod(CalleeSaveType type) const {
9454f0d07c783afef89703dce32c94440fc8621a29bIan Rogers  CHECK(HasCalleeSaveMethod(type));
9464f0d07c783afef89703dce32c94440fc8621a29bIan Rogers  return callee_save_method_[type];
9471f87008b165d26541d832ff805250afdc89c253dBrian Carlstrom}
9481f87008b165d26541d832ff805250afdc89c253dBrian Carlstrom
9494f0d07c783afef89703dce32c94440fc8621a29bIan Rogersvoid Runtime::SetCalleeSaveMethod(Method* method, CalleeSaveType type) {
9504f0d07c783afef89703dce32c94440fc8621a29bIan Rogers  DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
9514f0d07c783afef89703dce32c94440fc8621a29bIan Rogers  callee_save_method_[type] = method;
952e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom}
953e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom
9542692b573a56cd63a3c8c8aa1636e3766b6d8c9c4jeffhaovoid Runtime::EnableMethodTracing(Trace* tracer) {
9552692b573a56cd63a3c8c8aa1636e3766b6d8c9c4jeffhao  CHECK(!IsMethodTracingActive());
9562692b573a56cd63a3c8c8aa1636e3766b6d8c9c4jeffhao  tracer_ = tracer;
9572692b573a56cd63a3c8c8aa1636e3766b6d8c9c4jeffhao}
9582692b573a56cd63a3c8c8aa1636e3766b6d8c9c4jeffhao
9592692b573a56cd63a3c8c8aa1636e3766b6d8c9c4jeffhaovoid Runtime::DisableMethodTracing() {
9602692b573a56cd63a3c8c8aa1636e3766b6d8c9c4jeffhao  CHECK(IsMethodTracingActive());
9612692b573a56cd63a3c8c8aa1636e3766b6d8c9c4jeffhao  delete tracer_;
9622692b573a56cd63a3c8c8aa1636e3766b6d8c9c4jeffhao  tracer_ = NULL;
9632692b573a56cd63a3c8c8aa1636e3766b6d8c9c4jeffhao}
9642692b573a56cd63a3c8c8aa1636e3766b6d8c9c4jeffhao
9652692b573a56cd63a3c8c8aa1636e3766b6d8c9c4jeffhaobool Runtime::IsMethodTracingActive() const {
9662692b573a56cd63a3c8c8aa1636e3766b6d8c9c4jeffhao  return (tracer_ != NULL);
9672692b573a56cd63a3c8c8aa1636e3766b6d8c9c4jeffhao}
9682692b573a56cd63a3c8c8aa1636e3766b6d8c9c4jeffhao
9692692b573a56cd63a3c8c8aa1636e3766b6d8c9c4jeffhaoTrace* Runtime::GetTracer() const {
9702692b573a56cd63a3c8c8aa1636e3766b6d8c9c4jeffhao  CHECK(IsMethodTracingActive());
9712692b573a56cd63a3c8c8aa1636e3766b6d8c9c4jeffhao  return tracer_;
9722692b573a56cd63a3c8c8aa1636e3766b6d8c9c4jeffhao}
9732692b573a56cd63a3c8c8aa1636e3766b6d8c9c4jeffhao
9746b6b5f0e67ce03f38223a525612955663bc1799bCarl Shapiro}  // namespace art
975