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