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> |
Elliott Hughes | 2acf36d | 2012-04-17 13:30:13 -0700 | [diff] [blame] | 21 | #include <signal.h> |
Brian Carlstrom | dbf05b7 | 2011-12-15 00:55:24 -0800 | [diff] [blame] | 22 | #include <sys/resource.h> |
| 23 | #include <sys/time.h> |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 24 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 25 | #include <algorithm> |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 26 | #include <bitset> |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 27 | #include <cerrno> |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 28 | #include <iostream> |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 29 | #include <list> |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 30 | |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 31 | #include "class_linker.h" |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 32 | #include "class_loader.h" |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 33 | #include "debugger.h" |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 34 | #include "heap.h" |
Elliott Hughes | c5f7c91 | 2011-08-18 14:00:42 -0700 | [diff] [blame] | 35 | #include "jni_internal.h" |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 36 | #include "monitor.h" |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 37 | #include "oat/runtime/context.h" |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 38 | #include "object.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 39 | #include "object_utils.h" |
Jesse Wilson | 9a6bae8 | 2011-11-14 14:57:30 -0500 | [diff] [blame] | 40 | #include "reflection.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 41 | #include "runtime.h" |
buzbee | 5433072 | 2011-08-23 16:46:55 -0700 | [diff] [blame] | 42 | #include "runtime_support.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 43 | #include "scoped_thread_state_change.h" |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 44 | #include "ScopedLocalRef.h" |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 45 | #include "space.h" |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 46 | #include "stack.h" |
| 47 | #include "stack_indirect_reference_table.h" |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 48 | #include "thread_list.h" |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 49 | #include "utils.h" |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 50 | #include "verifier/gc_map.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 51 | #include "well_known_classes.h" |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 52 | |
| 53 | namespace art { |
| 54 | |
| 55 | pthread_key_t Thread::pthread_key_self_; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 56 | ConditionVariable* Thread::resume_cond_; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 57 | |
Elliott Hughes | 7dc5166 | 2012-05-16 14:48:43 -0700 | [diff] [blame] | 58 | static const char* kThreadNameDuringStartup = "<native thread without managed peer>"; |
| 59 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 60 | void Thread::InitCardTable() { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 61 | card_table_ = Runtime::Current()->GetHeap()->GetCardTable()->GetBiasedBegin(); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Elliott Hughes | 99250ba | 2012-04-17 11:09:17 -0700 | [diff] [blame] | 64 | #if !defined(__APPLE__) |
Elliott Hughes | 3ea0f42 | 2012-04-16 17:01:43 -0700 | [diff] [blame] | 65 | static void UnimplementedEntryPoint() { |
| 66 | UNIMPLEMENTED(FATAL); |
| 67 | } |
Elliott Hughes | 99250ba | 2012-04-17 11:09:17 -0700 | [diff] [blame] | 68 | #endif |
Elliott Hughes | 3ea0f42 | 2012-04-16 17:01:43 -0700 | [diff] [blame] | 69 | |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 70 | void Thread::InitFunctionPointers() { |
Elliott Hughes | 99250ba | 2012-04-17 11:09:17 -0700 | [diff] [blame] | 71 | #if !defined(__APPLE__) // The Mac GCC is too old to accept this code. |
Elliott Hughes | 3ea0f42 | 2012-04-16 17:01:43 -0700 | [diff] [blame] | 72 | // Insert a placeholder so we can easily tell if we call an unimplemented entry point. |
| 73 | uintptr_t* begin = reinterpret_cast<uintptr_t*>(&entrypoints_); |
| 74 | uintptr_t* end = reinterpret_cast<uintptr_t*>(reinterpret_cast<uint8_t*>(begin) + sizeof(entrypoints_)); |
| 75 | for (uintptr_t* it = begin; it != end; ++it) { |
| 76 | *it = reinterpret_cast<uintptr_t>(UnimplementedEntryPoint); |
| 77 | } |
Elliott Hughes | 99250ba | 2012-04-17 11:09:17 -0700 | [diff] [blame] | 78 | #endif |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 79 | InitEntryPoints(&entrypoints_); |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void Thread::SetDebuggerUpdatesEnabled(bool enabled) { |
| 83 | LOG(INFO) << "Turning debugger updates " << (enabled ? "on" : "off") << " for " << *this; |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 84 | #if !defined(ART_USE_LLVM_COMPILER) |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 85 | ChangeDebuggerEntryPoint(&entrypoints_, enabled); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 86 | #else |
| 87 | UNIMPLEMENTED(FATAL); |
| 88 | #endif |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 91 | void Thread::InitTid() { |
| 92 | tid_ = ::art::GetTid(); |
| 93 | } |
| 94 | |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 95 | void Thread::InitAfterFork() { |
Elliott Hughes | 8029cbe | 2012-05-22 09:13:08 -0700 | [diff] [blame] | 96 | // One thread (us) survived the fork, but we have a new tid so we need to |
| 97 | // update the value stashed in this Thread*. |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 98 | InitTid(); |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 101 | void* Thread::CreateCallback(void* arg) { |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 102 | Thread* self = reinterpret_cast<Thread*>(arg); |
Elliott Hughes | 462c944 | 2012-03-23 18:47:50 -0700 | [diff] [blame] | 103 | self->Init(); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 104 | |
Elliott Hughes | 47179f7 | 2011-10-27 16:44:39 -0700 | [diff] [blame] | 105 | { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 106 | ScopedObjectAccess soa(self); |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 107 | { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 108 | SirtRef<String> thread_name(self->GetThreadName(soa)); |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 109 | self->SetThreadName(thread_name->ToModifiedUtf8().c_str()); |
| 110 | } |
| 111 | |
| 112 | Dbg::PostThreadStart(self); |
| 113 | |
| 114 | // Invoke the 'run' method of our java.lang.Thread. |
| 115 | CHECK(self->peer_ != NULL); |
| 116 | Object* receiver = self->peer_; |
| 117 | jmethodID mid = WellKnownClasses::java_lang_Thread_run; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 118 | Method* m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(soa.DecodeMethod(mid)); |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 119 | m->Invoke(self, receiver, NULL, NULL); |
Elliott Hughes | 47179f7 | 2011-10-27 16:44:39 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 122 | // Detach and delete self. |
| 123 | Runtime::Current()->GetThreadList()->Unregister(self); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 124 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 125 | return NULL; |
| 126 | } |
| 127 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 128 | static void SetVmData(const ScopedObjectAccess& soa, Object* managed_thread, |
| 129 | Thread* native_thread) |
| 130 | SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) { |
| 131 | Field* f = soa.DecodeField(WellKnownClasses::java_lang_Thread_vmData); |
Elliott Hughes | af8d15a | 2012-05-29 09:12:18 -0700 | [diff] [blame] | 132 | f->SetInt(managed_thread, reinterpret_cast<uintptr_t>(native_thread)); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 135 | Thread* Thread::FromManagedThread(const ScopedObjectAccessUnchecked& soa, Object* thread_peer) { |
| 136 | Field* f = soa.DecodeField(WellKnownClasses::java_lang_Thread_vmData); |
| 137 | Thread* result = reinterpret_cast<Thread*>(static_cast<uintptr_t>(f->GetInt(thread_peer))); |
| 138 | // Sanity check that if we have a result it is either suspended or we hold the thread_list_lock_ |
| 139 | // to stop it from going away. |
| 140 | MutexLock mu(*GlobalSynchronization::thread_suspend_count_lock_); |
| 141 | if (result != NULL && !result->IsSuspended()) { |
| 142 | GlobalSynchronization::thread_list_lock_->AssertHeld(); |
| 143 | } |
| 144 | return result; |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 147 | Thread* Thread::FromManagedThread(const ScopedObjectAccessUnchecked& soa, jobject java_thread) { |
| 148 | return FromManagedThread(soa, soa.Decode<Object*>(java_thread)); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Elliott Hughes | ab7b9dc | 2012-03-27 13:16:29 -0700 | [diff] [blame] | 151 | static size_t FixStackSize(size_t stack_size) { |
Elliott Hughes | 7502e2a | 2011-10-02 13:24:37 -0700 | [diff] [blame] | 152 | // A stack size of zero means "use the default". |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 153 | if (stack_size == 0) { |
| 154 | stack_size = Runtime::Current()->GetDefaultStackSize(); |
| 155 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 156 | |
Brian Carlstrom | 6414a97 | 2012-04-14 14:20:04 -0700 | [diff] [blame] | 157 | // Dalvik used the bionic pthread default stack size for native threads, |
| 158 | // so include that here to support apps that expect large native stacks. |
| 159 | stack_size += 1 * MB; |
| 160 | |
Elliott Hughes | 7502e2a | 2011-10-02 13:24:37 -0700 | [diff] [blame] | 161 | // It's not possible to request a stack smaller than the system-defined PTHREAD_STACK_MIN. |
| 162 | if (stack_size < PTHREAD_STACK_MIN) { |
| 163 | stack_size = PTHREAD_STACK_MIN; |
| 164 | } |
| 165 | |
| 166 | // It's likely that callers are trying to ensure they have at least a certain amount of |
| 167 | // stack space, so we should add our reserved space on top of what they requested, rather |
| 168 | // than implicitly take it away from them. |
| 169 | stack_size += Thread::kStackOverflowReservedBytes; |
| 170 | |
| 171 | // Some systems require the stack size to be a multiple of the system page size, so round up. |
| 172 | stack_size = RoundUp(stack_size, kPageSize); |
| 173 | |
| 174 | return stack_size; |
| 175 | } |
| 176 | |
Elliott Hughes | d8af159 | 2012-04-16 20:40:15 -0700 | [diff] [blame] | 177 | static void SigAltStack(stack_t* new_stack, stack_t* old_stack) { |
| 178 | if (sigaltstack(new_stack, old_stack) == -1) { |
| 179 | PLOG(FATAL) << "sigaltstack failed"; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | static void SetUpAlternateSignalStack() { |
| 184 | // Create and set an alternate signal stack. |
| 185 | stack_t ss; |
| 186 | ss.ss_sp = new uint8_t[SIGSTKSZ]; |
| 187 | ss.ss_size = SIGSTKSZ; |
| 188 | ss.ss_flags = 0; |
| 189 | CHECK(ss.ss_sp != NULL); |
| 190 | SigAltStack(&ss, NULL); |
| 191 | |
| 192 | // Double-check that it worked. |
| 193 | ss.ss_sp = NULL; |
| 194 | SigAltStack(NULL, &ss); |
| 195 | VLOG(threads) << "Alternate signal stack is " << PrettySize(ss.ss_size) << " at " << ss.ss_sp; |
| 196 | } |
| 197 | |
| 198 | static void TearDownAlternateSignalStack() { |
| 199 | // Get the pointer so we can free the memory. |
| 200 | stack_t ss; |
| 201 | SigAltStack(NULL, &ss); |
| 202 | uint8_t* allocated_signal_stack = reinterpret_cast<uint8_t*>(ss.ss_sp); |
| 203 | |
| 204 | // Tell the kernel to stop using it. |
| 205 | ss.ss_sp = NULL; |
| 206 | ss.ss_flags = SS_DISABLE; |
Elliott Hughes | 4c5231d | 2012-04-18 16:54:31 -0700 | [diff] [blame] | 207 | ss.ss_size = SIGSTKSZ; // Avoid ENOMEM failure with Mac OS' buggy libc. |
Elliott Hughes | d8af159 | 2012-04-16 20:40:15 -0700 | [diff] [blame] | 208 | SigAltStack(&ss, NULL); |
| 209 | |
| 210 | // Free it. |
| 211 | delete[] allocated_signal_stack; |
| 212 | } |
| 213 | |
Ian Rogers | 52673ff | 2012-06-27 23:25:34 -0700 | [diff] [blame] | 214 | void Thread::CreateNativeThread(JNIEnv* env, jobject java_peer, size_t stack_size, bool daemon) { |
| 215 | Thread* native_thread = new Thread(daemon); |
Elliott Hughes | 47179f7 | 2011-10-27 16:44:39 -0700 | [diff] [blame] | 216 | { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 217 | ScopedObjectAccess soa(env); |
| 218 | Object* peer = soa.Decode<Object*>(java_peer); |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 219 | CHECK(peer != NULL); |
| 220 | native_thread->peer_ = peer; |
| 221 | |
| 222 | stack_size = FixStackSize(stack_size); |
| 223 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 224 | // Thread.start is synchronized, so we know that vmData is 0, and know that we're not racing to |
| 225 | // assign it. |
| 226 | SetVmData(soa, peer, native_thread); |
Elliott Hughes | 47179f7 | 2011-10-27 16:44:39 -0700 | [diff] [blame] | 227 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 228 | |
| 229 | pthread_t new_pthread; |
| 230 | pthread_attr_t attr; |
| 231 | CHECK_PTHREAD_CALL(pthread_attr_init, (&attr), "new thread"); |
| 232 | CHECK_PTHREAD_CALL(pthread_attr_setdetachstate, (&attr, PTHREAD_CREATE_DETACHED), "PTHREAD_CREATE_DETACHED"); |
| 233 | CHECK_PTHREAD_CALL(pthread_attr_setstacksize, (&attr, stack_size), stack_size); |
| 234 | int pthread_create_result = pthread_create(&new_pthread, &attr, Thread::CreateCallback, native_thread); |
| 235 | CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attr), "new thread"); |
| 236 | |
| 237 | if (UNLIKELY(pthread_create_result != 0)) { |
| 238 | // pthread_create(3) failed, so clean up. |
| 239 | ScopedObjectAccess soa(env); |
| 240 | Object* peer = soa.Decode<Object*>(java_peer); |
| 241 | SetVmData(soa, peer, 0); |
| 242 | delete native_thread; |
| 243 | |
| 244 | std::string msg(StringPrintf("pthread_create (%s stack) failed: %s", |
| 245 | PrettySize(stack_size).c_str(), strerror(pthread_create_result))); |
| 246 | Thread::Current()->ThrowOutOfMemoryError(msg.c_str()); |
| 247 | return; |
| 248 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Elliott Hughes | 462c944 | 2012-03-23 18:47:50 -0700 | [diff] [blame] | 251 | void Thread::Init() { |
| 252 | // This function does all the initialization that must be run by the native thread it applies to. |
| 253 | // (When we create a new thread from managed code, we allocate the Thread* in Thread::Create so |
| 254 | // we can handshake with the corresponding native thread when it's ready.) Check this native |
| 255 | // thread hasn't been through here already... |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 256 | CHECK(Thread::Current() == NULL); |
| 257 | |
Elliott Hughes | d8af159 | 2012-04-16 20:40:15 -0700 | [diff] [blame] | 258 | SetUpAlternateSignalStack(); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 259 | InitCpu(); |
| 260 | InitFunctionPointers(); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 261 | InitCardTable(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 262 | |
Elliott Hughes | 462c944 | 2012-03-23 18:47:50 -0700 | [diff] [blame] | 263 | Runtime* runtime = Runtime::Current(); |
| 264 | CHECK(runtime != NULL); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 265 | if (runtime->IsShuttingDown()) { |
| 266 | UNIMPLEMENTED(WARNING) << "Thread attaching whilst runtime is shutting down"; |
| 267 | } |
Elliott Hughes | 462c944 | 2012-03-23 18:47:50 -0700 | [diff] [blame] | 268 | thin_lock_id_ = runtime->GetThreadList()->AllocThreadId(); |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 269 | pthread_self_ = pthread_self(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 270 | |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 271 | InitTid(); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 272 | InitStackHwm(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 273 | |
Elliott Hughes | 6a607ad | 2012-07-13 20:40:00 -0700 | [diff] [blame] | 274 | CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, this), "attach self"); |
Elliott Hughes | a5780da | 2011-07-17 11:39:39 -0700 | [diff] [blame] | 275 | |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 276 | jni_env_ = new JNIEnvExt(this, runtime->GetJavaVM()); |
Elliott Hughes | 330304d | 2011-08-12 14:28:05 -0700 | [diff] [blame] | 277 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 278 | runtime->GetThreadList()->Register(this); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 281 | Thread* Thread::Attach(const char* thread_name, bool as_daemon, jobject thread_group) { |
Ian Rogers | 52673ff | 2012-06-27 23:25:34 -0700 | [diff] [blame] | 282 | Thread* self = new Thread(as_daemon); |
Elliott Hughes | 462c944 | 2012-03-23 18:47:50 -0700 | [diff] [blame] | 283 | self->Init(); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 284 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 285 | { |
| 286 | MutexLock mu(*GlobalSynchronization::thread_suspend_count_lock_); |
| 287 | CHECK_NE(self->GetState(), kRunnable); |
| 288 | self->SetState(kNative); |
| 289 | } |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 290 | |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 291 | // If we're the main thread, ClassLinker won't be created until after we're attached, |
| 292 | // so that thread needs a two-stage attach. Regular threads don't need this hack. |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 293 | // In the compiler, all threads need this hack, because no-one's going to be getting |
| 294 | // a native peer! |
| 295 | if (self->thin_lock_id_ != ThreadList::kMainId && !Runtime::Current()->IsCompiler()) { |
Elliott Hughes | 462c944 | 2012-03-23 18:47:50 -0700 | [diff] [blame] | 296 | self->CreatePeer(thread_name, as_daemon, thread_group); |
Elliott Hughes | 06e3ad4 | 2012-02-07 14:51:57 -0800 | [diff] [blame] | 297 | } else { |
| 298 | // These aren't necessary, but they improve diagnostics for unit tests & command-line tools. |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 299 | if (thread_name != NULL) { |
| 300 | self->name_->assign(thread_name); |
| 301 | ::art::SetThreadName(thread_name); |
| 302 | } |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 303 | } |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 304 | |
| 305 | self->GetJniEnv()->locals.AssertEmpty(); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 306 | return self; |
| 307 | } |
| 308 | |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 309 | void Thread::CreatePeer(const char* name, bool as_daemon, jobject thread_group) { |
| 310 | Runtime* runtime = Runtime::Current(); |
| 311 | CHECK(runtime->IsStarted()); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 312 | JNIEnv* env = jni_env_; |
| 313 | |
Elliott Hughes | 462c944 | 2012-03-23 18:47:50 -0700 | [diff] [blame] | 314 | if (thread_group == NULL) { |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 315 | thread_group = runtime->GetMainThreadGroup(); |
Elliott Hughes | 462c944 | 2012-03-23 18:47:50 -0700 | [diff] [blame] | 316 | } |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 317 | ScopedLocalRef<jobject> thread_name(env, env->NewStringUTF(name)); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 318 | jint thread_priority = GetNativePriority(); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 319 | jboolean thread_is_daemon = as_daemon; |
| 320 | |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 321 | ScopedLocalRef<jobject> peer(env, env->AllocObject(WellKnownClasses::java_lang_Thread)); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 322 | { |
| 323 | ScopedObjectAccess soa(env); |
| 324 | peer_ = DecodeJObject(peer.get()); |
| 325 | if (peer_ == NULL) { |
| 326 | CHECK(IsExceptionPending()); |
| 327 | return; |
| 328 | } |
Ian Rogers | 5d4bdc2 | 2011-11-02 22:15:43 -0700 | [diff] [blame] | 329 | } |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 330 | env->CallNonvirtualVoidMethod(peer.get(), |
| 331 | WellKnownClasses::java_lang_Thread, |
| 332 | WellKnownClasses::java_lang_Thread_init, |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 333 | thread_group, thread_name.get(), thread_priority, thread_is_daemon); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 334 | AssertNoPendingException(); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 335 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 336 | ScopedObjectAccess soa(this); |
| 337 | SetVmData(soa, peer_, Thread::Current()); |
| 338 | SirtRef<String> peer_thread_name(GetThreadName(soa)); |
Brian Carlstrom | 00fae58 | 2011-10-28 01:16:28 -0700 | [diff] [blame] | 339 | if (peer_thread_name.get() == NULL) { |
| 340 | // The Thread constructor should have set the Thread.name to a |
| 341 | // non-null value. However, because we can run without code |
| 342 | // available (in the compiler, in tests), we manually assign the |
| 343 | // fields the constructor should have set. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 344 | soa.DecodeField(WellKnownClasses::java_lang_Thread_daemon)->SetBoolean(peer_, thread_is_daemon); |
| 345 | soa.DecodeField(WellKnownClasses::java_lang_Thread_group)->SetObject(peer_, soa.Decode<Object*>(thread_group)); |
| 346 | soa.DecodeField(WellKnownClasses::java_lang_Thread_name)->SetObject(peer_, soa.Decode<Object*>(thread_name.get())); |
| 347 | soa.DecodeField(WellKnownClasses::java_lang_Thread_priority)->SetInt(peer_, thread_priority); |
| 348 | peer_thread_name.reset(GetThreadName(soa)); |
Brian Carlstrom | 00fae58 | 2011-10-28 01:16:28 -0700 | [diff] [blame] | 349 | } |
Elliott Hughes | 225f5a1 | 2012-06-11 11:23:48 -0700 | [diff] [blame] | 350 | // 'thread_name' may have been null, so don't trust 'peer_thread_name' to be non-null. |
Brian Carlstrom | 00fae58 | 2011-10-28 01:16:28 -0700 | [diff] [blame] | 351 | if (peer_thread_name.get() != NULL) { |
Elliott Hughes | 899e789 | 2012-01-24 14:57:32 -0800 | [diff] [blame] | 352 | SetThreadName(peer_thread_name->ToModifiedUtf8().c_str()); |
Brian Carlstrom | 00fae58 | 2011-10-28 01:16:28 -0700 | [diff] [blame] | 353 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Elliott Hughes | 899e789 | 2012-01-24 14:57:32 -0800 | [diff] [blame] | 356 | void Thread::SetThreadName(const char* name) { |
| 357 | name_->assign(name); |
| 358 | ::art::SetThreadName(name); |
| 359 | Dbg::DdmSendThreadNotification(this, CHUNK_TYPE("THNM")); |
| 360 | } |
| 361 | |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 362 | void Thread::InitStackHwm() { |
Elliott Hughes | e188419 | 2012-04-23 12:38:15 -0700 | [diff] [blame] | 363 | void* stack_base; |
| 364 | size_t stack_size; |
| 365 | GetThreadStack(stack_base, stack_size); |
Elliott Hughes | 36ecb78 | 2012-04-17 16:55:45 -0700 | [diff] [blame] | 366 | |
| 367 | // TODO: include this in the thread dumps; potentially useful in SIGQUIT output? |
Elliott Hughes | e188419 | 2012-04-23 12:38:15 -0700 | [diff] [blame] | 368 | VLOG(threads) << StringPrintf("Native stack is at %p (%s)", stack_base, PrettySize(stack_size).c_str()); |
| 369 | |
| 370 | stack_begin_ = reinterpret_cast<byte*>(stack_base); |
| 371 | stack_size_ = stack_size; |
Elliott Hughes | 36ecb78 | 2012-04-17 16:55:45 -0700 | [diff] [blame] | 372 | |
Ian Rogers | 932746a | 2011-09-22 18:57:50 -0700 | [diff] [blame] | 373 | if (stack_size_ <= kStackOverflowReservedBytes) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 374 | LOG(FATAL) << "Attempt to attach a thread with a too-small stack (" << stack_size_ << " bytes)"; |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 375 | } |
Elliott Hughes | 449b4bd | 2011-09-09 12:01:38 -0700 | [diff] [blame] | 376 | |
Elliott Hughes | e188419 | 2012-04-23 12:38:15 -0700 | [diff] [blame] | 377 | // TODO: move this into the Linux GetThreadStack implementation. |
| 378 | #if !defined(__APPLE__) |
Elliott Hughes | 36ecb78 | 2012-04-17 16:55:45 -0700 | [diff] [blame] | 379 | // If we're the main thread, check whether we were run with an unlimited stack. In that case, |
| 380 | // glibc will have reported a 2GB stack for our 32-bit process, and our stack overflow detection |
| 381 | // will be broken because we'll die long before we get close to 2GB. |
| 382 | if (thin_lock_id_ == 1) { |
| 383 | rlimit stack_limit; |
| 384 | if (getrlimit(RLIMIT_STACK, &stack_limit) == -1) { |
| 385 | PLOG(FATAL) << "getrlimit(RLIMIT_STACK) failed"; |
| 386 | } |
| 387 | if (stack_limit.rlim_cur == RLIM_INFINITY) { |
| 388 | // Find the default stack size for new threads... |
| 389 | pthread_attr_t default_attributes; |
| 390 | size_t default_stack_size; |
| 391 | CHECK_PTHREAD_CALL(pthread_attr_init, (&default_attributes), "default stack size query"); |
| 392 | CHECK_PTHREAD_CALL(pthread_attr_getstacksize, (&default_attributes, &default_stack_size), |
| 393 | "default stack size query"); |
| 394 | CHECK_PTHREAD_CALL(pthread_attr_destroy, (&default_attributes), "default stack size query"); |
| 395 | |
| 396 | // ...and use that as our limit. |
| 397 | size_t old_stack_size = stack_size_; |
| 398 | stack_size_ = default_stack_size; |
| 399 | stack_begin_ += (old_stack_size - stack_size_); |
Elliott Hughes | faf4ba0 | 2012-05-02 16:12:19 -0700 | [diff] [blame] | 400 | VLOG(threads) << "Limiting unlimited stack (reported as " << PrettySize(old_stack_size) << ")" |
| 401 | << " to " << PrettySize(stack_size_) |
| 402 | << " with base " << reinterpret_cast<void*>(stack_begin_); |
Elliott Hughes | 36ecb78 | 2012-04-17 16:55:45 -0700 | [diff] [blame] | 403 | } |
| 404 | } |
Elliott Hughes | e188419 | 2012-04-23 12:38:15 -0700 | [diff] [blame] | 405 | #endif |
Elliott Hughes | 36ecb78 | 2012-04-17 16:55:45 -0700 | [diff] [blame] | 406 | |
Ian Rogers | 932746a | 2011-09-22 18:57:50 -0700 | [diff] [blame] | 407 | // Set stack_end_ to the bottom of the stack saving space of stack overflows |
| 408 | ResetDefaultStackEnd(); |
Elliott Hughes | 449b4bd | 2011-09-09 12:01:38 -0700 | [diff] [blame] | 409 | |
| 410 | // Sanity check. |
| 411 | int stack_variable; |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 412 | CHECK_GT(&stack_variable, reinterpret_cast<void*>(stack_end_)); |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 415 | void Thread::ShortDump(std::ostream& os) const { |
| 416 | os << "Thread["; |
| 417 | if (GetThinLockId() != 0) { |
| 418 | // If we're in kStarting, we won't have a thin lock id or tid yet. |
| 419 | os << GetThinLockId() |
| 420 | << ",tid=" << GetTid() << ','; |
Elliott Hughes | e091855 | 2011-10-28 17:18:29 -0700 | [diff] [blame] | 421 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 422 | os << GetStateUnsafe() |
| 423 | << ",Thread*=" << this |
| 424 | << ",peer=" << peer_ |
| 425 | << ",\"" << *name_ << "\"" |
| 426 | << "]"; |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 429 | void Thread::Dump(std::ostream& os) const { |
| 430 | DumpState(os); |
| 431 | DumpStack(os); |
| 432 | } |
| 433 | |
| 434 | String* Thread::GetThreadName(const ScopedObjectAccessUnchecked& soa) const { |
| 435 | Field* f = soa.DecodeField(WellKnownClasses::java_lang_Thread_name); |
Elliott Hughes | af8d15a | 2012-05-29 09:12:18 -0700 | [diff] [blame] | 436 | return (peer_ != NULL) ? reinterpret_cast<String*>(f->GetObject(peer_)) : NULL; |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 437 | } |
| 438 | |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 439 | void Thread::GetThreadName(std::string& name) const { |
| 440 | name.assign(*name_); |
| 441 | } |
| 442 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 443 | // Attempt to rectify locks so that we dump thread list with required locks before exiting. |
| 444 | static void UnsafeLogFatalForSuspendCount(Thread* self) NO_THREAD_SAFETY_ANALYSIS { |
| 445 | GlobalSynchronization::thread_suspend_count_lock_->Unlock(); |
| 446 | GlobalSynchronization::mutator_lock_->SharedTryLock(); |
| 447 | if (!GlobalSynchronization::mutator_lock_->IsSharedHeld()) { |
| 448 | LOG(WARNING) << "Dumping thread list without holding mutator_lock_"; |
| 449 | } |
| 450 | GlobalSynchronization::thread_list_lock_->TryLock(); |
| 451 | if (!GlobalSynchronization::thread_list_lock_->IsExclusiveHeld()) { |
| 452 | LOG(WARNING) << "Dumping thread list without holding thread_list_lock_"; |
| 453 | } |
| 454 | std::ostringstream ss; |
| 455 | Runtime::Current()->GetThreadList()->DumpLocked(ss); |
| 456 | LOG(FATAL) << self << " suspend count already zero.\n" << ss.str(); |
| 457 | } |
| 458 | |
| 459 | void Thread::ModifySuspendCount(int delta, bool for_debugger) { |
| 460 | DCHECK(delta == -1 || delta == +1 || delta == -debug_suspend_count_) |
| 461 | << delta << " " << debug_suspend_count_ << " " << this; |
| 462 | DCHECK_GE(suspend_count_, debug_suspend_count_) << this; |
| 463 | GlobalSynchronization::thread_suspend_count_lock_->AssertHeld(); |
| 464 | |
| 465 | if (delta == -1 && suspend_count_ <= 0) { |
| 466 | // This is expected if you attach a thread during a GC. |
| 467 | if (UNLIKELY(!IsStillStarting())) { |
| 468 | UnsafeLogFatalForSuspendCount(this); |
| 469 | } |
| 470 | return; |
| 471 | } |
| 472 | suspend_count_ += delta; |
| 473 | if (for_debugger) { |
| 474 | debug_suspend_count_ += delta; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | void Thread::FullSuspendCheck() { |
| 479 | VLOG(threads) << this << " self-suspending"; |
| 480 | // Make thread appear suspended to other threads, release mutator_lock_. |
| 481 | TransitionFromRunnableToSuspended(kSuspended); |
| 482 | // Transition back to runnable noting requests to suspend, re-acquire share on mutator_lock_. |
| 483 | TransitionFromSuspendedToRunnable(); |
| 484 | VLOG(threads) << this << " self-reviving"; |
| 485 | } |
| 486 | |
| 487 | void Thread::TransitionFromRunnableToSuspended(ThreadState new_state) { |
| 488 | AssertThreadSuspensionIsAllowable(); |
| 489 | CHECK_NE(new_state, kRunnable); |
| 490 | CHECK_EQ(this, Thread::Current()); |
| 491 | { |
| 492 | MutexLock mu(*GlobalSynchronization::thread_suspend_count_lock_); |
| 493 | CHECK_EQ(GetState(), kRunnable); |
| 494 | SetState(new_state); |
| 495 | } |
| 496 | // Release share on mutator_lock_. |
| 497 | GlobalSynchronization::mutator_lock_->SharedUnlock(); |
| 498 | } |
| 499 | |
| 500 | ThreadState Thread::TransitionFromSuspendedToRunnable() { |
| 501 | bool done = false; |
| 502 | ThreadState old_state; |
| 503 | do { |
| 504 | { |
| 505 | // Wait while our suspend count is non-zero. |
| 506 | MutexLock mu(*GlobalSynchronization::thread_suspend_count_lock_); |
| 507 | old_state = GetState(); |
| 508 | CHECK_NE(old_state, kRunnable); |
| 509 | GlobalSynchronization::mutator_lock_->AssertNotHeld(); // Otherwise we starve GC.. |
| 510 | while (GetSuspendCount() != 0) { |
| 511 | // Re-check when Thread::resume_cond_ is notified. |
| 512 | Thread::resume_cond_->Wait(*GlobalSynchronization::thread_suspend_count_lock_); |
| 513 | } |
| 514 | } |
| 515 | // Re-acquire shared mutator_lock_ access. |
| 516 | GlobalSynchronization::mutator_lock_->SharedLock(); |
| 517 | // Holding the mutator_lock_, synchronize with any thread trying to raise the suspend count |
| 518 | // and change state to Runnable if no suspend is pending. |
| 519 | MutexLock mu(*GlobalSynchronization::thread_suspend_count_lock_); |
| 520 | if (GetSuspendCount() == 0) { |
| 521 | SetState(kRunnable); |
| 522 | done = true; |
| 523 | } else { |
| 524 | // Release shared mutator_lock_ access and try again. |
| 525 | GlobalSynchronization::mutator_lock_->SharedUnlock(); |
| 526 | } |
| 527 | } while (!done); |
| 528 | return old_state; |
| 529 | } |
| 530 | |
| 531 | Thread* Thread::SuspendForDebugger(jobject peer, bool request_suspension, bool* timeout) { |
| 532 | static const useconds_t kTimeoutUs = 30 * 1000000; // 30s. |
| 533 | useconds_t total_delay_us = 0; |
| 534 | useconds_t delay_us = 0; |
| 535 | bool did_suspend_request = false; |
| 536 | *timeout = false; |
| 537 | while (true) { |
| 538 | Thread* thread; |
| 539 | { |
| 540 | ScopedObjectAccess soa(Thread::Current()); |
| 541 | MutexLock mu(*GlobalSynchronization::thread_list_lock_); |
| 542 | thread = Thread::FromManagedThread(soa, peer); |
| 543 | if (thread == NULL) { |
| 544 | LOG(WARNING) << "No such thread for suspend: " << peer; |
| 545 | return NULL; |
| 546 | } |
| 547 | { |
| 548 | MutexLock mu(*GlobalSynchronization::thread_suspend_count_lock_); |
| 549 | if (request_suspension) { |
| 550 | thread->ModifySuspendCount(+1, true /* for_debugger */); |
| 551 | request_suspension = false; |
| 552 | did_suspend_request = true; |
| 553 | } |
| 554 | // IsSuspended on the current thread will fail as the current thread is changed into |
| 555 | // Runnable above. As the suspend count is now raised if this is the current thread |
| 556 | // it will self suspend on transition to Runnable, making it hard to work with. Its simpler |
| 557 | // to just explicitly handle the current thread in the callers to this code. |
| 558 | CHECK_NE(thread, soa.Self()) << "Attempt to suspend for debugger the current thread"; |
| 559 | // If thread is suspended (perhaps it was already not Runnable but didn't have a suspend |
| 560 | // count, or else we've waited and it has self suspended) or is the current thread, we're |
| 561 | // done. |
| 562 | if (thread->IsSuspended()) { |
| 563 | return thread; |
| 564 | } |
| 565 | if (total_delay_us >= kTimeoutUs) { |
| 566 | LOG(ERROR) << "Thread suspension timed out: " << peer; |
| 567 | if (did_suspend_request) { |
| 568 | thread->ModifySuspendCount(-1, true /* for_debugger */); |
| 569 | } |
| 570 | *timeout = true; |
| 571 | return NULL; |
| 572 | } |
| 573 | } |
| 574 | // Release locks and come out of runnable state. |
| 575 | } |
| 576 | for (int i = kMaxMutexLevel; i >= 0; --i) { |
| 577 | BaseMutex* held_mutex = Thread::Current()->GetHeldMutex(static_cast<MutexLevel>(i)); |
| 578 | if (held_mutex != NULL) { |
| 579 | LOG(FATAL) << "Holding " << held_mutex->GetName() |
| 580 | << " while sleeping for thread suspension"; |
| 581 | } |
| 582 | } |
| 583 | { |
| 584 | useconds_t new_delay_us = delay_us * 2; |
| 585 | CHECK_GE(new_delay_us, delay_us); |
| 586 | if (new_delay_us < 500000) { // Don't allow sleeping to be more than 0.5s. |
| 587 | delay_us = new_delay_us; |
| 588 | } |
| 589 | } |
| 590 | if (delay_us == 0) { |
| 591 | sched_yield(); |
| 592 | // Default to 1 milliseconds (note that this gets multiplied by 2 before the first sleep). |
| 593 | delay_us = 500; |
| 594 | } else { |
| 595 | usleep(delay_us); |
| 596 | total_delay_us += delay_us; |
| 597 | } |
| 598 | } |
| 599 | } |
| 600 | |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 601 | void Thread::DumpState(std::ostream& os, const Thread* thread, pid_t tid) { |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 602 | std::string group_name; |
| 603 | int priority; |
| 604 | bool is_daemon = false; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 605 | |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 606 | if (thread != NULL && thread->peer_ != NULL) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 607 | ScopedObjectAccess soa(Thread::Current()); |
| 608 | priority = soa.DecodeField(WellKnownClasses::java_lang_Thread_priority)->GetInt(thread->peer_); |
| 609 | is_daemon = soa.DecodeField(WellKnownClasses::java_lang_Thread_daemon)->GetBoolean(thread->peer_); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 610 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 611 | Object* thread_group = thread->GetThreadGroup(soa); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 612 | if (thread_group != NULL) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 613 | Field* group_name_field = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_name); |
Elliott Hughes | af8d15a | 2012-05-29 09:12:18 -0700 | [diff] [blame] | 614 | String* group_name_string = reinterpret_cast<String*>(group_name_field->GetObject(thread_group)); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 615 | group_name = (group_name_string != NULL) ? group_name_string->ToModifiedUtf8() : "<null>"; |
| 616 | } |
| 617 | } else { |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 618 | priority = GetNativePriority(); |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 619 | } |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 620 | |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 621 | std::string scheduler_group_name(GetSchedulerGroupName(tid)); |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 622 | if (scheduler_group_name.empty()) { |
| 623 | scheduler_group_name = "default"; |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 624 | } |
| 625 | |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 626 | if (thread != NULL) { |
| 627 | os << '"' << *thread->name_ << '"'; |
| 628 | if (is_daemon) { |
| 629 | os << " daemon"; |
| 630 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 631 | MutexLock mu(*GlobalSynchronization::thread_suspend_count_lock_); |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 632 | os << " prio=" << priority |
| 633 | << " tid=" << thread->GetThinLockId() |
| 634 | << " " << thread->GetState() << "\n"; |
| 635 | } else { |
Elliott Hughes | 289be85 | 2012-06-12 13:57:20 -0700 | [diff] [blame] | 636 | os << '"' << ::art::GetThreadName(tid) << '"' |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 637 | << " prio=" << priority |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 638 | << " (not attached)\n"; |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 639 | } |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 640 | |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 641 | if (thread != NULL) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 642 | MutexLock mu(*GlobalSynchronization::thread_suspend_count_lock_); |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 643 | os << " | group=\"" << group_name << "\"" |
| 644 | << " sCount=" << thread->suspend_count_ |
| 645 | << " dsCount=" << thread->debug_suspend_count_ |
| 646 | << " obj=" << reinterpret_cast<void*>(thread->peer_) |
| 647 | << " self=" << reinterpret_cast<const void*>(thread) << "\n"; |
| 648 | } |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 649 | |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 650 | os << " | sysTid=" << tid |
| 651 | << " nice=" << getpriority(PRIO_PROCESS, tid) |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 652 | << " cgrp=" << scheduler_group_name; |
| 653 | if (thread != NULL) { |
| 654 | int policy; |
| 655 | sched_param sp; |
| 656 | CHECK_PTHREAD_CALL(pthread_getschedparam, (thread->pthread_self_, &policy, &sp), __FUNCTION__); |
| 657 | os << " sched=" << policy << "/" << sp.sched_priority |
| 658 | << " handle=" << reinterpret_cast<void*>(thread->pthread_self_); |
| 659 | } |
| 660 | os << "\n"; |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 661 | |
| 662 | // Grab the scheduler stats for this thread. |
| 663 | std::string scheduler_stats; |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 664 | if (ReadFileToString(StringPrintf("/proc/self/task/%d/schedstat", tid), &scheduler_stats)) { |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 665 | scheduler_stats.resize(scheduler_stats.size() - 1); // Lose the trailing '\n'. |
| 666 | } else { |
| 667 | scheduler_stats = "0 0 0"; |
| 668 | } |
| 669 | |
| 670 | int utime = 0; |
| 671 | int stime = 0; |
| 672 | int task_cpu = 0; |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 673 | GetTaskStats(tid, utime, stime, task_cpu); |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 674 | |
| 675 | os << " | schedstat=( " << scheduler_stats << " )" |
| 676 | << " utm=" << utime |
| 677 | << " stm=" << stime |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 678 | << " core=" << task_cpu |
| 679 | << " HZ=" << sysconf(_SC_CLK_TCK) << "\n"; |
| 680 | if (thread != NULL) { |
| 681 | os << " | stack=" << reinterpret_cast<void*>(thread->stack_begin_) << "-" << reinterpret_cast<void*>(thread->stack_end_) |
| 682 | << " stackSize=" << PrettySize(thread->stack_size_) << "\n"; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | void Thread::DumpState(std::ostream& os) const { |
| 687 | Thread::DumpState(os, this, GetTid()); |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 688 | } |
| 689 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 690 | struct StackDumpVisitor : public StackVisitor { |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 691 | StackDumpVisitor(std::ostream& os, const Thread* thread, Context* context, bool can_allocate) |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 692 | SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 693 | : StackVisitor(thread->GetManagedStack(), thread->GetTraceStack(), context), |
| 694 | os(os), thread(thread), can_allocate(can_allocate), |
| 695 | last_method(NULL), last_line_number(0), repetition_count(0), frame_count(0) { |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 696 | } |
| 697 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 698 | virtual ~StackDumpVisitor() { |
Elliott Hughes | e85d2e9 | 2012-05-01 14:02:10 -0700 | [diff] [blame] | 699 | if (frame_count == 0) { |
| 700 | os << " (no managed stack frames)\n"; |
| 701 | } |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 702 | } |
| 703 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 704 | bool VisitFrame() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 705 | Method* m = GetMethod(); |
| 706 | if (m->IsRuntimeMethod()) { |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 707 | return true; |
Ian Rogers | 9086572 | 2011-09-19 11:11:44 -0700 | [diff] [blame] | 708 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 709 | const int kMaxRepetition = 3; |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 710 | Class* c = m->GetDeclaringClass(); |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 711 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Ian Rogers | b861dc0 | 2011-11-14 17:00:05 -0800 | [diff] [blame] | 712 | const DexCache* dex_cache = c->GetDexCache(); |
| 713 | int line_number = -1; |
| 714 | if (dex_cache != NULL) { // be tolerant of bad input |
| 715 | const DexFile& dex_file = class_linker->FindDexFile(dex_cache); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 716 | line_number = dex_file.GetLineNumFromPC(m, GetDexPc()); |
Ian Rogers | b861dc0 | 2011-11-14 17:00:05 -0800 | [diff] [blame] | 717 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 718 | if (line_number == last_line_number && last_method == m) { |
| 719 | repetition_count++; |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 720 | } else { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 721 | if (repetition_count >= kMaxRepetition) { |
| 722 | os << " ... repeated " << (repetition_count - kMaxRepetition) << " times\n"; |
| 723 | } |
| 724 | repetition_count = 0; |
| 725 | last_line_number = line_number; |
| 726 | last_method = m; |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 727 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 728 | if (repetition_count < kMaxRepetition) { |
| 729 | os << " at " << PrettyMethod(m, false); |
| 730 | if (m->IsNative()) { |
| 731 | os << "(Native method)"; |
| 732 | } else { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 733 | mh.ChangeMethod(m); |
| 734 | const char* source_file(mh.GetDeclaringClassSourceFile()); |
| 735 | os << "(" << (source_file != NULL ? source_file : "unavailable") |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 736 | << ":" << line_number << ")"; |
| 737 | } |
| 738 | os << "\n"; |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 739 | if (frame_count == 0) { |
| 740 | Monitor::DescribeWait(os, thread); |
| 741 | } |
| 742 | if (can_allocate) { |
| 743 | Monitor::DescribeLocks(os, this); |
| 744 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 745 | } |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 746 | |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 747 | ++frame_count; |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 748 | return true; |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 749 | } |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 750 | std::ostream& os; |
| 751 | const Thread* thread; |
| 752 | bool can_allocate; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 753 | MethodHelper mh; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 754 | Method* last_method; |
| 755 | int last_line_number; |
| 756 | int repetition_count; |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 757 | int frame_count; |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 758 | }; |
| 759 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 760 | void Thread::DumpStack(std::ostream& os) const { |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 761 | // If we're currently in native code, dump that stack before dumping the managed stack. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 762 | ThreadState state; |
| 763 | { |
| 764 | MutexLock mu(*GlobalSynchronization::thread_suspend_count_lock_); |
| 765 | state = GetState(); |
| 766 | } |
| 767 | if (state == kNative) { |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 768 | DumpKernelStack(os, GetTid(), " kernel: ", false); |
| 769 | DumpNativeStack(os, GetTid(), " native: ", false); |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 770 | } |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 771 | UniquePtr<Context> context(Context::Create()); |
| 772 | StackDumpVisitor dumper(os, this, context.get(), !throwing_OutOfMemoryError_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 773 | dumper.WalkStack(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 774 | } |
| 775 | |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 776 | void Thread::ThreadExitCallback(void* arg) { |
| 777 | Thread* self = reinterpret_cast<Thread*>(arg); |
Elliott Hughes | 6a607ad | 2012-07-13 20:40:00 -0700 | [diff] [blame] | 778 | if (self->thread_exit_check_count_ == 0) { |
| 779 | LOG(WARNING) << "Native thread exiting without having called DetachCurrentThread (maybe it's going to use a pthread_key_create destructor?): " << *self; |
| 780 | CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, self), "reattach self"); |
| 781 | self->thread_exit_check_count_ = 1; |
| 782 | } else { |
| 783 | LOG(FATAL) << "Native thread exited without calling DetachCurrentThread: " << *self; |
| 784 | } |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 785 | } |
| 786 | |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 787 | void Thread::Startup() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 788 | { |
| 789 | MutexLock mu(*GlobalSynchronization::thread_suspend_count_lock_); // Keep GCC happy. |
| 790 | resume_cond_ = new ConditionVariable("Thread resumption condition variable"); |
| 791 | } |
| 792 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 793 | // Allocate a TLS slot. |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 794 | 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] | 795 | |
| 796 | // Double-check the TLS slot allocation. |
| 797 | if (pthread_getspecific(pthread_key_self_) != NULL) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 798 | LOG(FATAL) << "Newly-created pthread TLS slot is not NULL"; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 799 | } |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 800 | } |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 801 | |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 802 | void Thread::FinishStartup() { |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 803 | Runtime* runtime = Runtime::Current(); |
| 804 | CHECK(runtime->IsStarted()); |
Brian Carlstrom | b82b687 | 2011-10-26 17:18:07 -0700 | [diff] [blame] | 805 | |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 806 | // Finish attaching the main thread. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 807 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 808 | Thread::Current()->CreatePeer("main", false, runtime->GetMainThreadGroup()); |
Jesse Wilson | 9a6bae8 | 2011-11-14 14:57:30 -0500 | [diff] [blame] | 809 | |
Elliott Hughes | af8d15a | 2012-05-29 09:12:18 -0700 | [diff] [blame] | 810 | Runtime::Current()->GetClassLinker()->RunRootClinits(); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 811 | } |
| 812 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 813 | void Thread::Shutdown() { |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 814 | CHECK_PTHREAD_CALL(pthread_key_delete, (Thread::pthread_key_self_), "self key"); |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 815 | } |
| 816 | |
Ian Rogers | 52673ff | 2012-06-27 23:25:34 -0700 | [diff] [blame] | 817 | Thread::Thread(bool daemon) |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 818 | : suspend_count_(0), |
| 819 | card_table_(NULL), |
| 820 | exception_(NULL), |
| 821 | stack_end_(NULL), |
| 822 | managed_stack_(), |
| 823 | jni_env_(NULL), |
| 824 | self_(NULL), |
| 825 | state_(kNative), |
Elliott Hughes | 47179f7 | 2011-10-27 16:44:39 -0700 | [diff] [blame] | 826 | peer_(NULL), |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 827 | stack_begin_(NULL), |
| 828 | stack_size_(0), |
| 829 | thin_lock_id_(0), |
| 830 | tid_(0), |
Elliott Hughes | e62934d | 2012-04-09 11:24:29 -0700 | [diff] [blame] | 831 | wait_mutex_(new Mutex("a thread wait mutex")), |
| 832 | wait_cond_(new ConditionVariable("a thread wait condition variable")), |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 833 | wait_monitor_(NULL), |
| 834 | interrupted_(false), |
Elliott Hughes | dc33ad5 | 2011-09-16 19:46:51 -0700 | [diff] [blame] | 835 | wait_next_(NULL), |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 836 | monitor_enter_object_(NULL), |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 837 | top_sirt_(NULL), |
Elliott Hughes | dc33ad5 | 2011-09-16 19:46:51 -0700 | [diff] [blame] | 838 | runtime_(NULL), |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 839 | class_loader_override_(NULL), |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 840 | long_jump_context_(NULL), |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 841 | throwing_OutOfMemoryError_(false), |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 842 | debug_suspend_count_(0), |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 843 | debug_invoke_req_(new DebugInvokeReq), |
Elliott Hughes | 899e789 | 2012-01-24 14:57:32 -0800 | [diff] [blame] | 844 | trace_stack_(new std::vector<TraceStackFrame>), |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 845 | name_(new std::string(kThreadNameDuringStartup)), |
Ian Rogers | 52673ff | 2012-06-27 23:25:34 -0700 | [diff] [blame] | 846 | daemon_(daemon), |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 847 | pthread_self_(0), |
Ian Rogers | 52673ff | 2012-06-27 23:25:34 -0700 | [diff] [blame] | 848 | no_thread_suspension_(0), |
Elliott Hughes | 6a607ad | 2012-07-13 20:40:00 -0700 | [diff] [blame] | 849 | last_no_thread_suspension_cause_(NULL), |
| 850 | thread_exit_check_count_(0) { |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 851 | CHECK_EQ((sizeof(Thread) % 4), 0U) << sizeof(Thread); |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 852 | memset(&held_mutexes_[0], 0, sizeof(held_mutexes_)); |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 853 | } |
| 854 | |
Elliott Hughes | 7dc5166 | 2012-05-16 14:48:43 -0700 | [diff] [blame] | 855 | bool Thread::IsStillStarting() const { |
| 856 | // You might think you can check whether the state is kStarting, but for much of thread startup, |
| 857 | // the thread might also be in kVmWait. |
| 858 | // You might think you can check whether the peer is NULL, but the peer is actually created and |
| 859 | // assigned fairly early on, and needs to be. |
| 860 | // It turns out that the last thing to change is the thread name; that's a good proxy for "has |
| 861 | // this thread _ever_ entered kRunnable". |
| 862 | return (*name_ == kThreadNameDuringStartup); |
| 863 | } |
| 864 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 865 | void Thread::AssertNoPendingException() const { |
| 866 | if (UNLIKELY(IsExceptionPending())) { |
| 867 | ScopedObjectAccess soa(Thread::Current()); |
| 868 | Throwable* exception = GetException(); |
| 869 | LOG(FATAL) << "No pending exception expected: " << exception->Dump(); |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | static void MonitorExitVisitor(const Object* object, void* arg) NO_THREAD_SAFETY_ANALYSIS { |
| 874 | Thread* self = reinterpret_cast<Thread*>(arg); |
Elliott Hughes | 02b48d1 | 2011-09-07 17:15:51 -0700 | [diff] [blame] | 875 | Object* entered_monitor = const_cast<Object*>(object); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 876 | if (self->HoldsLock(entered_monitor)) { |
| 877 | LOG(WARNING) << "Calling MonitorExit on object " |
| 878 | << object << " (" << PrettyTypeOf(object) << ")" |
| 879 | << " left locked by native thread " |
| 880 | << *Thread::Current() << " which is detaching"; |
| 881 | entered_monitor->MonitorExit(self); |
| 882 | } |
Elliott Hughes | 02b48d1 | 2011-09-07 17:15:51 -0700 | [diff] [blame] | 883 | } |
| 884 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 885 | void Thread::Destroy() { |
Elliott Hughes | 02b48d1 | 2011-09-07 17:15:51 -0700 | [diff] [blame] | 886 | // On thread detach, all monitors entered with JNI MonitorEnter are automatically exited. |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 887 | if (jni_env_ != NULL) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 888 | jni_env_->monitors.VisitRoots(MonitorExitVisitor, Thread::Current()); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 889 | } |
Elliott Hughes | 02b48d1 | 2011-09-07 17:15:51 -0700 | [diff] [blame] | 890 | |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 891 | if (peer_ != NULL) { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 892 | Thread* self = this; |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 893 | |
Elliott Hughes | 534da07 | 2012-03-27 15:17:42 -0700 | [diff] [blame] | 894 | // We may need to call user-supplied managed code. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 895 | ScopedObjectAccess soa(this); |
Elliott Hughes | 534da07 | 2012-03-27 15:17:42 -0700 | [diff] [blame] | 896 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 897 | HandleUncaughtExceptions(soa); |
| 898 | RemoveFromThreadGroup(soa); |
Elliott Hughes | 534da07 | 2012-03-27 15:17:42 -0700 | [diff] [blame] | 899 | |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 900 | // this.vmData = 0; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 901 | SetVmData(soa, peer_, NULL); |
Elliott Hughes | 02b48d1 | 2011-09-07 17:15:51 -0700 | [diff] [blame] | 902 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 903 | Dbg::PostThreadDeath(self); |
Elliott Hughes | 02b48d1 | 2011-09-07 17:15:51 -0700 | [diff] [blame] | 904 | |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 905 | // Thread.join() is implemented as an Object.wait() on the Thread.lock |
| 906 | // object. Signal anyone who is waiting. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 907 | Object* lock = soa.DecodeField(WellKnownClasses::java_lang_Thread_lock)->GetObject(peer_); |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 908 | // (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] | 909 | if (lock != NULL) { |
| 910 | lock->MonitorEnter(self); |
| 911 | lock->NotifyAll(); |
| 912 | lock->MonitorExit(self); |
| 913 | } |
| 914 | } |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 915 | } |
Elliott Hughes | 02b48d1 | 2011-09-07 17:15:51 -0700 | [diff] [blame] | 916 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 917 | Thread::~Thread() { |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 918 | delete jni_env_; |
Elliott Hughes | 02b48d1 | 2011-09-07 17:15:51 -0700 | [diff] [blame] | 919 | jni_env_ = NULL; |
| 920 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 921 | { |
| 922 | MutexLock mu(*GlobalSynchronization::thread_suspend_count_lock_); |
| 923 | CHECK_NE(GetState(), kRunnable); |
| 924 | SetState(kTerminated); |
| 925 | } |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 926 | |
| 927 | delete wait_cond_; |
| 928 | delete wait_mutex_; |
| 929 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 930 | #if !defined(ART_USE_LLVM_COMPILER) |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 931 | delete long_jump_context_; |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 932 | #endif |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 933 | |
| 934 | delete debug_invoke_req_; |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 935 | delete trace_stack_; |
Elliott Hughes | 899e789 | 2012-01-24 14:57:32 -0800 | [diff] [blame] | 936 | delete name_; |
Elliott Hughes | d8af159 | 2012-04-16 20:40:15 -0700 | [diff] [blame] | 937 | |
| 938 | TearDownAlternateSignalStack(); |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 939 | } |
| 940 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 941 | void Thread::HandleUncaughtExceptions(const ScopedObjectAccess& soa) { |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 942 | if (!IsExceptionPending()) { |
| 943 | return; |
| 944 | } |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 945 | // Get and clear the exception. |
| 946 | Object* exception = GetException(); |
| 947 | ClearException(); |
| 948 | |
| 949 | // If the thread has its own handler, use that. |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 950 | Object* handler = |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 951 | soa.DecodeField(WellKnownClasses::java_lang_Thread_uncaughtHandler)->GetObject(peer_); |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 952 | if (handler == NULL) { |
| 953 | // Otherwise use the thread group's default handler. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 954 | handler = GetThreadGroup(soa); |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | // Call the handler. |
Elliott Hughes | af8d15a | 2012-05-29 09:12:18 -0700 | [diff] [blame] | 958 | jmethodID mid = WellKnownClasses::java_lang_Thread$UncaughtExceptionHandler_uncaughtException; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 959 | Method* m = handler->GetClass()->FindVirtualMethodForVirtualOrInterface(soa.DecodeMethod(mid)); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 960 | JValue args[2]; |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 961 | args[0].SetL(peer_); |
| 962 | args[1].SetL(exception); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 963 | m->Invoke(this, handler, args, NULL); |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 964 | |
| 965 | // If the handler threw, clear that exception too. |
| 966 | ClearException(); |
| 967 | } |
| 968 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 969 | Object* Thread::GetThreadGroup(const ScopedObjectAccessUnchecked& soa) const { |
| 970 | return soa.DecodeField(WellKnownClasses::java_lang_Thread_group)->GetObject(peer_); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 971 | } |
| 972 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 973 | void Thread::RemoveFromThreadGroup(const ScopedObjectAccess& soa) { |
Brian Carlstrom | 4514d3c | 2011-10-21 17:01:31 -0700 | [diff] [blame] | 974 | // this.group.removeThread(this); |
| 975 | // group can be null if we're in the compiler or a test. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 976 | Object* group = GetThreadGroup(soa); |
Brian Carlstrom | 4514d3c | 2011-10-21 17:01:31 -0700 | [diff] [blame] | 977 | if (group != NULL) { |
Elliott Hughes | af8d15a | 2012-05-29 09:12:18 -0700 | [diff] [blame] | 978 | jmethodID mid = WellKnownClasses::java_lang_ThreadGroup_removeThread; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 979 | Method* m = group->GetClass()->FindVirtualMethodForVirtualOrInterface(soa.DecodeMethod(mid)); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 980 | JValue args[1]; |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 981 | args[0].SetL(peer_); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 982 | m->Invoke(this, group, args, NULL); |
Brian Carlstrom | 4514d3c | 2011-10-21 17:01:31 -0700 | [diff] [blame] | 983 | } |
| 984 | } |
| 985 | |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 986 | size_t Thread::NumSirtReferences() { |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 987 | size_t count = 0; |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 988 | for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->GetLink()) { |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 989 | count += cur->NumberOfReferences(); |
| 990 | } |
| 991 | return count; |
| 992 | } |
| 993 | |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 994 | bool Thread::SirtContains(jobject obj) { |
| 995 | Object** sirt_entry = reinterpret_cast<Object**>(obj); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 996 | for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->GetLink()) { |
| 997 | if (cur->Contains(sirt_entry)) { |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 998 | return true; |
| 999 | } |
| 1000 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1001 | // JNI code invoked from portable code uses shadow frames rather than the SIRT. |
| 1002 | return managed_stack_.ShadowFramesContain(sirt_entry); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 1003 | } |
| 1004 | |
Shih-wei Liao | 8dfc9d5 | 2011-09-28 18:06:15 -0700 | [diff] [blame] | 1005 | void Thread::SirtVisitRoots(Heap::RootVisitor* visitor, void* arg) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1006 | for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->GetLink()) { |
Shih-wei Liao | 8dfc9d5 | 2011-09-28 18:06:15 -0700 | [diff] [blame] | 1007 | size_t num_refs = cur->NumberOfReferences(); |
| 1008 | for (size_t j = 0; j < num_refs; j++) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1009 | Object* object = cur->GetReference(j); |
Brian Carlstrom | 5e73f9c | 2011-10-11 11:28:12 -0700 | [diff] [blame] | 1010 | if (object != NULL) { |
| 1011 | visitor(object, arg); |
| 1012 | } |
Shih-wei Liao | 8dfc9d5 | 2011-09-28 18:06:15 -0700 | [diff] [blame] | 1013 | } |
| 1014 | } |
| 1015 | } |
| 1016 | |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 1017 | Object* Thread::DecodeJObject(jobject obj) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1018 | DCHECK(CanAccessDirectReferences()); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 1019 | if (obj == NULL) { |
| 1020 | return NULL; |
| 1021 | } |
| 1022 | IndirectRef ref = reinterpret_cast<IndirectRef>(obj); |
| 1023 | IndirectRefKind kind = GetIndirectRefKind(ref); |
| 1024 | Object* result; |
| 1025 | switch (kind) { |
| 1026 | case kLocal: |
| 1027 | { |
Elliott Hughes | 69f5bc6 | 2011-08-24 09:26:14 -0700 | [diff] [blame] | 1028 | IndirectReferenceTable& locals = jni_env_->locals; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 1029 | result = const_cast<Object*>(locals.Get(ref)); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 1030 | break; |
| 1031 | } |
| 1032 | case kGlobal: |
| 1033 | { |
| 1034 | JavaVMExt* vm = Runtime::Current()->GetJavaVM(); |
| 1035 | IndirectReferenceTable& globals = vm->globals; |
| 1036 | MutexLock mu(vm->globals_lock); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 1037 | result = const_cast<Object*>(globals.Get(ref)); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 1038 | break; |
| 1039 | } |
| 1040 | case kWeakGlobal: |
| 1041 | { |
| 1042 | JavaVMExt* vm = Runtime::Current()->GetJavaVM(); |
| 1043 | IndirectReferenceTable& weak_globals = vm->weak_globals; |
| 1044 | MutexLock mu(vm->weak_globals_lock); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 1045 | result = const_cast<Object*>(weak_globals.Get(ref)); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 1046 | if (result == kClearedJniWeakGlobal) { |
| 1047 | // This is a special case where it's okay to return NULL. |
| 1048 | return NULL; |
| 1049 | } |
| 1050 | break; |
| 1051 | } |
| 1052 | case kSirtOrInvalid: |
| 1053 | default: |
| 1054 | // TODO: make stack indirect reference table lookup more efficient |
| 1055 | // Check if this is a local reference in the SIRT |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1056 | if (SirtContains(obj)) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1057 | result = *reinterpret_cast<Object**>(obj); // Read from SIRT |
Elliott Hughes | c2dc62d | 2012-01-17 20:06:12 -0800 | [diff] [blame] | 1058 | } else if (Runtime::Current()->GetJavaVM()->work_around_app_jni_bugs) { |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 1059 | // Assume an invalid local reference is actually a direct pointer. |
| 1060 | result = reinterpret_cast<Object*>(obj); |
| 1061 | } else { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1062 | result = kInvalidIndirectRefObject; |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | if (result == NULL) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 1067 | JniAbortF(NULL, "use of deleted %s %p", ToStr<IndirectRefKind>(kind).c_str(), obj); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1068 | } else { |
| 1069 | if (result != kInvalidIndirectRefObject) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1070 | Runtime::Current()->GetHeap()->VerifyObject(result); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1071 | } |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 1072 | } |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 1073 | return result; |
| 1074 | } |
| 1075 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1076 | class CountStackDepthVisitor : public StackVisitor { |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1077 | public: |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1078 | CountStackDepthVisitor(const ManagedStack* stack, |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 1079 | const std::vector<TraceStackFrame>* trace_stack) |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1080 | SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1081 | : StackVisitor(stack, trace_stack, NULL), |
| 1082 | depth_(0), skip_depth_(0), skipping_(true) {} |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 1083 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1084 | bool VisitFrame() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) { |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 1085 | // 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] | 1086 | // Note we also skip the frame if it doesn't have a method (namely the callee |
| 1087 | // save frame) |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1088 | Method* m = GetMethod(); |
| 1089 | if (skipping_ && !m->IsRuntimeMethod() && |
| 1090 | !Throwable::GetJavaLangThrowable()->IsAssignableFrom(m->GetDeclaringClass())) { |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 1091 | skipping_ = false; |
| 1092 | } |
| 1093 | if (!skipping_) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1094 | if (!m->IsRuntimeMethod()) { // Ignore runtime frames (in particular callee save). |
Ian Rogers | 6b0870d | 2011-12-15 19:38:12 -0800 | [diff] [blame] | 1095 | ++depth_; |
| 1096 | } |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 1097 | } else { |
| 1098 | ++skip_depth_; |
| 1099 | } |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 1100 | return true; |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 1101 | } |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1102 | |
| 1103 | int GetDepth() const { |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1104 | return depth_; |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1105 | } |
| 1106 | |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 1107 | int GetSkipDepth() const { |
| 1108 | return skip_depth_; |
| 1109 | } |
| 1110 | |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1111 | private: |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1112 | uint32_t depth_; |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 1113 | uint32_t skip_depth_; |
| 1114 | bool skipping_; |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1115 | }; |
| 1116 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1117 | class BuildInternalStackTraceVisitor : public StackVisitor { |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1118 | public: |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1119 | explicit BuildInternalStackTraceVisitor(const ManagedStack* stack, |
| 1120 | const std::vector<TraceStackFrame>* trace_stack, |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 1121 | int skip_depth) |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1122 | : StackVisitor(stack, trace_stack, NULL), |
| 1123 | skip_depth_(skip_depth), count_(0), dex_pc_trace_(NULL), method_trace_(NULL) {} |
Ian Rogers | 283ed0d | 2012-02-16 15:25:09 -0800 | [diff] [blame] | 1124 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1125 | bool Init(int depth, const ScopedObjectAccess& soa) |
| 1126 | SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) { |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1127 | // Allocate method trace with an extra slot that will hold the PC trace |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1128 | SirtRef<ObjectArray<Object> > |
| 1129 | method_trace(Runtime::Current()->GetClassLinker()->AllocObjectArray<Object>(depth + 1)); |
| 1130 | if (method_trace.get() == NULL) { |
Ian Rogers | 283ed0d | 2012-02-16 15:25:09 -0800 | [diff] [blame] | 1131 | return false; |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 1132 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1133 | IntArray* dex_pc_trace = IntArray::Alloc(depth); |
| 1134 | if (dex_pc_trace == NULL) { |
Ian Rogers | 283ed0d | 2012-02-16 15:25:09 -0800 | [diff] [blame] | 1135 | return false; |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 1136 | } |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1137 | // Save PC trace in last element of method trace, also places it into the |
| 1138 | // object graph. |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1139 | method_trace->Set(depth, dex_pc_trace); |
| 1140 | // Set the Object*s and assert that no thread suspension is now possible. |
Ian Rogers | 52673ff | 2012-06-27 23:25:34 -0700 | [diff] [blame] | 1141 | const char* last_no_suspend_cause = |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1142 | soa.Self()->StartAssertNoThreadSuspension("Building internal stack trace"); |
Ian Rogers | 52673ff | 2012-06-27 23:25:34 -0700 | [diff] [blame] | 1143 | CHECK(last_no_suspend_cause == NULL) << last_no_suspend_cause; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1144 | method_trace_ = method_trace.get(); |
| 1145 | dex_pc_trace_ = dex_pc_trace; |
Ian Rogers | 283ed0d | 2012-02-16 15:25:09 -0800 | [diff] [blame] | 1146 | return true; |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1147 | } |
| 1148 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1149 | virtual ~BuildInternalStackTraceVisitor() { |
Ian Rogers | 52673ff | 2012-06-27 23:25:34 -0700 | [diff] [blame] | 1150 | if (method_trace_ != NULL) { |
| 1151 | Thread::Current()->EndAssertNoThreadSuspension(NULL); |
| 1152 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1153 | } |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1154 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1155 | bool VisitFrame() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1156 | if (method_trace_ == NULL || dex_pc_trace_ == NULL) { |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 1157 | return true; // We're probably trying to fillInStackTrace for an OutOfMemoryError. |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 1158 | } |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 1159 | if (skip_depth_ > 0) { |
| 1160 | skip_depth_--; |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 1161 | return true; |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 1162 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1163 | Method* m = GetMethod(); |
| 1164 | if (m->IsRuntimeMethod()) { |
| 1165 | return true; // Ignore runtime frames (in particular callee save). |
Ian Rogers | 6b0870d | 2011-12-15 19:38:12 -0800 | [diff] [blame] | 1166 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1167 | method_trace_->Set(count_, m); |
| 1168 | dex_pc_trace_->Set(count_, GetDexPc()); |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1169 | ++count_; |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 1170 | return true; |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1171 | } |
| 1172 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1173 | ObjectArray<Object>* GetInternalStackTrace() const { |
| 1174 | return method_trace_; |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | private: |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 1178 | // How many more frames to skip. |
| 1179 | int32_t skip_depth_; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1180 | // Current position down stack trace. |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1181 | uint32_t count_; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1182 | // Array of dex PC values. |
| 1183 | IntArray* dex_pc_trace_; |
| 1184 | // An array of the methods on the stack, the last entry is a reference to the PC trace. |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1185 | ObjectArray<Object>* method_trace_; |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1186 | }; |
| 1187 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1188 | void Thread::PushSirt(StackIndirectReferenceTable* sirt) { |
| 1189 | sirt->SetLink(top_sirt_); |
| 1190 | top_sirt_ = sirt; |
| 1191 | } |
| 1192 | |
| 1193 | StackIndirectReferenceTable* Thread::PopSirt() { |
| 1194 | CHECK(top_sirt_ != NULL); |
| 1195 | StackIndirectReferenceTable* sirt = top_sirt_; |
| 1196 | top_sirt_ = top_sirt_->GetLink(); |
| 1197 | return sirt; |
| 1198 | } |
| 1199 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1200 | jobject Thread::CreateInternalStackTrace(const ScopedObjectAccess& soa) const { |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1201 | // Compute depth of stack |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1202 | CountStackDepthVisitor count_visitor(GetManagedStack(), GetTraceStack()); |
| 1203 | count_visitor.WalkStack(); |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1204 | int32_t depth = count_visitor.GetDepth(); |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 1205 | int32_t skip_depth = count_visitor.GetSkipDepth(); |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 1206 | |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1207 | // Build internal stack trace |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1208 | BuildInternalStackTraceVisitor build_trace_visitor(GetManagedStack(), GetTraceStack(), |
| 1209 | skip_depth); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1210 | if (!build_trace_visitor.Init(depth, soa)) { |
Ian Rogers | 283ed0d | 2012-02-16 15:25:09 -0800 | [diff] [blame] | 1211 | return NULL; // Allocation failed |
Ian Rogers | 283ed0d | 2012-02-16 15:25:09 -0800 | [diff] [blame] | 1212 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1213 | build_trace_visitor.WalkStack(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1214 | return soa.AddLocalReference<jobjectArray>(build_trace_visitor.GetInternalStackTrace()); |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1215 | } |
| 1216 | |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 1217 | jobjectArray Thread::InternalStackTraceToStackTraceElementArray(JNIEnv* env, jobject internal, |
| 1218 | jobjectArray output_array, int* stack_depth) { |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1219 | // Transition into runnable state to work on Object*/Array* |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1220 | ScopedObjectAccess soa(env); |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1221 | // Decode the internal stack trace into the depth, method trace and PC trace |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1222 | ObjectArray<Object>* method_trace = soa.Decode<ObjectArray<Object>*>(internal); |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 1223 | int32_t depth = method_trace->GetLength() - 1; |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1224 | IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth)); |
| 1225 | |
| 1226 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 1227 | |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 1228 | jobjectArray result; |
| 1229 | ObjectArray<StackTraceElement>* java_traces; |
| 1230 | if (output_array != NULL) { |
| 1231 | // Reuse the array we were given. |
| 1232 | result = output_array; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1233 | java_traces = soa.Decode<ObjectArray<StackTraceElement>*>(output_array); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 1234 | // ...adjusting the number of frames we'll write to not exceed the array length. |
| 1235 | depth = std::min(depth, java_traces->GetLength()); |
| 1236 | } else { |
| 1237 | // Create java_trace array and place in local reference table |
| 1238 | java_traces = class_linker->AllocStackTraceElementArray(depth); |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 1239 | if (java_traces == NULL) { |
| 1240 | return NULL; |
| 1241 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1242 | result = soa.AddLocalReference<jobjectArray>(java_traces); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 1243 | } |
| 1244 | |
| 1245 | if (stack_depth != NULL) { |
| 1246 | *stack_depth = depth; |
| 1247 | } |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 1248 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1249 | MethodHelper mh; |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1250 | for (int32_t i = 0; i < depth; ++i) { |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1251 | // Prepare parameters for StackTraceElement(String cls, String method, String file, int line) |
| 1252 | Method* method = down_cast<Method*>(method_trace->Get(i)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1253 | mh.ChangeMethod(method); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1254 | uint32_t dex_pc = pc_trace->Get(i); |
| 1255 | int32_t line_number = mh.GetLineNumFromDexPC(dex_pc); |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1256 | // Allocate element, potentially triggering GC |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1257 | // TODO: reuse class_name_object via Class::name_? |
Ian Rogers | 4860131 | 2011-12-07 16:45:19 -0800 | [diff] [blame] | 1258 | const char* descriptor = mh.GetDeclaringClassDescriptor(); |
| 1259 | CHECK(descriptor != NULL); |
| 1260 | std::string class_name(PrettyDescriptor(descriptor)); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1261 | SirtRef<String> class_name_object(String::AllocFromModifiedUtf8(class_name.c_str())); |
| 1262 | if (class_name_object.get() == NULL) { |
| 1263 | return NULL; |
| 1264 | } |
Ian Rogers | 4860131 | 2011-12-07 16:45:19 -0800 | [diff] [blame] | 1265 | const char* method_name = mh.GetName(); |
| 1266 | CHECK(method_name != NULL); |
| 1267 | SirtRef<String> method_name_object(String::AllocFromModifiedUtf8(method_name)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1268 | if (method_name_object.get() == NULL) { |
| 1269 | return NULL; |
| 1270 | } |
Ian Rogers | 4860131 | 2011-12-07 16:45:19 -0800 | [diff] [blame] | 1271 | const char* source_file = mh.GetDeclaringClassSourceFile(); |
| 1272 | SirtRef<String> source_name_object(String::AllocFromModifiedUtf8(source_file)); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1273 | StackTraceElement* obj = StackTraceElement::Alloc(class_name_object.get(), |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1274 | method_name_object.get(), |
| 1275 | source_name_object.get(), |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1276 | line_number); |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 1277 | if (obj == NULL) { |
| 1278 | return NULL; |
| 1279 | } |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1280 | #ifdef MOVING_GARBAGE_COLLECTOR |
| 1281 | // Re-read after potential GC |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1282 | java_traces = Decode<ObjectArray<Object>*>(soa.Env(), result); |
| 1283 | method_trace = down_cast<ObjectArray<Object>*>(Decode<Object*>(soa.Env(), internal)); |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1284 | pc_trace = down_cast<IntArray*>(method_trace->Get(depth)); |
| 1285 | #endif |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 1286 | java_traces->Set(i, obj); |
| 1287 | } |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1288 | return result; |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 1289 | } |
| 1290 | |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 1291 | void Thread::ThrowNewExceptionF(const char* exception_class_descriptor, const char* fmt, ...) { |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1292 | va_list args; |
| 1293 | va_start(args, fmt); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 1294 | ThrowNewExceptionV(exception_class_descriptor, fmt, args); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1295 | va_end(args); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | void Thread::ThrowNewExceptionV(const char* exception_class_descriptor, const char* fmt, va_list ap) { |
| 1299 | std::string msg; |
| 1300 | StringAppendV(&msg, fmt, ap); |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 1301 | ThrowNewException(exception_class_descriptor, msg.c_str()); |
| 1302 | } |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1303 | |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 1304 | void Thread::ThrowNewException(const char* exception_class_descriptor, const char* msg) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1305 | AssertNoPendingException(); // Callers should either clear or call ThrowNewWrappedException. |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 1306 | ThrowNewWrappedException(exception_class_descriptor, msg); |
| 1307 | } |
| 1308 | |
| 1309 | void Thread::ThrowNewWrappedException(const char* exception_class_descriptor, const char* msg) { |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 1310 | // Convert "Ljava/lang/Exception;" into JNI-style "java/lang/Exception". |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1311 | CHECK_EQ('L', exception_class_descriptor[0]); |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 1312 | std::string descriptor(exception_class_descriptor + 1); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1313 | CHECK_EQ(';', descriptor[descriptor.length() - 1]); |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 1314 | descriptor.erase(descriptor.length() - 1); |
| 1315 | |
| 1316 | JNIEnv* env = GetJniEnv(); |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 1317 | jobject cause = env->ExceptionOccurred(); |
| 1318 | env->ExceptionClear(); |
| 1319 | |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 1320 | ScopedLocalRef<jclass> exception_class(env, env->FindClass(descriptor.c_str())); |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 1321 | if (exception_class.get() == NULL) { |
| 1322 | LOG(ERROR) << "Couldn't throw new " << descriptor << " because JNI FindClass failed: " |
| 1323 | << PrettyTypeOf(GetException()); |
| 1324 | CHECK(IsExceptionPending()); |
| 1325 | return; |
| 1326 | } |
Brian Carlstrom | ebd1fd2 | 2011-12-07 15:46:26 -0800 | [diff] [blame] | 1327 | if (!Runtime::Current()->IsStarted()) { |
| 1328 | // Something is trying to throw an exception without a started |
| 1329 | // runtime, which is the common case in the compiler. We won't be |
| 1330 | // able to invoke the constructor of the exception, so use |
| 1331 | // AllocObject which will not invoke a constructor. |
| 1332 | ScopedLocalRef<jthrowable> exception( |
| 1333 | env, reinterpret_cast<jthrowable>(env->AllocObject(exception_class.get()))); |
| 1334 | if (exception.get() != NULL) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1335 | ScopedObjectAccessUnchecked soa(env); |
| 1336 | Throwable* t = reinterpret_cast<Throwable*>(soa.Self()->DecodeJObject(exception.get())); |
Ian Rogers | 02fbef0 | 2012-01-31 22:15:33 -0800 | [diff] [blame] | 1337 | t->SetDetailMessage(String::AllocFromModifiedUtf8(msg)); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1338 | soa.Self()->SetException(t); |
Brian Carlstrom | ebd1fd2 | 2011-12-07 15:46:26 -0800 | [diff] [blame] | 1339 | } else { |
| 1340 | LOG(ERROR) << "Couldn't throw new " << descriptor << " because JNI AllocObject failed: " |
| 1341 | << PrettyTypeOf(GetException()); |
| 1342 | CHECK(IsExceptionPending()); |
| 1343 | } |
| 1344 | return; |
| 1345 | } |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 1346 | int rc = ::art::ThrowNewException(env, exception_class.get(), msg, cause); |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 1347 | if (rc != JNI_OK) { |
| 1348 | LOG(ERROR) << "Couldn't throw new " << descriptor << " because JNI ThrowNew failed: " |
| 1349 | << PrettyTypeOf(GetException()); |
| 1350 | CHECK(IsExceptionPending()); |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 1351 | } |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1352 | } |
| 1353 | |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1354 | void Thread::ThrowOutOfMemoryError(const char* msg) { |
| 1355 | LOG(ERROR) << StringPrintf("Throwing OutOfMemoryError \"%s\"%s", |
| 1356 | msg, (throwing_OutOfMemoryError_ ? " (recursive case)" : "")); |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 1357 | if (!throwing_OutOfMemoryError_) { |
| 1358 | throwing_OutOfMemoryError_ = true; |
Elliott Hughes | 57aba86 | 2012-06-20 14:00:47 -0700 | [diff] [blame] | 1359 | ThrowNewException("Ljava/lang/OutOfMemoryError;", msg); |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 1360 | } else { |
Elliott Hughes | 225f5a1 | 2012-06-11 11:23:48 -0700 | [diff] [blame] | 1361 | Dump(LOG(ERROR)); // The pre-allocated OOME has no stack, so help out and log one. |
| 1362 | SetException(Runtime::Current()->GetPreAllocatedOutOfMemoryError()); |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 1363 | } |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 1364 | throwing_OutOfMemoryError_ = false; |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 1365 | } |
| 1366 | |
Elliott Hughes | 498508c | 2011-10-17 14:58:22 -0700 | [diff] [blame] | 1367 | Thread* Thread::CurrentFromGdb() { |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 1368 | return Thread::Current(); |
| 1369 | } |
| 1370 | |
| 1371 | void Thread::DumpFromGdb() const { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1372 | std::ostringstream ss; |
| 1373 | Dump(ss); |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 1374 | std::string str(ss.str()); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1375 | // log to stderr for debugging command line processes |
| 1376 | std::cerr << str; |
| 1377 | #ifdef HAVE_ANDROID_OS |
| 1378 | // log to logcat for debugging frameworks processes |
| 1379 | LOG(INFO) << str; |
| 1380 | #endif |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 1381 | } |
| 1382 | |
Elliott Hughes | 98e2017 | 2012-04-24 15:38:13 -0700 | [diff] [blame] | 1383 | struct EntryPointInfo { |
| 1384 | uint32_t offset; |
| 1385 | const char* name; |
| 1386 | }; |
| 1387 | #define ENTRY_POINT_INFO(x) { ENTRYPOINT_OFFSET(x), #x } |
| 1388 | static const EntryPointInfo gThreadEntryPointInfo[] = { |
| 1389 | ENTRY_POINT_INFO(pAllocArrayFromCode), |
| 1390 | ENTRY_POINT_INFO(pAllocArrayFromCodeWithAccessCheck), |
| 1391 | ENTRY_POINT_INFO(pAllocObjectFromCode), |
| 1392 | ENTRY_POINT_INFO(pAllocObjectFromCodeWithAccessCheck), |
| 1393 | ENTRY_POINT_INFO(pCheckAndAllocArrayFromCode), |
| 1394 | ENTRY_POINT_INFO(pCheckAndAllocArrayFromCodeWithAccessCheck), |
| 1395 | ENTRY_POINT_INFO(pInstanceofNonTrivialFromCode), |
| 1396 | ENTRY_POINT_INFO(pCanPutArrayElementFromCode), |
| 1397 | ENTRY_POINT_INFO(pCheckCastFromCode), |
| 1398 | ENTRY_POINT_INFO(pDebugMe), |
| 1399 | ENTRY_POINT_INFO(pUpdateDebuggerFromCode), |
| 1400 | ENTRY_POINT_INFO(pInitializeStaticStorage), |
| 1401 | ENTRY_POINT_INFO(pInitializeTypeAndVerifyAccessFromCode), |
| 1402 | ENTRY_POINT_INFO(pInitializeTypeFromCode), |
| 1403 | ENTRY_POINT_INFO(pResolveStringFromCode), |
| 1404 | ENTRY_POINT_INFO(pSet32Instance), |
| 1405 | ENTRY_POINT_INFO(pSet32Static), |
| 1406 | ENTRY_POINT_INFO(pSet64Instance), |
| 1407 | ENTRY_POINT_INFO(pSet64Static), |
| 1408 | ENTRY_POINT_INFO(pSetObjInstance), |
| 1409 | ENTRY_POINT_INFO(pSetObjStatic), |
| 1410 | ENTRY_POINT_INFO(pGet32Instance), |
| 1411 | ENTRY_POINT_INFO(pGet32Static), |
| 1412 | ENTRY_POINT_INFO(pGet64Instance), |
| 1413 | ENTRY_POINT_INFO(pGet64Static), |
| 1414 | ENTRY_POINT_INFO(pGetObjInstance), |
| 1415 | ENTRY_POINT_INFO(pGetObjStatic), |
| 1416 | ENTRY_POINT_INFO(pHandleFillArrayDataFromCode), |
Elliott Hughes | 98e2017 | 2012-04-24 15:38:13 -0700 | [diff] [blame] | 1417 | ENTRY_POINT_INFO(pFindNativeMethod), |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1418 | ENTRY_POINT_INFO(pJniMethodStart), |
| 1419 | ENTRY_POINT_INFO(pJniMethodStartSynchronized), |
| 1420 | ENTRY_POINT_INFO(pJniMethodEnd), |
| 1421 | ENTRY_POINT_INFO(pJniMethodEndSynchronized), |
| 1422 | ENTRY_POINT_INFO(pJniMethodEndWithReference), |
| 1423 | ENTRY_POINT_INFO(pJniMethodEndWithReferenceSynchronized), |
Elliott Hughes | 98e2017 | 2012-04-24 15:38:13 -0700 | [diff] [blame] | 1424 | ENTRY_POINT_INFO(pLockObjectFromCode), |
| 1425 | ENTRY_POINT_INFO(pUnlockObjectFromCode), |
| 1426 | ENTRY_POINT_INFO(pCmpgDouble), |
| 1427 | ENTRY_POINT_INFO(pCmpgFloat), |
| 1428 | ENTRY_POINT_INFO(pCmplDouble), |
| 1429 | ENTRY_POINT_INFO(pCmplFloat), |
| 1430 | ENTRY_POINT_INFO(pDadd), |
| 1431 | ENTRY_POINT_INFO(pDdiv), |
| 1432 | ENTRY_POINT_INFO(pDmul), |
| 1433 | ENTRY_POINT_INFO(pDsub), |
| 1434 | ENTRY_POINT_INFO(pF2d), |
| 1435 | ENTRY_POINT_INFO(pFmod), |
| 1436 | ENTRY_POINT_INFO(pI2d), |
| 1437 | ENTRY_POINT_INFO(pL2d), |
| 1438 | ENTRY_POINT_INFO(pD2f), |
| 1439 | ENTRY_POINT_INFO(pFadd), |
| 1440 | ENTRY_POINT_INFO(pFdiv), |
| 1441 | ENTRY_POINT_INFO(pFmodf), |
| 1442 | ENTRY_POINT_INFO(pFmul), |
| 1443 | ENTRY_POINT_INFO(pFsub), |
| 1444 | ENTRY_POINT_INFO(pI2f), |
| 1445 | ENTRY_POINT_INFO(pL2f), |
| 1446 | ENTRY_POINT_INFO(pD2iz), |
| 1447 | ENTRY_POINT_INFO(pF2iz), |
| 1448 | ENTRY_POINT_INFO(pIdivmod), |
| 1449 | ENTRY_POINT_INFO(pD2l), |
| 1450 | ENTRY_POINT_INFO(pF2l), |
| 1451 | ENTRY_POINT_INFO(pLdiv), |
| 1452 | ENTRY_POINT_INFO(pLdivmod), |
| 1453 | ENTRY_POINT_INFO(pLmul), |
| 1454 | ENTRY_POINT_INFO(pShlLong), |
| 1455 | ENTRY_POINT_INFO(pShrLong), |
| 1456 | ENTRY_POINT_INFO(pUshrLong), |
| 1457 | ENTRY_POINT_INFO(pIndexOf), |
| 1458 | ENTRY_POINT_INFO(pMemcmp16), |
| 1459 | ENTRY_POINT_INFO(pStringCompareTo), |
| 1460 | ENTRY_POINT_INFO(pMemcpy), |
| 1461 | ENTRY_POINT_INFO(pUnresolvedDirectMethodTrampolineFromCode), |
| 1462 | ENTRY_POINT_INFO(pInvokeDirectTrampolineWithAccessCheck), |
| 1463 | ENTRY_POINT_INFO(pInvokeInterfaceTrampoline), |
| 1464 | ENTRY_POINT_INFO(pInvokeInterfaceTrampolineWithAccessCheck), |
| 1465 | ENTRY_POINT_INFO(pInvokeStaticTrampolineWithAccessCheck), |
| 1466 | ENTRY_POINT_INFO(pInvokeSuperTrampolineWithAccessCheck), |
| 1467 | ENTRY_POINT_INFO(pInvokeVirtualTrampolineWithAccessCheck), |
| 1468 | ENTRY_POINT_INFO(pCheckSuspendFromCode), |
| 1469 | ENTRY_POINT_INFO(pTestSuspendFromCode), |
| 1470 | ENTRY_POINT_INFO(pDeliverException), |
| 1471 | ENTRY_POINT_INFO(pThrowAbstractMethodErrorFromCode), |
| 1472 | ENTRY_POINT_INFO(pThrowArrayBoundsFromCode), |
| 1473 | ENTRY_POINT_INFO(pThrowDivZeroFromCode), |
| 1474 | ENTRY_POINT_INFO(pThrowNoSuchMethodFromCode), |
| 1475 | ENTRY_POINT_INFO(pThrowNullPointerFromCode), |
| 1476 | ENTRY_POINT_INFO(pThrowStackOverflowFromCode), |
| 1477 | ENTRY_POINT_INFO(pThrowVerificationErrorFromCode), |
| 1478 | }; |
| 1479 | #undef ENTRY_POINT_INFO |
| 1480 | |
Elliott Hughes | 28fa76d | 2012-04-09 17:31:46 -0700 | [diff] [blame] | 1481 | void Thread::DumpThreadOffset(std::ostream& os, uint32_t offset, size_t size_of_pointers) { |
| 1482 | CHECK_EQ(size_of_pointers, 4U); // TODO: support 64-bit targets. |
Elliott Hughes | 98e2017 | 2012-04-24 15:38:13 -0700 | [diff] [blame] | 1483 | |
| 1484 | #define DO_THREAD_OFFSET(x) if (offset == static_cast<uint32_t>(OFFSETOF_VOLATILE_MEMBER(Thread, x))) { os << # x; return; } |
| 1485 | DO_THREAD_OFFSET(card_table_); |
| 1486 | DO_THREAD_OFFSET(exception_); |
| 1487 | DO_THREAD_OFFSET(jni_env_); |
| 1488 | DO_THREAD_OFFSET(self_); |
| 1489 | DO_THREAD_OFFSET(stack_end_); |
| 1490 | DO_THREAD_OFFSET(state_); |
| 1491 | DO_THREAD_OFFSET(suspend_count_); |
| 1492 | DO_THREAD_OFFSET(thin_lock_id_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1493 | //DO_THREAD_OFFSET(top_of_managed_stack_); |
| 1494 | //DO_THREAD_OFFSET(top_of_managed_stack_pc_); |
Elliott Hughes | 98e2017 | 2012-04-24 15:38:13 -0700 | [diff] [blame] | 1495 | DO_THREAD_OFFSET(top_sirt_); |
Elliott Hughes | 28fa76d | 2012-04-09 17:31:46 -0700 | [diff] [blame] | 1496 | #undef DO_THREAD_OFFSET |
Elliott Hughes | 98e2017 | 2012-04-24 15:38:13 -0700 | [diff] [blame] | 1497 | |
| 1498 | size_t entry_point_count = arraysize(gThreadEntryPointInfo); |
| 1499 | CHECK_EQ(entry_point_count * size_of_pointers, sizeof(EntryPoints)); |
| 1500 | uint32_t expected_offset = OFFSETOF_MEMBER(Thread, entrypoints_); |
| 1501 | for (size_t i = 0; i < entry_point_count; ++i) { |
| 1502 | CHECK_EQ(gThreadEntryPointInfo[i].offset, expected_offset); |
| 1503 | expected_offset += size_of_pointers; |
| 1504 | if (gThreadEntryPointInfo[i].offset == offset) { |
| 1505 | os << gThreadEntryPointInfo[i].name; |
| 1506 | return; |
| 1507 | } |
| 1508 | } |
| 1509 | os << offset; |
Elliott Hughes | 28fa76d | 2012-04-09 17:31:46 -0700 | [diff] [blame] | 1510 | } |
| 1511 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1512 | static const bool kDebugExceptionDelivery = false; |
| 1513 | class CatchBlockStackVisitor : public StackVisitor { |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1514 | public: |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1515 | CatchBlockStackVisitor(Thread* self, Throwable* exception) |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1516 | SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1517 | : StackVisitor(self->GetManagedStack(), self->GetTraceStack(), self->GetLongJumpContext()), |
| 1518 | self_(self), exception_(exception), to_find_(exception->GetClass()), throw_method_(NULL), |
| 1519 | throw_frame_id_(0), throw_dex_pc_(0), handler_quick_frame_(NULL), |
| 1520 | handler_quick_frame_pc_(0), handler_dex_pc_(0), native_method_count_(0), |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 1521 | method_tracing_active_(Runtime::Current()->IsMethodTracingActive()) { |
Ian Rogers | 52673ff | 2012-06-27 23:25:34 -0700 | [diff] [blame] | 1522 | // Exception not in root sets, can't allow GC. |
| 1523 | last_no_assert_suspension_cause_ = self->StartAssertNoThreadSuspension("Finding catch block"); |
| 1524 | } |
| 1525 | |
| 1526 | ~CatchBlockStackVisitor() { |
| 1527 | LOG(FATAL) << "UNREACHABLE"; // Expected to take long jump. |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1528 | } |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 1529 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1530 | bool VisitFrame() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) |
| 1531 | SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1532 | Method* method = GetMethod(); |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 1533 | if (method == NULL) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1534 | // This is the upcall, we remember the frame and last pc so that we may long jump to them. |
| 1535 | handler_quick_frame_pc_ = GetCurrentQuickFramePc(); |
| 1536 | handler_quick_frame_ = GetCurrentQuickFrame(); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 1537 | return false; // End stack walk. |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 1538 | } |
| 1539 | uint32_t dex_pc = DexFile::kDexNoIndex; |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 1540 | if (method->IsRuntimeMethod()) { |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 1541 | // ignore callee save method |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 1542 | DCHECK(method->IsCalleeSaveMethod()); |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 1543 | } else { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1544 | if (throw_method_ == NULL) { |
| 1545 | throw_method_ = method; |
| 1546 | throw_frame_id_ = GetFrameId(); |
| 1547 | throw_dex_pc_ = GetDexPc(); |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1548 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1549 | if (method->IsNative()) { |
| 1550 | native_method_count_++; |
| 1551 | } else { |
| 1552 | // Unwind stack when an exception occurs during method tracing |
| 1553 | if (UNLIKELY(method_tracing_active_ && IsTraceExitPc(GetCurrentQuickFramePc()))) { |
| 1554 | uintptr_t pc = AdjustQuickFramePcForDexPcComputation(TraceMethodUnwindFromCode(Thread::Current())); |
| 1555 | dex_pc = method->ToDexPC(pc); |
| 1556 | } else { |
| 1557 | dex_pc = GetDexPc(); |
| 1558 | } |
| 1559 | } |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 1560 | } |
| 1561 | if (dex_pc != DexFile::kDexNoIndex) { |
| 1562 | uint32_t found_dex_pc = method->FindCatchBlock(to_find_, dex_pc); |
| 1563 | if (found_dex_pc != DexFile::kDexNoIndex) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1564 | handler_dex_pc_ = found_dex_pc; |
| 1565 | handler_quick_frame_pc_ = method->ToNativePC(found_dex_pc); |
| 1566 | handler_quick_frame_ = GetCurrentQuickFrame(); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 1567 | return false; // End stack walk. |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 1568 | } |
| 1569 | } |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 1570 | return true; // Continue stack walk. |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 1571 | } |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1572 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1573 | void DoLongJump() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1574 | Method* catch_method = *handler_quick_frame_; |
Elliott Hughes | 6e9d22c | 2012-06-22 15:02:37 -0700 | [diff] [blame] | 1575 | Dbg::PostException(self_, throw_frame_id_, throw_method_, throw_dex_pc_, |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1576 | catch_method, handler_dex_pc_, exception_); |
| 1577 | if (kDebugExceptionDelivery) { |
| 1578 | if (catch_method == NULL) { |
| 1579 | LOG(INFO) << "Handler is upcall"; |
| 1580 | } else { |
| 1581 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 1582 | const DexFile& dex_file = |
| 1583 | class_linker->FindDexFile(catch_method->GetDeclaringClass()->GetDexCache()); |
| 1584 | int line_number = dex_file.GetLineNumFromPC(catch_method, handler_dex_pc_); |
| 1585 | LOG(INFO) << "Handler: " << PrettyMethod(catch_method) << " (line: " << line_number << ")"; |
| 1586 | } |
| 1587 | } |
Ian Rogers | 52673ff | 2012-06-27 23:25:34 -0700 | [diff] [blame] | 1588 | self_->SetException(exception_); // Exception back in root set. |
| 1589 | self_->EndAssertNoThreadSuspension(last_no_assert_suspension_cause_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1590 | // Place context back on thread so it will be available when we continue. |
| 1591 | self_->ReleaseLongJumpContext(context_); |
| 1592 | context_->SetSP(reinterpret_cast<uintptr_t>(handler_quick_frame_)); |
| 1593 | CHECK_NE(handler_quick_frame_pc_, 0u); |
| 1594 | context_->SetPC(handler_quick_frame_pc_); |
| 1595 | context_->SmashCallerSaves(); |
| 1596 | context_->DoLongJump(); |
| 1597 | } |
| 1598 | |
| 1599 | private: |
| 1600 | Thread* self_; |
| 1601 | Throwable* exception_; |
| 1602 | // The type of the exception catch block to find. |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1603 | Class* to_find_; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1604 | Method* throw_method_; |
| 1605 | JDWP::FrameId throw_frame_id_; |
| 1606 | uint32_t throw_dex_pc_; |
| 1607 | // Quick frame with found handler or last frame if no handler found. |
| 1608 | Method** handler_quick_frame_; |
| 1609 | // PC to branch to for the handler. |
| 1610 | uintptr_t handler_quick_frame_pc_; |
| 1611 | // Associated dex PC. |
| 1612 | uint32_t handler_dex_pc_; |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1613 | // Number of native methods passed in crawl (equates to number of SIRTs to pop) |
| 1614 | uint32_t native_method_count_; |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 1615 | // Is method tracing active? |
| 1616 | const bool method_tracing_active_; |
Ian Rogers | 52673ff | 2012-06-27 23:25:34 -0700 | [diff] [blame] | 1617 | // Support for nesting no thread suspension checks. |
| 1618 | const char* last_no_assert_suspension_cause_; |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1619 | }; |
| 1620 | |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1621 | void Thread::DeliverException() { |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 1622 | Throwable* exception = GetException(); // Get exception from thread |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 1623 | CHECK(exception != NULL); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1624 | // Don't leave exception visible while we try to find the handler, which may cause class |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 1625 | // resolution. |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1626 | ClearException(); |
| 1627 | if (kDebugExceptionDelivery) { |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 1628 | String* msg = exception->GetDetailMessage(); |
| 1629 | std::string str_msg(msg != NULL ? msg->ToModifiedUtf8() : ""); |
| 1630 | DumpStack(LOG(INFO) << "Delivering exception: " << PrettyTypeOf(exception) |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 1631 | << ": " << str_msg << "\n"); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1632 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1633 | CatchBlockStackVisitor catch_finder(this, exception); |
| 1634 | catch_finder.WalkStack(true); |
| 1635 | catch_finder.DoLongJump(); |
Ian Rogers | 9a8a888 | 2012-03-08 02:30:55 -0800 | [diff] [blame] | 1636 | LOG(FATAL) << "UNREACHABLE"; |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 1637 | } |
| 1638 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1639 | Context* Thread::GetLongJumpContext() { |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 1640 | Context* result = long_jump_context_; |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1641 | if (result == NULL) { |
| 1642 | result = Context::Create(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1643 | } else { |
| 1644 | long_jump_context_ = NULL; // Avoid context being shared. |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 1645 | } |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1646 | return result; |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 1647 | } |
| 1648 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1649 | Method* Thread::GetCurrentMethod(uint32_t* dex_pc, size_t* frame_id) const { |
| 1650 | struct CurrentMethodVisitor : public StackVisitor { |
| 1651 | CurrentMethodVisitor(const ManagedStack* stack, |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 1652 | const std::vector<TraceStackFrame>* trace_stack) |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1653 | SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1654 | : StackVisitor(stack, trace_stack, NULL), method_(NULL), dex_pc_(0), frame_id_(0) {} |
Elliott Hughes | 8be2d40 | 2012-02-23 14:22:41 -0800 | [diff] [blame] | 1655 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1656 | virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1657 | Method* m = GetMethod(); |
| 1658 | if (m->IsRuntimeMethod()) { |
| 1659 | // Continue if this is a runtime method. |
| 1660 | return true; |
| 1661 | } |
| 1662 | method_ = m; |
| 1663 | dex_pc_ = GetDexPc(); |
| 1664 | frame_id_ = GetFrameId(); |
| 1665 | return false; |
| 1666 | } |
| 1667 | Method* method_; |
| 1668 | uint32_t dex_pc_; |
| 1669 | size_t frame_id_; |
| 1670 | }; |
| 1671 | |
| 1672 | CurrentMethodVisitor visitor(GetManagedStack(), GetTraceStack()); |
| 1673 | visitor.WalkStack(false); |
| 1674 | if (dex_pc != NULL) { |
| 1675 | *dex_pc = visitor.dex_pc_; |
Elliott Hughes | 9fd66f5 | 2011-10-16 12:13:26 -0700 | [diff] [blame] | 1676 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1677 | if (frame_id != NULL) { |
| 1678 | *frame_id = visitor.frame_id_; |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 1679 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1680 | return visitor.method_; |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 1681 | } |
| 1682 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 1683 | bool Thread::HoldsLock(Object* object) { |
| 1684 | if (object == NULL) { |
| 1685 | return false; |
| 1686 | } |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 1687 | return object->GetThinLockId() == thin_lock_id_; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 1688 | } |
| 1689 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1690 | class ReferenceMapVisitor : public StackVisitor { |
Ian Rogers | d6b1f61 | 2011-09-27 13:38:14 -0700 | [diff] [blame] | 1691 | public: |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1692 | ReferenceMapVisitor(const ManagedStack* stack, const std::vector<TraceStackFrame>* trace_stack, |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 1693 | Context* context, Heap::RootVisitor* root_visitor, void* arg) |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1694 | SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 1695 | : StackVisitor(stack, trace_stack, context), root_visitor_(root_visitor), arg_(arg) {} |
Ian Rogers | d6b1f61 | 2011-09-27 13:38:14 -0700 | [diff] [blame] | 1696 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1697 | bool VisitFrame() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) { |
Brian Carlstrom | 6a4be3a | 2011-10-20 16:34:03 -0700 | [diff] [blame] | 1698 | if (false) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1699 | LOG(INFO) << "Visiting stack roots in " << PrettyMethod(GetMethod()) |
| 1700 | << StringPrintf("@ PC:%04x", GetDexPc()); |
Brian Carlstrom | 6a4be3a | 2011-10-20 16:34:03 -0700 | [diff] [blame] | 1701 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1702 | ShadowFrame* shadow_frame = GetCurrentShadowFrame(); |
| 1703 | if (shadow_frame != NULL) { |
| 1704 | shadow_frame->VisitRoots(root_visitor_, arg_); |
| 1705 | } else { |
| 1706 | Method* m = GetMethod(); |
| 1707 | // Process register map (which native and runtime methods don't have) |
Ian Rogers | 640495b | 2012-06-22 15:15:47 -0700 | [diff] [blame] | 1708 | if (!m->IsNative() && !m->IsRuntimeMethod() && !m->IsProxyMethod()) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1709 | const uint8_t* gc_map = m->GetGcMap(); |
| 1710 | CHECK(gc_map != NULL) << PrettyMethod(m); |
| 1711 | uint32_t gc_map_length = m->GetGcMapLength(); |
| 1712 | CHECK_NE(0U, gc_map_length) << PrettyMethod(m); |
| 1713 | verifier::PcToReferenceMap map(gc_map, gc_map_length); |
| 1714 | const uint8_t* reg_bitmap = map.FindBitMap(GetDexPc()); |
| 1715 | CHECK(reg_bitmap != NULL); |
| 1716 | const VmapTable vmap_table(m->GetVmapTableRaw()); |
| 1717 | const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem(); |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 1718 | DCHECK(code_item != NULL) << PrettyMethod(m); // Can't be NULL or how would we compile its instructions? |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1719 | uint32_t core_spills = m->GetCoreSpillMask(); |
| 1720 | uint32_t fp_spills = m->GetFpSpillMask(); |
| 1721 | size_t frame_size = m->GetFrameSizeInBytes(); |
| 1722 | // For all dex registers in the bitmap |
| 1723 | size_t num_regs = std::min(map.RegWidth() * 8, |
| 1724 | static_cast<size_t>(code_item->registers_size_)); |
Ian Rogers | 0ec569a | 2012-07-01 16:43:46 -0700 | [diff] [blame] | 1725 | Method** cur_quick_frame = GetCurrentQuickFrame(); |
| 1726 | DCHECK(cur_quick_frame != NULL); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1727 | for (size_t reg = 0; reg < num_regs; ++reg) { |
| 1728 | // Does this register hold a reference? |
| 1729 | if (TestBitmap(reg, reg_bitmap)) { |
| 1730 | uint32_t vmap_offset; |
| 1731 | Object* ref; |
| 1732 | if (vmap_table.IsInContext(reg, vmap_offset)) { |
| 1733 | // Compute the register we need to load from the context |
| 1734 | uint32_t spill_mask = core_spills; |
| 1735 | CHECK_LT(vmap_offset, static_cast<uint32_t>(__builtin_popcount(spill_mask))); |
| 1736 | uint32_t matches = 0; |
| 1737 | uint32_t spill_shifts = 0; |
| 1738 | while (matches != (vmap_offset + 1)) { |
| 1739 | DCHECK_NE(spill_mask, 0u); |
| 1740 | matches += spill_mask & 1; // Add 1 if the low bit is set |
| 1741 | spill_mask >>= 1; |
| 1742 | spill_shifts++; |
| 1743 | } |
| 1744 | spill_shifts--; // wind back one as we want the last match |
| 1745 | ref = reinterpret_cast<Object*>(GetGPR(spill_shifts)); |
| 1746 | } else { |
Ian Rogers | 0ec569a | 2012-07-01 16:43:46 -0700 | [diff] [blame] | 1747 | ref = reinterpret_cast<Object*>(GetVReg(cur_quick_frame, code_item, core_spills, |
| 1748 | fp_spills, frame_size, reg)); |
Ian Rogers | d6b1f61 | 2011-09-27 13:38:14 -0700 | [diff] [blame] | 1749 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1750 | if (ref != NULL) { |
| 1751 | root_visitor_(ref, arg_); |
| 1752 | } |
Shih-wei Liao | 4f894e3 | 2011-09-27 21:33:19 -0700 | [diff] [blame] | 1753 | } |
Ian Rogers | d6b1f61 | 2011-09-27 13:38:14 -0700 | [diff] [blame] | 1754 | } |
| 1755 | } |
| 1756 | } |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 1757 | return true; |
Ian Rogers | d6b1f61 | 2011-09-27 13:38:14 -0700 | [diff] [blame] | 1758 | } |
| 1759 | |
| 1760 | private: |
| 1761 | bool TestBitmap(int reg, const uint8_t* reg_vector) { |
| 1762 | return ((reg_vector[reg / 8] >> (reg % 8)) & 0x01) != 0; |
| 1763 | } |
| 1764 | |
Ian Rogers | d6b1f61 | 2011-09-27 13:38:14 -0700 | [diff] [blame] | 1765 | // Call-back when we visit a root |
| 1766 | Heap::RootVisitor* root_visitor_; |
| 1767 | // Argument to call-back |
| 1768 | void* arg_; |
| 1769 | }; |
| 1770 | |
| 1771 | void Thread::VisitRoots(Heap::RootVisitor* visitor, void* arg) { |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 1772 | if (exception_ != NULL) { |
| 1773 | visitor(exception_, arg); |
| 1774 | } |
| 1775 | if (peer_ != NULL) { |
| 1776 | visitor(peer_, arg); |
| 1777 | } |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1778 | if (class_loader_override_ != NULL) { |
| 1779 | visitor(class_loader_override_, arg); |
| 1780 | } |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 1781 | jni_env_->locals.VisitRoots(visitor, arg); |
| 1782 | jni_env_->monitors.VisitRoots(visitor, arg); |
Shih-wei Liao | 8dfc9d5 | 2011-09-28 18:06:15 -0700 | [diff] [blame] | 1783 | |
| 1784 | SirtVisitRoots(visitor, arg); |
| 1785 | |
Ian Rogers | d6b1f61 | 2011-09-27 13:38:14 -0700 | [diff] [blame] | 1786 | // Visit roots on this thread's stack |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1787 | Context* context = GetLongJumpContext(); |
| 1788 | ReferenceMapVisitor mapper(GetManagedStack(), GetTraceStack(), context, visitor, arg); |
| 1789 | mapper.WalkStack(); |
| 1790 | ReleaseLongJumpContext(context); |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 1791 | } |
| 1792 | |
jeffhao | 2504552 | 2012-03-13 19:34:37 -0700 | [diff] [blame] | 1793 | #if VERIFY_OBJECT_ENABLED |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1794 | static void VerifyObject(const Object* obj, void* arg) { |
| 1795 | Heap* heap = reinterpret_cast<Heap*>(arg); |
| 1796 | heap->VerifyObject(obj); |
jeffhao | 2504552 | 2012-03-13 19:34:37 -0700 | [diff] [blame] | 1797 | } |
| 1798 | |
| 1799 | void Thread::VerifyStack() { |
jeffhao | e66ac79 | 2012-03-19 16:08:46 -0700 | [diff] [blame] | 1800 | UniquePtr<Context> context(Context::Create()); |
Ian Rogers | 67054b5 | 2012-06-26 16:02:10 -0700 | [diff] [blame] | 1801 | ReferenceMapVisitor mapper(GetManagedStack(), GetTraceStack(), context.get(), VerifyObject, |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1802 | Runtime::Current()->GetHeap()); |
| 1803 | mapper.WalkStack(); |
jeffhao | 2504552 | 2012-03-13 19:34:37 -0700 | [diff] [blame] | 1804 | } |
| 1805 | #endif |
| 1806 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1807 | // Set the stack end to that to be used during a stack overflow |
| 1808 | void Thread::SetStackEndForStackOverflow() { |
| 1809 | // During stack overflow we allow use of the full stack |
| 1810 | if (stack_end_ == stack_begin_) { |
| 1811 | DumpStack(std::cerr); |
| 1812 | LOG(FATAL) << "Need to increase kStackOverflowReservedBytes (currently " |
| 1813 | << kStackOverflowReservedBytes << ")"; |
| 1814 | } |
| 1815 | |
| 1816 | stack_end_ = stack_begin_; |
| 1817 | } |
| 1818 | |
Elliott Hughes | 330304d | 2011-08-12 14:28:05 -0700 | [diff] [blame] | 1819 | std::ostream& operator<<(std::ostream& os, const Thread& thread) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1820 | thread.ShortDump(os); |
Elliott Hughes | 330304d | 2011-08-12 14:28:05 -0700 | [diff] [blame] | 1821 | return os; |
| 1822 | } |
| 1823 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1824 | #ifndef NDEBUG |
| 1825 | void Thread::AssertThreadSuspensionIsAllowable(bool check_locks) const { |
| 1826 | CHECK_EQ(0u, no_thread_suspension_) << last_no_thread_suspension_cause_; |
| 1827 | if (check_locks) { |
| 1828 | bool bad_mutexes_held = false; |
| 1829 | for (int i = kMaxMutexLevel; i >= 0; --i) { |
| 1830 | // We expect no locks except the mutator_lock_. |
| 1831 | if (i != kMutatorLock) { |
| 1832 | BaseMutex* held_mutex = GetHeldMutex(static_cast<MutexLevel>(i)); |
| 1833 | if (held_mutex != NULL) { |
| 1834 | LOG(ERROR) << "holding \"" << held_mutex->GetName() |
| 1835 | << "\" at point where thread suspension is expected"; |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 1836 | bad_mutexes_held = true; |
| 1837 | } |
| 1838 | } |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 1839 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1840 | CHECK(!bad_mutexes_held); |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 1841 | } |
| 1842 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 1843 | #endif |
Elliott Hughes | a4060e5 | 2012-03-02 16:51:35 -0800 | [diff] [blame] | 1844 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 1845 | } // namespace art |