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" |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 20 | #include "scoped_jni_thread_state.h" |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 21 | #include "thread_list.h" |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 22 | #include "utils.h" |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 23 | |
| 24 | namespace art { |
| 25 | |
| 26 | pthread_key_t Thread::pthread_key_self_; |
| 27 | |
buzbee | 4a3164f | 2011-09-03 11:25:10 -0700 | [diff] [blame] | 28 | // Temporary debugging hook for compiler. |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame^] | 29 | void DebugMe(Method* method, uint32_t info) { |
buzbee | 4a3164f | 2011-09-03 11:25:10 -0700 | [diff] [blame] | 30 | LOG(INFO) << "DebugMe"; |
| 31 | if (method != NULL) |
| 32 | LOG(INFO) << PrettyMethod(method); |
| 33 | LOG(INFO) << "Info: " << info; |
| 34 | } |
| 35 | |
| 36 | /* |
| 37 | * TODO: placeholder for a method that can be called by the |
| 38 | * invoke-interface trampoline to unwind and handle exception. The |
| 39 | * trampoline will arrange it so that the caller appears to be the |
| 40 | * callsite of the failed invoke-interface. See comments in |
| 41 | * compiler/runtime_support.S |
| 42 | */ |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame^] | 43 | extern "C" void artFailedInvokeInterface() { |
buzbee | 4a3164f | 2011-09-03 11:25:10 -0700 | [diff] [blame] | 44 | UNIMPLEMENTED(FATAL) << "Unimplemented exception throw"; |
| 45 | } |
| 46 | |
| 47 | // TODO: placeholder. See comments in compiler/runtime_support.S |
| 48 | extern "C" uint64_t artFindInterfaceMethodInCache(uint32_t method_idx, |
| 49 | Object* this_object , Method* caller_method) |
| 50 | { |
| 51 | /* |
| 52 | * Note: this_object has not yet been null-checked. To match |
| 53 | * the old-world state, nullcheck this_object and load |
| 54 | * Class* this_class = this_object->GetClass(). |
| 55 | * See comments and possible thrown exceptions in old-world |
| 56 | * Interp.cpp:dvmInterpFindInterfaceMethod, and complete with |
| 57 | * new-world FindVirtualMethodForInterface. |
| 58 | */ |
| 59 | UNIMPLEMENTED(FATAL) << "Unimplemented invoke interface"; |
| 60 | return 0LL; |
| 61 | } |
| 62 | |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame] | 63 | // TODO: placeholder. This is what generated code will call to throw |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame^] | 64 | void ThrowException(Thread* thread, Throwable* exception) { |
| 65 | /* |
| 66 | * exception may be NULL, in which case this routine should |
| 67 | * throw NPE. NOTE: this is a convenience for generated code, |
| 68 | * which previously did the null check inline and constructed |
| 69 | * and threw a NPE if NULL. This routine responsible for setting |
| 70 | * exception_ in thread. |
| 71 | */ |
| 72 | UNIMPLEMENTED(FATAL) << "Unimplemented exception throw: " << PrettyType(exception); |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | // TODO: placeholder. Helper function to type |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame^] | 76 | Class* InitializeTypeFromCode(uint32_t type_idx, Method* method) { |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame] | 77 | /* |
| 78 | * Should initialize & fix up method->dex_cache_resolved_types_[]. |
| 79 | * Returns initialized type. Does not return normally if an exception |
| 80 | * is thrown, but instead initiates the catch. Should be similar to |
| 81 | * ClassLinker::InitializeStaticStorageFromCode. |
| 82 | */ |
| 83 | UNIMPLEMENTED(FATAL); |
| 84 | return NULL; |
| 85 | } |
| 86 | |
buzbee | 561227c | 2011-09-02 15:28:19 -0700 | [diff] [blame] | 87 | // TODO: placeholder. Helper function to resolve virtual method |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame^] | 88 | void ResolveMethodFromCode(Method* method, uint32_t method_idx) { |
buzbee | 561227c | 2011-09-02 15:28:19 -0700 | [diff] [blame] | 89 | /* |
| 90 | * Slow-path handler on invoke virtual method path in which |
| 91 | * base method is unresolved at compile-time. Doesn't need to |
| 92 | * return anything - just either ensure that |
| 93 | * method->dex_cache_resolved_methods_(method_idx) != NULL or |
| 94 | * throw and unwind. The caller will restart call sequence |
| 95 | * from the beginning. |
| 96 | */ |
| 97 | } |
| 98 | |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame] | 99 | // TODO: placeholder. Helper function to alloc array for OP_FILLED_NEW_ARRAY |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame^] | 100 | Array* CheckAndAllocFromCode(uint32_t type_index, Method* method, int32_t component_count) { |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame] | 101 | /* |
| 102 | * Just a wrapper around Array::AllocFromCode() that additionally |
| 103 | * throws a runtime exception "bad Filled array req" for 'D' and 'J'. |
| 104 | */ |
| 105 | UNIMPLEMENTED(WARNING) << "Need check that not 'D' or 'J'"; |
| 106 | return Array::AllocFromCode(type_index, method, component_count); |
| 107 | } |
| 108 | |
buzbee | 2a475e7 | 2011-09-07 17:19:17 -0700 | [diff] [blame] | 109 | // TODO: placeholder (throw on failure) |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame^] | 110 | void CheckCastFromCode(const Class* a, const Class* b) { |
buzbee | 2a475e7 | 2011-09-07 17:19:17 -0700 | [diff] [blame] | 111 | if (a->IsAssignableFrom(b)) { |
| 112 | return; |
| 113 | } |
| 114 | UNIMPLEMENTED(FATAL); |
| 115 | } |
| 116 | |
| 117 | // TODO: placeholder |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame^] | 118 | void UnlockObjectFromCode(Thread* thread, Object* obj) { |
buzbee | 2a475e7 | 2011-09-07 17:19:17 -0700 | [diff] [blame] | 119 | // TODO: throw and unwind if lock not held |
| 120 | // TODO: throw and unwind on NPE |
buzbee | 4ef7652 | 2011-09-08 10:00:32 -0700 | [diff] [blame] | 121 | obj->MonitorExit(thread); |
buzbee | 2a475e7 | 2011-09-07 17:19:17 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | // TODO: placeholder |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame^] | 125 | void LockObjectFromCode(Thread* thread, Object* obj) { |
buzbee | 4ef7652 | 2011-09-08 10:00:32 -0700 | [diff] [blame] | 126 | obj->MonitorEnter(thread); |
buzbee | 2a475e7 | 2011-09-07 17:19:17 -0700 | [diff] [blame] | 127 | } |
| 128 | |
buzbee | 0d966cf | 2011-09-08 17:34:58 -0700 | [diff] [blame] | 129 | // TODO: placeholder |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame^] | 130 | void CheckSuspendFromCode(Thread* thread) { |
buzbee | 0d966cf | 2011-09-08 17:34:58 -0700 | [diff] [blame] | 131 | /* |
| 132 | * Code is at a safe point, suspend if needed. |
| 133 | * Also, this is where a pending safepoint callback |
| 134 | * would be fired. |
| 135 | */ |
| 136 | } |
| 137 | |
buzbee | cefd187 | 2011-09-09 09:59:52 -0700 | [diff] [blame] | 138 | // TODO: placeholder |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame^] | 139 | void StackOverflowFromCode(Method* method) { |
| 140 | //NOTE: to save code space, this handler needs to look up its own Thread* |
| 141 | UNIMPLEMENTED(FATAL) << "Stack overflow: " << PrettyMethod(method); |
buzbee | cefd187 | 2011-09-09 09:59:52 -0700 | [diff] [blame] | 142 | } |
| 143 | |
buzbee | 5ade1d2 | 2011-09-09 14:44:52 -0700 | [diff] [blame] | 144 | // TODO: placeholder |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame^] | 145 | void ThrowNullPointerFromCode() { |
| 146 | Thread::Current()->Dump(std::cerr); |
| 147 | //NOTE: to save code space, this handler must look up caller's Method* |
| 148 | UNIMPLEMENTED(FATAL) << "Null pointer exception"; |
buzbee | 5ade1d2 | 2011-09-09 14:44:52 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | // TODO: placeholder |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame^] | 152 | void ThrowDivZeroFromCode() { |
| 153 | UNIMPLEMENTED(FATAL) << "Divide by zero"; |
buzbee | 5ade1d2 | 2011-09-09 14:44:52 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | // TODO: placeholder |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame^] | 157 | void ThrowArrayBoundsFromCode(int32_t index, int32_t limit) { |
| 158 | UNIMPLEMENTED(FATAL) << "Bound check exception, idx: " << index << ", limit: " << limit; |
buzbee | 5ade1d2 | 2011-09-09 14:44:52 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | // TODO: placeholder |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame^] | 162 | void ThrowVerificationErrorFromCode(int32_t src1, int32_t ref) { |
buzbee | 5ade1d2 | 2011-09-09 14:44:52 -0700 | [diff] [blame] | 163 | UNIMPLEMENTED(FATAL) << "Verification error, src1: " << src1 << |
| 164 | " ref: " << ref; |
| 165 | } |
| 166 | |
| 167 | // TODO: placeholder |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame^] | 168 | void ThrowNegArraySizeFromCode(int32_t index) { |
buzbee | 5ade1d2 | 2011-09-09 14:44:52 -0700 | [diff] [blame] | 169 | UNIMPLEMENTED(FATAL) << "Negative array size: " << index; |
| 170 | } |
| 171 | |
| 172 | // TODO: placeholder |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame^] | 173 | void ThrowInternalErrorFromCode(int32_t errnum) { |
buzbee | 5ade1d2 | 2011-09-09 14:44:52 -0700 | [diff] [blame] | 174 | UNIMPLEMENTED(FATAL) << "Internal error: " << errnum; |
| 175 | } |
| 176 | |
| 177 | // TODO: placeholder |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame^] | 178 | void ThrowRuntimeExceptionFromCode(int32_t errnum) { |
buzbee | 5ade1d2 | 2011-09-09 14:44:52 -0700 | [diff] [blame] | 179 | UNIMPLEMENTED(FATAL) << "Internal error: " << errnum; |
| 180 | } |
| 181 | |
| 182 | // TODO: placeholder |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame^] | 183 | void ThrowNoSuchMethodFromCode(int32_t method_idx) { |
buzbee | 5ade1d2 | 2011-09-09 14:44:52 -0700 | [diff] [blame] | 184 | UNIMPLEMENTED(FATAL) << "No such method, idx: " << method_idx; |
| 185 | } |
| 186 | |
| 187 | /* |
| 188 | * Temporary placeholder. Should include run-time checks for size |
| 189 | * of fill data <= size of array. If not, throw arrayOutOfBoundsException. |
| 190 | * As with other new "FromCode" routines, this should return to the caller |
| 191 | * only if no exception has been thrown. |
| 192 | * |
| 193 | * NOTE: When dealing with a raw dex file, the data to be copied uses |
| 194 | * little-endian ordering. Require that oat2dex do any required swapping |
| 195 | * so this routine can get by with a memcpy(). |
| 196 | * |
| 197 | * Format of the data: |
| 198 | * ushort ident = 0x0300 magic value |
| 199 | * ushort width width of each element in the table |
| 200 | * uint size number of elements in the table |
| 201 | * ubyte data[size*width] table of data values (may contain a single-byte |
| 202 | * padding at the end) |
| 203 | */ |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame^] | 204 | void HandleFillArrayDataFromCode(Array* array, const uint16_t* table) { |
buzbee | 5ade1d2 | 2011-09-09 14:44:52 -0700 | [diff] [blame] | 205 | uint32_t size = (uint32_t)table[2] | (((uint32_t)table[3]) << 16); |
| 206 | uint32_t size_in_bytes = size * table[1]; |
| 207 | if (static_cast<int32_t>(size) > array->GetLength()) { |
| 208 | ThrowArrayBoundsFromCode(array->GetLength(), size); |
| 209 | } |
| 210 | memcpy((char*)array + art::Array::DataOffset().Int32Value(), |
| 211 | (char*)&table[4], size_in_bytes); |
| 212 | } |
| 213 | |
| 214 | // TODO: move to more appropriate location |
| 215 | /* |
| 216 | * Float/double conversion requires clamping to min and max of integer form. If |
| 217 | * target doesn't support this normally, use these. |
| 218 | */ |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame^] | 219 | int64_t D2L(double d) { |
buzbee | 5ade1d2 | 2011-09-09 14:44:52 -0700 | [diff] [blame] | 220 | static const double kMaxLong = (double)(int64_t)0x7fffffffffffffffULL; |
| 221 | static const double kMinLong = (double)(int64_t)0x8000000000000000ULL; |
| 222 | if (d >= kMaxLong) |
| 223 | return (int64_t)0x7fffffffffffffffULL; |
| 224 | else if (d <= kMinLong) |
| 225 | return (int64_t)0x8000000000000000ULL; |
| 226 | else if (d != d) // NaN case |
| 227 | return 0; |
| 228 | else |
| 229 | return (int64_t)d; |
| 230 | } |
| 231 | |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame^] | 232 | int64_t F2L(float f) { |
buzbee | 5ade1d2 | 2011-09-09 14:44:52 -0700 | [diff] [blame] | 233 | static const float kMaxLong = (float)(int64_t)0x7fffffffffffffffULL; |
| 234 | static const float kMinLong = (float)(int64_t)0x8000000000000000ULL; |
| 235 | if (f >= kMaxLong) |
| 236 | return (int64_t)0x7fffffffffffffffULL; |
| 237 | else if (f <= kMinLong) |
| 238 | return (int64_t)0x8000000000000000ULL; |
| 239 | else if (f != f) // NaN case |
| 240 | return 0; |
| 241 | else |
| 242 | return (int64_t)f; |
| 243 | } |
| 244 | |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 245 | void Thread::InitFunctionPointers() { |
buzbee | 5433072 | 2011-08-23 16:46:55 -0700 | [diff] [blame] | 246 | #if defined(__arm__) |
| 247 | pShlLong = art_shl_long; |
| 248 | pShrLong = art_shr_long; |
| 249 | pUshrLong = art_ushr_long; |
buzbee | 7b1b86d | 2011-08-26 18:59:10 -0700 | [diff] [blame] | 250 | pIdiv = __aeabi_idiv; |
| 251 | pIdivmod = __aeabi_idivmod; |
| 252 | pI2f = __aeabi_i2f; |
| 253 | pF2iz = __aeabi_f2iz; |
| 254 | pD2f = __aeabi_d2f; |
| 255 | pF2d = __aeabi_f2d; |
| 256 | pD2iz = __aeabi_d2iz; |
| 257 | pL2f = __aeabi_l2f; |
| 258 | pL2d = __aeabi_l2d; |
| 259 | pFadd = __aeabi_fadd; |
| 260 | pFsub = __aeabi_fsub; |
| 261 | pFdiv = __aeabi_fdiv; |
| 262 | pFmul = __aeabi_fmul; |
| 263 | pFmodf = fmodf; |
| 264 | pDadd = __aeabi_dadd; |
| 265 | pDsub = __aeabi_dsub; |
| 266 | pDdiv = __aeabi_ddiv; |
| 267 | pDmul = __aeabi_dmul; |
| 268 | pFmod = fmod; |
buzbee | 7b1b86d | 2011-08-26 18:59:10 -0700 | [diff] [blame] | 269 | pLdivmod = __aeabi_ldivmod; |
buzbee | 439c4fa | 2011-08-27 15:59:07 -0700 | [diff] [blame] | 270 | pLmul = __aeabi_lmul; |
buzbee | 4a3164f | 2011-09-03 11:25:10 -0700 | [diff] [blame] | 271 | pInvokeInterfaceTrampoline = art_invoke_interface_trampoline; |
buzbee | 5433072 | 2011-08-23 16:46:55 -0700 | [diff] [blame] | 272 | #endif |
buzbee | c396efc | 2011-09-11 09:36:41 -0700 | [diff] [blame] | 273 | pF2l = F2L; |
| 274 | pD2l = D2L; |
buzbee | dfd3d70 | 2011-08-28 12:56:51 -0700 | [diff] [blame] | 275 | pAllocFromCode = Array::AllocFromCode; |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame] | 276 | pCheckAndAllocFromCode = CheckAndAllocFromCode; |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 277 | pAllocObjectFromCode = Class::AllocObjectFromCode; |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 278 | pMemcpy = memcpy; |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame] | 279 | pHandleFillArrayDataFromCode = HandleFillArrayDataFromCode; |
buzbee | e193174 | 2011-08-28 21:15:53 -0700 | [diff] [blame] | 280 | pGet32Static = Field::Get32StaticFromCode; |
| 281 | pSet32Static = Field::Set32StaticFromCode; |
| 282 | pGet64Static = Field::Get64StaticFromCode; |
| 283 | pSet64Static = Field::Set64StaticFromCode; |
| 284 | pGetObjStatic = Field::GetObjStaticFromCode; |
| 285 | pSetObjStatic = Field::SetObjStaticFromCode; |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame] | 286 | pCanPutArrayElementFromCode = Class::CanPutArrayElementFromCode; |
| 287 | pThrowException = ThrowException; |
| 288 | pInitializeTypeFromCode = InitializeTypeFromCode; |
buzbee | 561227c | 2011-09-02 15:28:19 -0700 | [diff] [blame] | 289 | pResolveMethodFromCode = ResolveMethodFromCode; |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame] | 290 | pInitializeStaticStorage = ClassLinker::InitializeStaticStorageFromCode; |
buzbee | 2a475e7 | 2011-09-07 17:19:17 -0700 | [diff] [blame] | 291 | pInstanceofNonTrivialFromCode = Object::InstanceOf; |
| 292 | pCheckCastFromCode = CheckCastFromCode; |
| 293 | pLockObjectFromCode = LockObjectFromCode; |
| 294 | pUnlockObjectFromCode = UnlockObjectFromCode; |
buzbee | 34cd9e5 | 2011-09-08 14:31:52 -0700 | [diff] [blame] | 295 | pFindFieldFromCode = Field::FindFieldFromCode; |
buzbee | 0d966cf | 2011-09-08 17:34:58 -0700 | [diff] [blame] | 296 | pCheckSuspendFromCode = CheckSuspendFromCode; |
buzbee | cefd187 | 2011-09-09 09:59:52 -0700 | [diff] [blame] | 297 | pStackOverflowFromCode = StackOverflowFromCode; |
buzbee | 5ade1d2 | 2011-09-09 14:44:52 -0700 | [diff] [blame] | 298 | pThrowNullPointerFromCode = ThrowNullPointerFromCode; |
| 299 | pThrowArrayBoundsFromCode = ThrowArrayBoundsFromCode; |
| 300 | pThrowDivZeroFromCode = ThrowDivZeroFromCode; |
| 301 | pThrowVerificationErrorFromCode = ThrowVerificationErrorFromCode; |
| 302 | pThrowNegArraySizeFromCode = ThrowNegArraySizeFromCode; |
| 303 | pThrowRuntimeExceptionFromCode = ThrowRuntimeExceptionFromCode; |
| 304 | pThrowInternalErrorFromCode = ThrowInternalErrorFromCode; |
| 305 | pThrowNoSuchMethodFromCode = ThrowNoSuchMethodFromCode; |
buzbee | 4a3164f | 2011-09-03 11:25:10 -0700 | [diff] [blame] | 306 | pDebugMe = DebugMe; |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 309 | void Frame::Next() { |
| 310 | byte* next_sp = reinterpret_cast<byte*>(sp_) + |
Shih-wei Liao | d11af15 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 311 | GetMethod()->GetFrameSizeInBytes(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 312 | sp_ = reinterpret_cast<Method**>(next_sp); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 315 | uintptr_t Frame::GetPC() const { |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 316 | byte* pc_addr = reinterpret_cast<byte*>(sp_) + |
Shih-wei Liao | d11af15 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 317 | GetMethod()->GetReturnPcOffsetInBytes(); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 318 | return *reinterpret_cast<uintptr_t*>(pc_addr); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 321 | Method* Frame::NextMethod() const { |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 322 | byte* next_sp = reinterpret_cast<byte*>(sp_) + |
Shih-wei Liao | d11af15 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 323 | GetMethod()->GetFrameSizeInBytes(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 324 | return *reinterpret_cast<Method**>(next_sp); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 327 | void* ThreadStart(void *arg) { |
Elliott Hughes | 53b6131 | 2011-08-12 18:28:20 -0700 | [diff] [blame] | 328 | UNIMPLEMENTED(FATAL); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 329 | return NULL; |
| 330 | } |
| 331 | |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame^] | 332 | void Thread::Create(Object* peer, size_t stack_size) { |
| 333 | CHECK(peer != NULL); |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 334 | |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame^] | 335 | if (stack_size == 0) { |
| 336 | stack_size = Runtime::Current()->GetDefaultStackSize(); |
| 337 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 338 | |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame^] | 339 | Thread* self = new Thread; |
| 340 | self->peer_ = peer; |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 341 | |
| 342 | pthread_attr_t attr; |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 343 | errno = pthread_attr_init(&attr); |
| 344 | if (errno != 0) { |
| 345 | PLOG(FATAL) << "pthread_attr_init failed"; |
| 346 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 347 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 348 | errno = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 349 | if (errno != 0) { |
| 350 | PLOG(FATAL) << "pthread_attr_setdetachstate(PTHREAD_CREATE_DETACHED) failed"; |
| 351 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 352 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 353 | errno = pthread_attr_setstacksize(&attr, stack_size); |
| 354 | if (errno != 0) { |
| 355 | PLOG(FATAL) << "pthread_attr_setstacksize(" << stack_size << ") failed"; |
| 356 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 357 | |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame^] | 358 | errno = pthread_create(&self->pthread_, &attr, ThreadStart, self); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 359 | if (errno != 0) { |
| 360 | PLOG(FATAL) << "pthread_create failed"; |
| 361 | } |
| 362 | |
| 363 | errno = pthread_attr_destroy(&attr); |
| 364 | if (errno != 0) { |
| 365 | PLOG(FATAL) << "pthread_attr_destroy failed"; |
| 366 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 369 | Thread* Thread::Attach(const Runtime* runtime, const char* name, bool as_daemon) { |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 370 | Thread* self = new Thread; |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 371 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 372 | self->tid_ = ::art::GetTid(); |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 373 | self->pthread_ = pthread_self(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 374 | |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 375 | self->InitStackHwm(); |
| 376 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 377 | self->state_ = kRunnable; |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 378 | |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 379 | SetThreadName(name); |
| 380 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 381 | errno = pthread_setspecific(Thread::pthread_key_self_, self); |
Elliott Hughes | a5780da | 2011-07-17 11:39:39 -0700 | [diff] [blame] | 382 | if (errno != 0) { |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 383 | PLOG(FATAL) << "pthread_setspecific failed"; |
Elliott Hughes | a5780da | 2011-07-17 11:39:39 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 386 | self->jni_env_ = new JNIEnvExt(self, runtime->GetJavaVM()); |
Elliott Hughes | 330304d | 2011-08-12 14:28:05 -0700 | [diff] [blame] | 387 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 388 | runtime->GetThreadList()->Register(self); |
| 389 | |
| 390 | // If we're the main thread, ClassLinker won't be created until after we're attached, |
| 391 | // so that thread needs a two-stage attach. Regular threads don't need this hack. |
| 392 | if (self->thin_lock_id_ != ThreadList::kMainId) { |
| 393 | self->CreatePeer(name, as_daemon); |
| 394 | } |
| 395 | |
| 396 | return self; |
| 397 | } |
| 398 | |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame^] | 399 | jobject GetWellKnownThreadGroup(JNIEnv* env, const char* field_name) { |
| 400 | jclass thread_group_class = env->FindClass("java/lang/ThreadGroup"); |
| 401 | jfieldID fid = env->GetStaticFieldID(thread_group_class, field_name, "Ljava/lang/ThreadGroup;"); |
| 402 | jobject thread_group = env->GetStaticObjectField(thread_group_class, fid); |
| 403 | // This will be null in the compiler (and tests), but never in a running system. |
| 404 | //CHECK(thread_group != NULL) << "java.lang.ThreadGroup." << field_name << " not initialized"; |
| 405 | return thread_group; |
| 406 | } |
| 407 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 408 | void Thread::CreatePeer(const char* name, bool as_daemon) { |
| 409 | ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative); |
| 410 | |
| 411 | JNIEnv* env = jni_env_; |
| 412 | |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame^] | 413 | const char* field_name = (GetThinLockId() == ThreadList::kMainId) ? "mMain" : "mSystem"; |
| 414 | jobject thread_group = GetWellKnownThreadGroup(env, field_name); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 415 | jobject thread_name = env->NewStringUTF(name); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 416 | jint thread_priority = GetNativePriority(); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 417 | jboolean thread_is_daemon = as_daemon; |
| 418 | |
| 419 | jclass c = env->FindClass("java/lang/Thread"); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 420 | jmethodID mid = env->GetMethodID(c, "<init>", "(Ljava/lang/ThreadGroup;Ljava/lang/String;IZ)V"); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 421 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 422 | jobject peer = env->NewObject(c, mid, thread_group, thread_name, thread_priority, thread_is_daemon); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame^] | 423 | |
| 424 | // Because we mostly run without code available (in the compiler, in tests), we |
| 425 | // manually assign the fields the constructor should have set. |
| 426 | // TODO: lose this. |
| 427 | jfieldID fid; |
| 428 | fid = env->GetFieldID(c, "group", "Ljava/lang/ThreadGroup;"); |
| 429 | env->SetObjectField(peer, fid, thread_group); |
| 430 | fid = env->GetFieldID(c, "name", "Ljava/lang/String;"); |
| 431 | env->SetObjectField(peer, fid, thread_name); |
| 432 | fid = env->GetFieldID(c, "priority", "I"); |
| 433 | env->SetIntField(peer, fid, thread_priority); |
| 434 | fid = env->GetFieldID(c, "daemon", "Z"); |
| 435 | env->SetBooleanField(peer, fid, thread_is_daemon); |
| 436 | |
| 437 | peer_ = DecodeJObject(peer); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 440 | void Thread::InitStackHwm() { |
| 441 | pthread_attr_t attributes; |
| 442 | errno = pthread_getattr_np(pthread_, &attributes); |
| 443 | if (errno != 0) { |
| 444 | PLOG(FATAL) << "pthread_getattr_np failed"; |
| 445 | } |
| 446 | |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 447 | void* stack_base; |
| 448 | size_t stack_size; |
| 449 | errno = pthread_attr_getstack(&attributes, &stack_base, &stack_size); |
| 450 | if (errno != 0) { |
| 451 | PLOG(FATAL) << "pthread_attr_getstack failed"; |
| 452 | } |
| 453 | |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 454 | if (stack_size <= kStackOverflowReservedBytes) { |
| 455 | LOG(FATAL) << "attempt to attach a thread with a too-small stack (" << stack_size << " bytes)"; |
| 456 | } |
Elliott Hughes | 449b4bd | 2011-09-09 12:01:38 -0700 | [diff] [blame] | 457 | |
| 458 | // stack_base is the "lowest addressable byte" of the stack. |
| 459 | // Our stacks grow down, so we want stack_end_ to be near there, but reserving enough room |
| 460 | // to throw a StackOverflowError. |
buzbee | cefd187 | 2011-09-09 09:59:52 -0700 | [diff] [blame] | 461 | stack_end_ = reinterpret_cast<byte*>(stack_base) + kStackOverflowReservedBytes; |
Elliott Hughes | 449b4bd | 2011-09-09 12:01:38 -0700 | [diff] [blame] | 462 | |
| 463 | // Sanity check. |
| 464 | int stack_variable; |
| 465 | CHECK_GT(&stack_variable, (void*) stack_end_); |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 466 | |
| 467 | errno = pthread_attr_destroy(&attributes); |
| 468 | if (errno != 0) { |
| 469 | PLOG(FATAL) << "pthread_attr_destroy failed"; |
| 470 | } |
| 471 | } |
| 472 | |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 473 | void Thread::Dump(std::ostream& os) const { |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 474 | DumpState(os); |
| 475 | DumpStack(os); |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 478 | std::string GetSchedulerGroup(pid_t tid) { |
| 479 | // /proc/<pid>/group looks like this: |
| 480 | // 2:devices:/ |
| 481 | // 1:cpuacct,cpu:/ |
| 482 | // We want the third field from the line whose second field contains the "cpu" token. |
| 483 | std::string cgroup_file; |
| 484 | if (!ReadFileToString("/proc/self/cgroup", &cgroup_file)) { |
| 485 | return ""; |
| 486 | } |
| 487 | std::vector<std::string> cgroup_lines; |
| 488 | Split(cgroup_file, '\n', cgroup_lines); |
| 489 | for (size_t i = 0; i < cgroup_lines.size(); ++i) { |
| 490 | std::vector<std::string> cgroup_fields; |
| 491 | Split(cgroup_lines[i], ':', cgroup_fields); |
| 492 | std::vector<std::string> cgroups; |
| 493 | Split(cgroup_fields[1], ',', cgroups); |
| 494 | for (size_t i = 0; i < cgroups.size(); ++i) { |
| 495 | if (cgroups[i] == "cpu") { |
| 496 | return cgroup_fields[2].substr(1); // Skip the leading slash. |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | return ""; |
| 501 | } |
| 502 | |
| 503 | void Thread::DumpState(std::ostream& os) const { |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame^] | 504 | std::string thread_name("<native thread without managed peer>"); |
| 505 | std::string group_name; |
| 506 | int priority; |
| 507 | bool is_daemon = false; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 508 | |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame^] | 509 | if (peer_ != NULL) { |
| 510 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 511 | |
| 512 | Class* boolean_class = class_linker->FindPrimitiveClass('Z'); |
| 513 | Class* int_class = class_linker->FindPrimitiveClass('I'); |
| 514 | Class* string_class = class_linker->FindSystemClass("Ljava/lang/String;"); |
| 515 | Class* thread_class = class_linker->FindSystemClass("Ljava/lang/Thread;"); |
| 516 | Class* thread_group_class = class_linker->FindSystemClass("Ljava/lang/ThreadGroup;"); |
| 517 | |
| 518 | Field* name_field = thread_class->FindDeclaredInstanceField("name", string_class); |
| 519 | Field* priority_field = thread_class->FindDeclaredInstanceField("priority", int_class); |
| 520 | Field* daemon_field = thread_class->FindDeclaredInstanceField("daemon", boolean_class); |
| 521 | Field* thread_group_field = thread_class->FindDeclaredInstanceField("group", thread_group_class); |
| 522 | |
| 523 | String* thread_name_string = reinterpret_cast<String*>(name_field->GetObject(peer_)); |
| 524 | thread_name = (thread_name_string != NULL) ? thread_name_string->ToModifiedUtf8() : "<null>"; |
| 525 | priority = priority_field->GetInt(peer_); |
| 526 | is_daemon = daemon_field->GetBoolean(peer_); |
| 527 | |
| 528 | Object* thread_group = thread_group_field->GetObject(peer_); |
| 529 | if (thread_group != NULL) { |
| 530 | Field* name_field = thread_group_class->FindDeclaredInstanceField("name", string_class); |
| 531 | String* group_name_string = reinterpret_cast<String*>(name_field->GetObject(thread_group)); |
| 532 | group_name = (group_name_string != NULL) ? group_name_string->ToModifiedUtf8() : "<null>"; |
| 533 | } |
| 534 | } else { |
| 535 | // This name may be truncated, but it's the best we can do in the absence of a managed peer. |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 536 | std::string stats; |
| 537 | if (ReadFileToString(StringPrintf("/proc/self/task/%d/stat", GetTid()).c_str(), &stats)) { |
| 538 | size_t start = stats.find('(') + 1; |
| 539 | size_t end = stats.find(')') - start; |
| 540 | thread_name = stats.substr(start, end); |
| 541 | } |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame^] | 542 | priority = GetNativePriority(); |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 543 | } |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 544 | |
| 545 | int policy; |
| 546 | sched_param sp; |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 547 | errno = pthread_getschedparam(pthread_, &policy, &sp); |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 548 | if (errno != 0) { |
| 549 | PLOG(FATAL) << "pthread_getschedparam failed"; |
| 550 | } |
| 551 | |
| 552 | std::string scheduler_group(GetSchedulerGroup(GetTid())); |
| 553 | if (scheduler_group.empty()) { |
| 554 | scheduler_group = "default"; |
| 555 | } |
| 556 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 557 | os << '"' << thread_name << '"'; |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame^] | 558 | if (is_daemon) { |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 559 | os << " daemon"; |
| 560 | } |
| 561 | os << " prio=" << priority |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 562 | << " tid=" << GetThinLockId() |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 563 | << " " << state_ << "\n"; |
| 564 | |
| 565 | int suspend_count = 0; // TODO |
| 566 | int debug_suspend_count = 0; // TODO |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 567 | os << " | group=\"" << group_name << "\"" |
| 568 | << " sCount=" << suspend_count |
| 569 | << " dsCount=" << debug_suspend_count |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 570 | << " obj=" << reinterpret_cast<void*>(peer_) |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 571 | << " self=" << reinterpret_cast<const void*>(this) << "\n"; |
| 572 | os << " | sysTid=" << GetTid() |
| 573 | << " nice=" << getpriority(PRIO_PROCESS, GetTid()) |
| 574 | << " sched=" << policy << "/" << sp.sched_priority |
| 575 | << " cgrp=" << scheduler_group |
| 576 | << " handle=" << GetImpl() << "\n"; |
| 577 | |
| 578 | // Grab the scheduler stats for this thread. |
| 579 | std::string scheduler_stats; |
| 580 | if (ReadFileToString(StringPrintf("/proc/self/task/%d/schedstat", GetTid()).c_str(), &scheduler_stats)) { |
| 581 | scheduler_stats.resize(scheduler_stats.size() - 1); // Lose the trailing '\n'. |
| 582 | } else { |
| 583 | scheduler_stats = "0 0 0"; |
| 584 | } |
| 585 | |
| 586 | int utime = 0; |
| 587 | int stime = 0; |
| 588 | int task_cpu = 0; |
| 589 | std::string stats; |
| 590 | if (ReadFileToString(StringPrintf("/proc/self/task/%d/stat", GetTid()).c_str(), &stats)) { |
| 591 | // Skip the command, which may contain spaces. |
| 592 | stats = stats.substr(stats.find(')') + 2); |
| 593 | // Extract the three fields we care about. |
| 594 | std::vector<std::string> fields; |
| 595 | Split(stats, ' ', fields); |
| 596 | utime = strtoull(fields[11].c_str(), NULL, 10); |
| 597 | stime = strtoull(fields[12].c_str(), NULL, 10); |
| 598 | task_cpu = strtoull(fields[36].c_str(), NULL, 10); |
| 599 | } |
| 600 | |
| 601 | os << " | schedstat=( " << scheduler_stats << " )" |
| 602 | << " utm=" << utime |
| 603 | << " stm=" << stime |
| 604 | << " core=" << task_cpu |
| 605 | << " HZ=" << sysconf(_SC_CLK_TCK) << "\n"; |
| 606 | } |
| 607 | |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame^] | 608 | struct StackDumpVisitor : public Thread::StackVisitor { |
| 609 | StackDumpVisitor(std::ostream& os) : os(os) { |
| 610 | } |
| 611 | |
| 612 | ~StackDumpVisitor() { |
| 613 | } |
| 614 | |
| 615 | void VisitFrame(const Frame& frame) { |
| 616 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 617 | |
| 618 | Method* m = frame.GetMethod(); |
| 619 | Class* c = m->GetDeclaringClass(); |
| 620 | const DexFile& dex_file = class_linker->FindDexFile(c->GetDexCache()); |
| 621 | |
| 622 | os << " at " << PrettyMethod(m, false); |
| 623 | if (m->IsNative()) { |
| 624 | os << "(Native method)"; |
| 625 | } else { |
| 626 | int line_number = dex_file.GetLineNumFromPC(m, m->ToDexPC(frame.GetPC())); |
| 627 | os << "(" << c->GetSourceFile()->ToModifiedUtf8() << ":" << line_number << ")"; |
| 628 | } |
| 629 | os << "\n"; |
| 630 | } |
| 631 | |
| 632 | std::ostream& os; |
| 633 | }; |
| 634 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 635 | void Thread::DumpStack(std::ostream& os) const { |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame^] | 636 | StackDumpVisitor dumper(os); |
| 637 | WalkStack(&dumper); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 638 | } |
| 639 | |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 640 | void Thread::ThreadExitCallback(void* arg) { |
| 641 | Thread* self = reinterpret_cast<Thread*>(arg); |
| 642 | LOG(FATAL) << "Native thread exited without calling DetachCurrentThread: " << *self; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 643 | } |
| 644 | |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 645 | void Thread::Startup() { |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 646 | // Allocate a TLS slot. |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 647 | errno = pthread_key_create(&Thread::pthread_key_self_, Thread::ThreadExitCallback); |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 648 | if (errno != 0) { |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 649 | PLOG(FATAL) << "pthread_key_create failed"; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | // Double-check the TLS slot allocation. |
| 653 | if (pthread_getspecific(pthread_key_self_) != NULL) { |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 654 | LOG(FATAL) << "newly-created pthread TLS slot is not NULL"; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | // TODO: initialize other locks and condition variables |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 658 | } |
| 659 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 660 | void Thread::Shutdown() { |
| 661 | errno = pthread_key_delete(Thread::pthread_key_self_); |
| 662 | if (errno != 0) { |
| 663 | PLOG(WARNING) << "pthread_key_delete failed"; |
| 664 | } |
| 665 | } |
| 666 | |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 667 | Thread::Thread() |
Elliott Hughes | 02b48d1 | 2011-09-07 17:15:51 -0700 | [diff] [blame] | 668 | : peer_(NULL), |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 669 | wait_mutex_("Thread wait mutex"), |
| 670 | wait_monitor_(NULL), |
| 671 | interrupted_(false), |
| 672 | stack_end_(NULL), |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 673 | top_of_managed_stack_(), |
| 674 | native_to_managed_record_(NULL), |
| 675 | top_sirt_(NULL), |
| 676 | jni_env_(NULL), |
| 677 | exception_(NULL), |
| 678 | suspend_count_(0), |
| 679 | class_loader_override_(NULL) { |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 680 | InitCpu(); |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 681 | InitFunctionPointers(); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 682 | thin_lock_id_ = Runtime::Current()->GetThreadList()->AllocThreadId(); |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 683 | } |
| 684 | |
Elliott Hughes | 02b48d1 | 2011-09-07 17:15:51 -0700 | [diff] [blame] | 685 | void MonitorExitVisitor(const Object* object, void*) { |
| 686 | Object* entered_monitor = const_cast<Object*>(object); |
| 687 | entered_monitor->MonitorExit();; |
| 688 | } |
| 689 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 690 | Thread::~Thread() { |
Elliott Hughes | 02b48d1 | 2011-09-07 17:15:51 -0700 | [diff] [blame] | 691 | // TODO: check we're not calling the JNI DetachCurrentThread function from |
| 692 | // a call stack that includes managed frames. (It's only valid if the stack is all-native.) |
| 693 | |
| 694 | // On thread detach, all monitors entered with JNI MonitorEnter are automatically exited. |
| 695 | jni_env_->monitors.VisitRoots(MonitorExitVisitor, NULL); |
| 696 | |
| 697 | if (IsExceptionPending()) { |
| 698 | UNIMPLEMENTED(FATAL) << "threadExitUncaughtException()"; |
| 699 | } |
| 700 | |
| 701 | // TODO: ThreadGroup.removeThread(this); |
| 702 | |
| 703 | // TODO: this.vmData = 0; |
| 704 | |
| 705 | // TODO: say "bye" to the debugger. |
| 706 | //if (gDvm.debuggerConnected) { |
| 707 | // dvmDbgPostThreadDeath(self); |
| 708 | //} |
| 709 | |
| 710 | // Thread.join() is implemented as an Object.wait() on the Thread.lock |
| 711 | // object. Signal anyone who is waiting. |
| 712 | //Object* lock = dvmGetFieldObject(self->threadObj, gDvm.offJavaLangThread_lock); |
| 713 | //dvmLockObject(self, lock); |
| 714 | //dvmObjectNotifyAll(self, lock); |
| 715 | //dvmUnlockObject(self, lock); |
| 716 | //lock = NULL; |
| 717 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 718 | delete jni_env_; |
Elliott Hughes | 02b48d1 | 2011-09-07 17:15:51 -0700 | [diff] [blame] | 719 | jni_env_ = NULL; |
| 720 | |
| 721 | SetState(Thread::kTerminated); |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 722 | } |
| 723 | |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 724 | size_t Thread::NumSirtReferences() { |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 725 | size_t count = 0; |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 726 | for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->Link()) { |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 727 | count += cur->NumberOfReferences(); |
| 728 | } |
| 729 | return count; |
| 730 | } |
| 731 | |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 732 | bool Thread::SirtContains(jobject obj) { |
| 733 | Object** sirt_entry = reinterpret_cast<Object**>(obj); |
| 734 | for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->Link()) { |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 735 | size_t num_refs = cur->NumberOfReferences(); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 736 | // A SIRT should always have a jobject/jclass as a native method is passed |
| 737 | // in a this pointer or a class |
| 738 | DCHECK_GT(num_refs, 0u); |
Shih-wei Liao | 2f0ce9d | 2011-09-01 02:07:58 -0700 | [diff] [blame] | 739 | if ((&cur->References()[0] <= sirt_entry) && |
| 740 | (sirt_entry <= (&cur->References()[num_refs - 1]))) { |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 741 | return true; |
| 742 | } |
| 743 | } |
| 744 | return false; |
| 745 | } |
| 746 | |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 747 | Object* Thread::DecodeJObject(jobject obj) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 748 | DCHECK(CanAccessDirectReferences()); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 749 | if (obj == NULL) { |
| 750 | return NULL; |
| 751 | } |
| 752 | IndirectRef ref = reinterpret_cast<IndirectRef>(obj); |
| 753 | IndirectRefKind kind = GetIndirectRefKind(ref); |
| 754 | Object* result; |
| 755 | switch (kind) { |
| 756 | case kLocal: |
| 757 | { |
Elliott Hughes | 69f5bc6 | 2011-08-24 09:26:14 -0700 | [diff] [blame] | 758 | IndirectReferenceTable& locals = jni_env_->locals; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 759 | result = const_cast<Object*>(locals.Get(ref)); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 760 | break; |
| 761 | } |
| 762 | case kGlobal: |
| 763 | { |
| 764 | JavaVMExt* vm = Runtime::Current()->GetJavaVM(); |
| 765 | IndirectReferenceTable& globals = vm->globals; |
| 766 | MutexLock mu(vm->globals_lock); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 767 | result = const_cast<Object*>(globals.Get(ref)); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 768 | break; |
| 769 | } |
| 770 | case kWeakGlobal: |
| 771 | { |
| 772 | JavaVMExt* vm = Runtime::Current()->GetJavaVM(); |
| 773 | IndirectReferenceTable& weak_globals = vm->weak_globals; |
| 774 | MutexLock mu(vm->weak_globals_lock); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 775 | result = const_cast<Object*>(weak_globals.Get(ref)); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 776 | if (result == kClearedJniWeakGlobal) { |
| 777 | // This is a special case where it's okay to return NULL. |
| 778 | return NULL; |
| 779 | } |
| 780 | break; |
| 781 | } |
| 782 | case kSirtOrInvalid: |
| 783 | default: |
| 784 | // TODO: make stack indirect reference table lookup more efficient |
| 785 | // Check if this is a local reference in the SIRT |
| 786 | if (SirtContains(obj)) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 787 | result = *reinterpret_cast<Object**>(obj); // Read from SIRT |
Elliott Hughes | c5bfa8f | 2011-08-30 14:32:49 -0700 | [diff] [blame] | 788 | } else if (jni_env_->work_around_app_jni_bugs) { |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 789 | // Assume an invalid local reference is actually a direct pointer. |
| 790 | result = reinterpret_cast<Object*>(obj); |
| 791 | } else { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 792 | result = kInvalidIndirectRefObject; |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 793 | } |
| 794 | } |
| 795 | |
| 796 | if (result == NULL) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 797 | LOG(ERROR) << "JNI ERROR (app bug): use of deleted " << kind << ": " << obj; |
| 798 | JniAbort(NULL); |
| 799 | } else { |
| 800 | if (result != kInvalidIndirectRefObject) { |
| 801 | Heap::VerifyObject(result); |
| 802 | } |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 803 | } |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 804 | return result; |
| 805 | } |
| 806 | |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 807 | class CountStackDepthVisitor : public Thread::StackVisitor { |
| 808 | public: |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 809 | CountStackDepthVisitor() : depth_(0) {} |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame^] | 810 | |
| 811 | virtual void VisitFrame(const Frame&) { |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 812 | ++depth_; |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 813 | } |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 814 | |
| 815 | int GetDepth() const { |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 816 | return depth_; |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | private: |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 820 | uint32_t depth_; |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 821 | }; |
| 822 | |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 823 | // |
| 824 | class BuildInternalStackTraceVisitor : public Thread::StackVisitor { |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 825 | public: |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 826 | explicit BuildInternalStackTraceVisitor(int depth, ScopedJniThreadState& ts) : count_(0) { |
| 827 | // Allocate method trace with an extra slot that will hold the PC trace |
| 828 | method_trace_ = Runtime::Current()->GetClassLinker()-> |
| 829 | AllocObjectArray<Object>(depth + 1); |
| 830 | // Register a local reference as IntArray::Alloc may trigger GC |
| 831 | local_ref_ = AddLocalReference<jobject>(ts.Env(), method_trace_); |
| 832 | pc_trace_ = IntArray::Alloc(depth); |
| 833 | #ifdef MOVING_GARBAGE_COLLECTOR |
| 834 | // Re-read after potential GC |
| 835 | method_trace = Decode<ObjectArray<Object>*>(ts.Env(), local_ref_); |
| 836 | #endif |
| 837 | // Save PC trace in last element of method trace, also places it into the |
| 838 | // object graph. |
| 839 | method_trace_->Set(depth, pc_trace_); |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 840 | } |
| 841 | |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 842 | virtual ~BuildInternalStackTraceVisitor() {} |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 843 | |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame^] | 844 | virtual void VisitFrame(const Frame& frame) { |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 845 | method_trace_->Set(count_, frame.GetMethod()); |
| 846 | pc_trace_->Set(count_, frame.GetPC()); |
| 847 | ++count_; |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 848 | } |
| 849 | |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 850 | jobject GetInternalStackTrace() const { |
| 851 | return local_ref_; |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | private: |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 855 | // Current position down stack trace |
| 856 | uint32_t count_; |
| 857 | // Array of return PC values |
| 858 | IntArray* pc_trace_; |
| 859 | // An array of the methods on the stack, the last entry is a reference to the |
| 860 | // PC trace |
| 861 | ObjectArray<Object>* method_trace_; |
| 862 | // Local indirect reference table entry for method trace |
| 863 | jobject local_ref_; |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 864 | }; |
| 865 | |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 866 | void Thread::WalkStack(StackVisitor* visitor) const { |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame^] | 867 | Frame frame = GetTopOfStack(); |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 868 | // TODO: enable this CHECK after native_to_managed_record_ is initialized during startup. |
| 869 | // CHECK(native_to_managed_record_ != NULL); |
| 870 | NativeToManagedRecord* record = native_to_managed_record_; |
| 871 | |
| 872 | while (frame.GetSP()) { |
| 873 | for ( ; frame.GetMethod() != 0; frame.Next()) { |
| 874 | visitor->VisitFrame(frame); |
| 875 | } |
| 876 | if (record == NULL) { |
| 877 | break; |
| 878 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 879 | 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] | 880 | record = record->link; |
| 881 | } |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 882 | } |
| 883 | |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 884 | jobject Thread::CreateInternalStackTrace() const { |
| 885 | // Compute depth of stack |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 886 | CountStackDepthVisitor count_visitor; |
| 887 | WalkStack(&count_visitor); |
| 888 | int32_t depth = count_visitor.GetDepth(); |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 889 | |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 890 | // Transition into runnable state to work on Object*/Array* |
| 891 | ScopedJniThreadState ts(jni_env_); |
| 892 | |
| 893 | // Build internal stack trace |
| 894 | BuildInternalStackTraceVisitor build_trace_visitor(depth, ts); |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 895 | WalkStack(&build_trace_visitor); |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 896 | |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 897 | return build_trace_visitor.GetInternalStackTrace(); |
| 898 | } |
| 899 | |
| 900 | jobjectArray Thread::InternalStackTraceToStackTraceElementArray(jobject internal, |
| 901 | JNIEnv* env) { |
| 902 | // Transition into runnable state to work on Object*/Array* |
| 903 | ScopedJniThreadState ts(env); |
| 904 | |
| 905 | // Decode the internal stack trace into the depth, method trace and PC trace |
| 906 | ObjectArray<Object>* method_trace = |
| 907 | down_cast<ObjectArray<Object>*>(Decode<Object*>(ts.Env(), internal)); |
| 908 | int32_t depth = method_trace->GetLength()-1; |
| 909 | IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth)); |
| 910 | |
| 911 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 912 | |
| 913 | // Create java_trace array and place in local reference table |
| 914 | ObjectArray<StackTraceElement>* java_traces = |
| 915 | class_linker->AllocStackTraceElementArray(depth); |
| 916 | jobjectArray result = AddLocalReference<jobjectArray>(ts.Env(), java_traces); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 917 | |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 918 | for (int32_t i = 0; i < depth; ++i) { |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 919 | // Prepare parameters for StackTraceElement(String cls, String method, String file, int line) |
| 920 | Method* method = down_cast<Method*>(method_trace->Get(i)); |
| 921 | uint32_t native_pc = pc_trace->Get(i); |
| 922 | Class* klass = method->GetDeclaringClass(); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 923 | const DexFile& dex_file = class_linker->FindDexFile(klass->GetDexCache()); |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 924 | String* readable_descriptor = String::AllocFromModifiedUtf8( |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 925 | PrettyDescriptor(klass->GetDescriptor()).c_str()); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 926 | |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 927 | // Allocate element, potentially triggering GC |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 928 | StackTraceElement* obj = |
| 929 | StackTraceElement::Alloc(readable_descriptor, |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 930 | method->GetName(), |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 931 | klass->GetSourceFile(), |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 932 | dex_file.GetLineNumFromPC(method, |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 933 | method->ToDexPC(native_pc))); |
| 934 | #ifdef MOVING_GARBAGE_COLLECTOR |
| 935 | // Re-read after potential GC |
| 936 | java_traces = Decode<ObjectArray<Object>*>(ts.Env(), result); |
| 937 | method_trace = down_cast<ObjectArray<Object>*>(Decode<Object*>(ts.Env(), internal)); |
| 938 | pc_trace = down_cast<IntArray*>(method_trace->Get(depth)); |
| 939 | #endif |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 940 | java_traces->Set(i, obj); |
| 941 | } |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 942 | return result; |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 943 | } |
| 944 | |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 945 | void Thread::ThrowNewException(const char* exception_class_descriptor, const char* fmt, ...) { |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 946 | std::string msg; |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 947 | va_list args; |
| 948 | va_start(args, fmt); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 949 | StringAppendV(&msg, fmt, args); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 950 | va_end(args); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 951 | |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 952 | // Convert "Ljava/lang/Exception;" into JNI-style "java/lang/Exception". |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 953 | CHECK_EQ('L', exception_class_descriptor[0]); |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 954 | std::string descriptor(exception_class_descriptor + 1); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 955 | CHECK_EQ(';', descriptor[descriptor.length() - 1]); |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 956 | descriptor.erase(descriptor.length() - 1); |
| 957 | |
| 958 | JNIEnv* env = GetJniEnv(); |
| 959 | jclass exception_class = env->FindClass(descriptor.c_str()); |
| 960 | CHECK(exception_class != NULL) << "descriptor=\"" << descriptor << "\""; |
| 961 | int rc = env->ThrowNew(exception_class, msg.c_str()); |
| 962 | CHECK_EQ(rc, JNI_OK); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 963 | } |
| 964 | |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 965 | void Thread::ThrowOutOfMemoryError() { |
| 966 | UNIMPLEMENTED(FATAL); |
| 967 | } |
| 968 | |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 969 | Frame Thread::FindExceptionHandler(void* throw_pc, void** handler_pc) { |
| 970 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 971 | DCHECK(class_linker != NULL); |
| 972 | |
| 973 | Frame cur_frame = GetTopOfStack(); |
| 974 | for (int unwind_depth = 0; ; unwind_depth++) { |
| 975 | const Method* cur_method = cur_frame.GetMethod(); |
| 976 | DexCache* dex_cache = cur_method->GetDeclaringClass()->GetDexCache(); |
| 977 | const DexFile& dex_file = class_linker->FindDexFile(dex_cache); |
| 978 | |
| 979 | void* handler_addr = FindExceptionHandlerInMethod(cur_method, |
| 980 | throw_pc, |
| 981 | dex_file, |
| 982 | class_linker); |
| 983 | if (handler_addr) { |
| 984 | *handler_pc = handler_addr; |
| 985 | return cur_frame; |
| 986 | } else { |
| 987 | // Check if we are at the last frame |
| 988 | if (cur_frame.HasNext()) { |
| 989 | cur_frame.Next(); |
| 990 | } else { |
| 991 | // Either at the top of stack or next frame is native. |
| 992 | break; |
| 993 | } |
| 994 | } |
| 995 | } |
| 996 | *handler_pc = NULL; |
| 997 | return Frame(); |
| 998 | } |
| 999 | |
| 1000 | void* Thread::FindExceptionHandlerInMethod(const Method* method, |
| 1001 | void* throw_pc, |
| 1002 | const DexFile& dex_file, |
| 1003 | ClassLinker* class_linker) { |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 1004 | Throwable* exception_obj = exception_; |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 1005 | exception_ = NULL; |
| 1006 | |
| 1007 | intptr_t dex_pc = -1; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1008 | const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset()); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 1009 | DexFile::CatchHandlerIterator iter; |
| 1010 | for (iter = dex_file.dexFindCatchHandler(*code_item, |
| 1011 | method->ToDexPC(reinterpret_cast<intptr_t>(throw_pc))); |
| 1012 | !iter.HasNext(); |
| 1013 | iter.Next()) { |
| 1014 | Class* klass = class_linker->FindSystemClass(dex_file.dexStringByTypeIdx(iter.Get().type_idx_)); |
| 1015 | DCHECK(klass != NULL); |
| 1016 | if (exception_obj->InstanceOf(klass)) { |
| 1017 | dex_pc = iter.Get().address_; |
| 1018 | break; |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | exception_ = exception_obj; |
| 1023 | if (iter.HasNext()) { |
| 1024 | return NULL; |
| 1025 | } else { |
| 1026 | return reinterpret_cast<void*>( method->ToNativePC(dex_pc) ); |
| 1027 | } |
| 1028 | } |
| 1029 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 1030 | void Thread::VisitRoots(Heap::RootVisitor* visitor, void* arg) const { |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame^] | 1031 | if (exception_ != NULL) { |
| 1032 | visitor(exception_, arg); |
| 1033 | } |
| 1034 | if (peer_ != NULL) { |
| 1035 | visitor(peer_, arg); |
| 1036 | } |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 1037 | jni_env_->locals.VisitRoots(visitor, arg); |
| 1038 | jni_env_->monitors.VisitRoots(visitor, arg); |
| 1039 | // visitThreadStack(visitor, thread, arg); |
| 1040 | UNIMPLEMENTED(WARNING) << "some per-Thread roots not visited"; |
| 1041 | } |
| 1042 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 1043 | static const char* kStateNames[] = { |
| 1044 | "New", |
| 1045 | "Runnable", |
| 1046 | "Blocked", |
| 1047 | "Waiting", |
| 1048 | "TimedWaiting", |
| 1049 | "Native", |
| 1050 | "Terminated", |
| 1051 | }; |
| 1052 | std::ostream& operator<<(std::ostream& os, const Thread::State& state) { |
| 1053 | if (state >= Thread::kNew && state <= Thread::kTerminated) { |
| 1054 | os << kStateNames[state-Thread::kNew]; |
| 1055 | } else { |
| 1056 | os << "State[" << static_cast<int>(state) << "]"; |
| 1057 | } |
| 1058 | return os; |
| 1059 | } |
| 1060 | |
Elliott Hughes | 330304d | 2011-08-12 14:28:05 -0700 | [diff] [blame] | 1061 | std::ostream& operator<<(std::ostream& os, const Thread& thread) { |
| 1062 | os << "Thread[" << &thread |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 1063 | << ",pthread_t=" << thread.GetImpl() |
| 1064 | << ",tid=" << thread.GetTid() |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 1065 | << ",id=" << thread.GetThinLockId() |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 1066 | << ",state=" << thread.GetState() |
| 1067 | << ",peer=" << thread.GetPeer() |
| 1068 | << "]"; |
Elliott Hughes | 330304d | 2011-08-12 14:28:05 -0700 | [diff] [blame] | 1069 | return os; |
| 1070 | } |
| 1071 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 1072 | } // namespace art |