thread.cc revision ce4cc0d1818e872c1c7f3c3519a82259afd5c288
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 17578bbdc684db8ed68e9fedbc678669d27fa68b6eBrian Carlstrom#include "thread.h" 18b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 198d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes#include <dynamic_annotations.h> 20b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers#include <pthread.h> 212acf36d8cfeb5ddb293904148aa70f25ef6d8845Elliott Hughes#include <signal.h> 22dbf05b722af99ba2fd2f4c4fc7eb6c3e9880e5d1Brian Carlstrom#include <sys/resource.h> 23dbf05b722af99ba2fd2f4c4fc7eb6c3e9880e5d1Brian Carlstrom#include <sys/time.h> 24a09576416788b916095739e43a16917e7948f3a4Elliott Hughes 25b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro#include <algorithm> 26dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes#include <bitset> 27eb4f614f2eb53b92ebd416fa418f550861655887Elliott Hughes#include <cerrno> 28a09576416788b916095739e43a16917e7948f3a4Elliott Hughes#include <iostream> 29b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro#include <list> 30b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 31a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes#include "class_linker.h" 32df143242f4beaad4cc9fbabebfc033b68c40964eBrian Carlstrom#include "class_loader.h" 33474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers#include "cutils/atomic.h" 34474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers#include "cutils/atomic-inline.h" 3546e251bf7200cc06f5a9a82ee2030e650f5e1443Elliott Hughes#include "debugger.h" 360c7abda482f53db3d153c073d1c7a145f84e0626Ian Rogers#include "gc_map.h" 37408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers#include "heap.h" 38c5f7c91ab89055cffb573fff7e06dbdd860bccedElliott Hughes#include "jni_internal.h" 398e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes#include "monitor.h" 4081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers#include "mutex.h" 4157b86d47b66322693a070185fadfb43cb9c12eabIan Rogers#include "oat/runtime/context.h" 42a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes#include "object.h" 436d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers#include "object_utils.h" 449a6bae896a2f003d7216603bf29771d105c10ca4Jesse Wilson#include "reflection.h" 45578bbdc684db8ed68e9fedbc678669d27fa68b6eBrian Carlstrom#include "runtime.h" 465433072f589b61413e042eddf76e8190a048f71dbuzbee#include "runtime_support.h" 4700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#include "scoped_thread_state_change.h" 4846e251bf7200cc06f5a9a82ee2030e650f5e1443Elliott Hughes#include "ScopedLocalRef.h" 491f5393447b9f45be7918042d9ee7b521376de866Ian Rogers#include "sirt_ref.h" 507469ebf3888b8037421cb6834f37f946646265ecMathieu Chartier#include "gc/space.h" 5168e76526e98432625464022cb26f66b9ef6f5af4Elliott Hughes#include "stack.h" 5268e76526e98432625464022cb26f66b9ef6f5af4Elliott Hughes#include "stack_indirect_reference_table.h" 538daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes#include "thread_list.h" 54a09576416788b916095739e43a16917e7948f3a4Elliott Hughes#include "utils.h" 55ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa#include "verifier/dex_gc_map.h" 56eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes#include "well_known_classes.h" 57b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 58b557353b22c728eecbd1c68593b482622c7782a8Carl Shapironamespace art { 59b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 60b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiropthread_key_t Thread::pthread_key_self_; 6100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan RogersConditionVariable* Thread::resume_cond_; 62b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 637dc5166ea740359d381097a7ab382c1dd404055fElliott Hughesstatic const char* kThreadNameDuringStartup = "<native thread without managed peer>"; 647dc5166ea740359d381097a7ab382c1dd404055fElliott Hughes 655d76c435082332ef79a22962386fa92a0870e378Ian Rogersvoid Thread::InitCardTable() { 66b3bd5f07884f5a1f2b84224363b1372d7c28d447Elliott Hughes card_table_ = Runtime::Current()->GetHeap()->GetCardTable()->GetBiasedBegin(); 675d76c435082332ef79a22962386fa92a0870e378Ian Rogers} 685d76c435082332ef79a22962386fa92a0870e378Ian Rogers 6999250ba6a2cc72b8894dffa77c1fca5570087e3cElliott Hughes#if !defined(__APPLE__) 703ea0f42467790809fcfc9fc861605d465808090fElliott Hughesstatic void UnimplementedEntryPoint() { 713ea0f42467790809fcfc9fc861605d465808090fElliott Hughes UNIMPLEMENTED(FATAL); 723ea0f42467790809fcfc9fc861605d465808090fElliott Hughes} 7399250ba6a2cc72b8894dffa77c1fca5570087e3cElliott Hughes#endif 743ea0f42467790809fcfc9fc861605d465808090fElliott Hughes 753ea4ec5629613013ad9b0d7a69abdb94491ac46fbuzbeevoid Thread::InitFunctionPointers() { 7699250ba6a2cc72b8894dffa77c1fca5570087e3cElliott Hughes#if !defined(__APPLE__) // The Mac GCC is too old to accept this code. 773ea0f42467790809fcfc9fc861605d465808090fElliott Hughes // Insert a placeholder so we can easily tell if we call an unimplemented entry point. 783ea0f42467790809fcfc9fc861605d465808090fElliott Hughes uintptr_t* begin = reinterpret_cast<uintptr_t*>(&entrypoints_); 793ea0f42467790809fcfc9fc861605d465808090fElliott Hughes uintptr_t* end = reinterpret_cast<uintptr_t*>(reinterpret_cast<uint8_t*>(begin) + sizeof(entrypoints_)); 803ea0f42467790809fcfc9fc861605d465808090fElliott Hughes for (uintptr_t* it = begin; it != end; ++it) { 813ea0f42467790809fcfc9fc861605d465808090fElliott Hughes *it = reinterpret_cast<uintptr_t>(UnimplementedEntryPoint); 823ea0f42467790809fcfc9fc861605d465808090fElliott Hughes } 8399250ba6a2cc72b8894dffa77c1fca5570087e3cElliott Hughes#endif 8457b86d47b66322693a070185fadfb43cb9c12eabIan Rogers InitEntryPoints(&entrypoints_); 85c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes} 86c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes 87c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughesvoid Thread::SetDebuggerUpdatesEnabled(bool enabled) { 88c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes LOG(INFO) << "Turning debugger updates " << (enabled ? "on" : "off") << " for " << *this; 89776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#if !defined(ART_USE_LLVM_COMPILER) 9057b86d47b66322693a070185fadfb43cb9c12eabIan Rogers ChangeDebuggerEntryPoint(&entrypoints_, enabled); 91776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#else 92776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers UNIMPLEMENTED(FATAL); 93776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#endif 943ea4ec5629613013ad9b0d7a69abdb94491ac46fbuzbee} 953ea4ec5629613013ad9b0d7a69abdb94491ac46fbuzbee 96caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstromvoid Thread::InitTid() { 97caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom tid_ = ::art::GetTid(); 98caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom} 99caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom 100caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstromvoid Thread::InitAfterFork() { 1018029cbe626a467b344ff84d86170d757aa12ecd4Elliott Hughes // One thread (us) survived the fork, but we have a new tid so we need to 1028029cbe626a467b344ff84d86170d757aa12ecd4Elliott Hughes // update the value stashed in this Thread*. 103caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom InitTid(); 104caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom} 105caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom 10678128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstromvoid* Thread::CreateCallback(void* arg) { 10793e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes Thread* self = reinterpret_cast<Thread*>(arg); 108120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers Runtime* runtime = Runtime::Current(); 109120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers if (runtime == NULL) { 110120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers LOG(ERROR) << "Thread attaching to non-existent runtime: " << *self; 111120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers return NULL; 112120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers } 113120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers { 11450b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers // TODO: pass self to MutexLock - requires self to equal Thread::Current(), which is only true 11550b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers // after self->Init(). 11650b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers MutexLock mu(NULL, *Locks::runtime_shutdown_lock_); 117120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers // Check that if we got here we cannot be shutting down (as shutdown should never have started 118120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers // while threads are being born). 119120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers CHECK(!runtime->IsShuttingDown()); 120120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers self->Init(runtime->GetThreadList(), runtime->GetJavaVM()); 121120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers Runtime::Current()->EndThreadBirth(); 122120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers } 12347179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes { 12400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ScopedObjectAccess soa(self); 125cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers 126cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers // Copy peer into self, deleting global reference when done. 127cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers CHECK(self->jpeer_ != NULL); 128cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers self->opeer_ = soa.Decode<Object*>(self->jpeer_); 129cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers self->GetJniEnv()->DeleteGlobalRef(self->jpeer_); 130cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers self->jpeer_ = NULL; 131cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers 132365c10235438607541fa2259a5fec48061b90bd8Ian Rogers { 1331f5393447b9f45be7918042d9ee7b521376de866Ian Rogers SirtRef<String> thread_name(self, self->GetThreadName(soa)); 134365c10235438607541fa2259a5fec48061b90bd8Ian Rogers self->SetThreadName(thread_name->ToModifiedUtf8().c_str()); 135365c10235438607541fa2259a5fec48061b90bd8Ian Rogers } 136365c10235438607541fa2259a5fec48061b90bd8Ian Rogers Dbg::PostThreadStart(self); 13793e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 138365c10235438607541fa2259a5fec48061b90bd8Ian Rogers // Invoke the 'run' method of our java.lang.Thread. 139cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers Object* receiver = self->opeer_; 140365c10235438607541fa2259a5fec48061b90bd8Ian Rogers jmethodID mid = WellKnownClasses::java_lang_Thread_run; 141cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers AbstractMethod* m = 142cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(soa.DecodeMethod(mid)); 143365c10235438607541fa2259a5fec48061b90bd8Ian Rogers m->Invoke(self, receiver, NULL, NULL); 144365c10235438607541fa2259a5fec48061b90bd8Ian Rogers } 14500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Detach and delete self. 14600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Runtime::Current()->GetThreadList()->Unregister(self); 14793e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 148b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro return NULL; 149b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro} 150b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 15100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan RogersThread* Thread::FromManagedThread(const ScopedObjectAccessUnchecked& soa, Object* thread_peer) { 15200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Field* f = soa.DecodeField(WellKnownClasses::java_lang_Thread_vmData); 15300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Thread* result = reinterpret_cast<Thread*>(static_cast<uintptr_t>(f->GetInt(thread_peer))); 15400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Sanity check that if we have a result it is either suspended or we hold the thread_list_lock_ 15500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // to stop it from going away. 15681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers if (kIsDebugBuild) { 15781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers MutexLock mu(soa.Self(), *Locks::thread_suspend_count_lock_); 15881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers if (result != NULL && !result->IsSuspended()) { 15981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers Locks::thread_list_lock_->AssertHeld(soa.Self()); 16081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers } 16100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 16200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers return result; 163761928d24e4e7ed7776b52243eaf9095ad35f448Elliott Hughes} 164761928d24e4e7ed7776b52243eaf9095ad35f448Elliott Hughes 16500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan RogersThread* Thread::FromManagedThread(const ScopedObjectAccessUnchecked& soa, jobject java_thread) { 16600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers return FromManagedThread(soa, soa.Decode<Object*>(java_thread)); 16701158d7a57c8321370667a6045220237d16e0da8Elliott Hughes} 16801158d7a57c8321370667a6045220237d16e0da8Elliott Hughes 169ab7b9dcbfc4264a0bc4889c3e463ff88a67f6a30Elliott Hughesstatic size_t FixStackSize(size_t stack_size) { 1707502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // A stack size of zero means "use the default". 171d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (stack_size == 0) { 172d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes stack_size = Runtime::Current()->GetDefaultStackSize(); 173d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 17461e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 1756414a97a3c6dc101ae8ebc9480114d0c327e8e8dBrian Carlstrom // Dalvik used the bionic pthread default stack size for native threads, 1766414a97a3c6dc101ae8ebc9480114d0c327e8e8dBrian Carlstrom // so include that here to support apps that expect large native stacks. 1776414a97a3c6dc101ae8ebc9480114d0c327e8e8dBrian Carlstrom stack_size += 1 * MB; 1786414a97a3c6dc101ae8ebc9480114d0c327e8e8dBrian Carlstrom 1797502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // It's not possible to request a stack smaller than the system-defined PTHREAD_STACK_MIN. 1807502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes if (stack_size < PTHREAD_STACK_MIN) { 1817502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes stack_size = PTHREAD_STACK_MIN; 1827502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes } 1837502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 1847502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // It's likely that callers are trying to ensure they have at least a certain amount of 1857502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // stack space, so we should add our reserved space on top of what they requested, rather 1867502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // than implicitly take it away from them. 1877502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes stack_size += Thread::kStackOverflowReservedBytes; 1887502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 1897502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // Some systems require the stack size to be a multiple of the system page size, so round up. 1907502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes stack_size = RoundUp(stack_size, kPageSize); 1917502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 1927502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes return stack_size; 1937502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes} 1947502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 195d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughesstatic void SigAltStack(stack_t* new_stack, stack_t* old_stack) { 196d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes if (sigaltstack(new_stack, old_stack) == -1) { 197d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes PLOG(FATAL) << "sigaltstack failed"; 198d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes } 199d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes} 200d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes 201d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughesstatic void SetUpAlternateSignalStack() { 202d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes // Create and set an alternate signal stack. 203d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes stack_t ss; 204d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes ss.ss_sp = new uint8_t[SIGSTKSZ]; 205d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes ss.ss_size = SIGSTKSZ; 206d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes ss.ss_flags = 0; 207d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes CHECK(ss.ss_sp != NULL); 208d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes SigAltStack(&ss, NULL); 209d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes 210d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes // Double-check that it worked. 211d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes ss.ss_sp = NULL; 212d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes SigAltStack(NULL, &ss); 213d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes VLOG(threads) << "Alternate signal stack is " << PrettySize(ss.ss_size) << " at " << ss.ss_sp; 214d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes} 215d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes 216d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughesstatic void TearDownAlternateSignalStack() { 217d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes // Get the pointer so we can free the memory. 218d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes stack_t ss; 219d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes SigAltStack(NULL, &ss); 220d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes uint8_t* allocated_signal_stack = reinterpret_cast<uint8_t*>(ss.ss_sp); 221d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes 222d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes // Tell the kernel to stop using it. 223d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes ss.ss_sp = NULL; 224d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes ss.ss_flags = SS_DISABLE; 2254c5231d2cd83536584f7f1063d0afffa124fcec4Elliott Hughes ss.ss_size = SIGSTKSZ; // Avoid ENOMEM failure with Mac OS' buggy libc. 226d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes SigAltStack(&ss, NULL); 227d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes 228d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes // Free it. 229d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes delete[] allocated_signal_stack; 230d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes} 231d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes 232120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogersvoid Thread::CreateNativeThread(JNIEnv* env, jobject java_peer, size_t stack_size, bool is_daemon) { 233dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier CHECK(java_peer != NULL); 234120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers Thread* self = static_cast<JNIEnvExt*>(env)->self; 235120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers Runtime* runtime = Runtime::Current(); 236dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier 237120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers // Atomically start the birth of the thread ensuring the runtime isn't shutting down. 238120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers bool thread_start_during_shutdown = false; 239365c10235438607541fa2259a5fec48061b90bd8Ian Rogers { 240120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers MutexLock mu(self, *Locks::runtime_shutdown_lock_); 241120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers if (runtime->IsShuttingDown()) { 242120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers thread_start_during_shutdown = true; 243120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers } else { 244120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers runtime->StartThreadBirth(); 245120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers } 24600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 247120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers if (thread_start_during_shutdown) { 248120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers ScopedLocalRef<jclass> error_class(env, env->FindClass("java/lang/InternalError")); 249120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers env->ThrowNew(error_class.get(), "Thread starting during runtime shutdown"); 250120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers return; 251120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers } 252120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers 253120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers Thread* child_thread = new Thread(is_daemon); 254120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers // Use global JNI ref to hold peer live while child thread starts. 255cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers child_thread->jpeer_ = env->NewGlobalRef(java_peer); 256120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers stack_size = FixStackSize(stack_size); 257120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers 258120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers // Thread.start is synchronized, so we know that vmData is 0, and know that we're not racing to 259120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers // assign it. 260120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers env->SetIntField(java_peer, WellKnownClasses::java_lang_Thread_vmData, 261120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers reinterpret_cast<jint>(child_thread)); 26200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 26300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers pthread_t new_pthread; 26400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers pthread_attr_t attr; 26500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers CHECK_PTHREAD_CALL(pthread_attr_init, (&attr), "new thread"); 26600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers CHECK_PTHREAD_CALL(pthread_attr_setdetachstate, (&attr, PTHREAD_CREATE_DETACHED), "PTHREAD_CREATE_DETACHED"); 26700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers CHECK_PTHREAD_CALL(pthread_attr_setstacksize, (&attr, stack_size), stack_size); 268120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers int pthread_create_result = pthread_create(&new_pthread, &attr, Thread::CreateCallback, child_thread); 26900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attr), "new thread"); 27000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 271120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers if (pthread_create_result != 0) { 27200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // pthread_create(3) failed, so clean up. 2739efc3e03982f042c3ce67cc2c64dbe0f0986119bBrian Carlstrom { 274120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers MutexLock mu(self, *Locks::runtime_shutdown_lock_); 275120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers runtime->EndThreadBirth(); 276120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers } 277120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers // Manually delete the global reference since Thread::Init will not have been run. 278cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers env->DeleteGlobalRef(child_thread->jpeer_); 279cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers child_thread->jpeer_ = NULL; 280120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers delete child_thread; 281120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers child_thread = NULL; 282120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers // TODO: remove from thread group? 283120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers env->SetIntField(java_peer, WellKnownClasses::java_lang_Thread_vmData, 0); 284120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers { 2859efc3e03982f042c3ce67cc2c64dbe0f0986119bBrian Carlstrom std::string msg(StringPrintf("pthread_create (%s stack) failed: %s", 2869efc3e03982f042c3ce67cc2c64dbe0f0986119bBrian Carlstrom PrettySize(stack_size).c_str(), strerror(pthread_create_result))); 287120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers ScopedObjectAccess soa(env); 288120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers soa.Self()->ThrowOutOfMemoryError(msg.c_str()); 2899efc3e03982f042c3ce67cc2c64dbe0f0986119bBrian Carlstrom } 290365c10235438607541fa2259a5fec48061b90bd8Ian Rogers } 29193e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes} 29261e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 293120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogersvoid Thread::Init(ThreadList* thread_list, JavaVMExt* java_vm) { 294462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes // This function does all the initialization that must be run by the native thread it applies to. 295462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes // (When we create a new thread from managed code, we allocate the Thread* in Thread::Create so 296462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes // we can handshake with the corresponding native thread when it's ready.) Check this native 297462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes // thread hasn't been through here already... 298cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes CHECK(Thread::Current() == NULL); 299d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes SetUpAlternateSignalStack(); 30093e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes InitCpu(); 30193e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes InitFunctionPointers(); 3025d76c435082332ef79a22962386fa92a0870e378Ian Rogers InitCardTable(); 30301ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers InitTid(); 30450ffee20ced9c0c7ac68889c86be8844cf120cf2Ian Rogers if (Runtime::Current()->InterpreterOnly()) { 30550ffee20ced9c0c7ac68889c86be8844cf120cf2Ian Rogers AtomicSetFlag(kEnterInterpreter); 30650ffee20ced9c0c7ac68889c86be8844cf120cf2Ian Rogers } 307120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers // Set pthread_self_ ahead of pthread_setspecific, that makes Thread::Current function, this 308120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers // avoids pthread_self_ ever being invalid when discovered from Thread::Current(). 3090d39c12238499ca9ccc34d1532c443335e7c1044Elliott Hughes pthread_self_ = pthread_self(); 3106a607ad0902f3b8478e95d0b6b3e63a538571a3fElliott Hughes CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, this), "attach self"); 311120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers DCHECK_EQ(Thread::Current(), this); 312a5780dad67556297c8ca5f2608c53b193e6c4514Elliott Hughes 313cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers thin_lock_id_ = thread_list->AllocThreadId(this); 314120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers InitStackHwm(); 3155fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 316120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers jni_env_ = new JNIEnvExt(this, java_vm); 317120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers thread_list->Register(this); 31893e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes} 31993e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 320664bebf92eb2151b9b570ccd42ac4b6056c3ea9cMathieu ChartierThread* Thread::Attach(const char* thread_name, bool as_daemon, jobject thread_group, 321664bebf92eb2151b9b570ccd42ac4b6056c3ea9cMathieu Chartier bool create_peer) { 322120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers Thread* self; 323120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers Runtime* runtime = Runtime::Current(); 324120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers if (runtime == NULL) { 325120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers LOG(ERROR) << "Thread attaching to non-existent runtime: " << thread_name; 326120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers return NULL; 327120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers } 328120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers { 329120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers MutexLock mu(NULL, *Locks::runtime_shutdown_lock_); 330120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers if (runtime->IsShuttingDown()) { 331120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers LOG(ERROR) << "Thread attaching while runtime is shutting down: " << thread_name; 332120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers return NULL; 333120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers } else { 334120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers Runtime::Current()->StartThreadBirth(); 335120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers self = new Thread(as_daemon); 336120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers self->Init(runtime->GetThreadList(), runtime->GetJavaVM()); 337120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers Runtime::Current()->EndThreadBirth(); 338120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers } 339120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers } 3405fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 341dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier CHECK_NE(self->GetState(), kRunnable); 342dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier self->SetState(kNative); 343726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes 344cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes // If we're the main thread, ClassLinker won't be created until after we're attached, 345cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes // so that thread needs a two-stage attach. Regular threads don't need this hack. 346d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes // In the compiler, all threads need this hack, because no-one's going to be getting 347d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes // a native peer! 348664bebf92eb2151b9b570ccd42ac4b6056c3ea9cMathieu Chartier if (create_peer) { 349462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes self->CreatePeer(thread_name, as_daemon, thread_group); 35006e3ad4a651c2c58dba5e865cd06d2f98462bf1dElliott Hughes } else { 35106e3ad4a651c2c58dba5e865cd06d2f98462bf1dElliott Hughes // These aren't necessary, but they improve diagnostics for unit tests & command-line tools. 35222869a9026a08b544eca4cefd67386d347e30d2cElliott Hughes if (thread_name != NULL) { 35322869a9026a08b544eca4cefd67386d347e30d2cElliott Hughes self->name_->assign(thread_name); 35422869a9026a08b544eca4cefd67386d347e30d2cElliott Hughes ::art::SetThreadName(thread_name); 35522869a9026a08b544eca4cefd67386d347e30d2cElliott Hughes } 35636e0a955e48e9357bff1eab783e493107ebfff62Ian Rogers } 357cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes 3585fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes return self; 3595fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes} 3605fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 361365c10235438607541fa2259a5fec48061b90bd8Ian Rogersvoid Thread::CreatePeer(const char* name, bool as_daemon, jobject thread_group) { 362365c10235438607541fa2259a5fec48061b90bd8Ian Rogers Runtime* runtime = Runtime::Current(); 363365c10235438607541fa2259a5fec48061b90bd8Ian Rogers CHECK(runtime->IsStarted()); 3645fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes JNIEnv* env = jni_env_; 3655fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 366462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes if (thread_group == NULL) { 367365c10235438607541fa2259a5fec48061b90bd8Ian Rogers thread_group = runtime->GetMainThreadGroup(); 368462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes } 369726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes ScopedLocalRef<jobject> thread_name(env, env->NewStringUTF(name)); 3708daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes jint thread_priority = GetNativePriority(); 3715fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes jboolean thread_is_daemon = as_daemon; 3725fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 373eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes ScopedLocalRef<jobject> peer(env, env->AllocObject(WellKnownClasses::java_lang_Thread)); 374dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier if (peer.get() == NULL) { 375dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier CHECK(IsExceptionPending()); 376dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier return; 3775d4bdc29737a693027daaf6ed3f0792368eb4baeIan Rogers } 378cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers { 379cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers ScopedObjectAccess soa(this); 380cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers opeer_ = soa.Decode<Object*>(peer.get()); 381cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers } 382eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes env->CallNonvirtualVoidMethod(peer.get(), 383eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes WellKnownClasses::java_lang_Thread, 384eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes WellKnownClasses::java_lang_Thread_init, 385365c10235438607541fa2259a5fec48061b90bd8Ian Rogers thread_group, thread_name.get(), thread_priority, thread_is_daemon); 38600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers AssertNoPendingException(); 387d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 388120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers Thread* self = this; 389120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers DCHECK_EQ(self, Thread::Current()); 390120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers jni_env_->SetIntField(peer.get(), WellKnownClasses::java_lang_Thread_vmData, 391120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers reinterpret_cast<jint>(self)); 392120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers 393120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers ScopedObjectAccess soa(self); 3941f5393447b9f45be7918042d9ee7b521376de866Ian Rogers SirtRef<String> peer_thread_name(soa.Self(), GetThreadName(soa)); 39500fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom if (peer_thread_name.get() == NULL) { 39600fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom // The Thread constructor should have set the Thread.name to a 39700fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom // non-null value. However, because we can run without code 39800fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom // available (in the compiler, in tests), we manually assign the 39900fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom // fields the constructor should have set. 400dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier soa.DecodeField(WellKnownClasses::java_lang_Thread_daemon)-> 401cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers SetBoolean(opeer_, thread_is_daemon); 402dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier soa.DecodeField(WellKnownClasses::java_lang_Thread_group)-> 403cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers SetObject(opeer_, soa.Decode<Object*>(thread_group)); 404dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier soa.DecodeField(WellKnownClasses::java_lang_Thread_name)-> 405cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers SetObject(opeer_, soa.Decode<Object*>(thread_name.get())); 406dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier soa.DecodeField(WellKnownClasses::java_lang_Thread_priority)-> 407cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers SetInt(opeer_, thread_priority); 40800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers peer_thread_name.reset(GetThreadName(soa)); 40900fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom } 410225f5a1df25241babd16cdba54056b9e2cd166a2Elliott Hughes // 'thread_name' may have been null, so don't trust 'peer_thread_name' to be non-null. 41100fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom if (peer_thread_name.get() != NULL) { 412899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes SetThreadName(peer_thread_name->ToModifiedUtf8().c_str()); 41300fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom } 41461e019d291583029c01b61b93bea750f2b663c37Carl Shapiro} 41561e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 416899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughesvoid Thread::SetThreadName(const char* name) { 417899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes name_->assign(name); 418899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes ::art::SetThreadName(name); 419899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes Dbg::DdmSendThreadNotification(this, CHUNK_TYPE("THNM")); 420899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes} 421899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes 422be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughesvoid Thread::InitStackHwm() { 423e188419b971936086a188843378375f5ced13724Elliott Hughes void* stack_base; 424e188419b971936086a188843378375f5ced13724Elliott Hughes size_t stack_size; 425120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers GetThreadStack(pthread_self_, stack_base, stack_size); 42636ecb789775eb5bd284ce5dd35d2e31e42354f24Elliott Hughes 42736ecb789775eb5bd284ce5dd35d2e31e42354f24Elliott Hughes // TODO: include this in the thread dumps; potentially useful in SIGQUIT output? 428e188419b971936086a188843378375f5ced13724Elliott Hughes VLOG(threads) << StringPrintf("Native stack is at %p (%s)", stack_base, PrettySize(stack_size).c_str()); 429e188419b971936086a188843378375f5ced13724Elliott Hughes 430e188419b971936086a188843378375f5ced13724Elliott Hughes stack_begin_ = reinterpret_cast<byte*>(stack_base); 431e188419b971936086a188843378375f5ced13724Elliott Hughes stack_size_ = stack_size; 43236ecb789775eb5bd284ce5dd35d2e31e42354f24Elliott Hughes 433932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers if (stack_size_ <= kStackOverflowReservedBytes) { 4343d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes LOG(FATAL) << "Attempt to attach a thread with a too-small stack (" << stack_size_ << " bytes)"; 435be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes } 436449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes 437e188419b971936086a188843378375f5ced13724Elliott Hughes // TODO: move this into the Linux GetThreadStack implementation. 438e188419b971936086a188843378375f5ced13724Elliott Hughes#if !defined(__APPLE__) 43936ecb789775eb5bd284ce5dd35d2e31e42354f24Elliott Hughes // If we're the main thread, check whether we were run with an unlimited stack. In that case, 44036ecb789775eb5bd284ce5dd35d2e31e42354f24Elliott Hughes // glibc will have reported a 2GB stack for our 32-bit process, and our stack overflow detection 44136ecb789775eb5bd284ce5dd35d2e31e42354f24Elliott Hughes // will be broken because we'll die long before we get close to 2GB. 442120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers bool is_main_thread = (::art::GetTid() == getpid()); 443120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers if (is_main_thread) { 44436ecb789775eb5bd284ce5dd35d2e31e42354f24Elliott Hughes rlimit stack_limit; 44536ecb789775eb5bd284ce5dd35d2e31e42354f24Elliott Hughes if (getrlimit(RLIMIT_STACK, &stack_limit) == -1) { 44636ecb789775eb5bd284ce5dd35d2e31e42354f24Elliott Hughes PLOG(FATAL) << "getrlimit(RLIMIT_STACK) failed"; 44736ecb789775eb5bd284ce5dd35d2e31e42354f24Elliott Hughes } 44836ecb789775eb5bd284ce5dd35d2e31e42354f24Elliott Hughes if (stack_limit.rlim_cur == RLIM_INFINITY) { 44936ecb789775eb5bd284ce5dd35d2e31e42354f24Elliott Hughes // Find the default stack size for new threads... 45036ecb789775eb5bd284ce5dd35d2e31e42354f24Elliott Hughes pthread_attr_t default_attributes; 45136ecb789775eb5bd284ce5dd35d2e31e42354f24Elliott Hughes size_t default_stack_size; 45236ecb789775eb5bd284ce5dd35d2e31e42354f24Elliott Hughes CHECK_PTHREAD_CALL(pthread_attr_init, (&default_attributes), "default stack size query"); 45336ecb789775eb5bd284ce5dd35d2e31e42354f24Elliott Hughes CHECK_PTHREAD_CALL(pthread_attr_getstacksize, (&default_attributes, &default_stack_size), 45436ecb789775eb5bd284ce5dd35d2e31e42354f24Elliott Hughes "default stack size query"); 45536ecb789775eb5bd284ce5dd35d2e31e42354f24Elliott Hughes CHECK_PTHREAD_CALL(pthread_attr_destroy, (&default_attributes), "default stack size query"); 45636ecb789775eb5bd284ce5dd35d2e31e42354f24Elliott Hughes 45736ecb789775eb5bd284ce5dd35d2e31e42354f24Elliott Hughes // ...and use that as our limit. 45836ecb789775eb5bd284ce5dd35d2e31e42354f24Elliott Hughes size_t old_stack_size = stack_size_; 45936ecb789775eb5bd284ce5dd35d2e31e42354f24Elliott Hughes stack_size_ = default_stack_size; 46036ecb789775eb5bd284ce5dd35d2e31e42354f24Elliott Hughes stack_begin_ += (old_stack_size - stack_size_); 461faf4ba01738400f418b45c4323aa9e8e6b6ba2a0Elliott Hughes VLOG(threads) << "Limiting unlimited stack (reported as " << PrettySize(old_stack_size) << ")" 462faf4ba01738400f418b45c4323aa9e8e6b6ba2a0Elliott Hughes << " to " << PrettySize(stack_size_) 463faf4ba01738400f418b45c4323aa9e8e6b6ba2a0Elliott Hughes << " with base " << reinterpret_cast<void*>(stack_begin_); 46436ecb789775eb5bd284ce5dd35d2e31e42354f24Elliott Hughes } 46536ecb789775eb5bd284ce5dd35d2e31e42354f24Elliott Hughes } 466e188419b971936086a188843378375f5ced13724Elliott Hughes#endif 46736ecb789775eb5bd284ce5dd35d2e31e42354f24Elliott Hughes 468932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers // Set stack_end_ to the bottom of the stack saving space of stack overflows 469932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers ResetDefaultStackEnd(); 470449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes 471449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes // Sanity check. 472449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes int stack_variable; 473398f64b5805246765b699839b439e18c0dfbf2eeElliott Hughes CHECK_GT(&stack_variable, reinterpret_cast<void*>(stack_end_)); 474be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes} 475be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 47600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersvoid Thread::ShortDump(std::ostream& os) const { 47700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers os << "Thread["; 47800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers if (GetThinLockId() != 0) { 47900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // If we're in kStarting, we won't have a thin lock id or tid yet. 48000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers os << GetThinLockId() 48100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers << ",tid=" << GetTid() << ','; 482e0918556e7551de638870dcad3f2023f94f85a50Elliott Hughes } 483474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers os << GetState() 48400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers << ",Thread*=" << this 485cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers << ",peer=" << opeer_ 48600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers << ",\"" << *name_ << "\"" 48700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers << "]"; 488d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes} 489d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 49000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersvoid Thread::Dump(std::ostream& os) const { 49100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers DumpState(os); 49200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers DumpStack(os); 49300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers} 49400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 49500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan RogersString* Thread::GetThreadName(const ScopedObjectAccessUnchecked& soa) const { 49600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Field* f = soa.DecodeField(WellKnownClasses::java_lang_Thread_name); 497cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers return (opeer_ != NULL) ? reinterpret_cast<String*>(f->GetObject(opeer_)) : NULL; 498fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes} 499fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes 500ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughesvoid Thread::GetThreadName(std::string& name) const { 501ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes name.assign(*name_); 502ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes} 503ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes 504474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogersvoid Thread::AtomicSetFlag(ThreadFlag flag) { 50530e173f6da202e4caa37ceb477e2fbbe09cec990Ian Rogers android_atomic_or(flag, &state_and_flags_.as_int); 506474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers} 507474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers 508474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogersvoid Thread::AtomicClearFlag(ThreadFlag flag) { 50930e173f6da202e4caa37ceb477e2fbbe09cec990Ian Rogers android_atomic_and(-1 ^ flag, &state_and_flags_.as_int); 510474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers} 511474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers 512474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian RogersThreadState Thread::SetState(ThreadState new_state) { 513474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers // Cannot use this code to change into Runnable as changing to Runnable should fail if 514474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers // old_state_and_flags.suspend_request is true. 515474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers DCHECK_NE(new_state, kRunnable); 516474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers DCHECK_EQ(this, Thread::Current()); 51730e173f6da202e4caa37ceb477e2fbbe09cec990Ian Rogers union StateAndFlags old_state_and_flags = state_and_flags_; 51830e173f6da202e4caa37ceb477e2fbbe09cec990Ian Rogers state_and_flags_.as_struct.state = new_state; 51930e173f6da202e4caa37ceb477e2fbbe09cec990Ian Rogers return static_cast<ThreadState>(old_state_and_flags.as_struct.state); 520474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers} 521474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers 52201ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers// Attempt to rectify locks so that we dump thread list with required locks before exiting. 52301ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogersstatic void UnsafeLogFatalForSuspendCount(Thread* self, Thread* thread) NO_THREAD_SAFETY_ANALYSIS { 524120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers LOG(ERROR) << *thread << " suspend count already zero."; 52501ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers Locks::thread_suspend_count_lock_->Unlock(self); 52601ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers if (!Locks::mutator_lock_->IsSharedHeld(self)) { 52701ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers Locks::mutator_lock_->SharedTryLock(self); 52801ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers if (!Locks::mutator_lock_->IsSharedHeld(self)) { 52901ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers LOG(WARNING) << "Dumping thread list without holding mutator_lock_"; 53001ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers } 53101ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers } 53201ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers if (!Locks::thread_list_lock_->IsExclusiveHeld(self)) { 53301ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers Locks::thread_list_lock_->TryLock(self); 53401ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers if (!Locks::thread_list_lock_->IsExclusiveHeld(self)) { 53501ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers LOG(WARNING) << "Dumping thread list without holding thread_list_lock_"; 53601ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers } 53701ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers } 53801ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers std::ostringstream ss; 53901ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers Runtime::Current()->GetThreadList()->DumpLocked(ss); 540120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers LOG(FATAL) << ss.str(); 54101ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers} 54201ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers 54301ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogersvoid Thread::ModifySuspendCount(Thread* self, int delta, bool for_debugger) { 54400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers DCHECK(delta == -1 || delta == +1 || delta == -debug_suspend_count_) 54500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers << delta << " " << debug_suspend_count_ << " " << this; 54600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers DCHECK_GE(suspend_count_, debug_suspend_count_) << this; 54701ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers Locks::thread_suspend_count_lock_->AssertHeld(self); 548cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers if (this != self && !IsSuspended()) { 549cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers Locks::thread_list_lock_->AssertHeld(self); 550cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers } 55101ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers if (UNLIKELY(delta < 0 && suspend_count_ <= 0)) { 55201ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers UnsafeLogFatalForSuspendCount(self, this); 55300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers return; 55400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 55501ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers 55600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers suspend_count_ += delta; 55700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers if (for_debugger) { 55800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers debug_suspend_count_ += delta; 55900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 56001ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers 561474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers if (suspend_count_ == 0) { 562474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers AtomicClearFlag(kSuspendRequest); 563474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers } else { 564474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers AtomicSetFlag(kSuspendRequest); 565474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers } 56600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers} 56700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 5680e4627e593bc39f8e3d89c31f8977d55054c07ccMathieu Chartierbool Thread::RequestCheckpoint(Closure* function) { 569858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier CHECK(!ReadFlag(kCheckpointRequest)) << "Already have a pending checkpoint request"; 570858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier checkpoint_function_ = function; 571858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier union StateAndFlags old_state_and_flags = state_and_flags_; 572858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier // We must be runnable to request a checkpoint. 573858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier old_state_and_flags.as_struct.state = kRunnable; 574858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier union StateAndFlags new_state_and_flags = old_state_and_flags; 575858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier new_state_and_flags.as_struct.flags |= kCheckpointRequest; 576858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier int succeeded = android_atomic_cmpxchg(old_state_and_flags.as_int, new_state_and_flags.as_int, 577858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier &state_and_flags_.as_int); 578858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier return succeeded == 0; 579858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier} 580858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier 58100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersvoid Thread::FullSuspendCheck() { 58200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers VLOG(threads) << this << " self-suspending"; 58300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Make thread appear suspended to other threads, release mutator_lock_. 58400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers TransitionFromRunnableToSuspended(kSuspended); 58500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Transition back to runnable noting requests to suspend, re-acquire share on mutator_lock_. 58600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers TransitionFromSuspendedToRunnable(); 58700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers VLOG(threads) << this << " self-reviving"; 58800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers} 58900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 59000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersvoid Thread::TransitionFromRunnableToSuspended(ThreadState new_state) { 59100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers AssertThreadSuspensionIsAllowable(); 592474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers DCHECK_NE(new_state, kRunnable); 593474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers DCHECK_EQ(this, Thread::Current()); 594c747cffa950b8398cb8844d47e2253214c855750Ian Rogers // Change to non-runnable state, thereby appearing suspended to the system. 595474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers DCHECK_EQ(GetState(), kRunnable); 596858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier union StateAndFlags old_state_and_flags; 597858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier union StateAndFlags new_state_and_flags; 598858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier do { 599858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier old_state_and_flags = state_and_flags_; 600858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier // Copy over flags and try to clear the checkpoint bit if it is set. 601858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier new_state_and_flags.as_struct.flags = old_state_and_flags.as_struct.flags & ~kCheckpointRequest; 602858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier new_state_and_flags.as_struct.state = new_state; 603858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier } while (android_atomic_cmpxchg(old_state_and_flags.as_int, new_state_and_flags.as_int, 604858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier &state_and_flags_.as_int) != 0); 605858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier // If we toggled the checkpoint flag we must have cleared it. 606858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier uint16_t flag_change = new_state_and_flags.as_struct.flags ^ old_state_and_flags.as_struct.flags; 607858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier if ((flag_change & kCheckpointRequest) != 0) { 608858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier RunCheckpointFunction(); 609858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier } 61000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Release share on mutator_lock_. 61181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers Locks::mutator_lock_->SharedUnlock(this); 61200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers} 61300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 61400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan RogersThreadState Thread::TransitionFromSuspendedToRunnable() { 61500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers bool done = false; 61601ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers union StateAndFlags old_state_and_flags = state_and_flags_; 61701ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers int16_t old_state = old_state_and_flags.as_struct.state; 61801ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers DCHECK_NE(static_cast<ThreadState>(old_state), kRunnable); 61900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers do { 62081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers Locks::mutator_lock_->AssertNotHeld(this); // Otherwise we starve GC.. 62101ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers old_state_and_flags = state_and_flags_; 62201ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers DCHECK_EQ(old_state_and_flags.as_struct.state, old_state); 62301ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers if ((old_state_and_flags.as_struct.flags & kSuspendRequest) != 0) { 62400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Wait while our suspend count is non-zero. 62581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers MutexLock mu(this, *Locks::thread_suspend_count_lock_); 62601ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers old_state_and_flags = state_and_flags_; 62701ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers DCHECK_EQ(old_state_and_flags.as_struct.state, old_state); 62801ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers while ((old_state_and_flags.as_struct.flags & kSuspendRequest) != 0) { 62900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Re-check when Thread::resume_cond_ is notified. 630c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers Thread::resume_cond_->Wait(this); 63101ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers old_state_and_flags = state_and_flags_; 63201ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers DCHECK_EQ(old_state_and_flags.as_struct.state, old_state); 63300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 634474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers DCHECK_EQ(GetSuspendCount(), 0); 63500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 63600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Re-acquire shared mutator_lock_ access. 63781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers Locks::mutator_lock_->SharedLock(this); 638474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers // Atomically change from suspended to runnable if no suspend request pending. 63901ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers old_state_and_flags = state_and_flags_; 64001ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers DCHECK_EQ(old_state_and_flags.as_struct.state, old_state); 64101ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers if ((old_state_and_flags.as_struct.flags & kSuspendRequest) == 0) { 64201ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers union StateAndFlags new_state_and_flags = old_state_and_flags; 64301ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers new_state_and_flags.as_struct.state = kRunnable; 64401ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers done = android_atomic_cmpxchg(old_state_and_flags.as_int, new_state_and_flags.as_int, 64581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers &state_and_flags_.as_int) 646474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers == 0; 647474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers } 648474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers if (!done) { 649474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers // Failed to transition to Runnable. Release shared mutator_lock_ access and try again. 65081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers Locks::mutator_lock_->SharedUnlock(this); 65100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 65200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } while (!done); 65301ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers return static_cast<ThreadState>(old_state); 65400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers} 65500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 65600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan RogersThread* Thread::SuspendForDebugger(jobject peer, bool request_suspension, bool* timeout) { 65700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers static const useconds_t kTimeoutUs = 30 * 1000000; // 30s. 65800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers useconds_t total_delay_us = 0; 65900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers useconds_t delay_us = 0; 66000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers bool did_suspend_request = false; 66100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers *timeout = false; 66200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers while (true) { 66300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Thread* thread; 66400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers { 66500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ScopedObjectAccess soa(Thread::Current()); 66681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers MutexLock mu(soa.Self(), *Locks::thread_list_lock_); 66700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers thread = Thread::FromManagedThread(soa, peer); 66800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers if (thread == NULL) { 66900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers LOG(WARNING) << "No such thread for suspend: " << peer; 67000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers return NULL; 67100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 67200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers { 67381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers MutexLock mu(soa.Self(), *Locks::thread_suspend_count_lock_); 67400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers if (request_suspension) { 67501ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers thread->ModifySuspendCount(soa.Self(), +1, true /* for_debugger */); 67600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers request_suspension = false; 67700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers did_suspend_request = true; 67800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 67900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // IsSuspended on the current thread will fail as the current thread is changed into 68000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Runnable above. As the suspend count is now raised if this is the current thread 68100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // it will self suspend on transition to Runnable, making it hard to work with. Its simpler 68200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // to just explicitly handle the current thread in the callers to this code. 68300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers CHECK_NE(thread, soa.Self()) << "Attempt to suspend for debugger the current thread"; 68400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // If thread is suspended (perhaps it was already not Runnable but didn't have a suspend 68500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // count, or else we've waited and it has self suspended) or is the current thread, we're 68600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // done. 68700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers if (thread->IsSuspended()) { 68800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers return thread; 68900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 69000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers if (total_delay_us >= kTimeoutUs) { 69100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers LOG(ERROR) << "Thread suspension timed out: " << peer; 69200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers if (did_suspend_request) { 69301ae5808367e641a983e3f8bb82b3e0d364cd03eIan Rogers thread->ModifySuspendCount(soa.Self(), -1, true /* for_debugger */); 69400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 69500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers *timeout = true; 69600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers return NULL; 69700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 69800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 69900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Release locks and come out of runnable state. 70000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 70100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers for (int i = kMaxMutexLevel; i >= 0; --i) { 70281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers BaseMutex* held_mutex = Thread::Current()->GetHeldMutex(static_cast<LockLevel>(i)); 70300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers if (held_mutex != NULL) { 70400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers LOG(FATAL) << "Holding " << held_mutex->GetName() 70500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers << " while sleeping for thread suspension"; 70600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 70700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 70800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers { 70900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers useconds_t new_delay_us = delay_us * 2; 71000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers CHECK_GE(new_delay_us, delay_us); 71100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers if (new_delay_us < 500000) { // Don't allow sleeping to be more than 0.5s. 71200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers delay_us = new_delay_us; 71300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 71400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 71500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers if (delay_us == 0) { 71600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers sched_yield(); 71700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Default to 1 milliseconds (note that this gets multiplied by 2 before the first sleep). 71800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers delay_us = 500; 71900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } else { 72000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers usleep(delay_us); 72100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers total_delay_us += delay_us; 72200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 72300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 72400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers} 72500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 726abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughesvoid Thread::DumpState(std::ostream& os, const Thread* thread, pid_t tid) { 727d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes std::string group_name; 728d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes int priority; 729d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes bool is_daemon = false; 73081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers Thread* self = Thread::Current(); 731d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 732cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers if (thread != NULL && thread->opeer_ != NULL) { 733cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers ScopedObjectAccessUnchecked soa(self); 734cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers priority = soa.DecodeField(WellKnownClasses::java_lang_Thread_priority)->GetInt(thread->opeer_); 735cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers is_daemon = soa.DecodeField(WellKnownClasses::java_lang_Thread_daemon)->GetBoolean(thread->opeer_); 736d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 737120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers Object* thread_group = 738cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers soa.DecodeField(WellKnownClasses::java_lang_Thread_group)->GetObject(thread->opeer_); 739120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers 740d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (thread_group != NULL) { 74100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Field* group_name_field = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_name); 742af8d15a3267343dec135cc6df1db740c0a5c7b52Elliott Hughes String* group_name_string = reinterpret_cast<String*>(group_name_field->GetObject(thread_group)); 743d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes group_name = (group_name_string != NULL) ? group_name_string->ToModifiedUtf8() : "<null>"; 744d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 745d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } else { 746d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes priority = GetNativePriority(); 747dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes } 748d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 749abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes std::string scheduler_group_name(GetSchedulerGroupName(tid)); 7501bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughes if (scheduler_group_name.empty()) { 7511bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughes scheduler_group_name = "default"; 752d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 753d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 754abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes if (thread != NULL) { 755abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes os << '"' << *thread->name_ << '"'; 756abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes if (is_daemon) { 757abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes os << " daemon"; 758abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes } 759abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes os << " prio=" << priority 760abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes << " tid=" << thread->GetThinLockId() 761abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes << " " << thread->GetState() << "\n"; 762abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes } else { 763289be85116aaf7c48413858b5d0448868b4e61f3Elliott Hughes os << '"' << ::art::GetThreadName(tid) << '"' 764abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes << " prio=" << priority 765abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes << " (not attached)\n"; 766abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes } 767abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes 768abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes if (thread != NULL) { 76981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers MutexLock mu(self, *Locks::thread_suspend_count_lock_); 770abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes os << " | group=\"" << group_name << "\"" 771abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes << " sCount=" << thread->suspend_count_ 772abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes << " dsCount=" << thread->debug_suspend_count_ 773cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers << " obj=" << reinterpret_cast<void*>(thread->opeer_) 774abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes << " self=" << reinterpret_cast<const void*>(thread) << "\n"; 775d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 7760d39c12238499ca9ccc34d1532c443335e7c1044Elliott Hughes 777abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes os << " | sysTid=" << tid 778abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes << " nice=" << getpriority(PRIO_PROCESS, tid) 7790d39c12238499ca9ccc34d1532c443335e7c1044Elliott Hughes << " cgrp=" << scheduler_group_name; 7800d39c12238499ca9ccc34d1532c443335e7c1044Elliott Hughes if (thread != NULL) { 7810d39c12238499ca9ccc34d1532c443335e7c1044Elliott Hughes int policy; 7820d39c12238499ca9ccc34d1532c443335e7c1044Elliott Hughes sched_param sp; 7830d39c12238499ca9ccc34d1532c443335e7c1044Elliott Hughes CHECK_PTHREAD_CALL(pthread_getschedparam, (thread->pthread_self_, &policy, &sp), __FUNCTION__); 7840d39c12238499ca9ccc34d1532c443335e7c1044Elliott Hughes os << " sched=" << policy << "/" << sp.sched_priority 7850d39c12238499ca9ccc34d1532c443335e7c1044Elliott Hughes << " handle=" << reinterpret_cast<void*>(thread->pthread_self_); 7860d39c12238499ca9ccc34d1532c443335e7c1044Elliott Hughes } 7870d39c12238499ca9ccc34d1532c443335e7c1044Elliott Hughes os << "\n"; 788d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 789d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes // Grab the scheduler stats for this thread. 790d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes std::string scheduler_stats; 791abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes if (ReadFileToString(StringPrintf("/proc/self/task/%d/schedstat", tid), &scheduler_stats)) { 792d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes scheduler_stats.resize(scheduler_stats.size() - 1); // Lose the trailing '\n'. 793d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } else { 794d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes scheduler_stats = "0 0 0"; 795d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 796d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 797ba0b9c55adce3f533845ab1b25c552589e5b4118Elliott Hughes char native_thread_state = '?'; 798d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes int utime = 0; 799d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes int stime = 0; 800d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes int task_cpu = 0; 801ba0b9c55adce3f533845ab1b25c552589e5b4118Elliott Hughes GetTaskStats(tid, native_thread_state, utime, stime, task_cpu); 802d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 803ba0b9c55adce3f533845ab1b25c552589e5b4118Elliott Hughes os << " | state=" << native_thread_state 804ba0b9c55adce3f533845ab1b25c552589e5b4118Elliott Hughes << " schedstat=( " << scheduler_stats << " )" 805d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " utm=" << utime 806d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " stm=" << stime 807abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes << " core=" << task_cpu 808abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes << " HZ=" << sysconf(_SC_CLK_TCK) << "\n"; 809abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes if (thread != NULL) { 810abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes os << " | stack=" << reinterpret_cast<void*>(thread->stack_begin_) << "-" << reinterpret_cast<void*>(thread->stack_end_) 811abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes << " stackSize=" << PrettySize(thread->stack_size_) << "\n"; 812abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes } 813abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes} 814abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes 815abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughesvoid Thread::DumpState(std::ostream& os) const { 816abbe07d095547ded03c2e9d0d53943d43471278dElliott Hughes Thread::DumpState(os, this, GetTid()); 817a09576416788b916095739e43a16917e7948f3a4Elliott Hughes} 818a09576416788b916095739e43a16917e7948f3a4Elliott Hughes 8190399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogersstruct StackDumpVisitor : public StackVisitor { 82008fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes StackDumpVisitor(std::ostream& os, const Thread* thread, Context* context, bool can_allocate) 821b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) 822725a957985171d712d5c048cc3d00ff14968784bjeffhao : StackVisitor(thread->GetManagedStack(), thread->GetInstrumentationStack(), context), 82308fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes os(os), thread(thread), can_allocate(can_allocate), 82408fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes last_method(NULL), last_line_number(0), repetition_count(0), frame_count(0) { 825d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 826d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 827bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers virtual ~StackDumpVisitor() { 828e85d2e924953f1a13b7152edba1c13853e6f3a86Elliott Hughes if (frame_count == 0) { 829e85d2e924953f1a13b7152edba1c13853e6f3a86Elliott Hughes os << " (no managed stack frames)\n"; 830e85d2e924953f1a13b7152edba1c13853e6f3a86Elliott Hughes } 831d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 832d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 833b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 83466f19258f9728d4ffe026074d8fd429d639802faMathieu Chartier AbstractMethod* m = GetMethod(); 8350399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers if (m->IsRuntimeMethod()) { 836530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes return true; 8379086572fa809d1a19d886b467e4da3ce42016982Ian Rogers } 83828ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers const int kMaxRepetition = 3; 839d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes Class* c = m->GetDeclaringClass(); 840b861dc0077342d5a1d2dd9cade3f6990620778ecIan Rogers const DexCache* dex_cache = c->GetDexCache(); 841b861dc0077342d5a1d2dd9cade3f6990620778ecIan Rogers int line_number = -1; 842b861dc0077342d5a1d2dd9cade3f6990620778ecIan Rogers if (dex_cache != NULL) { // be tolerant of bad input 8434445a7e3398a6143939168097a3aa275b734504dIan Rogers const DexFile& dex_file = *dex_cache->GetDexFile(); 8440399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers line_number = dex_file.GetLineNumFromPC(m, GetDexPc()); 845b861dc0077342d5a1d2dd9cade3f6990620778ecIan Rogers } 84628ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers if (line_number == last_line_number && last_method == m) { 84728ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers repetition_count++; 848d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } else { 84928ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers if (repetition_count >= kMaxRepetition) { 85028ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers os << " ... repeated " << (repetition_count - kMaxRepetition) << " times\n"; 85128ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers } 85228ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers repetition_count = 0; 85328ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers last_line_number = line_number; 85428ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers last_method = m; 85528ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers } 85628ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers if (repetition_count < kMaxRepetition) { 85728ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers os << " at " << PrettyMethod(m, false); 85828ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers if (m->IsNative()) { 85928ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers os << "(Native method)"; 86028ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers } else { 8616d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers mh.ChangeMethod(m); 8626d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers const char* source_file(mh.GetDeclaringClassSourceFile()); 8636d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers os << "(" << (source_file != NULL ? source_file : "unavailable") 86428ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers << ":" << line_number << ")"; 86528ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers } 86628ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers os << "\n"; 86708fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes if (frame_count == 0) { 86808fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes Monitor::DescribeWait(os, thread); 86908fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes } 87008fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes if (can_allocate) { 87108fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes Monitor::DescribeLocks(os, this); 87208fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes } 873d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 8748e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 87508fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes ++frame_count; 876530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes return true; 877d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 87808fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes std::ostream& os; 87908fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes const Thread* thread; 88008fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes bool can_allocate; 8816d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers MethodHelper mh; 88266f19258f9728d4ffe026074d8fd429d639802faMathieu Chartier AbstractMethod* last_method; 88328ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers int last_line_number; 88428ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers int repetition_count; 8858e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes int frame_count; 886d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes}; 887d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 888d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughesvoid Thread::DumpStack(std::ostream& os) const { 889ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes // If we're currently in native code, dump that stack before dumping the managed stack. 890dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier if (GetState() == kNative) { 89146e251bf7200cc06f5a9a82ee2030e650f5e1443Elliott Hughes DumpKernelStack(os, GetTid(), " kernel: ", false); 89246e251bf7200cc06f5a9a82ee2030e650f5e1443Elliott Hughes DumpNativeStack(os, GetTid(), " native: ", false); 893ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes } 89408fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes UniquePtr<Context> context(Context::Create()); 89508fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes StackDumpVisitor dumper(os, this, context.get(), !throwing_OutOfMemoryError_); 8960399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers dumper.WalkStack(); 897e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes} 898e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes 899be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughesvoid Thread::ThreadExitCallback(void* arg) { 900be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes Thread* self = reinterpret_cast<Thread*>(arg); 9016a607ad0902f3b8478e95d0b6b3e63a538571a3fElliott Hughes if (self->thread_exit_check_count_ == 0) { 9026a607ad0902f3b8478e95d0b6b3e63a538571a3fElliott Hughes LOG(WARNING) << "Native thread exiting without having called DetachCurrentThread (maybe it's going to use a pthread_key_create destructor?): " << *self; 9036a607ad0902f3b8478e95d0b6b3e63a538571a3fElliott Hughes CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, self), "reattach self"); 9046a607ad0902f3b8478e95d0b6b3e63a538571a3fElliott Hughes self->thread_exit_check_count_ = 1; 9056a607ad0902f3b8478e95d0b6b3e63a538571a3fElliott Hughes } else { 9066a607ad0902f3b8478e95d0b6b3e63a538571a3fElliott Hughes LOG(FATAL) << "Native thread exited without calling DetachCurrentThread: " << *self; 9076a607ad0902f3b8478e95d0b6b3e63a538571a3fElliott Hughes } 908b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro} 909b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 910be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughesvoid Thread::Startup() { 91100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers { 91250b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers MutexLock mu(Thread::Current(), *Locks::thread_suspend_count_lock_); // Keep GCC happy. 913c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers resume_cond_ = new ConditionVariable("Thread resumption condition variable", 914c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers *Locks::thread_suspend_count_lock_); 91500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 91600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 917b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro // Allocate a TLS slot. 9188d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes CHECK_PTHREAD_CALL(pthread_key_create, (&Thread::pthread_key_self_, Thread::ThreadExitCallback), "self key"); 919b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 920b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro // Double-check the TLS slot allocation. 921b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro if (pthread_getspecific(pthread_key_self_) != NULL) { 9223d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes LOG(FATAL) << "Newly-created pthread TLS slot is not NULL"; 923b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro } 924038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes} 925038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes 926038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesvoid Thread::FinishStartup() { 927365c10235438607541fa2259a5fec48061b90bd8Ian Rogers Runtime* runtime = Runtime::Current(); 928365c10235438607541fa2259a5fec48061b90bd8Ian Rogers CHECK(runtime->IsStarted()); 929b82b6878fb000d4731063b1bf15c39ff7c50b61fBrian Carlstrom 93001158d7a57c8321370667a6045220237d16e0da8Elliott Hughes // Finish attaching the main thread. 93100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ScopedObjectAccess soa(Thread::Current()); 932365c10235438607541fa2259a5fec48061b90bd8Ian Rogers Thread::Current()->CreatePeer("main", false, runtime->GetMainThreadGroup()); 9339a6bae896a2f003d7216603bf29771d105c10ca4Jesse Wilson 934af8d15a3267343dec135cc6df1db740c0a5c7b52Elliott Hughes Runtime::Current()->GetClassLinker()->RunRootClinits(); 935b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro} 936b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 937c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughesvoid Thread::Shutdown() { 9388d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes CHECK_PTHREAD_CALL(pthread_key_delete, (Thread::pthread_key_self_), "self key"); 939c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes} 940c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes 94152673ffae0025d86f4023735581f19ebcc477487Ian RogersThread::Thread(bool daemon) 9420399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers : suspend_count_(0), 9430399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers card_table_(NULL), 9440399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers exception_(NULL), 9450399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers stack_end_(NULL), 9460399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers managed_stack_(), 9470399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers jni_env_(NULL), 9480399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers self_(NULL), 949cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers opeer_(NULL), 950cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers jpeer_(NULL), 9510399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers stack_begin_(NULL), 9520399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers stack_size_(0), 9530399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers thin_lock_id_(0), 9540399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers tid_(0), 955e62934d85fbc2d935afdad57eeade39ecbd7440aElliott Hughes wait_mutex_(new Mutex("a thread wait mutex")), 956c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers wait_cond_(new ConditionVariable("a thread wait condition variable", *wait_mutex_)), 9578daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes wait_monitor_(NULL), 9588daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes interrupted_(false), 959dc33ad5db2dc6ed9b76d5219888626a604debbe1Elliott Hughes wait_next_(NULL), 9608e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes monitor_enter_object_(NULL), 961dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes top_sirt_(NULL), 962dc33ad5db2dc6ed9b76d5219888626a604debbe1Elliott Hughes runtime_(NULL), 96385d1545e985ac689db4bad7849880e843707c862Elliott Hughes class_loader_override_(NULL), 964418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughes long_jump_context_(NULL), 965726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes throwing_OutOfMemoryError_(false), 9660399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers debug_suspend_count_(0), 967e343b76af81a005ef64f5e75a555389fd9147dabjeffhao debug_invoke_req_(new DebugInvokeReq), 968725a957985171d712d5c048cc3d00ff14968784bjeffhao instrumentation_stack_(new std::vector<InstrumentationStackFrame>), 9690399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers name_(new std::string(kThreadNameDuringStartup)), 97052673ffae0025d86f4023735581f19ebcc477487Ian Rogers daemon_(daemon), 97100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers pthread_self_(0), 97252673ffae0025d86f4023735581f19ebcc477487Ian Rogers no_thread_suspension_(0), 9736a607ad0902f3b8478e95d0b6b3e63a538571a3fElliott Hughes last_no_thread_suspension_cause_(NULL), 974858f1c5fd5e528d0b16040ced74d4636046a42d8Mathieu Chartier checkpoint_function_(0), 9756a607ad0902f3b8478e95d0b6b3e63a538571a3fElliott Hughes thread_exit_check_count_(0) { 976f5a7a476e7ea63e094ff0f011dccc170607e6f6bElliott Hughes CHECK_EQ((sizeof(Thread) % 4), 0U) << sizeof(Thread); 97730e173f6da202e4caa37ceb477e2fbbe09cec990Ian Rogers state_and_flags_.as_struct.flags = 0; 97830e173f6da202e4caa37ceb477e2fbbe09cec990Ian Rogers state_and_flags_.as_struct.state = kNative; 979ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes memset(&held_mutexes_[0], 0, sizeof(held_mutexes_)); 980dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes} 981dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes 9827dc5166ea740359d381097a7ab382c1dd404055fElliott Hughesbool Thread::IsStillStarting() const { 9837dc5166ea740359d381097a7ab382c1dd404055fElliott Hughes // You might think you can check whether the state is kStarting, but for much of thread startup, 9847dc5166ea740359d381097a7ab382c1dd404055fElliott Hughes // the thread might also be in kVmWait. 9857dc5166ea740359d381097a7ab382c1dd404055fElliott Hughes // You might think you can check whether the peer is NULL, but the peer is actually created and 9867dc5166ea740359d381097a7ab382c1dd404055fElliott Hughes // assigned fairly early on, and needs to be. 9877dc5166ea740359d381097a7ab382c1dd404055fElliott Hughes // It turns out that the last thing to change is the thread name; that's a good proxy for "has 9887dc5166ea740359d381097a7ab382c1dd404055fElliott Hughes // this thread _ever_ entered kRunnable". 9897dc5166ea740359d381097a7ab382c1dd404055fElliott Hughes return (*name_ == kThreadNameDuringStartup); 9907dc5166ea740359d381097a7ab382c1dd404055fElliott Hughes} 9917dc5166ea740359d381097a7ab382c1dd404055fElliott Hughes 99200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersvoid Thread::AssertNoPendingException() const { 99300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers if (UNLIKELY(IsExceptionPending())) { 99400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ScopedObjectAccess soa(Thread::Current()); 99500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Throwable* exception = GetException(); 99600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers LOG(FATAL) << "No pending exception expected: " << exception->Dump(); 99700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 99800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers} 99900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 100000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstatic void MonitorExitVisitor(const Object* object, void* arg) NO_THREAD_SAFETY_ANALYSIS { 100100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Thread* self = reinterpret_cast<Thread*>(arg); 100202b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes Object* entered_monitor = const_cast<Object*>(object); 100300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers if (self->HoldsLock(entered_monitor)) { 100400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers LOG(WARNING) << "Calling MonitorExit on object " 100500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers << object << " (" << PrettyTypeOf(object) << ")" 100600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers << " left locked by native thread " 100700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers << *Thread::Current() << " which is detaching"; 100800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers entered_monitor->MonitorExit(self); 100900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 101002b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes} 101102b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 1012c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughesvoid Thread::Destroy() { 1013120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers Thread* self = this; 1014120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers DCHECK_EQ(self, Thread::Current()); 101502b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 1016cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers if (opeer_ != NULL) { 1017cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers ScopedObjectAccess soa(self); 1018120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers // We may need to call user-supplied managed code, do this before final clean-up. 1019cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers HandleUncaughtExceptions(soa); 1020cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers RemoveFromThreadGroup(soa); 1021534da072d3db3beecbd74e5aa23d630d73695f2cElliott Hughes 102229f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // this.vmData = 0; 1023cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers soa.DecodeField(WellKnownClasses::java_lang_Thread_vmData)->SetInt(opeer_, 0); 1024cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers Dbg::PostThreadDeath(self); 102502b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 1026cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers // Thread.join() is implemented as an Object.wait() on the Thread.lock object. Signal anyone 1027cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers // who is waiting. 1028cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers Object* lock = soa.DecodeField(WellKnownClasses::java_lang_Thread_lock)->GetObject(opeer_); 1029038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes // (This conditional is only needed for tests, where Thread.lock won't have been set.) 1030cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers if (lock != NULL) { 1031cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers lock->MonitorEnter(self); 1032cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers lock->Notify(); 1033cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers lock->MonitorExit(self); 10345f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 10355f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 1036120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers 1037120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers // On thread detach, all monitors entered with JNI MonitorEnter are automatically exited. 1038120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers if (jni_env_ != NULL) { 1039120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers jni_env_->monitors.VisitRoots(MonitorExitVisitor, self); 1040120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers } 1041c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes} 104202b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 1043c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott HughesThread::~Thread() { 1044cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers if (jni_env_ != NULL && jpeer_ != NULL) { 1045dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier // If pthread_create fails we don't have a jni env here. 1046cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers jni_env_->DeleteGlobalRef(jpeer_); 1047cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers jpeer_ = NULL; 1048dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier } 1049cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers opeer_ = NULL; 1050dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier 1051c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes delete jni_env_; 105202b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes jni_env_ = NULL; 105302b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 1054dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier CHECK_NE(GetState(), kRunnable); 1055dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier // We may be deleting a still born thread. 1056dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier SetStateUnsafe(kTerminated); 105785d1545e985ac689db4bad7849880e843707c862Elliott Hughes 105885d1545e985ac689db4bad7849880e843707c862Elliott Hughes delete wait_cond_; 105985d1545e985ac689db4bad7849880e843707c862Elliott Hughes delete wait_mutex_; 106085d1545e985ac689db4bad7849880e843707c862Elliott Hughes 1061776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#if !defined(ART_USE_LLVM_COMPILER) 106285d1545e985ac689db4bad7849880e843707c862Elliott Hughes delete long_jump_context_; 1063776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#endif 1064475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes 1065475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes delete debug_invoke_req_; 1066725a957985171d712d5c048cc3d00ff14968784bjeffhao delete instrumentation_stack_; 1067899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes delete name_; 1068d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes 1069d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes TearDownAlternateSignalStack(); 1070c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes} 1071c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes 1072cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogersvoid Thread::HandleUncaughtExceptions(ScopedObjectAccess& soa) { 1073accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes if (!IsExceptionPending()) { 1074accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes return; 1075accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes } 1076cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers ScopedLocalRef<jobject> peer(jni_env_, soa.AddLocalReference<jobject>(opeer_)); 1077cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers ScopedThreadStateChange tsc(this, kNative); 1078120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers 1079accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes // Get and clear the exception. 1080120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers ScopedLocalRef<jthrowable> exception(jni_env_, jni_env_->ExceptionOccurred()); 1081120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers jni_env_->ExceptionClear(); 1082accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 1083accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes // If the thread has its own handler, use that. 1084120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers ScopedLocalRef<jobject> handler(jni_env_, 1085cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers jni_env_->GetObjectField(peer.get(), 1086120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers WellKnownClasses::java_lang_Thread_uncaughtHandler)); 1087120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers if (handler.get() == NULL) { 1088accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes // Otherwise use the thread group's default handler. 1089cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers handler.reset(jni_env_->GetObjectField(peer.get(), WellKnownClasses::java_lang_Thread_group)); 1090accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes } 1091accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 1092accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes // Call the handler. 1093120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers jni_env_->CallVoidMethod(handler.get(), 1094120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers WellKnownClasses::java_lang_Thread$UncaughtExceptionHandler_uncaughtException, 1095cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers peer.get(), exception.get()); 1096accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 1097accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes // If the handler threw, clear that exception too. 1098120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers jni_env_->ExceptionClear(); 1099a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes} 1100a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes 1101cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogersvoid Thread::RemoveFromThreadGroup(ScopedObjectAccess& soa) { 11024514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom // this.group.removeThread(this); 11034514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom // group can be null if we're in the compiler or a test. 1104cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers Object* ogroup = soa.DecodeField(WellKnownClasses::java_lang_Thread_group)->GetObject(opeer_); 1105cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers if (ogroup != NULL) { 1106cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers ScopedLocalRef<jobject> group(soa.Env(), soa.AddLocalReference<jobject>(ogroup)); 1107cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers ScopedLocalRef<jobject> peer(soa.Env(), soa.AddLocalReference<jobject>(opeer_)); 1108cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers ScopedThreadStateChange tsc(soa.Self(), kNative); 1109120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers jni_env_->CallVoidMethod(group.get(), WellKnownClasses::java_lang_ThreadGroup_removeThread, 1110cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers peer.get()); 11114514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom } 11124514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom} 1113accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 1114408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogerssize_t Thread::NumSirtReferences() { 1115a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers size_t count = 0; 111640381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->GetLink()) { 1117a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers count += cur->NumberOfReferences(); 1118a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers } 1119a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers return count; 1120a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers} 1121a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers 1122cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogersbool Thread::SirtContains(jobject obj) const { 1123408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers Object** sirt_entry = reinterpret_cast<Object**>(obj); 112440381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->GetLink()) { 112540381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom if (cur->Contains(sirt_entry)) { 1126a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers return true; 1127a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers } 1128a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers } 11290399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers // JNI code invoked from portable code uses shadow frames rather than the SIRT. 11300399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers return managed_stack_.ShadowFramesContain(sirt_entry); 113128f1a147d77ec772db98bed890b50a9ddcff2365TDYa} 113228f1a147d77ec772db98bed890b50a9ddcff2365TDYa 11338dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liaovoid Thread::SirtVisitRoots(Heap::RootVisitor* visitor, void* arg) { 113440381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->GetLink()) { 11358dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao size_t num_refs = cur->NumberOfReferences(); 11368dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao for (size_t j = 0; j < num_refs; j++) { 113740381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom Object* object = cur->GetReference(j); 11385e73f9c8990819054f8e0a60061ee8beff1e024aBrian Carlstrom if (object != NULL) { 11395e73f9c8990819054f8e0a60061ee8beff1e024aBrian Carlstrom visitor(object, arg); 11405e73f9c8990819054f8e0a60061ee8beff1e024aBrian Carlstrom } 11418dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao } 11428dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao } 11438dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao} 11448dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao 1145cfaa455374aae0a08c8cb28b5bb306b17866d652Ian RogersObject* Thread::DecodeJObject(jobject obj) const { 114681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers Locks::mutator_lock_->AssertSharedHeld(this); 1147408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers if (obj == NULL) { 1148408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers return NULL; 1149408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 1150408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers IndirectRef ref = reinterpret_cast<IndirectRef>(obj); 1151408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers IndirectRefKind kind = GetIndirectRefKind(ref); 1152408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers Object* result; 1153408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers switch (kind) { 1154408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers case kLocal: 1155408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers { 115669f5bc6759f256a146eefd8a7141d39fcc3b0421Elliott Hughes IndirectReferenceTable& locals = jni_env_->locals; 1157cf4c6c41b0084dc4567ff709fb8ce9ebd72b26acElliott Hughes result = const_cast<Object*>(locals.Get(ref)); 1158408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers break; 1159408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 1160408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers case kGlobal: 1161408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers { 1162408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers JavaVMExt* vm = Runtime::Current()->GetJavaVM(); 1163408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers IndirectReferenceTable& globals = vm->globals; 1164cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers MutexLock mu(const_cast<Thread*>(this), vm->globals_lock); 1165cf4c6c41b0084dc4567ff709fb8ce9ebd72b26acElliott Hughes result = const_cast<Object*>(globals.Get(ref)); 1166408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers break; 1167408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 1168408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers case kWeakGlobal: 1169408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers { 1170408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers JavaVMExt* vm = Runtime::Current()->GetJavaVM(); 1171408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers IndirectReferenceTable& weak_globals = vm->weak_globals; 1172cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers MutexLock mu(const_cast<Thread*>(this), vm->weak_globals_lock); 1173cf4c6c41b0084dc4567ff709fb8ce9ebd72b26acElliott Hughes result = const_cast<Object*>(weak_globals.Get(ref)); 1174408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers if (result == kClearedJniWeakGlobal) { 1175408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers // This is a special case where it's okay to return NULL. 1176408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers return NULL; 1177408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 1178408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers break; 1179408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 1180408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers case kSirtOrInvalid: 1181408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers default: 1182408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers // TODO: make stack indirect reference table lookup more efficient 1183408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers // Check if this is a local reference in the SIRT 11840399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers if (SirtContains(obj)) { 11850cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers result = *reinterpret_cast<Object**>(obj); // Read from SIRT 1186c2dc62d1c9241dfe880a81698713c314b13aa63fElliott Hughes } else if (Runtime::Current()->GetJavaVM()->work_around_app_jni_bugs) { 1187408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers // Assume an invalid local reference is actually a direct pointer. 1188408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers result = reinterpret_cast<Object*>(obj); 1189408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } else { 1190a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes result = kInvalidIndirectRefObject; 1191408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 1192408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 1193408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers 1194408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers if (result == NULL) { 11953f6635a2c84d645002f651c312d53f8348646c1fElliott Hughes JniAbortF(NULL, "use of deleted %s %p", ToStr<IndirectRefKind>(kind).c_str(), obj); 1196a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes } else { 1197a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes if (result != kInvalidIndirectRefObject) { 1198b3bd5f07884f5a1f2b84224363b1372d7c28d447Elliott Hughes Runtime::Current()->GetHeap()->VerifyObject(result); 1199a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes } 1200408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 1201408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers return result; 1202408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers} 1203408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers 120481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers// Implements java.lang.Thread.interrupted. 120581d425b0b232962441616f8b14f73620bffef5e5Ian Rogersbool Thread::Interrupted() { 120650b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers MutexLock mu(Thread::Current(), *wait_mutex_); 120781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers bool interrupted = interrupted_; 120881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers interrupted_ = false; 120981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers return interrupted; 121081d425b0b232962441616f8b14f73620bffef5e5Ian Rogers} 121181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers 121281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers// Implements java.lang.Thread.isInterrupted. 121381d425b0b232962441616f8b14f73620bffef5e5Ian Rogersbool Thread::IsInterrupted() { 121450b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers MutexLock mu(Thread::Current(), *wait_mutex_); 121581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers return interrupted_; 121681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers} 121781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers 121881d425b0b232962441616f8b14f73620bffef5e5Ian Rogersvoid Thread::Interrupt() { 1219c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers Thread* self = Thread::Current(); 1220c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers MutexLock mu(self, *wait_mutex_); 122181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers if (interrupted_) { 122281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers return; 122381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers } 122481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers interrupted_ = true; 1225c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers NotifyLocked(self); 122681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers} 122781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers 122881d425b0b232962441616f8b14f73620bffef5e5Ian Rogersvoid Thread::Notify() { 1229c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers Thread* self = Thread::Current(); 1230c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers MutexLock mu(self, *wait_mutex_); 1231c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers NotifyLocked(self); 123281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers} 123381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers 1234c604d731730b43231f63040c8db1d58304da0cf3Ian Rogersvoid Thread::NotifyLocked(Thread* self) { 123581d425b0b232962441616f8b14f73620bffef5e5Ian Rogers if (wait_monitor_ != NULL) { 1236c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers wait_cond_->Signal(self); 123781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers } 123881d425b0b232962441616f8b14f73620bffef5e5Ian Rogers} 123981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers 12400399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogersclass CountStackDepthVisitor : public StackVisitor { 12419b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao public: 12420399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers CountStackDepthVisitor(const ManagedStack* stack, 1243725a957985171d712d5c048cc3d00ff14968784bjeffhao const std::vector<InstrumentationStackFrame>* instrumentation_stack) 1244b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) 1245725a957985171d712d5c048cc3d00ff14968784bjeffhao : StackVisitor(stack, instrumentation_stack, NULL), 124608fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes depth_(0), skip_depth_(0), skipping_(true) {} 1247d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 1248b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 124929f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // We want to skip frames up to and including the exception's constructor. 12509086572fa809d1a19d886b467e4da3ce42016982Ian Rogers // Note we also skip the frame if it doesn't have a method (namely the callee 12519086572fa809d1a19d886b467e4da3ce42016982Ian Rogers // save frame) 125266f19258f9728d4ffe026074d8fd429d639802faMathieu Chartier AbstractMethod* m = GetMethod(); 12530399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers if (skipping_ && !m->IsRuntimeMethod() && 12540399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers !Throwable::GetJavaLangThrowable()->IsAssignableFrom(m->GetDeclaringClass())) { 125529f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes skipping_ = false; 125629f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } 125729f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes if (!skipping_) { 12580399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers if (!m->IsRuntimeMethod()) { // Ignore runtime frames (in particular callee save). 12596b0870dfaaa2e69a900ad7ed88e4e1d3697445aaIan Rogers ++depth_; 12606b0870dfaaa2e69a900ad7ed88e4e1d3697445aaIan Rogers } 126129f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } else { 126229f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes ++skip_depth_; 126329f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } 1264530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes return true; 12659b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 12669b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 12679b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao int GetDepth() const { 1268aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers return depth_; 12699b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 12709b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 127129f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes int GetSkipDepth() const { 127229f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes return skip_depth_; 127329f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } 127429f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes 12759b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao private: 1276aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers uint32_t depth_; 127729f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes uint32_t skip_depth_; 127829f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes bool skipping_; 12799b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao}; 12809b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 12810399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogersclass BuildInternalStackTraceVisitor : public StackVisitor { 12829b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao public: 12831f5393447b9f45be7918042d9ee7b521376de866Ian Rogers explicit BuildInternalStackTraceVisitor(Thread* self, const ManagedStack* stack, 1284725a957985171d712d5c048cc3d00ff14968784bjeffhao const std::vector<InstrumentationStackFrame>* instrumentation_stack, 1285ca190666fb11820153f74274c495ba1f913d8a69Ian Rogers int skip_depth) 1286725a957985171d712d5c048cc3d00ff14968784bjeffhao : StackVisitor(stack, instrumentation_stack, NULL), self_(self), 128708fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes skip_depth_(skip_depth), count_(0), dex_pc_trace_(NULL), method_trace_(NULL) {} 1288283ed0d9db3084d0fee46d47cd73bf84bd0cfc16Ian Rogers 12891f5393447b9f45be7918042d9ee7b521376de866Ian Rogers bool Init(int depth) 1290b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1291aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Allocate method trace with an extra slot that will hold the PC trace 12920399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers SirtRef<ObjectArray<Object> > 12931f5393447b9f45be7918042d9ee7b521376de866Ian Rogers method_trace(self_, 129450b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers Runtime::Current()->GetClassLinker()->AllocObjectArray<Object>(self_, 129550b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers depth + 1)); 12960399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers if (method_trace.get() == NULL) { 1297283ed0d9db3084d0fee46d47cd73bf84bd0cfc16Ian Rogers return false; 1298726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes } 129950b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers IntArray* dex_pc_trace = IntArray::Alloc(self_, depth); 13000399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers if (dex_pc_trace == NULL) { 1301283ed0d9db3084d0fee46d47cd73bf84bd0cfc16Ian Rogers return false; 1302726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes } 1303aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Save PC trace in last element of method trace, also places it into the 1304aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // object graph. 13050399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers method_trace->Set(depth, dex_pc_trace); 13060399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers // Set the Object*s and assert that no thread suspension is now possible. 130752673ffae0025d86f4023735581f19ebcc477487Ian Rogers const char* last_no_suspend_cause = 13081f5393447b9f45be7918042d9ee7b521376de866Ian Rogers self_->StartAssertNoThreadSuspension("Building internal stack trace"); 130952673ffae0025d86f4023735581f19ebcc477487Ian Rogers CHECK(last_no_suspend_cause == NULL) << last_no_suspend_cause; 13100399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers method_trace_ = method_trace.get(); 13110399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers dex_pc_trace_ = dex_pc_trace; 1312283ed0d9db3084d0fee46d47cd73bf84bd0cfc16Ian Rogers return true; 13139b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 13149b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 13150399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers virtual ~BuildInternalStackTraceVisitor() { 131652673ffae0025d86f4023735581f19ebcc477487Ian Rogers if (method_trace_ != NULL) { 13171f5393447b9f45be7918042d9ee7b521376de866Ian Rogers self_->EndAssertNoThreadSuspension(NULL); 131852673ffae0025d86f4023735581f19ebcc477487Ian Rogers } 13190399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers } 13209b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 1321b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 13220399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers if (method_trace_ == NULL || dex_pc_trace_ == NULL) { 1323530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes return true; // We're probably trying to fillInStackTrace for an OutOfMemoryError. 1324726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes } 132529f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes if (skip_depth_ > 0) { 132629f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes skip_depth_--; 1327530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes return true; 132829f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } 132966f19258f9728d4ffe026074d8fd429d639802faMathieu Chartier AbstractMethod* m = GetMethod(); 13300399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers if (m->IsRuntimeMethod()) { 13310399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers return true; // Ignore runtime frames (in particular callee save). 13326b0870dfaaa2e69a900ad7ed88e4e1d3697445aaIan Rogers } 13330399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers method_trace_->Set(count_, m); 13340399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers dex_pc_trace_->Set(count_, GetDexPc()); 1335aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers ++count_; 1336530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes return true; 13379b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 13389b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 13390399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers ObjectArray<Object>* GetInternalStackTrace() const { 13400399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers return method_trace_; 13419b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 13429b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 13439b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao private: 13441f5393447b9f45be7918042d9ee7b521376de866Ian Rogers Thread* const self_; 134529f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // How many more frames to skip. 134629f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes int32_t skip_depth_; 13470399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers // Current position down stack trace. 1348aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers uint32_t count_; 13490399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers // Array of dex PC values. 13500399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers IntArray* dex_pc_trace_; 13510399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers // An array of the methods on the stack, the last entry is a reference to the PC trace. 1352aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers ObjectArray<Object>* method_trace_; 13539b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao}; 13549b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 135564b6d145fa53b8dfb07a8fc2426af13f155d5a4dIan Rogersjobject Thread::CreateInternalStackTrace(const ScopedObjectAccessUnchecked& soa) const { 1356aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Compute depth of stack 1357725a957985171d712d5c048cc3d00ff14968784bjeffhao CountStackDepthVisitor count_visitor(GetManagedStack(), GetInstrumentationStack()); 13580399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers count_visitor.WalkStack(); 13599b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao int32_t depth = count_visitor.GetDepth(); 136029f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes int32_t skip_depth = count_visitor.GetSkipDepth(); 13614417536522fd2a9d8215d8672331984769c9520bShih-wei Liao 13621f5393447b9f45be7918042d9ee7b521376de866Ian Rogers // Build internal stack trace. 1363725a957985171d712d5c048cc3d00ff14968784bjeffhao BuildInternalStackTraceVisitor build_trace_visitor(soa.Self(), GetManagedStack(), GetInstrumentationStack(), 13640399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers skip_depth); 13651f5393447b9f45be7918042d9ee7b521376de866Ian Rogers if (!build_trace_visitor.Init(depth)) { 13661f5393447b9f45be7918042d9ee7b521376de866Ian Rogers return NULL; // Allocation failed. 1367283ed0d9db3084d0fee46d47cd73bf84bd0cfc16Ian Rogers } 13680399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers build_trace_visitor.WalkStack(); 136900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers return soa.AddLocalReference<jobjectArray>(build_trace_visitor.GetInternalStackTrace()); 1370aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers} 1371aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers 137201158d7a57c8321370667a6045220237d16e0da8Elliott HughesjobjectArray Thread::InternalStackTraceToStackTraceElementArray(JNIEnv* env, jobject internal, 137301158d7a57c8321370667a6045220237d16e0da8Elliott Hughes jobjectArray output_array, int* stack_depth) { 1374aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Transition into runnable state to work on Object*/Array* 137500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ScopedObjectAccess soa(env); 1376aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Decode the internal stack trace into the depth, method trace and PC trace 137700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ObjectArray<Object>* method_trace = soa.Decode<ObjectArray<Object>*>(internal); 13789074b99bfbd5419e340f138a018b673ce71f77e8Ian Rogers int32_t depth = method_trace->GetLength() - 1; 1379aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth)); 1380aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers 1381aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 1382aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers 138301158d7a57c8321370667a6045220237d16e0da8Elliott Hughes jobjectArray result; 138401158d7a57c8321370667a6045220237d16e0da8Elliott Hughes ObjectArray<StackTraceElement>* java_traces; 138501158d7a57c8321370667a6045220237d16e0da8Elliott Hughes if (output_array != NULL) { 138601158d7a57c8321370667a6045220237d16e0da8Elliott Hughes // Reuse the array we were given. 138701158d7a57c8321370667a6045220237d16e0da8Elliott Hughes result = output_array; 138800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers java_traces = soa.Decode<ObjectArray<StackTraceElement>*>(output_array); 138901158d7a57c8321370667a6045220237d16e0da8Elliott Hughes // ...adjusting the number of frames we'll write to not exceed the array length. 139001158d7a57c8321370667a6045220237d16e0da8Elliott Hughes depth = std::min(depth, java_traces->GetLength()); 139101158d7a57c8321370667a6045220237d16e0da8Elliott Hughes } else { 139201158d7a57c8321370667a6045220237d16e0da8Elliott Hughes // Create java_trace array and place in local reference table 139350b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers java_traces = class_linker->AllocStackTraceElementArray(soa.Self(), depth); 139430646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes if (java_traces == NULL) { 139530646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes return NULL; 139630646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes } 139700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers result = soa.AddLocalReference<jobjectArray>(java_traces); 139801158d7a57c8321370667a6045220237d16e0da8Elliott Hughes } 139901158d7a57c8321370667a6045220237d16e0da8Elliott Hughes 140001158d7a57c8321370667a6045220237d16e0da8Elliott Hughes if (stack_depth != NULL) { 140101158d7a57c8321370667a6045220237d16e0da8Elliott Hughes *stack_depth = depth; 140201158d7a57c8321370667a6045220237d16e0da8Elliott Hughes } 140355df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao 14046d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers MethodHelper mh; 14059b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao for (int32_t i = 0; i < depth; ++i) { 1406aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Prepare parameters for StackTraceElement(String cls, String method, String file, int line) 140766f19258f9728d4ffe026074d8fd429d639802faMathieu Chartier AbstractMethod* method = down_cast<AbstractMethod*>(method_trace->Get(i)); 14086d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers mh.ChangeMethod(method); 14090399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers uint32_t dex_pc = pc_trace->Get(i); 14100399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers int32_t line_number = mh.GetLineNumFromDexPC(dex_pc); 1411aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Allocate element, potentially triggering GC 141240381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom // TODO: reuse class_name_object via Class::name_? 1413486013193c94b4321414c28ce9251911e14a0599Ian Rogers const char* descriptor = mh.GetDeclaringClassDescriptor(); 1414486013193c94b4321414c28ce9251911e14a0599Ian Rogers CHECK(descriptor != NULL); 1415486013193c94b4321414c28ce9251911e14a0599Ian Rogers std::string class_name(PrettyDescriptor(descriptor)); 14161f5393447b9f45be7918042d9ee7b521376de866Ian Rogers SirtRef<String> class_name_object(soa.Self(), 141750b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers String::AllocFromModifiedUtf8(soa.Self(), 141850b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers class_name.c_str())); 141940381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom if (class_name_object.get() == NULL) { 142040381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom return NULL; 142140381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom } 1422486013193c94b4321414c28ce9251911e14a0599Ian Rogers const char* method_name = mh.GetName(); 1423486013193c94b4321414c28ce9251911e14a0599Ian Rogers CHECK(method_name != NULL); 142450b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers SirtRef<String> method_name_object(soa.Self(), String::AllocFromModifiedUtf8(soa.Self(), 142550b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers method_name)); 14266d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers if (method_name_object.get() == NULL) { 14276d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers return NULL; 14286d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers } 1429486013193c94b4321414c28ce9251911e14a0599Ian Rogers const char* source_file = mh.GetDeclaringClassSourceFile(); 143050b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers SirtRef<String> source_name_object(soa.Self(), String::AllocFromModifiedUtf8(soa.Self(), 143150b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers source_file)); 143250b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers StackTraceElement* obj = StackTraceElement::Alloc(soa.Self(), 143350b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers class_name_object.get(), 14346d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers method_name_object.get(), 14356d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers source_name_object.get(), 143640381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom line_number); 143730646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes if (obj == NULL) { 143830646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes return NULL; 143930646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes } 1440aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers#ifdef MOVING_GARBAGE_COLLECTOR 1441aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Re-read after potential GC 144200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers java_traces = Decode<ObjectArray<Object>*>(soa.Env(), result); 144300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers method_trace = down_cast<ObjectArray<Object>*>(Decode<Object*>(soa.Env(), internal)); 1444aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers pc_trace = down_cast<IntArray*>(method_trace->Get(depth)); 1445aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers#endif 144655df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao java_traces->Set(i, obj); 144755df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao } 1448aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers return result; 144955df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao} 145055df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao 14515cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughesvoid Thread::ThrowNewExceptionF(const char* exception_class_descriptor, const char* fmt, ...) { 1452a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes va_list args; 1453a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes va_start(args, fmt); 14544a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes ThrowNewExceptionV(exception_class_descriptor, fmt, args); 1455a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes va_end(args); 14564a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes} 14574a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes 14584a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughesvoid Thread::ThrowNewExceptionV(const char* exception_class_descriptor, const char* fmt, va_list ap) { 14594a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes std::string msg; 14604a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes StringAppendV(&msg, fmt, ap); 14615cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughes ThrowNewException(exception_class_descriptor, msg.c_str()); 14625cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughes} 146337f7a40f6789bb287f287a9af00777af9d6428eeElliott Hughes 14645cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughesvoid Thread::ThrowNewException(const char* exception_class_descriptor, const char* msg) { 146500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers AssertNoPendingException(); // Callers should either clear or call ThrowNewWrappedException. 1466a4f94740f9d66b21f4bcd3a225c434aa29cfa323Elliott Hughes ThrowNewWrappedException(exception_class_descriptor, msg); 1467a4f94740f9d66b21f4bcd3a225c434aa29cfa323Elliott Hughes} 1468a4f94740f9d66b21f4bcd3a225c434aa29cfa323Elliott Hughes 1469a4f94740f9d66b21f4bcd3a225c434aa29cfa323Elliott Hughesvoid Thread::ThrowNewWrappedException(const char* exception_class_descriptor, const char* msg) { 1470e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes // Convert "Ljava/lang/Exception;" into JNI-style "java/lang/Exception". 14710cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers CHECK_EQ('L', exception_class_descriptor[0]); 1472e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes std::string descriptor(exception_class_descriptor + 1); 14730cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers CHECK_EQ(';', descriptor[descriptor.length() - 1]); 1474e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes descriptor.erase(descriptor.length() - 1); 1475e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes 1476e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes JNIEnv* env = GetJniEnv(); 1477a4f94740f9d66b21f4bcd3a225c434aa29cfa323Elliott Hughes jobject cause = env->ExceptionOccurred(); 1478a4f94740f9d66b21f4bcd3a225c434aa29cfa323Elliott Hughes env->ExceptionClear(); 1479a4f94740f9d66b21f4bcd3a225c434aa29cfa323Elliott Hughes 1480726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes ScopedLocalRef<jclass> exception_class(env, env->FindClass(descriptor.c_str())); 148130646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes if (exception_class.get() == NULL) { 148230646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes LOG(ERROR) << "Couldn't throw new " << descriptor << " because JNI FindClass failed: " 148330646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes << PrettyTypeOf(GetException()); 148430646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes CHECK(IsExceptionPending()); 148530646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes return; 148630646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes } 1487ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom if (!Runtime::Current()->IsStarted()) { 1488ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom // Something is trying to throw an exception without a started 1489ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom // runtime, which is the common case in the compiler. We won't be 1490ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom // able to invoke the constructor of the exception, so use 1491ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom // AllocObject which will not invoke a constructor. 1492ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom ScopedLocalRef<jthrowable> exception( 1493ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom env, reinterpret_cast<jthrowable>(env->AllocObject(exception_class.get()))); 1494ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom if (exception.get() != NULL) { 149500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ScopedObjectAccessUnchecked soa(env); 149600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Throwable* t = reinterpret_cast<Throwable*>(soa.Self()->DecodeJObject(exception.get())); 149750b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers t->SetDetailMessage(String::AllocFromModifiedUtf8(soa.Self(), msg)); 149864b6d145fa53b8dfb07a8fc2426af13f155d5a4dIan Rogers if (cause != NULL) { 149964b6d145fa53b8dfb07a8fc2426af13f155d5a4dIan Rogers t->SetCause(soa.Decode<Throwable*>(cause)); 150064b6d145fa53b8dfb07a8fc2426af13f155d5a4dIan Rogers } 150100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers soa.Self()->SetException(t); 1502ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom } else { 1503ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom LOG(ERROR) << "Couldn't throw new " << descriptor << " because JNI AllocObject failed: " 1504ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom << PrettyTypeOf(GetException()); 1505ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom CHECK(IsExceptionPending()); 1506ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom } 1507ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom return; 1508ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom } 1509a4f94740f9d66b21f4bcd3a225c434aa29cfa323Elliott Hughes int rc = ::art::ThrowNewException(env, exception_class.get(), msg, cause); 151030646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes if (rc != JNI_OK) { 151130646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes LOG(ERROR) << "Couldn't throw new " << descriptor << " because JNI ThrowNew failed: " 151230646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes << PrettyTypeOf(GetException()); 151330646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes CHECK(IsExceptionPending()); 151430646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes } 1515a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes} 1516a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes 15172ced6a534157d5d963693346904389c19775d2daElliott Hughesvoid Thread::ThrowOutOfMemoryError(const char* msg) { 15182ced6a534157d5d963693346904389c19775d2daElliott Hughes LOG(ERROR) << StringPrintf("Throwing OutOfMemoryError \"%s\"%s", 15192ced6a534157d5d963693346904389c19775d2daElliott Hughes msg, (throwing_OutOfMemoryError_ ? " (recursive case)" : "")); 1520726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes if (!throwing_OutOfMemoryError_) { 1521726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes throwing_OutOfMemoryError_ = true; 152257aba86f29d7e795bf7e68c65cc464d2291b6af1Elliott Hughes ThrowNewException("Ljava/lang/OutOfMemoryError;", msg); 1523418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughes } else { 1524225f5a1df25241babd16cdba54056b9e2cd166a2Elliott Hughes Dump(LOG(ERROR)); // The pre-allocated OOME has no stack, so help out and log one. 1525225f5a1df25241babd16cdba54056b9e2cd166a2Elliott Hughes SetException(Runtime::Current()->GetPreAllocatedOutOfMemoryError()); 1526418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughes } 1527726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes throwing_OutOfMemoryError_ = false; 152879082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes} 152979082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes 1530498508c1187dc07d3eae5476784cde20f5224d93Elliott HughesThread* Thread::CurrentFromGdb() { 1531accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes return Thread::Current(); 1532accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes} 1533accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 1534accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughesvoid Thread::DumpFromGdb() const { 15356b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom std::ostringstream ss; 15366b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom Dump(ss); 1537955724179c6c739524f610023287f56b24dc31deElliott Hughes std::string str(ss.str()); 15386b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom // log to stderr for debugging command line processes 15396b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom std::cerr << str; 15406b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom#ifdef HAVE_ANDROID_OS 15416b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom // log to logcat for debugging frameworks processes 15426b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom LOG(INFO) << str; 15436b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom#endif 1544accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes} 1545accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 154698e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughesstruct EntryPointInfo { 154798e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes uint32_t offset; 154898e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes const char* name; 154998e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes}; 155098e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes#define ENTRY_POINT_INFO(x) { ENTRYPOINT_OFFSET(x), #x } 155198e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughesstatic const EntryPointInfo gThreadEntryPointInfo[] = { 155298e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pAllocArrayFromCode), 155398e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pAllocArrayFromCodeWithAccessCheck), 155498e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pAllocObjectFromCode), 155598e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pAllocObjectFromCodeWithAccessCheck), 155698e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pCheckAndAllocArrayFromCode), 155798e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pCheckAndAllocArrayFromCodeWithAccessCheck), 155898e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pInstanceofNonTrivialFromCode), 155998e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pCanPutArrayElementFromCode), 156098e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pCheckCastFromCode), 156198e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pDebugMe), 156298e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pUpdateDebuggerFromCode), 156398e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pInitializeStaticStorage), 156498e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pInitializeTypeAndVerifyAccessFromCode), 156598e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pInitializeTypeFromCode), 156698e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pResolveStringFromCode), 1567474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers ENTRY_POINT_INFO(pGetAndClearException), 156898e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pSet32Instance), 156998e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pSet32Static), 157098e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pSet64Instance), 157198e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pSet64Static), 157298e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pSetObjInstance), 157398e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pSetObjStatic), 157498e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pGet32Instance), 157598e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pGet32Static), 157698e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pGet64Instance), 157798e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pGet64Static), 157898e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pGetObjInstance), 157998e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pGetObjStatic), 158098e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pHandleFillArrayDataFromCode), 158198e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pFindNativeMethod), 158200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ENTRY_POINT_INFO(pJniMethodStart), 158300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ENTRY_POINT_INFO(pJniMethodStartSynchronized), 158400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ENTRY_POINT_INFO(pJniMethodEnd), 158500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ENTRY_POINT_INFO(pJniMethodEndSynchronized), 158600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ENTRY_POINT_INFO(pJniMethodEndWithReference), 158700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ENTRY_POINT_INFO(pJniMethodEndWithReferenceSynchronized), 158898e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pLockObjectFromCode), 158998e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pUnlockObjectFromCode), 159098e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pCmpgDouble), 159198e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pCmpgFloat), 159298e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pCmplDouble), 159398e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pCmplFloat), 159498e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pFmod), 15950183dd7297709ef0beb31e120fc34fdd12f61915Ian Rogers ENTRY_POINT_INFO(pSqrt), 159698e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pL2d), 159798e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pFmodf), 159898e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pL2f), 159998e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pD2iz), 160098e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pF2iz), 160198e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pIdivmod), 160298e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pD2l), 160398e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pF2l), 160498e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pLdiv), 160598e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pLdivmod), 160698e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pLmul), 160798e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pShlLong), 160898e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pShrLong), 160998e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pUshrLong), 161098e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pIndexOf), 161198e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pMemcmp16), 161298e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pStringCompareTo), 161398e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pMemcpy), 161498e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pUnresolvedDirectMethodTrampolineFromCode), 161598e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pInvokeDirectTrampolineWithAccessCheck), 161698e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pInvokeInterfaceTrampoline), 161798e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pInvokeInterfaceTrampolineWithAccessCheck), 161898e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pInvokeStaticTrampolineWithAccessCheck), 161998e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pInvokeSuperTrampolineWithAccessCheck), 162098e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pInvokeVirtualTrampolineWithAccessCheck), 162198e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pCheckSuspendFromCode), 162298e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pTestSuspendFromCode), 162398e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pDeliverException), 162498e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pThrowAbstractMethodErrorFromCode), 162598e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pThrowArrayBoundsFromCode), 162698e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pThrowDivZeroFromCode), 162798e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pThrowNoSuchMethodFromCode), 162898e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pThrowNullPointerFromCode), 162998e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes ENTRY_POINT_INFO(pThrowStackOverflowFromCode), 163098e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes}; 163198e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes#undef ENTRY_POINT_INFO 163298e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes 163328fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughesvoid Thread::DumpThreadOffset(std::ostream& os, uint32_t offset, size_t size_of_pointers) { 163428fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes CHECK_EQ(size_of_pointers, 4U); // TODO: support 64-bit targets. 163598e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes 163698e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes#define DO_THREAD_OFFSET(x) if (offset == static_cast<uint32_t>(OFFSETOF_VOLATILE_MEMBER(Thread, x))) { os << # x; return; } 1637474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers DO_THREAD_OFFSET(state_and_flags_); 163898e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes DO_THREAD_OFFSET(card_table_); 163998e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes DO_THREAD_OFFSET(exception_); 164007ec8e17c978e3bda0202693e6432b56349a5867Ian Rogers DO_THREAD_OFFSET(opeer_); 164198e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes DO_THREAD_OFFSET(jni_env_); 164298e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes DO_THREAD_OFFSET(self_); 164398e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes DO_THREAD_OFFSET(stack_end_); 164498e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes DO_THREAD_OFFSET(suspend_count_); 164598e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes DO_THREAD_OFFSET(thin_lock_id_); 16460399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers //DO_THREAD_OFFSET(top_of_managed_stack_); 16470399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers //DO_THREAD_OFFSET(top_of_managed_stack_pc_); 164898e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes DO_THREAD_OFFSET(top_sirt_); 164928fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes#undef DO_THREAD_OFFSET 165098e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes 165198e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes size_t entry_point_count = arraysize(gThreadEntryPointInfo); 165298e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes CHECK_EQ(entry_point_count * size_of_pointers, sizeof(EntryPoints)); 165398e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes uint32_t expected_offset = OFFSETOF_MEMBER(Thread, entrypoints_); 165498e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes for (size_t i = 0; i < entry_point_count; ++i) { 1655474b6da273c7ce6df50a4e51eb9929a77e1611c3Ian Rogers CHECK_EQ(gThreadEntryPointInfo[i].offset, expected_offset) << gThreadEntryPointInfo[i].name; 165698e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes expected_offset += size_of_pointers; 165798e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes if (gThreadEntryPointInfo[i].offset == offset) { 165898e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes os << gThreadEntryPointInfo[i].name; 165998e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes return; 166098e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes } 166198e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes } 166298e2017cac7ca554602480fcefabbbe630a4a39bElliott Hughes os << offset; 166328fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes} 166428fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes 16650399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogersstatic const bool kDebugExceptionDelivery = false; 16660399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogersclass CatchBlockStackVisitor : public StackVisitor { 1667bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers public: 16680399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers CatchBlockStackVisitor(Thread* self, Throwable* exception) 1669b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) 1670725a957985171d712d5c048cc3d00ff14968784bjeffhao : StackVisitor(self->GetManagedStack(), self->GetInstrumentationStack(), self->GetLongJumpContext()), 16710399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers self_(self), exception_(exception), to_find_(exception->GetClass()), throw_method_(NULL), 16720399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers throw_frame_id_(0), throw_dex_pc_(0), handler_quick_frame_(NULL), 16730399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers handler_quick_frame_pc_(0), handler_dex_pc_(0), native_method_count_(0), 167457b86d47b66322693a070185fadfb43cb9c12eabIan Rogers method_tracing_active_(Runtime::Current()->IsMethodTracingActive()) { 167552673ffae0025d86f4023735581f19ebcc477487Ian Rogers // Exception not in root sets, can't allow GC. 167652673ffae0025d86f4023735581f19ebcc477487Ian Rogers last_no_assert_suspension_cause_ = self->StartAssertNoThreadSuspension("Finding catch block"); 167752673ffae0025d86f4023735581f19ebcc477487Ian Rogers } 167852673ffae0025d86f4023735581f19ebcc477487Ian Rogers 167952673ffae0025d86f4023735581f19ebcc477487Ian Rogers ~CatchBlockStackVisitor() { 168052673ffae0025d86f4023735581f19ebcc477487Ian Rogers LOG(FATAL) << "UNREACHABLE"; // Expected to take long jump. 168167375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers } 1682bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 1683b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) 1684b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 168566f19258f9728d4ffe026074d8fd429d639802faMathieu Chartier AbstractMethod* method = GetMethod(); 1686530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes if (method == NULL) { 16870399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers // This is the upcall, we remember the frame and last pc so that we may long jump to them. 16880399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers handler_quick_frame_pc_ = GetCurrentQuickFramePc(); 16890399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers handler_quick_frame_ = GetCurrentQuickFrame(); 169057b86d47b66322693a070185fadfb43cb9c12eabIan Rogers return false; // End stack walk. 1691530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes } 1692530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes uint32_t dex_pc = DexFile::kDexNoIndex; 169357b86d47b66322693a070185fadfb43cb9c12eabIan Rogers if (method->IsRuntimeMethod()) { 1694530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes // ignore callee save method 169557b86d47b66322693a070185fadfb43cb9c12eabIan Rogers DCHECK(method->IsCalleeSaveMethod()); 1696530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes } else { 16970399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers if (throw_method_ == NULL) { 16980399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers throw_method_ = method; 16990399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers throw_frame_id_ = GetFrameId(); 17000399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers throw_dex_pc_ = GetDexPc(); 17010399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers } 17020399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers if (method->IsNative()) { 17030399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers native_method_count_++; 17040399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers } else { 1705725a957985171d712d5c048cc3d00ff14968784bjeffhao // Unwind stack when an exception occurs during instrumentation 1706725a957985171d712d5c048cc3d00ff14968784bjeffhao if (UNLIKELY(method_tracing_active_ && IsInstrumentationExitPc(GetCurrentQuickFramePc()))) { 1707725a957985171d712d5c048cc3d00ff14968784bjeffhao uintptr_t pc = InstrumentationMethodUnwindFromCode(Thread::Current()); 17080c7abda482f53db3d153c073d1c7a145f84e0626Ian Rogers dex_pc = method->ToDexPc(pc); 17090399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers } else { 17100399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers dex_pc = GetDexPc(); 1711530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes } 1712bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers } 1713530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes } 1714530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes if (dex_pc != DexFile::kDexNoIndex) { 1715530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes uint32_t found_dex_pc = method->FindCatchBlock(to_find_, dex_pc); 1716530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes if (found_dex_pc != DexFile::kDexNoIndex) { 17170399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers handler_dex_pc_ = found_dex_pc; 17180c7abda482f53db3d153c073d1c7a145f84e0626Ian Rogers handler_quick_frame_pc_ = method->ToNativePc(found_dex_pc); 17190399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers handler_quick_frame_ = GetCurrentQuickFrame(); 172057b86d47b66322693a070185fadfb43cb9c12eabIan Rogers return false; // End stack walk. 1721bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers } 17221a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao } 172357b86d47b66322693a070185fadfb43cb9c12eabIan Rogers return true; // Continue stack walk. 17241a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao } 17251a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao 1726b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers void DoLongJump() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 172766f19258f9728d4ffe026074d8fd429d639802faMathieu Chartier AbstractMethod* catch_method = *handler_quick_frame_; 17286e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes Dbg::PostException(self_, throw_frame_id_, throw_method_, throw_dex_pc_, 17290399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers catch_method, handler_dex_pc_, exception_); 17300399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers if (kDebugExceptionDelivery) { 17310399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers if (catch_method == NULL) { 17320399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers LOG(INFO) << "Handler is upcall"; 17330399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers } else { 17344445a7e3398a6143939168097a3aa275b734504dIan Rogers const DexFile& dex_file = *catch_method->GetDeclaringClass()->GetDexCache()->GetDexFile(); 17350399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers int line_number = dex_file.GetLineNumFromPC(catch_method, handler_dex_pc_); 17360399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers LOG(INFO) << "Handler: " << PrettyMethod(catch_method) << " (line: " << line_number << ")"; 17370399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers } 17380399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers } 173952673ffae0025d86f4023735581f19ebcc477487Ian Rogers self_->SetException(exception_); // Exception back in root set. 174052673ffae0025d86f4023735581f19ebcc477487Ian Rogers self_->EndAssertNoThreadSuspension(last_no_assert_suspension_cause_); 17410399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers // Place context back on thread so it will be available when we continue. 17420399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers self_->ReleaseLongJumpContext(context_); 17430399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers context_->SetSP(reinterpret_cast<uintptr_t>(handler_quick_frame_)); 17440399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers CHECK_NE(handler_quick_frame_pc_, 0u); 17450399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers context_->SetPC(handler_quick_frame_pc_); 17460399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers context_->SmashCallerSaves(); 17470399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers context_->DoLongJump(); 17480399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers } 17490399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers 17500399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers private: 17510399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers Thread* self_; 17520399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers Throwable* exception_; 17530399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers // The type of the exception catch block to find. 1754bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers Class* to_find_; 175566f19258f9728d4ffe026074d8fd429d639802faMathieu Chartier AbstractMethod* throw_method_; 17560399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers JDWP::FrameId throw_frame_id_; 17570399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers uint32_t throw_dex_pc_; 17580399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers // Quick frame with found handler or last frame if no handler found. 175966f19258f9728d4ffe026074d8fd429d639802faMathieu Chartier AbstractMethod** handler_quick_frame_; 17600399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers // PC to branch to for the handler. 17610399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers uintptr_t handler_quick_frame_pc_; 17620399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers // Associated dex PC. 17630399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers uint32_t handler_dex_pc_; 176467375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers // Number of native methods passed in crawl (equates to number of SIRTs to pop) 176567375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers uint32_t native_method_count_; 176657b86d47b66322693a070185fadfb43cb9c12eabIan Rogers // Is method tracing active? 176757b86d47b66322693a070185fadfb43cb9c12eabIan Rogers const bool method_tracing_active_; 176852673ffae0025d86f4023735581f19ebcc477487Ian Rogers // Support for nesting no thread suspension checks. 176952673ffae0025d86f4023735581f19ebcc477487Ian Rogers const char* last_no_assert_suspension_cause_; 1770bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers}; 1771bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 177294d6df471a406a03bb1afba8ca3ae9c0fbf366b5jeffhaovoid Thread::QuickDeliverException() { 1773d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes Throwable* exception = GetException(); // Get exception from thread 1774ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers CHECK(exception != NULL); 177528ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers // Don't leave exception visible while we try to find the handler, which may cause class 1776d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes // resolution. 177728ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers ClearException(); 177828ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers if (kDebugExceptionDelivery) { 1779a32a6fd4a781262dff4fec102da053d16b7ef6c0Ian Rogers String* msg = exception->GetDetailMessage(); 1780a32a6fd4a781262dff4fec102da053d16b7ef6c0Ian Rogers std::string str_msg(msg != NULL ? msg->ToModifiedUtf8() : ""); 1781a32a6fd4a781262dff4fec102da053d16b7ef6c0Ian Rogers DumpStack(LOG(INFO) << "Delivering exception: " << PrettyTypeOf(exception) 1782c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes << ": " << str_msg << "\n"); 178328ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers } 17840399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers CatchBlockStackVisitor catch_finder(this, exception); 17850399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers catch_finder.WalkStack(true); 17860399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers catch_finder.DoLongJump(); 17879a8a8889418e09bf2d4ae3f683fbb40a71c3c8f4Ian Rogers LOG(FATAL) << "UNREACHABLE"; 1788bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers} 1789bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 1790bdb0391258abc54bf77c676e36847d28a783bfe5Ian RogersContext* Thread::GetLongJumpContext() { 179185d1545e985ac689db4bad7849880e843707c862Elliott Hughes Context* result = long_jump_context_; 1792bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers if (result == NULL) { 1793bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers result = Context::Create(); 17940399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers } else { 17950399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers long_jump_context_ = NULL; // Avoid context being shared. 17961a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao } 1797bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers return result; 17981a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao} 17991a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao 180066f19258f9728d4ffe026074d8fd429d639802faMathieu ChartierAbstractMethod* Thread::GetCurrentMethod(uint32_t* dex_pc, size_t* frame_id) const { 18010399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers struct CurrentMethodVisitor : public StackVisitor { 18020399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers CurrentMethodVisitor(const ManagedStack* stack, 1803725a957985171d712d5c048cc3d00ff14968784bjeffhao const std::vector<InstrumentationStackFrame>* instrumentation_stack) 1804b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) 1805725a957985171d712d5c048cc3d00ff14968784bjeffhao : StackVisitor(stack, instrumentation_stack, NULL), method_(NULL), dex_pc_(0), frame_id_(0) {} 18068be2d40d42223b515de12629216151a558895969Elliott Hughes 1807b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 180866f19258f9728d4ffe026074d8fd429d639802faMathieu Chartier AbstractMethod* m = GetMethod(); 18090399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers if (m->IsRuntimeMethod()) { 18100399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers // Continue if this is a runtime method. 18110399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers return true; 18120399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers } 18130399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers method_ = m; 18140399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers dex_pc_ = GetDexPc(); 18150399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers frame_id_ = GetFrameId(); 18160399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers return false; 18170399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers } 181866f19258f9728d4ffe026074d8fd429d639802faMathieu Chartier AbstractMethod* method_; 18190399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers uint32_t dex_pc_; 18200399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers size_t frame_id_; 18210399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers }; 18220399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers 1823725a957985171d712d5c048cc3d00ff14968784bjeffhao CurrentMethodVisitor visitor(GetManagedStack(), GetInstrumentationStack()); 18240399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers visitor.WalkStack(false); 18250399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers if (dex_pc != NULL) { 18260399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers *dex_pc = visitor.dex_pc_; 1827d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes } 18280399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers if (frame_id != NULL) { 18290399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers *frame_id = visitor.frame_id_; 1830ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYa } 18310399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers return visitor.method_; 1832ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYa} 183333dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao 18345f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesbool Thread::HoldsLock(Object* object) { 18355f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (object == NULL) { 18365f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return false; 18375f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 183824a3c2e9924e8765c4a9b4d383cb8f3b922f9c9fBrian Carlstrom return object->GetThinLockId() == thin_lock_id_; 18395f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 18405f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 184140e3bacfd57bca2ca39c1caec64680bd0ed4a16dIan Rogers// RootVisitor parameters are: (const Object* obj, size_t vreg, const StackVisitor* visitor). 184240e3bacfd57bca2ca39c1caec64680bd0ed4a16dIan Rogerstemplate <typename RootVisitor> 18430399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogersclass ReferenceMapVisitor : public StackVisitor { 1844d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers public: 1845725a957985171d712d5c048cc3d00ff14968784bjeffhao ReferenceMapVisitor(const ManagedStack* stack, const std::vector<InstrumentationStackFrame>* instrumentation_stack, 184640e3bacfd57bca2ca39c1caec64680bd0ed4a16dIan Rogers Context* context, const RootVisitor& visitor) 1847b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) 1848725a957985171d712d5c048cc3d00ff14968784bjeffhao : StackVisitor(stack, instrumentation_stack, context), visitor_(visitor) {} 1849d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers 1850b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 18516a4be3a6226cec645cf905dd352e44f7968a7fa4Brian Carlstrom if (false) { 18520399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers LOG(INFO) << "Visiting stack roots in " << PrettyMethod(GetMethod()) 18530399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers << StringPrintf("@ PC:%04x", GetDexPc()); 18546a4be3a6226cec645cf905dd352e44f7968a7fa4Brian Carlstrom } 18550399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers ShadowFrame* shadow_frame = GetCurrentShadowFrame(); 18560399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers if (shadow_frame != NULL) { 1857ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa AbstractMethod* m = shadow_frame->GetMethod(); 1858ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa size_t num_regs = shadow_frame->NumberOfVRegs(); 1859ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa if (m->IsNative() || shadow_frame->HasReferenceArray()) { 1860ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa // SIRT for JNI or References for interpreter. 1861ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa for (size_t reg = 0; reg < num_regs; ++reg) { 1862ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa Object* ref = shadow_frame->GetVRegReference(reg); 1863ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa if (ref != NULL) { 1864ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa visitor_(ref, reg, this); 1865ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa } 1866ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa } 1867ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa } else { 1868ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa // Java method. 1869ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa // Portable path use DexGcMap and store in Method.native_gc_map_. 1870ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa const uint8_t* gc_map = m->GetNativeGcMap(); 1871ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa CHECK(gc_map != NULL) << PrettyMethod(m); 1872ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa uint32_t gc_map_length = static_cast<uint32_t>((gc_map[0] << 24) | 1873ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa (gc_map[1] << 16) | 1874ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa (gc_map[2] << 8) | 1875ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa (gc_map[3] << 0)); 1876ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa verifier::DexPcToReferenceMap dex_gc_map(gc_map + 4, gc_map_length); 1877ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa uint32_t dex_pc = GetDexPc(); 1878ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa const uint8_t* reg_bitmap = dex_gc_map.FindBitMap(dex_pc); 1879ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa DCHECK(reg_bitmap != NULL); 1880ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa num_regs = std::min(dex_gc_map.RegWidth() * 8, num_regs); 1881ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa for (size_t reg = 0; reg < num_regs; ++reg) { 1882ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa if (TestBitmap(reg, reg_bitmap)) { 1883ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa Object* ref = shadow_frame->GetVRegReference(reg); 1884ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa if (ref != NULL) { 1885ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa visitor_(ref, reg, this); 1886ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa } 1887ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa } 1888ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa } 1889ce4cc0d1818e872c1c7f3c3519a82259afd5c288TDYa } 18900399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers } else { 189166f19258f9728d4ffe026074d8fd429d639802faMathieu Chartier AbstractMethod* m = GetMethod(); 18920399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers // Process register map (which native and runtime methods don't have) 1893640495bd1aebcd0b429d59f62b57d0c984b2d8c6Ian Rogers if (!m->IsNative() && !m->IsRuntimeMethod() && !m->IsProxyMethod()) { 18940c7abda482f53db3d153c073d1c7a145f84e0626Ian Rogers const uint8_t* native_gc_map = m->GetNativeGcMap(); 18950c7abda482f53db3d153c073d1c7a145f84e0626Ian Rogers CHECK(native_gc_map != NULL) << PrettyMethod(m); 18960c7abda482f53db3d153c073d1c7a145f84e0626Ian Rogers mh_.ChangeMethod(m); 18970c7abda482f53db3d153c073d1c7a145f84e0626Ian Rogers const DexFile::CodeItem* code_item = mh_.GetCodeItem(); 1898caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes DCHECK(code_item != NULL) << PrettyMethod(m); // Can't be NULL or how would we compile its instructions? 18990c7abda482f53db3d153c073d1c7a145f84e0626Ian Rogers NativePcOffsetToReferenceMap map(native_gc_map); 19000399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers size_t num_regs = std::min(map.RegWidth() * 8, 19010399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers static_cast<size_t>(code_item->registers_size_)); 19020c7abda482f53db3d153c073d1c7a145f84e0626Ian Rogers if (num_regs > 0) { 19030c7abda482f53db3d153c073d1c7a145f84e0626Ian Rogers const uint8_t* reg_bitmap = map.FindBitMap(GetNativePcOffset()); 19040c7abda482f53db3d153c073d1c7a145f84e0626Ian Rogers DCHECK(reg_bitmap != NULL); 19050c7abda482f53db3d153c073d1c7a145f84e0626Ian Rogers const VmapTable vmap_table(m->GetVmapTableRaw()); 19060c7abda482f53db3d153c073d1c7a145f84e0626Ian Rogers uint32_t core_spills = m->GetCoreSpillMask(); 19070c7abda482f53db3d153c073d1c7a145f84e0626Ian Rogers uint32_t fp_spills = m->GetFpSpillMask(); 19080c7abda482f53db3d153c073d1c7a145f84e0626Ian Rogers size_t frame_size = m->GetFrameSizeInBytes(); 19090c7abda482f53db3d153c073d1c7a145f84e0626Ian Rogers // For all dex registers in the bitmap 191066f19258f9728d4ffe026074d8fd429d639802faMathieu Chartier AbstractMethod** cur_quick_frame = GetCurrentQuickFrame(); 19110c7abda482f53db3d153c073d1c7a145f84e0626Ian Rogers DCHECK(cur_quick_frame != NULL); 19120c7abda482f53db3d153c073d1c7a145f84e0626Ian Rogers for (size_t reg = 0; reg < num_regs; ++reg) { 19130c7abda482f53db3d153c073d1c7a145f84e0626Ian Rogers // Does this register hold a reference? 19140c7abda482f53db3d153c073d1c7a145f84e0626Ian Rogers if (TestBitmap(reg, reg_bitmap)) { 19150c7abda482f53db3d153c073d1c7a145f84e0626Ian Rogers uint32_t vmap_offset; 19160c7abda482f53db3d153c073d1c7a145f84e0626Ian Rogers Object* ref; 19172bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers if (vmap_table.IsInContext(reg, vmap_offset, kReferenceVReg)) { 19182bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers uintptr_t val = GetGPR(vmap_table.ComputeRegister(core_spills, vmap_offset, 19192bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers kReferenceVReg)); 19202bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers ref = reinterpret_cast<Object*>(val); 19210c7abda482f53db3d153c073d1c7a145f84e0626Ian Rogers } else { 19220c7abda482f53db3d153c073d1c7a145f84e0626Ian Rogers ref = reinterpret_cast<Object*>(GetVReg(cur_quick_frame, code_item, core_spills, 19230c7abda482f53db3d153c073d1c7a145f84e0626Ian Rogers fp_spills, frame_size, reg)); 19246f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier } 19256f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier 19266f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier if (ref != NULL) { 192740e3bacfd57bca2ca39c1caec64680bd0ed4a16dIan Rogers visitor_(ref, reg, this); 19280399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers } 1929d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 19304f894e385e8ac018f078be75fea0a45696626b15Shih-wei Liao } 1931d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1932d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1933d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1934530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes return true; 1935d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1936d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers 1937d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers private: 19386f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier static bool TestBitmap(int reg, const uint8_t* reg_vector) { 1939d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers return ((reg_vector[reg / 8] >> (reg % 8)) & 0x01) != 0; 1940d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1941d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers 19426f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier // Visitor for when we visit a root. 194340e3bacfd57bca2ca39c1caec64680bd0ed4a16dIan Rogers const RootVisitor& visitor_; 19446f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier 19450c7abda482f53db3d153c073d1c7a145f84e0626Ian Rogers // A method helper we keep around to avoid dex file/cache re-computations. 19460c7abda482f53db3d153c073d1c7a145f84e0626Ian Rogers MethodHelper mh_; 1947d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers}; 1948d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers 19496f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartierclass RootCallbackVisitor { 19506f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier public: 19516f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier RootCallbackVisitor(Heap::RootVisitor* visitor, void* arg) : visitor_(visitor), arg_(arg) { 19526f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier 19536f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier } 19546f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier 195540e3bacfd57bca2ca39c1caec64680bd0ed4a16dIan Rogers void operator()(const Object* obj, size_t, const StackVisitor*) const { 19566f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier visitor_(obj, arg_); 19576f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier } 19586f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier 19596f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier private: 19606f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier Heap::RootVisitor* visitor_; 19616f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier void* arg_; 19626f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier}; 19636f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier 19646f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartierclass VerifyCallbackVisitor { 19656f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier public: 19666f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier VerifyCallbackVisitor(Heap::VerifyRootVisitor* visitor, void* arg) 19676f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier : visitor_(visitor), 19686f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier arg_(arg) { 19696f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier } 19706f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier 197140e3bacfd57bca2ca39c1caec64680bd0ed4a16dIan Rogers void operator()(const Object* obj, size_t vreg, const StackVisitor* visitor) const { 197240e3bacfd57bca2ca39c1caec64680bd0ed4a16dIan Rogers visitor_(obj, arg_, vreg, visitor); 19736f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier } 19746f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier 19756f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier private: 197640e3bacfd57bca2ca39c1caec64680bd0ed4a16dIan Rogers Heap::VerifyRootVisitor* const visitor_; 197740e3bacfd57bca2ca39c1caec64680bd0ed4a16dIan Rogers void* const arg_; 19786f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier}; 19796f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier 19806f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartierstruct VerifyRootWrapperArg { 19816f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier Heap::VerifyRootVisitor* visitor; 19826f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier void* arg; 19836f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier}; 19846f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier 19856f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartierstatic void VerifyRootWrapperCallback(const Object* root, void* arg) { 19866f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier VerifyRootWrapperArg* wrapperArg = reinterpret_cast<VerifyRootWrapperArg*>(arg); 19876f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier wrapperArg->visitor(root, wrapperArg->arg, 0, NULL); 19886f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier} 19896f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier 19906f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartiervoid Thread::VerifyRoots(Heap::VerifyRootVisitor* visitor, void* arg) { 19916f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier // We need to map from a RootVisitor to VerifyRootVisitor, so pass in nulls for arguments we 19926f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier // don't have. 19936f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier VerifyRootWrapperArg wrapperArg; 19946f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier wrapperArg.arg = arg; 19956f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier wrapperArg.visitor = visitor; 19966f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier 1997cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers if (opeer_ != NULL) { 1998cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers VerifyRootWrapperCallback(opeer_, &wrapperArg); 1999cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers } 20006f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier if (exception_ != NULL) { 20016f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier VerifyRootWrapperCallback(exception_, &wrapperArg); 20026f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier } 20036f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier if (class_loader_override_ != NULL) { 20046f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier VerifyRootWrapperCallback(class_loader_override_, &wrapperArg); 20056f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier } 20066f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier jni_env_->locals.VisitRoots(VerifyRootWrapperCallback, &wrapperArg); 20076f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier jni_env_->monitors.VisitRoots(VerifyRootWrapperCallback, &wrapperArg); 20086f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier 20096f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier SirtVisitRoots(VerifyRootWrapperCallback, &wrapperArg); 20106f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier 20116f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier // Visit roots on this thread's stack 20126f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier Context* context = GetLongJumpContext(); 20136f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier VerifyCallbackVisitor visitorToCallback(visitor, arg); 2014725a957985171d712d5c048cc3d00ff14968784bjeffhao ReferenceMapVisitor<VerifyCallbackVisitor> mapper(GetManagedStack(), GetInstrumentationStack(), context, 20156f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier visitorToCallback); 20166f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier mapper.WalkStack(); 20176f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier ReleaseLongJumpContext(context); 20186f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier} 20196f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier 2020d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogersvoid Thread::VisitRoots(Heap::RootVisitor* visitor, void* arg) { 2021cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers if (opeer_ != NULL) { 2022cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers visitor(opeer_, arg); 2023cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers } 2024d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (exception_ != NULL) { 2025d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes visitor(exception_, arg); 2026d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 202740381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom if (class_loader_override_ != NULL) { 202840381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom visitor(class_loader_override_, arg); 202940381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom } 2030410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes jni_env_->locals.VisitRoots(visitor, arg); 2031410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes jni_env_->monitors.VisitRoots(visitor, arg); 20328dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao 20338dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao SirtVisitRoots(visitor, arg); 20348dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao 2035d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Visit roots on this thread's stack 20360399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers Context* context = GetLongJumpContext(); 20376f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier RootCallbackVisitor visitorToCallback(visitor, arg); 2038725a957985171d712d5c048cc3d00ff14968784bjeffhao ReferenceMapVisitor<RootCallbackVisitor> mapper(GetManagedStack(), GetInstrumentationStack(), context, 20396f1c94968ada57da433debf8e2d1b38a80ceb510Mathieu Chartier visitorToCallback); 20400399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers mapper.WalkStack(); 20410399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers ReleaseLongJumpContext(context); 2042410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes} 2043410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes 2044250455229aa0cc07bbd18174efe510bd52631a99jeffhao#if VERIFY_OBJECT_ENABLED 20450399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogersstatic void VerifyObject(const Object* obj, void* arg) { 20460399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers Heap* heap = reinterpret_cast<Heap*>(arg); 20470399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers heap->VerifyObject(obj); 2048250455229aa0cc07bbd18174efe510bd52631a99jeffhao} 2049250455229aa0cc07bbd18174efe510bd52631a99jeffhao 2050250455229aa0cc07bbd18174efe510bd52631a99jeffhaovoid Thread::VerifyStack() { 2051e66ac79b90a12b01b9d67ee9f7f586e638e67fabjeffhao UniquePtr<Context> context(Context::Create()); 20524eb68edeecc4d6641bcca2a40620631335e7cadcjeffhao RootCallbackVisitor visitorToCallback(VerifyObject, Runtime::Current()->GetHeap()); 2053725a957985171d712d5c048cc3d00ff14968784bjeffhao ReferenceMapVisitor<RootCallbackVisitor> mapper(GetManagedStack(), GetInstrumentationStack(), context.get(), 20544eb68edeecc4d6641bcca2a40620631335e7cadcjeffhao visitorToCallback); 20550399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers mapper.WalkStack(); 2056250455229aa0cc07bbd18174efe510bd52631a99jeffhao} 2057250455229aa0cc07bbd18174efe510bd52631a99jeffhao#endif 2058250455229aa0cc07bbd18174efe510bd52631a99jeffhao 205900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers// Set the stack end to that to be used during a stack overflow 206000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersvoid Thread::SetStackEndForStackOverflow() { 206100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // During stack overflow we allow use of the full stack 206200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers if (stack_end_ == stack_begin_) { 206300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers DumpStack(std::cerr); 206400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers LOG(FATAL) << "Need to increase kStackOverflowReservedBytes (currently " 206500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers << kStackOverflowReservedBytes << ")"; 206600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 206700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 206800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers stack_end_ = stack_begin_; 206900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers} 207000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 2071330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughesstd::ostream& operator<<(std::ostream& os, const Thread& thread) { 207200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers thread.ShortDump(os); 2073330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughes return os; 2074330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughes} 2075330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughes 207600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#ifndef NDEBUG 207700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersvoid Thread::AssertThreadSuspensionIsAllowable(bool check_locks) const { 207800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers CHECK_EQ(0u, no_thread_suspension_) << last_no_thread_suspension_cause_; 207900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers if (check_locks) { 208000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers bool bad_mutexes_held = false; 208100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers for (int i = kMaxMutexLevel; i >= 0; --i) { 208200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // We expect no locks except the mutator_lock_. 208300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers if (i != kMutatorLock) { 208481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers BaseMutex* held_mutex = GetHeldMutex(static_cast<LockLevel>(i)); 208500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers if (held_mutex != NULL) { 208600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers LOG(ERROR) << "holding \"" << held_mutex->GetName() 208700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers << "\" at point where thread suspension is expected"; 2088ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes bad_mutexes_held = true; 2089ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes } 2090ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes } 2091ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes } 209200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers CHECK(!bad_mutexes_held); 2093ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes } 2094ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes} 209500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#endif 2096a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes 20978daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes} // namespace art 2098