thread.cc revision 2acf36d8cfeb5ddb293904148aa70f25ef6d8845
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> 22b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers#include <sys/mman.h> 23dbf05b722af99ba2fd2f4c4fc7eb6c3e9880e5d1Brian Carlstrom#include <sys/resource.h> 24dbf05b722af99ba2fd2f4c4fc7eb6c3e9880e5d1Brian Carlstrom#include <sys/time.h> 25a09576416788b916095739e43a16917e7948f3a4Elliott Hughes 26b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro#include <algorithm> 27dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes#include <bitset> 28eb4f614f2eb53b92ebd416fa418f550861655887Elliott Hughes#include <cerrno> 29a09576416788b916095739e43a16917e7948f3a4Elliott Hughes#include <iostream> 30b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro#include <list> 31b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 32872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes#include "debugger.h" 33a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes#include "class_linker.h" 34df143242f4beaad4cc9fbabebfc033b68c40964eBrian Carlstrom#include "class_loader.h" 35408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers#include "heap.h" 36c5f7c91ab89055cffb573fff7e06dbdd860bccedElliott Hughes#include "jni_internal.h" 378e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes#include "monitor.h" 3857b86d47b66322693a070185fadfb43cb9c12eabIan Rogers#include "oat/runtime/context.h" 39a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes#include "object.h" 406d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers#include "object_utils.h" 419a6bae896a2f003d7216603bf29771d105c10ca4Jesse Wilson#include "reflection.h" 42578bbdc684db8ed68e9fedbc678669d27fa68b6eBrian Carlstrom#include "runtime.h" 435433072f589b61413e042eddf76e8190a048f71dbuzbee#include "runtime_support.h" 44726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes#include "ScopedLocalRef.h" 45aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers#include "scoped_jni_thread_state.h" 46f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien#include "shadow_frame.h" 4730fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers#include "space.h" 4868e76526e98432625464022cb26f66b9ef6f5af4Elliott Hughes#include "stack.h" 4968e76526e98432625464022cb26f66b9ef6f5af4Elliott Hughes#include "stack_indirect_reference_table.h" 508daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes#include "thread_list.h" 51a09576416788b916095739e43a16917e7948f3a4Elliott Hughes#include "utils.h" 52776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#include "verifier/gc_map.h" 53b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 54b557353b22c728eecbd1c68593b482622c7782a8Carl Shapironamespace art { 55b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 56b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiropthread_key_t Thread::pthread_key_self_; 57b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 58462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughesstatic Class* gThreadGroup = NULL; 598e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughesstatic Class* gThreadLock = NULL; 60038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThread_daemon = NULL; 61038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThread_group = NULL; 62038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThread_lock = NULL; 63038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThread_name = NULL; 64038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThread_priority = NULL; 6529f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughesstatic Field* gThread_uncaughtHandler = NULL; 66038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThread_vmData = NULL; 67462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughesstatic Field* gThreadGroup_mMain = NULL; 68462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughesstatic Field* gThreadGroup_mSystem = NULL; 69038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Field* gThreadGroup_name = NULL; 708e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughesstatic Field* gThreadLock_thread = NULL; 71038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesstatic Method* gThread_run = NULL; 7229f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughesstatic Method* gThreadGroup_removeThread = NULL; 7329f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughesstatic Method* gUncaughtExceptionHandler_uncaughtException = NULL; 74038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes 755d76c435082332ef79a22962386fa92a0870e378Ian Rogersvoid Thread::InitCardTable() { 76b3bd5f07884f5a1f2b84224363b1372d7c28d447Elliott Hughes card_table_ = Runtime::Current()->GetHeap()->GetCardTable()->GetBiasedBegin(); 775d76c435082332ef79a22962386fa92a0870e378Ian Rogers} 785d76c435082332ef79a22962386fa92a0870e378Ian Rogers 7999250ba6a2cc72b8894dffa77c1fca5570087e3cElliott Hughes#if !defined(__APPLE__) 803ea0f42467790809fcfc9fc861605d465808090fElliott Hughesstatic void UnimplementedEntryPoint() { 813ea0f42467790809fcfc9fc861605d465808090fElliott Hughes UNIMPLEMENTED(FATAL); 823ea0f42467790809fcfc9fc861605d465808090fElliott Hughes} 8399250ba6a2cc72b8894dffa77c1fca5570087e3cElliott Hughes#endif 843ea0f42467790809fcfc9fc861605d465808090fElliott Hughes 853ea4ec5629613013ad9b0d7a69abdb94491ac46fbuzbeevoid Thread::InitFunctionPointers() { 8699250ba6a2cc72b8894dffa77c1fca5570087e3cElliott Hughes#if !defined(__APPLE__) // The Mac GCC is too old to accept this code. 873ea0f42467790809fcfc9fc861605d465808090fElliott Hughes // Insert a placeholder so we can easily tell if we call an unimplemented entry point. 883ea0f42467790809fcfc9fc861605d465808090fElliott Hughes uintptr_t* begin = reinterpret_cast<uintptr_t*>(&entrypoints_); 893ea0f42467790809fcfc9fc861605d465808090fElliott Hughes uintptr_t* end = reinterpret_cast<uintptr_t*>(reinterpret_cast<uint8_t*>(begin) + sizeof(entrypoints_)); 903ea0f42467790809fcfc9fc861605d465808090fElliott Hughes for (uintptr_t* it = begin; it != end; ++it) { 913ea0f42467790809fcfc9fc861605d465808090fElliott Hughes *it = reinterpret_cast<uintptr_t>(UnimplementedEntryPoint); 923ea0f42467790809fcfc9fc861605d465808090fElliott Hughes } 9399250ba6a2cc72b8894dffa77c1fca5570087e3cElliott Hughes#endif 9457b86d47b66322693a070185fadfb43cb9c12eabIan Rogers InitEntryPoints(&entrypoints_); 95c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes} 96c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes 97c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughesvoid Thread::SetDebuggerUpdatesEnabled(bool enabled) { 98c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes LOG(INFO) << "Turning debugger updates " << (enabled ? "on" : "off") << " for " << *this; 99776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#if !defined(ART_USE_LLVM_COMPILER) 10057b86d47b66322693a070185fadfb43cb9c12eabIan Rogers ChangeDebuggerEntryPoint(&entrypoints_, enabled); 101776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#else 102776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers UNIMPLEMENTED(FATAL); 103776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#endif 1043ea4ec5629613013ad9b0d7a69abdb94491ac46fbuzbee} 1053ea4ec5629613013ad9b0d7a69abdb94491ac46fbuzbee 106caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstromvoid Thread::InitTid() { 107caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom tid_ = ::art::GetTid(); 108caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom} 109caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom 110caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstromvoid Thread::InitAfterFork() { 111caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom InitTid(); 1123147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes#if defined(__BIONIC__) 1133147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes // Work around a bionic bug. 1143147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes struct bionic_pthread_internal_t { 1153147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes void* next; 1163147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes void** pref; 1173147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes pthread_attr_t attr; 1183147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes pid_t kernel_id; 1193147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes // et cetera. we just need 'kernel_id' so we can stop here. 1203147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes }; 1213147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes reinterpret_cast<bionic_pthread_internal_t*>(pthread_self())->kernel_id = tid_; 1223147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes#endif 123caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom} 124caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom 12578128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstromvoid* Thread::CreateCallback(void* arg) { 12693e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes Thread* self = reinterpret_cast<Thread*>(arg); 127462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes self->Init(); 12893e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 12993e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // Wait until it's safe to start running code. (There may have been a suspend-all 13093e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // in progress while we were starting up.) 131462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes Runtime* runtime = Runtime::Current(); 13293e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes runtime->GetThreadList()->WaitForGo(); 13393e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 13447179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes { 13534e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes CHECK_EQ(self->GetState(), kRunnable); 136899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes SirtRef<String> thread_name(self->GetThreadName()); 137899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes self->SetThreadName(thread_name->ToModifiedUtf8().c_str()); 13847179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes } 13947179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes 140872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes Dbg::PostThreadStart(self); 14193e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 14293e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // Invoke the 'run' method of our java.lang.Thread. 14393e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes CHECK(self->peer_ != NULL); 14493e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes Object* receiver = self->peer_; 145038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes Method* m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(gThread_run); 14693e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes m->Invoke(self, receiver, NULL, NULL); 14793e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 14893e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // Detach. 14993e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes runtime->GetThreadList()->Unregister(); 15093e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 151b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro return NULL; 152b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro} 153b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 154ab7b9dcbfc4264a0bc4889c3e463ff88a67f6a30Elliott Hughesstatic void SetVmData(Object* managed_thread, Thread* native_thread) { 155038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes gThread_vmData->SetInt(managed_thread, reinterpret_cast<uintptr_t>(native_thread)); 15693e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes} 15793e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 158761928d24e4e7ed7776b52243eaf9095ad35f448Elliott HughesThread* Thread::FromManagedThread(Object* thread_peer) { 159761928d24e4e7ed7776b52243eaf9095ad35f448Elliott Hughes return reinterpret_cast<Thread*>(static_cast<uintptr_t>(gThread_vmData->GetInt(thread_peer))); 160761928d24e4e7ed7776b52243eaf9095ad35f448Elliott Hughes} 161761928d24e4e7ed7776b52243eaf9095ad35f448Elliott Hughes 16201158d7a57c8321370667a6045220237d16e0da8Elliott HughesThread* Thread::FromManagedThread(JNIEnv* env, jobject java_thread) { 163761928d24e4e7ed7776b52243eaf9095ad35f448Elliott Hughes return FromManagedThread(Decode<Object*>(env, java_thread)); 16401158d7a57c8321370667a6045220237d16e0da8Elliott Hughes} 16501158d7a57c8321370667a6045220237d16e0da8Elliott Hughes 166ab7b9dcbfc4264a0bc4889c3e463ff88a67f6a30Elliott Hughesstatic size_t FixStackSize(size_t stack_size) { 1677502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // A stack size of zero means "use the default". 168d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (stack_size == 0) { 169d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes stack_size = Runtime::Current()->GetDefaultStackSize(); 170d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 17161e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 1726414a97a3c6dc101ae8ebc9480114d0c327e8e8dBrian Carlstrom // Dalvik used the bionic pthread default stack size for native threads, 1736414a97a3c6dc101ae8ebc9480114d0c327e8e8dBrian Carlstrom // so include that here to support apps that expect large native stacks. 1746414a97a3c6dc101ae8ebc9480114d0c327e8e8dBrian Carlstrom stack_size += 1 * MB; 1756414a97a3c6dc101ae8ebc9480114d0c327e8e8dBrian Carlstrom 1766414a97a3c6dc101ae8ebc9480114d0c327e8e8dBrian Carlstrom 1777502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // It's not possible to request a stack smaller than the system-defined PTHREAD_STACK_MIN. 1787502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes if (stack_size < PTHREAD_STACK_MIN) { 1797502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes stack_size = PTHREAD_STACK_MIN; 1807502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes } 1817502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 1827502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // It's likely that callers are trying to ensure they have at least a certain amount of 1837502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // stack space, so we should add our reserved space on top of what they requested, rather 1847502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // than implicitly take it away from them. 1857502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes stack_size += Thread::kStackOverflowReservedBytes; 1867502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 1877502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes // Some systems require the stack size to be a multiple of the system page size, so round up. 1887502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes stack_size = RoundUp(stack_size, kPageSize); 1897502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 1907502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes return stack_size; 1917502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes} 1927502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 193d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughesstatic void SigAltStack(stack_t* new_stack, stack_t* old_stack) { 194d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes if (sigaltstack(new_stack, old_stack) == -1) { 195d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes PLOG(FATAL) << "sigaltstack failed"; 196d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes } 197d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes} 198d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes 199d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughesstatic void SetUpAlternateSignalStack() { 200d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes // Create and set an alternate signal stack. 201d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes stack_t ss; 202d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes ss.ss_sp = new uint8_t[SIGSTKSZ]; 203d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes ss.ss_size = SIGSTKSZ; 204d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes ss.ss_flags = 0; 205d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes CHECK(ss.ss_sp != NULL); 206d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes SigAltStack(&ss, NULL); 207d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes 208d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes // Double-check that it worked. 209d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes ss.ss_sp = NULL; 210d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes SigAltStack(NULL, &ss); 211d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes VLOG(threads) << "Alternate signal stack is " << PrettySize(ss.ss_size) << " at " << ss.ss_sp; 212d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes} 213d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes 214d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughesstatic void TearDownAlternateSignalStack() { 215d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes // Get the pointer so we can free the memory. 216d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes stack_t ss; 217d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes SigAltStack(NULL, &ss); 218d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes uint8_t* allocated_signal_stack = reinterpret_cast<uint8_t*>(ss.ss_sp); 219d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes 220d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes // Tell the kernel to stop using it. 221d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes ss.ss_sp = NULL; 222d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes ss.ss_flags = SS_DISABLE; 223d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes ss.ss_size = 0; 224d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes SigAltStack(&ss, NULL); 225d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes 226d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes // Free it. 227d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes delete[] allocated_signal_stack; 228d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes} 229d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes 2307502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughesvoid Thread::Create(Object* peer, size_t stack_size) { 2317502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes CHECK(peer != NULL); 2327502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 2337502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes stack_size = FixStackSize(stack_size); 2347502e2a419f84808518cf212b3d7145c7b959c76Elliott Hughes 23593e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes Thread* native_thread = new Thread; 23693e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes native_thread->peer_ = peer; 23793e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 23893e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // Thread.start is synchronized, so we know that vmData is 0, 23993e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // and know that we're not racing to assign it. 24093e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes SetVmData(peer, native_thread); 24161e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 24247179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes { 24334e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes ScopedThreadStateChange tsc(Thread::Current(), kVmWait); 24447179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes pthread_t new_pthread; 24547179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes pthread_attr_t attr; 24647179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes CHECK_PTHREAD_CALL(pthread_attr_init, (&attr), "new thread"); 24747179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes CHECK_PTHREAD_CALL(pthread_attr_setdetachstate, (&attr, PTHREAD_CREATE_DETACHED), "PTHREAD_CREATE_DETACHED"); 24847179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes CHECK_PTHREAD_CALL(pthread_attr_setstacksize, (&attr, stack_size), stack_size); 24947179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes CHECK_PTHREAD_CALL(pthread_create, (&new_pthread, &attr, Thread::CreateCallback, native_thread), "new thread"); 25047179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attr), "new thread"); 25147179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes } 25261e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 25393e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes // Let the child know when it's safe to start running. 25493e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes Runtime::Current()->GetThreadList()->SignalGo(native_thread); 25593e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes} 25661e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 257462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughesvoid Thread::Init() { 258462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes // This function does all the initialization that must be run by the native thread it applies to. 259462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes // (When we create a new thread from managed code, we allocate the Thread* in Thread::Create so 260462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes // we can handshake with the corresponding native thread when it's ready.) Check this native 261462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes // thread hasn't been through here already... 262cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes CHECK(Thread::Current() == NULL); 263cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes 264d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes SetUpAlternateSignalStack(); 26593e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes InitCpu(); 26693e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes InitFunctionPointers(); 2675d76c435082332ef79a22962386fa92a0870e378Ian Rogers InitCardTable(); 26861e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 269462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes Runtime* runtime = Runtime::Current(); 270462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes CHECK(runtime != NULL); 271462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes 272462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes thin_lock_id_ = runtime->GetThreadList()->AllocThreadId(); 273be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 274caabb1b77b4a55eb1bb45ebcd3071c9ea01dd3cfBrian Carlstrom InitTid(); 27593e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes InitStackHwm(); 276dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes 2773147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, this), "attach"); 278a5780dad67556297c8ca5f2608c53b193e6c4514Elliott Hughes 27993e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes jni_env_ = new JNIEnvExt(this, runtime->GetJavaVM()); 2805fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 2817a3aeb4d7580164c6a6905c63b96823b77ff5a64Elliott Hughes runtime->GetThreadList()->Register(); 28293e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes} 28393e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes 284462c94449720e0dc6b93d7138d835d345ccf67aeElliott HughesThread* Thread::Attach(const char* thread_name, bool as_daemon, Object* thread_group) { 285cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes Thread* self = new Thread; 286462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes self->Init(); 2875fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 28834e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes self->SetState(kNative); 289726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes 290cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes // If we're the main thread, ClassLinker won't be created until after we're attached, 291cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes // so that thread needs a two-stage attach. Regular threads don't need this hack. 292d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes // In the compiler, all threads need this hack, because no-one's going to be getting 293d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes // a native peer! 294d9c67be7c116875d96b31e640ad47d587b205605Elliott Hughes if (self->thin_lock_id_ != ThreadList::kMainId && !Runtime::Current()->IsCompiler()) { 295462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes self->CreatePeer(thread_name, as_daemon, thread_group); 29606e3ad4a651c2c58dba5e865cd06d2f98462bf1dElliott Hughes } else { 29706e3ad4a651c2c58dba5e865cd06d2f98462bf1dElliott Hughes // These aren't necessary, but they improve diagnostics for unit tests & command-line tools. 29822869a9026a08b544eca4cefd67386d347e30d2cElliott Hughes if (thread_name != NULL) { 29922869a9026a08b544eca4cefd67386d347e30d2cElliott Hughes self->name_->assign(thread_name); 30022869a9026a08b544eca4cefd67386d347e30d2cElliott Hughes ::art::SetThreadName(thread_name); 30122869a9026a08b544eca4cefd67386d347e30d2cElliott Hughes } 30236e0a955e48e9357bff1eab783e493107ebfff62Ian Rogers } 303cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes 304cac6cc72c3331257fa6437b36a131e5d551e2f3cElliott Hughes self->GetJniEnv()->locals.AssertEmpty(); 3055fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes return self; 3065fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes} 3075fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 308462c94449720e0dc6b93d7138d835d345ccf67aeElliott HughesObject* Thread::GetMainThreadGroup() { 3090045a290e6b79a274250e3112880c04bde437d4aIan Rogers if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(gThreadGroup, true, true)) { 310462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes return NULL; 311462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes } 312462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes return gThreadGroup_mMain->GetObject(NULL); 313d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes} 314d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 315462c94449720e0dc6b93d7138d835d345ccf67aeElliott HughesObject* Thread::GetSystemThreadGroup() { 3160045a290e6b79a274250e3112880c04bde437d4aIan Rogers if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(gThreadGroup, true, true)) { 317462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes return NULL; 318462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes } 319462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes return gThreadGroup_mSystem->GetObject(NULL); 320462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes} 321462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes 322462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughesvoid Thread::CreatePeer(const char* name, bool as_daemon, Object* thread_group) { 3239a6bae896a2f003d7216603bf29771d105c10ca4Jesse Wilson CHECK(Runtime::Current()->IsStarted()); 3245fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes JNIEnv* env = jni_env_; 3255fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 326462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes if (thread_group == NULL) { 327462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes thread_group = Thread::GetMainThreadGroup(); 328462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes } 329462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes ScopedLocalRef<jobject> java_thread_group(env, AddLocalReference<jobject>(env, thread_group)); 330726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes ScopedLocalRef<jobject> thread_name(env, env->NewStringUTF(name)); 3318daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes jint thread_priority = GetNativePriority(); 3325fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes jboolean thread_is_daemon = as_daemon; 3335fe594f576225dd7d333835e39c448a71ea9b433Elliott Hughes 334726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes ScopedLocalRef<jclass> c(env, env->FindClass("java/lang/Thread")); 3355d4bdc29737a693027daaf6ed3f0792368eb4baeIan Rogers ScopedLocalRef<jobject> peer(env, env->AllocObject(c.get())); 336726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes peer_ = DecodeJObject(peer.get()); 3375d4bdc29737a693027daaf6ed3f0792368eb4baeIan Rogers if (peer_ == NULL) { 3385d4bdc29737a693027daaf6ed3f0792368eb4baeIan Rogers CHECK(IsExceptionPending()); 3395d4bdc29737a693027daaf6ed3f0792368eb4baeIan Rogers return; 3405d4bdc29737a693027daaf6ed3f0792368eb4baeIan Rogers } 3415d4bdc29737a693027daaf6ed3f0792368eb4baeIan Rogers jmethodID mid = env->GetMethodID(c.get(), "<init>", "(Ljava/lang/ThreadGroup;Ljava/lang/String;IZ)V"); 342462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes env->CallNonvirtualVoidMethod(peer.get(), c.get(), mid, java_thread_group.get(), thread_name.get(), thread_priority, thread_is_daemon); 3439a6bae896a2f003d7216603bf29771d105c10ca4Jesse Wilson CHECK(!IsExceptionPending()) << " " << PrettyTypeOf(GetException()); 3447a3aeb4d7580164c6a6905c63b96823b77ff5a64Elliott Hughes SetVmData(peer_, Thread::Current()); 345d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 346899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes SirtRef<String> peer_thread_name(GetThreadName()); 34700fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom if (peer_thread_name.get() == NULL) { 34800fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom // The Thread constructor should have set the Thread.name to a 34900fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom // non-null value. However, because we can run without code 35000fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom // available (in the compiler, in tests), we manually assign the 35100fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom // fields the constructor should have set. 35200fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom gThread_daemon->SetBoolean(peer_, thread_is_daemon); 353462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes gThread_group->SetObject(peer_, thread_group); 35400fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom gThread_name->SetObject(peer_, Decode<Object*>(env, thread_name.get())); 35500fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom gThread_priority->SetInt(peer_, thread_priority); 356899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes peer_thread_name.reset(GetThreadName()); 35700fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom } 35800fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom // thread_name may have been null, so don't trust this to be non-null 35900fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom if (peer_thread_name.get() != NULL) { 360899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes SetThreadName(peer_thread_name->ToModifiedUtf8().c_str()); 36100fae585c6e4a37b964c77f557fbf84f11e2d930Brian Carlstrom } 362726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes 363726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes // Pre-allocate an OutOfMemoryError for the double-OOME case. 364726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes ThrowNewException("Ljava/lang/OutOfMemoryError;", 365726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes "OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack available"); 366726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes ScopedLocalRef<jthrowable> exception(env, env->ExceptionOccurred()); 367726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes env->ExceptionClear(); 368726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes pre_allocated_OutOfMemoryError_ = Decode<Throwable*>(env, exception.get()); 36961e019d291583029c01b61b93bea750f2b663c37Carl Shapiro} 37061e019d291583029c01b61b93bea750f2b663c37Carl Shapiro 371899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughesvoid Thread::SetThreadName(const char* name) { 372899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes name_->assign(name); 373899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes ::art::SetThreadName(name); 374899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes Dbg::DdmSendThreadNotification(this, CHUNK_TYPE("THNM")); 375899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes} 376899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes 377be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughesvoid Thread::InitStackHwm() { 378215f3144be114ac27002059330d903bf1ff5a592Elliott Hughes#if defined(__APPLE__) 3796cc332ef80edf56221676e33ea16bb4022130344Elliott Hughes // Only needed to run code. Try implementing this with pthread_get_stacksize_np and pthread_get_stackaddr_np. 3806cc332ef80edf56221676e33ea16bb4022130344Elliott Hughes UNIMPLEMENTED(WARNING); 381215f3144be114ac27002059330d903bf1ff5a592Elliott Hughes#else 382be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes pthread_attr_t attributes; 3833147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes CHECK_PTHREAD_CALL(pthread_getattr_np, (pthread_self(), &attributes), __FUNCTION__); 384be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 385932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers void* temp_stack_base; 386932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers CHECK_PTHREAD_CALL(pthread_attr_getstack, (&attributes, &temp_stack_base, &stack_size_), 387932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers __FUNCTION__); 38830fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers stack_begin_ = reinterpret_cast<byte*>(temp_stack_base); 389be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 390932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers if (stack_size_ <= kStackOverflowReservedBytes) { 3913d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes LOG(FATAL) << "Attempt to attach a thread with a too-small stack (" << stack_size_ << " bytes)"; 392be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes } 393449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes 394932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers // Set stack_end_ to the bottom of the stack saving space of stack overflows 395932746a4f22951abcba7b7c4c94c27b1bf164272Ian Rogers ResetDefaultStackEnd(); 396449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes 397449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes // Sanity check. 398449b4bdf90b527ef7a42faaf087494538e62363cElliott Hughes int stack_variable; 399398f64b5805246765b699839b439e18c0dfbf2eeElliott Hughes CHECK_GT(&stack_variable, reinterpret_cast<void*>(stack_end_)); 400be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 4018d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attributes), __FUNCTION__); 402215f3144be114ac27002059330d903bf1ff5a592Elliott Hughes#endif 403be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes} 404be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes 405899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughesvoid Thread::Dump(std::ostream& os, bool full) const { 406899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes if (full) { 407899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes DumpState(os); 408899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes DumpStack(os); 409899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes } else { 410899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes os << "Thread["; 411899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes if (GetThinLockId() != 0) { 412899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes // If we're in kStarting, we won't have a thin lock id or tid yet. 413899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes os << GetThinLockId() 414899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes << ",tid=" << GetTid() << ','; 415899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes } 416899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes os << GetState() 417899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes << ",Thread*=" << this 418ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes << ",peer=" << peer_ 419899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes << ",\"" << *name_ << "\"" 420899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes << "]"; 421e0918556e7551de638870dcad3f2023f94f85a50Elliott Hughes } 422d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes} 423d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 424899e789bd4741c0172268f7838ce8ab220a5f916Elliott HughesString* Thread::GetThreadName() const { 425fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes return (peer_ != NULL) ? reinterpret_cast<String*>(gThread_name->GetObject(peer_)) : NULL; 426fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes} 427fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes 428ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughesvoid Thread::GetThreadName(std::string& name) const { 429ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes name.assign(*name_); 430ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes} 431ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes 432d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughesvoid Thread::DumpState(std::ostream& os) const { 433d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes std::string group_name; 434d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes int priority; 435d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes bool is_daemon = false; 436d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 437d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (peer_ != NULL) { 438038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes priority = gThread_priority->GetInt(peer_); 439038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes is_daemon = gThread_daemon->GetBoolean(peer_); 440d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 441a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes Object* thread_group = GetThreadGroup(); 442d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (thread_group != NULL) { 443038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes String* group_name_string = reinterpret_cast<String*>(gThreadGroup_name->GetObject(thread_group)); 444d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes group_name = (group_name_string != NULL) ? group_name_string->ToModifiedUtf8() : "<null>"; 445d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 446d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } else { 447d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes priority = GetNativePriority(); 448dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes } 449d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 450d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes int policy; 451d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes sched_param sp; 4523147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes CHECK_PTHREAD_CALL(pthread_getschedparam, (pthread_self(), &policy, &sp), __FUNCTION__); 453d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 4541bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughes std::string scheduler_group_name(GetSchedulerGroupName(GetTid())); 4551bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughes if (scheduler_group_name.empty()) { 4561bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughes scheduler_group_name = "default"; 457d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 458d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 459899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes os << '"' << *name_ << '"'; 460d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (is_daemon) { 461d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes os << " daemon"; 462d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 463d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes os << " prio=" << priority 464dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes << " tid=" << GetThinLockId() 46593e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes << " " << GetState() << "\n"; 466d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 467d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes os << " | group=\"" << group_name << "\"" 4688d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes << " sCount=" << suspend_count_ 469234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes << " dsCount=" << debug_suspend_count_ 470dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes << " obj=" << reinterpret_cast<void*>(peer_) 471d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " self=" << reinterpret_cast<const void*>(this) << "\n"; 472d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes os << " | sysTid=" << GetTid() 473d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " nice=" << getpriority(PRIO_PROCESS, GetTid()) 474d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " sched=" << policy << "/" << sp.sched_priority 4751bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughes << " cgrp=" << scheduler_group_name 4763147a2347d4fd775d51ec0a558955332beb9d1feElliott Hughes << " handle=" << pthread_self() << "\n"; 477d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 478d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes // Grab the scheduler stats for this thread. 479d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes std::string scheduler_stats; 480d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes if (ReadFileToString(StringPrintf("/proc/self/task/%d/schedstat", GetTid()).c_str(), &scheduler_stats)) { 481d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes scheduler_stats.resize(scheduler_stats.size() - 1); // Lose the trailing '\n'. 482d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } else { 483d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes scheduler_stats = "0 0 0"; 484d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes } 485d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 486d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes int utime = 0; 487d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes int stime = 0; 488d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes int task_cpu = 0; 489bfe487be25652c5456236661b9d9c3579d2296c1Elliott Hughes GetTaskStats(GetTid(), utime, stime, task_cpu); 490d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes 491d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes os << " | schedstat=( " << scheduler_stats << " )" 492d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " utm=" << utime 493d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " stm=" << stime 494d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " core=" << task_cpu 495d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughes << " HZ=" << sysconf(_SC_CLK_TCK) << "\n"; 496a09576416788b916095739e43a16917e7948f3a4Elliott Hughes} 497a09576416788b916095739e43a16917e7948f3a4Elliott Hughes 498ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYa#if !defined(ART_USE_LLVM_COMPILER) 499b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogersvoid Thread::PushNativeToManagedRecord(NativeToManagedRecord* record) { 500b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers Method **sp = top_of_managed_stack_.GetSP(); 501b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers#ifndef NDEBUG 502b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers if (sp != NULL) { 503b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers Method* m = *sp; 504b3bd5f07884f5a1f2b84224363b1372d7c28d447Elliott Hughes Runtime::Current()->GetHeap()->VerifyObject(m); 505b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers DCHECK((m == NULL) || m->IsMethod()); 506b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers } 507b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers#endif 508b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers record->last_top_of_managed_stack_ = reinterpret_cast<void*>(sp); 509b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers record->last_top_of_managed_stack_pc_ = top_of_managed_stack_pc_; 510b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers record->link_ = native_to_managed_record_; 511b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers native_to_managed_record_ = record; 512b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers top_of_managed_stack_.SetSP(NULL); 513b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers} 514ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYa#else 515ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYavoid Thread::PushNativeToManagedRecord(NativeToManagedRecord*) { 516ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYa LOG(FATAL) << "Called non-LLVM method with LLVM"; 517ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYa} 518ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYa#endif 519b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers 520ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYa#if !defined(ART_USE_LLVM_COMPILER) 521b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogersvoid Thread::PopNativeToManagedRecord(const NativeToManagedRecord& record) { 522b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers native_to_managed_record_ = record.link_; 523b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers top_of_managed_stack_.SetSP(reinterpret_cast<Method**>(record.last_top_of_managed_stack_)); 524b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers top_of_managed_stack_pc_ = record.last_top_of_managed_stack_pc_; 525b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers} 526ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYa#else 527ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYavoid Thread::PopNativeToManagedRecord(const NativeToManagedRecord&) { 528ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYa LOG(FATAL) << "Called non-LLVM method with LLVM"; 529ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYa} 530ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYa#endif 531b04f69f90d2594092bec5b294bbe7329d295bd66Ian Rogers 532d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughesstruct StackDumpVisitor : public Thread::StackVisitor { 5338e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes StackDumpVisitor(std::ostream& os, const Thread* thread) 53428ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers : last_method(NULL), last_line_number(0), repetition_count(0), os(os), thread(thread), 53528ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers frame_count(0) { 536d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 537d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 538bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers virtual ~StackDumpVisitor() { 539d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 540d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 541530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes bool VisitFrame(const Frame& frame, uintptr_t pc) { 5429086572fa809d1a19d886b467e4da3ce42016982Ian Rogers if (!frame.HasMethod()) { 543530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes return true; 5449086572fa809d1a19d886b467e4da3ce42016982Ian Rogers } 54528ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers const int kMaxRepetition = 3; 546d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes Method* m = frame.GetMethod(); 547540a5b768bf5f76bd71ebf66612fb2298899480fTDYa#if !defined(ART_USE_LLVM_COMPILER) 548d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes Class* c = m->GetDeclaringClass(); 5498e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 550b861dc0077342d5a1d2dd9cade3f6990620778ecIan Rogers const DexCache* dex_cache = c->GetDexCache(); 551b861dc0077342d5a1d2dd9cade3f6990620778ecIan Rogers int line_number = -1; 552b861dc0077342d5a1d2dd9cade3f6990620778ecIan Rogers if (dex_cache != NULL) { // be tolerant of bad input 553b861dc0077342d5a1d2dd9cade3f6990620778ecIan Rogers const DexFile& dex_file = class_linker->FindDexFile(dex_cache); 554b861dc0077342d5a1d2dd9cade3f6990620778ecIan Rogers line_number = dex_file.GetLineNumFromPC(m, m->ToDexPC(pc)); 555b861dc0077342d5a1d2dd9cade3f6990620778ecIan Rogers } 556540a5b768bf5f76bd71ebf66612fb2298899480fTDYa#else 557540a5b768bf5f76bd71ebf66612fb2298899480fTDYa // Compiler_LLVM stores line_number in the ShadowFrame, and passes it to visitor. 558540a5b768bf5f76bd71ebf66612fb2298899480fTDYa int line_number = static_cast<int>(pc); 559540a5b768bf5f76bd71ebf66612fb2298899480fTDYa#endif 56028ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers if (line_number == last_line_number && last_method == m) { 56128ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers repetition_count++; 562d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } else { 56328ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers if (repetition_count >= kMaxRepetition) { 56428ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers os << " ... repeated " << (repetition_count - kMaxRepetition) << " times\n"; 56528ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers } 56628ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers repetition_count = 0; 56728ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers last_line_number = line_number; 56828ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers last_method = m; 56928ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers } 57028ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers if (repetition_count < kMaxRepetition) { 57128ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers os << " at " << PrettyMethod(m, false); 57228ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers if (m->IsNative()) { 57328ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers os << "(Native method)"; 57428ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers } else { 5756d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers mh.ChangeMethod(m); 5766d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers const char* source_file(mh.GetDeclaringClassSourceFile()); 5776d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers os << "(" << (source_file != NULL ? source_file : "unavailable") 57828ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers << ":" << line_number << ")"; 57928ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers } 58028ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers os << "\n"; 581d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 5828e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 5838e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes if (frame_count++ == 0) { 5848e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Monitor::DescribeWait(os, thread); 5858e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 586530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes return true; 587d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 5886d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers MethodHelper mh; 58928ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers Method* last_method; 59028ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers int last_line_number; 59128ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers int repetition_count; 592d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes std::ostream& os; 5938e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes const Thread* thread; 5948e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes int frame_count; 595d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes}; 596d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 597d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughesvoid Thread::DumpStack(std::ostream& os) const { 598ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes // If we're currently in native code, dump that stack before dumping the managed stack. 59934e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes if (GetState() == kNative || GetState() == kVmWait) { 600ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes DumpNativeStack(os); 601ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes } 6028e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes StackDumpVisitor dumper(os, this); 603d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes WalkStack(&dumper); 604e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes} 605e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes 60634e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughesvoid Thread::SetStateWithoutSuspendCheck(ThreadState new_state) { 607a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes volatile void* raw = reinterpret_cast<volatile void*>(&state_); 608a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes volatile int32_t* addr = reinterpret_cast<volatile int32_t*>(raw); 609a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes android_atomic_release_store(new_state, addr); 610a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes} 611a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes 61234e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott HughesThreadState Thread::SetState(ThreadState new_state) { 61334e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes ThreadState old_state = state_; 6148d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes if (old_state == new_state) { 6158d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes return old_state; 6168d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } 6178d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes 6188d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes volatile void* raw = reinterpret_cast<volatile void*>(&state_); 6198d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes volatile int32_t* addr = reinterpret_cast<volatile int32_t*>(raw); 6208d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes 62134e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes if (new_state == kRunnable) { 6228d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes /* 62334e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes * Change our status to kRunnable. The transition requires 62481ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes * that we check for pending suspension, because the runtime considers 6258d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * us to be "asleep" in all other states, and another thread could 6268d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * be performing a GC now. 6278d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 6288d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * The order of operations is very significant here. One way to 6298d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * do this wrong is: 6308d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 6318d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * GCing thread Our thread (in kNative) 6328d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * ------------ ---------------------- 6338d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * check suspend count (== 0) 6348d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * SuspendAllThreads() 6358d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * grab suspend-count lock 6368d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * increment all suspend counts 6378d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * release suspend-count lock 6388d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * check thread state (== kNative) 6398d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * all are suspended, begin GC 6408d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * set state to kRunnable 6418d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * (continue executing) 6428d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 6438d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * We can correct this by grabbing the suspend-count lock and 6448d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * performing both of our operations (check suspend count, set 6458d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * state) while holding it, now we need to grab a mutex on every 6468d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * transition to kRunnable. 6478d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 6488d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * What we do instead is change the order of operations so that 6498d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * the transition to kRunnable happens first. If we then detect 6508d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * that the suspend count is nonzero, we switch to kSuspended. 6518d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 6528d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * Appropriate compiler and memory barriers are required to ensure 6538d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * that the operations are observed in the expected order. 6548d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 6558d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * This does create a small window of opportunity where a GC in 6568d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * progress could observe what appears to be a running thread (if 6578d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * it happens to look between when we set to kRunnable and when we 6588d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * switch to kSuspended). At worst this only affects assertions 6598d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * and thread logging. (We could work around it with some sort 6608d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * of intermediate "pre-running" state that is generally treated 6618d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * as equivalent to running, but that doesn't seem worthwhile.) 6628d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 6638d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * We can also solve this by combining the "status" and "suspend 6648d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * count" fields into a single 32-bit value. This trades the 6658d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * store/load barrier on transition to kRunnable for an atomic RMW 6668d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * op on all transitions and all suspend count updates (also, all 6678d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * accesses to status or the thread count require bit-fiddling). 6688d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * It also eliminates the brief transition through kRunnable when 6698d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * the thread is supposed to be suspended. This is possibly faster 6708d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * on SMP and slightly more correct, but less convenient. 6718d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes */ 6728d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes android_atomic_acquire_store(new_state, addr); 67306e3ad4a651c2c58dba5e865cd06d2f98462bf1dElliott Hughes ANNOTATE_IGNORE_READS_BEGIN(); 67406e3ad4a651c2c58dba5e865cd06d2f98462bf1dElliott Hughes int suspend_count = suspend_count_; 67506e3ad4a651c2c58dba5e865cd06d2f98462bf1dElliott Hughes ANNOTATE_IGNORE_READS_END(); 67606e3ad4a651c2c58dba5e865cd06d2f98462bf1dElliott Hughes if (suspend_count != 0) { 6778d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes Runtime::Current()->GetThreadList()->FullSuspendCheck(this); 6788d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } 6798d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } else { 6808d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes /* 68134e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes * Not changing to kRunnable. No additional work required. 6828d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * 6838d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * We use a releasing store to ensure that, if we were runnable, 6848d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * any updates we previously made to objects on the managed heap 6858d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes * will be observed before the state change. 6868d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes */ 6878d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes android_atomic_release_store(new_state, addr); 6888d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } 6898d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes 6908d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes return old_state; 6918d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes} 6928d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes 693761928d24e4e7ed7776b52243eaf9095ad35f448Elliott Hughesbool Thread::IsSuspended() { 69406e3ad4a651c2c58dba5e865cd06d2f98462bf1dElliott Hughes ANNOTATE_IGNORE_READS_BEGIN(); 69506e3ad4a651c2c58dba5e865cd06d2f98462bf1dElliott Hughes int suspend_count = suspend_count_; 69606e3ad4a651c2c58dba5e865cd06d2f98462bf1dElliott Hughes ANNOTATE_IGNORE_READS_END(); 69734e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes return suspend_count != 0 && GetState() != kRunnable; 698761928d24e4e7ed7776b52243eaf9095ad35f448Elliott Hughes} 699761928d24e4e7ed7776b52243eaf9095ad35f448Elliott Hughes 700ff7380681c50129ff689a11ac0f49512b4be7295Elliott Hughesstatic void ReportThreadSuspendTimeout(Thread* waiting_thread) { 701ff7380681c50129ff689a11ac0f49512b4be7295Elliott Hughes Runtime* runtime = Runtime::Current(); 702ff7380681c50129ff689a11ac0f49512b4be7295Elliott Hughes std::ostringstream ss; 703ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes ss << "Thread suspend timeout waiting for thread " << *waiting_thread << "\n"; 704ff7380681c50129ff689a11ac0f49512b4be7295Elliott Hughes runtime->DumpLockHolders(ss); 705ff7380681c50129ff689a11ac0f49512b4be7295Elliott Hughes ss << "\n"; 706ff7380681c50129ff689a11ac0f49512b4be7295Elliott Hughes runtime->GetThreadList()->DumpLocked(ss); 707ff7380681c50129ff689a11ac0f49512b4be7295Elliott Hughes LOG(FATAL) << ss.str(); 708ff7380681c50129ff689a11ac0f49512b4be7295Elliott Hughes} 709ff7380681c50129ff689a11ac0f49512b4be7295Elliott Hughes 7108d768a954b101a9532f980253ac46be2c53aba11Elliott Hughesvoid Thread::WaitUntilSuspended() { 71121a5bf2dc8c9cb3fbe5a30a88d1149c328e3aacaElliott Hughes static const useconds_t kTimeoutUs = 30 * 1000000; // 30s. 71221a5bf2dc8c9cb3fbe5a30a88d1149c328e3aacaElliott Hughes 71321a5bf2dc8c9cb3fbe5a30a88d1149c328e3aacaElliott Hughes useconds_t total_delay = 0; 7148d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes useconds_t delay = 0; 71534e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes while (GetState() == kRunnable) { 71621a5bf2dc8c9cb3fbe5a30a88d1149c328e3aacaElliott Hughes if (total_delay >= kTimeoutUs) { 717ff7380681c50129ff689a11ac0f49512b4be7295Elliott Hughes ReportThreadSuspendTimeout(this); 71821a5bf2dc8c9cb3fbe5a30a88d1149c328e3aacaElliott Hughes } 7198d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes useconds_t new_delay = delay * 2; 7208d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes CHECK_GE(new_delay, delay); 7218d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes delay = new_delay; 7228d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes if (delay == 0) { 7238d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes sched_yield(); 7248d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes delay = 10000; 7258d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } else { 7268d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes usleep(delay); 72721a5bf2dc8c9cb3fbe5a30a88d1149c328e3aacaElliott Hughes total_delay += delay; 7288d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } 7298d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes } 7308d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes} 7318d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes 732be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughesvoid Thread::ThreadExitCallback(void* arg) { 733be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes Thread* self = reinterpret_cast<Thread*>(arg); 734be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughes LOG(FATAL) << "Native thread exited without calling DetachCurrentThread: " << *self; 735b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro} 736b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 737be759c63c6bb58b76ac71cad2c5a736bd31f374dElliott Hughesvoid Thread::Startup() { 738b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro // Allocate a TLS slot. 7398d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes CHECK_PTHREAD_CALL(pthread_key_create, (&Thread::pthread_key_self_, Thread::ThreadExitCallback), "self key"); 740b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 741b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro // Double-check the TLS slot allocation. 742b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro if (pthread_getspecific(pthread_key_self_) != NULL) { 7433d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes LOG(FATAL) << "Newly-created pthread TLS slot is not NULL"; 744b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro } 745038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes} 746038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes 7478e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes// TODO: make more accessible? 748ab7b9dcbfc4264a0bc4889c3e463ff88a67f6a30Elliott Hughesstatic Class* FindClassOrDie(ClassLinker* class_linker, const char* descriptor) { 7498e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Class* c = class_linker->FindSystemClass(descriptor); 7508e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes CHECK(c != NULL) << descriptor; 7518e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return c; 7528e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes} 7538e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 7548e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes// TODO: make more accessible? 755ab7b9dcbfc4264a0bc4889c3e463ff88a67f6a30Elliott Hughesstatic Field* FindFieldOrDie(Class* c, const char* name, const char* descriptor) { 7566b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom Field* f = c->FindDeclaredInstanceField(name, descriptor); 7576b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom CHECK(f != NULL) << PrettyClass(c) << " " << name << " " << descriptor; 7588e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return f; 7598e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes} 7608e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 7618e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes// TODO: make more accessible? 762ab7b9dcbfc4264a0bc4889c3e463ff88a67f6a30Elliott Hughesstatic Method* FindMethodOrDie(Class* c, const char* name, const char* signature) { 7638e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Method* m = c->FindVirtualMethod(name, signature); 7648e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes CHECK(m != NULL) << PrettyClass(c) << " " << name << " " << signature; 7658e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return m; 7668e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes} 7678e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 768462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes// TODO: make more accessible? 769ab7b9dcbfc4264a0bc4889c3e463ff88a67f6a30Elliott Hughesstatic Field* FindStaticFieldOrDie(Class* c, const char* name, const char* descriptor) { 770462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes Field* f = c->FindDeclaredStaticField(name, descriptor); 771462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes CHECK(f != NULL) << PrettyClass(c) << " " << name << " " << descriptor; 772462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes return f; 773462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes} 774462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes 775038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesvoid Thread::FinishStartup() { 7769a6bae896a2f003d7216603bf29771d105c10ca4Jesse Wilson CHECK(Runtime::Current()->IsStarted()); 777b82b6878fb000d4731063b1bf15c39ff7c50b61fBrian Carlstrom Thread* self = Thread::Current(); 778b82b6878fb000d4731063b1bf15c39ff7c50b61fBrian Carlstrom 779b82b6878fb000d4731063b1bf15c39ff7c50b61fBrian Carlstrom // Need to be kRunnable for FindClass 78034e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes ScopedThreadStateChange tsc(self, kRunnable); 781b82b6878fb000d4731063b1bf15c39ff7c50b61fBrian Carlstrom 782038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes // Now the ClassLinker is ready, we can find the various Class*, Field*, and Method*s we need. 783038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 7848e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 7858e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Class* Thread_class = FindClassOrDie(class_linker, "Ljava/lang/Thread;"); 7868e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Class* UncaughtExceptionHandler_class = FindClassOrDie(class_linker, "Ljava/lang/Thread$UncaughtExceptionHandler;"); 787462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes gThreadGroup = FindClassOrDie(class_linker, "Ljava/lang/ThreadGroup;"); 7888e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gThreadLock = FindClassOrDie(class_linker, "Ljava/lang/ThreadLock;"); 7898e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 7906b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom gThread_daemon = FindFieldOrDie(Thread_class, "daemon", "Z"); 7916b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom gThread_group = FindFieldOrDie(Thread_class, "group", "Ljava/lang/ThreadGroup;"); 7926b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom gThread_lock = FindFieldOrDie(Thread_class, "lock", "Ljava/lang/ThreadLock;"); 7936b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom gThread_name = FindFieldOrDie(Thread_class, "name", "Ljava/lang/String;"); 7946b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom gThread_priority = FindFieldOrDie(Thread_class, "priority", "I"); 7956b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom gThread_uncaughtHandler = FindFieldOrDie(Thread_class, "uncaughtHandler", "Ljava/lang/Thread$UncaughtExceptionHandler;"); 7966b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom gThread_vmData = FindFieldOrDie(Thread_class, "vmData", "I"); 797462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes gThreadGroup_name = FindFieldOrDie(gThreadGroup, "name", "Ljava/lang/String;"); 798462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes gThreadGroup_mMain = FindStaticFieldOrDie(gThreadGroup, "mMain", "Ljava/lang/ThreadGroup;"); 799462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes gThreadGroup_mSystem = FindStaticFieldOrDie(gThreadGroup, "mSystem", "Ljava/lang/ThreadGroup;"); 8006b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom gThreadLock_thread = FindFieldOrDie(gThreadLock, "thread", "Ljava/lang/Thread;"); 8018e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 8028e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gThread_run = FindMethodOrDie(Thread_class, "run", "()V"); 803462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes gThreadGroup_removeThread = FindMethodOrDie(gThreadGroup, "removeThread", "(Ljava/lang/Thread;)V"); 8048e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes gUncaughtExceptionHandler_uncaughtException = FindMethodOrDie(UncaughtExceptionHandler_class, 8058e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes "uncaughtException", "(Ljava/lang/Thread;Ljava/lang/Throwable;)V"); 80601158d7a57c8321370667a6045220237d16e0da8Elliott Hughes 80701158d7a57c8321370667a6045220237d16e0da8Elliott Hughes // Finish attaching the main thread. 808462c94449720e0dc6b93d7138d835d345ccf67aeElliott Hughes Thread::Current()->CreatePeer("main", false, Thread::GetMainThreadGroup()); 8099a6bae896a2f003d7216603bf29771d105c10ca4Jesse Wilson 8109a6bae896a2f003d7216603bf29771d105c10ca4Jesse Wilson InitBoxingMethods(); 8119a6bae896a2f003d7216603bf29771d105c10ca4Jesse Wilson class_linker->RunRootClinits(); 812b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro} 813b557353b22c728eecbd1c68593b482622c7782a8Carl Shapiro 814c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughesvoid Thread::Shutdown() { 8158d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes CHECK_PTHREAD_CALL(pthread_key_delete, (Thread::pthread_key_self_), "self key"); 816c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes} 817c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes 8188e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughesuint32_t Thread::LockOwnerFromThreadLock(Object* thread_lock) { 8198e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes if (thread_lock == NULL || thread_lock->GetClass() != gThreadLock) { 8208e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return ThreadList::kInvalidId; 8218e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 8228e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Object* managed_thread = gThreadLock_thread->GetObject(thread_lock); 8238e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes if (managed_thread == NULL) { 8248e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return ThreadList::kInvalidId; 8258e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 8268e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes uintptr_t vmData = static_cast<uintptr_t>(gThread_vmData->GetInt(managed_thread)); 8278e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Thread* thread = reinterpret_cast<Thread*>(vmData); 8288e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes if (thread == NULL) { 8298e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return ThreadList::kInvalidId; 8308e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 8318e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return thread->GetThinLockId(); 8328e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes} 8338e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 834dcc247493fd8fb243e335c3ec08e5e625896a47cElliott HughesThread::Thread() 83547179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes : thin_lock_id_(0), 83647179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes tid_(0), 83747179f76e3f03fe3eb21dfb081d50733ca316371Elliott Hughes peer_(NULL), 8388e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes top_of_managed_stack_(), 8398e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes top_of_managed_stack_pc_(0), 840e62934d85fbc2d935afdad57eeade39ecbd7440aElliott Hughes wait_mutex_(new Mutex("a thread wait mutex")), 841e62934d85fbc2d935afdad57eeade39ecbd7440aElliott Hughes wait_cond_(new ConditionVariable("a thread wait condition variable")), 8428daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes wait_monitor_(NULL), 8438daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes interrupted_(false), 844dc33ad5db2dc6ed9b76d5219888626a604debbe1Elliott Hughes wait_next_(NULL), 8458e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes monitor_enter_object_(NULL), 846dc33ad5db2dc6ed9b76d5219888626a604debbe1Elliott Hughes card_table_(0), 8478daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes stack_end_(NULL), 848dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes native_to_managed_record_(NULL), 849dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes top_sirt_(NULL), 850f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien top_shadow_frame_(NULL), 851dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes jni_env_(NULL), 85234e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes state_(kNative), 853dc33ad5db2dc6ed9b76d5219888626a604debbe1Elliott Hughes self_(NULL), 854dc33ad5db2dc6ed9b76d5219888626a604debbe1Elliott Hughes runtime_(NULL), 855dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes exception_(NULL), 856dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes suspend_count_(0), 857234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes debug_suspend_count_(0), 85885d1545e985ac689db4bad7849880e843707c862Elliott Hughes class_loader_override_(NULL), 859418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughes long_jump_context_(NULL), 860726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes throwing_OutOfMemoryError_(false), 861475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes pre_allocated_OutOfMemoryError_(NULL), 862e343b76af81a005ef64f5e75a555389fd9147dabjeffhao debug_invoke_req_(new DebugInvokeReq), 863899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes trace_stack_(new std::vector<TraceStackFrame>), 864899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes name_(new std::string("<native thread without managed peer>")) { 865f5a7a476e7ea63e094ff0f011dccc170607e6f6bElliott Hughes CHECK_EQ((sizeof(Thread) % 4), 0U) << sizeof(Thread); 866ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes memset(&held_mutexes_[0], 0, sizeof(held_mutexes_)); 867dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes} 868dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes 869ab7b9dcbfc4264a0bc4889c3e463ff88a67f6a30Elliott Hughesstatic void MonitorExitVisitor(const Object* object, void*) { 87002b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes Object* entered_monitor = const_cast<Object*>(object); 871ab7b9dcbfc4264a0bc4889c3e463ff88a67f6a30Elliott Hughes LOG(WARNING) << "Calling MonitorExit on object " << object << " (" << PrettyTypeOf(object) << ")" 872ab7b9dcbfc4264a0bc4889c3e463ff88a67f6a30Elliott Hughes << " left locked by native thread " << *Thread::Current() << " which is detaching"; 8735f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes entered_monitor->MonitorExit(Thread::Current()); 87402b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes} 87502b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 876c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughesvoid Thread::Destroy() { 87702b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes // On thread detach, all monitors entered with JNI MonitorEnter are automatically exited. 87893e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes if (jni_env_ != NULL) { 87993e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes jni_env_->monitors.VisitRoots(MonitorExitVisitor, NULL); 88093e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes } 88102b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 88229f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes if (peer_ != NULL) { 883c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes Thread* self = this; 88429f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes 885534da072d3db3beecbd74e5aa23d630d73695f2cElliott Hughes // We may need to call user-supplied managed code. 88634e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes SetState(kRunnable); 887534da072d3db3beecbd74e5aa23d630d73695f2cElliott Hughes 888534da072d3db3beecbd74e5aa23d630d73695f2cElliott Hughes HandleUncaughtExceptions(); 889534da072d3db3beecbd74e5aa23d630d73695f2cElliott Hughes RemoveFromThreadGroup(); 890534da072d3db3beecbd74e5aa23d630d73695f2cElliott Hughes 89129f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // this.vmData = 0; 89293e74e8d879270071c3aa163f8495ada8d21f42fElliott Hughes SetVmData(peer_, NULL); 89302b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 894c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes Dbg::PostThreadDeath(self); 89502b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 89629f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // Thread.join() is implemented as an Object.wait() on the Thread.lock 89729f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // object. Signal anyone who is waiting. 898038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes Object* lock = gThread_lock->GetObject(peer_); 899038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes // (This conditional is only needed for tests, where Thread.lock won't have been set.) 9005f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (lock != NULL) { 9015f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes lock->MonitorEnter(self); 9025f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes lock->NotifyAll(); 9035f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes lock->MonitorExit(self); 9045f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 9055f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 906c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes} 90702b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 908c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott HughesThread::~Thread() { 909c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes delete jni_env_; 91002b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes jni_env_ = NULL; 91102b48d1dae0c3adc01ef6668226fb648fb52990aElliott Hughes 91234e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes SetState(kTerminated); 91385d1545e985ac689db4bad7849880e843707c862Elliott Hughes 91485d1545e985ac689db4bad7849880e843707c862Elliott Hughes delete wait_cond_; 91585d1545e985ac689db4bad7849880e843707c862Elliott Hughes delete wait_mutex_; 91685d1545e985ac689db4bad7849880e843707c862Elliott Hughes 917776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#if !defined(ART_USE_LLVM_COMPILER) 91885d1545e985ac689db4bad7849880e843707c862Elliott Hughes delete long_jump_context_; 919776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#endif 920475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes 921475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes delete debug_invoke_req_; 922e343b76af81a005ef64f5e75a555389fd9147dabjeffhao delete trace_stack_; 923899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes delete name_; 924d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes 925d8af1592a97f7447ecf93f85098cb36340ab0fe2Elliott Hughes TearDownAlternateSignalStack(); 926c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes} 927c1674ed06662420213441ff2b818f2f71f9098dcElliott Hughes 928accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughesvoid Thread::HandleUncaughtExceptions() { 929accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes if (!IsExceptionPending()) { 930accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes return; 931accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes } 932accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 933accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes // Get and clear the exception. 934accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes Object* exception = GetException(); 935accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes ClearException(); 936accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 937accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes // If the thread has its own handler, use that. 938accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes Object* handler = gThread_uncaughtHandler->GetObject(peer_); 939accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes if (handler == NULL) { 940accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes // Otherwise use the thread group's default handler. 941a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes handler = GetThreadGroup(); 942accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes } 943accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 944accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes // Call the handler. 945accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes Method* m = handler->GetClass()->FindVirtualMethodForVirtualOrInterface(gUncaughtExceptionHandler_uncaughtException); 94677405796564c6c1353807cda18b28678a719bd68Elliott Hughes JValue args[2]; 947f24d3cedd395690f6904aaac80f84a100420f7a3Elliott Hughes args[0].SetL(peer_); 948f24d3cedd395690f6904aaac80f84a100420f7a3Elliott Hughes args[1].SetL(exception); 94977405796564c6c1353807cda18b28678a719bd68Elliott Hughes m->Invoke(this, handler, args, NULL); 950accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 951accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes // If the handler threw, clear that exception too. 952accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes ClearException(); 953accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes} 9544514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom 955a215526d5c789cbef0f81a1f9aba22541a841ccaElliott HughesObject* Thread::GetThreadGroup() const { 956a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes return gThread_group->GetObject(peer_); 957a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes} 958a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes 9594514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstromvoid Thread::RemoveFromThreadGroup() { 9604514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom // this.group.removeThread(this); 9614514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom // group can be null if we're in the compiler or a test. 962a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes Object* group = GetThreadGroup(); 9634514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom if (group != NULL) { 9644514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom Method* m = group->GetClass()->FindVirtualMethodForVirtualOrInterface(gThreadGroup_removeThread); 96577405796564c6c1353807cda18b28678a719bd68Elliott Hughes JValue args[1]; 966f24d3cedd395690f6904aaac80f84a100420f7a3Elliott Hughes args[0].SetL(peer_); 96777405796564c6c1353807cda18b28678a719bd68Elliott Hughes m->Invoke(this, group, args, NULL); 9684514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom } 9694514d3c0e69a49f5dbe19138330a2bb2aee36d63Brian Carlstrom} 970accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 971408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogerssize_t Thread::NumSirtReferences() { 972a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers size_t count = 0; 97340381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->GetLink()) { 974a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers count += cur->NumberOfReferences(); 975a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers } 976a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers return count; 977a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers} 978a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers 97928f1a147d77ec772db98bed890b50a9ddcff2365TDYasize_t Thread::NumShadowFrameReferences() { 98028f1a147d77ec772db98bed890b50a9ddcff2365TDYa size_t count = 0; 98128f1a147d77ec772db98bed890b50a9ddcff2365TDYa for (ShadowFrame* cur = top_shadow_frame_; cur; cur = cur->GetLink()) { 98228f1a147d77ec772db98bed890b50a9ddcff2365TDYa count += cur->NumberOfReferences(); 98328f1a147d77ec772db98bed890b50a9ddcff2365TDYa } 98428f1a147d77ec772db98bed890b50a9ddcff2365TDYa return count; 98528f1a147d77ec772db98bed890b50a9ddcff2365TDYa} 98628f1a147d77ec772db98bed890b50a9ddcff2365TDYa 987408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogersbool Thread::SirtContains(jobject obj) { 988408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers Object** sirt_entry = reinterpret_cast<Object**>(obj); 98940381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->GetLink()) { 99040381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom if (cur->Contains(sirt_entry)) { 991a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers return true; 992a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers } 993a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers } 994a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers return false; 995a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers} 996a8cd9f4a849835af7856f9c9782ea59e11ddef85Ian Rogers 99728f1a147d77ec772db98bed890b50a9ddcff2365TDYabool Thread::ShadowFrameContains(jobject obj) { 99828f1a147d77ec772db98bed890b50a9ddcff2365TDYa Object** shadow_frame_entry = reinterpret_cast<Object**>(obj); 99928f1a147d77ec772db98bed890b50a9ddcff2365TDYa for (ShadowFrame* cur = top_shadow_frame_; cur; cur = cur->GetLink()) { 100028f1a147d77ec772db98bed890b50a9ddcff2365TDYa if (cur->Contains(shadow_frame_entry)) { 100128f1a147d77ec772db98bed890b50a9ddcff2365TDYa return true; 100228f1a147d77ec772db98bed890b50a9ddcff2365TDYa } 100328f1a147d77ec772db98bed890b50a9ddcff2365TDYa } 100428f1a147d77ec772db98bed890b50a9ddcff2365TDYa return false; 100528f1a147d77ec772db98bed890b50a9ddcff2365TDYa} 100628f1a147d77ec772db98bed890b50a9ddcff2365TDYa 100728f1a147d77ec772db98bed890b50a9ddcff2365TDYabool Thread::StackReferencesContain(jobject obj) { 100828f1a147d77ec772db98bed890b50a9ddcff2365TDYa return SirtContains(obj) || ShadowFrameContains(obj); 100928f1a147d77ec772db98bed890b50a9ddcff2365TDYa} 101028f1a147d77ec772db98bed890b50a9ddcff2365TDYa 10118dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liaovoid Thread::SirtVisitRoots(Heap::RootVisitor* visitor, void* arg) { 101240381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom for (StackIndirectReferenceTable* cur = top_sirt_; cur; cur = cur->GetLink()) { 10138dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao size_t num_refs = cur->NumberOfReferences(); 10148dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao for (size_t j = 0; j < num_refs; j++) { 101540381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom Object* object = cur->GetReference(j); 10165e73f9c8990819054f8e0a60061ee8beff1e024aBrian Carlstrom if (object != NULL) { 10175e73f9c8990819054f8e0a60061ee8beff1e024aBrian Carlstrom visitor(object, arg); 10185e73f9c8990819054f8e0a60061ee8beff1e024aBrian Carlstrom } 10198dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao } 10208dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao } 10218dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao} 10228dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao 1023f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chienvoid Thread::ShadowFrameVisitRoots(Heap::RootVisitor* visitor, void* arg) { 1024f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien for (ShadowFrame* cur = top_shadow_frame_; cur; cur = cur->GetLink()) { 1025f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien size_t num_refs = cur->NumberOfReferences(); 1026f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien for (size_t j = 0; j < num_refs; j++) { 1027f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien Object* object = cur->GetReference(j); 1028f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien if (object != NULL) { 1029f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien visitor(object, arg); 1030f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien } 1031f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien } 1032f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien } 1033f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien} 1034f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien 1035408f79aeb676251ba35667a64e86c20638d7cb0bIan RogersObject* Thread::DecodeJObject(jobject obj) { 10360cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers DCHECK(CanAccessDirectReferences()); 1037408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers if (obj == NULL) { 1038408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers return NULL; 1039408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 1040408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers IndirectRef ref = reinterpret_cast<IndirectRef>(obj); 1041408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers IndirectRefKind kind = GetIndirectRefKind(ref); 1042408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers Object* result; 1043408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers switch (kind) { 1044408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers case kLocal: 1045408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers { 104669f5bc6759f256a146eefd8a7141d39fcc3b0421Elliott Hughes IndirectReferenceTable& locals = jni_env_->locals; 1047cf4c6c41b0084dc4567ff709fb8ce9ebd72b26acElliott Hughes result = const_cast<Object*>(locals.Get(ref)); 1048408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers break; 1049408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 1050408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers case kGlobal: 1051408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers { 1052408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers JavaVMExt* vm = Runtime::Current()->GetJavaVM(); 1053408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers IndirectReferenceTable& globals = vm->globals; 1054408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers MutexLock mu(vm->globals_lock); 1055cf4c6c41b0084dc4567ff709fb8ce9ebd72b26acElliott Hughes result = const_cast<Object*>(globals.Get(ref)); 1056408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers break; 1057408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 1058408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers case kWeakGlobal: 1059408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers { 1060408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers JavaVMExt* vm = Runtime::Current()->GetJavaVM(); 1061408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers IndirectReferenceTable& weak_globals = vm->weak_globals; 1062408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers MutexLock mu(vm->weak_globals_lock); 1063cf4c6c41b0084dc4567ff709fb8ce9ebd72b26acElliott Hughes result = const_cast<Object*>(weak_globals.Get(ref)); 1064408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers if (result == kClearedJniWeakGlobal) { 1065408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers // This is a special case where it's okay to return NULL. 1066408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers return NULL; 1067408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 1068408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers break; 1069408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 1070408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers case kSirtOrInvalid: 1071408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers default: 1072408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers // TODO: make stack indirect reference table lookup more efficient 1073408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers // Check if this is a local reference in the SIRT 107428f1a147d77ec772db98bed890b50a9ddcff2365TDYa if (StackReferencesContain(obj)) { 10750cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers result = *reinterpret_cast<Object**>(obj); // Read from SIRT 1076c2dc62d1c9241dfe880a81698713c314b13aa63fElliott Hughes } else if (Runtime::Current()->GetJavaVM()->work_around_app_jni_bugs) { 1077408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers // Assume an invalid local reference is actually a direct pointer. 1078408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers result = reinterpret_cast<Object*>(obj); 1079408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } else { 1080a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes result = kInvalidIndirectRefObject; 1081408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 1082408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 1083408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers 1084408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers if (result == NULL) { 1085a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes LOG(ERROR) << "JNI ERROR (app bug): use of deleted " << kind << ": " << obj; 1086a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes JniAbort(NULL); 1087a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes } else { 1088a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes if (result != kInvalidIndirectRefObject) { 1089b3bd5f07884f5a1f2b84224363b1372d7c28d447Elliott Hughes Runtime::Current()->GetHeap()->VerifyObject(result); 1090a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes } 1091408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers } 1092408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers return result; 1093408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers} 1094408f79aeb676251ba35667a64e86c20638d7cb0bIan Rogers 10959b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liaoclass CountStackDepthVisitor : public Thread::StackVisitor { 10969b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao public: 109729f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes CountStackDepthVisitor() : depth_(0), skip_depth_(0), skipping_(true) {} 1098d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes 10991bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughes bool VisitFrame(const Frame& frame, uintptr_t /*pc*/) { 110029f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // We want to skip frames up to and including the exception's constructor. 11019086572fa809d1a19d886b467e4da3ce42016982Ian Rogers // Note we also skip the frame if it doesn't have a method (namely the callee 11029086572fa809d1a19d886b467e4da3ce42016982Ian Rogers // save frame) 11035167c97a4f672ba821453418e3943988fabbfc43Ian Rogers if (skipping_ && frame.HasMethod() && 11045167c97a4f672ba821453418e3943988fabbfc43Ian Rogers !Throwable::GetJavaLangThrowable()->IsAssignableFrom(frame.GetMethod()->GetDeclaringClass())) { 110529f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes skipping_ = false; 110629f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } 110729f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes if (!skipping_) { 11086b0870dfaaa2e69a900ad7ed88e4e1d3697445aaIan Rogers if (frame.HasMethod()) { // ignore callee save frames 11096b0870dfaaa2e69a900ad7ed88e4e1d3697445aaIan Rogers ++depth_; 11106b0870dfaaa2e69a900ad7ed88e4e1d3697445aaIan Rogers } 111129f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } else { 111229f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes ++skip_depth_; 111329f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } 1114530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes return true; 11159b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 11169b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 11179b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao int GetDepth() const { 1118aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers return depth_; 11199b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 11209b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 112129f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes int GetSkipDepth() const { 112229f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes return skip_depth_; 112329f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } 112429f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes 11259b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao private: 1126aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers uint32_t depth_; 112729f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes uint32_t skip_depth_; 112829f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes bool skipping_; 11299b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao}; 11309b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 1131aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogersclass BuildInternalStackTraceVisitor : public Thread::StackVisitor { 11329b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao public: 1133283ed0d9db3084d0fee46d47cd73bf84bd0cfc16Ian Rogers explicit BuildInternalStackTraceVisitor(int skip_depth) 1134726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes : skip_depth_(skip_depth), count_(0), pc_trace_(NULL), method_trace_(NULL), local_ref_(NULL) { 1135283ed0d9db3084d0fee46d47cd73bf84bd0cfc16Ian Rogers } 1136283ed0d9db3084d0fee46d47cd73bf84bd0cfc16Ian Rogers 1137283ed0d9db3084d0fee46d47cd73bf84bd0cfc16Ian Rogers bool Init(int depth, ScopedJniThreadState& ts) { 1138aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Allocate method trace with an extra slot that will hold the PC trace 113901158d7a57c8321370667a6045220237d16e0da8Elliott Hughes method_trace_ = Runtime::Current()->GetClassLinker()->AllocObjectArray<Object>(depth + 1); 1140726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes if (method_trace_ == NULL) { 1141283ed0d9db3084d0fee46d47cd73bf84bd0cfc16Ian Rogers return false; 1142726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes } 1143aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Register a local reference as IntArray::Alloc may trigger GC 1144aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers local_ref_ = AddLocalReference<jobject>(ts.Env(), method_trace_); 1145aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers pc_trace_ = IntArray::Alloc(depth); 1146726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes if (pc_trace_ == NULL) { 1147283ed0d9db3084d0fee46d47cd73bf84bd0cfc16Ian Rogers return false; 1148726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes } 1149aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers#ifdef MOVING_GARBAGE_COLLECTOR 1150aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Re-read after potential GC 1151267364429db05d964c0922a8b4e49591c00bdc35Brian Carlstrom method_trace_ = Decode<ObjectArray<Object>*>(ts.Env(), local_ref_); 1152aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers#endif 1153aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Save PC trace in last element of method trace, also places it into the 1154aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // object graph. 1155aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers method_trace_->Set(depth, pc_trace_); 1156283ed0d9db3084d0fee46d47cd73bf84bd0cfc16Ian Rogers return true; 11579b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 11589b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 1159aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers virtual ~BuildInternalStackTraceVisitor() {} 11609b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 1161530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes bool VisitFrame(const Frame& frame, uintptr_t pc) { 1162726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes if (method_trace_ == NULL || pc_trace_ == NULL) { 1163530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes return true; // We're probably trying to fillInStackTrace for an OutOfMemoryError. 1164726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes } 116529f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes if (skip_depth_ > 0) { 116629f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes skip_depth_--; 1167530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes return true; 116829f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes } 11696b0870dfaaa2e69a900ad7ed88e4e1d3697445aaIan Rogers if (!frame.HasMethod()) { 1170530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes return true; // ignore callee save frames 11716b0870dfaaa2e69a900ad7ed88e4e1d3697445aaIan Rogers } 1172aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers method_trace_->Set(count_, frame.GetMethod()); 1173bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers pc_trace_->Set(count_, pc); 1174aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers ++count_; 1175530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes return true; 11769b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 11779b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 1178aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers jobject GetInternalStackTrace() const { 1179aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers return local_ref_; 11809b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 11819b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 11829b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao private: 118329f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes // How many more frames to skip. 118429f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes int32_t skip_depth_; 1185aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Current position down stack trace 1186aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers uint32_t count_; 1187aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Array of return PC values 1188aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers IntArray* pc_trace_; 1189aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // An array of the methods on the stack, the last entry is a reference to the 1190aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // PC trace 1191aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers ObjectArray<Object>* method_trace_; 1192aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Local indirect reference table entry for method trace 1193aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers jobject local_ref_; 11949b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao}; 11959b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao 1196776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#if !defined(ART_USE_LLVM_COMPILER) 1197a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes// TODO: remove this. 1198ab7b9dcbfc4264a0bc4889c3e463ff88a67f6a30Elliott Hughesstatic uintptr_t ManglePc(uintptr_t pc) { 1199a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes // Move the PC back 2 bytes as a call will frequently terminate the 1200a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes // decoding of a particular instruction and we want to make sure we 1201a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes // get the Dex PC of the instruction with the call and not the 1202a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes // instruction following. 1203a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes if (pc > 0) { pc -= 2; } 1204a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes return pc; 1205a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes} 1206776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#endif 1207a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes 1208a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes// TODO: remove this. 1209ab7b9dcbfc4264a0bc4889c3e463ff88a67f6a30Elliott Hughesstatic uintptr_t DemanglePc(uintptr_t pc) { 1210a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes // Revert mangling for the case where we need the PC to return to the upcall 12119a8a8889418e09bf2d4ae3f683fbb40a71c3c8f4Ian Rogers if (pc > 0) { pc += 2; } 12129a8a8889418e09bf2d4ae3f683fbb40a71c3c8f4Ian Rogers return pc; 1213a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes} 1214f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers 1215f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chienvoid Thread::PushShadowFrame(ShadowFrame* frame) { 1216f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien frame->SetLink(top_shadow_frame_); 1217f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien top_shadow_frame_ = frame; 1218f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien} 1219f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien 1220f7ad17e108b9357d7c94c6218a8521140a667f3dLogan ChienShadowFrame* Thread::PopShadowFrame() { 1221f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien CHECK(top_shadow_frame_ != NULL); 1222f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien ShadowFrame* frame = top_shadow_frame_; 1223f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien top_shadow_frame_ = frame->GetLink(); 1224f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien return frame; 1225f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien} 1226f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien 122740381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstromvoid Thread::PushSirt(StackIndirectReferenceTable* sirt) { 122840381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom sirt->SetLink(top_sirt_); 122940381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom top_sirt_ = sirt; 123040381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom} 123140381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom 123240381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian CarlstromStackIndirectReferenceTable* Thread::PopSirt() { 123340381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom CHECK(top_sirt_ != NULL); 123440381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom StackIndirectReferenceTable* sirt = top_sirt_; 123540381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom top_sirt_ = top_sirt_->GetLink(); 123640381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom return sirt; 123740381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom} 123840381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom 12399b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa#if !defined(ART_USE_LLVM_COMPILER) // LLVM use ShadowFrame 12409b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa 1241530fa005e2944d3b12712f80d974f0e753f568efElliott Hughesvoid Thread::WalkStack(StackVisitor* visitor, bool include_upcalls) const { 1242d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes Frame frame = GetTopOfStack(); 1243a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes uintptr_t pc = ManglePc(top_of_managed_stack_pc_); 1244a9ef3fd82bebc6370fc3ddbb094988feb6c83022jeffhao uint32_t trace_stack_depth = 0; 12459b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao // TODO: enable this CHECK after native_to_managed_record_ is initialized during startup. 12469b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao // CHECK(native_to_managed_record_ != NULL); 12479b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao NativeToManagedRecord* record = native_to_managed_record_; 124857b86d47b66322693a070185fadfb43cb9c12eabIan Rogers bool method_tracing_active = Runtime::Current()->IsMethodTracingActive(); 1249530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes while (frame.GetSP() != NULL) { 1250530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes for ( ; frame.GetMethod() != NULL; frame.Next()) { 1251f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom frame.GetMethod()->AssertPcIsWithinCode(pc); 1252530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes bool should_continue = visitor->VisitFrame(frame, pc); 125357b86d47b66322693a070185fadfb43cb9c12eabIan Rogers if (UNLIKELY(!should_continue)) { 1254530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes return; 1255530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes } 125657b86d47b66322693a070185fadfb43cb9c12eabIan Rogers uintptr_t return_pc = frame.GetReturnPC(); 125757b86d47b66322693a070185fadfb43cb9c12eabIan Rogers if (LIKELY(!method_tracing_active)) { 125857b86d47b66322693a070185fadfb43cb9c12eabIan Rogers pc = ManglePc(return_pc); 125957b86d47b66322693a070185fadfb43cb9c12eabIan Rogers } else { 12600791adc2249366c50684935a4c42ba5e58bc3746jeffhao // While profiling, the return pc is restored from the side stack, except when walking 12610791adc2249366c50684935a4c42ba5e58bc3746jeffhao // the stack for an exception where the side stack will be unwound in VisitFrame. 12620791adc2249366c50684935a4c42ba5e58bc3746jeffhao if (IsTraceExitPc(return_pc) && !include_upcalls) { 1263a9ef3fd82bebc6370fc3ddbb094988feb6c83022jeffhao TraceStackFrame trace_frame = GetTraceStackFrame(trace_stack_depth++); 126426c0a1a376d8cd639fe6b16594fcb55a723450c0jeffhao CHECK(trace_frame.method_ == frame.GetMethod()); 1265a9ef3fd82bebc6370fc3ddbb094988feb6c83022jeffhao pc = ManglePc(trace_frame.return_pc_); 126657b86d47b66322693a070185fadfb43cb9c12eabIan Rogers } else { 126757b86d47b66322693a070185fadfb43cb9c12eabIan Rogers pc = ManglePc(return_pc); 1268a9ef3fd82bebc6370fc3ddbb094988feb6c83022jeffhao } 1269a9ef3fd82bebc6370fc3ddbb094988feb6c83022jeffhao } 12709b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 1271530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes if (include_upcalls) { 1272530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes bool should_continue = visitor->VisitFrame(frame, pc); 1273530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes if (!should_continue) { 1274530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes return; 1275530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes } 1276530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes } 12779b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao if (record == NULL) { 1278530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes return; 12799b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao } 1280bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers // last_tos should return Frame instead of sp? 1281ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers frame.SetSP(reinterpret_cast<Method**>(record->last_top_of_managed_stack_)); 1282a43cb5e8fb29989dbb986b9b91a68cda150aa3c8Elliott Hughes pc = ManglePc(record->last_top_of_managed_stack_pc_); 1283bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers record = record->link_; 1284bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers } 1285bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers} 1286bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 12879b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa#else // defined(ART_USE_LLVM_COMPILER) // LLVM uses ShadowFrame 12889b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa 12899b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYavoid Thread::WalkStack(StackVisitor* visitor, bool /*include_upcalls*/) const { 12909b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa for (ShadowFrame* cur = top_shadow_frame_; cur; cur = cur->GetLink()) { 12919b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa Frame frame; 12929b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa frame.SetSP(reinterpret_cast<Method**>(reinterpret_cast<byte*>(cur) + 12939b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa ShadowFrame::MethodOffset())); 129402f01fedc5180d1d5a795674f8c5bd0a3ce974d2Shih-wei Liao bool should_continue = visitor->VisitFrame(frame, cur->GetLineNumber()); 12959b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa if (!should_continue) { 12969b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa return; 12979b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa } 12989b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa } 12999b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa} 13009b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa 13019b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa/* 13029b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * | | 13039b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * | | 13049b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * | | 13059b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * | . | 13069b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * | . | 13079b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * | . | 13089b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * | . | 13099b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * | Method* | 13109b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * | . | 13119b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * | . | <-- top_shadow_frame_ (ShadowFrame*) 13129b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * / +------------------------+ 13139b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * ->| . | 13149b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * . | . | 13159b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * . | . | 13169b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * /+------------------------+ 13179b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * / | . | 13189b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * / | . | 13199b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * --- | | . | 13209b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * | | | . | 13219b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * | | Method* | <-- frame.GetSP() (Method**) 13229b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * ShadowFrame \ | . | 13239b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * | ->| . | <-- cur (ShadowFrame*) 13249b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * --- /+------------------------+ 13259b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * / | . | 13269b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * / | . | 13279b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * --- | | . | 13289b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * | cur->GetLink() | | . | 13299b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * | | Method* | 13309b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * ShadowFrame \ | . | 13319b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * | ->| . | 13329b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * --- +------------------------+ 13339b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * | . | 13349b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * | . | 13359b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * | . | 13369b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa * +========================+ 13379b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa */ 13389b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa 13399b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa#endif 13409b2ba2ee9fafe46a08473657a2387fd20ab49a35TDYa 134101158d7a57c8321370667a6045220237d16e0da8Elliott Hughesjobject Thread::CreateInternalStackTrace(JNIEnv* env) const { 1342aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Compute depth of stack 13439b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao CountStackDepthVisitor count_visitor; 13449b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao WalkStack(&count_visitor); 13459b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao int32_t depth = count_visitor.GetDepth(); 134629f2742a478e4f5bd2ec7473410c5e5e7134a6f2Elliott Hughes int32_t skip_depth = count_visitor.GetSkipDepth(); 13474417536522fd2a9d8215d8672331984769c9520bShih-wei Liao 1348aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Transition into runnable state to work on Object*/Array* 134901158d7a57c8321370667a6045220237d16e0da8Elliott Hughes ScopedJniThreadState ts(env); 1350aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers 1351aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Build internal stack trace 135277f74c1455bd8d29deb8a9367cea35deb930ed10Ian Rogers BuildInternalStackTraceVisitor build_trace_visitor(skip_depth); 135377f74c1455bd8d29deb8a9367cea35deb930ed10Ian Rogers if (!build_trace_visitor.Init(depth, ts)) { 1354283ed0d9db3084d0fee46d47cd73bf84bd0cfc16Ian Rogers return NULL; // Allocation failed 1355283ed0d9db3084d0fee46d47cd73bf84bd0cfc16Ian Rogers } 1356267364429db05d964c0922a8b4e49591c00bdc35Brian Carlstrom WalkStack(&build_trace_visitor); 1357267364429db05d964c0922a8b4e49591c00bdc35Brian Carlstrom return build_trace_visitor.GetInternalStackTrace(); 1358aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers} 1359aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers 136001158d7a57c8321370667a6045220237d16e0da8Elliott HughesjobjectArray Thread::InternalStackTraceToStackTraceElementArray(JNIEnv* env, jobject internal, 136101158d7a57c8321370667a6045220237d16e0da8Elliott Hughes jobjectArray output_array, int* stack_depth) { 1362aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Transition into runnable state to work on Object*/Array* 1363aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers ScopedJniThreadState ts(env); 1364aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Decode the internal stack trace into the depth, method trace and PC trace 1365aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers ObjectArray<Object>* method_trace = 1366aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers down_cast<ObjectArray<Object>*>(Decode<Object*>(ts.Env(), internal)); 13679074b99bfbd5419e340f138a018b673ce71f77e8Ian Rogers int32_t depth = method_trace->GetLength() - 1; 1368aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth)); 1369aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers 1370aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 1371aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers 137201158d7a57c8321370667a6045220237d16e0da8Elliott Hughes jobjectArray result; 137301158d7a57c8321370667a6045220237d16e0da8Elliott Hughes ObjectArray<StackTraceElement>* java_traces; 137401158d7a57c8321370667a6045220237d16e0da8Elliott Hughes if (output_array != NULL) { 137501158d7a57c8321370667a6045220237d16e0da8Elliott Hughes // Reuse the array we were given. 137601158d7a57c8321370667a6045220237d16e0da8Elliott Hughes result = output_array; 137701158d7a57c8321370667a6045220237d16e0da8Elliott Hughes java_traces = reinterpret_cast<ObjectArray<StackTraceElement>*>(Decode<Array*>(env, 137801158d7a57c8321370667a6045220237d16e0da8Elliott Hughes output_array)); 137901158d7a57c8321370667a6045220237d16e0da8Elliott Hughes // ...adjusting the number of frames we'll write to not exceed the array length. 138001158d7a57c8321370667a6045220237d16e0da8Elliott Hughes depth = std::min(depth, java_traces->GetLength()); 138101158d7a57c8321370667a6045220237d16e0da8Elliott Hughes } else { 138201158d7a57c8321370667a6045220237d16e0da8Elliott Hughes // Create java_trace array and place in local reference table 138301158d7a57c8321370667a6045220237d16e0da8Elliott Hughes java_traces = class_linker->AllocStackTraceElementArray(depth); 138430646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes if (java_traces == NULL) { 138530646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes return NULL; 138630646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes } 138701158d7a57c8321370667a6045220237d16e0da8Elliott Hughes result = AddLocalReference<jobjectArray>(ts.Env(), java_traces); 138801158d7a57c8321370667a6045220237d16e0da8Elliott Hughes } 138901158d7a57c8321370667a6045220237d16e0da8Elliott Hughes 139001158d7a57c8321370667a6045220237d16e0da8Elliott Hughes if (stack_depth != NULL) { 139101158d7a57c8321370667a6045220237d16e0da8Elliott Hughes *stack_depth = depth; 139201158d7a57c8321370667a6045220237d16e0da8Elliott Hughes } 139355df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao 13946d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers MethodHelper mh; 13959b576b4ed1dbe035952f3106d8f4b6993125ed6fShih-wei Liao for (int32_t i = 0; i < depth; ++i) { 1396aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Prepare parameters for StackTraceElement(String cls, String method, String file, int line) 1397aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers Method* method = down_cast<Method*>(method_trace->Get(i)); 13986d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers mh.ChangeMethod(method); 1399aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers uint32_t native_pc = pc_trace->Get(i); 140002f01fedc5180d1d5a795674f8c5bd0a3ce974d2Shih-wei Liao#if !defined(ART_USE_LLVM_COMPILER) 14016d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers int32_t line_number = mh.GetLineNumFromNativePC(native_pc); 140202f01fedc5180d1d5a795674f8c5bd0a3ce974d2Shih-wei Liao#else 140302f01fedc5180d1d5a795674f8c5bd0a3ce974d2Shih-wei Liao int32_t line_number = native_pc; // LLVM stored line_number in the ShadowFrame 140402f01fedc5180d1d5a795674f8c5bd0a3ce974d2Shih-wei Liao#endif 1405aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Allocate element, potentially triggering GC 140640381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom // TODO: reuse class_name_object via Class::name_? 1407486013193c94b4321414c28ce9251911e14a0599Ian Rogers const char* descriptor = mh.GetDeclaringClassDescriptor(); 1408486013193c94b4321414c28ce9251911e14a0599Ian Rogers CHECK(descriptor != NULL); 1409486013193c94b4321414c28ce9251911e14a0599Ian Rogers std::string class_name(PrettyDescriptor(descriptor)); 141040381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom SirtRef<String> class_name_object(String::AllocFromModifiedUtf8(class_name.c_str())); 141140381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom if (class_name_object.get() == NULL) { 141240381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom return NULL; 141340381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom } 1414486013193c94b4321414c28ce9251911e14a0599Ian Rogers const char* method_name = mh.GetName(); 1415486013193c94b4321414c28ce9251911e14a0599Ian Rogers CHECK(method_name != NULL); 1416486013193c94b4321414c28ce9251911e14a0599Ian Rogers SirtRef<String> method_name_object(String::AllocFromModifiedUtf8(method_name)); 14176d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers if (method_name_object.get() == NULL) { 14186d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers return NULL; 14196d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers } 1420486013193c94b4321414c28ce9251911e14a0599Ian Rogers const char* source_file = mh.GetDeclaringClassSourceFile(); 1421486013193c94b4321414c28ce9251911e14a0599Ian Rogers SirtRef<String> source_name_object(String::AllocFromModifiedUtf8(source_file)); 142240381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom StackTraceElement* obj = StackTraceElement::Alloc(class_name_object.get(), 14236d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers method_name_object.get(), 14246d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers source_name_object.get(), 142540381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom line_number); 142630646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes if (obj == NULL) { 142730646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes return NULL; 142830646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes } 1429aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers#ifdef MOVING_GARBAGE_COLLECTOR 1430aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers // Re-read after potential GC 1431aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers java_traces = Decode<ObjectArray<Object>*>(ts.Env(), result); 1432aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers method_trace = down_cast<ObjectArray<Object>*>(Decode<Object*>(ts.Env(), internal)); 1433aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers pc_trace = down_cast<IntArray*>(method_trace->Get(depth)); 1434aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers#endif 143555df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao java_traces->Set(i, obj); 143655df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao } 1437aaa208006d7c8cc0f381c4aa9b525be24066c495Ian Rogers return result; 143855df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao} 143955df06be4369f5d8ab5eb61a5d22809255171036Shih-wei Liao 14405cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughesvoid Thread::ThrowNewExceptionF(const char* exception_class_descriptor, const char* fmt, ...) { 1441a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes va_list args; 1442a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes va_start(args, fmt); 14434a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes ThrowNewExceptionV(exception_class_descriptor, fmt, args); 1444a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes va_end(args); 14454a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes} 14464a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes 14474a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughesvoid Thread::ThrowNewExceptionV(const char* exception_class_descriptor, const char* fmt, va_list ap) { 14484a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes std::string msg; 14494a2b41793d18d402286ae37e9de4fd392bc75a08Elliott Hughes StringAppendV(&msg, fmt, ap); 14505cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughes ThrowNewException(exception_class_descriptor, msg.c_str()); 14515cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughes} 145237f7a40f6789bb287f287a9af00777af9d6428eeElliott Hughes 14535cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughesvoid Thread::ThrowNewException(const char* exception_class_descriptor, const char* msg) { 1454e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes // Convert "Ljava/lang/Exception;" into JNI-style "java/lang/Exception". 14550cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers CHECK_EQ('L', exception_class_descriptor[0]); 1456e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes std::string descriptor(exception_class_descriptor + 1); 14570cfe1fb7060576d047f7f894fc0d8b87de84fcabIan Rogers CHECK_EQ(';', descriptor[descriptor.length() - 1]); 1458e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes descriptor.erase(descriptor.length() - 1); 1459e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes 1460e5b0dc83537bf915c6abe4efeae6e501daf75a27Elliott Hughes JNIEnv* env = GetJniEnv(); 1461726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes ScopedLocalRef<jclass> exception_class(env, env->FindClass(descriptor.c_str())); 146230646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes if (exception_class.get() == NULL) { 146330646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes LOG(ERROR) << "Couldn't throw new " << descriptor << " because JNI FindClass failed: " 146430646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes << PrettyTypeOf(GetException()); 146530646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes CHECK(IsExceptionPending()); 146630646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes return; 146730646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes } 1468ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom if (!Runtime::Current()->IsStarted()) { 1469ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom // Something is trying to throw an exception without a started 1470ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom // runtime, which is the common case in the compiler. We won't be 1471ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom // able to invoke the constructor of the exception, so use 1472ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom // AllocObject which will not invoke a constructor. 1473ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom ScopedLocalRef<jthrowable> exception( 1474ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom env, reinterpret_cast<jthrowable>(env->AllocObject(exception_class.get()))); 1475ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom if (exception.get() != NULL) { 1476ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom ScopedJniThreadState ts(env); 1477ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom Throwable* t = reinterpret_cast<Throwable*>(ts.Self()->DecodeJObject(exception.get())); 147802fbef0e356ca43b2032044f870d59de4a4ae92aIan Rogers t->SetDetailMessage(String::AllocFromModifiedUtf8(msg)); 1479ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom ts.Self()->SetException(t); 1480ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom } else { 1481ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom LOG(ERROR) << "Couldn't throw new " << descriptor << " because JNI AllocObject failed: " 1482ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom << PrettyTypeOf(GetException()); 1483ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom CHECK(IsExceptionPending()); 1484ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom } 1485ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom return; 1486ebd1fd233c8e14f50844f15109ccd50b27079722Brian Carlstrom } 1487726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes int rc = env->ThrowNew(exception_class.get(), msg); 148830646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes if (rc != JNI_OK) { 148930646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes LOG(ERROR) << "Couldn't throw new " << descriptor << " because JNI ThrowNew failed: " 149030646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes << PrettyTypeOf(GetException()); 149130646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes CHECK(IsExceptionPending()); 149230646836b4a1c6e5e80ddaea246cf9669eaa0628Elliott Hughes } 1493a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes} 1494a5b897eae4b6f9f9608faa9eada7ddf42bf1bfd2Elliott Hughes 14952ced6a534157d5d963693346904389c19775d2daElliott Hughesvoid Thread::ThrowOutOfMemoryError(const char* msg) { 14962ced6a534157d5d963693346904389c19775d2daElliott Hughes LOG(ERROR) << StringPrintf("Throwing OutOfMemoryError \"%s\"%s", 14972ced6a534157d5d963693346904389c19775d2daElliott Hughes msg, (throwing_OutOfMemoryError_ ? " (recursive case)" : "")); 1498726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes if (!throwing_OutOfMemoryError_) { 1499726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes throwing_OutOfMemoryError_ = true; 1500accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes ThrowNewException("Ljava/lang/OutOfMemoryError;", NULL); 1501418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughes } else { 1502726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes SetException(pre_allocated_OutOfMemoryError_); 1503418dfe7849f45535b5388a91bd7a16cfc20a612bElliott Hughes } 1504726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes throwing_OutOfMemoryError_ = false; 150579082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes} 150679082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes 1507accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 1508498508c1187dc07d3eae5476784cde20f5224d93Elliott HughesThread* Thread::CurrentFromGdb() { 1509accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes return Thread::Current(); 1510accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes} 1511accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 1512accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughesvoid Thread::DumpFromGdb() const { 15136b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom std::ostringstream ss; 15146b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom Dump(ss); 1515955724179c6c739524f610023287f56b24dc31deElliott Hughes std::string str(ss.str()); 15166b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom // log to stderr for debugging command line processes 15176b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom std::cerr << str; 15186b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom#ifdef HAVE_ANDROID_OS 15196b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom // log to logcat for debugging frameworks processes 15206b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom LOG(INFO) << str; 15216b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstrom#endif 1522accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes} 1523accd83d1523545ac69bafd38e72a7d5cff8e2facElliott Hughes 152428fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughesvoid Thread::DumpThreadOffset(std::ostream& os, uint32_t offset, size_t size_of_pointers) { 152528fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes CHECK_EQ(size_of_pointers, 4U); // TODO: support 64-bit targets. 15263d71d0799748aac23ce5935d61b909bec6e96461Elliott Hughes#define DO_THREAD_OFFSET(x) if (offset == static_cast<uint32_t>(OFFSETOF_VOLATILE_MEMBER(Thread, x))) { os << # x; } else 152728fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes#define DO_THREAD_ENTRY_POINT_OFFSET(x) if (offset == ENTRYPOINT_OFFSET(x)) { os << # x; } else 152828fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_OFFSET(card_table_) 152928fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_OFFSET(exception_) 153028fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_OFFSET(jni_env_) 153128fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_OFFSET(self_) 153228fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_OFFSET(stack_end_) 15333d71d0799748aac23ce5935d61b909bec6e96461Elliott Hughes DO_THREAD_OFFSET(state_) 153428fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_OFFSET(suspend_count_) 153528fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_OFFSET(thin_lock_id_) 153628fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_OFFSET(top_of_managed_stack_) 153728fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_OFFSET(top_of_managed_stack_pc_) 153828fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_OFFSET(top_sirt_) 153928fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pAllocArrayFromCode) 154028fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pAllocArrayFromCodeWithAccessCheck) 154128fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pAllocObjectFromCode) 154228fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pAllocObjectFromCodeWithAccessCheck) 154328fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pCheckAndAllocArrayFromCode) 154428fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pCheckAndAllocArrayFromCodeWithAccessCheck) 154528fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pInstanceofNonTrivialFromCode) 154628fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pCanPutArrayElementFromCode) 154728fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pCheckCastFromCode) 154828fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pDebugMe) 154928fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pUpdateDebuggerFromCode) 155028fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pInitializeStaticStorage) 155128fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode) 155228fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pInitializeTypeFromCode) 155328fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pResolveStringFromCode) 155428fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pSet32Instance) 155528fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pSet32Static) 155628fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pSet64Instance) 155728fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pSet64Static) 155828fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pSetObjInstance) 155928fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pSetObjStatic) 156028fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pGet32Instance) 156128fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pGet32Static) 156228fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pGet64Instance) 156328fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pGet64Static) 156428fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pGetObjInstance) 156528fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pGetObjStatic) 156628fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pHandleFillArrayDataFromCode) 156728fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pDecodeJObjectInThread) 156828fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pFindNativeMethod) 156928fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pLockObjectFromCode) 157028fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pUnlockObjectFromCode) 157128fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pCmpgDouble) 157228fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pCmpgFloat) 157328fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pCmplDouble) 157428fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pCmplFloat) 157528fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pDadd) 157628fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pDdiv) 157728fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pDmul) 157828fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pDsub) 157928fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pF2d) 158028fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pFmod) 158128fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pI2d) 158228fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pL2d) 158328fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pD2f) 158428fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pFadd) 158528fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pFdiv) 158628fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pFmodf) 158728fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pFmul) 158828fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pFsub) 158928fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pI2f) 159028fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pL2f) 159128fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pD2iz) 159228fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pF2iz) 159328fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pIdiv) 159428fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pIdivmod) 159528fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pD2l) 159628fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pF2l) 159728fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pLdiv) 159828fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pLdivmod) 159928fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pLmul) 160028fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pShlLong) 160128fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pShrLong) 160228fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pUshrLong) 160328fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pIndexOf) 160428fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pMemcmp16) 160528fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pStringCompareTo) 160628fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pMemcpy) 160728fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pFindInterfaceMethodInCache) 160828fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pUnresolvedDirectMethodTrampolineFromCode) 160928fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pInvokeDirectTrampolineWithAccessCheck) 161028fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pInvokeInterfaceTrampoline) 161128fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pInvokeInterfaceTrampolineWithAccessCheck) 161228fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pInvokeStaticTrampolineWithAccessCheck) 161328fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pInvokeSuperTrampolineWithAccessCheck) 161428fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pInvokeVirtualTrampolineWithAccessCheck) 161528fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pCheckSuspendFromCode) 161628fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pTestSuspendFromCode) 161728fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pDeliverException) 161828fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pThrowAbstractMethodErrorFromCode) 161928fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pThrowArrayBoundsFromCode) 162028fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pThrowDivZeroFromCode) 162128fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pThrowNoSuchMethodFromCode) 162228fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pThrowNullPointerFromCode) 162328fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pThrowStackOverflowFromCode) 162428fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes DO_THREAD_ENTRY_POINT_OFFSET(pThrowVerificationErrorFromCode) 162528fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes { 162628fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes os << offset; 162728fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes } 162828fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes#undef DO_THREAD_OFFSET 162928fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes#undef DO_THREAD_ENTRY_POINT_OFFSET 163028fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes} 163128fa76d17d741238da86dbdb47f721ae97c9eac8Elliott Hughes 1632bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogersclass CatchBlockStackVisitor : public Thread::StackVisitor { 1633bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers public: 1634bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers CatchBlockStackVisitor(Class* to_find, Context* ljc) 163557b86d47b66322693a070185fadfb43cb9c12eabIan Rogers : to_find_(to_find), long_jump_context_(ljc), native_method_count_(0), 163657b86d47b66322693a070185fadfb43cb9c12eabIan Rogers method_tracing_active_(Runtime::Current()->IsMethodTracingActive()) { 163767375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers#ifndef NDEBUG 163867375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers handler_pc_ = 0xEBADC0DE; 163967375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers handler_frame_.SetSP(reinterpret_cast<Method**>(0xEBADF00D)); 164067375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers#endif 164167375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers } 1642bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 1643530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes bool VisitFrame(const Frame& fr, uintptr_t pc) { 1644530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes Method* method = fr.GetMethod(); 1645530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes if (method == NULL) { 1646530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes // This is the upcall, we remember the frame and last_pc so that we may 1647530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes // long jump to them 1648530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes handler_pc_ = DemanglePc(pc); 1649530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes handler_frame_ = fr; 165057b86d47b66322693a070185fadfb43cb9c12eabIan Rogers return false; // End stack walk. 1651530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes } 1652530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes uint32_t dex_pc = DexFile::kDexNoIndex; 165357b86d47b66322693a070185fadfb43cb9c12eabIan Rogers if (method->IsRuntimeMethod()) { 1654530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes // ignore callee save method 165557b86d47b66322693a070185fadfb43cb9c12eabIan Rogers DCHECK(method->IsCalleeSaveMethod()); 1656530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes } else if (method->IsNative()) { 1657530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes native_method_count_++; 1658530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes } else { 16590791adc2249366c50684935a4c42ba5e58bc3746jeffhao // Unwind stack when an exception occurs during method tracing 166057b86d47b66322693a070185fadfb43cb9c12eabIan Rogers if (UNLIKELY(method_tracing_active_)) { 1661776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#if !defined(ART_USE_LLVM_COMPILER) 166257b86d47b66322693a070185fadfb43cb9c12eabIan Rogers if (IsTraceExitPc(DemanglePc(pc))) { 1663530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes pc = ManglePc(TraceMethodUnwindFromCode(Thread::Current())); 1664530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes } 1665776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#else 1666776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers UNIMPLEMENTED(FATAL); 1667776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#endif 1668bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers } 1669530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes dex_pc = method->ToDexPC(pc); 1670530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes } 1671530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes if (dex_pc != DexFile::kDexNoIndex) { 1672530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes uint32_t found_dex_pc = method->FindCatchBlock(to_find_, dex_pc); 1673530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes if (found_dex_pc != DexFile::kDexNoIndex) { 1674530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes handler_pc_ = method->ToNativePC(found_dex_pc); 1675530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes handler_frame_ = fr; 167657b86d47b66322693a070185fadfb43cb9c12eabIan Rogers return false; // End stack walk. 1677bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers } 16781a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao } 1679776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#if !defined(ART_USE_LLVM_COMPILER) 1680530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes // Caller may be handler, fill in callee saves in context 1681530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes long_jump_context_->FillCalleeSaves(fr); 1682776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#endif 168357b86d47b66322693a070185fadfb43cb9c12eabIan Rogers return true; // Continue stack walk. 16841a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao } 16851a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao 1686bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers // The type of the exception catch block to find 1687bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers Class* to_find_; 1688bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers // Frame with found handler or last frame if no handler found 1689bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers Frame handler_frame_; 169067375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers // PC to branch to for the handler 169167375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers uintptr_t handler_pc_; 1692bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers // Context that will be the target of the long jump 1693bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers Context* long_jump_context_; 169467375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers // Number of native methods passed in crawl (equates to number of SIRTs to pop) 169567375acd9fec74cc2054554fe1ed0a7d213e1e47Ian Rogers uint32_t native_method_count_; 169657b86d47b66322693a070185fadfb43cb9c12eabIan Rogers // Is method tracing active? 169757b86d47b66322693a070185fadfb43cb9c12eabIan Rogers const bool method_tracing_active_; 1698bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers}; 1699bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 1700ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogersvoid Thread::DeliverException() { 1701776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#if !defined(ART_USE_LLVM_COMPILER) 170228ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers const bool kDebugExceptionDelivery = false; 1703d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes Throwable* exception = GetException(); // Get exception from thread 1704ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogers CHECK(exception != NULL); 170528ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers // Don't leave exception visible while we try to find the handler, which may cause class 1706d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes // resolution. 170728ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers ClearException(); 170828ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers if (kDebugExceptionDelivery) { 1709a32a6fd4a781262dff4fec102da053d16b7ef6c0Ian Rogers String* msg = exception->GetDetailMessage(); 1710a32a6fd4a781262dff4fec102da053d16b7ef6c0Ian Rogers std::string str_msg(msg != NULL ? msg->ToModifiedUtf8() : ""); 1711a32a6fd4a781262dff4fec102da053d16b7ef6c0Ian Rogers DumpStack(LOG(INFO) << "Delivering exception: " << PrettyTypeOf(exception) 1712a32a6fd4a781262dff4fec102da053d16b7ef6c0Ian Rogers << ": " << str_msg << std::endl); 171328ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers } 1714bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 1715bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers Context* long_jump_context = GetLongJumpContext(); 1716bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers CatchBlockStackVisitor catch_finder(exception->GetClass(), long_jump_context); 1717530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes WalkStack(&catch_finder, true); 1718bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 1719d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes Method** sp; 1720d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes uintptr_t throw_native_pc; 1721d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes Method* throw_method = GetCurrentMethod(&throw_native_pc, &sp); 1722d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes uintptr_t catch_native_pc = catch_finder.handler_pc_; 1723d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes Method* catch_method = catch_finder.handler_frame_.GetMethod(); 1724d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes Dbg::PostException(sp, throw_method, throw_native_pc, catch_method, catch_native_pc, exception); 1725d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes 172628ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers if (kDebugExceptionDelivery) { 1727d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes if (catch_method == NULL) { 172828ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers LOG(INFO) << "Handler is upcall"; 172928ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers } else { 173028ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 173128ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers const DexFile& dex_file = 1732d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes class_linker->FindDexFile(catch_method->GetDeclaringClass()->GetDexCache()); 1733d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes int line_number = dex_file.GetLineNumFromPC(catch_method, 1734d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes catch_method->ToDexPC(catch_finder.handler_pc_)); 1735d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes LOG(INFO) << "Handler: " << PrettyMethod(catch_method) << " (line: " << line_number << ")"; 173628ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers } 173728ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers } 173828ad40dc3ec2f09b0ffd4f6d6787bf1b532ccd5dIan Rogers SetException(exception); 17399a8a8889418e09bf2d4ae3f683fbb40a71c3c8f4Ian Rogers CHECK_NE(catch_native_pc, 0u); 1740d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes long_jump_context->SetSP(reinterpret_cast<uintptr_t>(catch_finder.handler_frame_.GetSP())); 17419a8a8889418e09bf2d4ae3f683fbb40a71c3c8f4Ian Rogers long_jump_context->SetPC(catch_native_pc); 17429c750f9b6283f62b3e6a93c0c6b2838abde5000eElliott Hughes long_jump_context->SmashCallerSaves(); 1743bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers long_jump_context->DoLongJump(); 1744776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#endif 17459a8a8889418e09bf2d4ae3f683fbb40a71c3c8f4Ian Rogers LOG(FATAL) << "UNREACHABLE"; 1746bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers} 1747bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers 1748bdb0391258abc54bf77c676e36847d28a783bfe5Ian RogersContext* Thread::GetLongJumpContext() { 174985d1545e985ac689db4bad7849880e843707c862Elliott Hughes Context* result = long_jump_context_; 1750776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#if !defined(ART_USE_LLVM_COMPILER) 1751bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers if (result == NULL) { 1752bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers result = Context::Create(); 175385d1545e985ac689db4bad7849880e843707c862Elliott Hughes long_jump_context_ = result; 17541a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao } 1755776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#endif 1756bdb0391258abc54bf77c676e36847d28a783bfe5Ian Rogers return result; 17571a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao} 17581a18c8c1c0e4ea1ff06177e93c7ff703376dcee2Shih-wei Liao 1759ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYa#if !defined(ART_USE_LLVM_COMPILER) 1760d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott HughesMethod* Thread::GetCurrentMethod(uintptr_t* pc, Method*** sp) const { 1761d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes Frame f = top_of_managed_stack_; 1762d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes Method* m = f.GetMethod(); 17638be2d40d42223b515de12629216151a558895969Elliott Hughes uintptr_t native_pc = top_of_managed_stack_pc_; 17648be2d40d42223b515de12629216151a558895969Elliott Hughes 17659fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes // We use JNI internally for exception throwing, so it's possible to arrive 17669fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes // here via a "FromCode" function, in which case there's a synthetic 17679fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes // callee-save method at the top of the stack. These shouldn't be user-visible, 17689fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes // so if we find one, skip it and return the compiled method underneath. 176933dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao if (m != NULL && m->IsCalleeSaveMethod()) { 17708be2d40d42223b515de12629216151a558895969Elliott Hughes native_pc = f.GetReturnPC(); 17719fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes f.Next(); 17729fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes m = f.GetMethod(); 17739fd66f5bb6a514147a53d57b419d18ecc7937122Elliott Hughes } 1774d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes if (pc != NULL) { 17758be2d40d42223b515de12629216151a558895969Elliott Hughes *pc = (m != NULL) ? ManglePc(native_pc) : 0; 177633dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao } 1777d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes if (sp != NULL) { 1778d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes *sp = f.GetSP(); 1779d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes } 1780d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes return m; 178133dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao} 1782ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYa#else 1783ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYaMethod* Thread::GetCurrentMethod(uintptr_t*, Method***) const { 1784ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYa ShadowFrame* frame = top_shadow_frame_; 178520efe54063f2740d16f01190b280cdba384c869bTDYa if (frame == NULL) { 178620efe54063f2740d16f01190b280cdba384c869bTDYa return NULL; 1787ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYa } 1788ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYa return frame->GetMethod(); 1789ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYa} 1790ee0d3fb2fbd736484fe8c3177a4e965ea86d1c65TDYa#endif 179133dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao 17925f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesbool Thread::HoldsLock(Object* object) { 17935f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (object == NULL) { 17945f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return false; 17955f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 179624a3c2e9924e8765c4a9b4d383cb8f3b922f9c9fBrian Carlstrom return object->GetThinLockId() == thin_lock_id_; 17975f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 17985f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 1799038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughesbool Thread::IsDaemon() { 1800038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes return gThread_daemon->GetBoolean(peer_); 1801038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes} 1802038a806df72f884d22283a84a31c9a1d35ba1fdfElliott Hughes 1803776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#if !defined(ART_USE_LLVM_COMPILER) 1804d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogersclass ReferenceMapVisitor : public Thread::StackVisitor { 1805d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers public: 1806d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers ReferenceMapVisitor(Context* context, Heap::RootVisitor* root_visitor, void* arg) : 1807d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers context_(context), root_visitor_(root_visitor), arg_(arg) { 1808d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1809d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers 1810530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes bool VisitFrame(const Frame& frame, uintptr_t pc) { 1811d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers Method* m = frame.GetMethod(); 18126a4be3a6226cec645cf905dd352e44f7968a7fa4Brian Carlstrom if (false) { 1813250455229aa0cc07bbd18174efe510bd52631a99jeffhao LOG(INFO) << "Visiting stack roots in " << PrettyMethod(m) 18140dd7ddaa36a2cf37490dc166ebc21818364130a7Brian Carlstrom << StringPrintf("@ PC:%04x", m->ToDexPC(pc)); 18156a4be3a6226cec645cf905dd352e44f7968a7fa4Brian Carlstrom } 1816d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Process register map (which native and callee save methods don't have) 18173891c775503b1113a918180d9ecdaa79e92c69c6Ian Rogers if (!m->IsNative() && !m->IsCalleeSaveMethod() && !m->IsProxyMethod()) { 1818e7d856b911222aa000ca2be0f8f63f5b292141c3Brian Carlstrom CHECK(m->GetGcMap() != NULL) << PrettyMethod(m); 1819e7d856b911222aa000ca2be0f8f63f5b292141c3Brian Carlstrom CHECK_NE(0U, m->GetGcMapLength()) << PrettyMethod(m); 1820e7d856b911222aa000ca2be0f8f63f5b292141c3Brian Carlstrom verifier::PcToReferenceMap map(m->GetGcMap(), m->GetGcMapLength()); 1821d81871cbbaa34c649e488f94f61a981db33123e5Ian Rogers const uint8_t* reg_bitmap = map.FindBitMap(m->ToDexPC(pc)); 1822d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers CHECK(reg_bitmap != NULL); 182368fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes const VmapTable vmap_table(m->GetVmapTableRaw()); 182411d1b0c31ddd710d26068da8e0e4621002205b4bElliott Hughes const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem(); 18256d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers DCHECK(code_item != NULL); // can't be NULL or how would we compile its instructions? 18266d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers uint32_t core_spills = m->GetCoreSpillMask(); 18276d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers uint32_t fp_spills = m->GetFpSpillMask(); 18286d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers size_t frame_size = m->GetFrameSizeInBytes(); 18290dd7ddaa36a2cf37490dc166ebc21818364130a7Brian Carlstrom // For all dex registers in the bitmap 18306d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers size_t num_regs = std::min(map.RegWidth() * 8, 18316d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers static_cast<size_t>(code_item->registers_size_)); 18320dd7ddaa36a2cf37490dc166ebc21818364130a7Brian Carlstrom for (size_t reg = 0; reg < num_regs; ++reg) { 1833d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Does this register hold a reference? 1834d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers if (TestBitmap(reg, reg_bitmap)) { 183568fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes uint32_t vmap_offset; 1836d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers Object* ref; 183768fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes if (vmap_table.IsInContext(reg, vmap_offset)) { 1838d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Compute the register we need to load from the context 1839d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers uint32_t spill_mask = m->GetCoreSpillMask(); 18400dd7ddaa36a2cf37490dc166ebc21818364130a7Brian Carlstrom CHECK_LT(vmap_offset, static_cast<uint32_t>(__builtin_popcount(spill_mask))); 1841f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers uint32_t matches = 0; 1842f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers uint32_t spill_shifts = 0; 1843f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers while (matches != (vmap_offset + 1)) { 18440dd7ddaa36a2cf37490dc166ebc21818364130a7Brian Carlstrom DCHECK_NE(spill_mask, 0u); 1845f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers matches += spill_mask & 1; // Add 1 if the low bit is set 1846f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers spill_mask >>= 1; 1847f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers spill_shifts++; 1848d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1849f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers spill_shifts--; // wind back one as we want the last match 1850f57c47c29cd3388d1d78008a71c4e18f2217b70cIan Rogers ref = reinterpret_cast<Object*>(context_->GetGPR(spill_shifts)); 1851d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } else { 18526d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers ref = reinterpret_cast<Object*>(frame.GetVReg(code_item, core_spills, fp_spills, 18536d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers frame_size, reg)); 1854d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 18554f894e385e8ac018f078be75fea0a45696626b15Shih-wei Liao if (ref != NULL) { 18564f894e385e8ac018f078be75fea0a45696626b15Shih-wei Liao root_visitor_(ref, arg_); 18574f894e385e8ac018f078be75fea0a45696626b15Shih-wei Liao } 1858d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1859d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1860d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1861d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers context_->FillCalleeSaves(frame); 1862530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes return true; 1863d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1864d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers 1865d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers private: 1866d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers bool TestBitmap(int reg, const uint8_t* reg_vector) { 1867d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers return ((reg_vector[reg / 8] >> (reg % 8)) & 0x01) != 0; 1868d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers } 1869d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers 1870d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Context used to build up picture of callee saves 1871d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers Context* context_; 1872d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Call-back when we visit a root 1873d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers Heap::RootVisitor* root_visitor_; 1874d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Argument to call-back 1875d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers void* arg_; 1876d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers}; 1877776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#endif 1878d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers 1879d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogersvoid Thread::VisitRoots(Heap::RootVisitor* visitor, void* arg) { 1880d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (exception_ != NULL) { 1881d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes visitor(exception_, arg); 1882d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 1883d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes if (peer_ != NULL) { 1884d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes visitor(peer_, arg); 1885d369bb76dee0df2d2a106e9bf7f4e6446ed6deaaElliott Hughes } 1886726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes if (pre_allocated_OutOfMemoryError_ != NULL) { 1887726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes visitor(pre_allocated_OutOfMemoryError_, arg); 1888726079d3e2e50854cd6ca4c393f4529a796dba58Elliott Hughes } 188940381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom if (class_loader_override_ != NULL) { 189040381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom visitor(class_loader_override_, arg); 189140381fb9dc4b4cf274f1e58b2cdf4396202c6189Brian Carlstrom } 1892410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes jni_env_->locals.VisitRoots(visitor, arg); 1893410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes jni_env_->monitors.VisitRoots(visitor, arg); 18948dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao 18958dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao SirtVisitRoots(visitor, arg); 1896f7ad17e108b9357d7c94c6218a8521140a667f3dLogan Chien ShadowFrameVisitRoots(visitor, arg); 18978dfc9d551a9603eb8bc8463621b7bc73c3035169Shih-wei Liao 1898540a5b768bf5f76bd71ebf66612fb2298899480fTDYa#if !defined(ART_USE_LLVM_COMPILER) 1899d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Cheat and steal the long jump context. Assume that we are not doing a GC during exception 1900d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // delivery. 1901d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers Context* context = GetLongJumpContext(); 1902d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers // Visit roots on this thread's stack 1903d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers ReferenceMapVisitor mapper(context, visitor, arg); 1904d6b1f6190c8ec42facb08aca34b093244774b318Ian Rogers WalkStack(&mapper); 1905540a5b768bf5f76bd71ebf66612fb2298899480fTDYa#endif 1906410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes} 1907410c0c876f326e14c176a39ba21fc4dd3f7db8abElliott Hughes 1908250455229aa0cc07bbd18174efe510bd52631a99jeffhao#if VERIFY_OBJECT_ENABLED 1909ab7b9dcbfc4264a0bc4889c3e463ff88a67f6a30Elliott Hughesstatic void VerifyObject(const Object* obj, void*) { 1910250455229aa0cc07bbd18174efe510bd52631a99jeffhao Runtime::Current()->GetHeap()->VerifyObject(obj); 1911250455229aa0cc07bbd18174efe510bd52631a99jeffhao} 1912250455229aa0cc07bbd18174efe510bd52631a99jeffhao 1913250455229aa0cc07bbd18174efe510bd52631a99jeffhaovoid Thread::VerifyStack() { 1914540a5b768bf5f76bd71ebf66612fb2298899480fTDYa#if !defined(ART_USE_LLVM_COMPILER) 1915e66ac79b90a12b01b9d67ee9f7f586e638e67fabjeffhao UniquePtr<Context> context(Context::Create()); 1916e66ac79b90a12b01b9d67ee9f7f586e638e67fabjeffhao ReferenceMapVisitor mapper(context.get(), VerifyObject, NULL); 1917250455229aa0cc07bbd18174efe510bd52631a99jeffhao WalkStack(&mapper); 1918540a5b768bf5f76bd71ebf66612fb2298899480fTDYa#endif 1919250455229aa0cc07bbd18174efe510bd52631a99jeffhao} 1920250455229aa0cc07bbd18174efe510bd52631a99jeffhao#endif 1921250455229aa0cc07bbd18174efe510bd52631a99jeffhao 1922330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughesstd::ostream& operator<<(std::ostream& os, const Thread& thread) { 1923899e789bd4741c0172268f7838ce8ab220a5f916Elliott Hughes thread.Dump(os, false); 1924330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughes return os; 1925330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughes} 1926330304de14dc7118b45b8e7b5bd11a172fa61701Elliott Hughes 1927a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughesvoid Thread::CheckSafeToLockOrUnlock(MutexRank rank, bool is_locking) { 192817057b15cb8d8da97b2bc28fd38bdcc7a34e846eElliott Hughes if (this == NULL) { 192917057b15cb8d8da97b2bc28fd38bdcc7a34e846eElliott Hughes CHECK(Runtime::Current()->IsShuttingDown()); 193017057b15cb8d8da97b2bc28fd38bdcc7a34e846eElliott Hughes return; 193117057b15cb8d8da97b2bc28fd38bdcc7a34e846eElliott Hughes } 1932ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes if (is_locking) { 1933ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes if (held_mutexes_[rank] == 0) { 1934ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes bool bad_mutexes_held = false; 1935ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes for (int i = kMaxMutexRank; i > rank; --i) { 1936ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes if (held_mutexes_[i] != 0) { 1937ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes LOG(ERROR) << "holding " << static_cast<MutexRank>(i) << " while " << (is_locking ? "locking" : "unlocking") << " " << rank; 1938ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes bad_mutexes_held = true; 1939ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes } 1940ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes } 1941b5db3bb5b17f2bb42f3c62408938f4bf939f8a39Elliott Hughes CHECK(!bad_mutexes_held) << rank; 1942ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes } 1943ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes ++held_mutexes_[rank]; 1944ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes } else { 1945b5db3bb5b17f2bb42f3c62408938f4bf939f8a39Elliott Hughes CHECK_GT(held_mutexes_[rank], 0U) << rank; 1946ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes --held_mutexes_[rank]; 1947ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes } 1948ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes} 1949ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes 1950a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughesvoid Thread::CheckSafeToWait(MutexRank rank) { 195117057b15cb8d8da97b2bc28fd38bdcc7a34e846eElliott Hughes if (this == NULL) { 195217057b15cb8d8da97b2bc28fd38bdcc7a34e846eElliott Hughes CHECK(Runtime::Current()->IsShuttingDown()); 195317057b15cb8d8da97b2bc28fd38bdcc7a34e846eElliott Hughes return; 195417057b15cb8d8da97b2bc28fd38bdcc7a34e846eElliott Hughes } 1955a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes bool bad_mutexes_held = false; 1956a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes for (int i = kMaxMutexRank; i >= 0; --i) { 1957a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes if (i != rank && held_mutexes_[i] != 0) { 1958a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes LOG(ERROR) << "holding " << static_cast<MutexRank>(i) << " while doing condition variable wait on " << rank; 1959a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes bad_mutexes_held = true; 1960a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes } 1961a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes } 1962a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes if (held_mutexes_[rank] == 0) { 1963a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes LOG(ERROR) << "*not* holding " << rank << " while doing condition variable wait on it"; 1964a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes bad_mutexes_held = true; 1965a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes } 1966a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes CHECK(!bad_mutexes_held); 1967a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes} 1968a4060e5fe4729fa30bde965efc35779690478fa4Elliott Hughes 19698daa0929f08a3080ea64dbd4e997e72f411e6fc9Elliott Hughes} // namespace art 1970