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