Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 1 | /* |
| 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 Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 17 | #include "thread.h" |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 18 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 19 | #include <dynamic_annotations.h> |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 20 | #include <pthread.h> |
| 21 | #include <sys/mman.h> |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 22 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 23 | #include <algorithm> |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 24 | #include <bitset> |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 25 | #include <cerrno> |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 26 | #include <iostream> |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 27 | #include <list> |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 28 | |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 29 | #include "class_linker.h" |
Ian Rogers | d6b1f61 | 2011-09-27 13:38:14 -0700 | [diff] [blame] | 30 | #include "compiler.h" |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 31 | #include "context.h" |
Ian Rogers | d6b1f61 | 2011-09-27 13:38:14 -0700 | [diff] [blame] | 32 | #include "dex_verifier.h" |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 33 | #include "heap.h" |
Elliott Hughes | c5f7c91 | 2011-08-18 14:00:42 -0700 | [diff] [blame] | 34 | #include "jni_internal.h" |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 35 | #include "monitor.h" |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 36 | #include "object.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 37 | #include "runtime.h" |
buzbee | 5433072 | 2011-08-23 16:46:55 -0700 | [diff] [blame] | 38 | #include "runtime_support.h" |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 39 | #include "scoped_jni_thread_state.h" |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 40 | #include "thread_list.h" |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 41 | #include "utils.h" |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 42 | |
| 43 | namespace art { |
| 44 | |
| 45 | pthread_key_t Thread::pthread_key_self_; |
| 46 | |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 47 | static Class* gThreadLock = NULL; |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 48 | static Class* gThrowable = NULL; |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 49 | static Field* gThread_daemon = NULL; |
| 50 | static Field* gThread_group = NULL; |
| 51 | static Field* gThread_lock = NULL; |
| 52 | static Field* gThread_name = NULL; |
| 53 | static Field* gThread_priority = NULL; |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 54 | static Field* gThread_uncaughtHandler = NULL; |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 55 | static Field* gThread_vmData = NULL; |
| 56 | static Field* gThreadGroup_name = NULL; |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 57 | static Field* gThreadLock_thread = NULL; |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 58 | static Method* gThread_run = NULL; |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 59 | static Method* gThreadGroup_removeThread = NULL; |
| 60 | static Method* gUncaughtExceptionHandler_uncaughtException = NULL; |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 61 | |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 62 | void Thread::InitFunctionPointers() { |
buzbee | 5433072 | 2011-08-23 16:46:55 -0700 | [diff] [blame] | 63 | #if defined(__arm__) |
| 64 | pShlLong = art_shl_long; |
| 65 | pShrLong = art_shr_long; |
| 66 | pUshrLong = art_ushr_long; |
buzbee | 7b1b86d | 2011-08-26 18:59:10 -0700 | [diff] [blame] | 67 | pIdiv = __aeabi_idiv; |
| 68 | pIdivmod = __aeabi_idivmod; |
| 69 | pI2f = __aeabi_i2f; |
| 70 | pF2iz = __aeabi_f2iz; |
| 71 | pD2f = __aeabi_d2f; |
| 72 | pF2d = __aeabi_f2d; |
| 73 | pD2iz = __aeabi_d2iz; |
| 74 | pL2f = __aeabi_l2f; |
| 75 | pL2d = __aeabi_l2d; |
| 76 | pFadd = __aeabi_fadd; |
| 77 | pFsub = __aeabi_fsub; |
| 78 | pFdiv = __aeabi_fdiv; |
| 79 | pFmul = __aeabi_fmul; |
| 80 | pFmodf = fmodf; |
| 81 | pDadd = __aeabi_dadd; |
| 82 | pDsub = __aeabi_dsub; |
| 83 | pDdiv = __aeabi_ddiv; |
| 84 | pDmul = __aeabi_dmul; |
| 85 | pFmod = fmod; |
buzbee | 7b1b86d | 2011-08-26 18:59:10 -0700 | [diff] [blame] | 86 | pLdivmod = __aeabi_ldivmod; |
buzbee | 439c4fa | 2011-08-27 15:59:07 -0700 | [diff] [blame] | 87 | pLmul = __aeabi_lmul; |
Ian Rogers | 21d9e83 | 2011-09-23 17:05:09 -0700 | [diff] [blame] | 88 | pAllocObjectFromCode = art_alloc_object_from_code; |
Ian Rogers | b886da8 | 2011-09-23 16:27:54 -0700 | [diff] [blame] | 89 | pArrayAllocFromCode = art_array_alloc_from_code; |
Ian Rogers | e51a511 | 2011-09-23 14:16:35 -0700 | [diff] [blame] | 90 | pCanPutArrayElementFromCode = art_can_put_array_element_from_code; |
Ian Rogers | b886da8 | 2011-09-23 16:27:54 -0700 | [diff] [blame] | 91 | pCheckAndArrayAllocFromCode = art_check_and_array_alloc_from_code; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 92 | pCheckCastFromCode = art_check_cast_from_code; |
| 93 | pHandleFillArrayDataFromCode = art_handle_fill_data_from_code; |
Ian Rogers | cbba6ac | 2011-09-22 16:28:37 -0700 | [diff] [blame] | 94 | pInitializeStaticStorage = art_initialize_static_storage_from_code; |
buzbee | 4a3164f | 2011-09-03 11:25:10 -0700 | [diff] [blame] | 95 | pInvokeInterfaceTrampoline = art_invoke_interface_trampoline; |
buzbee | c1f4504 | 2011-09-21 16:03:19 -0700 | [diff] [blame] | 96 | pTestSuspendFromCode = art_test_suspend; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 97 | pThrowArrayBoundsFromCode = art_throw_array_bounds_from_code; |
| 98 | pThrowDivZeroFromCode = art_throw_div_zero_from_code; |
Ian Rogers | c0c8dc8 | 2011-09-24 18:15:59 -0700 | [diff] [blame] | 99 | pThrowInternalErrorFromCode = art_throw_internal_error_from_code; |
| 100 | pThrowNegArraySizeFromCode = art_throw_neg_array_size_from_code; |
| 101 | pThrowNoSuchMethodFromCode = art_throw_no_such_method_from_code; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 102 | pThrowNullPointerFromCode = art_throw_null_pointer_exception_from_code; |
Ian Rogers | c0c8dc8 | 2011-09-24 18:15:59 -0700 | [diff] [blame] | 103 | pThrowRuntimeExceptionFromCode = art_throw_runtime_exception_from_code; |
Ian Rogers | 932746a | 2011-09-22 18:57:50 -0700 | [diff] [blame] | 104 | pThrowStackOverflowFromCode = art_throw_stack_overflow_from_code; |
Ian Rogers | c0c8dc8 | 2011-09-24 18:15:59 -0700 | [diff] [blame] | 105 | pThrowVerificationErrorFromCode = art_throw_verification_error_from_code; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 106 | pUnlockObjectFromCode = art_unlock_object_from_code; |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 107 | #endif |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 108 | pDeliverException = art_deliver_exception_from_code; |
Ian Rogers | c0c8dc8 | 2011-09-24 18:15:59 -0700 | [diff] [blame] | 109 | pThrowAbstractMethodErrorFromCode = ThrowAbstractMethodErrorFromCode; |
buzbee | c396efc | 2011-09-11 09:36:41 -0700 | [diff] [blame] | 110 | pF2l = F2L; |
| 111 | pD2l = D2L; |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 112 | pMemcpy = memcpy; |
buzbee | e193174 | 2011-08-28 21:15:53 -0700 | [diff] [blame] | 113 | pGet32Static = Field::Get32StaticFromCode; |
| 114 | pSet32Static = Field::Set32StaticFromCode; |
| 115 | pGet64Static = Field::Get64StaticFromCode; |
| 116 | pSet64Static = Field::Set64StaticFromCode; |
| 117 | pGetObjStatic = Field::GetObjStaticFromCode; |
| 118 | pSetObjStatic = Field::SetObjStaticFromCode; |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame] | 119 | pInitializeTypeFromCode = InitializeTypeFromCode; |
buzbee | 561227c | 2011-09-02 15:28:19 -0700 | [diff] [blame] | 120 | pResolveMethodFromCode = ResolveMethodFromCode; |
buzbee | 991e3ac | 2011-09-29 15:44:22 -0700 | [diff] [blame] | 121 | pInstanceofNonTrivialFromCode = Class::IsAssignableFromCode; |
buzbee | 2a475e7 | 2011-09-07 17:19:17 -0700 | [diff] [blame] | 122 | pLockObjectFromCode = LockObjectFromCode; |
Brian Carlstrom | 845490b | 2011-09-19 15:56:53 -0700 | [diff] [blame] | 123 | pFindInstanceFieldFromCode = Field::FindInstanceFieldFromCode; |
buzbee | c1f4504 | 2011-09-21 16:03:19 -0700 | [diff] [blame] | 124 | pCheckSuspendFromCode = artCheckSuspendFromCode; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 125 | pFindNativeMethod = FindNativeMethod; |
| 126 | pDecodeJObjectInThread = DecodeJObjectInThread; |
buzbee | 4a3164f | 2011-09-03 11:25:10 -0700 | [diff] [blame] | 127 | pDebugMe = DebugMe; |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 130 | void Frame::Next() { |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 131 | size_t frame_size = GetMethod()->GetFrameSizeInBytes(); |
| 132 | DCHECK_NE(frame_size, 0u); |
| 133 | DCHECK_LT(frame_size, 1024u); |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 134 | byte* next_sp = reinterpret_cast<byte*>(sp_) + frame_size; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 135 | sp_ = reinterpret_cast<Method**>(next_sp); |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 136 | if (*sp_ != NULL) { |
| 137 | DCHECK((*sp_)->GetClass() == Method::GetMethodClass() || |
| 138 | (*sp_)->GetClass() == Method::GetConstructorClass()); |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 139 | } |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Ian Rogers | 9086572 | 2011-09-19 11:11:44 -0700 | [diff] [blame] | 142 | bool Frame::HasMethod() const { |
| 143 | return GetMethod() != NULL && (!GetMethod()->IsPhony()); |
| 144 | } |
| 145 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 146 | uintptr_t Frame::GetReturnPC() const { |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 147 | byte* pc_addr = reinterpret_cast<byte*>(sp_) + GetMethod()->GetReturnPcOffsetInBytes(); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 148 | return *reinterpret_cast<uintptr_t*>(pc_addr); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Ian Rogers | d6b1f61 | 2011-09-27 13:38:14 -0700 | [diff] [blame] | 151 | uintptr_t Frame::GetVReg(Method* method, int vreg) const { |
| 152 | DCHECK(method == GetMethod()); |
| 153 | int offset = oatVRegOffsetFromMethod(method, vreg); |
| 154 | byte* vreg_addr = reinterpret_cast<byte*>(sp_) + offset; |
| 155 | return *reinterpret_cast<uintptr_t*>(vreg_addr); |
| 156 | } |
| 157 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 158 | uintptr_t Frame::LoadCalleeSave(int num) const { |
| 159 | // Callee saves are held at the top of the frame |
| 160 | Method* method = GetMethod(); |
| 161 | DCHECK(method != NULL); |
| 162 | size_t frame_size = method->GetFrameSizeInBytes(); |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 163 | byte* save_addr = reinterpret_cast<byte*>(sp_) + frame_size - ((num + 1) * kPointerSize); |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 164 | #if defined(__i386__) |
| 165 | save_addr -= kPointerSize; // account for return address |
| 166 | #endif |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 167 | return *reinterpret_cast<uintptr_t*>(save_addr); |
| 168 | } |
| 169 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 170 | Method* Frame::NextMethod() const { |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 171 | byte* next_sp = reinterpret_cast<byte*>(sp_) + |
Shih-wei Liao | d11af15 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 172 | GetMethod()->GetFrameSizeInBytes(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 173 | return *reinterpret_cast<Method**>(next_sp); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 176 | void* Thread::CreateCallback(void* arg) { |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 177 | Thread* self = reinterpret_cast<Thread*>(arg); |
| 178 | Runtime* runtime = Runtime::Current(); |
| 179 | |
| 180 | self->Attach(runtime); |
| 181 | |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 182 | String* thread_name = reinterpret_cast<String*>(gThread_name->GetObject(self->peer_)); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 183 | if (thread_name != NULL) { |
| 184 | SetThreadName(thread_name->ToModifiedUtf8().c_str()); |
| 185 | } |
| 186 | |
| 187 | // Wait until it's safe to start running code. (There may have been a suspend-all |
| 188 | // in progress while we were starting up.) |
| 189 | runtime->GetThreadList()->WaitForGo(); |
| 190 | |
| 191 | // TODO: say "hi" to the debugger. |
| 192 | //if (gDvm.debuggerConnected) { |
| 193 | // dvmDbgPostThreadStart(self); |
| 194 | //} |
| 195 | |
| 196 | // Invoke the 'run' method of our java.lang.Thread. |
| 197 | CHECK(self->peer_ != NULL); |
| 198 | Object* receiver = self->peer_; |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 199 | Method* m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(gThread_run); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 200 | m->Invoke(self, receiver, NULL, NULL); |
| 201 | |
| 202 | // Detach. |
| 203 | runtime->GetThreadList()->Unregister(); |
| 204 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 205 | return NULL; |
| 206 | } |
| 207 | |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 208 | void SetVmData(Object* managed_thread, Thread* native_thread) { |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 209 | gThread_vmData->SetInt(managed_thread, reinterpret_cast<uintptr_t>(native_thread)); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 212 | Thread* Thread::FromManagedThread(JNIEnv* env, jobject java_thread) { |
| 213 | Object* thread = Decode<Object*>(env, java_thread); |
| 214 | return reinterpret_cast<Thread*>(static_cast<uintptr_t>(gThread_vmData->GetInt(thread))); |
| 215 | } |
| 216 | |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 217 | void Thread::Create(Object* peer, size_t stack_size) { |
| 218 | CHECK(peer != NULL); |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 219 | |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 220 | if (stack_size == 0) { |
| 221 | stack_size = Runtime::Current()->GetDefaultStackSize(); |
| 222 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 223 | |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 224 | Thread* native_thread = new Thread; |
| 225 | native_thread->peer_ = peer; |
| 226 | |
| 227 | // Thread.start is synchronized, so we know that vmData is 0, |
| 228 | // and know that we're not racing to assign it. |
| 229 | SetVmData(peer, native_thread); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 230 | |
| 231 | pthread_attr_t attr; |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 232 | CHECK_PTHREAD_CALL(pthread_attr_init, (&attr), "new thread"); |
| 233 | CHECK_PTHREAD_CALL(pthread_attr_setdetachstate, (&attr, PTHREAD_CREATE_DETACHED), "PTHREAD_CREATE_DETACHED"); |
| 234 | CHECK_PTHREAD_CALL(pthread_attr_setstacksize, (&attr, stack_size), stack_size); |
| 235 | CHECK_PTHREAD_CALL(pthread_create, (&native_thread->pthread_, &attr, Thread::CreateCallback, native_thread), "new thread"); |
| 236 | CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attr), "new thread"); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 237 | |
| 238 | // Let the child know when it's safe to start running. |
| 239 | Runtime::Current()->GetThreadList()->SignalGo(native_thread); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 242 | void Thread::Attach(const Runtime* runtime) { |
| 243 | InitCpu(); |
| 244 | InitFunctionPointers(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 245 | |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 246 | thin_lock_id_ = Runtime::Current()->GetThreadList()->AllocThreadId(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 247 | |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 248 | tid_ = ::art::GetTid(); |
| 249 | pthread_ = pthread_self(); |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 250 | |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 251 | InitStackHwm(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 252 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 253 | CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, this), "attach"); |
Elliott Hughes | a5780da | 2011-07-17 11:39:39 -0700 | [diff] [blame] | 254 | |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 255 | jni_env_ = new JNIEnvExt(this, runtime->GetJavaVM()); |
Elliott Hughes | 330304d | 2011-08-12 14:28:05 -0700 | [diff] [blame] | 256 | |
Elliott Hughes | 7a3aeb4 | 2011-09-25 17:39:47 -0700 | [diff] [blame] | 257 | runtime->GetThreadList()->Register(); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | Thread* Thread::Attach(const Runtime* runtime, const char* name, bool as_daemon) { |
| 261 | Thread* self = new Thread; |
| 262 | self->Attach(runtime); |
| 263 | |
Elliott Hughes | 7a3aeb4 | 2011-09-25 17:39:47 -0700 | [diff] [blame] | 264 | self->SetState(Thread::kNative); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 265 | |
| 266 | SetThreadName(name); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 267 | |
| 268 | // If we're the main thread, ClassLinker won't be created until after we're attached, |
| 269 | // so that thread needs a two-stage attach. Regular threads don't need this hack. |
| 270 | if (self->thin_lock_id_ != ThreadList::kMainId) { |
| 271 | self->CreatePeer(name, as_daemon); |
| 272 | } |
| 273 | |
| 274 | return self; |
| 275 | } |
| 276 | |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 277 | jobject GetWellKnownThreadGroup(JNIEnv* env, const char* field_name) { |
| 278 | jclass thread_group_class = env->FindClass("java/lang/ThreadGroup"); |
| 279 | jfieldID fid = env->GetStaticFieldID(thread_group_class, field_name, "Ljava/lang/ThreadGroup;"); |
| 280 | jobject thread_group = env->GetStaticObjectField(thread_group_class, fid); |
| 281 | // This will be null in the compiler (and tests), but never in a running system. |
| 282 | //CHECK(thread_group != NULL) << "java.lang.ThreadGroup." << field_name << " not initialized"; |
| 283 | return thread_group; |
| 284 | } |
| 285 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 286 | void Thread::CreatePeer(const char* name, bool as_daemon) { |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 287 | JNIEnv* env = jni_env_; |
| 288 | |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 289 | const char* field_name = (GetThinLockId() == ThreadList::kMainId) ? "mMain" : "mSystem"; |
| 290 | jobject thread_group = GetWellKnownThreadGroup(env, field_name); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 291 | jobject thread_name = env->NewStringUTF(name); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 292 | jint thread_priority = GetNativePriority(); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 293 | jboolean thread_is_daemon = as_daemon; |
| 294 | |
| 295 | jclass c = env->FindClass("java/lang/Thread"); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 296 | jmethodID mid = env->GetMethodID(c, "<init>", "(Ljava/lang/ThreadGroup;Ljava/lang/String;IZ)V"); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 297 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 298 | jobject peer = env->NewObject(c, mid, thread_group, thread_name, thread_priority, thread_is_daemon); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 299 | peer_ = DecodeJObject(peer); |
Elliott Hughes | 7a3aeb4 | 2011-09-25 17:39:47 -0700 | [diff] [blame] | 300 | SetVmData(peer_, Thread::Current()); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 301 | |
| 302 | // Because we mostly run without code available (in the compiler, in tests), we |
| 303 | // manually assign the fields the constructor should have set. |
| 304 | // TODO: lose this. |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 305 | gThread_daemon->SetBoolean(peer_, thread_is_daemon); |
| 306 | gThread_group->SetObject(peer_, Decode<Object*>(env, thread_group)); |
| 307 | gThread_name->SetObject(peer_, Decode<Object*>(env, thread_name)); |
| 308 | gThread_priority->SetInt(peer_, thread_priority); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 311 | void Thread::InitStackHwm() { |
| 312 | pthread_attr_t attributes; |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 313 | CHECK_PTHREAD_CALL(pthread_getattr_np, (pthread_, &attributes), __FUNCTION__); |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 314 | |
Ian Rogers | 932746a | 2011-09-22 18:57:50 -0700 | [diff] [blame] | 315 | void* temp_stack_base; |
| 316 | CHECK_PTHREAD_CALL(pthread_attr_getstack, (&attributes, &temp_stack_base, &stack_size_), |
| 317 | __FUNCTION__); |
| 318 | stack_base_ = reinterpret_cast<byte*>(temp_stack_base); |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 319 | |
Ian Rogers | 932746a | 2011-09-22 18:57:50 -0700 | [diff] [blame] | 320 | if (stack_size_ <= kStackOverflowReservedBytes) { |
| 321 | LOG(FATAL) << "attempt to attach a thread with a too-small stack (" << stack_size_ << " bytes)"; |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 322 | } |
Elliott Hughes | 449b4bd | 2011-09-09 12:01:38 -0700 | [diff] [blame] | 323 | |
Ian Rogers | 932746a | 2011-09-22 18:57:50 -0700 | [diff] [blame] | 324 | // Set stack_end_ to the bottom of the stack saving space of stack overflows |
| 325 | ResetDefaultStackEnd(); |
Elliott Hughes | 449b4bd | 2011-09-09 12:01:38 -0700 | [diff] [blame] | 326 | |
| 327 | // Sanity check. |
| 328 | int stack_variable; |
| 329 | CHECK_GT(&stack_variable, (void*) stack_end_); |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 330 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 331 | CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attributes), __FUNCTION__); |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 334 | void Thread::Dump(std::ostream& os) const { |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 335 | DumpState(os); |
| 336 | DumpStack(os); |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 339 | std::string GetSchedulerGroup(pid_t tid) { |
| 340 | // /proc/<pid>/group looks like this: |
| 341 | // 2:devices:/ |
| 342 | // 1:cpuacct,cpu:/ |
| 343 | // We want the third field from the line whose second field contains the "cpu" token. |
| 344 | std::string cgroup_file; |
| 345 | if (!ReadFileToString("/proc/self/cgroup", &cgroup_file)) { |
| 346 | return ""; |
| 347 | } |
| 348 | std::vector<std::string> cgroup_lines; |
| 349 | Split(cgroup_file, '\n', cgroup_lines); |
| 350 | for (size_t i = 0; i < cgroup_lines.size(); ++i) { |
| 351 | std::vector<std::string> cgroup_fields; |
| 352 | Split(cgroup_lines[i], ':', cgroup_fields); |
| 353 | std::vector<std::string> cgroups; |
| 354 | Split(cgroup_fields[1], ',', cgroups); |
| 355 | for (size_t i = 0; i < cgroups.size(); ++i) { |
| 356 | if (cgroups[i] == "cpu") { |
| 357 | return cgroup_fields[2].substr(1); // Skip the leading slash. |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | return ""; |
| 362 | } |
| 363 | |
| 364 | void Thread::DumpState(std::ostream& os) const { |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 365 | std::string thread_name("<native thread without managed peer>"); |
| 366 | std::string group_name; |
| 367 | int priority; |
| 368 | bool is_daemon = false; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 369 | |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 370 | if (peer_ != NULL) { |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 371 | String* thread_name_string = reinterpret_cast<String*>(gThread_name->GetObject(peer_)); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 372 | thread_name = (thread_name_string != NULL) ? thread_name_string->ToModifiedUtf8() : "<null>"; |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 373 | priority = gThread_priority->GetInt(peer_); |
| 374 | is_daemon = gThread_daemon->GetBoolean(peer_); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 375 | |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 376 | Object* thread_group = gThread_group->GetObject(peer_); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 377 | if (thread_group != NULL) { |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 378 | String* group_name_string = reinterpret_cast<String*>(gThreadGroup_name->GetObject(thread_group)); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 379 | group_name = (group_name_string != NULL) ? group_name_string->ToModifiedUtf8() : "<null>"; |
| 380 | } |
| 381 | } else { |
| 382 | // This name may be truncated, but it's the best we can do in the absence of a managed peer. |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 383 | std::string stats; |
| 384 | if (ReadFileToString(StringPrintf("/proc/self/task/%d/stat", GetTid()).c_str(), &stats)) { |
| 385 | size_t start = stats.find('(') + 1; |
| 386 | size_t end = stats.find(')') - start; |
| 387 | thread_name = stats.substr(start, end); |
| 388 | } |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 389 | priority = GetNativePriority(); |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 390 | } |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 391 | |
| 392 | int policy; |
| 393 | sched_param sp; |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 394 | CHECK_PTHREAD_CALL(pthread_getschedparam, (pthread_, &policy, &sp), __FUNCTION__); |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 395 | |
| 396 | std::string scheduler_group(GetSchedulerGroup(GetTid())); |
| 397 | if (scheduler_group.empty()) { |
| 398 | scheduler_group = "default"; |
| 399 | } |
| 400 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 401 | os << '"' << thread_name << '"'; |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 402 | if (is_daemon) { |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 403 | os << " daemon"; |
| 404 | } |
| 405 | os << " prio=" << priority |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 406 | << " tid=" << GetThinLockId() |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 407 | << " " << GetState() << "\n"; |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 408 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 409 | int debug_suspend_count = 0; // TODO |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 410 | os << " | group=\"" << group_name << "\"" |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 411 | << " sCount=" << suspend_count_ |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 412 | << " dsCount=" << debug_suspend_count |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 413 | << " obj=" << reinterpret_cast<void*>(peer_) |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 414 | << " self=" << reinterpret_cast<const void*>(this) << "\n"; |
| 415 | os << " | sysTid=" << GetTid() |
| 416 | << " nice=" << getpriority(PRIO_PROCESS, GetTid()) |
| 417 | << " sched=" << policy << "/" << sp.sched_priority |
| 418 | << " cgrp=" << scheduler_group |
| 419 | << " handle=" << GetImpl() << "\n"; |
| 420 | |
| 421 | // Grab the scheduler stats for this thread. |
| 422 | std::string scheduler_stats; |
| 423 | if (ReadFileToString(StringPrintf("/proc/self/task/%d/schedstat", GetTid()).c_str(), &scheduler_stats)) { |
| 424 | scheduler_stats.resize(scheduler_stats.size() - 1); // Lose the trailing '\n'. |
| 425 | } else { |
| 426 | scheduler_stats = "0 0 0"; |
| 427 | } |
| 428 | |
| 429 | int utime = 0; |
| 430 | int stime = 0; |
| 431 | int task_cpu = 0; |
| 432 | std::string stats; |
| 433 | if (ReadFileToString(StringPrintf("/proc/self/task/%d/stat", GetTid()).c_str(), &stats)) { |
| 434 | // Skip the command, which may contain spaces. |
| 435 | stats = stats.substr(stats.find(')') + 2); |
| 436 | // Extract the three fields we care about. |
| 437 | std::vector<std::string> fields; |
| 438 | Split(stats, ' ', fields); |
| 439 | utime = strtoull(fields[11].c_str(), NULL, 10); |
| 440 | stime = strtoull(fields[12].c_str(), NULL, 10); |
| 441 | task_cpu = strtoull(fields[36].c_str(), NULL, 10); |
| 442 | } |
| 443 | |
| 444 | os << " | schedstat=( " << scheduler_stats << " )" |
| 445 | << " utm=" << utime |
| 446 | << " stm=" << stime |
| 447 | << " core=" << task_cpu |
| 448 | << " HZ=" << sysconf(_SC_CLK_TCK) << "\n"; |
| 449 | } |
| 450 | |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 451 | struct StackDumpVisitor : public Thread::StackVisitor { |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 452 | StackDumpVisitor(std::ostream& os, const Thread* thread) |
| 453 | : os(os), thread(thread), frame_count(0) { |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 454 | } |
| 455 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 456 | virtual ~StackDumpVisitor() { |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 457 | } |
| 458 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 459 | void VisitFrame(const Frame& frame, uintptr_t pc) { |
Ian Rogers | 9086572 | 2011-09-19 11:11:44 -0700 | [diff] [blame] | 460 | if (!frame.HasMethod()) { |
| 461 | return; |
| 462 | } |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 463 | |
| 464 | Method* m = frame.GetMethod(); |
| 465 | Class* c = m->GetDeclaringClass(); |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 466 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 467 | const DexFile& dex_file = class_linker->FindDexFile(c->GetDexCache()); |
| 468 | |
| 469 | os << " at " << PrettyMethod(m, false); |
| 470 | if (m->IsNative()) { |
| 471 | os << "(Native method)"; |
| 472 | } else { |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 473 | int line_number = dex_file.GetLineNumFromPC(m, m->ToDexPC(pc)); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 474 | os << "(" << c->GetSourceFile()->ToModifiedUtf8() << ":" << line_number << ")"; |
| 475 | } |
| 476 | os << "\n"; |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 477 | |
| 478 | if (frame_count++ == 0) { |
| 479 | Monitor::DescribeWait(os, thread); |
| 480 | } |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | std::ostream& os; |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 484 | const Thread* thread; |
| 485 | int frame_count; |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 486 | }; |
| 487 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 488 | void Thread::DumpStack(std::ostream& os) const { |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 489 | StackDumpVisitor dumper(os, this); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 490 | WalkStack(&dumper); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 491 | } |
| 492 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 493 | Thread::State Thread::SetState(Thread::State new_state) { |
| 494 | Thread::State old_state = state_; |
| 495 | if (old_state == new_state) { |
| 496 | return old_state; |
| 497 | } |
| 498 | |
| 499 | volatile void* raw = reinterpret_cast<volatile void*>(&state_); |
| 500 | volatile int32_t* addr = reinterpret_cast<volatile int32_t*>(raw); |
| 501 | |
| 502 | if (new_state == Thread::kRunnable) { |
| 503 | /* |
| 504 | * Change our status to Thread::kRunnable. The transition requires |
| 505 | * that we check for pending suspension, because the VM considers |
| 506 | * us to be "asleep" in all other states, and another thread could |
| 507 | * be performing a GC now. |
| 508 | * |
| 509 | * The order of operations is very significant here. One way to |
| 510 | * do this wrong is: |
| 511 | * |
| 512 | * GCing thread Our thread (in kNative) |
| 513 | * ------------ ---------------------- |
| 514 | * check suspend count (== 0) |
| 515 | * SuspendAllThreads() |
| 516 | * grab suspend-count lock |
| 517 | * increment all suspend counts |
| 518 | * release suspend-count lock |
| 519 | * check thread state (== kNative) |
| 520 | * all are suspended, begin GC |
| 521 | * set state to kRunnable |
| 522 | * (continue executing) |
| 523 | * |
| 524 | * We can correct this by grabbing the suspend-count lock and |
| 525 | * performing both of our operations (check suspend count, set |
| 526 | * state) while holding it, now we need to grab a mutex on every |
| 527 | * transition to kRunnable. |
| 528 | * |
| 529 | * What we do instead is change the order of operations so that |
| 530 | * the transition to kRunnable happens first. If we then detect |
| 531 | * that the suspend count is nonzero, we switch to kSuspended. |
| 532 | * |
| 533 | * Appropriate compiler and memory barriers are required to ensure |
| 534 | * that the operations are observed in the expected order. |
| 535 | * |
| 536 | * This does create a small window of opportunity where a GC in |
| 537 | * progress could observe what appears to be a running thread (if |
| 538 | * it happens to look between when we set to kRunnable and when we |
| 539 | * switch to kSuspended). At worst this only affects assertions |
| 540 | * and thread logging. (We could work around it with some sort |
| 541 | * of intermediate "pre-running" state that is generally treated |
| 542 | * as equivalent to running, but that doesn't seem worthwhile.) |
| 543 | * |
| 544 | * We can also solve this by combining the "status" and "suspend |
| 545 | * count" fields into a single 32-bit value. This trades the |
| 546 | * store/load barrier on transition to kRunnable for an atomic RMW |
| 547 | * op on all transitions and all suspend count updates (also, all |
| 548 | * accesses to status or the thread count require bit-fiddling). |
| 549 | * It also eliminates the brief transition through kRunnable when |
| 550 | * the thread is supposed to be suspended. This is possibly faster |
| 551 | * on SMP and slightly more correct, but less convenient. |
| 552 | */ |
| 553 | android_atomic_acquire_store(new_state, addr); |
| 554 | if (ANNOTATE_UNPROTECTED_READ(suspend_count_) != 0) { |
| 555 | Runtime::Current()->GetThreadList()->FullSuspendCheck(this); |
| 556 | } |
| 557 | } else { |
| 558 | /* |
| 559 | * Not changing to Thread::kRunnable. No additional work required. |
| 560 | * |
| 561 | * We use a releasing store to ensure that, if we were runnable, |
| 562 | * any updates we previously made to objects on the managed heap |
| 563 | * will be observed before the state change. |
| 564 | */ |
| 565 | android_atomic_release_store(new_state, addr); |
| 566 | } |
| 567 | |
| 568 | return old_state; |
| 569 | } |
| 570 | |
| 571 | void Thread::WaitUntilSuspended() { |
| 572 | // TODO: dalvik dropped the waiting thread's priority after a while. |
| 573 | // TODO: dalvik timed out and aborted. |
| 574 | useconds_t delay = 0; |
| 575 | while (GetState() == Thread::kRunnable) { |
| 576 | useconds_t new_delay = delay * 2; |
| 577 | CHECK_GE(new_delay, delay); |
| 578 | delay = new_delay; |
| 579 | if (delay == 0) { |
| 580 | sched_yield(); |
| 581 | delay = 10000; |
| 582 | } else { |
| 583 | usleep(delay); |
| 584 | } |
| 585 | } |
| 586 | } |
| 587 | |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 588 | void Thread::ThreadExitCallback(void* arg) { |
| 589 | Thread* self = reinterpret_cast<Thread*>(arg); |
| 590 | LOG(FATAL) << "Native thread exited without calling DetachCurrentThread: " << *self; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 593 | void Thread::Startup() { |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 594 | // Allocate a TLS slot. |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 595 | CHECK_PTHREAD_CALL(pthread_key_create, (&Thread::pthread_key_self_, Thread::ThreadExitCallback), "self key"); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 596 | |
| 597 | // Double-check the TLS slot allocation. |
| 598 | if (pthread_getspecific(pthread_key_self_) != NULL) { |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 599 | LOG(FATAL) << "newly-created pthread TLS slot is not NULL"; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 600 | } |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 601 | } |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 602 | |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 603 | // TODO: make more accessible? |
| 604 | Class* FindPrimitiveClassOrDie(ClassLinker* class_linker, char descriptor) { |
| 605 | Class* c = class_linker->FindPrimitiveClass(descriptor); |
| 606 | CHECK(c != NULL) << descriptor; |
| 607 | return c; |
| 608 | } |
| 609 | |
| 610 | // TODO: make more accessible? |
| 611 | Class* FindClassOrDie(ClassLinker* class_linker, const char* descriptor) { |
| 612 | Class* c = class_linker->FindSystemClass(descriptor); |
| 613 | CHECK(c != NULL) << descriptor; |
| 614 | return c; |
| 615 | } |
| 616 | |
| 617 | // TODO: make more accessible? |
| 618 | Field* FindFieldOrDie(Class* c, const char* name, Class* type) { |
| 619 | Field* f = c->FindDeclaredInstanceField(name, type); |
| 620 | CHECK(f != NULL) << PrettyClass(c) << " " << name << " " << PrettyClass(type); |
| 621 | return f; |
| 622 | } |
| 623 | |
| 624 | // TODO: make more accessible? |
| 625 | Method* FindMethodOrDie(Class* c, const char* name, const char* signature) { |
| 626 | Method* m = c->FindVirtualMethod(name, signature); |
| 627 | CHECK(m != NULL) << PrettyClass(c) << " " << name << " " << signature; |
| 628 | return m; |
| 629 | } |
| 630 | |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 631 | void Thread::FinishStartup() { |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 632 | // Now the ClassLinker is ready, we can find the various Class*, Field*, and Method*s we need. |
| 633 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 634 | |
| 635 | Class* boolean_class = FindPrimitiveClassOrDie(class_linker, 'Z'); |
| 636 | Class* int_class = FindPrimitiveClassOrDie(class_linker, 'I'); |
| 637 | Class* String_class = FindClassOrDie(class_linker, "Ljava/lang/String;"); |
| 638 | Class* Thread_class = FindClassOrDie(class_linker, "Ljava/lang/Thread;"); |
| 639 | Class* ThreadGroup_class = FindClassOrDie(class_linker, "Ljava/lang/ThreadGroup;"); |
| 640 | Class* UncaughtExceptionHandler_class = FindClassOrDie(class_linker, "Ljava/lang/Thread$UncaughtExceptionHandler;"); |
| 641 | gThreadLock = FindClassOrDie(class_linker, "Ljava/lang/ThreadLock;"); |
| 642 | gThrowable = FindClassOrDie(class_linker, "Ljava/lang/Throwable;"); |
| 643 | |
| 644 | gThread_daemon = FindFieldOrDie(Thread_class, "daemon", boolean_class); |
| 645 | gThread_group = FindFieldOrDie(Thread_class, "group", ThreadGroup_class); |
| 646 | gThread_lock = FindFieldOrDie(Thread_class, "lock", gThreadLock); |
| 647 | gThread_name = FindFieldOrDie(Thread_class, "name", String_class); |
| 648 | gThread_priority = FindFieldOrDie(Thread_class, "priority", int_class); |
| 649 | gThread_uncaughtHandler = FindFieldOrDie(Thread_class, "uncaughtHandler", UncaughtExceptionHandler_class); |
| 650 | gThread_vmData = FindFieldOrDie(Thread_class, "vmData", int_class); |
| 651 | gThreadGroup_name = FindFieldOrDie(ThreadGroup_class, "name", String_class); |
| 652 | gThreadLock_thread = FindFieldOrDie(gThreadLock, "thread", Thread_class); |
| 653 | |
| 654 | gThread_run = FindMethodOrDie(Thread_class, "run", "()V"); |
| 655 | gThreadGroup_removeThread = FindMethodOrDie(ThreadGroup_class, "removeThread", "(Ljava/lang/Thread;)V"); |
| 656 | gUncaughtExceptionHandler_uncaughtException = FindMethodOrDie(UncaughtExceptionHandler_class, |
| 657 | "uncaughtException", "(Ljava/lang/Thread;Ljava/lang/Throwable;)V"); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 658 | |
| 659 | // Finish attaching the main thread. |
| 660 | Thread::Current()->CreatePeer("main", false); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 661 | } |
| 662 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 663 | void Thread::Shutdown() { |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 664 | CHECK_PTHREAD_CALL(pthread_key_delete, (Thread::pthread_key_self_), "self key"); |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 665 | } |
| 666 | |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 667 | uint32_t Thread::LockOwnerFromThreadLock(Object* thread_lock) { |
| 668 | if (thread_lock == NULL || thread_lock->GetClass() != gThreadLock) { |
| 669 | return ThreadList::kInvalidId; |
| 670 | } |
| 671 | Object* managed_thread = gThreadLock_thread->GetObject(thread_lock); |
| 672 | if (managed_thread == NULL) { |
| 673 | return ThreadList::kInvalidId; |
| 674 | } |
| 675 | uintptr_t vmData = static_cast<uintptr_t>(gThread_vmData->GetInt(managed_thread)); |
| 676 | Thread* thread = reinterpret_cast<Thread*>(vmData); |
| 677 | if (thread == NULL) { |
| 678 | return ThreadList::kInvalidId; |
| 679 | } |
| 680 | return thread->GetThinLockId(); |
| 681 | } |
| 682 | |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 683 | Thread::Thread() |
Elliott Hughes | 02b48d1 | 2011-09-07 17:15:51 -0700 | [diff] [blame] | 684 | : peer_(NULL), |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 685 | top_of_managed_stack_(), |
| 686 | top_of_managed_stack_pc_(0), |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 687 | wait_mutex_(new Mutex("Thread wait mutex")), |
| 688 | wait_cond_(new ConditionVariable("Thread wait condition variable")), |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 689 | wait_monitor_(NULL), |
| 690 | interrupted_(false), |
Elliott Hughes | dc33ad5 | 2011-09-16 19:46:51 -0700 | [diff] [blame] | 691 | wait_next_(NULL), |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 692 | monitor_enter_object_(NULL), |
Elliott Hughes | dc33ad5 | 2011-09-16 19:46:51 -0700 | [diff] [blame] | 693 | card_table_(0), |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 694 | stack_end_(NULL), |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 695 | native_to_managed_record_(NULL), |
| 696 | top_sirt_(NULL), |
| 697 | jni_env_(NULL), |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 698 | state_(Thread::kNative), |
Elliott Hughes | dc33ad5 | 2011-09-16 19:46:51 -0700 | [diff] [blame] | 699 | self_(NULL), |
| 700 | runtime_(NULL), |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 701 | exception_(NULL), |
| 702 | suspend_count_(0), |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 703 | class_loader_override_(NULL), |
| 704 | long_jump_context_(NULL) { |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 705 | CHECK((sizeof(Thread) % 4) == 0) << sizeof(Thread); |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 706 | } |
| 707 | |
Elliott Hughes | 02b48d1 | 2011-09-07 17:15:51 -0700 | [diff] [blame] | 708 | void MonitorExitVisitor(const Object* object, void*) { |
| 709 | Object* entered_monitor = const_cast<Object*>(object); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 710 | entered_monitor->MonitorExit(Thread::Current()); |
Elliott Hughes | 02b48d1 | 2011-09-07 17:15:51 -0700 | [diff] [blame] | 711 | } |
| 712 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 713 | Thread::~Thread() { |
Elliott Hughes | 7a3aeb4 | 2011-09-25 17:39:47 -0700 | [diff] [blame] | 714 | SetState(Thread::kRunnable); |
| 715 | |
Elliott Hughes | 02b48d1 | 2011-09-07 17:15:51 -0700 | [diff] [blame] | 716 | // On thread detach, all monitors entered with JNI MonitorEnter are automatically exited. |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 717 | if (jni_env_ != NULL) { |
| 718 | jni_env_->monitors.VisitRoots(MonitorExitVisitor, NULL); |
| 719 | } |
Elliott Hughes | 02b48d1 | 2011-09-07 17:15:51 -0700 | [diff] [blame] | 720 | |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 721 | if (peer_ != NULL) { |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 722 | Object* group = gThread_group->GetObject(peer_); |
| 723 | |
| 724 | // Handle any pending exception. |
| 725 | if (IsExceptionPending()) { |
| 726 | // Get and clear the exception. |
| 727 | Object* exception = GetException(); |
| 728 | ClearException(); |
| 729 | |
| 730 | // If the thread has its own handler, use that. |
| 731 | Object* handler = gThread_uncaughtHandler->GetObject(peer_); |
| 732 | if (handler == NULL) { |
| 733 | // Otherwise use the thread group's default handler. |
| 734 | handler = group; |
| 735 | } |
| 736 | |
| 737 | // Call the handler. |
| 738 | Method* m = handler->GetClass()->FindVirtualMethodForVirtualOrInterface(gUncaughtExceptionHandler_uncaughtException); |
| 739 | Object* args[2]; |
| 740 | args[0] = peer_; |
| 741 | args[1] = exception; |
| 742 | m->Invoke(this, handler, reinterpret_cast<byte*>(&args), NULL); |
| 743 | |
| 744 | // If the handler threw, clear that exception too. |
| 745 | ClearException(); |
| 746 | } |
| 747 | |
| 748 | // this.group.removeThread(this); |
Elliott Hughes | 081be7f | 2011-09-18 16:50:26 -0700 | [diff] [blame] | 749 | // group can be null if we're in the compiler or a test. |
| 750 | if (group != NULL) { |
| 751 | Method* m = group->GetClass()->FindVirtualMethodForVirtualOrInterface(gThreadGroup_removeThread); |
| 752 | Object* args = peer_; |
| 753 | m->Invoke(this, group, reinterpret_cast<byte*>(&args), NULL); |
| 754 | } |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 755 | |
| 756 | // this.vmData = 0; |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 757 | SetVmData(peer_, NULL); |
Elliott Hughes | 02b48d1 | 2011-09-07 17:15:51 -0700 | [diff] [blame] | 758 | |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 759 | // TODO: say "bye" to the debugger. |
| 760 | //if (gDvm.debuggerConnected) { |
| 761 | // dvmDbgPostThreadDeath(self); |
| 762 | //} |
Elliott Hughes | 02b48d1 | 2011-09-07 17:15:51 -0700 | [diff] [blame] | 763 | |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 764 | // Thread.join() is implemented as an Object.wait() on the Thread.lock |
| 765 | // object. Signal anyone who is waiting. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 766 | Thread* self = Thread::Current(); |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 767 | Object* lock = gThread_lock->GetObject(peer_); |
| 768 | // (This conditional is only needed for tests, where Thread.lock won't have been set.) |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 769 | if (lock != NULL) { |
| 770 | lock->MonitorEnter(self); |
| 771 | lock->NotifyAll(); |
| 772 | lock->MonitorExit(self); |
| 773 | } |
| 774 | } |
Elliott Hughes | 02b48d1 | 2011-09-07 17:15:51 -0700 | [diff] [blame] | 775 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 776 | delete jni_env_; |
Elliott Hughes | 02b48d1 | 2011-09-07 17:15:51 -0700 | [diff] [blame] | 777 | jni_env_ = NULL; |
| 778 | |
| 779 | SetState(Thread::kTerminated); |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 780 | |
| 781 | delete wait_cond_; |
| 782 | delete wait_mutex_; |
| 783 | |
| 784 | delete long_jump_context_; |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 785 | } |
| 786 | |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 787 | size_t Thread::NumSirtReferences() { |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 788 | size_t count = 0; |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 789 | for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->Link()) { |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 790 | count += cur->NumberOfReferences(); |
| 791 | } |
| 792 | return count; |
| 793 | } |
| 794 | |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 795 | bool Thread::SirtContains(jobject obj) { |
| 796 | Object** sirt_entry = reinterpret_cast<Object**>(obj); |
| 797 | for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->Link()) { |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 798 | size_t num_refs = cur->NumberOfReferences(); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 799 | // A SIRT should always have a jobject/jclass as a native method is passed |
| 800 | // in a this pointer or a class |
| 801 | DCHECK_GT(num_refs, 0u); |
Shih-wei Liao | 2f0ce9d | 2011-09-01 02:07:58 -0700 | [diff] [blame] | 802 | if ((&cur->References()[0] <= sirt_entry) && |
| 803 | (sirt_entry <= (&cur->References()[num_refs - 1]))) { |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 804 | return true; |
| 805 | } |
| 806 | } |
| 807 | return false; |
| 808 | } |
| 809 | |
Shih-wei Liao | 8dfc9d5 | 2011-09-28 18:06:15 -0700 | [diff] [blame] | 810 | void Thread::SirtVisitRoots(Heap::RootVisitor* visitor, void* arg) { |
| 811 | for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->Link()) { |
| 812 | size_t num_refs = cur->NumberOfReferences(); |
| 813 | for (size_t j = 0; j < num_refs; j++) { |
| 814 | visitor(cur->References()[j], arg); |
| 815 | } |
| 816 | } |
| 817 | } |
| 818 | |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 819 | void Thread::PopSirt() { |
| 820 | CHECK(top_sirt_ != NULL); |
| 821 | top_sirt_ = top_sirt_->Link(); |
| 822 | } |
| 823 | |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 824 | Object* Thread::DecodeJObject(jobject obj) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 825 | DCHECK(CanAccessDirectReferences()); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 826 | if (obj == NULL) { |
| 827 | return NULL; |
| 828 | } |
| 829 | IndirectRef ref = reinterpret_cast<IndirectRef>(obj); |
| 830 | IndirectRefKind kind = GetIndirectRefKind(ref); |
| 831 | Object* result; |
| 832 | switch (kind) { |
| 833 | case kLocal: |
| 834 | { |
Elliott Hughes | 69f5bc6 | 2011-08-24 09:26:14 -0700 | [diff] [blame] | 835 | IndirectReferenceTable& locals = jni_env_->locals; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 836 | result = const_cast<Object*>(locals.Get(ref)); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 837 | break; |
| 838 | } |
| 839 | case kGlobal: |
| 840 | { |
| 841 | JavaVMExt* vm = Runtime::Current()->GetJavaVM(); |
| 842 | IndirectReferenceTable& globals = vm->globals; |
| 843 | MutexLock mu(vm->globals_lock); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 844 | result = const_cast<Object*>(globals.Get(ref)); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 845 | break; |
| 846 | } |
| 847 | case kWeakGlobal: |
| 848 | { |
| 849 | JavaVMExt* vm = Runtime::Current()->GetJavaVM(); |
| 850 | IndirectReferenceTable& weak_globals = vm->weak_globals; |
| 851 | MutexLock mu(vm->weak_globals_lock); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 852 | result = const_cast<Object*>(weak_globals.Get(ref)); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 853 | if (result == kClearedJniWeakGlobal) { |
| 854 | // This is a special case where it's okay to return NULL. |
| 855 | return NULL; |
| 856 | } |
| 857 | break; |
| 858 | } |
| 859 | case kSirtOrInvalid: |
| 860 | default: |
| 861 | // TODO: make stack indirect reference table lookup more efficient |
| 862 | // Check if this is a local reference in the SIRT |
| 863 | if (SirtContains(obj)) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 864 | result = *reinterpret_cast<Object**>(obj); // Read from SIRT |
Elliott Hughes | c5bfa8f | 2011-08-30 14:32:49 -0700 | [diff] [blame] | 865 | } else if (jni_env_->work_around_app_jni_bugs) { |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 866 | // Assume an invalid local reference is actually a direct pointer. |
| 867 | result = reinterpret_cast<Object*>(obj); |
| 868 | } else { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 869 | result = kInvalidIndirectRefObject; |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 870 | } |
| 871 | } |
| 872 | |
| 873 | if (result == NULL) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 874 | LOG(ERROR) << "JNI ERROR (app bug): use of deleted " << kind << ": " << obj; |
| 875 | JniAbort(NULL); |
| 876 | } else { |
| 877 | if (result != kInvalidIndirectRefObject) { |
| 878 | Heap::VerifyObject(result); |
| 879 | } |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 880 | } |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 881 | return result; |
| 882 | } |
| 883 | |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 884 | class CountStackDepthVisitor : public Thread::StackVisitor { |
| 885 | public: |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 886 | CountStackDepthVisitor() : depth_(0), skip_depth_(0), skipping_(true) {} |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 887 | |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 888 | virtual void VisitFrame(const Frame& frame, uintptr_t pc) { |
| 889 | // We want to skip frames up to and including the exception's constructor. |
Ian Rogers | 9086572 | 2011-09-19 11:11:44 -0700 | [diff] [blame] | 890 | // Note we also skip the frame if it doesn't have a method (namely the callee |
| 891 | // save frame) |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 892 | DCHECK(gThrowable != NULL); |
Ian Rogers | 9086572 | 2011-09-19 11:11:44 -0700 | [diff] [blame] | 893 | if (skipping_ && frame.HasMethod() && !gThrowable->IsAssignableFrom(frame.GetMethod()->GetDeclaringClass())) { |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 894 | skipping_ = false; |
| 895 | } |
| 896 | if (!skipping_) { |
| 897 | ++depth_; |
| 898 | } else { |
| 899 | ++skip_depth_; |
| 900 | } |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 901 | } |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 902 | |
| 903 | int GetDepth() const { |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 904 | return depth_; |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 905 | } |
| 906 | |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 907 | int GetSkipDepth() const { |
| 908 | return skip_depth_; |
| 909 | } |
| 910 | |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 911 | private: |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 912 | uint32_t depth_; |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 913 | uint32_t skip_depth_; |
| 914 | bool skipping_; |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 915 | }; |
| 916 | |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 917 | class BuildInternalStackTraceVisitor : public Thread::StackVisitor { |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 918 | public: |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 919 | explicit BuildInternalStackTraceVisitor(int depth, int skip_depth, ScopedJniThreadState& ts) |
| 920 | : skip_depth_(skip_depth), count_(0) { |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 921 | // Allocate method trace with an extra slot that will hold the PC trace |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 922 | method_trace_ = Runtime::Current()->GetClassLinker()->AllocObjectArray<Object>(depth + 1); |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 923 | // Register a local reference as IntArray::Alloc may trigger GC |
| 924 | local_ref_ = AddLocalReference<jobject>(ts.Env(), method_trace_); |
| 925 | pc_trace_ = IntArray::Alloc(depth); |
| 926 | #ifdef MOVING_GARBAGE_COLLECTOR |
| 927 | // Re-read after potential GC |
| 928 | method_trace = Decode<ObjectArray<Object>*>(ts.Env(), local_ref_); |
| 929 | #endif |
| 930 | // Save PC trace in last element of method trace, also places it into the |
| 931 | // object graph. |
| 932 | method_trace_->Set(depth, pc_trace_); |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 933 | } |
| 934 | |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 935 | virtual ~BuildInternalStackTraceVisitor() {} |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 936 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 937 | virtual void VisitFrame(const Frame& frame, uintptr_t pc) { |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 938 | if (skip_depth_ > 0) { |
| 939 | skip_depth_--; |
| 940 | return; |
| 941 | } |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 942 | method_trace_->Set(count_, frame.GetMethod()); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 943 | pc_trace_->Set(count_, pc); |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 944 | ++count_; |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 945 | } |
| 946 | |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 947 | jobject GetInternalStackTrace() const { |
| 948 | return local_ref_; |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | private: |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 952 | // How many more frames to skip. |
| 953 | int32_t skip_depth_; |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 954 | // Current position down stack trace |
| 955 | uint32_t count_; |
| 956 | // Array of return PC values |
| 957 | IntArray* pc_trace_; |
| 958 | // An array of the methods on the stack, the last entry is a reference to the |
| 959 | // PC trace |
| 960 | ObjectArray<Object>* method_trace_; |
| 961 | // Local indirect reference table entry for method trace |
| 962 | jobject local_ref_; |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 963 | }; |
| 964 | |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 965 | void Thread::WalkStack(StackVisitor* visitor) const { |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 966 | Frame frame = GetTopOfStack(); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 967 | uintptr_t pc = top_of_managed_stack_pc_; |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 968 | // TODO: enable this CHECK after native_to_managed_record_ is initialized during startup. |
| 969 | // CHECK(native_to_managed_record_ != NULL); |
| 970 | NativeToManagedRecord* record = native_to_managed_record_; |
| 971 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 972 | while (frame.GetSP() != 0) { |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 973 | for ( ; frame.GetMethod() != 0; frame.Next()) { |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 974 | DCHECK(frame.GetMethod()->IsWithinCode(pc)); |
| 975 | visitor->VisitFrame(frame, pc); |
| 976 | pc = frame.GetReturnPC(); |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 977 | } |
| 978 | if (record == NULL) { |
| 979 | break; |
| 980 | } |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 981 | // last_tos should return Frame instead of sp? |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 982 | frame.SetSP(reinterpret_cast<Method**>(record->last_top_of_managed_stack_)); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 983 | pc = record->last_top_of_managed_stack_pc_; |
| 984 | record = record->link_; |
| 985 | } |
| 986 | } |
| 987 | |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 988 | void Thread::WalkStackUntilUpCall(StackVisitor* visitor, bool include_upcall) const { |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 989 | Frame frame = GetTopOfStack(); |
| 990 | uintptr_t pc = top_of_managed_stack_pc_; |
| 991 | |
| 992 | if (frame.GetSP() != 0) { |
| 993 | for ( ; frame.GetMethod() != 0; frame.Next()) { |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 994 | DCHECK(frame.GetMethod()->IsWithinCode(pc)); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 995 | visitor->VisitFrame(frame, pc); |
| 996 | pc = frame.GetReturnPC(); |
| 997 | } |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 998 | if (include_upcall) { |
| 999 | visitor->VisitFrame(frame, pc); |
| 1000 | } |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1001 | } |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 1002 | } |
| 1003 | |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 1004 | jobject Thread::CreateInternalStackTrace(JNIEnv* env) const { |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1005 | // Compute depth of stack |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1006 | CountStackDepthVisitor count_visitor; |
| 1007 | WalkStack(&count_visitor); |
| 1008 | int32_t depth = count_visitor.GetDepth(); |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 1009 | int32_t skip_depth = count_visitor.GetSkipDepth(); |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 1010 | |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1011 | // Transition into runnable state to work on Object*/Array* |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 1012 | ScopedJniThreadState ts(env); |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1013 | |
| 1014 | // Build internal stack trace |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 1015 | BuildInternalStackTraceVisitor build_trace_visitor(depth, skip_depth, ts); |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1016 | WalkStack(&build_trace_visitor); |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 1017 | |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1018 | return build_trace_visitor.GetInternalStackTrace(); |
| 1019 | } |
| 1020 | |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 1021 | jobjectArray Thread::InternalStackTraceToStackTraceElementArray(JNIEnv* env, jobject internal, |
| 1022 | jobjectArray output_array, int* stack_depth) { |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1023 | // Transition into runnable state to work on Object*/Array* |
| 1024 | ScopedJniThreadState ts(env); |
| 1025 | |
| 1026 | // Decode the internal stack trace into the depth, method trace and PC trace |
| 1027 | ObjectArray<Object>* method_trace = |
| 1028 | down_cast<ObjectArray<Object>*>(Decode<Object*>(ts.Env(), internal)); |
| 1029 | int32_t depth = method_trace->GetLength()-1; |
| 1030 | IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth)); |
| 1031 | |
| 1032 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 1033 | |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 1034 | jobjectArray result; |
| 1035 | ObjectArray<StackTraceElement>* java_traces; |
| 1036 | if (output_array != NULL) { |
| 1037 | // Reuse the array we were given. |
| 1038 | result = output_array; |
| 1039 | java_traces = reinterpret_cast<ObjectArray<StackTraceElement>*>(Decode<Array*>(env, |
| 1040 | output_array)); |
| 1041 | // ...adjusting the number of frames we'll write to not exceed the array length. |
| 1042 | depth = std::min(depth, java_traces->GetLength()); |
| 1043 | } else { |
| 1044 | // Create java_trace array and place in local reference table |
| 1045 | java_traces = class_linker->AllocStackTraceElementArray(depth); |
| 1046 | result = AddLocalReference<jobjectArray>(ts.Env(), java_traces); |
| 1047 | } |
| 1048 | |
| 1049 | if (stack_depth != NULL) { |
| 1050 | *stack_depth = depth; |
| 1051 | } |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 1052 | |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1053 | for (int32_t i = 0; i < depth; ++i) { |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1054 | // Prepare parameters for StackTraceElement(String cls, String method, String file, int line) |
| 1055 | Method* method = down_cast<Method*>(method_trace->Get(i)); |
| 1056 | uint32_t native_pc = pc_trace->Get(i); |
| 1057 | Class* klass = method->GetDeclaringClass(); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 1058 | const DexFile& dex_file = class_linker->FindDexFile(klass->GetDexCache()); |
Elliott Hughes | 3893357 | 2011-09-16 12:29:03 -0700 | [diff] [blame] | 1059 | std::string class_name(PrettyDescriptor(klass->GetDescriptor())); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 1060 | |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1061 | // Allocate element, potentially triggering GC |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 1062 | StackTraceElement* obj = |
Elliott Hughes | 3893357 | 2011-09-16 12:29:03 -0700 | [diff] [blame] | 1063 | StackTraceElement::Alloc(String::AllocFromModifiedUtf8(class_name.c_str()), |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 1064 | method->GetName(), |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 1065 | klass->GetSourceFile(), |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 1066 | dex_file.GetLineNumFromPC(method, |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1067 | method->ToDexPC(native_pc))); |
| 1068 | #ifdef MOVING_GARBAGE_COLLECTOR |
| 1069 | // Re-read after potential GC |
| 1070 | java_traces = Decode<ObjectArray<Object>*>(ts.Env(), result); |
| 1071 | method_trace = down_cast<ObjectArray<Object>*>(Decode<Object*>(ts.Env(), internal)); |
| 1072 | pc_trace = down_cast<IntArray*>(method_trace->Get(depth)); |
| 1073 | #endif |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 1074 | java_traces->Set(i, obj); |
| 1075 | } |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1076 | return result; |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 1077 | } |
| 1078 | |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame^] | 1079 | void Thread::ThrowNewExceptionF(const char* exception_class_descriptor, const char* fmt, ...) { |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1080 | va_list args; |
| 1081 | va_start(args, fmt); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 1082 | ThrowNewExceptionV(exception_class_descriptor, fmt, args); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1083 | va_end(args); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | void Thread::ThrowNewExceptionV(const char* exception_class_descriptor, const char* fmt, va_list ap) { |
| 1087 | std::string msg; |
| 1088 | StringAppendV(&msg, fmt, ap); |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame^] | 1089 | ThrowNewException(exception_class_descriptor, msg.c_str()); |
| 1090 | } |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1091 | |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame^] | 1092 | void Thread::ThrowNewException(const char* exception_class_descriptor, const char* msg) { |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 1093 | // Convert "Ljava/lang/Exception;" into JNI-style "java/lang/Exception". |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1094 | CHECK_EQ('L', exception_class_descriptor[0]); |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 1095 | std::string descriptor(exception_class_descriptor + 1); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1096 | CHECK_EQ(';', descriptor[descriptor.length() - 1]); |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 1097 | descriptor.erase(descriptor.length() - 1); |
| 1098 | |
| 1099 | JNIEnv* env = GetJniEnv(); |
| 1100 | jclass exception_class = env->FindClass(descriptor.c_str()); |
| 1101 | CHECK(exception_class != NULL) << "descriptor=\"" << descriptor << "\""; |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame^] | 1102 | int rc = env->ThrowNew(exception_class, msg); |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 1103 | CHECK_EQ(rc, JNI_OK); |
Brian Carlstrom | bc2f3e3 | 2011-09-22 17:16:54 -0700 | [diff] [blame] | 1104 | env->DeleteLocalRef(exception_class); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1105 | } |
| 1106 | |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 1107 | void Thread::ThrowOutOfMemoryError() { |
| 1108 | UNIMPLEMENTED(FATAL); |
| 1109 | } |
| 1110 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1111 | class CatchBlockStackVisitor : public Thread::StackVisitor { |
| 1112 | public: |
| 1113 | CatchBlockStackVisitor(Class* to_find, Context* ljc) |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1114 | : found_(false), to_find_(to_find), long_jump_context_(ljc), native_method_count_(0) { |
| 1115 | #ifndef NDEBUG |
| 1116 | handler_pc_ = 0xEBADC0DE; |
| 1117 | handler_frame_.SetSP(reinterpret_cast<Method**>(0xEBADF00D)); |
| 1118 | #endif |
| 1119 | } |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 1120 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1121 | virtual void VisitFrame(const Frame& fr, uintptr_t pc) { |
| 1122 | if (!found_) { |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1123 | Method* method = fr.GetMethod(); |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1124 | if (method == NULL) { |
| 1125 | // This is the upcall, we remember the frame and last_pc so that we may |
| 1126 | // long jump to them |
| 1127 | handler_pc_ = pc; |
| 1128 | handler_frame_ = fr; |
| 1129 | return; |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1130 | } |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1131 | uint32_t dex_pc = DexFile::kDexNoIndex; |
Ian Rogers | 9086572 | 2011-09-19 11:11:44 -0700 | [diff] [blame] | 1132 | if (method->IsPhony()) { |
| 1133 | // ignore callee save method |
| 1134 | } else if (method->IsNative()) { |
| 1135 | native_method_count_++; |
| 1136 | } else { |
| 1137 | // Move the PC back 2 bytes as a call will frequently terminate the |
| 1138 | // decoding of a particular instruction and we want to make sure we |
| 1139 | // get the Dex PC of the instruction with the call and not the |
| 1140 | // instruction following. |
| 1141 | pc -= 2; |
| 1142 | dex_pc = method->ToDexPC(pc); |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1143 | } |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1144 | if (dex_pc != DexFile::kDexNoIndex) { |
| 1145 | uint32_t found_dex_pc = method->FindCatchBlock(to_find_, dex_pc); |
| 1146 | if (found_dex_pc != DexFile::kDexNoIndex) { |
| 1147 | found_ = true; |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1148 | handler_pc_ = method->ToNativePC(found_dex_pc); |
| 1149 | handler_frame_ = fr; |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1150 | } |
| 1151 | } |
| 1152 | if (!found_) { |
| 1153 | // Caller may be handler, fill in callee saves in context |
| 1154 | long_jump_context_->FillCalleeSaves(fr); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 1155 | } |
| 1156 | } |
| 1157 | } |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1158 | |
| 1159 | // Did we find a catch block yet? |
| 1160 | bool found_; |
| 1161 | // The type of the exception catch block to find |
| 1162 | Class* to_find_; |
| 1163 | // Frame with found handler or last frame if no handler found |
| 1164 | Frame handler_frame_; |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1165 | // PC to branch to for the handler |
| 1166 | uintptr_t handler_pc_; |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1167 | // Context that will be the target of the long jump |
| 1168 | Context* long_jump_context_; |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1169 | // Number of native methods passed in crawl (equates to number of SIRTs to pop) |
| 1170 | uint32_t native_method_count_; |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1171 | }; |
| 1172 | |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1173 | void Thread::DeliverException() { |
| 1174 | Throwable *exception = GetException(); // Set exception on thread |
| 1175 | CHECK(exception != NULL); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1176 | |
| 1177 | Context* long_jump_context = GetLongJumpContext(); |
| 1178 | CatchBlockStackVisitor catch_finder(exception->GetClass(), long_jump_context); |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1179 | WalkStackUntilUpCall(&catch_finder, true); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1180 | |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1181 | // Pop any SIRT |
| 1182 | if (catch_finder.native_method_count_ == 1) { |
| 1183 | PopSirt(); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1184 | } else { |
Ian Rogers | ad42e13 | 2011-09-17 20:23:33 -0700 | [diff] [blame] | 1185 | // We only expect the stack crawl to have passed 1 native method as it's terminated |
| 1186 | // by an up call |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1187 | DCHECK_EQ(catch_finder.native_method_count_, 0u); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1188 | } |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1189 | long_jump_context->SetSP(reinterpret_cast<intptr_t>(catch_finder.handler_frame_.GetSP())); |
| 1190 | long_jump_context->SetPC(catch_finder.handler_pc_); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1191 | long_jump_context->DoLongJump(); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 1192 | } |
| 1193 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1194 | Context* Thread::GetLongJumpContext() { |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 1195 | Context* result = long_jump_context_; |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1196 | if (result == NULL) { |
| 1197 | result = Context::Create(); |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 1198 | long_jump_context_ = result; |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 1199 | } |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1200 | return result; |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 1201 | } |
| 1202 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 1203 | bool Thread::HoldsLock(Object* object) { |
| 1204 | if (object == NULL) { |
| 1205 | return false; |
| 1206 | } |
| 1207 | return object->GetLockOwner() == thin_lock_id_; |
| 1208 | } |
| 1209 | |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 1210 | bool Thread::IsDaemon() { |
| 1211 | return gThread_daemon->GetBoolean(peer_); |
| 1212 | } |
| 1213 | |
Shih-wei Liao | 4f894e3 | 2011-09-27 21:33:19 -0700 | [diff] [blame] | 1214 | // blx is 2-byte in Thumb2. Need to offset PC back to a call site. |
| 1215 | static const int kThumb2InstSize = 2; |
| 1216 | |
Ian Rogers | d6b1f61 | 2011-09-27 13:38:14 -0700 | [diff] [blame] | 1217 | class ReferenceMapVisitor : public Thread::StackVisitor { |
| 1218 | public: |
| 1219 | ReferenceMapVisitor(Context* context, Heap::RootVisitor* root_visitor, void* arg) : |
| 1220 | context_(context), root_visitor_(root_visitor), arg_(arg) { |
| 1221 | } |
| 1222 | |
| 1223 | void VisitFrame(const Frame& frame, uintptr_t pc) { |
| 1224 | Method* m = frame.GetMethod(); |
Ian Rogers | d6b1f61 | 2011-09-27 13:38:14 -0700 | [diff] [blame] | 1225 | |
| 1226 | // Process register map (which native and callee save methods don't have) |
| 1227 | if (!m->IsNative() && !m->IsPhony()) { |
| 1228 | UniquePtr<art::DexVerifier::RegisterMap> map(art::DexVerifier::GetExpandedRegisterMap(m)); |
| 1229 | |
Shih-wei Liao | 4f894e3 | 2011-09-27 21:33:19 -0700 | [diff] [blame] | 1230 | const uint8_t* reg_bitmap = art::DexVerifier::RegisterMapGetLine( |
| 1231 | map.get(), |
| 1232 | m->ToDexPC(pc -kThumb2InstSize)); |
| 1233 | |
| 1234 | LOG(INFO) << "Visiting stack roots in " << PrettyMethod(m, false) |
| 1235 | << "@ PC: " << m->ToDexPC(pc - kThumb2InstSize); |
| 1236 | |
Ian Rogers | d6b1f61 | 2011-09-27 13:38:14 -0700 | [diff] [blame] | 1237 | CHECK(reg_bitmap != NULL); |
| 1238 | ShortArray* vmap = m->GetVMapTable(); |
| 1239 | // For all dex registers |
| 1240 | for (int reg = 0; reg < m->NumRegisters(); ++reg) { |
| 1241 | // Does this register hold a reference? |
| 1242 | if (TestBitmap(reg, reg_bitmap)) { |
| 1243 | // Is the reference in the context or on the stack? |
| 1244 | bool in_context = false; |
| 1245 | int vmap_offset = -1; |
| 1246 | // TODO: take advantage of the registers being ordered |
| 1247 | for (int i = 0; i < vmap->GetLength(); i++) { |
| 1248 | if (vmap->Get(i) == reg) { |
| 1249 | in_context = true; |
| 1250 | vmap_offset = i; |
| 1251 | break; |
| 1252 | } |
| 1253 | } |
| 1254 | Object* ref; |
| 1255 | if (in_context) { |
| 1256 | // Compute the register we need to load from the context |
| 1257 | uint32_t spill_mask = m->GetCoreSpillMask(); |
| 1258 | uint32_t reg = 0; |
| 1259 | for (int i = 0; i < vmap_offset; i++) { |
| 1260 | while ((spill_mask & 1) == 0) { |
| 1261 | CHECK_NE(spill_mask, 0u); |
| 1262 | spill_mask >>= 1; |
| 1263 | reg++; |
| 1264 | } |
| 1265 | } |
| 1266 | ref = reinterpret_cast<Object*>(context_->GetGPR(reg)); |
| 1267 | } else { |
| 1268 | ref = reinterpret_cast<Object*>(frame.GetVReg(m ,reg)); |
| 1269 | } |
Shih-wei Liao | 4f894e3 | 2011-09-27 21:33:19 -0700 | [diff] [blame] | 1270 | if (ref != NULL) { |
| 1271 | root_visitor_(ref, arg_); |
| 1272 | } |
Ian Rogers | d6b1f61 | 2011-09-27 13:38:14 -0700 | [diff] [blame] | 1273 | } |
| 1274 | } |
| 1275 | } |
| 1276 | context_->FillCalleeSaves(frame); |
| 1277 | } |
| 1278 | |
| 1279 | private: |
| 1280 | bool TestBitmap(int reg, const uint8_t* reg_vector) { |
| 1281 | return ((reg_vector[reg / 8] >> (reg % 8)) & 0x01) != 0; |
| 1282 | } |
| 1283 | |
| 1284 | // Context used to build up picture of callee saves |
| 1285 | Context* context_; |
| 1286 | // Call-back when we visit a root |
| 1287 | Heap::RootVisitor* root_visitor_; |
| 1288 | // Argument to call-back |
| 1289 | void* arg_; |
| 1290 | }; |
| 1291 | |
| 1292 | void Thread::VisitRoots(Heap::RootVisitor* visitor, void* arg) { |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 1293 | if (exception_ != NULL) { |
| 1294 | visitor(exception_, arg); |
| 1295 | } |
| 1296 | if (peer_ != NULL) { |
| 1297 | visitor(peer_, arg); |
| 1298 | } |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 1299 | jni_env_->locals.VisitRoots(visitor, arg); |
| 1300 | jni_env_->monitors.VisitRoots(visitor, arg); |
Shih-wei Liao | 8dfc9d5 | 2011-09-28 18:06:15 -0700 | [diff] [blame] | 1301 | |
| 1302 | SirtVisitRoots(visitor, arg); |
| 1303 | |
Ian Rogers | d6b1f61 | 2011-09-27 13:38:14 -0700 | [diff] [blame] | 1304 | // Cheat and steal the long jump context. Assume that we are not doing a GC during exception |
| 1305 | // delivery. |
| 1306 | Context* context = GetLongJumpContext(); |
| 1307 | // Visit roots on this thread's stack |
| 1308 | ReferenceMapVisitor mapper(context, visitor, arg); |
| 1309 | WalkStack(&mapper); |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 1310 | } |
| 1311 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 1312 | static const char* kStateNames[] = { |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 1313 | "Terminated", |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 1314 | "Runnable", |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 1315 | "TimedWaiting", |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 1316 | "Blocked", |
| 1317 | "Waiting", |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 1318 | "Initializing", |
| 1319 | "Starting", |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 1320 | "Native", |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 1321 | "VmWait", |
| 1322 | "Suspended", |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 1323 | }; |
| 1324 | std::ostream& operator<<(std::ostream& os, const Thread::State& state) { |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 1325 | int32_t int_state = static_cast<int32_t>(state); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 1326 | if (state >= Thread::kTerminated && state <= Thread::kSuspended) { |
| 1327 | os << kStateNames[int_state]; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 1328 | } else { |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 1329 | os << "State[" << int_state << "]"; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 1330 | } |
| 1331 | return os; |
| 1332 | } |
| 1333 | |
Elliott Hughes | 330304d | 2011-08-12 14:28:05 -0700 | [diff] [blame] | 1334 | std::ostream& operator<<(std::ostream& os, const Thread& thread) { |
| 1335 | os << "Thread[" << &thread |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 1336 | << ",pthread_t=" << thread.GetImpl() |
| 1337 | << ",tid=" << thread.GetTid() |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 1338 | << ",id=" << thread.GetThinLockId() |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 1339 | << ",state=" << thread.GetState() |
| 1340 | << ",peer=" << thread.GetPeer() |
| 1341 | << "]"; |
Elliott Hughes | 330304d | 2011-08-12 14:28:05 -0700 | [diff] [blame] | 1342 | return os; |
| 1343 | } |
| 1344 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 1345 | } // namespace art |