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