thread.cc revision 5a4b8a236030460651a3136397d23ca6744e7eb7
18d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes/* 28d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * Copyright (C) 2011 The Android Open Source Project 38d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 48d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 58d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * you may not use this file except in compliance with the License. 68d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * You may obtain a copy of the License at 78d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 88d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 98d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 108d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * Unless required by applicable law or agreed to in writing, software 118d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 128d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 138d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * See the License for the specific language governing permissions and 148d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * limitations under the License. 158d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes */ 16b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 17752a0e606afdc7aa4825ebbe62b187b8abba14ccMathieu Chartier#define ATRACE_TAG ATRACE_TAG_DALVIK 18752a0e606afdc7aa4825ebbe62b187b8abba14ccMathieu Chartier 19578bbdc684db8ed68e9fedbc678669d27fa68b6eBrian Carlstrom#include "thread.h" 20b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 21752a0e606afdc7aa4825ebbe62b187b8abba14ccMathieu Chartier#include <cutils/trace.h> 22b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers#include <pthread.h> 232acf36d8cfeb5ddb293904148aa70f25ef6d8845Elliott Hughes#include <signal.h> 24dbf05b722af99ba2fd2f4c4fc7eb6c3e9880e5d1Brian Carlstrom#include <sys/resource.h> 25dbf05b722af99ba2fd2f4c4fc7eb6c3e9880e5d1Brian Carlstrom#include <sys/time.h> 26a09576416788b916095739e43a16917e7948f3a4Elliott Hughes 27b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro#include <algorithm> 28dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes#include <bitset> 29eb4f614f2eb53b92ebd416fa418f550861655887Elliott Hughes#include <cerrno> 30a09576416788b916095739e43a16917e7948f3a4Elliott Hughes#include <iostream> 31b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro#include <list> 32b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 33166db04e259ca51838c311891598664deeed85adIan Rogers#include "arch/context.h" 3476b6167407c2b6f5d40ad895b2793a6b037f54b2Elliott Hughes#include "base/mutex.h" 352dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "class_linker-inl.h" 3622d5e735f403c57525fe868304c7123f0ce66399Ian Rogers#include "class_linker.h" 3746e251bf7200cc06f5a9a82ee2030e650f5e1443Elliott Hughes#include "debugger.h" 384f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers#include "dex_file-inl.h" 397655f29fabc0a12765de828914a18314382e5a35Ian Rogers#include "entrypoints/entrypoint_utils.h" 40d889178ec78930538d9d6a66c3df9ee9afaffbb4Mathieu Chartier#include "entrypoints/quick/quick_alloc_entrypoints.h" 410c7abda482f53db3d153c073d1c7a145f84e0626Ian Rogers#include "gc_map.h" 421d54e73444e017d3a65234e0f193846f3e27472bIan Rogers#include "gc/accounting/card_table-inl.h" 43e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#include "gc/allocator/rosalloc.h" 441d54e73444e017d3a65234e0f193846f3e27472bIan Rogers#include "gc/heap.h" 451d54e73444e017d3a65234e0f193846f3e27472bIan Rogers#include "gc/space/space.h" 4622d5e735f403c57525fe868304c7123f0ce66399Ian Rogers#include "handle_scope-inl.h" 47eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier#include "handle_scope.h" 48c56057e40938c587a74984651a510e320a8cb4fdMathieu Chartier#include "indirect_reference_table-inl.h" 49c5f7c91ab89055cffb573fff7e06dbdd860bccedElliott Hughes#include "jni_internal.h" 50ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom#include "mirror/art_field-inl.h" 51ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom#include "mirror/art_method-inl.h" 522dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/class_loader.h" 5322d5e735f403c57525fe868304c7123f0ce66399Ian Rogers#include "mirror/class-inl.h" 542dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/object_array-inl.h" 552dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/stack_trace_element.h" 568e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes#include "monitor.h" 5722d5e735f403c57525fe868304c7123f0ce66399Ian Rogers#include "object_lock.h" 58fd3077e4b9ebadd281777310d26e64443858f653Sebastien Hertz#include "quick_exception_handler.h" 597624d25dad2d1ba25969ae704fccf68649103ae5Vladimir Marko#include "quick/quick_method_frame_info.h" 609a6bae896a2f003d7216603bf29771d105c10ca4Jesse Wilson#include "reflection.h" 61578bbdc684db8ed68e9fedbc678669d27fa68b6eBrian Carlstrom#include "runtime.h" 6200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#include "scoped_thread_state_change.h" 6346e251bf7200cc06f5a9a82ee2030e650f5e1443Elliott Hughes#include "ScopedLocalRef.h" 643c539ffccabada93c404c0dfba8b52926ae06d0cAnwar Ghuloum#include "ScopedUtfChars.h" 6568e76526e98432625464022cb26f66b9ef6f5af4Elliott Hughes#include "stack.h" 668daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes#include "thread_list.h" 6722d5e735f403c57525fe868304c7123f0ce66399Ian Rogers#include "thread-inl.h" 68a09576416788b916095739e43a16917e7948f3a4Elliott Hughes#include "utils.h" 69ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa#include "verifier/dex_gc_map.h" 704e30541a92381fb280cd0be9a1763b713ee4d64cMathieu Chartier#include "verify_object-inl.h" 711809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers#include "vmap_table.h" 72eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes#include "well_known_classes.h" 73b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 74b557353b22c728eecbd1c68593b482622c7782a8Carl Shapironamespace art { 75b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 760878d654e7be8c9666579e22522704d8887415ccIan Rogersbool Thread::is_started_ = false; 77b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiropthread_key_t Thread::pthread_key_self_; 780aded089f565008ba5908e395e5914ca4f91f2deDave AllisonConditionVariable* Thread::resume_cond_ = nullptr; 79648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allisonconst size_t Thread::kStackOverflowImplicitCheckSize = GetStackOverflowReservedBytes(kRuntimeISA); 80b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 817dc5166ea740359d381097a7ab382c1dd404055fElliott Hughesstatic const char* kThreadNameDuringStartup = "<native thread without managed peer>"; 827dc5166ea740359d381097a7ab382c1dd404055fElliott Hughes 835d76c435082332ef79a22962386fa92a0870e378Ian Rogersvoid Thread::InitCardTable() { 84dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tlsPtr_.card_table = Runtime::Current()->GetHeap()->GetCardTable()->GetBiasedBegin(); 855d76c435082332ef79a22962386fa92a0870e378Ian Rogers} 865d76c435082332ef79a22962386fa92a0870e378Ian Rogers 873ea0f42467790809fcfc9fc861605d465808090fElliott Hughesstatic void UnimplementedEntryPoint() { 883ea0f42467790809fcfc9fc861605d465808090fElliott Hughes UNIMPLEMENTED(FATAL); 893ea0f42467790809fcfc9fc861605d465808090fElliott Hughes} 903ea0f42467790809fcfc9fc861605d465808090fElliott Hughes 91848871b4d8481229c32e0d048a9856e5a9a17ef9Ian Rogersvoid InitEntryPoints(InterpreterEntryPoints* ipoints, JniEntryPoints* jpoints, 92848871b4d8481229c32e0d048a9856e5a9a17ef9Ian Rogers PortableEntryPoints* ppoints, QuickEntryPoints* qpoints); 937655f29fabc0a12765de828914a18314382e5a35Ian Rogers 94848871b4d8481229c32e0d048a9856e5a9a17ef9Ian Rogersvoid Thread::InitTlsEntryPoints() { 953ea0f42467790809fcfc9fc861605d465808090fElliott Hughes // Insert a placeholder so we can easily tell if we call an unimplemented entry point. 96dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers uintptr_t* begin = reinterpret_cast<uintptr_t*>(&tlsPtr_.interpreter_entrypoints); 97dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers uintptr_t* end = reinterpret_cast<uintptr_t*>(reinterpret_cast<uint8_t*>(begin) + 98dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers sizeof(tlsPtr_.quick_entrypoints)); 997655f29fabc0a12765de828914a18314382e5a35Ian Rogers for (uintptr_t* it = begin; it != end; ++it) { 1007655f29fabc0a12765de828914a18314382e5a35Ian Rogers *it = reinterpret_cast<uintptr_t>(UnimplementedEntryPoint); 1017655f29fabc0a12765de828914a18314382e5a35Ian Rogers } 102dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers InitEntryPoints(&tlsPtr_.interpreter_entrypoints, &tlsPtr_.jni_entrypoints, 103dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers &tlsPtr_.portable_entrypoints, &tlsPtr_.quick_entrypoints); 104c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes} 105c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes 1063b4c18933c24b8a33f38573c2ebcdb9aa16efeb5Hiroshi Yamauchivoid Thread::ResetQuickAllocEntryPointsForThread() { 107dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers ResetQuickAllocEntryPoints(&tlsPtr_.quick_entrypoints); 1083b4c18933c24b8a33f38573c2ebcdb9aa16efeb5Hiroshi Yamauchi} 1093b4c18933c24b8a33f38573c2ebcdb9aa16efeb5Hiroshi Yamauchi 11062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogersvoid Thread::SetDeoptimizationShadowFrame(ShadowFrame* sf) { 111dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tlsPtr_.deoptimization_shadow_frame = sf; 1123ea4ec5629613013ad9b0d7a69abdb94491ac46fbuzbee} 1133ea4ec5629613013ad9b0d7a69abdb94491ac46fbuzbee 11462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogersvoid Thread::SetDeoptimizationReturnValue(const JValue& ret_val) { 115dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tls64_.deoptimization_return_value.SetJ(ret_val.GetJ()); 116306057fd278d75bf3794bd5243a3b6652c487d18Ian Rogers} 117306057fd278d75bf3794bd5243a3b6652c487d18Ian Rogers 118306057fd278d75bf3794bd5243a3b6652c487d18Ian RogersShadowFrame* Thread::GetAndClearDeoptimizationShadowFrame(JValue* ret_val) { 119dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers ShadowFrame* sf = tlsPtr_.deoptimization_shadow_frame; 120dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tlsPtr_.deoptimization_shadow_frame = nullptr; 121dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers ret_val->SetJ(tls64_.deoptimization_return_value.GetJ()); 122306057fd278d75bf3794bd5243a3b6652c487d18Ian Rogers return sf; 123306057fd278d75bf3794bd5243a3b6652c487d18Ian Rogers} 124306057fd278d75bf3794bd5243a3b6652c487d18Ian Rogers 1252a0d4ec9532a89abe722e5babdfbb846ffaad721Andreas Gampevoid Thread::SetShadowFrameUnderConstruction(ShadowFrame* sf) { 1262a0d4ec9532a89abe722e5babdfbb846ffaad721Andreas Gampe sf->SetLink(tlsPtr_.shadow_frame_under_construction); 1272a0d4ec9532a89abe722e5babdfbb846ffaad721Andreas Gampe tlsPtr_.shadow_frame_under_construction = sf; 1282a0d4ec9532a89abe722e5babdfbb846ffaad721Andreas Gampe} 1292a0d4ec9532a89abe722e5babdfbb846ffaad721Andreas Gampe 1302a0d4ec9532a89abe722e5babdfbb846ffaad721Andreas Gampevoid Thread::ClearShadowFrameUnderConstruction() { 1312a0d4ec9532a89abe722e5babdfbb846ffaad721Andreas Gampe CHECK_NE(static_cast<ShadowFrame*>(nullptr), tlsPtr_.shadow_frame_under_construction); 1322a0d4ec9532a89abe722e5babdfbb846ffaad721Andreas Gampe tlsPtr_.shadow_frame_under_construction = tlsPtr_.shadow_frame_under_construction->GetLink(); 1332a0d4ec9532a89abe722e5babdfbb846ffaad721Andreas Gampe} 1342a0d4ec9532a89abe722e5babdfbb846ffaad721Andreas Gampe 135caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstromvoid Thread::InitTid() { 136dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tls32_.tid = ::art::GetTid(); 137caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom} 138caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom 139caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstromvoid Thread::InitAfterFork() { 1408029cbe626a467b344ff84d86170d757aa12ecd4Elliott Hughes // One thread (us) survived the fork, but we have a new tid so we need to 1418029cbe626a467b344ff84d86170d757aa12ecd4Elliott Hughes // update the value stashed in this Thread*. 142caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom InitTid(); 143caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom} 144caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom 14578128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstromvoid* Thread::CreateCallback(void* arg) { 14693e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes Thread* self = reinterpret_cast<Thread*>(arg); 147120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers Runtime* runtime = Runtime::Current(); 1480aded089f565008ba5908e395e5914ca4f91f2deDave Allison if (runtime == nullptr) { 149120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers LOG(ERROR) << "Thread attaching to non-existent runtime: " << *self; 1500aded089f565008ba5908e395e5914ca4f91f2deDave Allison return nullptr; 151120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers } 152120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers { 15350b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers // TODO: pass self to MutexLock - requires self to equal Thread::Current(), which is only true 15450b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers // after self->Init(). 1550aded089f565008ba5908e395e5914ca4f91f2deDave Allison MutexLock mu(nullptr, *Locks::runtime_shutdown_lock_); 156120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers // Check that if we got here we cannot be shutting down (as shutdown should never have started 157120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers // while threads are being born). 158590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier CHECK(!runtime->IsShuttingDownLocked()); 159120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers self->Init(runtime->GetThreadList(), runtime->GetJavaVM()); 160120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers Runtime::Current()->EndThreadBirth(); 161120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers } 16247179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes { 16300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ScopedObjectAccess soa(self); 164cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers 165cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers // Copy peer into self, deleting global reference when done. 166dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers CHECK(self->tlsPtr_.jpeer != nullptr); 167dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers self->tlsPtr_.opeer = soa.Decode<mirror::Object*>(self->tlsPtr_.jpeer); 168dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers self->GetJniEnv()->DeleteGlobalRef(self->tlsPtr_.jpeer); 169dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers self->tlsPtr_.jpeer = nullptr; 170eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier self->SetThreadName(self->GetThreadName(soa)->ToModifiedUtf8().c_str()); 171365c10235438607541fa2259a5fec48061b90bd8Ian Rogers Dbg::PostThreadStart(self); 17293e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 173365c10235438607541fa2259a5fec48061b90bd8Ian Rogers // Invoke the 'run' method of our java.lang.Thread. 174dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers mirror::Object* receiver = self->tlsPtr_.opeer; 175365c10235438607541fa2259a5fec48061b90bd8Ian Rogers jmethodID mid = WellKnownClasses::java_lang_Thread_run; 17653b8b09fc80329539585dcf43657bc5f4ecefdffIan Rogers InvokeVirtualOrInterfaceWithJValues(soa, receiver, mid, nullptr); 177365c10235438607541fa2259a5fec48061b90bd8Ian Rogers } 17800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Detach and delete self. 17900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Runtime::Current()->GetThreadList()->Unregister(self); 18093e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 1810aded089f565008ba5908e395e5914ca4f91f2deDave Allison return nullptr; 182b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro} 183b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 1842b7c4d196c8abe32f4ca633534917da9de53c359Mathieu ChartierThread* Thread::FromManagedThread(const ScopedObjectAccessAlreadyRunnable& soa, 1852dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::Object* thread_peer) { 186ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom mirror::ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_Thread_nativePeer); 187ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers Thread* result = reinterpret_cast<Thread*>(static_cast<uintptr_t>(f->GetLong(thread_peer))); 18800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Sanity check that if we have a result it is either suspended or we hold the thread_list_lock_ 18900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // to stop it from going away. 19081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers if (kIsDebugBuild) { 19181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers MutexLock mu(soa.Self(), *Locks::thread_suspend_count_lock_); 1920aded089f565008ba5908e395e5914ca4f91f2deDave Allison if (result != nullptr && !result->IsSuspended()) { 19381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers Locks::thread_list_lock_->AssertHeld(soa.Self()); 19481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers } 19500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 19600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers return result; 197761928d24e4e7ed7776b52243eaf9095ad35f448Elliott Hughes} 198761928d24e4e7ed7776b52243eaf9095ad35f448Elliott Hughes 1992b7c4d196c8abe32f4ca633534917da9de53c359Mathieu ChartierThread* Thread::FromManagedThread(const ScopedObjectAccessAlreadyRunnable& soa, 2002b7c4d196c8abe32f4ca633534917da9de53c359Mathieu Chartier jobject java_thread) { 2012dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers return FromManagedThread(soa, soa.Decode<mirror::Object*>(java_thread)); 20201158d7a57c8321370667a6045220237d16e0da8Elliott Hughes} 20301158d7a57c8321370667a6045220237d16e0da8Elliott Hughes 204ab7b9dcbfc4264a0bc4889c3e463ff88a67f6a30Elliott Hughesstatic size_t FixStackSize(size_t stack_size) { 2057502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // A stack size of zero means "use the default". 206d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (stack_size == 0) { 207d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes stack_size = Runtime::Current()->GetDefaultStackSize(); 208d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 20961e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 2106414a97a3c6dc101ae8ebc9480114d0c327e8e8dBrian Carlstrom // Dalvik used the bionic pthread default stack size for native threads, 2116414a97a3c6dc101ae8ebc9480114d0c327e8e8dBrian Carlstrom // so include that here to support apps that expect large native stacks. 2126414a97a3c6dc101ae8ebc9480114d0c327e8e8dBrian Carlstrom stack_size += 1 * MB; 2136414a97a3c6dc101ae8ebc9480114d0c327e8e8dBrian Carlstrom 2147502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // It's not possible to request a stack smaller than the system-defined PTHREAD_STACK_MIN. 2157502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes if (stack_size < PTHREAD_STACK_MIN) { 2167502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes stack_size = PTHREAD_STACK_MIN; 2177502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes } 2187502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 219f943914730db8ad2ff03d49a2cacd31885d08fd7Dave Allison if (Runtime::Current()->ExplicitStackOverflowChecks()) { 220f943914730db8ad2ff03d49a2cacd31885d08fd7Dave Allison // It's likely that callers are trying to ensure they have at least a certain amount of 221f943914730db8ad2ff03d49a2cacd31885d08fd7Dave Allison // stack space, so we should add our reserved space on top of what they requested, rather 222f943914730db8ad2ff03d49a2cacd31885d08fd7Dave Allison // than implicitly take it away from them. 2237ea6f79bbddd69d5db86a8656a31aaaf64ae2582Andreas Gampe stack_size += GetStackOverflowReservedBytes(kRuntimeISA); 224f943914730db8ad2ff03d49a2cacd31885d08fd7Dave Allison } else { 225f943914730db8ad2ff03d49a2cacd31885d08fd7Dave Allison // If we are going to use implicit stack checks, allocate space for the protected 226f943914730db8ad2ff03d49a2cacd31885d08fd7Dave Allison // region at the bottom of the stack. 227b090a18e4292dc339a3b2668bf6dc855928dee3aDave Allison stack_size += Thread::kStackOverflowImplicitCheckSize + 228b090a18e4292dc339a3b2668bf6dc855928dee3aDave Allison GetStackOverflowReservedBytes(kRuntimeISA); 229f943914730db8ad2ff03d49a2cacd31885d08fd7Dave Allison } 2307502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 2317502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // Some systems require the stack size to be a multiple of the system page size, so round up. 2327502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes stack_size = RoundUp(stack_size, kPageSize); 2337502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 2347502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes return stack_size; 2357502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes} 2367502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 23769dfe51b684dd9d510dbcb63295fe180f998efdeDave Allison// Global variable to prevent the compiler optimizing away the page reads for the stack. 23869dfe51b684dd9d510dbcb63295fe180f998efdeDave Allisonbyte dont_optimize_this; 23969dfe51b684dd9d510dbcb63295fe180f998efdeDave Allison 240f943914730db8ad2ff03d49a2cacd31885d08fd7Dave Allison// Install a protected region in the stack. This is used to trigger a SIGSEGV if a stack 241648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison// overflow is detected. It is located right below the stack_begin_. 24269dfe51b684dd9d510dbcb63295fe180f998efdeDave Allison// 243648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison// There is a little complexity here that deserves a special mention. On some 244648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison// architectures, the stack created using a VM_GROWSDOWN flag 24569dfe51b684dd9d510dbcb63295fe180f998efdeDave Allison// to prevent memory being allocated when it's not needed. This flag makes the 24669dfe51b684dd9d510dbcb63295fe180f998efdeDave Allison// kernel only allocate memory for the stack by growing down in memory. Because we 24769dfe51b684dd9d510dbcb63295fe180f998efdeDave Allison// want to put an mprotected region far away from that at the stack top, we need 24869dfe51b684dd9d510dbcb63295fe180f998efdeDave Allison// to make sure the pages for the stack are mapped in before we call mprotect. We do 24969dfe51b684dd9d510dbcb63295fe180f998efdeDave Allison// this by reading every page from the stack bottom (highest address) to the stack top. 25069dfe51b684dd9d510dbcb63295fe180f998efdeDave Allison// We then madvise this away. 251648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allisonvoid Thread::InstallImplicitProtection() { 252648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison byte* pregion = tlsPtr_.stack_begin - kStackOverflowProtectedSize; 253648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison byte* stack_himem = tlsPtr_.stack_end; 254648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison byte* stack_top = reinterpret_cast<byte*>(reinterpret_cast<uintptr_t>(&stack_himem) & 25569dfe51b684dd9d510dbcb63295fe180f998efdeDave Allison ~(kPageSize - 1)); // Page containing current top of stack. 25669dfe51b684dd9d510dbcb63295fe180f998efdeDave Allison 257648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison // First remove the protection on the protected region as will want to read and 258648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison // write it. This may fail (on the first attempt when the stack is not mapped) 259648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison // but we ignore that. 260648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison UnprotectStack(); 261f943914730db8ad2ff03d49a2cacd31885d08fd7Dave Allison 262648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison // Map in the stack. This must be done by reading from the 263648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison // current stack pointer downwards as the stack may be mapped using VM_GROWSDOWN 264648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison // in the kernel. Any access more than a page below the current SP might cause 265648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison // a segv. 266f943914730db8ad2ff03d49a2cacd31885d08fd7Dave Allison 267648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison // Read every page from the high address to the low. 268b090a18e4292dc339a3b2668bf6dc855928dee3aDave Allison for (byte* p = stack_top; p >= pregion; p -= kPageSize) { 269648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison dont_optimize_this = *p; 270f943914730db8ad2ff03d49a2cacd31885d08fd7Dave Allison } 2715cd33753b96d92c03e3cb10cb802e68fb6ef2f21Dave Allison 272f943914730db8ad2ff03d49a2cacd31885d08fd7Dave Allison VLOG(threads) << "installing stack protected region at " << std::hex << 273f943914730db8ad2ff03d49a2cacd31885d08fd7Dave Allison static_cast<void*>(pregion) << " to " << 274f943914730db8ad2ff03d49a2cacd31885d08fd7Dave Allison static_cast<void*>(pregion + kStackOverflowProtectedSize - 1); 275f943914730db8ad2ff03d49a2cacd31885d08fd7Dave Allison 276648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison // Protect the bottom of the stack to prevent read/write to it. 277648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison ProtectStack(); 2785cd33753b96d92c03e3cb10cb802e68fb6ef2f21Dave Allison 2795cd33753b96d92c03e3cb10cb802e68fb6ef2f21Dave Allison // Tell the kernel that we won't be needing these pages any more. 28069dfe51b684dd9d510dbcb63295fe180f998efdeDave Allison // NB. madvise will probably write zeroes into the memory (on linux it does). 281648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison uint32_t unwanted_size = stack_top - pregion - kPageSize; 282648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison madvise(pregion, unwanted_size, MADV_DONTNEED); 283f943914730db8ad2ff03d49a2cacd31885d08fd7Dave Allison} 284f943914730db8ad2ff03d49a2cacd31885d08fd7Dave Allison 285120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogersvoid Thread::CreateNativeThread(JNIEnv* env, jobject java_peer, size_t stack_size, bool is_daemon) { 2860aded089f565008ba5908e395e5914ca4f91f2deDave Allison CHECK(java_peer != nullptr); 287120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers Thread* self = static_cast<JNIEnvExt*>(env)->self; 288120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers Runtime* runtime = Runtime::Current(); 289dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier 290120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers // Atomically start the birth of the thread ensuring the runtime isn't shutting down. 291120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers bool thread_start_during_shutdown = false; 292365c10235438607541fa2259a5fec48061b90bd8Ian Rogers { 293120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers MutexLock mu(self, *Locks::runtime_shutdown_lock_); 294590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier if (runtime->IsShuttingDownLocked()) { 295120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers thread_start_during_shutdown = true; 296120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers } else { 297120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers runtime->StartThreadBirth(); 298120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers } 29900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 300120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers if (thread_start_during_shutdown) { 301120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers ScopedLocalRef<jclass> error_class(env, env->FindClass("java/lang/InternalError")); 302120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers env->ThrowNew(error_class.get(), "Thread starting during runtime shutdown"); 303120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers return; 304120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers } 305120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers 306120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers Thread* child_thread = new Thread(is_daemon); 307120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers // Use global JNI ref to hold peer live while child thread starts. 308dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers child_thread->tlsPtr_.jpeer = env->NewGlobalRef(java_peer); 309120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers stack_size = FixStackSize(stack_size); 310120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers 3113c50a4b4ba6d7d9369ee9a0bd6d30bf4c9c79bb0Anwar Ghuloum // Thread.start is synchronized, so we know that nativePeer is 0, and know that we're not racing to 312120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers // assign it. 313ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers env->SetLongField(java_peer, WellKnownClasses::java_lang_Thread_nativePeer, 314ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers reinterpret_cast<jlong>(child_thread)); 31500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 31600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers pthread_t new_pthread; 31700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers pthread_attr_t attr; 31800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers CHECK_PTHREAD_CALL(pthread_attr_init, (&attr), "new thread"); 31900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers CHECK_PTHREAD_CALL(pthread_attr_setdetachstate, (&attr, PTHREAD_CREATE_DETACHED), "PTHREAD_CREATE_DETACHED"); 32000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers CHECK_PTHREAD_CALL(pthread_attr_setstacksize, (&attr, stack_size), stack_size); 321120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers int pthread_create_result = pthread_create(&new_pthread, &attr, Thread::CreateCallback, child_thread); 32200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attr), "new thread"); 32300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 324120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers if (pthread_create_result != 0) { 32500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // pthread_create(3) failed, so clean up. 3269efc3e03982f042c3ce67cc2c64dbe0f0986119bBrian Carlstrom { 327120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers MutexLock mu(self, *Locks::runtime_shutdown_lock_); 328120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers runtime->EndThreadBirth(); 329120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers } 330120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers // Manually delete the global reference since Thread::Init will not have been run. 331dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers env->DeleteGlobalRef(child_thread->tlsPtr_.jpeer); 332dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers child_thread->tlsPtr_.jpeer = nullptr; 333120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers delete child_thread; 3340aded089f565008ba5908e395e5914ca4f91f2deDave Allison child_thread = nullptr; 335120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers // TODO: remove from thread group? 336ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers env->SetLongField(java_peer, WellKnownClasses::java_lang_Thread_nativePeer, 0); 337120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers { 3389efc3e03982f042c3ce67cc2c64dbe0f0986119bBrian Carlstrom std::string msg(StringPrintf("pthread_create (%s stack) failed: %s", 3399efc3e03982f042c3ce67cc2c64dbe0f0986119bBrian Carlstrom PrettySize(stack_size).c_str(), strerror(pthread_create_result))); 340120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers ScopedObjectAccess soa(env); 341120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers soa.Self()->ThrowOutOfMemoryError(msg.c_str()); 3429efc3e03982f042c3ce67cc2c64dbe0f0986119bBrian Carlstrom } 343365c10235438607541fa2259a5fec48061b90bd8Ian Rogers } 34493e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes} 34561e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 346120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogersvoid Thread::Init(ThreadList* thread_list, JavaVMExt* java_vm) { 347462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes // This function does all the initialization that must be run by the native thread it applies to. 348462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes // (When we create a new thread from managed code, we allocate the Thread* in Thread::Create so 349462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes // we can handshake with the corresponding native thread when it's ready.) Check this native 350462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes // thread hasn't been through here already... 3510aded089f565008ba5908e395e5914ca4f91f2deDave Allison CHECK(Thread::Current() == nullptr); 352d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes SetUpAlternateSignalStack(); 35393e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes InitCpu(); 354848871b4d8481229c32e0d048a9856e5a9a17ef9Ian Rogers InitTlsEntryPoints(); 355b373e091eac39b1a79c11f2dcbd610af01e9e8a9Dave Allison RemoveSuspendTrigger(); 3565d76c435082332ef79a22962386fa92a0870e378Ian Rogers InitCardTable(); 35701ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers InitTid(); 358120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers // Set pthread_self_ ahead of pthread_setspecific, that makes Thread::Current function, this 359120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers // avoids pthread_self_ ever being invalid when discovered from Thread::Current(). 360dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tlsPtr_.pthread_self = pthread_self(); 3610878d654e7be8c9666579e22522704d8887415ccIan Rogers CHECK(is_started_); 3626a607ad0902f3b8478e95d0b6b3e63a538571a3fElliott Hughes CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, this), "attach self"); 363120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers DCHECK_EQ(Thread::Current(), this); 364a5780dad67556297c8ca5f2608c53b193e6c4514Elliott Hughes 365dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tls32_.thin_lock_thread_id = thread_list->AllocThreadId(this); 366120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers InitStackHwm(); 3675fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 368dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tlsPtr_.jni_env = new JNIEnvExt(this, java_vm); 369120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers thread_list->Register(this); 37093e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes} 37193e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 372664bebf92eb2151b9b570ccd42ac4b6056c3ea9cMathieu ChartierThread* Thread::Attach(const char* thread_name, bool as_daemon, jobject thread_group, 373664bebf92eb2151b9b570ccd42ac4b6056c3ea9cMathieu Chartier bool create_peer) { 374120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers Thread* self; 375120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers Runtime* runtime = Runtime::Current(); 3760aded089f565008ba5908e395e5914ca4f91f2deDave Allison if (runtime == nullptr) { 377120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers LOG(ERROR) << "Thread attaching to non-existent runtime: " << thread_name; 3780aded089f565008ba5908e395e5914ca4f91f2deDave Allison return nullptr; 379120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers } 380120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers { 3810aded089f565008ba5908e395e5914ca4f91f2deDave Allison MutexLock mu(nullptr, *Locks::runtime_shutdown_lock_); 382590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier if (runtime->IsShuttingDownLocked()) { 383120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers LOG(ERROR) << "Thread attaching while runtime is shutting down: " << thread_name; 3840aded089f565008ba5908e395e5914ca4f91f2deDave Allison return nullptr; 385120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers } else { 386120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers Runtime::Current()->StartThreadBirth(); 387120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers self = new Thread(as_daemon); 388120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers self->Init(runtime->GetThreadList(), runtime->GetJavaVM()); 389120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers Runtime::Current()->EndThreadBirth(); 390120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers } 391120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers } 3925fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 393dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier CHECK_NE(self->GetState(), kRunnable); 394dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier self->SetState(kNative); 395726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes 396cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes // If we're the main thread, ClassLinker won't be created until after we're attached, 397cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes // so that thread needs a two-stage attach. Regular threads don't need this hack. 398d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes // In the compiler, all threads need this hack, because no-one's going to be getting 399d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes // a native peer! 400664bebf92eb2151b9b570ccd42ac4b6056c3ea9cMathieu Chartier if (create_peer) { 401462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes self->CreatePeer(thread_name, as_daemon, thread_group); 40206e3ad4a651c2c58dba5e865cd06d2f98462bf1dElliott Hughes } else { 40306e3ad4a651c2c58dba5e865cd06d2f98462bf1dElliott Hughes // These aren't necessary, but they improve diagnostics for unit tests & command-line tools. 4040aded089f565008ba5908e395e5914ca4f91f2deDave Allison if (thread_name != nullptr) { 405dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers self->tlsPtr_.name->assign(thread_name); 40622869a9026a08b544eca4cefd67386d347e30d2cElliott Hughes ::art::SetThreadName(thread_name); 4073ea69c0abce1b81b3c45033867d49b00e6d6b709Brian Carlstrom } else if (self->GetJniEnv()->check_jni) { 4083ea69c0abce1b81b3c45033867d49b00e6d6b709Brian Carlstrom LOG(WARNING) << *Thread::Current() << " attached without supplying a name"; 40922869a9026a08b544eca4cefd67386d347e30d2cElliott Hughes } 41036e0a955e48e9357bff1eab783e493107ebfff62Ian Rogers } 411cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes 4125fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes return self; 4135fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes} 4145fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 415365c10235438607541fa2259a5fec48061b90bd8Ian Rogersvoid Thread::CreatePeer(const char* name, bool as_daemon, jobject thread_group) { 416365c10235438607541fa2259a5fec48061b90bd8Ian Rogers Runtime* runtime = Runtime::Current(); 417365c10235438607541fa2259a5fec48061b90bd8Ian Rogers CHECK(runtime->IsStarted()); 418dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers JNIEnv* env = tlsPtr_.jni_env; 4195fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 4200aded089f565008ba5908e395e5914ca4f91f2deDave Allison if (thread_group == nullptr) { 421365c10235438607541fa2259a5fec48061b90bd8Ian Rogers thread_group = runtime->GetMainThreadGroup(); 422462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes } 423726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes ScopedLocalRef<jobject> thread_name(env, env->NewStringUTF(name)); 4248daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes jint thread_priority = GetNativePriority(); 4255fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes jboolean thread_is_daemon = as_daemon; 4265fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 427eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes ScopedLocalRef<jobject> peer(env, env->AllocObject(WellKnownClasses::java_lang_Thread)); 4280aded089f565008ba5908e395e5914ca4f91f2deDave Allison if (peer.get() == nullptr) { 429dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier CHECK(IsExceptionPending()); 430dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier return; 4315d4bdc29737a693027daaf6ed3f0792368eb4baeIan Rogers } 432cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers { 433cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers ScopedObjectAccess soa(this); 434dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tlsPtr_.opeer = soa.Decode<mirror::Object*>(peer.get()); 435cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers } 436eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes env->CallNonvirtualVoidMethod(peer.get(), 437eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes WellKnownClasses::java_lang_Thread, 438eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes WellKnownClasses::java_lang_Thread_init, 439365c10235438607541fa2259a5fec48061b90bd8Ian Rogers thread_group, thread_name.get(), thread_priority, thread_is_daemon); 44000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers AssertNoPendingException(); 441d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 442120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers Thread* self = this; 443120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers DCHECK_EQ(self, Thread::Current()); 444dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers env->SetLongField(peer.get(), WellKnownClasses::java_lang_Thread_nativePeer, 445dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers reinterpret_cast<jlong>(self)); 446120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers 447120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers ScopedObjectAccess soa(self); 448eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier StackHandleScope<1> hs(self); 4495a4b8a236030460651a3136397d23ca6744e7eb7Andreas Gampe MutableHandle<mirror::String> peer_thread_name(hs.NewHandle(GetThreadName(soa))); 450eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier if (peer_thread_name.Get() == nullptr) { 45100fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom // The Thread constructor should have set the Thread.name to a 45200fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom // non-null value. However, because we can run without code 45300fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom // available (in the compiler, in tests), we manually assign the 45400fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom // fields the constructor should have set. 455d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz if (runtime->IsActiveTransaction()) { 456d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz InitPeer<true>(soa, thread_is_daemon, thread_group, thread_name.get(), thread_priority); 457d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz } else { 458d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz InitPeer<false>(soa, thread_is_daemon, thread_group, thread_name.get(), thread_priority); 459d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz } 460eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier peer_thread_name.Assign(GetThreadName(soa)); 46100fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom } 462225f5a1df25241babd16cdba54056b9e2cd166a2Elliott Hughes // 'thread_name' may have been null, so don't trust 'peer_thread_name' to be non-null. 463eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier if (peer_thread_name.Get() != nullptr) { 464899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes SetThreadName(peer_thread_name->ToModifiedUtf8().c_str()); 46500fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom } 46661e019d291583029c01b61b93bea750f2b663c37Carl Shapiro} 46761e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 468d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertztemplate<bool kTransactionActive> 469d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertzvoid Thread::InitPeer(ScopedObjectAccess& soa, jboolean thread_is_daemon, jobject thread_group, 470d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz jobject thread_name, jint thread_priority) { 471d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz soa.DecodeField(WellKnownClasses::java_lang_Thread_daemon)-> 472dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers SetBoolean<kTransactionActive>(tlsPtr_.opeer, thread_is_daemon); 473d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz soa.DecodeField(WellKnownClasses::java_lang_Thread_group)-> 474dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers SetObject<kTransactionActive>(tlsPtr_.opeer, soa.Decode<mirror::Object*>(thread_group)); 475d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz soa.DecodeField(WellKnownClasses::java_lang_Thread_name)-> 476dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers SetObject<kTransactionActive>(tlsPtr_.opeer, soa.Decode<mirror::Object*>(thread_name)); 477d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz soa.DecodeField(WellKnownClasses::java_lang_Thread_priority)-> 478dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers SetInt<kTransactionActive>(tlsPtr_.opeer, thread_priority); 479d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz} 480d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz 481899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughesvoid Thread::SetThreadName(const char* name) { 482dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tlsPtr_.name->assign(name); 483899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes ::art::SetThreadName(name); 484899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes Dbg::DdmSendThreadNotification(this, CHUNK_TYPE("THNM")); 485899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes} 486899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes 487be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughesvoid Thread::InitStackHwm() { 488dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers void* read_stack_base; 489dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers size_t read_stack_size; 4906d3fc5615612e500a00aba0a0d331436fae8d996Elliott Hughes size_t read_guard_size; 4916d3fc5615612e500a00aba0a0d331436fae8d996Elliott Hughes GetThreadStack(tlsPtr_.pthread_self, &read_stack_base, &read_stack_size, &read_guard_size); 49236ecb789775eb5bd284ce5dd35d2e31e42354f24Elliott Hughes 4936d3fc5615612e500a00aba0a0d331436fae8d996Elliott Hughes // This is included in the SIGQUIT output, but it's useful here for thread debugging. 4946d3fc5615612e500a00aba0a0d331436fae8d996Elliott Hughes VLOG(threads) << StringPrintf("Native stack is at %p (%s with %s guard)", 4956d3fc5615612e500a00aba0a0d331436fae8d996Elliott Hughes read_stack_base, 4966d3fc5615612e500a00aba0a0d331436fae8d996Elliott Hughes PrettySize(read_stack_size).c_str(), 4976d3fc5615612e500a00aba0a0d331436fae8d996Elliott Hughes PrettySize(read_guard_size).c_str()); 498e188419b971936086a188843378375f5ced13724Elliott Hughes 499dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tlsPtr_.stack_begin = reinterpret_cast<byte*>(read_stack_base); 500dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tlsPtr_.stack_size = read_stack_size; 50136ecb789775eb5bd284ce5dd35d2e31e42354f24Elliott Hughes 502648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison // The minimum stack size we can cope with is the overflow reserved bytes (typically 503648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison // 8K) + the protected region size (4K) + another page (4K). Typically this will 504648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison // be 8+4+4 = 16K. The thread won't be able to do much with this stack even the GC takes 505648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison // between 8K and 12K. 506648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison uint32_t min_stack = GetStackOverflowReservedBytes(kRuntimeISA) + kStackOverflowProtectedSize 507648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison + 4 * KB; 508648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison if (read_stack_size <= min_stack) { 509dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers LOG(FATAL) << "Attempt to attach a thread with a too-small stack (" << read_stack_size 510839cc30467d51414e8185eee6101e19f5ab20871Elliott Hughes << " bytes)"; 511be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes } 512449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes 513932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers // Set stack_end_ to the bottom of the stack saving space of stack overflows 5148e219ae27624116b6d23e858fb21e93342f81d66Mathieu Chartier 5158e219ae27624116b6d23e858fb21e93342f81d66Mathieu Chartier Runtime* runtime = Runtime::Current(); 5168e219ae27624116b6d23e858fb21e93342f81d66Mathieu Chartier bool implicit_stack_check = !runtime->ExplicitStackOverflowChecks() && !runtime->IsCompiler(); 517b090a18e4292dc339a3b2668bf6dc855928dee3aDave Allison ResetDefaultStackEnd(); 518f943914730db8ad2ff03d49a2cacd31885d08fd7Dave Allison 519f943914730db8ad2ff03d49a2cacd31885d08fd7Dave Allison // Install the protected region if we are doing implicit overflow checks. 520f943914730db8ad2ff03d49a2cacd31885d08fd7Dave Allison if (implicit_stack_check) { 521648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison // The thread might have protected region at the bottom. We need 522648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison // to install our own region so we need to move the limits 523648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison // of the stack to make room for it. 524216cf23663789b06508f65f6dc0a72f181c9c03aDave Allison 5256d3fc5615612e500a00aba0a0d331436fae8d996Elliott Hughes tlsPtr_.stack_begin += read_guard_size + kStackOverflowProtectedSize; 5266d3fc5615612e500a00aba0a0d331436fae8d996Elliott Hughes tlsPtr_.stack_end += read_guard_size + kStackOverflowProtectedSize; 5276d3fc5615612e500a00aba0a0d331436fae8d996Elliott Hughes tlsPtr_.stack_size -= read_guard_size; 528648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison 529648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison InstallImplicitProtection(); 530f943914730db8ad2ff03d49a2cacd31885d08fd7Dave Allison } 531449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes 532449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes // Sanity check. 533449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes int stack_variable; 534dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers CHECK_GT(&stack_variable, reinterpret_cast<void*>(tlsPtr_.stack_end)); 535be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes} 536be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 53700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersvoid Thread::ShortDump(std::ostream& os) const { 53800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers os << "Thread["; 539d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers if (GetThreadId() != 0) { 54000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // If we're in kStarting, we won't have a thin lock id or tid yet. 541d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers os << GetThreadId() 54200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers << ",tid=" << GetTid() << ','; 543e0918556e7551de638870dcad3f2023f94f85a50Elliott Hughes } 544474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers os << GetState() 54500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers << ",Thread*=" << this 546dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers << ",peer=" << tlsPtr_.opeer 547dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers << ",\"" << *tlsPtr_.name << "\"" 54800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers << "]"; 549d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes} 550d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 55100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersvoid Thread::Dump(std::ostream& os) const { 55200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers DumpState(os); 55300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers DumpStack(os); 55400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers} 55500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 5562b7c4d196c8abe32f4ca633534917da9de53c359Mathieu Chartiermirror::String* Thread::GetThreadName(const ScopedObjectAccessAlreadyRunnable& soa) const { 557ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom mirror::ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_Thread_name); 558dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers return (tlsPtr_.opeer != nullptr) ? reinterpret_cast<mirror::String*>(f->GetObject(tlsPtr_.opeer)) : nullptr; 559fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes} 560fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes 561ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughesvoid Thread::GetThreadName(std::string& name) const { 562dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers name.assign(*tlsPtr_.name); 563ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes} 564ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes 56557dac6ed61a0a25c14d4e2fabc84435578d42360Jeff Haouint64_t Thread::GetCpuMicroTime() const { 56657dac6ed61a0a25c14d4e2fabc84435578d42360Jeff Hao#if defined(HAVE_POSIX_CLOCKS) 56757dac6ed61a0a25c14d4e2fabc84435578d42360Jeff Hao clockid_t cpu_clock_id; 568dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers pthread_getcpuclockid(tlsPtr_.pthread_self, &cpu_clock_id); 56957dac6ed61a0a25c14d4e2fabc84435578d42360Jeff Hao timespec now; 57057dac6ed61a0a25c14d4e2fabc84435578d42360Jeff Hao clock_gettime(cpu_clock_id, &now); 5710f6784737882199197796b67b99e5f1ded383beeIan Rogers return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000) + now.tv_nsec / UINT64_C(1000); 57257dac6ed61a0a25c14d4e2fabc84435578d42360Jeff Hao#else 57357dac6ed61a0a25c14d4e2fabc84435578d42360Jeff Hao UNIMPLEMENTED(WARNING); 57457dac6ed61a0a25c14d4e2fabc84435578d42360Jeff Hao return -1; 57557dac6ed61a0a25c14d4e2fabc84435578d42360Jeff Hao#endif 57657dac6ed61a0a25c14d4e2fabc84435578d42360Jeff Hao} 57757dac6ed61a0a25c14d4e2fabc84435578d42360Jeff Hao 57801ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers// Attempt to rectify locks so that we dump thread list with required locks before exiting. 57901ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogersstatic void UnsafeLogFatalForSuspendCount(Thread* self, Thread* thread) NO_THREAD_SAFETY_ANALYSIS { 580120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers LOG(ERROR) << *thread << " suspend count already zero."; 58101ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers Locks::thread_suspend_count_lock_->Unlock(self); 58201ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers if (!Locks::mutator_lock_->IsSharedHeld(self)) { 58301ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers Locks::mutator_lock_->SharedTryLock(self); 58401ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers if (!Locks::mutator_lock_->IsSharedHeld(self)) { 58501ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers LOG(WARNING) << "Dumping thread list without holding mutator_lock_"; 58601ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers } 58701ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers } 58801ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers if (!Locks::thread_list_lock_->IsExclusiveHeld(self)) { 58901ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers Locks::thread_list_lock_->TryLock(self); 59001ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers if (!Locks::thread_list_lock_->IsExclusiveHeld(self)) { 59101ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers LOG(WARNING) << "Dumping thread list without holding thread_list_lock_"; 59201ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers } 59301ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers } 59401ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers std::ostringstream ss; 5957b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers Runtime::Current()->GetThreadList()->Dump(ss); 596120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers LOG(FATAL) << ss.str(); 59701ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers} 59801ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers 59901ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogersvoid Thread::ModifySuspendCount(Thread* self, int delta, bool for_debugger) { 6002966e13d504a72d55c62bf864e183ec80703c699Ian Rogers if (kIsDebugBuild) { 6012966e13d504a72d55c62bf864e183ec80703c699Ian Rogers DCHECK(delta == -1 || delta == +1 || delta == -tls32_.debug_suspend_count) 6022966e13d504a72d55c62bf864e183ec80703c699Ian Rogers << delta << " " << tls32_.debug_suspend_count << " " << this; 6032966e13d504a72d55c62bf864e183ec80703c699Ian Rogers DCHECK_GE(tls32_.suspend_count, tls32_.debug_suspend_count) << this; 6042966e13d504a72d55c62bf864e183ec80703c699Ian Rogers Locks::thread_suspend_count_lock_->AssertHeld(self); 6052966e13d504a72d55c62bf864e183ec80703c699Ian Rogers if (this != self && !IsSuspended()) { 6062966e13d504a72d55c62bf864e183ec80703c699Ian Rogers Locks::thread_list_lock_->AssertHeld(self); 6072966e13d504a72d55c62bf864e183ec80703c699Ian Rogers } 608cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers } 609dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers if (UNLIKELY(delta < 0 && tls32_.suspend_count <= 0)) { 61001ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers UnsafeLogFatalForSuspendCount(self, this); 61100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers return; 61200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 61301ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers 614dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tls32_.suspend_count += delta; 61500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers if (for_debugger) { 616dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tls32_.debug_suspend_count += delta; 61700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 61801ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers 619dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers if (tls32_.suspend_count == 0) { 620474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers AtomicClearFlag(kSuspendRequest); 621474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers } else { 622474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers AtomicSetFlag(kSuspendRequest); 623b373e091eac39b1a79c11f2dcbd610af01e9e8a9Dave Allison TriggerSuspend(); 624474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers } 62500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers} 62600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 627752a0e606afdc7aa4825ebbe62b187b8abba14ccMathieu Chartiervoid Thread::RunCheckpointFunction() { 6280aded089f565008ba5908e395e5914ca4f91f2deDave Allison Closure *checkpoints[kMaxCheckpoints]; 6290aded089f565008ba5908e395e5914ca4f91f2deDave Allison 6300aded089f565008ba5908e395e5914ca4f91f2deDave Allison // Grab the suspend_count lock and copy the current set of 6310aded089f565008ba5908e395e5914ca4f91f2deDave Allison // checkpoints. Then clear the list and the flag. The RequestCheckpoint 6320aded089f565008ba5908e395e5914ca4f91f2deDave Allison // function will also grab this lock so we prevent a race between setting 6330aded089f565008ba5908e395e5914ca4f91f2deDave Allison // the kCheckpointRequest flag and clearing it. 6340aded089f565008ba5908e395e5914ca4f91f2deDave Allison { 6350aded089f565008ba5908e395e5914ca4f91f2deDave Allison MutexLock mu(this, *Locks::thread_suspend_count_lock_); 6360aded089f565008ba5908e395e5914ca4f91f2deDave Allison for (uint32_t i = 0; i < kMaxCheckpoints; ++i) { 637dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers checkpoints[i] = tlsPtr_.checkpoint_functions[i]; 638dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tlsPtr_.checkpoint_functions[i] = nullptr; 6390aded089f565008ba5908e395e5914ca4f91f2deDave Allison } 6400aded089f565008ba5908e395e5914ca4f91f2deDave Allison AtomicClearFlag(kCheckpointRequest); 6410aded089f565008ba5908e395e5914ca4f91f2deDave Allison } 6420aded089f565008ba5908e395e5914ca4f91f2deDave Allison 6430aded089f565008ba5908e395e5914ca4f91f2deDave Allison // Outside the lock, run all the checkpoint functions that 6440aded089f565008ba5908e395e5914ca4f91f2deDave Allison // we collected. 6450aded089f565008ba5908e395e5914ca4f91f2deDave Allison bool found_checkpoint = false; 6460aded089f565008ba5908e395e5914ca4f91f2deDave Allison for (uint32_t i = 0; i < kMaxCheckpoints; ++i) { 6470aded089f565008ba5908e395e5914ca4f91f2deDave Allison if (checkpoints[i] != nullptr) { 6480aded089f565008ba5908e395e5914ca4f91f2deDave Allison ATRACE_BEGIN("Checkpoint function"); 6490aded089f565008ba5908e395e5914ca4f91f2deDave Allison checkpoints[i]->Run(this); 6500aded089f565008ba5908e395e5914ca4f91f2deDave Allison ATRACE_END(); 6510aded089f565008ba5908e395e5914ca4f91f2deDave Allison found_checkpoint = true; 6520aded089f565008ba5908e395e5914ca4f91f2deDave Allison } 6530aded089f565008ba5908e395e5914ca4f91f2deDave Allison } 6540aded089f565008ba5908e395e5914ca4f91f2deDave Allison CHECK(found_checkpoint); 655752a0e606afdc7aa4825ebbe62b187b8abba14ccMathieu Chartier} 656752a0e606afdc7aa4825ebbe62b187b8abba14ccMathieu Chartier 6570e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartierbool Thread::RequestCheckpoint(Closure* function) { 65859cde534aa295bad7de29472b3cce9576d7996a8Chris Dearman union StateAndFlags old_state_and_flags; 659dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers old_state_and_flags.as_int = tls32_.state_and_flags.as_int; 660d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers if (old_state_and_flags.as_struct.state != kRunnable) { 661d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers return false; // Fail, thread is suspended and so can't run a checkpoint. 662d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers } 6630aded089f565008ba5908e395e5914ca4f91f2deDave Allison 6640aded089f565008ba5908e395e5914ca4f91f2deDave Allison uint32_t available_checkpoint = kMaxCheckpoints; 6650aded089f565008ba5908e395e5914ca4f91f2deDave Allison for (uint32_t i = 0 ; i < kMaxCheckpoints; ++i) { 666dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers if (tlsPtr_.checkpoint_functions[i] == nullptr) { 6670aded089f565008ba5908e395e5914ca4f91f2deDave Allison available_checkpoint = i; 6680aded089f565008ba5908e395e5914ca4f91f2deDave Allison break; 6690aded089f565008ba5908e395e5914ca4f91f2deDave Allison } 6700aded089f565008ba5908e395e5914ca4f91f2deDave Allison } 6710aded089f565008ba5908e395e5914ca4f91f2deDave Allison if (available_checkpoint == kMaxCheckpoints) { 6720aded089f565008ba5908e395e5914ca4f91f2deDave Allison // No checkpoint functions available, we can't run a checkpoint 6730aded089f565008ba5908e395e5914ca4f91f2deDave Allison return false; 674d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers } 675dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tlsPtr_.checkpoint_functions[available_checkpoint] = function; 6760aded089f565008ba5908e395e5914ca4f91f2deDave Allison 677d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers // Checkpoint function installed now install flag bit. 678858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier // We must be runnable to request a checkpoint. 6795f51d4b80058236759fea1d932470a57f348c199Mathieu Chartier DCHECK_EQ(old_state_and_flags.as_struct.state, kRunnable); 68059cde534aa295bad7de29472b3cce9576d7996a8Chris Dearman union StateAndFlags new_state_and_flags; 68159cde534aa295bad7de29472b3cce9576d7996a8Chris Dearman new_state_and_flags.as_int = old_state_and_flags.as_int; 682858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier new_state_and_flags.as_struct.flags |= kCheckpointRequest; 6838c1b5f71a8005743756206120624121d7678381fIan Rogers bool success = 6848c1b5f71a8005743756206120624121d7678381fIan Rogers tls32_.state_and_flags.as_atomic_int.CompareExchangeStrongSequentiallyConsistent(old_state_and_flags.as_int, 6858c1b5f71a8005743756206120624121d7678381fIan Rogers new_state_and_flags.as_int); 6868c1b5f71a8005743756206120624121d7678381fIan Rogers if (UNLIKELY(!success)) { 687d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers // The thread changed state before the checkpoint was installed. 688dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers CHECK_EQ(tlsPtr_.checkpoint_functions[available_checkpoint], function); 689dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tlsPtr_.checkpoint_functions[available_checkpoint] = nullptr; 6900aded089f565008ba5908e395e5914ca4f91f2deDave Allison } else { 6910aded089f565008ba5908e395e5914ca4f91f2deDave Allison CHECK_EQ(ReadFlag(kCheckpointRequest), true); 692b373e091eac39b1a79c11f2dcbd610af01e9e8a9Dave Allison TriggerSuspend(); 693d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers } 6948c1b5f71a8005743756206120624121d7678381fIan Rogers return success; 695858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier} 696858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier 69700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersvoid Thread::FullSuspendCheck() { 69800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers VLOG(threads) << this << " self-suspending"; 699752a0e606afdc7aa4825ebbe62b187b8abba14ccMathieu Chartier ATRACE_BEGIN("Full suspend check"); 70000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Make thread appear suspended to other threads, release mutator_lock_. 70100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers TransitionFromRunnableToSuspended(kSuspended); 70200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Transition back to runnable noting requests to suspend, re-acquire share on mutator_lock_. 70300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers TransitionFromSuspendedToRunnable(); 704752a0e606afdc7aa4825ebbe62b187b8abba14ccMathieu Chartier ATRACE_END(); 70500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers VLOG(threads) << this << " self-reviving"; 70600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers} 70700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 708abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughesvoid Thread::DumpState(std::ostream& os, const Thread* thread, pid_t tid) { 709d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes std::string group_name; 710d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes int priority; 711d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes bool is_daemon = false; 71281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers Thread* self = Thread::Current(); 713d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 714c7a966dbba6902618ff0959d604c05d7570df8c8Mathieu Chartier // Don't do this if we are aborting since the GC may have all the threads suspended. This will 715c7a966dbba6902618ff0959d604c05d7570df8c8Mathieu Chartier // cause ScopedObjectAccessUnchecked to deadlock. 716c7a966dbba6902618ff0959d604c05d7570df8c8Mathieu Chartier if (gAborting == 0 && self != nullptr && thread != nullptr && thread->tlsPtr_.opeer != nullptr) { 717cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers ScopedObjectAccessUnchecked soa(self); 718dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers priority = soa.DecodeField(WellKnownClasses::java_lang_Thread_priority) 719dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers ->GetInt(thread->tlsPtr_.opeer); 720dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers is_daemon = soa.DecodeField(WellKnownClasses::java_lang_Thread_daemon) 721dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers ->GetBoolean(thread->tlsPtr_.opeer); 722d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 7232dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::Object* thread_group = 724dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers soa.DecodeField(WellKnownClasses::java_lang_Thread_group)->GetObject(thread->tlsPtr_.opeer); 725120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers 7260aded089f565008ba5908e395e5914ca4f91f2deDave Allison if (thread_group != nullptr) { 727ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom mirror::ArtField* group_name_field = 7282dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_name); 7292dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::String* group_name_string = 7302dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers reinterpret_cast<mirror::String*>(group_name_field->GetObject(thread_group)); 7310aded089f565008ba5908e395e5914ca4f91f2deDave Allison group_name = (group_name_string != nullptr) ? group_name_string->ToModifiedUtf8() : "<null>"; 732d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 733d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } else { 734d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes priority = GetNativePriority(); 735dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes } 736d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 737abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes std::string scheduler_group_name(GetSchedulerGroupName(tid)); 7381bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughes if (scheduler_group_name.empty()) { 7391bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughes scheduler_group_name = "default"; 740d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 741d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 7420aded089f565008ba5908e395e5914ca4f91f2deDave Allison if (thread != nullptr) { 743dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers os << '"' << *thread->tlsPtr_.name << '"'; 744abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes if (is_daemon) { 745abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes os << " daemon"; 746abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes } 747abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes os << " prio=" << priority 748d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers << " tid=" << thread->GetThreadId() 74980537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes << " " << thread->GetState(); 75080537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes if (thread->IsStillStarting()) { 75180537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes os << " (still starting up)"; 75280537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes } 75380537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes os << "\n"; 754abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes } else { 755289be85116aaf7c48413858b5d0448868b4e61f3Elliott Hughes os << '"' << ::art::GetThreadName(tid) << '"' 756abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes << " prio=" << priority 757abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes << " (not attached)\n"; 758abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes } 759abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes 7600aded089f565008ba5908e395e5914ca4f91f2deDave Allison if (thread != nullptr) { 76181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers MutexLock mu(self, *Locks::thread_suspend_count_lock_); 762abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes os << " | group=\"" << group_name << "\"" 763dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers << " sCount=" << thread->tls32_.suspend_count 764dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers << " dsCount=" << thread->tls32_.debug_suspend_count 765dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers << " obj=" << reinterpret_cast<void*>(thread->tlsPtr_.opeer) 766abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes << " self=" << reinterpret_cast<const void*>(thread) << "\n"; 767d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 7680d39c12238499ca9ccc34d1532c443335e7c1044Elliott Hughes 769abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes os << " | sysTid=" << tid 770abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes << " nice=" << getpriority(PRIO_PROCESS, tid) 7710d39c12238499ca9ccc34d1532c443335e7c1044Elliott Hughes << " cgrp=" << scheduler_group_name; 7720aded089f565008ba5908e395e5914ca4f91f2deDave Allison if (thread != nullptr) { 7730d39c12238499ca9ccc34d1532c443335e7c1044Elliott Hughes int policy; 7740d39c12238499ca9ccc34d1532c443335e7c1044Elliott Hughes sched_param sp; 775dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers CHECK_PTHREAD_CALL(pthread_getschedparam, (thread->tlsPtr_.pthread_self, &policy, &sp), 776dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers __FUNCTION__); 7770d39c12238499ca9ccc34d1532c443335e7c1044Elliott Hughes os << " sched=" << policy << "/" << sp.sched_priority 778dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers << " handle=" << reinterpret_cast<void*>(thread->tlsPtr_.pthread_self); 7790d39c12238499ca9ccc34d1532c443335e7c1044Elliott Hughes } 7800d39c12238499ca9ccc34d1532c443335e7c1044Elliott Hughes os << "\n"; 781d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 782d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes // Grab the scheduler stats for this thread. 783d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes std::string scheduler_stats; 784abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes if (ReadFileToString(StringPrintf("/proc/self/task/%d/schedstat", tid), &scheduler_stats)) { 7857934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom scheduler_stats.resize(scheduler_stats.size() - 1); // Lose the trailing '\n'. 786d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } else { 787d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes scheduler_stats = "0 0 0"; 788d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 789d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 790ba0b9c55adce3f533845ab1b25c552589e5b4118Elliott Hughes char native_thread_state = '?'; 791d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes int utime = 0; 792d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes int stime = 0; 793d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes int task_cpu = 0; 7942921201dce37ba40c55b89c0deca3c34bf64168eBrian Carlstrom GetTaskStats(tid, &native_thread_state, &utime, &stime, &task_cpu); 795d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 796ba0b9c55adce3f533845ab1b25c552589e5b4118Elliott Hughes os << " | state=" << native_thread_state 797ba0b9c55adce3f533845ab1b25c552589e5b4118Elliott Hughes << " schedstat=( " << scheduler_stats << " )" 798d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " utm=" << utime 799d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " stm=" << stime 800abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes << " core=" << task_cpu 801abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes << " HZ=" << sysconf(_SC_CLK_TCK) << "\n"; 8020aded089f565008ba5908e395e5914ca4f91f2deDave Allison if (thread != nullptr) { 803dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers os << " | stack=" << reinterpret_cast<void*>(thread->tlsPtr_.stack_begin) << "-" 804dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers << reinterpret_cast<void*>(thread->tlsPtr_.stack_end) << " stackSize=" 805dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers << PrettySize(thread->tlsPtr_.stack_size) << "\n"; 8069db911eb76976fc1c73c8e563beb3620f1cadf99Mathieu Chartier // Dump the held mutexes. 8079db911eb76976fc1c73c8e563beb3620f1cadf99Mathieu Chartier os << " | held mutexes="; 8089db911eb76976fc1c73c8e563beb3620f1cadf99Mathieu Chartier for (size_t i = 0; i < kLockLevelCount; ++i) { 8099db911eb76976fc1c73c8e563beb3620f1cadf99Mathieu Chartier if (i != kMonitorLock) { 8109db911eb76976fc1c73c8e563beb3620f1cadf99Mathieu Chartier BaseMutex* mutex = thread->GetHeldMutex(static_cast<LockLevel>(i)); 8119db911eb76976fc1c73c8e563beb3620f1cadf99Mathieu Chartier if (mutex != nullptr) { 8129db911eb76976fc1c73c8e563beb3620f1cadf99Mathieu Chartier os << " \"" << mutex->GetName() << "\""; 8139db911eb76976fc1c73c8e563beb3620f1cadf99Mathieu Chartier if (mutex->IsReaderWriterMutex()) { 8149db911eb76976fc1c73c8e563beb3620f1cadf99Mathieu Chartier ReaderWriterMutex* rw_mutex = down_cast<ReaderWriterMutex*>(mutex); 815a9d8c4ce62fff49580fa70d3fcb3aee45344d8caIan Rogers if (rw_mutex->GetExclusiveOwnerTid() == static_cast<uint64_t>(tid)) { 8169db911eb76976fc1c73c8e563beb3620f1cadf99Mathieu Chartier os << "(exclusive held)"; 8179db911eb76976fc1c73c8e563beb3620f1cadf99Mathieu Chartier } else { 8189db911eb76976fc1c73c8e563beb3620f1cadf99Mathieu Chartier os << "(shared held)"; 8199db911eb76976fc1c73c8e563beb3620f1cadf99Mathieu Chartier } 8209db911eb76976fc1c73c8e563beb3620f1cadf99Mathieu Chartier } 8219db911eb76976fc1c73c8e563beb3620f1cadf99Mathieu Chartier } 8229db911eb76976fc1c73c8e563beb3620f1cadf99Mathieu Chartier } 8239db911eb76976fc1c73c8e563beb3620f1cadf99Mathieu Chartier } 8249db911eb76976fc1c73c8e563beb3620f1cadf99Mathieu Chartier os << "\n"; 825abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes } 826abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes} 827abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes 828abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughesvoid Thread::DumpState(std::ostream& os) const { 829abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes Thread::DumpState(os, this, GetTid()); 830a09576416788b916095739e43a16917e7948f3a4Elliott Hughes} 831a09576416788b916095739e43a16917e7948f3a4Elliott Hughes 8320399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogersstruct StackDumpVisitor : public StackVisitor { 8337a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers StackDumpVisitor(std::ostream& os, Thread* thread, Context* context, bool can_allocate) 834b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) 8357a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers : StackVisitor(thread, context), os(os), thread(thread), can_allocate(can_allocate), 8360aded089f565008ba5908e395e5914ca4f91f2deDave Allison last_method(nullptr), last_line_number(0), repetition_count(0), frame_count(0) { 837d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 838d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 839bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers virtual ~StackDumpVisitor() { 840e85d2e924953f1a13b7152edba1c13853e6f3a86Elliott Hughes if (frame_count == 0) { 841e85d2e924953f1a13b7152edba1c13853e6f3a86Elliott Hughes os << " (no managed stack frames)\n"; 842e85d2e924953f1a13b7152edba1c13853e6f3a86Elliott Hughes } 843d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 844d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 845b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 846ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom mirror::ArtMethod* m = GetMethod(); 8470399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers if (m->IsRuntimeMethod()) { 848530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes return true; 8499086572fa809d1a19d886b467e4da3ce42016982Ian Rogers } 85028ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers const int kMaxRepetition = 3; 8512dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::Class* c = m->GetDeclaringClass(); 852ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers mirror::DexCache* dex_cache = c->GetDexCache(); 853b861dc0077342d5a1d2dd9cade3f6990620778ecIan Rogers int line_number = -1; 8540aded089f565008ba5908e395e5914ca4f91f2deDave Allison if (dex_cache != nullptr) { // be tolerant of bad input 8554445a7e3398a6143939168097a3aa275b734504dIan Rogers const DexFile& dex_file = *dex_cache->GetDexFile(); 856c751fdcc9491c1b60c3db517fbc41bb98e92441fMathieu Chartier line_number = dex_file.GetLineNumFromPC(m, GetDexPc(false)); 857b861dc0077342d5a1d2dd9cade3f6990620778ecIan Rogers } 85828ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers if (line_number == last_line_number && last_method == m) { 8590aded089f565008ba5908e395e5914ca4f91f2deDave Allison ++repetition_count; 860d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } else { 86128ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers if (repetition_count >= kMaxRepetition) { 86228ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers os << " ... repeated " << (repetition_count - kMaxRepetition) << " times\n"; 86328ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers } 86428ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers repetition_count = 0; 86528ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers last_line_number = line_number; 86628ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers last_method = m; 86728ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers } 86828ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers if (repetition_count < kMaxRepetition) { 86928ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers os << " at " << PrettyMethod(m, false); 87028ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers if (m->IsNative()) { 87128ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers os << "(Native method)"; 87228ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers } else { 873bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier const char* source_file(m->GetDeclaringClassSourceFile()); 8740aded089f565008ba5908e395e5914ca4f91f2deDave Allison os << "(" << (source_file != nullptr ? source_file : "unavailable") 8752e450bf45e1bacc9c356f6ab239ccfb31bd8d7e4Brian Carlstrom << ":" << line_number << ")"; 87628ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers } 87728ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers os << "\n"; 87808fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes if (frame_count == 0) { 87908fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes Monitor::DescribeWait(os, thread); 88008fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes } 88108fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes if (can_allocate) { 882956a5228276693a7317ae6b41bfe7a7f0f3cbe6bAndreas Gampe // Visit locks, but do not abort on errors. This would trigger a nested abort. 883956a5228276693a7317ae6b41bfe7a7f0f3cbe6bAndreas Gampe Monitor::VisitLocks(this, DumpLockedObject, &os, false); 88408fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes } 885d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 8868e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 88708fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes ++frame_count; 888530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes return true; 889d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 8904993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes 8912dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers static void DumpLockedObject(mirror::Object* o, void* context) 8924993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 8934993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes std::ostream& os = *reinterpret_cast<std::ostream*>(context); 894d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers os << " - locked "; 895d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers if (o == nullptr) { 896d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers os << "an unknown object"; 897d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers } else { 8984d7f61d44a732cfbc8573e5d93364983fd746888Mathieu Chartier if ((o->GetLockWord(false).GetState() == LockWord::kThinLocked) && 899d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers Locks::mutator_lock_->IsExclusiveHeld(Thread::Current())) { 900d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers // Getting the identity hashcode here would result in lock inflation and suspension of the 901d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers // current thread, which isn't safe if this is the only runnable thread. 902d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers os << StringPrintf("<@addr=0x%" PRIxPTR "> (a %s)", reinterpret_cast<intptr_t>(o), 903d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers PrettyTypeOf(o).c_str()); 904d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers } else { 905d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers os << StringPrintf("<0x%08x> (a %s)", o->IdentityHashCode(), PrettyTypeOf(o).c_str()); 906d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers } 907d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers } 908d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers os << "\n"; 9094993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes } 9104993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes 91108fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes std::ostream& os; 91208fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes const Thread* thread; 9130878d654e7be8c9666579e22522704d8887415ccIan Rogers const bool can_allocate; 914ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom mirror::ArtMethod* last_method; 91528ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers int last_line_number; 91628ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers int repetition_count; 9178e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes int frame_count; 918d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes}; 919d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 92033e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogersstatic bool ShouldShowNativeStack(const Thread* thread) 92133e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 922aef701da38d14bd479cb275f78e2adaa00f1c54aElliott Hughes ThreadState state = thread->GetState(); 923aef701da38d14bd479cb275f78e2adaa00f1c54aElliott Hughes 924aef701da38d14bd479cb275f78e2adaa00f1c54aElliott Hughes // In native code somewhere in the VM (one of the kWaitingFor* states)? That's interesting. 925aef701da38d14bd479cb275f78e2adaa00f1c54aElliott Hughes if (state > kWaiting && state < kStarting) { 926aef701da38d14bd479cb275f78e2adaa00f1c54aElliott Hughes return true; 927aef701da38d14bd479cb275f78e2adaa00f1c54aElliott Hughes } 928aef701da38d14bd479cb275f78e2adaa00f1c54aElliott Hughes 929aef701da38d14bd479cb275f78e2adaa00f1c54aElliott Hughes // In an Object.wait variant or Thread.sleep? That's not interesting. 930aef701da38d14bd479cb275f78e2adaa00f1c54aElliott Hughes if (state == kTimedWaiting || state == kSleeping || state == kWaiting) { 931aef701da38d14bd479cb275f78e2adaa00f1c54aElliott Hughes return false; 932aef701da38d14bd479cb275f78e2adaa00f1c54aElliott Hughes } 933aef701da38d14bd479cb275f78e2adaa00f1c54aElliott Hughes 934aef701da38d14bd479cb275f78e2adaa00f1c54aElliott Hughes // In some other native method? That's interesting. 935aef701da38d14bd479cb275f78e2adaa00f1c54aElliott Hughes // We don't just check kNative because native methods will be in state kSuspended if they're 936aef701da38d14bd479cb275f78e2adaa00f1c54aElliott Hughes // calling back into the VM, or kBlocked if they're blocked on a monitor, or one of the 937aef701da38d14bd479cb275f78e2adaa00f1c54aElliott Hughes // thread-startup states if it's early enough in their life cycle (http://b/7432159). 9380aded089f565008ba5908e395e5914ca4f91f2deDave Allison mirror::ArtMethod* current_method = thread->GetCurrentMethod(nullptr); 9390aded089f565008ba5908e395e5914ca4f91f2deDave Allison return current_method != nullptr && current_method->IsNative(); 940aef701da38d14bd479cb275f78e2adaa00f1c54aElliott Hughes} 941aef701da38d14bd479cb275f78e2adaa00f1c54aElliott Hughes 942c751fdcc9491c1b60c3db517fbc41bb98e92441fMathieu Chartiervoid Thread::DumpJavaStack(std::ostream& os) const { 943700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers std::unique_ptr<Context> context(Context::Create()); 944c751fdcc9491c1b60c3db517fbc41bb98e92441fMathieu Chartier StackDumpVisitor dumper(os, const_cast<Thread*>(this), context.get(), 945c751fdcc9491c1b60c3db517fbc41bb98e92441fMathieu Chartier !tls32_.throwing_OutOfMemoryError); 946c751fdcc9491c1b60c3db517fbc41bb98e92441fMathieu Chartier dumper.WalkStack(); 947c751fdcc9491c1b60c3db517fbc41bb98e92441fMathieu Chartier} 948c751fdcc9491c1b60c3db517fbc41bb98e92441fMathieu Chartier 949d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughesvoid Thread::DumpStack(std::ostream& os) const { 950ed1790e83352e54420018d1495eb010f7cd48a64Jeff Hao // TODO: we call this code when dying but may not have suspended the thread ourself. The 951ed1790e83352e54420018d1495eb010f7cd48a64Jeff Hao // IsSuspended check is therefore racy with the use for dumping (normally we inhibit 952ed1790e83352e54420018d1495eb010f7cd48a64Jeff Hao // the race with the thread_suspend_count_lock_). 953eef2e54113ba10cd76ddc368b928a2864401700eIan Rogers bool dump_for_abort = (gAborting > 0); 954eef2e54113ba10cd76ddc368b928a2864401700eIan Rogers bool safe_to_dump = (this == Thread::Current() || IsSuspended()); 955eef2e54113ba10cd76ddc368b928a2864401700eIan Rogers if (!kIsDebugBuild) { 956eef2e54113ba10cd76ddc368b928a2864401700eIan Rogers // We always want to dump the stack for an abort, however, there is no point dumping another 957eef2e54113ba10cd76ddc368b928a2864401700eIan Rogers // thread's stack in debug builds where we'll hit the not suspended check in the stack walk. 958eef2e54113ba10cd76ddc368b928a2864401700eIan Rogers safe_to_dump = (safe_to_dump || dump_for_abort); 959eef2e54113ba10cd76ddc368b928a2864401700eIan Rogers } 960eef2e54113ba10cd76ddc368b928a2864401700eIan Rogers if (safe_to_dump) { 961f08e473519dc5c7ccb85eb2b333f9d0aff23a329Ian Rogers // If we're currently in native code, dump that stack before dumping the managed stack. 962d090cf4f2420a228251e27dbae600f4f44ae2c57Ian Rogers if (dump_for_abort || ShouldShowNativeStack(this)) { 963f08e473519dc5c7ccb85eb2b333f9d0aff23a329Ian Rogers DumpKernelStack(os, GetTid(), " kernel: ", false); 964eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier DumpNativeStack(os, GetTid(), " native: ", GetCurrentMethod(nullptr)); 965f08e473519dc5c7ccb85eb2b333f9d0aff23a329Ian Rogers } 966c751fdcc9491c1b60c3db517fbc41bb98e92441fMathieu Chartier DumpJavaStack(os); 967f08e473519dc5c7ccb85eb2b333f9d0aff23a329Ian Rogers } else { 968f08e473519dc5c7ccb85eb2b333f9d0aff23a329Ian Rogers os << "Not able to dump stack of thread that isn't suspended"; 969f08e473519dc5c7ccb85eb2b333f9d0aff23a329Ian Rogers } 970e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes} 971e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes 972be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughesvoid Thread::ThreadExitCallback(void* arg) { 973be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes Thread* self = reinterpret_cast<Thread*>(arg); 974dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers if (self->tls32_.thread_exit_check_count == 0) { 975dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers LOG(WARNING) << "Native thread exiting without having called DetachCurrentThread (maybe it's " 976dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers "going to use a pthread_key_create destructor?): " << *self; 9770878d654e7be8c9666579e22522704d8887415ccIan Rogers CHECK(is_started_); 9786a607ad0902f3b8478e95d0b6b3e63a538571a3fElliott Hughes CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, self), "reattach self"); 979dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers self->tls32_.thread_exit_check_count = 1; 9806a607ad0902f3b8478e95d0b6b3e63a538571a3fElliott Hughes } else { 9816a607ad0902f3b8478e95d0b6b3e63a538571a3fElliott Hughes LOG(FATAL) << "Native thread exited without calling DetachCurrentThread: " << *self; 9826a607ad0902f3b8478e95d0b6b3e63a538571a3fElliott Hughes } 983b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro} 984b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 985be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughesvoid Thread::Startup() { 9860878d654e7be8c9666579e22522704d8887415ccIan Rogers CHECK(!is_started_); 9870878d654e7be8c9666579e22522704d8887415ccIan Rogers is_started_ = true; 98800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers { 9896d69f592696f3bbd3bb0978ae3b25f2c359aac0dBrian Carlstrom // MutexLock to keep annotalysis happy. 9906d69f592696f3bbd3bb0978ae3b25f2c359aac0dBrian Carlstrom // 9910aded089f565008ba5908e395e5914ca4f91f2deDave Allison // Note we use nullptr for the thread because Thread::Current can 9926d69f592696f3bbd3bb0978ae3b25f2c359aac0dBrian Carlstrom // return garbage since (is_started_ == true) and 9936d69f592696f3bbd3bb0978ae3b25f2c359aac0dBrian Carlstrom // Thread::pthread_key_self_ is not yet initialized. 9946d69f592696f3bbd3bb0978ae3b25f2c359aac0dBrian Carlstrom // This was seen on glibc. 9950aded089f565008ba5908e395e5914ca4f91f2deDave Allison MutexLock mu(nullptr, *Locks::thread_suspend_count_lock_); 996c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers resume_cond_ = new ConditionVariable("Thread resumption condition variable", 997c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers *Locks::thread_suspend_count_lock_); 99800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 99900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 1000b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro // Allocate a TLS slot. 10018d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes CHECK_PTHREAD_CALL(pthread_key_create, (&Thread::pthread_key_self_, Thread::ThreadExitCallback), "self key"); 1002b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 1003b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro // Double-check the TLS slot allocation. 10040aded089f565008ba5908e395e5914ca4f91f2deDave Allison if (pthread_getspecific(pthread_key_self_) != nullptr) { 10050aded089f565008ba5908e395e5914ca4f91f2deDave Allison LOG(FATAL) << "Newly-created pthread TLS slot is not nullptr"; 1006b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro } 1007038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes} 1008038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes 1009038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesvoid Thread::FinishStartup() { 1010365c10235438607541fa2259a5fec48061b90bd8Ian Rogers Runtime* runtime = Runtime::Current(); 1011365c10235438607541fa2259a5fec48061b90bd8Ian Rogers CHECK(runtime->IsStarted()); 1012b82b6878fb000d4731063b1bf15c39ff7c50b61fBrian Carlstrom 101301158d7a57c8321370667a6045220237d16e0da8Elliott Hughes // Finish attaching the main thread. 101400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ScopedObjectAccess soa(Thread::Current()); 1015365c10235438607541fa2259a5fec48061b90bd8Ian Rogers Thread::Current()->CreatePeer("main", false, runtime->GetMainThreadGroup()); 10169a6bae896a2f003d7216603bf29771d105c10ca4Jesse Wilson 1017af8d15a3267343dec135cc6df1db740c0a5c7b52Elliott Hughes Runtime::Current()->GetClassLinker()->RunRootClinits(); 1018b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro} 1019b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 1020c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughesvoid Thread::Shutdown() { 10210878d654e7be8c9666579e22522704d8887415ccIan Rogers CHECK(is_started_); 10220878d654e7be8c9666579e22522704d8887415ccIan Rogers is_started_ = false; 10238d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes CHECK_PTHREAD_CALL(pthread_key_delete, (Thread::pthread_key_self_), "self key"); 102433e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers MutexLock mu(Thread::Current(), *Locks::thread_suspend_count_lock_); 10250aded089f565008ba5908e395e5914ca4f91f2deDave Allison if (resume_cond_ != nullptr) { 10260878d654e7be8c9666579e22522704d8887415ccIan Rogers delete resume_cond_; 10270aded089f565008ba5908e395e5914ca4f91f2deDave Allison resume_cond_ = nullptr; 10280878d654e7be8c9666579e22522704d8887415ccIan Rogers } 1029c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes} 1030c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes 1031dd7624d2b9e599d57762d12031b10b89defc9807Ian RogersThread::Thread(bool daemon) : tls32_(daemon), wait_monitor_(nullptr), interrupted_(false) { 1032dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers wait_mutex_ = new Mutex("a thread wait mutex"); 1033dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers wait_cond_ = new ConditionVariable("a thread wait condition variable", *wait_mutex_); 1034dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tlsPtr_.debug_invoke_req = new DebugInvokeReq; 1035dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tlsPtr_.single_step_control = new SingleStepControl; 1036dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tlsPtr_.instrumentation_stack = new std::deque<instrumentation::InstrumentationStackFrame>; 1037dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tlsPtr_.name = new std::string(kThreadNameDuringStartup); 10388ce6b9040747054b444a7fa706503cd257801936Dave Allison tlsPtr_.nested_signal_state = static_cast<jmp_buf*>(malloc(sizeof(jmp_buf))); 1039dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers 1040f5a7a476e7ea63e094ff0f011dccc170607e6f6bElliott Hughes CHECK_EQ((sizeof(Thread) % 4), 0U) << sizeof(Thread); 1041dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tls32_.state_and_flags.as_struct.flags = 0; 1042dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tls32_.state_and_flags.as_struct.state = kNative; 1043dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers memset(&tlsPtr_.held_mutexes[0], 0, sizeof(tlsPtr_.held_mutexes)); 10440651d41e41341fb2e9ef3ee41dc1f1bfc832dbbbMathieu Chartier std::fill(tlsPtr_.rosalloc_runs, 1045e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers tlsPtr_.rosalloc_runs + kNumRosAllocThreadLocalSizeBrackets, 104673d1e17b3afc7d5e56184f90bf819dc64956448aMathieu Chartier gc::allocator::RosAlloc::GetDedicatedFullRun()); 10470aded089f565008ba5908e395e5914ca4f91f2deDave Allison for (uint32_t i = 0; i < kMaxCheckpoints; ++i) { 1048dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tlsPtr_.checkpoint_functions[i] = nullptr; 10490aded089f565008ba5908e395e5914ca4f91f2deDave Allison } 1050dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes} 1051dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes 10527dc5166ea740359d381097a7ab382c1dd404055fElliott Hughesbool Thread::IsStillStarting() const { 10537dc5166ea740359d381097a7ab382c1dd404055fElliott Hughes // You might think you can check whether the state is kStarting, but for much of thread startup, 105480537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes // the thread is in kNative; it might also be in kVmWait. 10550aded089f565008ba5908e395e5914ca4f91f2deDave Allison // You might think you can check whether the peer is nullptr, but the peer is actually created and 10567dc5166ea740359d381097a7ab382c1dd404055fElliott Hughes // assigned fairly early on, and needs to be. 10577dc5166ea740359d381097a7ab382c1dd404055fElliott Hughes // It turns out that the last thing to change is the thread name; that's a good proxy for "has 10587dc5166ea740359d381097a7ab382c1dd404055fElliott Hughes // this thread _ever_ entered kRunnable". 1059dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers return (tlsPtr_.jpeer == nullptr && tlsPtr_.opeer == nullptr) || 1060dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers (*tlsPtr_.name == kThreadNameDuringStartup); 10617dc5166ea740359d381097a7ab382c1dd404055fElliott Hughes} 10627dc5166ea740359d381097a7ab382c1dd404055fElliott Hughes 1063d9efea646485fe996c967f4680a3fc4cdc40b251Andreas Gampevoid Thread::AssertPendingException() const { 1064d9efea646485fe996c967f4680a3fc4cdc40b251Andreas Gampe if (UNLIKELY(!IsExceptionPending())) { 1065d9efea646485fe996c967f4680a3fc4cdc40b251Andreas Gampe LOG(FATAL) << "Pending exception expected."; 1066d9efea646485fe996c967f4680a3fc4cdc40b251Andreas Gampe } 1067d9efea646485fe996c967f4680a3fc4cdc40b251Andreas Gampe} 1068d9efea646485fe996c967f4680a3fc4cdc40b251Andreas Gampe 106900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersvoid Thread::AssertNoPendingException() const { 107000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers if (UNLIKELY(IsExceptionPending())) { 107100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ScopedObjectAccess soa(Thread::Current()); 10720aded089f565008ba5908e395e5914ca4f91f2deDave Allison mirror::Throwable* exception = GetException(nullptr); 107300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers LOG(FATAL) << "No pending exception expected: " << exception->Dump(); 107400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 107500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers} 107600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 10778d7672e476a526aa6205a008b5a06c8216342ac6Mathieu Chartiervoid Thread::AssertNoPendingExceptionForNewException(const char* msg) const { 10788d7672e476a526aa6205a008b5a06c8216342ac6Mathieu Chartier if (UNLIKELY(IsExceptionPending())) { 10798d7672e476a526aa6205a008b5a06c8216342ac6Mathieu Chartier ScopedObjectAccess soa(Thread::Current()); 10808d7672e476a526aa6205a008b5a06c8216342ac6Mathieu Chartier mirror::Throwable* exception = GetException(nullptr); 1081c114b5fbc91e6d19ef430d9bc3468386ca61b324Ian Rogers LOG(FATAL) << "Throwing new exception '" << msg << "' with unexpected pending exception: " 10828d7672e476a526aa6205a008b5a06c8216342ac6Mathieu Chartier << exception->Dump(); 10838d7672e476a526aa6205a008b5a06c8216342ac6Mathieu Chartier } 10848d7672e476a526aa6205a008b5a06c8216342ac6Mathieu Chartier} 10858d7672e476a526aa6205a008b5a06c8216342ac6Mathieu Chartier 1086815873ecc312b1d231acce71e1a16f42cdaf09f2Mathieu Chartierstatic void MonitorExitVisitor(mirror::Object** object, void* arg, uint32_t /*thread_id*/, 1087815873ecc312b1d231acce71e1a16f42cdaf09f2Mathieu Chartier RootType /*root_type*/) 1088423d2a3dcbb260b020efb5da59f784c9f02accbfMathieu Chartier NO_THREAD_SAFETY_ANALYSIS { 108900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Thread* self = reinterpret_cast<Thread*>(arg); 1090815873ecc312b1d231acce71e1a16f42cdaf09f2Mathieu Chartier mirror::Object* entered_monitor = *object; 109100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers if (self->HoldsLock(entered_monitor)) { 109200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers LOG(WARNING) << "Calling MonitorExit on object " 1093815873ecc312b1d231acce71e1a16f42cdaf09f2Mathieu Chartier << object << " (" << PrettyTypeOf(entered_monitor) << ")" 109400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers << " left locked by native thread " 109500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers << *Thread::Current() << " which is detaching"; 109600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers entered_monitor->MonitorExit(self); 109700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 109802b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes} 109902b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 1100c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughesvoid Thread::Destroy() { 1101120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers Thread* self = this; 1102120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers DCHECK_EQ(self, Thread::Current()); 110302b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 110468d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers if (tlsPtr_.jni_env != nullptr) { 110568d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers // On thread detach, all monitors entered with JNI MonitorEnter are automatically exited. 110668d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers tlsPtr_.jni_env->monitors.VisitRoots(MonitorExitVisitor, self, 0, kRootVMInternal); 110768d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers // Release locally held global references which releasing may require the mutator lock. 110868d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers if (tlsPtr_.jpeer != nullptr) { 110968d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers // If pthread_create fails we don't have a jni env here. 111068d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers tlsPtr_.jni_env->DeleteGlobalRef(tlsPtr_.jpeer); 111168d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers tlsPtr_.jpeer = nullptr; 111268d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers } 111368d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers if (tlsPtr_.class_loader_override != nullptr) { 111468d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers tlsPtr_.jni_env->DeleteGlobalRef(tlsPtr_.class_loader_override); 111568d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers tlsPtr_.class_loader_override = nullptr; 111668d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers } 111768d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers } 111868d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers 1119dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers if (tlsPtr_.opeer != nullptr) { 1120cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers ScopedObjectAccess soa(self); 1121120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers // We may need to call user-supplied managed code, do this before final clean-up. 1122cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers HandleUncaughtExceptions(soa); 1123cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers RemoveFromThreadGroup(soa); 1124534da072d3db3beecbd74e5aa23d630d73695f2cElliott Hughes 11253c50a4b4ba6d7d9369ee9a0bd6d30bf4c9c79bb0Anwar Ghuloum // this.nativePeer = 0; 1126d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz if (Runtime::Current()->IsActiveTransaction()) { 1127dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers soa.DecodeField(WellKnownClasses::java_lang_Thread_nativePeer) 1128dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers ->SetLong<true>(tlsPtr_.opeer, 0); 1129d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz } else { 1130dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers soa.DecodeField(WellKnownClasses::java_lang_Thread_nativePeer) 1131dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers ->SetLong<false>(tlsPtr_.opeer, 0); 1132d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz } 1133cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers Dbg::PostThreadDeath(self); 113402b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 1135cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers // Thread.join() is implemented as an Object.wait() on the Thread.lock object. Signal anyone 1136cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers // who is waiting. 11372dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::Object* lock = 1138dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers soa.DecodeField(WellKnownClasses::java_lang_Thread_lock)->GetObject(tlsPtr_.opeer); 1139038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes // (This conditional is only needed for tests, where Thread.lock won't have been set.) 1140c528dba35b5faece51ca658fc008b688f8b690adMathieu Chartier if (lock != nullptr) { 1141eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier StackHandleScope<1> hs(self); 1142eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier Handle<mirror::Object> h_obj(hs.NewHandle(lock)); 1143db2633ce0358c704f97130a94b582602cb01d14aMathieu Chartier ObjectLock<mirror::Object> locker(self, h_obj); 114463e8a68c9a2cdbbe9bfd8d200610119dd359ee59Yevgeny Rouban locker.NotifyAll(); 11455f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 114668d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers tlsPtr_.opeer = nullptr; 11475f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 1148120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers 114968d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers Runtime::Current()->GetHeap()->RevokeThreadLocalBuffers(this); 1150c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes} 115102b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 1152c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott HughesThread::~Thread() { 115368d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers CHECK(tlsPtr_.class_loader_override == nullptr); 115468d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers CHECK(tlsPtr_.jpeer == nullptr); 115568d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers CHECK(tlsPtr_.opeer == nullptr); 1156dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers bool initialized = (tlsPtr_.jni_env != nullptr); // Did Thread::Init run? 11571efa0a9d6cd5f5b40b8a21d39f1103a3610250eeAlexei Zavjalov if (initialized) { 1158dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers delete tlsPtr_.jni_env; 1159dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tlsPtr_.jni_env = nullptr; 11601efa0a9d6cd5f5b40b8a21d39f1103a3610250eeAlexei Zavjalov } 1161dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier CHECK_NE(GetState(), kRunnable); 11620aded089f565008ba5908e395e5914ca4f91f2deDave Allison CHECK_NE(ReadFlag(kCheckpointRequest), true); 1163dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers CHECK(tlsPtr_.checkpoint_functions[0] == nullptr); 1164dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers CHECK(tlsPtr_.checkpoint_functions[1] == nullptr); 1165dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers CHECK(tlsPtr_.checkpoint_functions[2] == nullptr); 11660aded089f565008ba5908e395e5914ca4f91f2deDave Allison 1167dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier // We may be deleting a still born thread. 1168dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier SetStateUnsafe(kTerminated); 116985d1545e985ac689db4bad7849880e843707c862Elliott Hughes 117085d1545e985ac689db4bad7849880e843707c862Elliott Hughes delete wait_cond_; 117185d1545e985ac689db4bad7849880e843707c862Elliott Hughes delete wait_mutex_; 117285d1545e985ac689db4bad7849880e843707c862Elliott Hughes 1173dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers if (tlsPtr_.long_jump_context != nullptr) { 1174dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers delete tlsPtr_.long_jump_context; 1175c928de90ad22ecdf83c18a07008409595f13d3b1Ian Rogers } 1176475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes 11771efa0a9d6cd5f5b40b8a21d39f1103a3610250eeAlexei Zavjalov if (initialized) { 11781efa0a9d6cd5f5b40b8a21d39f1103a3610250eeAlexei Zavjalov CleanupCpu(); 11791efa0a9d6cd5f5b40b8a21d39f1103a3610250eeAlexei Zavjalov } 11801efa0a9d6cd5f5b40b8a21d39f1103a3610250eeAlexei Zavjalov 1181dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers delete tlsPtr_.debug_invoke_req; 1182dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers delete tlsPtr_.single_step_control; 1183dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers delete tlsPtr_.instrumentation_stack; 1184dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers delete tlsPtr_.name; 1185dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers delete tlsPtr_.stack_trace_sample; 11868ce6b9040747054b444a7fa706503cd257801936Dave Allison free(tlsPtr_.nested_signal_state); 1187d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes 118868d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers Runtime::Current()->GetHeap()->AssertThreadLocalBuffersAreRevoked(this); 1189cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi 1190d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes TearDownAlternateSignalStack(); 1191c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes} 1192c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes 1193cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogersvoid Thread::HandleUncaughtExceptions(ScopedObjectAccess& soa) { 1194accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes if (!IsExceptionPending()) { 1195accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes return; 1196accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes } 1197dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers ScopedLocalRef<jobject> peer(tlsPtr_.jni_env, soa.AddLocalReference<jobject>(tlsPtr_.opeer)); 1198cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers ScopedThreadStateChange tsc(this, kNative); 1199120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers 1200accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes // Get and clear the exception. 1201dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers ScopedLocalRef<jthrowable> exception(tlsPtr_.jni_env, tlsPtr_.jni_env->ExceptionOccurred()); 1202dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tlsPtr_.jni_env->ExceptionClear(); 1203accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 1204accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes // If the thread has its own handler, use that. 1205dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers ScopedLocalRef<jobject> handler(tlsPtr_.jni_env, 1206dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tlsPtr_.jni_env->GetObjectField(peer.get(), 1207dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers WellKnownClasses::java_lang_Thread_uncaughtHandler)); 12080aded089f565008ba5908e395e5914ca4f91f2deDave Allison if (handler.get() == nullptr) { 1209accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes // Otherwise use the thread group's default handler. 1210dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers handler.reset(tlsPtr_.jni_env->GetObjectField(peer.get(), 1211dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers WellKnownClasses::java_lang_Thread_group)); 1212accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes } 1213accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 1214accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes // Call the handler. 1215dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tlsPtr_.jni_env->CallVoidMethod(handler.get(), 1216dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers WellKnownClasses::java_lang_Thread$UncaughtExceptionHandler_uncaughtException, 1217dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers peer.get(), exception.get()); 1218accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 1219accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes // If the handler threw, clear that exception too. 1220dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tlsPtr_.jni_env->ExceptionClear(); 1221a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes} 1222a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes 1223cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogersvoid Thread::RemoveFromThreadGroup(ScopedObjectAccess& soa) { 12244514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom // this.group.removeThread(this); 12254514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom // group can be null if we're in the compiler or a test. 1226dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers mirror::Object* ogroup = soa.DecodeField(WellKnownClasses::java_lang_Thread_group) 1227dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers ->GetObject(tlsPtr_.opeer); 12280aded089f565008ba5908e395e5914ca4f91f2deDave Allison if (ogroup != nullptr) { 1229cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers ScopedLocalRef<jobject> group(soa.Env(), soa.AddLocalReference<jobject>(ogroup)); 1230dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers ScopedLocalRef<jobject> peer(soa.Env(), soa.AddLocalReference<jobject>(tlsPtr_.opeer)); 1231cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers ScopedThreadStateChange tsc(soa.Self(), kNative); 1232dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tlsPtr_.jni_env->CallVoidMethod(group.get(), 1233dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers WellKnownClasses::java_lang_ThreadGroup_removeThread, 1234dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers peer.get()); 12354514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom } 12364514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom} 1237accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 1238eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartiersize_t Thread::NumHandleReferences() { 1239a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers size_t count = 0; 1240c0542af3e2170143ba40d89136e284997e16bf64Ian Rogers for (HandleScope* cur = tlsPtr_.top_handle_scope; cur != nullptr; cur = cur->GetLink()) { 1241a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers count += cur->NumberOfReferences(); 1242a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers } 1243a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers return count; 1244a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers} 1245a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers 1246eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartierbool Thread::HandleScopeContains(jobject obj) const { 1247eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier StackReference<mirror::Object>* hs_entry = 1248ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers reinterpret_cast<StackReference<mirror::Object>*>(obj); 1249c0542af3e2170143ba40d89136e284997e16bf64Ian Rogers for (HandleScope* cur = tlsPtr_.top_handle_scope; cur!= nullptr; cur = cur->GetLink()) { 1250eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier if (cur->Contains(hs_entry)) { 1251a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers return true; 1252a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers } 1253a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers } 1254eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier // JNI code invoked from portable code uses shadow frames rather than the handle scope. 1255eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier return tlsPtr_.managed_stack.ShadowFramesContain(hs_entry); 125628f1a147d77ec772db98bed890b50a9ddcff2365TDYa} 125728f1a147d77ec772db98bed890b50a9ddcff2365TDYa 1258eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartiervoid Thread::HandleScopeVisitRoots(RootCallback* visitor, void* arg, uint32_t thread_id) { 1259eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier for (HandleScope* cur = tlsPtr_.top_handle_scope; cur; cur = cur->GetLink()) { 12608dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao size_t num_refs = cur->NumberOfReferences(); 12610aded089f565008ba5908e395e5914ca4f91f2deDave Allison for (size_t j = 0; j < num_refs; ++j) { 12622dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::Object* object = cur->GetReference(j); 1263423d2a3dcbb260b020efb5da59f784c9f02accbfMathieu Chartier if (object != nullptr) { 1264815873ecc312b1d231acce71e1a16f42cdaf09f2Mathieu Chartier mirror::Object* old_obj = object; 1265815873ecc312b1d231acce71e1a16f42cdaf09f2Mathieu Chartier visitor(&object, arg, thread_id, kRootNativeStack); 1266815873ecc312b1d231acce71e1a16f42cdaf09f2Mathieu Chartier if (old_obj != object) { 1267815873ecc312b1d231acce71e1a16f42cdaf09f2Mathieu Chartier cur->SetReference(j, object); 1268423d2a3dcbb260b020efb5da59f784c9f02accbfMathieu Chartier } 12695e73f9c8990819054f8e0a60061ee8beff1e024aBrian Carlstrom } 12708dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao } 12718dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao } 12728dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao} 12738dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao 12742dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersmirror::Object* Thread::DecodeJObject(jobject obj) const { 127581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers Locks::mutator_lock_->AssertSharedHeld(this); 12760aded089f565008ba5908e395e5914ca4f91f2deDave Allison if (obj == nullptr) { 12770aded089f565008ba5908e395e5914ca4f91f2deDave Allison return nullptr; 1278408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 1279408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers IndirectRef ref = reinterpret_cast<IndirectRef>(obj); 1280408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers IndirectRefKind kind = GetIndirectRefKind(ref); 12812dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::Object* result; 1282c0542af3e2170143ba40d89136e284997e16bf64Ian Rogers bool expect_null = false; 12834f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers // The "kinds" below are sorted by the frequency we expect to encounter them. 12844f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers if (kind == kLocal) { 1285dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers IndirectReferenceTable& locals = tlsPtr_.jni_env->locals; 1286196851b634a5bfdd8ab3fb59a320e550b21b0f4dHiroshi Yamauchi // Local references do not need a read barrier. 1287196851b634a5bfdd8ab3fb59a320e550b21b0f4dHiroshi Yamauchi result = locals.Get<kWithoutReadBarrier>(ref); 1288eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier } else if (kind == kHandleScopeOrInvalid) { 1289ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers // TODO: make stack indirect reference table lookup more efficient. 1290eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier // Check if this is a local reference in the handle scope. 1291eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier if (LIKELY(HandleScopeContains(obj))) { 1292eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier // Read from handle scope. 1293ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers result = reinterpret_cast<StackReference<mirror::Object>*>(obj)->AsMirrorPtr(); 1294c645f1ddb7c40bea6a38eda4b3f83f6b6dec405bMathieu Chartier VerifyObject(result); 1295408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } else { 1296c0542af3e2170143ba40d89136e284997e16bf64Ian Rogers tlsPtr_.jni_env->vm->JniAbortF(nullptr, "use of invalid jobject %p", obj); 1297c0542af3e2170143ba40d89136e284997e16bf64Ian Rogers expect_null = true; 1298c0542af3e2170143ba40d89136e284997e16bf64Ian Rogers result = nullptr; 1299408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 13004f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers } else if (kind == kGlobal) { 130168d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers result = tlsPtr_.jni_env->vm->DecodeGlobal(const_cast<Thread*>(this), ref); 13024f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers } else { 13034f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers DCHECK_EQ(kind, kWeakGlobal); 130468d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers result = tlsPtr_.jni_env->vm->DecodeWeakGlobal(const_cast<Thread*>(this), ref); 1305c0542af3e2170143ba40d89136e284997e16bf64Ian Rogers if (Runtime::Current()->IsClearedJniWeakGlobal(result)) { 13060aded089f565008ba5908e395e5914ca4f91f2deDave Allison // This is a special case where it's okay to return nullptr. 1307c0542af3e2170143ba40d89136e284997e16bf64Ian Rogers expect_null = true; 1308c0542af3e2170143ba40d89136e284997e16bf64Ian Rogers result = nullptr; 13094f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers } 1310408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 1311408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers 1312c0542af3e2170143ba40d89136e284997e16bf64Ian Rogers if (UNLIKELY(!expect_null && result == nullptr)) { 131368d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers tlsPtr_.jni_env->vm->JniAbortF(nullptr, "use of deleted %s %p", 131468d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers ToStr<IndirectRefKind>(kind).c_str(), obj); 1315408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 1316408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers return result; 1317408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers} 1318408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers 131981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers// Implements java.lang.Thread.interrupted. 132081d425b0b232962441616f8b14f73620bffef5e5Ian Rogersbool Thread::Interrupted() { 132150b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers MutexLock mu(Thread::Current(), *wait_mutex_); 1322dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers bool interrupted = IsInterruptedLocked(); 1323dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers SetInterruptedLocked(false); 132481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers return interrupted; 132581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers} 132681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers 132781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers// Implements java.lang.Thread.isInterrupted. 132881d425b0b232962441616f8b14f73620bffef5e5Ian Rogersbool Thread::IsInterrupted() { 132950b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers MutexLock mu(Thread::Current(), *wait_mutex_); 1330dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers return IsInterruptedLocked(); 133181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers} 133281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers 1333dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogersvoid Thread::Interrupt(Thread* self) { 1334c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers MutexLock mu(self, *wait_mutex_); 133581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers if (interrupted_) { 133681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers return; 133781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers } 133881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers interrupted_ = true; 1339c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers NotifyLocked(self); 134081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers} 134181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers 134281d425b0b232962441616f8b14f73620bffef5e5Ian Rogersvoid Thread::Notify() { 1343c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers Thread* self = Thread::Current(); 1344c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers MutexLock mu(self, *wait_mutex_); 1345c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers NotifyLocked(self); 134681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers} 134781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers 1348c604d731730b43231f63040c8db1d58304da0cf3Ian Rogersvoid Thread::NotifyLocked(Thread* self) { 13490aded089f565008ba5908e395e5914ca4f91f2deDave Allison if (wait_monitor_ != nullptr) { 1350c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers wait_cond_->Signal(self); 135181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers } 135281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers} 135381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers 135468d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogersvoid Thread::SetClassLoaderOverride(jobject class_loader_override) { 135568d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers if (tlsPtr_.class_loader_override != nullptr) { 135668d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers GetJniEnv()->DeleteGlobalRef(tlsPtr_.class_loader_override); 135768d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers } 135868d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers tlsPtr_.class_loader_override = GetJniEnv()->NewGlobalRef(class_loader_override); 135968d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers} 136068d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers 13610399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogersclass CountStackDepthVisitor : public StackVisitor { 13629b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao public: 136393ba893c20532990a430741e0a97212900094e8cBrian Carlstrom explicit CountStackDepthVisitor(Thread* thread) 1364b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) 13650aded089f565008ba5908e395e5914ca4f91f2deDave Allison : StackVisitor(thread, nullptr), 136608fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes depth_(0), skip_depth_(0), skipping_(true) {} 1367d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 1368b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 136929f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // We want to skip frames up to and including the exception's constructor. 13709086572fa809d1a19d886b467e4da3ce42016982Ian Rogers // Note we also skip the frame if it doesn't have a method (namely the callee 13719086572fa809d1a19d886b467e4da3ce42016982Ian Rogers // save frame) 1372ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom mirror::ArtMethod* m = GetMethod(); 13730399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers if (skipping_ && !m->IsRuntimeMethod() && 13742dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers !mirror::Throwable::GetJavaLangThrowable()->IsAssignableFrom(m->GetDeclaringClass())) { 137529f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes skipping_ = false; 137629f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } 137729f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes if (!skipping_) { 13780399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers if (!m->IsRuntimeMethod()) { // Ignore runtime frames (in particular callee save). 13796b0870dfaaa2e69a900ad7ed88e4e1d3697445aaIan Rogers ++depth_; 13806b0870dfaaa2e69a900ad7ed88e4e1d3697445aaIan Rogers } 138129f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } else { 138229f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes ++skip_depth_; 138329f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } 1384530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes return true; 13859b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 13869b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 13879b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao int GetDepth() const { 1388aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers return depth_; 13899b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 13909b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 139129f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes int GetSkipDepth() const { 139229f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes return skip_depth_; 139329f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } 139429f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes 13959b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao private: 1396aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers uint32_t depth_; 139729f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes uint32_t skip_depth_; 139829f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes bool skipping_; 13999b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao}; 14009b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 1401ee1d79a603c77c0667b27c075a983579d5c51f7eSebastien Hertztemplate<bool kTransactionActive> 14020399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogersclass BuildInternalStackTraceVisitor : public StackVisitor { 14039b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao public: 14047a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers explicit BuildInternalStackTraceVisitor(Thread* self, Thread* thread, int skip_depth) 14050aded089f565008ba5908e395e5914ca4f91f2deDave Allison : StackVisitor(thread, nullptr), self_(self), 14060aded089f565008ba5908e395e5914ca4f91f2deDave Allison skip_depth_(skip_depth), count_(0), dex_pc_trace_(nullptr), method_trace_(nullptr) {} 1407283ed0d9db3084d0fee46d47cd73bf84bd0cfc16Ian Rogers 14081f5393447b9f45be7918042d9ee7b521376de866Ian Rogers bool Init(int depth) 1409b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1410aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Allocate method trace with an extra slot that will hold the PC trace 1411eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier StackHandleScope<1> hs(self_); 1412eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 1413eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier Handle<mirror::ObjectArray<mirror::Object>> method_trace( 1414eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier hs.NewHandle(class_linker->AllocObjectArray<mirror::Object>(self_, depth + 1))); 1415eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier if (method_trace.Get() == nullptr) { 1416283ed0d9db3084d0fee46d47cd73bf84bd0cfc16Ian Rogers return false; 1417726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes } 14182dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::IntArray* dex_pc_trace = mirror::IntArray::Alloc(self_, depth); 14190aded089f565008ba5908e395e5914ca4f91f2deDave Allison if (dex_pc_trace == nullptr) { 1420283ed0d9db3084d0fee46d47cd73bf84bd0cfc16Ian Rogers return false; 1421726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes } 1422aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Save PC trace in last element of method trace, also places it into the 1423aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // object graph. 1424d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz // We are called from native: use non-transactional mode. 1425ee1d79a603c77c0667b27c075a983579d5c51f7eSebastien Hertz method_trace->Set<kTransactionActive>(depth, dex_pc_trace); 14260399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers // Set the Object*s and assert that no thread suspension is now possible. 142752673ffae0025d86f4023735581f19ebcc477487Ian Rogers const char* last_no_suspend_cause = 14281f5393447b9f45be7918042d9ee7b521376de866Ian Rogers self_->StartAssertNoThreadSuspension("Building internal stack trace"); 14290aded089f565008ba5908e395e5914ca4f91f2deDave Allison CHECK(last_no_suspend_cause == nullptr) << last_no_suspend_cause; 1430eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier method_trace_ = method_trace.Get(); 14310399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers dex_pc_trace_ = dex_pc_trace; 1432283ed0d9db3084d0fee46d47cd73bf84bd0cfc16Ian Rogers return true; 14339b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 14349b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 14350399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers virtual ~BuildInternalStackTraceVisitor() { 14360aded089f565008ba5908e395e5914ca4f91f2deDave Allison if (method_trace_ != nullptr) { 14370aded089f565008ba5908e395e5914ca4f91f2deDave Allison self_->EndAssertNoThreadSuspension(nullptr); 143852673ffae0025d86f4023735581f19ebcc477487Ian Rogers } 14390399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers } 14409b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 1441b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 14420aded089f565008ba5908e395e5914ca4f91f2deDave Allison if (method_trace_ == nullptr || dex_pc_trace_ == nullptr) { 14437934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom return true; // We're probably trying to fillInStackTrace for an OutOfMemoryError. 1444726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes } 144529f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes if (skip_depth_ > 0) { 144629f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes skip_depth_--; 1447530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes return true; 144829f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } 1449ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom mirror::ArtMethod* m = GetMethod(); 14500399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers if (m->IsRuntimeMethod()) { 14510399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers return true; // Ignore runtime frames (in particular callee save). 14526b0870dfaaa2e69a900ad7ed88e4e1d3697445aaIan Rogers } 1453ee1d79a603c77c0667b27c075a983579d5c51f7eSebastien Hertz method_trace_->Set<kTransactionActive>(count_, m); 1454b0fa5dc7769c1e054032f39de0a3f6d6dd06f8cfIan Rogers dex_pc_trace_->Set<kTransactionActive>(count_, 1455b0fa5dc7769c1e054032f39de0a3f6d6dd06f8cfIan Rogers m->IsProxyMethod() ? DexFile::kDexNoIndex : GetDexPc()); 1456aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers ++count_; 1457530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes return true; 14589b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 14599b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 14602dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::ObjectArray<mirror::Object>* GetInternalStackTrace() const { 14610399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers return method_trace_; 14629b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 14639b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 14649b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao private: 14651f5393447b9f45be7918042d9ee7b521376de866Ian Rogers Thread* const self_; 146629f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // How many more frames to skip. 146729f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes int32_t skip_depth_; 14680399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers // Current position down stack trace. 1469aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers uint32_t count_; 14700399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers // Array of dex PC values. 14712dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::IntArray* dex_pc_trace_; 14720399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers // An array of the methods on the stack, the last entry is a reference to the PC trace. 14732dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::ObjectArray<mirror::Object>* method_trace_; 14749b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao}; 14759b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 1476ee1d79a603c77c0667b27c075a983579d5c51f7eSebastien Hertztemplate<bool kTransactionActive> 14772b7c4d196c8abe32f4ca633534917da9de53c359Mathieu Chartierjobject Thread::CreateInternalStackTrace(const ScopedObjectAccessAlreadyRunnable& soa) const { 1478aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Compute depth of stack 14797a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers CountStackDepthVisitor count_visitor(const_cast<Thread*>(this)); 14800399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers count_visitor.WalkStack(); 14819b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao int32_t depth = count_visitor.GetDepth(); 148229f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes int32_t skip_depth = count_visitor.GetSkipDepth(); 14834417536522fd2a9d8215d8672331984769c9520bShih-wei Liao 14841f5393447b9f45be7918042d9ee7b521376de866Ian Rogers // Build internal stack trace. 1485ee1d79a603c77c0667b27c075a983579d5c51f7eSebastien Hertz BuildInternalStackTraceVisitor<kTransactionActive> build_trace_visitor(soa.Self(), 1486ee1d79a603c77c0667b27c075a983579d5c51f7eSebastien Hertz const_cast<Thread*>(this), 1487ee1d79a603c77c0667b27c075a983579d5c51f7eSebastien Hertz skip_depth); 14881f5393447b9f45be7918042d9ee7b521376de866Ian Rogers if (!build_trace_visitor.Init(depth)) { 14890aded089f565008ba5908e395e5914ca4f91f2deDave Allison return nullptr; // Allocation failed. 1490283ed0d9db3084d0fee46d47cd73bf84bd0cfc16Ian Rogers } 14910399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers build_trace_visitor.WalkStack(); 14922dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::ObjectArray<mirror::Object>* trace = build_trace_visitor.GetInternalStackTrace(); 14937a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers if (kIsDebugBuild) { 14947a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers for (int32_t i = 0; i < trace->GetLength(); ++i) { 14950aded089f565008ba5908e395e5914ca4f91f2deDave Allison CHECK(trace->Get(i) != nullptr); 14967a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers } 14977a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers } 14987a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers return soa.AddLocalReference<jobjectArray>(trace); 1499aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers} 15002b7c4d196c8abe32f4ca633534917da9de53c359Mathieu Chartiertemplate jobject Thread::CreateInternalStackTrace<false>( 15012b7c4d196c8abe32f4ca633534917da9de53c359Mathieu Chartier const ScopedObjectAccessAlreadyRunnable& soa) const; 15022b7c4d196c8abe32f4ca633534917da9de53c359Mathieu Chartiertemplate jobject Thread::CreateInternalStackTrace<true>( 15032b7c4d196c8abe32f4ca633534917da9de53c359Mathieu Chartier const ScopedObjectAccessAlreadyRunnable& soa) const; 15042b7c4d196c8abe32f4ca633534917da9de53c359Mathieu Chartier 15052b7c4d196c8abe32f4ca633534917da9de53c359Mathieu ChartierjobjectArray Thread::InternalStackTraceToStackTraceElementArray( 15062b7c4d196c8abe32f4ca633534917da9de53c359Mathieu Chartier const ScopedObjectAccessAlreadyRunnable& soa, jobject internal, jobjectArray output_array, 15072b7c4d196c8abe32f4ca633534917da9de53c359Mathieu Chartier int* stack_depth) { 1508aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Decode the internal stack trace into the depth, method trace and PC trace 1509530825ba92e317822116efffd470577ddfd142d4Mathieu Chartier int32_t depth = soa.Decode<mirror::ObjectArray<mirror::Object>*>(internal)->GetLength() - 1; 1510aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers 1511aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 1512aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers 151301158d7a57c8321370667a6045220237d16e0da8Elliott Hughes jobjectArray result; 1514530825ba92e317822116efffd470577ddfd142d4Mathieu Chartier 15150aded089f565008ba5908e395e5914ca4f91f2deDave Allison if (output_array != nullptr) { 151601158d7a57c8321370667a6045220237d16e0da8Elliott Hughes // Reuse the array we were given. 151701158d7a57c8321370667a6045220237d16e0da8Elliott Hughes result = output_array; 151801158d7a57c8321370667a6045220237d16e0da8Elliott Hughes // ...adjusting the number of frames we'll write to not exceed the array length. 1519530825ba92e317822116efffd470577ddfd142d4Mathieu Chartier const int32_t traces_length = 1520530825ba92e317822116efffd470577ddfd142d4Mathieu Chartier soa.Decode<mirror::ObjectArray<mirror::StackTraceElement>*>(result)->GetLength(); 1521530825ba92e317822116efffd470577ddfd142d4Mathieu Chartier depth = std::min(depth, traces_length); 152201158d7a57c8321370667a6045220237d16e0da8Elliott Hughes } else { 152301158d7a57c8321370667a6045220237d16e0da8Elliott Hughes // Create java_trace array and place in local reference table 1524530825ba92e317822116efffd470577ddfd142d4Mathieu Chartier mirror::ObjectArray<mirror::StackTraceElement>* java_traces = 1525530825ba92e317822116efffd470577ddfd142d4Mathieu Chartier class_linker->AllocStackTraceElementArray(soa.Self(), depth); 15260aded089f565008ba5908e395e5914ca4f91f2deDave Allison if (java_traces == nullptr) { 15270aded089f565008ba5908e395e5914ca4f91f2deDave Allison return nullptr; 152830646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes } 152900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers result = soa.AddLocalReference<jobjectArray>(java_traces); 153001158d7a57c8321370667a6045220237d16e0da8Elliott Hughes } 153101158d7a57c8321370667a6045220237d16e0da8Elliott Hughes 15320aded089f565008ba5908e395e5914ca4f91f2deDave Allison if (stack_depth != nullptr) { 153301158d7a57c8321370667a6045220237d16e0da8Elliott Hughes *stack_depth = depth; 153401158d7a57c8321370667a6045220237d16e0da8Elliott Hughes } 153555df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao 15369b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao for (int32_t i = 0; i < depth; ++i) { 1537530825ba92e317822116efffd470577ddfd142d4Mathieu Chartier mirror::ObjectArray<mirror::Object>* method_trace = 1538530825ba92e317822116efffd470577ddfd142d4Mathieu Chartier soa.Decode<mirror::ObjectArray<mirror::Object>*>(internal); 1539aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Prepare parameters for StackTraceElement(String cls, String method, String file, int line) 1540ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom mirror::ArtMethod* method = down_cast<mirror::ArtMethod*>(method_trace->Get(i)); 1541228d6b8a4f0a21c1e9b2372c3104ce4ee19f65b4Jeff Hao int32_t line_number; 1542eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier StackHandleScope<3> hs(soa.Self()); 1543eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier auto class_name_object(hs.NewHandle<mirror::String>(nullptr)); 1544eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier auto source_name_object(hs.NewHandle<mirror::String>(nullptr)); 1545228d6b8a4f0a21c1e9b2372c3104ce4ee19f65b4Jeff Hao if (method->IsProxyMethod()) { 1546228d6b8a4f0a21c1e9b2372c3104ce4ee19f65b4Jeff Hao line_number = -1; 1547eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier class_name_object.Assign(method->GetDeclaringClass()->GetName()); 1548228d6b8a4f0a21c1e9b2372c3104ce4ee19f65b4Jeff Hao // source_name_object intentionally left null for proxy methods 1549228d6b8a4f0a21c1e9b2372c3104ce4ee19f65b4Jeff Hao } else { 1550228d6b8a4f0a21c1e9b2372c3104ce4ee19f65b4Jeff Hao mirror::IntArray* pc_trace = down_cast<mirror::IntArray*>(method_trace->Get(depth)); 1551228d6b8a4f0a21c1e9b2372c3104ce4ee19f65b4Jeff Hao uint32_t dex_pc = pc_trace->Get(i); 1552bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier line_number = method->GetLineNumFromDexPC(dex_pc); 1553228d6b8a4f0a21c1e9b2372c3104ce4ee19f65b4Jeff Hao // Allocate element, potentially triggering GC 1554228d6b8a4f0a21c1e9b2372c3104ce4ee19f65b4Jeff Hao // TODO: reuse class_name_object via Class::name_? 1555bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier const char* descriptor = method->GetDeclaringClassDescriptor(); 15567c6aca27dd2df58ac3d83a93ec5848e2b7d3159aBrian Carlstrom CHECK(descriptor != nullptr); 1557228d6b8a4f0a21c1e9b2372c3104ce4ee19f65b4Jeff Hao std::string class_name(PrettyDescriptor(descriptor)); 1558eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier class_name_object.Assign(mirror::String::AllocFromModifiedUtf8(soa.Self(), class_name.c_str())); 1559eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier if (class_name_object.Get() == nullptr) { 15607c6aca27dd2df58ac3d83a93ec5848e2b7d3159aBrian Carlstrom return nullptr; 1561228d6b8a4f0a21c1e9b2372c3104ce4ee19f65b4Jeff Hao } 1562bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier const char* source_file = method->GetDeclaringClassSourceFile(); 15637c6aca27dd2df58ac3d83a93ec5848e2b7d3159aBrian Carlstrom if (source_file != nullptr) { 1564eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier source_name_object.Assign(mirror::String::AllocFromModifiedUtf8(soa.Self(), source_file)); 1565eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier if (source_name_object.Get() == nullptr) { 15667c6aca27dd2df58ac3d83a93ec5848e2b7d3159aBrian Carlstrom return nullptr; 15677c6aca27dd2df58ac3d83a93ec5848e2b7d3159aBrian Carlstrom } 1568228d6b8a4f0a21c1e9b2372c3104ce4ee19f65b4Jeff Hao } 156940381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom } 1570bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier const char* method_name = method->GetName(); 15710aded089f565008ba5908e395e5914ca4f91f2deDave Allison CHECK(method_name != nullptr); 1572eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier Handle<mirror::String> method_name_object( 1573eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), method_name))); 1574eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier if (method_name_object.Get() == nullptr) { 15750aded089f565008ba5908e395e5914ca4f91f2deDave Allison return nullptr; 15766d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers } 1577590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier mirror::StackTraceElement* obj = mirror::StackTraceElement::Alloc( 1578590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier soa.Self(), class_name_object, method_name_object, source_name_object, line_number); 15790aded089f565008ba5908e395e5914ca4f91f2deDave Allison if (obj == nullptr) { 15800aded089f565008ba5908e395e5914ca4f91f2deDave Allison return nullptr; 158130646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes } 1582d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz // We are called from native: use non-transactional mode. 1583d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz soa.Decode<mirror::ObjectArray<mirror::StackTraceElement>*>(result)->Set<false>(i, obj); 158455df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao } 1585aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers return result; 158655df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao} 158755df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao 158862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogersvoid Thread::ThrowNewExceptionF(const ThrowLocation& throw_location, 158962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers const char* exception_class_descriptor, const char* fmt, ...) { 1590a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes va_list args; 1591a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes va_start(args, fmt); 159262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers ThrowNewExceptionV(throw_location, exception_class_descriptor, 159362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers fmt, args); 1594a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes va_end(args); 15954a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes} 15964a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes 159762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogersvoid Thread::ThrowNewExceptionV(const ThrowLocation& throw_location, 159862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers const char* exception_class_descriptor, 159962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers const char* fmt, va_list ap) { 16004a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes std::string msg; 16014a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes StringAppendV(&msg, fmt, ap); 160262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers ThrowNewException(throw_location, exception_class_descriptor, msg.c_str()); 16035cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughes} 160437f7a40f6789bb287f287a9af00777af9d6428eeElliott Hughes 16057b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogersvoid Thread::ThrowNewException(const ThrowLocation& throw_location, 16067b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers const char* exception_class_descriptor, 160762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers const char* msg) { 16088d7672e476a526aa6205a008b5a06c8216342ac6Mathieu Chartier // Callers should either clear or call ThrowNewWrappedException. 16098d7672e476a526aa6205a008b5a06c8216342ac6Mathieu Chartier AssertNoPendingExceptionForNewException(msg); 161062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers ThrowNewWrappedException(throw_location, exception_class_descriptor, msg); 161162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers} 161262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers 161362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogersvoid Thread::ThrowNewWrappedException(const ThrowLocation& throw_location, 161462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers const char* exception_class_descriptor, 161562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers const char* msg) { 161662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers DCHECK_EQ(this, Thread::Current()); 161753b8b09fc80329539585dcf43657bc5f4ecefdffIan Rogers ScopedObjectAccessUnchecked soa(this); 1618eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier StackHandleScope<5> hs(soa.Self()); 161962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers // Ensure we don't forget arguments over object allocation. 1620eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier Handle<mirror::Object> saved_throw_this(hs.NewHandle(throw_location.GetThis())); 1621eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier Handle<mirror::ArtMethod> saved_throw_method(hs.NewHandle(throw_location.GetMethod())); 162262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers // Ignore the cause throw location. TODO: should we report this as a re-throw? 162353b8b09fc80329539585dcf43657bc5f4ecefdffIan Rogers ScopedLocalRef<jobject> cause(GetJniEnv(), soa.AddLocalReference<jobject>(GetException(nullptr))); 16249f1020305292a21fd14a402b189c765a125226abSebastien Hertz bool is_exception_reported = IsExceptionReportedToInstrumentation(); 162562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers ClearException(); 162662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers Runtime* runtime = Runtime::Current(); 1627a4f94740f9d66b21f4bcd3a225c434aa29cfa323Elliott Hughes 1628c528dba35b5faece51ca658fc008b688f8b690adMathieu Chartier mirror::ClassLoader* cl = nullptr; 1629eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier if (saved_throw_method.Get() != nullptr) { 1630eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier cl = saved_throw_method.Get()->GetDeclaringClass()->GetClassLoader(); 1631eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier } 1632eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier Handle<mirror::ClassLoader> class_loader(hs.NewHandle(cl)); 1633eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier Handle<mirror::Class> exception_class( 1634eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier hs.NewHandle(runtime->GetClassLinker()->FindClass(this, exception_class_descriptor, 1635eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier class_loader))); 1636eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier if (UNLIKELY(exception_class.Get() == nullptr)) { 163730646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes CHECK(IsExceptionPending()); 163862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers LOG(ERROR) << "No exception class " << PrettyDescriptor(exception_class_descriptor); 163930646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes return; 164030646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes } 164162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers 16427b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers if (UNLIKELY(!runtime->GetClassLinker()->EnsureInitialized(soa.Self(), exception_class, true, 16437b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers true))) { 164462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers DCHECK(IsExceptionPending()); 164562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers return; 164662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers } 164762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers DCHECK(!runtime->IsStarted() || exception_class->IsThrowableClass()); 1648eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier Handle<mirror::Throwable> exception( 1649eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier hs.NewHandle(down_cast<mirror::Throwable*>(exception_class->AllocObject(this)))); 165062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers 1651590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier // If we couldn't allocate the exception, throw the pre-allocated out of memory exception. 1652eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier if (exception.Get() == nullptr) { 1653eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier ThrowLocation gc_safe_throw_location(saved_throw_this.Get(), saved_throw_method.Get(), 1654c528dba35b5faece51ca658fc008b688f8b690adMathieu Chartier throw_location.GetDexPc()); 1655c528dba35b5faece51ca658fc008b688f8b690adMathieu Chartier SetException(gc_safe_throw_location, Runtime::Current()->GetPreAllocatedOutOfMemoryError()); 16569f1020305292a21fd14a402b189c765a125226abSebastien Hertz SetExceptionReportedToInstrumentation(is_exception_reported); 1657590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier return; 1658590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier } 1659590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier 166062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers // Choose an appropriate constructor and set up the arguments. 166162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers const char* signature; 166253b8b09fc80329539585dcf43657bc5f4ecefdffIan Rogers ScopedLocalRef<jstring> msg_string(GetJniEnv(), nullptr); 16630aded089f565008ba5908e395e5914ca4f91f2deDave Allison if (msg != nullptr) { 166462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers // Ensure we remember this and the method over the String allocation. 166553b8b09fc80329539585dcf43657bc5f4ecefdffIan Rogers msg_string.reset( 166653b8b09fc80329539585dcf43657bc5f4ecefdffIan Rogers soa.AddLocalReference<jstring>(mirror::String::AllocFromModifiedUtf8(this, msg))); 16670aded089f565008ba5908e395e5914ca4f91f2deDave Allison if (UNLIKELY(msg_string.get() == nullptr)) { 166862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers CHECK(IsExceptionPending()); // OOME. 166962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers return; 167062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers } 16710aded089f565008ba5908e395e5914ca4f91f2deDave Allison if (cause.get() == nullptr) { 167262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers signature = "(Ljava/lang/String;)V"; 1673ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom } else { 167462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers signature = "(Ljava/lang/String;Ljava/lang/Throwable;)V"; 167562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers } 167662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers } else { 16770aded089f565008ba5908e395e5914ca4f91f2deDave Allison if (cause.get() == nullptr) { 167862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers signature = "()V"; 167962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers } else { 168062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers signature = "(Ljava/lang/Throwable;)V"; 1681ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom } 1682ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom } 1683ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom mirror::ArtMethod* exception_init_method = 168462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers exception_class->FindDeclaredDirectMethod("<init>", signature); 168562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers 16860aded089f565008ba5908e395e5914ca4f91f2deDave Allison CHECK(exception_init_method != nullptr) << "No <init>" << signature << " in " 168762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers << PrettyDescriptor(exception_class_descriptor); 168862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers 168962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers if (UNLIKELY(!runtime->IsStarted())) { 169062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers // Something is trying to throw an exception without a started runtime, which is the common 169162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers // case in the compiler. We won't be able to invoke the constructor of the exception, so set 169262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers // the exception fields directly. 16930aded089f565008ba5908e395e5914ca4f91f2deDave Allison if (msg != nullptr) { 169453b8b09fc80329539585dcf43657bc5f4ecefdffIan Rogers exception->SetDetailMessage(down_cast<mirror::String*>(DecodeJObject(msg_string.get()))); 169562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers } 16960aded089f565008ba5908e395e5914ca4f91f2deDave Allison if (cause.get() != nullptr) { 169753b8b09fc80329539585dcf43657bc5f4ecefdffIan Rogers exception->SetCause(down_cast<mirror::Throwable*>(DecodeJObject(cause.get()))); 169862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers } 1699c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers ScopedLocalRef<jobject> trace(GetJniEnv(), 1700c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers Runtime::Current()->IsActiveTransaction() 1701c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers ? CreateInternalStackTrace<true>(soa) 1702c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers : CreateInternalStackTrace<false>(soa)); 1703c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers if (trace.get() != nullptr) { 1704c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers exception->SetStackState(down_cast<mirror::Throwable*>(DecodeJObject(trace.get()))); 1705c45b8b582be5c98941ca3869fcdc9a08d520da41Ian Rogers } 1706eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier ThrowLocation gc_safe_throw_location(saved_throw_this.Get(), saved_throw_method.Get(), 170762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers throw_location.GetDexPc()); 1708eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier SetException(gc_safe_throw_location, exception.Get()); 17099f1020305292a21fd14a402b189c765a125226abSebastien Hertz SetExceptionReportedToInstrumentation(is_exception_reported); 171062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers } else { 171153b8b09fc80329539585dcf43657bc5f4ecefdffIan Rogers jvalue jv_args[2]; 171253b8b09fc80329539585dcf43657bc5f4ecefdffIan Rogers size_t i = 0; 171353b8b09fc80329539585dcf43657bc5f4ecefdffIan Rogers 17140aded089f565008ba5908e395e5914ca4f91f2deDave Allison if (msg != nullptr) { 171553b8b09fc80329539585dcf43657bc5f4ecefdffIan Rogers jv_args[i].l = msg_string.get(); 171653b8b09fc80329539585dcf43657bc5f4ecefdffIan Rogers ++i; 171762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers } 17180aded089f565008ba5908e395e5914ca4f91f2deDave Allison if (cause.get() != nullptr) { 171953b8b09fc80329539585dcf43657bc5f4ecefdffIan Rogers jv_args[i].l = cause.get(); 172053b8b09fc80329539585dcf43657bc5f4ecefdffIan Rogers ++i; 172162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers } 1722eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier InvokeWithJValues(soa, exception.Get(), soa.EncodeMethod(exception_init_method), jv_args); 172362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers if (LIKELY(!IsExceptionPending())) { 1724eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier ThrowLocation gc_safe_throw_location(saved_throw_this.Get(), saved_throw_method.Get(), 172562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers throw_location.GetDexPc()); 1726eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier SetException(gc_safe_throw_location, exception.Get()); 17279f1020305292a21fd14a402b189c765a125226abSebastien Hertz SetExceptionReportedToInstrumentation(is_exception_reported); 172862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers } 172930646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes } 1730a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes} 1731a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes 17322ced6a534157d5d963693346904389c19775d2daElliott Hughesvoid Thread::ThrowOutOfMemoryError(const char* msg) { 17332ced6a534157d5d963693346904389c19775d2daElliott Hughes LOG(ERROR) << StringPrintf("Throwing OutOfMemoryError \"%s\"%s", 1734dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers msg, (tls32_.throwing_OutOfMemoryError ? " (recursive case)" : "")); 173562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers ThrowLocation throw_location = GetCurrentLocationForThrow(); 1736dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers if (!tls32_.throwing_OutOfMemoryError) { 1737dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tls32_.throwing_OutOfMemoryError = true; 173862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers ThrowNewException(throw_location, "Ljava/lang/OutOfMemoryError;", msg); 1739dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tls32_.throwing_OutOfMemoryError = false; 1740418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughes } else { 17417934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom Dump(LOG(ERROR)); // The pre-allocated OOME has no stack, so help out and log one. 174262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers SetException(throw_location, Runtime::Current()->GetPreAllocatedOutOfMemoryError()); 1743418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughes } 174479082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes} 174579082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes 1746498508c1187dc07d3eae5476784cde20f5224d93Elliott HughesThread* Thread::CurrentFromGdb() { 1747accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes return Thread::Current(); 1748accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes} 1749accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 1750accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughesvoid Thread::DumpFromGdb() const { 17516b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom std::ostringstream ss; 17526b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom Dump(ss); 1753955724179c6c739524f610023287f56b24dc31deElliott Hughes std::string str(ss.str()); 17546b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom // log to stderr for debugging command line processes 17556b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom std::cerr << str; 17566b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom#ifdef HAVE_ANDROID_OS 17576b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom // log to logcat for debugging frameworks processes 17586b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom LOG(INFO) << str; 17596b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom#endif 1760accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes} 1761accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 1762dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers// Explicitly instantiate 32 and 64bit thread offset dumping support. 1763dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogerstemplate void Thread::DumpThreadOffset<4>(std::ostream& os, uint32_t offset); 1764dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogerstemplate void Thread::DumpThreadOffset<8>(std::ostream& os, uint32_t offset); 176598e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes 1766dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogerstemplate<size_t ptr_size> 1767dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogersvoid Thread::DumpThreadOffset(std::ostream& os, uint32_t offset) { 1768dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers#define DO_THREAD_OFFSET(x, y) \ 1769dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers if (offset == x.Uint32Value()) { \ 1770dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers os << y; \ 1771f69863b3039fc621ff4250e262d2a024d5e79ec8Brian Carlstrom return; \ 1772f69863b3039fc621ff4250e262d2a024d5e79ec8Brian Carlstrom } 1773dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers DO_THREAD_OFFSET(ThreadFlagsOffset<ptr_size>(), "state_and_flags") 1774dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers DO_THREAD_OFFSET(CardTableOffset<ptr_size>(), "card_table") 1775dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers DO_THREAD_OFFSET(ExceptionOffset<ptr_size>(), "exception") 1776dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers DO_THREAD_OFFSET(PeerOffset<ptr_size>(), "peer"); 1777dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers DO_THREAD_OFFSET(JniEnvOffset<ptr_size>(), "jni_env") 1778dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers DO_THREAD_OFFSET(SelfOffset<ptr_size>(), "self") 1779dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers DO_THREAD_OFFSET(StackEndOffset<ptr_size>(), "stack_end") 1780dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers DO_THREAD_OFFSET(ThinLockIdOffset<ptr_size>(), "thin_lock_thread_id") 1781dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers DO_THREAD_OFFSET(TopOfManagedStackOffset<ptr_size>(), "top_quick_frame_method") 1782dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers DO_THREAD_OFFSET(TopOfManagedStackPcOffset<ptr_size>(), "top_quick_frame_pc") 1783dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers DO_THREAD_OFFSET(TopShadowFrameOffset<ptr_size>(), "top_shadow_frame") 1784eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier DO_THREAD_OFFSET(TopHandleScopeOffset<ptr_size>(), "top_handle_scope") 1785dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers DO_THREAD_OFFSET(ThreadSuspendTriggerOffset<ptr_size>(), "suspend_trigger") 178628fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes#undef DO_THREAD_OFFSET 178798e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes 1788dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers#define INTERPRETER_ENTRY_POINT_INFO(x) \ 1789dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers if (INTERPRETER_ENTRYPOINT_OFFSET(ptr_size, x).Uint32Value() == offset) { \ 1790dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers os << #x; \ 1791dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers return; \ 179298e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes } 1793dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers INTERPRETER_ENTRY_POINT_INFO(pInterpreterToInterpreterBridge) 1794dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers INTERPRETER_ENTRY_POINT_INFO(pInterpreterToCompiledCodeBridge) 1795dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers#undef INTERPRETER_ENTRY_POINT_INFO 1796dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers 1797dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers#define JNI_ENTRY_POINT_INFO(x) \ 1798dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers if (JNI_ENTRYPOINT_OFFSET(ptr_size, x).Uint32Value() == offset) { \ 1799dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers os << #x; \ 1800dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers return; \ 1801dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers } 1802dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers JNI_ENTRY_POINT_INFO(pDlsymLookup) 1803dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers#undef JNI_ENTRY_POINT_INFO 1804dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers 1805dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers#define PORTABLE_ENTRY_POINT_INFO(x) \ 1806dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers if (PORTABLE_ENTRYPOINT_OFFSET(ptr_size, x).Uint32Value() == offset) { \ 1807dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers os << #x; \ 1808dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers return; \ 1809dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers } 1810dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers PORTABLE_ENTRY_POINT_INFO(pPortableImtConflictTrampoline) 1811dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers PORTABLE_ENTRY_POINT_INFO(pPortableResolutionTrampoline) 1812dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers PORTABLE_ENTRY_POINT_INFO(pPortableToInterpreterBridge) 1813dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers#undef PORTABLE_ENTRY_POINT_INFO 1814dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers 1815dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers#define QUICK_ENTRY_POINT_INFO(x) \ 1816dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers if (QUICK_ENTRYPOINT_OFFSET(ptr_size, x).Uint32Value() == offset) { \ 1817dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers os << #x; \ 1818dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers return; \ 1819dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers } 1820dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pAllocArray) 1821dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pAllocArrayResolved) 1822dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pAllocArrayWithAccessCheck) 1823dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pAllocObject) 1824dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pAllocObjectResolved) 1825dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pAllocObjectInitialized) 1826dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pAllocObjectWithAccessCheck) 1827dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pCheckAndAllocArray) 1828dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pCheckAndAllocArrayWithAccessCheck) 1829dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pInstanceofNonTrivial) 1830dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pCheckCast) 1831dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pInitializeStaticStorage) 1832dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pInitializeTypeAndVerifyAccess) 1833dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pInitializeType) 1834dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pResolveString) 183537f05ef45e0393de812d51261dc293240c17294dFred Shih QUICK_ENTRY_POINT_INFO(pSet8Instance) 183637f05ef45e0393de812d51261dc293240c17294dFred Shih QUICK_ENTRY_POINT_INFO(pSet8Static) 183737f05ef45e0393de812d51261dc293240c17294dFred Shih QUICK_ENTRY_POINT_INFO(pSet16Instance) 183837f05ef45e0393de812d51261dc293240c17294dFred Shih QUICK_ENTRY_POINT_INFO(pSet16Static) 1839dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pSet32Instance) 1840dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pSet32Static) 1841dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pSet64Instance) 1842dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pSet64Static) 1843dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pSetObjInstance) 1844dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pSetObjStatic) 184537f05ef45e0393de812d51261dc293240c17294dFred Shih QUICK_ENTRY_POINT_INFO(pGetByteInstance) 184637f05ef45e0393de812d51261dc293240c17294dFred Shih QUICK_ENTRY_POINT_INFO(pGetBooleanInstance) 184737f05ef45e0393de812d51261dc293240c17294dFred Shih QUICK_ENTRY_POINT_INFO(pGetByteStatic) 184837f05ef45e0393de812d51261dc293240c17294dFred Shih QUICK_ENTRY_POINT_INFO(pGetBooleanStatic) 184937f05ef45e0393de812d51261dc293240c17294dFred Shih QUICK_ENTRY_POINT_INFO(pGetShortInstance) 185037f05ef45e0393de812d51261dc293240c17294dFred Shih QUICK_ENTRY_POINT_INFO(pGetCharInstance) 185137f05ef45e0393de812d51261dc293240c17294dFred Shih QUICK_ENTRY_POINT_INFO(pGetShortStatic) 185237f05ef45e0393de812d51261dc293240c17294dFred Shih QUICK_ENTRY_POINT_INFO(pGetCharStatic) 1853dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pGet32Instance) 1854dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pGet32Static) 1855dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pGet64Instance) 1856dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pGet64Static) 1857dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pGetObjInstance) 1858dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pGetObjStatic) 1859dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pAputObjectWithNullAndBoundCheck) 1860dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pAputObjectWithBoundCheck) 1861dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pAputObject) 1862dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pHandleFillArrayData) 1863dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pJniMethodStart) 1864dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pJniMethodStartSynchronized) 1865dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pJniMethodEnd) 1866dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pJniMethodEndSynchronized) 1867dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pJniMethodEndWithReference) 1868dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pJniMethodEndWithReferenceSynchronized) 1869dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pQuickGenericJniTrampoline) 1870dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pLockObject) 1871dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pUnlockObject) 1872dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pCmpgDouble) 1873dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pCmpgFloat) 1874dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pCmplDouble) 1875dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pCmplFloat) 1876dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pFmod) 1877dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pL2d) 1878dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pFmodf) 1879dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pL2f) 1880dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pD2iz) 1881dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pF2iz) 1882dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pIdivmod) 1883dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pD2l) 1884dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pF2l) 1885dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pLdiv) 1886dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pLmod) 1887dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pLmul) 1888dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pShlLong) 1889dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pShrLong) 1890dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pUshrLong) 1891dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pIndexOf) 1892dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pStringCompareTo) 1893dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pMemcpy) 1894dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pQuickImtConflictTrampoline) 1895dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pQuickResolutionTrampoline) 1896dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pQuickToInterpreterBridge) 1897dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pInvokeDirectTrampolineWithAccessCheck) 1898dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pInvokeInterfaceTrampolineWithAccessCheck) 1899dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pInvokeStaticTrampolineWithAccessCheck) 1900dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pInvokeSuperTrampolineWithAccessCheck) 1901dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pInvokeVirtualTrampolineWithAccessCheck) 1902dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pTestSuspend) 1903dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pDeliverException) 1904dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pThrowArrayBounds) 1905dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pThrowDivZero) 1906dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pThrowNoSuchMethod) 1907dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pThrowNullPointer) 1908dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers QUICK_ENTRY_POINT_INFO(pThrowStackOverflow) 1909d9cb8ae2ed78f957a773af61759432d7a7bf78afDouglas Leung QUICK_ENTRY_POINT_INFO(pA64Load) 1910d9cb8ae2ed78f957a773af61759432d7a7bf78afDouglas Leung QUICK_ENTRY_POINT_INFO(pA64Store) 1911dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers#undef QUICK_ENTRY_POINT_INFO 1912dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers 191398e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes os << offset; 191428fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes} 191528fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes 191694d6df471a406a03bb1afba8ca3ae9c0fbf366b5jeffhaovoid Thread::QuickDeliverException() { 191762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers // Get exception from thread. 191862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers ThrowLocation throw_location; 191962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers mirror::Throwable* exception = GetException(&throw_location); 19200aded089f565008ba5908e395e5914ca4f91f2deDave Allison CHECK(exception != nullptr); 192128ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers // Don't leave exception visible while we try to find the handler, which may cause class 1922d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes // resolution. 19239f1020305292a21fd14a402b189c765a125226abSebastien Hertz bool is_exception_reported = IsExceptionReportedToInstrumentation(); 192428ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers ClearException(); 1925fd3077e4b9ebadd281777310d26e64443858f653Sebastien Hertz bool is_deoptimization = (exception == GetDeoptimizationException()); 1926fd3077e4b9ebadd281777310d26e64443858f653Sebastien Hertz QuickExceptionHandler exception_handler(this, is_deoptimization); 1927fd3077e4b9ebadd281777310d26e64443858f653Sebastien Hertz if (is_deoptimization) { 1928fd3077e4b9ebadd281777310d26e64443858f653Sebastien Hertz exception_handler.DeoptimizeStack(); 1929fd3077e4b9ebadd281777310d26e64443858f653Sebastien Hertz } else { 19309f1020305292a21fd14a402b189c765a125226abSebastien Hertz exception_handler.FindCatch(throw_location, exception, is_exception_reported); 1931fd3077e4b9ebadd281777310d26e64443858f653Sebastien Hertz } 1932fd3077e4b9ebadd281777310d26e64443858f653Sebastien Hertz exception_handler.UpdateInstrumentationStack(); 1933fd3077e4b9ebadd281777310d26e64443858f653Sebastien Hertz exception_handler.DoLongJump(); 19349a8a8889418e09bf2d4ae3f683fbb40a71c3c8f4Ian Rogers LOG(FATAL) << "UNREACHABLE"; 1935bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers} 1936bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 1937bdb0391258abc54bf77c676e36847d28a783bfe5Ian RogersContext* Thread::GetLongJumpContext() { 1938dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers Context* result = tlsPtr_.long_jump_context; 19390aded089f565008ba5908e395e5914ca4f91f2deDave Allison if (result == nullptr) { 1940bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers result = Context::Create(); 19410399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers } else { 1942dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tlsPtr_.long_jump_context = nullptr; // Avoid context being shared. 19436702243ea2332b566d8e8b871cc9db0906d835adMathieu Chartier result->Reset(); 19441a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao } 1945bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers return result; 19461a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao} 19471a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao 19484a8c3fa4bd8d95fac5671ab778dd00b6dc3ec0e4Andreas Gampe// Note: this visitor may return with a method set, but dex_pc_ being DexFile:kDexNoIndex. This is 19494a8c3fa4bd8d95fac5671ab778dd00b6dc3ec0e4Andreas Gampe// so we don't abort in a special situation (thinlocked monitor) when dumping the Java stack. 1950e0dcd46314d07eeb332edea292f5110178e4e3d2Ian Rogersstruct CurrentMethodVisitor FINAL : public StackVisitor { 19516ec8ebd178ed39aa09e4c7fad194900114c4121aAndreas Gampe CurrentMethodVisitor(Thread* thread, Context* context, bool abort_on_error) 195262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) 1953f9df5c1639a9418fcdf70476556a4c30b210701eAndreas Gampe : StackVisitor(thread, context), this_object_(nullptr), method_(nullptr), dex_pc_(0), 19546ec8ebd178ed39aa09e4c7fad194900114c4121aAndreas Gampe abort_on_error_(abort_on_error) {} 1955e0dcd46314d07eeb332edea292f5110178e4e3d2Ian Rogers bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1956ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom mirror::ArtMethod* m = GetMethod(); 195762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers if (m->IsRuntimeMethod()) { 195862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers // Continue if this is a runtime method. 195962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers return true; 196062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers } 19610aded089f565008ba5908e395e5914ca4f91f2deDave Allison if (context_ != nullptr) { 196262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers this_object_ = GetThisObject(); 19630399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers } 196462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers method_ = m; 19656ec8ebd178ed39aa09e4c7fad194900114c4121aAndreas Gampe dex_pc_ = GetDexPc(abort_on_error_); 196662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers return false; 196762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers } 196862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers mirror::Object* this_object_; 1969ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom mirror::ArtMethod* method_; 197062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers uint32_t dex_pc_; 19716ec8ebd178ed39aa09e4c7fad194900114c4121aAndreas Gampe const bool abort_on_error_; 197262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers}; 19730399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers 19746ec8ebd178ed39aa09e4c7fad194900114c4121aAndreas Gampemirror::ArtMethod* Thread::GetCurrentMethod(uint32_t* dex_pc, bool abort_on_error) const { 19756ec8ebd178ed39aa09e4c7fad194900114c4121aAndreas Gampe CurrentMethodVisitor visitor(const_cast<Thread*>(this), nullptr, abort_on_error); 19760399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers visitor.WalkStack(false); 19770aded089f565008ba5908e395e5914ca4f91f2deDave Allison if (dex_pc != nullptr) { 19780399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers *dex_pc = visitor.dex_pc_; 1979d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes } 19800399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers return visitor.method_; 1981ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYa} 198233dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao 198362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian RogersThrowLocation Thread::GetCurrentLocationForThrow() { 198462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers Context* context = GetLongJumpContext(); 1985f9df5c1639a9418fcdf70476556a4c30b210701eAndreas Gampe CurrentMethodVisitor visitor(this, context, true); 198662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers visitor.WalkStack(false); 198762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers ReleaseLongJumpContext(context); 198862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers return ThrowLocation(visitor.this_object_, visitor.method_, visitor.dex_pc_); 198962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers} 199062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers 1991dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogersbool Thread::HoldsLock(mirror::Object* object) const { 19920aded089f565008ba5908e395e5914ca4f91f2deDave Allison if (object == nullptr) { 19935f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return false; 19945f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 1995dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers return object->GetLockOwnerThreadId() == GetThreadId(); 19965f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 19975f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 199840e3bacfd57bca2ca39c1caec64680bd0ed4a16dIan Rogers// RootVisitor parameters are: (const Object* obj, size_t vreg, const StackVisitor* visitor). 199940e3bacfd57bca2ca39c1caec64680bd0ed4a16dIan Rogerstemplate <typename RootVisitor> 20000399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogersclass ReferenceMapVisitor : public StackVisitor { 2001d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers public: 20027a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers ReferenceMapVisitor(Thread* thread, Context* context, const RootVisitor& visitor) 2003b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) 20047a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers : StackVisitor(thread, context), visitor_(visitor) {} 2005d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers 2006b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 20076a4be3a6226cec645cf905dd352e44f7968a7fa4Brian Carlstrom if (false) { 20080399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers LOG(INFO) << "Visiting stack roots in " << PrettyMethod(GetMethod()) 2009714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz << StringPrintf("@ PC:%04x", GetDexPc()); 20106a4be3a6226cec645cf905dd352e44f7968a7fa4Brian Carlstrom } 20110399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers ShadowFrame* shadow_frame = GetCurrentShadowFrame(); 20120aded089f565008ba5908e395e5914ca4f91f2deDave Allison if (shadow_frame != nullptr) { 2013714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz VisitShadowFrame(shadow_frame); 2014714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz } else { 2015714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz VisitQuickFrame(); 2016714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz } 2017714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz return true; 2018714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz } 2019714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz 2020714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz void VisitShadowFrame(ShadowFrame* shadow_frame) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 202192d1a666534aa98b173bb33dc5dba86b2d48aedbHiroshi Yamauchi mirror::ArtMethod** method_addr = shadow_frame->GetMethodAddress(); 202292d1a666534aa98b173bb33dc5dba86b2d48aedbHiroshi Yamauchi visitor_(reinterpret_cast<mirror::Object**>(method_addr), 0 /*ignored*/, this); 202392d1a666534aa98b173bb33dc5dba86b2d48aedbHiroshi Yamauchi mirror::ArtMethod* m = *method_addr; 202492d1a666534aa98b173bb33dc5dba86b2d48aedbHiroshi Yamauchi DCHECK(m != nullptr); 2025714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz size_t num_regs = shadow_frame->NumberOfVRegs(); 2026714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz if (m->IsNative() || shadow_frame->HasReferenceArray()) { 2027eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier // handle scope for JNI or References for interpreter. 2028714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz for (size_t reg = 0; reg < num_regs; ++reg) { 2029714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz mirror::Object* ref = shadow_frame->GetVRegReference(reg); 2030714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz if (ref != nullptr) { 2031714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz mirror::Object* new_ref = ref; 2032714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz visitor_(&new_ref, reg, this); 2033714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz if (new_ref != ref) { 2034714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz shadow_frame->SetVRegReference(reg, new_ref); 2035714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz } 2036714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz } 2037714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz } 2038714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz } else { 2039714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz // Java method. 2040714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz // Portable path use DexGcMap and store in Method.native_gc_map_. 2041714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz const uint8_t* gc_map = m->GetNativeGcMap(); 2042714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz CHECK(gc_map != nullptr) << PrettyMethod(m); 2043714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz verifier::DexPcToReferenceMap dex_gc_map(gc_map); 2044714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz uint32_t dex_pc = shadow_frame->GetDexPC(); 2045714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz const uint8_t* reg_bitmap = dex_gc_map.FindBitMap(dex_pc); 2046714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz DCHECK(reg_bitmap != nullptr); 2047714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz num_regs = std::min(dex_gc_map.RegWidth() * 8, num_regs); 2048714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz for (size_t reg = 0; reg < num_regs; ++reg) { 2049714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz if (TestBitmap(reg, reg_bitmap)) { 20502dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::Object* ref = shadow_frame->GetVRegReference(reg); 2051423d2a3dcbb260b020efb5da59f784c9f02accbfMathieu Chartier if (ref != nullptr) { 2052815873ecc312b1d231acce71e1a16f42cdaf09f2Mathieu Chartier mirror::Object* new_ref = ref; 2053815873ecc312b1d231acce71e1a16f42cdaf09f2Mathieu Chartier visitor_(&new_ref, reg, this); 2054423d2a3dcbb260b020efb5da59f784c9f02accbfMathieu Chartier if (new_ref != ref) { 2055714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz shadow_frame->SetVRegReference(reg, new_ref); 2056423d2a3dcbb260b020efb5da59f784c9f02accbfMathieu Chartier } 2057ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa } 2058ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa } 2059714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz } 2060714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz } 2061714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz } 2062714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz 2063714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz private: 2064714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz void VisitQuickFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 2065cf4035a4c41ccfcc3e89a0cee25f5218a11b0705Andreas Gampe StackReference<mirror::ArtMethod>* cur_quick_frame = GetCurrentQuickFrame(); 2066cf4035a4c41ccfcc3e89a0cee25f5218a11b0705Andreas Gampe mirror::ArtMethod* m = cur_quick_frame->AsMirrorPtr(); 2067cf4035a4c41ccfcc3e89a0cee25f5218a11b0705Andreas Gampe mirror::ArtMethod* old_method = m; 2068cf4035a4c41ccfcc3e89a0cee25f5218a11b0705Andreas Gampe visitor_(reinterpret_cast<mirror::Object**>(&m), 0 /*ignored*/, this); 2069cf4035a4c41ccfcc3e89a0cee25f5218a11b0705Andreas Gampe if (m != old_method) { 2070cf4035a4c41ccfcc3e89a0cee25f5218a11b0705Andreas Gampe cur_quick_frame->Assign(m); 2071cf4035a4c41ccfcc3e89a0cee25f5218a11b0705Andreas Gampe } 2072cf4035a4c41ccfcc3e89a0cee25f5218a11b0705Andreas Gampe 2073714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz // Process register map (which native and runtime methods don't have) 2074714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz if (!m->IsNative() && !m->IsRuntimeMethod() && !m->IsProxyMethod()) { 20753946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray if (m->IsOptimized()) { 20764c1c510bea6f20f4d8b09e15547cd2967ad51c88Vladimir Marko Runtime* runtime = Runtime::Current(); 20774c1c510bea6f20f4d8b09e15547cd2967ad51c88Vladimir Marko const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(m); 20784c1c510bea6f20f4d8b09e15547cd2967ad51c88Vladimir Marko uintptr_t native_pc_offset = m->NativePcOffset(GetCurrentQuickFramePc(), entry_point); 20793946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray StackMap map = m->GetStackMap(native_pc_offset); 20803946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray MemoryRegion mask = map.GetStackMask(); 20813946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray for (size_t i = 0; i < mask.size_in_bits(); ++i) { 20823946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray if (mask.LoadBit(i)) { 20833946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray StackReference<mirror::Object>* ref_addr = 20843946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray reinterpret_cast<StackReference<mirror::Object>*>(cur_quick_frame) + i; 20853946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray mirror::Object* ref = ref_addr->AsMirrorPtr(); 20863946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray if (ref != nullptr) { 20873946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray mirror::Object* new_ref = ref; 20883946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray visitor_(&new_ref, -1, this); 20893946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray if (ref != new_ref) { 20903946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray ref_addr->Assign(new_ref); 2091423d2a3dcbb260b020efb5da59f784c9f02accbfMathieu Chartier } 20923946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 20933946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 20943946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 20953946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } else { 20963946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray const uint8_t* native_gc_map = m->GetNativeGcMap(); 20973946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray CHECK(native_gc_map != nullptr) << PrettyMethod(m); 20983946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray const DexFile::CodeItem* code_item = m->GetCodeItem(); 20993946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // Can't be nullptr or how would we compile its instructions? 21003946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray DCHECK(code_item != nullptr) << PrettyMethod(m); 21013946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray NativePcOffsetToReferenceMap map(native_gc_map); 21023946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray size_t num_regs = std::min(map.RegWidth() * 8, 21033946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray static_cast<size_t>(code_item->registers_size_)); 21043946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray if (num_regs > 0) { 21053946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray Runtime* runtime = Runtime::Current(); 21063946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(m); 21073946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray uintptr_t native_pc_offset = m->NativePcOffset(GetCurrentQuickFramePc(), entry_point); 21083946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset); 21093946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray DCHECK(reg_bitmap != nullptr); 21103946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray const void* code_pointer = mirror::ArtMethod::EntryPointToCodePointer(entry_point); 21113946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray const VmapTable vmap_table(m->GetVmapTable(code_pointer)); 21123946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray QuickMethodFrameInfo frame_info = m->GetQuickFrameInfo(code_pointer); 21133946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // For all dex registers in the bitmap 21143946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray StackReference<mirror::ArtMethod>* cur_quick_frame = GetCurrentQuickFrame(); 21153946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray DCHECK(cur_quick_frame != nullptr); 21163946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray for (size_t reg = 0; reg < num_regs; ++reg) { 21173946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // Does this register hold a reference? 21183946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray if (TestBitmap(reg, reg_bitmap)) { 21193946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray uint32_t vmap_offset; 21203946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray if (vmap_table.IsInContext(reg, kReferenceVReg, &vmap_offset)) { 21213946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray int vmap_reg = vmap_table.ComputeRegister(frame_info.CoreSpillMask(), vmap_offset, 21223946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray kReferenceVReg); 21233946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray // This is sound as spilled GPRs will be word sized (ie 32 or 64bit). 21243946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray mirror::Object** ref_addr = 21253946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray reinterpret_cast<mirror::Object**>(GetGPRAddress(vmap_reg)); 21263946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray if (*ref_addr != nullptr) { 21273946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray visitor_(ref_addr, reg, this); 21283946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 21293946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } else { 21303946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray StackReference<mirror::Object>* ref_addr = 21313946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray reinterpret_cast<StackReference<mirror::Object>*>( 21323946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray GetVRegAddr(cur_quick_frame, code_item, frame_info.CoreSpillMask(), 21333946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray frame_info.FpSpillMask(), frame_info.FrameSizeInBytes(), reg)); 21343946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray mirror::Object* ref = ref_addr->AsMirrorPtr(); 21353946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray if (ref != nullptr) { 21363946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray mirror::Object* new_ref = ref; 21373946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray visitor_(&new_ref, reg, this); 21383946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray if (ref != new_ref) { 21393946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray ref_addr->Assign(new_ref); 21403946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 2141423d2a3dcbb260b020efb5da59f784c9f02accbfMathieu Chartier } 21420399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers } 2143d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 21444f894e385e8ac018f078be75fea0a45696626b15Shih-wei Liao } 2145d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 2146d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 2147d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 2148d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 2149d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers 2150815873ecc312b1d231acce71e1a16f42cdaf09f2Mathieu Chartier static bool TestBitmap(size_t reg, const uint8_t* reg_vector) { 2151815873ecc312b1d231acce71e1a16f42cdaf09f2Mathieu Chartier return ((reg_vector[reg / kBitsPerByte] >> (reg % kBitsPerByte)) & 0x01) != 0; 2152d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 2153d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers 21546f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier // Visitor for when we visit a root. 215540e3bacfd57bca2ca39c1caec64680bd0ed4a16dIan Rogers const RootVisitor& visitor_; 2156d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers}; 2157d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers 21586f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartierclass RootCallbackVisitor { 21596f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier public: 216083c8ee000d525017ead8753fce6bc1020249b96aMathieu Chartier RootCallbackVisitor(RootCallback* callback, void* arg, uint32_t tid) 216183c8ee000d525017ead8753fce6bc1020249b96aMathieu Chartier : callback_(callback), arg_(arg), tid_(tid) {} 21626f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier 2163815873ecc312b1d231acce71e1a16f42cdaf09f2Mathieu Chartier void operator()(mirror::Object** obj, size_t, const StackVisitor*) const { 2164815873ecc312b1d231acce71e1a16f42cdaf09f2Mathieu Chartier callback_(obj, arg_, tid_, kRootJavaFrame); 21656f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier } 21666f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier 21676f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier private: 216883c8ee000d525017ead8753fce6bc1020249b96aMathieu Chartier RootCallback* const callback_; 216940e3bacfd57bca2ca39c1caec64680bd0ed4a16dIan Rogers void* const arg_; 217083c8ee000d525017ead8753fce6bc1020249b96aMathieu Chartier const uint32_t tid_; 21716f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier}; 21726f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier 217383c8ee000d525017ead8753fce6bc1020249b96aMathieu Chartiervoid Thread::VisitRoots(RootCallback* visitor, void* arg) { 217483c8ee000d525017ead8753fce6bc1020249b96aMathieu Chartier uint32_t thread_id = GetThreadId(); 2175dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers if (tlsPtr_.opeer != nullptr) { 2176dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers visitor(&tlsPtr_.opeer, arg, thread_id, kRootThreadObject); 2177cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers } 2178fd3077e4b9ebadd281777310d26e64443858f653Sebastien Hertz if (tlsPtr_.exception != nullptr && tlsPtr_.exception != GetDeoptimizationException()) { 2179dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers visitor(reinterpret_cast<mirror::Object**>(&tlsPtr_.exception), arg, thread_id, kRootNativeStack); 2180d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 2181dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tlsPtr_.throw_location.VisitRoots(visitor, arg); 218243d8bb870fadd4775a8513a1df955cdf66ad4b92Mathieu Chartier if (tlsPtr_.monitor_enter_object != nullptr) { 218343d8bb870fadd4775a8513a1df955cdf66ad4b92Mathieu Chartier visitor(&tlsPtr_.monitor_enter_object, arg, thread_id, kRootNativeStack); 218443d8bb870fadd4775a8513a1df955cdf66ad4b92Mathieu Chartier } 2185dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tlsPtr_.jni_env->locals.VisitRoots(visitor, arg, thread_id, kRootJNILocal); 2186dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tlsPtr_.jni_env->monitors.VisitRoots(visitor, arg, thread_id, kRootJNIMonitor); 2187eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier HandleScopeVisitRoots(visitor, arg, thread_id); 2188dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers if (tlsPtr_.debug_invoke_req != nullptr) { 2189dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tlsPtr_.debug_invoke_req->VisitRoots(visitor, arg, thread_id, kRootDebugger); 21903b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier } 2191dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers if (tlsPtr_.single_step_control != nullptr) { 2192dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tlsPtr_.single_step_control->VisitRoots(visitor, arg, thread_id, kRootDebugger); 21933b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier } 2194714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz if (tlsPtr_.deoptimization_shadow_frame != nullptr) { 2195714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz RootCallbackVisitor visitorToCallback(visitor, arg, thread_id); 2196714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz ReferenceMapVisitor<RootCallbackVisitor> mapper(this, nullptr, visitorToCallback); 2197714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz for (ShadowFrame* shadow_frame = tlsPtr_.deoptimization_shadow_frame; shadow_frame != nullptr; 2198714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz shadow_frame = shadow_frame->GetLink()) { 2199714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz mapper.VisitShadowFrame(shadow_frame); 2200714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz } 2201714f175bd66d03225927a84f3d5dbc923c5a3e7eSebastien Hertz } 22022a0d4ec9532a89abe722e5babdfbb846ffaad721Andreas Gampe if (tlsPtr_.shadow_frame_under_construction != nullptr) { 22032a0d4ec9532a89abe722e5babdfbb846ffaad721Andreas Gampe RootCallbackVisitor visitorToCallback(visitor, arg, thread_id); 22042a0d4ec9532a89abe722e5babdfbb846ffaad721Andreas Gampe ReferenceMapVisitor<RootCallbackVisitor> mapper(this, nullptr, visitorToCallback); 22052a0d4ec9532a89abe722e5babdfbb846ffaad721Andreas Gampe for (ShadowFrame* shadow_frame = tlsPtr_.shadow_frame_under_construction; 22062a0d4ec9532a89abe722e5babdfbb846ffaad721Andreas Gampe shadow_frame != nullptr; 22072a0d4ec9532a89abe722e5babdfbb846ffaad721Andreas Gampe shadow_frame = shadow_frame->GetLink()) { 22082a0d4ec9532a89abe722e5babdfbb846ffaad721Andreas Gampe mapper.VisitShadowFrame(shadow_frame); 22092a0d4ec9532a89abe722e5babdfbb846ffaad721Andreas Gampe } 22102a0d4ec9532a89abe722e5babdfbb846ffaad721Andreas Gampe } 2211d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Visit roots on this thread's stack 22120399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers Context* context = GetLongJumpContext(); 221383c8ee000d525017ead8753fce6bc1020249b96aMathieu Chartier RootCallbackVisitor visitorToCallback(visitor, arg, thread_id); 22147a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers ReferenceMapVisitor<RootCallbackVisitor> mapper(this, context, visitorToCallback); 22150399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers mapper.WalkStack(); 22160399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers ReleaseLongJumpContext(context); 2217423d2a3dcbb260b020efb5da59f784c9f02accbfMathieu Chartier for (instrumentation::InstrumentationStackFrame& frame : *GetInstrumentationStack()) { 2218423d2a3dcbb260b020efb5da59f784c9f02accbfMathieu Chartier if (frame.this_object_ != nullptr) { 2219815873ecc312b1d231acce71e1a16f42cdaf09f2Mathieu Chartier visitor(&frame.this_object_, arg, thread_id, kRootJavaFrame); 222062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers } 2221423d2a3dcbb260b020efb5da59f784c9f02accbfMathieu Chartier DCHECK(frame.method_ != nullptr); 2222815873ecc312b1d231acce71e1a16f42cdaf09f2Mathieu Chartier visitor(reinterpret_cast<mirror::Object**>(&frame.method_), arg, thread_id, kRootJavaFrame); 222362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers } 2224410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes} 2225410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes 22264e30541a92381fb280cd0be9a1763b713ee4d64cMathieu Chartierstatic void VerifyRoot(mirror::Object** root, void* /*arg*/, uint32_t /*thread_id*/, 22274e30541a92381fb280cd0be9a1763b713ee4d64cMathieu Chartier RootType /*root_type*/) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 22284e30541a92381fb280cd0be9a1763b713ee4d64cMathieu Chartier VerifyObject(*root); 2229250455229aa0cc07bbd18174efe510bd52631a99jeffhao} 2230250455229aa0cc07bbd18174efe510bd52631a99jeffhao 223104d7aa92bc5548bc4d272b9480614f06248194ccIan Rogersvoid Thread::VerifyStackImpl() { 2232700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers std::unique_ptr<Context> context(Context::Create()); 223389bb1449bbef1863b12dfa3813ab7c78be94316fMathieu Chartier RootCallbackVisitor visitorToCallback(VerifyRoot, Runtime::Current()->GetHeap(), GetThreadId()); 223404d7aa92bc5548bc4d272b9480614f06248194ccIan Rogers ReferenceMapVisitor<RootCallbackVisitor> mapper(this, context.get(), visitorToCallback); 22350399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers mapper.WalkStack(); 2236250455229aa0cc07bbd18174efe510bd52631a99jeffhao} 2237250455229aa0cc07bbd18174efe510bd52631a99jeffhao 223800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Set the stack end to that to be used during a stack overflow 223900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersvoid Thread::SetStackEndForStackOverflow() { 22407571e8b761ebc2c923525e12ea9fcf07e62cb33eBrian Carlstrom // During stack overflow we allow use of the full stack. 2241dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers if (tlsPtr_.stack_end == tlsPtr_.stack_begin) { 22427571e8b761ebc2c923525e12ea9fcf07e62cb33eBrian Carlstrom // However, we seem to have already extended to use the full stack. 22437571e8b761ebc2c923525e12ea9fcf07e62cb33eBrian Carlstrom LOG(ERROR) << "Need to increase kStackOverflowReservedBytes (currently " 22447ea6f79bbddd69d5db86a8656a31aaaf64ae2582Andreas Gampe << GetStackOverflowReservedBytes(kRuntimeISA) << ")?"; 22457571e8b761ebc2c923525e12ea9fcf07e62cb33eBrian Carlstrom DumpStack(LOG(ERROR)); 22467571e8b761ebc2c923525e12ea9fcf07e62cb33eBrian Carlstrom LOG(FATAL) << "Recursive stack overflow."; 224700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 224800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 2249dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tlsPtr_.stack_end = tlsPtr_.stack_begin; 2250648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison 2251648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison // Remove the stack overflow protection if is it set up. 2252648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison bool implicit_stack_check = !Runtime::Current()->ExplicitStackOverflowChecks(); 2253648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison if (implicit_stack_check) { 2254648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison if (!UnprotectStack()) { 2255648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison LOG(ERROR) << "Unable to remove stack protection for stack overflow"; 2256648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison } 2257648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison } 225800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers} 225900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 2260e6da9af8dfe0a3e3fbc2be700554f6478380e7b9Mathieu Chartiervoid Thread::SetTlab(byte* start, byte* end) { 2261692fafd9778141fa6ef0048c9569abd7ee0253bfMathieu Chartier DCHECK_LE(start, end); 2262dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tlsPtr_.thread_local_start = start; 2263dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tlsPtr_.thread_local_pos = tlsPtr_.thread_local_start; 2264dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tlsPtr_.thread_local_end = end; 2265dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers tlsPtr_.thread_local_objects = 0; 2266692fafd9778141fa6ef0048c9569abd7ee0253bfMathieu Chartier} 2267692fafd9778141fa6ef0048c9569abd7ee0253bfMathieu Chartier 2268c93c530efc175954160c3834c93961a1a946a35aHiroshi Yamauchibool Thread::HasTlab() const { 2269dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers bool has_tlab = tlsPtr_.thread_local_pos != nullptr; 2270c93c530efc175954160c3834c93961a1a946a35aHiroshi Yamauchi if (has_tlab) { 2271dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers DCHECK(tlsPtr_.thread_local_start != nullptr && tlsPtr_.thread_local_end != nullptr); 2272c93c530efc175954160c3834c93961a1a946a35aHiroshi Yamauchi } else { 2273dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers DCHECK(tlsPtr_.thread_local_start == nullptr && tlsPtr_.thread_local_end == nullptr); 2274c93c530efc175954160c3834c93961a1a946a35aHiroshi Yamauchi } 2275c93c530efc175954160c3834c93961a1a946a35aHiroshi Yamauchi return has_tlab; 2276c93c530efc175954160c3834c93961a1a946a35aHiroshi Yamauchi} 2277c93c530efc175954160c3834c93961a1a946a35aHiroshi Yamauchi 2278330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughesstd::ostream& operator<<(std::ostream& os, const Thread& thread) { 227900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers thread.ShortDump(os); 2280330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughes return os; 2281330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughes} 2282330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughes 2283648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allisonvoid Thread::ProtectStack() { 2284648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison void* pregion = tlsPtr_.stack_begin - kStackOverflowProtectedSize; 2285648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison VLOG(threads) << "Protecting stack at " << pregion; 2286648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison if (mprotect(pregion, kStackOverflowProtectedSize, PROT_NONE) == -1) { 2287648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison LOG(FATAL) << "Unable to create protected region in stack for implicit overflow check. " 2288648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison "Reason: " 2289648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison << strerror(errno) << " size: " << kStackOverflowProtectedSize; 2290648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison } 2291648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison} 2292648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison 2293648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allisonbool Thread::UnprotectStack() { 2294648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison void* pregion = tlsPtr_.stack_begin - kStackOverflowProtectedSize; 2295648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison VLOG(threads) << "Unprotecting stack at " << pregion; 2296648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison return mprotect(pregion, kStackOverflowProtectedSize, PROT_READ|PROT_WRITE) == 0; 2297648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison} 2298648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison 2299648d7112609dd19c38131b3e71c37bcbbd19d11eDave Allison 23008daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes} // namespace art 2301