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