Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3 | #include "thread.h" |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 4 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 5 | #include <pthread.h> |
| 6 | #include <sys/mman.h> |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 7 | #include <algorithm> |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 8 | #include <cerrno> |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 9 | #include <list> |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 10 | |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 11 | #include "class_linker.h" |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 12 | #include "heap.h" |
Elliott Hughes | c5f7c91 | 2011-08-18 14:00:42 -0700 | [diff] [blame] | 13 | #include "jni_internal.h" |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 14 | #include "object.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 15 | #include "runtime.h" |
| 16 | #include "utils.h" |
buzbee | 5433072 | 2011-08-23 16:46:55 -0700 | [diff] [blame] | 17 | #include "runtime_support.h" |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 18 | |
| 19 | namespace art { |
| 20 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 21 | /* desktop Linux needs a little help with gettid() */ |
| 22 | #if !defined(HAVE_ANDROID_OS) |
| 23 | #define __KERNEL__ |
| 24 | # include <linux/unistd.h> |
| 25 | #ifdef _syscall0 |
| 26 | _syscall0(pid_t, gettid) |
| 27 | #else |
| 28 | pid_t gettid() { return syscall(__NR_gettid);} |
| 29 | #endif |
| 30 | #undef __KERNEL__ |
| 31 | #endif |
| 32 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 33 | pthread_key_t Thread::pthread_key_self_; |
| 34 | |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame^] | 35 | // TODO: placeholder. This is what generated code will call to throw |
| 36 | static void ThrowException(Thread* thread, Throwable* exception) { |
| 37 | /* |
| 38 | * exception may be NULL, in which case this routine should |
| 39 | * throw NPE. NOTE: this is a convenience for generated code, |
| 40 | * which previuosly did the null check inline and constructed |
| 41 | * and threw a NPE if NULL. This routine responsible for setting |
| 42 | * exception_ in thread. |
| 43 | */ |
| 44 | UNIMPLEMENTED(FATAL) << "Unimplemented exception throw"; |
| 45 | } |
| 46 | |
| 47 | // TODO: placeholder. Helper function to type |
| 48 | static Class* InitializeTypeFromCode(uint32_t type_idx, Method* method) { |
| 49 | /* |
| 50 | * Should initialize & fix up method->dex_cache_resolved_types_[]. |
| 51 | * Returns initialized type. Does not return normally if an exception |
| 52 | * is thrown, but instead initiates the catch. Should be similar to |
| 53 | * ClassLinker::InitializeStaticStorageFromCode. |
| 54 | */ |
| 55 | UNIMPLEMENTED(FATAL); |
| 56 | return NULL; |
| 57 | } |
| 58 | |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 59 | void Thread::InitFunctionPointers() { |
buzbee | 5433072 | 2011-08-23 16:46:55 -0700 | [diff] [blame] | 60 | #if defined(__arm__) |
| 61 | pShlLong = art_shl_long; |
| 62 | pShrLong = art_shr_long; |
| 63 | pUshrLong = art_ushr_long; |
buzbee | 7b1b86d | 2011-08-26 18:59:10 -0700 | [diff] [blame] | 64 | pIdiv = __aeabi_idiv; |
| 65 | pIdivmod = __aeabi_idivmod; |
| 66 | pI2f = __aeabi_i2f; |
| 67 | pF2iz = __aeabi_f2iz; |
| 68 | pD2f = __aeabi_d2f; |
| 69 | pF2d = __aeabi_f2d; |
| 70 | pD2iz = __aeabi_d2iz; |
| 71 | pL2f = __aeabi_l2f; |
| 72 | pL2d = __aeabi_l2d; |
| 73 | pFadd = __aeabi_fadd; |
| 74 | pFsub = __aeabi_fsub; |
| 75 | pFdiv = __aeabi_fdiv; |
| 76 | pFmul = __aeabi_fmul; |
| 77 | pFmodf = fmodf; |
| 78 | pDadd = __aeabi_dadd; |
| 79 | pDsub = __aeabi_dsub; |
| 80 | pDdiv = __aeabi_ddiv; |
| 81 | pDmul = __aeabi_dmul; |
| 82 | pFmod = fmod; |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame^] | 83 | pF2l = F2L; |
| 84 | pD2l = D2L; |
buzbee | 7b1b86d | 2011-08-26 18:59:10 -0700 | [diff] [blame] | 85 | pLdivmod = __aeabi_ldivmod; |
buzbee | 439c4fa | 2011-08-27 15:59:07 -0700 | [diff] [blame] | 86 | pLmul = __aeabi_lmul; |
buzbee | 5433072 | 2011-08-23 16:46:55 -0700 | [diff] [blame] | 87 | #endif |
buzbee | dfd3d70 | 2011-08-28 12:56:51 -0700 | [diff] [blame] | 88 | pAllocFromCode = Array::AllocFromCode; |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 89 | pAllocObjectFromCode = Class::AllocObjectFromCode; |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 90 | pMemcpy = memcpy; |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame^] | 91 | pHandleFillArrayDataFromCode = HandleFillArrayDataFromCode; |
buzbee | e193174 | 2011-08-28 21:15:53 -0700 | [diff] [blame] | 92 | pGet32Static = Field::Get32StaticFromCode; |
| 93 | pSet32Static = Field::Set32StaticFromCode; |
| 94 | pGet64Static = Field::Get64StaticFromCode; |
| 95 | pSet64Static = Field::Set64StaticFromCode; |
| 96 | pGetObjStatic = Field::GetObjStaticFromCode; |
| 97 | pSetObjStatic = Field::SetObjStaticFromCode; |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame^] | 98 | pCanPutArrayElementFromCode = Class::CanPutArrayElementFromCode; |
| 99 | pThrowException = ThrowException; |
| 100 | pInitializeTypeFromCode = InitializeTypeFromCode; |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 101 | #if 0 |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame^] | 102 | bool (Thread::*pUnlockObject)(Thread*, Object*); |
| 103 | int (Thread::*pInstanceofNonTrivialFromCode)(const Class*, const Class*); |
| 104 | Method* (Thread::*pFindInterfaceMethodInCache)(Class*, uint32_t, const Method*, DvmDex*); |
| 105 | bool (Thread::*pUnlockObjectFromCode)(Thread*, Object*); |
| 106 | void (Thread::*pLockObjectFromCode)(Thread*, Object*); |
| 107 | Object* (Thread::*pAllocObjectFromCode)(Class*, int); |
| 108 | void (Thread::*pThrowException)(Thread*, Object*); |
| 109 | bool (Thread::*pHandleFillArrayDataFromCode)(Array*, const uint16_t*); |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 110 | #endif |
| 111 | } |
| 112 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 113 | Mutex* Mutex::Create(const char* name) { |
| 114 | Mutex* mu = new Mutex(name); |
| 115 | int result = pthread_mutex_init(&mu->lock_impl_, NULL); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 116 | CHECK_EQ(0, result); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 117 | return mu; |
| 118 | } |
| 119 | |
| 120 | void Mutex::Lock() { |
| 121 | int result = pthread_mutex_lock(&lock_impl_); |
| 122 | CHECK_EQ(result, 0); |
| 123 | SetOwner(Thread::Current()); |
| 124 | } |
| 125 | |
| 126 | bool Mutex::TryLock() { |
| 127 | int result = pthread_mutex_lock(&lock_impl_); |
| 128 | if (result == EBUSY) { |
| 129 | return false; |
| 130 | } else { |
| 131 | CHECK_EQ(result, 0); |
| 132 | SetOwner(Thread::Current()); |
| 133 | return true; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | void Mutex::Unlock() { |
| 138 | CHECK(GetOwner() == Thread::Current()); |
| 139 | int result = pthread_mutex_unlock(&lock_impl_); |
| 140 | CHECK_EQ(result, 0); |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 141 | SetOwner(NULL); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 144 | void Frame::Next() { |
| 145 | byte* next_sp = reinterpret_cast<byte*>(sp_) + |
Shih-wei Liao | d11af15 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 146 | GetMethod()->GetFrameSizeInBytes(); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 147 | sp_ = reinterpret_cast<const Method**>(next_sp); |
| 148 | } |
| 149 | |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 150 | uintptr_t Frame::GetPC() const { |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 151 | byte* pc_addr = reinterpret_cast<byte*>(sp_) + |
Shih-wei Liao | d11af15 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 152 | GetMethod()->GetReturnPcOffsetInBytes(); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 153 | return *reinterpret_cast<uintptr_t*>(pc_addr); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | const Method* Frame::NextMethod() const { |
| 157 | byte* next_sp = reinterpret_cast<byte*>(sp_) + |
Shih-wei Liao | d11af15 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 158 | GetMethod()->GetFrameSizeInBytes(); |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 159 | return *reinterpret_cast<const Method**>(next_sp); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 162 | void* ThreadStart(void *arg) { |
Elliott Hughes | 53b6131 | 2011-08-12 18:28:20 -0700 | [diff] [blame] | 163 | UNIMPLEMENTED(FATAL); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 164 | return NULL; |
| 165 | } |
| 166 | |
Brian Carlstrom | b765be0 | 2011-08-17 23:54:10 -0700 | [diff] [blame] | 167 | Thread* Thread::Create(const Runtime* runtime) { |
| 168 | size_t stack_size = runtime->GetStackSize(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 169 | |
| 170 | Thread* new_thread = new Thread; |
Ian Rogers | 176f59c | 2011-07-20 13:14:11 -0700 | [diff] [blame] | 171 | new_thread->InitCpu(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 172 | |
| 173 | pthread_attr_t attr; |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 174 | errno = pthread_attr_init(&attr); |
| 175 | if (errno != 0) { |
| 176 | PLOG(FATAL) << "pthread_attr_init failed"; |
| 177 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 178 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 179 | errno = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 180 | if (errno != 0) { |
| 181 | PLOG(FATAL) << "pthread_attr_setdetachstate(PTHREAD_CREATE_DETACHED) failed"; |
| 182 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 183 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 184 | errno = pthread_attr_setstacksize(&attr, stack_size); |
| 185 | if (errno != 0) { |
| 186 | PLOG(FATAL) << "pthread_attr_setstacksize(" << stack_size << ") failed"; |
| 187 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 188 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 189 | errno = pthread_create(&new_thread->handle_, &attr, ThreadStart, new_thread); |
| 190 | if (errno != 0) { |
| 191 | PLOG(FATAL) << "pthread_create failed"; |
| 192 | } |
| 193 | |
| 194 | errno = pthread_attr_destroy(&attr); |
| 195 | if (errno != 0) { |
| 196 | PLOG(FATAL) << "pthread_attr_destroy failed"; |
| 197 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 198 | |
| 199 | return new_thread; |
| 200 | } |
| 201 | |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 202 | Thread* Thread::Attach(const Runtime* runtime) { |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 203 | Thread* thread = new Thread; |
Ian Rogers | 176f59c | 2011-07-20 13:14:11 -0700 | [diff] [blame] | 204 | thread->InitCpu(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 205 | |
| 206 | thread->handle_ = pthread_self(); |
| 207 | |
| 208 | thread->state_ = kRunnable; |
| 209 | |
Elliott Hughes | a5780da | 2011-07-17 11:39:39 -0700 | [diff] [blame] | 210 | errno = pthread_setspecific(Thread::pthread_key_self_, thread); |
| 211 | if (errno != 0) { |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 212 | PLOG(FATAL) << "pthread_setspecific failed"; |
Elliott Hughes | a5780da | 2011-07-17 11:39:39 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 215 | thread->jni_env_ = new JNIEnvExt(thread, runtime->GetJavaVM()); |
Elliott Hughes | 330304d | 2011-08-12 14:28:05 -0700 | [diff] [blame] | 216 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 217 | return thread; |
| 218 | } |
| 219 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 220 | pid_t Thread::GetTid() const { |
| 221 | return gettid(); |
| 222 | } |
| 223 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 224 | static void ThreadExitCheck(void* arg) { |
| 225 | LG << "Thread exit check"; |
| 226 | } |
| 227 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 228 | bool Thread::Startup() { |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 229 | // Allocate a TLS slot. |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 230 | errno = pthread_key_create(&Thread::pthread_key_self_, ThreadExitCheck); |
| 231 | if (errno != 0) { |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 232 | PLOG(WARNING) << "pthread_key_create failed"; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 233 | return false; |
| 234 | } |
| 235 | |
| 236 | // Double-check the TLS slot allocation. |
| 237 | if (pthread_getspecific(pthread_key_self_) != NULL) { |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 238 | LOG(WARNING) << "newly-created pthread TLS slot is not NULL"; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 239 | return false; |
| 240 | } |
| 241 | |
| 242 | // TODO: initialize other locks and condition variables |
| 243 | |
| 244 | return true; |
| 245 | } |
| 246 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 247 | void Thread::Shutdown() { |
| 248 | errno = pthread_key_delete(Thread::pthread_key_self_); |
| 249 | if (errno != 0) { |
| 250 | PLOG(WARNING) << "pthread_key_delete failed"; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | Thread::~Thread() { |
| 255 | delete jni_env_; |
| 256 | } |
| 257 | |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 258 | size_t Thread::NumSirtReferences() { |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 259 | size_t count = 0; |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 260 | for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->Link()) { |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 261 | count += cur->NumberOfReferences(); |
| 262 | } |
| 263 | return count; |
| 264 | } |
| 265 | |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 266 | bool Thread::SirtContains(jobject obj) { |
| 267 | Object** sirt_entry = reinterpret_cast<Object**>(obj); |
| 268 | for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->Link()) { |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 269 | size_t num_refs = cur->NumberOfReferences(); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 270 | // A SIRT should always have a jobject/jclass as a native method is passed |
| 271 | // in a this pointer or a class |
| 272 | DCHECK_GT(num_refs, 0u); |
Shih-wei Liao | 2f0ce9d | 2011-09-01 02:07:58 -0700 | [diff] [blame] | 273 | if ((&cur->References()[0] <= sirt_entry) && |
| 274 | (sirt_entry <= (&cur->References()[num_refs - 1]))) { |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 275 | return true; |
| 276 | } |
| 277 | } |
| 278 | return false; |
| 279 | } |
| 280 | |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 281 | Object* Thread::DecodeJObject(jobject obj) { |
| 282 | // TODO: Only allowed to hold Object* when in the runnable state |
| 283 | // DCHECK(state_ == kRunnable); |
| 284 | if (obj == NULL) { |
| 285 | return NULL; |
| 286 | } |
| 287 | IndirectRef ref = reinterpret_cast<IndirectRef>(obj); |
| 288 | IndirectRefKind kind = GetIndirectRefKind(ref); |
| 289 | Object* result; |
| 290 | switch (kind) { |
| 291 | case kLocal: |
| 292 | { |
Elliott Hughes | 69f5bc6 | 2011-08-24 09:26:14 -0700 | [diff] [blame] | 293 | IndirectReferenceTable& locals = jni_env_->locals; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 294 | result = const_cast<Object*>(locals.Get(ref)); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 295 | break; |
| 296 | } |
| 297 | case kGlobal: |
| 298 | { |
| 299 | JavaVMExt* vm = Runtime::Current()->GetJavaVM(); |
| 300 | IndirectReferenceTable& globals = vm->globals; |
| 301 | MutexLock mu(vm->globals_lock); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 302 | result = const_cast<Object*>(globals.Get(ref)); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 303 | break; |
| 304 | } |
| 305 | case kWeakGlobal: |
| 306 | { |
| 307 | JavaVMExt* vm = Runtime::Current()->GetJavaVM(); |
| 308 | IndirectReferenceTable& weak_globals = vm->weak_globals; |
| 309 | MutexLock mu(vm->weak_globals_lock); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 310 | result = const_cast<Object*>(weak_globals.Get(ref)); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 311 | if (result == kClearedJniWeakGlobal) { |
| 312 | // This is a special case where it's okay to return NULL. |
| 313 | return NULL; |
| 314 | } |
| 315 | break; |
| 316 | } |
| 317 | case kSirtOrInvalid: |
| 318 | default: |
| 319 | // TODO: make stack indirect reference table lookup more efficient |
| 320 | // Check if this is a local reference in the SIRT |
| 321 | if (SirtContains(obj)) { |
| 322 | result = *reinterpret_cast<Object**>(obj); // Read from SIRT |
Elliott Hughes | c5bfa8f | 2011-08-30 14:32:49 -0700 | [diff] [blame] | 323 | } else if (jni_env_->work_around_app_jni_bugs) { |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 324 | // Assume an invalid local reference is actually a direct pointer. |
| 325 | result = reinterpret_cast<Object*>(obj); |
| 326 | } else { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 327 | result = kInvalidIndirectRefObject; |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | |
| 331 | if (result == NULL) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 332 | LOG(ERROR) << "JNI ERROR (app bug): use of deleted " << kind << ": " << obj; |
| 333 | JniAbort(NULL); |
| 334 | } else { |
| 335 | if (result != kInvalidIndirectRefObject) { |
| 336 | Heap::VerifyObject(result); |
| 337 | } |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 338 | } |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 339 | return result; |
| 340 | } |
| 341 | |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 342 | class CountStackDepthVisitor : public Thread::StackVisitor { |
| 343 | public: |
| 344 | CountStackDepthVisitor() : depth(0) {} |
| 345 | virtual bool VisitFrame(const Frame&) { |
| 346 | ++depth; |
| 347 | return true; |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 348 | } |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 349 | |
| 350 | int GetDepth() const { |
| 351 | return depth; |
| 352 | } |
| 353 | |
| 354 | private: |
| 355 | uint32_t depth; |
| 356 | }; |
| 357 | |
| 358 | class BuildStackTraceVisitor : public Thread::StackVisitor { |
| 359 | public: |
| 360 | BuildStackTraceVisitor(int depth) : count(0) { |
| 361 | method_trace = Runtime::Current()->GetClassLinker()->AllocObjectArray<const Method>(depth); |
| 362 | pc_trace = IntArray::Alloc(depth); |
| 363 | } |
| 364 | |
| 365 | virtual ~BuildStackTraceVisitor() {} |
| 366 | |
| 367 | virtual bool VisitFrame(const Frame& frame) { |
| 368 | method_trace->Set(count, frame.GetMethod()); |
| 369 | pc_trace->Set(count, frame.GetPC()); |
| 370 | ++count; |
| 371 | return true; |
| 372 | } |
| 373 | |
| 374 | const Method* GetMethod(uint32_t i) { |
| 375 | DCHECK(i < count); |
| 376 | return method_trace->Get(i); |
| 377 | } |
| 378 | |
| 379 | uintptr_t GetPC(uint32_t i) { |
| 380 | DCHECK(i < count); |
| 381 | return pc_trace->Get(i); |
| 382 | } |
| 383 | |
| 384 | private: |
| 385 | uint32_t count; |
| 386 | ObjectArray<const Method>* method_trace; |
| 387 | IntArray* pc_trace; |
| 388 | }; |
| 389 | |
| 390 | void Thread::WalkStack(StackVisitor* visitor) { |
| 391 | Frame frame = Thread::Current()->GetTopOfStack(); |
| 392 | // TODO: enable this CHECK after native_to_managed_record_ is initialized during startup. |
| 393 | // CHECK(native_to_managed_record_ != NULL); |
| 394 | NativeToManagedRecord* record = native_to_managed_record_; |
| 395 | |
| 396 | while (frame.GetSP()) { |
| 397 | for ( ; frame.GetMethod() != 0; frame.Next()) { |
| 398 | visitor->VisitFrame(frame); |
| 399 | } |
| 400 | if (record == NULL) { |
| 401 | break; |
| 402 | } |
| 403 | frame.SetSP(reinterpret_cast<const art::Method**>(record->last_top_of_managed_stack)); // last_tos should return Frame instead of sp? |
| 404 | record = record->link; |
| 405 | } |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 408 | ObjectArray<StackTraceElement>* Thread::AllocStackTrace() { |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 409 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 410 | |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 411 | CountStackDepthVisitor count_visitor; |
| 412 | WalkStack(&count_visitor); |
| 413 | int32_t depth = count_visitor.GetDepth(); |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 414 | |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 415 | BuildStackTraceVisitor build_trace_visitor(depth); |
| 416 | WalkStack(&build_trace_visitor); |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 417 | |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 418 | ObjectArray<StackTraceElement>* java_traces = class_linker->AllocStackTraceElementArray(depth); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 419 | |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 420 | for (int32_t i = 0; i < depth; ++i) { |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 421 | // Prepare parameter for StackTraceElement(String cls, String method, String file, int line) |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 422 | const Method* method = build_trace_visitor.GetMethod(i); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 423 | const Class* klass = method->GetDeclaringClass(); |
| 424 | const DexFile& dex_file = class_linker->FindDexFile(klass->GetDexCache()); |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 425 | String* readable_descriptor = String::AllocFromModifiedUtf8( |
| 426 | PrettyDescriptor(klass->GetDescriptor()).c_str() |
| 427 | ); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 428 | |
| 429 | StackTraceElement* obj = |
| 430 | StackTraceElement::Alloc(readable_descriptor, |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 431 | method->GetName(), |
| 432 | String::AllocFromModifiedUtf8(klass->source_file_), |
| 433 | dex_file.GetLineNumFromPC(method, |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 434 | method->ToDexPC(build_trace_visitor.GetPC(i)))); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 435 | java_traces->Set(i, obj); |
| 436 | } |
| 437 | return java_traces; |
| 438 | } |
| 439 | |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 440 | void Thread::ThrowNewException(const char* exception_class_descriptor, const char* fmt, ...) { |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 441 | std::string msg; |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 442 | va_list args; |
| 443 | va_start(args, fmt); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 444 | StringAppendV(&msg, fmt, args); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 445 | va_end(args); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 446 | |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 447 | // Convert "Ljava/lang/Exception;" into JNI-style "java/lang/Exception". |
| 448 | CHECK(exception_class_descriptor[0] == 'L'); |
| 449 | std::string descriptor(exception_class_descriptor + 1); |
| 450 | CHECK(descriptor[descriptor.length() - 1] == ';'); |
| 451 | descriptor.erase(descriptor.length() - 1); |
| 452 | |
| 453 | JNIEnv* env = GetJniEnv(); |
| 454 | jclass exception_class = env->FindClass(descriptor.c_str()); |
| 455 | CHECK(exception_class != NULL) << "descriptor=\"" << descriptor << "\""; |
| 456 | int rc = env->ThrowNew(exception_class, msg.c_str()); |
| 457 | CHECK_EQ(rc, JNI_OK); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 460 | void Thread::ThrowOutOfMemoryError() { |
| 461 | UNIMPLEMENTED(FATAL); |
| 462 | } |
| 463 | |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 464 | Frame Thread::FindExceptionHandler(void* throw_pc, void** handler_pc) { |
| 465 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 466 | DCHECK(class_linker != NULL); |
| 467 | |
| 468 | Frame cur_frame = GetTopOfStack(); |
| 469 | for (int unwind_depth = 0; ; unwind_depth++) { |
| 470 | const Method* cur_method = cur_frame.GetMethod(); |
| 471 | DexCache* dex_cache = cur_method->GetDeclaringClass()->GetDexCache(); |
| 472 | const DexFile& dex_file = class_linker->FindDexFile(dex_cache); |
| 473 | |
| 474 | void* handler_addr = FindExceptionHandlerInMethod(cur_method, |
| 475 | throw_pc, |
| 476 | dex_file, |
| 477 | class_linker); |
| 478 | if (handler_addr) { |
| 479 | *handler_pc = handler_addr; |
| 480 | return cur_frame; |
| 481 | } else { |
| 482 | // Check if we are at the last frame |
| 483 | if (cur_frame.HasNext()) { |
| 484 | cur_frame.Next(); |
| 485 | } else { |
| 486 | // Either at the top of stack or next frame is native. |
| 487 | break; |
| 488 | } |
| 489 | } |
| 490 | } |
| 491 | *handler_pc = NULL; |
| 492 | return Frame(); |
| 493 | } |
| 494 | |
| 495 | void* Thread::FindExceptionHandlerInMethod(const Method* method, |
| 496 | void* throw_pc, |
| 497 | const DexFile& dex_file, |
| 498 | ClassLinker* class_linker) { |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 499 | Throwable* exception_obj = exception_; |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 500 | exception_ = NULL; |
| 501 | |
| 502 | intptr_t dex_pc = -1; |
| 503 | const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->code_off_); |
| 504 | DexFile::CatchHandlerIterator iter; |
| 505 | for (iter = dex_file.dexFindCatchHandler(*code_item, |
| 506 | method->ToDexPC(reinterpret_cast<intptr_t>(throw_pc))); |
| 507 | !iter.HasNext(); |
| 508 | iter.Next()) { |
| 509 | Class* klass = class_linker->FindSystemClass(dex_file.dexStringByTypeIdx(iter.Get().type_idx_)); |
| 510 | DCHECK(klass != NULL); |
| 511 | if (exception_obj->InstanceOf(klass)) { |
| 512 | dex_pc = iter.Get().address_; |
| 513 | break; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | exception_ = exception_obj; |
| 518 | if (iter.HasNext()) { |
| 519 | return NULL; |
| 520 | } else { |
| 521 | return reinterpret_cast<void*>( method->ToNativePC(dex_pc) ); |
| 522 | } |
| 523 | } |
| 524 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 525 | static const char* kStateNames[] = { |
| 526 | "New", |
| 527 | "Runnable", |
| 528 | "Blocked", |
| 529 | "Waiting", |
| 530 | "TimedWaiting", |
| 531 | "Native", |
| 532 | "Terminated", |
| 533 | }; |
| 534 | std::ostream& operator<<(std::ostream& os, const Thread::State& state) { |
| 535 | if (state >= Thread::kNew && state <= Thread::kTerminated) { |
| 536 | os << kStateNames[state-Thread::kNew]; |
| 537 | } else { |
| 538 | os << "State[" << static_cast<int>(state) << "]"; |
| 539 | } |
| 540 | return os; |
| 541 | } |
| 542 | |
Elliott Hughes | 330304d | 2011-08-12 14:28:05 -0700 | [diff] [blame] | 543 | std::ostream& operator<<(std::ostream& os, const Thread& thread) { |
| 544 | os << "Thread[" << &thread |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 545 | << ",pthread_t=" << thread.GetImpl() |
| 546 | << ",tid=" << thread.GetTid() |
| 547 | << ",id=" << thread.GetId() |
| 548 | << ",state=" << thread.GetState() << "]"; |
Elliott Hughes | 330304d | 2011-08-12 14:28:05 -0700 | [diff] [blame] | 549 | return os; |
| 550 | } |
| 551 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 552 | ThreadList* ThreadList::Create() { |
| 553 | return new ThreadList; |
| 554 | } |
| 555 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 556 | ThreadList::ThreadList() { |
| 557 | lock_ = Mutex::Create("ThreadList::Lock"); |
| 558 | } |
| 559 | |
| 560 | ThreadList::~ThreadList() { |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 561 | if (Contains(Thread::Current())) { |
| 562 | Runtime::Current()->DetachCurrentThread(); |
| 563 | } |
| 564 | |
| 565 | // All threads should have exited and unregistered when we |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 566 | // reach this point. This means that all daemon threads had been |
| 567 | // shutdown cleanly. |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 568 | // TODO: dump ThreadList if non-empty. |
| 569 | CHECK_EQ(list_.size(), 0U); |
| 570 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 571 | delete lock_; |
| 572 | lock_ = NULL; |
| 573 | } |
| 574 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 575 | bool ThreadList::Contains(Thread* thread) { |
| 576 | return find(list_.begin(), list_.end(), thread) != list_.end(); |
| 577 | } |
| 578 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 579 | void ThreadList::Register(Thread* thread) { |
| 580 | MutexLock mu(lock_); |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 581 | CHECK(!Contains(thread)); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 582 | list_.push_front(thread); |
| 583 | } |
| 584 | |
| 585 | void ThreadList::Unregister(Thread* thread) { |
| 586 | MutexLock mu(lock_); |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 587 | CHECK(Contains(thread)); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 588 | list_.remove(thread); |
| 589 | } |
| 590 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 591 | } // namespace |