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