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 | |
Mathieu Chartier | 752a0e6 | 2013-06-27 11:03:27 -0700 | [diff] [blame] | 17 | #define ATRACE_TAG ATRACE_TAG_DALVIK |
| 18 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 19 | #include "thread.h" |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 20 | |
Mathieu Chartier | 752a0e6 | 2013-06-27 11:03:27 -0700 | [diff] [blame] | 21 | #include <cutils/trace.h> |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 22 | #include <pthread.h> |
Elliott Hughes | 2acf36d | 2012-04-17 13:30:13 -0700 | [diff] [blame] | 23 | #include <signal.h> |
Brian Carlstrom | dbf05b7 | 2011-12-15 00:55:24 -0800 | [diff] [blame] | 24 | #include <sys/resource.h> |
| 25 | #include <sys/time.h> |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 26 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 27 | #include <algorithm> |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 28 | #include <bitset> |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 29 | #include <cerrno> |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 30 | #include <iostream> |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 31 | #include <list> |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 32 | |
Ian Rogers | 166db04 | 2013-07-26 12:05:57 -0700 | [diff] [blame] | 33 | #include "arch/context.h" |
Elliott Hughes | 76b6167 | 2012-12-12 17:47:30 -0800 | [diff] [blame] | 34 | #include "base/mutex.h" |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 35 | #include "catch_finder.h" |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 36 | #include "class_linker.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 37 | #include "class_linker-inl.h" |
Ian Rogers | 474b6da | 2012-09-25 00:20:38 -0700 | [diff] [blame] | 38 | #include "cutils/atomic.h" |
| 39 | #include "cutils/atomic-inline.h" |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 40 | #include "debugger.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 41 | #include "dex_file-inl.h" |
Ian Rogers | 7655f29 | 2013-07-29 11:07:13 -0700 | [diff] [blame] | 42 | #include "entrypoints/entrypoint_utils.h" |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 43 | #include "gc_map.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 44 | #include "gc/accounting/card_table-inl.h" |
| 45 | #include "gc/heap.h" |
| 46 | #include "gc/space/space.h" |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 47 | #include "invoke_arg_array_builder.h" |
Elliott Hughes | c5f7c91 | 2011-08-18 14:00:42 -0700 | [diff] [blame] | 48 | #include "jni_internal.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 49 | #include "mirror/art_field-inl.h" |
| 50 | #include "mirror/art_method-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 51 | #include "mirror/class-inl.h" |
| 52 | #include "mirror/class_loader.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 53 | #include "mirror/object_array-inl.h" |
| 54 | #include "mirror/stack_trace_element.h" |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 55 | #include "monitor.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 56 | #include "object_utils.h" |
Jesse Wilson | 9a6bae8 | 2011-11-14 14:57:30 -0500 | [diff] [blame] | 57 | #include "reflection.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 58 | #include "runtime.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 59 | #include "scoped_thread_state_change.h" |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 60 | #include "ScopedLocalRef.h" |
Anwar Ghuloum | 3c539ff | 2013-06-20 08:58:23 -0700 | [diff] [blame] | 61 | #include "ScopedUtfChars.h" |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 62 | #include "sirt_ref.h" |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 63 | #include "stack.h" |
| 64 | #include "stack_indirect_reference_table.h" |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 65 | #include "thread-inl.h" |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 66 | #include "thread_list.h" |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 67 | #include "utils.h" |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 68 | #include "verifier/dex_gc_map.h" |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame^] | 69 | #include "verify_object-inl.h" |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 70 | #include "vmap_table.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 71 | #include "well_known_classes.h" |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 72 | |
| 73 | namespace art { |
| 74 | |
Ian Rogers | 0878d65 | 2013-04-18 17:38:35 -0700 | [diff] [blame] | 75 | bool Thread::is_started_ = false; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 76 | pthread_key_t Thread::pthread_key_self_; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 77 | ConditionVariable* Thread::resume_cond_ = nullptr; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 78 | |
Elliott Hughes | 7dc5166 | 2012-05-16 14:48:43 -0700 | [diff] [blame] | 79 | static const char* kThreadNameDuringStartup = "<native thread without managed peer>"; |
| 80 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 81 | void Thread::InitCardTable() { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 82 | card_table_ = Runtime::Current()->GetHeap()->GetCardTable()->GetBiasedBegin(); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Elliott Hughes | 99250ba | 2012-04-17 11:09:17 -0700 | [diff] [blame] | 85 | #if !defined(__APPLE__) |
Elliott Hughes | 3ea0f42 | 2012-04-16 17:01:43 -0700 | [diff] [blame] | 86 | static void UnimplementedEntryPoint() { |
| 87 | UNIMPLEMENTED(FATAL); |
| 88 | } |
Elliott Hughes | 99250ba | 2012-04-17 11:09:17 -0700 | [diff] [blame] | 89 | #endif |
Elliott Hughes | 3ea0f42 | 2012-04-16 17:01:43 -0700 | [diff] [blame] | 90 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 91 | void InitEntryPoints(InterpreterEntryPoints* ipoints, JniEntryPoints* jpoints, |
| 92 | PortableEntryPoints* ppoints, QuickEntryPoints* qpoints); |
Ian Rogers | 7655f29 | 2013-07-29 11:07:13 -0700 | [diff] [blame] | 93 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 94 | void Thread::InitTlsEntryPoints() { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 95 | #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] | 96 | // Insert a placeholder so we can easily tell if we call an unimplemented entry point. |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 97 | uintptr_t* begin = reinterpret_cast<uintptr_t*>(&interpreter_entrypoints_); |
Ian Rogers | 7655f29 | 2013-07-29 11:07:13 -0700 | [diff] [blame] | 98 | uintptr_t* end = reinterpret_cast<uintptr_t*>(reinterpret_cast<uint8_t*>(begin) + sizeof(quick_entrypoints_)); |
| 99 | for (uintptr_t* it = begin; it != end; ++it) { |
| 100 | *it = reinterpret_cast<uintptr_t>(UnimplementedEntryPoint); |
| 101 | } |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 102 | begin = reinterpret_cast<uintptr_t*>(&interpreter_entrypoints_); |
Ian Rogers | 7655f29 | 2013-07-29 11:07:13 -0700 | [diff] [blame] | 103 | end = reinterpret_cast<uintptr_t*>(reinterpret_cast<uint8_t*>(begin) + sizeof(portable_entrypoints_)); |
Elliott Hughes | 3ea0f42 | 2012-04-16 17:01:43 -0700 | [diff] [blame] | 104 | for (uintptr_t* it = begin; it != end; ++it) { |
| 105 | *it = reinterpret_cast<uintptr_t>(UnimplementedEntryPoint); |
| 106 | } |
Elliott Hughes | 99250ba | 2012-04-17 11:09:17 -0700 | [diff] [blame] | 107 | #endif |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 108 | InitEntryPoints(&interpreter_entrypoints_, &jni_entrypoints_, &portable_entrypoints_, |
| 109 | &quick_entrypoints_); |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 112 | void ResetQuickAllocEntryPoints(QuickEntryPoints* qpoints); |
| 113 | |
| 114 | void Thread::ResetQuickAllocEntryPointsForThread() { |
| 115 | ResetQuickAllocEntryPoints(&quick_entrypoints_); |
| 116 | } |
| 117 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 118 | void Thread::SetDeoptimizationShadowFrame(ShadowFrame* sf) { |
| 119 | deoptimization_shadow_frame_ = sf; |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 122 | void Thread::SetDeoptimizationReturnValue(const JValue& ret_val) { |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 123 | deoptimization_return_value_.SetJ(ret_val.GetJ()); |
| 124 | } |
| 125 | |
| 126 | ShadowFrame* Thread::GetAndClearDeoptimizationShadowFrame(JValue* ret_val) { |
| 127 | ShadowFrame* sf = deoptimization_shadow_frame_; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 128 | deoptimization_shadow_frame_ = nullptr; |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 129 | ret_val->SetJ(deoptimization_return_value_.GetJ()); |
| 130 | return sf; |
| 131 | } |
| 132 | |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 133 | void Thread::InitTid() { |
| 134 | tid_ = ::art::GetTid(); |
| 135 | } |
| 136 | |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 137 | void Thread::InitAfterFork() { |
Elliott Hughes | 8029cbe | 2012-05-22 09:13:08 -0700 | [diff] [blame] | 138 | // One thread (us) survived the fork, but we have a new tid so we need to |
| 139 | // update the value stashed in this Thread*. |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 140 | InitTid(); |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 143 | void* Thread::CreateCallback(void* arg) { |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 144 | Thread* self = reinterpret_cast<Thread*>(arg); |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 145 | Runtime* runtime = Runtime::Current(); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 146 | if (runtime == nullptr) { |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 147 | LOG(ERROR) << "Thread attaching to non-existent runtime: " << *self; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 148 | return nullptr; |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 149 | } |
| 150 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 151 | // TODO: pass self to MutexLock - requires self to equal Thread::Current(), which is only true |
| 152 | // after self->Init(). |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 153 | MutexLock mu(nullptr, *Locks::runtime_shutdown_lock_); |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 154 | // Check that if we got here we cannot be shutting down (as shutdown should never have started |
| 155 | // while threads are being born). |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 156 | CHECK(!runtime->IsShuttingDownLocked()); |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 157 | self->Init(runtime->GetThreadList(), runtime->GetJavaVM()); |
| 158 | Runtime::Current()->EndThreadBirth(); |
| 159 | } |
Elliott Hughes | 47179f7 | 2011-10-27 16:44:39 -0700 | [diff] [blame] | 160 | { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 161 | ScopedObjectAccess soa(self); |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 162 | |
| 163 | // Copy peer into self, deleting global reference when done. |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 164 | CHECK(self->jpeer_ != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 165 | self->opeer_ = soa.Decode<mirror::Object*>(self->jpeer_); |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 166 | self->GetJniEnv()->DeleteGlobalRef(self->jpeer_); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 167 | self->jpeer_ = nullptr; |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 168 | |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 169 | { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 170 | SirtRef<mirror::String> thread_name(self, self->GetThreadName(soa)); |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 171 | self->SetThreadName(thread_name->ToModifiedUtf8().c_str()); |
| 172 | } |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 173 | Dbg::PostThreadStart(self); |
| 174 | |
| 175 | // Invoke the 'run' method of our java.lang.Thread. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 176 | mirror::Object* receiver = self->opeer_; |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 177 | jmethodID mid = WellKnownClasses::java_lang_Thread_run; |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 178 | mirror::ArtMethod* m = |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 179 | receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(soa.DecodeMethod(mid)); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 180 | JValue result; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 181 | ArgArray arg_array(nullptr, 0); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 182 | arg_array.Append(receiver); |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 183 | m->Invoke(self, arg_array.GetArray(), arg_array.GetNumBytes(), &result, "V"); |
Elliott Hughes | 47179f7 | 2011-10-27 16:44:39 -0700 | [diff] [blame] | 184 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 185 | // Detach and delete self. |
| 186 | Runtime::Current()->GetThreadList()->Unregister(self); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 187 | |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 188 | return nullptr; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 191 | Thread* Thread::FromManagedThread(const ScopedObjectAccessUnchecked& soa, |
| 192 | mirror::Object* thread_peer) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 193 | mirror::ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_Thread_nativePeer); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 194 | Thread* result = reinterpret_cast<Thread*>(static_cast<uintptr_t>(f->GetLong(thread_peer))); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 195 | // Sanity check that if we have a result it is either suspended or we hold the thread_list_lock_ |
| 196 | // to stop it from going away. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 197 | if (kIsDebugBuild) { |
| 198 | MutexLock mu(soa.Self(), *Locks::thread_suspend_count_lock_); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 199 | if (result != nullptr && !result->IsSuspended()) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 200 | Locks::thread_list_lock_->AssertHeld(soa.Self()); |
| 201 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 202 | } |
| 203 | return result; |
Elliott Hughes | 761928d | 2011-11-16 18:33:03 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 206 | Thread* Thread::FromManagedThread(const ScopedObjectAccessUnchecked& soa, jobject java_thread) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 207 | return FromManagedThread(soa, soa.Decode<mirror::Object*>(java_thread)); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Elliott Hughes | ab7b9dc | 2012-03-27 13:16:29 -0700 | [diff] [blame] | 210 | static size_t FixStackSize(size_t stack_size) { |
Elliott Hughes | 7502e2a | 2011-10-02 13:24:37 -0700 | [diff] [blame] | 211 | // A stack size of zero means "use the default". |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 212 | if (stack_size == 0) { |
| 213 | stack_size = Runtime::Current()->GetDefaultStackSize(); |
| 214 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 215 | |
Brian Carlstrom | 6414a97 | 2012-04-14 14:20:04 -0700 | [diff] [blame] | 216 | // Dalvik used the bionic pthread default stack size for native threads, |
| 217 | // so include that here to support apps that expect large native stacks. |
| 218 | stack_size += 1 * MB; |
| 219 | |
Elliott Hughes | 7502e2a | 2011-10-02 13:24:37 -0700 | [diff] [blame] | 220 | // It's not possible to request a stack smaller than the system-defined PTHREAD_STACK_MIN. |
| 221 | if (stack_size < PTHREAD_STACK_MIN) { |
| 222 | stack_size = PTHREAD_STACK_MIN; |
| 223 | } |
| 224 | |
| 225 | // It's likely that callers are trying to ensure they have at least a certain amount of |
| 226 | // stack space, so we should add our reserved space on top of what they requested, rather |
| 227 | // than implicitly take it away from them. |
| 228 | stack_size += Thread::kStackOverflowReservedBytes; |
| 229 | |
| 230 | // Some systems require the stack size to be a multiple of the system page size, so round up. |
| 231 | stack_size = RoundUp(stack_size, kPageSize); |
| 232 | |
| 233 | return stack_size; |
| 234 | } |
| 235 | |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 236 | void Thread::CreateNativeThread(JNIEnv* env, jobject java_peer, size_t stack_size, bool is_daemon) { |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 237 | CHECK(java_peer != nullptr); |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 238 | Thread* self = static_cast<JNIEnvExt*>(env)->self; |
| 239 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | dbe6f46 | 2012-09-25 16:54:50 -0700 | [diff] [blame] | 240 | |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 241 | // Atomically start the birth of the thread ensuring the runtime isn't shutting down. |
| 242 | bool thread_start_during_shutdown = false; |
Elliott Hughes | 47179f7 | 2011-10-27 16:44:39 -0700 | [diff] [blame] | 243 | { |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 244 | MutexLock mu(self, *Locks::runtime_shutdown_lock_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 245 | if (runtime->IsShuttingDownLocked()) { |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 246 | thread_start_during_shutdown = true; |
| 247 | } else { |
| 248 | runtime->StartThreadBirth(); |
| 249 | } |
Elliott Hughes | 47179f7 | 2011-10-27 16:44:39 -0700 | [diff] [blame] | 250 | } |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 251 | if (thread_start_during_shutdown) { |
| 252 | ScopedLocalRef<jclass> error_class(env, env->FindClass("java/lang/InternalError")); |
| 253 | env->ThrowNew(error_class.get(), "Thread starting during runtime shutdown"); |
| 254 | return; |
| 255 | } |
| 256 | |
| 257 | Thread* child_thread = new Thread(is_daemon); |
| 258 | // Use global JNI ref to hold peer live while child thread starts. |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 259 | child_thread->jpeer_ = env->NewGlobalRef(java_peer); |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 260 | stack_size = FixStackSize(stack_size); |
| 261 | |
Anwar Ghuloum | 3c50a4b | 2013-06-21 13:05:23 -0700 | [diff] [blame] | 262 | // Thread.start is synchronized, so we know that nativePeer is 0, and know that we're not racing to |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 263 | // assign it. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 264 | env->SetLongField(java_peer, WellKnownClasses::java_lang_Thread_nativePeer, |
| 265 | reinterpret_cast<jlong>(child_thread)); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 266 | |
| 267 | pthread_t new_pthread; |
| 268 | pthread_attr_t attr; |
| 269 | CHECK_PTHREAD_CALL(pthread_attr_init, (&attr), "new thread"); |
| 270 | CHECK_PTHREAD_CALL(pthread_attr_setdetachstate, (&attr, PTHREAD_CREATE_DETACHED), "PTHREAD_CREATE_DETACHED"); |
| 271 | CHECK_PTHREAD_CALL(pthread_attr_setstacksize, (&attr, stack_size), stack_size); |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 272 | int pthread_create_result = pthread_create(&new_pthread, &attr, Thread::CreateCallback, child_thread); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 273 | CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attr), "new thread"); |
| 274 | |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 275 | if (pthread_create_result != 0) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 276 | // pthread_create(3) failed, so clean up. |
Brian Carlstrom | 9efc3e0 | 2012-08-17 17:47:17 -0700 | [diff] [blame] | 277 | { |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 278 | MutexLock mu(self, *Locks::runtime_shutdown_lock_); |
| 279 | runtime->EndThreadBirth(); |
| 280 | } |
| 281 | // Manually delete the global reference since Thread::Init will not have been run. |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 282 | env->DeleteGlobalRef(child_thread->jpeer_); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 283 | child_thread->jpeer_ = nullptr; |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 284 | delete child_thread; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 285 | child_thread = nullptr; |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 286 | // TODO: remove from thread group? |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 287 | env->SetLongField(java_peer, WellKnownClasses::java_lang_Thread_nativePeer, 0); |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 288 | { |
Brian Carlstrom | 9efc3e0 | 2012-08-17 17:47:17 -0700 | [diff] [blame] | 289 | std::string msg(StringPrintf("pthread_create (%s stack) failed: %s", |
| 290 | PrettySize(stack_size).c_str(), strerror(pthread_create_result))); |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 291 | ScopedObjectAccess soa(env); |
| 292 | soa.Self()->ThrowOutOfMemoryError(msg.c_str()); |
Brian Carlstrom | 9efc3e0 | 2012-08-17 17:47:17 -0700 | [diff] [blame] | 293 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 294 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 297 | void Thread::Init(ThreadList* thread_list, JavaVMExt* java_vm) { |
Elliott Hughes | 462c944 | 2012-03-23 18:47:50 -0700 | [diff] [blame] | 298 | // This function does all the initialization that must be run by the native thread it applies to. |
| 299 | // (When we create a new thread from managed code, we allocate the Thread* in Thread::Create so |
| 300 | // we can handshake with the corresponding native thread when it's ready.) Check this native |
| 301 | // thread hasn't been through here already... |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 302 | CHECK(Thread::Current() == nullptr); |
Elliott Hughes | d8af159 | 2012-04-16 20:40:15 -0700 | [diff] [blame] | 303 | SetUpAlternateSignalStack(); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 304 | InitCpu(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 305 | InitTlsEntryPoints(); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 306 | InitCardTable(); |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 307 | InitTid(); |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 308 | // Set pthread_self_ ahead of pthread_setspecific, that makes Thread::Current function, this |
| 309 | // avoids pthread_self_ ever being invalid when discovered from Thread::Current(). |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 310 | pthread_self_ = pthread_self(); |
Ian Rogers | 0878d65 | 2013-04-18 17:38:35 -0700 | [diff] [blame] | 311 | CHECK(is_started_); |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 312 | CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, this), "attach self"); |
| 313 | DCHECK_EQ(Thread::Current(), this); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 314 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 315 | thin_lock_thread_id_ = thread_list->AllocThreadId(this); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 316 | InitStackHwm(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 317 | |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 318 | jni_env_ = new JNIEnvExt(this, java_vm); |
| 319 | thread_list->Register(this); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Mathieu Chartier | 664bebf | 2012-11-12 16:54:11 -0800 | [diff] [blame] | 322 | Thread* Thread::Attach(const char* thread_name, bool as_daemon, jobject thread_group, |
| 323 | bool create_peer) { |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 324 | Thread* self; |
| 325 | Runtime* runtime = Runtime::Current(); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 326 | if (runtime == nullptr) { |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 327 | LOG(ERROR) << "Thread attaching to non-existent runtime: " << thread_name; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 328 | return nullptr; |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 329 | } |
| 330 | { |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 331 | MutexLock mu(nullptr, *Locks::runtime_shutdown_lock_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 332 | if (runtime->IsShuttingDownLocked()) { |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 333 | LOG(ERROR) << "Thread attaching while runtime is shutting down: " << thread_name; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 334 | return nullptr; |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 335 | } else { |
| 336 | Runtime::Current()->StartThreadBirth(); |
| 337 | self = new Thread(as_daemon); |
| 338 | self->Init(runtime->GetThreadList(), runtime->GetJavaVM()); |
| 339 | Runtime::Current()->EndThreadBirth(); |
| 340 | } |
| 341 | } |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 342 | |
Mathieu Chartier | dbe6f46 | 2012-09-25 16:54:50 -0700 | [diff] [blame] | 343 | CHECK_NE(self->GetState(), kRunnable); |
| 344 | self->SetState(kNative); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 345 | |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 346 | // If we're the main thread, ClassLinker won't be created until after we're attached, |
| 347 | // 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] | 348 | // In the compiler, all threads need this hack, because no-one's going to be getting |
| 349 | // a native peer! |
Mathieu Chartier | 664bebf | 2012-11-12 16:54:11 -0800 | [diff] [blame] | 350 | if (create_peer) { |
Elliott Hughes | 462c944 | 2012-03-23 18:47:50 -0700 | [diff] [blame] | 351 | self->CreatePeer(thread_name, as_daemon, thread_group); |
Elliott Hughes | 06e3ad4 | 2012-02-07 14:51:57 -0800 | [diff] [blame] | 352 | } else { |
| 353 | // These aren't necessary, but they improve diagnostics for unit tests & command-line tools. |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 354 | if (thread_name != nullptr) { |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 355 | self->name_->assign(thread_name); |
| 356 | ::art::SetThreadName(thread_name); |
| 357 | } |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 358 | } |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 359 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 360 | return self; |
| 361 | } |
| 362 | |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 363 | void Thread::CreatePeer(const char* name, bool as_daemon, jobject thread_group) { |
| 364 | Runtime* runtime = Runtime::Current(); |
| 365 | CHECK(runtime->IsStarted()); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 366 | JNIEnv* env = jni_env_; |
| 367 | |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 368 | if (thread_group == nullptr) { |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 369 | thread_group = runtime->GetMainThreadGroup(); |
Elliott Hughes | 462c944 | 2012-03-23 18:47:50 -0700 | [diff] [blame] | 370 | } |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 371 | ScopedLocalRef<jobject> thread_name(env, env->NewStringUTF(name)); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 372 | jint thread_priority = GetNativePriority(); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 373 | jboolean thread_is_daemon = as_daemon; |
| 374 | |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 375 | ScopedLocalRef<jobject> peer(env, env->AllocObject(WellKnownClasses::java_lang_Thread)); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 376 | if (peer.get() == nullptr) { |
Mathieu Chartier | dbe6f46 | 2012-09-25 16:54:50 -0700 | [diff] [blame] | 377 | CHECK(IsExceptionPending()); |
| 378 | return; |
Ian Rogers | 5d4bdc2 | 2011-11-02 22:15:43 -0700 | [diff] [blame] | 379 | } |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 380 | { |
| 381 | ScopedObjectAccess soa(this); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 382 | opeer_ = soa.Decode<mirror::Object*>(peer.get()); |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 383 | } |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 384 | env->CallNonvirtualVoidMethod(peer.get(), |
| 385 | WellKnownClasses::java_lang_Thread, |
| 386 | WellKnownClasses::java_lang_Thread_init, |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 387 | thread_group, thread_name.get(), thread_priority, thread_is_daemon); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 388 | AssertNoPendingException(); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 389 | |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 390 | Thread* self = this; |
| 391 | DCHECK_EQ(self, Thread::Current()); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 392 | jni_env_->SetLongField(peer.get(), WellKnownClasses::java_lang_Thread_nativePeer, |
| 393 | reinterpret_cast<jlong>(self)); |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 394 | |
| 395 | ScopedObjectAccess soa(self); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 396 | SirtRef<mirror::String> peer_thread_name(soa.Self(), GetThreadName(soa)); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 397 | if (peer_thread_name.get() == nullptr) { |
Brian Carlstrom | 00fae58 | 2011-10-28 01:16:28 -0700 | [diff] [blame] | 398 | // The Thread constructor should have set the Thread.name to a |
| 399 | // non-null value. However, because we can run without code |
| 400 | // available (in the compiler, in tests), we manually assign the |
| 401 | // fields the constructor should have set. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 402 | if (runtime->IsActiveTransaction()) { |
| 403 | InitPeer<true>(soa, thread_is_daemon, thread_group, thread_name.get(), thread_priority); |
| 404 | } else { |
| 405 | InitPeer<false>(soa, thread_is_daemon, thread_group, thread_name.get(), thread_priority); |
| 406 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 407 | peer_thread_name.reset(GetThreadName(soa)); |
Brian Carlstrom | 00fae58 | 2011-10-28 01:16:28 -0700 | [diff] [blame] | 408 | } |
Elliott Hughes | 225f5a1 | 2012-06-11 11:23:48 -0700 | [diff] [blame] | 409 | // 'thread_name' may have been null, so don't trust 'peer_thread_name' to be non-null. |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 410 | if (peer_thread_name.get() != nullptr) { |
Elliott Hughes | 899e789 | 2012-01-24 14:57:32 -0800 | [diff] [blame] | 411 | SetThreadName(peer_thread_name->ToModifiedUtf8().c_str()); |
Brian Carlstrom | 00fae58 | 2011-10-28 01:16:28 -0700 | [diff] [blame] | 412 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 415 | template<bool kTransactionActive> |
| 416 | void Thread::InitPeer(ScopedObjectAccess& soa, jboolean thread_is_daemon, jobject thread_group, |
| 417 | jobject thread_name, jint thread_priority) { |
| 418 | soa.DecodeField(WellKnownClasses::java_lang_Thread_daemon)-> |
| 419 | SetBoolean<kTransactionActive>(opeer_, thread_is_daemon); |
| 420 | soa.DecodeField(WellKnownClasses::java_lang_Thread_group)-> |
| 421 | SetObject<kTransactionActive>(opeer_, soa.Decode<mirror::Object*>(thread_group)); |
| 422 | soa.DecodeField(WellKnownClasses::java_lang_Thread_name)-> |
| 423 | SetObject<kTransactionActive>(opeer_, soa.Decode<mirror::Object*>(thread_name)); |
| 424 | soa.DecodeField(WellKnownClasses::java_lang_Thread_priority)-> |
| 425 | SetInt<kTransactionActive>(opeer_, thread_priority); |
| 426 | } |
| 427 | |
Elliott Hughes | 899e789 | 2012-01-24 14:57:32 -0800 | [diff] [blame] | 428 | void Thread::SetThreadName(const char* name) { |
| 429 | name_->assign(name); |
| 430 | ::art::SetThreadName(name); |
| 431 | Dbg::DdmSendThreadNotification(this, CHUNK_TYPE("THNM")); |
| 432 | } |
| 433 | |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 434 | void Thread::InitStackHwm() { |
Elliott Hughes | e188419 | 2012-04-23 12:38:15 -0700 | [diff] [blame] | 435 | void* stack_base; |
| 436 | size_t stack_size; |
Brian Carlstrom | 2921201 | 2013-09-12 22:18:30 -0700 | [diff] [blame] | 437 | GetThreadStack(pthread_self_, &stack_base, &stack_size); |
Elliott Hughes | 36ecb78 | 2012-04-17 16:55:45 -0700 | [diff] [blame] | 438 | |
| 439 | // TODO: include this in the thread dumps; potentially useful in SIGQUIT output? |
Elliott Hughes | e188419 | 2012-04-23 12:38:15 -0700 | [diff] [blame] | 440 | VLOG(threads) << StringPrintf("Native stack is at %p (%s)", stack_base, PrettySize(stack_size).c_str()); |
| 441 | |
| 442 | stack_begin_ = reinterpret_cast<byte*>(stack_base); |
| 443 | stack_size_ = stack_size; |
Elliott Hughes | 36ecb78 | 2012-04-17 16:55:45 -0700 | [diff] [blame] | 444 | |
Ian Rogers | 932746a | 2011-09-22 18:57:50 -0700 | [diff] [blame] | 445 | if (stack_size_ <= kStackOverflowReservedBytes) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 446 | 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] | 447 | } |
Elliott Hughes | 449b4bd | 2011-09-09 12:01:38 -0700 | [diff] [blame] | 448 | |
Elliott Hughes | e188419 | 2012-04-23 12:38:15 -0700 | [diff] [blame] | 449 | // TODO: move this into the Linux GetThreadStack implementation. |
| 450 | #if !defined(__APPLE__) |
Elliott Hughes | 36ecb78 | 2012-04-17 16:55:45 -0700 | [diff] [blame] | 451 | // If we're the main thread, check whether we were run with an unlimited stack. In that case, |
| 452 | // glibc will have reported a 2GB stack for our 32-bit process, and our stack overflow detection |
| 453 | // will be broken because we'll die long before we get close to 2GB. |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 454 | bool is_main_thread = (::art::GetTid() == getpid()); |
| 455 | if (is_main_thread) { |
Elliott Hughes | 36ecb78 | 2012-04-17 16:55:45 -0700 | [diff] [blame] | 456 | rlimit stack_limit; |
| 457 | if (getrlimit(RLIMIT_STACK, &stack_limit) == -1) { |
| 458 | PLOG(FATAL) << "getrlimit(RLIMIT_STACK) failed"; |
| 459 | } |
| 460 | if (stack_limit.rlim_cur == RLIM_INFINITY) { |
| 461 | // Find the default stack size for new threads... |
| 462 | pthread_attr_t default_attributes; |
| 463 | size_t default_stack_size; |
| 464 | CHECK_PTHREAD_CALL(pthread_attr_init, (&default_attributes), "default stack size query"); |
| 465 | CHECK_PTHREAD_CALL(pthread_attr_getstacksize, (&default_attributes, &default_stack_size), |
| 466 | "default stack size query"); |
| 467 | CHECK_PTHREAD_CALL(pthread_attr_destroy, (&default_attributes), "default stack size query"); |
| 468 | |
| 469 | // ...and use that as our limit. |
| 470 | size_t old_stack_size = stack_size_; |
| 471 | stack_size_ = default_stack_size; |
| 472 | stack_begin_ += (old_stack_size - stack_size_); |
Elliott Hughes | faf4ba0 | 2012-05-02 16:12:19 -0700 | [diff] [blame] | 473 | VLOG(threads) << "Limiting unlimited stack (reported as " << PrettySize(old_stack_size) << ")" |
| 474 | << " to " << PrettySize(stack_size_) |
| 475 | << " with base " << reinterpret_cast<void*>(stack_begin_); |
Elliott Hughes | 36ecb78 | 2012-04-17 16:55:45 -0700 | [diff] [blame] | 476 | } |
| 477 | } |
Elliott Hughes | e188419 | 2012-04-23 12:38:15 -0700 | [diff] [blame] | 478 | #endif |
Elliott Hughes | 36ecb78 | 2012-04-17 16:55:45 -0700 | [diff] [blame] | 479 | |
Ian Rogers | 932746a | 2011-09-22 18:57:50 -0700 | [diff] [blame] | 480 | // Set stack_end_ to the bottom of the stack saving space of stack overflows |
| 481 | ResetDefaultStackEnd(); |
Elliott Hughes | 449b4bd | 2011-09-09 12:01:38 -0700 | [diff] [blame] | 482 | |
| 483 | // Sanity check. |
| 484 | int stack_variable; |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 485 | CHECK_GT(&stack_variable, reinterpret_cast<void*>(stack_end_)); |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 488 | void Thread::ShortDump(std::ostream& os) const { |
| 489 | os << "Thread["; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 490 | if (GetThreadId() != 0) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 491 | // If we're in kStarting, we won't have a thin lock id or tid yet. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 492 | os << GetThreadId() |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 493 | << ",tid=" << GetTid() << ','; |
Elliott Hughes | e091855 | 2011-10-28 17:18:29 -0700 | [diff] [blame] | 494 | } |
Ian Rogers | 474b6da | 2012-09-25 00:20:38 -0700 | [diff] [blame] | 495 | os << GetState() |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 496 | << ",Thread*=" << this |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 497 | << ",peer=" << opeer_ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 498 | << ",\"" << *name_ << "\"" |
| 499 | << "]"; |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 502 | void Thread::Dump(std::ostream& os) const { |
| 503 | DumpState(os); |
| 504 | DumpStack(os); |
| 505 | } |
| 506 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 507 | mirror::String* Thread::GetThreadName(const ScopedObjectAccessUnchecked& soa) const { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 508 | mirror::ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_Thread_name); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 509 | return (opeer_ != nullptr) ? reinterpret_cast<mirror::String*>(f->GetObject(opeer_)) : nullptr; |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 510 | } |
| 511 | |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 512 | void Thread::GetThreadName(std::string& name) const { |
| 513 | name.assign(*name_); |
| 514 | } |
| 515 | |
Jeff Hao | 57dac6e | 2013-08-15 16:36:24 -0700 | [diff] [blame] | 516 | uint64_t Thread::GetCpuMicroTime() const { |
| 517 | #if defined(HAVE_POSIX_CLOCKS) |
| 518 | clockid_t cpu_clock_id; |
| 519 | pthread_getcpuclockid(pthread_self_, &cpu_clock_id); |
| 520 | timespec now; |
| 521 | clock_gettime(cpu_clock_id, &now); |
| 522 | return static_cast<uint64_t>(now.tv_sec) * 1000000LL + now.tv_nsec / 1000LL; |
| 523 | #else |
| 524 | UNIMPLEMENTED(WARNING); |
| 525 | return -1; |
| 526 | #endif |
| 527 | } |
| 528 | |
Ian Rogers | 474b6da | 2012-09-25 00:20:38 -0700 | [diff] [blame] | 529 | void Thread::AtomicSetFlag(ThreadFlag flag) { |
Ian Rogers | 30e173f | 2012-09-26 14:35:03 -0700 | [diff] [blame] | 530 | android_atomic_or(flag, &state_and_flags_.as_int); |
Ian Rogers | 474b6da | 2012-09-25 00:20:38 -0700 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | void Thread::AtomicClearFlag(ThreadFlag flag) { |
Ian Rogers | 30e173f | 2012-09-26 14:35:03 -0700 | [diff] [blame] | 534 | android_atomic_and(-1 ^ flag, &state_and_flags_.as_int); |
Ian Rogers | 474b6da | 2012-09-25 00:20:38 -0700 | [diff] [blame] | 535 | } |
| 536 | |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 537 | // Attempt to rectify locks so that we dump thread list with required locks before exiting. |
| 538 | static void UnsafeLogFatalForSuspendCount(Thread* self, Thread* thread) NO_THREAD_SAFETY_ANALYSIS { |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 539 | LOG(ERROR) << *thread << " suspend count already zero."; |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 540 | Locks::thread_suspend_count_lock_->Unlock(self); |
| 541 | if (!Locks::mutator_lock_->IsSharedHeld(self)) { |
| 542 | Locks::mutator_lock_->SharedTryLock(self); |
| 543 | if (!Locks::mutator_lock_->IsSharedHeld(self)) { |
| 544 | LOG(WARNING) << "Dumping thread list without holding mutator_lock_"; |
| 545 | } |
| 546 | } |
| 547 | if (!Locks::thread_list_lock_->IsExclusiveHeld(self)) { |
| 548 | Locks::thread_list_lock_->TryLock(self); |
| 549 | if (!Locks::thread_list_lock_->IsExclusiveHeld(self)) { |
| 550 | LOG(WARNING) << "Dumping thread list without holding thread_list_lock_"; |
| 551 | } |
| 552 | } |
| 553 | std::ostringstream ss; |
| 554 | Runtime::Current()->GetThreadList()->DumpLocked(ss); |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 555 | LOG(FATAL) << ss.str(); |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | void Thread::ModifySuspendCount(Thread* self, int delta, bool for_debugger) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 559 | DCHECK(delta == -1 || delta == +1 || delta == -debug_suspend_count_) |
| 560 | << delta << " " << debug_suspend_count_ << " " << this; |
| 561 | DCHECK_GE(suspend_count_, debug_suspend_count_) << this; |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 562 | Locks::thread_suspend_count_lock_->AssertHeld(self); |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 563 | if (this != self && !IsSuspended()) { |
| 564 | Locks::thread_list_lock_->AssertHeld(self); |
| 565 | } |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 566 | if (UNLIKELY(delta < 0 && suspend_count_ <= 0)) { |
| 567 | UnsafeLogFatalForSuspendCount(self, this); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 568 | return; |
| 569 | } |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 570 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 571 | suspend_count_ += delta; |
| 572 | if (for_debugger) { |
| 573 | debug_suspend_count_ += delta; |
| 574 | } |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 575 | |
Ian Rogers | 474b6da | 2012-09-25 00:20:38 -0700 | [diff] [blame] | 576 | if (suspend_count_ == 0) { |
| 577 | AtomicClearFlag(kSuspendRequest); |
| 578 | } else { |
| 579 | AtomicSetFlag(kSuspendRequest); |
| 580 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 581 | } |
| 582 | |
Mathieu Chartier | 752a0e6 | 2013-06-27 11:03:27 -0700 | [diff] [blame] | 583 | void Thread::RunCheckpointFunction() { |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 584 | Closure *checkpoints[kMaxCheckpoints]; |
| 585 | |
| 586 | // Grab the suspend_count lock and copy the current set of |
| 587 | // checkpoints. Then clear the list and the flag. The RequestCheckpoint |
| 588 | // function will also grab this lock so we prevent a race between setting |
| 589 | // the kCheckpointRequest flag and clearing it. |
| 590 | { |
| 591 | MutexLock mu(this, *Locks::thread_suspend_count_lock_); |
| 592 | for (uint32_t i = 0; i < kMaxCheckpoints; ++i) { |
| 593 | checkpoints[i] = checkpoint_functions_[i]; |
| 594 | checkpoint_functions_[i] = nullptr; |
| 595 | } |
| 596 | AtomicClearFlag(kCheckpointRequest); |
| 597 | } |
| 598 | |
| 599 | // Outside the lock, run all the checkpoint functions that |
| 600 | // we collected. |
| 601 | bool found_checkpoint = false; |
| 602 | for (uint32_t i = 0; i < kMaxCheckpoints; ++i) { |
| 603 | if (checkpoints[i] != nullptr) { |
| 604 | ATRACE_BEGIN("Checkpoint function"); |
| 605 | checkpoints[i]->Run(this); |
| 606 | ATRACE_END(); |
| 607 | found_checkpoint = true; |
| 608 | } |
| 609 | } |
| 610 | CHECK(found_checkpoint); |
Mathieu Chartier | 752a0e6 | 2013-06-27 11:03:27 -0700 | [diff] [blame] | 611 | } |
| 612 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 613 | bool Thread::RequestCheckpoint(Closure* function) { |
Chris Dearman | 59cde53 | 2013-12-04 18:53:49 -0800 | [diff] [blame] | 614 | union StateAndFlags old_state_and_flags; |
| 615 | old_state_and_flags.as_int = state_and_flags_.as_int; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 616 | if (old_state_and_flags.as_struct.state != kRunnable) { |
| 617 | return false; // Fail, thread is suspended and so can't run a checkpoint. |
| 618 | } |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 619 | |
| 620 | uint32_t available_checkpoint = kMaxCheckpoints; |
| 621 | for (uint32_t i = 0 ; i < kMaxCheckpoints; ++i) { |
| 622 | if (checkpoint_functions_[i] == nullptr) { |
| 623 | available_checkpoint = i; |
| 624 | break; |
| 625 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 626 | } |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 627 | if (available_checkpoint == kMaxCheckpoints) { |
| 628 | // No checkpoint functions available, we can't run a checkpoint |
| 629 | return false; |
| 630 | } |
| 631 | checkpoint_functions_[available_checkpoint] = function; |
| 632 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 633 | // Checkpoint function installed now install flag bit. |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 634 | // We must be runnable to request a checkpoint. |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 635 | DCHECK_EQ(old_state_and_flags.as_struct.state, kRunnable); |
Chris Dearman | 59cde53 | 2013-12-04 18:53:49 -0800 | [diff] [blame] | 636 | union StateAndFlags new_state_and_flags; |
| 637 | new_state_and_flags.as_int = old_state_and_flags.as_int; |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 638 | new_state_and_flags.as_struct.flags |= kCheckpointRequest; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 639 | int succeeded = android_atomic_acquire_cas(old_state_and_flags.as_int, new_state_and_flags.as_int, |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 640 | &state_and_flags_.as_int); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 641 | if (UNLIKELY(succeeded != 0)) { |
| 642 | // The thread changed state before the checkpoint was installed. |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 643 | CHECK_EQ(checkpoint_functions_[available_checkpoint], function); |
| 644 | checkpoint_functions_[available_checkpoint] = nullptr; |
| 645 | } else { |
| 646 | CHECK_EQ(ReadFlag(kCheckpointRequest), true); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 647 | } |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 648 | return succeeded == 0; |
| 649 | } |
| 650 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 651 | void Thread::FullSuspendCheck() { |
| 652 | VLOG(threads) << this << " self-suspending"; |
Mathieu Chartier | 752a0e6 | 2013-06-27 11:03:27 -0700 | [diff] [blame] | 653 | ATRACE_BEGIN("Full suspend check"); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 654 | // Make thread appear suspended to other threads, release mutator_lock_. |
| 655 | TransitionFromRunnableToSuspended(kSuspended); |
| 656 | // Transition back to runnable noting requests to suspend, re-acquire share on mutator_lock_. |
| 657 | TransitionFromSuspendedToRunnable(); |
Mathieu Chartier | 752a0e6 | 2013-06-27 11:03:27 -0700 | [diff] [blame] | 658 | ATRACE_END(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 659 | VLOG(threads) << this << " self-reviving"; |
| 660 | } |
| 661 | |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 662 | void Thread::DumpState(std::ostream& os, const Thread* thread, pid_t tid) { |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 663 | std::string group_name; |
| 664 | int priority; |
| 665 | bool is_daemon = false; |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 666 | Thread* self = Thread::Current(); |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 667 | |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 668 | if (self != nullptr && thread != nullptr && thread->opeer_ != nullptr) { |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 669 | ScopedObjectAccessUnchecked soa(self); |
| 670 | priority = soa.DecodeField(WellKnownClasses::java_lang_Thread_priority)->GetInt(thread->opeer_); |
| 671 | is_daemon = soa.DecodeField(WellKnownClasses::java_lang_Thread_daemon)->GetBoolean(thread->opeer_); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 672 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 673 | mirror::Object* thread_group = |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 674 | soa.DecodeField(WellKnownClasses::java_lang_Thread_group)->GetObject(thread->opeer_); |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 675 | |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 676 | if (thread_group != nullptr) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 677 | mirror::ArtField* group_name_field = |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 678 | soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_name); |
| 679 | mirror::String* group_name_string = |
| 680 | reinterpret_cast<mirror::String*>(group_name_field->GetObject(thread_group)); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 681 | group_name = (group_name_string != nullptr) ? group_name_string->ToModifiedUtf8() : "<null>"; |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 682 | } |
| 683 | } else { |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 684 | priority = GetNativePriority(); |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 685 | } |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 686 | |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 687 | std::string scheduler_group_name(GetSchedulerGroupName(tid)); |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 688 | if (scheduler_group_name.empty()) { |
| 689 | scheduler_group_name = "default"; |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 690 | } |
| 691 | |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 692 | if (thread != nullptr) { |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 693 | os << '"' << *thread->name_ << '"'; |
| 694 | if (is_daemon) { |
| 695 | os << " daemon"; |
| 696 | } |
| 697 | os << " prio=" << priority |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 698 | << " tid=" << thread->GetThreadId() |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 699 | << " " << thread->GetState(); |
| 700 | if (thread->IsStillStarting()) { |
| 701 | os << " (still starting up)"; |
| 702 | } |
| 703 | os << "\n"; |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 704 | } else { |
Elliott Hughes | 289be85 | 2012-06-12 13:57:20 -0700 | [diff] [blame] | 705 | os << '"' << ::art::GetThreadName(tid) << '"' |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 706 | << " prio=" << priority |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 707 | << " (not attached)\n"; |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 708 | } |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 709 | |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 710 | if (thread != nullptr) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 711 | MutexLock mu(self, *Locks::thread_suspend_count_lock_); |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 712 | os << " | group=\"" << group_name << "\"" |
| 713 | << " sCount=" << thread->suspend_count_ |
| 714 | << " dsCount=" << thread->debug_suspend_count_ |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 715 | << " obj=" << reinterpret_cast<void*>(thread->opeer_) |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 716 | << " self=" << reinterpret_cast<const void*>(thread) << "\n"; |
| 717 | } |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 718 | |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 719 | os << " | sysTid=" << tid |
| 720 | << " nice=" << getpriority(PRIO_PROCESS, tid) |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 721 | << " cgrp=" << scheduler_group_name; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 722 | if (thread != nullptr) { |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 723 | int policy; |
| 724 | sched_param sp; |
| 725 | CHECK_PTHREAD_CALL(pthread_getschedparam, (thread->pthread_self_, &policy, &sp), __FUNCTION__); |
| 726 | os << " sched=" << policy << "/" << sp.sched_priority |
| 727 | << " handle=" << reinterpret_cast<void*>(thread->pthread_self_); |
| 728 | } |
| 729 | os << "\n"; |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 730 | |
| 731 | // Grab the scheduler stats for this thread. |
| 732 | std::string scheduler_stats; |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 733 | if (ReadFileToString(StringPrintf("/proc/self/task/%d/schedstat", tid), &scheduler_stats)) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 734 | scheduler_stats.resize(scheduler_stats.size() - 1); // Lose the trailing '\n'. |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 735 | } else { |
| 736 | scheduler_stats = "0 0 0"; |
| 737 | } |
| 738 | |
Elliott Hughes | ba0b9c5 | 2012-09-20 11:25:12 -0700 | [diff] [blame] | 739 | char native_thread_state = '?'; |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 740 | int utime = 0; |
| 741 | int stime = 0; |
| 742 | int task_cpu = 0; |
Brian Carlstrom | 2921201 | 2013-09-12 22:18:30 -0700 | [diff] [blame] | 743 | GetTaskStats(tid, &native_thread_state, &utime, &stime, &task_cpu); |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 744 | |
Elliott Hughes | ba0b9c5 | 2012-09-20 11:25:12 -0700 | [diff] [blame] | 745 | os << " | state=" << native_thread_state |
| 746 | << " schedstat=( " << scheduler_stats << " )" |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 747 | << " utm=" << utime |
| 748 | << " stm=" << stime |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 749 | << " core=" << task_cpu |
| 750 | << " HZ=" << sysconf(_SC_CLK_TCK) << "\n"; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 751 | if (thread != nullptr) { |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 752 | os << " | stack=" << reinterpret_cast<void*>(thread->stack_begin_) << "-" << reinterpret_cast<void*>(thread->stack_end_) |
| 753 | << " stackSize=" << PrettySize(thread->stack_size_) << "\n"; |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | void Thread::DumpState(std::ostream& os) const { |
| 758 | Thread::DumpState(os, this, GetTid()); |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 759 | } |
| 760 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 761 | struct StackDumpVisitor : public StackVisitor { |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 762 | StackDumpVisitor(std::ostream& os, Thread* thread, Context* context, bool can_allocate) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 763 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 764 | : StackVisitor(thread, context), os(os), thread(thread), can_allocate(can_allocate), |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 765 | last_method(nullptr), last_line_number(0), repetition_count(0), frame_count(0) { |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 766 | } |
| 767 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 768 | virtual ~StackDumpVisitor() { |
Elliott Hughes | e85d2e9 | 2012-05-01 14:02:10 -0700 | [diff] [blame] | 769 | if (frame_count == 0) { |
| 770 | os << " (no managed stack frames)\n"; |
| 771 | } |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 772 | } |
| 773 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 774 | bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 775 | mirror::ArtMethod* m = GetMethod(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 776 | if (m->IsRuntimeMethod()) { |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 777 | return true; |
Ian Rogers | 9086572 | 2011-09-19 11:11:44 -0700 | [diff] [blame] | 778 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 779 | const int kMaxRepetition = 3; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 780 | mirror::Class* c = m->GetDeclaringClass(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 781 | mirror::DexCache* dex_cache = c->GetDexCache(); |
Ian Rogers | b861dc0 | 2011-11-14 17:00:05 -0800 | [diff] [blame] | 782 | int line_number = -1; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 783 | if (dex_cache != nullptr) { // be tolerant of bad input |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 784 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 785 | line_number = dex_file.GetLineNumFromPC(m, GetDexPc()); |
Ian Rogers | b861dc0 | 2011-11-14 17:00:05 -0800 | [diff] [blame] | 786 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 787 | if (line_number == last_line_number && last_method == m) { |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 788 | ++repetition_count; |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 789 | } else { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 790 | if (repetition_count >= kMaxRepetition) { |
| 791 | os << " ... repeated " << (repetition_count - kMaxRepetition) << " times\n"; |
| 792 | } |
| 793 | repetition_count = 0; |
| 794 | last_line_number = line_number; |
| 795 | last_method = m; |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 796 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 797 | if (repetition_count < kMaxRepetition) { |
| 798 | os << " at " << PrettyMethod(m, false); |
| 799 | if (m->IsNative()) { |
| 800 | os << "(Native method)"; |
| 801 | } else { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 802 | mh.ChangeMethod(m); |
| 803 | const char* source_file(mh.GetDeclaringClassSourceFile()); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 804 | os << "(" << (source_file != nullptr ? source_file : "unavailable") |
Brian Carlstrom | 2e450bf | 2013-09-06 15:39:46 -0700 | [diff] [blame] | 805 | << ":" << line_number << ")"; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 806 | } |
| 807 | os << "\n"; |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 808 | if (frame_count == 0) { |
| 809 | Monitor::DescribeWait(os, thread); |
| 810 | } |
| 811 | if (can_allocate) { |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 812 | Monitor::VisitLocks(this, DumpLockedObject, &os); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 813 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 814 | } |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 815 | |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 816 | ++frame_count; |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 817 | return true; |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 818 | } |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 819 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 820 | static void DumpLockedObject(mirror::Object* o, void* context) |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 821 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 822 | std::ostream& os = *reinterpret_cast<std::ostream*>(context); |
| 823 | os << " - locked <" << o << "> (a " << PrettyTypeOf(o) << ")\n"; |
| 824 | } |
| 825 | |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 826 | std::ostream& os; |
| 827 | const Thread* thread; |
Ian Rogers | 0878d65 | 2013-04-18 17:38:35 -0700 | [diff] [blame] | 828 | const bool can_allocate; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 829 | MethodHelper mh; |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 830 | mirror::ArtMethod* last_method; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 831 | int last_line_number; |
| 832 | int repetition_count; |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 833 | int frame_count; |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 834 | }; |
| 835 | |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 836 | static bool ShouldShowNativeStack(const Thread* thread) |
| 837 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | aef701d | 2013-04-05 18:15:40 -0700 | [diff] [blame] | 838 | ThreadState state = thread->GetState(); |
| 839 | |
| 840 | // In native code somewhere in the VM (one of the kWaitingFor* states)? That's interesting. |
| 841 | if (state > kWaiting && state < kStarting) { |
| 842 | return true; |
| 843 | } |
| 844 | |
| 845 | // In an Object.wait variant or Thread.sleep? That's not interesting. |
| 846 | if (state == kTimedWaiting || state == kSleeping || state == kWaiting) { |
| 847 | return false; |
| 848 | } |
| 849 | |
| 850 | // In some other native method? That's interesting. |
| 851 | // We don't just check kNative because native methods will be in state kSuspended if they're |
| 852 | // calling back into the VM, or kBlocked if they're blocked on a monitor, or one of the |
| 853 | // thread-startup states if it's early enough in their life cycle (http://b/7432159). |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 854 | mirror::ArtMethod* current_method = thread->GetCurrentMethod(nullptr); |
| 855 | return current_method != nullptr && current_method->IsNative(); |
Elliott Hughes | aef701d | 2013-04-05 18:15:40 -0700 | [diff] [blame] | 856 | } |
| 857 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 858 | void Thread::DumpStack(std::ostream& os) const { |
Jeff Hao | ed1790e | 2013-04-12 09:58:14 -0700 | [diff] [blame] | 859 | // TODO: we call this code when dying but may not have suspended the thread ourself. The |
| 860 | // IsSuspended check is therefore racy with the use for dumping (normally we inhibit |
| 861 | // the race with the thread_suspend_count_lock_). |
Ian Rogers | 6553049 | 2013-08-19 08:54:22 -0700 | [diff] [blame] | 862 | // No point dumping for an abort in debug builds where we'll hit the not suspended check in stack. |
| 863 | bool dump_for_abort = (gAborting > 0) && !kIsDebugBuild; |
Ian Rogers | d090cf4 | 2013-06-06 15:35:18 -0700 | [diff] [blame] | 864 | if (this == Thread::Current() || IsSuspended() || dump_for_abort) { |
Ian Rogers | f08e473 | 2013-04-09 09:45:49 -0700 | [diff] [blame] | 865 | // If we're currently in native code, dump that stack before dumping the managed stack. |
Ian Rogers | d090cf4 | 2013-06-06 15:35:18 -0700 | [diff] [blame] | 866 | if (dump_for_abort || ShouldShowNativeStack(this)) { |
Ian Rogers | f08e473 | 2013-04-09 09:45:49 -0700 | [diff] [blame] | 867 | DumpKernelStack(os, GetTid(), " kernel: ", false); |
| 868 | DumpNativeStack(os, GetTid(), " native: ", false); |
| 869 | } |
| 870 | UniquePtr<Context> context(Context::Create()); |
| 871 | StackDumpVisitor dumper(os, const_cast<Thread*>(this), context.get(), !throwing_OutOfMemoryError_); |
| 872 | dumper.WalkStack(); |
| 873 | } else { |
| 874 | os << "Not able to dump stack of thread that isn't suspended"; |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 875 | } |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 876 | } |
| 877 | |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 878 | void Thread::ThreadExitCallback(void* arg) { |
| 879 | Thread* self = reinterpret_cast<Thread*>(arg); |
Elliott Hughes | 6a607ad | 2012-07-13 20:40:00 -0700 | [diff] [blame] | 880 | if (self->thread_exit_check_count_ == 0) { |
| 881 | LOG(WARNING) << "Native thread exiting without having called DetachCurrentThread (maybe it's going to use a pthread_key_create destructor?): " << *self; |
Ian Rogers | 0878d65 | 2013-04-18 17:38:35 -0700 | [diff] [blame] | 882 | CHECK(is_started_); |
Elliott Hughes | 6a607ad | 2012-07-13 20:40:00 -0700 | [diff] [blame] | 883 | CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, self), "reattach self"); |
| 884 | self->thread_exit_check_count_ = 1; |
| 885 | } else { |
| 886 | LOG(FATAL) << "Native thread exited without calling DetachCurrentThread: " << *self; |
| 887 | } |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 888 | } |
| 889 | |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 890 | void Thread::Startup() { |
Ian Rogers | 0878d65 | 2013-04-18 17:38:35 -0700 | [diff] [blame] | 891 | CHECK(!is_started_); |
| 892 | is_started_ = true; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 893 | { |
Brian Carlstrom | 6d69f59 | 2013-07-16 17:18:16 -0700 | [diff] [blame] | 894 | // MutexLock to keep annotalysis happy. |
| 895 | // |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 896 | // Note we use nullptr for the thread because Thread::Current can |
Brian Carlstrom | 6d69f59 | 2013-07-16 17:18:16 -0700 | [diff] [blame] | 897 | // return garbage since (is_started_ == true) and |
| 898 | // Thread::pthread_key_self_ is not yet initialized. |
| 899 | // This was seen on glibc. |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 900 | MutexLock mu(nullptr, *Locks::thread_suspend_count_lock_); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 901 | resume_cond_ = new ConditionVariable("Thread resumption condition variable", |
| 902 | *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 903 | } |
| 904 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 905 | // Allocate a TLS slot. |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 906 | 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] | 907 | |
| 908 | // Double-check the TLS slot allocation. |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 909 | if (pthread_getspecific(pthread_key_self_) != nullptr) { |
| 910 | LOG(FATAL) << "Newly-created pthread TLS slot is not nullptr"; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 911 | } |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 912 | } |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 913 | |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 914 | void Thread::FinishStartup() { |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 915 | Runtime* runtime = Runtime::Current(); |
| 916 | CHECK(runtime->IsStarted()); |
Brian Carlstrom | b82b687 | 2011-10-26 17:18:07 -0700 | [diff] [blame] | 917 | |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 918 | // Finish attaching the main thread. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 919 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 920 | Thread::Current()->CreatePeer("main", false, runtime->GetMainThreadGroup()); |
Jesse Wilson | 9a6bae8 | 2011-11-14 14:57:30 -0500 | [diff] [blame] | 921 | |
Elliott Hughes | af8d15a | 2012-05-29 09:12:18 -0700 | [diff] [blame] | 922 | Runtime::Current()->GetClassLinker()->RunRootClinits(); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 923 | } |
| 924 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 925 | void Thread::Shutdown() { |
Ian Rogers | 0878d65 | 2013-04-18 17:38:35 -0700 | [diff] [blame] | 926 | CHECK(is_started_); |
| 927 | is_started_ = false; |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 928 | CHECK_PTHREAD_CALL(pthread_key_delete, (Thread::pthread_key_self_), "self key"); |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 929 | MutexLock mu(Thread::Current(), *Locks::thread_suspend_count_lock_); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 930 | if (resume_cond_ != nullptr) { |
Ian Rogers | 0878d65 | 2013-04-18 17:38:35 -0700 | [diff] [blame] | 931 | delete resume_cond_; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 932 | resume_cond_ = nullptr; |
Ian Rogers | 0878d65 | 2013-04-18 17:38:35 -0700 | [diff] [blame] | 933 | } |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 934 | } |
| 935 | |
Ian Rogers | 52673ff | 2012-06-27 23:25:34 -0700 | [diff] [blame] | 936 | Thread::Thread(bool daemon) |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 937 | : suspend_count_(0), |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 938 | card_table_(nullptr), |
| 939 | exception_(nullptr), |
| 940 | stack_end_(nullptr), |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 941 | managed_stack_(), |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 942 | jni_env_(nullptr), |
| 943 | self_(nullptr), |
| 944 | opeer_(nullptr), |
| 945 | jpeer_(nullptr), |
| 946 | stack_begin_(nullptr), |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 947 | stack_size_(0), |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 948 | thin_lock_thread_id_(0), |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 949 | stack_trace_sample_(nullptr), |
Jeff Hao | 5ce4b17 | 2013-08-16 16:27:18 -0700 | [diff] [blame] | 950 | trace_clock_base_(0), |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 951 | tid_(0), |
Elliott Hughes | e62934d | 2012-04-09 11:24:29 -0700 | [diff] [blame] | 952 | wait_mutex_(new Mutex("a thread wait mutex")), |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 953 | wait_cond_(new ConditionVariable("a thread wait condition variable", *wait_mutex_)), |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 954 | wait_monitor_(nullptr), |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 955 | interrupted_(false), |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 956 | wait_next_(nullptr), |
| 957 | monitor_enter_object_(nullptr), |
| 958 | top_sirt_(nullptr), |
| 959 | runtime_(nullptr), |
| 960 | class_loader_override_(nullptr), |
| 961 | long_jump_context_(nullptr), |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 962 | throwing_OutOfMemoryError_(false), |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 963 | debug_suspend_count_(0), |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 964 | debug_invoke_req_(new DebugInvokeReq), |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 965 | single_step_control_(new SingleStepControl), |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 966 | deoptimization_shadow_frame_(nullptr), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 967 | instrumentation_stack_(new std::deque<instrumentation::InstrumentationStackFrame>), |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 968 | name_(new std::string(kThreadNameDuringStartup)), |
Ian Rogers | 52673ff | 2012-06-27 23:25:34 -0700 | [diff] [blame] | 969 | daemon_(daemon), |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 970 | pthread_self_(0), |
Ian Rogers | 52673ff | 2012-06-27 23:25:34 -0700 | [diff] [blame] | 971 | no_thread_suspension_(0), |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 972 | last_no_thread_suspension_cause_(nullptr), |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 973 | thread_exit_check_count_(0), |
| 974 | thread_local_start_(nullptr), |
| 975 | thread_local_pos_(nullptr), |
| 976 | thread_local_end_(nullptr), |
Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 977 | thread_local_objects_(0), |
| 978 | thread_local_alloc_stack_top_(nullptr), |
| 979 | thread_local_alloc_stack_end_(nullptr) { |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 980 | CHECK_EQ((sizeof(Thread) % 4), 0U) << sizeof(Thread); |
Ian Rogers | 30e173f | 2012-09-26 14:35:03 -0700 | [diff] [blame] | 981 | state_and_flags_.as_struct.flags = 0; |
| 982 | state_and_flags_.as_struct.state = kNative; |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 983 | memset(&held_mutexes_[0], 0, sizeof(held_mutexes_)); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 984 | memset(rosalloc_runs_, 0, sizeof(rosalloc_runs_)); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 985 | for (uint32_t i = 0; i < kMaxCheckpoints; ++i) { |
| 986 | checkpoint_functions_[i] = nullptr; |
| 987 | } |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 988 | } |
| 989 | |
Elliott Hughes | 7dc5166 | 2012-05-16 14:48:43 -0700 | [diff] [blame] | 990 | bool Thread::IsStillStarting() const { |
| 991 | // You might think you can check whether the state is kStarting, but for much of thread startup, |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 992 | // the thread is in kNative; it might also be in kVmWait. |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 993 | // You might think you can check whether the peer is nullptr, but the peer is actually created and |
Elliott Hughes | 7dc5166 | 2012-05-16 14:48:43 -0700 | [diff] [blame] | 994 | // assigned fairly early on, and needs to be. |
| 995 | // It turns out that the last thing to change is the thread name; that's a good proxy for "has |
| 996 | // this thread _ever_ entered kRunnable". |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 997 | return (jpeer_ == nullptr && opeer_ == nullptr) || (*name_ == kThreadNameDuringStartup); |
Elliott Hughes | 7dc5166 | 2012-05-16 14:48:43 -0700 | [diff] [blame] | 998 | } |
| 999 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1000 | void Thread::AssertNoPendingException() const { |
| 1001 | if (UNLIKELY(IsExceptionPending())) { |
| 1002 | ScopedObjectAccess soa(Thread::Current()); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1003 | mirror::Throwable* exception = GetException(nullptr); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1004 | LOG(FATAL) << "No pending exception expected: " << exception->Dump(); |
| 1005 | } |
| 1006 | } |
| 1007 | |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 1008 | static void MonitorExitVisitor(mirror::Object** object, void* arg, uint32_t /*thread_id*/, |
| 1009 | RootType /*root_type*/) |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 1010 | NO_THREAD_SAFETY_ANALYSIS { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1011 | Thread* self = reinterpret_cast<Thread*>(arg); |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 1012 | mirror::Object* entered_monitor = *object; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1013 | if (self->HoldsLock(entered_monitor)) { |
| 1014 | LOG(WARNING) << "Calling MonitorExit on object " |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 1015 | << object << " (" << PrettyTypeOf(entered_monitor) << ")" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1016 | << " left locked by native thread " |
| 1017 | << *Thread::Current() << " which is detaching"; |
| 1018 | entered_monitor->MonitorExit(self); |
| 1019 | } |
Elliott Hughes | 02b48d1 | 2011-09-07 17:15:51 -0700 | [diff] [blame] | 1020 | } |
| 1021 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 1022 | void Thread::Destroy() { |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 1023 | Thread* self = this; |
| 1024 | DCHECK_EQ(self, Thread::Current()); |
Elliott Hughes | 02b48d1 | 2011-09-07 17:15:51 -0700 | [diff] [blame] | 1025 | |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1026 | if (opeer_ != nullptr) { |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 1027 | ScopedObjectAccess soa(self); |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 1028 | // We may need to call user-supplied managed code, do this before final clean-up. |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 1029 | HandleUncaughtExceptions(soa); |
| 1030 | RemoveFromThreadGroup(soa); |
Elliott Hughes | 534da07 | 2012-03-27 15:17:42 -0700 | [diff] [blame] | 1031 | |
Anwar Ghuloum | 3c50a4b | 2013-06-21 13:05:23 -0700 | [diff] [blame] | 1032 | // this.nativePeer = 0; |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1033 | if (Runtime::Current()->IsActiveTransaction()) { |
| 1034 | soa.DecodeField(WellKnownClasses::java_lang_Thread_nativePeer)->SetLong<true>(opeer_, 0); |
| 1035 | } else { |
| 1036 | soa.DecodeField(WellKnownClasses::java_lang_Thread_nativePeer)->SetLong<false>(opeer_, 0); |
| 1037 | } |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 1038 | Dbg::PostThreadDeath(self); |
Elliott Hughes | 02b48d1 | 2011-09-07 17:15:51 -0700 | [diff] [blame] | 1039 | |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 1040 | // Thread.join() is implemented as an Object.wait() on the Thread.lock object. Signal anyone |
| 1041 | // who is waiting. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1042 | mirror::Object* lock = |
| 1043 | soa.DecodeField(WellKnownClasses::java_lang_Thread_lock)->GetObject(opeer_); |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 1044 | // (This conditional is only needed for tests, where Thread.lock won't have been set.) |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 1045 | if (lock != nullptr) { |
| 1046 | SirtRef<mirror::Object> sirt_obj(self, lock); |
| 1047 | ObjectLock<mirror::Object> locker(self, &sirt_obj); |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 1048 | locker.Notify(); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 1049 | } |
| 1050 | } |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 1051 | |
| 1052 | // On thread detach, all monitors entered with JNI MonitorEnter are automatically exited. |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1053 | if (jni_env_ != nullptr) { |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 1054 | jni_env_->monitors.VisitRoots(MonitorExitVisitor, self, 0, kRootVMInternal); |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 1055 | } |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 1056 | } |
Elliott Hughes | 02b48d1 | 2011-09-07 17:15:51 -0700 | [diff] [blame] | 1057 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 1058 | Thread::~Thread() { |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1059 | if (jni_env_ != nullptr && jpeer_ != nullptr) { |
Mathieu Chartier | dbe6f46 | 2012-09-25 16:54:50 -0700 | [diff] [blame] | 1060 | // If pthread_create fails we don't have a jni env here. |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 1061 | jni_env_->DeleteGlobalRef(jpeer_); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1062 | jpeer_ = nullptr; |
Mathieu Chartier | dbe6f46 | 2012-09-25 16:54:50 -0700 | [diff] [blame] | 1063 | } |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1064 | opeer_ = nullptr; |
Mathieu Chartier | dbe6f46 | 2012-09-25 16:54:50 -0700 | [diff] [blame] | 1065 | |
Alexei Zavjalov | 1efa0a9 | 2014-02-04 02:08:31 +0700 | [diff] [blame] | 1066 | bool initialized = (jni_env_ != nullptr); // Did Thread::Init run? |
| 1067 | if (initialized) { |
| 1068 | delete jni_env_; |
| 1069 | jni_env_ = nullptr; |
| 1070 | } |
Mathieu Chartier | dbe6f46 | 2012-09-25 16:54:50 -0700 | [diff] [blame] | 1071 | CHECK_NE(GetState(), kRunnable); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1072 | CHECK_NE(ReadFlag(kCheckpointRequest), true); |
| 1073 | CHECK(checkpoint_functions_[0] == nullptr); |
| 1074 | CHECK(checkpoint_functions_[1] == nullptr); |
| 1075 | CHECK(checkpoint_functions_[2] == nullptr); |
| 1076 | |
Mathieu Chartier | dbe6f46 | 2012-09-25 16:54:50 -0700 | [diff] [blame] | 1077 | // We may be deleting a still born thread. |
| 1078 | SetStateUnsafe(kTerminated); |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 1079 | |
| 1080 | delete wait_cond_; |
| 1081 | delete wait_mutex_; |
| 1082 | |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1083 | if (long_jump_context_ != nullptr) { |
Ian Rogers | c928de9 | 2013-02-27 14:30:44 -0800 | [diff] [blame] | 1084 | delete long_jump_context_; |
| 1085 | } |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 1086 | |
Alexei Zavjalov | 1efa0a9 | 2014-02-04 02:08:31 +0700 | [diff] [blame] | 1087 | if (initialized) { |
| 1088 | CleanupCpu(); |
| 1089 | } |
| 1090 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 1091 | delete debug_invoke_req_; |
Sebastien Hertz | 61b7f1b | 2013-11-15 15:59:30 +0100 | [diff] [blame] | 1092 | delete single_step_control_; |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 1093 | delete instrumentation_stack_; |
Elliott Hughes | 899e789 | 2012-01-24 14:57:32 -0800 | [diff] [blame] | 1094 | delete name_; |
Jeff Hao | 5ce4b17 | 2013-08-16 16:27:18 -0700 | [diff] [blame] | 1095 | delete stack_trace_sample_; |
Elliott Hughes | d8af159 | 2012-04-16 20:40:15 -0700 | [diff] [blame] | 1096 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1097 | Runtime::Current()->GetHeap()->RevokeThreadLocalBuffers(this); |
| 1098 | |
Elliott Hughes | d8af159 | 2012-04-16 20:40:15 -0700 | [diff] [blame] | 1099 | TearDownAlternateSignalStack(); |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 1100 | } |
| 1101 | |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 1102 | void Thread::HandleUncaughtExceptions(ScopedObjectAccess& soa) { |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 1103 | if (!IsExceptionPending()) { |
| 1104 | return; |
| 1105 | } |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 1106 | ScopedLocalRef<jobject> peer(jni_env_, soa.AddLocalReference<jobject>(opeer_)); |
| 1107 | ScopedThreadStateChange tsc(this, kNative); |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 1108 | |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 1109 | // Get and clear the exception. |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 1110 | ScopedLocalRef<jthrowable> exception(jni_env_, jni_env_->ExceptionOccurred()); |
| 1111 | jni_env_->ExceptionClear(); |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 1112 | |
| 1113 | // If the thread has its own handler, use that. |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 1114 | ScopedLocalRef<jobject> handler(jni_env_, |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 1115 | jni_env_->GetObjectField(peer.get(), |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 1116 | WellKnownClasses::java_lang_Thread_uncaughtHandler)); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1117 | if (handler.get() == nullptr) { |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 1118 | // Otherwise use the thread group's default handler. |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 1119 | handler.reset(jni_env_->GetObjectField(peer.get(), WellKnownClasses::java_lang_Thread_group)); |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | // Call the handler. |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 1123 | jni_env_->CallVoidMethod(handler.get(), |
| 1124 | WellKnownClasses::java_lang_Thread$UncaughtExceptionHandler_uncaughtException, |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 1125 | peer.get(), exception.get()); |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 1126 | |
| 1127 | // If the handler threw, clear that exception too. |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 1128 | jni_env_->ExceptionClear(); |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 1129 | } |
| 1130 | |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 1131 | void Thread::RemoveFromThreadGroup(ScopedObjectAccess& soa) { |
Brian Carlstrom | 4514d3c | 2011-10-21 17:01:31 -0700 | [diff] [blame] | 1132 | // this.group.removeThread(this); |
| 1133 | // group can be null if we're in the compiler or a test. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1134 | mirror::Object* ogroup = soa.DecodeField(WellKnownClasses::java_lang_Thread_group)->GetObject(opeer_); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1135 | if (ogroup != nullptr) { |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 1136 | ScopedLocalRef<jobject> group(soa.Env(), soa.AddLocalReference<jobject>(ogroup)); |
| 1137 | ScopedLocalRef<jobject> peer(soa.Env(), soa.AddLocalReference<jobject>(opeer_)); |
| 1138 | ScopedThreadStateChange tsc(soa.Self(), kNative); |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 1139 | jni_env_->CallVoidMethod(group.get(), WellKnownClasses::java_lang_ThreadGroup_removeThread, |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 1140 | peer.get()); |
Brian Carlstrom | 4514d3c | 2011-10-21 17:01:31 -0700 | [diff] [blame] | 1141 | } |
| 1142 | } |
| 1143 | |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 1144 | size_t Thread::NumSirtReferences() { |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 1145 | size_t count = 0; |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1146 | for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->GetLink()) { |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 1147 | count += cur->NumberOfReferences(); |
| 1148 | } |
| 1149 | return count; |
| 1150 | } |
| 1151 | |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 1152 | bool Thread::SirtContains(jobject obj) const { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1153 | StackReference<mirror::Object>* sirt_entry = |
| 1154 | reinterpret_cast<StackReference<mirror::Object>*>(obj); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1155 | for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->GetLink()) { |
| 1156 | if (cur->Contains(sirt_entry)) { |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 1157 | return true; |
| 1158 | } |
| 1159 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1160 | // JNI code invoked from portable code uses shadow frames rather than the SIRT. |
| 1161 | return managed_stack_.ShadowFramesContain(sirt_entry); |
TDYa127 | 28f1a14 | 2012-03-15 21:51:52 -0700 | [diff] [blame] | 1162 | } |
| 1163 | |
Mathieu Chartier | 89bb144 | 2014-02-12 10:06:23 -0800 | [diff] [blame] | 1164 | void Thread::SirtVisitRoots(RootCallback* visitor, void* arg, uint32_t thread_id) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1165 | for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->GetLink()) { |
Shih-wei Liao | 8dfc9d5 | 2011-09-28 18:06:15 -0700 | [diff] [blame] | 1166 | size_t num_refs = cur->NumberOfReferences(); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1167 | for (size_t j = 0; j < num_refs; ++j) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1168 | mirror::Object* object = cur->GetReference(j); |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 1169 | if (object != nullptr) { |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 1170 | mirror::Object* old_obj = object; |
| 1171 | visitor(&object, arg, thread_id, kRootNativeStack); |
| 1172 | if (old_obj != object) { |
| 1173 | cur->SetReference(j, object); |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 1174 | } |
Brian Carlstrom | 5e73f9c | 2011-10-11 11:28:12 -0700 | [diff] [blame] | 1175 | } |
Shih-wei Liao | 8dfc9d5 | 2011-09-28 18:06:15 -0700 | [diff] [blame] | 1176 | } |
| 1177 | } |
| 1178 | } |
| 1179 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1180 | mirror::Object* Thread::DecodeJObject(jobject obj) const { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1181 | Locks::mutator_lock_->AssertSharedHeld(this); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1182 | if (obj == nullptr) { |
| 1183 | return nullptr; |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 1184 | } |
| 1185 | IndirectRef ref = reinterpret_cast<IndirectRef>(obj); |
| 1186 | IndirectRefKind kind = GetIndirectRefKind(ref); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1187 | mirror::Object* result; |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 1188 | // The "kinds" below are sorted by the frequency we expect to encounter them. |
| 1189 | if (kind == kLocal) { |
| 1190 | IndirectReferenceTable& locals = jni_env_->locals; |
| 1191 | result = const_cast<mirror::Object*>(locals.Get(ref)); |
| 1192 | } else if (kind == kSirtOrInvalid) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1193 | // TODO: make stack indirect reference table lookup more efficient. |
| 1194 | // Check if this is a local reference in the SIRT. |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 1195 | if (LIKELY(SirtContains(obj))) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1196 | // Read from SIRT. |
| 1197 | result = reinterpret_cast<StackReference<mirror::Object>*>(obj)->AsMirrorPtr(); |
Elliott Hughes | c2dc62d | 2012-01-17 20:06:12 -0800 | [diff] [blame] | 1198 | } else if (Runtime::Current()->GetJavaVM()->work_around_app_jni_bugs) { |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 1199 | // Assume an invalid local reference is actually a direct pointer. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1200 | result = reinterpret_cast<mirror::Object*>(obj); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 1201 | } else { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1202 | result = kInvalidIndirectRefObject; |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 1203 | } |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 1204 | } else if (kind == kGlobal) { |
| 1205 | JavaVMExt* vm = Runtime::Current()->GetJavaVM(); |
| 1206 | IndirectReferenceTable& globals = vm->globals; |
Ian Rogers | b8a0b94 | 2013-08-20 18:09:52 -0700 | [diff] [blame] | 1207 | ReaderMutexLock mu(const_cast<Thread*>(this), vm->globals_lock); |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 1208 | result = const_cast<mirror::Object*>(globals.Get(ref)); |
| 1209 | } else { |
| 1210 | DCHECK_EQ(kind, kWeakGlobal); |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 1211 | result = Runtime::Current()->GetJavaVM()->DecodeWeakGlobal(const_cast<Thread*>(this), ref); |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 1212 | if (result == kClearedJniWeakGlobal) { |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1213 | // This is a special case where it's okay to return nullptr. |
Mathieu Chartier | e8c48db | 2013-12-19 14:59:00 -0800 | [diff] [blame] | 1214 | return nullptr; |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 1215 | } |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 1216 | } |
| 1217 | |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1218 | if (UNLIKELY(result == nullptr)) { |
| 1219 | JniAbortF(nullptr, "use of deleted %s %p", ToStr<IndirectRefKind>(kind).c_str(), obj); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1220 | } else { |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame^] | 1221 | if (result != kInvalidIndirectRefObject) { |
| 1222 | VerifyObject(result); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 1223 | } |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 1224 | } |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 1225 | return result; |
| 1226 | } |
| 1227 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1228 | // Implements java.lang.Thread.interrupted. |
| 1229 | bool Thread::Interrupted() { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1230 | MutexLock mu(Thread::Current(), *wait_mutex_); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1231 | bool interrupted = interrupted_; |
| 1232 | interrupted_ = false; |
| 1233 | return interrupted; |
| 1234 | } |
| 1235 | |
| 1236 | // Implements java.lang.Thread.isInterrupted. |
| 1237 | bool Thread::IsInterrupted() { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1238 | MutexLock mu(Thread::Current(), *wait_mutex_); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1239 | return interrupted_; |
| 1240 | } |
| 1241 | |
| 1242 | void Thread::Interrupt() { |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 1243 | Thread* self = Thread::Current(); |
| 1244 | MutexLock mu(self, *wait_mutex_); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1245 | if (interrupted_) { |
| 1246 | return; |
| 1247 | } |
| 1248 | interrupted_ = true; |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 1249 | NotifyLocked(self); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1250 | } |
| 1251 | |
| 1252 | void Thread::Notify() { |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 1253 | Thread* self = Thread::Current(); |
| 1254 | MutexLock mu(self, *wait_mutex_); |
| 1255 | NotifyLocked(self); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1256 | } |
| 1257 | |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 1258 | void Thread::NotifyLocked(Thread* self) { |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1259 | if (wait_monitor_ != nullptr) { |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 1260 | wait_cond_->Signal(self); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1261 | } |
| 1262 | } |
| 1263 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1264 | class CountStackDepthVisitor : public StackVisitor { |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1265 | public: |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 1266 | explicit CountStackDepthVisitor(Thread* thread) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1267 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1268 | : StackVisitor(thread, nullptr), |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1269 | depth_(0), skip_depth_(0), skipping_(true) {} |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 1270 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1271 | bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 1272 | // 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] | 1273 | // Note we also skip the frame if it doesn't have a method (namely the callee |
| 1274 | // save frame) |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1275 | mirror::ArtMethod* m = GetMethod(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1276 | if (skipping_ && !m->IsRuntimeMethod() && |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1277 | !mirror::Throwable::GetJavaLangThrowable()->IsAssignableFrom(m->GetDeclaringClass())) { |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 1278 | skipping_ = false; |
| 1279 | } |
| 1280 | if (!skipping_) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1281 | if (!m->IsRuntimeMethod()) { // Ignore runtime frames (in particular callee save). |
Ian Rogers | 6b0870d | 2011-12-15 19:38:12 -0800 | [diff] [blame] | 1282 | ++depth_; |
| 1283 | } |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 1284 | } else { |
| 1285 | ++skip_depth_; |
| 1286 | } |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 1287 | return true; |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 1288 | } |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1289 | |
| 1290 | int GetDepth() const { |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1291 | return depth_; |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1292 | } |
| 1293 | |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 1294 | int GetSkipDepth() const { |
| 1295 | return skip_depth_; |
| 1296 | } |
| 1297 | |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1298 | private: |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1299 | uint32_t depth_; |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 1300 | uint32_t skip_depth_; |
| 1301 | bool skipping_; |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1302 | }; |
| 1303 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1304 | class BuildInternalStackTraceVisitor : public StackVisitor { |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1305 | public: |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 1306 | explicit BuildInternalStackTraceVisitor(Thread* self, Thread* thread, int skip_depth) |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1307 | : StackVisitor(thread, nullptr), self_(self), |
| 1308 | skip_depth_(skip_depth), count_(0), dex_pc_trace_(nullptr), method_trace_(nullptr) {} |
Ian Rogers | 283ed0d | 2012-02-16 15:25:09 -0800 | [diff] [blame] | 1309 | |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 1310 | bool Init(int depth) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1311 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1312 | // Allocate method trace with an extra slot that will hold the PC trace |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1313 | SirtRef<mirror::ObjectArray<mirror::Object> > |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 1314 | method_trace(self_, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1315 | Runtime::Current()->GetClassLinker()->AllocObjectArray<mirror::Object>(self_, |
| 1316 | depth + 1)); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1317 | if (method_trace.get() == nullptr) { |
Ian Rogers | 283ed0d | 2012-02-16 15:25:09 -0800 | [diff] [blame] | 1318 | return false; |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 1319 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1320 | mirror::IntArray* dex_pc_trace = mirror::IntArray::Alloc(self_, depth); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1321 | if (dex_pc_trace == nullptr) { |
Ian Rogers | 283ed0d | 2012-02-16 15:25:09 -0800 | [diff] [blame] | 1322 | return false; |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 1323 | } |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1324 | // Save PC trace in last element of method trace, also places it into the |
| 1325 | // object graph. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1326 | // We are called from native: use non-transactional mode. |
| 1327 | method_trace->Set<false>(depth, dex_pc_trace); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1328 | // 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] | 1329 | const char* last_no_suspend_cause = |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 1330 | self_->StartAssertNoThreadSuspension("Building internal stack trace"); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1331 | CHECK(last_no_suspend_cause == nullptr) << last_no_suspend_cause; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1332 | method_trace_ = method_trace.get(); |
| 1333 | dex_pc_trace_ = dex_pc_trace; |
Ian Rogers | 283ed0d | 2012-02-16 15:25:09 -0800 | [diff] [blame] | 1334 | return true; |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1335 | } |
| 1336 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1337 | virtual ~BuildInternalStackTraceVisitor() { |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1338 | if (method_trace_ != nullptr) { |
| 1339 | self_->EndAssertNoThreadSuspension(nullptr); |
Ian Rogers | 52673ff | 2012-06-27 23:25:34 -0700 | [diff] [blame] | 1340 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1341 | } |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1342 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1343 | bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1344 | if (method_trace_ == nullptr || dex_pc_trace_ == nullptr) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1345 | return true; // We're probably trying to fillInStackTrace for an OutOfMemoryError. |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 1346 | } |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 1347 | if (skip_depth_ > 0) { |
| 1348 | skip_depth_--; |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 1349 | return true; |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 1350 | } |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1351 | mirror::ArtMethod* m = GetMethod(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1352 | if (m->IsRuntimeMethod()) { |
| 1353 | return true; // Ignore runtime frames (in particular callee save). |
Ian Rogers | 6b0870d | 2011-12-15 19:38:12 -0800 | [diff] [blame] | 1354 | } |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1355 | // TODO dedup this code. |
| 1356 | if (Runtime::Current()->IsActiveTransaction()) { |
| 1357 | method_trace_->Set<true>(count_, m); |
| 1358 | dex_pc_trace_->Set<true>(count_, m->IsProxyMethod() ? DexFile::kDexNoIndex : GetDexPc()); |
| 1359 | } else { |
| 1360 | method_trace_->Set<false>(count_, m); |
| 1361 | dex_pc_trace_->Set<false>(count_, m->IsProxyMethod() ? DexFile::kDexNoIndex : GetDexPc()); |
| 1362 | } |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1363 | ++count_; |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 1364 | return true; |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1365 | } |
| 1366 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1367 | mirror::ObjectArray<mirror::Object>* GetInternalStackTrace() const { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1368 | return method_trace_; |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | private: |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 1372 | Thread* const self_; |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 1373 | // How many more frames to skip. |
| 1374 | int32_t skip_depth_; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1375 | // Current position down stack trace. |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1376 | uint32_t count_; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1377 | // Array of dex PC values. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1378 | mirror::IntArray* dex_pc_trace_; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1379 | // An array of the methods on the stack, the last entry is a reference to the PC trace. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1380 | mirror::ObjectArray<mirror::Object>* method_trace_; |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1381 | }; |
| 1382 | |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 1383 | jobject Thread::CreateInternalStackTrace(const ScopedObjectAccessUnchecked& soa) const { |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1384 | // Compute depth of stack |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 1385 | CountStackDepthVisitor count_visitor(const_cast<Thread*>(this)); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1386 | count_visitor.WalkStack(); |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1387 | int32_t depth = count_visitor.GetDepth(); |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 1388 | int32_t skip_depth = count_visitor.GetSkipDepth(); |
Shih-wei Liao | 4417536 | 2011-08-28 16:59:17 -0700 | [diff] [blame] | 1389 | |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 1390 | // Build internal stack trace. |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 1391 | BuildInternalStackTraceVisitor build_trace_visitor(soa.Self(), const_cast<Thread*>(this), |
| 1392 | skip_depth); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 1393 | if (!build_trace_visitor.Init(depth)) { |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1394 | return nullptr; // Allocation failed. |
Ian Rogers | 283ed0d | 2012-02-16 15:25:09 -0800 | [diff] [blame] | 1395 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1396 | build_trace_visitor.WalkStack(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1397 | mirror::ObjectArray<mirror::Object>* trace = build_trace_visitor.GetInternalStackTrace(); |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 1398 | if (kIsDebugBuild) { |
| 1399 | for (int32_t i = 0; i < trace->GetLength(); ++i) { |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1400 | CHECK(trace->Get(i) != nullptr); |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 1401 | } |
| 1402 | } |
| 1403 | return soa.AddLocalReference<jobjectArray>(trace); |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1404 | } |
| 1405 | |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 1406 | jobjectArray Thread::InternalStackTraceToStackTraceElementArray(JNIEnv* env, jobject internal, |
| 1407 | jobjectArray output_array, int* stack_depth) { |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1408 | // Transition into runnable state to work on Object*/Array* |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1409 | ScopedObjectAccess soa(env); |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1410 | // Decode the internal stack trace into the depth, method trace and PC trace |
Mathieu Chartier | 530825b | 2013-09-25 17:56:49 -0700 | [diff] [blame] | 1411 | int32_t depth = soa.Decode<mirror::ObjectArray<mirror::Object>*>(internal)->GetLength() - 1; |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1412 | |
| 1413 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 1414 | |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 1415 | jobjectArray result; |
Mathieu Chartier | 530825b | 2013-09-25 17:56:49 -0700 | [diff] [blame] | 1416 | |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1417 | if (output_array != nullptr) { |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 1418 | // Reuse the array we were given. |
| 1419 | result = output_array; |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 1420 | // ...adjusting the number of frames we'll write to not exceed the array length. |
Mathieu Chartier | 530825b | 2013-09-25 17:56:49 -0700 | [diff] [blame] | 1421 | const int32_t traces_length = |
| 1422 | soa.Decode<mirror::ObjectArray<mirror::StackTraceElement>*>(result)->GetLength(); |
| 1423 | depth = std::min(depth, traces_length); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 1424 | } else { |
| 1425 | // Create java_trace array and place in local reference table |
Mathieu Chartier | 530825b | 2013-09-25 17:56:49 -0700 | [diff] [blame] | 1426 | mirror::ObjectArray<mirror::StackTraceElement>* java_traces = |
| 1427 | class_linker->AllocStackTraceElementArray(soa.Self(), depth); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1428 | if (java_traces == nullptr) { |
| 1429 | return nullptr; |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 1430 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1431 | result = soa.AddLocalReference<jobjectArray>(java_traces); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 1432 | } |
| 1433 | |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1434 | if (stack_depth != nullptr) { |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 1435 | *stack_depth = depth; |
| 1436 | } |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 1437 | |
Shih-wei Liao | 9b576b4 | 2011-08-29 01:45:07 -0700 | [diff] [blame] | 1438 | for (int32_t i = 0; i < depth; ++i) { |
Mathieu Chartier | 530825b | 2013-09-25 17:56:49 -0700 | [diff] [blame] | 1439 | mirror::ObjectArray<mirror::Object>* method_trace = |
| 1440 | soa.Decode<mirror::ObjectArray<mirror::Object>*>(internal); |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1441 | // Prepare parameters for StackTraceElement(String cls, String method, String file, int line) |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1442 | mirror::ArtMethod* method = down_cast<mirror::ArtMethod*>(method_trace->Get(i)); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1443 | MethodHelper mh(method); |
Jeff Hao | 228d6b8 | 2013-12-03 15:00:05 -0800 | [diff] [blame] | 1444 | int32_t line_number; |
Brian Carlstrom | 7c6aca2 | 2014-01-31 18:44:56 -0800 | [diff] [blame] | 1445 | SirtRef<mirror::String> class_name_object(soa.Self(), nullptr); |
| 1446 | SirtRef<mirror::String> source_name_object(soa.Self(), nullptr); |
Jeff Hao | 228d6b8 | 2013-12-03 15:00:05 -0800 | [diff] [blame] | 1447 | if (method->IsProxyMethod()) { |
| 1448 | line_number = -1; |
| 1449 | class_name_object.reset(method->GetDeclaringClass()->GetName()); |
| 1450 | // source_name_object intentionally left null for proxy methods |
| 1451 | } else { |
| 1452 | mirror::IntArray* pc_trace = down_cast<mirror::IntArray*>(method_trace->Get(depth)); |
| 1453 | uint32_t dex_pc = pc_trace->Get(i); |
| 1454 | line_number = mh.GetLineNumFromDexPC(dex_pc); |
| 1455 | // Allocate element, potentially triggering GC |
| 1456 | // TODO: reuse class_name_object via Class::name_? |
| 1457 | const char* descriptor = mh.GetDeclaringClassDescriptor(); |
Brian Carlstrom | 7c6aca2 | 2014-01-31 18:44:56 -0800 | [diff] [blame] | 1458 | CHECK(descriptor != nullptr); |
Jeff Hao | 228d6b8 | 2013-12-03 15:00:05 -0800 | [diff] [blame] | 1459 | std::string class_name(PrettyDescriptor(descriptor)); |
| 1460 | class_name_object.reset(mirror::String::AllocFromModifiedUtf8(soa.Self(), class_name.c_str())); |
Brian Carlstrom | 7c6aca2 | 2014-01-31 18:44:56 -0800 | [diff] [blame] | 1461 | if (class_name_object.get() == nullptr) { |
| 1462 | return nullptr; |
Jeff Hao | 228d6b8 | 2013-12-03 15:00:05 -0800 | [diff] [blame] | 1463 | } |
| 1464 | const char* source_file = mh.GetDeclaringClassSourceFile(); |
Brian Carlstrom | 7c6aca2 | 2014-01-31 18:44:56 -0800 | [diff] [blame] | 1465 | if (source_file != nullptr) { |
| 1466 | source_name_object.reset(mirror::String::AllocFromModifiedUtf8(soa.Self(), source_file)); |
| 1467 | if (source_name_object.get() == nullptr) { |
| 1468 | return nullptr; |
| 1469 | } |
Jeff Hao | 228d6b8 | 2013-12-03 15:00:05 -0800 | [diff] [blame] | 1470 | } |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1471 | } |
Ian Rogers | 4860131 | 2011-12-07 16:45:19 -0800 | [diff] [blame] | 1472 | const char* method_name = mh.GetName(); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1473 | CHECK(method_name != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1474 | SirtRef<mirror::String> method_name_object(soa.Self(), |
| 1475 | mirror::String::AllocFromModifiedUtf8(soa.Self(), |
| 1476 | method_name)); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1477 | if (method_name_object.get() == nullptr) { |
| 1478 | return nullptr; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1479 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1480 | mirror::StackTraceElement* obj = mirror::StackTraceElement::Alloc( |
| 1481 | soa.Self(), class_name_object, method_name_object, source_name_object, line_number); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1482 | if (obj == nullptr) { |
| 1483 | return nullptr; |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 1484 | } |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1485 | // We are called from native: use non-transactional mode. |
| 1486 | soa.Decode<mirror::ObjectArray<mirror::StackTraceElement>*>(result)->Set<false>(i, obj); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 1487 | } |
Ian Rogers | aaa2080 | 2011-09-11 21:47:37 -0700 | [diff] [blame] | 1488 | return result; |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 1489 | } |
| 1490 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1491 | void Thread::ThrowNewExceptionF(const ThrowLocation& throw_location, |
| 1492 | const char* exception_class_descriptor, const char* fmt, ...) { |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1493 | va_list args; |
| 1494 | va_start(args, fmt); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1495 | ThrowNewExceptionV(throw_location, exception_class_descriptor, |
| 1496 | fmt, args); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1497 | va_end(args); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 1498 | } |
| 1499 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1500 | void Thread::ThrowNewExceptionV(const ThrowLocation& throw_location, |
| 1501 | const char* exception_class_descriptor, |
| 1502 | const char* fmt, va_list ap) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 1503 | std::string msg; |
| 1504 | StringAppendV(&msg, fmt, ap); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1505 | ThrowNewException(throw_location, exception_class_descriptor, msg.c_str()); |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 1506 | } |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 1507 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1508 | void Thread::ThrowNewException(const ThrowLocation& throw_location, const char* exception_class_descriptor, |
| 1509 | const char* msg) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1510 | AssertNoPendingException(); // Callers should either clear or call ThrowNewWrappedException. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1511 | ThrowNewWrappedException(throw_location, exception_class_descriptor, msg); |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 1512 | } |
| 1513 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1514 | void Thread::ThrowNewWrappedException(const ThrowLocation& throw_location, |
| 1515 | const char* exception_class_descriptor, |
| 1516 | const char* msg) { |
| 1517 | DCHECK_EQ(this, Thread::Current()); |
| 1518 | // Ensure we don't forget arguments over object allocation. |
| 1519 | SirtRef<mirror::Object> saved_throw_this(this, throw_location.GetThis()); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1520 | SirtRef<mirror::ArtMethod> saved_throw_method(this, throw_location.GetMethod()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1521 | // Ignore the cause throw location. TODO: should we report this as a re-throw? |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1522 | SirtRef<mirror::Throwable> cause(this, GetException(nullptr)); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1523 | ClearException(); |
| 1524 | Runtime* runtime = Runtime::Current(); |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 1525 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 1526 | mirror::ClassLoader* cl = nullptr; |
| 1527 | if (saved_throw_method.get() != nullptr) { |
| 1528 | cl = saved_throw_method.get()->GetDeclaringClass()->GetClassLoader(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1529 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1530 | SirtRef<mirror::ClassLoader> class_loader(this, cl); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1531 | SirtRef<mirror::Class> |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1532 | exception_class(this, runtime->GetClassLinker()->FindClass(exception_class_descriptor, |
| 1533 | class_loader)); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1534 | if (UNLIKELY(exception_class.get() == nullptr)) { |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 1535 | CHECK(IsExceptionPending()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1536 | LOG(ERROR) << "No exception class " << PrettyDescriptor(exception_class_descriptor); |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 1537 | return; |
| 1538 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1539 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 1540 | if (UNLIKELY(!runtime->GetClassLinker()->EnsureInitialized(exception_class, true, true))) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1541 | DCHECK(IsExceptionPending()); |
| 1542 | return; |
| 1543 | } |
| 1544 | DCHECK(!runtime->IsStarted() || exception_class->IsThrowableClass()); |
| 1545 | SirtRef<mirror::Throwable> exception(this, |
| 1546 | down_cast<mirror::Throwable*>(exception_class->AllocObject(this))); |
| 1547 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1548 | // If we couldn't allocate the exception, throw the pre-allocated out of memory exception. |
| 1549 | if (exception.get() == nullptr) { |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 1550 | ThrowLocation gc_safe_throw_location(saved_throw_this.get(), saved_throw_method.get(), |
| 1551 | throw_location.GetDexPc()); |
| 1552 | SetException(gc_safe_throw_location, Runtime::Current()->GetPreAllocatedOutOfMemoryError()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1553 | return; |
| 1554 | } |
| 1555 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1556 | // Choose an appropriate constructor and set up the arguments. |
| 1557 | const char* signature; |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 1558 | const char* shorty; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1559 | SirtRef<mirror::String> msg_string(this, nullptr); |
| 1560 | if (msg != nullptr) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1561 | // Ensure we remember this and the method over the String allocation. |
| 1562 | msg_string.reset(mirror::String::AllocFromModifiedUtf8(this, msg)); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1563 | if (UNLIKELY(msg_string.get() == nullptr)) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1564 | CHECK(IsExceptionPending()); // OOME. |
| 1565 | return; |
Brian Carlstrom | ebd1fd2 | 2011-12-07 15:46:26 -0800 | [diff] [blame] | 1566 | } |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1567 | if (cause.get() == nullptr) { |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 1568 | shorty = "VL"; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1569 | signature = "(Ljava/lang/String;)V"; |
| 1570 | } else { |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 1571 | shorty = "VLL"; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1572 | signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V"; |
| 1573 | } |
| 1574 | } else { |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1575 | if (cause.get() == nullptr) { |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 1576 | shorty = "V"; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1577 | signature = "()V"; |
| 1578 | } else { |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 1579 | shorty = "VL"; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1580 | signature = "(Ljava/lang/Throwable;)V"; |
| 1581 | } |
Brian Carlstrom | ebd1fd2 | 2011-12-07 15:46:26 -0800 | [diff] [blame] | 1582 | } |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1583 | mirror::ArtMethod* exception_init_method = |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1584 | exception_class->FindDeclaredDirectMethod("<init>", signature); |
| 1585 | |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1586 | CHECK(exception_init_method != nullptr) << "No <init>" << signature << " in " |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1587 | << PrettyDescriptor(exception_class_descriptor); |
| 1588 | |
| 1589 | if (UNLIKELY(!runtime->IsStarted())) { |
| 1590 | // Something is trying to throw an exception without a started runtime, which is the common |
| 1591 | // case in the compiler. We won't be able to invoke the constructor of the exception, so set |
| 1592 | // the exception fields directly. |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1593 | if (msg != nullptr) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1594 | exception->SetDetailMessage(msg_string.get()); |
| 1595 | } |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1596 | if (cause.get() != nullptr) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1597 | exception->SetCause(cause.get()); |
| 1598 | } |
| 1599 | ThrowLocation gc_safe_throw_location(saved_throw_this.get(), saved_throw_method.get(), |
| 1600 | throw_location.GetDexPc()); |
| 1601 | SetException(gc_safe_throw_location, exception.get()); |
| 1602 | } else { |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 1603 | ArgArray args(shorty, strlen(shorty)); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1604 | args.Append(exception.get()); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1605 | if (msg != nullptr) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1606 | args.Append(msg_string.get()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1607 | } |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1608 | if (cause.get() != nullptr) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1609 | args.Append(cause.get()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1610 | } |
| 1611 | JValue result; |
Ian Rogers | 0177e53 | 2014-02-11 16:30:46 -0800 | [diff] [blame] | 1612 | exception_init_method->Invoke(this, args.GetArray(), args.GetNumBytes(), &result, shorty); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1613 | if (LIKELY(!IsExceptionPending())) { |
| 1614 | ThrowLocation gc_safe_throw_location(saved_throw_this.get(), saved_throw_method.get(), |
| 1615 | throw_location.GetDexPc()); |
| 1616 | SetException(gc_safe_throw_location, exception.get()); |
| 1617 | } |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 1618 | } |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1619 | } |
| 1620 | |
Elliott Hughes | 2ced6a5 | 2011-10-16 18:44:48 -0700 | [diff] [blame] | 1621 | void Thread::ThrowOutOfMemoryError(const char* msg) { |
| 1622 | LOG(ERROR) << StringPrintf("Throwing OutOfMemoryError \"%s\"%s", |
| 1623 | msg, (throwing_OutOfMemoryError_ ? " (recursive case)" : "")); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1624 | ThrowLocation throw_location = GetCurrentLocationForThrow(); |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 1625 | if (!throwing_OutOfMemoryError_) { |
| 1626 | throwing_OutOfMemoryError_ = true; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1627 | ThrowNewException(throw_location, "Ljava/lang/OutOfMemoryError;", msg); |
Ian Rogers | 33e15bc | 2013-05-20 20:04:20 -0700 | [diff] [blame] | 1628 | throwing_OutOfMemoryError_ = false; |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 1629 | } else { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1630 | Dump(LOG(ERROR)); // The pre-allocated OOME has no stack, so help out and log one. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1631 | SetException(throw_location, Runtime::Current()->GetPreAllocatedOutOfMemoryError()); |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 1632 | } |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 1633 | } |
| 1634 | |
Elliott Hughes | 498508c | 2011-10-17 14:58:22 -0700 | [diff] [blame] | 1635 | Thread* Thread::CurrentFromGdb() { |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 1636 | return Thread::Current(); |
| 1637 | } |
| 1638 | |
| 1639 | void Thread::DumpFromGdb() const { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1640 | std::ostringstream ss; |
| 1641 | Dump(ss); |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 1642 | std::string str(ss.str()); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1643 | // log to stderr for debugging command line processes |
| 1644 | std::cerr << str; |
| 1645 | #ifdef HAVE_ANDROID_OS |
| 1646 | // log to logcat for debugging frameworks processes |
| 1647 | LOG(INFO) << str; |
| 1648 | #endif |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 1649 | } |
| 1650 | |
Elliott Hughes | 98e2017 | 2012-04-24 15:38:13 -0700 | [diff] [blame] | 1651 | struct EntryPointInfo { |
| 1652 | uint32_t offset; |
| 1653 | const char* name; |
| 1654 | }; |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1655 | #define INTERPRETER_ENTRY_POINT_INFO(x) { INTERPRETER_ENTRYPOINT_OFFSET(x).Uint32Value(), #x } |
| 1656 | #define JNI_ENTRY_POINT_INFO(x) { JNI_ENTRYPOINT_OFFSET(x).Uint32Value(), #x } |
| 1657 | #define PORTABLE_ENTRY_POINT_INFO(x) { PORTABLE_ENTRYPOINT_OFFSET(x).Uint32Value(), #x } |
| 1658 | #define QUICK_ENTRY_POINT_INFO(x) { QUICK_ENTRYPOINT_OFFSET(x).Uint32Value(), #x } |
Elliott Hughes | 98e2017 | 2012-04-24 15:38:13 -0700 | [diff] [blame] | 1659 | static const EntryPointInfo gThreadEntryPointInfo[] = { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1660 | INTERPRETER_ENTRY_POINT_INFO(pInterpreterToInterpreterBridge), |
| 1661 | INTERPRETER_ENTRY_POINT_INFO(pInterpreterToCompiledCodeBridge), |
| 1662 | JNI_ENTRY_POINT_INFO(pDlsymLookup), |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1663 | PORTABLE_ENTRY_POINT_INFO(pPortableImtConflictTrampoline), |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1664 | PORTABLE_ENTRY_POINT_INFO(pPortableResolutionTrampoline), |
| 1665 | PORTABLE_ENTRY_POINT_INFO(pPortableToInterpreterBridge), |
| 1666 | QUICK_ENTRY_POINT_INFO(pAllocArray), |
Hiroshi Yamauchi | bb8f0ab | 2014-01-27 16:50:29 -0800 | [diff] [blame] | 1667 | QUICK_ENTRY_POINT_INFO(pAllocArrayResolved), |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1668 | QUICK_ENTRY_POINT_INFO(pAllocArrayWithAccessCheck), |
| 1669 | QUICK_ENTRY_POINT_INFO(pAllocObject), |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 1670 | QUICK_ENTRY_POINT_INFO(pAllocObjectResolved), |
| 1671 | QUICK_ENTRY_POINT_INFO(pAllocObjectInitialized), |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1672 | QUICK_ENTRY_POINT_INFO(pAllocObjectWithAccessCheck), |
| 1673 | QUICK_ENTRY_POINT_INFO(pCheckAndAllocArray), |
| 1674 | QUICK_ENTRY_POINT_INFO(pCheckAndAllocArrayWithAccessCheck), |
| 1675 | QUICK_ENTRY_POINT_INFO(pInstanceofNonTrivial), |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1676 | QUICK_ENTRY_POINT_INFO(pCheckCast), |
Ian Rogers | 7655f29 | 2013-07-29 11:07:13 -0700 | [diff] [blame] | 1677 | QUICK_ENTRY_POINT_INFO(pInitializeStaticStorage), |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1678 | QUICK_ENTRY_POINT_INFO(pInitializeTypeAndVerifyAccess), |
| 1679 | QUICK_ENTRY_POINT_INFO(pInitializeType), |
| 1680 | QUICK_ENTRY_POINT_INFO(pResolveString), |
Ian Rogers | 7655f29 | 2013-07-29 11:07:13 -0700 | [diff] [blame] | 1681 | QUICK_ENTRY_POINT_INFO(pSet32Instance), |
| 1682 | QUICK_ENTRY_POINT_INFO(pSet32Static), |
| 1683 | QUICK_ENTRY_POINT_INFO(pSet64Instance), |
| 1684 | QUICK_ENTRY_POINT_INFO(pSet64Static), |
| 1685 | QUICK_ENTRY_POINT_INFO(pSetObjInstance), |
| 1686 | QUICK_ENTRY_POINT_INFO(pSetObjStatic), |
| 1687 | QUICK_ENTRY_POINT_INFO(pGet32Instance), |
| 1688 | QUICK_ENTRY_POINT_INFO(pGet32Static), |
| 1689 | QUICK_ENTRY_POINT_INFO(pGet64Instance), |
| 1690 | QUICK_ENTRY_POINT_INFO(pGet64Static), |
| 1691 | QUICK_ENTRY_POINT_INFO(pGetObjInstance), |
| 1692 | QUICK_ENTRY_POINT_INFO(pGetObjStatic), |
Ian Rogers | a9a8254 | 2013-10-04 11:17:26 -0700 | [diff] [blame] | 1693 | QUICK_ENTRY_POINT_INFO(pAputObjectWithNullAndBoundCheck), |
| 1694 | QUICK_ENTRY_POINT_INFO(pAputObjectWithBoundCheck), |
| 1695 | QUICK_ENTRY_POINT_INFO(pAputObject), |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1696 | QUICK_ENTRY_POINT_INFO(pHandleFillArrayData), |
Ian Rogers | 7655f29 | 2013-07-29 11:07:13 -0700 | [diff] [blame] | 1697 | QUICK_ENTRY_POINT_INFO(pJniMethodStart), |
| 1698 | QUICK_ENTRY_POINT_INFO(pJniMethodStartSynchronized), |
| 1699 | QUICK_ENTRY_POINT_INFO(pJniMethodEnd), |
| 1700 | QUICK_ENTRY_POINT_INFO(pJniMethodEndSynchronized), |
| 1701 | QUICK_ENTRY_POINT_INFO(pJniMethodEndWithReference), |
| 1702 | QUICK_ENTRY_POINT_INFO(pJniMethodEndWithReferenceSynchronized), |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1703 | QUICK_ENTRY_POINT_INFO(pLockObject), |
| 1704 | QUICK_ENTRY_POINT_INFO(pUnlockObject), |
Ian Rogers | 7655f29 | 2013-07-29 11:07:13 -0700 | [diff] [blame] | 1705 | QUICK_ENTRY_POINT_INFO(pCmpgDouble), |
| 1706 | QUICK_ENTRY_POINT_INFO(pCmpgFloat), |
| 1707 | QUICK_ENTRY_POINT_INFO(pCmplDouble), |
| 1708 | QUICK_ENTRY_POINT_INFO(pCmplFloat), |
| 1709 | QUICK_ENTRY_POINT_INFO(pFmod), |
| 1710 | QUICK_ENTRY_POINT_INFO(pSqrt), |
| 1711 | QUICK_ENTRY_POINT_INFO(pL2d), |
| 1712 | QUICK_ENTRY_POINT_INFO(pFmodf), |
| 1713 | QUICK_ENTRY_POINT_INFO(pL2f), |
| 1714 | QUICK_ENTRY_POINT_INFO(pD2iz), |
| 1715 | QUICK_ENTRY_POINT_INFO(pF2iz), |
| 1716 | QUICK_ENTRY_POINT_INFO(pIdivmod), |
| 1717 | QUICK_ENTRY_POINT_INFO(pD2l), |
| 1718 | QUICK_ENTRY_POINT_INFO(pF2l), |
| 1719 | QUICK_ENTRY_POINT_INFO(pLdiv), |
Ian Rogers | a9a8254 | 2013-10-04 11:17:26 -0700 | [diff] [blame] | 1720 | QUICK_ENTRY_POINT_INFO(pLmod), |
Ian Rogers | 7655f29 | 2013-07-29 11:07:13 -0700 | [diff] [blame] | 1721 | QUICK_ENTRY_POINT_INFO(pLmul), |
| 1722 | QUICK_ENTRY_POINT_INFO(pShlLong), |
| 1723 | QUICK_ENTRY_POINT_INFO(pShrLong), |
| 1724 | QUICK_ENTRY_POINT_INFO(pUshrLong), |
Ian Rogers | 7655f29 | 2013-07-29 11:07:13 -0700 | [diff] [blame] | 1725 | QUICK_ENTRY_POINT_INFO(pIndexOf), |
| 1726 | QUICK_ENTRY_POINT_INFO(pMemcmp16), |
| 1727 | QUICK_ENTRY_POINT_INFO(pStringCompareTo), |
| 1728 | QUICK_ENTRY_POINT_INFO(pMemcpy), |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1729 | QUICK_ENTRY_POINT_INFO(pQuickImtConflictTrampoline), |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1730 | QUICK_ENTRY_POINT_INFO(pQuickResolutionTrampoline), |
| 1731 | QUICK_ENTRY_POINT_INFO(pQuickToInterpreterBridge), |
Ian Rogers | 7655f29 | 2013-07-29 11:07:13 -0700 | [diff] [blame] | 1732 | QUICK_ENTRY_POINT_INFO(pInvokeDirectTrampolineWithAccessCheck), |
Ian Rogers | 7655f29 | 2013-07-29 11:07:13 -0700 | [diff] [blame] | 1733 | QUICK_ENTRY_POINT_INFO(pInvokeInterfaceTrampolineWithAccessCheck), |
| 1734 | QUICK_ENTRY_POINT_INFO(pInvokeStaticTrampolineWithAccessCheck), |
| 1735 | QUICK_ENTRY_POINT_INFO(pInvokeSuperTrampolineWithAccessCheck), |
| 1736 | QUICK_ENTRY_POINT_INFO(pInvokeVirtualTrampolineWithAccessCheck), |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1737 | QUICK_ENTRY_POINT_INFO(pCheckSuspend), |
| 1738 | QUICK_ENTRY_POINT_INFO(pTestSuspend), |
Ian Rogers | 7655f29 | 2013-07-29 11:07:13 -0700 | [diff] [blame] | 1739 | QUICK_ENTRY_POINT_INFO(pDeliverException), |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1740 | QUICK_ENTRY_POINT_INFO(pThrowArrayBounds), |
| 1741 | QUICK_ENTRY_POINT_INFO(pThrowDivZero), |
| 1742 | QUICK_ENTRY_POINT_INFO(pThrowNoSuchMethod), |
| 1743 | QUICK_ENTRY_POINT_INFO(pThrowNullPointer), |
| 1744 | QUICK_ENTRY_POINT_INFO(pThrowStackOverflow), |
Elliott Hughes | 98e2017 | 2012-04-24 15:38:13 -0700 | [diff] [blame] | 1745 | }; |
Ian Rogers | 7655f29 | 2013-07-29 11:07:13 -0700 | [diff] [blame] | 1746 | #undef QUICK_ENTRY_POINT_INFO |
Elliott Hughes | 98e2017 | 2012-04-24 15:38:13 -0700 | [diff] [blame] | 1747 | |
Elliott Hughes | 28fa76d | 2012-04-09 17:31:46 -0700 | [diff] [blame] | 1748 | void Thread::DumpThreadOffset(std::ostream& os, uint32_t offset, size_t size_of_pointers) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1749 | CHECK_EQ(size_of_pointers, 4U); // TODO: support 64-bit targets. |
Elliott Hughes | 98e2017 | 2012-04-24 15:38:13 -0700 | [diff] [blame] | 1750 | |
Brian Carlstrom | f69863b | 2013-07-17 21:53:13 -0700 | [diff] [blame] | 1751 | #define DO_THREAD_OFFSET(x) \ |
| 1752 | if (offset == static_cast<uint32_t>(OFFSETOF_VOLATILE_MEMBER(Thread, x))) { \ |
| 1753 | os << # x; \ |
| 1754 | return; \ |
| 1755 | } |
Ian Rogers | 474b6da | 2012-09-25 00:20:38 -0700 | [diff] [blame] | 1756 | DO_THREAD_OFFSET(state_and_flags_); |
Elliott Hughes | 98e2017 | 2012-04-24 15:38:13 -0700 | [diff] [blame] | 1757 | DO_THREAD_OFFSET(card_table_); |
| 1758 | DO_THREAD_OFFSET(exception_); |
Ian Rogers | 07ec8e1 | 2012-12-01 01:26:51 -0800 | [diff] [blame] | 1759 | DO_THREAD_OFFSET(opeer_); |
Elliott Hughes | 98e2017 | 2012-04-24 15:38:13 -0700 | [diff] [blame] | 1760 | DO_THREAD_OFFSET(jni_env_); |
| 1761 | DO_THREAD_OFFSET(self_); |
| 1762 | DO_THREAD_OFFSET(stack_end_); |
Elliott Hughes | 98e2017 | 2012-04-24 15:38:13 -0700 | [diff] [blame] | 1763 | DO_THREAD_OFFSET(suspend_count_); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1764 | DO_THREAD_OFFSET(thin_lock_thread_id_); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1765 | // DO_THREAD_OFFSET(top_of_managed_stack_); |
| 1766 | // DO_THREAD_OFFSET(top_of_managed_stack_pc_); |
Elliott Hughes | 98e2017 | 2012-04-24 15:38:13 -0700 | [diff] [blame] | 1767 | DO_THREAD_OFFSET(top_sirt_); |
Elliott Hughes | 28fa76d | 2012-04-09 17:31:46 -0700 | [diff] [blame] | 1768 | #undef DO_THREAD_OFFSET |
Elliott Hughes | 98e2017 | 2012-04-24 15:38:13 -0700 | [diff] [blame] | 1769 | |
| 1770 | size_t entry_point_count = arraysize(gThreadEntryPointInfo); |
Ian Rogers | 7655f29 | 2013-07-29 11:07:13 -0700 | [diff] [blame] | 1771 | CHECK_EQ(entry_point_count * size_of_pointers, |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1772 | sizeof(InterpreterEntryPoints) + sizeof(JniEntryPoints) + sizeof(PortableEntryPoints) + |
| 1773 | sizeof(QuickEntryPoints)); |
| 1774 | uint32_t expected_offset = OFFSETOF_MEMBER(Thread, interpreter_entrypoints_); |
Elliott Hughes | 98e2017 | 2012-04-24 15:38:13 -0700 | [diff] [blame] | 1775 | for (size_t i = 0; i < entry_point_count; ++i) { |
Ian Rogers | 474b6da | 2012-09-25 00:20:38 -0700 | [diff] [blame] | 1776 | CHECK_EQ(gThreadEntryPointInfo[i].offset, expected_offset) << gThreadEntryPointInfo[i].name; |
Elliott Hughes | 98e2017 | 2012-04-24 15:38:13 -0700 | [diff] [blame] | 1777 | expected_offset += size_of_pointers; |
| 1778 | if (gThreadEntryPointInfo[i].offset == offset) { |
| 1779 | os << gThreadEntryPointInfo[i].name; |
| 1780 | return; |
| 1781 | } |
| 1782 | } |
| 1783 | os << offset; |
Elliott Hughes | 28fa76d | 2012-04-09 17:31:46 -0700 | [diff] [blame] | 1784 | } |
| 1785 | |
jeffhao | 94d6df4 | 2012-11-26 16:02:12 -0800 | [diff] [blame] | 1786 | void Thread::QuickDeliverException() { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1787 | // Get exception from thread. |
| 1788 | ThrowLocation throw_location; |
| 1789 | mirror::Throwable* exception = GetException(&throw_location); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1790 | CHECK(exception != nullptr); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1791 | // 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] | 1792 | // resolution. |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1793 | ClearException(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1794 | bool is_deoptimization = (exception == reinterpret_cast<mirror::Throwable*>(-1)); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1795 | if (kDebugExceptionDelivery) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1796 | if (!is_deoptimization) { |
| 1797 | mirror::String* msg = exception->GetDetailMessage(); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1798 | std::string str_msg(msg != nullptr ? msg->ToModifiedUtf8() : ""); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1799 | DumpStack(LOG(INFO) << "Delivering exception: " << PrettyTypeOf(exception) |
| 1800 | << ": " << str_msg << "\n"); |
| 1801 | } else { |
| 1802 | DumpStack(LOG(INFO) << "Deoptimizing: "); |
| 1803 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1804 | } |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 1805 | CatchFinder catch_finder(this, throw_location, exception, is_deoptimization); |
| 1806 | catch_finder.FindCatch(); |
| 1807 | catch_finder.UpdateInstrumentationStack(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1808 | catch_finder.DoLongJump(); |
Ian Rogers | 9a8a888 | 2012-03-08 02:30:55 -0800 | [diff] [blame] | 1809 | LOG(FATAL) << "UNREACHABLE"; |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 1810 | } |
| 1811 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1812 | Context* Thread::GetLongJumpContext() { |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 1813 | Context* result = long_jump_context_; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1814 | if (result == nullptr) { |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1815 | result = Context::Create(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1816 | } else { |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1817 | long_jump_context_ = nullptr; // Avoid context being shared. |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 1818 | result->Reset(); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 1819 | } |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 1820 | return result; |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 1821 | } |
| 1822 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1823 | struct CurrentMethodVisitor : public StackVisitor { |
| 1824 | CurrentMethodVisitor(Thread* thread, Context* context) |
| 1825 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1826 | : StackVisitor(thread, context), this_object_(nullptr), method_(nullptr), dex_pc_(0) {} |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1827 | virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1828 | mirror::ArtMethod* m = GetMethod(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1829 | if (m->IsRuntimeMethod()) { |
| 1830 | // Continue if this is a runtime method. |
| 1831 | return true; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1832 | } |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1833 | if (context_ != nullptr) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1834 | this_object_ = GetThisObject(); |
| 1835 | } |
| 1836 | method_ = m; |
| 1837 | dex_pc_ = GetDexPc(); |
| 1838 | return false; |
| 1839 | } |
| 1840 | mirror::Object* this_object_; |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1841 | mirror::ArtMethod* method_; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1842 | uint32_t dex_pc_; |
| 1843 | }; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1844 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1845 | mirror::ArtMethod* Thread::GetCurrentMethod(uint32_t* dex_pc) const { |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1846 | CurrentMethodVisitor visitor(const_cast<Thread*>(this), nullptr); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1847 | visitor.WalkStack(false); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1848 | if (dex_pc != nullptr) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1849 | *dex_pc = visitor.dex_pc_; |
Elliott Hughes | 9fd66f5 | 2011-10-16 12:13:26 -0700 | [diff] [blame] | 1850 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1851 | return visitor.method_; |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 1852 | } |
| 1853 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1854 | ThrowLocation Thread::GetCurrentLocationForThrow() { |
| 1855 | Context* context = GetLongJumpContext(); |
| 1856 | CurrentMethodVisitor visitor(this, context); |
| 1857 | visitor.WalkStack(false); |
| 1858 | ReleaseLongJumpContext(context); |
| 1859 | return ThrowLocation(visitor.this_object_, visitor.method_, visitor.dex_pc_); |
| 1860 | } |
| 1861 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1862 | bool Thread::HoldsLock(mirror::Object* object) { |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1863 | if (object == nullptr) { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 1864 | return false; |
| 1865 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1866 | return object->GetLockOwnerThreadId() == thin_lock_thread_id_; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 1867 | } |
| 1868 | |
Ian Rogers | 40e3bac | 2012-11-20 00:09:14 -0800 | [diff] [blame] | 1869 | // RootVisitor parameters are: (const Object* obj, size_t vreg, const StackVisitor* visitor). |
| 1870 | template <typename RootVisitor> |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1871 | class ReferenceMapVisitor : public StackVisitor { |
Ian Rogers | d6b1f61 | 2011-09-27 13:38:14 -0700 | [diff] [blame] | 1872 | public: |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 1873 | ReferenceMapVisitor(Thread* thread, Context* context, const RootVisitor& visitor) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1874 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 1875 | : StackVisitor(thread, context), visitor_(visitor) {} |
Ian Rogers | d6b1f61 | 2011-09-27 13:38:14 -0700 | [diff] [blame] | 1876 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1877 | bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | 6a4be3a | 2011-10-20 16:34:03 -0700 | [diff] [blame] | 1878 | if (false) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1879 | LOG(INFO) << "Visiting stack roots in " << PrettyMethod(GetMethod()) |
| 1880 | << StringPrintf("@ PC:%04x", GetDexPc()); |
Brian Carlstrom | 6a4be3a | 2011-10-20 16:34:03 -0700 | [diff] [blame] | 1881 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1882 | ShadowFrame* shadow_frame = GetCurrentShadowFrame(); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1883 | if (shadow_frame != nullptr) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1884 | mirror::ArtMethod* m = shadow_frame->GetMethod(); |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 1885 | size_t num_regs = shadow_frame->NumberOfVRegs(); |
| 1886 | if (m->IsNative() || shadow_frame->HasReferenceArray()) { |
| 1887 | // SIRT for JNI or References for interpreter. |
| 1888 | for (size_t reg = 0; reg < num_regs; ++reg) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1889 | mirror::Object* ref = shadow_frame->GetVRegReference(reg); |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 1890 | if (ref != nullptr) { |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 1891 | mirror::Object* new_ref = ref; |
| 1892 | visitor_(&new_ref, reg, this); |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 1893 | if (new_ref != ref) { |
| 1894 | shadow_frame->SetVRegReference(reg, new_ref); |
| 1895 | } |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 1896 | } |
| 1897 | } |
| 1898 | } else { |
| 1899 | // Java method. |
| 1900 | // Portable path use DexGcMap and store in Method.native_gc_map_. |
| 1901 | const uint8_t* gc_map = m->GetNativeGcMap(); |
Brian Carlstrom | 7c6aca2 | 2014-01-31 18:44:56 -0800 | [diff] [blame] | 1902 | CHECK(gc_map != nullptr) << PrettyMethod(m); |
Vladimir Marko | 8171fc3 | 2013-11-26 17:05:58 +0000 | [diff] [blame] | 1903 | verifier::DexPcToReferenceMap dex_gc_map(gc_map); |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 1904 | uint32_t dex_pc = GetDexPc(); |
| 1905 | const uint8_t* reg_bitmap = dex_gc_map.FindBitMap(dex_pc); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1906 | DCHECK(reg_bitmap != nullptr); |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 1907 | num_regs = std::min(dex_gc_map.RegWidth() * 8, num_regs); |
| 1908 | for (size_t reg = 0; reg < num_regs; ++reg) { |
| 1909 | if (TestBitmap(reg, reg_bitmap)) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1910 | mirror::Object* ref = shadow_frame->GetVRegReference(reg); |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 1911 | if (ref != nullptr) { |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 1912 | mirror::Object* new_ref = ref; |
| 1913 | visitor_(&new_ref, reg, this); |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 1914 | if (new_ref != ref) { |
| 1915 | shadow_frame->SetVRegReference(reg, new_ref); |
| 1916 | } |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 1917 | } |
| 1918 | } |
| 1919 | } |
| 1920 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1921 | } else { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1922 | mirror::ArtMethod* m = GetMethod(); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1923 | // Process register map (which native and runtime methods don't have) |
Ian Rogers | 640495b | 2012-06-22 15:15:47 -0700 | [diff] [blame] | 1924 | if (!m->IsNative() && !m->IsRuntimeMethod() && !m->IsProxyMethod()) { |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 1925 | const uint8_t* native_gc_map = m->GetNativeGcMap(); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1926 | CHECK(native_gc_map != nullptr) << PrettyMethod(m); |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 1927 | mh_.ChangeMethod(m); |
| 1928 | const DexFile::CodeItem* code_item = mh_.GetCodeItem(); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1929 | DCHECK(code_item != nullptr) << PrettyMethod(m); // Can't be nullptr or how would we compile its instructions? |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 1930 | NativePcOffsetToReferenceMap map(native_gc_map); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1931 | size_t num_regs = std::min(map.RegWidth() * 8, |
| 1932 | static_cast<size_t>(code_item->registers_size_)); |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 1933 | if (num_regs > 0) { |
| 1934 | const uint8_t* reg_bitmap = map.FindBitMap(GetNativePcOffset()); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1935 | DCHECK(reg_bitmap != nullptr); |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 1936 | const VmapTable vmap_table(m->GetVmapTable()); |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 1937 | uint32_t core_spills = m->GetCoreSpillMask(); |
| 1938 | uint32_t fp_spills = m->GetFpSpillMask(); |
| 1939 | size_t frame_size = m->GetFrameSizeInBytes(); |
| 1940 | // For all dex registers in the bitmap |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1941 | mirror::ArtMethod** cur_quick_frame = GetCurrentQuickFrame(); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1942 | DCHECK(cur_quick_frame != nullptr); |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 1943 | for (size_t reg = 0; reg < num_regs; ++reg) { |
| 1944 | // Does this register hold a reference? |
| 1945 | if (TestBitmap(reg, reg_bitmap)) { |
| 1946 | uint32_t vmap_offset; |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 1947 | if (vmap_table.IsInContext(reg, kReferenceVReg, &vmap_offset)) { |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 1948 | int vmap_reg = vmap_table.ComputeRegister(core_spills, vmap_offset, kReferenceVReg); |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 1949 | // This is sound as spilled GPRs will be word sized (ie 32 or 64bit). |
| 1950 | mirror::Object** ref_addr = reinterpret_cast<mirror::Object**>(GetGPRAddress(vmap_reg)); |
| 1951 | if (*ref_addr != nullptr) { |
| 1952 | visitor_(ref_addr, reg, this); |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 1953 | } |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 1954 | } else { |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 1955 | StackReference<mirror::Object>* ref_addr = |
| 1956 | reinterpret_cast<StackReference<mirror::Object>*>( |
| 1957 | GetVRegAddr(cur_quick_frame, code_item, core_spills, fp_spills, frame_size, |
| 1958 | reg)); |
| 1959 | mirror::Object* ref = ref_addr->AsMirrorPtr(); |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 1960 | if (ref != nullptr) { |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 1961 | mirror::Object* new_ref = ref; |
| 1962 | visitor_(&new_ref, reg, this); |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 1963 | if (ref != new_ref) { |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 1964 | ref_addr->Assign(new_ref); |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 1965 | } |
| 1966 | } |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 1967 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 1968 | } |
Shih-wei Liao | 4f894e3 | 2011-09-27 21:33:19 -0700 | [diff] [blame] | 1969 | } |
Ian Rogers | d6b1f61 | 2011-09-27 13:38:14 -0700 | [diff] [blame] | 1970 | } |
| 1971 | } |
| 1972 | } |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 1973 | return true; |
Ian Rogers | d6b1f61 | 2011-09-27 13:38:14 -0700 | [diff] [blame] | 1974 | } |
| 1975 | |
| 1976 | private: |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 1977 | static bool TestBitmap(size_t reg, const uint8_t* reg_vector) { |
| 1978 | return ((reg_vector[reg / kBitsPerByte] >> (reg % kBitsPerByte)) & 0x01) != 0; |
Ian Rogers | d6b1f61 | 2011-09-27 13:38:14 -0700 | [diff] [blame] | 1979 | } |
| 1980 | |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 1981 | // Visitor for when we visit a root. |
Ian Rogers | 40e3bac | 2012-11-20 00:09:14 -0800 | [diff] [blame] | 1982 | const RootVisitor& visitor_; |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 1983 | |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 1984 | // A method helper we keep around to avoid dex file/cache re-computations. |
| 1985 | MethodHelper mh_; |
Ian Rogers | d6b1f61 | 2011-09-27 13:38:14 -0700 | [diff] [blame] | 1986 | }; |
| 1987 | |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 1988 | class RootCallbackVisitor { |
| 1989 | public: |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 1990 | RootCallbackVisitor(RootCallback* callback, void* arg, uint32_t tid) |
| 1991 | : callback_(callback), arg_(arg), tid_(tid) {} |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 1992 | |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 1993 | void operator()(mirror::Object** obj, size_t, const StackVisitor*) const { |
| 1994 | callback_(obj, arg_, tid_, kRootJavaFrame); |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 1995 | } |
| 1996 | |
| 1997 | private: |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 1998 | RootCallback* const callback_; |
Ian Rogers | 40e3bac | 2012-11-20 00:09:14 -0800 | [diff] [blame] | 1999 | void* const arg_; |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 2000 | const uint32_t tid_; |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 2001 | }; |
| 2002 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2003 | void Thread::SetClassLoaderOverride(mirror::ClassLoader* class_loader_override) { |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame^] | 2004 | VerifyObject(class_loader_override); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2005 | class_loader_override_ = class_loader_override; |
| 2006 | } |
| 2007 | |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 2008 | void Thread::VisitRoots(RootCallback* visitor, void* arg) { |
| 2009 | uint32_t thread_id = GetThreadId(); |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 2010 | if (opeer_ != nullptr) { |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 2011 | visitor(&opeer_, arg, thread_id, kRootThreadObject); |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 2012 | } |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 2013 | if (exception_ != nullptr) { |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 2014 | visitor(reinterpret_cast<mirror::Object**>(&exception_), arg, thread_id, kRootNativeStack); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 2015 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2016 | throw_location_.VisitRoots(visitor, arg); |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 2017 | if (class_loader_override_ != nullptr) { |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 2018 | visitor(reinterpret_cast<mirror::Object**>(&class_loader_override_), arg, thread_id, |
| 2019 | kRootNativeStack); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 2020 | } |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 2021 | jni_env_->locals.VisitRoots(visitor, arg, thread_id, kRootJNILocal); |
| 2022 | jni_env_->monitors.VisitRoots(visitor, arg, thread_id, kRootJNIMonitor); |
Mathieu Chartier | 89bb144 | 2014-02-12 10:06:23 -0800 | [diff] [blame] | 2023 | SirtVisitRoots(visitor, arg, thread_id); |
Ian Rogers | d6b1f61 | 2011-09-27 13:38:14 -0700 | [diff] [blame] | 2024 | // Visit roots on this thread's stack |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2025 | Context* context = GetLongJumpContext(); |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 2026 | RootCallbackVisitor visitorToCallback(visitor, arg, thread_id); |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 2027 | ReferenceMapVisitor<RootCallbackVisitor> mapper(this, context, visitorToCallback); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2028 | mapper.WalkStack(); |
| 2029 | ReleaseLongJumpContext(context); |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 2030 | for (instrumentation::InstrumentationStackFrame& frame : *GetInstrumentationStack()) { |
| 2031 | if (frame.this_object_ != nullptr) { |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 2032 | visitor(&frame.this_object_, arg, thread_id, kRootJavaFrame); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2033 | } |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 2034 | DCHECK(frame.method_ != nullptr); |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 2035 | visitor(reinterpret_cast<mirror::Object**>(&frame.method_), arg, thread_id, kRootJavaFrame); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2036 | } |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 2037 | } |
| 2038 | |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame^] | 2039 | static void VerifyRoot(mirror::Object** root, void* /*arg*/, uint32_t /*thread_id*/, |
| 2040 | RootType /*root_type*/) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2041 | VerifyObject(*root); |
jeffhao | 2504552 | 2012-03-13 19:34:37 -0700 | [diff] [blame] | 2042 | } |
| 2043 | |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame] | 2044 | void Thread::VerifyStackImpl() { |
jeffhao | e66ac79 | 2012-03-19 16:08:46 -0700 | [diff] [blame] | 2045 | UniquePtr<Context> context(Context::Create()); |
Mathieu Chartier | 89bb144 | 2014-02-12 10:06:23 -0800 | [diff] [blame] | 2046 | RootCallbackVisitor visitorToCallback(VerifyRoot, Runtime::Current()->GetHeap(), GetThreadId()); |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame] | 2047 | ReferenceMapVisitor<RootCallbackVisitor> mapper(this, context.get(), visitorToCallback); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 2048 | mapper.WalkStack(); |
jeffhao | 2504552 | 2012-03-13 19:34:37 -0700 | [diff] [blame] | 2049 | } |
jeffhao | 2504552 | 2012-03-13 19:34:37 -0700 | [diff] [blame] | 2050 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2051 | // Set the stack end to that to be used during a stack overflow |
| 2052 | void Thread::SetStackEndForStackOverflow() { |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 2053 | // During stack overflow we allow use of the full stack. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2054 | if (stack_end_ == stack_begin_) { |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 2055 | // However, we seem to have already extended to use the full stack. |
| 2056 | LOG(ERROR) << "Need to increase kStackOverflowReservedBytes (currently " |
| 2057 | << kStackOverflowReservedBytes << ")?"; |
| 2058 | DumpStack(LOG(ERROR)); |
| 2059 | LOG(FATAL) << "Recursive stack overflow."; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2060 | } |
| 2061 | |
| 2062 | stack_end_ = stack_begin_; |
| 2063 | } |
| 2064 | |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 2065 | void Thread::SetTlab(byte* start, byte* end) { |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 2066 | DCHECK_LE(start, end); |
| 2067 | thread_local_start_ = start; |
| 2068 | thread_local_pos_ = thread_local_start_; |
| 2069 | thread_local_end_ = end; |
| 2070 | thread_local_objects_ = 0; |
| 2071 | } |
| 2072 | |
Elliott Hughes | 330304d | 2011-08-12 14:28:05 -0700 | [diff] [blame] | 2073 | std::ostream& operator<<(std::ostream& os, const Thread& thread) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2074 | thread.ShortDump(os); |
Elliott Hughes | 330304d | 2011-08-12 14:28:05 -0700 | [diff] [blame] | 2075 | return os; |
| 2076 | } |
| 2077 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 2078 | } // namespace art |