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