blob: 10b099d48aa4bfbbffcdfca642d252a37d6a11cf [file] [log] [blame]
Elliott Hughes8d768a92011-09-14 16:35:25 -07001/*
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 Shapirob5573532011-07-12 18:22:59 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "thread.h"
Carl Shapirob5573532011-07-12 18:22:59 -070018
Elliott Hughes8d768a92011-09-14 16:35:25 -070019#include <dynamic_annotations.h>
Ian Rogersb033c752011-07-20 12:22:35 -070020#include <pthread.h>
21#include <sys/mman.h>
Brian Carlstromdbf05b72011-12-15 00:55:24 -080022#include <sys/resource.h>
23#include <sys/time.h>
Elliott Hughesa0957642011-09-02 14:27:33 -070024
Carl Shapirob5573532011-07-12 18:22:59 -070025#include <algorithm>
Elliott Hughesdcc24742011-09-07 14:02:44 -070026#include <bitset>
Elliott Hugheseb4f6142011-07-15 17:43:51 -070027#include <cerrno>
Elliott Hughesa0957642011-09-02 14:27:33 -070028#include <iostream>
Carl Shapirob5573532011-07-12 18:22:59 -070029#include <list>
Carl Shapirob5573532011-07-12 18:22:59 -070030
Elliott Hughes872d4ec2011-10-21 17:07:15 -070031#include "debugger.h"
Elliott Hughesa5b897e2011-08-16 11:33:06 -070032#include "class_linker.h"
Brian Carlstromdf143242011-10-10 18:05:34 -070033#include "class_loader.h"
Ian Rogersd6b1f612011-09-27 13:38:14 -070034#include "dex_verifier.h"
Ian Rogers408f79a2011-08-23 18:22:33 -070035#include "heap.h"
Elliott Hughesc5f7c912011-08-18 14:00:42 -070036#include "jni_internal.h"
Elliott Hughes8e4aac52011-09-26 17:03:36 -070037#include "monitor.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070038#include "oat/runtime/context.h"
Elliott Hughesa5b897e2011-08-16 11:33:06 -070039#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080040#include "object_utils.h"
Jesse Wilson9a6bae82011-11-14 14:57:30 -050041#include "reflection.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070042#include "runtime.h"
buzbee54330722011-08-23 16:46:55 -070043#include "runtime_support.h"
Elliott Hughes726079d2011-10-07 18:43:44 -070044#include "ScopedLocalRef.h"
Ian Rogersaaa20802011-09-11 21:47:37 -070045#include "scoped_jni_thread_state.h"
Logan Chienf7ad17e2012-03-15 03:10:03 +080046#include "shadow_frame.h"
Ian Rogers30fab402012-01-23 15:43:46 -080047#include "space.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070048#include "stack.h"
49#include "stack_indirect_reference_table.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070050#include "thread_list.h"
Elliott Hughesa0957642011-09-02 14:27:33 -070051#include "utils.h"
Carl Shapirob5573532011-07-12 18:22:59 -070052
53namespace art {
54
55pthread_key_t Thread::pthread_key_self_;
56
Elliott Hughes462c9442012-03-23 18:47:50 -070057static Class* gThreadGroup = NULL;
Elliott Hughes8e4aac52011-09-26 17:03:36 -070058static Class* gThreadLock = NULL;
Elliott Hughes038a8062011-09-18 14:12:41 -070059static Field* gThread_daemon = NULL;
60static Field* gThread_group = NULL;
61static Field* gThread_lock = NULL;
62static Field* gThread_name = NULL;
63static Field* gThread_priority = NULL;
Elliott Hughes29f27422011-09-18 16:02:18 -070064static Field* gThread_uncaughtHandler = NULL;
Elliott Hughes038a8062011-09-18 14:12:41 -070065static Field* gThread_vmData = NULL;
Elliott Hughes462c9442012-03-23 18:47:50 -070066static Field* gThreadGroup_mMain = NULL;
67static Field* gThreadGroup_mSystem = NULL;
Elliott Hughes038a8062011-09-18 14:12:41 -070068static Field* gThreadGroup_name = NULL;
Elliott Hughes8e4aac52011-09-26 17:03:36 -070069static Field* gThreadLock_thread = NULL;
Elliott Hughes038a8062011-09-18 14:12:41 -070070static Method* gThread_run = NULL;
Elliott Hughes29f27422011-09-18 16:02:18 -070071static Method* gThreadGroup_removeThread = NULL;
72static Method* gUncaughtExceptionHandler_uncaughtException = NULL;
Elliott Hughes038a8062011-09-18 14:12:41 -070073
Ian Rogers5d76c432011-10-31 21:42:49 -070074void Thread::InitCardTable() {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080075 card_table_ = Runtime::Current()->GetHeap()->GetCardTable()->GetBiasedBegin();
Ian Rogers5d76c432011-10-31 21:42:49 -070076}
77
buzbee3ea4ec52011-08-22 17:37:19 -070078void Thread::InitFunctionPointers() {
Ian Rogers57b86d42012-03-27 16:05:41 -070079 InitEntryPoints(&entrypoints_);
Elliott Hughesc0f09332012-03-26 13:27:06 -070080}
81
82void Thread::SetDebuggerUpdatesEnabled(bool enabled) {
83 LOG(INFO) << "Turning debugger updates " << (enabled ? "on" : "off") << " for " << *this;
Ian Rogers57b86d42012-03-27 16:05:41 -070084 ChangeDebuggerEntryPoint(&entrypoints_, enabled);
buzbee3ea4ec52011-08-22 17:37:19 -070085}
86
Brian Carlstromcaabb1b2011-10-11 18:09:13 -070087void Thread::InitTid() {
88 tid_ = ::art::GetTid();
89}
90
Brian Carlstromcaabb1b2011-10-11 18:09:13 -070091void Thread::InitAfterFork() {
92 InitTid();
Elliott Hughes3147a232011-10-12 15:55:07 -070093#if defined(__BIONIC__)
94 // Work around a bionic bug.
95 struct bionic_pthread_internal_t {
96 void* next;
97 void** pref;
98 pthread_attr_t attr;
99 pid_t kernel_id;
100 // et cetera. we just need 'kernel_id' so we can stop here.
101 };
102 reinterpret_cast<bionic_pthread_internal_t*>(pthread_self())->kernel_id = tid_;
103#endif
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700104}
105
Brian Carlstrom78128a62011-09-15 17:21:19 -0700106void* Thread::CreateCallback(void* arg) {
Elliott Hughes93e74e82011-09-13 11:07:03 -0700107 Thread* self = reinterpret_cast<Thread*>(arg);
Elliott Hughes462c9442012-03-23 18:47:50 -0700108 self->Init();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700109
Elliott Hughes93e74e82011-09-13 11:07:03 -0700110 // Wait until it's safe to start running code. (There may have been a suspend-all
111 // in progress while we were starting up.)
Elliott Hughes462c9442012-03-23 18:47:50 -0700112 Runtime* runtime = Runtime::Current();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700113 runtime->GetThreadList()->WaitForGo();
114
Elliott Hughes47179f72011-10-27 16:44:39 -0700115 {
116 CHECK_EQ(self->GetState(), Thread::kRunnable);
Elliott Hughes899e7892012-01-24 14:57:32 -0800117 SirtRef<String> thread_name(self->GetThreadName());
118 self->SetThreadName(thread_name->ToModifiedUtf8().c_str());
Elliott Hughes47179f72011-10-27 16:44:39 -0700119 }
120
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700121 Dbg::PostThreadStart(self);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700122
123 // Invoke the 'run' method of our java.lang.Thread.
124 CHECK(self->peer_ != NULL);
125 Object* receiver = self->peer_;
Elliott Hughes038a8062011-09-18 14:12:41 -0700126 Method* m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(gThread_run);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700127 m->Invoke(self, receiver, NULL, NULL);
128
129 // Detach.
130 runtime->GetThreadList()->Unregister();
131
Carl Shapirob5573532011-07-12 18:22:59 -0700132 return NULL;
133}
134
Elliott Hughesab7b9dc2012-03-27 13:16:29 -0700135static void SetVmData(Object* managed_thread, Thread* native_thread) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700136 gThread_vmData->SetInt(managed_thread, reinterpret_cast<uintptr_t>(native_thread));
Elliott Hughes93e74e82011-09-13 11:07:03 -0700137}
138
Elliott Hughes761928d2011-11-16 18:33:03 -0800139Thread* Thread::FromManagedThread(Object* thread_peer) {
140 return reinterpret_cast<Thread*>(static_cast<uintptr_t>(gThread_vmData->GetInt(thread_peer)));
141}
142
Elliott Hughes01158d72011-09-19 19:47:10 -0700143Thread* Thread::FromManagedThread(JNIEnv* env, jobject java_thread) {
Elliott Hughes761928d2011-11-16 18:33:03 -0800144 return FromManagedThread(Decode<Object*>(env, java_thread));
Elliott Hughes01158d72011-09-19 19:47:10 -0700145}
146
Elliott Hughesab7b9dc2012-03-27 13:16:29 -0700147static size_t FixStackSize(size_t stack_size) {
Elliott Hughes7502e2a2011-10-02 13:24:37 -0700148 // A stack size of zero means "use the default".
Elliott Hughesd369bb72011-09-12 14:41:14 -0700149 if (stack_size == 0) {
150 stack_size = Runtime::Current()->GetDefaultStackSize();
151 }
Carl Shapiro61e019d2011-07-14 16:53:09 -0700152
Elliott Hughes7502e2a2011-10-02 13:24:37 -0700153 // It's not possible to request a stack smaller than the system-defined PTHREAD_STACK_MIN.
154 if (stack_size < PTHREAD_STACK_MIN) {
155 stack_size = PTHREAD_STACK_MIN;
156 }
157
158 // It's likely that callers are trying to ensure they have at least a certain amount of
159 // stack space, so we should add our reserved space on top of what they requested, rather
160 // than implicitly take it away from them.
161 stack_size += Thread::kStackOverflowReservedBytes;
162
163 // Some systems require the stack size to be a multiple of the system page size, so round up.
164 stack_size = RoundUp(stack_size, kPageSize);
165
166 return stack_size;
167}
168
169void Thread::Create(Object* peer, size_t stack_size) {
170 CHECK(peer != NULL);
171
172 stack_size = FixStackSize(stack_size);
173
Elliott Hughes93e74e82011-09-13 11:07:03 -0700174 Thread* native_thread = new Thread;
175 native_thread->peer_ = peer;
176
177 // Thread.start is synchronized, so we know that vmData is 0,
178 // and know that we're not racing to assign it.
179 SetVmData(peer, native_thread);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700180
Elliott Hughes47179f72011-10-27 16:44:39 -0700181 {
182 ScopedThreadStateChange tsc(Thread::Current(), Thread::kVmWait);
183 pthread_t new_pthread;
184 pthread_attr_t attr;
185 CHECK_PTHREAD_CALL(pthread_attr_init, (&attr), "new thread");
186 CHECK_PTHREAD_CALL(pthread_attr_setdetachstate, (&attr, PTHREAD_CREATE_DETACHED), "PTHREAD_CREATE_DETACHED");
187 CHECK_PTHREAD_CALL(pthread_attr_setstacksize, (&attr, stack_size), stack_size);
188 CHECK_PTHREAD_CALL(pthread_create, (&new_pthread, &attr, Thread::CreateCallback, native_thread), "new thread");
189 CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attr), "new thread");
190 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700191
192 // Let the child know when it's safe to start running.
193 Runtime::Current()->GetThreadList()->SignalGo(native_thread);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700194}
195
Elliott Hughes462c9442012-03-23 18:47:50 -0700196void Thread::Init() {
197 // This function does all the initialization that must be run by the native thread it applies to.
198 // (When we create a new thread from managed code, we allocate the Thread* in Thread::Create so
199 // we can handshake with the corresponding native thread when it's ready.) Check this native
200 // thread hasn't been through here already...
Elliott Hughescac6cc72011-11-03 20:31:21 -0700201 CHECK(Thread::Current() == NULL);
202
Elliott Hughes93e74e82011-09-13 11:07:03 -0700203 InitCpu();
204 InitFunctionPointers();
Ian Rogers5d76c432011-10-31 21:42:49 -0700205 InitCardTable();
Carl Shapiro61e019d2011-07-14 16:53:09 -0700206
Elliott Hughes462c9442012-03-23 18:47:50 -0700207 Runtime* runtime = Runtime::Current();
208 CHECK(runtime != NULL);
209
210 thin_lock_id_ = runtime->GetThreadList()->AllocThreadId();
Carl Shapiro61e019d2011-07-14 16:53:09 -0700211
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700212 InitTid();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700213 InitStackHwm();
Carl Shapiro61e019d2011-07-14 16:53:09 -0700214
Elliott Hughes3147a232011-10-12 15:55:07 -0700215 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, this), "attach");
Elliott Hughesa5780da2011-07-17 11:39:39 -0700216
Elliott Hughes93e74e82011-09-13 11:07:03 -0700217 jni_env_ = new JNIEnvExt(this, runtime->GetJavaVM());
Elliott Hughes330304d2011-08-12 14:28:05 -0700218
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700219 runtime->GetThreadList()->Register();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700220}
221
Elliott Hughes462c9442012-03-23 18:47:50 -0700222Thread* Thread::Attach(const char* thread_name, bool as_daemon, Object* thread_group) {
Elliott Hughescac6cc72011-11-03 20:31:21 -0700223 Thread* self = new Thread;
Elliott Hughes462c9442012-03-23 18:47:50 -0700224 self->Init();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700225
Elliott Hughescac6cc72011-11-03 20:31:21 -0700226 self->SetState(Thread::kNative);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700227
Elliott Hughescac6cc72011-11-03 20:31:21 -0700228 // If we're the main thread, ClassLinker won't be created until after we're attached,
229 // so that thread needs a two-stage attach. Regular threads don't need this hack.
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800230 // In the compiler, all threads need this hack, because no-one's going to be getting
231 // a native peer!
232 if (self->thin_lock_id_ != ThreadList::kMainId && !Runtime::Current()->IsCompiler()) {
Elliott Hughes462c9442012-03-23 18:47:50 -0700233 self->CreatePeer(thread_name, as_daemon, thread_group);
Elliott Hughes06e3ad42012-02-07 14:51:57 -0800234 } else {
235 // These aren't necessary, but they improve diagnostics for unit tests & command-line tools.
Elliott Hughes22869a92012-03-27 14:08:24 -0700236 if (thread_name != NULL) {
237 self->name_->assign(thread_name);
238 ::art::SetThreadName(thread_name);
239 }
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700240 }
Elliott Hughescac6cc72011-11-03 20:31:21 -0700241
242 self->GetJniEnv()->locals.AssertEmpty();
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700243 return self;
244}
245
Elliott Hughes462c9442012-03-23 18:47:50 -0700246Object* Thread::GetMainThreadGroup() {
Ian Rogers0045a292012-03-31 21:08:41 -0700247 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(gThreadGroup, true, true)) {
Elliott Hughes462c9442012-03-23 18:47:50 -0700248 return NULL;
249 }
250 return gThreadGroup_mMain->GetObject(NULL);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700251}
252
Elliott Hughes462c9442012-03-23 18:47:50 -0700253Object* Thread::GetSystemThreadGroup() {
Ian Rogers0045a292012-03-31 21:08:41 -0700254 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(gThreadGroup, true, true)) {
Elliott Hughes462c9442012-03-23 18:47:50 -0700255 return NULL;
256 }
257 return gThreadGroup_mSystem->GetObject(NULL);
258}
259
260void Thread::CreatePeer(const char* name, bool as_daemon, Object* thread_group) {
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500261 CHECK(Runtime::Current()->IsStarted());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700262 JNIEnv* env = jni_env_;
263
Elliott Hughes462c9442012-03-23 18:47:50 -0700264 if (thread_group == NULL) {
265 thread_group = Thread::GetMainThreadGroup();
266 }
267 ScopedLocalRef<jobject> java_thread_group(env, AddLocalReference<jobject>(env, thread_group));
Elliott Hughes726079d2011-10-07 18:43:44 -0700268 ScopedLocalRef<jobject> thread_name(env, env->NewStringUTF(name));
Elliott Hughes8daa0922011-09-11 13:46:25 -0700269 jint thread_priority = GetNativePriority();
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700270 jboolean thread_is_daemon = as_daemon;
271
Elliott Hughes726079d2011-10-07 18:43:44 -0700272 ScopedLocalRef<jclass> c(env, env->FindClass("java/lang/Thread"));
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700273 ScopedLocalRef<jobject> peer(env, env->AllocObject(c.get()));
Elliott Hughes726079d2011-10-07 18:43:44 -0700274 peer_ = DecodeJObject(peer.get());
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700275 if (peer_ == NULL) {
276 CHECK(IsExceptionPending());
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700277 return;
278 }
279 jmethodID mid = env->GetMethodID(c.get(), "<init>", "(Ljava/lang/ThreadGroup;Ljava/lang/String;IZ)V");
Elliott Hughes462c9442012-03-23 18:47:50 -0700280 env->CallNonvirtualVoidMethod(peer.get(), c.get(), mid, java_thread_group.get(), thread_name.get(), thread_priority, thread_is_daemon);
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500281 CHECK(!IsExceptionPending()) << " " << PrettyTypeOf(GetException());
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700282 SetVmData(peer_, Thread::Current());
Elliott Hughesd369bb72011-09-12 14:41:14 -0700283
Elliott Hughes899e7892012-01-24 14:57:32 -0800284 SirtRef<String> peer_thread_name(GetThreadName());
Brian Carlstrom00fae582011-10-28 01:16:28 -0700285 if (peer_thread_name.get() == NULL) {
286 // The Thread constructor should have set the Thread.name to a
287 // non-null value. However, because we can run without code
288 // available (in the compiler, in tests), we manually assign the
289 // fields the constructor should have set.
290 gThread_daemon->SetBoolean(peer_, thread_is_daemon);
Elliott Hughes462c9442012-03-23 18:47:50 -0700291 gThread_group->SetObject(peer_, thread_group);
Brian Carlstrom00fae582011-10-28 01:16:28 -0700292 gThread_name->SetObject(peer_, Decode<Object*>(env, thread_name.get()));
293 gThread_priority->SetInt(peer_, thread_priority);
Elliott Hughes899e7892012-01-24 14:57:32 -0800294 peer_thread_name.reset(GetThreadName());
Brian Carlstrom00fae582011-10-28 01:16:28 -0700295 }
296 // thread_name may have been null, so don't trust this to be non-null
297 if (peer_thread_name.get() != NULL) {
Elliott Hughes899e7892012-01-24 14:57:32 -0800298 SetThreadName(peer_thread_name->ToModifiedUtf8().c_str());
Brian Carlstrom00fae582011-10-28 01:16:28 -0700299 }
Elliott Hughes726079d2011-10-07 18:43:44 -0700300
301 // Pre-allocate an OutOfMemoryError for the double-OOME case.
302 ThrowNewException("Ljava/lang/OutOfMemoryError;",
303 "OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack available");
304 ScopedLocalRef<jthrowable> exception(env, env->ExceptionOccurred());
305 env->ExceptionClear();
306 pre_allocated_OutOfMemoryError_ = Decode<Throwable*>(env, exception.get());
Carl Shapiro61e019d2011-07-14 16:53:09 -0700307}
308
Elliott Hughes899e7892012-01-24 14:57:32 -0800309void Thread::SetThreadName(const char* name) {
310 name_->assign(name);
311 ::art::SetThreadName(name);
312 Dbg::DdmSendThreadNotification(this, CHUNK_TYPE("THNM"));
313}
314
Elliott Hughesbe759c62011-09-08 19:38:21 -0700315void Thread::InitStackHwm() {
Elliott Hughes215f3142012-01-19 00:22:47 -0800316#if defined(__APPLE__)
Elliott Hughes6cc332e2012-01-20 22:59:20 -0800317 // Only needed to run code. Try implementing this with pthread_get_stacksize_np and pthread_get_stackaddr_np.
318 UNIMPLEMENTED(WARNING);
Elliott Hughes215f3142012-01-19 00:22:47 -0800319#else
Elliott Hughesbe759c62011-09-08 19:38:21 -0700320 pthread_attr_t attributes;
Elliott Hughes3147a232011-10-12 15:55:07 -0700321 CHECK_PTHREAD_CALL(pthread_getattr_np, (pthread_self(), &attributes), __FUNCTION__);
Elliott Hughesbe759c62011-09-08 19:38:21 -0700322
Ian Rogers932746a2011-09-22 18:57:50 -0700323 void* temp_stack_base;
324 CHECK_PTHREAD_CALL(pthread_attr_getstack, (&attributes, &temp_stack_base, &stack_size_),
325 __FUNCTION__);
Ian Rogers30fab402012-01-23 15:43:46 -0800326 stack_begin_ = reinterpret_cast<byte*>(temp_stack_base);
Elliott Hughesbe759c62011-09-08 19:38:21 -0700327
Ian Rogers932746a2011-09-22 18:57:50 -0700328 if (stack_size_ <= kStackOverflowReservedBytes) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800329 LOG(FATAL) << "Attempt to attach a thread with a too-small stack (" << stack_size_ << " bytes)";
Elliott Hughesbe759c62011-09-08 19:38:21 -0700330 }
Elliott Hughes449b4bd2011-09-09 12:01:38 -0700331
Ian Rogers932746a2011-09-22 18:57:50 -0700332 // Set stack_end_ to the bottom of the stack saving space of stack overflows
333 ResetDefaultStackEnd();
Elliott Hughes449b4bd2011-09-09 12:01:38 -0700334
335 // Sanity check.
336 int stack_variable;
Elliott Hughes398f64b2012-03-26 18:05:48 -0700337 CHECK_GT(&stack_variable, reinterpret_cast<void*>(stack_end_));
Elliott Hughesbe759c62011-09-08 19:38:21 -0700338
Elliott Hughes8d768a92011-09-14 16:35:25 -0700339 CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attributes), __FUNCTION__);
Elliott Hughes215f3142012-01-19 00:22:47 -0800340#endif
Elliott Hughesbe759c62011-09-08 19:38:21 -0700341}
342
Elliott Hughes899e7892012-01-24 14:57:32 -0800343void Thread::Dump(std::ostream& os, bool full) const {
344 if (full) {
345 DumpState(os);
346 DumpStack(os);
347 } else {
348 os << "Thread[";
349 if (GetThinLockId() != 0) {
350 // If we're in kStarting, we won't have a thin lock id or tid yet.
351 os << GetThinLockId()
352 << ",tid=" << GetTid() << ',';
353 }
354 os << GetState()
355 << ",Thread*=" << this
Elliott Hughesffb465f2012-03-01 18:46:05 -0800356 << ",peer=" << peer_
Elliott Hughes899e7892012-01-24 14:57:32 -0800357 << ",\"" << *name_ << "\""
358 << "]";
Elliott Hughese0918552011-10-28 17:18:29 -0700359 }
Elliott Hughesa0957642011-09-02 14:27:33 -0700360}
361
Elliott Hughes899e7892012-01-24 14:57:32 -0800362String* Thread::GetThreadName() const {
Elliott Hughesfc861622011-10-17 17:57:47 -0700363 return (peer_ != NULL) ? reinterpret_cast<String*>(gThread_name->GetObject(peer_)) : NULL;
364}
365
Elliott Hughesffb465f2012-03-01 18:46:05 -0800366void Thread::GetThreadName(std::string& name) const {
367 name.assign(*name_);
368}
369
Elliott Hughesd92bec42011-09-02 17:04:36 -0700370void Thread::DumpState(std::ostream& os) const {
Elliott Hughesd369bb72011-09-12 14:41:14 -0700371 std::string group_name;
372 int priority;
373 bool is_daemon = false;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700374
Elliott Hughesd369bb72011-09-12 14:41:14 -0700375 if (peer_ != NULL) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700376 priority = gThread_priority->GetInt(peer_);
377 is_daemon = gThread_daemon->GetBoolean(peer_);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700378
Elliott Hughesa2155262011-11-16 16:26:58 -0800379 Object* thread_group = GetThreadGroup();
Elliott Hughesd369bb72011-09-12 14:41:14 -0700380 if (thread_group != NULL) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700381 String* group_name_string = reinterpret_cast<String*>(gThreadGroup_name->GetObject(thread_group));
Elliott Hughesd369bb72011-09-12 14:41:14 -0700382 group_name = (group_name_string != NULL) ? group_name_string->ToModifiedUtf8() : "<null>";
383 }
384 } else {
Elliott Hughesd369bb72011-09-12 14:41:14 -0700385 priority = GetNativePriority();
Elliott Hughesdcc24742011-09-07 14:02:44 -0700386 }
Elliott Hughesd92bec42011-09-02 17:04:36 -0700387
388 int policy;
389 sched_param sp;
Elliott Hughes3147a232011-10-12 15:55:07 -0700390 CHECK_PTHREAD_CALL(pthread_getschedparam, (pthread_self(), &policy, &sp), __FUNCTION__);
Elliott Hughesd92bec42011-09-02 17:04:36 -0700391
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700392 std::string scheduler_group_name(GetSchedulerGroupName(GetTid()));
393 if (scheduler_group_name.empty()) {
394 scheduler_group_name = "default";
Elliott Hughesd92bec42011-09-02 17:04:36 -0700395 }
396
Elliott Hughes899e7892012-01-24 14:57:32 -0800397 os << '"' << *name_ << '"';
Elliott Hughesd369bb72011-09-12 14:41:14 -0700398 if (is_daemon) {
Elliott Hughesd92bec42011-09-02 17:04:36 -0700399 os << " daemon";
400 }
401 os << " prio=" << priority
Elliott Hughesdcc24742011-09-07 14:02:44 -0700402 << " tid=" << GetThinLockId()
Elliott Hughes93e74e82011-09-13 11:07:03 -0700403 << " " << GetState() << "\n";
Elliott Hughesd92bec42011-09-02 17:04:36 -0700404
Elliott Hughesd92bec42011-09-02 17:04:36 -0700405 os << " | group=\"" << group_name << "\""
Elliott Hughes8d768a92011-09-14 16:35:25 -0700406 << " sCount=" << suspend_count_
Elliott Hughes234ab152011-10-26 14:02:26 -0700407 << " dsCount=" << debug_suspend_count_
Elliott Hughesdcc24742011-09-07 14:02:44 -0700408 << " obj=" << reinterpret_cast<void*>(peer_)
Elliott Hughesd92bec42011-09-02 17:04:36 -0700409 << " self=" << reinterpret_cast<const void*>(this) << "\n";
410 os << " | sysTid=" << GetTid()
411 << " nice=" << getpriority(PRIO_PROCESS, GetTid())
412 << " sched=" << policy << "/" << sp.sched_priority
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700413 << " cgrp=" << scheduler_group_name
Elliott Hughes3147a232011-10-12 15:55:07 -0700414 << " handle=" << pthread_self() << "\n";
Elliott Hughesd92bec42011-09-02 17:04:36 -0700415
416 // Grab the scheduler stats for this thread.
417 std::string scheduler_stats;
418 if (ReadFileToString(StringPrintf("/proc/self/task/%d/schedstat", GetTid()).c_str(), &scheduler_stats)) {
419 scheduler_stats.resize(scheduler_stats.size() - 1); // Lose the trailing '\n'.
420 } else {
421 scheduler_stats = "0 0 0";
422 }
423
424 int utime = 0;
425 int stime = 0;
426 int task_cpu = 0;
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700427 GetTaskStats(GetTid(), utime, stime, task_cpu);
Elliott Hughesd92bec42011-09-02 17:04:36 -0700428
429 os << " | schedstat=( " << scheduler_stats << " )"
430 << " utm=" << utime
431 << " stm=" << stime
432 << " core=" << task_cpu
433 << " HZ=" << sysconf(_SC_CLK_TCK) << "\n";
434}
435
TDYa127ee0d3fb2012-04-01 14:55:33 -0700436#if !defined(ART_USE_LLVM_COMPILER)
Ian Rogersb04f69f2011-10-17 00:40:54 -0700437void Thread::PushNativeToManagedRecord(NativeToManagedRecord* record) {
438 Method **sp = top_of_managed_stack_.GetSP();
439#ifndef NDEBUG
440 if (sp != NULL) {
441 Method* m = *sp;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800442 Runtime::Current()->GetHeap()->VerifyObject(m);
Ian Rogersb04f69f2011-10-17 00:40:54 -0700443 DCHECK((m == NULL) || m->IsMethod());
444 }
445#endif
446 record->last_top_of_managed_stack_ = reinterpret_cast<void*>(sp);
447 record->last_top_of_managed_stack_pc_ = top_of_managed_stack_pc_;
448 record->link_ = native_to_managed_record_;
449 native_to_managed_record_ = record;
450 top_of_managed_stack_.SetSP(NULL);
451}
TDYa127ee0d3fb2012-04-01 14:55:33 -0700452#else
453void Thread::PushNativeToManagedRecord(NativeToManagedRecord*) {
454 LOG(FATAL) << "Called non-LLVM method with LLVM";
455}
456#endif
Ian Rogersb04f69f2011-10-17 00:40:54 -0700457
TDYa127ee0d3fb2012-04-01 14:55:33 -0700458#if !defined(ART_USE_LLVM_COMPILER)
Ian Rogersb04f69f2011-10-17 00:40:54 -0700459void Thread::PopNativeToManagedRecord(const NativeToManagedRecord& record) {
460 native_to_managed_record_ = record.link_;
461 top_of_managed_stack_.SetSP(reinterpret_cast<Method**>(record.last_top_of_managed_stack_));
462 top_of_managed_stack_pc_ = record.last_top_of_managed_stack_pc_;
463}
TDYa127ee0d3fb2012-04-01 14:55:33 -0700464#else
465void Thread::PopNativeToManagedRecord(const NativeToManagedRecord&) {
466 LOG(FATAL) << "Called non-LLVM method with LLVM";
467}
468#endif
Ian Rogersb04f69f2011-10-17 00:40:54 -0700469
Elliott Hughesd369bb72011-09-12 14:41:14 -0700470struct StackDumpVisitor : public Thread::StackVisitor {
Elliott Hughes8e4aac52011-09-26 17:03:36 -0700471 StackDumpVisitor(std::ostream& os, const Thread* thread)
Ian Rogers28ad40d2011-10-27 15:19:26 -0700472 : last_method(NULL), last_line_number(0), repetition_count(0), os(os), thread(thread),
473 frame_count(0) {
Elliott Hughesd369bb72011-09-12 14:41:14 -0700474 }
475
Ian Rogersbdb03912011-09-14 00:55:44 -0700476 virtual ~StackDumpVisitor() {
Elliott Hughesd369bb72011-09-12 14:41:14 -0700477 }
478
Elliott Hughes530fa002012-03-12 11:44:49 -0700479 bool VisitFrame(const Frame& frame, uintptr_t pc) {
Ian Rogers90865722011-09-19 11:11:44 -0700480 if (!frame.HasMethod()) {
Elliott Hughes530fa002012-03-12 11:44:49 -0700481 return true;
Ian Rogers90865722011-09-19 11:11:44 -0700482 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700483 const int kMaxRepetition = 3;
Elliott Hughesd369bb72011-09-12 14:41:14 -0700484 Method* m = frame.GetMethod();
485 Class* c = m->GetDeclaringClass();
Elliott Hughes8e4aac52011-09-26 17:03:36 -0700486 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersb861dc02011-11-14 17:00:05 -0800487 const DexCache* dex_cache = c->GetDexCache();
488 int line_number = -1;
489 if (dex_cache != NULL) { // be tolerant of bad input
490 const DexFile& dex_file = class_linker->FindDexFile(dex_cache);
491 line_number = dex_file.GetLineNumFromPC(m, m->ToDexPC(pc));
492 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700493 if (line_number == last_line_number && last_method == m) {
494 repetition_count++;
Elliott Hughesd369bb72011-09-12 14:41:14 -0700495 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -0700496 if (repetition_count >= kMaxRepetition) {
497 os << " ... repeated " << (repetition_count - kMaxRepetition) << " times\n";
498 }
499 repetition_count = 0;
500 last_line_number = line_number;
501 last_method = m;
Elliott Hughesd369bb72011-09-12 14:41:14 -0700502 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700503 if (repetition_count < kMaxRepetition) {
504 os << " at " << PrettyMethod(m, false);
505 if (m->IsNative()) {
506 os << "(Native method)";
507 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800508 mh.ChangeMethod(m);
509 const char* source_file(mh.GetDeclaringClassSourceFile());
510 os << "(" << (source_file != NULL ? source_file : "unavailable")
Ian Rogers28ad40d2011-10-27 15:19:26 -0700511 << ":" << line_number << ")";
512 }
513 os << "\n";
514 }
Elliott Hughes8e4aac52011-09-26 17:03:36 -0700515
516 if (frame_count++ == 0) {
517 Monitor::DescribeWait(os, thread);
518 }
Elliott Hughes530fa002012-03-12 11:44:49 -0700519 return true;
Elliott Hughesd369bb72011-09-12 14:41:14 -0700520 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800521 MethodHelper mh;
Ian Rogers28ad40d2011-10-27 15:19:26 -0700522 Method* last_method;
523 int last_line_number;
524 int repetition_count;
Elliott Hughesd369bb72011-09-12 14:41:14 -0700525 std::ostream& os;
Elliott Hughes8e4aac52011-09-26 17:03:36 -0700526 const Thread* thread;
527 int frame_count;
Elliott Hughesd369bb72011-09-12 14:41:14 -0700528};
529
Elliott Hughesd92bec42011-09-02 17:04:36 -0700530void Thread::DumpStack(std::ostream& os) const {
Elliott Hughesffb465f2012-03-01 18:46:05 -0800531 // If we're currently in native code, dump that stack before dumping the managed stack.
532 if (GetState() == Thread::kNative || GetState() == Thread::kVmWait) {
533 DumpNativeStack(os);
534 }
Elliott Hughes8e4aac52011-09-26 17:03:36 -0700535 StackDumpVisitor dumper(os, this);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700536 WalkStack(&dumper);
Elliott Hughese27955c2011-08-26 15:21:24 -0700537}
538
Elliott Hughesa4060e52012-03-02 16:51:35 -0800539void Thread::SetStateWithoutSuspendCheck(Thread::State new_state) {
540 volatile void* raw = reinterpret_cast<volatile void*>(&state_);
541 volatile int32_t* addr = reinterpret_cast<volatile int32_t*>(raw);
542 android_atomic_release_store(new_state, addr);
543}
544
Elliott Hughes8d768a92011-09-14 16:35:25 -0700545Thread::State Thread::SetState(Thread::State new_state) {
546 Thread::State old_state = state_;
547 if (old_state == new_state) {
548 return old_state;
549 }
550
551 volatile void* raw = reinterpret_cast<volatile void*>(&state_);
552 volatile int32_t* addr = reinterpret_cast<volatile int32_t*>(raw);
553
554 if (new_state == Thread::kRunnable) {
555 /*
556 * Change our status to Thread::kRunnable. The transition requires
Elliott Hughes81ff3182012-03-23 20:35:56 -0700557 * that we check for pending suspension, because the runtime considers
Elliott Hughes8d768a92011-09-14 16:35:25 -0700558 * us to be "asleep" in all other states, and another thread could
559 * be performing a GC now.
560 *
561 * The order of operations is very significant here. One way to
562 * do this wrong is:
563 *
564 * GCing thread Our thread (in kNative)
565 * ------------ ----------------------
566 * check suspend count (== 0)
567 * SuspendAllThreads()
568 * grab suspend-count lock
569 * increment all suspend counts
570 * release suspend-count lock
571 * check thread state (== kNative)
572 * all are suspended, begin GC
573 * set state to kRunnable
574 * (continue executing)
575 *
576 * We can correct this by grabbing the suspend-count lock and
577 * performing both of our operations (check suspend count, set
578 * state) while holding it, now we need to grab a mutex on every
579 * transition to kRunnable.
580 *
581 * What we do instead is change the order of operations so that
582 * the transition to kRunnable happens first. If we then detect
583 * that the suspend count is nonzero, we switch to kSuspended.
584 *
585 * Appropriate compiler and memory barriers are required to ensure
586 * that the operations are observed in the expected order.
587 *
588 * This does create a small window of opportunity where a GC in
589 * progress could observe what appears to be a running thread (if
590 * it happens to look between when we set to kRunnable and when we
591 * switch to kSuspended). At worst this only affects assertions
592 * and thread logging. (We could work around it with some sort
593 * of intermediate "pre-running" state that is generally treated
594 * as equivalent to running, but that doesn't seem worthwhile.)
595 *
596 * We can also solve this by combining the "status" and "suspend
597 * count" fields into a single 32-bit value. This trades the
598 * store/load barrier on transition to kRunnable for an atomic RMW
599 * op on all transitions and all suspend count updates (also, all
600 * accesses to status or the thread count require bit-fiddling).
601 * It also eliminates the brief transition through kRunnable when
602 * the thread is supposed to be suspended. This is possibly faster
603 * on SMP and slightly more correct, but less convenient.
604 */
605 android_atomic_acquire_store(new_state, addr);
Elliott Hughes06e3ad42012-02-07 14:51:57 -0800606 ANNOTATE_IGNORE_READS_BEGIN();
607 int suspend_count = suspend_count_;
608 ANNOTATE_IGNORE_READS_END();
609 if (suspend_count != 0) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700610 Runtime::Current()->GetThreadList()->FullSuspendCheck(this);
611 }
612 } else {
613 /*
614 * Not changing to Thread::kRunnable. No additional work required.
615 *
616 * We use a releasing store to ensure that, if we were runnable,
617 * any updates we previously made to objects on the managed heap
618 * will be observed before the state change.
619 */
620 android_atomic_release_store(new_state, addr);
621 }
622
623 return old_state;
624}
625
Elliott Hughes761928d2011-11-16 18:33:03 -0800626bool Thread::IsSuspended() {
Elliott Hughes06e3ad42012-02-07 14:51:57 -0800627 ANNOTATE_IGNORE_READS_BEGIN();
628 int suspend_count = suspend_count_;
629 ANNOTATE_IGNORE_READS_END();
630 return suspend_count != 0 && GetState() != Thread::kRunnable;
Elliott Hughes761928d2011-11-16 18:33:03 -0800631}
632
Elliott Hughesff738062012-02-03 15:00:42 -0800633static void ReportThreadSuspendTimeout(Thread* waiting_thread) {
634 Runtime* runtime = Runtime::Current();
635 std::ostringstream ss;
Elliott Hughesffb465f2012-03-01 18:46:05 -0800636 ss << "Thread suspend timeout waiting for thread " << *waiting_thread << "\n";
Elliott Hughesff738062012-02-03 15:00:42 -0800637 runtime->DumpLockHolders(ss);
638 ss << "\n";
639 runtime->GetThreadList()->DumpLocked(ss);
640 LOG(FATAL) << ss.str();
641}
642
Elliott Hughes8d768a92011-09-14 16:35:25 -0700643void Thread::WaitUntilSuspended() {
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800644 static const useconds_t kTimeoutUs = 30 * 1000000; // 30s.
645
646 useconds_t total_delay = 0;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700647 useconds_t delay = 0;
648 while (GetState() == Thread::kRunnable) {
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800649 if (total_delay >= kTimeoutUs) {
Elliott Hughesff738062012-02-03 15:00:42 -0800650 ReportThreadSuspendTimeout(this);
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800651 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700652 useconds_t new_delay = delay * 2;
653 CHECK_GE(new_delay, delay);
654 delay = new_delay;
655 if (delay == 0) {
656 sched_yield();
657 delay = 10000;
658 } else {
659 usleep(delay);
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800660 total_delay += delay;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700661 }
662 }
663}
664
Elliott Hughesbe759c62011-09-08 19:38:21 -0700665void Thread::ThreadExitCallback(void* arg) {
666 Thread* self = reinterpret_cast<Thread*>(arg);
667 LOG(FATAL) << "Native thread exited without calling DetachCurrentThread: " << *self;
Carl Shapirob5573532011-07-12 18:22:59 -0700668}
669
Elliott Hughesbe759c62011-09-08 19:38:21 -0700670void Thread::Startup() {
Carl Shapirob5573532011-07-12 18:22:59 -0700671 // Allocate a TLS slot.
Elliott Hughes8d768a92011-09-14 16:35:25 -0700672 CHECK_PTHREAD_CALL(pthread_key_create, (&Thread::pthread_key_self_, Thread::ThreadExitCallback), "self key");
Carl Shapirob5573532011-07-12 18:22:59 -0700673
674 // Double-check the TLS slot allocation.
675 if (pthread_getspecific(pthread_key_self_) != NULL) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800676 LOG(FATAL) << "Newly-created pthread TLS slot is not NULL";
Carl Shapirob5573532011-07-12 18:22:59 -0700677 }
Elliott Hughes038a8062011-09-18 14:12:41 -0700678}
Carl Shapirob5573532011-07-12 18:22:59 -0700679
Elliott Hughes8e4aac52011-09-26 17:03:36 -0700680// TODO: make more accessible?
Elliott Hughesab7b9dc2012-03-27 13:16:29 -0700681static Class* FindClassOrDie(ClassLinker* class_linker, const char* descriptor) {
Elliott Hughes8e4aac52011-09-26 17:03:36 -0700682 Class* c = class_linker->FindSystemClass(descriptor);
683 CHECK(c != NULL) << descriptor;
684 return c;
685}
686
687// TODO: make more accessible?
Elliott Hughesab7b9dc2012-03-27 13:16:29 -0700688static Field* FindFieldOrDie(Class* c, const char* name, const char* descriptor) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700689 Field* f = c->FindDeclaredInstanceField(name, descriptor);
690 CHECK(f != NULL) << PrettyClass(c) << " " << name << " " << descriptor;
Elliott Hughes8e4aac52011-09-26 17:03:36 -0700691 return f;
692}
693
694// TODO: make more accessible?
Elliott Hughesab7b9dc2012-03-27 13:16:29 -0700695static Method* FindMethodOrDie(Class* c, const char* name, const char* signature) {
Elliott Hughes8e4aac52011-09-26 17:03:36 -0700696 Method* m = c->FindVirtualMethod(name, signature);
697 CHECK(m != NULL) << PrettyClass(c) << " " << name << " " << signature;
698 return m;
699}
700
Elliott Hughes462c9442012-03-23 18:47:50 -0700701// TODO: make more accessible?
Elliott Hughesab7b9dc2012-03-27 13:16:29 -0700702static Field* FindStaticFieldOrDie(Class* c, const char* name, const char* descriptor) {
Elliott Hughes462c9442012-03-23 18:47:50 -0700703 Field* f = c->FindDeclaredStaticField(name, descriptor);
704 CHECK(f != NULL) << PrettyClass(c) << " " << name << " " << descriptor;
705 return f;
706}
707
Elliott Hughes038a8062011-09-18 14:12:41 -0700708void Thread::FinishStartup() {
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500709 CHECK(Runtime::Current()->IsStarted());
Brian Carlstromb82b6872011-10-26 17:18:07 -0700710 Thread* self = Thread::Current();
711
712 // Need to be kRunnable for FindClass
713 ScopedThreadStateChange tsc(self, Thread::kRunnable);
714
Elliott Hughes038a8062011-09-18 14:12:41 -0700715 // Now the ClassLinker is ready, we can find the various Class*, Field*, and Method*s we need.
716 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Elliott Hughes8e4aac52011-09-26 17:03:36 -0700717
Elliott Hughes8e4aac52011-09-26 17:03:36 -0700718 Class* Thread_class = FindClassOrDie(class_linker, "Ljava/lang/Thread;");
Elliott Hughes8e4aac52011-09-26 17:03:36 -0700719 Class* UncaughtExceptionHandler_class = FindClassOrDie(class_linker, "Ljava/lang/Thread$UncaughtExceptionHandler;");
Elliott Hughes462c9442012-03-23 18:47:50 -0700720 gThreadGroup = FindClassOrDie(class_linker, "Ljava/lang/ThreadGroup;");
Elliott Hughes8e4aac52011-09-26 17:03:36 -0700721 gThreadLock = FindClassOrDie(class_linker, "Ljava/lang/ThreadLock;");
Elliott Hughes8e4aac52011-09-26 17:03:36 -0700722
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700723 gThread_daemon = FindFieldOrDie(Thread_class, "daemon", "Z");
724 gThread_group = FindFieldOrDie(Thread_class, "group", "Ljava/lang/ThreadGroup;");
725 gThread_lock = FindFieldOrDie(Thread_class, "lock", "Ljava/lang/ThreadLock;");
726 gThread_name = FindFieldOrDie(Thread_class, "name", "Ljava/lang/String;");
727 gThread_priority = FindFieldOrDie(Thread_class, "priority", "I");
728 gThread_uncaughtHandler = FindFieldOrDie(Thread_class, "uncaughtHandler", "Ljava/lang/Thread$UncaughtExceptionHandler;");
729 gThread_vmData = FindFieldOrDie(Thread_class, "vmData", "I");
Elliott Hughes462c9442012-03-23 18:47:50 -0700730 gThreadGroup_name = FindFieldOrDie(gThreadGroup, "name", "Ljava/lang/String;");
731 gThreadGroup_mMain = FindStaticFieldOrDie(gThreadGroup, "mMain", "Ljava/lang/ThreadGroup;");
732 gThreadGroup_mSystem = FindStaticFieldOrDie(gThreadGroup, "mSystem", "Ljava/lang/ThreadGroup;");
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700733 gThreadLock_thread = FindFieldOrDie(gThreadLock, "thread", "Ljava/lang/Thread;");
Elliott Hughes8e4aac52011-09-26 17:03:36 -0700734
735 gThread_run = FindMethodOrDie(Thread_class, "run", "()V");
Elliott Hughes462c9442012-03-23 18:47:50 -0700736 gThreadGroup_removeThread = FindMethodOrDie(gThreadGroup, "removeThread", "(Ljava/lang/Thread;)V");
Elliott Hughes8e4aac52011-09-26 17:03:36 -0700737 gUncaughtExceptionHandler_uncaughtException = FindMethodOrDie(UncaughtExceptionHandler_class,
738 "uncaughtException", "(Ljava/lang/Thread;Ljava/lang/Throwable;)V");
Elliott Hughes01158d72011-09-19 19:47:10 -0700739
740 // Finish attaching the main thread.
Elliott Hughes462c9442012-03-23 18:47:50 -0700741 Thread::Current()->CreatePeer("main", false, Thread::GetMainThreadGroup());
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500742
743 InitBoxingMethods();
744 class_linker->RunRootClinits();
Carl Shapirob5573532011-07-12 18:22:59 -0700745}
746
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700747void Thread::Shutdown() {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700748 CHECK_PTHREAD_CALL(pthread_key_delete, (Thread::pthread_key_self_), "self key");
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700749}
750
Elliott Hughes8e4aac52011-09-26 17:03:36 -0700751uint32_t Thread::LockOwnerFromThreadLock(Object* thread_lock) {
752 if (thread_lock == NULL || thread_lock->GetClass() != gThreadLock) {
753 return ThreadList::kInvalidId;
754 }
755 Object* managed_thread = gThreadLock_thread->GetObject(thread_lock);
756 if (managed_thread == NULL) {
757 return ThreadList::kInvalidId;
758 }
759 uintptr_t vmData = static_cast<uintptr_t>(gThread_vmData->GetInt(managed_thread));
760 Thread* thread = reinterpret_cast<Thread*>(vmData);
761 if (thread == NULL) {
762 return ThreadList::kInvalidId;
763 }
764 return thread->GetThinLockId();
765}
766
Elliott Hughesdcc24742011-09-07 14:02:44 -0700767Thread::Thread()
Elliott Hughes47179f72011-10-27 16:44:39 -0700768 : thin_lock_id_(0),
769 tid_(0),
770 peer_(NULL),
Elliott Hughes8e4aac52011-09-26 17:03:36 -0700771 top_of_managed_stack_(),
772 top_of_managed_stack_pc_(0),
Elliott Hughes85d15452011-09-16 17:33:01 -0700773 wait_mutex_(new Mutex("Thread wait mutex")),
774 wait_cond_(new ConditionVariable("Thread wait condition variable")),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700775 wait_monitor_(NULL),
776 interrupted_(false),
Elliott Hughesdc33ad52011-09-16 19:46:51 -0700777 wait_next_(NULL),
Elliott Hughes8e4aac52011-09-26 17:03:36 -0700778 monitor_enter_object_(NULL),
Elliott Hughesdc33ad52011-09-16 19:46:51 -0700779 card_table_(0),
Elliott Hughes8daa0922011-09-11 13:46:25 -0700780 stack_end_(NULL),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700781 native_to_managed_record_(NULL),
782 top_sirt_(NULL),
Logan Chienf7ad17e2012-03-15 03:10:03 +0800783 top_shadow_frame_(NULL),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700784 jni_env_(NULL),
Elliott Hughes8e4aac52011-09-26 17:03:36 -0700785 state_(Thread::kNative),
Elliott Hughesdc33ad52011-09-16 19:46:51 -0700786 self_(NULL),
787 runtime_(NULL),
Elliott Hughesdcc24742011-09-07 14:02:44 -0700788 exception_(NULL),
789 suspend_count_(0),
Elliott Hughes234ab152011-10-26 14:02:26 -0700790 debug_suspend_count_(0),
Elliott Hughes85d15452011-09-16 17:33:01 -0700791 class_loader_override_(NULL),
Elliott Hughes418dfe72011-10-06 18:56:27 -0700792 long_jump_context_(NULL),
Elliott Hughes726079d2011-10-07 18:43:44 -0700793 throwing_OutOfMemoryError_(false),
Elliott Hughes475fc232011-10-25 15:00:35 -0700794 pre_allocated_OutOfMemoryError_(NULL),
jeffhaoe343b762011-12-05 16:36:44 -0800795 debug_invoke_req_(new DebugInvokeReq),
Elliott Hughes899e7892012-01-24 14:57:32 -0800796 trace_stack_(new std::vector<TraceStackFrame>),
797 name_(new std::string("<native thread without managed peer>")) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700798 CHECK_EQ((sizeof(Thread) % 4), 0U) << sizeof(Thread);
Elliott Hughesffb465f2012-03-01 18:46:05 -0800799 memset(&held_mutexes_[0], 0, sizeof(held_mutexes_));
Elliott Hughesdcc24742011-09-07 14:02:44 -0700800}
801
Elliott Hughesab7b9dc2012-03-27 13:16:29 -0700802static void MonitorExitVisitor(const Object* object, void*) {
Elliott Hughes02b48d12011-09-07 17:15:51 -0700803 Object* entered_monitor = const_cast<Object*>(object);
Elliott Hughesab7b9dc2012-03-27 13:16:29 -0700804 LOG(WARNING) << "Calling MonitorExit on object " << object << " (" << PrettyTypeOf(object) << ")"
805 << " left locked by native thread " << *Thread::Current() << " which is detaching";
Elliott Hughes5f791332011-09-15 17:45:30 -0700806 entered_monitor->MonitorExit(Thread::Current());
Elliott Hughes02b48d12011-09-07 17:15:51 -0700807}
808
Elliott Hughesc0f09332012-03-26 13:27:06 -0700809void Thread::Destroy() {
Elliott Hughes02b48d12011-09-07 17:15:51 -0700810 // On thread detach, all monitors entered with JNI MonitorEnter are automatically exited.
Elliott Hughes93e74e82011-09-13 11:07:03 -0700811 if (jni_env_ != NULL) {
812 jni_env_->monitors.VisitRoots(MonitorExitVisitor, NULL);
813 }
Elliott Hughes02b48d12011-09-07 17:15:51 -0700814
Elliott Hughes93e74e82011-09-13 11:07:03 -0700815 if (peer_ != NULL) {
Elliott Hughesc0f09332012-03-26 13:27:06 -0700816 Thread* self = this;
Elliott Hughes29f27422011-09-18 16:02:18 -0700817
Elliott Hughes534da072012-03-27 15:17:42 -0700818 // We may need to call user-supplied managed code.
819 SetState(Thread::kRunnable);
820
821 HandleUncaughtExceptions();
822 RemoveFromThreadGroup();
823
Elliott Hughes29f27422011-09-18 16:02:18 -0700824 // this.vmData = 0;
Elliott Hughes93e74e82011-09-13 11:07:03 -0700825 SetVmData(peer_, NULL);
Elliott Hughes02b48d12011-09-07 17:15:51 -0700826
Elliott Hughesc0f09332012-03-26 13:27:06 -0700827 Dbg::PostThreadDeath(self);
Elliott Hughes02b48d12011-09-07 17:15:51 -0700828
Elliott Hughes29f27422011-09-18 16:02:18 -0700829 // Thread.join() is implemented as an Object.wait() on the Thread.lock
830 // object. Signal anyone who is waiting.
Elliott Hughes038a8062011-09-18 14:12:41 -0700831 Object* lock = gThread_lock->GetObject(peer_);
832 // (This conditional is only needed for tests, where Thread.lock won't have been set.)
Elliott Hughes5f791332011-09-15 17:45:30 -0700833 if (lock != NULL) {
834 lock->MonitorEnter(self);
835 lock->NotifyAll();
836 lock->MonitorExit(self);
837 }
838 }
Elliott Hughesc0f09332012-03-26 13:27:06 -0700839}
Elliott Hughes02b48d12011-09-07 17:15:51 -0700840
Elliott Hughesc0f09332012-03-26 13:27:06 -0700841Thread::~Thread() {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700842 delete jni_env_;
Elliott Hughes02b48d12011-09-07 17:15:51 -0700843 jni_env_ = NULL;
844
845 SetState(Thread::kTerminated);
Elliott Hughes85d15452011-09-16 17:33:01 -0700846
847 delete wait_cond_;
848 delete wait_mutex_;
849
850 delete long_jump_context_;
Elliott Hughes475fc232011-10-25 15:00:35 -0700851
852 delete debug_invoke_req_;
jeffhaoe343b762011-12-05 16:36:44 -0800853 delete trace_stack_;
Elliott Hughes899e7892012-01-24 14:57:32 -0800854 delete name_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700855}
856
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700857void Thread::HandleUncaughtExceptions() {
858 if (!IsExceptionPending()) {
859 return;
860 }
861
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700862 // Get and clear the exception.
863 Object* exception = GetException();
864 ClearException();
865
866 // If the thread has its own handler, use that.
867 Object* handler = gThread_uncaughtHandler->GetObject(peer_);
868 if (handler == NULL) {
869 // Otherwise use the thread group's default handler.
Elliott Hughesa2155262011-11-16 16:26:58 -0800870 handler = GetThreadGroup();
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700871 }
872
873 // Call the handler.
874 Method* m = handler->GetClass()->FindVirtualMethodForVirtualOrInterface(gUncaughtExceptionHandler_uncaughtException);
Elliott Hughes77405792012-03-15 15:22:12 -0700875 JValue args[2];
876 args[0].l = peer_;
877 args[1].l = exception;
878 m->Invoke(this, handler, args, NULL);
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700879
880 // If the handler threw, clear that exception too.
881 ClearException();
882}
883
Elliott Hughesa2155262011-11-16 16:26:58 -0800884Object* Thread::GetThreadGroup() const {
885 return gThread_group->GetObject(peer_);
886}
887
Brian Carlstrom4514d3c2011-10-21 17:01:31 -0700888void Thread::RemoveFromThreadGroup() {
889 // this.group.removeThread(this);
890 // group can be null if we're in the compiler or a test.
Elliott Hughesa2155262011-11-16 16:26:58 -0800891 Object* group = GetThreadGroup();
Brian Carlstrom4514d3c2011-10-21 17:01:31 -0700892 if (group != NULL) {
893 Method* m = group->GetClass()->FindVirtualMethodForVirtualOrInterface(gThreadGroup_removeThread);
Elliott Hughes77405792012-03-15 15:22:12 -0700894 JValue args[1];
895 args[0].l = peer_;
896 m->Invoke(this, group, args, NULL);
Brian Carlstrom4514d3c2011-10-21 17:01:31 -0700897 }
898}
899
Ian Rogers408f79a2011-08-23 18:22:33 -0700900size_t Thread::NumSirtReferences() {
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700901 size_t count = 0;
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700902 for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->GetLink()) {
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700903 count += cur->NumberOfReferences();
904 }
905 return count;
906}
907
TDYa12728f1a142012-03-15 21:51:52 -0700908size_t Thread::NumShadowFrameReferences() {
909 size_t count = 0;
910 for (ShadowFrame* cur = top_shadow_frame_; cur; cur = cur->GetLink()) {
911 count += cur->NumberOfReferences();
912 }
913 return count;
914}
915
Ian Rogers408f79a2011-08-23 18:22:33 -0700916bool Thread::SirtContains(jobject obj) {
917 Object** sirt_entry = reinterpret_cast<Object**>(obj);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700918 for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->GetLink()) {
919 if (cur->Contains(sirt_entry)) {
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700920 return true;
921 }
922 }
923 return false;
924}
925
TDYa12728f1a142012-03-15 21:51:52 -0700926bool Thread::ShadowFrameContains(jobject obj) {
927 Object** shadow_frame_entry = reinterpret_cast<Object**>(obj);
928 for (ShadowFrame* cur = top_shadow_frame_; cur; cur = cur->GetLink()) {
929 if (cur->Contains(shadow_frame_entry)) {
930 return true;
931 }
932 }
933 return false;
934}
935
936bool Thread::StackReferencesContain(jobject obj) {
937 return SirtContains(obj) || ShadowFrameContains(obj);
938}
939
Shih-wei Liao8dfc9d52011-09-28 18:06:15 -0700940void Thread::SirtVisitRoots(Heap::RootVisitor* visitor, void* arg) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700941 for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->GetLink()) {
Shih-wei Liao8dfc9d52011-09-28 18:06:15 -0700942 size_t num_refs = cur->NumberOfReferences();
943 for (size_t j = 0; j < num_refs; j++) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700944 Object* object = cur->GetReference(j);
Brian Carlstrom5e73f9c2011-10-11 11:28:12 -0700945 if (object != NULL) {
946 visitor(object, arg);
947 }
Shih-wei Liao8dfc9d52011-09-28 18:06:15 -0700948 }
949 }
950}
951
Logan Chienf7ad17e2012-03-15 03:10:03 +0800952void Thread::ShadowFrameVisitRoots(Heap::RootVisitor* visitor, void* arg) {
953 for (ShadowFrame* cur = top_shadow_frame_; cur; cur = cur->GetLink()) {
954 size_t num_refs = cur->NumberOfReferences();
955 for (size_t j = 0; j < num_refs; j++) {
956 Object* object = cur->GetReference(j);
957 if (object != NULL) {
958 visitor(object, arg);
959 }
960 }
961 }
962}
963
Ian Rogers408f79a2011-08-23 18:22:33 -0700964Object* Thread::DecodeJObject(jobject obj) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700965 DCHECK(CanAccessDirectReferences());
Ian Rogers408f79a2011-08-23 18:22:33 -0700966 if (obj == NULL) {
967 return NULL;
968 }
969 IndirectRef ref = reinterpret_cast<IndirectRef>(obj);
970 IndirectRefKind kind = GetIndirectRefKind(ref);
971 Object* result;
972 switch (kind) {
973 case kLocal:
974 {
Elliott Hughes69f5bc62011-08-24 09:26:14 -0700975 IndirectReferenceTable& locals = jni_env_->locals;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700976 result = const_cast<Object*>(locals.Get(ref));
Ian Rogers408f79a2011-08-23 18:22:33 -0700977 break;
978 }
979 case kGlobal:
980 {
981 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
982 IndirectReferenceTable& globals = vm->globals;
983 MutexLock mu(vm->globals_lock);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700984 result = const_cast<Object*>(globals.Get(ref));
Ian Rogers408f79a2011-08-23 18:22:33 -0700985 break;
986 }
987 case kWeakGlobal:
988 {
989 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
990 IndirectReferenceTable& weak_globals = vm->weak_globals;
991 MutexLock mu(vm->weak_globals_lock);
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700992 result = const_cast<Object*>(weak_globals.Get(ref));
Ian Rogers408f79a2011-08-23 18:22:33 -0700993 if (result == kClearedJniWeakGlobal) {
994 // This is a special case where it's okay to return NULL.
995 return NULL;
996 }
997 break;
998 }
999 case kSirtOrInvalid:
1000 default:
1001 // TODO: make stack indirect reference table lookup more efficient
1002 // Check if this is a local reference in the SIRT
TDYa12728f1a142012-03-15 21:51:52 -07001003 if (StackReferencesContain(obj)) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001004 result = *reinterpret_cast<Object**>(obj); // Read from SIRT
Elliott Hughesc2dc62d2012-01-17 20:06:12 -08001005 } else if (Runtime::Current()->GetJavaVM()->work_around_app_jni_bugs) {
Ian Rogers408f79a2011-08-23 18:22:33 -07001006 // Assume an invalid local reference is actually a direct pointer.
1007 result = reinterpret_cast<Object*>(obj);
1008 } else {
Elliott Hughesa2501992011-08-26 19:39:54 -07001009 result = kInvalidIndirectRefObject;
Ian Rogers408f79a2011-08-23 18:22:33 -07001010 }
1011 }
1012
1013 if (result == NULL) {
Elliott Hughesa2501992011-08-26 19:39:54 -07001014 LOG(ERROR) << "JNI ERROR (app bug): use of deleted " << kind << ": " << obj;
1015 JniAbort(NULL);
1016 } else {
1017 if (result != kInvalidIndirectRefObject) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001018 Runtime::Current()->GetHeap()->VerifyObject(result);
Elliott Hughesa2501992011-08-26 19:39:54 -07001019 }
Ian Rogers408f79a2011-08-23 18:22:33 -07001020 }
Ian Rogers408f79a2011-08-23 18:22:33 -07001021 return result;
1022}
1023
Shih-wei Liao9b576b42011-08-29 01:45:07 -07001024class CountStackDepthVisitor : public Thread::StackVisitor {
1025 public:
Elliott Hughes29f27422011-09-18 16:02:18 -07001026 CountStackDepthVisitor() : depth_(0), skip_depth_(0), skipping_(true) {}
Elliott Hughesd369bb72011-09-12 14:41:14 -07001027
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001028 bool VisitFrame(const Frame& frame, uintptr_t /*pc*/) {
Elliott Hughes29f27422011-09-18 16:02:18 -07001029 // We want to skip frames up to and including the exception's constructor.
Ian Rogers90865722011-09-19 11:11:44 -07001030 // Note we also skip the frame if it doesn't have a method (namely the callee
1031 // save frame)
Ian Rogers5167c972012-02-03 10:41:20 -08001032 if (skipping_ && frame.HasMethod() &&
1033 !Throwable::GetJavaLangThrowable()->IsAssignableFrom(frame.GetMethod()->GetDeclaringClass())) {
Elliott Hughes29f27422011-09-18 16:02:18 -07001034 skipping_ = false;
1035 }
1036 if (!skipping_) {
Ian Rogers6b0870d2011-12-15 19:38:12 -08001037 if (frame.HasMethod()) { // ignore callee save frames
1038 ++depth_;
1039 }
Elliott Hughes29f27422011-09-18 16:02:18 -07001040 } else {
1041 ++skip_depth_;
1042 }
Elliott Hughes530fa002012-03-12 11:44:49 -07001043 return true;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001044 }
Shih-wei Liao9b576b42011-08-29 01:45:07 -07001045
1046 int GetDepth() const {
Ian Rogersaaa20802011-09-11 21:47:37 -07001047 return depth_;
Shih-wei Liao9b576b42011-08-29 01:45:07 -07001048 }
1049
Elliott Hughes29f27422011-09-18 16:02:18 -07001050 int GetSkipDepth() const {
1051 return skip_depth_;
1052 }
1053
Shih-wei Liao9b576b42011-08-29 01:45:07 -07001054 private:
Ian Rogersaaa20802011-09-11 21:47:37 -07001055 uint32_t depth_;
Elliott Hughes29f27422011-09-18 16:02:18 -07001056 uint32_t skip_depth_;
1057 bool skipping_;
Shih-wei Liao9b576b42011-08-29 01:45:07 -07001058};
1059
Ian Rogersaaa20802011-09-11 21:47:37 -07001060class BuildInternalStackTraceVisitor : public Thread::StackVisitor {
Shih-wei Liao9b576b42011-08-29 01:45:07 -07001061 public:
Ian Rogers283ed0d2012-02-16 15:25:09 -08001062 explicit BuildInternalStackTraceVisitor(int skip_depth)
Elliott Hughes726079d2011-10-07 18:43:44 -07001063 : skip_depth_(skip_depth), count_(0), pc_trace_(NULL), method_trace_(NULL), local_ref_(NULL) {
Ian Rogers283ed0d2012-02-16 15:25:09 -08001064 }
1065
1066 bool Init(int depth, ScopedJniThreadState& ts) {
Ian Rogersaaa20802011-09-11 21:47:37 -07001067 // Allocate method trace with an extra slot that will hold the PC trace
Elliott Hughes01158d72011-09-19 19:47:10 -07001068 method_trace_ = Runtime::Current()->GetClassLinker()->AllocObjectArray<Object>(depth + 1);
Elliott Hughes726079d2011-10-07 18:43:44 -07001069 if (method_trace_ == NULL) {
Ian Rogers283ed0d2012-02-16 15:25:09 -08001070 return false;
Elliott Hughes726079d2011-10-07 18:43:44 -07001071 }
Ian Rogersaaa20802011-09-11 21:47:37 -07001072 // Register a local reference as IntArray::Alloc may trigger GC
1073 local_ref_ = AddLocalReference<jobject>(ts.Env(), method_trace_);
1074 pc_trace_ = IntArray::Alloc(depth);
Elliott Hughes726079d2011-10-07 18:43:44 -07001075 if (pc_trace_ == NULL) {
Ian Rogers283ed0d2012-02-16 15:25:09 -08001076 return false;
Elliott Hughes726079d2011-10-07 18:43:44 -07001077 }
Ian Rogersaaa20802011-09-11 21:47:37 -07001078#ifdef MOVING_GARBAGE_COLLECTOR
1079 // Re-read after potential GC
Brian Carlstrom26736442012-02-16 18:24:26 -08001080 method_trace_ = Decode<ObjectArray<Object>*>(ts.Env(), local_ref_);
Ian Rogersaaa20802011-09-11 21:47:37 -07001081#endif
1082 // Save PC trace in last element of method trace, also places it into the
1083 // object graph.
1084 method_trace_->Set(depth, pc_trace_);
Ian Rogers283ed0d2012-02-16 15:25:09 -08001085 return true;
Shih-wei Liao9b576b42011-08-29 01:45:07 -07001086 }
1087
Ian Rogersaaa20802011-09-11 21:47:37 -07001088 virtual ~BuildInternalStackTraceVisitor() {}
Shih-wei Liao9b576b42011-08-29 01:45:07 -07001089
Elliott Hughes530fa002012-03-12 11:44:49 -07001090 bool VisitFrame(const Frame& frame, uintptr_t pc) {
Elliott Hughes726079d2011-10-07 18:43:44 -07001091 if (method_trace_ == NULL || pc_trace_ == NULL) {
Elliott Hughes530fa002012-03-12 11:44:49 -07001092 return true; // We're probably trying to fillInStackTrace for an OutOfMemoryError.
Elliott Hughes726079d2011-10-07 18:43:44 -07001093 }
Elliott Hughes29f27422011-09-18 16:02:18 -07001094 if (skip_depth_ > 0) {
1095 skip_depth_--;
Elliott Hughes530fa002012-03-12 11:44:49 -07001096 return true;
Elliott Hughes29f27422011-09-18 16:02:18 -07001097 }
Ian Rogers6b0870d2011-12-15 19:38:12 -08001098 if (!frame.HasMethod()) {
Elliott Hughes530fa002012-03-12 11:44:49 -07001099 return true; // ignore callee save frames
Ian Rogers6b0870d2011-12-15 19:38:12 -08001100 }
Ian Rogersaaa20802011-09-11 21:47:37 -07001101 method_trace_->Set(count_, frame.GetMethod());
Ian Rogersbdb03912011-09-14 00:55:44 -07001102 pc_trace_->Set(count_, pc);
Ian Rogersaaa20802011-09-11 21:47:37 -07001103 ++count_;
Elliott Hughes530fa002012-03-12 11:44:49 -07001104 return true;
Shih-wei Liao9b576b42011-08-29 01:45:07 -07001105 }
1106
Ian Rogersaaa20802011-09-11 21:47:37 -07001107 jobject GetInternalStackTrace() const {
1108 return local_ref_;
Shih-wei Liao9b576b42011-08-29 01:45:07 -07001109 }
1110
1111 private:
Elliott Hughes29f27422011-09-18 16:02:18 -07001112 // How many more frames to skip.
1113 int32_t skip_depth_;
Ian Rogersaaa20802011-09-11 21:47:37 -07001114 // Current position down stack trace
1115 uint32_t count_;
1116 // Array of return PC values
1117 IntArray* pc_trace_;
1118 // An array of the methods on the stack, the last entry is a reference to the
1119 // PC trace
1120 ObjectArray<Object>* method_trace_;
1121 // Local indirect reference table entry for method trace
1122 jobject local_ref_;
Shih-wei Liao9b576b42011-08-29 01:45:07 -07001123};
1124
Elliott Hughesa43cb5e2011-10-07 11:37:59 -07001125// TODO: remove this.
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07001126static uintptr_t ManglePc(uintptr_t pc) {
Elliott Hughesa43cb5e2011-10-07 11:37:59 -07001127 // Move the PC back 2 bytes as a call will frequently terminate the
1128 // decoding of a particular instruction and we want to make sure we
1129 // get the Dex PC of the instruction with the call and not the
1130 // instruction following.
1131 if (pc > 0) { pc -= 2; }
1132 return pc;
1133}
1134
1135// TODO: remove this.
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07001136static uintptr_t DemanglePc(uintptr_t pc) {
Elliott Hughesa43cb5e2011-10-07 11:37:59 -07001137 // Revert mangling for the case where we need the PC to return to the upcall
Ian Rogers9a8a8882012-03-08 02:30:55 -08001138 if (pc > 0) { pc += 2; }
1139 return pc;
Elliott Hughesa43cb5e2011-10-07 11:37:59 -07001140}
Ian Rogersf57c47c2011-10-06 00:06:17 -07001141
Logan Chienf7ad17e2012-03-15 03:10:03 +08001142void Thread::PushShadowFrame(ShadowFrame* frame) {
1143 frame->SetLink(top_shadow_frame_);
1144 top_shadow_frame_ = frame;
1145}
1146
1147ShadowFrame* Thread::PopShadowFrame() {
1148 CHECK(top_shadow_frame_ != NULL);
1149 ShadowFrame* frame = top_shadow_frame_;
1150 top_shadow_frame_ = frame->GetLink();
1151 return frame;
1152}
1153
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001154void Thread::PushSirt(StackIndirectReferenceTable* sirt) {
1155 sirt->SetLink(top_sirt_);
1156 top_sirt_ = sirt;
1157}
1158
1159StackIndirectReferenceTable* Thread::PopSirt() {
1160 CHECK(top_sirt_ != NULL);
1161 StackIndirectReferenceTable* sirt = top_sirt_;
1162 top_sirt_ = top_sirt_->GetLink();
1163 return sirt;
1164}
1165
TDYa1279b2ba2e2012-03-19 10:12:32 -07001166#if !defined(ART_USE_LLVM_COMPILER) // LLVM use ShadowFrame
1167
Elliott Hughes530fa002012-03-12 11:44:49 -07001168void Thread::WalkStack(StackVisitor* visitor, bool include_upcalls) const {
Elliott Hughesd369bb72011-09-12 14:41:14 -07001169 Frame frame = GetTopOfStack();
Elliott Hughesa43cb5e2011-10-07 11:37:59 -07001170 uintptr_t pc = ManglePc(top_of_managed_stack_pc_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001171 uint32_t trace_stack_depth = 0;
Shih-wei Liao9b576b42011-08-29 01:45:07 -07001172 // TODO: enable this CHECK after native_to_managed_record_ is initialized during startup.
1173 // CHECK(native_to_managed_record_ != NULL);
1174 NativeToManagedRecord* record = native_to_managed_record_;
Ian Rogers57b86d42012-03-27 16:05:41 -07001175 bool method_tracing_active = Runtime::Current()->IsMethodTracingActive();
Elliott Hughes530fa002012-03-12 11:44:49 -07001176 while (frame.GetSP() != NULL) {
1177 for ( ; frame.GetMethod() != NULL; frame.Next()) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001178 frame.GetMethod()->AssertPcIsWithinCode(pc);
Elliott Hughes530fa002012-03-12 11:44:49 -07001179 bool should_continue = visitor->VisitFrame(frame, pc);
Ian Rogers57b86d42012-03-27 16:05:41 -07001180 if (UNLIKELY(!should_continue)) {
Elliott Hughes530fa002012-03-12 11:44:49 -07001181 return;
1182 }
Ian Rogers57b86d42012-03-27 16:05:41 -07001183 uintptr_t return_pc = frame.GetReturnPC();
1184 if (LIKELY(!method_tracing_active)) {
1185 pc = ManglePc(return_pc);
1186 } else {
1187 if (IsTraceExitPc(return_pc)) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001188 TraceStackFrame trace_frame = GetTraceStackFrame(trace_stack_depth++);
jeffhao26c0a1a2012-01-17 16:28:33 -08001189 CHECK(trace_frame.method_ == frame.GetMethod());
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001190 pc = ManglePc(trace_frame.return_pc_);
Ian Rogers57b86d42012-03-27 16:05:41 -07001191 } else {
1192 pc = ManglePc(return_pc);
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001193 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -08001194 }
Shih-wei Liao9b576b42011-08-29 01:45:07 -07001195 }
Elliott Hughes530fa002012-03-12 11:44:49 -07001196 if (include_upcalls) {
1197 bool should_continue = visitor->VisitFrame(frame, pc);
1198 if (!should_continue) {
1199 return;
1200 }
1201 }
Shih-wei Liao9b576b42011-08-29 01:45:07 -07001202 if (record == NULL) {
Elliott Hughes530fa002012-03-12 11:44:49 -07001203 return;
Shih-wei Liao9b576b42011-08-29 01:45:07 -07001204 }
Ian Rogersbdb03912011-09-14 00:55:44 -07001205 // last_tos should return Frame instead of sp?
Ian Rogersff1ed472011-09-20 13:46:24 -07001206 frame.SetSP(reinterpret_cast<Method**>(record->last_top_of_managed_stack_));
Elliott Hughesa43cb5e2011-10-07 11:37:59 -07001207 pc = ManglePc(record->last_top_of_managed_stack_pc_);
Ian Rogersbdb03912011-09-14 00:55:44 -07001208 record = record->link_;
1209 }
1210}
1211
TDYa1279b2ba2e2012-03-19 10:12:32 -07001212#else // defined(ART_USE_LLVM_COMPILER) // LLVM uses ShadowFrame
1213
1214void Thread::WalkStack(StackVisitor* visitor, bool /*include_upcalls*/) const {
1215 for (ShadowFrame* cur = top_shadow_frame_; cur; cur = cur->GetLink()) {
1216 Frame frame;
1217 frame.SetSP(reinterpret_cast<Method**>(reinterpret_cast<byte*>(cur) +
1218 ShadowFrame::MethodOffset()));
Shih-wei Liao02f01fe2012-03-26 12:58:11 -07001219 bool should_continue = visitor->VisitFrame(frame, cur->GetLineNumber());
TDYa1279b2ba2e2012-03-19 10:12:32 -07001220 if (!should_continue) {
1221 return;
1222 }
1223 }
1224}
1225
1226/*
1227 * | |
1228 * | |
1229 * | |
1230 * | . |
1231 * | . |
1232 * | . |
1233 * | . |
1234 * | Method* |
1235 * | . |
1236 * | . | <-- top_shadow_frame_ (ShadowFrame*)
1237 * / +------------------------+
1238 * ->| . |
1239 * . | . |
1240 * . | . |
1241 * /+------------------------+
1242 * / | . |
1243 * / | . |
1244 * --- | | . |
1245 * | | | . |
1246 * | | Method* | <-- frame.GetSP() (Method**)
1247 * ShadowFrame \ | . |
1248 * | ->| . | <-- cur (ShadowFrame*)
1249 * --- /+------------------------+
1250 * / | . |
1251 * / | . |
1252 * --- | | . |
1253 * | cur->GetLink() | | . |
1254 * | | Method* |
1255 * ShadowFrame \ | . |
1256 * | ->| . |
1257 * --- +------------------------+
1258 * | . |
1259 * | . |
1260 * | . |
1261 * +========================+
1262 */
1263
1264#endif
1265
Elliott Hughes01158d72011-09-19 19:47:10 -07001266jobject Thread::CreateInternalStackTrace(JNIEnv* env) const {
Ian Rogersaaa20802011-09-11 21:47:37 -07001267 // Compute depth of stack
Shih-wei Liao9b576b42011-08-29 01:45:07 -07001268 CountStackDepthVisitor count_visitor;
1269 WalkStack(&count_visitor);
1270 int32_t depth = count_visitor.GetDepth();
Elliott Hughes29f27422011-09-18 16:02:18 -07001271 int32_t skip_depth = count_visitor.GetSkipDepth();
Shih-wei Liao44175362011-08-28 16:59:17 -07001272
Ian Rogersaaa20802011-09-11 21:47:37 -07001273 // Transition into runnable state to work on Object*/Array*
Elliott Hughes01158d72011-09-19 19:47:10 -07001274 ScopedJniThreadState ts(env);
Ian Rogersaaa20802011-09-11 21:47:37 -07001275
1276 // Build internal stack trace
Ian Rogers77f74c12012-02-16 22:35:30 -08001277 BuildInternalStackTraceVisitor build_trace_visitor(skip_depth);
1278 if (!build_trace_visitor.Init(depth, ts)) {
Ian Rogers283ed0d2012-02-16 15:25:09 -08001279 return NULL; // Allocation failed
Ian Rogers283ed0d2012-02-16 15:25:09 -08001280 }
Brian Carlstrom26736442012-02-16 18:24:26 -08001281 WalkStack(&build_trace_visitor);
1282 return build_trace_visitor.GetInternalStackTrace();
Ian Rogersaaa20802011-09-11 21:47:37 -07001283}
1284
Elliott Hughes01158d72011-09-19 19:47:10 -07001285jobjectArray Thread::InternalStackTraceToStackTraceElementArray(JNIEnv* env, jobject internal,
1286 jobjectArray output_array, int* stack_depth) {
Ian Rogersaaa20802011-09-11 21:47:37 -07001287 // Transition into runnable state to work on Object*/Array*
1288 ScopedJniThreadState ts(env);
Ian Rogersaaa20802011-09-11 21:47:37 -07001289 // Decode the internal stack trace into the depth, method trace and PC trace
1290 ObjectArray<Object>* method_trace =
1291 down_cast<ObjectArray<Object>*>(Decode<Object*>(ts.Env(), internal));
Ian Rogers9074b992011-10-26 17:41:55 -07001292 int32_t depth = method_trace->GetLength() - 1;
Ian Rogersaaa20802011-09-11 21:47:37 -07001293 IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth));
1294
1295 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1296
Elliott Hughes01158d72011-09-19 19:47:10 -07001297 jobjectArray result;
1298 ObjectArray<StackTraceElement>* java_traces;
1299 if (output_array != NULL) {
1300 // Reuse the array we were given.
1301 result = output_array;
1302 java_traces = reinterpret_cast<ObjectArray<StackTraceElement>*>(Decode<Array*>(env,
1303 output_array));
1304 // ...adjusting the number of frames we'll write to not exceed the array length.
1305 depth = std::min(depth, java_traces->GetLength());
1306 } else {
1307 // Create java_trace array and place in local reference table
1308 java_traces = class_linker->AllocStackTraceElementArray(depth);
Elliott Hughes30646832011-10-13 16:59:46 -07001309 if (java_traces == NULL) {
1310 return NULL;
1311 }
Elliott Hughes01158d72011-09-19 19:47:10 -07001312 result = AddLocalReference<jobjectArray>(ts.Env(), java_traces);
1313 }
1314
1315 if (stack_depth != NULL) {
1316 *stack_depth = depth;
1317 }
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001318
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001319 MethodHelper mh;
Shih-wei Liao9b576b42011-08-29 01:45:07 -07001320 for (int32_t i = 0; i < depth; ++i) {
Ian Rogersaaa20802011-09-11 21:47:37 -07001321 // Prepare parameters for StackTraceElement(String cls, String method, String file, int line)
1322 Method* method = down_cast<Method*>(method_trace->Get(i));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001323 mh.ChangeMethod(method);
Ian Rogersaaa20802011-09-11 21:47:37 -07001324 uint32_t native_pc = pc_trace->Get(i);
Shih-wei Liao02f01fe2012-03-26 12:58:11 -07001325#if !defined(ART_USE_LLVM_COMPILER)
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001326 int32_t line_number = mh.GetLineNumFromNativePC(native_pc);
Shih-wei Liao02f01fe2012-03-26 12:58:11 -07001327#else
1328 int32_t line_number = native_pc; // LLVM stored line_number in the ShadowFrame
1329#endif
Ian Rogersaaa20802011-09-11 21:47:37 -07001330 // Allocate element, potentially triggering GC
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001331 // TODO: reuse class_name_object via Class::name_?
Ian Rogers48601312011-12-07 16:45:19 -08001332 const char* descriptor = mh.GetDeclaringClassDescriptor();
1333 CHECK(descriptor != NULL);
1334 std::string class_name(PrettyDescriptor(descriptor));
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001335 SirtRef<String> class_name_object(String::AllocFromModifiedUtf8(class_name.c_str()));
1336 if (class_name_object.get() == NULL) {
1337 return NULL;
1338 }
Ian Rogers48601312011-12-07 16:45:19 -08001339 const char* method_name = mh.GetName();
1340 CHECK(method_name != NULL);
1341 SirtRef<String> method_name_object(String::AllocFromModifiedUtf8(method_name));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001342 if (method_name_object.get() == NULL) {
1343 return NULL;
1344 }
Ian Rogers48601312011-12-07 16:45:19 -08001345 const char* source_file = mh.GetDeclaringClassSourceFile();
1346 SirtRef<String> source_name_object(String::AllocFromModifiedUtf8(source_file));
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001347 StackTraceElement* obj = StackTraceElement::Alloc(class_name_object.get(),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001348 method_name_object.get(),
1349 source_name_object.get(),
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001350 line_number);
Elliott Hughes30646832011-10-13 16:59:46 -07001351 if (obj == NULL) {
1352 return NULL;
1353 }
Ian Rogersaaa20802011-09-11 21:47:37 -07001354#ifdef MOVING_GARBAGE_COLLECTOR
1355 // Re-read after potential GC
1356 java_traces = Decode<ObjectArray<Object>*>(ts.Env(), result);
1357 method_trace = down_cast<ObjectArray<Object>*>(Decode<Object*>(ts.Env(), internal));
1358 pc_trace = down_cast<IntArray*>(method_trace->Get(depth));
1359#endif
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001360 java_traces->Set(i, obj);
1361 }
Ian Rogersaaa20802011-09-11 21:47:37 -07001362 return result;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001363}
1364
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001365void Thread::ThrowNewExceptionF(const char* exception_class_descriptor, const char* fmt, ...) {
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001366 va_list args;
1367 va_start(args, fmt);
Elliott Hughes4a2b4172011-09-20 17:08:25 -07001368 ThrowNewExceptionV(exception_class_descriptor, fmt, args);
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001369 va_end(args);
Elliott Hughes4a2b4172011-09-20 17:08:25 -07001370}
1371
1372void Thread::ThrowNewExceptionV(const char* exception_class_descriptor, const char* fmt, va_list ap) {
1373 std::string msg;
1374 StringAppendV(&msg, fmt, ap);
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001375 ThrowNewException(exception_class_descriptor, msg.c_str());
1376}
Elliott Hughes37f7a402011-08-22 18:56:01 -07001377
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001378void Thread::ThrowNewException(const char* exception_class_descriptor, const char* msg) {
Elliott Hughese5b0dc82011-08-23 09:59:02 -07001379 // Convert "Ljava/lang/Exception;" into JNI-style "java/lang/Exception".
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001380 CHECK_EQ('L', exception_class_descriptor[0]);
Elliott Hughese5b0dc82011-08-23 09:59:02 -07001381 std::string descriptor(exception_class_descriptor + 1);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001382 CHECK_EQ(';', descriptor[descriptor.length() - 1]);
Elliott Hughese5b0dc82011-08-23 09:59:02 -07001383 descriptor.erase(descriptor.length() - 1);
1384
1385 JNIEnv* env = GetJniEnv();
Elliott Hughes726079d2011-10-07 18:43:44 -07001386 ScopedLocalRef<jclass> exception_class(env, env->FindClass(descriptor.c_str()));
Elliott Hughes30646832011-10-13 16:59:46 -07001387 if (exception_class.get() == NULL) {
1388 LOG(ERROR) << "Couldn't throw new " << descriptor << " because JNI FindClass failed: "
1389 << PrettyTypeOf(GetException());
1390 CHECK(IsExceptionPending());
1391 return;
1392 }
Brian Carlstromebd1fd22011-12-07 15:46:26 -08001393 if (!Runtime::Current()->IsStarted()) {
1394 // Something is trying to throw an exception without a started
1395 // runtime, which is the common case in the compiler. We won't be
1396 // able to invoke the constructor of the exception, so use
1397 // AllocObject which will not invoke a constructor.
1398 ScopedLocalRef<jthrowable> exception(
1399 env, reinterpret_cast<jthrowable>(env->AllocObject(exception_class.get())));
1400 if (exception.get() != NULL) {
1401 ScopedJniThreadState ts(env);
1402 Throwable* t = reinterpret_cast<Throwable*>(ts.Self()->DecodeJObject(exception.get()));
Ian Rogers02fbef02012-01-31 22:15:33 -08001403 t->SetDetailMessage(String::AllocFromModifiedUtf8(msg));
Brian Carlstromebd1fd22011-12-07 15:46:26 -08001404 ts.Self()->SetException(t);
1405 } else {
1406 LOG(ERROR) << "Couldn't throw new " << descriptor << " because JNI AllocObject failed: "
1407 << PrettyTypeOf(GetException());
1408 CHECK(IsExceptionPending());
1409 }
1410 return;
1411 }
Elliott Hughes726079d2011-10-07 18:43:44 -07001412 int rc = env->ThrowNew(exception_class.get(), msg);
Elliott Hughes30646832011-10-13 16:59:46 -07001413 if (rc != JNI_OK) {
1414 LOG(ERROR) << "Couldn't throw new " << descriptor << " because JNI ThrowNew failed: "
1415 << PrettyTypeOf(GetException());
1416 CHECK(IsExceptionPending());
Elliott Hughes30646832011-10-13 16:59:46 -07001417 }
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001418}
1419
Elliott Hughes418dfe72011-10-06 18:56:27 -07001420void Thread::ThrowOutOfMemoryError(Class* c, size_t byte_count) {
Elliott Hughes2ced6a52011-10-16 18:44:48 -07001421 std::string msg(StringPrintf("Failed to allocate a %zd-byte %s", byte_count,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001422 PrettyDescriptor(c).c_str()));
Elliott Hughes2ced6a52011-10-16 18:44:48 -07001423 ThrowOutOfMemoryError(msg.c_str());
1424}
1425
1426void Thread::ThrowOutOfMemoryError(const char* msg) {
1427 LOG(ERROR) << StringPrintf("Throwing OutOfMemoryError \"%s\"%s",
1428 msg, (throwing_OutOfMemoryError_ ? " (recursive case)" : ""));
Elliott Hughes726079d2011-10-07 18:43:44 -07001429 if (!throwing_OutOfMemoryError_) {
1430 throwing_OutOfMemoryError_ = true;
Elliott Hughesaccd83d2011-10-17 14:25:58 -07001431 ThrowNewException("Ljava/lang/OutOfMemoryError;", NULL);
Elliott Hughes418dfe72011-10-06 18:56:27 -07001432 } else {
Elliott Hughes726079d2011-10-07 18:43:44 -07001433 SetException(pre_allocated_OutOfMemoryError_);
Elliott Hughes418dfe72011-10-06 18:56:27 -07001434 }
Elliott Hughes726079d2011-10-07 18:43:44 -07001435 throwing_OutOfMemoryError_ = false;
Elliott Hughes79082e32011-08-25 12:07:32 -07001436}
1437
Elliott Hughesaccd83d2011-10-17 14:25:58 -07001438
Elliott Hughes498508c2011-10-17 14:58:22 -07001439Thread* Thread::CurrentFromGdb() {
Elliott Hughesaccd83d2011-10-17 14:25:58 -07001440 return Thread::Current();
1441}
1442
1443void Thread::DumpFromGdb() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001444 std::ostringstream ss;
1445 Dump(ss);
Elliott Hughes95572412011-12-13 18:14:20 -08001446 std::string str(ss.str());
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001447 // log to stderr for debugging command line processes
1448 std::cerr << str;
1449#ifdef HAVE_ANDROID_OS
1450 // log to logcat for debugging frameworks processes
1451 LOG(INFO) << str;
1452#endif
Elliott Hughesaccd83d2011-10-17 14:25:58 -07001453}
1454
Ian Rogersbdb03912011-09-14 00:55:44 -07001455class CatchBlockStackVisitor : public Thread::StackVisitor {
1456 public:
1457 CatchBlockStackVisitor(Class* to_find, Context* ljc)
Ian Rogers57b86d42012-03-27 16:05:41 -07001458 : to_find_(to_find), long_jump_context_(ljc), native_method_count_(0),
1459 method_tracing_active_(Runtime::Current()->IsMethodTracingActive()) {
Ian Rogers67375ac2011-09-14 00:55:44 -07001460#ifndef NDEBUG
1461 handler_pc_ = 0xEBADC0DE;
1462 handler_frame_.SetSP(reinterpret_cast<Method**>(0xEBADF00D));
1463#endif
1464 }
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -07001465
Elliott Hughes530fa002012-03-12 11:44:49 -07001466 bool VisitFrame(const Frame& fr, uintptr_t pc) {
1467 Method* method = fr.GetMethod();
1468 if (method == NULL) {
1469 // This is the upcall, we remember the frame and last_pc so that we may
1470 // long jump to them
1471 handler_pc_ = DemanglePc(pc);
1472 handler_frame_ = fr;
Ian Rogers57b86d42012-03-27 16:05:41 -07001473 return false; // End stack walk.
Elliott Hughes530fa002012-03-12 11:44:49 -07001474 }
1475 uint32_t dex_pc = DexFile::kDexNoIndex;
Ian Rogers57b86d42012-03-27 16:05:41 -07001476 if (method->IsRuntimeMethod()) {
Elliott Hughes530fa002012-03-12 11:44:49 -07001477 // ignore callee save method
Ian Rogers57b86d42012-03-27 16:05:41 -07001478 DCHECK(method->IsCalleeSaveMethod());
Elliott Hughes530fa002012-03-12 11:44:49 -07001479 } else if (method->IsNative()) {
1480 native_method_count_++;
1481 } else {
1482 // Unwind stack during method tracing
Ian Rogers57b86d42012-03-27 16:05:41 -07001483 if (UNLIKELY(method_tracing_active_)) {
1484 if (IsTraceExitPc(DemanglePc(pc))) {
Elliott Hughes530fa002012-03-12 11:44:49 -07001485 pc = ManglePc(TraceMethodUnwindFromCode(Thread::Current()));
1486 }
Ian Rogers67375ac2011-09-14 00:55:44 -07001487 }
Elliott Hughes530fa002012-03-12 11:44:49 -07001488 dex_pc = method->ToDexPC(pc);
1489 }
1490 if (dex_pc != DexFile::kDexNoIndex) {
1491 uint32_t found_dex_pc = method->FindCatchBlock(to_find_, dex_pc);
1492 if (found_dex_pc != DexFile::kDexNoIndex) {
1493 handler_pc_ = method->ToNativePC(found_dex_pc);
1494 handler_frame_ = fr;
Ian Rogers57b86d42012-03-27 16:05:41 -07001495 return false; // End stack walk.
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -07001496 }
1497 }
Elliott Hughes530fa002012-03-12 11:44:49 -07001498 // Caller may be handler, fill in callee saves in context
1499 long_jump_context_->FillCalleeSaves(fr);
Ian Rogers57b86d42012-03-27 16:05:41 -07001500 return true; // Continue stack walk.
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -07001501 }
Ian Rogersbdb03912011-09-14 00:55:44 -07001502
Ian Rogersbdb03912011-09-14 00:55:44 -07001503 // The type of the exception catch block to find
1504 Class* to_find_;
1505 // Frame with found handler or last frame if no handler found
1506 Frame handler_frame_;
Ian Rogers67375ac2011-09-14 00:55:44 -07001507 // PC to branch to for the handler
1508 uintptr_t handler_pc_;
Ian Rogersbdb03912011-09-14 00:55:44 -07001509 // Context that will be the target of the long jump
1510 Context* long_jump_context_;
Ian Rogers67375ac2011-09-14 00:55:44 -07001511 // Number of native methods passed in crawl (equates to number of SIRTs to pop)
1512 uint32_t native_method_count_;
Ian Rogers57b86d42012-03-27 16:05:41 -07001513 // Is method tracing active?
1514 const bool method_tracing_active_;
Ian Rogersbdb03912011-09-14 00:55:44 -07001515};
1516
Ian Rogersff1ed472011-09-20 13:46:24 -07001517void Thread::DeliverException() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001518 const bool kDebugExceptionDelivery = false;
Elliott Hughesd07986f2011-12-06 18:27:45 -08001519 Throwable* exception = GetException(); // Get exception from thread
Ian Rogersff1ed472011-09-20 13:46:24 -07001520 CHECK(exception != NULL);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001521 // Don't leave exception visible while we try to find the handler, which may cause class
Elliott Hughesd07986f2011-12-06 18:27:45 -08001522 // resolution.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001523 ClearException();
1524 if (kDebugExceptionDelivery) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001525 String* msg = exception->GetDetailMessage();
1526 std::string str_msg(msg != NULL ? msg->ToModifiedUtf8() : "");
1527 DumpStack(LOG(INFO) << "Delivering exception: " << PrettyTypeOf(exception)
1528 << ": " << str_msg << std::endl);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001529 }
Ian Rogersbdb03912011-09-14 00:55:44 -07001530
1531 Context* long_jump_context = GetLongJumpContext();
1532 CatchBlockStackVisitor catch_finder(exception->GetClass(), long_jump_context);
Elliott Hughes530fa002012-03-12 11:44:49 -07001533 WalkStack(&catch_finder, true);
Ian Rogersbdb03912011-09-14 00:55:44 -07001534
Elliott Hughesd07986f2011-12-06 18:27:45 -08001535 Method** sp;
1536 uintptr_t throw_native_pc;
1537 Method* throw_method = GetCurrentMethod(&throw_native_pc, &sp);
1538 uintptr_t catch_native_pc = catch_finder.handler_pc_;
1539 Method* catch_method = catch_finder.handler_frame_.GetMethod();
1540 Dbg::PostException(sp, throw_method, throw_native_pc, catch_method, catch_native_pc, exception);
1541
Ian Rogers28ad40d2011-10-27 15:19:26 -07001542 if (kDebugExceptionDelivery) {
Elliott Hughesd07986f2011-12-06 18:27:45 -08001543 if (catch_method == NULL) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001544 LOG(INFO) << "Handler is upcall";
1545 } else {
1546 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1547 const DexFile& dex_file =
Elliott Hughesd07986f2011-12-06 18:27:45 -08001548 class_linker->FindDexFile(catch_method->GetDeclaringClass()->GetDexCache());
1549 int line_number = dex_file.GetLineNumFromPC(catch_method,
1550 catch_method->ToDexPC(catch_finder.handler_pc_));
1551 LOG(INFO) << "Handler: " << PrettyMethod(catch_method) << " (line: " << line_number << ")";
Ian Rogers28ad40d2011-10-27 15:19:26 -07001552 }
1553 }
1554 SetException(exception);
Ian Rogers9a8a8882012-03-08 02:30:55 -08001555 CHECK_NE(catch_native_pc, 0u);
Elliott Hughesd07986f2011-12-06 18:27:45 -08001556 long_jump_context->SetSP(reinterpret_cast<uintptr_t>(catch_finder.handler_frame_.GetSP()));
Ian Rogers9a8a8882012-03-08 02:30:55 -08001557 long_jump_context->SetPC(catch_native_pc);
Ian Rogersbdb03912011-09-14 00:55:44 -07001558 long_jump_context->DoLongJump();
Ian Rogers9a8a8882012-03-08 02:30:55 -08001559 LOG(FATAL) << "UNREACHABLE";
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -07001560}
1561
Ian Rogersbdb03912011-09-14 00:55:44 -07001562Context* Thread::GetLongJumpContext() {
Elliott Hughes85d15452011-09-16 17:33:01 -07001563 Context* result = long_jump_context_;
Ian Rogersbdb03912011-09-14 00:55:44 -07001564 if (result == NULL) {
1565 result = Context::Create();
Elliott Hughes85d15452011-09-16 17:33:01 -07001566 long_jump_context_ = result;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -07001567 }
Ian Rogersbdb03912011-09-14 00:55:44 -07001568 return result;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -07001569}
1570
TDYa127ee0d3fb2012-04-01 14:55:33 -07001571#if !defined(ART_USE_LLVM_COMPILER)
Elliott Hughesd07986f2011-12-06 18:27:45 -08001572Method* Thread::GetCurrentMethod(uintptr_t* pc, Method*** sp) const {
1573 Frame f = top_of_managed_stack_;
1574 Method* m = f.GetMethod();
Elliott Hughes8be2d402012-02-23 14:22:41 -08001575 uintptr_t native_pc = top_of_managed_stack_pc_;
1576
Elliott Hughes9fd66f52011-10-16 12:13:26 -07001577 // We use JNI internally for exception throwing, so it's possible to arrive
1578 // here via a "FromCode" function, in which case there's a synthetic
1579 // callee-save method at the top of the stack. These shouldn't be user-visible,
1580 // so if we find one, skip it and return the compiled method underneath.
jeffhao33dc7712011-11-09 17:54:24 -08001581 if (m != NULL && m->IsCalleeSaveMethod()) {
Elliott Hughes8be2d402012-02-23 14:22:41 -08001582 native_pc = f.GetReturnPC();
Elliott Hughes9fd66f52011-10-16 12:13:26 -07001583 f.Next();
1584 m = f.GetMethod();
1585 }
Elliott Hughesd07986f2011-12-06 18:27:45 -08001586 if (pc != NULL) {
Elliott Hughes8be2d402012-02-23 14:22:41 -08001587 *pc = (m != NULL) ? ManglePc(native_pc) : 0;
jeffhao33dc7712011-11-09 17:54:24 -08001588 }
Elliott Hughesd07986f2011-12-06 18:27:45 -08001589 if (sp != NULL) {
1590 *sp = f.GetSP();
1591 }
1592 return m;
jeffhao33dc7712011-11-09 17:54:24 -08001593}
TDYa127ee0d3fb2012-04-01 14:55:33 -07001594#else
1595Method* Thread::GetCurrentMethod(uintptr_t*, Method***) const {
1596 ShadowFrame* frame = top_shadow_frame_;
TDYa1273db52852012-04-01 15:11:43 -07001597 while (frame->GetMethod()->IsNative()) {
TDYa127ee0d3fb2012-04-01 14:55:33 -07001598 frame = frame->GetLink();
1599 }
1600 return frame->GetMethod();
1601}
1602#endif
jeffhao33dc7712011-11-09 17:54:24 -08001603
Elliott Hughes5f791332011-09-15 17:45:30 -07001604bool Thread::HoldsLock(Object* object) {
1605 if (object == NULL) {
1606 return false;
1607 }
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -07001608 return object->GetThinLockId() == thin_lock_id_;
Elliott Hughes5f791332011-09-15 17:45:30 -07001609}
1610
Elliott Hughes038a8062011-09-18 14:12:41 -07001611bool Thread::IsDaemon() {
1612 return gThread_daemon->GetBoolean(peer_);
1613}
1614
Ian Rogersd6b1f612011-09-27 13:38:14 -07001615class ReferenceMapVisitor : public Thread::StackVisitor {
1616 public:
1617 ReferenceMapVisitor(Context* context, Heap::RootVisitor* root_visitor, void* arg) :
1618 context_(context), root_visitor_(root_visitor), arg_(arg) {
1619 }
1620
Elliott Hughes530fa002012-03-12 11:44:49 -07001621 bool VisitFrame(const Frame& frame, uintptr_t pc) {
Ian Rogersd6b1f612011-09-27 13:38:14 -07001622 Method* m = frame.GetMethod();
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -07001623 if (false) {
jeffhao25045522012-03-13 19:34:37 -07001624 LOG(INFO) << "Visiting stack roots in " << PrettyMethod(m)
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -07001625 << StringPrintf("@ PC:%04x", m->ToDexPC(pc));
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -07001626 }
Ian Rogersd6b1f612011-09-27 13:38:14 -07001627 // Process register map (which native and callee save methods don't have)
Ian Rogers3891c772011-12-06 15:38:28 -08001628 if (!m->IsNative() && !m->IsCalleeSaveMethod() && !m->IsProxyMethod()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001629 CHECK(m->GetGcMap() != NULL) << PrettyMethod(m);
1630 CHECK_NE(0U, m->GetGcMapLength()) << PrettyMethod(m);
1631 verifier::PcToReferenceMap map(m->GetGcMap(), m->GetGcMapLength());
Ian Rogersd81871c2011-10-03 13:57:23 -07001632 const uint8_t* reg_bitmap = map.FindBitMap(m->ToDexPC(pc));
Ian Rogersd6b1f612011-09-27 13:38:14 -07001633 CHECK(reg_bitmap != NULL);
Elliott Hughes68fdbd02011-11-29 19:22:47 -08001634 const VmapTable vmap_table(m->GetVmapTableRaw());
Elliott Hughes11d1b0c2012-01-23 16:57:47 -08001635 const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001636 DCHECK(code_item != NULL); // can't be NULL or how would we compile its instructions?
1637 uint32_t core_spills = m->GetCoreSpillMask();
1638 uint32_t fp_spills = m->GetFpSpillMask();
1639 size_t frame_size = m->GetFrameSizeInBytes();
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -07001640 // For all dex registers in the bitmap
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001641 size_t num_regs = std::min(map.RegWidth() * 8,
1642 static_cast<size_t>(code_item->registers_size_));
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -07001643 for (size_t reg = 0; reg < num_regs; ++reg) {
Ian Rogersd6b1f612011-09-27 13:38:14 -07001644 // Does this register hold a reference?
1645 if (TestBitmap(reg, reg_bitmap)) {
Elliott Hughes68fdbd02011-11-29 19:22:47 -08001646 uint32_t vmap_offset;
Ian Rogersd6b1f612011-09-27 13:38:14 -07001647 Object* ref;
Elliott Hughes68fdbd02011-11-29 19:22:47 -08001648 if (vmap_table.IsInContext(reg, vmap_offset)) {
Ian Rogersd6b1f612011-09-27 13:38:14 -07001649 // Compute the register we need to load from the context
1650 uint32_t spill_mask = m->GetCoreSpillMask();
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -07001651 CHECK_LT(vmap_offset, static_cast<uint32_t>(__builtin_popcount(spill_mask)));
Ian Rogersf57c47c2011-10-06 00:06:17 -07001652 uint32_t matches = 0;
1653 uint32_t spill_shifts = 0;
1654 while (matches != (vmap_offset + 1)) {
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -07001655 DCHECK_NE(spill_mask, 0u);
Ian Rogersf57c47c2011-10-06 00:06:17 -07001656 matches += spill_mask & 1; // Add 1 if the low bit is set
1657 spill_mask >>= 1;
1658 spill_shifts++;
Ian Rogersd6b1f612011-09-27 13:38:14 -07001659 }
Ian Rogersf57c47c2011-10-06 00:06:17 -07001660 spill_shifts--; // wind back one as we want the last match
1661 ref = reinterpret_cast<Object*>(context_->GetGPR(spill_shifts));
Ian Rogersd6b1f612011-09-27 13:38:14 -07001662 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001663 ref = reinterpret_cast<Object*>(frame.GetVReg(code_item, core_spills, fp_spills,
1664 frame_size, reg));
Ian Rogersd6b1f612011-09-27 13:38:14 -07001665 }
Shih-wei Liao4f894e32011-09-27 21:33:19 -07001666 if (ref != NULL) {
1667 root_visitor_(ref, arg_);
1668 }
Ian Rogersd6b1f612011-09-27 13:38:14 -07001669 }
1670 }
1671 }
1672 context_->FillCalleeSaves(frame);
Elliott Hughes530fa002012-03-12 11:44:49 -07001673 return true;
Ian Rogersd6b1f612011-09-27 13:38:14 -07001674 }
1675
1676 private:
1677 bool TestBitmap(int reg, const uint8_t* reg_vector) {
1678 return ((reg_vector[reg / 8] >> (reg % 8)) & 0x01) != 0;
1679 }
1680
1681 // Context used to build up picture of callee saves
1682 Context* context_;
1683 // Call-back when we visit a root
1684 Heap::RootVisitor* root_visitor_;
1685 // Argument to call-back
1686 void* arg_;
1687};
1688
1689void Thread::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
Elliott Hughesd369bb72011-09-12 14:41:14 -07001690 if (exception_ != NULL) {
1691 visitor(exception_, arg);
1692 }
1693 if (peer_ != NULL) {
1694 visitor(peer_, arg);
1695 }
Elliott Hughes726079d2011-10-07 18:43:44 -07001696 if (pre_allocated_OutOfMemoryError_ != NULL) {
1697 visitor(pre_allocated_OutOfMemoryError_, arg);
1698 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001699 if (class_loader_override_ != NULL) {
1700 visitor(class_loader_override_, arg);
1701 }
Elliott Hughes410c0c82011-09-01 17:58:25 -07001702 jni_env_->locals.VisitRoots(visitor, arg);
1703 jni_env_->monitors.VisitRoots(visitor, arg);
Shih-wei Liao8dfc9d52011-09-28 18:06:15 -07001704
1705 SirtVisitRoots(visitor, arg);
Logan Chienf7ad17e2012-03-15 03:10:03 +08001706 ShadowFrameVisitRoots(visitor, arg);
Shih-wei Liao8dfc9d52011-09-28 18:06:15 -07001707
Ian Rogersd6b1f612011-09-27 13:38:14 -07001708 // Cheat and steal the long jump context. Assume that we are not doing a GC during exception
1709 // delivery.
1710 Context* context = GetLongJumpContext();
1711 // Visit roots on this thread's stack
1712 ReferenceMapVisitor mapper(context, visitor, arg);
1713 WalkStack(&mapper);
Elliott Hughes410c0c82011-09-01 17:58:25 -07001714}
1715
jeffhao25045522012-03-13 19:34:37 -07001716#if VERIFY_OBJECT_ENABLED
Elliott Hughesab7b9dc2012-03-27 13:16:29 -07001717static void VerifyObject(const Object* obj, void*) {
jeffhao25045522012-03-13 19:34:37 -07001718 Runtime::Current()->GetHeap()->VerifyObject(obj);
1719}
1720
1721void Thread::VerifyStack() {
jeffhaoe66ac792012-03-19 16:08:46 -07001722 UniquePtr<Context> context(Context::Create());
1723 ReferenceMapVisitor mapper(context.get(), VerifyObject, NULL);
jeffhao25045522012-03-13 19:34:37 -07001724 WalkStack(&mapper);
1725}
1726#endif
1727
Ian Rogersb033c752011-07-20 12:22:35 -07001728static const char* kStateNames[] = {
Elliott Hughes93e74e82011-09-13 11:07:03 -07001729 "Terminated",
Ian Rogersb033c752011-07-20 12:22:35 -07001730 "Runnable",
Elliott Hughes93e74e82011-09-13 11:07:03 -07001731 "TimedWaiting",
Ian Rogersb033c752011-07-20 12:22:35 -07001732 "Blocked",
1733 "Waiting",
Elliott Hughes93e74e82011-09-13 11:07:03 -07001734 "Initializing",
1735 "Starting",
Ian Rogersb033c752011-07-20 12:22:35 -07001736 "Native",
Elliott Hughes93e74e82011-09-13 11:07:03 -07001737 "VmWait",
1738 "Suspended",
Ian Rogersb033c752011-07-20 12:22:35 -07001739};
1740std::ostream& operator<<(std::ostream& os, const Thread::State& state) {
Elliott Hughes8e4aac52011-09-26 17:03:36 -07001741 int32_t int_state = static_cast<int32_t>(state);
Elliott Hughes93e74e82011-09-13 11:07:03 -07001742 if (state >= Thread::kTerminated && state <= Thread::kSuspended) {
1743 os << kStateNames[int_state];
Ian Rogersb033c752011-07-20 12:22:35 -07001744 } else {
Elliott Hughes93e74e82011-09-13 11:07:03 -07001745 os << "State[" << int_state << "]";
Ian Rogersb033c752011-07-20 12:22:35 -07001746 }
1747 return os;
1748}
1749
Elliott Hughes330304d2011-08-12 14:28:05 -07001750std::ostream& operator<<(std::ostream& os, const Thread& thread) {
Elliott Hughes899e7892012-01-24 14:57:32 -08001751 thread.Dump(os, false);
Elliott Hughes330304d2011-08-12 14:28:05 -07001752 return os;
1753}
1754
Elliott Hughesa4060e52012-03-02 16:51:35 -08001755void Thread::CheckSafeToLockOrUnlock(MutexRank rank, bool is_locking) {
Elliott Hughes17057b12012-04-03 10:22:39 -07001756 if (this == NULL) {
1757 CHECK(Runtime::Current()->IsShuttingDown());
1758 return;
1759 }
Elliott Hughesffb465f2012-03-01 18:46:05 -08001760 if (is_locking) {
1761 if (held_mutexes_[rank] == 0) {
1762 bool bad_mutexes_held = false;
1763 for (int i = kMaxMutexRank; i > rank; --i) {
1764 if (held_mutexes_[i] != 0) {
1765 LOG(ERROR) << "holding " << static_cast<MutexRank>(i) << " while " << (is_locking ? "locking" : "unlocking") << " " << rank;
1766 bad_mutexes_held = true;
1767 }
1768 }
Elliott Hughesb5db3bb2012-03-29 20:00:31 -07001769 CHECK(!bad_mutexes_held) << rank;
Elliott Hughesffb465f2012-03-01 18:46:05 -08001770 }
1771 ++held_mutexes_[rank];
1772 } else {
Elliott Hughesb5db3bb2012-03-29 20:00:31 -07001773 CHECK_GT(held_mutexes_[rank], 0U) << rank;
Elliott Hughesffb465f2012-03-01 18:46:05 -08001774 --held_mutexes_[rank];
1775 }
1776}
1777
Elliott Hughesa4060e52012-03-02 16:51:35 -08001778void Thread::CheckSafeToWait(MutexRank rank) {
Elliott Hughes17057b12012-04-03 10:22:39 -07001779 if (this == NULL) {
1780 CHECK(Runtime::Current()->IsShuttingDown());
1781 return;
1782 }
Elliott Hughesa4060e52012-03-02 16:51:35 -08001783 bool bad_mutexes_held = false;
1784 for (int i = kMaxMutexRank; i >= 0; --i) {
1785 if (i != rank && held_mutexes_[i] != 0) {
1786 LOG(ERROR) << "holding " << static_cast<MutexRank>(i) << " while doing condition variable wait on " << rank;
1787 bad_mutexes_held = true;
1788 }
1789 }
1790 if (held_mutexes_[rank] == 0) {
1791 LOG(ERROR) << "*not* holding " << rank << " while doing condition variable wait on it";
1792 bad_mutexes_held = true;
1793 }
1794 CHECK(!bad_mutexes_held);
1795}
1796
Elliott Hughes8daa0922011-09-11 13:46:25 -07001797} // namespace art