Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 17 | #include "thread.h" |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 18 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 19 | #include <dynamic_annotations.h> |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 20 | #include <pthread.h> |
| 21 | #include <sys/mman.h> |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 22 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 23 | #include <algorithm> |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 24 | #include <bitset> |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 25 | #include <cerrno> |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 26 | #include <iostream> |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 27 | #include <list> |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 28 | |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 29 | #include "class_linker.h" |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 30 | #include "context.h" |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 31 | #include "heap.h" |
Elliott Hughes | c5f7c91 | 2011-08-18 14:00:42 -0700 | [diff] [blame] | 32 | #include "jni_internal.h" |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 33 | #include "object.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 34 | #include "runtime.h" |
buzbee | 5433072 | 2011-08-23 16:46:55 -0700 | [diff] [blame] | 35 | #include "runtime_support.h" |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 36 | #include "scoped_jni_thread_state.h" |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 37 | #include "thread_list.h" |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 38 | #include "utils.h" |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 39 | |
| 40 | namespace art { |
| 41 | |
| 42 | pthread_key_t Thread::pthread_key_self_; |
| 43 | |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 44 | static Class* gThrowable = NULL; |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 45 | static Field* gThread_daemon = NULL; |
| 46 | static Field* gThread_group = NULL; |
| 47 | static Field* gThread_lock = NULL; |
| 48 | static Field* gThread_name = NULL; |
| 49 | static Field* gThread_priority = NULL; |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 50 | static Field* gThread_uncaughtHandler = NULL; |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 51 | static Field* gThread_vmData = NULL; |
| 52 | static Field* gThreadGroup_name = NULL; |
| 53 | static Method* gThread_run = NULL; |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 54 | static Method* gThreadGroup_removeThread = NULL; |
| 55 | static Method* gUncaughtExceptionHandler_uncaughtException = NULL; |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 56 | |
buzbee | 4a3164f | 2011-09-03 11:25:10 -0700 | [diff] [blame] | 57 | // Temporary debugging hook for compiler. |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 58 | void DebugMe(Method* method, uint32_t info) { |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 59 | LOG(INFO) << "DebugMe"; |
| 60 | if (method != NULL) { |
| 61 | LOG(INFO) << PrettyMethod(method); |
| 62 | } |
| 63 | LOG(INFO) << "Info: " << info; |
buzbee | 4a3164f | 2011-09-03 11:25:10 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 66 | // Called by generated call to throw an exception |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 67 | extern "C" void artDeliverExceptionFromCode(Throwable* exception, Thread* thread, Method** sp) { |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 68 | /* |
| 69 | * exception may be NULL, in which case this routine should |
| 70 | * throw NPE. NOTE: this is a convenience for generated code, |
| 71 | * which previously did the null check inline and constructed |
| 72 | * and threw a NPE if NULL. This routine responsible for setting |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 73 | * exception_ in thread and delivering the exception. |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 74 | */ |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 75 | // Place a special frame at the TOS that will save all callee saves |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 76 | *sp = Runtime::Current()->GetCalleeSaveMethod(); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 77 | thread->SetTopOfStack(sp, 0); |
Ian Rogers | 93dd966 | 2011-09-17 23:21:22 -0700 | [diff] [blame] | 78 | if (exception == NULL) { |
| 79 | thread->ThrowNewException("Ljava/lang/NullPointerException;", "throw with null exception"); |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 80 | } else { |
| 81 | thread->SetException(exception); |
Ian Rogers | 93dd966 | 2011-09-17 23:21:22 -0700 | [diff] [blame] | 82 | } |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 83 | thread->DeliverException(); |
| 84 | } |
| 85 | |
| 86 | // Deliver an exception that's pending on thread helping set up a callee save frame on the way |
| 87 | extern "C" void artDeliverPendingExceptionFromCode(Thread* thread, Method** sp) { |
| 88 | *sp = Runtime::Current()->GetCalleeSaveMethod(); |
| 89 | thread->SetTopOfStack(sp, 0); |
| 90 | thread->DeliverException(); |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Ian Rogers | 9651f42 | 2011-09-19 20:26:07 -0700 | [diff] [blame] | 93 | // Called by generated call to throw a NPE exception |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 94 | extern "C" void artThrowNullPointerExceptionFromCode(Thread* thread, Method** sp) { |
Ian Rogers | 9651f42 | 2011-09-19 20:26:07 -0700 | [diff] [blame] | 95 | // Place a special frame at the TOS that will save all callee saves |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 96 | *sp = Runtime::Current()->GetCalleeSaveMethod(); |
Ian Rogers | 9651f42 | 2011-09-19 20:26:07 -0700 | [diff] [blame] | 97 | thread->SetTopOfStack(sp, 0); |
| 98 | thread->ThrowNewException("Ljava/lang/NullPointerException;", "unexpected null reference"); |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 99 | thread->DeliverException(); |
Ian Rogers | 9651f42 | 2011-09-19 20:26:07 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | // Called by generated call to throw an arithmetic divide by zero exception |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 103 | extern "C" void artThrowDivZeroFromCode(Thread* thread, Method** sp) { |
Ian Rogers | 9651f42 | 2011-09-19 20:26:07 -0700 | [diff] [blame] | 104 | // Place a special frame at the TOS that will save all callee saves |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 105 | *sp = Runtime::Current()->GetCalleeSaveMethod(); |
Ian Rogers | 9651f42 | 2011-09-19 20:26:07 -0700 | [diff] [blame] | 106 | thread->SetTopOfStack(sp, 0); |
| 107 | thread->ThrowNewException("Ljava/lang/ArithmeticException;", "divide by zero"); |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 108 | thread->DeliverException(); |
Ian Rogers | 9651f42 | 2011-09-19 20:26:07 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | // Called by generated call to throw an arithmetic divide by zero exception |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 112 | extern "C" void artThrowArrayBoundsFromCode(int index, int limit, Thread* thread, Method** sp) { |
Ian Rogers | 9651f42 | 2011-09-19 20:26:07 -0700 | [diff] [blame] | 113 | // Place a special frame at the TOS that will save all callee saves |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 114 | *sp = Runtime::Current()->GetCalleeSaveMethod(); |
Ian Rogers | 9651f42 | 2011-09-19 20:26:07 -0700 | [diff] [blame] | 115 | thread->SetTopOfStack(sp, 0); |
| 116 | thread->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;", |
| 117 | "length=%d; index=%d", limit, index); |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 118 | thread->DeliverException(); |
Ian Rogers | 9651f42 | 2011-09-19 20:26:07 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 121 | // Called by the AbstractMethodError stub (not runtime support) |
| 122 | void ThrowAbstractMethodErrorFromCode(Method* method, Thread* thread, Method** sp) { |
| 123 | *sp = Runtime::Current()->GetCalleeSaveMethod(); |
| 124 | thread->SetTopOfStack(sp, 0); |
Ian Rogers | a0841a8 | 2011-09-22 14:16:31 -0700 | [diff] [blame] | 125 | thread->ThrowNewException("Ljava/lang/AbstractMethodError;", |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 126 | "abstract method \"%s\"", |
| 127 | PrettyMethod(method).c_str()); |
| 128 | thread->DeliverException(); |
| 129 | } |
| 130 | |
Ian Rogers | 932746a | 2011-09-22 18:57:50 -0700 | [diff] [blame] | 131 | extern "C" void artThrowStackOverflowFromCode(Method* method, Thread* thread, Method** sp) { |
| 132 | // Place a special frame at the TOS that will save all callee saves |
| 133 | Runtime* runtime = Runtime::Current(); |
| 134 | *sp = runtime->GetCalleeSaveMethod(); |
| 135 | thread->SetTopOfStack(sp, 0); |
| 136 | thread->SetStackEndForStackOverflow(); |
| 137 | thread->ThrowNewException("Ljava/lang/StackOverflowError;", |
| 138 | "stack size %zdkb; default stack size: %zdkb", |
| 139 | thread->GetStackSize() / KB, runtime->GetDefaultStackSize() / KB); |
| 140 | thread->ResetDefaultStackEnd(); |
| 141 | thread->DeliverException(); |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | // TODO: placeholder |
| 145 | void ThrowVerificationErrorFromCode(int32_t src1, int32_t ref) { |
| 146 | UNIMPLEMENTED(FATAL) << "Verification error, src1: " << src1 << |
| 147 | " ref: " << ref; |
| 148 | } |
| 149 | |
| 150 | // TODO: placeholder |
| 151 | void ThrowNegArraySizeFromCode(int32_t index) { |
| 152 | UNIMPLEMENTED(FATAL) << "Negative array size: " << index; |
| 153 | } |
| 154 | |
| 155 | // TODO: placeholder |
| 156 | void ThrowInternalErrorFromCode(int32_t errnum) { |
| 157 | UNIMPLEMENTED(FATAL) << "Internal error: " << errnum; |
| 158 | } |
| 159 | |
| 160 | // TODO: placeholder |
| 161 | void ThrowRuntimeExceptionFromCode(int32_t errnum) { |
| 162 | UNIMPLEMENTED(FATAL) << "Internal error: " << errnum; |
| 163 | } |
| 164 | |
| 165 | // TODO: placeholder |
| 166 | void ThrowNoSuchMethodFromCode(int32_t method_idx) { |
| 167 | UNIMPLEMENTED(FATAL) << "No such method, idx: " << method_idx; |
| 168 | } |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 169 | |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame] | 170 | // TODO: placeholder. Helper function to type |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 171 | Class* InitializeTypeFromCode(uint32_t type_idx, Method* method) { |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame] | 172 | /* |
| 173 | * Should initialize & fix up method->dex_cache_resolved_types_[]. |
| 174 | * Returns initialized type. Does not return normally if an exception |
| 175 | * is thrown, but instead initiates the catch. Should be similar to |
| 176 | * ClassLinker::InitializeStaticStorageFromCode. |
| 177 | */ |
| 178 | UNIMPLEMENTED(FATAL); |
| 179 | return NULL; |
| 180 | } |
| 181 | |
buzbee | 561227c | 2011-09-02 15:28:19 -0700 | [diff] [blame] | 182 | // TODO: placeholder. Helper function to resolve virtual method |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 183 | void ResolveMethodFromCode(Method* method, uint32_t method_idx) { |
buzbee | 561227c | 2011-09-02 15:28:19 -0700 | [diff] [blame] | 184 | /* |
| 185 | * Slow-path handler on invoke virtual method path in which |
| 186 | * base method is unresolved at compile-time. Doesn't need to |
| 187 | * return anything - just either ensure that |
| 188 | * method->dex_cache_resolved_methods_(method_idx) != NULL or |
| 189 | * throw and unwind. The caller will restart call sequence |
| 190 | * from the beginning. |
| 191 | */ |
| 192 | } |
| 193 | |
Ian Rogers | b886da8 | 2011-09-23 16:27:54 -0700 | [diff] [blame^] | 194 | // Helper function to alloc array for OP_FILLED_NEW_ARRAY |
| 195 | extern "C" Array* artCheckAndArrayAllocFromCode(uint32_t type_idx, Method* method, |
| 196 | int32_t component_count) { |
| 197 | if (component_count < 0) { |
| 198 | Thread::Current()->ThrowNewException("Ljava/lang/NegativeArraySizeException;", "%d", |
| 199 | component_count); |
| 200 | return NULL; // Failure |
| 201 | } |
| 202 | Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx); |
| 203 | if (klass == NULL) { // Not in dex cache so try to resolve |
| 204 | klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method); |
| 205 | if (klass == NULL) { // Error |
| 206 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 207 | return NULL; // Failure |
| 208 | } |
| 209 | } |
| 210 | if (klass->IsPrimitive() && !klass->IsPrimitiveInt()) { |
| 211 | if (klass->IsPrimitiveLong() || klass->IsPrimitiveDouble()) { |
| 212 | Thread::Current()->ThrowNewException("Ljava/lang/RuntimeException;", |
| 213 | "Bad filled array request for type %s", |
| 214 | PrettyDescriptor(klass->GetDescriptor()).c_str()); |
| 215 | } else { |
| 216 | Thread::Current()->ThrowNewException("Ljava/lang/InternalError;", |
| 217 | "Found type %s; filled-new-array not implemented for anything but \'int\'", |
| 218 | PrettyDescriptor(klass->GetDescriptor()).c_str()); |
| 219 | } |
| 220 | return NULL; // Failure |
| 221 | } else { |
| 222 | CHECK(klass->IsArrayClass()); |
| 223 | return Array::Alloc(klass, component_count); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | // Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If |
| 228 | // it cannot be resolved, throw an error. If it can, use it to create an array. |
| 229 | extern "C" Array* artArrayAllocFromCode(uint32_t type_idx, Method* method, int32_t component_count) { |
| 230 | if (component_count < 0) { |
| 231 | Thread::Current()->ThrowNewException("Ljava/lang/NegativeArraySizeException;", "%d", |
| 232 | component_count); |
| 233 | return NULL; // Failure |
| 234 | } |
| 235 | Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx); |
| 236 | if (klass == NULL) { // Not in dex cache so try to resolve |
| 237 | klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method); |
| 238 | if (klass == NULL) { // Error |
| 239 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 240 | return NULL; // Failure |
| 241 | } |
| 242 | CHECK(klass->IsArrayClass()); |
| 243 | } |
| 244 | return Array::Alloc(klass, component_count); |
buzbee | 1da522d | 2011-09-04 11:22:20 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Ian Rogers | e51a511 | 2011-09-23 14:16:35 -0700 | [diff] [blame] | 247 | // Check whether it is safe to cast one class to the other, throw exception and return -1 on failure |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 248 | extern "C" int artCheckCastFromCode(const Class* a, const Class* b) { |
Brian Carlstrom | c228252 | 2011-09-17 10:33:14 -0700 | [diff] [blame] | 249 | DCHECK(a->IsClass()); |
| 250 | DCHECK(b->IsClass()); |
| 251 | if (b->IsAssignableFrom(a)) { |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 252 | return 0; // Success |
| 253 | } else { |
| 254 | Thread::Current()->ThrowNewException("Ljava/lang/ClassCastException;", |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 255 | "%s cannot be cast to %s", |
| 256 | PrettyDescriptor(a->GetDescriptor()).c_str(), |
| 257 | PrettyDescriptor(b->GetDescriptor()).c_str()); |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 258 | return -1; // Failure |
Brian Carlstrom | c228252 | 2011-09-17 10:33:14 -0700 | [diff] [blame] | 259 | } |
buzbee | 2a475e7 | 2011-09-07 17:19:17 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Ian Rogers | e51a511 | 2011-09-23 14:16:35 -0700 | [diff] [blame] | 262 | // Tests whether 'element' can be assigned into an array of type 'array_class'. |
| 263 | // Returns 0 on success and -1 if an exception is pending. |
| 264 | extern "C" int artCanPutArrayElementFromCode(const Object* element, const Class* array_class) { |
| 265 | DCHECK(array_class != NULL); |
| 266 | // element can't be NULL as we catch this is screened in runtime_support |
| 267 | Class* element_class = element->GetClass(); |
| 268 | Class* component_type = array_class->GetComponentType(); |
| 269 | if (component_type->IsAssignableFrom(element_class)) { |
| 270 | return 0; // Success |
| 271 | } else { |
| 272 | Thread::Current()->ThrowNewException("Ljava/lang/ArrayStoreException;", |
Ian Rogers | b886da8 | 2011-09-23 16:27:54 -0700 | [diff] [blame^] | 273 | "Cannot store an object of type %s in to an array of type %s", |
| 274 | PrettyDescriptor(element_class->GetDescriptor()).c_str(), |
| 275 | PrettyDescriptor(array_class->GetDescriptor()).c_str()); |
Ian Rogers | e51a511 | 2011-09-23 14:16:35 -0700 | [diff] [blame] | 276 | return -1; // Failure |
| 277 | } |
| 278 | } |
| 279 | |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 280 | extern "C" int artUnlockObjectFromCode(Thread* thread, Object* obj) { |
| 281 | DCHECK(obj != NULL); // Assumed to have been checked before entry |
| 282 | return obj->MonitorExit(thread) ? 0 /* Success */ : -1 /* Failure */; |
buzbee | 2a475e7 | 2011-09-07 17:19:17 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 285 | void LockObjectFromCode(Thread* thread, Object* obj) { |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 286 | DCHECK(obj != NULL); // Assumed to have been checked before entry |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 287 | obj->MonitorEnter(thread); |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 288 | DCHECK(thread->HoldsLock(obj)); |
| 289 | // Only possible exception is NPE and is handled before entry |
| 290 | DCHECK(thread->GetException() == NULL); |
buzbee | 2a475e7 | 2011-09-07 17:19:17 -0700 | [diff] [blame] | 291 | } |
| 292 | |
buzbee | c1f4504 | 2011-09-21 16:03:19 -0700 | [diff] [blame] | 293 | extern "C" void artCheckSuspendFromCode(Thread* thread) { |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 294 | Runtime::Current()->GetThreadList()->FullSuspendCheck(thread); |
buzbee | 0d966cf | 2011-09-08 17:34:58 -0700 | [diff] [blame] | 295 | } |
| 296 | |
buzbee | 5ade1d2 | 2011-09-09 14:44:52 -0700 | [diff] [blame] | 297 | /* |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 298 | * Fill the array with predefined constant values, throwing exceptions if the array is null or |
| 299 | * not of sufficient length. |
buzbee | 5ade1d2 | 2011-09-09 14:44:52 -0700 | [diff] [blame] | 300 | * |
| 301 | * NOTE: When dealing with a raw dex file, the data to be copied uses |
| 302 | * little-endian ordering. Require that oat2dex do any required swapping |
| 303 | * so this routine can get by with a memcpy(). |
| 304 | * |
| 305 | * Format of the data: |
| 306 | * ushort ident = 0x0300 magic value |
| 307 | * ushort width width of each element in the table |
| 308 | * uint size number of elements in the table |
| 309 | * ubyte data[size*width] table of data values (may contain a single-byte |
| 310 | * padding at the end) |
| 311 | */ |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 312 | extern "C" int artHandleFillArrayDataFromCode(Array* array, const uint16_t* table) { |
| 313 | DCHECK_EQ(table[0], 0x0300); |
| 314 | if (array == NULL) { |
| 315 | Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", |
| 316 | "null array in fill array"); |
| 317 | return -1; // Error |
| 318 | } |
| 319 | DCHECK(array->IsArrayInstance() && !array->IsObjectArray()); |
| 320 | uint32_t size = (uint32_t)table[2] | (((uint32_t)table[3]) << 16); |
| 321 | if (static_cast<int32_t>(size) > array->GetLength()) { |
| 322 | Thread::Current()->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;", |
| 323 | "failed array fill. length=%d; index=%d", |
| 324 | array->GetLength(), size); |
| 325 | return -1; // Error |
| 326 | } |
| 327 | uint16_t width = table[1]; |
| 328 | uint32_t size_in_bytes = size * width; |
| 329 | memcpy((char*)array + Array::DataOffset().Int32Value(), (char*)&table[4], size_in_bytes); |
| 330 | return 0; // Success |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | // See comments in runtime_support.S |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 334 | extern "C" uint64_t artFindInterfaceMethodInCacheFromCode(uint32_t method_idx, |
| 335 | Object* this_object , |
| 336 | Method* caller_method) { |
| 337 | Thread* thread = Thread::Current(); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 338 | if (this_object == NULL) { |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 339 | thread->ThrowNewException("Ljava/lang/NullPointerException;", |
| 340 | "null receiver during interface dispatch"); |
| 341 | return 0; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 342 | } |
| 343 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 344 | Method* interface_method = class_linker->ResolveMethod(method_idx, caller_method, false); |
| 345 | if (interface_method == NULL) { |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 346 | // Could not resolve interface method. Throw error and unwind |
| 347 | CHECK(thread->GetException() != NULL); |
| 348 | return 0; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 349 | } |
| 350 | Method* method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method); |
| 351 | const void* code = method->GetCode(); |
| 352 | |
| 353 | uint32_t method_uint = reinterpret_cast<uint32_t>(method); |
| 354 | uint64_t code_uint = reinterpret_cast<uint32_t>(code); |
| 355 | uint64_t result = ((code_uint << 32) | method_uint); |
| 356 | return result; |
| 357 | } |
| 358 | |
buzbee | 5ade1d2 | 2011-09-09 14:44:52 -0700 | [diff] [blame] | 359 | // TODO: move to more appropriate location |
| 360 | /* |
| 361 | * Float/double conversion requires clamping to min and max of integer form. If |
| 362 | * target doesn't support this normally, use these. |
| 363 | */ |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 364 | int64_t D2L(double d) { |
buzbee | 5ade1d2 | 2011-09-09 14:44:52 -0700 | [diff] [blame] | 365 | static const double kMaxLong = (double)(int64_t)0x7fffffffffffffffULL; |
| 366 | static const double kMinLong = (double)(int64_t)0x8000000000000000ULL; |
| 367 | if (d >= kMaxLong) |
| 368 | return (int64_t)0x7fffffffffffffffULL; |
| 369 | else if (d <= kMinLong) |
| 370 | return (int64_t)0x8000000000000000ULL; |
| 371 | else if (d != d) // NaN case |
| 372 | return 0; |
| 373 | else |
| 374 | return (int64_t)d; |
| 375 | } |
| 376 | |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 377 | int64_t F2L(float f) { |
buzbee | 5ade1d2 | 2011-09-09 14:44:52 -0700 | [diff] [blame] | 378 | static const float kMaxLong = (float)(int64_t)0x7fffffffffffffffULL; |
| 379 | static const float kMinLong = (float)(int64_t)0x8000000000000000ULL; |
| 380 | if (f >= kMaxLong) |
| 381 | return (int64_t)0x7fffffffffffffffULL; |
| 382 | else if (f <= kMinLong) |
| 383 | return (int64_t)0x8000000000000000ULL; |
| 384 | else if (f != f) // NaN case |
| 385 | return 0; |
| 386 | else |
| 387 | return (int64_t)f; |
| 388 | } |
| 389 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 390 | // Return value helper for jobject return types |
| 391 | static Object* DecodeJObjectInThread(Thread* thread, jobject obj) { |
| 392 | return thread->DecodeJObject(obj); |
| 393 | } |
| 394 | |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 395 | void Thread::InitFunctionPointers() { |
buzbee | 5433072 | 2011-08-23 16:46:55 -0700 | [diff] [blame] | 396 | #if defined(__arm__) |
| 397 | pShlLong = art_shl_long; |
| 398 | pShrLong = art_shr_long; |
| 399 | pUshrLong = art_ushr_long; |
buzbee | 7b1b86d | 2011-08-26 18:59:10 -0700 | [diff] [blame] | 400 | pIdiv = __aeabi_idiv; |
| 401 | pIdivmod = __aeabi_idivmod; |
| 402 | pI2f = __aeabi_i2f; |
| 403 | pF2iz = __aeabi_f2iz; |
| 404 | pD2f = __aeabi_d2f; |
| 405 | pF2d = __aeabi_f2d; |
| 406 | pD2iz = __aeabi_d2iz; |
| 407 | pL2f = __aeabi_l2f; |
| 408 | pL2d = __aeabi_l2d; |
| 409 | pFadd = __aeabi_fadd; |
| 410 | pFsub = __aeabi_fsub; |
| 411 | pFdiv = __aeabi_fdiv; |
| 412 | pFmul = __aeabi_fmul; |
| 413 | pFmodf = fmodf; |
| 414 | pDadd = __aeabi_dadd; |
| 415 | pDsub = __aeabi_dsub; |
| 416 | pDdiv = __aeabi_ddiv; |
| 417 | pDmul = __aeabi_dmul; |
| 418 | pFmod = fmod; |
buzbee | 7b1b86d | 2011-08-26 18:59:10 -0700 | [diff] [blame] | 419 | pLdivmod = __aeabi_ldivmod; |
buzbee | 439c4fa | 2011-08-27 15:59:07 -0700 | [diff] [blame] | 420 | pLmul = __aeabi_lmul; |
Ian Rogers | b886da8 | 2011-09-23 16:27:54 -0700 | [diff] [blame^] | 421 | pArrayAllocFromCode = art_array_alloc_from_code; |
Ian Rogers | e51a511 | 2011-09-23 14:16:35 -0700 | [diff] [blame] | 422 | pCanPutArrayElementFromCode = art_can_put_array_element_from_code; |
Ian Rogers | b886da8 | 2011-09-23 16:27:54 -0700 | [diff] [blame^] | 423 | pCheckAndArrayAllocFromCode = art_check_and_array_alloc_from_code; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 424 | pCheckCastFromCode = art_check_cast_from_code; |
| 425 | pHandleFillArrayDataFromCode = art_handle_fill_data_from_code; |
Ian Rogers | cbba6ac | 2011-09-22 16:28:37 -0700 | [diff] [blame] | 426 | pInitializeStaticStorage = art_initialize_static_storage_from_code; |
buzbee | 4a3164f | 2011-09-03 11:25:10 -0700 | [diff] [blame] | 427 | pInvokeInterfaceTrampoline = art_invoke_interface_trampoline; |
buzbee | c1f4504 | 2011-09-21 16:03:19 -0700 | [diff] [blame] | 428 | pTestSuspendFromCode = art_test_suspend; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 429 | pThrowArrayBoundsFromCode = art_throw_array_bounds_from_code; |
| 430 | pThrowDivZeroFromCode = art_throw_div_zero_from_code; |
| 431 | pThrowNullPointerFromCode = art_throw_null_pointer_exception_from_code; |
Ian Rogers | 932746a | 2011-09-22 18:57:50 -0700 | [diff] [blame] | 432 | pThrowStackOverflowFromCode = art_throw_stack_overflow_from_code; |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 433 | pUnlockObjectFromCode = art_unlock_object_from_code; |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 434 | #endif |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 435 | pDeliverException = art_deliver_exception_from_code; |
buzbee | c396efc | 2011-09-11 09:36:41 -0700 | [diff] [blame] | 436 | pF2l = F2L; |
| 437 | pD2l = D2L; |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 438 | pAllocObjectFromCode = Class::AllocObjectFromCode; |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 439 | pMemcpy = memcpy; |
buzbee | e193174 | 2011-08-28 21:15:53 -0700 | [diff] [blame] | 440 | pGet32Static = Field::Get32StaticFromCode; |
| 441 | pSet32Static = Field::Set32StaticFromCode; |
| 442 | pGet64Static = Field::Get64StaticFromCode; |
| 443 | pSet64Static = Field::Set64StaticFromCode; |
| 444 | pGetObjStatic = Field::GetObjStaticFromCode; |
| 445 | pSetObjStatic = Field::SetObjStaticFromCode; |
buzbee | 1b4c859 | 2011-08-31 10:43:51 -0700 | [diff] [blame] | 446 | pInitializeTypeFromCode = InitializeTypeFromCode; |
buzbee | 561227c | 2011-09-02 15:28:19 -0700 | [diff] [blame] | 447 | pResolveMethodFromCode = ResolveMethodFromCode; |
buzbee | 2a475e7 | 2011-09-07 17:19:17 -0700 | [diff] [blame] | 448 | pInstanceofNonTrivialFromCode = Object::InstanceOf; |
buzbee | 2a475e7 | 2011-09-07 17:19:17 -0700 | [diff] [blame] | 449 | pLockObjectFromCode = LockObjectFromCode; |
Brian Carlstrom | 845490b | 2011-09-19 15:56:53 -0700 | [diff] [blame] | 450 | pFindInstanceFieldFromCode = Field::FindInstanceFieldFromCode; |
buzbee | c1f4504 | 2011-09-21 16:03:19 -0700 | [diff] [blame] | 451 | pCheckSuspendFromCode = artCheckSuspendFromCode; |
buzbee | 5ade1d2 | 2011-09-09 14:44:52 -0700 | [diff] [blame] | 452 | pThrowVerificationErrorFromCode = ThrowVerificationErrorFromCode; |
| 453 | pThrowNegArraySizeFromCode = ThrowNegArraySizeFromCode; |
| 454 | pThrowRuntimeExceptionFromCode = ThrowRuntimeExceptionFromCode; |
| 455 | pThrowInternalErrorFromCode = ThrowInternalErrorFromCode; |
| 456 | pThrowNoSuchMethodFromCode = ThrowNoSuchMethodFromCode; |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 457 | pThrowAbstractMethodErrorFromCode = ThrowAbstractMethodErrorFromCode; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 458 | pFindNativeMethod = FindNativeMethod; |
| 459 | pDecodeJObjectInThread = DecodeJObjectInThread; |
buzbee | 4a3164f | 2011-09-03 11:25:10 -0700 | [diff] [blame] | 460 | pDebugMe = DebugMe; |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 463 | void Frame::Next() { |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 464 | size_t frame_size = GetMethod()->GetFrameSizeInBytes(); |
| 465 | DCHECK_NE(frame_size, 0u); |
| 466 | DCHECK_LT(frame_size, 1024u); |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 467 | byte* next_sp = reinterpret_cast<byte*>(sp_) + frame_size; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 468 | sp_ = reinterpret_cast<Method**>(next_sp); |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 469 | if(*sp_ != NULL) { |
| 470 | DCHECK_EQ((*sp_)->GetClass(), Method::GetMethodClass()); |
| 471 | } |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Ian Rogers | 9086572 | 2011-09-19 11:11:44 -0700 | [diff] [blame] | 474 | bool Frame::HasMethod() const { |
| 475 | return GetMethod() != NULL && (!GetMethod()->IsPhony()); |
| 476 | } |
| 477 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 478 | uintptr_t Frame::GetReturnPC() const { |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 479 | byte* pc_addr = reinterpret_cast<byte*>(sp_) + GetMethod()->GetReturnPcOffsetInBytes(); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 480 | return *reinterpret_cast<uintptr_t*>(pc_addr); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 483 | uintptr_t Frame::LoadCalleeSave(int num) const { |
| 484 | // Callee saves are held at the top of the frame |
| 485 | Method* method = GetMethod(); |
| 486 | DCHECK(method != NULL); |
| 487 | size_t frame_size = method->GetFrameSizeInBytes(); |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 488 | byte* save_addr = reinterpret_cast<byte*>(sp_) + frame_size - ((num + 1) * kPointerSize); |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 489 | #if defined(__i386__) |
| 490 | save_addr -= kPointerSize; // account for return address |
| 491 | #endif |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 492 | return *reinterpret_cast<uintptr_t*>(save_addr); |
| 493 | } |
| 494 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 495 | Method* Frame::NextMethod() const { |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 496 | byte* next_sp = reinterpret_cast<byte*>(sp_) + |
Shih-wei Liao | d11af15 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 497 | GetMethod()->GetFrameSizeInBytes(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 498 | return *reinterpret_cast<Method**>(next_sp); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 501 | void* Thread::CreateCallback(void* arg) { |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 502 | Thread* self = reinterpret_cast<Thread*>(arg); |
| 503 | Runtime* runtime = Runtime::Current(); |
| 504 | |
| 505 | self->Attach(runtime); |
| 506 | |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 507 | String* thread_name = reinterpret_cast<String*>(gThread_name->GetObject(self->peer_)); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 508 | if (thread_name != NULL) { |
| 509 | SetThreadName(thread_name->ToModifiedUtf8().c_str()); |
| 510 | } |
| 511 | |
| 512 | // Wait until it's safe to start running code. (There may have been a suspend-all |
| 513 | // in progress while we were starting up.) |
| 514 | runtime->GetThreadList()->WaitForGo(); |
| 515 | |
| 516 | // TODO: say "hi" to the debugger. |
| 517 | //if (gDvm.debuggerConnected) { |
| 518 | // dvmDbgPostThreadStart(self); |
| 519 | //} |
| 520 | |
| 521 | // Invoke the 'run' method of our java.lang.Thread. |
| 522 | CHECK(self->peer_ != NULL); |
| 523 | Object* receiver = self->peer_; |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 524 | Method* m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(gThread_run); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 525 | m->Invoke(self, receiver, NULL, NULL); |
| 526 | |
| 527 | // Detach. |
| 528 | runtime->GetThreadList()->Unregister(); |
| 529 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 530 | return NULL; |
| 531 | } |
| 532 | |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 533 | void SetVmData(Object* managed_thread, Thread* native_thread) { |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 534 | gThread_vmData->SetInt(managed_thread, reinterpret_cast<uintptr_t>(native_thread)); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 535 | } |
| 536 | |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 537 | Thread* Thread::FromManagedThread(JNIEnv* env, jobject java_thread) { |
| 538 | Object* thread = Decode<Object*>(env, java_thread); |
| 539 | return reinterpret_cast<Thread*>(static_cast<uintptr_t>(gThread_vmData->GetInt(thread))); |
| 540 | } |
| 541 | |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 542 | void Thread::Create(Object* peer, size_t stack_size) { |
| 543 | CHECK(peer != NULL); |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 544 | |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 545 | if (stack_size == 0) { |
| 546 | stack_size = Runtime::Current()->GetDefaultStackSize(); |
| 547 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 548 | |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 549 | Thread* native_thread = new Thread; |
| 550 | native_thread->peer_ = peer; |
| 551 | |
| 552 | // Thread.start is synchronized, so we know that vmData is 0, |
| 553 | // and know that we're not racing to assign it. |
| 554 | SetVmData(peer, native_thread); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 555 | |
| 556 | pthread_attr_t attr; |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 557 | CHECK_PTHREAD_CALL(pthread_attr_init, (&attr), "new thread"); |
| 558 | CHECK_PTHREAD_CALL(pthread_attr_setdetachstate, (&attr, PTHREAD_CREATE_DETACHED), "PTHREAD_CREATE_DETACHED"); |
| 559 | CHECK_PTHREAD_CALL(pthread_attr_setstacksize, (&attr, stack_size), stack_size); |
| 560 | CHECK_PTHREAD_CALL(pthread_create, (&native_thread->pthread_, &attr, Thread::CreateCallback, native_thread), "new thread"); |
| 561 | CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attr), "new thread"); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 562 | |
| 563 | // Let the child know when it's safe to start running. |
| 564 | Runtime::Current()->GetThreadList()->SignalGo(native_thread); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 565 | } |
| 566 | |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 567 | void Thread::Attach(const Runtime* runtime) { |
| 568 | InitCpu(); |
| 569 | InitFunctionPointers(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 570 | |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 571 | thin_lock_id_ = Runtime::Current()->GetThreadList()->AllocThreadId(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 572 | |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 573 | tid_ = ::art::GetTid(); |
| 574 | pthread_ = pthread_self(); |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 575 | |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 576 | InitStackHwm(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 577 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 578 | CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, this), "attach"); |
Elliott Hughes | a5780da | 2011-07-17 11:39:39 -0700 | [diff] [blame] | 579 | |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 580 | jni_env_ = new JNIEnvExt(this, runtime->GetJavaVM()); |
Elliott Hughes | 330304d | 2011-08-12 14:28:05 -0700 | [diff] [blame] | 581 | |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 582 | runtime->GetThreadList()->Register(this); |
| 583 | } |
| 584 | |
| 585 | Thread* Thread::Attach(const Runtime* runtime, const char* name, bool as_daemon) { |
| 586 | Thread* self = new Thread; |
| 587 | self->Attach(runtime); |
| 588 | |
| 589 | self->SetState(Thread::kRunnable); |
| 590 | |
| 591 | SetThreadName(name); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 592 | |
| 593 | // If we're the main thread, ClassLinker won't be created until after we're attached, |
| 594 | // so that thread needs a two-stage attach. Regular threads don't need this hack. |
| 595 | if (self->thin_lock_id_ != ThreadList::kMainId) { |
| 596 | self->CreatePeer(name, as_daemon); |
| 597 | } |
| 598 | |
| 599 | return self; |
| 600 | } |
| 601 | |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 602 | jobject GetWellKnownThreadGroup(JNIEnv* env, const char* field_name) { |
| 603 | jclass thread_group_class = env->FindClass("java/lang/ThreadGroup"); |
| 604 | jfieldID fid = env->GetStaticFieldID(thread_group_class, field_name, "Ljava/lang/ThreadGroup;"); |
| 605 | jobject thread_group = env->GetStaticObjectField(thread_group_class, fid); |
| 606 | // This will be null in the compiler (and tests), but never in a running system. |
| 607 | //CHECK(thread_group != NULL) << "java.lang.ThreadGroup." << field_name << " not initialized"; |
| 608 | return thread_group; |
| 609 | } |
| 610 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 611 | void Thread::CreatePeer(const char* name, bool as_daemon) { |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 612 | Thread* self = Thread::Current(); |
| 613 | ScopedThreadStateChange tsc(self, Thread::kNative); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 614 | |
| 615 | JNIEnv* env = jni_env_; |
| 616 | |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 617 | const char* field_name = (GetThinLockId() == ThreadList::kMainId) ? "mMain" : "mSystem"; |
| 618 | jobject thread_group = GetWellKnownThreadGroup(env, field_name); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 619 | jobject thread_name = env->NewStringUTF(name); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 620 | jint thread_priority = GetNativePriority(); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 621 | jboolean thread_is_daemon = as_daemon; |
| 622 | |
| 623 | jclass c = env->FindClass("java/lang/Thread"); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 624 | 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] | 625 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 626 | jobject peer = env->NewObject(c, mid, thread_group, thread_name, thread_priority, thread_is_daemon); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 627 | peer_ = DecodeJObject(peer); |
| 628 | SetVmData(peer_, self); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 629 | |
| 630 | // Because we mostly run without code available (in the compiler, in tests), we |
| 631 | // manually assign the fields the constructor should have set. |
| 632 | // TODO: lose this. |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 633 | gThread_daemon->SetBoolean(peer_, thread_is_daemon); |
| 634 | gThread_group->SetObject(peer_, Decode<Object*>(env, thread_group)); |
| 635 | gThread_name->SetObject(peer_, Decode<Object*>(env, thread_name)); |
| 636 | gThread_priority->SetInt(peer_, thread_priority); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 637 | } |
| 638 | |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 639 | void Thread::InitStackHwm() { |
| 640 | pthread_attr_t attributes; |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 641 | CHECK_PTHREAD_CALL(pthread_getattr_np, (pthread_, &attributes), __FUNCTION__); |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 642 | |
Ian Rogers | 932746a | 2011-09-22 18:57:50 -0700 | [diff] [blame] | 643 | void* temp_stack_base; |
| 644 | CHECK_PTHREAD_CALL(pthread_attr_getstack, (&attributes, &temp_stack_base, &stack_size_), |
| 645 | __FUNCTION__); |
| 646 | stack_base_ = reinterpret_cast<byte*>(temp_stack_base); |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 647 | |
Ian Rogers | 932746a | 2011-09-22 18:57:50 -0700 | [diff] [blame] | 648 | if (stack_size_ <= kStackOverflowReservedBytes) { |
| 649 | LOG(FATAL) << "attempt to attach a thread with a too-small stack (" << stack_size_ << " bytes)"; |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 650 | } |
Elliott Hughes | 449b4bd | 2011-09-09 12:01:38 -0700 | [diff] [blame] | 651 | |
Ian Rogers | 932746a | 2011-09-22 18:57:50 -0700 | [diff] [blame] | 652 | // Set stack_end_ to the bottom of the stack saving space of stack overflows |
| 653 | ResetDefaultStackEnd(); |
Elliott Hughes | 449b4bd | 2011-09-09 12:01:38 -0700 | [diff] [blame] | 654 | |
| 655 | // Sanity check. |
| 656 | int stack_variable; |
| 657 | CHECK_GT(&stack_variable, (void*) stack_end_); |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 658 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 659 | CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attributes), __FUNCTION__); |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 660 | } |
| 661 | |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 662 | void Thread::Dump(std::ostream& os) const { |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 663 | DumpState(os); |
| 664 | DumpStack(os); |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 665 | } |
| 666 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 667 | std::string GetSchedulerGroup(pid_t tid) { |
| 668 | // /proc/<pid>/group looks like this: |
| 669 | // 2:devices:/ |
| 670 | // 1:cpuacct,cpu:/ |
| 671 | // We want the third field from the line whose second field contains the "cpu" token. |
| 672 | std::string cgroup_file; |
| 673 | if (!ReadFileToString("/proc/self/cgroup", &cgroup_file)) { |
| 674 | return ""; |
| 675 | } |
| 676 | std::vector<std::string> cgroup_lines; |
| 677 | Split(cgroup_file, '\n', cgroup_lines); |
| 678 | for (size_t i = 0; i < cgroup_lines.size(); ++i) { |
| 679 | std::vector<std::string> cgroup_fields; |
| 680 | Split(cgroup_lines[i], ':', cgroup_fields); |
| 681 | std::vector<std::string> cgroups; |
| 682 | Split(cgroup_fields[1], ',', cgroups); |
| 683 | for (size_t i = 0; i < cgroups.size(); ++i) { |
| 684 | if (cgroups[i] == "cpu") { |
| 685 | return cgroup_fields[2].substr(1); // Skip the leading slash. |
| 686 | } |
| 687 | } |
| 688 | } |
| 689 | return ""; |
| 690 | } |
| 691 | |
| 692 | void Thread::DumpState(std::ostream& os) const { |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 693 | std::string thread_name("<native thread without managed peer>"); |
| 694 | std::string group_name; |
| 695 | int priority; |
| 696 | bool is_daemon = false; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 697 | |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 698 | if (peer_ != NULL) { |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 699 | String* thread_name_string = reinterpret_cast<String*>(gThread_name->GetObject(peer_)); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 700 | thread_name = (thread_name_string != NULL) ? thread_name_string->ToModifiedUtf8() : "<null>"; |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 701 | priority = gThread_priority->GetInt(peer_); |
| 702 | is_daemon = gThread_daemon->GetBoolean(peer_); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 703 | |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 704 | Object* thread_group = gThread_group->GetObject(peer_); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 705 | if (thread_group != NULL) { |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 706 | String* group_name_string = reinterpret_cast<String*>(gThreadGroup_name->GetObject(thread_group)); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 707 | group_name = (group_name_string != NULL) ? group_name_string->ToModifiedUtf8() : "<null>"; |
| 708 | } |
| 709 | } else { |
| 710 | // 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] | 711 | std::string stats; |
| 712 | if (ReadFileToString(StringPrintf("/proc/self/task/%d/stat", GetTid()).c_str(), &stats)) { |
| 713 | size_t start = stats.find('(') + 1; |
| 714 | size_t end = stats.find(')') - start; |
| 715 | thread_name = stats.substr(start, end); |
| 716 | } |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 717 | priority = GetNativePriority(); |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 718 | } |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 719 | |
| 720 | int policy; |
| 721 | sched_param sp; |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 722 | CHECK_PTHREAD_CALL(pthread_getschedparam, (pthread_, &policy, &sp), __FUNCTION__); |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 723 | |
| 724 | std::string scheduler_group(GetSchedulerGroup(GetTid())); |
| 725 | if (scheduler_group.empty()) { |
| 726 | scheduler_group = "default"; |
| 727 | } |
| 728 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 729 | os << '"' << thread_name << '"'; |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 730 | if (is_daemon) { |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 731 | os << " daemon"; |
| 732 | } |
| 733 | os << " prio=" << priority |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 734 | << " tid=" << GetThinLockId() |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 735 | << " " << GetState() << "\n"; |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 736 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 737 | int debug_suspend_count = 0; // TODO |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 738 | os << " | group=\"" << group_name << "\"" |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 739 | << " sCount=" << suspend_count_ |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 740 | << " dsCount=" << debug_suspend_count |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 741 | << " obj=" << reinterpret_cast<void*>(peer_) |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 742 | << " self=" << reinterpret_cast<const void*>(this) << "\n"; |
| 743 | os << " | sysTid=" << GetTid() |
| 744 | << " nice=" << getpriority(PRIO_PROCESS, GetTid()) |
| 745 | << " sched=" << policy << "/" << sp.sched_priority |
| 746 | << " cgrp=" << scheduler_group |
| 747 | << " handle=" << GetImpl() << "\n"; |
| 748 | |
| 749 | // Grab the scheduler stats for this thread. |
| 750 | std::string scheduler_stats; |
| 751 | if (ReadFileToString(StringPrintf("/proc/self/task/%d/schedstat", GetTid()).c_str(), &scheduler_stats)) { |
| 752 | scheduler_stats.resize(scheduler_stats.size() - 1); // Lose the trailing '\n'. |
| 753 | } else { |
| 754 | scheduler_stats = "0 0 0"; |
| 755 | } |
| 756 | |
| 757 | int utime = 0; |
| 758 | int stime = 0; |
| 759 | int task_cpu = 0; |
| 760 | std::string stats; |
| 761 | if (ReadFileToString(StringPrintf("/proc/self/task/%d/stat", GetTid()).c_str(), &stats)) { |
| 762 | // Skip the command, which may contain spaces. |
| 763 | stats = stats.substr(stats.find(')') + 2); |
| 764 | // Extract the three fields we care about. |
| 765 | std::vector<std::string> fields; |
| 766 | Split(stats, ' ', fields); |
| 767 | utime = strtoull(fields[11].c_str(), NULL, 10); |
| 768 | stime = strtoull(fields[12].c_str(), NULL, 10); |
| 769 | task_cpu = strtoull(fields[36].c_str(), NULL, 10); |
| 770 | } |
| 771 | |
| 772 | os << " | schedstat=( " << scheduler_stats << " )" |
| 773 | << " utm=" << utime |
| 774 | << " stm=" << stime |
| 775 | << " core=" << task_cpu |
| 776 | << " HZ=" << sysconf(_SC_CLK_TCK) << "\n"; |
| 777 | } |
| 778 | |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 779 | struct StackDumpVisitor : public Thread::StackVisitor { |
| 780 | StackDumpVisitor(std::ostream& os) : os(os) { |
| 781 | } |
| 782 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 783 | virtual ~StackDumpVisitor() { |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 784 | } |
| 785 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 786 | void VisitFrame(const Frame& frame, uintptr_t pc) { |
Ian Rogers | 9086572 | 2011-09-19 11:11:44 -0700 | [diff] [blame] | 787 | if (!frame.HasMethod()) { |
| 788 | return; |
| 789 | } |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 790 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 791 | |
| 792 | Method* m = frame.GetMethod(); |
| 793 | Class* c = m->GetDeclaringClass(); |
| 794 | const DexFile& dex_file = class_linker->FindDexFile(c->GetDexCache()); |
| 795 | |
| 796 | os << " at " << PrettyMethod(m, false); |
| 797 | if (m->IsNative()) { |
| 798 | os << "(Native method)"; |
| 799 | } else { |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 800 | int line_number = dex_file.GetLineNumFromPC(m, m->ToDexPC(pc)); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 801 | os << "(" << c->GetSourceFile()->ToModifiedUtf8() << ":" << line_number << ")"; |
| 802 | } |
| 803 | os << "\n"; |
| 804 | } |
| 805 | |
| 806 | std::ostream& os; |
| 807 | }; |
| 808 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 809 | void Thread::DumpStack(std::ostream& os) const { |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 810 | StackDumpVisitor dumper(os); |
| 811 | WalkStack(&dumper); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 812 | } |
| 813 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 814 | Thread::State Thread::SetState(Thread::State new_state) { |
| 815 | Thread::State old_state = state_; |
| 816 | if (old_state == new_state) { |
| 817 | return old_state; |
| 818 | } |
| 819 | |
| 820 | volatile void* raw = reinterpret_cast<volatile void*>(&state_); |
| 821 | volatile int32_t* addr = reinterpret_cast<volatile int32_t*>(raw); |
| 822 | |
| 823 | if (new_state == Thread::kRunnable) { |
| 824 | /* |
| 825 | * Change our status to Thread::kRunnable. The transition requires |
| 826 | * that we check for pending suspension, because the VM considers |
| 827 | * us to be "asleep" in all other states, and another thread could |
| 828 | * be performing a GC now. |
| 829 | * |
| 830 | * The order of operations is very significant here. One way to |
| 831 | * do this wrong is: |
| 832 | * |
| 833 | * GCing thread Our thread (in kNative) |
| 834 | * ------------ ---------------------- |
| 835 | * check suspend count (== 0) |
| 836 | * SuspendAllThreads() |
| 837 | * grab suspend-count lock |
| 838 | * increment all suspend counts |
| 839 | * release suspend-count lock |
| 840 | * check thread state (== kNative) |
| 841 | * all are suspended, begin GC |
| 842 | * set state to kRunnable |
| 843 | * (continue executing) |
| 844 | * |
| 845 | * We can correct this by grabbing the suspend-count lock and |
| 846 | * performing both of our operations (check suspend count, set |
| 847 | * state) while holding it, now we need to grab a mutex on every |
| 848 | * transition to kRunnable. |
| 849 | * |
| 850 | * What we do instead is change the order of operations so that |
| 851 | * the transition to kRunnable happens first. If we then detect |
| 852 | * that the suspend count is nonzero, we switch to kSuspended. |
| 853 | * |
| 854 | * Appropriate compiler and memory barriers are required to ensure |
| 855 | * that the operations are observed in the expected order. |
| 856 | * |
| 857 | * This does create a small window of opportunity where a GC in |
| 858 | * progress could observe what appears to be a running thread (if |
| 859 | * it happens to look between when we set to kRunnable and when we |
| 860 | * switch to kSuspended). At worst this only affects assertions |
| 861 | * and thread logging. (We could work around it with some sort |
| 862 | * of intermediate "pre-running" state that is generally treated |
| 863 | * as equivalent to running, but that doesn't seem worthwhile.) |
| 864 | * |
| 865 | * We can also solve this by combining the "status" and "suspend |
| 866 | * count" fields into a single 32-bit value. This trades the |
| 867 | * store/load barrier on transition to kRunnable for an atomic RMW |
| 868 | * op on all transitions and all suspend count updates (also, all |
| 869 | * accesses to status or the thread count require bit-fiddling). |
| 870 | * It also eliminates the brief transition through kRunnable when |
| 871 | * the thread is supposed to be suspended. This is possibly faster |
| 872 | * on SMP and slightly more correct, but less convenient. |
| 873 | */ |
| 874 | android_atomic_acquire_store(new_state, addr); |
| 875 | if (ANNOTATE_UNPROTECTED_READ(suspend_count_) != 0) { |
| 876 | Runtime::Current()->GetThreadList()->FullSuspendCheck(this); |
| 877 | } |
| 878 | } else { |
| 879 | /* |
| 880 | * Not changing to Thread::kRunnable. No additional work required. |
| 881 | * |
| 882 | * We use a releasing store to ensure that, if we were runnable, |
| 883 | * any updates we previously made to objects on the managed heap |
| 884 | * will be observed before the state change. |
| 885 | */ |
| 886 | android_atomic_release_store(new_state, addr); |
| 887 | } |
| 888 | |
| 889 | return old_state; |
| 890 | } |
| 891 | |
| 892 | void Thread::WaitUntilSuspended() { |
| 893 | // TODO: dalvik dropped the waiting thread's priority after a while. |
| 894 | // TODO: dalvik timed out and aborted. |
| 895 | useconds_t delay = 0; |
| 896 | while (GetState() == Thread::kRunnable) { |
| 897 | useconds_t new_delay = delay * 2; |
| 898 | CHECK_GE(new_delay, delay); |
| 899 | delay = new_delay; |
| 900 | if (delay == 0) { |
| 901 | sched_yield(); |
| 902 | delay = 10000; |
| 903 | } else { |
| 904 | usleep(delay); |
| 905 | } |
| 906 | } |
| 907 | } |
| 908 | |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 909 | void Thread::ThreadExitCallback(void* arg) { |
| 910 | Thread* self = reinterpret_cast<Thread*>(arg); |
| 911 | LOG(FATAL) << "Native thread exited without calling DetachCurrentThread: " << *self; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 912 | } |
| 913 | |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 914 | void Thread::Startup() { |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 915 | // Allocate a TLS slot. |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 916 | CHECK_PTHREAD_CALL(pthread_key_create, (&Thread::pthread_key_self_, Thread::ThreadExitCallback), "self key"); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 917 | |
| 918 | // Double-check the TLS slot allocation. |
| 919 | if (pthread_getspecific(pthread_key_self_) != NULL) { |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 920 | LOG(FATAL) << "newly-created pthread TLS slot is not NULL"; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 921 | } |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 922 | } |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 923 | |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 924 | void Thread::FinishStartup() { |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 925 | // Now the ClassLinker is ready, we can find the various Class*, Field*, and Method*s we need. |
| 926 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 927 | Class* boolean_class = class_linker->FindPrimitiveClass('Z'); |
| 928 | Class* int_class = class_linker->FindPrimitiveClass('I'); |
| 929 | Class* String_class = class_linker->FindSystemClass("Ljava/lang/String;"); |
| 930 | Class* Thread_class = class_linker->FindSystemClass("Ljava/lang/Thread;"); |
| 931 | Class* ThreadGroup_class = class_linker->FindSystemClass("Ljava/lang/ThreadGroup;"); |
| 932 | Class* ThreadLock_class = class_linker->FindSystemClass("Ljava/lang/ThreadLock;"); |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 933 | Class* UncaughtExceptionHandler_class = class_linker->FindSystemClass("Ljava/lang/Thread$UncaughtExceptionHandler;"); |
| 934 | gThrowable = class_linker->FindSystemClass("Ljava/lang/Throwable;"); |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 935 | gThread_daemon = Thread_class->FindDeclaredInstanceField("daemon", boolean_class); |
| 936 | gThread_group = Thread_class->FindDeclaredInstanceField("group", ThreadGroup_class); |
| 937 | gThread_lock = Thread_class->FindDeclaredInstanceField("lock", ThreadLock_class); |
| 938 | gThread_name = Thread_class->FindDeclaredInstanceField("name", String_class); |
| 939 | gThread_priority = Thread_class->FindDeclaredInstanceField("priority", int_class); |
| 940 | gThread_run = Thread_class->FindVirtualMethod("run", "()V"); |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 941 | gThread_uncaughtHandler = Thread_class->FindDeclaredInstanceField("uncaughtHandler", UncaughtExceptionHandler_class); |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 942 | gThread_vmData = Thread_class->FindDeclaredInstanceField("vmData", int_class); |
| 943 | gThreadGroup_name = ThreadGroup_class->FindDeclaredInstanceField("name", String_class); |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 944 | gThreadGroup_removeThread = ThreadGroup_class->FindVirtualMethod("removeThread", "(Ljava/lang/Thread;)V"); |
| 945 | gUncaughtExceptionHandler_uncaughtException = |
| 946 | UncaughtExceptionHandler_class->FindVirtualMethod("uncaughtException", "(Ljava/lang/Thread;Ljava/lang/Throwable;)V"); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 947 | |
| 948 | // Finish attaching the main thread. |
| 949 | Thread::Current()->CreatePeer("main", false); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 950 | } |
| 951 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 952 | void Thread::Shutdown() { |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 953 | CHECK_PTHREAD_CALL(pthread_key_delete, (Thread::pthread_key_self_), "self key"); |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 954 | } |
| 955 | |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 956 | Thread::Thread() |
Elliott Hughes | 02b48d1 | 2011-09-07 17:15:51 -0700 | [diff] [blame] | 957 | : peer_(NULL), |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 958 | wait_mutex_(new Mutex("Thread wait mutex")), |
| 959 | wait_cond_(new ConditionVariable("Thread wait condition variable")), |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 960 | wait_monitor_(NULL), |
| 961 | interrupted_(false), |
Elliott Hughes | dc33ad5 | 2011-09-16 19:46:51 -0700 | [diff] [blame] | 962 | wait_next_(NULL), |
| 963 | card_table_(0), |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 964 | stack_end_(NULL), |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 965 | top_of_managed_stack_(), |
Elliott Hughes | dc33ad5 | 2011-09-16 19:46:51 -0700 | [diff] [blame] | 966 | top_of_managed_stack_pc_(0), |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 967 | native_to_managed_record_(NULL), |
| 968 | top_sirt_(NULL), |
| 969 | jni_env_(NULL), |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 970 | state_(Thread::kUnknown), |
Elliott Hughes | dc33ad5 | 2011-09-16 19:46:51 -0700 | [diff] [blame] | 971 | self_(NULL), |
| 972 | runtime_(NULL), |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 973 | exception_(NULL), |
| 974 | suspend_count_(0), |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 975 | class_loader_override_(NULL), |
| 976 | long_jump_context_(NULL) { |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 977 | } |
| 978 | |
Elliott Hughes | 02b48d1 | 2011-09-07 17:15:51 -0700 | [diff] [blame] | 979 | void MonitorExitVisitor(const Object* object, void*) { |
| 980 | Object* entered_monitor = const_cast<Object*>(object); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 981 | entered_monitor->MonitorExit(Thread::Current()); |
Elliott Hughes | 02b48d1 | 2011-09-07 17:15:51 -0700 | [diff] [blame] | 982 | } |
| 983 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 984 | Thread::~Thread() { |
Elliott Hughes | 02b48d1 | 2011-09-07 17:15:51 -0700 | [diff] [blame] | 985 | // On thread detach, all monitors entered with JNI MonitorEnter are automatically exited. |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 986 | if (jni_env_ != NULL) { |
| 987 | jni_env_->monitors.VisitRoots(MonitorExitVisitor, NULL); |
| 988 | } |
Elliott Hughes | 02b48d1 | 2011-09-07 17:15:51 -0700 | [diff] [blame] | 989 | |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 990 | if (peer_ != NULL) { |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 991 | Object* group = gThread_group->GetObject(peer_); |
| 992 | |
| 993 | // Handle any pending exception. |
| 994 | if (IsExceptionPending()) { |
| 995 | // Get and clear the exception. |
| 996 | Object* exception = GetException(); |
| 997 | ClearException(); |
| 998 | |
| 999 | // If the thread has its own handler, use that. |
| 1000 | Object* handler = gThread_uncaughtHandler->GetObject(peer_); |
| 1001 | if (handler == NULL) { |
| 1002 | // Otherwise use the thread group's default handler. |
| 1003 | handler = group; |
| 1004 | } |
| 1005 | |
| 1006 | // Call the handler. |
| 1007 | Method* m = handler->GetClass()->FindVirtualMethodForVirtualOrInterface(gUncaughtExceptionHandler_uncaughtException); |
| 1008 | Object* args[2]; |
| 1009 | args[0] = peer_; |
| 1010 | args[1] = exception; |
| 1011 | m->Invoke(this, handler, reinterpret_cast<byte*>(&args), NULL); |
| 1012 | |
| 1013 | // If the handler threw, clear that exception too. |
| 1014 | ClearException(); |
| 1015 | } |
| 1016 | |
| 1017 | // this.group.removeThread(this); |
Elliott Hughes | 081be7f | 2011-09-18 16:50:26 -0700 | [diff] [blame] | 1018 | // group can be null if we're in the compiler or a test. |
| 1019 | if (group != NULL) { |
| 1020 | Method* m = group->GetClass()->FindVirtualMethodForVirtualOrInterface(gThreadGroup_removeThread); |
| 1021 | Object* args = peer_; |
| 1022 | m->Invoke(this, group, reinterpret_cast<byte*>(&args), NULL); |
| 1023 | } |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 1024 | |
| 1025 | // this.vmData = 0; |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 1026 | SetVmData(peer_, NULL); |
Elliott Hughes | 02b48d1 | 2011-09-07 17:15:51 -0700 | [diff] [blame] | 1027 | |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 1028 | // TODO: say "bye" to the debugger. |
| 1029 | //if (gDvm.debuggerConnected) { |
| 1030 | // dvmDbgPostThreadDeath(self); |
| 1031 | //} |
Elliott Hughes | 02b48d1 | 2011-09-07 17:15:51 -0700 | [diff] [blame] | 1032 | |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 1033 | // Thread.join() is implemented as an Object.wait() on the Thread.lock |
| 1034 | // object. Signal anyone who is waiting. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 1035 | Thread* self = Thread::Current(); |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 1036 | Object* lock = gThread_lock->GetObject(peer_); |
| 1037 | // (This conditional is only needed for tests, where Thread.lock won't have been set.) |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 1038 | if (lock != NULL) { |
| 1039 | lock->MonitorEnter(self); |
| 1040 | lock->NotifyAll(); |
| 1041 | lock->MonitorExit(self); |
| 1042 | } |
| 1043 | } |
Elliott Hughes | 02b48d1 | 2011-09-07 17:15:51 -0700 | [diff] [blame] | 1044 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 1045 | delete jni_env_; |
Elliott Hughes | 02b48d1 | 2011-09-07 17:15:51 -0700 | [diff] [blame] | 1046 | jni_env_ = NULL; |
| 1047 | |
| 1048 | SetState(Thread::kTerminated); |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 1049 | |
| 1050 | delete wait_cond_; |
| 1051 | delete wait_mutex_; |
| 1052 | |
| 1053 | delete long_jump_context_; |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 1056 | size_t Thread::NumSirtReferences() { |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 1057 | size_t count = 0; |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 1058 | for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->Link()) { |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 1059 | count += cur->NumberOfReferences(); |
| 1060 | } |
| 1061 | return count; |
| 1062 | } |
| 1063 | |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 1064 | bool Thread::SirtContains(jobject obj) { |
| 1065 | Object** sirt_entry = reinterpret_cast<Object**>(obj); |
| 1066 | for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->Link()) { |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 1067 | size_t num_refs = cur->NumberOfReferences(); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 1068 | // A SIRT should always have a jobject/jclass as a native method is passed |
| 1069 | // in a this pointer or a class |
| 1070 | DCHECK_GT(num_refs, 0u); |
Shih-wei Liao | 2f0ce9d | 2011-09-01 02:07:58 -0700 | [diff] [blame] | 1071 | if ((&cur->References()[0] <= sirt_entry) && |
| 1072 | (sirt_entry <= (&cur->References()[num_refs - 1]))) { |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 1073 | return true; |
| 1074 | } |
| 1075 | } |
| 1076 | return false; |
| 1077 | } |
| 1078 | |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1079 | void Thread::PopSirt() { |
| 1080 | CHECK(top_sirt_ != NULL); |
| 1081 | top_sirt_ = top_sirt_->Link(); |
| 1082 | } |
| 1083 | |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 1084 | Object* Thread::DecodeJObject(jobject obj) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1085 | DCHECK(CanAccessDirectReferences()); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 1086 | if (obj == NULL) { |
| 1087 | return NULL; |
| 1088 | } |
| 1089 | IndirectRef ref = reinterpret_cast<IndirectRef>(obj); |
| 1090 | IndirectRefKind kind = GetIndirectRefKind(ref); |
| 1091 | Object* result; |
| 1092 | switch (kind) { |
| 1093 | case kLocal: |
| 1094 | { |
Elliott Hughes | 69f5bc6 | 2011-08-24 09:26:14 -0700 | [diff] [blame] | 1095 | IndirectReferenceTable& locals = jni_env_->locals; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 1096 | result = const_cast<Object*>(locals.Get(ref)); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 1097 | break; |
| 1098 | } |
| 1099 | case kGlobal: |
| 1100 | { |
| 1101 | JavaVMExt* vm = Runtime::Current()->GetJavaVM(); |
| 1102 | IndirectReferenceTable& globals = vm->globals; |
| 1103 | MutexLock mu(vm->globals_lock); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 1104 | result = const_cast<Object*>(globals.Get(ref)); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 1105 | break; |
| 1106 | } |
| 1107 | case kWeakGlobal: |
| 1108 | { |
| 1109 | JavaVMExt* vm = Runtime::Current()->GetJavaVM(); |
| 1110 | IndirectReferenceTable& weak_globals = vm->weak_globals; |
| 1111 | MutexLock mu(vm->weak_globals_lock); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 1112 | result = const_cast<Object*>(weak_globals.Get(ref)); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 1113 | if (result == kClearedJniWeakGlobal) { |
| 1114 | // This is a special case where it's okay to return NULL. |
| 1115 | return NULL; |
| 1116 | } |
| 1117 | break; |
| 1118 | } |
| 1119 | case kSirtOrInvalid: |
| 1120 | default: |
| 1121 | // TODO: make stack indirect reference table lookup more efficient |
| 1122 | // Check if this is a local reference in the SIRT |
| 1123 | if (SirtContains(obj)) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1124 | result = *reinterpret_cast<Object**>(obj); // Read from SIRT |
Elliott Hughes | c5bfa8f | 2011-08-30 14:32:49 -0700 | [diff] [blame] | 1125 | } else if (jni_env_->work_around_app_jni_bugs) { |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 1126 | // Assume an invalid local reference is actually a direct pointer. |
| 1127 | result = reinterpret_cast<Object*>(obj); |
| 1128 | } else { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1129 | result = kInvalidIndirectRefObject; |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 1130 | } |
| 1131 | } |
| 1132 | |
| 1133 | if (result == NULL) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1134 | LOG(ERROR) << "JNI ERROR (app bug): use of deleted " << kind << ": " << obj; |
| 1135 | JniAbort(NULL); |
| 1136 | } else { |
| 1137 | if (result != kInvalidIndirectRefObject) { |
| 1138 | Heap::VerifyObject(result); |
| 1139 | } |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 1140 | } |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 1141 | return result; |
| 1142 | } |
| 1143 | |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1144 | class CountStackDepthVisitor : public Thread::StackVisitor { |
| 1145 | public: |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 1146 | CountStackDepthVisitor() : depth_(0), skip_depth_(0), skipping_(true) {} |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 1147 | |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 1148 | virtual void VisitFrame(const Frame& frame, uintptr_t pc) { |
| 1149 | // We want to skip frames up to and including the exception's constructor. |
Ian Rogers | 9086572 | 2011-09-19 11:11:44 -0700 | [diff] [blame] | 1150 | // Note we also skip the frame if it doesn't have a method (namely the callee |
| 1151 | // save frame) |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 1152 | DCHECK(gThrowable != NULL); |
Ian Rogers | 9086572 | 2011-09-19 11:11:44 -0700 | [diff] [blame] | 1153 | if (skipping_ && frame.HasMethod() && !gThrowable->IsAssignableFrom(frame.GetMethod()->GetDeclaringClass())) { |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 1154 | skipping_ = false; |
| 1155 | } |
| 1156 | if (!skipping_) { |
| 1157 | ++depth_; |
| 1158 | } else { |
| 1159 | ++skip_depth_; |
| 1160 | } |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 1161 | } |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1162 | |
| 1163 | int GetDepth() const { |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1164 | return depth_; |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1165 | } |
| 1166 | |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 1167 | int GetSkipDepth() const { |
| 1168 | return skip_depth_; |
| 1169 | } |
| 1170 | |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1171 | private: |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1172 | uint32_t depth_; |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 1173 | uint32_t skip_depth_; |
| 1174 | bool skipping_; |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1175 | }; |
| 1176 | |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1177 | class BuildInternalStackTraceVisitor : public Thread::StackVisitor { |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1178 | public: |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 1179 | explicit BuildInternalStackTraceVisitor(int depth, int skip_depth, ScopedJniThreadState& ts) |
| 1180 | : skip_depth_(skip_depth), count_(0) { |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1181 | // Allocate method trace with an extra slot that will hold the PC trace |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 1182 | method_trace_ = Runtime::Current()->GetClassLinker()->AllocObjectArray<Object>(depth + 1); |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1183 | // Register a local reference as IntArray::Alloc may trigger GC |
| 1184 | local_ref_ = AddLocalReference<jobject>(ts.Env(), method_trace_); |
| 1185 | pc_trace_ = IntArray::Alloc(depth); |
| 1186 | #ifdef MOVING_GARBAGE_COLLECTOR |
| 1187 | // Re-read after potential GC |
| 1188 | method_trace = Decode<ObjectArray<Object>*>(ts.Env(), local_ref_); |
| 1189 | #endif |
| 1190 | // Save PC trace in last element of method trace, also places it into the |
| 1191 | // object graph. |
| 1192 | method_trace_->Set(depth, pc_trace_); |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1193 | } |
| 1194 | |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1195 | virtual ~BuildInternalStackTraceVisitor() {} |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1196 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1197 | virtual void VisitFrame(const Frame& frame, uintptr_t pc) { |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 1198 | if (skip_depth_ > 0) { |
| 1199 | skip_depth_--; |
| 1200 | return; |
| 1201 | } |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1202 | method_trace_->Set(count_, frame.GetMethod()); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1203 | pc_trace_->Set(count_, pc); |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1204 | ++count_; |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1205 | } |
| 1206 | |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1207 | jobject GetInternalStackTrace() const { |
| 1208 | return local_ref_; |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | private: |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 1212 | // How many more frames to skip. |
| 1213 | int32_t skip_depth_; |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1214 | // Current position down stack trace |
| 1215 | uint32_t count_; |
| 1216 | // Array of return PC values |
| 1217 | IntArray* pc_trace_; |
| 1218 | // An array of the methods on the stack, the last entry is a reference to the |
| 1219 | // PC trace |
| 1220 | ObjectArray<Object>* method_trace_; |
| 1221 | // Local indirect reference table entry for method trace |
| 1222 | jobject local_ref_; |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1223 | }; |
| 1224 | |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1225 | void Thread::WalkStack(StackVisitor* visitor) const { |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 1226 | Frame frame = GetTopOfStack(); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1227 | uintptr_t pc = top_of_managed_stack_pc_; |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1228 | // TODO: enable this CHECK after native_to_managed_record_ is initialized during startup. |
| 1229 | // CHECK(native_to_managed_record_ != NULL); |
| 1230 | NativeToManagedRecord* record = native_to_managed_record_; |
| 1231 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1232 | while (frame.GetSP() != 0) { |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1233 | for ( ; frame.GetMethod() != 0; frame.Next()) { |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1234 | DCHECK(frame.GetMethod()->IsWithinCode(pc)); |
| 1235 | visitor->VisitFrame(frame, pc); |
| 1236 | pc = frame.GetReturnPC(); |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1237 | } |
| 1238 | if (record == NULL) { |
| 1239 | break; |
| 1240 | } |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1241 | // last_tos should return Frame instead of sp? |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1242 | frame.SetSP(reinterpret_cast<Method**>(record->last_top_of_managed_stack_)); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1243 | pc = record->last_top_of_managed_stack_pc_; |
| 1244 | record = record->link_; |
| 1245 | } |
| 1246 | } |
| 1247 | |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1248 | void Thread::WalkStackUntilUpCall(StackVisitor* visitor, bool include_upcall) const { |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1249 | Frame frame = GetTopOfStack(); |
| 1250 | uintptr_t pc = top_of_managed_stack_pc_; |
| 1251 | |
| 1252 | if (frame.GetSP() != 0) { |
| 1253 | for ( ; frame.GetMethod() != 0; frame.Next()) { |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1254 | DCHECK(frame.GetMethod()->IsWithinCode(pc)); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1255 | visitor->VisitFrame(frame, pc); |
| 1256 | pc = frame.GetReturnPC(); |
| 1257 | } |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1258 | if (include_upcall) { |
| 1259 | visitor->VisitFrame(frame, pc); |
| 1260 | } |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1261 | } |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 1262 | } |
| 1263 | |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 1264 | jobject Thread::CreateInternalStackTrace(JNIEnv* env) const { |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1265 | // Compute depth of stack |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1266 | CountStackDepthVisitor count_visitor; |
| 1267 | WalkStack(&count_visitor); |
| 1268 | int32_t depth = count_visitor.GetDepth(); |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 1269 | int32_t skip_depth = count_visitor.GetSkipDepth(); |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 1270 | |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1271 | // Transition into runnable state to work on Object*/Array* |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 1272 | ScopedJniThreadState ts(env); |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1273 | |
| 1274 | // Build internal stack trace |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 1275 | BuildInternalStackTraceVisitor build_trace_visitor(depth, skip_depth, ts); |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1276 | WalkStack(&build_trace_visitor); |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 1277 | |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1278 | return build_trace_visitor.GetInternalStackTrace(); |
| 1279 | } |
| 1280 | |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 1281 | jobjectArray Thread::InternalStackTraceToStackTraceElementArray(JNIEnv* env, jobject internal, |
| 1282 | jobjectArray output_array, int* stack_depth) { |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1283 | // Transition into runnable state to work on Object*/Array* |
| 1284 | ScopedJniThreadState ts(env); |
| 1285 | |
| 1286 | // Decode the internal stack trace into the depth, method trace and PC trace |
| 1287 | ObjectArray<Object>* method_trace = |
| 1288 | down_cast<ObjectArray<Object>*>(Decode<Object*>(ts.Env(), internal)); |
| 1289 | int32_t depth = method_trace->GetLength()-1; |
| 1290 | IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth)); |
| 1291 | |
| 1292 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 1293 | |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 1294 | jobjectArray result; |
| 1295 | ObjectArray<StackTraceElement>* java_traces; |
| 1296 | if (output_array != NULL) { |
| 1297 | // Reuse the array we were given. |
| 1298 | result = output_array; |
| 1299 | java_traces = reinterpret_cast<ObjectArray<StackTraceElement>*>(Decode<Array*>(env, |
| 1300 | output_array)); |
| 1301 | // ...adjusting the number of frames we'll write to not exceed the array length. |
| 1302 | depth = std::min(depth, java_traces->GetLength()); |
| 1303 | } else { |
| 1304 | // Create java_trace array and place in local reference table |
| 1305 | java_traces = class_linker->AllocStackTraceElementArray(depth); |
| 1306 | result = AddLocalReference<jobjectArray>(ts.Env(), java_traces); |
| 1307 | } |
| 1308 | |
| 1309 | if (stack_depth != NULL) { |
| 1310 | *stack_depth = depth; |
| 1311 | } |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 1312 | |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1313 | for (int32_t i = 0; i < depth; ++i) { |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1314 | // Prepare parameters for StackTraceElement(String cls, String method, String file, int line) |
| 1315 | Method* method = down_cast<Method*>(method_trace->Get(i)); |
| 1316 | uint32_t native_pc = pc_trace->Get(i); |
| 1317 | Class* klass = method->GetDeclaringClass(); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 1318 | const DexFile& dex_file = class_linker->FindDexFile(klass->GetDexCache()); |
Elliott Hughes | 3893357 | 2011-09-16 12:29:03 -0700 | [diff] [blame] | 1319 | std::string class_name(PrettyDescriptor(klass->GetDescriptor())); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 1320 | |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1321 | // Allocate element, potentially triggering GC |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 1322 | StackTraceElement* obj = |
Elliott Hughes | 3893357 | 2011-09-16 12:29:03 -0700 | [diff] [blame] | 1323 | StackTraceElement::Alloc(String::AllocFromModifiedUtf8(class_name.c_str()), |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 1324 | method->GetName(), |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 1325 | klass->GetSourceFile(), |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 1326 | dex_file.GetLineNumFromPC(method, |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1327 | method->ToDexPC(native_pc))); |
| 1328 | #ifdef MOVING_GARBAGE_COLLECTOR |
| 1329 | // Re-read after potential GC |
| 1330 | java_traces = Decode<ObjectArray<Object>*>(ts.Env(), result); |
| 1331 | method_trace = down_cast<ObjectArray<Object>*>(Decode<Object*>(ts.Env(), internal)); |
| 1332 | pc_trace = down_cast<IntArray*>(method_trace->Get(depth)); |
| 1333 | #endif |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 1334 | java_traces->Set(i, obj); |
| 1335 | } |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1336 | return result; |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 1337 | } |
| 1338 | |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 1339 | void Thread::ThrowNewException(const char* exception_class_descriptor, const char* fmt, ...) { |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1340 | va_list args; |
| 1341 | va_start(args, fmt); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 1342 | ThrowNewExceptionV(exception_class_descriptor, fmt, args); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1343 | va_end(args); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 1344 | } |
| 1345 | |
| 1346 | void Thread::ThrowNewExceptionV(const char* exception_class_descriptor, const char* fmt, va_list ap) { |
| 1347 | std::string msg; |
| 1348 | StringAppendV(&msg, fmt, ap); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1349 | |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 1350 | // Convert "Ljava/lang/Exception;" into JNI-style "java/lang/Exception". |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1351 | CHECK_EQ('L', exception_class_descriptor[0]); |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 1352 | std::string descriptor(exception_class_descriptor + 1); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1353 | CHECK_EQ(';', descriptor[descriptor.length() - 1]); |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 1354 | descriptor.erase(descriptor.length() - 1); |
| 1355 | |
| 1356 | JNIEnv* env = GetJniEnv(); |
| 1357 | jclass exception_class = env->FindClass(descriptor.c_str()); |
| 1358 | CHECK(exception_class != NULL) << "descriptor=\"" << descriptor << "\""; |
| 1359 | int rc = env->ThrowNew(exception_class, msg.c_str()); |
| 1360 | CHECK_EQ(rc, JNI_OK); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1361 | } |
| 1362 | |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 1363 | void Thread::ThrowOutOfMemoryError() { |
| 1364 | UNIMPLEMENTED(FATAL); |
| 1365 | } |
| 1366 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1367 | class CatchBlockStackVisitor : public Thread::StackVisitor { |
| 1368 | public: |
| 1369 | CatchBlockStackVisitor(Class* to_find, Context* ljc) |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1370 | : found_(false), to_find_(to_find), long_jump_context_(ljc), native_method_count_(0) { |
| 1371 | #ifndef NDEBUG |
| 1372 | handler_pc_ = 0xEBADC0DE; |
| 1373 | handler_frame_.SetSP(reinterpret_cast<Method**>(0xEBADF00D)); |
| 1374 | #endif |
| 1375 | } |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 1376 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1377 | virtual void VisitFrame(const Frame& fr, uintptr_t pc) { |
| 1378 | if (!found_) { |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1379 | Method* method = fr.GetMethod(); |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1380 | if (method == NULL) { |
| 1381 | // This is the upcall, we remember the frame and last_pc so that we may |
| 1382 | // long jump to them |
| 1383 | handler_pc_ = pc; |
| 1384 | handler_frame_ = fr; |
| 1385 | return; |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1386 | } |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1387 | uint32_t dex_pc = DexFile::kDexNoIndex; |
Ian Rogers | 9086572 | 2011-09-19 11:11:44 -0700 | [diff] [blame] | 1388 | if (method->IsPhony()) { |
| 1389 | // ignore callee save method |
| 1390 | } else if (method->IsNative()) { |
| 1391 | native_method_count_++; |
| 1392 | } else { |
| 1393 | // Move the PC back 2 bytes as a call will frequently terminate the |
| 1394 | // decoding of a particular instruction and we want to make sure we |
| 1395 | // get the Dex PC of the instruction with the call and not the |
| 1396 | // instruction following. |
| 1397 | pc -= 2; |
| 1398 | dex_pc = method->ToDexPC(pc); |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1399 | } |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1400 | if (dex_pc != DexFile::kDexNoIndex) { |
| 1401 | uint32_t found_dex_pc = method->FindCatchBlock(to_find_, dex_pc); |
| 1402 | if (found_dex_pc != DexFile::kDexNoIndex) { |
| 1403 | found_ = true; |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1404 | handler_pc_ = method->ToNativePC(found_dex_pc); |
| 1405 | handler_frame_ = fr; |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1406 | } |
| 1407 | } |
| 1408 | if (!found_) { |
| 1409 | // Caller may be handler, fill in callee saves in context |
| 1410 | long_jump_context_->FillCalleeSaves(fr); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 1411 | } |
| 1412 | } |
| 1413 | } |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1414 | |
| 1415 | // Did we find a catch block yet? |
| 1416 | bool found_; |
| 1417 | // The type of the exception catch block to find |
| 1418 | Class* to_find_; |
| 1419 | // Frame with found handler or last frame if no handler found |
| 1420 | Frame handler_frame_; |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1421 | // PC to branch to for the handler |
| 1422 | uintptr_t handler_pc_; |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1423 | // Context that will be the target of the long jump |
| 1424 | Context* long_jump_context_; |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1425 | // Number of native methods passed in crawl (equates to number of SIRTs to pop) |
| 1426 | uint32_t native_method_count_; |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1427 | }; |
| 1428 | |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1429 | void Thread::DeliverException() { |
| 1430 | Throwable *exception = GetException(); // Set exception on thread |
| 1431 | CHECK(exception != NULL); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1432 | |
| 1433 | Context* long_jump_context = GetLongJumpContext(); |
| 1434 | CatchBlockStackVisitor catch_finder(exception->GetClass(), long_jump_context); |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1435 | WalkStackUntilUpCall(&catch_finder, true); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1436 | |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1437 | // Pop any SIRT |
| 1438 | if (catch_finder.native_method_count_ == 1) { |
| 1439 | PopSirt(); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1440 | } else { |
Ian Rogers | ad42e13 | 2011-09-17 20:23:33 -0700 | [diff] [blame] | 1441 | // We only expect the stack crawl to have passed 1 native method as it's terminated |
| 1442 | // by an up call |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1443 | DCHECK_EQ(catch_finder.native_method_count_, 0u); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1444 | } |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1445 | long_jump_context->SetSP(reinterpret_cast<intptr_t>(catch_finder.handler_frame_.GetSP())); |
| 1446 | long_jump_context->SetPC(catch_finder.handler_pc_); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1447 | long_jump_context->DoLongJump(); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 1448 | } |
| 1449 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1450 | Context* Thread::GetLongJumpContext() { |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 1451 | Context* result = long_jump_context_; |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1452 | if (result == NULL) { |
| 1453 | result = Context::Create(); |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 1454 | long_jump_context_ = result; |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 1455 | } |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1456 | return result; |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 1457 | } |
| 1458 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 1459 | bool Thread::HoldsLock(Object* object) { |
| 1460 | if (object == NULL) { |
| 1461 | return false; |
| 1462 | } |
| 1463 | return object->GetLockOwner() == thin_lock_id_; |
| 1464 | } |
| 1465 | |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 1466 | bool Thread::IsDaemon() { |
| 1467 | return gThread_daemon->GetBoolean(peer_); |
| 1468 | } |
| 1469 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 1470 | void Thread::VisitRoots(Heap::RootVisitor* visitor, void* arg) const { |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 1471 | if (exception_ != NULL) { |
| 1472 | visitor(exception_, arg); |
| 1473 | } |
| 1474 | if (peer_ != NULL) { |
| 1475 | visitor(peer_, arg); |
| 1476 | } |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 1477 | jni_env_->locals.VisitRoots(visitor, arg); |
| 1478 | jni_env_->monitors.VisitRoots(visitor, arg); |
| 1479 | // visitThreadStack(visitor, thread, arg); |
| 1480 | UNIMPLEMENTED(WARNING) << "some per-Thread roots not visited"; |
| 1481 | } |
| 1482 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 1483 | static const char* kStateNames[] = { |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 1484 | "Terminated", |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 1485 | "Runnable", |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 1486 | "TimedWaiting", |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 1487 | "Blocked", |
| 1488 | "Waiting", |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 1489 | "Initializing", |
| 1490 | "Starting", |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 1491 | "Native", |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 1492 | "VmWait", |
| 1493 | "Suspended", |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 1494 | }; |
| 1495 | std::ostream& operator<<(std::ostream& os, const Thread::State& state) { |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 1496 | int int_state = static_cast<int>(state); |
| 1497 | if (state >= Thread::kTerminated && state <= Thread::kSuspended) { |
| 1498 | os << kStateNames[int_state]; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 1499 | } else { |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 1500 | os << "State[" << int_state << "]"; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 1501 | } |
| 1502 | return os; |
| 1503 | } |
| 1504 | |
Elliott Hughes | 330304d | 2011-08-12 14:28:05 -0700 | [diff] [blame] | 1505 | std::ostream& operator<<(std::ostream& os, const Thread& thread) { |
| 1506 | os << "Thread[" << &thread |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 1507 | << ",pthread_t=" << thread.GetImpl() |
| 1508 | << ",tid=" << thread.GetTid() |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 1509 | << ",id=" << thread.GetThinLockId() |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 1510 | << ",state=" << thread.GetState() |
| 1511 | << ",peer=" << thread.GetPeer() |
| 1512 | << "]"; |
Elliott Hughes | 330304d | 2011-08-12 14:28:05 -0700 | [diff] [blame] | 1513 | return os; |
| 1514 | } |
| 1515 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 1516 | } // namespace art |