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 | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame^] | 9 | #include <bitset> |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 10 | #include <cerrno> |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 11 | #include <iostream> |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 12 | #include <list> |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 13 | |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 14 | #include "class_linker.h" |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 15 | #include "heap.h" |
Elliott Hughes | c5f7c91 | 2011-08-18 14:00:42 -0700 | [diff] [blame] | 16 | #include "jni_internal.h" |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 17 | #include "object.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 18 | #include "runtime.h" |
buzbee | 5433072 | 2011-08-23 16:46:55 -0700 | [diff] [blame] | 19 | #include "runtime_support.h" |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 20 | #include "utils.h" |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 21 | |
| 22 | namespace art { |
| 23 | |
| 24 | pthread_key_t Thread::pthread_key_self_; |
| 25 | |
buzbee | 4a3164f | 2011-09-03 11:25:10 -0700 | [diff] [blame] | 26 | // Temporary debugging hook for compiler. |
| 27 | static void DebugMe(Method* method, uint32_t info) { |
| 28 | LOG(INFO) << "DebugMe"; |
| 29 | if (method != NULL) |
| 30 | LOG(INFO) << PrettyMethod(method); |
| 31 | LOG(INFO) << "Info: " << info; |
| 32 | } |
| 33 | |
| 34 | /* |
| 35 | * TODO: placeholder for a method that can be called by the |
| 36 | * invoke-interface trampoline to unwind and handle exception. The |
| 37 | * trampoline will arrange it so that the caller appears to be the |
| 38 | * callsite of the failed invoke-interface. See comments in |
| 39 | * compiler/runtime_support.S |
| 40 | */ |
| 41 | extern "C" void artFailedInvokeInterface() |
| 42 | { |
| 43 | UNIMPLEMENTED(FATAL) << "Unimplemented exception throw"; |
| 44 | } |
| 45 | |
| 46 | // TODO: placeholder. See comments in compiler/runtime_support.S |
| 47 | extern "C" uint64_t artFindInterfaceMethodInCache(uint32_t method_idx, |
| 48 | Object* this_object , Method* caller_method) |
| 49 | { |
| 50 | /* |
| 51 | * Note: this_object has not yet been null-checked. To match |
| 52 | * the old-world state, nullcheck this_object and load |
| 53 | * Class* this_class = this_object->GetClass(). |
| 54 | * See comments and possible thrown exceptions in old-world |
| 55 | * Interp.cpp:dvmInterpFindInterfaceMethod, and complete with |
| 56 | * new-world FindVirtualMethodForInterface. |
| 57 | */ |
| 58 | UNIMPLEMENTED(FATAL) << "Unimplemented invoke interface"; |
| 59 | return 0LL; |
| 60 | } |
| 61 | |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame] | 62 | // TODO: placeholder. This is what generated code will call to throw |
| 63 | static void ThrowException(Thread* thread, Throwable* exception) { |
| 64 | /* |
| 65 | * exception may be NULL, in which case this routine should |
| 66 | * throw NPE. NOTE: this is a convenience for generated code, |
| 67 | * which previuosly did the null check inline and constructed |
| 68 | * and threw a NPE if NULL. This routine responsible for setting |
| 69 | * exception_ in thread. |
| 70 | */ |
| 71 | UNIMPLEMENTED(FATAL) << "Unimplemented exception throw"; |
| 72 | } |
| 73 | |
| 74 | // TODO: placeholder. Helper function to type |
| 75 | static Class* InitializeTypeFromCode(uint32_t type_idx, Method* method) { |
| 76 | /* |
| 77 | * Should initialize & fix up method->dex_cache_resolved_types_[]. |
| 78 | * Returns initialized type. Does not return normally if an exception |
| 79 | * is thrown, but instead initiates the catch. Should be similar to |
| 80 | * ClassLinker::InitializeStaticStorageFromCode. |
| 81 | */ |
| 82 | UNIMPLEMENTED(FATAL); |
| 83 | return NULL; |
| 84 | } |
| 85 | |
buzbee | 561227c | 2011-09-02 15:28:19 -0700 | [diff] [blame] | 86 | // TODO: placeholder. Helper function to resolve virtual method |
| 87 | static void ResolveMethodFromCode(Method* method, uint32_t method_idx) { |
| 88 | /* |
| 89 | * Slow-path handler on invoke virtual method path in which |
| 90 | * base method is unresolved at compile-time. Doesn't need to |
| 91 | * return anything - just either ensure that |
| 92 | * method->dex_cache_resolved_methods_(method_idx) != NULL or |
| 93 | * throw and unwind. The caller will restart call sequence |
| 94 | * from the beginning. |
| 95 | */ |
| 96 | } |
| 97 | |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame] | 98 | // TODO: placeholder. Helper function to alloc array for OP_FILLED_NEW_ARRAY |
| 99 | static Array* CheckAndAllocFromCode(uint32_t type_index, Method* method, |
| 100 | int32_t component_count) |
| 101 | { |
| 102 | /* |
| 103 | * Just a wrapper around Array::AllocFromCode() that additionally |
| 104 | * throws a runtime exception "bad Filled array req" for 'D' and 'J'. |
| 105 | */ |
| 106 | UNIMPLEMENTED(WARNING) << "Need check that not 'D' or 'J'"; |
| 107 | return Array::AllocFromCode(type_index, method, component_count); |
| 108 | } |
| 109 | |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 110 | void Thread::InitFunctionPointers() { |
buzbee | 5433072 | 2011-08-23 16:46:55 -0700 | [diff] [blame] | 111 | #if defined(__arm__) |
| 112 | pShlLong = art_shl_long; |
| 113 | pShrLong = art_shr_long; |
| 114 | pUshrLong = art_ushr_long; |
buzbee | 7b1b86d | 2011-08-26 18:59:10 -0700 | [diff] [blame] | 115 | pIdiv = __aeabi_idiv; |
| 116 | pIdivmod = __aeabi_idivmod; |
| 117 | pI2f = __aeabi_i2f; |
| 118 | pF2iz = __aeabi_f2iz; |
| 119 | pD2f = __aeabi_d2f; |
| 120 | pF2d = __aeabi_f2d; |
| 121 | pD2iz = __aeabi_d2iz; |
| 122 | pL2f = __aeabi_l2f; |
| 123 | pL2d = __aeabi_l2d; |
| 124 | pFadd = __aeabi_fadd; |
| 125 | pFsub = __aeabi_fsub; |
| 126 | pFdiv = __aeabi_fdiv; |
| 127 | pFmul = __aeabi_fmul; |
| 128 | pFmodf = fmodf; |
| 129 | pDadd = __aeabi_dadd; |
| 130 | pDsub = __aeabi_dsub; |
| 131 | pDdiv = __aeabi_ddiv; |
| 132 | pDmul = __aeabi_dmul; |
| 133 | pFmod = fmod; |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame] | 134 | pF2l = F2L; |
| 135 | pD2l = D2L; |
buzbee | 7b1b86d | 2011-08-26 18:59:10 -0700 | [diff] [blame] | 136 | pLdivmod = __aeabi_ldivmod; |
buzbee | 439c4fa | 2011-08-27 15:59:07 -0700 | [diff] [blame] | 137 | pLmul = __aeabi_lmul; |
buzbee | 4a3164f | 2011-09-03 11:25:10 -0700 | [diff] [blame] | 138 | pInvokeInterfaceTrampoline = art_invoke_interface_trampoline; |
buzbee | 5433072 | 2011-08-23 16:46:55 -0700 | [diff] [blame] | 139 | #endif |
buzbee | dfd3d70 | 2011-08-28 12:56:51 -0700 | [diff] [blame] | 140 | pAllocFromCode = Array::AllocFromCode; |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame] | 141 | pCheckAndAllocFromCode = CheckAndAllocFromCode; |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 142 | pAllocObjectFromCode = Class::AllocObjectFromCode; |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 143 | pMemcpy = memcpy; |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame] | 144 | pHandleFillArrayDataFromCode = HandleFillArrayDataFromCode; |
buzbee | e193174 | 2011-08-28 21:15:53 -0700 | [diff] [blame] | 145 | pGet32Static = Field::Get32StaticFromCode; |
| 146 | pSet32Static = Field::Set32StaticFromCode; |
| 147 | pGet64Static = Field::Get64StaticFromCode; |
| 148 | pSet64Static = Field::Set64StaticFromCode; |
| 149 | pGetObjStatic = Field::GetObjStaticFromCode; |
| 150 | pSetObjStatic = Field::SetObjStaticFromCode; |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame] | 151 | pCanPutArrayElementFromCode = Class::CanPutArrayElementFromCode; |
| 152 | pThrowException = ThrowException; |
| 153 | pInitializeTypeFromCode = InitializeTypeFromCode; |
buzbee | 561227c | 2011-09-02 15:28:19 -0700 | [diff] [blame] | 154 | pResolveMethodFromCode = ResolveMethodFromCode; |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame] | 155 | pInitializeStaticStorage = ClassLinker::InitializeStaticStorageFromCode; |
buzbee | 4a3164f | 2011-09-03 11:25:10 -0700 | [diff] [blame] | 156 | pDebugMe = DebugMe; |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 157 | #if 0 |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame] | 158 | bool (Thread::*pUnlockObject)(Thread*, Object*); |
| 159 | int (Thread::*pInstanceofNonTrivialFromCode)(const Class*, const Class*); |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame] | 160 | bool (Thread::*pUnlockObjectFromCode)(Thread*, Object*); |
| 161 | void (Thread::*pLockObjectFromCode)(Thread*, Object*); |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 162 | #endif |
| 163 | } |
| 164 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 165 | Mutex* Mutex::Create(const char* name) { |
| 166 | Mutex* mu = new Mutex(name); |
| 167 | int result = pthread_mutex_init(&mu->lock_impl_, NULL); |
Elliott Hughes | 0f4c41d | 2011-09-04 14:58:03 -0700 | [diff] [blame] | 168 | CHECK_EQ(result, 0); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 169 | return mu; |
| 170 | } |
| 171 | |
| 172 | void Mutex::Lock() { |
| 173 | int result = pthread_mutex_lock(&lock_impl_); |
| 174 | CHECK_EQ(result, 0); |
| 175 | SetOwner(Thread::Current()); |
| 176 | } |
| 177 | |
| 178 | bool Mutex::TryLock() { |
| 179 | int result = pthread_mutex_lock(&lock_impl_); |
| 180 | if (result == EBUSY) { |
| 181 | return false; |
| 182 | } else { |
| 183 | CHECK_EQ(result, 0); |
| 184 | SetOwner(Thread::Current()); |
| 185 | return true; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | void Mutex::Unlock() { |
| 190 | CHECK(GetOwner() == Thread::Current()); |
| 191 | int result = pthread_mutex_unlock(&lock_impl_); |
| 192 | CHECK_EQ(result, 0); |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 193 | SetOwner(NULL); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 196 | void Frame::Next() { |
| 197 | byte* next_sp = reinterpret_cast<byte*>(sp_) + |
Shih-wei Liao | d11af15 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 198 | GetMethod()->GetFrameSizeInBytes(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 199 | sp_ = reinterpret_cast<Method**>(next_sp); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 202 | uintptr_t Frame::GetPC() const { |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 203 | byte* pc_addr = reinterpret_cast<byte*>(sp_) + |
Shih-wei Liao | d11af15 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 204 | GetMethod()->GetReturnPcOffsetInBytes(); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 205 | return *reinterpret_cast<uintptr_t*>(pc_addr); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 208 | Method* Frame::NextMethod() const { |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 209 | byte* next_sp = reinterpret_cast<byte*>(sp_) + |
Shih-wei Liao | d11af15 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 210 | GetMethod()->GetFrameSizeInBytes(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 211 | return *reinterpret_cast<Method**>(next_sp); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 214 | void* ThreadStart(void *arg) { |
Elliott Hughes | 53b6131 | 2011-08-12 18:28:20 -0700 | [diff] [blame] | 215 | UNIMPLEMENTED(FATAL); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 216 | return NULL; |
| 217 | } |
| 218 | |
Brian Carlstrom | b765be0 | 2011-08-17 23:54:10 -0700 | [diff] [blame] | 219 | Thread* Thread::Create(const Runtime* runtime) { |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame^] | 220 | UNIMPLEMENTED(FATAL) << "need to pass in a java.lang.Thread"; |
| 221 | |
Brian Carlstrom | b765be0 | 2011-08-17 23:54:10 -0700 | [diff] [blame] | 222 | size_t stack_size = runtime->GetStackSize(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 223 | |
| 224 | Thread* new_thread = new Thread; |
Ian Rogers | 176f59c | 2011-07-20 13:14:11 -0700 | [diff] [blame] | 225 | new_thread->InitCpu(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 226 | |
| 227 | pthread_attr_t attr; |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 228 | errno = pthread_attr_init(&attr); |
| 229 | if (errno != 0) { |
| 230 | PLOG(FATAL) << "pthread_attr_init failed"; |
| 231 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 232 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 233 | errno = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 234 | if (errno != 0) { |
| 235 | PLOG(FATAL) << "pthread_attr_setdetachstate(PTHREAD_CREATE_DETACHED) failed"; |
| 236 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 237 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 238 | errno = pthread_attr_setstacksize(&attr, stack_size); |
| 239 | if (errno != 0) { |
| 240 | PLOG(FATAL) << "pthread_attr_setstacksize(" << stack_size << ") failed"; |
| 241 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 242 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 243 | errno = pthread_create(&new_thread->handle_, &attr, ThreadStart, new_thread); |
| 244 | if (errno != 0) { |
| 245 | PLOG(FATAL) << "pthread_create failed"; |
| 246 | } |
| 247 | |
| 248 | errno = pthread_attr_destroy(&attr); |
| 249 | if (errno != 0) { |
| 250 | PLOG(FATAL) << "pthread_attr_destroy failed"; |
| 251 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 252 | |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame^] | 253 | // TODO: get the "daemon" field from the java.lang.Thread. |
| 254 | // new_thread->is_daemon_ = dvmGetFieldBoolean(threadObj, gDvm.offJavaLangThread_daemon); |
| 255 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 256 | return new_thread; |
| 257 | } |
| 258 | |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame^] | 259 | static const uint32_t kMaxThreadId = ((1 << 16) - 1); |
| 260 | std::bitset<kMaxThreadId> gAllocatedThreadIds; |
| 261 | |
| 262 | uint32_t AllocThreadId() { |
| 263 | Runtime::Current()->GetThreadList()->Lock(); |
| 264 | for (size_t i = 0; i < gAllocatedThreadIds.size(); ++i) { |
| 265 | if (!gAllocatedThreadIds[i]) { |
| 266 | gAllocatedThreadIds.set(i); |
| 267 | Runtime::Current()->GetThreadList()->Unlock(); |
| 268 | return i + 1; // Zero is reserved to mean "invalid". |
| 269 | } |
| 270 | } |
| 271 | LOG(FATAL) << "Out of internal thread ids"; |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | void ReleaseThreadId(uint32_t id) { |
| 276 | Runtime::Current()->GetThreadList()->Lock(); |
| 277 | CHECK(gAllocatedThreadIds[id]); |
| 278 | gAllocatedThreadIds.reset(id); |
| 279 | Runtime::Current()->GetThreadList()->Unlock(); |
| 280 | } |
| 281 | |
| 282 | Thread* Thread::Attach(const Runtime* runtime, const char* name, bool as_daemon) { |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 283 | Thread* thread = new Thread; |
Ian Rogers | 176f59c | 2011-07-20 13:14:11 -0700 | [diff] [blame] | 284 | thread->InitCpu(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 285 | |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame^] | 286 | thread->thin_lock_id_ = AllocThreadId(); |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 287 | thread->tid_ = ::art::GetTid(); |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame^] | 288 | thread->handle_ = pthread_self(); |
| 289 | thread->is_daemon_ = as_daemon; |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 290 | |
| 291 | thread->state_ = kRunnable; |
| 292 | |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame^] | 293 | SetThreadName(name); |
| 294 | |
Elliott Hughes | a5780da | 2011-07-17 11:39:39 -0700 | [diff] [blame] | 295 | errno = pthread_setspecific(Thread::pthread_key_self_, thread); |
| 296 | if (errno != 0) { |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 297 | PLOG(FATAL) << "pthread_setspecific failed"; |
Elliott Hughes | a5780da | 2011-07-17 11:39:39 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 300 | thread->jni_env_ = new JNIEnvExt(thread, runtime->GetJavaVM()); |
Elliott Hughes | 330304d | 2011-08-12 14:28:05 -0700 | [diff] [blame] | 301 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 302 | return thread; |
| 303 | } |
| 304 | |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 305 | void Thread::Dump(std::ostream& os) const { |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 306 | /* |
| 307 | * Get the java.lang.Thread object. This function gets called from |
| 308 | * some weird debug contexts, so it's possible that there's a GC in |
| 309 | * progress on some other thread. To decrease the chances of the |
| 310 | * thread object being moved out from under us, we add the reference |
| 311 | * to the tracked allocation list, which pins it in place. |
| 312 | * |
| 313 | * If threadObj is NULL, the thread is still in the process of being |
| 314 | * attached to the VM, and there's really nothing interesting to |
| 315 | * say about it yet. |
| 316 | */ |
| 317 | os << "TODO: pin Thread before dumping\n"; |
| 318 | #if 0 |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame^] | 319 | // TODO: dalvikvm had this limitation, but we probably still want to do our best. |
| 320 | if (peer_ == NULL) { |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 321 | LOGI("Can't dump thread %d: threadObj not set", threadId); |
| 322 | return; |
| 323 | } |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame^] | 324 | dvmAddTrackedAlloc(peer_, NULL); |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 325 | #endif |
| 326 | |
| 327 | DumpState(os); |
| 328 | DumpStack(os); |
| 329 | |
| 330 | #if 0 |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame^] | 331 | dvmReleaseTrackedAlloc(peer_, NULL); |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 332 | #endif |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 335 | std::string GetSchedulerGroup(pid_t tid) { |
| 336 | // /proc/<pid>/group looks like this: |
| 337 | // 2:devices:/ |
| 338 | // 1:cpuacct,cpu:/ |
| 339 | // We want the third field from the line whose second field contains the "cpu" token. |
| 340 | std::string cgroup_file; |
| 341 | if (!ReadFileToString("/proc/self/cgroup", &cgroup_file)) { |
| 342 | return ""; |
| 343 | } |
| 344 | std::vector<std::string> cgroup_lines; |
| 345 | Split(cgroup_file, '\n', cgroup_lines); |
| 346 | for (size_t i = 0; i < cgroup_lines.size(); ++i) { |
| 347 | std::vector<std::string> cgroup_fields; |
| 348 | Split(cgroup_lines[i], ':', cgroup_fields); |
| 349 | std::vector<std::string> cgroups; |
| 350 | Split(cgroup_fields[1], ',', cgroups); |
| 351 | for (size_t i = 0; i < cgroups.size(); ++i) { |
| 352 | if (cgroups[i] == "cpu") { |
| 353 | return cgroup_fields[2].substr(1); // Skip the leading slash. |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | return ""; |
| 358 | } |
| 359 | |
| 360 | void Thread::DumpState(std::ostream& os) const { |
| 361 | std::string thread_name("unknown"); |
| 362 | int priority = -1; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame^] | 363 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 364 | #if 0 // TODO |
| 365 | nameStr = (StringObject*) dvmGetFieldObject(threadObj, gDvm.offJavaLangThread_name); |
| 366 | threadName = dvmCreateCstrFromString(nameStr); |
| 367 | priority = dvmGetFieldInt(threadObj, gDvm.offJavaLangThread_priority); |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 368 | #else |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame^] | 369 | { |
| 370 | // TODO: this may be truncated; we should use the java.lang.Thread 'name' field instead. |
| 371 | std::string stats; |
| 372 | if (ReadFileToString(StringPrintf("/proc/self/task/%d/stat", GetTid()).c_str(), &stats)) { |
| 373 | size_t start = stats.find('(') + 1; |
| 374 | size_t end = stats.find(')') - start; |
| 375 | thread_name = stats.substr(start, end); |
| 376 | } |
| 377 | } |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 378 | priority = -1; |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 379 | #endif |
| 380 | |
| 381 | int policy; |
| 382 | sched_param sp; |
| 383 | errno = pthread_getschedparam(handle_, &policy, &sp); |
| 384 | if (errno != 0) { |
| 385 | PLOG(FATAL) << "pthread_getschedparam failed"; |
| 386 | } |
| 387 | |
| 388 | std::string scheduler_group(GetSchedulerGroup(GetTid())); |
| 389 | if (scheduler_group.empty()) { |
| 390 | scheduler_group = "default"; |
| 391 | } |
| 392 | |
| 393 | std::string group_name("(null; initializing?)"); |
| 394 | #if 0 |
| 395 | groupObj = (Object*) dvmGetFieldObject(threadObj, gDvm.offJavaLangThread_group); |
| 396 | if (groupObj != NULL) { |
| 397 | nameStr = (StringObject*) dvmGetFieldObject(groupObj, gDvm.offJavaLangThreadGroup_name); |
| 398 | groupName = dvmCreateCstrFromString(nameStr); |
| 399 | } |
| 400 | #else |
| 401 | group_name = "TODO"; |
| 402 | #endif |
| 403 | |
| 404 | os << '"' << thread_name << '"'; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame^] | 405 | if (is_daemon_) { |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 406 | os << " daemon"; |
| 407 | } |
| 408 | os << " prio=" << priority |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame^] | 409 | << " tid=" << GetThinLockId() |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 410 | << " " << state_ << "\n"; |
| 411 | |
| 412 | int suspend_count = 0; // TODO |
| 413 | int debug_suspend_count = 0; // TODO |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame^] | 414 | void* peer_ = NULL; // TODO |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 415 | os << " | group=\"" << group_name << "\"" |
| 416 | << " sCount=" << suspend_count |
| 417 | << " dsCount=" << debug_suspend_count |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame^] | 418 | << " obj=" << reinterpret_cast<void*>(peer_) |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 419 | << " self=" << reinterpret_cast<const void*>(this) << "\n"; |
| 420 | os << " | sysTid=" << GetTid() |
| 421 | << " nice=" << getpriority(PRIO_PROCESS, GetTid()) |
| 422 | << " sched=" << policy << "/" << sp.sched_priority |
| 423 | << " cgrp=" << scheduler_group |
| 424 | << " handle=" << GetImpl() << "\n"; |
| 425 | |
| 426 | // Grab the scheduler stats for this thread. |
| 427 | std::string scheduler_stats; |
| 428 | if (ReadFileToString(StringPrintf("/proc/self/task/%d/schedstat", GetTid()).c_str(), &scheduler_stats)) { |
| 429 | scheduler_stats.resize(scheduler_stats.size() - 1); // Lose the trailing '\n'. |
| 430 | } else { |
| 431 | scheduler_stats = "0 0 0"; |
| 432 | } |
| 433 | |
| 434 | int utime = 0; |
| 435 | int stime = 0; |
| 436 | int task_cpu = 0; |
| 437 | std::string stats; |
| 438 | if (ReadFileToString(StringPrintf("/proc/self/task/%d/stat", GetTid()).c_str(), &stats)) { |
| 439 | // Skip the command, which may contain spaces. |
| 440 | stats = stats.substr(stats.find(')') + 2); |
| 441 | // Extract the three fields we care about. |
| 442 | std::vector<std::string> fields; |
| 443 | Split(stats, ' ', fields); |
| 444 | utime = strtoull(fields[11].c_str(), NULL, 10); |
| 445 | stime = strtoull(fields[12].c_str(), NULL, 10); |
| 446 | task_cpu = strtoull(fields[36].c_str(), NULL, 10); |
| 447 | } |
| 448 | |
| 449 | os << " | schedstat=( " << scheduler_stats << " )" |
| 450 | << " utm=" << utime |
| 451 | << " stm=" << stime |
| 452 | << " core=" << task_cpu |
| 453 | << " HZ=" << sysconf(_SC_CLK_TCK) << "\n"; |
| 454 | } |
| 455 | |
| 456 | void Thread::DumpStack(std::ostream& os) const { |
| 457 | os << "UNIMPLEMENTED: Thread::DumpStack\n"; |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 460 | static void ThreadExitCheck(void* arg) { |
| 461 | LG << "Thread exit check"; |
| 462 | } |
| 463 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 464 | bool Thread::Startup() { |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 465 | // Allocate a TLS slot. |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 466 | errno = pthread_key_create(&Thread::pthread_key_self_, ThreadExitCheck); |
| 467 | if (errno != 0) { |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 468 | PLOG(WARNING) << "pthread_key_create failed"; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 469 | return false; |
| 470 | } |
| 471 | |
| 472 | // Double-check the TLS slot allocation. |
| 473 | if (pthread_getspecific(pthread_key_self_) != NULL) { |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 474 | LOG(WARNING) << "newly-created pthread TLS slot is not NULL"; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 475 | return false; |
| 476 | } |
| 477 | |
| 478 | // TODO: initialize other locks and condition variables |
| 479 | |
| 480 | return true; |
| 481 | } |
| 482 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 483 | void Thread::Shutdown() { |
| 484 | errno = pthread_key_delete(Thread::pthread_key_self_); |
| 485 | if (errno != 0) { |
| 486 | PLOG(WARNING) << "pthread_key_delete failed"; |
| 487 | } |
| 488 | } |
| 489 | |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame^] | 490 | Thread::Thread() |
| 491 | : thin_lock_id_(0), |
| 492 | peer_(NULL), |
| 493 | top_of_managed_stack_(), |
| 494 | native_to_managed_record_(NULL), |
| 495 | top_sirt_(NULL), |
| 496 | jni_env_(NULL), |
| 497 | exception_(NULL), |
| 498 | suspend_count_(0), |
| 499 | class_loader_override_(NULL) { |
| 500 | InitFunctionPointers(); |
| 501 | } |
| 502 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 503 | Thread::~Thread() { |
| 504 | delete jni_env_; |
| 505 | } |
| 506 | |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 507 | size_t Thread::NumSirtReferences() { |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 508 | size_t count = 0; |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 509 | for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->Link()) { |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 510 | count += cur->NumberOfReferences(); |
| 511 | } |
| 512 | return count; |
| 513 | } |
| 514 | |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 515 | bool Thread::SirtContains(jobject obj) { |
| 516 | Object** sirt_entry = reinterpret_cast<Object**>(obj); |
| 517 | for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->Link()) { |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 518 | size_t num_refs = cur->NumberOfReferences(); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 519 | // A SIRT should always have a jobject/jclass as a native method is passed |
| 520 | // in a this pointer or a class |
| 521 | DCHECK_GT(num_refs, 0u); |
Shih-wei Liao | 2f0ce9d | 2011-09-01 02:07:58 -0700 | [diff] [blame] | 522 | if ((&cur->References()[0] <= sirt_entry) && |
| 523 | (sirt_entry <= (&cur->References()[num_refs - 1]))) { |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 524 | return true; |
| 525 | } |
| 526 | } |
| 527 | return false; |
| 528 | } |
| 529 | |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 530 | Object* Thread::DecodeJObject(jobject obj) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 531 | DCHECK(CanAccessDirectReferences()); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 532 | if (obj == NULL) { |
| 533 | return NULL; |
| 534 | } |
| 535 | IndirectRef ref = reinterpret_cast<IndirectRef>(obj); |
| 536 | IndirectRefKind kind = GetIndirectRefKind(ref); |
| 537 | Object* result; |
| 538 | switch (kind) { |
| 539 | case kLocal: |
| 540 | { |
Elliott Hughes | 69f5bc6 | 2011-08-24 09:26:14 -0700 | [diff] [blame] | 541 | IndirectReferenceTable& locals = jni_env_->locals; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 542 | result = const_cast<Object*>(locals.Get(ref)); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 543 | break; |
| 544 | } |
| 545 | case kGlobal: |
| 546 | { |
| 547 | JavaVMExt* vm = Runtime::Current()->GetJavaVM(); |
| 548 | IndirectReferenceTable& globals = vm->globals; |
| 549 | MutexLock mu(vm->globals_lock); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 550 | result = const_cast<Object*>(globals.Get(ref)); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 551 | break; |
| 552 | } |
| 553 | case kWeakGlobal: |
| 554 | { |
| 555 | JavaVMExt* vm = Runtime::Current()->GetJavaVM(); |
| 556 | IndirectReferenceTable& weak_globals = vm->weak_globals; |
| 557 | MutexLock mu(vm->weak_globals_lock); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 558 | result = const_cast<Object*>(weak_globals.Get(ref)); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 559 | if (result == kClearedJniWeakGlobal) { |
| 560 | // This is a special case where it's okay to return NULL. |
| 561 | return NULL; |
| 562 | } |
| 563 | break; |
| 564 | } |
| 565 | case kSirtOrInvalid: |
| 566 | default: |
| 567 | // TODO: make stack indirect reference table lookup more efficient |
| 568 | // Check if this is a local reference in the SIRT |
| 569 | if (SirtContains(obj)) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 570 | result = *reinterpret_cast<Object**>(obj); // Read from SIRT |
Elliott Hughes | c5bfa8f | 2011-08-30 14:32:49 -0700 | [diff] [blame] | 571 | } else if (jni_env_->work_around_app_jni_bugs) { |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 572 | // Assume an invalid local reference is actually a direct pointer. |
| 573 | result = reinterpret_cast<Object*>(obj); |
| 574 | } else { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 575 | result = kInvalidIndirectRefObject; |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | |
| 579 | if (result == NULL) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 580 | LOG(ERROR) << "JNI ERROR (app bug): use of deleted " << kind << ": " << obj; |
| 581 | JniAbort(NULL); |
| 582 | } else { |
| 583 | if (result != kInvalidIndirectRefObject) { |
| 584 | Heap::VerifyObject(result); |
| 585 | } |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 586 | } |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 587 | return result; |
| 588 | } |
| 589 | |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 590 | class CountStackDepthVisitor : public Thread::StackVisitor { |
| 591 | public: |
| 592 | CountStackDepthVisitor() : depth(0) {} |
| 593 | virtual bool VisitFrame(const Frame&) { |
| 594 | ++depth; |
| 595 | return true; |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 596 | } |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 597 | |
| 598 | int GetDepth() const { |
| 599 | return depth; |
| 600 | } |
| 601 | |
| 602 | private: |
| 603 | uint32_t depth; |
| 604 | }; |
| 605 | |
| 606 | class BuildStackTraceVisitor : public Thread::StackVisitor { |
| 607 | public: |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 608 | explicit BuildStackTraceVisitor(int depth) : count(0) { |
| 609 | method_trace = Runtime::Current()->GetClassLinker()->AllocObjectArray<Method>(depth); |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 610 | pc_trace = IntArray::Alloc(depth); |
| 611 | } |
| 612 | |
| 613 | virtual ~BuildStackTraceVisitor() {} |
| 614 | |
| 615 | virtual bool VisitFrame(const Frame& frame) { |
| 616 | method_trace->Set(count, frame.GetMethod()); |
| 617 | pc_trace->Set(count, frame.GetPC()); |
| 618 | ++count; |
| 619 | return true; |
| 620 | } |
| 621 | |
| 622 | const Method* GetMethod(uint32_t i) { |
| 623 | DCHECK(i < count); |
| 624 | return method_trace->Get(i); |
| 625 | } |
| 626 | |
| 627 | uintptr_t GetPC(uint32_t i) { |
| 628 | DCHECK(i < count); |
| 629 | return pc_trace->Get(i); |
| 630 | } |
| 631 | |
| 632 | private: |
| 633 | uint32_t count; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 634 | ObjectArray<Method>* method_trace; |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 635 | IntArray* pc_trace; |
| 636 | }; |
| 637 | |
| 638 | void Thread::WalkStack(StackVisitor* visitor) { |
| 639 | Frame frame = Thread::Current()->GetTopOfStack(); |
| 640 | // TODO: enable this CHECK after native_to_managed_record_ is initialized during startup. |
| 641 | // CHECK(native_to_managed_record_ != NULL); |
| 642 | NativeToManagedRecord* record = native_to_managed_record_; |
| 643 | |
| 644 | while (frame.GetSP()) { |
| 645 | for ( ; frame.GetMethod() != 0; frame.Next()) { |
| 646 | visitor->VisitFrame(frame); |
| 647 | } |
| 648 | if (record == NULL) { |
| 649 | break; |
| 650 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 651 | 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] | 652 | record = record->link; |
| 653 | } |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 654 | } |
| 655 | |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 656 | ObjectArray<StackTraceElement>* Thread::AllocStackTrace() { |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 657 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 658 | |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 659 | CountStackDepthVisitor count_visitor; |
| 660 | WalkStack(&count_visitor); |
| 661 | int32_t depth = count_visitor.GetDepth(); |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 662 | |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 663 | BuildStackTraceVisitor build_trace_visitor(depth); |
| 664 | WalkStack(&build_trace_visitor); |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 665 | |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 666 | ObjectArray<StackTraceElement>* java_traces = class_linker->AllocStackTraceElementArray(depth); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 667 | |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 668 | for (int32_t i = 0; i < depth; ++i) { |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 669 | // 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] | 670 | const Method* method = build_trace_visitor.GetMethod(i); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 671 | const Class* klass = method->GetDeclaringClass(); |
| 672 | const DexFile& dex_file = class_linker->FindDexFile(klass->GetDexCache()); |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 673 | String* readable_descriptor = String::AllocFromModifiedUtf8( |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 674 | PrettyDescriptor(klass->GetDescriptor()).c_str()); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 675 | |
| 676 | StackTraceElement* obj = |
| 677 | StackTraceElement::Alloc(readable_descriptor, |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 678 | method->GetName(), |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 679 | String::AllocFromModifiedUtf8(klass->GetSourceFile()), |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 680 | dex_file.GetLineNumFromPC(method, |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 681 | method->ToDexPC(build_trace_visitor.GetPC(i)))); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 682 | java_traces->Set(i, obj); |
| 683 | } |
| 684 | return java_traces; |
| 685 | } |
| 686 | |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 687 | void Thread::ThrowNewException(const char* exception_class_descriptor, const char* fmt, ...) { |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 688 | std::string msg; |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 689 | va_list args; |
| 690 | va_start(args, fmt); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 691 | StringAppendV(&msg, fmt, args); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 692 | va_end(args); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 693 | |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 694 | // Convert "Ljava/lang/Exception;" into JNI-style "java/lang/Exception". |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 695 | CHECK_EQ('L', exception_class_descriptor[0]); |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 696 | std::string descriptor(exception_class_descriptor + 1); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 697 | CHECK_EQ(';', descriptor[descriptor.length() - 1]); |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 698 | descriptor.erase(descriptor.length() - 1); |
| 699 | |
| 700 | JNIEnv* env = GetJniEnv(); |
| 701 | jclass exception_class = env->FindClass(descriptor.c_str()); |
| 702 | CHECK(exception_class != NULL) << "descriptor=\"" << descriptor << "\""; |
| 703 | int rc = env->ThrowNew(exception_class, msg.c_str()); |
| 704 | CHECK_EQ(rc, JNI_OK); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 705 | } |
| 706 | |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 707 | void Thread::ThrowOutOfMemoryError() { |
| 708 | UNIMPLEMENTED(FATAL); |
| 709 | } |
| 710 | |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 711 | Frame Thread::FindExceptionHandler(void* throw_pc, void** handler_pc) { |
| 712 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 713 | DCHECK(class_linker != NULL); |
| 714 | |
| 715 | Frame cur_frame = GetTopOfStack(); |
| 716 | for (int unwind_depth = 0; ; unwind_depth++) { |
| 717 | const Method* cur_method = cur_frame.GetMethod(); |
| 718 | DexCache* dex_cache = cur_method->GetDeclaringClass()->GetDexCache(); |
| 719 | const DexFile& dex_file = class_linker->FindDexFile(dex_cache); |
| 720 | |
| 721 | void* handler_addr = FindExceptionHandlerInMethod(cur_method, |
| 722 | throw_pc, |
| 723 | dex_file, |
| 724 | class_linker); |
| 725 | if (handler_addr) { |
| 726 | *handler_pc = handler_addr; |
| 727 | return cur_frame; |
| 728 | } else { |
| 729 | // Check if we are at the last frame |
| 730 | if (cur_frame.HasNext()) { |
| 731 | cur_frame.Next(); |
| 732 | } else { |
| 733 | // Either at the top of stack or next frame is native. |
| 734 | break; |
| 735 | } |
| 736 | } |
| 737 | } |
| 738 | *handler_pc = NULL; |
| 739 | return Frame(); |
| 740 | } |
| 741 | |
| 742 | void* Thread::FindExceptionHandlerInMethod(const Method* method, |
| 743 | void* throw_pc, |
| 744 | const DexFile& dex_file, |
| 745 | ClassLinker* class_linker) { |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 746 | Throwable* exception_obj = exception_; |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 747 | exception_ = NULL; |
| 748 | |
| 749 | intptr_t dex_pc = -1; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 750 | const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset()); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 751 | DexFile::CatchHandlerIterator iter; |
| 752 | for (iter = dex_file.dexFindCatchHandler(*code_item, |
| 753 | method->ToDexPC(reinterpret_cast<intptr_t>(throw_pc))); |
| 754 | !iter.HasNext(); |
| 755 | iter.Next()) { |
| 756 | Class* klass = class_linker->FindSystemClass(dex_file.dexStringByTypeIdx(iter.Get().type_idx_)); |
| 757 | DCHECK(klass != NULL); |
| 758 | if (exception_obj->InstanceOf(klass)) { |
| 759 | dex_pc = iter.Get().address_; |
| 760 | break; |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | exception_ = exception_obj; |
| 765 | if (iter.HasNext()) { |
| 766 | return NULL; |
| 767 | } else { |
| 768 | return reinterpret_cast<void*>( method->ToNativePC(dex_pc) ); |
| 769 | } |
| 770 | } |
| 771 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 772 | void Thread::VisitRoots(Heap::RootVisitor* visitor, void* arg) const { |
| 773 | //(*visitor)(&thread->threadObj, threadId, ROOT_THREAD_OBJECT, arg); |
| 774 | //(*visitor)(&thread->exception, threadId, ROOT_NATIVE_STACK, arg); |
| 775 | jni_env_->locals.VisitRoots(visitor, arg); |
| 776 | jni_env_->monitors.VisitRoots(visitor, arg); |
| 777 | // visitThreadStack(visitor, thread, arg); |
| 778 | UNIMPLEMENTED(WARNING) << "some per-Thread roots not visited"; |
| 779 | } |
| 780 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 781 | static const char* kStateNames[] = { |
| 782 | "New", |
| 783 | "Runnable", |
| 784 | "Blocked", |
| 785 | "Waiting", |
| 786 | "TimedWaiting", |
| 787 | "Native", |
| 788 | "Terminated", |
| 789 | }; |
| 790 | std::ostream& operator<<(std::ostream& os, const Thread::State& state) { |
| 791 | if (state >= Thread::kNew && state <= Thread::kTerminated) { |
| 792 | os << kStateNames[state-Thread::kNew]; |
| 793 | } else { |
| 794 | os << "State[" << static_cast<int>(state) << "]"; |
| 795 | } |
| 796 | return os; |
| 797 | } |
| 798 | |
Elliott Hughes | 330304d | 2011-08-12 14:28:05 -0700 | [diff] [blame] | 799 | std::ostream& operator<<(std::ostream& os, const Thread& thread) { |
| 800 | os << "Thread[" << &thread |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 801 | << ",pthread_t=" << thread.GetImpl() |
| 802 | << ",tid=" << thread.GetTid() |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame^] | 803 | << ",id=" << thread.GetThinLockId() |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 804 | << ",state=" << thread.GetState() << "]"; |
Elliott Hughes | 330304d | 2011-08-12 14:28:05 -0700 | [diff] [blame] | 805 | return os; |
| 806 | } |
| 807 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 808 | ThreadList* ThreadList::Create() { |
| 809 | return new ThreadList; |
| 810 | } |
| 811 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 812 | ThreadList::ThreadList() { |
| 813 | lock_ = Mutex::Create("ThreadList::Lock"); |
| 814 | } |
| 815 | |
| 816 | ThreadList::~ThreadList() { |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 817 | if (Contains(Thread::Current())) { |
| 818 | Runtime::Current()->DetachCurrentThread(); |
| 819 | } |
| 820 | |
| 821 | // All threads should have exited and unregistered when we |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 822 | // reach this point. This means that all daemon threads had been |
| 823 | // shutdown cleanly. |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 824 | // TODO: dump ThreadList if non-empty. |
| 825 | CHECK_EQ(list_.size(), 0U); |
| 826 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 827 | delete lock_; |
| 828 | lock_ = NULL; |
| 829 | } |
| 830 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 831 | bool ThreadList::Contains(Thread* thread) { |
| 832 | return find(list_.begin(), list_.end(), thread) != list_.end(); |
| 833 | } |
| 834 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 835 | void ThreadList::Dump(std::ostream& os) { |
| 836 | MutexLock mu(lock_); |
| 837 | os << "DALVIK THREADS (" << list_.size() << "):\n"; |
| 838 | typedef std::list<Thread*>::const_iterator It; // TODO: C++0x auto |
| 839 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 840 | (*it)->Dump(os); |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame^] | 841 | os << "\n"; |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 842 | } |
| 843 | } |
| 844 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 845 | void ThreadList::Register(Thread* thread) { |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 846 | //LOG(INFO) << "ThreadList::Register() " << *thread; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 847 | MutexLock mu(lock_); |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 848 | CHECK(!Contains(thread)); |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame^] | 849 | list_.push_back(thread); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | void ThreadList::Unregister(Thread* thread) { |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 853 | //LOG(INFO) << "ThreadList::Unregister() " << *thread; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 854 | MutexLock mu(lock_); |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 855 | CHECK(Contains(thread)); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 856 | list_.remove(thread); |
| 857 | } |
| 858 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 859 | void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const { |
| 860 | MutexLock mu(lock_); |
| 861 | typedef std::list<Thread*>::const_iterator It; // TODO: C++0x auto |
| 862 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 863 | (*it)->VisitRoots(visitor, arg); |
| 864 | } |
| 865 | } |
| 866 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 867 | } // namespace |