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