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